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