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