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