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