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