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