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