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