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