2 * Unit tests for registry functions
4 * Copyright (c) 2002 Alexandre Julliard
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
24 #include "wine/test.h"
32 static HKEY hkey_main;
35 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
36 static const char * sTestpath2 = "%FOO%\\subdir1";
38 static DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
39 static DWORD (WINAPI *pRegDeleteTreeA)(HKEY,LPCSTR);
40 static NTSTATUS (WINAPI * pNtDeleteKey)(HANDLE);
41 static NTSTATUS (WINAPI * pRtlFormatCurrentUserKeyPath)(UNICODE_STRING*);
42 static NTSTATUS (WINAPI * pRtlFreeUnicodeString)(PUNICODE_STRING);
45 /* Debugging functions from wine/libs/wine/debug.c */
47 /* allocate some tmp string space */
48 /* FIXME: this is not 100% thread-safe */
49 static char *get_temp_buffer( int size )
51 static char *list[32];
56 idx = ++pos % (sizeof(list)/sizeof(list[0]));
58 ret = HeapReAlloc( GetProcessHeap(), 0, list[idx], size );
60 ret = HeapAlloc( GetProcessHeap(), 0, size );
61 if (ret) list[idx] = ret;
65 static const char *wine_debugstr_an( const char *str, int n )
67 static const char hex[16] = "0123456789abcdef";
71 if (!((ULONG_PTR)str >> 16))
73 if (!str) return "(null)";
74 res = get_temp_buffer( 6 );
75 sprintf( res, "#%04x", LOWORD(str) );
78 if (n == -1) n = strlen(str);
80 size = 10 + min( 300, n * 4 );
81 dst = res = get_temp_buffer( size );
83 while (n-- > 0 && dst <= res + size - 9)
85 unsigned char c = *str++;
88 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
89 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
90 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
91 case '"': *dst++ = '\\'; *dst++ = '"'; break;
92 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
94 if (c >= ' ' && c <= 126)
100 *dst++ = hex[(c >> 4) & 0x0f];
101 *dst++ = hex[c & 0x0f];
116 #define ADVAPI32_GET_PROC(func) \
117 p ## func = (void*)GetProcAddress(hadvapi32, #func)
119 static void InitFunctionPtrs(void)
121 HMODULE hntdll = GetModuleHandleA("ntdll.dll");
122 HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
124 /* This function was introduced with Windows 2003 SP1 */
125 ADVAPI32_GET_PROC(RegGetValueA);
126 ADVAPI32_GET_PROC(RegDeleteTreeA);
128 pRtlFormatCurrentUserKeyPath = (void *)GetProcAddress( hntdll, "RtlFormatCurrentUserKeyPath" );
129 pRtlFreeUnicodeString = (void *)GetProcAddress(hntdll, "RtlFreeUnicodeString");
130 pNtDeleteKey = (void *)GetProcAddress( hntdll, "NtDeleteKey" );
133 /* delete key and all its subkeys */
134 static DWORD delete_key( HKEY hkey )
139 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
142 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
144 ret = delete_key( tmp );
149 if (ret != ERROR_NO_MORE_ITEMS) return ret;
150 RegDeleteKeyA( hkey, "" );
154 static void setup_main_key(void)
156 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
158 assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
161 #define lok ok_(__FILE__, line)
162 #define test_hkey_main_Value_A(name, string, full_byte_len) _test_hkey_main_Value_A(__LINE__, name, string, full_byte_len)
163 static void _test_hkey_main_Value_A(int line, LPCSTR name, LPCSTR string,
166 DWORD ret, type, cbData;
172 /* When successful RegQueryValueExA() leaves GLE as is,
173 * so we must reset it to detect unimplemented functions.
175 SetLastError(0xdeadbeef);
176 ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
177 GLE = GetLastError();
178 lok(ret == ERROR_SUCCESS, "RegQueryValueExA/1 failed: %d, GLE=%d\n", ret, GLE);
179 /* It is wrong for the Ansi version to not be implemented */
180 ok(GLE == 0xdeadbeef, "RegQueryValueExA set GLE = %u\n", GLE);
181 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
183 str_byte_len = (string ? lstrlenA(string) : 0) + 1;
184 lok(type == REG_SZ, "RegQueryValueExA/1 returned type %d\n", type);
185 lok(cbData == full_byte_len || cbData == str_byte_len /* Win9x */,
186 "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
188 value = HeapAlloc(GetProcessHeap(), 0, cbData+1);
189 memset(value, 0xbd, cbData+1);
191 ret = RegQueryValueExA(hkey_main, name, NULL, &type, value, &cbData);
192 GLE = GetLastError();
193 lok(ret == ERROR_SUCCESS, "RegQueryValueExA/2 failed: %d, GLE=%d\n", ret, GLE);
196 /* When cbData == 0, RegQueryValueExA() should not modify the buffer */
197 lok(*value == 0xbd || (cbData == 1 && *value == '\0') /* Win9x */,
198 "RegQueryValueExA overflowed: cbData=%u *value=%02x\n", cbData, *value);
202 lok(memcmp(value, string, cbData) == 0, "RegQueryValueExA/2 failed: %s/%d != %s/%d\n",
203 wine_debugstr_an((char*)value, cbData), cbData,
204 wine_debugstr_an(string, full_byte_len), full_byte_len);
205 lok(*(value+cbData) == 0xbd, "RegQueryValueExA/2 overflowed at offset %u: %02x != bd\n", cbData, *(value+cbData));
207 HeapFree(GetProcessHeap(), 0, value);
210 #define test_hkey_main_Value_W(name, string, full_byte_len) _test_hkey_main_Value_W(__LINE__, name, string, full_byte_len)
211 static void _test_hkey_main_Value_W(int line, LPCWSTR name, LPCWSTR string,
214 DWORD ret, type, cbData;
219 /* When successful RegQueryValueExW() leaves GLE as is,
220 * so we must reset it to detect unimplemented functions.
222 SetLastError(0xdeadbeef);
223 ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
224 GLE = GetLastError();
225 lok(ret == ERROR_SUCCESS, "RegQueryValueExW/1 failed: %d, GLE=%d\n", ret, GLE);
226 if(GLE == ERROR_CALL_NOT_IMPLEMENTED)
228 win_skip("RegQueryValueExW() is not implemented\n");
232 lok(type == REG_SZ, "RegQueryValueExW/1 returned type %d\n", type);
233 lok(cbData == full_byte_len,
234 "cbData=%d instead of %d\n", cbData, full_byte_len);
236 /* Give enough space to overflow by one WCHAR */
237 value = HeapAlloc(GetProcessHeap(), 0, cbData+2);
238 memset(value, 0xbd, cbData+2);
240 ret = RegQueryValueExW(hkey_main, name, NULL, &type, value, &cbData);
241 GLE = GetLastError();
242 lok(ret == ERROR_SUCCESS, "RegQueryValueExW/2 failed: %d, GLE=%d\n", ret, GLE);
245 lok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
246 wine_dbgstr_wn((WCHAR*)value, cbData / sizeof(WCHAR)), cbData,
247 wine_dbgstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len);
249 /* This implies that when cbData == 0, RegQueryValueExW() should not modify the buffer */
250 lok(*(value+cbData) == 0xbd, "RegQueryValueExW/2 overflowed at %u: %02x != bd\n", cbData, *(value+cbData));
251 lok(*(value+cbData+1) == 0xbd, "RegQueryValueExW/2 overflowed at %u+1: %02x != bd\n", cbData, *(value+cbData+1));
252 HeapFree(GetProcessHeap(), 0, value);
255 static void test_set_value(void)
259 static const WCHAR name1W[] = {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
260 static const WCHAR name2W[] = {'S','o','m','e','I','n','t','r','a','Z','e','r','o','e','d','S','t','r','i','n','g', 0};
261 static const WCHAR emptyW[] = {0};
262 static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
263 static const WCHAR string2W[] = {'T','h','i','s', 0 ,'B','r','e','a','k','s', 0 , 0 ,'A', 0 , 0 , 0 , 'L','o','t', 0 , 0 , 0 , 0, 0};
264 static const WCHAR substring2W[] = {'T','h','i','s',0};
266 static const char name1A[] = "CleanSingleString";
267 static const char name2A[] = "SomeIntraZeroedString";
268 static const char emptyA[] = "";
269 static const char string1A[] = "ThisNeverBreaks";
270 static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
271 static const char substring2A[] = "This";
275 /* Crashes on NT4, Windows 2000 and XP SP1 */
276 ret = RegSetValueA(hkey_main, NULL, REG_SZ, NULL, 0);
277 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
280 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, sizeof(string1A));
281 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
282 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
283 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
285 /* RegSetValueA ignores the size passed in */
286 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, 4);
287 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
288 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
289 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
291 /* stops at first null */
292 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string2A, sizeof(string2A));
293 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
294 test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
295 test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
297 /* only REG_SZ is supported on NT*/
298 ret = RegSetValueA(hkey_main, NULL, REG_BINARY, string2A, sizeof(string2A));
299 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
300 ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
301 "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
303 ret = RegSetValueA(hkey_main, NULL, REG_EXPAND_SZ, string2A, sizeof(string2A));
304 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
305 ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
306 "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
308 ret = RegSetValueA(hkey_main, NULL, REG_MULTI_SZ, string2A, sizeof(string2A));
309 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
310 ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
311 "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
313 /* Test RegSetValueExA with a 'zero-byte' string (as Office 2003 does).
314 * Surprisingly enough we're supposed to get zero bytes out of it.
316 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, 0);
317 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
318 test_hkey_main_Value_A(name1A, NULL, 0);
319 test_hkey_main_Value_W(name1W, NULL, 0);
321 /* test RegSetValueExA with an empty string */
322 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, sizeof(emptyA));
323 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
324 test_hkey_main_Value_A(name1A, emptyA, sizeof(emptyA));
325 test_hkey_main_Value_W(name1W, emptyW, sizeof(emptyW));
327 /* test RegSetValueExA with off-by-one size */
328 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A)-sizeof(string1A[0]));
329 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
330 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
331 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
333 /* test RegSetValueExA with normal string */
334 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
335 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
336 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
337 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
339 /* test RegSetValueExA with intrazeroed string */
340 ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
341 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
342 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
343 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
345 /* 9x doesn't support W-calls, so don't test them then */
346 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
350 /* Crashes on NT4, Windows 2000 and XP SP1 */
351 ret = RegSetValueW(hkey_main, NULL, REG_SZ, NULL, 0);
352 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
355 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, sizeof(string1W));
356 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
357 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
358 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
360 /* RegSetValueA ignores the size passed in */
361 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, 4 * sizeof(string1W[0]));
362 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
363 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
364 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
366 /* stops at first null */
367 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string2W, sizeof(string2W));
368 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
369 test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
370 test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
372 /* only REG_SZ is supported */
373 ret = RegSetValueW(hkey_main, NULL, REG_BINARY, string2W, sizeof(string2W));
374 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
375 ret = RegSetValueW(hkey_main, NULL, REG_EXPAND_SZ, string2W, sizeof(string2W));
376 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
377 ret = RegSetValueW(hkey_main, NULL, REG_MULTI_SZ, string2W, sizeof(string2W));
378 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
380 /* test RegSetValueExW with off-by-one size */
381 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W)-sizeof(string1W[0]));
382 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
383 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
384 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
386 /* test RegSetValueExW with normal string */
387 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
388 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
389 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
390 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
392 /* test RegSetValueExW with intrazeroed string */
393 ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
394 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
395 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
396 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
399 static void create_test_entries(void)
401 static const DWORD qw[2] = { 0x12345678, 0x87654321 };
403 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
404 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
406 ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
407 "RegSetValueExA failed\n");
408 ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
409 "RegSetValueExA failed\n");
410 ok(!RegSetValueExA(hkey_main,"TP1_ZB_SZ",0,REG_SZ, (const BYTE *)"", 0),
411 "RegSetValueExA failed\n");
412 ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1),
413 "RegSetValueExA failed\n");
414 ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
415 "RegSetValueExA failed\n");
416 ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
417 "RegSetValueExA failed\n");
418 ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
419 "RegSetValueExA failed\n");
422 static void test_enum_value(void)
426 char value[20], data[20];
427 WCHAR valueW[20], dataW[20];
428 DWORD val_count, data_count, type;
429 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
430 static const WCHAR testW[] = {'T','e','s','t',0};
431 static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
433 /* create the working key for new 'Test' value */
434 res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
435 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
437 /* check NULL data with zero length */
438 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
439 if (GetVersion() & 0x80000000)
440 ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
442 ok( !res, "RegSetValueExA returned %d\n", res );
443 res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
444 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
445 res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
446 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
448 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
449 ok( res == 0, "RegSetValueExA failed error %d\n", res );
451 /* overflow both name and data */
455 strcpy( value, "xxxxxxxxxx" );
456 strcpy( data, "xxxxxxxxxx" );
457 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
458 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
459 ok( val_count == 2, "val_count set to %d\n", val_count );
460 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
461 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
462 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
463 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
469 strcpy( value, "xxxxxxxxxx" );
470 strcpy( data, "xxxxxxxxxx" );
471 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
472 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
473 /* Win9x returns 2 as specified by MSDN but NT returns 3... */
474 ok( val_count == 2 || val_count == 3, "val_count set to %d\n", val_count );
475 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
476 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
477 /* v5.1.2600.0 (XP Home and Professional) does not touch value or data in this case */
478 ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ),
479 "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
480 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
481 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
483 /* overflow empty name */
487 strcpy( value, "xxxxxxxxxx" );
488 strcpy( data, "xxxxxxxxxx" );
489 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
490 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
491 ok( val_count == 0, "val_count set to %d\n", val_count );
492 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
493 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
494 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
495 /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
496 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
497 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
503 strcpy( value, "xxxxxxxxxx" );
504 strcpy( data, "xxxxxxxxxx" );
505 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
506 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
507 ok( val_count == 20, "val_count set to %d\n", val_count );
508 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
509 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
510 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
511 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
517 strcpy( value, "xxxxxxxxxx" );
518 strcpy( data, "xxxxxxxxxx" );
519 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
520 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
521 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
522 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
523 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
524 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
525 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
529 SetLastError(0xdeadbeef);
530 res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
531 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
533 win_skip("RegSetValueExW is not implemented\n");
536 ok( res == 0, "RegSetValueExW failed error %d\n", res );
538 /* overflow both name and data */
542 memcpy( valueW, xxxW, sizeof(xxxW) );
543 memcpy( dataW, xxxW, sizeof(xxxW) );
544 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
545 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
546 ok( val_count == 2, "val_count set to %d\n", val_count );
547 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
548 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
549 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
550 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
556 memcpy( valueW, xxxW, sizeof(xxxW) );
557 memcpy( dataW, xxxW, sizeof(xxxW) );
558 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
559 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
560 ok( val_count == 3, "val_count set to %d\n", val_count );
561 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
562 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
563 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
564 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
570 memcpy( valueW, xxxW, sizeof(xxxW) );
571 memcpy( dataW, xxxW, sizeof(xxxW) );
572 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
573 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
574 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
575 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
576 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
577 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
578 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
584 memcpy( valueW, xxxW, sizeof(xxxW) );
585 memcpy( dataW, xxxW, sizeof(xxxW) );
586 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
587 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
588 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
589 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
590 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
591 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
592 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
595 RegDeleteKeyA(test_key, "");
596 RegCloseKey(test_key);
599 static void test_query_value_ex(void)
606 ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
607 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
608 ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
609 ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
613 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
614 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
615 /* the type parameter is cleared on Win9x, but is set to a random value on
616 * NT, so don't do that test there. The size parameter is left untouched on Win9x
617 * but cleared on NT+, this can be tested on all platforms.
619 if (GetVersion() & 0x80000000)
621 ok(type == 0, "type should have been set to 0 instead of 0x%x\n", type);
622 ok(size == 0xdeadbeef, "size should have been left untouched (0xdeadbeef)\n");
626 trace("test_query_value_ex: type set to: 0x%08x\n", type);
627 ok(size == 0, "size should have been set to 0 instead of %d\n", size);
630 size = sizeof(buffer);
631 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
632 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
633 ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
636 ret = RegQueryValueExA(hkey_main, "BIN32", NULL, &size, buffer, &size);
637 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
640 static void test_get_value(void)
647 CHAR expanded[] = "bar\\subdir1";
648 CHAR expanded2[] = "ImARatherLongButIndeedNeededString\\subdir1";
652 win_skip("RegGetValue not available on this platform\n");
656 /* Invalid parameter */
657 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
658 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
660 /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
661 size = type = dw = 0xdeadbeef;
662 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
663 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
664 ok(size == 4, "size=%d\n", size);
665 ok(type == REG_DWORD, "type=%d\n", type);
666 ok(dw == 0x12345678, "dw=%d\n", dw);
668 /* Query by subkey-name */
669 ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
670 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
672 /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
673 size = type = dw = 0xdeadbeef;
674 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
675 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
676 /* Although the function failed all values are retrieved */
677 ok(size == 4, "size=%d\n", size);
678 ok(type == REG_DWORD, "type=%d\n", type);
679 ok(dw == 0x12345678, "dw=%d\n", dw);
681 /* Test RRF_ZEROONFAILURE */
682 type = dw = 0xdeadbeef; size = 4;
683 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
684 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
685 /* Again all values are retrieved ... */
686 ok(size == 4, "size=%d\n", size);
687 ok(type == REG_DWORD, "type=%d\n", type);
688 /* ... except the buffer, which is zeroed out */
689 ok(dw == 0, "dw=%d\n", dw);
691 /* Test RRF_ZEROONFAILURE with a NULL buffer... */
692 type = size = 0xbadbeef;
693 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
694 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
695 ok(size == 4, "size=%d\n", size);
696 ok(type == REG_DWORD, "type=%d\n", type);
698 /* Query REG_DWORD using RRF_RT_DWORD (ok) */
699 size = type = dw = 0xdeadbeef;
700 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
701 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
702 ok(size == 4, "size=%d\n", size);
703 ok(type == REG_DWORD, "type=%d\n", type);
704 ok(dw == 0x12345678, "dw=%d\n", dw);
706 /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
707 size = type = dw = 0xdeadbeef;
708 ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
709 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
710 ok(size == 4, "size=%d\n", size);
711 ok(type == REG_BINARY, "type=%d\n", type);
712 ok(dw == 0x12345678, "dw=%d\n", dw);
714 /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
715 qw[0] = qw[1] = size = type = 0xdeadbeef;
716 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
717 ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
718 ok(size == 8, "size=%d\n", size);
719 ok(type == REG_BINARY, "type=%d\n", type);
720 ok(qw[0] == 0x12345678 &&
721 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
723 /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
724 type = dw = 0xdeadbeef; size = 4;
725 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
726 ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
727 ok(dw == 0xdeadbeef, "dw=%d\n", dw);
728 ok(size == 8, "size=%d\n", size);
730 /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
731 qw[0] = qw[1] = size = type = 0xdeadbeef;
732 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
733 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
734 ok(size == 8, "size=%d\n", size);
735 ok(type == REG_BINARY, "type=%d\n", type);
736 ok(qw[0] == 0x12345678 &&
737 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
739 /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
740 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
741 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
742 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
743 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
744 ok(type == REG_SZ, "type=%d\n", type);
745 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
747 /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
748 type = 0xdeadbeef; size = 0;
749 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
750 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
751 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
752 ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
753 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
754 ok(type == REG_SZ, "type=%d\n", type);
756 /* Query REG_SZ using RRF_RT_REG_SZ on a zero-byte value (ok) */
757 strcpy(buf, sTestpath1);
760 ret = pRegGetValueA(hkey_main, NULL, "TP1_ZB_SZ", RRF_RT_REG_SZ, &type, buf, &size);
761 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
762 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
764 size == 1, /* win2k3 */
766 ok(type == REG_SZ, "type=%d\n", type);
767 ok(!strcmp(sTestpath1, buf) ||
769 "Expected \"%s\" or \"\", got \"%s\"\n", sTestpath1, buf);
771 /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
772 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
773 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
774 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
775 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
776 ok(type == REG_SZ, "type=%d\n", type);
777 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
779 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
781 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
782 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
783 ok((size == strlen(expanded2)+1) || /* win2k3 SP1 */
784 (size == strlen(expanded2)+2) || /* win2k3 SP2 */
785 (size == strlen(sTestpath2)+1),
786 "strlen(expanded2)=%d, strlen(sTestpath2)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
788 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
789 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
790 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
791 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
792 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
793 ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
794 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
795 ok(type == REG_SZ, "type=%d\n", type);
796 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
798 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands a lot) */
799 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
800 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
801 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
802 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
803 ok(size == strlen(expanded2)+1 || broken(size == strlen(sTestpath2)+1),
804 "strlen(expanded2)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
805 ok(type == REG_SZ, "type=%d\n", type);
806 ok(!strcmp(expanded2, buf), "expanded2=\"%s\" buf=\"%s\"\n", expanded2, buf);
808 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
809 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
810 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
811 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
812 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
813 ok(type == REG_EXPAND_SZ, "type=%d\n", type);
814 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
816 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
818 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
819 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
820 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
821 ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
822 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
824 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
825 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
826 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
828 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
829 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
830 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
832 /* Query REG_EXPAND_SZ using RRF_RT_ANY */
833 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
834 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_ANY, &type, buf, &size);
835 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
836 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
837 ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
838 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
839 ok(type == REG_SZ, "type=%d\n", type);
840 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
843 static void test_reg_open_key(void)
846 HKEY hkResult = NULL;
847 HKEY hkPreserve = NULL;
849 /* successful open */
850 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
851 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
852 ok(hkResult != NULL, "expected hkResult != NULL\n");
853 hkPreserve = hkResult;
855 /* these tests fail on Win9x, but we want to be compatible with NT, so
856 * run them if we can */
857 if (!(GetVersion() & 0x80000000))
859 /* open same key twice */
860 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
861 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
862 ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
863 ok(hkResult != NULL, "hkResult != NULL\n");
864 RegCloseKey(hkResult);
866 /* open nonexistent key
867 * check that hkResult is set to NULL
869 hkResult = hkPreserve;
870 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
871 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
872 ok(hkResult == NULL, "expected hkResult == NULL\n");
874 /* open the same nonexistent key again to make sure the key wasn't created */
875 hkResult = hkPreserve;
876 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
877 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
878 ok(hkResult == NULL, "expected hkResult == NULL\n");
880 /* send in NULL lpSubKey
881 * check that hkResult receives the value of hKey
883 hkResult = hkPreserve;
884 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
885 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
886 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
888 /* send empty-string in lpSubKey */
889 hkResult = hkPreserve;
890 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
891 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
892 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
894 /* send in NULL lpSubKey and NULL hKey
895 * hkResult is set to NULL
897 hkResult = hkPreserve;
898 ret = RegOpenKeyA(NULL, NULL, &hkResult);
899 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
900 ok(hkResult == NULL, "expected hkResult == NULL\n");
903 /* only send NULL hKey
904 * the value of hkResult remains unchanged
906 hkResult = hkPreserve;
907 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
908 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
909 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
910 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
911 RegCloseKey(hkResult);
913 /* send in NULL hkResult */
914 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
915 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
917 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, NULL);
918 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
920 ret = RegOpenKeyA(NULL, NULL, NULL);
921 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
923 /* beginning backslash character */
924 ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
925 ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
926 ret == ERROR_FILE_NOT_FOUND || /* Win9x,ME */
927 broken(ret == ERROR_SUCCESS), /* wow64 */
928 "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
929 if (!ret) RegCloseKey(hkResult);
932 ret = RegOpenKeyExA(HKEY_CLASSES_ROOT, "\\clsid", 0, KEY_QUERY_VALUE, &hkResult);
933 ok(ret == ERROR_SUCCESS || /* 2k/XP */
934 ret == ERROR_BAD_PATHNAME || /* NT */
935 ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
936 , "expected ERROR_SUCCESS, ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
937 RegCloseKey(hkResult);
941 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_32KEY, &hkResult);
942 ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
943 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
944 RegCloseKey(hkResult);
947 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_64KEY, &hkResult);
948 ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
949 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
950 RegCloseKey(hkResult);
953 static void test_reg_create_key(void)
957 ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
958 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
959 /* should succeed: all versions of Windows ignore the access rights
960 * to the parent handle */
961 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
962 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
965 RegDeleteKey(hkey2, "");
966 RegDeleteKey(hkey1, "");
970 /* test creation of volatile keys */
971 ret = RegCreateKeyExA(hkey_main, "Volatile", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey1, NULL);
972 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
973 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
974 ok(ret == ERROR_CHILD_MUST_BE_VOLATILE || broken(!ret), /* win9x */
975 "RegCreateKeyExA failed with error %d\n", ret);
976 if (!ret) RegCloseKey( hkey2 );
977 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
978 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
980 /* should succeed if the key already exists */
981 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey2, NULL);
982 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
985 RegDeleteKey(hkey2, "");
986 RegDeleteKey(hkey1, "");
990 /* beginning backslash character */
991 ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
992 if (!(GetVersion() & 0x80000000))
993 ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
995 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
996 RegDeleteKey(hkey1, NULL);
1000 /* WOW64 flags - open an existing key */
1002 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_32KEY, NULL, &hkey1, NULL);
1003 ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1004 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1008 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_64KEY, NULL, &hkey1, NULL);
1009 ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1010 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1014 static void test_reg_close_key(void)
1019 /* successfully close key
1020 * hkHandle remains changed after call to RegCloseKey
1022 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
1023 ret = RegCloseKey(hkHandle);
1024 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1026 /* try to close the key twice */
1027 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
1028 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
1029 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
1031 /* try to close a NULL handle */
1032 ret = RegCloseKey(NULL);
1033 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
1034 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1036 /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
1037 * win98 doesn't give a new handle when the same key is opened.
1038 * Not re-opening will make some next tests fail.
1040 if (hkey_main == hkHandle)
1042 trace("The main handle is most likely closed, so re-opening\n");
1043 RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
1047 static void test_reg_delete_key(void)
1051 ret = RegDeleteKey(hkey_main, NULL);
1053 /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
1054 * there are also no subkeys available it will delete the key pointed to by hkey_main.
1055 * Not re-creating will make some next tests fail.
1057 if (ret == ERROR_SUCCESS)
1059 trace("We are probably running on NT4 or W2K as the main key is deleted,"
1060 " re-creating the main key\n");
1064 ok(ret == ERROR_INVALID_PARAMETER ||
1065 ret == ERROR_ACCESS_DENIED ||
1066 ret == ERROR_BADKEY, /* Win95 */
1070 static void test_reg_save_key(void)
1074 ret = RegSaveKey(hkey_main, "saved_key", NULL);
1075 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1078 static void test_reg_load_key(void)
1083 ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
1084 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1086 ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
1087 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1089 RegCloseKey(hkHandle);
1092 static void test_reg_unload_key(void)
1096 ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
1097 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1099 DeleteFile("saved_key");
1100 DeleteFile("saved_key.LOG");
1103 static BOOL set_privileges(LPCSTR privilege, BOOL set)
1105 TOKEN_PRIVILEGES tp;
1109 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1112 if(!LookupPrivilegeValue(NULL, privilege, &luid))
1114 CloseHandle(hToken);
1118 tp.PrivilegeCount = 1;
1119 tp.Privileges[0].Luid = luid;
1122 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1124 tp.Privileges[0].Attributes = 0;
1126 AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
1127 if (GetLastError() != ERROR_SUCCESS)
1129 CloseHandle(hToken);
1133 CloseHandle(hToken);
1137 /* tests that show that RegConnectRegistry and
1138 OpenSCManager accept computer names without the
1139 \\ prefix (what MSDN says). */
1140 static void test_regconnectregistry( void)
1142 CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1143 CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
1144 DWORD len = sizeof(compName) ;
1150 SetLastError(0xdeadbeef);
1151 ret = GetComputerNameA(compName, &len);
1152 ok( ret, "GetComputerName failed err = %d\n", GetLastError());
1155 lstrcpyA(netwName, "\\\\");
1156 lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1158 retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1160 retl == ERROR_DLL_INIT_FAILED ||
1161 retl == ERROR_BAD_NETPATH, /* some win2k */
1162 "RegConnectRegistryA failed err = %d\n", retl);
1163 if( !retl) RegCloseKey( hkey);
1165 retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1167 retl == ERROR_DLL_INIT_FAILED ||
1168 retl == ERROR_BAD_NETPATH, /* some win2k */
1169 "RegConnectRegistryA failed err = %d\n", retl);
1170 if( !retl) RegCloseKey( hkey);
1172 SetLastError(0xdeadbeef);
1173 schnd = OpenSCManagerA( compName, NULL, GENERIC_READ);
1174 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1176 win_skip("OpenSCManagerA is not implemented\n");
1180 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1181 CloseServiceHandle( schnd);
1183 SetLastError(0xdeadbeef);
1184 schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ);
1185 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1186 CloseServiceHandle( schnd);
1190 static void test_reg_query_value(void)
1197 static const WCHAR expected[] = {'d','a','t','a',0};
1199 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1200 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1202 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1203 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1205 /* try an invalid hkey */
1206 SetLastError(0xdeadbeef);
1208 ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1209 ok(ret == ERROR_INVALID_HANDLE ||
1210 ret == ERROR_BADKEY || /* Windows 98 returns BADKEY */
1211 ret == ERROR_ACCESS_DENIED, /* non-admin winxp */
1212 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1213 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1215 /* try a NULL hkey */
1216 SetLastError(0xdeadbeef);
1218 ret = RegQueryValueA(NULL, "subkey", val, &size);
1219 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1220 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1221 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1223 /* try a NULL value */
1225 ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1226 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1227 ok(size == 5, "Expected 5, got %d\n", size);
1229 /* try a NULL size */
1230 SetLastError(0xdeadbeef);
1232 ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1233 ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1234 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1235 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1237 /* try a NULL value and size */
1238 ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1239 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1241 /* try a size too small */
1242 SetLastError(0xdeadbeef);
1245 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1246 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1247 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1248 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1249 ok(size == 5, "Expected 5, got %d\n", size);
1251 /* successfully read the value using 'subkey' */
1253 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1254 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1255 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1256 ok(size == 5, "Expected 5, got %d\n", size);
1258 /* successfully read the value using the subkey key */
1260 ret = RegQueryValueA(subkey, NULL, val, &size);
1261 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1262 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1263 ok(size == 5, "Expected 5, got %d\n", size);
1265 /* unicode - try size too small */
1266 SetLastError(0xdeadbeef);
1269 ret = RegQueryValueW(subkey, NULL, valW, &size);
1270 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1272 win_skip("RegQueryValueW is not implemented\n");
1275 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1276 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1277 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1278 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1280 /* unicode - try size in WCHARS */
1281 SetLastError(0xdeadbeef);
1282 size = sizeof(valW) / sizeof(WCHAR);
1283 ret = RegQueryValueW(subkey, NULL, valW, &size);
1284 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1285 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1286 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1287 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1289 /* unicode - successfully read the value */
1290 size = sizeof(valW);
1291 ret = RegQueryValueW(subkey, NULL, valW, &size);
1292 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1293 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1294 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1296 /* unicode - set the value without a NULL terminator */
1297 ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1298 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1300 /* unicode - read the unterminated value, value is terminated for us */
1301 memset(valW, 'a', sizeof(valW));
1302 size = sizeof(valW);
1303 ret = RegQueryValueW(subkey, NULL, valW, &size);
1304 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1305 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1306 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1309 RegDeleteKeyA(subkey, "");
1310 RegCloseKey(subkey);
1313 static void test_string_termination(void)
1317 static const char string[] = "FullString";
1320 DWORD insize, outsize, nsize;
1322 ret = RegCreateKeyA(hkey_main, "string_termination", &subkey);
1323 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1325 /* Off-by-one RegSetValueExA -> adds a trailing '\0'! */
1326 insize=sizeof(string)-1;
1327 ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1328 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1330 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1331 ok(ret == ERROR_MORE_DATA, "RegQueryValueExA returned: %d\n", ret);
1333 /* Off-by-two RegSetValueExA -> no trailing '\0', except on Win9x */
1334 insize=sizeof(string)-2;
1335 ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1336 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1338 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, NULL, &outsize);
1339 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1340 ok(outsize == insize || broken(outsize == sizeof(string)) /* Win9x */,
1341 "wrong size %u != %u\n", outsize, insize);
1343 if (outsize == insize)
1345 /* RegQueryValueExA may return a string with no trailing '\0' */
1347 memset(buffer, 0xbd, sizeof(buffer));
1348 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1349 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1350 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1351 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1352 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1353 ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1355 /* RegQueryValueExA adds a trailing '\0' if there is room */
1357 memset(buffer, 0xbd, sizeof(buffer));
1358 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1359 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1360 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1361 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1362 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1363 ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1365 /* RegEnumValueA may return a string with no trailing '\0' */
1367 memset(buffer, 0xbd, sizeof(buffer));
1369 ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1370 ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1371 ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1372 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1373 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1374 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1375 ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1377 /* RegEnumValueA adds a trailing '\0' if there is room */
1379 memset(buffer, 0xbd, sizeof(buffer));
1381 ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1382 ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1383 ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1384 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1385 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1386 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1387 ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1390 RegDeleteKeyA(subkey, "");
1391 RegCloseKey(subkey);
1394 static void test_reg_delete_tree(void)
1396 CHAR buffer[MAX_PATH];
1397 HKEY subkey, subkey2;
1400 if(!pRegDeleteTreeA) {
1401 win_skip("Skipping RegDeleteTreeA tests, function not present\n");
1405 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1406 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1407 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1408 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1409 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1410 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1411 ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1412 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1413 ret = RegCloseKey(subkey2);
1414 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1416 ret = pRegDeleteTreeA(subkey, "subkey2");
1417 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1418 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1419 "subkey2 was not deleted\n");
1421 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1422 "Default value of subkey not longer present\n");
1424 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1425 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1426 ret = RegCloseKey(subkey2);
1427 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1428 ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1429 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1430 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1431 "subkey2 was not deleted\n");
1432 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1433 "Default value of subkey not longer present\n");
1435 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1436 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1437 ret = RegCloseKey(subkey2);
1438 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1439 ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1440 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1441 ret = RegCloseKey(subkey2);
1442 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1443 ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1444 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1445 ret = pRegDeleteTreeA(subkey, NULL);
1446 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1447 ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1448 "subkey was deleted\n");
1449 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1450 "subkey2 was not deleted\n");
1451 ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1452 "subkey3 was not deleted\n");
1454 ret = RegQueryValueA(subkey, NULL, buffer, &size);
1455 ok(ret == ERROR_SUCCESS,
1456 "Default value of subkey is not present\n");
1457 ok(!lstrlenA(buffer),
1458 "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1460 ok(RegQueryValueA(subkey, "value", buffer, &size),
1461 "Value is still present\n");
1463 ret = pRegDeleteTreeA(hkey_main, "not-here");
1464 ok(ret == ERROR_FILE_NOT_FOUND,
1465 "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1468 static void test_rw_order(void)
1472 static char keyname[] = "test_rw_order";
1474 DWORD values, value_len, value_name_max_len;
1477 RegDeleteKeyA(HKEY_CURRENT_USER, keyname);
1478 ret = RegCreateKeyA(HKEY_CURRENT_USER, keyname, &hKey);
1479 if(ret != ERROR_SUCCESS) {
1480 skip("Couldn't create key. Skipping.\n");
1484 ok(!RegSetValueExA(hKey, "A", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1485 "RegSetValueExA for value \"A\" failed\n");
1486 ok(!RegSetValueExA(hKey, "C", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1487 "RegSetValueExA for value \"C\" failed\n");
1488 ok(!RegSetValueExA(hKey, "D", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1489 "RegSetValueExA for value \"D\" failed\n");
1490 ok(!RegSetValueExA(hKey, "B", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1491 "RegSetValueExA for value \"B\" failed\n");
1493 ok(!RegQueryInfoKeyA(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &values,
1494 &value_name_max_len, NULL, NULL, NULL), "RegQueryInfoKeyA failed\n");
1495 ok(values == 4, "Expected 4 values, got %u\n", values);
1497 /* Value enumeration preserves RegSetValueEx call order */
1499 ok(!RegEnumValueA(hKey, 0, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1500 ok(strcmp(value_buf, "A") == 0, "Expected name \"A\", got %s\n", value_buf);
1502 ok(!RegEnumValueA(hKey, 1, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1503 todo_wine ok(strcmp(value_buf, "C") == 0, "Expected name \"C\", got %s\n", value_buf);
1505 ok(!RegEnumValueA(hKey, 2, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1506 todo_wine ok(strcmp(value_buf, "D") == 0, "Expected name \"D\", got %s\n", value_buf);
1508 ok(!RegEnumValueA(hKey, 3, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1509 todo_wine ok(strcmp(value_buf, "B") == 0, "Expected name \"B\", got %s\n", value_buf);
1511 ok(!RegDeleteKey(HKEY_CURRENT_USER, keyname), "Failed to delete key\n");
1514 static void test_symlinks(void)
1516 static const WCHAR targetW[] = {'\\','S','o','f','t','w','a','r','e','\\','W','i','n','e',
1517 '\\','T','e','s','t','\\','t','a','r','g','e','t',0};
1519 UNICODE_STRING target_str;
1523 DWORD target_len, type, len, dw, err;
1525 if (!pRtlFormatCurrentUserKeyPath || !pNtDeleteKey)
1527 win_skip( "Can't perform symlink tests\n" );
1531 pRtlFormatCurrentUserKeyPath( &target_str );
1533 target_len = target_str.Length + sizeof(targetW);
1534 target = HeapAlloc( GetProcessHeap(), 0, target_len );
1535 memcpy( target, target_str.Buffer, target_str.Length );
1536 memcpy( target + target_str.Length/sizeof(WCHAR), targetW, sizeof(targetW) );
1538 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK,
1539 KEY_ALL_ACCESS, NULL, &link, NULL );
1540 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed: %u\n", err );
1542 /* REG_SZ is not allowed */
1543 err = RegSetValueExA( link, "SymbolicLinkValue", 0, REG_SZ, (BYTE *)"foobar", sizeof("foobar") );
1544 ok( err == ERROR_ACCESS_DENIED, "RegSetValueEx wrong error %u\n", err );
1545 err = RegSetValueExA( link, "SymbolicLinkValue", 0, REG_LINK,
1546 (BYTE *)target, target_len - sizeof(WCHAR) );
1547 ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1548 /* other values are not allowed */
1549 err = RegSetValueExA( link, "link", 0, REG_LINK, (BYTE *)target, target_len - sizeof(WCHAR) );
1550 ok( err == ERROR_ACCESS_DENIED, "RegSetValueEx wrong error %u\n", err );
1552 /* try opening the target through the link */
1554 err = RegOpenKeyA( hkey_main, "link", &key );
1555 ok( err == ERROR_FILE_NOT_FOUND, "RegOpenKey wrong error %u\n", err );
1557 err = RegCreateKeyExA( hkey_main, "target", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
1558 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1561 err = RegSetValueExA( key, "value", 0, REG_DWORD, (BYTE *)&dw, sizeof(dw) );
1562 ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1565 err = RegOpenKeyA( hkey_main, "link", &key );
1566 ok( err == ERROR_SUCCESS, "RegOpenKey failed error %u\n", err );
1568 len = sizeof(buffer);
1569 err = RegQueryValueExA( key, "value", NULL, &type, buffer, &len );
1570 ok( err == ERROR_SUCCESS, "RegOpenKey failed error %u\n", err );
1571 ok( len == sizeof(DWORD), "wrong len %u\n", len );
1573 len = sizeof(buffer);
1574 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1575 ok( err == ERROR_FILE_NOT_FOUND, "RegQueryValueEx wrong error %u\n", err );
1577 /* REG_LINK can be created in non-link keys */
1578 err = RegSetValueExA( key, "SymbolicLinkValue", 0, REG_LINK,
1579 (BYTE *)target, target_len - sizeof(WCHAR) );
1580 ok( err == ERROR_SUCCESS, "RegSetValueEx failed error %u\n", err );
1581 len = sizeof(buffer);
1582 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1583 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1584 ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1585 err = RegDeleteValueA( key, "SymbolicLinkValue" );
1586 ok( err == ERROR_SUCCESS, "RegDeleteValue failed error %u\n", err );
1590 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &key, NULL );
1591 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1593 len = sizeof(buffer);
1594 err = RegQueryValueExA( key, "value", NULL, &type, buffer, &len );
1595 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1596 ok( len == sizeof(DWORD), "wrong len %u\n", len );
1598 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1599 ok( err == ERROR_FILE_NOT_FOUND, "RegQueryValueEx wrong error %u\n", err );
1602 /* now open the symlink itself */
1604 err = RegOpenKeyExA( hkey_main, "link", REG_OPTION_OPEN_LINK, KEY_ALL_ACCESS, &key );
1605 ok( err == ERROR_SUCCESS, "RegOpenKeyEx failed error %u\n", err );
1606 len = sizeof(buffer);
1607 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1608 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1609 ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1612 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_OPEN_LINK,
1613 KEY_ALL_ACCESS, NULL, &key, NULL );
1614 ok( err == ERROR_SUCCESS, "RegCreateKeyEx failed error %u\n", err );
1615 len = sizeof(buffer);
1616 err = RegQueryValueExA( key, "SymbolicLinkValue", NULL, &type, buffer, &len );
1617 ok( err == ERROR_SUCCESS, "RegQueryValueEx failed error %u\n", err );
1618 ok( len == target_len - sizeof(WCHAR), "wrong len %u\n", len );
1621 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK,
1622 KEY_ALL_ACCESS, NULL, &key, NULL );
1623 ok( err == ERROR_ALREADY_EXISTS, "RegCreateKeyEx wrong error %u\n", err );
1625 err = RegCreateKeyExA( hkey_main, "link", 0, NULL, REG_OPTION_CREATE_LINK | REG_OPTION_OPEN_LINK,
1626 KEY_ALL_ACCESS, NULL, &key, NULL );
1627 ok( err == ERROR_ALREADY_EXISTS, "RegCreateKeyEx wrong error %u\n", err );
1629 err = RegDeleteKey( hkey_main, "target" );
1630 ok( err == ERROR_SUCCESS, "RegDeleteKey failed error %u\n", err );
1632 err = RegDeleteKey( hkey_main, "link" );
1633 ok( err == ERROR_FILE_NOT_FOUND, "RegDeleteKey wrong error %u\n", err );
1635 status = pNtDeleteKey( link );
1636 ok( !status, "NtDeleteKey failed: 0x%08x\n", status );
1637 RegCloseKey( link );
1639 HeapFree( GetProcessHeap(), 0, target );
1640 pRtlFreeUnicodeString( &target_str );
1643 START_TEST(registry)
1645 /* Load pointers for functions that are not available in all Windows versions */
1650 create_test_entries();
1652 test_query_value_ex();
1654 test_reg_open_key();
1655 test_reg_create_key();
1656 test_reg_close_key();
1657 test_reg_delete_key();
1658 test_reg_query_value();
1659 test_string_termination();
1662 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
1663 if (set_privileges(SE_BACKUP_NAME, TRUE) &&
1664 set_privileges(SE_RESTORE_NAME, TRUE))
1666 test_reg_save_key();
1667 test_reg_load_key();
1668 test_reg_unload_key();
1670 set_privileges(SE_BACKUP_NAME, FALSE);
1671 set_privileges(SE_RESTORE_NAME, FALSE);
1674 test_reg_delete_tree();
1678 delete_key( hkey_main );
1680 test_regconnectregistry();