Release 1.6-rc2.
[wine/testsucceed.git] / dlls / shlwapi / tests / shreg.c
blob3634041c01e73d7590a4f6932e466ca7a3bed05d
1 /* Unit test suite for SHReg* functions
3 * Copyright 2002 Juergen Schmied
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include <stdarg.h>
21 #include <stdio.h>
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "winreg.h"
28 #include "winuser.h"
29 #include "shlwapi.h"
31 /* Keys used for testing */
32 #define REG_TEST_KEY "Software\\Wine\\Test"
33 #define REG_CURRENT_VERSION "Software\\Microsoft\\Windows\\CurrentVersion\\explorer"
35 static HMODULE hshlwapi;
37 static DWORD (WINAPI *pSHCopyKeyA)(HKEY,LPCSTR,HKEY,DWORD);
38 static DWORD (WINAPI *pSHRegGetPathA)(HKEY,LPCSTR,LPCSTR,LPSTR,DWORD);
39 static LSTATUS (WINAPI *pSHRegGetValueA)(HKEY,LPCSTR,LPCSTR,SRRF,LPDWORD,LPVOID,LPDWORD);
40 static LSTATUS (WINAPI *pSHRegCreateUSKeyW)(LPCWSTR,REGSAM,HUSKEY,PHUSKEY,DWORD);
42 static char sTestpath1[] = "%LONGSYSTEMVAR%\\subdir1";
43 static char sTestpath2[] = "%FOO%\\subdir1";
45 static const char * sEnvvar1 = "bar";
46 static const char * sEnvvar2 = "ImARatherLongButIndeedNeededString";
48 static char sExpTestpath1[MAX_PATH];
49 static char sExpTestpath2[MAX_PATH];
50 static DWORD nExpLen1;
51 static DWORD nExpLen2;
53 static const char * sEmptyBuffer ="0123456789";
55 /* delete key and all its subkeys */
56 static DWORD delete_key( HKEY hkey, LPCSTR parent, LPCSTR keyname )
58 HKEY parentKey;
59 DWORD ret;
61 RegCloseKey(hkey);
63 /* open the parent of the key to close */
64 ret = RegOpenKeyExA( HKEY_CURRENT_USER, parent, 0, KEY_ALL_ACCESS, &parentKey);
65 if (ret != ERROR_SUCCESS)
66 return ret;
68 ret = SHDeleteKeyA( parentKey, keyname );
69 RegCloseKey(parentKey);
71 return ret;
74 static HKEY create_test_entries(void)
76 HKEY hKey;
77 DWORD ret;
78 DWORD nExpectedLen1, nExpectedLen2;
80 SetEnvironmentVariableA("LONGSYSTEMVAR", sEnvvar1);
81 SetEnvironmentVariableA("FOO", sEnvvar2);
83 ret = RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKey);
84 ok( ERROR_SUCCESS == ret, "RegCreateKeyA failed, ret=%u\n", ret);
86 if (hKey)
88 ok(!RegSetValueExA(hKey,"Test1",0,REG_EXPAND_SZ, (LPBYTE) sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed\n");
89 ok(!RegSetValueExA(hKey,"Test2",0,REG_SZ, (LPBYTE) sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed\n");
90 ok(!RegSetValueExA(hKey,"Test3",0,REG_EXPAND_SZ, (LPBYTE) sTestpath2, strlen(sTestpath2)+1), "RegSetValueExA failed\n");
93 nExpLen1 = ExpandEnvironmentStringsA(sTestpath1, sExpTestpath1, sizeof(sExpTestpath1));
94 nExpLen2 = ExpandEnvironmentStringsA(sTestpath2, sExpTestpath2, sizeof(sExpTestpath2));
96 nExpectedLen1 = strlen(sTestpath1) - strlen("%LONGSYSTEMVAR%") + strlen(sEnvvar1) + 1;
97 nExpectedLen2 = strlen(sTestpath2) - strlen("%FOO%") + strlen(sEnvvar2) + 1;
98 /* ExpandEnvironmentStringsA on NT4 returns 2x the correct result */
99 trace("sExplen1 = (%d)\n", nExpLen1);
100 if (nExpectedLen1 != nExpLen1)
101 trace( "Expanding %s failed (expected %d) - known bug in NT4\n", sTestpath1, nExpectedLen1 );
103 trace("sExplen2 = (%d)\n", nExpLen2);
104 if (nExpectedLen2 != nExpLen2)
105 trace( "Expanding %s failed (expected %d) - known bug in NT4\n", sTestpath2, nExpectedLen2 );
107 /* Make sure we carry on with correct values */
108 nExpLen1 = nExpectedLen1;
109 nExpLen2 = nExpectedLen2;
110 return hKey;
113 static void test_SHGetValue(void)
115 DWORD dwSize;
116 DWORD dwType;
117 DWORD dwRet;
118 char buf[MAX_PATH];
120 strcpy(buf, sEmptyBuffer);
121 dwSize = MAX_PATH;
122 dwType = -1;
123 dwRet = SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", &dwType, buf, &dwSize);
124 ok( ERROR_SUCCESS == dwRet, "SHGetValueA failed, ret=%u\n", dwRet);
125 ok( 0 == strcmp(sExpTestpath1, buf) ||
126 broken(0 == strcmp(sTestpath1, buf)), /* IE4.x */
127 "Comparing of (%s) with (%s) failed\n", buf, sExpTestpath1);
128 ok( REG_SZ == dwType ||
129 broken(REG_EXPAND_SZ == dwType), /* IE4.x */
130 "Expected REG_SZ, got (%u)\n", dwType);
132 strcpy(buf, sEmptyBuffer);
133 dwSize = MAX_PATH;
134 dwType = -1;
135 dwRet = SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", &dwType, buf, &dwSize);
136 ok( ERROR_SUCCESS == dwRet, "SHGetValueA failed, ret=%u\n", dwRet);
137 ok( 0 == strcmp(sTestpath1, buf) , "Comparing of (%s) with (%s) failed\n", buf, sTestpath1);
138 ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
141 static void test_SHRegGetValue(void)
143 LSTATUS ret;
144 DWORD size, type;
145 char data[MAX_PATH];
147 if(!pSHRegGetValueA)
148 return;
150 size = MAX_PATH;
151 ret = pSHRegGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", SRRF_RT_REG_EXPAND_SZ, &type, data, &size);
152 ok(ret == ERROR_INVALID_PARAMETER, "SHRegGetValue failed, ret=%u\n", ret);
154 size = MAX_PATH;
155 ret = pSHRegGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", SRRF_RT_REG_SZ, &type, data, &size);
156 ok(ret == ERROR_SUCCESS, "SHRegGetValue failed, ret=%u\n", ret);
157 ok(!strcmp(data, sExpTestpath1), "data = %s, expected %s\n", data, sExpTestpath1);
158 ok(type == REG_SZ, "type = %d, expected REG_SZ\n", type);
160 size = MAX_PATH;
161 ret = pSHRegGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", SRRF_RT_REG_DWORD, &type, data, &size);
162 ok(ret == ERROR_UNSUPPORTED_TYPE, "SHRegGetValue failed, ret=%u\n", ret);
164 size = MAX_PATH;
165 ret = pSHRegGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", SRRF_RT_REG_EXPAND_SZ, &type, data, &size);
166 ok(ret == ERROR_INVALID_PARAMETER, "SHRegGetValue failed, ret=%u\n", ret);
168 size = MAX_PATH;
169 ret = pSHRegGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", SRRF_RT_REG_SZ, &type, data, &size);
170 ok(ret == ERROR_SUCCESS, "SHRegGetValue failed, ret=%u\n", ret);
171 ok(!strcmp(data, sTestpath1), "data = %s, expected %s\n", data, sTestpath1);
172 ok(type == REG_SZ, "type = %d, expected REG_SZ\n", type);
174 size = MAX_PATH;
175 ret = pSHRegGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", SRRF_RT_REG_QWORD, &type, data, &size);
176 ok(ret == ERROR_UNSUPPORTED_TYPE, "SHRegGetValue failed, ret=%u\n", ret);
179 static void test_SHGetRegPath(void)
181 char buf[MAX_PATH];
182 DWORD dwRet;
184 if (!pSHRegGetPathA)
185 return;
187 strcpy(buf, sEmptyBuffer);
188 dwRet = (*pSHRegGetPathA)(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", buf, 0);
189 ok( ERROR_SUCCESS == dwRet, "SHRegGetPathA failed, ret=%u\n", dwRet);
190 ok( 0 == strcmp(sExpTestpath1, buf) , "Comparing (%s) with (%s) failed\n", buf, sExpTestpath1);
193 static void test_SHQueryValueEx(void)
195 HKEY hKey;
196 DWORD dwSize;
197 DWORD dwType;
198 char buf[MAX_PATH];
199 DWORD dwRet;
200 const char * sTestedFunction = "";
201 DWORD nUsedBuffer1,nUsedBuffer2;
203 sTestedFunction = "RegOpenKeyExA";
204 dwRet = RegOpenKeyExA(HKEY_CURRENT_USER, REG_TEST_KEY, 0, KEY_QUERY_VALUE, &hKey);
205 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
207 /****** SHQueryValueExA ******/
209 sTestedFunction = "SHQueryValueExA";
210 nUsedBuffer1 = max(strlen(sExpTestpath1)+1, strlen(sTestpath1)+1);
211 nUsedBuffer2 = max(strlen(sExpTestpath2)+1, strlen(sTestpath2)+1);
213 * Case 1.1 All arguments are NULL
215 dwRet = SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, NULL);
216 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
219 * Case 1.2 dwType is set
221 dwType = -1;
222 dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, NULL, NULL);
223 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
224 ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
227 * dwSize is set
228 * dwExpanded < dwUnExpanded
230 dwSize = 6;
231 dwRet = SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, &dwSize);
232 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
233 ok( dwSize == nUsedBuffer1, "Buffer sizes (%u) and (%u) are not equal\n", dwSize, nUsedBuffer1);
236 * dwExpanded > dwUnExpanded
238 dwSize = 6;
239 dwRet = SHQueryValueExA( hKey, "Test3", NULL, NULL, NULL, &dwSize);
240 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
241 ok( dwSize >= nUsedBuffer2 ||
242 broken(dwSize == (strlen(sTestpath2) + 1)), /* < IE4.x */
243 "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
246 * Case 1 string shrinks during expanding
248 strcpy(buf, sEmptyBuffer);
249 dwSize = 6;
250 dwType = -1;
251 dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, buf, &dwSize);
252 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%u)\n", dwRet);
253 ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
254 ok( dwSize == nUsedBuffer1, "Buffer sizes (%u) and (%u) are not equal\n", dwSize, nUsedBuffer1);
255 ok( REG_SZ == dwType ||
256 broken(REG_EXPAND_SZ == dwType), /* < IE6 */
257 "Expected REG_SZ, got (%u)\n", dwType);
260 * string grows during expanding
261 * dwSize is smaller than the size of the unexpanded string
263 strcpy(buf, sEmptyBuffer);
264 dwSize = 6;
265 dwType = -1;
266 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
267 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%u)\n", dwRet);
268 ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
269 ok( dwSize >= nUsedBuffer2 ||
270 broken(dwSize == (strlen(sTestpath2) + 1)), /* < IE6 */
271 "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
272 ok( REG_SZ == dwType ||
273 broken(REG_EXPAND_SZ == dwType), /* < IE6 */
274 "Expected REG_SZ, got (%u)\n", dwType);
277 * string grows during expanding
278 * dwSize is larger than the size of the unexpanded string, but
279 * smaller than the part before the backslash. If the unexpanded
280 * string fits into the buffer, it can get cut when expanded.
282 strcpy(buf, sEmptyBuffer);
283 dwSize = strlen(sEnvvar2) - 2;
284 dwType = -1;
285 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
286 ok( ERROR_MORE_DATA == dwRet ||
287 broken(ERROR_ENVVAR_NOT_FOUND == dwRet) || /* IE5.5 */
288 broken(ERROR_SUCCESS == dwRet), /* < IE5.5*/
289 "Expected ERROR_MORE_DATA, got (%u)\n", dwRet);
291 todo_wine
293 ok( (0 == strcmp("", buf)) || (0 == strcmp(sTestpath2, buf)),
294 "Expected empty or unexpanded string (win98), got (%s)\n", buf);
297 ok( dwSize >= nUsedBuffer2 ||
298 broken(dwSize == (strlen("") + 1)), /* < IE 5.5 */
299 "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
300 ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
303 * string grows during expanding
304 * dwSize is larger than the size of the part before the backslash,
305 * but smaller than the expanded string. If the unexpanded string fits
306 * into the buffer, it can get cut when expanded.
308 strcpy(buf, sEmptyBuffer);
309 dwSize = nExpLen2 - 4;
310 dwType = -1;
311 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
312 ok( ERROR_MORE_DATA == dwRet ||
313 broken(ERROR_ENVVAR_NOT_FOUND == dwRet) || /* IE5.5 */
314 broken(ERROR_SUCCESS == dwRet), /* < IE5.5 */
315 "Expected ERROR_MORE_DATA, got (%u)\n", dwRet);
317 todo_wine
319 ok( (0 == strcmp("", buf)) || (0 == strcmp(sEnvvar2, buf)) ||
320 broken(0 == strcmp(sTestpath2, buf)), /* IE 5.5 */
321 "Expected empty or first part of the string \"%s\", got \"%s\"\n", sEnvvar2, buf);
324 ok( dwSize >= nUsedBuffer2 ||
325 broken(dwSize == (strlen(sEnvvar2) + 1)) || /* IE4.01 SP1 (W98) and IE5 (W98SE) */
326 broken(dwSize == (strlen("") + 1)), /* IE4.01 (NT4) and IE5.x (W2K) */
327 "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
328 ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
331 * The buffer is NULL but the size is set
333 strcpy(buf, sEmptyBuffer);
334 dwSize = 6;
335 dwType = -1;
336 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, NULL, &dwSize);
337 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
338 ok( dwSize >= nUsedBuffer2 ||
339 broken(dwSize == (strlen(sTestpath2) + 1)), /* IE4.01 SP1 (Win98) */
340 "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
341 ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
343 RegCloseKey(hKey);
346 static void test_SHCopyKey(void)
348 HKEY hKeySrc, hKeyDst;
349 DWORD dwRet;
351 if (!pSHCopyKeyA)
353 win_skip("SHCopyKeyA is not available\n");
354 return;
357 /* Delete existing destination sub keys */
358 hKeyDst = NULL;
359 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) && hKeyDst)
361 SHDeleteKeyA(hKeyDst, NULL);
362 RegCloseKey(hKeyDst);
365 hKeyDst = NULL;
366 dwRet = RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst);
367 if (dwRet || !hKeyDst)
369 ok( 0, "Destination couldn't be created, RegCreateKeyA returned (%u)\n", dwRet);
370 return;
373 hKeySrc = NULL;
374 dwRet = RegOpenKeyA(HKEY_LOCAL_MACHINE, REG_CURRENT_VERSION, &hKeySrc);
375 if (dwRet || !hKeySrc)
377 ok( 0, "Source couldn't be opened, RegOpenKeyA returned (%u)\n", dwRet);
378 RegCloseKey(hKeyDst);
379 return;
382 dwRet = (*pSHCopyKeyA)(hKeySrc, NULL, hKeyDst, 0);
383 ok ( ERROR_SUCCESS == dwRet, "Copy failed, ret=(%u)\n", dwRet);
385 RegCloseKey(hKeySrc);
386 RegCloseKey(hKeyDst);
388 /* Check we copied the sub keys, i.e. something that's on every windows system (including Wine) */
389 hKeyDst = NULL;
390 dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination\\Shell Folders", &hKeyDst);
391 if (dwRet || !hKeyDst)
393 ok ( 0, "Copy couldn't be opened, RegOpenKeyA returned (%u)\n", dwRet);
394 return;
397 /* And the we copied the values too */
398 ok(!SHQueryValueExA(hKeyDst, "Common AppData", NULL, NULL, NULL, NULL), "SHQueryValueExA failed\n");
400 RegCloseKey(hKeyDst);
403 static void test_SHDeleteKey(void)
405 HKEY hKeyTest, hKeyS;
406 DWORD dwRet;
407 int sysfail = 1;
409 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKeyTest))
411 if (!RegCreateKey(hKeyTest, "ODBC", &hKeyS))
413 HKEY hKeyO;
415 if (!RegCreateKey(hKeyS, "ODBC.INI", &hKeyO))
417 RegCloseKey (hKeyO);
419 if (!RegCreateKey(hKeyS, "ODBCINST.INI", &hKeyO))
421 RegCloseKey (hKeyO);
422 sysfail = 0;
425 RegCloseKey (hKeyS);
427 RegCloseKey (hKeyTest);
430 if (!sysfail)
433 dwRet = SHDeleteKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC");
434 ok ( ERROR_SUCCESS == dwRet, "SHDeleteKey failed, ret=(%u)\n", dwRet);
436 dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC", &hKeyS);
437 ok ( ERROR_FILE_NOT_FOUND == dwRet, "SHDeleteKey did not delete\n");
439 if (dwRet == ERROR_SUCCESS)
440 RegCloseKey (hKeyS);
442 else
443 ok( 0, "Could not set up SHDeleteKey test\n");
446 static void test_SHRegCreateUSKeyW(void)
448 static const WCHAR subkeyW[] = {'s','u','b','k','e','y',0};
449 LONG ret;
451 if (!pSHRegCreateUSKeyW)
453 win_skip("SHRegCreateUSKeyW not available\n");
454 return;
457 ret = pSHRegCreateUSKeyW(subkeyW, KEY_ALL_ACCESS, NULL, NULL, SHREGSET_FORCE_HKCU);
458 ok(ret == ERROR_INVALID_PARAMETER, "got %d\n", ret);
461 START_TEST(shreg)
463 HKEY hkey = create_test_entries();
465 if (!hkey) return;
467 hshlwapi = GetModuleHandleA("shlwapi.dll");
469 /* SHCreateStreamOnFileEx was introduced in shlwapi v6.0 */
470 if(!GetProcAddress(hshlwapi, "SHCreateStreamOnFileEx")){
471 win_skip("Too old shlwapi version\n");
472 return;
475 pSHCopyKeyA = (void*)GetProcAddress(hshlwapi,"SHCopyKeyA");
476 pSHRegGetPathA = (void*)GetProcAddress(hshlwapi,"SHRegGetPathA");
477 pSHRegGetValueA = (void*)GetProcAddress(hshlwapi,"SHRegGetValueA");
478 pSHRegCreateUSKeyW = (void*)GetProcAddress(hshlwapi, "SHRegCreateUSKeyW");
480 test_SHGetValue();
481 test_SHRegGetValue();
482 test_SHQueryValueEx();
483 test_SHGetRegPath();
484 test_SHCopyKey();
485 test_SHDeleteKey();
486 test_SHRegCreateUSKeyW();
488 delete_key( hkey, "Software\\Wine", "Test" );