push 5f793418e14616d83a4fb368b755a8821b49820b
[wine/hacks.git] / dlls / advapi32 / tests / registry.c
bloba3d9f3006723cc59448e2f612465cbc77b3ada71
1 /*
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
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include "wine/test.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winreg.h"
28 #include "winsvc.h"
29 #include "winerror.h"
31 static HKEY hkey_main;
32 static DWORD GLE;
34 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
35 static const char * sTestpath2 = "%FOO%\\subdir1";
37 static HMODULE hadvapi32;
38 static DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
39 static DWORD (WINAPI *pRegDeleteTreeA)(HKEY,LPCSTR);
43 /* Debugging functions from wine/libs/wine/debug.c */
45 /* allocate some tmp string space */
46 /* FIXME: this is not 100% thread-safe */
47 static char *get_temp_buffer( int size )
49 static char *list[32];
50 static UINT pos;
51 char *ret;
52 UINT idx;
54 idx = ++pos % (sizeof(list)/sizeof(list[0]));
55 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
56 return ret;
59 static const char *wine_debugstr_an( const char *str, int n )
61 static const char hex[16] = "0123456789abcdef";
62 char *dst, *res;
63 size_t size;
65 if (!((ULONG_PTR)str >> 16))
67 if (!str) return "(null)";
68 res = get_temp_buffer( 6 );
69 sprintf( res, "#%04x", LOWORD(str) );
70 return res;
72 if (n == -1) n = strlen(str);
73 if (n < 0) n = 0;
74 size = 10 + min( 300, n * 4 );
75 dst = res = get_temp_buffer( size );
76 *dst++ = '"';
77 while (n-- > 0 && dst <= res + size - 9)
79 unsigned char c = *str++;
80 switch (c)
82 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
83 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
84 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
85 case '"': *dst++ = '\\'; *dst++ = '"'; break;
86 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
87 default:
88 if (c >= ' ' && c <= 126)
89 *dst++ = c;
90 else
92 *dst++ = '\\';
93 *dst++ = 'x';
94 *dst++ = hex[(c >> 4) & 0x0f];
95 *dst++ = hex[c & 0x0f];
99 *dst++ = '"';
100 if (n > 0)
102 *dst++ = '.';
103 *dst++ = '.';
104 *dst++ = '.';
106 *dst++ = 0;
107 return res;
110 static const char *wine_debugstr_wn( const WCHAR *str, int n )
112 char *dst, *res;
113 size_t size;
115 if (!HIWORD(str))
117 if (!str) return "(null)";
118 res = get_temp_buffer( 6 );
119 sprintf( res, "#%04x", LOWORD(str) );
120 return res;
122 if (n == -1) n = lstrlenW(str);
123 if (n < 0) n = 0;
124 size = 12 + min( 300, n * 5);
125 dst = res = get_temp_buffer( n * 5 + 7 );
126 *dst++ = 'L';
127 *dst++ = '"';
128 while (n-- > 0 && dst <= res + size - 10)
130 WCHAR c = *str++;
131 switch (c)
133 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
134 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
135 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
136 case '"': *dst++ = '\\'; *dst++ = '"'; break;
137 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
138 default:
139 if (c >= ' ' && c <= 126)
140 *dst++ = (char)c;
141 else
143 *dst++ = '\\';
144 sprintf(dst,"%04x",c);
145 dst+=4;
149 *dst++ = '"';
150 if (n > 0)
152 *dst++ = '.';
153 *dst++ = '.';
154 *dst++ = '.';
156 *dst = 0;
157 return res;
161 #define ADVAPI32_GET_PROC(func) \
162 p ## func = (void*)GetProcAddress(hadvapi32, #func);
165 static void InitFunctionPtrs(void)
167 hadvapi32 = GetModuleHandleA("advapi32.dll");
169 /* This function was introduced with Windows 2003 SP1 */
170 ADVAPI32_GET_PROC(RegGetValueA)
171 ADVAPI32_GET_PROC(RegDeleteTreeA)
174 /* delete key and all its subkeys */
175 static DWORD delete_key( HKEY hkey )
177 char name[MAX_PATH];
178 DWORD ret;
180 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
182 HKEY tmp;
183 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
185 ret = delete_key( tmp );
186 RegCloseKey( tmp );
188 if (ret) break;
190 if (ret != ERROR_NO_MORE_ITEMS) return ret;
191 RegDeleteKeyA( hkey, "" );
192 return 0;
195 static void setup_main_key(void)
197 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
199 assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
202 #define lok ok_(__FILE__, line)
203 #define test_hkey_main_Value_A(name, string, full_byte_len) _test_hkey_main_Value_A(__LINE__, name, string, full_byte_len)
204 static void _test_hkey_main_Value_A(int line, LPCSTR name, LPCSTR string,
205 DWORD full_byte_len)
207 DWORD ret, type, cbData;
208 DWORD str_byte_len;
209 BYTE* value;
211 type=0xdeadbeef;
212 cbData=0xdeadbeef;
213 /* When successful RegQueryValueExA() leaves GLE as is,
214 * so we must reset it to detect unimplemented functions.
216 SetLastError(0xdeadbeef);
217 ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
218 GLE = GetLastError();
219 lok(ret == ERROR_SUCCESS, "RegQueryValueExA/1 failed: %d, GLE=%d\n", ret, GLE);
220 /* It is wrong for the Ansi version to not be implemented */
221 ok(GLE == 0xdeadbeef, "RegQueryValueExA set GLE = %u\n", GLE);
222 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
224 str_byte_len = (string ? lstrlenA(string) : 0) + 1;
225 lok(type == REG_SZ, "RegQueryValueExA/1 returned type %d\n", type);
226 lok(cbData == full_byte_len || cbData == str_byte_len /* Win9x */,
227 "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
229 value = HeapAlloc(GetProcessHeap(), 0, cbData+1);
230 memset(value, 0xbd, cbData+1);
231 type=0xdeadbeef;
232 ret = RegQueryValueExA(hkey_main, name, NULL, &type, value, &cbData);
233 GLE = GetLastError();
234 lok(ret == ERROR_SUCCESS, "RegQueryValueExA/2 failed: %d, GLE=%d\n", ret, GLE);
235 if (!string)
237 /* When cbData == 0, RegQueryValueExA() should not modify the buffer */
238 lok(*value == 0xbd || (cbData == 1 && *value == '\0') /* Win9x */,
239 "RegQueryValueExA overflowed: cbData=%u *value=%02x\n", cbData, *value);
241 else
243 lok(memcmp(value, string, cbData) == 0, "RegQueryValueExA/2 failed: %s/%d != %s/%d\n",
244 wine_debugstr_an((char*)value, cbData), cbData,
245 wine_debugstr_an(string, full_byte_len), full_byte_len);
246 lok(*(value+cbData) == 0xbd, "RegQueryValueExA/2 overflowed at offset %u: %02x != bd\n", cbData, *(value+cbData));
248 HeapFree(GetProcessHeap(), 0, value);
251 #define test_hkey_main_Value_W(name, string, full_byte_len) _test_hkey_main_Value_W(__LINE__, name, string, full_byte_len)
252 static void _test_hkey_main_Value_W(int line, LPCWSTR name, LPCWSTR string,
253 DWORD full_byte_len)
255 DWORD ret, type, cbData;
256 BYTE* value;
258 type=0xdeadbeef;
259 cbData=0xdeadbeef;
260 /* When successful RegQueryValueExW() leaves GLE as is,
261 * so we must reset it to detect unimplemented functions.
263 SetLastError(0xdeadbeef);
264 ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
265 GLE = GetLastError();
266 lok(ret == ERROR_SUCCESS, "RegQueryValueExW/1 failed: %d, GLE=%d\n", ret, GLE);
267 if(GLE == ERROR_CALL_NOT_IMPLEMENTED)
269 win_skip("RegQueryValueExW() is not implemented\n");
270 return;
273 lok(type == REG_SZ, "RegQueryValueExW/1 returned type %d\n", type);
274 lok(cbData == full_byte_len,
275 "cbData=%d instead of %d\n", cbData, full_byte_len);
277 /* Give enough space to overflow by one WCHAR */
278 value = HeapAlloc(GetProcessHeap(), 0, cbData+2);
279 memset(value, 0xbd, cbData+2);
280 type=0xdeadbeef;
281 ret = RegQueryValueExW(hkey_main, name, NULL, &type, value, &cbData);
282 GLE = GetLastError();
283 lok(ret == ERROR_SUCCESS, "RegQueryValueExW/2 failed: %d, GLE=%d\n", ret, GLE);
284 if (string)
286 lok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
287 wine_debugstr_wn((WCHAR*)value, cbData / sizeof(WCHAR)), cbData,
288 wine_debugstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len);
290 /* This implies that when cbData == 0, RegQueryValueExW() should not modify the buffer */
291 lok(*(value+cbData) == 0xbd, "RegQueryValueExW/2 overflowed at %u: %02x != bd\n", cbData, *(value+cbData));
292 lok(*(value+cbData+1) == 0xbd, "RegQueryValueExW/2 overflowed at %u+1: %02x != bd\n", cbData, *(value+cbData+1));
293 HeapFree(GetProcessHeap(), 0, value);
296 static void test_set_value(void)
298 DWORD ret;
300 static const WCHAR name1W[] = {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
301 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};
302 static const WCHAR emptyW[] = {0};
303 static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
304 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};
305 static const WCHAR substring2W[] = {'T','h','i','s',0};
307 static const char name1A[] = "CleanSingleString";
308 static const char name2A[] = "SomeIntraZeroedString";
309 static const char emptyA[] = "";
310 static const char string1A[] = "ThisNeverBreaks";
311 static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
312 static const char substring2A[] = "This";
314 if (0)
316 /* Crashes on NT4, Windows 2000 and XP SP1 */
317 ret = RegSetValueA(hkey_main, NULL, REG_SZ, NULL, 0);
318 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueA should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
321 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, sizeof(string1A));
322 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
323 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
324 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
326 /* RegSetValueA ignores the size passed in */
327 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string1A, 4);
328 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
329 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
330 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
332 /* stops at first null */
333 ret = RegSetValueA(hkey_main, NULL, REG_SZ, string2A, sizeof(string2A));
334 ok(ret == ERROR_SUCCESS, "RegSetValueA failed: %d, GLE=%d\n", ret, GetLastError());
335 test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
336 test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
338 /* only REG_SZ is supported on NT*/
339 ret = RegSetValueA(hkey_main, NULL, REG_BINARY, string2A, sizeof(string2A));
340 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
341 ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
342 "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
344 ret = RegSetValueA(hkey_main, NULL, REG_EXPAND_SZ, string2A, sizeof(string2A));
345 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
346 ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
347 "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
349 ret = RegSetValueA(hkey_main, NULL, REG_MULTI_SZ, string2A, sizeof(string2A));
350 /* NT: ERROR_INVALID_PARAMETER, 9x: ERROR_SUCCESS */
351 ok(ret == ERROR_INVALID_PARAMETER || broken(ret == ERROR_SUCCESS),
352 "got %d (expected ERROR_INVALID_PARAMETER or ERROR_SUCCESS)\n", ret);
354 /* Test RegSetValueExA with a 'zero-byte' string (as Office 2003 does).
355 * Surprisingly enough we're supposed to get zero bytes out of it.
357 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, 0);
358 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
359 test_hkey_main_Value_A(name1A, NULL, 0);
360 test_hkey_main_Value_W(name1W, NULL, 0);
362 /* test RegSetValueExA with an empty string */
363 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, sizeof(emptyA));
364 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
365 test_hkey_main_Value_A(name1A, emptyA, sizeof(emptyA));
366 test_hkey_main_Value_W(name1W, emptyW, sizeof(emptyW));
368 /* test RegSetValueExA with off-by-one size */
369 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A)-sizeof(string1A[0]));
370 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
371 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
372 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
374 /* test RegSetValueExA with normal string */
375 ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
376 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
377 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
378 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
380 /* test RegSetValueExA with intrazeroed string */
381 ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
382 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
383 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
384 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
386 /* 9x doesn't support W-calls, so don't test them then */
387 if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
389 if (0)
391 /* Crashes on NT4, Windows 2000 and XP SP1 */
392 ret = RegSetValueW(hkey_main, NULL, REG_SZ, NULL, 0);
393 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have failed with ERROR_INVALID_PARAMETER instead of %d\n", ret);
396 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, sizeof(string1W));
397 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
398 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
399 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
401 /* RegSetValueA ignores the size passed in */
402 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string1W, 4 * sizeof(string1W[0]));
403 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
404 test_hkey_main_Value_A(NULL, string1A, sizeof(string1A));
405 test_hkey_main_Value_W(NULL, string1W, sizeof(string1W));
407 /* stops at first null */
408 ret = RegSetValueW(hkey_main, NULL, REG_SZ, string2W, sizeof(string2W));
409 ok(ret == ERROR_SUCCESS, "RegSetValueW failed: %d, GLE=%d\n", ret, GetLastError());
410 test_hkey_main_Value_A(NULL, substring2A, sizeof(substring2A));
411 test_hkey_main_Value_W(NULL, substring2W, sizeof(substring2W));
413 /* only REG_SZ is supported */
414 ret = RegSetValueW(hkey_main, NULL, REG_BINARY, string2W, sizeof(string2W));
415 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
416 ret = RegSetValueW(hkey_main, NULL, REG_EXPAND_SZ, string2W, sizeof(string2W));
417 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
418 ret = RegSetValueW(hkey_main, NULL, REG_MULTI_SZ, string2W, sizeof(string2W));
419 ok(ret == ERROR_INVALID_PARAMETER, "RegSetValueW should have returned ERROR_INVALID_PARAMETER instead of %d\n", ret);
421 /* test RegSetValueExW with off-by-one size */
422 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W)-sizeof(string1W[0]));
423 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
424 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
425 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
427 /* test RegSetValueExW with normal string */
428 ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
429 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
430 test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
431 test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
433 /* test RegSetValueExW with intrazeroed string */
434 ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
435 ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
436 test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
437 test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
440 static void create_test_entries(void)
442 static const DWORD qw[2] = { 0x12345678, 0x87654321 };
444 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
445 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
447 ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
448 "RegSetValueExA failed\n");
449 ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1),
450 "RegSetValueExA failed\n");
451 ok(!RegSetValueExA(hkey_main,"TP1_ZB_SZ",0,REG_SZ, (const BYTE *)"", 0),
452 "RegSetValueExA failed\n");
453 ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1),
454 "RegSetValueExA failed\n");
455 ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
456 "RegSetValueExA failed\n");
457 ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
458 "RegSetValueExA failed\n");
459 ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
460 "RegSetValueExA failed\n");
463 static void test_enum_value(void)
465 DWORD res;
466 HKEY test_key;
467 char value[20], data[20];
468 WCHAR valueW[20], dataW[20];
469 DWORD val_count, data_count, type;
470 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
471 static const WCHAR testW[] = {'T','e','s','t',0};
472 static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
474 /* create the working key for new 'Test' value */
475 res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
476 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
478 /* check NULL data with zero length */
479 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
480 if (GetVersion() & 0x80000000)
481 ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
482 else
483 ok( !res, "RegSetValueExA returned %d\n", res );
484 res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
485 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
486 res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
487 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
489 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
490 ok( res == 0, "RegSetValueExA failed error %d\n", res );
492 /* overflow both name and data */
493 val_count = 2;
494 data_count = 2;
495 type = 1234;
496 strcpy( value, "xxxxxxxxxx" );
497 strcpy( data, "xxxxxxxxxx" );
498 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
499 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
500 ok( val_count == 2, "val_count set to %d\n", val_count );
501 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
502 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
503 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
504 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
506 /* overflow name */
507 val_count = 3;
508 data_count = 20;
509 type = 1234;
510 strcpy( value, "xxxxxxxxxx" );
511 strcpy( data, "xxxxxxxxxx" );
512 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
513 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
514 /* Win9x returns 2 as specified by MSDN but NT returns 3... */
515 ok( val_count == 2 || val_count == 3, "val_count set to %d\n", val_count );
516 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
517 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
518 /* v5.1.2600.0 (XP Home and Professional) does not touch value or data in this case */
519 ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ),
520 "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
521 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
522 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
524 /* overflow empty name */
525 val_count = 0;
526 data_count = 20;
527 type = 1234;
528 strcpy( value, "xxxxxxxxxx" );
529 strcpy( data, "xxxxxxxxxx" );
530 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
531 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
532 ok( val_count == 0, "val_count set to %d\n", val_count );
533 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
534 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
535 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
536 /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
537 ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ),
538 "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
540 /* overflow data */
541 val_count = 20;
542 data_count = 2;
543 type = 1234;
544 strcpy( value, "xxxxxxxxxx" );
545 strcpy( data, "xxxxxxxxxx" );
546 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
547 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
548 ok( val_count == 20, "val_count set to %d\n", val_count );
549 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
550 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
551 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
552 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
554 /* no overflow */
555 val_count = 20;
556 data_count = 20;
557 type = 1234;
558 strcpy( value, "xxxxxxxxxx" );
559 strcpy( data, "xxxxxxxxxx" );
560 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
561 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
562 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
563 ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
564 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
565 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
566 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
568 /* Unicode tests */
570 SetLastError(0xdeadbeef);
571 res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
572 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
574 win_skip("RegSetValueExW is not implemented\n");
575 goto cleanup;
577 ok( res == 0, "RegSetValueExW failed error %d\n", res );
579 /* overflow both name and data */
580 val_count = 2;
581 data_count = 2;
582 type = 1234;
583 memcpy( valueW, xxxW, sizeof(xxxW) );
584 memcpy( dataW, xxxW, sizeof(xxxW) );
585 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
586 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
587 ok( val_count == 2, "val_count set to %d\n", val_count );
588 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
589 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
590 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
591 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
593 /* overflow name */
594 val_count = 3;
595 data_count = 20;
596 type = 1234;
597 memcpy( valueW, xxxW, sizeof(xxxW) );
598 memcpy( dataW, xxxW, sizeof(xxxW) );
599 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
600 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
601 ok( val_count == 3, "val_count set to %d\n", val_count );
602 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
603 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
604 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
605 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
607 /* overflow data */
608 val_count = 20;
609 data_count = 2;
610 type = 1234;
611 memcpy( valueW, xxxW, sizeof(xxxW) );
612 memcpy( dataW, xxxW, sizeof(xxxW) );
613 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
614 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
615 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
616 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
617 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
618 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
619 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
621 /* no overflow */
622 val_count = 20;
623 data_count = 20;
624 type = 1234;
625 memcpy( valueW, xxxW, sizeof(xxxW) );
626 memcpy( dataW, xxxW, sizeof(xxxW) );
627 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
628 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
629 ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
630 ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
631 ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
632 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
633 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
635 cleanup:
636 RegDeleteKeyA(test_key, "");
637 RegCloseKey(test_key);
640 static void test_query_value_ex(void)
642 DWORD ret;
643 DWORD size;
644 DWORD type;
645 BYTE buffer[10];
647 ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
648 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
649 ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
650 ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
652 type = 0xdeadbeef;
653 size = 0xdeadbeef;
654 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
655 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
656 /* the type parameter is cleared on Win9x, but is set to a random value on
657 * NT, so don't do that test there. The size parameter is left untouched on Win9x
658 * but cleared on NT+, this can be tested on all platforms.
660 if (GetVersion() & 0x80000000)
662 ok(type == 0, "type should have been set to 0 instead of 0x%x\n", type);
663 ok(size == 0xdeadbeef, "size should have been left untouched (0xdeadbeef)\n");
665 else
667 trace("test_query_value_ex: type set to: 0x%08x\n", type);
668 ok(size == 0, "size should have been set to 0 instead of %d\n", size);
671 size = sizeof(buffer);
672 ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
673 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
674 ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
676 size = 4;
677 ret = RegQueryValueExA(hkey_main, "BIN32", NULL, &size, buffer, &size);
678 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
681 static void test_get_value(void)
683 DWORD ret;
684 DWORD size;
685 DWORD type;
686 DWORD dw, qw[2];
687 CHAR buf[80];
688 CHAR expanded[] = "bar\\subdir1";
689 CHAR expanded2[] = "ImARatherLongButIndeedNeededString\\subdir1";
691 if(!pRegGetValueA)
693 win_skip("RegGetValue not available on this platform\n");
694 return;
697 /* Invalid parameter */
698 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
699 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
701 /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
702 size = type = dw = 0xdeadbeef;
703 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
704 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
705 ok(size == 4, "size=%d\n", size);
706 ok(type == REG_DWORD, "type=%d\n", type);
707 ok(dw == 0x12345678, "dw=%d\n", dw);
709 /* Query by subkey-name */
710 ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
711 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
713 /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
714 size = type = dw = 0xdeadbeef;
715 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
716 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
717 /* Although the function failed all values are retrieved */
718 ok(size == 4, "size=%d\n", size);
719 ok(type == REG_DWORD, "type=%d\n", type);
720 ok(dw == 0x12345678, "dw=%d\n", dw);
722 /* Test RRF_ZEROONFAILURE */
723 type = dw = 0xdeadbeef; size = 4;
724 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
725 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
726 /* Again all values are retrieved ... */
727 ok(size == 4, "size=%d\n", size);
728 ok(type == REG_DWORD, "type=%d\n", type);
729 /* ... except the buffer, which is zeroed out */
730 ok(dw == 0, "dw=%d\n", dw);
732 /* Test RRF_ZEROONFAILURE with a NULL buffer... */
733 type = size = 0xbadbeef;
734 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
735 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
736 ok(size == 4, "size=%d\n", size);
737 ok(type == REG_DWORD, "type=%d\n", type);
739 /* Query REG_DWORD using RRF_RT_DWORD (ok) */
740 size = type = dw = 0xdeadbeef;
741 ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
742 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
743 ok(size == 4, "size=%d\n", size);
744 ok(type == REG_DWORD, "type=%d\n", type);
745 ok(dw == 0x12345678, "dw=%d\n", dw);
747 /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
748 size = type = dw = 0xdeadbeef;
749 ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
750 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
751 ok(size == 4, "size=%d\n", size);
752 ok(type == REG_BINARY, "type=%d\n", type);
753 ok(dw == 0x12345678, "dw=%d\n", dw);
755 /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
756 qw[0] = qw[1] = size = type = 0xdeadbeef;
757 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
758 ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
759 ok(size == 8, "size=%d\n", size);
760 ok(type == REG_BINARY, "type=%d\n", type);
761 ok(qw[0] == 0x12345678 &&
762 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
764 /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
765 type = dw = 0xdeadbeef; size = 4;
766 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
767 ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
768 ok(dw == 0xdeadbeef, "dw=%d\n", dw);
769 ok(size == 8, "size=%d\n", size);
771 /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
772 qw[0] = qw[1] = size = type = 0xdeadbeef;
773 ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
774 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
775 ok(size == 8, "size=%d\n", size);
776 ok(type == REG_BINARY, "type=%d\n", type);
777 ok(qw[0] == 0x12345678 &&
778 qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
780 /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
781 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
782 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
783 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
784 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
785 ok(type == REG_SZ, "type=%d\n", type);
786 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
788 /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
789 type = 0xdeadbeef; size = 0;
790 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
791 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
792 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
793 ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
794 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
795 ok(type == REG_SZ, "type=%d\n", type);
797 /* Query REG_SZ using RRF_RT_REG_SZ on a zero-byte value (ok) */
798 strcpy(buf, sTestpath1);
799 type = 0xdeadbeef;
800 size = sizeof(buf);
801 ret = pRegGetValueA(hkey_main, NULL, "TP1_ZB_SZ", RRF_RT_REG_SZ, &type, buf, &size);
802 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
803 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
804 ok(size == 0 ||
805 size == 1, /* win2k3 */
806 "size=%d\n", size);
807 ok(type == REG_SZ, "type=%d\n", type);
808 ok(!strcmp(sTestpath1, buf) ||
809 !strcmp(buf, ""),
810 "Expected \"%s\" or \"\", got \"%s\"\n", sTestpath1, buf);
812 /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
813 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
814 ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
815 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
816 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
817 ok(type == REG_SZ, "type=%d\n", type);
818 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
820 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
821 size = 0;
822 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
823 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
824 ok((size == strlen(expanded2)+1) || /* win2k3 SP1 */
825 (size == strlen(expanded2)+2) || /* win2k3 SP2 */
826 (size == strlen(sTestpath2)+1),
827 "strlen(expanded2)=%d, strlen(sTestpath2)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
829 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
830 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
831 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
832 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
833 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
834 ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
835 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
836 ok(type == REG_SZ, "type=%d\n", type);
837 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
839 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands a lot) */
840 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
841 ret = pRegGetValueA(hkey_main, NULL, "TP2_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
842 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
843 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath2 length + 1 here. */
844 ok(size == strlen(expanded2)+1 || broken(size == strlen(sTestpath2)+1),
845 "strlen(expanded2)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded2), lstrlenA(sTestpath2), size);
846 ok(type == REG_SZ, "type=%d\n", type);
847 ok(!strcmp(expanded2, buf), "expanded2=\"%s\" buf=\"%s\"\n", expanded2, buf);
849 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
850 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
851 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
852 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
853 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
854 ok(type == REG_EXPAND_SZ, "type=%d\n", type);
855 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
857 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
858 size = 0xbadbeef;
859 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
860 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
861 /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
862 ok(size == strlen(sTestpath1)+1 || broken(size == strlen(sTestpath1)+2),
863 "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
865 /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
866 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
867 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
869 /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
870 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
871 ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
873 /* Query REG_EXPAND_SZ using RRF_RT_ANY */
874 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
875 ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_ANY, &type, buf, &size);
876 ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
877 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
878 ok(size == strlen(expanded)+1 || broken(size == strlen(sTestpath1)+1),
879 "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
880 ok(type == REG_SZ, "type=%d\n", type);
881 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
884 static void test_reg_open_key(void)
886 DWORD ret = 0;
887 HKEY hkResult = NULL;
888 HKEY hkPreserve = NULL;
890 /* successful open */
891 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
892 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
893 ok(hkResult != NULL, "expected hkResult != NULL\n");
894 hkPreserve = hkResult;
896 /* these tests fail on Win9x, but we want to be compatible with NT, so
897 * run them if we can */
898 if (!(GetVersion() & 0x80000000))
900 /* open same key twice */
901 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
902 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
903 ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
904 ok(hkResult != NULL, "hkResult != NULL\n");
905 RegCloseKey(hkResult);
907 /* open nonexistent key
908 * check that hkResult is set to NULL
910 hkResult = hkPreserve;
911 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
912 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
913 ok(hkResult == NULL, "expected hkResult == NULL\n");
915 /* open the same nonexistent key again to make sure the key wasn't created */
916 hkResult = hkPreserve;
917 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
918 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
919 ok(hkResult == NULL, "expected hkResult == NULL\n");
921 /* send in NULL lpSubKey
922 * check that hkResult receives the value of hKey
924 hkResult = hkPreserve;
925 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
926 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
927 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
929 /* send empty-string in lpSubKey */
930 hkResult = hkPreserve;
931 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
932 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
933 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
935 /* send in NULL lpSubKey and NULL hKey
936 * hkResult is set to NULL
938 hkResult = hkPreserve;
939 ret = RegOpenKeyA(NULL, NULL, &hkResult);
940 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
941 ok(hkResult == NULL, "expected hkResult == NULL\n");
944 /* only send NULL hKey
945 * the value of hkResult remains unchanged
947 hkResult = hkPreserve;
948 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
949 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
950 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
951 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
952 RegCloseKey(hkResult);
954 /* send in NULL hkResult */
955 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
956 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
958 /* beginning backslash character */
959 ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
960 ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
961 ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
962 , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
964 hkResult = NULL;
965 ret = RegOpenKeyExA(HKEY_CLASSES_ROOT, "\\clsid", 0, KEY_QUERY_VALUE, &hkResult);
966 ok(ret == ERROR_SUCCESS || /* 2k/XP */
967 ret == ERROR_BAD_PATHNAME || /* NT */
968 ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
969 , "expected ERROR_SUCCESS, ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
970 RegCloseKey(hkResult);
972 /* WOW64 flags */
973 hkResult = NULL;
974 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_32KEY, &hkResult);
975 ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
976 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
977 RegCloseKey(hkResult);
979 hkResult = NULL;
980 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, KEY_READ|KEY_WOW64_64KEY, &hkResult);
981 ok((ret == ERROR_SUCCESS && hkResult != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
982 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
983 RegCloseKey(hkResult);
986 static void test_reg_create_key(void)
988 LONG ret;
989 HKEY hkey1, hkey2;
990 ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
991 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
992 /* should succeed: all versions of Windows ignore the access rights
993 * to the parent handle */
994 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
995 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
997 /* clean up */
998 RegDeleteKey(hkey2, "");
999 RegDeleteKey(hkey1, "");
1001 /* beginning backslash character */
1002 ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
1003 if (!(GetVersion() & 0x80000000))
1004 ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
1005 else {
1006 ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
1007 RegDeleteKey(hkey1, NULL);
1010 /* WOW64 flags - open an existing key */
1011 hkey1 = NULL;
1012 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_32KEY, NULL, &hkey1, NULL);
1013 ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1014 "RegOpenKeyEx with KEY_WOW64_32KEY failed (err=%u)\n", ret);
1015 RegCloseKey(hkey1);
1017 hkey1 = NULL;
1018 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software", 0, NULL, 0, KEY_READ|KEY_WOW64_64KEY, NULL, &hkey1, NULL);
1019 ok((ret == ERROR_SUCCESS && hkey1 != NULL) || broken(ret == ERROR_ACCESS_DENIED /* NT4, win2k */),
1020 "RegOpenKeyEx with KEY_WOW64_64KEY failed (err=%u)\n", ret);
1021 RegCloseKey(hkey1);
1024 static void test_reg_close_key(void)
1026 DWORD ret = 0;
1027 HKEY hkHandle;
1029 /* successfully close key
1030 * hkHandle remains changed after call to RegCloseKey
1032 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
1033 ret = RegCloseKey(hkHandle);
1034 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1036 /* try to close the key twice */
1037 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
1038 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
1039 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
1041 /* try to close a NULL handle */
1042 ret = RegCloseKey(NULL);
1043 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
1044 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1046 /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
1047 * win98 doesn't give a new handle when the same key is opened.
1048 * Not re-opening will make some next tests fail.
1050 if (hkey_main == hkHandle)
1052 trace("The main handle is most likely closed, so re-opening\n");
1053 RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
1057 static void test_reg_delete_key(void)
1059 DWORD ret;
1061 ret = RegDeleteKey(hkey_main, NULL);
1063 /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
1064 * there are also no subkeys available it will delete the key pointed to by hkey_main.
1065 * Not re-creating will make some next tests fail.
1067 if (ret == ERROR_SUCCESS)
1069 trace("We are probably running on NT4 or W2K as the main key is deleted,"
1070 " re-creating the main key\n");
1071 setup_main_key();
1073 else
1074 ok(ret == ERROR_INVALID_PARAMETER ||
1075 ret == ERROR_ACCESS_DENIED ||
1076 ret == ERROR_BADKEY, /* Win95 */
1077 "ret=%d\n", ret);
1080 static void test_reg_save_key(void)
1082 DWORD ret;
1084 ret = RegSaveKey(hkey_main, "saved_key", NULL);
1085 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1088 static void test_reg_load_key(void)
1090 DWORD ret;
1091 HKEY hkHandle;
1093 ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
1094 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1096 ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
1097 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1099 RegCloseKey(hkHandle);
1102 static void test_reg_unload_key(void)
1104 DWORD ret;
1106 ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
1107 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
1109 DeleteFile("saved_key");
1110 DeleteFile("saved_key.LOG");
1113 static BOOL set_privileges(LPCSTR privilege, BOOL set)
1115 TOKEN_PRIVILEGES tp;
1116 HANDLE hToken;
1117 LUID luid;
1119 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
1120 return FALSE;
1122 if(!LookupPrivilegeValue(NULL, privilege, &luid))
1124 CloseHandle(hToken);
1125 return FALSE;
1128 tp.PrivilegeCount = 1;
1129 tp.Privileges[0].Luid = luid;
1131 if (set)
1132 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
1133 else
1134 tp.Privileges[0].Attributes = 0;
1136 AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
1137 if (GetLastError() != ERROR_SUCCESS)
1139 CloseHandle(hToken);
1140 return FALSE;
1143 CloseHandle(hToken);
1144 return TRUE;
1147 /* tests that show that RegConnectRegistry and
1148 OpenSCManager accept computer names without the
1149 \\ prefix (what MSDN says). */
1150 static void test_regconnectregistry( void)
1152 CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
1153 CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
1154 DWORD len = sizeof(compName) ;
1155 BOOL ret;
1156 LONG retl;
1157 HKEY hkey;
1158 SC_HANDLE schnd;
1160 SetLastError(0xdeadbeef);
1161 ret = GetComputerNameA(compName, &len);
1162 ok( ret, "GetComputerName failed err = %d\n", GetLastError());
1163 if( !ret) return;
1165 lstrcpyA(netwName, "\\\\");
1166 lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1168 retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1169 ok( !retl ||
1170 retl == ERROR_DLL_INIT_FAILED ||
1171 retl == ERROR_BAD_NETPATH, /* some win2k */
1172 "RegConnectRegistryA failed err = %d\n", retl);
1173 if( !retl) RegCloseKey( hkey);
1175 retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1176 ok( !retl ||
1177 retl == ERROR_DLL_INIT_FAILED ||
1178 retl == ERROR_BAD_NETPATH, /* some win2k */
1179 "RegConnectRegistryA failed err = %d\n", retl);
1180 if( !retl) RegCloseKey( hkey);
1182 SetLastError(0xdeadbeef);
1183 schnd = OpenSCManagerA( compName, NULL, GENERIC_READ);
1184 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1186 win_skip("OpenSCManagerA is not implemented\n");
1187 return;
1190 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1191 CloseServiceHandle( schnd);
1193 SetLastError(0xdeadbeef);
1194 schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ);
1195 ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1196 CloseServiceHandle( schnd);
1200 static void test_reg_query_value(void)
1202 HKEY subkey;
1203 CHAR val[MAX_PATH];
1204 WCHAR valW[5];
1205 LONG size, ret;
1207 static const WCHAR expected[] = {'d','a','t','a',0};
1209 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1210 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1212 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1213 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1215 /* try an invalid hkey */
1216 SetLastError(0xdeadbeef);
1217 size = MAX_PATH;
1218 ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1219 ok(ret == ERROR_INVALID_HANDLE ||
1220 ret == ERROR_BADKEY || /* Windows 98 returns BADKEY */
1221 ret == ERROR_ACCESS_DENIED, /* non-admin winxp */
1222 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1223 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1225 /* try a NULL hkey */
1226 SetLastError(0xdeadbeef);
1227 size = MAX_PATH;
1228 ret = RegQueryValueA(NULL, "subkey", val, &size);
1229 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1230 "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1231 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1233 /* try a NULL value */
1234 size = MAX_PATH;
1235 ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1236 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1237 ok(size == 5, "Expected 5, got %d\n", size);
1239 /* try a NULL size */
1240 SetLastError(0xdeadbeef);
1241 val[0] = '\0';
1242 ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1243 ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1244 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1245 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1247 /* try a NULL value and size */
1248 ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1249 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1251 /* try a size too small */
1252 SetLastError(0xdeadbeef);
1253 val[0] = '\0';
1254 size = 1;
1255 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1256 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1257 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1258 ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1259 ok(size == 5, "Expected 5, got %d\n", size);
1261 /* successfully read the value using 'subkey' */
1262 size = MAX_PATH;
1263 ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1264 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1265 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1266 ok(size == 5, "Expected 5, got %d\n", size);
1268 /* successfully read the value using the subkey key */
1269 size = MAX_PATH;
1270 ret = RegQueryValueA(subkey, NULL, val, &size);
1271 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1272 ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1273 ok(size == 5, "Expected 5, got %d\n", size);
1275 /* unicode - try size too small */
1276 SetLastError(0xdeadbeef);
1277 valW[0] = '\0';
1278 size = 0;
1279 ret = RegQueryValueW(subkey, NULL, valW, &size);
1280 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1282 win_skip("RegQueryValueW is not implemented\n");
1283 goto cleanup;
1285 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1286 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1287 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1288 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1290 /* unicode - try size in WCHARS */
1291 SetLastError(0xdeadbeef);
1292 size = sizeof(valW) / sizeof(WCHAR);
1293 ret = RegQueryValueW(subkey, NULL, valW, &size);
1294 ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1295 ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1296 ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1297 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1299 /* unicode - successfully read the value */
1300 size = sizeof(valW);
1301 ret = RegQueryValueW(subkey, NULL, valW, &size);
1302 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1303 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1304 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1306 /* unicode - set the value without a NULL terminator */
1307 ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1308 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1310 /* unicode - read the unterminated value, value is terminated for us */
1311 memset(valW, 'a', sizeof(valW));
1312 size = sizeof(valW);
1313 ret = RegQueryValueW(subkey, NULL, valW, &size);
1314 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1315 ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1316 ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1318 cleanup:
1319 RegDeleteKeyA(subkey, "");
1320 RegCloseKey(subkey);
1323 static void test_string_termination(void)
1325 HKEY subkey;
1326 LSTATUS ret;
1327 static const char string[] = "FullString";
1328 char name[11];
1329 BYTE buffer[11];
1330 DWORD insize, outsize, nsize;
1332 ret = RegCreateKeyA(hkey_main, "string_termination", &subkey);
1333 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1335 /* Off-by-one RegSetValueExA -> adds a trailing '\0'! */
1336 insize=sizeof(string)-1;
1337 ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1338 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1339 outsize=insize;
1340 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1341 ok(ret == ERROR_MORE_DATA, "RegQueryValueExA returned: %d\n", ret);
1343 /* Off-by-two RegSetValueExA -> no trailing '\0', except on Win9x */
1344 insize=sizeof(string)-2;
1345 ret = RegSetValueExA(subkey, "stringtest", 0, REG_SZ, (BYTE*)string, insize);
1346 ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d\n", ret);
1347 outsize=0;
1348 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, NULL, &outsize);
1349 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1350 ok(outsize == insize || broken(outsize == sizeof(string)) /* Win9x */,
1351 "wrong size %u != %u\n", outsize, insize);
1353 if (outsize == insize)
1355 /* RegQueryValueExA may return a string with no trailing '\0' */
1356 outsize=insize;
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] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1365 /* RegQueryValueExA adds a trailing '\0' if there is room */
1366 outsize=insize+1;
1367 memset(buffer, 0xbd, sizeof(buffer));
1368 ret = RegQueryValueExA(subkey, "stringtest", NULL, NULL, buffer, &outsize);
1369 ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d\n", ret);
1370 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1371 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1372 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1373 ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1375 /* RegEnumValueA may return a string with no trailing '\0' */
1376 outsize=insize;
1377 memset(buffer, 0xbd, sizeof(buffer));
1378 nsize=sizeof(name);
1379 ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1380 ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1381 ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1382 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1383 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1384 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1385 ok(buffer[insize] == 0xbd, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1387 /* RegEnumValueA adds a trailing '\0' if there is room */
1388 outsize=insize+1;
1389 memset(buffer, 0xbd, sizeof(buffer));
1390 nsize=sizeof(name);
1391 ret = RegEnumValueA(subkey, 0, name, &nsize, NULL, NULL, buffer, &outsize);
1392 ok(ret == ERROR_SUCCESS, "RegEnumValueA failed: %d\n", ret);
1393 ok(strcmp(name, "stringtest") == 0, "wrong name: %s\n", name);
1394 ok(outsize == insize, "wrong size: %u != %u\n", outsize, insize);
1395 ok(memcmp(buffer, string, outsize) == 0, "bad string: %s/%u != %s\n",
1396 wine_debugstr_an((char*)buffer, outsize), outsize, string);
1397 ok(buffer[insize] == 0, "buffer overflow at %u %02x\n", insize, buffer[insize]);
1400 RegDeleteKeyA(subkey, "");
1401 RegCloseKey(subkey);
1404 static void test_reg_delete_tree(void)
1406 CHAR buffer[MAX_PATH];
1407 HKEY subkey, subkey2;
1408 LONG size, ret;
1410 if(!pRegDeleteTreeA) {
1411 win_skip("Skipping RegDeleteTreeA tests, function not present\n");
1412 return;
1415 ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1416 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1417 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1418 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1419 ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1420 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1421 ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1422 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1423 ret = RegCloseKey(subkey2);
1424 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1426 ret = pRegDeleteTreeA(subkey, "subkey2");
1427 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1428 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1429 "subkey2 was not deleted\n");
1430 size = MAX_PATH;
1431 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1432 "Default value of subkey not longer present\n");
1434 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1435 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1436 ret = RegCloseKey(subkey2);
1437 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1438 ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1439 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1440 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1441 "subkey2 was not deleted\n");
1442 ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1443 "Default value of subkey not longer present\n");
1445 ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1446 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1447 ret = RegCloseKey(subkey2);
1448 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1449 ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1450 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1451 ret = RegCloseKey(subkey2);
1452 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1453 ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1454 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1455 ret = pRegDeleteTreeA(subkey, NULL);
1456 ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1457 ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1458 "subkey was deleted\n");
1459 ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1460 "subkey2 was not deleted\n");
1461 ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1462 "subkey3 was not deleted\n");
1463 size = MAX_PATH;
1464 ret = RegQueryValueA(subkey, NULL, buffer, &size);
1465 ok(ret == ERROR_SUCCESS,
1466 "Default value of subkey is not present\n");
1467 ok(!lstrlenA(buffer),
1468 "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1469 size = MAX_PATH;
1470 ok(RegQueryValueA(subkey, "value", buffer, &size),
1471 "Value is still present\n");
1473 ret = pRegDeleteTreeA(hkey_main, "not-here");
1474 ok(ret == ERROR_FILE_NOT_FOUND,
1475 "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1478 static void test_rw_order(void)
1480 HKEY hKey;
1481 DWORD dw = 0;
1482 static char keyname[] = "test_rw_order";
1483 char value_buf[2];
1484 DWORD values, value_len, value_name_max_len;
1485 LSTATUS ret;
1487 RegDeleteKeyA(HKEY_CURRENT_USER, keyname);
1488 ret = RegCreateKeyA(HKEY_CURRENT_USER, keyname, &hKey);
1489 if(ret != ERROR_SUCCESS) {
1490 skip("Couldn't create key. Skipping.\n");
1491 return;
1494 ok(!RegSetValueExA(hKey, "A", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1495 "RegSetValueExA for value \"A\" failed\n");
1496 ok(!RegSetValueExA(hKey, "C", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1497 "RegSetValueExA for value \"C\" failed\n");
1498 ok(!RegSetValueExA(hKey, "D", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1499 "RegSetValueExA for value \"D\" failed\n");
1500 ok(!RegSetValueExA(hKey, "B", 0, REG_DWORD, (LPBYTE)&dw, sizeof(dw)),
1501 "RegSetValueExA for value \"B\" failed\n");
1503 ok(!RegQueryInfoKeyA(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &values,
1504 &value_name_max_len, NULL, NULL, NULL), "RegQueryInfoKeyA failed\n");
1505 ok(values == 4, "Expected 4 values, got %u\n", values);
1507 /* Value enumeration preserves RegSetValueEx call order */
1508 value_len = 2;
1509 ok(!RegEnumValueA(hKey, 0, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1510 ok(strcmp(value_buf, "A") == 0, "Expected name \"A\", got %s\n", value_buf);
1511 value_len = 2;
1512 ok(!RegEnumValueA(hKey, 1, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1513 todo_wine ok(strcmp(value_buf, "C") == 0, "Expected name \"C\", got %s\n", value_buf);
1514 value_len = 2;
1515 ok(!RegEnumValueA(hKey, 2, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1516 todo_wine ok(strcmp(value_buf, "D") == 0, "Expected name \"D\", got %s\n", value_buf);
1517 value_len = 2;
1518 ok(!RegEnumValueA(hKey, 3, value_buf, &value_len, NULL, NULL, NULL, NULL), "RegEnumValueA failed\n");
1519 todo_wine ok(strcmp(value_buf, "B") == 0, "Expected name \"B\", got %s\n", value_buf);
1521 ok(!RegDeleteKey(HKEY_CURRENT_USER, keyname), "Failed to delete key\n");
1524 START_TEST(registry)
1526 /* Load pointers for functions that are not available in all Windows versions */
1527 InitFunctionPtrs();
1529 setup_main_key();
1530 test_set_value();
1531 create_test_entries();
1532 test_enum_value();
1533 test_query_value_ex();
1534 test_get_value();
1535 test_reg_open_key();
1536 test_reg_create_key();
1537 test_reg_close_key();
1538 test_reg_delete_key();
1539 test_reg_query_value();
1540 test_string_termination();
1542 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
1543 if (set_privileges(SE_BACKUP_NAME, TRUE) &&
1544 set_privileges(SE_RESTORE_NAME, TRUE))
1546 test_reg_save_key();
1547 test_reg_load_key();
1548 test_reg_unload_key();
1550 set_privileges(SE_BACKUP_NAME, FALSE);
1551 set_privileges(SE_RESTORE_NAME, FALSE);
1554 test_reg_delete_tree();
1555 test_rw_order();
1557 /* cleanup */
1558 delete_key( hkey_main );
1560 test_regconnectregistry();