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