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