push 378fe7a60681a28e8b22f62dcfe122d585b92570
[wine/hacks.git] / dlls / shell32 / tests / shellpath.c
blob7c7cc524f45c35cff6f190c11166982da0bfa8b6
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;
79 BYTE altPidlType;
82 static HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO *);
83 static HRESULT (WINAPI *pSHGetFolderPathA)(HWND, int, HANDLE, DWORD, LPSTR);
84 static HRESULT (WINAPI *pSHGetFolderLocation)(HWND, int, HANDLE, DWORD,
85 LPITEMIDLIST *);
86 static BOOL (WINAPI *pSHGetSpecialFolderPathA)(HWND, LPSTR, int, BOOL);
87 static HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
88 static LPITEMIDLIST (WINAPI *pILFindLastID)(LPCITEMIDLIST);
89 static int (WINAPI *pSHFileOperationA)(LPSHFILEOPSTRUCTA);
90 static HRESULT (WINAPI *pSHGetMalloc)(LPMALLOC *);
91 static DLLVERSIONINFO shellVersion = { 0 };
92 static LPMALLOC pMalloc;
93 static const struct shellExpectedValues requiredShellValues[] = {
94 { CSIDL_BITBUCKET, PT_GUID, 0 },
95 { CSIDL_CONTROLS, PT_SHELLEXT, PT_GUID },
96 { CSIDL_COOKIES, PT_FOLDER, 0 },
97 { CSIDL_DESKTOPDIRECTORY, PT_FOLDER, 0 },
98 { CSIDL_DRIVES, PT_GUID, 0 },
99 /* Note that 0 is an expected type for CSIDL_FAVORITES. Inverting the order
100 * will cause the test to fail, as it'll only check for PT_FOLDER.
102 { CSIDL_FAVORITES, 0, PT_FOLDER },
103 { CSIDL_FONTS, PT_FOLDER, PT_IESPECIAL2 },
104 /* FIXME: the following fails in Wine, returns type PT_FOLDER
105 { CSIDL_HISTORY, PT_IESPECIAL2, 0 },
107 { CSIDL_INTERNET, PT_GUID, 0 },
108 { CSIDL_NETHOOD, PT_FOLDER, 0 },
109 { CSIDL_NETWORK, PT_GUID, 0 },
110 /* FIXME: don't know the type of 0x71 returned by Vista/2008 for printers */
111 { CSIDL_PRINTERS, PT_YAGUID, 0x71 },
112 { CSIDL_PRINTHOOD, PT_FOLDER, 0 },
113 { CSIDL_PROGRAMS, PT_FOLDER, 0 },
114 { CSIDL_RECENT, PT_FOLDER, PT_IESPECIAL2 },
115 { CSIDL_SENDTO, PT_FOLDER, 0 },
116 { CSIDL_STARTMENU, PT_FOLDER, 0 },
117 { CSIDL_STARTUP, PT_FOLDER, 0 },
118 { CSIDL_TEMPLATES, PT_FOLDER, 0 },
120 static const struct shellExpectedValues optionalShellValues[] = {
121 /* FIXME: the following only semi-succeed; they return NULL PIDLs on XP.. hmm.
122 { CSIDL_ALTSTARTUP, PT_FOLDER, 0 },
123 { CSIDL_COMMON_ALTSTARTUP, PT_FOLDER, 0 },
124 { CSIDL_COMMON_OEM_LINKS, PT_FOLDER, 0 },
126 /* Windows NT-only: */
127 { CSIDL_COMMON_DESKTOPDIRECTORY, PT_FOLDER, 0 },
128 { CSIDL_COMMON_DOCUMENTS, PT_SHELLEXT, 0 },
129 { CSIDL_COMMON_FAVORITES, PT_FOLDER, 0 },
130 { CSIDL_COMMON_PROGRAMS, PT_FOLDER, 0 },
131 { CSIDL_COMMON_STARTMENU, PT_FOLDER, 0 },
132 { CSIDL_COMMON_STARTUP, PT_FOLDER, 0 },
133 { CSIDL_COMMON_TEMPLATES, PT_FOLDER, 0 },
134 /* first appearing in shell32 version 4.71: */
135 { CSIDL_APPDATA, PT_FOLDER, 0 },
136 /* first appearing in shell32 version 4.72: */
137 { CSIDL_INTERNET_CACHE, PT_IESPECIAL2, 0 },
138 /* first appearing in shell32 version 5.0: */
139 { CSIDL_ADMINTOOLS, PT_FOLDER, 0 },
140 { CSIDL_COMMON_APPDATA, PT_FOLDER, 0 },
141 { CSIDL_LOCAL_APPDATA, PT_FOLDER, 0 },
142 { OLD_CSIDL_MYDOCUMENTS, PT_FOLDER, 0 },
143 { CSIDL_MYMUSIC, PT_FOLDER, 0 },
144 { CSIDL_MYPICTURES, PT_FOLDER, 0 },
145 { CSIDL_MYVIDEO, PT_FOLDER, 0 },
146 { CSIDL_PROFILE, PT_FOLDER, 0 },
147 { CSIDL_PROGRAM_FILES, PT_FOLDER, 0 },
148 { CSIDL_PROGRAM_FILESX86, PT_FOLDER, 0 },
149 { CSIDL_PROGRAM_FILES_COMMON, PT_FOLDER, 0 },
150 { CSIDL_PROGRAM_FILES_COMMONX86, PT_FOLDER, 0 },
151 { CSIDL_SYSTEM, PT_FOLDER, 0 },
152 { CSIDL_WINDOWS, PT_FOLDER, 0 },
153 /* first appearing in shell32 6.0: */
154 { CSIDL_CDBURN_AREA, PT_FOLDER, 0 },
155 { CSIDL_COMMON_MUSIC, PT_FOLDER, 0 },
156 { CSIDL_COMMON_PICTURES, PT_FOLDER, 0 },
157 { CSIDL_COMMON_VIDEO, PT_FOLDER, 0 },
158 { CSIDL_COMPUTERSNEARME, PT_WORKGRP, 0 },
159 { CSIDL_RESOURCES, PT_FOLDER, 0 },
160 { CSIDL_RESOURCES_LOCALIZED, PT_FOLDER, 0 },
163 static void loadShell32(void)
165 HMODULE hShell32 = GetModuleHandleA("shell32");
167 #define GET_PROC(func) \
168 p ## func = (void*)GetProcAddress(hShell32, #func); \
169 if(!p ## func) \
170 trace("GetProcAddress(%s) failed\n", #func);
172 GET_PROC(DllGetVersion)
173 GET_PROC(SHGetFolderPathA)
174 GET_PROC(SHGetFolderLocation)
175 GET_PROC(SHGetSpecialFolderPathA)
176 GET_PROC(SHGetSpecialFolderLocation)
177 GET_PROC(ILFindLastID)
178 if (!pILFindLastID)
179 pILFindLastID = (void *)GetProcAddress(hShell32, (LPCSTR)16);
180 GET_PROC(SHFileOperationA)
181 GET_PROC(SHGetMalloc)
183 ok(pSHGetMalloc != NULL, "shell32 is missing SHGetMalloc\n");
184 if (pSHGetMalloc)
186 HRESULT hr = pSHGetMalloc(&pMalloc);
188 ok(SUCCEEDED(hr), "SHGetMalloc failed: 0x%08x\n", hr);
189 ok(pMalloc != NULL, "SHGetMalloc returned a NULL IMalloc\n");
192 if (pDllGetVersion)
194 shellVersion.cbSize = sizeof(shellVersion);
195 pDllGetVersion(&shellVersion);
196 trace("shell32 version is %d.%d\n",
197 shellVersion.dwMajorVersion, shellVersion.dwMinorVersion);
199 #undef GET_PROC
202 #ifndef CSIDL_PROFILES
203 #define CSIDL_PROFILES 0x003e
204 #endif
206 /* A couple utility printing functions */
207 static const char *getFolderName(int folder)
209 static char unknown[32];
211 #define CSIDL_TO_STR(x) case x: return#x;
212 switch (folder)
214 CSIDL_TO_STR(CSIDL_DESKTOP);
215 CSIDL_TO_STR(CSIDL_INTERNET);
216 CSIDL_TO_STR(CSIDL_PROGRAMS);
217 CSIDL_TO_STR(CSIDL_CONTROLS);
218 CSIDL_TO_STR(CSIDL_PRINTERS);
219 CSIDL_TO_STR(CSIDL_PERSONAL);
220 CSIDL_TO_STR(CSIDL_FAVORITES);
221 CSIDL_TO_STR(CSIDL_STARTUP);
222 CSIDL_TO_STR(CSIDL_RECENT);
223 CSIDL_TO_STR(CSIDL_SENDTO);
224 CSIDL_TO_STR(CSIDL_BITBUCKET);
225 CSIDL_TO_STR(CSIDL_STARTMENU);
226 CSIDL_TO_STR(OLD_CSIDL_MYDOCUMENTS);
227 CSIDL_TO_STR(CSIDL_MYMUSIC);
228 CSIDL_TO_STR(CSIDL_MYVIDEO);
229 CSIDL_TO_STR(CSIDL_DESKTOPDIRECTORY);
230 CSIDL_TO_STR(CSIDL_DRIVES);
231 CSIDL_TO_STR(CSIDL_NETWORK);
232 CSIDL_TO_STR(CSIDL_NETHOOD);
233 CSIDL_TO_STR(CSIDL_FONTS);
234 CSIDL_TO_STR(CSIDL_TEMPLATES);
235 CSIDL_TO_STR(CSIDL_COMMON_STARTMENU);
236 CSIDL_TO_STR(CSIDL_COMMON_PROGRAMS);
237 CSIDL_TO_STR(CSIDL_COMMON_STARTUP);
238 CSIDL_TO_STR(CSIDL_COMMON_DESKTOPDIRECTORY);
239 CSIDL_TO_STR(CSIDL_APPDATA);
240 CSIDL_TO_STR(CSIDL_PRINTHOOD);
241 CSIDL_TO_STR(CSIDL_LOCAL_APPDATA);
242 CSIDL_TO_STR(CSIDL_ALTSTARTUP);
243 CSIDL_TO_STR(CSIDL_COMMON_ALTSTARTUP);
244 CSIDL_TO_STR(CSIDL_COMMON_FAVORITES);
245 CSIDL_TO_STR(CSIDL_INTERNET_CACHE);
246 CSIDL_TO_STR(CSIDL_COOKIES);
247 CSIDL_TO_STR(CSIDL_HISTORY);
248 CSIDL_TO_STR(CSIDL_COMMON_APPDATA);
249 CSIDL_TO_STR(CSIDL_WINDOWS);
250 CSIDL_TO_STR(CSIDL_SYSTEM);
251 CSIDL_TO_STR(CSIDL_PROGRAM_FILES);
252 CSIDL_TO_STR(CSIDL_MYPICTURES);
253 CSIDL_TO_STR(CSIDL_PROFILE);
254 CSIDL_TO_STR(CSIDL_SYSTEMX86);
255 CSIDL_TO_STR(CSIDL_PROGRAM_FILESX86);
256 CSIDL_TO_STR(CSIDL_PROGRAM_FILES_COMMON);
257 CSIDL_TO_STR(CSIDL_PROGRAM_FILES_COMMONX86);
258 CSIDL_TO_STR(CSIDL_COMMON_TEMPLATES);
259 CSIDL_TO_STR(CSIDL_COMMON_DOCUMENTS);
260 CSIDL_TO_STR(CSIDL_COMMON_ADMINTOOLS);
261 CSIDL_TO_STR(CSIDL_ADMINTOOLS);
262 CSIDL_TO_STR(CSIDL_CONNECTIONS);
263 CSIDL_TO_STR(CSIDL_PROFILES);
264 CSIDL_TO_STR(CSIDL_COMMON_MUSIC);
265 CSIDL_TO_STR(CSIDL_COMMON_PICTURES);
266 CSIDL_TO_STR(CSIDL_COMMON_VIDEO);
267 CSIDL_TO_STR(CSIDL_RESOURCES);
268 CSIDL_TO_STR(CSIDL_RESOURCES_LOCALIZED);
269 CSIDL_TO_STR(CSIDL_COMMON_OEM_LINKS);
270 CSIDL_TO_STR(CSIDL_CDBURN_AREA);
271 CSIDL_TO_STR(CSIDL_COMPUTERSNEARME);
272 #undef CSIDL_TO_STR
273 default:
274 sprintf(unknown, "unknown (0x%04x)", folder);
275 return unknown;
279 static const char *printGUID(const GUID *guid, char * guidSTR)
281 if (!guid) return NULL;
283 sprintf(guidSTR, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
284 guid->Data1, guid->Data2, guid->Data3,
285 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
286 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
287 return guidSTR;
290 static void testSHGetFolderLocationInvalidArgs(void)
292 LPITEMIDLIST pidl;
293 HRESULT hr;
295 if (!pSHGetFolderLocation) return;
297 /* check a bogus CSIDL: */
298 pidl = NULL;
299 hr = pSHGetFolderLocation(NULL, 0xeeee, NULL, 0, &pidl);
300 ok(hr == E_INVALIDARG,
301 "SHGetFolderLocation(NULL, 0xeeee, NULL, 0, &pidl) returned 0x%08x, expected E_INVALIDARG\n", hr);
302 if (SUCCEEDED(hr))
303 IMalloc_Free(pMalloc, pidl);
304 /* check a bogus user token: */
305 pidl = NULL;
306 hr = pSHGetFolderLocation(NULL, CSIDL_FAVORITES, (HANDLE)2, 0, &pidl);
307 ok(hr == E_FAIL || hr == E_HANDLE,
308 "SHGetFolderLocation(NULL, CSIDL_FAVORITES, 2, 0, &pidl) returned 0x%08x, expected E_FAIL or E_HANDLE\n", hr);
309 if (SUCCEEDED(hr))
310 IMalloc_Free(pMalloc, pidl);
311 /* a NULL pidl pointer crashes, so don't test it */
314 static void testSHGetSpecialFolderLocationInvalidArgs(void)
316 LPITEMIDLIST pidl = NULL;
317 HRESULT hr;
319 if (!pSHGetSpecialFolderLocation) return;
321 /* SHGetSpecialFolderLocation(NULL, 0, NULL) crashes */
322 hr = pSHGetSpecialFolderLocation(NULL, 0xeeee, &pidl);
323 ok(hr == E_INVALIDARG,
324 "SHGetSpecialFolderLocation(NULL, 0xeeee, &pidl) returned 0x%08x, "
325 "expected E_INVALIDARG\n", hr);
328 static void testSHGetFolderPathInvalidArgs(void)
330 char path[MAX_PATH];
331 HRESULT hr;
333 if (!pSHGetFolderPathA) return;
335 /* expect 2's a bogus handle, especially since we didn't open it */
336 hr = pSHGetFolderPathA(NULL, CSIDL_DESKTOP, (HANDLE)2,
337 SHGFP_TYPE_DEFAULT, path);
338 ok(hr == E_FAIL ||
339 hr == E_HANDLE || /* Windows Vista and 2008 */
340 broken(hr == S_OK), /* Windows 2000 and Me */
341 "SHGetFolderPathA(NULL, CSIDL_DESKTOP, 2, SHGFP_TYPE_DEFAULT, path) returned 0x%08x, expected E_FAIL\n", hr);
342 hr = pSHGetFolderPathA(NULL, 0xeeee, NULL, SHGFP_TYPE_DEFAULT, path);
343 ok(hr == E_INVALIDARG,
344 "SHGetFolderPathA(NULL, 0xeeee, NULL, SHGFP_TYPE_DEFAULT, path) returned 0x%08x, expected E_INVALIDARG\n", hr);
347 static void testSHGetSpecialFolderPathInvalidArgs(void)
349 char path[MAX_PATH];
350 BOOL ret;
352 if (!pSHGetSpecialFolderPathA) return;
354 #if 0
355 ret = pSHGetSpecialFolderPathA(NULL, NULL, CSIDL_BITBUCKET, FALSE);
356 ok(!ret,
357 "SHGetSpecialFolderPathA(NULL, NULL, CSIDL_BITBUCKET, FALSE) returned TRUE, expected FALSE\n");
358 #endif
359 /* odd but true: calling with a NULL path still succeeds if it's a real
360 * dir (on some windows platform). on winME it generates exception.
362 ret = pSHGetSpecialFolderPathA(NULL, path, CSIDL_PROGRAMS, FALSE);
363 ok(ret,
364 "SHGetSpecialFolderPathA(NULL, path, CSIDL_PROGRAMS, FALSE) returned FALSE, expected TRUE\n");
365 ret = pSHGetSpecialFolderPathA(NULL, path, 0xeeee, FALSE);
366 ok(!ret,
367 "SHGetSpecialFolderPathA(NULL, path, 0xeeee, FALSE) returned TRUE, expected FALSE\n");
370 static void testApiParameters(void)
372 testSHGetFolderLocationInvalidArgs();
373 testSHGetSpecialFolderLocationInvalidArgs();
374 testSHGetFolderPathInvalidArgs();
375 testSHGetSpecialFolderPathInvalidArgs();
378 /* Returns the folder's PIDL type, or 0xff if one can't be found. */
379 static BYTE testSHGetFolderLocation(BOOL optional, int folder)
381 LPITEMIDLIST pidl;
382 HRESULT hr;
383 BYTE ret = 0xff;
385 /* treat absence of function as success */
386 if (!pSHGetFolderLocation) return TRUE;
388 pidl = NULL;
389 hr = pSHGetFolderLocation(NULL, folder, NULL, 0, &pidl);
390 ok(SUCCEEDED(hr) || optional,
391 "SHGetFolderLocation(NULL, %s, NULL, 0, &pidl) failed: 0x%08x\n", getFolderName(folder), hr);
392 if (SUCCEEDED(hr))
394 ok(pidl != NULL,
395 "SHGetFolderLocation(NULL, %s, NULL, 0, &pidl) succeeded, but returned pidl is NULL\n", getFolderName(folder));
396 if (pidl)
398 LPITEMIDLIST pidlLast = pILFindLastID(pidl);
400 ok(pidlLast != NULL, "%s: ILFindLastID failed\n",
401 getFolderName(folder));
402 if (pidlLast)
403 ret = pidlLast->mkid.abID[0];
404 IMalloc_Free(pMalloc, pidl);
407 return ret;
410 /* Returns the folder's PIDL type, or 0xff if one can't be found. */
411 static BYTE testSHGetSpecialFolderLocation(BOOL optional, int folder)
413 LPITEMIDLIST pidl;
414 HRESULT hr;
415 BYTE ret = 0xff;
417 /* treat absence of function as success */
418 if (!pSHGetSpecialFolderLocation) return TRUE;
420 pidl = NULL;
421 hr = pSHGetSpecialFolderLocation(NULL, folder, &pidl);
422 ok(SUCCEEDED(hr) || optional,
423 "SHGetSpecialFolderLocation(NULL, %s, &pidl) failed: 0x%08x\n", getFolderName(folder), hr);
424 if (SUCCEEDED(hr))
426 ok(pidl != NULL,
427 "SHGetSpecialFolderLocation(NULL, %s, &pidl) succeeded, but returned pidl is NULL\n", getFolderName(folder));
428 if (pidl)
430 LPITEMIDLIST pidlLast = pILFindLastID(pidl);
432 ok(pidlLast != NULL,
433 "%s: ILFindLastID failed\n", getFolderName(folder));
434 if (pidlLast)
435 ret = pidlLast->mkid.abID[0];
436 IMalloc_Free(pMalloc, pidl);
439 return ret;
442 static void testSHGetFolderPath(BOOL optional, int folder)
444 char path[MAX_PATH];
445 HRESULT hr;
447 if (!pSHGetFolderPathA) return;
449 hr = pSHGetFolderPathA(NULL, folder, NULL, SHGFP_TYPE_CURRENT, path);
450 ok(SUCCEEDED(hr) || optional,
451 "SHGetFolderPathA(NULL, %s, NULL, SHGFP_TYPE_CURRENT, path) failed: 0x%08x\n", getFolderName(folder), hr);
454 static void testSHGetSpecialFolderPath(BOOL optional, int folder)
456 char path[MAX_PATH];
457 BOOL ret;
459 if (!pSHGetSpecialFolderPathA) return;
461 ret = pSHGetSpecialFolderPathA(NULL, path, folder, FALSE);
462 if (ret && winetest_interactive)
463 printf("%s: %s\n", getFolderName(folder), path);
464 ok(ret || optional,
465 "SHGetSpecialFolderPathA(NULL, path, %s, FALSE) failed\n",
466 getFolderName(folder));
469 static void testShellValues(const struct shellExpectedValues testEntries[],
470 int numEntries, BOOL optional)
472 int i;
474 for (i = 0; i < numEntries; i++)
476 BYTE type;
478 if (pSHGetFolderLocation)
480 type = testSHGetFolderLocation(optional, testEntries[i].folder);
481 if (!testEntries[i].altPidlType)
482 ok(type == testEntries[i].pidlType || optional ||
483 broken(type == 0xff) /* Win9x */,
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);
487 else
488 ok(type == testEntries[i].pidlType ||
489 type == testEntries[i].altPidlType ||
490 optional || broken(type == 0xff) /* Win9x */,
491 "%s has type %d (0x%02x), expected %d (0x%02x) or %d (0x%02x)\n",
492 getFolderName(testEntries[i].folder), type, type,
493 testEntries[i].pidlType, testEntries[i].pidlType,
494 testEntries[i].altPidlType, testEntries[i].altPidlType);
496 type = testSHGetSpecialFolderLocation(optional, testEntries[i].folder);
497 if (!testEntries[i].altPidlType)
498 ok(type == testEntries[i].pidlType || optional ||
499 broken(type == 0xff) /* Win9x */,
500 "%s has type %d (0x%02x), expected %d (0x%02x)\n",
501 getFolderName(testEntries[i].folder), type, type,
502 testEntries[i].pidlType, testEntries[i].pidlType);
503 else
504 ok(type == testEntries[i].pidlType ||
505 type == testEntries[i].altPidlType ||
506 optional || broken(type == 0xff) /* Win9x */,
507 "%s has type %d (0x%02x), expected %d (0x%02x) or %d (0x%02x)\n",
508 getFolderName(testEntries[i].folder), type, type,
509 testEntries[i].pidlType, testEntries[i].pidlType,
510 testEntries[i].altPidlType, testEntries[i].altPidlType);
511 switch (type)
513 case PT_FOLDER:
514 case PT_DRIVE:
515 case PT_DRIVE2:
516 case PT_IESPECIAL2:
517 testSHGetFolderPath(optional, testEntries[i].folder);
518 testSHGetSpecialFolderPath(optional, testEntries[i].folder);
519 break;
524 /* Attempts to verify that the folder path corresponding to the folder CSIDL
525 * value has the same value as the environment variable with name envVar.
526 * Doesn't mind if SHGetSpecialFolderPath fails for folder or if envVar isn't
527 * set in this environment; different OS and shell version behave differently.
528 * However, if both are present, fails if envVar's value is not the same
529 * (byte-for-byte) as what SHGetSpecialFolderPath returns.
531 static void matchSpecialFolderPathToEnv(int folder, const char *envVar)
533 char path[MAX_PATH];
535 if (!pSHGetSpecialFolderPathA) return;
537 if (pSHGetSpecialFolderPathA(NULL, path, folder, FALSE))
539 char *envVal = getenv(envVar);
541 ok(!envVal || !lstrcmpiA(envVal, path),
542 "%%%s%% does not match SHGetSpecialFolderPath:\n"
543 "%%%s%% is %s\nSHGetSpecialFolderPath returns %s\n",
544 envVar, envVar, envVal, path);
548 /* Attempts to match the GUID returned by SHGetFolderLocation for folder with
549 * GUID. Assumes the type of the returned PIDL is in fact a GUID, but doesn't
550 * fail if it isn't--that check should already have been done.
551 * Fails if the returned PIDL is a GUID whose value does not match guid.
553 static void matchGUID(int folder, const GUID *guid)
555 LPITEMIDLIST pidl;
556 HRESULT hr;
558 if (!pSHGetFolderLocation) return;
559 if (!guid) return;
561 pidl = NULL;
562 hr = pSHGetFolderLocation(NULL, folder, NULL, 0, &pidl);
563 if (SUCCEEDED(hr))
565 LPITEMIDLIST pidlLast = pILFindLastID(pidl);
567 if (pidlLast && (pidlLast->mkid.abID[0] == PT_SHELLEXT ||
568 pidlLast->mkid.abID[0] == PT_GUID))
570 GUID *shellGuid = (GUID *)(pidlLast->mkid.abID + 2);
571 char shellGuidStr[39], guidStr[39];
573 ok(IsEqualIID(shellGuid, guid),
574 "%s: got GUID %s, expected %s\n", getFolderName(folder),
575 printGUID(shellGuid, shellGuidStr), printGUID(guid, guidStr));
577 IMalloc_Free(pMalloc, pidl);
581 static void testDesktop(void)
583 testSHGetFolderPath(FALSE, CSIDL_DESKTOP);
584 testSHGetSpecialFolderPath(FALSE, CSIDL_DESKTOP);
585 /* Test the desktop; even though SHITEMID should always contain abID of at
586 * least one type, when cb is 0 its value is undefined. So don't check
587 * what the returned type is, just make sure it exists.
589 testSHGetFolderLocation(FALSE, CSIDL_DESKTOP);
590 testSHGetSpecialFolderLocation(FALSE, CSIDL_DESKTOP);
593 static void testPersonal(void)
595 BYTE type;
597 /* The pidl may be a real folder, or a virtual directory, or a drive if the
598 * home directory is set to the root directory of a drive.
600 if (pSHGetFolderLocation)
602 type = testSHGetFolderLocation(FALSE, CSIDL_PERSONAL);
603 ok(type == PT_FOLDER || type == PT_GUID || type == PT_DRIVE ||
604 broken(type == 0xff) /* Win9x */,
605 "CSIDL_PERSONAL returned invalid type 0x%02x, "
606 "expected PT_FOLDER or PT_GUID or PT_DRIVE\n", type);
607 if (type == PT_FOLDER)
608 testSHGetFolderPath(FALSE, CSIDL_PERSONAL);
610 type = testSHGetSpecialFolderLocation(FALSE, CSIDL_PERSONAL);
611 ok(type == PT_FOLDER || type == PT_GUID || type == PT_DRIVE,
612 "CSIDL_PERSONAL returned invalid type 0x%02x, "
613 "expected PT_FOLDER or PT_GUID\n", type);
614 if (type == PT_FOLDER)
615 testSHGetSpecialFolderPath(FALSE, CSIDL_PERSONAL);
618 /* Checks the PIDL type of all the known values. */
619 static void testPidlTypes(void)
621 testDesktop();
622 testPersonal();
623 testShellValues(requiredShellValues, ARRAY_SIZE(requiredShellValues),
624 FALSE);
625 testShellValues(optionalShellValues, ARRAY_SIZE(optionalShellValues),
626 TRUE);
629 /* Verifies various shell virtual folders have the correct well-known GUIDs. */
630 static void testGUIDs(void)
632 matchGUID(CSIDL_BITBUCKET, &CLSID_RecycleBin);
633 matchGUID(CSIDL_CONTROLS, &CLSID_ControlPanel);
634 matchGUID(CSIDL_DRIVES, &CLSID_MyComputer);
635 matchGUID(CSIDL_INTERNET, &CLSID_Internet);
636 matchGUID(CSIDL_NETWORK, &CLSID_NetworkPlaces);
637 matchGUID(CSIDL_PERSONAL, &CLSID_MyDocuments);
638 matchGUID(CSIDL_COMMON_DOCUMENTS, &CLSID_CommonDocuments);
641 /* Verifies various shell paths match the environment variables to which they
642 * correspond.
644 static void testEnvVars(void)
646 matchSpecialFolderPathToEnv(CSIDL_PROGRAM_FILES, "ProgramFiles");
647 matchSpecialFolderPathToEnv(CSIDL_APPDATA, "APPDATA");
648 matchSpecialFolderPathToEnv(CSIDL_PROFILE, "USERPROFILE");
649 matchSpecialFolderPathToEnv(CSIDL_WINDOWS, "SystemRoot");
650 matchSpecialFolderPathToEnv(CSIDL_WINDOWS, "windir");
651 matchSpecialFolderPathToEnv(CSIDL_PROGRAM_FILES_COMMON,
652 "CommonProgramFiles");
653 /* this is only set on Wine, but can't hurt to verify it: */
654 matchSpecialFolderPathToEnv(CSIDL_SYSTEM, "winsysdir");
657 /* Loosely based on PathRemoveBackslashA from dlls/shlwapi/path.c */
658 static BOOL myPathIsRootA(LPCSTR lpszPath)
660 if (lpszPath && *lpszPath &&
661 lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
662 return TRUE; /* X:\ */
663 return FALSE;
665 static LPSTR myPathRemoveBackslashA( LPSTR lpszPath )
667 LPSTR szTemp = NULL;
669 if(lpszPath)
671 szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
672 if (!myPathIsRootA(lpszPath) && *szTemp == '\\')
673 *szTemp = '\0';
675 return szTemp;
678 /* Verifies the shell path for CSIDL_WINDOWS matches the return from
679 * GetWindowsDirectory. If SHGetSpecialFolderPath fails, no harm, no foul--not
680 * every shell32 version supports CSIDL_WINDOWS.
682 static void testWinDir(void)
684 char windowsShellPath[MAX_PATH], windowsDir[MAX_PATH] = { 0 };
686 if (!pSHGetSpecialFolderPathA) return;
688 if (pSHGetSpecialFolderPathA(NULL, windowsShellPath, CSIDL_WINDOWS, FALSE))
690 myPathRemoveBackslashA(windowsShellPath);
691 GetWindowsDirectoryA(windowsDir, sizeof(windowsDir));
692 myPathRemoveBackslashA(windowsDir);
693 ok(!lstrcmpiA(windowsDir, windowsShellPath),
694 "GetWindowsDirectory returns %s SHGetSpecialFolderPath returns %s\n",
695 windowsDir, windowsShellPath);
699 /* Verifies the shell path for CSIDL_SYSTEM and CSIDL_SYSTEMX86 matches the
700 * return from GetSystemDirectory. If SHGetSpecialFolderPath fails, no harm,
701 * no foul--not every shell32 version supports CSIDL_SYSTEM.
703 static void testSystemDir(void)
705 char systemShellPath[MAX_PATH], systemDir[MAX_PATH] = { 0 };
707 if (!pSHGetSpecialFolderPathA) return;
709 GetSystemDirectoryA(systemDir, sizeof(systemDir));
710 myPathRemoveBackslashA(systemDir);
711 if (pSHGetSpecialFolderPathA(NULL, systemShellPath, CSIDL_SYSTEM, FALSE))
713 myPathRemoveBackslashA(systemShellPath);
714 ok(!lstrcmpiA(systemDir, systemShellPath),
715 "GetSystemDirectory returns %s SHGetSpecialFolderPath returns %s\n",
716 systemDir, systemShellPath);
718 /* check CSIDL_SYSTEMX86; note that this isn't always present, so don't
719 * worry if it fails
721 if (pSHGetSpecialFolderPathA(NULL, systemShellPath, CSIDL_SYSTEMX86, FALSE))
723 myPathRemoveBackslashA(systemShellPath);
724 ok(!lstrcmpiA(systemDir, systemShellPath),
725 "GetSystemDirectory returns %s SHGetSpecialFolderPath returns %s\n",
726 systemDir, systemShellPath);
730 /* Globals used by subprocesses */
731 static int myARGC;
732 static char **myARGV;
733 static char base[MAX_PATH];
734 static char selfname[MAX_PATH];
736 static int init(void)
738 myARGC = winetest_get_mainargs(&myARGV);
739 if (!GetCurrentDirectoryA(sizeof(base), base)) return 0;
740 strcpy(selfname, myARGV[0]);
741 return 1;
744 /* Subprocess helper 1: test what happens when CSIDL_FAVORITES is set to a
745 * nonexistent directory.
747 static void testNonExistentPath1(void)
749 HRESULT hr;
750 LPITEMIDLIST pidl;
751 char *p, path[MAX_PATH];
753 /* test some failure cases first: */
754 hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES, NULL,
755 SHGFP_TYPE_CURRENT, path);
756 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
757 "SHGetFolderPath returned 0x%08x, expected 0x80070002\n", hr);
758 pidl = NULL;
759 hr = pSHGetFolderLocation(NULL, CSIDL_FAVORITES, NULL, 0,
760 &pidl);
761 ok(hr == E_FAIL || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
762 "SHGetFolderLocation returned 0x%08x\n", hr);
763 if (SUCCEEDED(hr) && pidl)
764 IMalloc_Free(pMalloc, pidl);
765 ok(!pSHGetSpecialFolderPathA(NULL, path, CSIDL_FAVORITES, FALSE),
766 "SHGetSpecialFolderPath succeeded, expected failure\n");
767 pidl = NULL;
768 hr = pSHGetSpecialFolderLocation(NULL, CSIDL_FAVORITES, &pidl);
769 ok(hr == E_FAIL || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
770 "SHGetFolderLocation returned 0x%08x\n", hr);
771 if (SUCCEEDED(hr) && pidl)
772 IMalloc_Free(pMalloc, pidl);
773 /* now test success: */
774 hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES | CSIDL_FLAG_CREATE, NULL,
775 SHGFP_TYPE_CURRENT, path);
776 if (SUCCEEDED(hr))
778 BOOL ret;
780 trace("CSIDL_FAVORITES was changed to %s\n", path);
781 ret = CreateDirectoryA(path, NULL);
782 ok(!ret,
783 "CreateDirectoryA succeeded but should have failed "
784 "with ERROR_ALREADY_EXISTS\n");
785 if (!ret)
786 ok(GetLastError() == ERROR_ALREADY_EXISTS,
787 "CreateDirectoryA failed with %d, "
788 "expected ERROR_ALREADY_EXISTS\n",
789 GetLastError());
790 p = path + strlen(path);
791 strcpy(p, "\\desktop.ini");
792 DeleteFileA(path);
793 *p = 0;
794 SetFileAttributesA( path, FILE_ATTRIBUTE_NORMAL );
795 ret = RemoveDirectoryA(path);
796 ok( ret, "failed to remove %s error %u\n", path, GetLastError() );
798 ok(SUCCEEDED(hr),
799 "SHGetFolderPath(NULL, CSIDL_FAVORITES | CSIDL_FLAG_CREATE, "
800 "NULL, SHGFP_TYPE_CURRENT, path) failed: 0x%08x\n", hr);
803 /* Subprocess helper 2: make sure SHGetFolderPath still succeeds when the
804 * original value of CSIDL_FAVORITES is restored.
806 static void testNonExistentPath2(void)
808 HRESULT hr;
809 char path[MAX_PATH];
811 hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES | CSIDL_FLAG_CREATE, NULL,
812 SHGFP_TYPE_CURRENT, path);
813 ok(SUCCEEDED(hr), "SHGetFolderPath failed: 0x%08x\n", hr);
816 static void doChild(const char *arg)
818 if (arg[0] == '1')
819 testNonExistentPath1();
820 else if (arg[0] == '2')
821 testNonExistentPath2();
824 /* Tests the return values from the various shell functions both with and
825 * without the use of the CSIDL_FLAG_CREATE flag. This flag only appeared in
826 * version 5 of the shell, so don't test unless it's at least version 5.
827 * The test reads a value from the registry, modifies it, calls
828 * SHGetFolderPath once with the CSIDL_FLAG_CREATE flag, and immediately
829 * afterward without it. Then it restores the registry and deletes the folder
830 * that was created.
831 * One oddity with respect to restoration: shell32 caches somehow, so it needs
832 * to be reloaded in order to see the correct (restored) value.
833 * Some APIs unrelated to the ones under test may fail, but I expect they're
834 * covered by other unit tests; I just print out something about failure to
835 * help trace what's going on.
837 static void testNonExistentPath(void)
839 static const char userShellFolders[] =
840 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders";
841 char originalPath[MAX_PATH], modifiedPath[MAX_PATH];
842 HKEY key;
844 if (!pSHGetFolderPathA) return;
845 if (!pSHGetFolderLocation) return;
846 if (!pSHGetSpecialFolderPathA) return;
847 if (!pSHGetSpecialFolderLocation) return;
848 if (!pSHFileOperationA) return;
849 if (shellVersion.dwMajorVersion < 5) return;
851 if (!RegOpenKeyExA(HKEY_CURRENT_USER, userShellFolders, 0, KEY_ALL_ACCESS,
852 &key))
854 DWORD len, type;
856 len = sizeof(originalPath);
857 if (!RegQueryValueExA(key, "Favorites", NULL, &type,
858 (LPBYTE)&originalPath, &len))
860 size_t len = strlen(originalPath);
862 memcpy(modifiedPath, originalPath, len);
863 modifiedPath[len++] = '2';
864 modifiedPath[len++] = '\0';
865 trace("Changing CSIDL_FAVORITES to %s\n", modifiedPath);
866 if (!RegSetValueExA(key, "Favorites", 0, type,
867 (LPBYTE)modifiedPath, len))
869 char buffer[MAX_PATH+20];
870 STARTUPINFOA startup;
871 PROCESS_INFORMATION info;
873 sprintf(buffer, "%s tests/shellpath.c 1", selfname);
874 memset(&startup, 0, sizeof(startup));
875 startup.cb = sizeof(startup);
876 startup.dwFlags = STARTF_USESHOWWINDOW;
877 startup.dwFlags = SW_SHOWNORMAL;
878 CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL,
879 &startup, &info);
880 winetest_wait_child_process( info.hProcess );
882 /* restore original values: */
883 trace("Restoring CSIDL_FAVORITES to %s\n", originalPath);
884 RegSetValueExA(key, "Favorites", 0, type, (LPBYTE) originalPath,
885 strlen(originalPath) + 1);
886 RegFlushKey(key);
888 sprintf(buffer, "%s tests/shellpath.c 2", selfname);
889 memset(&startup, 0, sizeof(startup));
890 startup.cb = sizeof(startup);
891 startup.dwFlags = STARTF_USESHOWWINDOW;
892 startup.dwFlags = SW_SHOWNORMAL;
893 CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL,
894 &startup, &info);
895 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0,
896 "child process termination\n");
899 else skip("RegQueryValueExA(key, Favorites, ...) failed\n");
900 if (key)
901 RegCloseKey(key);
903 else skip("RegOpenKeyExA(HKEY_CURRENT_USER, %s, ...) failed\n", userShellFolders);
906 START_TEST(shellpath)
908 if (!init()) return;
910 loadShell32();
912 if (myARGC >= 3)
913 doChild(myARGV[2]);
914 else
916 /* Report missing functions once */
917 if (!pSHGetFolderLocation)
918 skip("SHGetFolderLocation is not available\n");
920 /* first test various combinations of parameters: */
921 testApiParameters();
923 /* check known values: */
924 testPidlTypes();
925 testGUIDs();
926 testEnvVars();
927 testWinDir();
928 testSystemDir();
929 testNonExistentPath();