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