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 #define WIN32_NO_STATUS
22 #define COM_NO_WINDOWS_H
27 #include <wine/test.h>
29 //#include "winbase.h"
30 //#include "winerror.h"
32 //#include "winuser.h"
35 /* Keys used for testing */
36 #define REG_TEST_KEY "Software\\Wine\\Test"
37 #define REG_CURRENT_VERSION "Software\\Microsoft\\Windows\\CurrentVersion\\explorer"
39 static HMODULE hshlwapi
;
41 static DWORD (WINAPI
*pSHCopyKeyA
)(HKEY
,LPCSTR
,HKEY
,DWORD
);
42 static DWORD (WINAPI
*pSHRegGetPathA
)(HKEY
,LPCSTR
,LPCSTR
,LPSTR
,DWORD
);
43 static LSTATUS (WINAPI
*pSHRegGetValueA
)(HKEY
,LPCSTR
,LPCSTR
,SRRF
,LPDWORD
,LPVOID
,LPDWORD
);
44 static LSTATUS (WINAPI
*pSHRegCreateUSKeyW
)(LPCWSTR
,REGSAM
,HUSKEY
,PHUSKEY
,DWORD
);
46 static const char sTestpath1
[] = "%LONGSYSTEMVAR%\\subdir1";
47 static const char sTestpath2
[] = "%FOO%\\subdir1";
49 static const char * sEnvvar1
= "bar";
50 static const char * sEnvvar2
= "ImARatherLongButIndeedNeededString";
52 static char sExpTestpath1
[MAX_PATH
];
53 static char sExpTestpath2
[MAX_PATH
];
54 static DWORD nExpLen1
;
55 static DWORD nExpLen2
;
57 static const char * sEmptyBuffer
="0123456789";
59 /* delete key and all its subkeys */
60 static DWORD
delete_key( HKEY hkey
, LPCSTR parent
, LPCSTR keyname
)
67 /* open the parent of the key to close */
68 ret
= RegOpenKeyExA( HKEY_CURRENT_USER
, parent
, 0, KEY_ALL_ACCESS
, &parentKey
);
69 if (ret
!= ERROR_SUCCESS
)
72 ret
= SHDeleteKeyA( parentKey
, keyname
);
73 RegCloseKey(parentKey
);
78 static HKEY
create_test_entries(void)
82 DWORD nExpectedLen1
, nExpectedLen2
;
84 SetEnvironmentVariableA("LONGSYSTEMVAR", sEnvvar1
);
85 SetEnvironmentVariableA("FOO", sEnvvar2
);
87 ret
= RegCreateKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
, &hKey
);
88 ok( ERROR_SUCCESS
== ret
, "RegCreateKeyA failed, ret=%u\n", ret
);
92 ok(!RegSetValueExA(hKey
,"Test1",0,REG_EXPAND_SZ
, (LPBYTE
) sTestpath1
, strlen(sTestpath1
)+1), "RegSetValueExA failed\n");
93 ok(!RegSetValueExA(hKey
,"Test2",0,REG_SZ
, (LPBYTE
) sTestpath1
, strlen(sTestpath1
)+1), "RegSetValueExA failed\n");
94 ok(!RegSetValueExA(hKey
,"Test3",0,REG_EXPAND_SZ
, (LPBYTE
) sTestpath2
, strlen(sTestpath2
)+1), "RegSetValueExA failed\n");
97 nExpLen1
= ExpandEnvironmentStringsA(sTestpath1
, sExpTestpath1
, sizeof(sExpTestpath1
));
98 nExpLen2
= ExpandEnvironmentStringsA(sTestpath2
, sExpTestpath2
, sizeof(sExpTestpath2
));
100 nExpectedLen1
= strlen(sTestpath1
) - strlen("%LONGSYSTEMVAR%") + strlen(sEnvvar1
) + 1;
101 nExpectedLen2
= strlen(sTestpath2
) - strlen("%FOO%") + strlen(sEnvvar2
) + 1;
102 /* ExpandEnvironmentStringsA on NT4 returns 2x the correct result */
103 trace("sExplen1 = (%d)\n", nExpLen1
);
104 if (nExpectedLen1
!= nExpLen1
)
105 trace( "Expanding %s failed (expected %d) - known bug in NT4\n", sTestpath1
, nExpectedLen1
);
107 trace("sExplen2 = (%d)\n", nExpLen2
);
108 if (nExpectedLen2
!= nExpLen2
)
109 trace( "Expanding %s failed (expected %d) - known bug in NT4\n", sTestpath2
, nExpectedLen2
);
111 /* Make sure we carry on with correct values */
112 nExpLen1
= nExpectedLen1
;
113 nExpLen2
= nExpectedLen2
;
117 static void test_SHGetValue(void)
124 strcpy(buf
, sEmptyBuffer
);
127 dwRet
= SHGetValueA(HKEY_CURRENT_USER
, REG_TEST_KEY
, "Test1", &dwType
, buf
, &dwSize
);
128 ok( ERROR_SUCCESS
== dwRet
, "SHGetValueA failed, ret=%u\n", dwRet
);
129 ok( 0 == strcmp(sExpTestpath1
, buf
) ||
130 broken(0 == strcmp(sTestpath1
, buf
)), /* IE4.x */
131 "Comparing of (%s) with (%s) failed\n", buf
, sExpTestpath1
);
132 ok( REG_SZ
== dwType
||
133 broken(REG_EXPAND_SZ
== dwType
), /* IE4.x */
134 "Expected REG_SZ, got (%u)\n", dwType
);
136 strcpy(buf
, sEmptyBuffer
);
139 dwRet
= SHGetValueA(HKEY_CURRENT_USER
, REG_TEST_KEY
, "Test2", &dwType
, buf
, &dwSize
);
140 ok( ERROR_SUCCESS
== dwRet
, "SHGetValueA failed, ret=%u\n", dwRet
);
141 ok( 0 == strcmp(sTestpath1
, buf
) , "Comparing of (%s) with (%s) failed\n", buf
, sTestpath1
);
142 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%u)\n", dwType
);
145 static void test_SHRegGetValue(void)
155 ret
= pSHRegGetValueA(HKEY_CURRENT_USER
, REG_TEST_KEY
, "Test1", SRRF_RT_REG_EXPAND_SZ
, &type
, data
, &size
);
156 ok(ret
== ERROR_INVALID_PARAMETER
, "SHRegGetValue failed, ret=%u\n", ret
);
159 ret
= pSHRegGetValueA(HKEY_CURRENT_USER
, REG_TEST_KEY
, "Test1", SRRF_RT_REG_SZ
, &type
, data
, &size
);
160 ok(ret
== ERROR_SUCCESS
, "SHRegGetValue failed, ret=%u\n", ret
);
161 ok(!strcmp(data
, sExpTestpath1
), "data = %s, expected %s\n", data
, sExpTestpath1
);
162 ok(type
== REG_SZ
, "type = %d, expected REG_SZ\n", type
);
165 ret
= pSHRegGetValueA(HKEY_CURRENT_USER
, REG_TEST_KEY
, "Test1", SRRF_RT_REG_DWORD
, &type
, data
, &size
);
166 ok(ret
== ERROR_UNSUPPORTED_TYPE
, "SHRegGetValue failed, ret=%u\n", ret
);
169 ret
= pSHRegGetValueA(HKEY_CURRENT_USER
, REG_TEST_KEY
, "Test2", SRRF_RT_REG_EXPAND_SZ
, &type
, data
, &size
);
170 ok(ret
== ERROR_INVALID_PARAMETER
, "SHRegGetValue failed, ret=%u\n", ret
);
173 ret
= pSHRegGetValueA(HKEY_CURRENT_USER
, REG_TEST_KEY
, "Test2", SRRF_RT_REG_SZ
, &type
, data
, &size
);
174 ok(ret
== ERROR_SUCCESS
, "SHRegGetValue failed, ret=%u\n", ret
);
175 ok(!strcmp(data
, sTestpath1
), "data = %s, expected %s\n", data
, sTestpath1
);
176 ok(type
== REG_SZ
, "type = %d, expected REG_SZ\n", type
);
179 ret
= pSHRegGetValueA(HKEY_CURRENT_USER
, REG_TEST_KEY
, "Test2", SRRF_RT_REG_QWORD
, &type
, data
, &size
);
180 ok(ret
== ERROR_UNSUPPORTED_TYPE
, "SHRegGetValue failed, ret=%u\n", ret
);
183 static void test_SHGetRegPath(void)
191 strcpy(buf
, sEmptyBuffer
);
192 dwRet
= (*pSHRegGetPathA
)(HKEY_CURRENT_USER
, REG_TEST_KEY
, "Test1", buf
, 0);
193 ok( ERROR_SUCCESS
== dwRet
, "SHRegGetPathA failed, ret=%u\n", dwRet
);
194 ok( 0 == strcmp(sExpTestpath1
, buf
) , "Comparing (%s) with (%s) failed\n", buf
, sExpTestpath1
);
197 static void test_SHQueryValueEx(void)
204 const char * sTestedFunction
= "";
205 DWORD nUsedBuffer1
,nUsedBuffer2
;
207 sTestedFunction
= "RegOpenKeyExA";
208 dwRet
= RegOpenKeyExA(HKEY_CURRENT_USER
, REG_TEST_KEY
, 0, KEY_QUERY_VALUE
, &hKey
);
209 ok( ERROR_SUCCESS
== dwRet
, "%s failed, ret=%u\n", sTestedFunction
, dwRet
);
211 /****** SHQueryValueExA ******/
213 sTestedFunction
= "SHQueryValueExA";
214 nUsedBuffer1
= max(strlen(sExpTestpath1
)+1, strlen(sTestpath1
)+1);
215 nUsedBuffer2
= max(strlen(sExpTestpath2
)+1, strlen(sTestpath2
)+1);
217 * Case 1.1 All arguments are NULL
219 dwRet
= SHQueryValueExA( hKey
, "Test1", NULL
, NULL
, NULL
, NULL
);
220 ok( ERROR_SUCCESS
== dwRet
, "%s failed, ret=%u\n", sTestedFunction
, dwRet
);
223 * Case 1.2 dwType is set
226 dwRet
= SHQueryValueExA( hKey
, "Test1", NULL
, &dwType
, NULL
, NULL
);
227 ok( ERROR_SUCCESS
== dwRet
, "%s failed, ret=%u\n", sTestedFunction
, dwRet
);
228 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%u)\n", dwType
);
232 * dwExpanded < dwUnExpanded
235 dwRet
= SHQueryValueExA( hKey
, "Test1", NULL
, NULL
, NULL
, &dwSize
);
236 ok( ERROR_SUCCESS
== dwRet
, "%s failed, ret=%u\n", sTestedFunction
, dwRet
);
237 ok( dwSize
== nUsedBuffer1
, "Buffer sizes (%u) and (%u) are not equal\n", dwSize
, nUsedBuffer1
);
240 * dwExpanded > dwUnExpanded
243 dwRet
= SHQueryValueExA( hKey
, "Test3", NULL
, NULL
, NULL
, &dwSize
);
244 ok( ERROR_SUCCESS
== dwRet
, "%s failed, ret=%u\n", sTestedFunction
, dwRet
);
245 ok( dwSize
>= nUsedBuffer2
||
246 broken(dwSize
== (strlen(sTestpath2
) + 1)), /* < IE4.x */
247 "Buffer size (%u) should be >= (%u)\n", dwSize
, nUsedBuffer2
);
250 * Case 1 string shrinks during expanding
252 strcpy(buf
, sEmptyBuffer
);
255 dwRet
= SHQueryValueExA( hKey
, "Test1", NULL
, &dwType
, buf
, &dwSize
);
256 ok( ERROR_MORE_DATA
== dwRet
, "Expected ERROR_MORE_DATA, got (%u)\n", dwRet
);
257 ok( 0 == strcmp(sEmptyBuffer
, buf
) , "Comparing (%s) with (%s) failed\n", buf
, sEmptyBuffer
);
258 ok( dwSize
== nUsedBuffer1
, "Buffer sizes (%u) and (%u) are not equal\n", dwSize
, nUsedBuffer1
);
259 ok( REG_SZ
== dwType
||
260 broken(REG_EXPAND_SZ
== dwType
), /* < IE6 */
261 "Expected REG_SZ, got (%u)\n", dwType
);
264 * string grows during expanding
265 * dwSize is smaller than the size of the unexpanded string
267 strcpy(buf
, sEmptyBuffer
);
270 dwRet
= SHQueryValueExA( hKey
, "Test3", NULL
, &dwType
, buf
, &dwSize
);
271 ok( ERROR_MORE_DATA
== dwRet
, "Expected ERROR_MORE_DATA, got (%u)\n", dwRet
);
272 ok( 0 == strcmp(sEmptyBuffer
, buf
) , "Comparing (%s) with (%s) failed\n", buf
, sEmptyBuffer
);
273 ok( dwSize
>= nUsedBuffer2
||
274 broken(dwSize
== (strlen(sTestpath2
) + 1)), /* < IE6 */
275 "Buffer size (%u) should be >= (%u)\n", dwSize
, nUsedBuffer2
);
276 ok( REG_SZ
== dwType
||
277 broken(REG_EXPAND_SZ
== dwType
), /* < IE6 */
278 "Expected REG_SZ, got (%u)\n", dwType
);
281 * string grows during expanding
282 * dwSize is larger than the size of the unexpanded string, but
283 * smaller than the part before the backslash. If the unexpanded
284 * string fits into the buffer, it can get cut when expanded.
286 strcpy(buf
, sEmptyBuffer
);
287 dwSize
= strlen(sEnvvar2
) - 2;
289 dwRet
= SHQueryValueExA( hKey
, "Test3", NULL
, &dwType
, buf
, &dwSize
);
290 ok( ERROR_MORE_DATA
== dwRet
||
291 broken(ERROR_ENVVAR_NOT_FOUND
== dwRet
) || /* IE5.5 */
292 broken(ERROR_SUCCESS
== dwRet
), /* < IE5.5*/
293 "Expected ERROR_MORE_DATA, got (%u)\n", dwRet
);
297 ok( (0 == strcmp("", buf
)) || (0 == strcmp(sTestpath2
, buf
)),
298 "Expected empty or unexpanded string (win98), got (%s)\n", buf
);
301 ok( dwSize
>= nUsedBuffer2
||
302 broken(dwSize
== (strlen("") + 1)), /* < IE 5.5 */
303 "Buffer size (%u) should be >= (%u)\n", dwSize
, nUsedBuffer2
);
304 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%u)\n", dwType
);
307 * string grows during expanding
308 * dwSize is larger than the size of the part before the backslash,
309 * but smaller than the expanded string. If the unexpanded string fits
310 * into the buffer, it can get cut when expanded.
312 strcpy(buf
, sEmptyBuffer
);
313 dwSize
= nExpLen2
- 4;
315 dwRet
= SHQueryValueExA( hKey
, "Test3", NULL
, &dwType
, buf
, &dwSize
);
316 ok( ERROR_MORE_DATA
== dwRet
||
317 broken(ERROR_ENVVAR_NOT_FOUND
== dwRet
) || /* IE5.5 */
318 broken(ERROR_SUCCESS
== dwRet
), /* < IE5.5 */
319 "Expected ERROR_MORE_DATA, got (%u)\n", dwRet
);
323 ok( (0 == strcmp("", buf
)) || (0 == strcmp(sEnvvar2
, buf
)) ||
324 broken(0 == strcmp(sTestpath2
, buf
)), /* IE 5.5 */
325 "Expected empty or first part of the string \"%s\", got \"%s\"\n", sEnvvar2
, buf
);
328 ok( dwSize
>= nUsedBuffer2
||
329 broken(dwSize
== (strlen(sEnvvar2
) + 1)) || /* IE4.01 SP1 (W98) and IE5 (W98SE) */
330 broken(dwSize
== (strlen("") + 1)), /* IE4.01 (NT4) and IE5.x (W2K) */
331 "Buffer size (%u) should be >= (%u)\n", dwSize
, nUsedBuffer2
);
332 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%u)\n", dwType
);
335 * The buffer is NULL but the size is set
337 strcpy(buf
, sEmptyBuffer
);
340 dwRet
= SHQueryValueExA( hKey
, "Test3", NULL
, &dwType
, NULL
, &dwSize
);
341 ok( ERROR_SUCCESS
== dwRet
, "%s failed, ret=%u\n", sTestedFunction
, dwRet
);
342 ok( dwSize
>= nUsedBuffer2
||
343 broken(dwSize
== (strlen(sTestpath2
) + 1)), /* IE4.01 SP1 (Win98) */
344 "Buffer size (%u) should be >= (%u)\n", dwSize
, nUsedBuffer2
);
345 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%u)\n", dwType
);
350 static void test_SHCopyKey(void)
352 HKEY hKeySrc
, hKeyDst
;
357 win_skip("SHCopyKeyA is not available\n");
361 /* Delete existing destination sub keys */
363 if (!RegOpenKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\CopyDestination", &hKeyDst
) && hKeyDst
)
365 SHDeleteKeyA(hKeyDst
, NULL
);
366 RegCloseKey(hKeyDst
);
370 dwRet
= RegCreateKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\CopyDestination", &hKeyDst
);
371 if (dwRet
|| !hKeyDst
)
373 ok( 0, "Destination couldn't be created, RegCreateKeyA returned (%u)\n", dwRet
);
378 dwRet
= RegOpenKeyA(HKEY_LOCAL_MACHINE
, REG_CURRENT_VERSION
, &hKeySrc
);
379 if (dwRet
|| !hKeySrc
)
381 ok( 0, "Source couldn't be opened, RegOpenKeyA returned (%u)\n", dwRet
);
382 RegCloseKey(hKeyDst
);
386 dwRet
= (*pSHCopyKeyA
)(hKeySrc
, NULL
, hKeyDst
, 0);
387 ok ( ERROR_SUCCESS
== dwRet
, "Copy failed, ret=(%u)\n", dwRet
);
389 RegCloseKey(hKeySrc
);
390 RegCloseKey(hKeyDst
);
392 /* Check we copied the sub keys, i.e. something that's on every windows system (including Wine) */
394 dwRet
= RegOpenKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\CopyDestination\\Shell Folders", &hKeyDst
);
395 if (dwRet
|| !hKeyDst
)
397 ok ( 0, "Copy couldn't be opened, RegOpenKeyA returned (%u)\n", dwRet
);
401 /* And the we copied the values too */
402 ok(!SHQueryValueExA(hKeyDst
, "Common AppData", NULL
, NULL
, NULL
, NULL
), "SHQueryValueExA failed\n");
404 RegCloseKey(hKeyDst
);
407 static void test_SHDeleteKey(void)
409 HKEY hKeyTest
, hKeyS
;
413 if (!RegOpenKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
, &hKeyTest
))
415 if (!RegCreateKeyA(hKeyTest
, "ODBC", &hKeyS
))
419 if (!RegCreateKeyA(hKeyS
, "ODBC.INI", &hKeyO
))
423 if (!RegCreateKeyA(hKeyS
, "ODBCINST.INI", &hKeyO
))
431 RegCloseKey (hKeyTest
);
437 dwRet
= SHDeleteKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\ODBC");
438 ok ( ERROR_SUCCESS
== dwRet
, "SHDeleteKey failed, ret=(%u)\n", dwRet
);
440 dwRet
= RegOpenKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\ODBC", &hKeyS
);
441 ok ( ERROR_FILE_NOT_FOUND
== dwRet
, "SHDeleteKey did not delete\n");
443 if (dwRet
== ERROR_SUCCESS
)
447 ok( 0, "Could not set up SHDeleteKey test\n");
450 static void test_SHRegCreateUSKeyW(void)
452 static const WCHAR subkeyW
[] = {'s','u','b','k','e','y',0};
455 if (!pSHRegCreateUSKeyW
)
457 win_skip("SHRegCreateUSKeyW not available\n");
461 ret
= pSHRegCreateUSKeyW(subkeyW
, KEY_ALL_ACCESS
, NULL
, NULL
, SHREGSET_FORCE_HKCU
);
462 ok(ret
== ERROR_INVALID_PARAMETER
, "got %d\n", ret
);
467 HKEY hkey
= create_test_entries();
471 hshlwapi
= GetModuleHandleA("shlwapi.dll");
473 /* SHCreateStreamOnFileEx was introduced in shlwapi v6.0 */
474 if(!GetProcAddress(hshlwapi
, "SHCreateStreamOnFileEx")){
475 win_skip("Too old shlwapi version\n");
479 pSHCopyKeyA
= (void*)GetProcAddress(hshlwapi
,"SHCopyKeyA");
480 pSHRegGetPathA
= (void*)GetProcAddress(hshlwapi
,"SHRegGetPathA");
481 pSHRegGetValueA
= (void*)GetProcAddress(hshlwapi
,"SHRegGetValueA");
482 pSHRegCreateUSKeyW
= (void*)GetProcAddress(hshlwapi
, "SHRegCreateUSKeyW");
485 test_SHRegGetValue();
486 test_SHQueryValueEx();
490 test_SHRegCreateUSKeyW();
492 delete_key( hkey
, "Software\\Wine", "Test" );