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