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