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