push d1f5df181c120dbe494f7c89b454752c2e0dcc04
[wine/hacks.git] / dlls / shell32 / tests / shellpath.c
blob71c236bcbbf270561df32990d73608a25ea04cb6
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).
24 #define COBJMACROS
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "shlguid.h"
31 #include "shlobj.h"
32 #include "shlwapi.h"
33 #include "wine/test.h"
35 /* CSIDL_MYDOCUMENTS is now the same as CSIDL_PERSONAL, but what we want
36 * here is its original value.
38 #define OLD_CSIDL_MYDOCUMENTS 0x000c
40 #ifndef ARRAY_SIZE
41 #define ARRAY_SIZE(x) ( sizeof(x) / sizeof((x)[0]) )
42 #endif
44 /* from pidl.h, not included here: */
45 #ifndef PT_GUID
46 #define PT_GUID 0x1f /* no path */
47 #endif
48 #ifndef PT_DRIVE
49 #define PT_DRIVE 0x23 /* has path */
50 #endif
51 #ifndef PT_DRIVE2
52 #define PT_DRIVE2 0x25 /* has path */
53 #endif
54 #ifndef PT_SHELLEXT
55 #define PT_SHELLEXT 0x2e /* no path */
56 #endif
57 #ifndef PT_FOLDER
58 #define PT_FOLDER 0x31 /* has path */
59 #endif
60 #ifndef PT_WORKGRP
61 #define PT_WORKGRP 0x41 /* no path */
62 #endif
63 #ifndef PT_YAGUID
64 #define PT_YAGUID 0x70 /* no path */
65 #endif
66 /* FIXME: this is used for history/favorites folders; what's a better name? */
67 #ifndef PT_IESPECIAL2
68 #define PT_IESPECIAL2 0xb1 /* has path */
69 #endif
71 static GUID CLSID_CommonDocuments = { 0x0000000c, 0x0000, 0x0000, { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x1a } };
73 struct shellExpectedValues {
74 int folder;
75 int numTypes;
76 const BYTE *types;
79 static HRESULT (WINAPI *pDllGetVersion)(DLLVERSIONINFO *);
80 static HRESULT (WINAPI *pSHGetFolderPathA)(HWND, int, HANDLE, DWORD, LPSTR);
81 static HRESULT (WINAPI *pSHGetFolderLocation)(HWND, int, HANDLE, DWORD,
82 LPITEMIDLIST *);
83 static BOOL (WINAPI *pSHGetSpecialFolderPathA)(HWND, LPSTR, int, BOOL);
84 static HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *);
85 static LPITEMIDLIST (WINAPI *pILFindLastID)(LPCITEMIDLIST);
86 static int (WINAPI *pSHFileOperationA)(LPSHFILEOPSTRUCTA);
87 static HRESULT (WINAPI *pSHGetMalloc)(LPMALLOC *);
88 static DLLVERSIONINFO shellVersion = { 0 };
89 static LPMALLOC pMalloc;
90 static const BYTE guidType[] = { PT_GUID };
91 static const BYTE controlPanelType[] = { PT_SHELLEXT, PT_GUID };
92 static const BYTE folderType[] = { PT_FOLDER };
93 static const BYTE favoritesType[] = { PT_FOLDER, 0 };
94 static const BYTE folderOrSpecialType[] = { PT_FOLDER, PT_IESPECIAL2 };
95 static const BYTE personalType[] = { PT_FOLDER, PT_GUID, PT_DRIVE, 0xff /* Win9x */ };
96 /* FIXME: don't know the type of 0x71 returned by Vista/2008 for printers */
97 static const BYTE printersType[] = { PT_YAGUID, PT_SHELLEXT, 0x71 };
98 static const BYTE ieSpecialType[] = { PT_IESPECIAL2 };
99 static const BYTE shellExtType[] = { PT_SHELLEXT };
100 static const BYTE workgroupType[] = { PT_WORKGRP };
101 #define DECLARE_TYPE(x, y) { x, sizeof(y) / sizeof(y[0]), y }
102 static const struct shellExpectedValues requiredShellValues[] = {
103 DECLARE_TYPE(CSIDL_BITBUCKET, guidType),
104 DECLARE_TYPE(CSIDL_CONTROLS, controlPanelType),
105 DECLARE_TYPE(CSIDL_COOKIES, folderType),
106 DECLARE_TYPE(CSIDL_DESKTOPDIRECTORY, folderType),
107 DECLARE_TYPE(CSIDL_DRIVES, guidType),
108 DECLARE_TYPE(CSIDL_FAVORITES, favoritesType),
109 DECLARE_TYPE(CSIDL_FONTS, folderOrSpecialType),
110 /* FIXME: the following fails in Wine, returns type PT_FOLDER
111 DECLARE_TYPE(CSIDL_HISTORY, ieSpecialType),
113 DECLARE_TYPE(CSIDL_INTERNET, guidType),
114 DECLARE_TYPE(CSIDL_NETHOOD, folderType),
115 DECLARE_TYPE(CSIDL_NETWORK, guidType),
116 DECLARE_TYPE(CSIDL_PERSONAL, personalType),
117 DECLARE_TYPE(CSIDL_PRINTERS, printersType),
118 DECLARE_TYPE(CSIDL_PRINTHOOD, folderType),
119 DECLARE_TYPE(CSIDL_PROGRAMS, folderType),
120 DECLARE_TYPE(CSIDL_RECENT, folderOrSpecialType),
121 DECLARE_TYPE(CSIDL_SENDTO, folderType),
122 DECLARE_TYPE(CSIDL_STARTMENU, folderType),
123 DECLARE_TYPE(CSIDL_STARTUP, folderType),
124 DECLARE_TYPE(CSIDL_TEMPLATES, folderType),
126 static const struct shellExpectedValues optionalShellValues[] = {
127 /* FIXME: the following only semi-succeed; they return NULL PIDLs on XP.. hmm.
128 DECLARE_TYPE(CSIDL_ALTSTARTUP, folderType),
129 DECLARE_TYPE(CSIDL_COMMON_ALTSTARTUP, folderType),
130 DECLARE_TYPE(CSIDL_COMMON_OEM_LINKS, folderType),
132 /* Windows NT-only: */
133 DECLARE_TYPE(CSIDL_COMMON_DESKTOPDIRECTORY, folderType),
134 DECLARE_TYPE(CSIDL_COMMON_DOCUMENTS, shellExtType),
135 DECLARE_TYPE(CSIDL_COMMON_FAVORITES, folderType),
136 DECLARE_TYPE(CSIDL_COMMON_PROGRAMS, folderType),
137 DECLARE_TYPE(CSIDL_COMMON_STARTMENU, folderType),
138 DECLARE_TYPE(CSIDL_COMMON_STARTUP, folderType),
139 DECLARE_TYPE(CSIDL_COMMON_TEMPLATES, folderType),
140 /* first appearing in shell32 version 4.71: */
141 DECLARE_TYPE(CSIDL_APPDATA, folderType),
142 /* first appearing in shell32 version 4.72: */
143 DECLARE_TYPE(CSIDL_INTERNET_CACHE, ieSpecialType),
144 /* first appearing in shell32 version 5.0: */
145 DECLARE_TYPE(CSIDL_ADMINTOOLS, folderType),
146 DECLARE_TYPE(CSIDL_COMMON_APPDATA, folderType),
147 DECLARE_TYPE(CSIDL_LOCAL_APPDATA, folderType),
148 DECLARE_TYPE(OLD_CSIDL_MYDOCUMENTS, folderType),
149 DECLARE_TYPE(CSIDL_MYMUSIC, folderType),
150 DECLARE_TYPE(CSIDL_MYPICTURES, folderType),
151 DECLARE_TYPE(CSIDL_MYVIDEO, folderType),
152 DECLARE_TYPE(CSIDL_PROFILE, folderType),
153 DECLARE_TYPE(CSIDL_PROGRAM_FILES, folderType),
154 DECLARE_TYPE(CSIDL_PROGRAM_FILESX86, folderType),
155 DECLARE_TYPE(CSIDL_PROGRAM_FILES_COMMON, folderType),
156 DECLARE_TYPE(CSIDL_PROGRAM_FILES_COMMONX86, folderType),
157 DECLARE_TYPE(CSIDL_SYSTEM, folderType),
158 DECLARE_TYPE(CSIDL_WINDOWS, folderType),
159 /* first appearing in shell32 6.0: */
160 DECLARE_TYPE(CSIDL_CDBURN_AREA, folderType),
161 DECLARE_TYPE(CSIDL_COMMON_MUSIC, folderType),
162 DECLARE_TYPE(CSIDL_COMMON_PICTURES, folderType),
163 DECLARE_TYPE(CSIDL_COMMON_VIDEO, folderType),
164 DECLARE_TYPE(CSIDL_COMPUTERSNEARME, workgroupType),
165 DECLARE_TYPE(CSIDL_RESOURCES, folderType),
166 DECLARE_TYPE(CSIDL_RESOURCES_LOCALIZED, folderType),
168 #undef DECLARE_TYPE
170 static void loadShell32(void)
172 HMODULE hShell32 = GetModuleHandleA("shell32");
174 #define GET_PROC(func) \
175 p ## func = (void*)GetProcAddress(hShell32, #func); \
176 if(!p ## func) \
177 trace("GetProcAddress(%s) failed\n", #func);
179 GET_PROC(DllGetVersion)
180 GET_PROC(SHGetFolderPathA)
181 GET_PROC(SHGetFolderLocation)
182 GET_PROC(SHGetSpecialFolderPathA)
183 GET_PROC(SHGetSpecialFolderLocation)
184 GET_PROC(ILFindLastID)
185 if (!pILFindLastID)
186 pILFindLastID = (void *)GetProcAddress(hShell32, (LPCSTR)16);
187 GET_PROC(SHFileOperationA)
188 GET_PROC(SHGetMalloc)
190 ok(pSHGetMalloc != NULL, "shell32 is missing SHGetMalloc\n");
191 if (pSHGetMalloc)
193 HRESULT hr = pSHGetMalloc(&pMalloc);
195 ok(SUCCEEDED(hr), "SHGetMalloc failed: 0x%08x\n", hr);
196 ok(pMalloc != NULL, "SHGetMalloc returned a NULL IMalloc\n");
199 if (pDllGetVersion)
201 shellVersion.cbSize = sizeof(shellVersion);
202 pDllGetVersion(&shellVersion);
203 trace("shell32 version is %d.%d\n",
204 shellVersion.dwMajorVersion, shellVersion.dwMinorVersion);
206 #undef GET_PROC
209 #ifndef CSIDL_PROFILES
210 #define CSIDL_PROFILES 0x003e
211 #endif
213 /* A couple utility printing functions */
214 static const char *getFolderName(int folder)
216 static char unknown[32];
218 #define CSIDL_TO_STR(x) case x: return#x;
219 switch (folder)
221 CSIDL_TO_STR(CSIDL_DESKTOP);
222 CSIDL_TO_STR(CSIDL_INTERNET);
223 CSIDL_TO_STR(CSIDL_PROGRAMS);
224 CSIDL_TO_STR(CSIDL_CONTROLS);
225 CSIDL_TO_STR(CSIDL_PRINTERS);
226 CSIDL_TO_STR(CSIDL_PERSONAL);
227 CSIDL_TO_STR(CSIDL_FAVORITES);
228 CSIDL_TO_STR(CSIDL_STARTUP);
229 CSIDL_TO_STR(CSIDL_RECENT);
230 CSIDL_TO_STR(CSIDL_SENDTO);
231 CSIDL_TO_STR(CSIDL_BITBUCKET);
232 CSIDL_TO_STR(CSIDL_STARTMENU);
233 CSIDL_TO_STR(OLD_CSIDL_MYDOCUMENTS);
234 CSIDL_TO_STR(CSIDL_MYMUSIC);
235 CSIDL_TO_STR(CSIDL_MYVIDEO);
236 CSIDL_TO_STR(CSIDL_DESKTOPDIRECTORY);
237 CSIDL_TO_STR(CSIDL_DRIVES);
238 CSIDL_TO_STR(CSIDL_NETWORK);
239 CSIDL_TO_STR(CSIDL_NETHOOD);
240 CSIDL_TO_STR(CSIDL_FONTS);
241 CSIDL_TO_STR(CSIDL_TEMPLATES);
242 CSIDL_TO_STR(CSIDL_COMMON_STARTMENU);
243 CSIDL_TO_STR(CSIDL_COMMON_PROGRAMS);
244 CSIDL_TO_STR(CSIDL_COMMON_STARTUP);
245 CSIDL_TO_STR(CSIDL_COMMON_DESKTOPDIRECTORY);
246 CSIDL_TO_STR(CSIDL_APPDATA);
247 CSIDL_TO_STR(CSIDL_PRINTHOOD);
248 CSIDL_TO_STR(CSIDL_LOCAL_APPDATA);
249 CSIDL_TO_STR(CSIDL_ALTSTARTUP);
250 CSIDL_TO_STR(CSIDL_COMMON_ALTSTARTUP);
251 CSIDL_TO_STR(CSIDL_COMMON_FAVORITES);
252 CSIDL_TO_STR(CSIDL_INTERNET_CACHE);
253 CSIDL_TO_STR(CSIDL_COOKIES);
254 CSIDL_TO_STR(CSIDL_HISTORY);
255 CSIDL_TO_STR(CSIDL_COMMON_APPDATA);
256 CSIDL_TO_STR(CSIDL_WINDOWS);
257 CSIDL_TO_STR(CSIDL_SYSTEM);
258 CSIDL_TO_STR(CSIDL_PROGRAM_FILES);
259 CSIDL_TO_STR(CSIDL_MYPICTURES);
260 CSIDL_TO_STR(CSIDL_PROFILE);
261 CSIDL_TO_STR(CSIDL_SYSTEMX86);
262 CSIDL_TO_STR(CSIDL_PROGRAM_FILESX86);
263 CSIDL_TO_STR(CSIDL_PROGRAM_FILES_COMMON);
264 CSIDL_TO_STR(CSIDL_PROGRAM_FILES_COMMONX86);
265 CSIDL_TO_STR(CSIDL_COMMON_TEMPLATES);
266 CSIDL_TO_STR(CSIDL_COMMON_DOCUMENTS);
267 CSIDL_TO_STR(CSIDL_COMMON_ADMINTOOLS);
268 CSIDL_TO_STR(CSIDL_ADMINTOOLS);
269 CSIDL_TO_STR(CSIDL_CONNECTIONS);
270 CSIDL_TO_STR(CSIDL_PROFILES);
271 CSIDL_TO_STR(CSIDL_COMMON_MUSIC);
272 CSIDL_TO_STR(CSIDL_COMMON_PICTURES);
273 CSIDL_TO_STR(CSIDL_COMMON_VIDEO);
274 CSIDL_TO_STR(CSIDL_RESOURCES);
275 CSIDL_TO_STR(CSIDL_RESOURCES_LOCALIZED);
276 CSIDL_TO_STR(CSIDL_COMMON_OEM_LINKS);
277 CSIDL_TO_STR(CSIDL_CDBURN_AREA);
278 CSIDL_TO_STR(CSIDL_COMPUTERSNEARME);
279 #undef CSIDL_TO_STR
280 default:
281 sprintf(unknown, "unknown (0x%04x)", folder);
282 return unknown;
286 static const char *printGUID(const GUID *guid, char * guidSTR)
288 if (!guid) return NULL;
290 sprintf(guidSTR, "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
291 guid->Data1, guid->Data2, guid->Data3,
292 guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
293 guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);
294 return guidSTR;
297 static void testSHGetFolderLocationInvalidArgs(void)
299 LPITEMIDLIST pidl;
300 HRESULT hr;
302 if (!pSHGetFolderLocation) return;
304 /* check a bogus CSIDL: */
305 pidl = NULL;
306 hr = pSHGetFolderLocation(NULL, 0xeeee, NULL, 0, &pidl);
307 ok(hr == E_INVALIDARG,
308 "SHGetFolderLocation(NULL, 0xeeee, NULL, 0, &pidl) returned 0x%08x, expected E_INVALIDARG\n", hr);
309 if (SUCCEEDED(hr))
310 IMalloc_Free(pMalloc, pidl);
311 /* check a bogus user token: */
312 pidl = NULL;
313 hr = pSHGetFolderLocation(NULL, CSIDL_FAVORITES, (HANDLE)2, 0, &pidl);
314 ok(hr == E_FAIL || hr == E_HANDLE,
315 "SHGetFolderLocation(NULL, CSIDL_FAVORITES, 2, 0, &pidl) returned 0x%08x, expected E_FAIL or E_HANDLE\n", hr);
316 if (SUCCEEDED(hr))
317 IMalloc_Free(pMalloc, pidl);
318 /* a NULL pidl pointer crashes, so don't test it */
321 static void testSHGetSpecialFolderLocationInvalidArgs(void)
323 LPITEMIDLIST pidl = NULL;
324 HRESULT hr;
326 if (!pSHGetSpecialFolderLocation) return;
328 /* SHGetSpecialFolderLocation(NULL, 0, NULL) crashes */
329 hr = pSHGetSpecialFolderLocation(NULL, 0xeeee, &pidl);
330 ok(hr == E_INVALIDARG,
331 "SHGetSpecialFolderLocation(NULL, 0xeeee, &pidl) returned 0x%08x, "
332 "expected E_INVALIDARG\n", hr);
335 static void testSHGetFolderPathInvalidArgs(void)
337 char path[MAX_PATH];
338 HRESULT hr;
340 if (!pSHGetFolderPathA) return;
342 /* expect 2's a bogus handle, especially since we didn't open it */
343 hr = pSHGetFolderPathA(NULL, CSIDL_DESKTOP, (HANDLE)2,
344 SHGFP_TYPE_DEFAULT, path);
345 ok(hr == E_FAIL ||
346 hr == E_HANDLE || /* Windows Vista and 2008 */
347 broken(hr == S_OK), /* Windows 2000 and Me */
348 "SHGetFolderPathA(NULL, CSIDL_DESKTOP, 2, SHGFP_TYPE_DEFAULT, path) returned 0x%08x, expected E_FAIL\n", hr);
349 hr = pSHGetFolderPathA(NULL, 0xeeee, NULL, SHGFP_TYPE_DEFAULT, path);
350 ok(hr == E_INVALIDARG,
351 "SHGetFolderPathA(NULL, 0xeeee, NULL, SHGFP_TYPE_DEFAULT, path) returned 0x%08x, expected E_INVALIDARG\n", hr);
354 static void testSHGetSpecialFolderPathInvalidArgs(void)
356 char path[MAX_PATH];
357 BOOL ret;
359 if (!pSHGetSpecialFolderPathA) return;
361 #if 0
362 ret = pSHGetSpecialFolderPathA(NULL, NULL, CSIDL_BITBUCKET, FALSE);
363 ok(!ret,
364 "SHGetSpecialFolderPathA(NULL, NULL, CSIDL_BITBUCKET, FALSE) returned TRUE, expected FALSE\n");
365 #endif
366 /* odd but true: calling with a NULL path still succeeds if it's a real
367 * dir (on some windows platform). on winME it generates exception.
369 ret = pSHGetSpecialFolderPathA(NULL, path, CSIDL_PROGRAMS, FALSE);
370 ok(ret,
371 "SHGetSpecialFolderPathA(NULL, path, CSIDL_PROGRAMS, FALSE) returned FALSE, expected TRUE\n");
372 ret = pSHGetSpecialFolderPathA(NULL, path, 0xeeee, FALSE);
373 ok(!ret,
374 "SHGetSpecialFolderPathA(NULL, path, 0xeeee, FALSE) returned TRUE, expected FALSE\n");
377 static void testApiParameters(void)
379 testSHGetFolderLocationInvalidArgs();
380 testSHGetSpecialFolderLocationInvalidArgs();
381 testSHGetFolderPathInvalidArgs();
382 testSHGetSpecialFolderPathInvalidArgs();
385 /* Returns the folder's PIDL type, or 0xff if one can't be found. */
386 static BYTE testSHGetFolderLocation(BOOL optional, int folder)
388 LPITEMIDLIST pidl;
389 HRESULT hr;
390 BYTE ret = 0xff;
392 /* treat absence of function as success */
393 if (!pSHGetFolderLocation) return TRUE;
395 pidl = NULL;
396 hr = pSHGetFolderLocation(NULL, folder, NULL, 0, &pidl);
397 ok(SUCCEEDED(hr) || optional,
398 "SHGetFolderLocation(NULL, %s, NULL, 0, &pidl) failed: 0x%08x\n", getFolderName(folder), hr);
399 if (SUCCEEDED(hr))
401 ok(pidl != NULL,
402 "SHGetFolderLocation(NULL, %s, NULL, 0, &pidl) succeeded, but returned pidl is NULL\n", getFolderName(folder));
403 if (pidl)
405 LPITEMIDLIST pidlLast = pILFindLastID(pidl);
407 ok(pidlLast != NULL, "%s: ILFindLastID failed\n",
408 getFolderName(folder));
409 if (pidlLast)
410 ret = pidlLast->mkid.abID[0];
411 IMalloc_Free(pMalloc, pidl);
414 return ret;
417 /* Returns the folder's PIDL type, or 0xff if one can't be found. */
418 static BYTE testSHGetSpecialFolderLocation(BOOL optional, int folder)
420 LPITEMIDLIST pidl;
421 HRESULT hr;
422 BYTE ret = 0xff;
424 /* treat absence of function as success */
425 if (!pSHGetSpecialFolderLocation) return TRUE;
427 pidl = NULL;
428 hr = pSHGetSpecialFolderLocation(NULL, folder, &pidl);
429 ok(SUCCEEDED(hr) || optional ||
430 broken((folder == CSIDL_COOKIES || folder == CSIDL_INTERNET) &&
431 hr == E_INVALIDARG) /* NT4 */,
432 "SHGetSpecialFolderLocation(NULL, %s, &pidl) failed: 0x%08x\n", getFolderName(folder), hr);
433 if (SUCCEEDED(hr))
435 ok(pidl != NULL,
436 "SHGetSpecialFolderLocation(NULL, %s, &pidl) succeeded, but returned pidl is NULL\n", getFolderName(folder));
437 if (pidl)
439 LPITEMIDLIST pidlLast = pILFindLastID(pidl);
441 ok(pidlLast != NULL,
442 "%s: ILFindLastID failed\n", getFolderName(folder));
443 if (pidlLast)
444 ret = pidlLast->mkid.abID[0];
445 IMalloc_Free(pMalloc, pidl);
448 return ret;
451 static void testSHGetFolderPath(BOOL optional, int folder)
453 char path[MAX_PATH];
454 HRESULT hr;
456 if (!pSHGetFolderPathA) return;
458 hr = pSHGetFolderPathA(NULL, folder, NULL, SHGFP_TYPE_CURRENT, path);
459 ok(SUCCEEDED(hr) || optional,
460 "SHGetFolderPathA(NULL, %s, NULL, SHGFP_TYPE_CURRENT, path) failed: 0x%08x\n", getFolderName(folder), hr);
463 static void testSHGetSpecialFolderPath(BOOL optional, int folder)
465 char path[MAX_PATH];
466 BOOL ret;
468 if (!pSHGetSpecialFolderPathA) return;
470 ret = pSHGetSpecialFolderPathA(NULL, path, folder, FALSE);
471 if (ret && winetest_interactive)
472 printf("%s: %s\n", getFolderName(folder), path);
473 ok(ret || optional,
474 "SHGetSpecialFolderPathA(NULL, path, %s, FALSE) failed\n",
475 getFolderName(folder));
478 static void testShellValues(const struct shellExpectedValues testEntries[],
479 int numEntries, BOOL optional)
481 int i;
483 for (i = 0; i < numEntries; i++)
485 BYTE type;
486 int j;
487 BOOL foundTypeMatch = FALSE;
489 if (pSHGetFolderLocation)
491 type = testSHGetFolderLocation(optional, testEntries[i].folder);
492 for (j = 0; !foundTypeMatch && j < testEntries[i].numTypes; j++)
493 if (testEntries[i].types[j] == type)
494 foundTypeMatch = TRUE;
495 ok(foundTypeMatch || optional || broken(type == 0xff) /* Win9x */,
496 "%s has unexpected type %d (0x%02x)\n",
497 getFolderName(testEntries[i].folder), type, type);
499 type = testSHGetSpecialFolderLocation(optional, testEntries[i].folder);
500 for (j = 0, foundTypeMatch = FALSE; !foundTypeMatch &&
501 j < testEntries[i].numTypes; j++)
502 if (testEntries[i].types[j] == type)
503 foundTypeMatch = TRUE;
504 ok(foundTypeMatch || optional || broken(type == 0xff) /* Win9x */,
505 "%s has unexpected type %d (0x%02x)\n",
506 getFolderName(testEntries[i].folder), type, type);
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);
567 char shellGuidStr[39], guidStr[39];
569 ok(IsEqualIID(shellGuid, guid),
570 "%s: got GUID %s, expected %s\n", getFolderName(folder),
571 printGUID(shellGuid, shellGuidStr), printGUID(guid, guidStr));
573 IMalloc_Free(pMalloc, pidl);
577 static void testDesktop(void)
579 testSHGetFolderPath(FALSE, CSIDL_DESKTOP);
580 testSHGetSpecialFolderPath(FALSE, CSIDL_DESKTOP);
581 /* Test the desktop; even though SHITEMID should always contain abID of at
582 * least one type, when cb is 0 its value is undefined. So don't check
583 * what the returned type is, just make sure it exists.
585 testSHGetFolderLocation(FALSE, CSIDL_DESKTOP);
586 testSHGetSpecialFolderLocation(FALSE, CSIDL_DESKTOP);
589 /* Checks the PIDL type of all the known values. */
590 static void testPidlTypes(void)
592 testDesktop();
593 testShellValues(requiredShellValues, ARRAY_SIZE(requiredShellValues),
594 FALSE);
595 testShellValues(optionalShellValues, ARRAY_SIZE(optionalShellValues),
596 TRUE);
599 /* Verifies various shell virtual folders have the correct well-known GUIDs. */
600 static void testGUIDs(void)
602 matchGUID(CSIDL_BITBUCKET, &CLSID_RecycleBin);
603 matchGUID(CSIDL_CONTROLS, &CLSID_ControlPanel);
604 matchGUID(CSIDL_DRIVES, &CLSID_MyComputer);
605 matchGUID(CSIDL_INTERNET, &CLSID_Internet);
606 matchGUID(CSIDL_NETWORK, &CLSID_NetworkPlaces);
607 matchGUID(CSIDL_PERSONAL, &CLSID_MyDocuments);
608 matchGUID(CSIDL_COMMON_DOCUMENTS, &CLSID_CommonDocuments);
611 /* Verifies various shell paths match the environment variables to which they
612 * correspond.
614 static void testEnvVars(void)
616 matchSpecialFolderPathToEnv(CSIDL_PROGRAM_FILES, "ProgramFiles");
617 matchSpecialFolderPathToEnv(CSIDL_APPDATA, "APPDATA");
618 matchSpecialFolderPathToEnv(CSIDL_PROFILE, "USERPROFILE");
619 matchSpecialFolderPathToEnv(CSIDL_WINDOWS, "SystemRoot");
620 matchSpecialFolderPathToEnv(CSIDL_WINDOWS, "windir");
621 matchSpecialFolderPathToEnv(CSIDL_PROGRAM_FILES_COMMON,
622 "CommonProgramFiles");
623 /* this is only set on Wine, but can't hurt to verify it: */
624 matchSpecialFolderPathToEnv(CSIDL_SYSTEM, "winsysdir");
627 /* Loosely based on PathRemoveBackslashA from dlls/shlwapi/path.c */
628 static BOOL myPathIsRootA(LPCSTR lpszPath)
630 if (lpszPath && *lpszPath &&
631 lpszPath[1] == ':' && lpszPath[2] == '\\' && lpszPath[3] == '\0')
632 return TRUE; /* X:\ */
633 return FALSE;
635 static LPSTR myPathRemoveBackslashA( LPSTR lpszPath )
637 LPSTR szTemp = NULL;
639 if(lpszPath)
641 szTemp = CharPrevA(lpszPath, lpszPath + strlen(lpszPath));
642 if (!myPathIsRootA(lpszPath) && *szTemp == '\\')
643 *szTemp = '\0';
645 return szTemp;
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 myPathRemoveBackslashA(windowsShellPath);
661 GetWindowsDirectoryA(windowsDir, sizeof(windowsDir));
662 myPathRemoveBackslashA(windowsDir);
663 ok(!lstrcmpiA(windowsDir, windowsShellPath),
664 "GetWindowsDirectory returns %s SHGetSpecialFolderPath returns %s\n",
665 windowsDir, windowsShellPath);
669 /* Verifies the shell path for CSIDL_SYSTEM matches the return from
670 * GetSystemDirectory. If SHGetSpecialFolderPath fails, no harm,
671 * no foul--not every shell32 version supports CSIDL_SYSTEM.
673 static void testSystemDir(void)
675 char systemShellPath[MAX_PATH], systemDir[MAX_PATH] = { 0 };
677 if (!pSHGetSpecialFolderPathA) return;
679 GetSystemDirectoryA(systemDir, sizeof(systemDir));
680 myPathRemoveBackslashA(systemDir);
681 if (pSHGetSpecialFolderPathA(NULL, systemShellPath, CSIDL_SYSTEM, FALSE))
683 myPathRemoveBackslashA(systemShellPath);
684 ok(!lstrcmpiA(systemDir, systemShellPath),
685 "GetSystemDirectory returns %s SHGetSpecialFolderPath returns %s\n",
686 systemDir, systemShellPath);
688 /* CSIDL_SYSTEMX86 isn't checked in the same way, since it's different
689 * on Win64 (and non-x86 Windows systems, if there are any still in
690 * existence) than on Win32.
694 /* Globals used by subprocesses */
695 static int myARGC;
696 static char **myARGV;
697 static char base[MAX_PATH];
698 static char selfname[MAX_PATH];
700 static int init(void)
702 myARGC = winetest_get_mainargs(&myARGV);
703 if (!GetCurrentDirectoryA(sizeof(base), base)) return 0;
704 strcpy(selfname, myARGV[0]);
705 return 1;
708 /* Subprocess helper 1: test what happens when CSIDL_FAVORITES is set to a
709 * nonexistent directory.
711 static void testNonExistentPath1(void)
713 HRESULT hr;
714 LPITEMIDLIST pidl;
715 char *p, path[MAX_PATH];
717 /* test some failure cases first: */
718 hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES, NULL,
719 SHGFP_TYPE_CURRENT, path);
720 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
721 "SHGetFolderPath returned 0x%08x, expected 0x80070002\n", hr);
722 pidl = NULL;
723 hr = pSHGetFolderLocation(NULL, CSIDL_FAVORITES, NULL, 0,
724 &pidl);
725 ok(hr == E_FAIL || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
726 "SHGetFolderLocation returned 0x%08x\n", hr);
727 if (SUCCEEDED(hr) && pidl)
728 IMalloc_Free(pMalloc, pidl);
729 ok(!pSHGetSpecialFolderPathA(NULL, path, CSIDL_FAVORITES, FALSE),
730 "SHGetSpecialFolderPath succeeded, expected failure\n");
731 pidl = NULL;
732 hr = pSHGetSpecialFolderLocation(NULL, CSIDL_FAVORITES, &pidl);
733 ok(hr == E_FAIL || hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
734 "SHGetFolderLocation returned 0x%08x\n", hr);
735 if (SUCCEEDED(hr) && pidl)
736 IMalloc_Free(pMalloc, pidl);
737 /* now test success: */
738 hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES | CSIDL_FLAG_CREATE, NULL,
739 SHGFP_TYPE_CURRENT, path);
740 if (SUCCEEDED(hr))
742 BOOL ret;
744 trace("CSIDL_FAVORITES was changed to %s\n", path);
745 ret = CreateDirectoryA(path, NULL);
746 ok(!ret,
747 "CreateDirectoryA succeeded but should have failed "
748 "with ERROR_ALREADY_EXISTS\n");
749 if (!ret)
750 ok(GetLastError() == ERROR_ALREADY_EXISTS,
751 "CreateDirectoryA failed with %d, "
752 "expected ERROR_ALREADY_EXISTS\n",
753 GetLastError());
754 p = path + strlen(path);
755 strcpy(p, "\\desktop.ini");
756 DeleteFileA(path);
757 *p = 0;
758 SetFileAttributesA( path, FILE_ATTRIBUTE_NORMAL );
759 ret = RemoveDirectoryA(path);
760 ok( ret, "failed to remove %s error %u\n", path, GetLastError() );
762 ok(SUCCEEDED(hr),
763 "SHGetFolderPath(NULL, CSIDL_FAVORITES | CSIDL_FLAG_CREATE, "
764 "NULL, SHGFP_TYPE_CURRENT, path) failed: 0x%08x\n", hr);
767 /* Subprocess helper 2: make sure SHGetFolderPath still succeeds when the
768 * original value of CSIDL_FAVORITES is restored.
770 static void testNonExistentPath2(void)
772 HRESULT hr;
773 char path[MAX_PATH];
775 hr = pSHGetFolderPathA(NULL, CSIDL_FAVORITES | CSIDL_FLAG_CREATE, NULL,
776 SHGFP_TYPE_CURRENT, path);
777 ok(SUCCEEDED(hr), "SHGetFolderPath failed: 0x%08x\n", hr);
780 static void doChild(const char *arg)
782 if (arg[0] == '1')
783 testNonExistentPath1();
784 else if (arg[0] == '2')
785 testNonExistentPath2();
788 /* Tests the return values from the various shell functions both with and
789 * without the use of the CSIDL_FLAG_CREATE flag. This flag only appeared in
790 * version 5 of the shell, so don't test unless it's at least version 5.
791 * The test reads a value from the registry, modifies it, calls
792 * SHGetFolderPath once with the CSIDL_FLAG_CREATE flag, and immediately
793 * afterward without it. Then it restores the registry and deletes the folder
794 * that was created.
795 * One oddity with respect to restoration: shell32 caches somehow, so it needs
796 * to be reloaded in order to see the correct (restored) value.
797 * Some APIs unrelated to the ones under test may fail, but I expect they're
798 * covered by other unit tests; I just print out something about failure to
799 * help trace what's going on.
801 static void testNonExistentPath(void)
803 static const char userShellFolders[] =
804 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders";
805 char originalPath[MAX_PATH], modifiedPath[MAX_PATH];
806 HKEY key;
808 if (!pSHGetFolderPathA) return;
809 if (!pSHGetFolderLocation) return;
810 if (!pSHGetSpecialFolderPathA) return;
811 if (!pSHGetSpecialFolderLocation) return;
812 if (!pSHFileOperationA) return;
813 if (shellVersion.dwMajorVersion < 5) return;
815 if (!RegOpenKeyExA(HKEY_CURRENT_USER, userShellFolders, 0, KEY_ALL_ACCESS,
816 &key))
818 DWORD len, type;
820 len = sizeof(originalPath);
821 if (!RegQueryValueExA(key, "Favorites", NULL, &type,
822 (LPBYTE)&originalPath, &len))
824 size_t len = strlen(originalPath);
826 memcpy(modifiedPath, originalPath, len);
827 modifiedPath[len++] = '2';
828 modifiedPath[len++] = '\0';
829 trace("Changing CSIDL_FAVORITES to %s\n", modifiedPath);
830 if (!RegSetValueExA(key, "Favorites", 0, type,
831 (LPBYTE)modifiedPath, len))
833 char buffer[MAX_PATH+20];
834 STARTUPINFOA startup;
835 PROCESS_INFORMATION info;
837 sprintf(buffer, "%s tests/shellpath.c 1", selfname);
838 memset(&startup, 0, sizeof(startup));
839 startup.cb = sizeof(startup);
840 startup.dwFlags = STARTF_USESHOWWINDOW;
841 startup.dwFlags = SW_SHOWNORMAL;
842 CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL,
843 &startup, &info);
844 winetest_wait_child_process( info.hProcess );
846 /* restore original values: */
847 trace("Restoring CSIDL_FAVORITES to %s\n", originalPath);
848 RegSetValueExA(key, "Favorites", 0, type, (LPBYTE) originalPath,
849 strlen(originalPath) + 1);
850 RegFlushKey(key);
852 sprintf(buffer, "%s tests/shellpath.c 2", selfname);
853 memset(&startup, 0, sizeof(startup));
854 startup.cb = sizeof(startup);
855 startup.dwFlags = STARTF_USESHOWWINDOW;
856 startup.dwFlags = SW_SHOWNORMAL;
857 CreateProcessA(NULL, buffer, NULL, NULL, FALSE, 0L, NULL, NULL,
858 &startup, &info);
859 ok(WaitForSingleObject(info.hProcess, 30000) == WAIT_OBJECT_0,
860 "child process termination\n");
863 else skip("RegQueryValueExA(key, Favorites, ...) failed\n");
864 if (key)
865 RegCloseKey(key);
867 else skip("RegOpenKeyExA(HKEY_CURRENT_USER, %s, ...) failed\n", userShellFolders);
870 START_TEST(shellpath)
872 if (!init()) return;
874 loadShell32();
876 if (myARGC >= 3)
877 doChild(myARGV[2]);
878 else
880 /* Report missing functions once */
881 if (!pSHGetFolderLocation)
882 skip("SHGetFolderLocation is not available\n");
884 /* first test various combinations of parameters: */
885 testApiParameters();
887 /* check known values: */
888 testPidlTypes();
889 testGUIDs();
890 testEnvVars();
891 testWinDir();
892 testSystemDir();
893 testNonExistentPath();