d3dx8: Implement D3DX*Normalize.
[wine] / dlls / shell32 / tests / shellpath.c
1 /*
2  * Unit tests for shell32 SHGet{Special}Folder{Path|Location} functions.
3  *
4  * Copyright 2004 Juan Lang
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  * This is a test program for the SHGet{Special}Folder{Path|Location} functions
20  * of shell32, that get either a filesytem path or a LPITEMIDLIST (shell
21  * namespace) path for a given folder (CSIDL value).
22  *
23  * FIXME:
24  * - Need to verify on more systems.
25  */
26
27 #define COBJMACROS
28
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "shlguid.h"
34 #include "shlobj.h"
35 #include "shlwapi.h"
36 #include "wine/test.h"
37
38 /* CSIDL_MYDOCUMENTS is now the same as CSIDL_PERSONAL, but what we want
39  * here is its original value.
40  */
41 #define OLD_CSIDL_MYDOCUMENTS  0x000c
42
43 #ifndef ARRAY_SIZE
44 #define ARRAY_SIZE(x) ( sizeof(x) / sizeof((x)[0]) )
45 #endif
46
47 /* from pidl.h, not included here: */
48 #ifndef PT_GUID
49 #define PT_GUID       0x1f /* no path */
50 #endif
51 #ifndef PT_DRIVE 
52 #define PT_DRIVE      0x23 /* has path */
53 #endif
54 #ifndef PT_DRIVE2
55 #define PT_DRIVE2     0x25 /* has path */
56 #endif
57 #ifndef PT_SHELLEXT
58 #define PT_SHELLEXT   0x2e /* no path */
59 #endif
60 #ifndef PT_FOLDER
61 #define PT_FOLDER     0x31 /* has path */
62 #endif
63 #ifndef PT_WORKGRP
64 #define PT_WORKGRP    0x41 /* no path */
65 #endif
66 #ifndef PT_YAGUID
67 #define PT_YAGUID     0x70 /* no path */
68 #endif
69 /* FIXME: this is used for history/favorites folders; what's a better name? */
70 #ifndef PT_IESPECIAL2
71 #define PT_IESPECIAL2 0xb1 /* has path */
72 #endif
73
74 static GUID CLSID_CommonDocuments = { 0x0000000c, 0x0000, 0x0000, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x1a } };
75
76 struct shellExpectedValues {
77     int  folder;
78     BYTE pidlType;
79 };
80
81 static HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO *);
82 static HRESULT (WINAPI *pSHGetFolderPathA)(HWND, int, HANDLE, DWORD, LPSTR);
83 static HRESULT (WINAPI *pSHGetFolderLocation)(HWND, int, HANDLE, DWORD,
84  LPITEMIDLIST *);
85 static BOOL    (WINAPI *pSHGetSpecialFolderPathA)(HWND, LPSTR, int, BOOL);
86 static HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
87 static LPITEMIDLIST (WINAPI *pILFindLastID)(LPCITEMIDLIST);
88 static int (WINAPI *pSHFileOperationA)(LPSHFILEOPSTRUCTA);
89 static HRESULT (WINAPI *pSHGetMalloc)(LPMALLOC *);
90 static DLLVERSIONINFO shellVersion = { 0 };
91 static LPMALLOC pMalloc;
92 static const struct shellExpectedValues requiredShellValues[] = {
93  { CSIDL_BITBUCKET, PT_GUID },
94  { CSIDL_CONTROLS, PT_SHELLEXT },
95  { CSIDL_COOKIES, PT_FOLDER },
96  { CSIDL_DESKTOPDIRECTORY, PT_FOLDER },
97  { CSIDL_DRIVES, PT_GUID },
98  { CSIDL_FAVORITES, PT_FOLDER },
99  { CSIDL_FONTS, PT_FOLDER },
100 /* FIXME: the following fails in Wine, returns type PT_FOLDER
101  { CSIDL_HISTORY, PT_IESPECIAL2 },
102  */
103  { CSIDL_INTERNET, PT_GUID },
104  { CSIDL_NETHOOD, PT_FOLDER },
105  { CSIDL_NETWORK, PT_GUID },
106  { CSIDL_PRINTERS, PT_YAGUID },
107  { CSIDL_PRINTHOOD, PT_FOLDER },
108  { CSIDL_PROGRAMS, PT_FOLDER },
109  { CSIDL_RECENT, PT_FOLDER },
110  { CSIDL_SENDTO, PT_FOLDER },
111  { CSIDL_STARTMENU, PT_FOLDER },
112  { CSIDL_STARTUP, PT_FOLDER },
113  { CSIDL_TEMPLATES, PT_FOLDER },
114 };
115 static const struct shellExpectedValues optionalShellValues[] = {
116 /* FIXME: the following only semi-succeed; they return NULL PIDLs on XP.. hmm.
117  { CSIDL_ALTSTARTUP, PT_FOLDER },
118  { CSIDL_COMMON_ALTSTARTUP, PT_FOLDER },
119  { CSIDL_COMMON_OEM_LINKS, PT_FOLDER },
120  */
121 /* Windows NT-only: */
122  { CSIDL_COMMON_DESKTOPDIRECTORY, PT_FOLDER },
123  { CSIDL_COMMON_DOCUMENTS, PT_SHELLEXT },
124  { CSIDL_COMMON_FAVORITES, PT_FOLDER },
125  { CSIDL_COMMON_PROGRAMS, PT_FOLDER },
126  { CSIDL_COMMON_STARTMENU, PT_FOLDER },
127  { CSIDL_COMMON_STARTUP, PT_FOLDER },
128  { CSIDL_COMMON_TEMPLATES, PT_FOLDER },
129 /* first appearing in shell32 version 4.71: */
130  { CSIDL_APPDATA, PT_FOLDER },
131 /* first appearing in shell32 version 4.72: */
132  { CSIDL_INTERNET_CACHE, PT_IESPECIAL2 },
133 /* first appearing in shell32 version 5.0: */
134  { CSIDL_ADMINTOOLS, PT_FOLDER },
135  { CSIDL_COMMON_APPDATA, PT_FOLDER },
136  { CSIDL_LOCAL_APPDATA, PT_FOLDER },
137  { OLD_CSIDL_MYDOCUMENTS, PT_FOLDER },
138  { CSIDL_MYMUSIC, PT_FOLDER },
139  { CSIDL_MYPICTURES, PT_FOLDER },
140  { CSIDL_MYVIDEO, PT_FOLDER },
141  { CSIDL_PROFILE, PT_FOLDER },
142  { CSIDL_PROGRAM_FILES, PT_FOLDER },
143  { CSIDL_PROGRAM_FILESX86, PT_FOLDER },
144  { CSIDL_PROGRAM_FILES_COMMON, PT_FOLDER },
145  { CSIDL_PROGRAM_FILES_COMMONX86, PT_FOLDER },
146  { CSIDL_SYSTEM, PT_FOLDER },
147  { CSIDL_WINDOWS, PT_FOLDER },
148 /* first appearing in shell32 6.0: */
149  { CSIDL_CDBURN_AREA, PT_FOLDER },
150  { CSIDL_COMMON_MUSIC, PT_FOLDER },
151  { CSIDL_COMMON_PICTURES, PT_FOLDER },
152  { CSIDL_COMMON_VIDEO, PT_FOLDER },
153  { CSIDL_COMPUTERSNEARME, PT_WORKGRP },
154  { CSIDL_RESOURCES, PT_FOLDER },
155  { CSIDL_RESOURCES_LOCALIZED, PT_FOLDER },
156 };
157
158 static void loadShell32(void)
159 {
160     HMODULE hShell32 = GetModuleHandleA("shell32");
161
162 #define GET_PROC(func) \
163     p ## func = (void*)GetProcAddress(hShell32, #func); \
164     if(!p ## func) \
165       trace("GetProcAddress(%s) failed\n", #func);
166
167     GET_PROC(DllGetVersion)
168     GET_PROC(SHGetFolderPathA)
169     GET_PROC(SHGetFolderLocation)
170     GET_PROC(SHGetSpecialFolderPathA)
171     GET_PROC(SHGetSpecialFolderLocation)
172     GET_PROC(ILFindLastID)
173     if (!pILFindLastID)
174         pILFindLastID = (void *)GetProcAddress(hShell32, (LPCSTR)16);
175     GET_PROC(SHFileOperationA)
176     GET_PROC(SHGetMalloc)
177
178     ok(pSHGetMalloc != NULL, "shell32 is missing SHGetMalloc\n");
179     if (pSHGetMalloc)
180     {
181         HRESULT hr = pSHGetMalloc(&pMalloc);
182
183         ok(SUCCEEDED(hr), "SHGetMalloc failed: 0x%08x\n", hr);
184         ok(pMalloc != NULL, "SHGetMalloc returned a NULL IMalloc\n");
185     }
186
187     if (pDllGetVersion)
188     {
189         shellVersion.cbSize = sizeof(shellVersion);
190         pDllGetVersion(&shellVersion);
191         if (winetest_interactive)
192             printf("shell32 version is %d.%d\n",
193              shellVersion.dwMajorVersion, shellVersion.dwMinorVersion);
194     }
195 #undef GET_PROC
196 }
197
198 #ifndef CSIDL_PROFILES
199 #define CSIDL_PROFILES          0x003e
200 #endif
201
202 /* A couple utility printing functions */
203 static const char *getFolderName(int folder)
204 {
205     static char unknown[32];
206
207 #define CSIDL_TO_STR(x) case x: return#x;
208     switch (folder)
209     {
210     CSIDL_TO_STR(CSIDL_DESKTOP);
211     CSIDL_TO_STR(CSIDL_INTERNET);
212     CSIDL_TO_STR(CSIDL_PROGRAMS);
213     CSIDL_TO_STR(CSIDL_CONTROLS);
214     CSIDL_TO_STR(CSIDL_PRINTERS);
215     CSIDL_TO_STR(CSIDL_PERSONAL);
216     CSIDL_TO_STR(CSIDL_FAVORITES);
217     CSIDL_TO_STR(CSIDL_STARTUP);
218     CSIDL_TO_STR(CSIDL_RECENT);
219     CSIDL_TO_STR(CSIDL_SENDTO);
220     CSIDL_TO_STR(CSIDL_BITBUCKET);
221     CSIDL_TO_STR(CSIDL_STARTMENU);
222     CSIDL_TO_STR(OLD_CSIDL_MYDOCUMENTS);
223     CSIDL_TO_STR(CSIDL_MYMUSIC);
224     CSIDL_TO_STR(CSIDL_MYVIDEO);
225     CSIDL_TO_STR(CSIDL_DESKTOPDIRECTORY);
226     CSIDL_TO_STR(CSIDL_DRIVES);
227     CSIDL_TO_STR(CSIDL_NETWORK);
228     CSIDL_TO_STR(CSIDL_NETHOOD);
229     CSIDL_TO_STR(CSIDL_FONTS);
230     CSIDL_TO_STR(CSIDL_TEMPLATES);
231     CSIDL_TO_STR(CSIDL_COMMON_STARTMENU);
232     CSIDL_TO_STR(CSIDL_COMMON_PROGRAMS);
233     CSIDL_TO_STR(CSIDL_COMMON_STARTUP);
234     CSIDL_TO_STR(CSIDL_COMMON_DESKTOPDIRECTORY);
235     CSIDL_TO_STR(CSIDL_APPDATA);
236     CSIDL_TO_STR(CSIDL_PRINTHOOD);
237     CSIDL_TO_STR(CSIDL_LOCAL_APPDATA);
238     CSIDL_TO_STR(CSIDL_ALTSTARTUP);
239     CSIDL_TO_STR(CSIDL_COMMON_ALTSTARTUP);
240     CSIDL_TO_STR(CSIDL_COMMON_FAVORITES);
241     CSIDL_TO_STR(CSIDL_INTERNET_CACHE);
242     CSIDL_TO_STR(CSIDL_COOKIES);
243     CSIDL_TO_STR(CSIDL_HISTORY);
244     CSIDL_TO_STR(CSIDL_COMMON_APPDATA);
245     CSIDL_TO_STR(CSIDL_WINDOWS);
246     CSIDL_TO_STR(CSIDL_SYSTEM);
247     CSIDL_TO_STR(CSIDL_PROGRAM_FILES);
248     CSIDL_TO_STR(CSIDL_MYPICTURES);
249     CSIDL_TO_STR(CSIDL_PROFILE);
250     CSIDL_TO_STR(CSIDL_SYSTEMX86);
251     CSIDL_TO_STR(CSIDL_PROGRAM_FILESX86);
252     CSIDL_TO_STR(CSIDL_PROGRAM_FILES_COMMON);
253     CSIDL_TO_STR(CSIDL_PROGRAM_FILES_COMMONX86);
254     CSIDL_TO_STR(CSIDL_COMMON_TEMPLATES);
255     CSIDL_TO_STR(CSIDL_COMMON_DOCUMENTS);
256     CSIDL_TO_STR(CSIDL_COMMON_ADMINTOOLS);
257     CSIDL_TO_STR(CSIDL_ADMINTOOLS);
258     CSIDL_TO_STR(CSIDL_CONNECTIONS);
259     CSIDL_TO_STR(CSIDL_PROFILES);
260     CSIDL_TO_STR(CSIDL_COMMON_MUSIC);
261     CSIDL_TO_STR(CSIDL_COMMON_PICTURES);
262     CSIDL_TO_STR(CSIDL_COMMON_VIDEO);
263     CSIDL_TO_STR(CSIDL_RESOURCES);
264     CSIDL_TO_STR(CSIDL_RESOURCES_LOCALIZED);
265     CSIDL_TO_STR(CSIDL_COMMON_OEM_LINKS);
266     CSIDL_TO_STR(CSIDL_CDBURN_AREA);
267     CSIDL_TO_STR(CSIDL_COMPUTERSNEARME);
268 #undef CSIDL_TO_STR
269     default:
270         sprintf(unknown, "unknown (0x%04x)", folder);
271         return unknown;
272     }
273 }
274
275 static const char *printGUID(const GUID *guid)
276 {
277     static char guidSTR[39];
278
279     if (!guid) return NULL;
280
281     sprintf(guidSTR, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
282      guid->Data1, guid->Data2, guid->Data3,
283      guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
284      guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
285     return guidSTR;
286 }
287
288 static void testSHGetFolderLocationInvalidArgs(void)
289 {
290     LPITEMIDLIST pidl;
291     HRESULT hr;
292
293     if (!pSHGetFolderLocation) return;
294
295     /* check a bogus CSIDL: */
296     pidl = NULL;
297     hr = pSHGetFolderLocation(NULL, 0xeeee, NULL, 0, &pidl);
298     ok(hr == E_INVALIDARG,
299      "SHGetFolderLocation(NULL, 0xeeee, NULL, 0, &pidl)\n"
300      "returned 0x%08x, expected E_INVALIDARG\n", hr);
301     if (SUCCEEDED(hr))
302         IMalloc_Free(pMalloc, pidl);
303     /* check a bogus user token: */
304     pidl = NULL;
305     hr = pSHGetFolderLocation(NULL, CSIDL_FAVORITES, (HANDLE)2, 0, &pidl);
306     ok(hr == E_FAIL,
307      "SHGetFolderLocation(NULL, CSIDL_FAVORITES, 2, 0, &pidl)\n"
308      "returned 0x%08x, expected E_FAIL\n", hr);
309     if (SUCCEEDED(hr))
310         IMalloc_Free(pMalloc, pidl);
311     /* check reserved is not zero: */
312     pidl = NULL;
313     hr = pSHGetFolderLocation(NULL, CSIDL_DESKTOP, NULL, 1, &pidl);
314     ok(hr == E_INVALIDARG,
315      "SHGetFolderLocation(NULL, CSIDL_DESKTOP, NULL, 1, &pidl)\n"
316      "returned 0x%08x, expected E_INVALIDARG\n", hr);
317     if (SUCCEEDED(hr))
318         IMalloc_Free(pMalloc, pidl);
319     /* a NULL pidl pointer crashes, so don't test it */
320 }
321
322 static void testSHGetSpecialFolderLocationInvalidArgs(void)
323 {
324     LPITEMIDLIST pidl = NULL;
325     HRESULT hr;
326
327     if (!pSHGetSpecialFolderLocation) return;
328
329     /* SHGetSpecialFolderLocation(NULL, 0, NULL) crashes */
330     hr = pSHGetSpecialFolderLocation(NULL, 0xeeee, &pidl);
331     ok(hr == E_INVALIDARG,
332      "SHGetSpecialFolderLocation(NULL, 0xeeee, &pidl) returned 0x%08x, "
333      "expected E_INVALIDARG\n", hr);
334 }
335
336 static void testSHGetFolderPathInvalidArgs(void)
337 {
338     char path[MAX_PATH];
339     HRESULT hr;
340
341     if (!pSHGetFolderPathA) return;
342
343     /* expect 2's a bogus handle, especially since we didn't open it */
344     hr = pSHGetFolderPathA(NULL, CSIDL_DESKTOP, (HANDLE)2,
345      SHGFP_TYPE_DEFAULT, path);
346     ok(hr == E_FAIL,
347      "SHGetFolderPathA(NULL, CSIDL_DESKTOP, 2, SHGFP_TYPE_DEFAULT, path)\n"
348      "returned 0x%08x, expected E_FAIL\n", hr);
349     hr = pSHGetFolderPathA(NULL, 0xeeee, NULL, SHGFP_TYPE_DEFAULT, path);
350     ok(hr == E_INVALIDARG,
351      "SHGetFolderPathA(NULL, 0xeeee, NULL, SHGFP_TYPE_DEFAULT, path)\n"
352      "returned 0x%08x, expected E_INVALIDARG\n", hr);
353 }
354
355 static void testSHGetSpecialFolderPathInvalidArgs(void)
356 {
357     char path[MAX_PATH];
358     BOOL ret;
359
360     if (!pSHGetSpecialFolderPathA) return;
361
362 #if 0
363     ret = pSHGetSpecialFolderPathA(NULL, NULL, CSIDL_BITBUCKET, FALSE);
364     ok(!ret,
365      "SHGetSpecialFolderPathA(NULL, NULL, CSIDL_BITBUCKET, FALSE)\n"
366      "returned TRUE, expected FALSE\n");
367 #endif
368     /* odd but true: calling with a NULL path still succeeds if it's a real
369      * dir (on some windows platform).  on winME it generates exception.
370      */
371     ret = pSHGetSpecialFolderPathA(NULL, path, CSIDL_PROGRAMS, FALSE);
372     ok(ret,
373      "SHGetSpecialFolderPathA(NULL, path, CSIDL_PROGRAMS, FALSE)\n"
374      "returned FALSE, expected TRUE\n");
375     ret = pSHGetSpecialFolderPathA(NULL, path, 0xeeee, FALSE);
376     ok(!ret,
377      "SHGetSpecialFolderPathA(NULL, path, 0xeeee, FALSE)\n"
378      "returned TRUE, expected FALSE\n");
379 }
380
381 static void testApiParameters(void)
382 {
383     testSHGetFolderLocationInvalidArgs();
384     testSHGetSpecialFolderLocationInvalidArgs();
385     testSHGetFolderPathInvalidArgs();
386     testSHGetSpecialFolderPathInvalidArgs();
387 }
388
389 /* Returns the folder's PIDL type, or 0xff if one can't be found. */
390 static BYTE testSHGetFolderLocation(BOOL optional, int folder)
391 {
392     LPITEMIDLIST pidl;
393     HRESULT hr;
394     BYTE ret = 0xff;
395
396     /* treat absence of function as success */
397     if (!pSHGetFolderLocation) return TRUE;
398
399     pidl = NULL;
400     hr = pSHGetFolderLocation(NULL, folder, NULL, 0, &pidl);
401     ok(SUCCEEDED(hr) || optional,
402      "SHGetFolderLocation(NULL, %s, NULL, 0, &pidl)\n"
403      "failed: 0x%08x\n", getFolderName(folder), hr);
404     if (SUCCEEDED(hr))
405     {
406         ok(pidl != NULL,
407          "SHGetFolderLocation(NULL, %s, NULL, 0, &pidl)\n"
408          "succeeded, but returned pidl is NULL\n", getFolderName(folder));
409         if (pidl)
410         {
411             LPITEMIDLIST pidlLast = pILFindLastID(pidl);
412
413             ok(pidlLast != NULL, "%s: ILFindLastID failed\n",
414              getFolderName(folder));
415             if (pidlLast)
416                 ret = pidlLast->mkid.abID[0];
417             IMalloc_Free(pMalloc, pidl);
418         }
419     }
420     return ret;
421 }
422
423 /* Returns the folder's PIDL type, or 0xff if one can't be found. */
424 static BYTE testSHGetSpecialFolderLocation(BOOL optional, int folder)
425 {
426     LPITEMIDLIST pidl;
427     HRESULT hr;
428     BYTE ret = 0xff;
429
430     /* treat absence of function as success */
431     if (!pSHGetSpecialFolderLocation) return TRUE;
432
433     pidl = NULL;
434     hr = pSHGetSpecialFolderLocation(NULL, folder, &pidl);
435     ok(SUCCEEDED(hr) || optional,
436      "SHGetSpecialFolderLocation(NULL, %s, &pidl)\n"
437      "failed: 0x%08x\n", getFolderName(folder), hr);
438     if (SUCCEEDED(hr))
439     {
440         ok(pidl != NULL,
441          "SHGetSpecialFolderLocation(NULL, %s, &pidl)\n"
442          "succeeded, but returned pidl is NULL\n", getFolderName(folder));
443         if (pidl)
444         {
445             LPITEMIDLIST pidlLast = pILFindLastID(pidl);
446
447             ok(pidlLast != NULL,
448                 "%s: ILFindLastID failed\n", getFolderName(folder));
449             if (pidlLast)
450                 ret = pidlLast->mkid.abID[0];
451             IMalloc_Free(pMalloc, pidl);
452         }
453     }
454     return ret;
455 }
456
457 static void testSHGetFolderPath(BOOL optional, int folder)
458 {
459     char path[MAX_PATH];
460     HRESULT hr;
461
462     if (!pSHGetFolderPathA) return;
463
464     hr = pSHGetFolderPathA(NULL, folder, NULL, SHGFP_TYPE_CURRENT, path);
465     ok(SUCCEEDED(hr) || optional,
466      "SHGetFolderPathA(NULL, %s, NULL, SHGFP_TYPE_CURRENT, path)\n"
467      "failed: 0x%08x\n", getFolderName(folder), hr);
468 }
469
470 static void testSHGetSpecialFolderPath(BOOL optional, int folder)
471 {
472     char path[MAX_PATH];
473     BOOL ret;
474
475     if (!pSHGetSpecialFolderPathA) return;
476
477     ret = pSHGetSpecialFolderPathA(NULL, path, folder, FALSE);
478     if (ret && winetest_interactive)
479         printf("%s: %s\n", getFolderName(folder), path);
480     ok(ret || optional,
481      "SHGetSpecialFolderPathA(NULL, path, %s, FALSE) failed\n",
482      getFolderName(folder));
483 }
484
485 static void testShellValues(const struct shellExpectedValues testEntries[],
486  int numEntries, BOOL optional)
487 {
488     int i;
489
490     for (i = 0; i < numEntries; i++)
491     {
492         BYTE type;
493
494         type = testSHGetFolderLocation(optional, testEntries[i].folder);
495         ok(type == testEntries[i].pidlType || optional,
496          "%s has type %d (0x%02x), expected %d (0x%02x)\n",
497          getFolderName(testEntries[i].folder), type, type,
498          testEntries[i].pidlType, testEntries[i].pidlType);
499         type = testSHGetSpecialFolderLocation(optional, testEntries[i].folder);
500         ok(type == testEntries[i].pidlType || optional,
501          "%s has type %d (0x%02x), expected %d (0x%02x)\n",
502          getFolderName(testEntries[i].folder), type, type,
503          testEntries[i].pidlType, testEntries[i].pidlType);
504         switch (type)
505         {
506             case PT_FOLDER:
507             case PT_DRIVE:
508             case PT_DRIVE2:
509             case PT_IESPECIAL2:
510                 testSHGetFolderPath(optional, testEntries[i].folder);
511                 testSHGetSpecialFolderPath(optional, testEntries[i].folder);
512                 break;
513         }
514     }
515 }
516
517 /* Attempts to verify that the folder path corresponding to the folder CSIDL
518  * value has the same value as the environment variable with name envVar.
519  * Doesn't mind if SHGetSpecialFolderPath fails for folder or if envVar isn't
520  * set in this environment; different OS and shell version behave differently.
521  * However, if both are present, fails if envVar's value is not the same
522  * (byte-for-byte) as what SHGetSpecialFolderPath returns.
523  */
524 static void matchSpecialFolderPathToEnv(int folder, const char *envVar)
525 {
526     char path[MAX_PATH];
527
528     if (!pSHGetSpecialFolderPathA) return;
529
530     if (pSHGetSpecialFolderPathA(NULL, path, folder, FALSE))
531     {
532         char *envVal = getenv(envVar);
533
534         ok(!envVal || !lstrcmpiA(envVal, path),
535          "%%%s%% does not match SHGetSpecialFolderPath:\n"
536          "%%%s%% is %s\nSHGetSpecialFolderPath returns %s\n",
537          envVar, envVar, envVal, path);
538     }
539 }
540
541 /* Attempts to match the GUID returned by SHGetFolderLocation for folder with
542  * GUID.  Assumes the type of the returned PIDL is in fact a GUID, but doesn't
543  * fail if it isn't--that check should already have been done.
544  * Fails if the returned PIDL is a GUID whose value does not match guid.
545  */
546 static void matchGUID(int folder, const GUID *guid)
547 {
548     LPITEMIDLIST pidl;
549     HRESULT hr;
550
551     if (!pSHGetFolderLocation) return;
552     if (!guid) return;
553
554     pidl = NULL;
555     hr = pSHGetFolderLocation(NULL, folder, NULL, 0, &pidl);
556     if (SUCCEEDED(hr))
557     {
558         LPITEMIDLIST pidlLast = pILFindLastID(pidl);
559
560         if (pidlLast && (pidlLast->mkid.abID[0] == PT_SHELLEXT ||
561          pidlLast->mkid.abID[0] == PT_GUID))
562         {
563             GUID *shellGuid = (GUID *)(pidlLast->mkid.abID + 2);
564
565             ok(IsEqualIID(shellGuid, guid),
566              "%s: got GUID %s, expected %s\n", getFolderName(folder),
567              printGUID(shellGuid), printGUID(guid));
568         }
569         IMalloc_Free(pMalloc, pidl);
570     }
571 }
572
573 static void testDesktop(void)
574 {
575     testSHGetFolderPath(FALSE, CSIDL_DESKTOP);
576     testSHGetSpecialFolderPath(FALSE, CSIDL_DESKTOP);
577     /* Test the desktop; even though SHITEMID should always contain abID of at
578      * least one type, when cb is 0 its value is undefined.  So don't check
579      * what the returned type is, just make sure it exists.
580      */
581     testSHGetFolderLocation(FALSE, CSIDL_DESKTOP);
582     testSHGetSpecialFolderLocation(FALSE, CSIDL_DESKTOP);
583 }
584
585 static void testPersonal(void)
586 {
587     BYTE type;
588
589     /* The pidl may be a real folder, or a virtual directory, or a drive if the
590      * home directory is set to the root directory of a drive.
591      */
592     type = testSHGetFolderLocation(FALSE, CSIDL_PERSONAL);
593     ok(type == PT_FOLDER || type == PT_GUID || type == PT_DRIVE,
594      "CSIDL_PERSONAL returned invalid type 0x%02x, "
595      "expected PT_FOLDER or PT_GUID\n", type);
596     if (type == PT_FOLDER)
597         testSHGetFolderPath(FALSE, CSIDL_PERSONAL);
598     type = testSHGetSpecialFolderLocation(FALSE, CSIDL_PERSONAL);
599     ok(type == PT_FOLDER || type == PT_GUID || type == PT_DRIVE,
600      "CSIDL_PERSONAL returned invalid type 0x%02x, "
601      "expected PT_FOLDER or PT_GUID\n", type);
602     if (type == PT_FOLDER)
603         testSHGetSpecialFolderPath(FALSE, CSIDL_PERSONAL);
604 }
605
606 /* Checks the PIDL type of all the known values. */
607 static void testPidlTypes(void)
608 {
609     testDesktop();
610     testPersonal();
611     testShellValues(requiredShellValues, ARRAY_SIZE(requiredShellValues),
612      FALSE);
613     testShellValues(optionalShellValues, ARRAY_SIZE(optionalShellValues),
614      TRUE);
615 }
616
617 /* Verifies various shell virtual folders have the correct well-known GUIDs. */
618 static void testGUIDs(void)
619 {
620     matchGUID(CSIDL_BITBUCKET, &CLSID_RecycleBin);
621     matchGUID(CSIDL_CONTROLS, &CLSID_ControlPanel);
622     matchGUID(CSIDL_DRIVES, &CLSID_MyComputer);
623     matchGUID(CSIDL_INTERNET, &CLSID_Internet);
624     matchGUID(CSIDL_NETWORK, &CLSID_NetworkPlaces);
625     matchGUID(CSIDL_PERSONAL, &CLSID_MyDocuments);
626     matchGUID(CSIDL_COMMON_DOCUMENTS, &CLSID_CommonDocuments);
627 }
628
629 /* Verifies various shell paths match the environment variables to which they
630  * correspond.
631  */
632 static void testEnvVars(void)
633 {
634     matchSpecialFolderPathToEnv(CSIDL_PROGRAM_FILES, "ProgramFiles");
635     matchSpecialFolderPathToEnv(CSIDL_APPDATA, "APPDATA");
636     matchSpecialFolderPathToEnv(CSIDL_PROFILE, "USERPROFILE");
637     matchSpecialFolderPathToEnv(CSIDL_WINDOWS, "SystemRoot");
638     matchSpecialFolderPathToEnv(CSIDL_WINDOWS, "windir");
639     matchSpecialFolderPathToEnv(CSIDL_PROGRAM_FILES_COMMON,
640      "CommonProgramFiles");
641     /* this is only set on Wine, but can't hurt to verify it: */
642     matchSpecialFolderPathToEnv(CSIDL_SYSTEM, "winsysdir");
643 }
644
645 /* Verifies the shell path for CSIDL_WINDOWS matches the return from
646  * GetWindowsDirectory.  If SHGetSpecialFolderPath fails, no harm, no foul--not
647  * every shell32 version supports CSIDL_WINDOWS.
648  */
649 static void testWinDir(void)
650 {
651     char windowsShellPath[MAX_PATH], windowsDir[MAX_PATH] = { 0 };
652
653     if (!pSHGetSpecialFolderPathA) return;
654
655     if (pSHGetSpecialFolderPathA(NULL, windowsShellPath, CSIDL_WINDOWS, FALSE))
656     {
657         PathRemoveBackslashA(windowsShellPath);
658         GetWindowsDirectoryA(windowsDir, sizeof(windowsDir));
659         PathRemoveBackslashA(windowsDir);
660         ok(!lstrcmpiA(windowsDir, windowsShellPath),
661          "GetWindowsDirectory does not match SHGetSpecialFolderPath:\n"
662          "GetWindowsDirectory returns %s\nSHGetSpecialFolderPath returns %s\n",
663          windowsDir, windowsShellPath);
664     }
665 }
666
667 /* Verifies the shell path for CSIDL_SYSTEM and CSIDL_SYSTEMX86 matches the
668  * return from GetSystemDirectory.  If SHGetSpecialFolderPath fails, no harm,
669  * no foul--not every shell32 version supports CSIDL_SYSTEM.
670  */
671 static void testSystemDir(void)
672 {
673     char systemShellPath[MAX_PATH], systemDir[MAX_PATH] = { 0 };
674
675     if (!pSHGetSpecialFolderPathA) return;
676
677     GetSystemDirectoryA(systemDir, sizeof(systemDir));
678     PathRemoveBackslashA(systemDir);
679     if (pSHGetSpecialFolderPathA(NULL, systemShellPath, CSIDL_SYSTEM, FALSE))
680     {
681         PathRemoveBackslashA(systemShellPath);
682         ok(!lstrcmpiA(systemDir, systemShellPath),
683          "GetSystemDirectory does not match SHGetSpecialFolderPath:\n"
684          "GetSystemDirectory returns %s\nSHGetSpecialFolderPath returns %s\n",
685          systemDir, systemShellPath);
686     }
687     /* check CSIDL_SYSTEMX86; note that this isn't always present, so don't
688      * worry if it fails
689      */
690     if (pSHGetSpecialFolderPathA(NULL, systemShellPath, CSIDL_SYSTEMX86, FALSE))
691     {
692         PathRemoveBackslashA(systemShellPath);
693         ok(!lstrcmpiA(systemDir, systemShellPath),
694          "GetSystemDirectory does not match SHGetSpecialFolderPath:\n"
695          "GetSystemDirectory returns %s\nSHGetSpecialFolderPath returns %s\n",
696          systemDir, systemShellPath);
697     }
698 }
699
700 /* Globals used by subprocesses */
701 static int    myARGC;
702 static char **myARGV;
703 static char   base[MAX_PATH];
704 static char   selfname[MAX_PATH];
705
706 static int init(void)
707 {
708     myARGC = winetest_get_mainargs(&myARGV);
709     if (!GetCurrentDirectoryA(sizeof(base), base)) return 0;
710     strcpy(selfname, myARGV[0]);
711     return 1;
712 }
713
714 /* Subprocess helper 1: test what happens when CSIDL_FAVORITES is set to a
715  * nonexistent directory.
716  */
717 static void testNonExistentPath1(void)
718 {
719     HRESULT hr;
720     LPITEMIDLIST pidl;
721     char path[MAX_PATH];
722
723     /* test some failure cases first: */
724     hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES, NULL,
725      SHGFP_TYPE_CURRENT, path);
726     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
727      "SHGetFolderPath returned 0x%08x, expected 0x80070002\n", hr);
728     pidl = NULL;
729     hr = pSHGetFolderLocation(NULL, CSIDL_FAVORITES, NULL, 0,
730      &pidl);
731     ok(hr == E_FAIL,
732      "SHGetFolderLocation returned 0x%08x, expected E_FAIL\n", hr);
733     if (SUCCEEDED(hr) && pidl)
734         IMalloc_Free(pMalloc, pidl);
735     ok(!pSHGetSpecialFolderPathA(NULL, path, CSIDL_FAVORITES, FALSE),
736      "SHGetSpecialFolderPath succeeded, expected failure\n");
737     pidl = NULL;
738     hr = pSHGetSpecialFolderLocation(NULL, CSIDL_FAVORITES, &pidl);
739     ok(hr == E_FAIL, "SHGetFolderLocation returned 0x%08x, expected E_FAIL\n",
740      hr);
741     if (SUCCEEDED(hr) && pidl)
742         IMalloc_Free(pMalloc, pidl);
743     /* now test success: */
744     hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES | CSIDL_FLAG_CREATE, NULL,
745      SHGFP_TYPE_CURRENT, path);
746     if (SUCCEEDED(hr))
747     {
748         BOOL ret;
749
750         if (winetest_interactive)
751             printf("CSIDL_FAVORITES was changed to %s\n", path);
752         ret = CreateDirectoryA(path, NULL);
753         ok(!ret,
754          "CreateDirectoryA succeeded but should have failed "
755          "with ERROR_ALREADY_EXISTS\n");
756         if (!ret)
757             ok(GetLastError() == ERROR_ALREADY_EXISTS,
758              "CreateDirectoryA failed with %d, "
759              "expected ERROR_ALREADY_EXISTS\n",
760              GetLastError());
761     }
762     ok(SUCCEEDED(hr),
763      "SHGetFolderPath(NULL, CSIDL_FAVORITES | CSIDL_FLAG_CREATE, "
764      "NULL, SHGFP_TYPE_CURRENT, path)\nfailed: 0x%08x\n", hr);
765 }
766
767 /* Subprocess helper 2: make sure SHGetFolderPath still succeeds when the
768  * original value of CSIDL_FAVORITES is restored.
769  */
770 static void testNonExistentPath2(void)
771 {
772     HRESULT hr;
773     char path[MAX_PATH];
774
775     hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES | CSIDL_FLAG_CREATE, NULL,
776      SHGFP_TYPE_CURRENT, path);
777     ok(SUCCEEDED(hr), "SHGetFolderPath failed: 0x%08x\n", hr);
778 }
779
780 static void doChild(const char *arg)
781 {
782     if (arg[0] == '1')
783         testNonExistentPath1();
784     else if (arg[0] == '2')
785         testNonExistentPath2();
786 }
787
788 /* Tests the return values from the various shell functions both with and
789  * without the use of the CSIDL_FLAG_CREATE flag.  This flag only appeared in
790  * version 5 of the shell, so don't test unless it's at least version 5.
791  * The test reads a value from the registry, modifies it, calls
792  * SHGetFolderPath once with the CSIDL_FLAG_CREATE flag, and immediately
793  * afterward without it.  Then it restores the registry and deletes the folder
794  * that was created.
795  * One oddity with respect to restoration: shell32 caches somehow, so it needs
796  * to be reloaded in order to see the correct (restored) value.
797  * Some APIs unrelated to the ones under test may fail, but I expect they're
798  * covered by other unit tests; I just print out something about failure to
799  * help trace what's going on.
800  */
801 static void testNonExistentPath(void)
802 {
803     static const char userShellFolders[] = 
804      "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders";
805     char originalPath[MAX_PATH], modifiedPath[MAX_PATH];
806     HKEY key;
807
808     if (!pSHGetFolderPathA) return;
809     if (!pSHGetFolderLocation) return;
810     if (!pSHGetSpecialFolderPathA) return;
811     if (!pSHGetSpecialFolderLocation) return;
812     if (!pSHFileOperationA) return;
813     if (shellVersion.dwMajorVersion < 5) return;
814
815     if (!RegOpenKeyExA(HKEY_CURRENT_USER, userShellFolders, 0, KEY_ALL_ACCESS,
816      &key))
817     {
818         DWORD len, type;
819
820         len = sizeof(originalPath);
821         if (!RegQueryValueExA(key, "Favorites", NULL, &type,
822          (LPBYTE)&originalPath, &len))
823         {
824             size_t len = strlen(originalPath);
825
826             memcpy(modifiedPath, originalPath, len);
827             modifiedPath[len++] = '2';
828             modifiedPath[len++] = '\0';
829             if (winetest_interactive)
830                 printf("Changing CSIDL_FAVORITES to %s\n", modifiedPath);
831             if (!RegSetValueExA(key, "Favorites", 0, type,
832              (LPBYTE)modifiedPath, len))
833             {
834                 char buffer[MAX_PATH+20];
835                 STARTUPINFOA startup;
836                 PROCESS_INFORMATION info;
837                 HRESULT hr;
838
839                 sprintf(buffer, "%s tests/shellpath.c 1", selfname);
840                 memset(&startup, 0, sizeof(startup));
841                 startup.cb = sizeof(startup);
842                 startup.dwFlags = STARTF_USESHOWWINDOW;
843                 startup.dwFlags = SW_SHOWNORMAL;
844                 CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL,
845                  &startup, &info);
846                 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0,
847                  "child process termination\n");
848
849                 /* Query the path to be able to delete it below */
850                 hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES, NULL,
851                  SHGFP_TYPE_CURRENT, modifiedPath);
852                 ok(SUCCEEDED(hr), "SHGetFolderPathA failed: 0x%08x\n", hr);
853
854                 /* restore original values: */
855                 if (winetest_interactive)
856                     printf("Restoring CSIDL_FAVORITES to %s\n", originalPath);
857                 RegSetValueExA(key, "Favorites", 0, type, (LPBYTE) originalPath,
858                  strlen(originalPath) + 1);
859                 RegFlushKey(key);
860
861                 sprintf(buffer, "%s tests/shellpath.c 2", selfname);
862                 memset(&startup, 0, sizeof(startup));
863                 startup.cb = sizeof(startup);
864                 startup.dwFlags = STARTF_USESHOWWINDOW;
865                 startup.dwFlags = SW_SHOWNORMAL;
866                 CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL,
867                  &startup, &info);
868                 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0,
869                  "child process termination\n");
870
871                 strcpy(buffer, modifiedPath);
872                 strcat(buffer, "\\desktop.ini");
873                 DeleteFileA(buffer);
874                 RemoveDirectoryA(modifiedPath);
875             }
876         }
877         else if (winetest_interactive)
878             printf("RegQueryValueExA(key, Favorites, ...) failed\n");
879         if (key)
880             RegCloseKey(key);
881     }
882     else if (winetest_interactive)
883         printf("RegOpenKeyExA(HKEY_CURRENT_USER, %s, ...) failed\n",
884          userShellFolders);
885 }
886
887 START_TEST(shellpath)
888 {
889     if (!init()) return;
890
891     loadShell32();
892
893     if (myARGC >= 3)
894         doChild(myARGV[2]);
895     else
896     {
897         /* first test various combinations of parameters: */
898         testApiParameters();
899
900         /* check known values: */
901         testPidlTypes();
902         testGUIDs();
903         testEnvVars();
904         testWinDir();
905         testSystemDir();
906         testNonExistentPath();
907     }
908 }