shell32: Add support of registry-registered folders to IKnownFolderManager::GetFolder.
[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 *pSHSetKnownFolderPath)(REFKNOWNFOLDERID, DWORD, HANDLE, PWSTR);
99 static HRESULT (WINAPI *pSHGetFolderPathEx)(REFKNOWNFOLDERID, DWORD, HANDLE, LPWSTR, DWORD);
100
101 static DLLVERSIONINFO shellVersion = { 0 };
102 static LPMALLOC pMalloc;
103 static const BYTE guidType[] = { PT_GUID };
104 static const BYTE controlPanelType[] = { PT_SHELLEXT, PT_GUID, PT_CPL };
105 static const BYTE folderType[] = { PT_FOLDER, PT_FOLDERW };
106 static const BYTE favoritesType[] = { PT_FOLDER, PT_FOLDERW, 0, PT_IESPECIAL2 /* Win98 */ };
107 static const BYTE folderOrSpecialType[] = { PT_FOLDER, PT_IESPECIAL2 };
108 static const BYTE personalType[] = { PT_FOLDER, PT_GUID, PT_DRIVE, 0xff /* Win9x */,
109  PT_IESPECIAL2 /* Win98 */, 0 /* Vista */ };
110 /* FIXME: don't know the type of 0x71 returned by Vista/2008 for printers */
111 static const BYTE printersType[] = { PT_YAGUID, PT_SHELLEXT, 0x71 };
112 static const BYTE ieSpecialType[] = { PT_IESPECIAL2 };
113 static const BYTE shellExtType[] = { PT_SHELLEXT };
114 static const BYTE workgroupType[] = { PT_WORKGRP };
115 #define DECLARE_TYPE(x, y) { x, sizeof(y) / sizeof(y[0]), y }
116 static const struct shellExpectedValues requiredShellValues[] = {
117  DECLARE_TYPE(CSIDL_BITBUCKET, guidType),
118  DECLARE_TYPE(CSIDL_CONTROLS, controlPanelType),
119  DECLARE_TYPE(CSIDL_COOKIES, folderType),
120  DECLARE_TYPE(CSIDL_DESKTOPDIRECTORY, folderType),
121  DECLARE_TYPE(CSIDL_DRIVES, guidType),
122  DECLARE_TYPE(CSIDL_FAVORITES, favoritesType),
123  DECLARE_TYPE(CSIDL_FONTS, folderOrSpecialType),
124 /* FIXME: the following fails in Wine, returns type PT_FOLDER
125  DECLARE_TYPE(CSIDL_HISTORY, ieSpecialType),
126  */
127  DECLARE_TYPE(CSIDL_INTERNET, guidType),
128  DECLARE_TYPE(CSIDL_NETHOOD, folderType),
129  DECLARE_TYPE(CSIDL_NETWORK, guidType),
130  DECLARE_TYPE(CSIDL_PERSONAL, personalType),
131  DECLARE_TYPE(CSIDL_PRINTERS, printersType),
132  DECLARE_TYPE(CSIDL_PRINTHOOD, folderType),
133  DECLARE_TYPE(CSIDL_PROGRAMS, folderType),
134  DECLARE_TYPE(CSIDL_RECENT, folderOrSpecialType),
135  DECLARE_TYPE(CSIDL_SENDTO, folderType),
136  DECLARE_TYPE(CSIDL_STARTMENU, folderType),
137  DECLARE_TYPE(CSIDL_STARTUP, folderType),
138  DECLARE_TYPE(CSIDL_TEMPLATES, folderType),
139 };
140 static const struct shellExpectedValues optionalShellValues[] = {
141 /* FIXME: the following only semi-succeed; they return NULL PIDLs on XP.. hmm.
142  DECLARE_TYPE(CSIDL_ALTSTARTUP, folderType),
143  DECLARE_TYPE(CSIDL_COMMON_ALTSTARTUP, folderType),
144  DECLARE_TYPE(CSIDL_COMMON_OEM_LINKS, folderType),
145  */
146 /* Windows NT-only: */
147  DECLARE_TYPE(CSIDL_COMMON_DESKTOPDIRECTORY, folderType),
148  DECLARE_TYPE(CSIDL_COMMON_DOCUMENTS, shellExtType),
149  DECLARE_TYPE(CSIDL_COMMON_FAVORITES, folderType),
150  DECLARE_TYPE(CSIDL_COMMON_PROGRAMS, folderType),
151  DECLARE_TYPE(CSIDL_COMMON_STARTMENU, folderType),
152  DECLARE_TYPE(CSIDL_COMMON_STARTUP, folderType),
153  DECLARE_TYPE(CSIDL_COMMON_TEMPLATES, folderType),
154 /* first appearing in shell32 version 4.71: */
155  DECLARE_TYPE(CSIDL_APPDATA, folderType),
156 /* first appearing in shell32 version 4.72: */
157  DECLARE_TYPE(CSIDL_INTERNET_CACHE, ieSpecialType),
158 /* first appearing in shell32 version 5.0: */
159  DECLARE_TYPE(CSIDL_ADMINTOOLS, folderType),
160  DECLARE_TYPE(CSIDL_COMMON_APPDATA, folderType),
161  DECLARE_TYPE(CSIDL_LOCAL_APPDATA, folderType),
162  DECLARE_TYPE(OLD_CSIDL_MYDOCUMENTS, folderType),
163  DECLARE_TYPE(CSIDL_MYMUSIC, folderType),
164  DECLARE_TYPE(CSIDL_MYPICTURES, folderType),
165  DECLARE_TYPE(CSIDL_MYVIDEO, folderType),
166  DECLARE_TYPE(CSIDL_PROFILE, folderType),
167  DECLARE_TYPE(CSIDL_PROGRAM_FILES, folderType),
168  DECLARE_TYPE(CSIDL_PROGRAM_FILESX86, folderType),
169  DECLARE_TYPE(CSIDL_PROGRAM_FILES_COMMON, folderType),
170  DECLARE_TYPE(CSIDL_PROGRAM_FILES_COMMONX86, folderType),
171  DECLARE_TYPE(CSIDL_SYSTEM, folderType),
172  DECLARE_TYPE(CSIDL_WINDOWS, folderType),
173 /* first appearing in shell32 6.0: */
174  DECLARE_TYPE(CSIDL_CDBURN_AREA, folderType),
175  DECLARE_TYPE(CSIDL_COMMON_MUSIC, folderType),
176  DECLARE_TYPE(CSIDL_COMMON_PICTURES, folderType),
177  DECLARE_TYPE(CSIDL_COMMON_VIDEO, folderType),
178  DECLARE_TYPE(CSIDL_COMPUTERSNEARME, workgroupType),
179  DECLARE_TYPE(CSIDL_RESOURCES, folderType),
180  DECLARE_TYPE(CSIDL_RESOURCES_LOCALIZED, folderType),
181 };
182 #undef DECLARE_TYPE
183
184 static void loadShell32(void)
185 {
186     HMODULE hShell32 = GetModuleHandleA("shell32");
187
188 #define GET_PROC(func) \
189     p ## func = (void*)GetProcAddress(hShell32, #func); \
190     if(!p ## func) \
191       trace("GetProcAddress(%s) failed\n", #func);
192
193     GET_PROC(DllGetVersion)
194     GET_PROC(SHGetFolderPathA)
195     GET_PROC(SHGetFolderPathEx)
196     GET_PROC(SHGetFolderLocation)
197     GET_PROC(SHGetKnownFolderPath)
198     GET_PROC(SHSetKnownFolderPath)
199     GET_PROC(SHGetSpecialFolderPathA)
200     GET_PROC(SHGetSpecialFolderLocation)
201     GET_PROC(ILFindLastID)
202     if (!pILFindLastID)
203         pILFindLastID = (void *)GetProcAddress(hShell32, (LPCSTR)16);
204     GET_PROC(SHFileOperationA)
205     GET_PROC(SHGetMalloc)
206
207     ok(pSHGetMalloc != NULL, "shell32 is missing SHGetMalloc\n");
208     if (pSHGetMalloc)
209     {
210         HRESULT hr = pSHGetMalloc(&pMalloc);
211
212         ok(hr == S_OK, "SHGetMalloc failed: 0x%08x\n", hr);
213         ok(pMalloc != NULL, "SHGetMalloc returned a NULL IMalloc\n");
214     }
215
216     if (pDllGetVersion)
217     {
218         shellVersion.cbSize = sizeof(shellVersion);
219         pDllGetVersion(&shellVersion);
220         trace("shell32 version is %d.%d\n",
221               shellVersion.dwMajorVersion, shellVersion.dwMinorVersion);
222     }
223 #undef GET_PROC
224 }
225
226 #ifndef CSIDL_PROFILES
227 #define CSIDL_PROFILES          0x003e
228 #endif
229
230 /* A couple utility printing functions */
231 static const char *getFolderName(int folder)
232 {
233     static char unknown[32];
234
235 #define CSIDL_TO_STR(x) case x: return#x;
236     switch (folder)
237     {
238     CSIDL_TO_STR(CSIDL_DESKTOP);
239     CSIDL_TO_STR(CSIDL_INTERNET);
240     CSIDL_TO_STR(CSIDL_PROGRAMS);
241     CSIDL_TO_STR(CSIDL_CONTROLS);
242     CSIDL_TO_STR(CSIDL_PRINTERS);
243     CSIDL_TO_STR(CSIDL_PERSONAL);
244     CSIDL_TO_STR(CSIDL_FAVORITES);
245     CSIDL_TO_STR(CSIDL_STARTUP);
246     CSIDL_TO_STR(CSIDL_RECENT);
247     CSIDL_TO_STR(CSIDL_SENDTO);
248     CSIDL_TO_STR(CSIDL_BITBUCKET);
249     CSIDL_TO_STR(CSIDL_STARTMENU);
250     CSIDL_TO_STR(OLD_CSIDL_MYDOCUMENTS);
251     CSIDL_TO_STR(CSIDL_MYMUSIC);
252     CSIDL_TO_STR(CSIDL_MYVIDEO);
253     CSIDL_TO_STR(CSIDL_DESKTOPDIRECTORY);
254     CSIDL_TO_STR(CSIDL_DRIVES);
255     CSIDL_TO_STR(CSIDL_NETWORK);
256     CSIDL_TO_STR(CSIDL_NETHOOD);
257     CSIDL_TO_STR(CSIDL_FONTS);
258     CSIDL_TO_STR(CSIDL_TEMPLATES);
259     CSIDL_TO_STR(CSIDL_COMMON_STARTMENU);
260     CSIDL_TO_STR(CSIDL_COMMON_PROGRAMS);
261     CSIDL_TO_STR(CSIDL_COMMON_STARTUP);
262     CSIDL_TO_STR(CSIDL_COMMON_DESKTOPDIRECTORY);
263     CSIDL_TO_STR(CSIDL_APPDATA);
264     CSIDL_TO_STR(CSIDL_PRINTHOOD);
265     CSIDL_TO_STR(CSIDL_LOCAL_APPDATA);
266     CSIDL_TO_STR(CSIDL_ALTSTARTUP);
267     CSIDL_TO_STR(CSIDL_COMMON_ALTSTARTUP);
268     CSIDL_TO_STR(CSIDL_COMMON_FAVORITES);
269     CSIDL_TO_STR(CSIDL_INTERNET_CACHE);
270     CSIDL_TO_STR(CSIDL_COOKIES);
271     CSIDL_TO_STR(CSIDL_HISTORY);
272     CSIDL_TO_STR(CSIDL_COMMON_APPDATA);
273     CSIDL_TO_STR(CSIDL_WINDOWS);
274     CSIDL_TO_STR(CSIDL_SYSTEM);
275     CSIDL_TO_STR(CSIDL_PROGRAM_FILES);
276     CSIDL_TO_STR(CSIDL_MYPICTURES);
277     CSIDL_TO_STR(CSIDL_PROFILE);
278     CSIDL_TO_STR(CSIDL_SYSTEMX86);
279     CSIDL_TO_STR(CSIDL_PROGRAM_FILESX86);
280     CSIDL_TO_STR(CSIDL_PROGRAM_FILES_COMMON);
281     CSIDL_TO_STR(CSIDL_PROGRAM_FILES_COMMONX86);
282     CSIDL_TO_STR(CSIDL_COMMON_TEMPLATES);
283     CSIDL_TO_STR(CSIDL_COMMON_DOCUMENTS);
284     CSIDL_TO_STR(CSIDL_COMMON_ADMINTOOLS);
285     CSIDL_TO_STR(CSIDL_ADMINTOOLS);
286     CSIDL_TO_STR(CSIDL_CONNECTIONS);
287     CSIDL_TO_STR(CSIDL_PROFILES);
288     CSIDL_TO_STR(CSIDL_COMMON_MUSIC);
289     CSIDL_TO_STR(CSIDL_COMMON_PICTURES);
290     CSIDL_TO_STR(CSIDL_COMMON_VIDEO);
291     CSIDL_TO_STR(CSIDL_RESOURCES);
292     CSIDL_TO_STR(CSIDL_RESOURCES_LOCALIZED);
293     CSIDL_TO_STR(CSIDL_COMMON_OEM_LINKS);
294     CSIDL_TO_STR(CSIDL_CDBURN_AREA);
295     CSIDL_TO_STR(CSIDL_COMPUTERSNEARME);
296 #undef CSIDL_TO_STR
297     default:
298         sprintf(unknown, "unknown (0x%04x)", folder);
299         return unknown;
300     }
301 }
302
303 static const char *printGUID(const GUID *guid, char * guidSTR)
304 {
305     if (!guid) return NULL;
306
307     sprintf(guidSTR, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
308      guid->Data1, guid->Data2, guid->Data3,
309      guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
310      guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
311     return guidSTR;
312 }
313
314 static void test_parameters(void)
315 {
316     LPITEMIDLIST pidl = NULL;
317     char path[MAX_PATH];
318     HRESULT hr;
319
320     if (pSHGetFolderLocation)
321     {
322         /* check a bogus CSIDL: */
323         pidl = NULL;
324         hr = pSHGetFolderLocation(NULL, 0xeeee, NULL, 0, &pidl);
325         ok(hr == E_INVALIDARG, "got 0x%08x, expected E_INVALIDARG\n", hr);
326         if (hr == S_OK) IMalloc_Free(pMalloc, pidl);
327
328         /* check a bogus user token: */
329         pidl = NULL;
330         hr = pSHGetFolderLocation(NULL, CSIDL_FAVORITES, (HANDLE)2, 0, &pidl);
331         ok(hr == E_FAIL || hr == E_HANDLE, "got 0x%08x, expected E_FAIL or E_HANDLE\n", hr);
332         if (hr == S_OK) IMalloc_Free(pMalloc, pidl);
333
334         /* a NULL pidl pointer crashes, so don't test it */
335     }
336
337     if (pSHGetSpecialFolderLocation)
338     {
339         if (0)
340             /* crashes */
341             SHGetSpecialFolderLocation(NULL, 0, NULL);
342
343         hr = pSHGetSpecialFolderLocation(NULL, 0xeeee, &pidl);
344         ok(hr == E_INVALIDARG, "got returned 0x%08x\n", hr);
345     }
346
347     if (pSHGetFolderPathA)
348     {
349         /* expect 2's a bogus handle, especially since we didn't open it */
350         hr = pSHGetFolderPathA(NULL, CSIDL_DESKTOP, (HANDLE)2, SHGFP_TYPE_DEFAULT, path);
351         ok(hr == E_FAIL || hr == E_HANDLE || /* Vista and 2k8 */
352            broken(hr == S_OK), /* W2k and Me */ "got 0x%08x, expected E_FAIL\n", hr);
353
354         hr = pSHGetFolderPathA(NULL, 0xeeee, NULL, SHGFP_TYPE_DEFAULT, path);
355         ok(hr == E_INVALIDARG, "got 0x%08x, expected E_INVALIDARG\n", hr);
356     }
357
358     if (pSHGetSpecialFolderPathA)
359     {
360         BOOL ret;
361
362         if (0)
363            pSHGetSpecialFolderPathA(NULL, NULL, CSIDL_BITBUCKET, FALSE);
364
365         /* odd but true: calling with a NULL path still succeeds if it's a real
366          * dir (on some windows platform).  on winME it generates exception.
367          */
368         ret = pSHGetSpecialFolderPathA(NULL, path, CSIDL_PROGRAMS, FALSE);
369         ok(ret, "got %d\n", ret);
370
371         ret = pSHGetSpecialFolderPathA(NULL, path, 0xeeee, FALSE);
372         ok(!ret, "got %d\n", ret);
373     }
374 }
375
376 /* Returns the folder's PIDL type, or 0xff if one can't be found. */
377 static BYTE testSHGetFolderLocation(int folder)
378 {
379     LPITEMIDLIST pidl;
380     HRESULT hr;
381     BYTE ret = 0xff;
382
383     /* treat absence of function as success */
384     if (!pSHGetFolderLocation) return TRUE;
385
386     pidl = NULL;
387     hr = pSHGetFolderLocation(NULL, folder, NULL, 0, &pidl);
388     if (hr == S_OK)
389     {
390         if (pidl)
391         {
392             LPITEMIDLIST pidlLast = pILFindLastID(pidl);
393
394             ok(pidlLast != NULL, "%s: ILFindLastID failed\n",
395              getFolderName(folder));
396             if (pidlLast)
397                 ret = pidlLast->mkid.abID[0];
398             IMalloc_Free(pMalloc, pidl);
399         }
400     }
401     return ret;
402 }
403
404 /* Returns the folder's PIDL type, or 0xff if one can't be found. */
405 static BYTE testSHGetSpecialFolderLocation(int folder)
406 {
407     LPITEMIDLIST pidl;
408     HRESULT hr;
409     BYTE ret = 0xff;
410
411     /* treat absence of function as success */
412     if (!pSHGetSpecialFolderLocation) return TRUE;
413
414     pidl = NULL;
415     hr = pSHGetSpecialFolderLocation(NULL, folder, &pidl);
416     if (hr == S_OK)
417     {
418         if (pidl)
419         {
420             LPITEMIDLIST pidlLast = pILFindLastID(pidl);
421
422             ok(pidlLast != NULL,
423                 "%s: ILFindLastID failed\n", getFolderName(folder));
424             if (pidlLast)
425                 ret = pidlLast->mkid.abID[0];
426             IMalloc_Free(pMalloc, pidl);
427         }
428     }
429     return ret;
430 }
431
432 static void test_SHGetFolderPath(BOOL optional, int folder)
433 {
434     char path[MAX_PATH];
435     HRESULT hr;
436
437     if (!pSHGetFolderPathA) return;
438
439     hr = pSHGetFolderPathA(NULL, folder, NULL, SHGFP_TYPE_CURRENT, path);
440     ok(hr == S_OK || optional,
441      "SHGetFolderPathA(NULL, %s, NULL, SHGFP_TYPE_CURRENT, path) failed: 0x%08x\n", getFolderName(folder), hr);
442 }
443
444 static void test_SHGetSpecialFolderPath(BOOL optional, int folder)
445 {
446     char path[MAX_PATH];
447     BOOL ret;
448
449     if (!pSHGetSpecialFolderPathA) return;
450
451     ret = pSHGetSpecialFolderPathA(NULL, path, folder, FALSE);
452     if (ret && winetest_interactive)
453         printf("%s: %s\n", getFolderName(folder), path);
454     ok(ret || optional,
455      "SHGetSpecialFolderPathA(NULL, path, %s, FALSE) failed\n",
456      getFolderName(folder));
457 }
458
459 static void test_ShellValues(const struct shellExpectedValues testEntries[],
460  int numEntries, BOOL optional)
461 {
462     int i;
463
464     for (i = 0; i < numEntries; i++)
465     {
466         BYTE type;
467         int j;
468         BOOL foundTypeMatch = FALSE;
469
470         if (pSHGetFolderLocation)
471         {
472             type = testSHGetFolderLocation(testEntries[i].folder);
473             for (j = 0; !foundTypeMatch && j < testEntries[i].numTypes; j++)
474                 if (testEntries[i].types[j] == type)
475                     foundTypeMatch = TRUE;
476             ok(foundTypeMatch || optional || broken(type == 0xff) /* Win9x */,
477              "%s has unexpected type %d (0x%02x)\n",
478              getFolderName(testEntries[i].folder), type, type);
479         }
480         type = testSHGetSpecialFolderLocation(testEntries[i].folder);
481         for (j = 0, foundTypeMatch = FALSE; !foundTypeMatch &&
482          j < testEntries[i].numTypes; j++)
483             if (testEntries[i].types[j] == type)
484                 foundTypeMatch = TRUE;
485         ok(foundTypeMatch || optional || broken(type == 0xff) /* Win9x */,
486          "%s has unexpected type %d (0x%02x)\n",
487          getFolderName(testEntries[i].folder), type, type);
488         switch (type)
489         {
490             case PT_FOLDER:
491             case PT_DRIVE:
492             case PT_DRIVE2:
493             case PT_IESPECIAL2:
494                 test_SHGetFolderPath(optional, testEntries[i].folder);
495                 test_SHGetSpecialFolderPath(optional, testEntries[i].folder);
496                 break;
497         }
498     }
499 }
500
501 /* Attempts to verify that the folder path corresponding to the folder CSIDL
502  * value has the same value as the environment variable with name envVar.
503  * Doesn't mind if SHGetSpecialFolderPath fails for folder or if envVar isn't
504  * set in this environment; different OS and shell version behave differently.
505  * However, if both are present, fails if envVar's value is not the same
506  * (byte-for-byte) as what SHGetSpecialFolderPath returns.
507  */
508 static void matchSpecialFolderPathToEnv(int folder, const char *envVar)
509 {
510     char path[MAX_PATH];
511
512     if (!pSHGetSpecialFolderPathA) return;
513
514     if (pSHGetSpecialFolderPathA(NULL, path, folder, FALSE))
515     {
516         char *envVal = getenv(envVar);
517
518         ok(!envVal || !lstrcmpiA(envVal, path),
519          "%%%s%% does not match SHGetSpecialFolderPath:\n"
520          "%%%s%% is %s\nSHGetSpecialFolderPath returns %s\n",
521          envVar, envVar, envVal, path);
522     }
523 }
524
525 /* Attempts to match the GUID returned by SHGetFolderLocation for folder with
526  * GUID.  Assumes the type of the returned PIDL is in fact a GUID, but doesn't
527  * fail if it isn't--that check should already have been done.
528  * Fails if the returned PIDL is a GUID whose value does not match guid.
529  */
530 static void matchGUID(int folder, const GUID *guid, const GUID *guid_alt)
531 {
532     LPITEMIDLIST pidl;
533     HRESULT hr;
534
535     if (!pSHGetFolderLocation) return;
536     if (!guid) return;
537
538     pidl = NULL;
539     hr = pSHGetFolderLocation(NULL, folder, NULL, 0, &pidl);
540     if (hr == S_OK)
541     {
542         LPITEMIDLIST pidlLast = pILFindLastID(pidl);
543
544         if (pidlLast && (pidlLast->mkid.abID[0] == PT_SHELLEXT ||
545          pidlLast->mkid.abID[0] == PT_GUID))
546         {
547             GUID *shellGuid = (GUID *)(pidlLast->mkid.abID + 2);
548             char shellGuidStr[39], guidStr[39], guid_altStr[39];
549
550             if (!guid_alt)
551              ok(IsEqualIID(shellGuid, guid),
552               "%s: got GUID %s, expected %s\n", getFolderName(folder),
553               printGUID(shellGuid, shellGuidStr), printGUID(guid, guidStr));
554             else
555              ok(IsEqualIID(shellGuid, guid) ||
556               IsEqualIID(shellGuid, guid_alt),
557               "%s: got GUID %s, expected %s or %s\n", getFolderName(folder),
558               printGUID(shellGuid, shellGuidStr), printGUID(guid, guidStr),
559               printGUID(guid_alt, guid_altStr));
560         }
561         IMalloc_Free(pMalloc, pidl);
562     }
563 }
564
565 /* Checks the PIDL type of all the known values. */
566 static void test_PidlTypes(void)
567 {
568     /* Desktop */
569     test_SHGetFolderPath(FALSE, CSIDL_DESKTOP);
570     test_SHGetSpecialFolderPath(FALSE, CSIDL_DESKTOP);
571
572     test_ShellValues(requiredShellValues, ARRAY_SIZE(requiredShellValues), FALSE);
573     test_ShellValues(optionalShellValues, ARRAY_SIZE(optionalShellValues), TRUE);
574 }
575
576 /* FIXME: Should be in shobjidl.idl */
577 DEFINE_GUID(CLSID_NetworkExplorerFolder, 0xF02C1A0D, 0xBE21, 0x4350, 0x88, 0xB0, 0x73, 0x67, 0xFC, 0x96, 0xEF, 0x3C);
578
579 /* Verifies various shell virtual folders have the correct well-known GUIDs. */
580 static void test_GUIDs(void)
581 {
582     matchGUID(CSIDL_BITBUCKET, &CLSID_RecycleBin, NULL);
583     matchGUID(CSIDL_CONTROLS, &CLSID_ControlPanel, NULL);
584     matchGUID(CSIDL_DRIVES, &CLSID_MyComputer, NULL);
585     matchGUID(CSIDL_INTERNET, &CLSID_Internet, NULL);
586     matchGUID(CSIDL_NETWORK, &CLSID_NetworkPlaces, &CLSID_NetworkExplorerFolder); /* Vista and higher */
587     matchGUID(CSIDL_PERSONAL, &CLSID_MyDocuments, NULL);
588     matchGUID(CSIDL_COMMON_DOCUMENTS, &CLSID_CommonDocuments, NULL);
589     matchGUID(CSIDL_PRINTERS, &CLSID_Printers, NULL);
590 }
591
592 /* Verifies various shell paths match the environment variables to which they
593  * correspond.
594  */
595 static void test_EnvVars(void)
596 {
597     matchSpecialFolderPathToEnv(CSIDL_PROGRAM_FILES, "ProgramFiles");
598     matchSpecialFolderPathToEnv(CSIDL_APPDATA, "APPDATA");
599     matchSpecialFolderPathToEnv(CSIDL_PROFILE, "USERPROFILE");
600     matchSpecialFolderPathToEnv(CSIDL_WINDOWS, "SystemRoot");
601     matchSpecialFolderPathToEnv(CSIDL_WINDOWS, "windir");
602     matchSpecialFolderPathToEnv(CSIDL_PROGRAM_FILES_COMMON, "CommonProgramFiles");
603     /* this is only set on Wine, but can't hurt to verify it: */
604     matchSpecialFolderPathToEnv(CSIDL_SYSTEM, "winsysdir");
605 }
606
607 /* Loosely based on PathRemoveBackslashA from dlls/shlwapi/path.c */
608 static BOOL myPathIsRootA(LPCSTR lpszPath)
609 {
610   if (lpszPath && *lpszPath &&
611       lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
612       return TRUE; /* X:\ */
613   return FALSE;
614 }
615 static LPSTR myPathRemoveBackslashA( LPSTR lpszPath )
616 {
617   LPSTR szTemp = NULL;
618
619   if(lpszPath)
620   {
621     szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
622     if (!myPathIsRootA(lpszPath) && *szTemp == '\\')
623       *szTemp = '\0';
624   }
625   return szTemp;
626 }
627
628 /* Verifies the shell path for CSIDL_WINDOWS matches the return from
629  * GetWindowsDirectory.  If SHGetSpecialFolderPath fails, no harm, no foul--not
630  * every shell32 version supports CSIDL_WINDOWS.
631  */
632 static void testWinDir(void)
633 {
634     char windowsShellPath[MAX_PATH], windowsDir[MAX_PATH] = { 0 };
635
636     if (!pSHGetSpecialFolderPathA) return;
637
638     if (pSHGetSpecialFolderPathA(NULL, windowsShellPath, CSIDL_WINDOWS, FALSE))
639     {
640         myPathRemoveBackslashA(windowsShellPath);
641         GetWindowsDirectoryA(windowsDir, sizeof(windowsDir));
642         myPathRemoveBackslashA(windowsDir);
643         ok(!lstrcmpiA(windowsDir, windowsShellPath),
644          "GetWindowsDirectory returns %s SHGetSpecialFolderPath returns %s\n",
645          windowsDir, windowsShellPath);
646     }
647 }
648
649 /* Verifies the shell path for CSIDL_SYSTEM matches the return from
650  * GetSystemDirectory.  If SHGetSpecialFolderPath fails, no harm,
651  * no foul--not every shell32 version supports CSIDL_SYSTEM.
652  */
653 static void testSystemDir(void)
654 {
655     char systemShellPath[MAX_PATH], systemDir[MAX_PATH], systemDirx86[MAX_PATH];
656
657     if (!pSHGetSpecialFolderPathA) return;
658
659     GetSystemDirectoryA(systemDir, sizeof(systemDir));
660     myPathRemoveBackslashA(systemDir);
661     if (pSHGetSpecialFolderPathA(NULL, systemShellPath, CSIDL_SYSTEM, FALSE))
662     {
663         myPathRemoveBackslashA(systemShellPath);
664         ok(!lstrcmpiA(systemDir, systemShellPath),
665          "GetSystemDirectory returns %s SHGetSpecialFolderPath returns %s\n",
666          systemDir, systemShellPath);
667     }
668
669     if (!pGetSystemWow64DirectoryA || !pGetSystemWow64DirectoryA(systemDirx86, sizeof(systemDirx86)))
670         GetSystemDirectoryA(systemDirx86, sizeof(systemDirx86));
671     myPathRemoveBackslashA(systemDirx86);
672     if (pSHGetSpecialFolderPathA(NULL, systemShellPath, CSIDL_SYSTEMX86, FALSE))
673     {
674         myPathRemoveBackslashA(systemShellPath);
675         ok(!lstrcmpiA(systemDirx86, systemShellPath) || broken(!lstrcmpiA(systemDir, systemShellPath)),
676          "GetSystemDirectory returns %s SHGetSpecialFolderPath returns %s\n",
677          systemDir, systemShellPath);
678     }
679 }
680
681 /* Globals used by subprocesses */
682 static int    myARGC;
683 static char **myARGV;
684 static char   base[MAX_PATH];
685 static char   selfname[MAX_PATH];
686
687 static int init(void)
688 {
689     myARGC = winetest_get_mainargs(&myARGV);
690     if (!GetCurrentDirectoryA(sizeof(base), base)) return 0;
691     strcpy(selfname, myARGV[0]);
692     return 1;
693 }
694
695 static void doChild(const char *arg)
696 {
697     char path[MAX_PATH];
698     HRESULT hr;
699
700     if (arg[0] == '1')
701     {
702         LPITEMIDLIST pidl;
703         char *p;
704
705         /* test what happens when CSIDL_FAVORITES is set to a nonexistent directory */
706
707         /* test some failure cases first: */
708         hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES, NULL, SHGFP_TYPE_CURRENT, path);
709         ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
710             "SHGetFolderPath returned 0x%08x, expected 0x80070002\n", hr);
711
712         pidl = NULL;
713         hr = pSHGetFolderLocation(NULL, CSIDL_FAVORITES, NULL, 0, &pidl);
714         ok(hr == E_FAIL || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
715             "SHGetFolderLocation returned 0x%08x\n", hr);
716         if (hr == S_OK && pidl) IMalloc_Free(pMalloc, pidl);
717
718         ok(!pSHGetSpecialFolderPathA(NULL, path, CSIDL_FAVORITES, FALSE),
719             "SHGetSpecialFolderPath succeeded, expected failure\n");
720
721         pidl = NULL;
722         hr = pSHGetSpecialFolderLocation(NULL, CSIDL_FAVORITES, &pidl);
723         ok(hr == E_FAIL || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
724             "SHGetFolderLocation returned 0x%08x\n", hr);
725
726         if (hr == S_OK && pidl) IMalloc_Free(pMalloc, pidl);
727
728         /* now test success: */
729         hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES | CSIDL_FLAG_CREATE, NULL,
730                                SHGFP_TYPE_CURRENT, path);
731         ok (hr == S_OK, "got 0x%08x\n", hr);
732         if (hr == S_OK)
733         {
734             BOOL ret;
735
736             trace("CSIDL_FAVORITES was changed to %s\n", path);
737             ret = CreateDirectoryA(path, NULL);
738             ok(!ret, "expected failure with ERROR_ALREADY_EXISTS\n");
739             if (!ret)
740                 ok(GetLastError() == ERROR_ALREADY_EXISTS,
741                   "got %d, expected ERROR_ALREADY_EXISTS\n", GetLastError());
742
743             p = path + strlen(path);
744             strcpy(p, "\\desktop.ini");
745             DeleteFileA(path);
746             *p = 0;
747             SetFileAttributesA( path, FILE_ATTRIBUTE_NORMAL );
748             ret = RemoveDirectoryA(path);
749             ok( ret, "failed to remove %s error %u\n", path, GetLastError() );
750         }
751     }
752     else if (arg[0] == '2')
753     {
754         /* make sure SHGetFolderPath still succeeds when the
755            original value of CSIDL_FAVORITES is restored. */
756         hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES | CSIDL_FLAG_CREATE, NULL,
757             SHGFP_TYPE_CURRENT, path);
758         ok(hr == S_OK, "SHGetFolderPath failed: 0x%08x\n", hr);
759     }
760 }
761
762 /* Tests the return values from the various shell functions both with and
763  * without the use of the CSIDL_FLAG_CREATE flag.  This flag only appeared in
764  * version 5 of the shell, so don't test unless it's at least version 5.
765  * The test reads a value from the registry, modifies it, calls
766  * SHGetFolderPath once with the CSIDL_FLAG_CREATE flag, and immediately
767  * afterward without it.  Then it restores the registry and deletes the folder
768  * that was created.
769  * One oddity with respect to restoration: shell32 caches somehow, so it needs
770  * to be reloaded in order to see the correct (restored) value.
771  * Some APIs unrelated to the ones under test may fail, but I expect they're
772  * covered by other unit tests; I just print out something about failure to
773  * help trace what's going on.
774  */
775 static void test_NonExistentPath(void)
776 {
777     static const char userShellFolders[] = 
778      "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders";
779     char originalPath[MAX_PATH], modifiedPath[MAX_PATH];
780     HKEY key;
781
782     if (!pSHGetFolderPathA) return;
783     if (!pSHGetFolderLocation) return;
784     if (!pSHGetSpecialFolderPathA) return;
785     if (!pSHGetSpecialFolderLocation) return;
786     if (!pSHFileOperationA) return;
787     if (shellVersion.dwMajorVersion < 5) return;
788
789     if (!RegOpenKeyExA(HKEY_CURRENT_USER, userShellFolders, 0, KEY_ALL_ACCESS,
790      &key))
791     {
792         DWORD len, type;
793
794         len = sizeof(originalPath);
795         if (!RegQueryValueExA(key, "Favorites", NULL, &type,
796          (LPBYTE)&originalPath, &len))
797         {
798             size_t len = strlen(originalPath);
799
800             memcpy(modifiedPath, originalPath, len);
801             modifiedPath[len++] = '2';
802             modifiedPath[len++] = '\0';
803             trace("Changing CSIDL_FAVORITES to %s\n", modifiedPath);
804             if (!RegSetValueExA(key, "Favorites", 0, type,
805              (LPBYTE)modifiedPath, len))
806             {
807                 char buffer[MAX_PATH+20];
808                 STARTUPINFOA startup;
809                 PROCESS_INFORMATION info;
810
811                 sprintf(buffer, "%s tests/shellpath.c 1", selfname);
812                 memset(&startup, 0, sizeof(startup));
813                 startup.cb = sizeof(startup);
814                 startup.dwFlags = STARTF_USESHOWWINDOW;
815                 startup.dwFlags = SW_SHOWNORMAL;
816                 CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL,
817                  &startup, &info);
818                 winetest_wait_child_process( info.hProcess );
819
820                 /* restore original values: */
821                 trace("Restoring CSIDL_FAVORITES to %s\n", originalPath);
822                 RegSetValueExA(key, "Favorites", 0, type, (LPBYTE) originalPath,
823                  strlen(originalPath) + 1);
824                 RegFlushKey(key);
825
826                 sprintf(buffer, "%s tests/shellpath.c 2", selfname);
827                 memset(&startup, 0, sizeof(startup));
828                 startup.cb = sizeof(startup);
829                 startup.dwFlags = STARTF_USESHOWWINDOW;
830                 startup.dwFlags = SW_SHOWNORMAL;
831                 CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL,
832                  &startup, &info);
833                 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0,
834                  "child process termination\n");
835             }
836         }
837         else skip("RegQueryValueExA(key, Favorites, ...) failed\n");
838         if (key)
839             RegCloseKey(key);
840     }
841     else skip("RegOpenKeyExA(HKEY_CURRENT_USER, %s, ...) failed\n", userShellFolders);
842 }
843
844 static void test_SHGetFolderPathEx(void)
845 {
846     HRESULT hr;
847     WCHAR buffer[MAX_PATH], *path;
848     DWORD len;
849
850     if (!pSHGetKnownFolderPath || !pSHGetFolderPathEx)
851     {
852         win_skip("SHGetKnownFolderPath or SHGetFolderPathEx not available\n");
853         return;
854     }
855
856 if (0) { /* crashes */
857     hr = pSHGetKnownFolderPath(&FOLDERID_Desktop, 0, NULL, NULL);
858     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
859 }
860     path = NULL;
861     hr = pSHGetKnownFolderPath(&FOLDERID_Desktop, 0, NULL, &path);
862     ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
863     ok(path != NULL, "expected path != NULL\n");
864
865     hr = pSHGetFolderPathEx(&FOLDERID_Desktop, 0, NULL, buffer, MAX_PATH);
866     ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
867     ok(!lstrcmpiW(path, buffer), "expected equal paths\n");
868     len = lstrlenW(buffer);
869     CoTaskMemFree(path);
870
871     hr = pSHGetFolderPathEx(&FOLDERID_Desktop, 0, NULL, buffer, 0);
872     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
873
874 if (0) { /* crashes */
875     hr = pSHGetFolderPathEx(&FOLDERID_Desktop, 0, NULL, NULL, len + 1);
876     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
877
878     hr = pSHGetFolderPathEx(NULL, 0, NULL, buffer, MAX_PATH);
879     ok(hr == E_INVALIDARG, "expected E_INVALIDARG, got 0x%08x\n", hr);
880 }
881     hr = pSHGetFolderPathEx(&FOLDERID_Desktop, 0, NULL, buffer, len);
882     ok(hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER), "expected 0x8007007a, got 0x%08x\n", hr);
883
884     hr = pSHGetFolderPathEx(&FOLDERID_Desktop, 0, NULL, buffer, len + 1);
885     ok(hr == S_OK, "expected S_OK, got 0x%08x\n", hr);
886 }
887
888 /* Standard CSIDL values (and their flags) uses only two less-significant bytes */
889 #define NO_CSIDL 0x10000
890 #define CSIDL_TODO_WINE 0x20000
891 #define KNOWN_FOLDER(id, csidl) \
892     { &id, # id, csidl, # csidl, __LINE__ }
893
894 struct knownFolderDef {
895     const KNOWNFOLDERID *folderId;
896     const char *sFolderId;
897     const int csidl;
898     const char *sCsidl;
899     const int line;
900 };
901
902 static const struct knownFolderDef known_folders[] = {
903     KNOWN_FOLDER(FOLDERID_AddNewPrograms,         NO_CSIDL),
904     KNOWN_FOLDER(FOLDERID_AddNewPrograms,         NO_CSIDL),
905     KNOWN_FOLDER(FOLDERID_AdminTools,             CSIDL_ADMINTOOLS),
906     KNOWN_FOLDER(FOLDERID_AppUpdates,             NO_CSIDL),
907     KNOWN_FOLDER(FOLDERID_CDBurning,              CSIDL_CDBURN_AREA),
908     KNOWN_FOLDER(FOLDERID_ChangeRemovePrograms,   NO_CSIDL),
909     KNOWN_FOLDER(FOLDERID_CommonAdminTools,       CSIDL_COMMON_ADMINTOOLS),
910     KNOWN_FOLDER(FOLDERID_CommonOEMLinks,         CSIDL_COMMON_OEM_LINKS),
911     KNOWN_FOLDER(FOLDERID_CommonPrograms,         CSIDL_COMMON_PROGRAMS),
912     KNOWN_FOLDER(FOLDERID_CommonStartMenu,        CSIDL_COMMON_STARTMENU),
913     KNOWN_FOLDER(FOLDERID_CommonStartup,          CSIDL_COMMON_STARTUP),
914     KNOWN_FOLDER(FOLDERID_CommonTemplates,        CSIDL_COMMON_TEMPLATES),
915     KNOWN_FOLDER(FOLDERID_ComputerFolder,         CSIDL_DRIVES),
916     KNOWN_FOLDER(FOLDERID_ConflictFolder,         NO_CSIDL),
917     KNOWN_FOLDER(FOLDERID_ConnectionsFolder,      CSIDL_CONNECTIONS),
918     KNOWN_FOLDER(FOLDERID_Contacts,               NO_CSIDL),
919     KNOWN_FOLDER(FOLDERID_ControlPanelFolder,     CSIDL_CONTROLS),
920     KNOWN_FOLDER(FOLDERID_Cookies,                CSIDL_COOKIES),
921     KNOWN_FOLDER(FOLDERID_Desktop,                CSIDL_DESKTOP),
922     KNOWN_FOLDER(FOLDERID_DeviceMetadataStore,    NO_CSIDL),
923     KNOWN_FOLDER(FOLDERID_Documents,              CSIDL_MYDOCUMENTS | CSIDL_TODO_WINE),
924     KNOWN_FOLDER(FOLDERID_DocumentsLibrary,       NO_CSIDL),
925     KNOWN_FOLDER(FOLDERID_Downloads,              NO_CSIDL),
926     KNOWN_FOLDER(FOLDERID_Favorites,              CSIDL_FAVORITES),
927     KNOWN_FOLDER(FOLDERID_Fonts,                  CSIDL_FONTS),
928     KNOWN_FOLDER(FOLDERID_Games,                  NO_CSIDL),
929     KNOWN_FOLDER(FOLDERID_GameTasks,              NO_CSIDL),
930     KNOWN_FOLDER(FOLDERID_History,                CSIDL_HISTORY),
931     KNOWN_FOLDER(FOLDERID_HomeGroup,              NO_CSIDL),
932     KNOWN_FOLDER(FOLDERID_ImplicitAppShortcuts,   NO_CSIDL),
933     KNOWN_FOLDER(FOLDERID_InternetCache,          CSIDL_INTERNET_CACHE),
934     KNOWN_FOLDER(FOLDERID_InternetFolder,         CSIDL_INTERNET),
935     KNOWN_FOLDER(FOLDERID_Libraries,              NO_CSIDL),
936     KNOWN_FOLDER(FOLDERID_Links,                  NO_CSIDL),
937     KNOWN_FOLDER(FOLDERID_LocalAppData,           CSIDL_LOCAL_APPDATA),
938     KNOWN_FOLDER(FOLDERID_LocalAppDataLow,        NO_CSIDL),
939     KNOWN_FOLDER(FOLDERID_LocalizedResourcesDir,  CSIDL_RESOURCES_LOCALIZED),
940     KNOWN_FOLDER(FOLDERID_Music,                  CSIDL_MYMUSIC),
941     KNOWN_FOLDER(FOLDERID_MusicLibrary,           NO_CSIDL),
942     KNOWN_FOLDER(FOLDERID_NetHood,                CSIDL_NETHOOD),
943     KNOWN_FOLDER(FOLDERID_NetworkFolder,          CSIDL_NETWORK),
944     KNOWN_FOLDER(FOLDERID_OriginalImages,         NO_CSIDL),
945     KNOWN_FOLDER(FOLDERID_PhotoAlbums,            NO_CSIDL),
946     KNOWN_FOLDER(FOLDERID_Pictures,               CSIDL_MYPICTURES),
947     KNOWN_FOLDER(FOLDERID_PicturesLibrary,        NO_CSIDL),
948     KNOWN_FOLDER(FOLDERID_Playlists,              NO_CSIDL),
949     KNOWN_FOLDER(FOLDERID_PrintersFolder,         CSIDL_PRINTERS),
950     KNOWN_FOLDER(FOLDERID_PrintHood,              CSIDL_PRINTHOOD),
951     KNOWN_FOLDER(FOLDERID_Profile,                CSIDL_PROFILE),
952     KNOWN_FOLDER(FOLDERID_ProgramData,            CSIDL_COMMON_APPDATA),
953     KNOWN_FOLDER(FOLDERID_ProgramFiles,           CSIDL_PROGRAM_FILES),
954     KNOWN_FOLDER(FOLDERID_ProgramFilesCommon,     CSIDL_PROGRAM_FILES_COMMON),
955     KNOWN_FOLDER(FOLDERID_ProgramFilesCommonX86,  NO_CSIDL),
956     KNOWN_FOLDER(FOLDERID_ProgramFilesX86,        CSIDL_PROGRAM_FILESX86),
957     KNOWN_FOLDER(FOLDERID_Programs,               CSIDL_PROGRAMS),
958     KNOWN_FOLDER(FOLDERID_Public,                 NO_CSIDL),
959     KNOWN_FOLDER(FOLDERID_PublicDesktop,          CSIDL_COMMON_DESKTOPDIRECTORY),
960     KNOWN_FOLDER(FOLDERID_PublicDocuments,        CSIDL_COMMON_DOCUMENTS),
961     KNOWN_FOLDER(FOLDERID_PublicDownloads,        NO_CSIDL),
962     KNOWN_FOLDER(FOLDERID_PublicGameTasks,        NO_CSIDL),
963     KNOWN_FOLDER(FOLDERID_PublicLibraries,        NO_CSIDL),
964     KNOWN_FOLDER(FOLDERID_PublicMusic,            CSIDL_COMMON_MUSIC),
965     KNOWN_FOLDER(FOLDERID_PublicPictures,         CSIDL_COMMON_PICTURES),
966     KNOWN_FOLDER(FOLDERID_PublicRingtones,        NO_CSIDL),
967     KNOWN_FOLDER(FOLDERID_PublicVideos,           CSIDL_COMMON_VIDEO),
968     KNOWN_FOLDER(FOLDERID_QuickLaunch,            NO_CSIDL),
969     KNOWN_FOLDER(FOLDERID_Recent,                 CSIDL_RECENT),
970     KNOWN_FOLDER(FOLDERID_RecordedTVLibrary,      NO_CSIDL),
971     KNOWN_FOLDER(FOLDERID_RecycleBinFolder,       CSIDL_BITBUCKET),
972     KNOWN_FOLDER(FOLDERID_ResourceDir,            CSIDL_RESOURCES),
973     KNOWN_FOLDER(FOLDERID_Ringtones,              NO_CSIDL),
974     KNOWN_FOLDER(FOLDERID_RoamingAppData,         CSIDL_APPDATA),
975     KNOWN_FOLDER(FOLDERID_SampleMusic,            NO_CSIDL),
976     KNOWN_FOLDER(FOLDERID_SamplePictures,         NO_CSIDL),
977     KNOWN_FOLDER(FOLDERID_SamplePlaylists,        NO_CSIDL),
978     KNOWN_FOLDER(FOLDERID_SampleVideos,           NO_CSIDL),
979     KNOWN_FOLDER(FOLDERID_SavedGames,             NO_CSIDL),
980     KNOWN_FOLDER(FOLDERID_SavedSearches,          NO_CSIDL),
981     KNOWN_FOLDER(FOLDERID_SEARCH_CSC,             NO_CSIDL),
982     KNOWN_FOLDER(FOLDERID_SearchHome,             NO_CSIDL),
983     KNOWN_FOLDER(FOLDERID_SEARCH_MAPI,            NO_CSIDL),
984     KNOWN_FOLDER(FOLDERID_SendTo,                 CSIDL_SENDTO),
985     KNOWN_FOLDER(FOLDERID_SidebarDefaultParts,    NO_CSIDL),
986     KNOWN_FOLDER(FOLDERID_SidebarParts,           NO_CSIDL),
987     KNOWN_FOLDER(FOLDERID_StartMenu,              CSIDL_STARTMENU),
988     KNOWN_FOLDER(FOLDERID_Startup,                CSIDL_STARTUP),
989     KNOWN_FOLDER(FOLDERID_SyncManagerFolder,      NO_CSIDL),
990     KNOWN_FOLDER(FOLDERID_SyncResultsFolder,      NO_CSIDL),
991     KNOWN_FOLDER(FOLDERID_SyncSetupFolder,        NO_CSIDL),
992     KNOWN_FOLDER(FOLDERID_System,                 CSIDL_SYSTEM),
993     KNOWN_FOLDER(FOLDERID_SystemX86,              CSIDL_SYSTEMX86),
994     KNOWN_FOLDER(FOLDERID_Templates,              CSIDL_TEMPLATES),
995     KNOWN_FOLDER(FOLDERID_UserPinned,             NO_CSIDL),
996     KNOWN_FOLDER(FOLDERID_UserProfiles,           NO_CSIDL),
997     KNOWN_FOLDER(FOLDERID_UserProgramFiles,       NO_CSIDL),
998     KNOWN_FOLDER(FOLDERID_UserProgramFilesCommon, NO_CSIDL),
999     KNOWN_FOLDER(FOLDERID_UsersFiles,             NO_CSIDL),
1000     KNOWN_FOLDER(FOLDERID_UsersLibraries,         NO_CSIDL),
1001     KNOWN_FOLDER(FOLDERID_Videos,                 CSIDL_MYVIDEO),
1002     KNOWN_FOLDER(FOLDERID_VideosLibrary,          NO_CSIDL),
1003     KNOWN_FOLDER(FOLDERID_Windows,                CSIDL_WINDOWS),
1004     { NULL, NULL, 0, NULL }
1005 };
1006 #undef KNOWN_FOLDER
1007
1008 static void check_known_folder(IKnownFolderManager *mgr, KNOWNFOLDERID *folderId)
1009 {
1010     HRESULT hr;
1011     const struct knownFolderDef *known_folder = &known_folders[0];
1012     int csidl, expectedCsidl;
1013
1014     while(known_folder->folderId != NULL)
1015     {
1016         if(IsEqualGUID(known_folder->folderId, folderId))
1017         {
1018             /* verify CSIDL */
1019             if(known_folder->csidl != NO_CSIDL)
1020             {
1021                 expectedCsidl = known_folder->csidl & (~CSIDL_TODO_WINE);
1022
1023                 hr = IKnownFolderManager_FolderIdToCsidl(mgr, folderId, &csidl);
1024                 ok_(__FILE__, known_folder->line)(hr == S_OK, "cannot retrieve CSIDL for folder %s\n", known_folder->sFolderId);
1025
1026                 if(known_folder->csidl & CSIDL_TODO_WINE)
1027                     todo_wine ok_(__FILE__, known_folder->line)(csidl == expectedCsidl, "invalid CSIDL retrieved for folder %s. %d (%s) expected, but %d found\n", known_folder->sFolderId, expectedCsidl, known_folder->sCsidl, csidl);
1028                 else
1029                     ok_(__FILE__, known_folder->line)(csidl == expectedCsidl, "invalid CSIDL retrieved for folder %s. %d (%s) expected, but %d found\n", known_folder->sFolderId, expectedCsidl, known_folder->sCsidl, csidl);
1030             }
1031
1032             break;
1033         }
1034         known_folder++;
1035     }
1036 }
1037 #undef NO_CSIDL
1038 #undef CSIDL_TODO_WINE
1039
1040 static void test_knownFolders(void)
1041 {
1042     static const WCHAR sWindows[] = {'W','i','n','d','o','w','s',0};
1043     static const WCHAR sExample[] = {'E','x','a','m','p','l','e',0};
1044     static const WCHAR sExample2[] = {'E','x','a','m','p','l','e','2',0};
1045     static const KNOWNFOLDERID newFolderId = {0x01234567, 0x89AB, 0xCDEF, {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x01} };
1046     HRESULT hr;
1047     IKnownFolderManager *mgr = NULL;
1048     IKnownFolder *folder = NULL;
1049     KNOWNFOLDERID folderId, *folders;
1050     KF_CATEGORY cat = 0;
1051     KNOWNFOLDER_DEFINITION kfDefinition;
1052     int csidl, i;
1053     UINT nCount = 0;
1054     LPWSTR folderPath;
1055     KF_REDIRECTION_CAPABILITIES redirectionCapabilities = 1;
1056     WCHAR sWinDir[MAX_PATH], sExamplePath[MAX_PATH], sExample2Path[MAX_PATH];
1057     BOOL bRes;
1058
1059     GetWindowsDirectoryW( sWinDir, MAX_PATH );
1060
1061     GetTempPathW(sizeof(sExamplePath)/sizeof(sExamplePath[0]), sExamplePath);
1062     lstrcatW(sExamplePath, sExample);
1063
1064     GetTempPathW(sizeof(sExample2Path)/sizeof(sExample2Path[0]), sExample2Path);
1065     lstrcatW(sExample2Path, sExample2);
1066
1067     CoInitialize(NULL);
1068
1069     hr = CoCreateInstance(&CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER,
1070                           &IID_IKnownFolderManager, (LPVOID*)&mgr);
1071     if(hr == REGDB_E_CLASSNOTREG)
1072         win_skip("IKnownFolderManager unavailable\n");
1073     else
1074     {
1075         ok(hr == S_OK, "failed to create KnownFolderManager instance: 0x%08x\n", hr);
1076
1077         hr = IKnownFolderManager_FolderIdFromCsidl(mgr, CSIDL_WINDOWS, &folderId);
1078         ok(hr == S_OK, "failed to convert CSIDL to KNOWNFOLDERID: 0x%08x\n", hr);
1079         ok(IsEqualGUID(&folderId, &FOLDERID_Windows)==TRUE, "invalid KNOWNFOLDERID returned\n");
1080
1081         hr = IKnownFolderManager_FolderIdToCsidl(mgr, &FOLDERID_Windows, &csidl);
1082         ok(hr == S_OK, "failed to convert CSIDL to KNOWNFOLDERID: 0x%08x\n", hr);
1083         ok(csidl == CSIDL_WINDOWS, "invalid CSIDL returned\n");
1084
1085         hr = IKnownFolderManager_GetFolder(mgr, &FOLDERID_Windows, &folder);
1086         ok(hr == S_OK, "failed to get known folder: 0x%08x\n", hr);
1087         if(SUCCEEDED(hr))
1088         {
1089             hr = IKnownFolder_GetCategory(folder, &cat);
1090             todo_wine
1091             ok(hr == S_OK, "failed to get folder category: 0x%08x\n", hr);
1092             todo_wine
1093             ok(cat==KF_CATEGORY_FIXED, "invalid folder category: %d\n", cat);
1094
1095             hr = IKnownFolder_GetId(folder, &folderId);
1096             ok(hr == S_OK, "failed to get folder id: 0x%08x\n", hr);
1097             ok(IsEqualGUID(&folderId, &FOLDERID_Windows)==TRUE, "invalid KNOWNFOLDERID returned\n");
1098
1099             hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1100             ok(lstrcmpiW(sWinDir, folderPath)==0, "invalid path returned: \"%s\", expected: \"%s\"\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sWinDir));
1101             CoTaskMemFree(folderPath);
1102
1103             hr = IKnownFolder_GetRedirectionCapabilities(folder, &redirectionCapabilities);
1104             todo_wine
1105             ok(hr == S_OK, "failed to get redirection capabilities: 0x%08x\n", hr);
1106             todo_wine
1107             ok(redirectionCapabilities==0, "invalid redirection capabilities returned: %d\n", redirectionCapabilities);
1108
1109             hr = IKnownFolder_SetPath(folder, 0, sWinDir);
1110             todo_wine
1111             ok(hr == E_INVALIDARG, "unexpected value from SetPath: 0x%08x\n", hr);
1112
1113             hr = IKnownFolder_GetFolderDefinition(folder, &kfDefinition);
1114             todo_wine
1115             ok(hr == S_OK, "failed to get folder definition: 0x%08x\n", hr);
1116             if(SUCCEEDED(hr))
1117             {
1118                 todo_wine
1119                 ok(kfDefinition.category==KF_CATEGORY_FIXED, "invalid folder category: 0x%08x\n", kfDefinition.category);
1120                 todo_wine
1121                 ok(lstrcmpW(kfDefinition.pszName, sWindows)==0, "invalid folder name: %s\n", wine_dbgstr_w(kfDefinition.pszName));
1122                 todo_wine
1123                 ok(kfDefinition.dwAttributes==0, "invalid folder attributes: %d\n", kfDefinition.dwAttributes);
1124                 FreeKnownFolderDefinitionFields(&kfDefinition);
1125             }
1126
1127             hr = IKnownFolder_Release(folder);
1128             ok(hr == S_OK, "failed to release KnownFolder instance: 0x%08x\n", hr);
1129         }
1130
1131         hr = IKnownFolderManager_GetFolderByName(mgr, sWindows, &folder);
1132         todo_wine
1133         ok(hr == S_OK, "failed to get known folder: 0x%08x\n", hr);
1134         if(SUCCEEDED(hr))
1135         {
1136             hr = IKnownFolder_GetId(folder, &folderId);
1137             ok(hr == S_OK, "failed to get folder id: 0x%08x\n", hr);
1138             ok(IsEqualGUID(&folderId, &FOLDERID_Windows)==TRUE, "invalid KNOWNFOLDERID returned\n");
1139
1140             hr = IKnownFolder_Release(folder);
1141             ok(hr == S_OK, "failed to release KnownFolder instance: 0x%08x\n", hr);
1142         }
1143
1144         hr = IKnownFolderManager_GetFolderIds(mgr, &folders, &nCount);
1145         ok(hr == S_OK, "failed to get known folders: 0x%08x\n", hr);
1146         for(i=0;i<nCount;++i)
1147             check_known_folder(mgr, &folders[i]);
1148
1149         CoTaskMemFree(folders);
1150
1151         /* test of registering new known folders */
1152         bRes = CreateDirectoryW(sExamplePath, NULL);
1153         ok(bRes, "cannot create example directory: %s\n", wine_dbgstr_w(sExamplePath));
1154         bRes = CreateDirectoryW(sExample2Path, NULL);
1155         ok(bRes, "cannot create example directory: %s\n", wine_dbgstr_w(sExamplePath));
1156
1157         ZeroMemory(&kfDefinition, sizeof(kfDefinition));
1158         kfDefinition.category = KF_CATEGORY_PERUSER;
1159         kfDefinition.pszName = CoTaskMemAlloc(sizeof(sExample));
1160         lstrcpyW(kfDefinition.pszName, sExample);
1161         kfDefinition.pszDescription = CoTaskMemAlloc(sizeof(sExample));
1162         lstrcpyW(kfDefinition.pszDescription, sExample);
1163         kfDefinition.pszRelativePath = CoTaskMemAlloc(sizeof(sExamplePath));
1164         lstrcpyW(kfDefinition.pszRelativePath, sExamplePath);
1165
1166         hr = IKnownFolderManager_RegisterFolder(mgr, &newFolderId, &kfDefinition);
1167         if(hr == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED))
1168             win_skip("No permissions required to register custom known folder\n");
1169         else
1170         {
1171             ok(hr == S_OK, "failed to register known folder: 0x%08x\n", hr);
1172             if(SUCCEEDED(hr))
1173             {
1174                 hr = IKnownFolderManager_GetFolder(mgr, &newFolderId, &folder);
1175                 ok(hr == S_OK, "failed to get known folder: 0x%08x\n", hr);
1176                 if(SUCCEEDED(hr))
1177                 {
1178                     hr = IKnownFolder_GetId(folder, &folderId);
1179                     ok(hr == S_OK, "failed to get folder id: 0x%08x\n", hr);
1180                     ok(IsEqualGUID(&folderId, &newFolderId)==TRUE, "invalid KNOWNFOLDERID returned\n");
1181
1182                     hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1183                     todo_wine
1184                     ok(hr == S_OK, "failed to get path from known folder: 0x%08x\n", hr);
1185                     todo_wine
1186                     ok(lstrcmpiW(folderPath, sExamplePath)==0, "invalid known folder path retreived: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExamplePath));
1187                     CoTaskMemFree(folderPath);
1188
1189                     /* try to set new path for folder */
1190                     hr = IKnownFolder_SetPath(folder, 0, sExample2Path);
1191                     todo_wine
1192                     ok(hr == S_OK, "setting path failed: 0x%0x\n", hr);
1193
1194                     /* verify modified path */
1195                     hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1196                     todo_wine
1197                     ok(hr == S_OK, "failed to get path from known folder: 0x%08x\n", hr);
1198                     todo_wine
1199                     ok(lstrcmpiW(folderPath, sExample2Path)==0, "invalid known folder path retreived: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExamplePath));
1200                     CoTaskMemFree(folderPath);
1201
1202                     /* set again to first path, but use another way */
1203                     hr = IKnownFolderManager_Redirect(mgr, &newFolderId, NULL, 0, sExamplePath, 0, NULL, NULL);
1204                     todo_wine
1205                     ok(hr == S_OK, "redirection failed: 0x%08x\n", hr);
1206
1207                     /* again verify modified path */
1208                     hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1209                     todo_wine
1210                     ok(hr == S_OK, "failed to get path from known folder: 0x%08x\n", hr);
1211                     todo_wine
1212                     ok(lstrcmpiW(folderPath, sExamplePath)==0, "invalid known folder path retreived: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExamplePath));
1213                     CoTaskMemFree(folderPath);
1214
1215                     /* check shell utility functions */
1216                     if(!pSHGetKnownFolderPath || !pSHSetKnownFolderPath)
1217                         todo_wine
1218                         win_skip("cannot get SHGet/SetKnownFolderPath routines\n");
1219                     else
1220                     {
1221                         /* check shell utility functions */
1222                         /* try to get current known folder path */
1223                         hr = pSHGetKnownFolderPath(&newFolderId, 0, NULL, &folderPath);
1224                         ok(hr==S_OK, "cannot get known folder path: hr=0x%0x\n", hr);
1225                         ok(lstrcmpW(folderPath, sExamplePath)==0, "invalid path returned: %s\n", wine_dbgstr_w(folderPath));
1226
1227                         /* set it to new value */
1228                         hr = pSHSetKnownFolderPath(&newFolderId, 0, NULL, sExample2Path);
1229                         ok(hr==S_OK, "cannot set known folder path: hr=0x%0x\n", hr);
1230
1231                         /* check if it changed */
1232                         hr = pSHGetKnownFolderPath(&newFolderId, 0, NULL, &folderPath);
1233                         ok(hr==S_OK, "cannot get known folder path: hr=0x%0x\n", hr);
1234                         ok(lstrcmpW(folderPath, sExample2Path)==0, "invalid path returned: %s\n", wine_dbgstr_w(folderPath));
1235                     }
1236
1237                     hr = IKnownFolder_Release(folder);
1238                     ok(hr == S_OK, "failed to release KnownFolder instance: 0x%08x\n", hr);
1239                 }
1240
1241                 hr = IKnownFolderManager_UnregisterFolder(mgr, &newFolderId);
1242                 ok(hr == S_OK, "failed to unregister folder: 0x%08x\n", hr);
1243             }
1244         }
1245         FreeKnownFolderDefinitionFields(&kfDefinition);
1246
1247         RemoveDirectoryW(sExamplePath);
1248         RemoveDirectoryW(sExample2Path);
1249
1250         hr = IKnownFolderManager_Release(mgr);
1251         ok(hr == S_OK, "failed to release KnownFolderManager instance: 0x%08x\n", hr);
1252     }
1253     CoUninitialize();
1254 }
1255
1256 START_TEST(shellpath)
1257 {
1258     if (!init()) return;
1259
1260     loadShell32();
1261     pGetSystemWow64DirectoryA = (void *)GetProcAddress( GetModuleHandleA("kernel32.dll"),
1262                                                         "GetSystemWow64DirectoryA" );
1263     if (myARGC >= 3)
1264         doChild(myARGV[2]);
1265     else
1266     {
1267         /* Report missing functions once */
1268         if (!pSHGetFolderLocation)
1269             win_skip("SHGetFolderLocation is not available\n");
1270
1271         /* first test various combinations of parameters: */
1272         test_parameters();
1273
1274         /* check known values: */
1275         test_PidlTypes();
1276         test_GUIDs();
1277         test_EnvVars();
1278         testWinDir();
1279         testSystemDir();
1280         test_NonExistentPath();
1281         test_SHGetFolderPathEx();
1282         test_knownFolders();
1283     }
1284 }