mcicda: Exclude unused headers.
[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 #ifndef ARRAY_SIZE
39 #define ARRAY_SIZE(x) ( sizeof(x) / sizeof((x)[0]) )
40 #endif
41
42 /* from pidl.h, not included here: */
43 #ifndef PT_GUID
44 #define PT_GUID       0x1f /* no path */
45 #endif
46 #ifndef PT_DRIVE 
47 #define PT_DRIVE      0x23 /* has path */
48 #endif
49 #ifndef PT_DRIVE2
50 #define PT_DRIVE2     0x25 /* has path */
51 #endif
52 #ifndef PT_SHELLEXT
53 #define PT_SHELLEXT   0x2e /* no path */
54 #endif
55 #ifndef PT_FOLDER
56 #define PT_FOLDER     0x31 /* has path */
57 #endif
58 #ifndef PT_WORKGRP
59 #define PT_WORKGRP    0x41 /* no path */
60 #endif
61 #ifndef PT_YAGUID
62 #define PT_YAGUID     0x70 /* no path */
63 #endif
64 /* FIXME: this is used for history/favorites folders; what's a better name? */
65 #ifndef PT_IESPECIAL2
66 #define PT_IESPECIAL2 0xb1 /* has path */
67 #endif
68
69 static GUID CLSID_CommonDocuments = { 0x0000000c, 0x0000, 0x0000, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x1a } };
70
71 struct shellExpectedValues {
72     int  folder;
73     BYTE pidlType;
74 };
75
76 static HMODULE hShell32;
77 static HRESULT (WINAPI *pSHGetFolderPathA)(HWND, int, HANDLE, DWORD, LPSTR);
78 static HRESULT (WINAPI *pSHGetFolderLocation)(HWND, int, HANDLE, DWORD,
79  LPITEMIDLIST *);
80 static BOOL    (WINAPI *pSHGetSpecialFolderPathA)(HWND, LPSTR, int, BOOL);
81 static HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
82 static LPITEMIDLIST (WINAPI *pILFindLastID)(LPCITEMIDLIST);
83 static int (WINAPI *pSHFileOperationA)(LPSHFILEOPSTRUCTA);
84 static HRESULT (WINAPI *pSHGetMalloc)(LPMALLOC *);
85 static DLLVERSIONINFO shellVersion = { 0 };
86 static LPMALLOC pMalloc;
87 static const struct shellExpectedValues requiredShellValues[] = {
88  { CSIDL_BITBUCKET, PT_GUID },
89  { CSIDL_CONTROLS, PT_SHELLEXT },
90  { CSIDL_COOKIES, PT_FOLDER },
91  { CSIDL_DESKTOPDIRECTORY, PT_FOLDER },
92  { CSIDL_DRIVES, PT_GUID },
93  { CSIDL_FAVORITES, PT_FOLDER },
94  { CSIDL_FONTS, PT_FOLDER },
95 /* FIXME: the following fails in Wine, returns type PT_FOLDER
96  { CSIDL_HISTORY, PT_IESPECIAL2 },
97  */
98  { CSIDL_INTERNET, PT_GUID },
99  { CSIDL_NETHOOD, PT_FOLDER },
100  { CSIDL_NETWORK, PT_GUID },
101  { CSIDL_PRINTERS, PT_YAGUID },
102  { CSIDL_PRINTHOOD, PT_FOLDER },
103  { CSIDL_PROGRAMS, PT_FOLDER },
104  { CSIDL_RECENT, PT_FOLDER },
105  { CSIDL_SENDTO, PT_FOLDER },
106  { CSIDL_STARTMENU, PT_FOLDER },
107  { CSIDL_STARTUP, PT_FOLDER },
108  { CSIDL_TEMPLATES, PT_FOLDER },
109 };
110 static const struct shellExpectedValues optionalShellValues[] = {
111 /* FIXME: the following only semi-succeed; they return NULL PIDLs on XP.. hmm.
112  { CSIDL_ALTSTARTUP, PT_FOLDER },
113  { CSIDL_COMMON_ALTSTARTUP, PT_FOLDER },
114  { CSIDL_COMMON_OEM_LINKS, PT_FOLDER },
115  */
116 /* Windows NT-only: */
117  { CSIDL_COMMON_DESKTOPDIRECTORY, PT_FOLDER },
118  { CSIDL_COMMON_DOCUMENTS, PT_SHELLEXT },
119  { CSIDL_COMMON_FAVORITES, PT_FOLDER },
120  { CSIDL_COMMON_PROGRAMS, PT_FOLDER },
121  { CSIDL_COMMON_STARTMENU, PT_FOLDER },
122  { CSIDL_COMMON_STARTUP, PT_FOLDER },
123  { CSIDL_COMMON_TEMPLATES, PT_FOLDER },
124 /* first appearing in shell32 version 4.71: */
125  { CSIDL_APPDATA, PT_FOLDER },
126 /* first appearing in shell32 version 4.72: */
127  { CSIDL_INTERNET_CACHE, PT_IESPECIAL2 },
128 /* first appearing in shell32 version 5.0: */
129  { CSIDL_ADMINTOOLS, PT_FOLDER },
130  { CSIDL_COMMON_APPDATA, PT_FOLDER },
131  { CSIDL_LOCAL_APPDATA, PT_FOLDER },
132  { CSIDL_MYDOCUMENTS, PT_FOLDER },
133  { CSIDL_MYMUSIC, PT_FOLDER },
134  { CSIDL_MYPICTURES, PT_FOLDER },
135  { CSIDL_MYVIDEO, PT_FOLDER },
136  { CSIDL_PROFILE, PT_FOLDER },
137  { CSIDL_PROGRAM_FILES, PT_FOLDER },
138  { CSIDL_PROGRAM_FILESX86, PT_FOLDER },
139  { CSIDL_PROGRAM_FILES_COMMON, PT_FOLDER },
140  { CSIDL_PROGRAM_FILES_COMMONX86, PT_FOLDER },
141  { CSIDL_SYSTEM, PT_FOLDER },
142  { CSIDL_WINDOWS, PT_FOLDER },
143 /* first appearing in shell32 6.0: */
144  { CSIDL_CDBURN_AREA, PT_FOLDER },
145  { CSIDL_COMMON_MUSIC, PT_FOLDER },
146  { CSIDL_COMMON_PICTURES, PT_FOLDER },
147  { CSIDL_COMMON_VIDEO, PT_FOLDER },
148  { CSIDL_COMPUTERSNEARME, PT_WORKGRP },
149  { CSIDL_RESOURCES, PT_FOLDER },
150  { CSIDL_RESOURCES_LOCALIZED, PT_FOLDER },
151 };
152
153 static void loadShell32(void)
154 {
155     hShell32 = LoadLibraryA("shell32");
156     if (hShell32)
157     {
158         HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO *);
159
160         pSHGetFolderPathA = (void *)GetProcAddress(hShell32,
161          "SHGetFolderPathA");
162         pSHGetFolderLocation = (void *)GetProcAddress(hShell32,
163          "SHGetFolderLocation");
164         pSHGetSpecialFolderPathA = (void *)GetProcAddress(hShell32,
165          "SHGetSpecialFolderPathA");
166         pSHGetSpecialFolderLocation = (void *)GetProcAddress(hShell32,
167          "SHGetSpecialFolderLocation");
168         pDllGetVersion = (void *)GetProcAddress(hShell32, "DllGetVersion");
169         pILFindLastID = (void *)GetProcAddress(hShell32, "ILFindLastID");
170         if (!pILFindLastID)
171             pILFindLastID = (void *)GetProcAddress(hShell32, (LPCSTR)16);
172         pSHFileOperationA = (void *)GetProcAddress(hShell32,
173          "SHFileOperationA");
174         pSHGetMalloc = (void *)GetProcAddress(hShell32, "SHGetMalloc");
175
176         ok(pSHGetMalloc != NULL, "shell32 is missing SHGetMalloc\n");
177         if (pSHGetMalloc)
178         {
179             HRESULT hr = pSHGetMalloc(&pMalloc);
180
181             ok(SUCCEEDED(hr), "SHGetMalloc failed: 0x%08x\n", hr);
182             ok(pMalloc != NULL, "SHGetMalloc returned a NULL IMalloc\n");
183         }
184
185         if (pDllGetVersion)
186         {
187             shellVersion.cbSize = sizeof(shellVersion);
188             pDllGetVersion(&shellVersion);
189             if (winetest_interactive)
190                 printf("shell32 version is %d.%d\n",
191                  shellVersion.dwMajorVersion, shellVersion.dwMinorVersion);
192         }
193     }
194 }
195
196 #ifndef CSIDL_PROFILES
197 #define CSIDL_PROFILES          0x003e
198 #endif
199
200 /* A couple utility printing functions */
201 static const char *getFolderName(int folder)
202 {
203     static char unknown[32];
204
205 #define CSIDL_TO_STR(x) case x: return#x;
206     switch (folder)
207     {
208     CSIDL_TO_STR(CSIDL_DESKTOP);
209     CSIDL_TO_STR(CSIDL_INTERNET);
210     CSIDL_TO_STR(CSIDL_PROGRAMS);
211     CSIDL_TO_STR(CSIDL_CONTROLS);
212     CSIDL_TO_STR(CSIDL_PRINTERS);
213     CSIDL_TO_STR(CSIDL_PERSONAL);
214     CSIDL_TO_STR(CSIDL_FAVORITES);
215     CSIDL_TO_STR(CSIDL_STARTUP);
216     CSIDL_TO_STR(CSIDL_RECENT);
217     CSIDL_TO_STR(CSIDL_SENDTO);
218     CSIDL_TO_STR(CSIDL_BITBUCKET);
219     CSIDL_TO_STR(CSIDL_STARTMENU);
220     CSIDL_TO_STR(CSIDL_MYDOCUMENTS);
221     CSIDL_TO_STR(CSIDL_MYMUSIC);
222     CSIDL_TO_STR(CSIDL_MYVIDEO);
223     CSIDL_TO_STR(CSIDL_DESKTOPDIRECTORY);
224     CSIDL_TO_STR(CSIDL_DRIVES);
225     CSIDL_TO_STR(CSIDL_NETWORK);
226     CSIDL_TO_STR(CSIDL_NETHOOD);
227     CSIDL_TO_STR(CSIDL_FONTS);
228     CSIDL_TO_STR(CSIDL_TEMPLATES);
229     CSIDL_TO_STR(CSIDL_COMMON_STARTMENU);
230     CSIDL_TO_STR(CSIDL_COMMON_PROGRAMS);
231     CSIDL_TO_STR(CSIDL_COMMON_STARTUP);
232     CSIDL_TO_STR(CSIDL_COMMON_DESKTOPDIRECTORY);
233     CSIDL_TO_STR(CSIDL_APPDATA);
234     CSIDL_TO_STR(CSIDL_PRINTHOOD);
235     CSIDL_TO_STR(CSIDL_LOCAL_APPDATA);
236     CSIDL_TO_STR(CSIDL_ALTSTARTUP);
237     CSIDL_TO_STR(CSIDL_COMMON_ALTSTARTUP);
238     CSIDL_TO_STR(CSIDL_COMMON_FAVORITES);
239     CSIDL_TO_STR(CSIDL_INTERNET_CACHE);
240     CSIDL_TO_STR(CSIDL_COOKIES);
241     CSIDL_TO_STR(CSIDL_HISTORY);
242     CSIDL_TO_STR(CSIDL_COMMON_APPDATA);
243     CSIDL_TO_STR(CSIDL_WINDOWS);
244     CSIDL_TO_STR(CSIDL_SYSTEM);
245     CSIDL_TO_STR(CSIDL_PROGRAM_FILES);
246     CSIDL_TO_STR(CSIDL_MYPICTURES);
247     CSIDL_TO_STR(CSIDL_PROFILE);
248     CSIDL_TO_STR(CSIDL_SYSTEMX86);
249     CSIDL_TO_STR(CSIDL_PROGRAM_FILESX86);
250     CSIDL_TO_STR(CSIDL_PROGRAM_FILES_COMMON);
251     CSIDL_TO_STR(CSIDL_PROGRAM_FILES_COMMONX86);
252     CSIDL_TO_STR(CSIDL_COMMON_TEMPLATES);
253     CSIDL_TO_STR(CSIDL_COMMON_DOCUMENTS);
254     CSIDL_TO_STR(CSIDL_COMMON_ADMINTOOLS);
255     CSIDL_TO_STR(CSIDL_ADMINTOOLS);
256     CSIDL_TO_STR(CSIDL_CONNECTIONS);
257     CSIDL_TO_STR(CSIDL_PROFILES);
258     CSIDL_TO_STR(CSIDL_COMMON_MUSIC);
259     CSIDL_TO_STR(CSIDL_COMMON_PICTURES);
260     CSIDL_TO_STR(CSIDL_COMMON_VIDEO);
261     CSIDL_TO_STR(CSIDL_RESOURCES);
262     CSIDL_TO_STR(CSIDL_RESOURCES_LOCALIZED);
263     CSIDL_TO_STR(CSIDL_COMMON_OEM_LINKS);
264     CSIDL_TO_STR(CSIDL_CDBURN_AREA);
265     CSIDL_TO_STR(CSIDL_COMPUTERSNEARME);
266 #undef CSIDL_TO_STR
267     default:
268         sprintf(unknown, "unknown (0x%04x)", folder);
269         return unknown;
270     }
271 }
272
273 static const char *printGUID(const GUID *guid)
274 {
275     static char guidSTR[39];
276
277     if (!guid) return NULL;
278
279     sprintf(guidSTR, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
280      guid->Data1, guid->Data2, guid->Data3,
281      guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
282      guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
283     return guidSTR;
284 }
285
286 static void testSHGetFolderLocationInvalidArgs(void)
287 {
288     LPITEMIDLIST pidl;
289     HRESULT hr;
290
291     if (!pSHGetFolderLocation) return;
292
293     /* check a bogus CSIDL: */
294     pidl = NULL;
295     hr = pSHGetFolderLocation(NULL, 0xeeee, NULL, 0, &pidl);
296     ok(hr == E_INVALIDARG,
297      "SHGetFolderLocation(NULL, 0xeeee, NULL, 0, &pidl)\n"
298      "returned 0x%08x, expected E_INVALIDARG\n", hr);
299     if (SUCCEEDED(hr))
300         IMalloc_Free(pMalloc, pidl);
301     /* check a bogus user token: */
302     pidl = NULL;
303     hr = pSHGetFolderLocation(NULL, CSIDL_FAVORITES, (HANDLE)2, 0, &pidl);
304     ok(hr == E_FAIL,
305      "SHGetFolderLocation(NULL, CSIDL_FAVORITES, 2, 0, &pidl)\n"
306      "returned 0x%08x, expected E_FAIL\n", hr);
307     if (SUCCEEDED(hr))
308         IMalloc_Free(pMalloc, pidl);
309     /* check reserved is not zero: */
310     pidl = NULL;
311     hr = pSHGetFolderLocation(NULL, CSIDL_DESKTOP, NULL, 1, &pidl);
312     ok(hr == E_INVALIDARG,
313      "SHGetFolderLocation(NULL, CSIDL_DESKTOP, NULL, 1, &pidl)\n"
314      "returned 0x%08x, expected E_INVALIDARG\n", hr);
315     if (SUCCEEDED(hr))
316         IMalloc_Free(pMalloc, pidl);
317     /* a NULL pidl pointer crashes, so don't test it */
318 }
319
320 static void testSHGetSpecialFolderLocationInvalidArgs(void)
321 {
322     LPITEMIDLIST pidl = NULL;
323     HRESULT hr;
324
325     if (!pSHGetSpecialFolderLocation) return;
326
327     /* SHGetSpecialFolderLocation(NULL, 0, NULL) crashes */
328     hr = pSHGetSpecialFolderLocation(NULL, 0xeeee, &pidl);
329     ok(hr == E_INVALIDARG,
330      "SHGetSpecialFolderLocation(NULL, 0xeeee, &pidl) returned 0x%08x, "
331      "expected E_INVALIDARG\n", hr);
332 }
333
334 static void testSHGetFolderPathInvalidArgs(void)
335 {
336     char path[MAX_PATH];
337     HRESULT hr;
338
339     if (!pSHGetFolderPathA) return;
340
341     /* expect 2's a bogus handle, especially since we didn't open it */
342     hr = pSHGetFolderPathA(NULL, CSIDL_DESKTOP, (HANDLE)2,
343      SHGFP_TYPE_DEFAULT, path);
344     ok(hr == E_FAIL,
345      "SHGetFolderPathA(NULL, CSIDL_DESKTOP, 2, SHGFP_TYPE_DEFAULT, path)\n"
346      "returned 0x%08x, expected E_FAIL\n", hr);
347     hr = pSHGetFolderPathA(NULL, 0xeeee, NULL, SHGFP_TYPE_DEFAULT, path);
348     ok(hr == E_INVALIDARG,
349      "SHGetFolderPathA(NULL, 0xeeee, NULL, SHGFP_TYPE_DEFAULT, path)\n"
350      "returned 0x%08x, expected E_INVALIDARG\n", hr);
351 }
352
353 static void testSHGetSpecialFolderPathInvalidArgs(void)
354 {
355     char path[MAX_PATH];
356     BOOL ret;
357
358     if (!pSHGetSpecialFolderPathA) return;
359
360 #if 0
361     ret = pSHGetSpecialFolderPathA(NULL, NULL, CSIDL_BITBUCKET, FALSE);
362     ok(!ret,
363      "SHGetSpecialFolderPathA(NULL, NULL, CSIDL_BITBUCKET, FALSE)\n"
364      "returned TRUE, expected FALSE\n");
365 #endif
366     /* odd but true: calling with a NULL path still succeeds if it's a real
367      * dir (on some windows platform).  on winME it generates exception.
368      */
369     ret = pSHGetSpecialFolderPathA(NULL, path, CSIDL_PROGRAMS, FALSE);
370     ok(ret,
371      "SHGetSpecialFolderPathA(NULL, path, 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%08x\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%08x\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%08x\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, path);
724     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
725      "SHGetFolderPath returned 0x%08x, 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%08x, 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%08x, 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 %d, "
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%08x\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     char path[MAX_PATH];
772
773     hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES | CSIDL_FLAG_CREATE, NULL,
774      SHGFP_TYPE_CURRENT, path);
775     ok(SUCCEEDED(hr), "SHGetFolderPath failed: 0x%08x\n", hr);
776 }
777
778 static void doChild(const char *arg)
779 {
780     if (arg[0] == '1')
781         testNonExistentPath1();
782     else if (arg[0] == '2')
783         testNonExistentPath2();
784 }
785
786 /* Tests the return values from the various shell functions both with and
787  * without the use of the CSIDL_FLAG_CREATE flag.  This flag only appeared in
788  * version 5 of the shell, so don't test unless it's at least version 5.
789  * The test reads a value from the registry, modifies it, calls
790  * SHGetFolderPath once with the CSIDL_FLAG_CREATE flag, and immediately
791  * afterward without it.  Then it restores the registry and deletes the folder
792  * that was created.
793  * One oddity with respect to restoration: shell32 caches somehow, so it needs
794  * to be reloaded in order to see the correct (restored) value.
795  * Some APIs unrelated to the ones under test may fail, but I expect they're
796  * covered by other unit tests; I just print out something about failure to
797  * help trace what's going on.
798  */
799 static void testNonExistentPath(void)
800 {
801     static const char userShellFolders[] = 
802      "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders";
803     char originalPath[MAX_PATH], modifiedPath[MAX_PATH];
804     HKEY key;
805
806     if (!pSHGetFolderPathA) return;
807     if (!pSHGetFolderLocation) return;
808     if (!pSHGetSpecialFolderPathA) return;
809     if (!pSHGetSpecialFolderLocation) return;
810     if (!pSHFileOperationA) return;
811     if (shellVersion.dwMajorVersion < 5) return;
812
813     if (!RegOpenKeyExA(HKEY_CURRENT_USER, userShellFolders, 0, KEY_ALL_ACCESS,
814      &key))
815     {
816         DWORD len, type;
817
818         len = sizeof(originalPath);
819         if (!RegQueryValueExA(key, "Favorites", NULL, &type,
820          (LPBYTE)&originalPath, &len))
821         {
822             size_t len = strlen(originalPath);
823
824             memcpy(modifiedPath, originalPath, len);
825             modifiedPath[len++] = '2';
826             modifiedPath[len++] = '\0';
827             if (winetest_interactive)
828                 printf("Changing CSIDL_FAVORITES to %s\n", modifiedPath);
829             if (!RegSetValueExA(key, "Favorites", 0, type,
830              (LPBYTE)modifiedPath, len))
831             {
832                 char buffer[MAX_PATH+20];
833                 STARTUPINFOA startup;
834                 PROCESS_INFORMATION info;
835                 HRESULT hr;
836
837                 sprintf(buffer, "%s tests/shellpath.c 1", 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%08x\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, (LPBYTE) originalPath,
856                  strlen(originalPath) + 1);
857                 RegFlushKey(key);
858
859                 sprintf(buffer, "%s tests/shellpath.c 2", selfname);
860                 memset(&startup, 0, sizeof(startup));
861                 startup.cb = sizeof(startup);
862                 startup.dwFlags = STARTF_USESHOWWINDOW;
863                 startup.dwFlags = SW_SHOWNORMAL;
864                 CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL,
865                  &startup, &info);
866                 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0,
867                  "child process termination\n");
868
869                 strcpy(buffer, modifiedPath);
870                 strcat(buffer, "\\desktop.ini");
871                 DeleteFileA(buffer);
872                 RemoveDirectoryA(modifiedPath);
873             }
874         }
875         else if (winetest_interactive)
876             printf("RegQueryValueExA(key, Favorites, ...) failed\n");
877         if (key)
878             RegCloseKey(key);
879     }
880     else if (winetest_interactive)
881         printf("RegOpenKeyExA(HKEY_CURRENT_USER, %s, ...) failed\n",
882          userShellFolders);
883 }
884
885 START_TEST(shellpath)
886 {
887     if (!init()) return;
888
889     loadShell32();
890     if (!hShell32) return;
891
892     if (myARGC >= 3)
893         doChild(myARGV[2]);
894     else
895     {
896         /* first test various combinations of parameters: */
897         testApiParameters();
898
899         /* check known values: */
900         testPidlTypes();
901         testGUIDs();
902         testEnvVars();
903         testWinDir();
904         testSystemDir();
905         testNonExistentPath();
906     }
907 }