ieframe: Moved InternetShortcut implementation to ieframe.dll.
[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, name) \
892     { &id, # id, csidl, # csidl, name, __LINE__ }
893
894 struct knownFolderDef {
895     const KNOWNFOLDERID *folderId;
896     const char *sFolderId;
897     const int csidl;
898     const char *sCsidl;
899     const char *sName;
900     const int line;
901 };
902
903 static const struct knownFolderDef known_folders[] = {
904     KNOWN_FOLDER(FOLDERID_AddNewPrograms,             NO_CSIDL,                             "AddNewPrograms"),
905     KNOWN_FOLDER(FOLDERID_AdminTools,                 CSIDL_ADMINTOOLS,                     "Administrative Tools"),
906     KNOWN_FOLDER(FOLDERID_AppUpdates,                 NO_CSIDL,                             "AppUpdates"),
907     KNOWN_FOLDER(FOLDERID_CDBurning,                  CSIDL_CDBURN_AREA,                    "CD Burning"),
908     KNOWN_FOLDER(FOLDERID_ChangeRemovePrograms,       NO_CSIDL,                             "ChangeRemovePrograms"),
909     KNOWN_FOLDER(FOLDERID_CommonAdminTools,           CSIDL_COMMON_ADMINTOOLS,              "Common Administrative Tools"),
910     KNOWN_FOLDER(FOLDERID_CommonOEMLinks,             CSIDL_COMMON_OEM_LINKS,               "OEM Links"),
911     KNOWN_FOLDER(FOLDERID_CommonPrograms,             CSIDL_COMMON_PROGRAMS,                "Common Programs"),
912     KNOWN_FOLDER(FOLDERID_CommonStartMenu,            CSIDL_COMMON_STARTMENU,               "Common Start Menu"),
913     KNOWN_FOLDER(FOLDERID_CommonStartup,              CSIDL_COMMON_STARTUP,                 "Common Startup"),
914     KNOWN_FOLDER(FOLDERID_CommonTemplates,            CSIDL_COMMON_TEMPLATES,               "Common Templates"),
915     KNOWN_FOLDER(FOLDERID_ComputerFolder,             CSIDL_DRIVES,                         "MyComputerFolder"),
916     KNOWN_FOLDER(FOLDERID_ConflictFolder,             NO_CSIDL,                             "ConflictFolder"),
917     KNOWN_FOLDER(FOLDERID_ConnectionsFolder,          CSIDL_CONNECTIONS,                    "ConnectionsFolder"),
918     KNOWN_FOLDER(FOLDERID_Contacts,                   NO_CSIDL,                             "Contacts"),
919     KNOWN_FOLDER(FOLDERID_ControlPanelFolder,         CSIDL_CONTROLS,                       "ControlPanelFolder"),
920     KNOWN_FOLDER(FOLDERID_Cookies,                    CSIDL_COOKIES,                        "Cookies"),
921     KNOWN_FOLDER(FOLDERID_Desktop,                    CSIDL_DESKTOP,                        "Desktop"),
922     KNOWN_FOLDER(FOLDERID_DeviceMetadataStore,        NO_CSIDL,                             "DeviceMetadataStore"),
923     KNOWN_FOLDER(FOLDERID_Documents,                  CSIDL_MYDOCUMENTS,                    "Personal"),
924     KNOWN_FOLDER(FOLDERID_DocumentsLibrary,           NO_CSIDL,                             "DocumentsLibrary"),
925     KNOWN_FOLDER(FOLDERID_Downloads,                  NO_CSIDL,                             "Downloads"),
926     KNOWN_FOLDER(FOLDERID_Favorites,                  CSIDL_FAVORITES,                      "Favorites"),
927     KNOWN_FOLDER(FOLDERID_Fonts,                      CSIDL_FONTS,                          "Fonts"),
928     KNOWN_FOLDER(FOLDERID_Games,                      NO_CSIDL,                             "Games"),
929     KNOWN_FOLDER(FOLDERID_GameTasks,                  NO_CSIDL,                             "GameTasks"),
930     KNOWN_FOLDER(FOLDERID_History,                    CSIDL_HISTORY,                        "History"),
931     KNOWN_FOLDER(FOLDERID_HomeGroup,                  NO_CSIDL,                             "HomeGroup"),
932     KNOWN_FOLDER(FOLDERID_ImplicitAppShortcuts,       NO_CSIDL,                             "ImplicitAppShortcuts"),
933     KNOWN_FOLDER(FOLDERID_InternetCache,              CSIDL_INTERNET_CACHE,                 "Cache"),
934     KNOWN_FOLDER(FOLDERID_InternetFolder,             CSIDL_INTERNET,                       "InternetFolder"),
935     KNOWN_FOLDER(FOLDERID_Libraries,                  NO_CSIDL,                             "Libraries"),
936     KNOWN_FOLDER(FOLDERID_Links,                      NO_CSIDL,                             "Links"),
937     KNOWN_FOLDER(FOLDERID_LocalAppData,               CSIDL_LOCAL_APPDATA,                  "Local AppData"),
938     KNOWN_FOLDER(FOLDERID_LocalAppDataLow,            NO_CSIDL,                             "LocalAppDataLow"),
939     KNOWN_FOLDER(FOLDERID_LocalizedResourcesDir,      CSIDL_RESOURCES_LOCALIZED,            "LocalizedResourcesDir"),
940     KNOWN_FOLDER(FOLDERID_Music,                      CSIDL_MYMUSIC,                        "My Music"),
941     KNOWN_FOLDER(FOLDERID_MusicLibrary,               NO_CSIDL,                             "MusicLibrary"),
942     KNOWN_FOLDER(FOLDERID_NetHood,                    CSIDL_NETHOOD,                        "NetHood"),
943     KNOWN_FOLDER(FOLDERID_NetworkFolder,              CSIDL_NETWORK,                        "NetworkPlacesFolder"),
944     KNOWN_FOLDER(FOLDERID_OriginalImages,             NO_CSIDL,                             "OriginalImages"),
945     KNOWN_FOLDER(FOLDERID_PhotoAlbums,                NO_CSIDL,                             "PhotoAlbums"),
946     KNOWN_FOLDER(FOLDERID_Pictures,                   CSIDL_MYPICTURES,                     "My Pictures"),
947     KNOWN_FOLDER(FOLDERID_PicturesLibrary,            NO_CSIDL,                             "PicturesLibrary"),
948     KNOWN_FOLDER(FOLDERID_Playlists,                  NO_CSIDL,                             "Playlists"),
949     KNOWN_FOLDER(FOLDERID_PrintersFolder,             CSIDL_PRINTERS,                       "PrintersFolder"),
950     KNOWN_FOLDER(FOLDERID_PrintHood,                  CSIDL_PRINTHOOD,                      "PrintHood"),
951     KNOWN_FOLDER(FOLDERID_Profile,                    CSIDL_PROFILE,                        "Profile"),
952     KNOWN_FOLDER(FOLDERID_ProgramData,                CSIDL_COMMON_APPDATA,                 "Common AppData"),
953     KNOWN_FOLDER(FOLDERID_ProgramFiles,               CSIDL_PROGRAM_FILES,                  "ProgramFiles"),
954     KNOWN_FOLDER(FOLDERID_ProgramFilesCommon,         CSIDL_PROGRAM_FILES_COMMON,           "ProgramFilesCommon"),
955     KNOWN_FOLDER(FOLDERID_ProgramFilesCommonX86,      NO_CSIDL,                             "ProgramFilesCommonX86"),
956     KNOWN_FOLDER(FOLDERID_ProgramFilesX86,            CSIDL_PROGRAM_FILESX86,               "ProgramFilesX86"),
957     KNOWN_FOLDER(FOLDERID_Programs,                   CSIDL_PROGRAMS,                       "Programs"),
958     KNOWN_FOLDER(FOLDERID_Public,                     NO_CSIDL,                             "Public"),
959     KNOWN_FOLDER(FOLDERID_PublicDesktop,              CSIDL_COMMON_DESKTOPDIRECTORY,        "Common Desktop"),
960     KNOWN_FOLDER(FOLDERID_PublicDocuments,            CSIDL_COMMON_DOCUMENTS,               "Common Documents"),
961     KNOWN_FOLDER(FOLDERID_PublicDownloads,            NO_CSIDL,                             "PublicDownloads"),
962     KNOWN_FOLDER(FOLDERID_PublicGameTasks,            NO_CSIDL,                             "PublicGameTasks"),
963     KNOWN_FOLDER(FOLDERID_PublicLibraries,            NO_CSIDL,                             "PublicLibraries"),
964     KNOWN_FOLDER(FOLDERID_PublicMusic,                CSIDL_COMMON_MUSIC,                   "CommonMusic"),
965     KNOWN_FOLDER(FOLDERID_PublicPictures,             CSIDL_COMMON_PICTURES,                "CommonPictures"),
966     KNOWN_FOLDER(FOLDERID_PublicRingtones,            NO_CSIDL,                             "PublicRingtones"),
967     KNOWN_FOLDER(FOLDERID_PublicVideos,               CSIDL_COMMON_VIDEO,                   "CommonVideo"),
968     KNOWN_FOLDER(FOLDERID_QuickLaunch,                NO_CSIDL,                             "QuickLaunch"),
969     KNOWN_FOLDER(FOLDERID_Recent,                     CSIDL_RECENT,                         "Recent"),
970     KNOWN_FOLDER(FOLDERID_RecordedTVLibrary,          NO_CSIDL,                             "RecordedTVLibrary"),
971     KNOWN_FOLDER(FOLDERID_RecycleBinFolder,           CSIDL_BITBUCKET,                      "RecycleBinFolder"),
972     KNOWN_FOLDER(FOLDERID_ResourceDir,                CSIDL_RESOURCES,                      "ResourceDir"),
973     KNOWN_FOLDER(FOLDERID_Ringtones,                  NO_CSIDL,                             "Ringtones"),
974     KNOWN_FOLDER(FOLDERID_RoamingAppData,             CSIDL_APPDATA,                        "AppData"),
975     KNOWN_FOLDER(FOLDERID_SampleMusic,                NO_CSIDL,                             "SampleMusic"),
976     KNOWN_FOLDER(FOLDERID_SamplePictures,             NO_CSIDL,                             "SamplePictures"),
977     KNOWN_FOLDER(FOLDERID_SamplePlaylists,            NO_CSIDL,                             "SamplePlaylists"),
978     KNOWN_FOLDER(FOLDERID_SampleVideos,               NO_CSIDL,                             "SampleVideos"),
979     KNOWN_FOLDER(FOLDERID_SavedGames,                 NO_CSIDL,                             "SavedGames"),
980     KNOWN_FOLDER(FOLDERID_SavedSearches,              NO_CSIDL,                             "SavedSearches"),
981     KNOWN_FOLDER(FOLDERID_SEARCH_CSC,                 NO_CSIDL,                             "SEARCH_CSC"),
982     KNOWN_FOLDER(FOLDERID_SearchHome,                 NO_CSIDL,                             "SearchHome"),
983     KNOWN_FOLDER(FOLDERID_SEARCH_MAPI,                NO_CSIDL,                             "SEARCH_MAPI"),
984     KNOWN_FOLDER(FOLDERID_SendTo,                     CSIDL_SENDTO,                         "SendTo"),
985     KNOWN_FOLDER(FOLDERID_SidebarDefaultParts,        NO_CSIDL,                             "SidebarDefaultParts"),
986     KNOWN_FOLDER(FOLDERID_SidebarParts,               NO_CSIDL,                             "SidebarParts"),
987     KNOWN_FOLDER(FOLDERID_StartMenu,                  CSIDL_STARTMENU,                      "Start Menu"),
988     KNOWN_FOLDER(FOLDERID_Startup,                    CSIDL_STARTUP,                        "Startup"),
989     KNOWN_FOLDER(FOLDERID_SyncManagerFolder,          NO_CSIDL,                             "SyncManagerFolder"),
990     KNOWN_FOLDER(FOLDERID_SyncResultsFolder,          NO_CSIDL,                             "SyncResultsFolder"),
991     KNOWN_FOLDER(FOLDERID_SyncSetupFolder,            NO_CSIDL,                             "SyncSetupFolder"),
992     KNOWN_FOLDER(FOLDERID_System,                     CSIDL_SYSTEM,                         "System"),
993     KNOWN_FOLDER(FOLDERID_SystemX86,                  CSIDL_SYSTEMX86,                      "SystemX86"),
994     KNOWN_FOLDER(FOLDERID_Templates,                  CSIDL_TEMPLATES,                      "Templates"),
995     KNOWN_FOLDER(FOLDERID_UserPinned,                 NO_CSIDL,                             "UserPinned"),
996     KNOWN_FOLDER(FOLDERID_UserProfiles,               NO_CSIDL,                             "UserProfiles"),
997     KNOWN_FOLDER(FOLDERID_UserProgramFiles,           NO_CSIDL,                             "UserProgramFiles"),
998     KNOWN_FOLDER(FOLDERID_UserProgramFilesCommon,     NO_CSIDL,                             "UserProgramFilesCommon"),
999     KNOWN_FOLDER(FOLDERID_UsersFiles,                 NO_CSIDL,                             "UsersFiles"),
1000     KNOWN_FOLDER(FOLDERID_UsersLibraries,             NO_CSIDL,                             "UsersLibraries"),
1001     KNOWN_FOLDER(FOLDERID_Videos,                     CSIDL_MYVIDEO,                        "My Video"),
1002     KNOWN_FOLDER(FOLDERID_VideosLibrary,              NO_CSIDL,                             "VideosLibrary"),
1003     KNOWN_FOLDER(FOLDERID_Windows,                    CSIDL_WINDOWS,                        "Windows"),
1004     { NULL, NULL, 0, NULL, NULL, 0 }
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, ret;
1013     KNOWNFOLDER_DEFINITION kfd;
1014     IKnownFolder *folder;
1015     WCHAR sName[1024];
1016
1017     while(known_folder->folderId != NULL)
1018     {
1019         if(IsEqualGUID(known_folder->folderId, folderId))
1020         {
1021             /* verify CSIDL */
1022             if(known_folder->csidl != NO_CSIDL)
1023             {
1024                 expectedCsidl = known_folder->csidl & (~CSIDL_TODO_WINE);
1025
1026                 hr = IKnownFolderManager_FolderIdToCsidl(mgr, folderId, &csidl);
1027                 ok_(__FILE__, known_folder->line)(hr == S_OK, "cannot retrieve CSIDL for folder %s\n", known_folder->sFolderId);
1028
1029                 if(known_folder->csidl & CSIDL_TODO_WINE)
1030                     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);
1031                 else
1032                     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);
1033
1034                 hr = IKnownFolderManager_GetFolder(mgr, folderId, &folder);
1035                 ok_(__FILE__, known_folder->line)(hr == S_OK, "cannot get known folder for %s\n", known_folder->sFolderId);
1036                 if(SUCCEEDED(hr))
1037                 {
1038                     hr = IKnownFolder_GetFolderDefinition(folder, &kfd);
1039                     todo_wine
1040                     ok_(__FILE__, known_folder->line)(hr == S_OK, "cannot get known folder definition for %s\n", known_folder->sFolderId);
1041                     if(SUCCEEDED(hr))
1042                     {
1043                         ret = MultiByteToWideChar(CP_ACP, 0, known_folder->sName, -1,  sName, sizeof(sName)/sizeof(sName[0]));
1044                         ok_(__FILE__, known_folder->line)(ret != 0, "cannot convert known folder name \"%s\" to wide characters\n", known_folder->sName);
1045
1046                         todo_wine
1047                         ok_(__FILE__, known_folder->line)(lstrcmpW(kfd.pszName, sName)==0, "invalid known folder name returned for %s: %s expected, but %s retrieved\n", known_folder->sFolderId, wine_dbgstr_w(sName), wine_dbgstr_w(kfd.pszName));
1048
1049                         FreeKnownFolderDefinitionFields(&kfd);
1050                     }
1051
1052                 IKnownFolder_Release(folder);
1053                 }
1054             }
1055
1056             break;
1057         }
1058         known_folder++;
1059     }
1060 }
1061 #undef NO_CSIDL
1062 #undef CSIDL_TODO_WINE
1063
1064 static void test_knownFolders(void)
1065 {
1066     static const WCHAR sWindows[] = {'W','i','n','d','o','w','s',0};
1067     static const WCHAR sExample[] = {'E','x','a','m','p','l','e',0};
1068     static const WCHAR sExample2[] = {'E','x','a','m','p','l','e','2',0};
1069     static const WCHAR sSubFolder[] = {'S','u','b','F','o','l','d','e','r',0};
1070     static const WCHAR sBackslash[] = {'\\',0};
1071     static const KNOWNFOLDERID newFolderId = {0x01234567, 0x89AB, 0xCDEF, {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x01} };
1072     static const KNOWNFOLDERID subFolderId = {0xFEDCBA98, 0x7654, 0x3210, {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF} };
1073     HRESULT hr;
1074     IKnownFolderManager *mgr = NULL;
1075     IKnownFolder *folder = NULL, *subFolder = NULL;
1076     KNOWNFOLDERID folderId, *folders;
1077     KF_CATEGORY cat = 0;
1078     KNOWNFOLDER_DEFINITION kfDefinition, kfSubDefinition;
1079     int csidl, i;
1080     UINT nCount = 0;
1081     LPWSTR folderPath, errorMsg;
1082     KF_REDIRECTION_CAPABILITIES redirectionCapabilities = 1;
1083     WCHAR sWinDir[MAX_PATH], sExamplePath[MAX_PATH], sExample2Path[MAX_PATH], sSubFolderPath[MAX_PATH], sSubFolder2Path[MAX_PATH];
1084     BOOL bRes;
1085     DWORD dwAttributes;
1086
1087     GetWindowsDirectoryW( sWinDir, MAX_PATH );
1088
1089     GetTempPathW(sizeof(sExamplePath)/sizeof(sExamplePath[0]), sExamplePath);
1090     lstrcatW(sExamplePath, sExample);
1091
1092     GetTempPathW(sizeof(sExample2Path)/sizeof(sExample2Path[0]), sExample2Path);
1093     lstrcatW(sExample2Path, sExample2);
1094
1095     lstrcpyW(sSubFolderPath, sExamplePath);
1096     lstrcatW(sSubFolderPath, sBackslash);
1097     lstrcatW(sSubFolderPath, sSubFolder);
1098
1099     lstrcpyW(sSubFolder2Path, sExample2Path);
1100     lstrcatW(sSubFolder2Path, sBackslash);
1101     lstrcatW(sSubFolder2Path, sSubFolder);
1102
1103     CoInitialize(NULL);
1104
1105     hr = CoCreateInstance(&CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER,
1106                           &IID_IKnownFolderManager, (LPVOID*)&mgr);
1107     if(hr == REGDB_E_CLASSNOTREG)
1108         win_skip("IKnownFolderManager unavailable\n");
1109     else
1110     {
1111         ok(hr == S_OK, "failed to create KnownFolderManager instance: 0x%08x\n", hr);
1112
1113         hr = IKnownFolderManager_FolderIdFromCsidl(mgr, CSIDL_WINDOWS, &folderId);
1114         ok(hr == S_OK, "failed to convert CSIDL to KNOWNFOLDERID: 0x%08x\n", hr);
1115         ok(IsEqualGUID(&folderId, &FOLDERID_Windows)==TRUE, "invalid KNOWNFOLDERID returned\n");
1116
1117         hr = IKnownFolderManager_FolderIdToCsidl(mgr, &FOLDERID_Windows, &csidl);
1118         ok(hr == S_OK, "failed to convert CSIDL to KNOWNFOLDERID: 0x%08x\n", hr);
1119         ok(csidl == CSIDL_WINDOWS, "invalid CSIDL returned\n");
1120
1121         hr = IKnownFolderManager_GetFolder(mgr, &FOLDERID_Windows, &folder);
1122         ok(hr == S_OK, "failed to get known folder: 0x%08x\n", hr);
1123         if(SUCCEEDED(hr))
1124         {
1125             hr = IKnownFolder_GetCategory(folder, &cat);
1126             todo_wine
1127             ok(hr == S_OK, "failed to get folder category: 0x%08x\n", hr);
1128             todo_wine
1129             ok(cat==KF_CATEGORY_FIXED, "invalid folder category: %d\n", cat);
1130
1131             hr = IKnownFolder_GetId(folder, &folderId);
1132             ok(hr == S_OK, "failed to get folder id: 0x%08x\n", hr);
1133             ok(IsEqualGUID(&folderId, &FOLDERID_Windows)==TRUE, "invalid KNOWNFOLDERID returned\n");
1134
1135             hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1136             ok(lstrcmpiW(sWinDir, folderPath)==0, "invalid path returned: \"%s\", expected: \"%s\"\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sWinDir));
1137             CoTaskMemFree(folderPath);
1138
1139             hr = IKnownFolder_GetRedirectionCapabilities(folder, &redirectionCapabilities);
1140             todo_wine
1141             ok(hr == S_OK, "failed to get redirection capabilities: 0x%08x\n", hr);
1142             todo_wine
1143             ok(redirectionCapabilities==0, "invalid redirection capabilities returned: %d\n", redirectionCapabilities);
1144
1145             hr = IKnownFolder_SetPath(folder, 0, sWinDir);
1146             todo_wine
1147             ok(hr == E_INVALIDARG, "unexpected value from SetPath: 0x%08x\n", hr);
1148
1149             hr = IKnownFolder_GetFolderDefinition(folder, &kfDefinition);
1150             todo_wine
1151             ok(hr == S_OK, "failed to get folder definition: 0x%08x\n", hr);
1152             if(SUCCEEDED(hr))
1153             {
1154                 todo_wine
1155                 ok(kfDefinition.category==KF_CATEGORY_FIXED, "invalid folder category: 0x%08x\n", kfDefinition.category);
1156                 todo_wine
1157                 ok(lstrcmpW(kfDefinition.pszName, sWindows)==0, "invalid folder name: %s\n", wine_dbgstr_w(kfDefinition.pszName));
1158                 todo_wine
1159                 ok(kfDefinition.dwAttributes==0, "invalid folder attributes: %d\n", kfDefinition.dwAttributes);
1160                 FreeKnownFolderDefinitionFields(&kfDefinition);
1161             }
1162
1163             hr = IKnownFolder_Release(folder);
1164             ok(hr == S_OK, "failed to release KnownFolder instance: 0x%08x\n", hr);
1165         }
1166
1167         hr = IKnownFolderManager_GetFolderByName(mgr, sWindows, &folder);
1168         todo_wine
1169         ok(hr == S_OK, "failed to get known folder: 0x%08x\n", hr);
1170         if(SUCCEEDED(hr))
1171         {
1172             hr = IKnownFolder_GetId(folder, &folderId);
1173             ok(hr == S_OK, "failed to get folder id: 0x%08x\n", hr);
1174             ok(IsEqualGUID(&folderId, &FOLDERID_Windows)==TRUE, "invalid KNOWNFOLDERID returned\n");
1175
1176             hr = IKnownFolder_Release(folder);
1177             ok(hr == S_OK, "failed to release KnownFolder instance: 0x%08x\n", hr);
1178         }
1179
1180         hr = IKnownFolderManager_GetFolderIds(mgr, &folders, &nCount);
1181         ok(hr == S_OK, "failed to get known folders: 0x%08x\n", hr);
1182         for(i=0;i<nCount;++i)
1183             check_known_folder(mgr, &folders[i]);
1184
1185         CoTaskMemFree(folders);
1186
1187         /* test of registering new known folders */
1188         bRes = CreateDirectoryW(sExamplePath, NULL);
1189         ok(bRes, "cannot create example directory: %s\n", wine_dbgstr_w(sExamplePath));
1190         bRes = CreateDirectoryW(sExample2Path, NULL);
1191         ok(bRes, "cannot create example directory: %s\n", wine_dbgstr_w(sExample2Path));
1192         bRes = CreateDirectoryW(sSubFolderPath, NULL);
1193         ok(bRes, "cannot create example directory: %s\n", wine_dbgstr_w(sSubFolderPath));
1194
1195         ZeroMemory(&kfDefinition, sizeof(kfDefinition));
1196         kfDefinition.category = KF_CATEGORY_PERUSER;
1197         kfDefinition.pszName = CoTaskMemAlloc(sizeof(sExample));
1198         lstrcpyW(kfDefinition.pszName, sExample);
1199         kfDefinition.pszDescription = CoTaskMemAlloc(sizeof(sExample));
1200         lstrcpyW(kfDefinition.pszDescription, sExample);
1201         kfDefinition.pszRelativePath = CoTaskMemAlloc(sizeof(sExamplePath));
1202         lstrcpyW(kfDefinition.pszRelativePath, sExamplePath);
1203
1204         hr = IKnownFolderManager_RegisterFolder(mgr, &newFolderId, &kfDefinition);
1205         if(hr == HRESULT_FROM_WIN32(ERROR_ACCESS_DENIED))
1206             win_skip("No permissions required to register custom known folder\n");
1207         else
1208         {
1209             ok(hr == S_OK, "failed to register known folder: 0x%08x\n", hr);
1210             if(SUCCEEDED(hr))
1211             {
1212                 hr = IKnownFolderManager_GetFolder(mgr, &newFolderId, &folder);
1213                 ok(hr == S_OK, "failed to get known folder: 0x%08x\n", hr);
1214                 if(SUCCEEDED(hr))
1215                 {
1216                     hr = IKnownFolder_GetCategory(folder, &cat);
1217                     ok(hr == S_OK, "failed to get folder category: hr=0x%0x\n", hr);
1218                     ok(cat == KF_CATEGORY_PERUSER, "invalid category returned: %d, while %d (KF_CATEGORY_PERUSER) expected\n", cat, KF_CATEGORY_PERUSER);
1219
1220                     hr = IKnownFolder_GetId(folder, &folderId);
1221                     ok(hr == S_OK, "failed to get folder id: 0x%08x\n", hr);
1222                     ok(IsEqualGUID(&folderId, &newFolderId)==TRUE, "invalid KNOWNFOLDERID returned\n");
1223
1224                     /* current path should be Temp\Example */
1225                     hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1226                     ok(hr == S_OK, "failed to get path from known folder: 0x%08x\n", hr);
1227                     ok(lstrcmpiW(folderPath, sExamplePath)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExamplePath));
1228                     CoTaskMemFree(folderPath);
1229
1230                     /* register sub-folder and mark it as child of Example folder */
1231                     ZeroMemory(&kfSubDefinition, sizeof(kfSubDefinition));
1232                     kfSubDefinition.category = KF_CATEGORY_PERUSER;
1233                     kfSubDefinition.pszName = CoTaskMemAlloc(sizeof(sSubFolder));
1234                     lstrcpyW(kfSubDefinition.pszName, sSubFolder);
1235                     kfSubDefinition.pszDescription = CoTaskMemAlloc(sizeof(sSubFolder));
1236                     lstrcpyW(kfSubDefinition.pszDescription, sSubFolder);
1237                     kfSubDefinition.pszRelativePath = CoTaskMemAlloc(sizeof(sSubFolder));
1238                     lstrcpyW(kfSubDefinition.pszRelativePath, sSubFolder);
1239                     kfSubDefinition.fidParent = newFolderId;
1240
1241                     hr = IKnownFolderManager_RegisterFolder(mgr, &subFolderId, &kfSubDefinition);
1242                     ok(hr == S_OK, "failed to register known folder: 0x%08x\n", hr);
1243                     if(SUCCEEDED(hr))
1244                     {
1245
1246                         hr = IKnownFolderManager_GetFolder(mgr, &subFolderId, &subFolder);
1247                         ok(hr == S_OK, "failed to get known folder: 0x%08x\n", hr);
1248                         if(SUCCEEDED(hr))
1249                         {
1250                             /* check sub folder path */
1251                             hr = IKnownFolder_GetPath(subFolder, 0, &folderPath);
1252                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1253                             ok(lstrcmpiW(folderPath, sSubFolderPath)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sSubFolderPath));
1254                             CoTaskMemFree(folderPath);
1255
1256
1257                             /* try to redirect Example to Temp\Example2  */
1258                             hr = IKnownFolderManager_Redirect(mgr, &newFolderId, NULL, 0, sExample2Path, 0, NULL, &errorMsg);
1259                             ok(hr == S_OK, "failed to redirect known folder: 0x%08x, errorMsg: %s\n", hr, wine_dbgstr_w(errorMsg));
1260
1261                             /* verify */
1262                             hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1263                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1264                             ok(lstrcmpiW(folderPath, sExample2Path)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExample2Path));
1265                             CoTaskMemFree(folderPath);
1266
1267                             /* verify sub folder - it should fail now, as we redirected it's parent folder, but we have no sub folder in new location */
1268                             hr = IKnownFolder_GetPath(subFolder, 0, &folderPath);
1269                             ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "unexpected value from GetPath(): 0x%08x\n", hr);
1270                             ok(folderPath==NULL, "invalid known folder path retrieved: \"%s\" when NULL pointer was expected\n", wine_dbgstr_w(folderPath));
1271                             CoTaskMemFree(folderPath);
1272
1273
1274                             /* set Example path to original. Using SetPath() is valid here, as it also uses redirection internally */
1275                             hr = IKnownFolder_SetPath(folder, 0, sExamplePath);
1276                             ok(hr == S_OK, "SetPath() failed: 0x%08x\n", hr);
1277
1278                             /* verify */
1279                             hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1280                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1281                             ok(lstrcmpiW(folderPath, sExamplePath)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExamplePath));
1282                             CoTaskMemFree(folderPath);
1283
1284
1285                             /* create sub folder in Temp\Example2 */
1286                             bRes = CreateDirectoryW(sSubFolder2Path, NULL);
1287                             ok(bRes, "cannot create example directory: %s\n", wine_dbgstr_w(sSubFolder2Path));
1288
1289                             /* again perform that same redirection */
1290                             hr = IKnownFolderManager_Redirect(mgr, &newFolderId, NULL, 0, sExample2Path, 0, NULL, &errorMsg);
1291                             ok(hr == S_OK, "failed to redirect known folder: 0x%08x, errorMsg: %s\n", hr, wine_dbgstr_w(errorMsg));
1292
1293                             /* verify sub folder. It should succeed now, as the required sub folder exists */
1294                             hr = IKnownFolder_GetPath(subFolder, 0, &folderPath);
1295                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1296                             ok(lstrcmpiW(folderPath, sSubFolder2Path)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sSubFolder2Path));
1297                             CoTaskMemFree(folderPath);
1298
1299                             /* remove newly created directory */
1300                             RemoveDirectoryW(sSubFolder2Path);
1301
1302                             /* verify sub folder. It still succeedes, so Windows does not check folder presence each time */
1303                             hr = IKnownFolder_GetPath(subFolder, 0, &folderPath);
1304                             todo_wine
1305                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1306                             todo_wine
1307                             ok(lstrcmpiW(folderPath, sSubFolder2Path)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sSubFolder2Path));
1308                             CoTaskMemFree(folderPath);
1309
1310
1311                             /* set Example path to original */
1312                             hr = IKnownFolder_SetPath(folder, 0, sExamplePath);
1313                             ok(hr == S_OK, "SetPath() failed: 0x%08x\n", hr);
1314
1315                             /* verify */
1316                             hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1317                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1318                             ok(lstrcmpiW(folderPath, sExamplePath)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExamplePath));
1319                             CoTaskMemFree(folderPath);
1320
1321                             /* verify sub folder */
1322                             hr = IKnownFolder_GetPath(subFolder, 0, &folderPath);
1323                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1324                             ok(lstrcmpiW(folderPath, sSubFolderPath)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sSubFolderPath));
1325                             CoTaskMemFree(folderPath);
1326
1327
1328                             /* create sub folder in Temp\Example2 */
1329                             bRes = CreateDirectoryW(sSubFolder2Path, NULL);
1330                             ok(bRes, "cannot create example directory: %s\n", wine_dbgstr_w(sSubFolder2Path));
1331
1332                             /* do that same redirection, but try to exclude sub-folder */
1333                             hr = IKnownFolderManager_Redirect(mgr, &newFolderId, NULL, 0, sExample2Path, 1, &subFolderId, &errorMsg);
1334                             ok(hr == S_OK, "failed to redirect known folder: 0x%08x, errorMsg: %s\n", hr, wine_dbgstr_w(errorMsg));
1335
1336                             /* verify */
1337                             hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1338                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1339                             ok(lstrcmpiW(folderPath, sExample2Path)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExample2Path));
1340                             CoTaskMemFree(folderPath);
1341
1342                             /* verify sub folder. Unexpectedly, this path was also changed. So, exclusion seems to be ignored (Windows bug)? This test however will let us know, if this behavior is changed */
1343                             hr = IKnownFolder_GetPath(subFolder, 0, &folderPath);
1344                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1345                             ok(lstrcmpiW(folderPath, sSubFolder2Path)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sSubFolder2Path));
1346                             CoTaskMemFree(folderPath);
1347
1348                             /* remove newly created directory */
1349                             RemoveDirectoryW(sSubFolder2Path);
1350
1351
1352                             /* set Example path to original */
1353                             hr = IKnownFolder_SetPath(folder, 0, sExamplePath);
1354                             ok(hr == S_OK, "SetPath() failed: 0x%08x\n", hr);
1355
1356                             /* verify */
1357                             hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1358                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1359                             ok(lstrcmpiW(folderPath, sExamplePath)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExamplePath));
1360                             CoTaskMemFree(folderPath);
1361
1362                             /* verify sub folder */
1363                             hr = IKnownFolder_GetPath(subFolder, 0, &folderPath);
1364                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1365                             ok(lstrcmpiW(folderPath, sSubFolderPath)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sSubFolderPath));
1366                             CoTaskMemFree(folderPath);
1367
1368
1369                             /* do that same redirection again, but set it to copy content. It should also copy the sub folder, so checking it would succeed now */
1370                             hr = IKnownFolderManager_Redirect(mgr, &newFolderId, NULL, KF_REDIRECT_COPY_CONTENTS, sExample2Path, 0, NULL, &errorMsg);
1371                             ok(hr == S_OK, "failed to redirect known folder: 0x%08x, errorMsg: %s\n", hr, wine_dbgstr_w(errorMsg));
1372
1373                             /* verify */
1374                             hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1375                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1376                             ok(lstrcmpiW(folderPath, sExample2Path)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExample2Path));
1377                             CoTaskMemFree(folderPath);
1378
1379                             /* verify sub folder */
1380                             hr = IKnownFolder_GetPath(subFolder, 0, &folderPath);
1381                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1382                             ok(lstrcmpiW(folderPath, sSubFolder2Path)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sSubFolder2Path));
1383                             CoTaskMemFree(folderPath);
1384
1385                             /* remove copied directory */
1386                             RemoveDirectoryW(sSubFolder2Path);
1387
1388
1389                             /* set Example path to original */
1390                             hr = IKnownFolder_SetPath(folder, 0, sExamplePath);
1391                             ok(hr == S_OK, "SetPath() failed: 0x%08x\n", hr);
1392
1393                             /* verify */
1394                             hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1395                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1396                             ok(lstrcmpiW(folderPath, sExamplePath)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExamplePath));
1397                             CoTaskMemFree(folderPath);
1398
1399                             /* verify sub folder */
1400                             hr = IKnownFolder_GetPath(subFolder, 0, &folderPath);
1401                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1402                             ok(lstrcmpiW(folderPath, sSubFolderPath)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sSubFolderPath));
1403                             CoTaskMemFree(folderPath);
1404
1405
1406                             /* redirect again, set it to copy content and remove originals */
1407                             hr = IKnownFolderManager_Redirect(mgr, &newFolderId, NULL, KF_REDIRECT_COPY_CONTENTS | KF_REDIRECT_DEL_SOURCE_CONTENTS, sExample2Path, 0, NULL, &errorMsg);
1408                             ok(hr == S_OK, "failed to redirect known folder: 0x%08x, errorMsg: %s\n", hr, wine_dbgstr_w(errorMsg));
1409
1410                             /* verify */
1411                             hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1412                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1413                             ok(lstrcmpiW(folderPath, sExample2Path)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExample2Path));
1414                             CoTaskMemFree(folderPath);
1415
1416                             /* verify sub folder */
1417                             hr = IKnownFolder_GetPath(subFolder, 0, &folderPath);
1418                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1419                             ok(lstrcmpiW(folderPath, sSubFolder2Path)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sSubFolder2Path));
1420                             CoTaskMemFree(folderPath);
1421
1422                             /* check if original directory was really removed */
1423                             dwAttributes = GetFileAttributesW(sExamplePath);
1424                             ok(dwAttributes==INVALID_FILE_ATTRIBUTES, "directory should not exist, but has attributes: 0x%08x\n", dwAttributes );
1425
1426
1427                             /* redirect (with copy) to original path */
1428                             hr = IKnownFolderManager_Redirect(mgr, &newFolderId, NULL, KF_REDIRECT_COPY_CONTENTS,  sExamplePath, 0, NULL, &errorMsg);
1429                             ok(hr == S_OK, "failed to redirect known folder: 0x%08x, errorMsg: %s\n", hr, wine_dbgstr_w(errorMsg));
1430
1431                             /* verify */
1432                             hr = IKnownFolder_GetPath(folder, 0, &folderPath);
1433                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1434                             ok(lstrcmpiW(folderPath, sExamplePath)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sExamplePath));
1435                             CoTaskMemFree(folderPath);
1436
1437                             /* verify sub folder */
1438                             hr = IKnownFolder_GetPath(subFolder, 0, &folderPath);
1439                             ok(hr == S_OK, "failed to get known folder path: 0x%08x\n", hr);
1440                             ok(lstrcmpiW(folderPath, sSubFolderPath)==0, "invalid known folder path retrieved: \"%s\" when \"%s\" was expected\n", wine_dbgstr_w(folderPath), wine_dbgstr_w(sSubFolderPath));
1441                             CoTaskMemFree(folderPath);
1442
1443                             /* check shell utility functions */
1444                             if(!pSHGetKnownFolderPath || !pSHSetKnownFolderPath)
1445                                 todo_wine
1446                                 win_skip("cannot get SHGet/SetKnownFolderPath routines\n");
1447                             else
1448                             {
1449                                 /* try to get current known folder path */
1450                                 hr = pSHGetKnownFolderPath(&newFolderId, 0, NULL, &folderPath);
1451                                 todo_wine
1452                                 ok(hr==S_OK, "cannot get known folder path: hr=0x%0x\n", hr);
1453                                 todo_wine
1454                                 ok(lstrcmpW(folderPath, sExamplePath)==0, "invalid path returned: %s\n", wine_dbgstr_w(folderPath));
1455
1456                                 /* set it to new value */
1457                                 hr = pSHSetKnownFolderPath(&newFolderId, 0, NULL, sExample2Path);
1458                                 todo_wine
1459                                 ok(hr==S_OK, "cannot set known folder path: hr=0x%0x\n", hr);
1460
1461                                 /* check if it changed */
1462                                 hr = pSHGetKnownFolderPath(&newFolderId, 0, NULL, &folderPath);
1463                                 todo_wine
1464                                 ok(hr==S_OK, "cannot get known folder path: hr=0x%0x\n", hr);
1465                                 todo_wine
1466                                 ok(lstrcmpW(folderPath, sExample2Path)==0, "invalid path returned: %s\n", wine_dbgstr_w(folderPath));
1467
1468                                 /* set it back */
1469                                 hr = pSHSetKnownFolderPath(&newFolderId, 0, NULL, sExamplePath);
1470                                 todo_wine
1471                                 ok(hr==S_OK, "cannot set known folder path: hr=0x%0x\n", hr);
1472                             }
1473
1474                             IKnownFolder_Release(subFolder);
1475                         }
1476
1477                         hr = IKnownFolderManager_UnregisterFolder(mgr, &subFolderId);
1478                         ok(hr == S_OK, "failed to unregister folder: 0x%08x\n", hr);
1479                     }
1480
1481                     FreeKnownFolderDefinitionFields(&kfSubDefinition);
1482
1483                     hr = IKnownFolder_Release(folder);
1484                     ok(hr == S_OK, "failed to release KnownFolder instance: 0x%08x\n", hr);
1485                 }
1486
1487                 hr = IKnownFolderManager_UnregisterFolder(mgr, &newFolderId);
1488                 ok(hr == S_OK, "failed to unregister folder: 0x%08x\n", hr);
1489             }
1490         }
1491         FreeKnownFolderDefinitionFields(&kfDefinition);
1492
1493         RemoveDirectoryW(sSubFolder2Path);
1494         RemoveDirectoryW(sSubFolderPath);
1495         RemoveDirectoryW(sExamplePath);
1496         RemoveDirectoryW(sExample2Path);
1497
1498         hr = IKnownFolderManager_Release(mgr);
1499         ok(hr == S_OK, "failed to release KnownFolderManager instance: 0x%08x\n", hr);
1500     }
1501     CoUninitialize();
1502 }
1503
1504 START_TEST(shellpath)
1505 {
1506     if (!init()) return;
1507
1508     loadShell32();
1509     pGetSystemWow64DirectoryA = (void *)GetProcAddress( GetModuleHandleA("kernel32.dll"),
1510                                                         "GetSystemWow64DirectoryA" );
1511     if (myARGC >= 3)
1512         doChild(myARGV[2]);
1513     else
1514     {
1515         /* Report missing functions once */
1516         if (!pSHGetFolderLocation)
1517             win_skip("SHGetFolderLocation is not available\n");
1518
1519         /* first test various combinations of parameters: */
1520         test_parameters();
1521
1522         /* check known values: */
1523         test_PidlTypes();
1524         test_GUIDs();
1525         test_EnvVars();
1526         testWinDir();
1527         testSystemDir();
1528         test_NonExistentPath();
1529         test_SHGetFolderPathEx();
1530         test_knownFolders();
1531     }
1532 }