user32: Change the desktop colour and pattern to match win2k.
[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 long pos;
51     char *ret;
52     int idx;
53
54     idx = ++pos % (sizeof(list)/sizeof(list[0]));
55     if ((ret = realloc( list[idx], size ))) list[idx] = ret;
56     return ret;
57 }
58
59 static const char *wine_debugstr_an( const char *str, int n )
60 {
61     static const char hex[16] = "0123456789abcdef";
62     char *dst, *res;
63     size_t size;
64
65     if (!((ULONG_PTR)str >> 16))
66     {
67         if (!str) return "(null)";
68         res = get_temp_buffer( 6 );
69         sprintf( res, "#%04x", LOWORD(str) );
70         return res;
71     }
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)
78     {
79         unsigned char c = *str++;
80         switch (c)
81         {
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
91             {
92                 *dst++ = '\\';
93                 *dst++ = 'x';
94                 *dst++ = hex[(c >> 4) & 0x0f];
95                 *dst++ = hex[c & 0x0f];
96             }
97         }
98     }
99     *dst++ = '"';
100     if (n > 0)
101     {
102         *dst++ = '.';
103         *dst++ = '.';
104         *dst++ = '.';
105     }
106     *dst++ = 0;
107     return res;
108 }
109
110 static const char *wine_debugstr_wn( const WCHAR *str, int n )
111 {
112     char *dst, *res;
113     size_t size;
114
115     if (!HIWORD(str))
116     {
117         if (!str) return "(null)";
118         res = get_temp_buffer( 6 );
119         sprintf( res, "#%04x", LOWORD(str) );
120         return res;
121     }
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)
129     {
130         WCHAR c = *str++;
131         switch (c)
132         {
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
142             {
143                 *dst++ = '\\';
144                 sprintf(dst,"%04x",c);
145                 dst+=4;
146             }
147         }
148     }
149     *dst++ = '"';
150     if (n > 0)
151     {
152         *dst++ = '.';
153         *dst++ = '.';
154         *dst++ = '.';
155     }
156     *dst = 0;
157     return res;
158 }
159
160
161 #define ADVAPI32_GET_PROC(func) \
162     p ## func = (void*)GetProcAddress(hadvapi32, #func); \
163     if(!p ## func) \
164       trace("GetProcAddress(%s) failed\n", #func);
165
166 static void InitFunctionPtrs(void)
167 {
168     hadvapi32 = GetModuleHandleA("advapi32.dll");
169
170     /* This function was introduced with Windows 2003 SP1 */
171     ADVAPI32_GET_PROC(RegGetValueA)
172     ADVAPI32_GET_PROC(RegDeleteTreeA)
173 }
174
175 /* delete key and all its subkeys */
176 static DWORD delete_key( HKEY hkey )
177 {
178     char name[MAX_PATH];
179     DWORD ret;
180
181     while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
182     {
183         HKEY tmp;
184         if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
185         {
186             ret = delete_key( tmp );
187             RegCloseKey( tmp );
188         }
189         if (ret) break;
190     }
191     if (ret != ERROR_NO_MORE_ITEMS) return ret;
192     RegDeleteKeyA( hkey, "" );
193     return 0;
194 }
195
196 static void setup_main_key(void)
197 {
198     if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
199
200     assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
201 }
202
203 static void test_hkey_main_Value_A(LPCSTR name, LPCSTR string,
204                                    DWORD full_byte_len)
205 {
206     DWORD ret, type, cbData;
207     DWORD str_byte_len;
208     LPSTR value;
209     static const char nA[]={'N', 0};
210
211     type=0xdeadbeef;
212     cbData=0xdeadbeef;
213     /* When successful RegQueryValueExA() leaves GLE as is,
214      * so we must reset it to detect unimplemented functions.
215      */
216     SetLastError(0xdeadbeef);
217     ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
218     GLE = GetLastError();
219     ok(ret == ERROR_SUCCESS, "RegQueryValueExA 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;
223
224     str_byte_len = (string ? lstrlenA(string) : 0) + 1;
225     ok(type == REG_SZ, "RegQueryValueExA returned type %d\n", type);
226     ok(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);
228
229     value = HeapAlloc(GetProcessHeap(), 0, (cbData+2)*sizeof(*value));
230     strcpy(value, nA);
231     type=0xdeadbeef;
232     ret = RegQueryValueExA(hkey_main, name, NULL, &type, (BYTE*)value, &cbData);
233     GLE = GetLastError();
234     ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%d\n", ret, GLE);
235     if (!string)
236     {
237         /* When cbData == 0, RegQueryValueExA() should not modify the buffer */
238         ok(strcmp(value, nA) == 0 || (cbData == 1 && *value == '\0') /* Win9x */,
239            "RegQueryValueExA failed: '%s' != '%s'\n", value, string);
240     }
241     else
242     {
243         ok(memcmp(value, string, cbData) == 0, "RegQueryValueExA failed: %s/%d != %s/%d\n",
244            wine_debugstr_an(value, cbData), cbData,
245            wine_debugstr_an(string, full_byte_len), full_byte_len);
246     }
247     HeapFree(GetProcessHeap(), 0, value);
248 }
249
250 static void test_hkey_main_Value_W(LPCWSTR name, LPCWSTR string,
251                                    DWORD full_byte_len)
252 {
253     DWORD ret, type, cbData;
254     LPWSTR value;
255     static const WCHAR nW[]={'N', 0};
256
257     type=0xdeadbeef;
258     cbData=0xdeadbeef;
259     /* When successful RegQueryValueExW() leaves GLE as is,
260      * so we must reset it to detect unimplemented functions.
261      */
262     SetLastError(0xdeadbeef);
263     ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
264     GLE = GetLastError();
265     ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %d, GLE=%d\n", ret, GLE);
266     if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
267
268     ok(type == REG_SZ, "RegQueryValueExW returned type %d\n", type);
269     ok(cbData == full_byte_len,
270         "cbData=%d instead of %d\n", cbData, full_byte_len);
271
272     value = HeapAlloc(GetProcessHeap(), 0, (cbData+2)*sizeof(*value));
273     lstrcpyW(value, nW);
274     type=0xdeadbeef;
275     ret = RegQueryValueExW(hkey_main, name, NULL, &type, (BYTE*)value, &cbData);
276     GLE = GetLastError();
277     ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %d, GLE=%d\n", ret, GLE);
278     if (!string)
279     {
280         /* When cbData == 0, RegQueryValueExW() should not modify the buffer */
281         string=nW;
282     }
283     ok(memcmp(value, string, cbData) == 0, "RegQueryValueExW failed: %s/%d != %s/%d\n",
284        wine_debugstr_wn(value, cbData / sizeof(WCHAR)), cbData,
285        wine_debugstr_wn(string, full_byte_len / sizeof(WCHAR)), full_byte_len / sizeof(WCHAR));
286     HeapFree(GetProcessHeap(), 0, value);
287 }
288
289 static void test_set_value(void)
290 {
291     DWORD ret;
292
293     static const WCHAR name1W[] =   {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
294     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};
295     static const WCHAR emptyW[] = {0};
296     static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
297     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};
298
299     static const char name1A[] =   "CleanSingleString";
300     static const char name2A[] =   "SomeIntraZeroedString";
301     static const char emptyA[] = "";
302     static const char string1A[] = "ThisNeverBreaks";
303     static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
304
305     /* Test RegSetValueExA with a 'zero-byte' string (as Office 2003 does).
306      * Surprisingly enough we're supposed to get zero bytes out of it.
307      * FIXME: Wine's on-disk file format does not differentiate this with
308      *        regular empty strings but there's no way to test as it requires
309      *        stopping the wineserver.
310      */
311     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, 0);
312     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
313     test_hkey_main_Value_A(name1A, NULL, 0);
314     test_hkey_main_Value_W(name1W, NULL, 0);
315
316     /* test RegSetValueExA with an empty string */
317     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)emptyA, sizeof(emptyA));
318     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
319     test_hkey_main_Value_A(name1A, emptyA, sizeof(emptyA));
320     test_hkey_main_Value_W(name1W, emptyW, sizeof(emptyW));
321
322     /* test RegSetValueExA with normal string */
323     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
324     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
325     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
326     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
327
328     /* test RegSetValueExA with intrazeroed string */
329     ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
330     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
331     test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
332     test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
333
334     /* 9x doesn't support W-calls, so don't test them then */
335     if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return; 
336
337     /* test RegSetValueExW with normal string */
338     ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
339     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
340     test_hkey_main_Value_A(name1A, string1A, sizeof(string1A));
341     test_hkey_main_Value_W(name1W, string1W, sizeof(string1W));
342
343     /* test RegSetValueExW with intrazeroed string */
344     ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
345     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
346     test_hkey_main_Value_A(name2A, string2A, sizeof(string2A));
347     test_hkey_main_Value_W(name2W, string2W, sizeof(string2W));
348 }
349
350 static void create_test_entries(void)
351 {
352     static const DWORD qw[2] = { 0x12345678, 0x87654321 };
353
354     SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
355     SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
356
357     ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1), 
358         "RegSetValueExA failed\n");
359     ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1), 
360         "RegSetValueExA failed\n");
361     ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1), 
362         "RegSetValueExA failed\n");
363     ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
364         "RegSetValueExA failed\n");
365     ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
366         "RegSetValueExA failed\n");
367     ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
368         "RegSetValueExA failed\n");
369 }
370         
371 static void test_enum_value(void)
372 {
373     DWORD res;
374     HKEY test_key;
375     char value[20], data[20];
376     WCHAR valueW[20], dataW[20];
377     DWORD val_count, data_count, type;
378     static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
379     static const WCHAR testW[] = {'T','e','s','t',0};
380     static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
381
382     /* create the working key for new 'Test' value */
383     res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
384     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
385
386     /* check NULL data with zero length */
387     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
388     if (GetVersion() & 0x80000000)
389         ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
390     else
391         ok( !res, "RegSetValueExA returned %d\n", res );
392     res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
393     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
394     res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
395     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
396
397     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
398     ok( res == 0, "RegSetValueExA failed error %d\n", res );
399
400     /* overflow both name and data */
401     val_count = 2;
402     data_count = 2;
403     type = 1234;
404     strcpy( value, "xxxxxxxxxx" );
405     strcpy( data, "xxxxxxxxxx" );
406     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
407     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
408     ok( val_count == 2, "val_count set to %d\n", val_count );
409     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
410     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
411     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
412     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
413
414     /* overflow name */
415     val_count = 3;
416     data_count = 20;
417     type = 1234;
418     strcpy( value, "xxxxxxxxxx" );
419     strcpy( data, "xxxxxxxxxx" );
420     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
421     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
422     /* Win9x returns 2 as specified by MSDN but NT returns 3... */
423     ok( val_count == 2 || val_count == 3, "val_count set to %d\n", val_count );
424     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
425     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
426     /* v5.1.2600.0 (XP Home and Proffesional) does not touch value or data in this case */
427     ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ), 
428         "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
429     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ), 
430         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
431
432     /* overflow empty name */
433     val_count = 0;
434     data_count = 20;
435     type = 1234;
436     strcpy( value, "xxxxxxxxxx" );
437     strcpy( data, "xxxxxxxxxx" );
438     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
439     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
440     ok( val_count == 0, "val_count set to %d\n", val_count );
441     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
442     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
443     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
444     /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
445     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ), 
446         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
447
448     /* overflow data */
449     val_count = 20;
450     data_count = 2;
451     type = 1234;
452     strcpy( value, "xxxxxxxxxx" );
453     strcpy( data, "xxxxxxxxxx" );
454     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
455     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
456     ok( val_count == 20, "val_count set to %d\n", val_count );
457     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
458     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
459     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
460     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
461
462     /* no overflow */
463     val_count = 20;
464     data_count = 20;
465     type = 1234;
466     strcpy( value, "xxxxxxxxxx" );
467     strcpy( data, "xxxxxxxxxx" );
468     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
469     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
470     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
471     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
472     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
473     ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
474     ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
475
476     /* Unicode tests */
477
478     SetLastError(0xdeadbeef);
479     res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
480     if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
481     {
482         skip("RegSetValueExW is not implemented\n");
483         goto cleanup;
484     }
485     ok( res == 0, "RegSetValueExW failed error %d\n", res );
486
487     /* overflow both name and data */
488     val_count = 2;
489     data_count = 2;
490     type = 1234;
491     memcpy( valueW, xxxW, sizeof(xxxW) );
492     memcpy( dataW, xxxW, sizeof(xxxW) );
493     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
494     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
495     ok( val_count == 2, "val_count set to %d\n", val_count );
496     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
497     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
498     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
499     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
500
501     /* overflow name */
502     val_count = 3;
503     data_count = 20;
504     type = 1234;
505     memcpy( valueW, xxxW, sizeof(xxxW) );
506     memcpy( dataW, xxxW, sizeof(xxxW) );
507     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
508     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
509     ok( val_count == 3, "val_count set to %d\n", val_count );
510     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
511     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
512     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
513     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
514
515     /* overflow data */
516     val_count = 20;
517     data_count = 2;
518     type = 1234;
519     memcpy( valueW, xxxW, sizeof(xxxW) );
520     memcpy( dataW, xxxW, sizeof(xxxW) );
521     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
522     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
523     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
524     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
525     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
526     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
527     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
528
529     /* no overflow */
530     val_count = 20;
531     data_count = 20;
532     type = 1234;
533     memcpy( valueW, xxxW, sizeof(xxxW) );
534     memcpy( dataW, xxxW, sizeof(xxxW) );
535     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
536     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
537     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
538     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
539     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
540     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
541     ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
542
543 cleanup:
544     RegDeleteKeyA(test_key, "");
545     RegCloseKey(test_key);
546 }
547
548 static void test_query_value_ex(void)
549 {
550     DWORD ret;
551     DWORD size;
552     DWORD type;
553     BYTE buffer[10];
554     
555     ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
556     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
557     ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
558     ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
559
560     type = 0xdeadbeef;
561     size = 0xdeadbeef;
562     ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
563     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
564     /* the type parameter is cleared on Win9x, but is set to a random value on
565      * NT, so don't do that test there. The size parameter is left untouched on Win9x
566      * but cleared on NT+, this can be tested on all platforms.
567      */
568     if (GetVersion() & 0x80000000)
569     {
570         ok(type == 0, "type should have been set to 0 instead of 0x%x\n", type);
571         ok(size == 0xdeadbeef, "size should have been left untouched (0xdeadbeef)\n");
572     }
573     else
574     {
575         trace("test_query_value_ex: type set to: 0x%08x\n", type);
576         ok(size == 0, "size should have been set to 0 instead of %d\n", size);
577     }
578
579     size = sizeof(buffer);
580     ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
581     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
582     ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
583 }
584
585 static void test_get_value(void)
586 {
587     DWORD ret;
588     DWORD size;
589     DWORD type;
590     DWORD dw, qw[2];
591     CHAR buf[80];
592     CHAR expanded[] = "bar\\subdir1";
593    
594     if(!pRegGetValueA)
595     {
596         skip("RegGetValue not available on this platform\n");
597         return;
598     }
599
600     /* Invalid parameter */
601     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
602     ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
603
604     /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
605     size = type = dw = 0xdeadbeef;
606     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
607     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
608     ok(size == 4, "size=%d\n", size);
609     ok(type == REG_DWORD, "type=%d\n", type);
610     ok(dw == 0x12345678, "dw=%d\n", dw);
611
612     /* Query by subkey-name */
613     ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
614     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
615
616     /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
617     size = type = dw = 0xdeadbeef;
618     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
619     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
620     /* Although the function failed all values are retrieved */
621     ok(size == 4, "size=%d\n", size);
622     ok(type == REG_DWORD, "type=%d\n", type);
623     ok(dw == 0x12345678, "dw=%d\n", dw);
624
625     /* Test RRF_ZEROONFAILURE */
626     type = dw = 0xdeadbeef; size = 4;
627     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
628     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
629     /* Again all values are retrieved ... */
630     ok(size == 4, "size=%d\n", size);
631     ok(type == REG_DWORD, "type=%d\n", type);
632     /* ... except the buffer, which is zeroed out */
633     ok(dw == 0, "dw=%d\n", dw);
634
635     /* Test RRF_ZEROONFAILURE with a NULL buffer... */
636     type = size = 0xbadbeef;
637     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
638     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
639     ok(size == 4, "size=%d\n", size);
640     ok(type == REG_DWORD, "type=%d\n", type);
641
642     /* Query REG_DWORD using RRF_RT_DWORD (ok) */
643     size = type = dw = 0xdeadbeef;
644     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
645     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
646     ok(size == 4, "size=%d\n", size);
647     ok(type == REG_DWORD, "type=%d\n", type);
648     ok(dw == 0x12345678, "dw=%d\n", dw);
649
650     /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
651     size = type = dw = 0xdeadbeef;
652     ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
653     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
654     ok(size == 4, "size=%d\n", size);
655     ok(type == REG_BINARY, "type=%d\n", type);
656     ok(dw == 0x12345678, "dw=%d\n", dw);
657     
658     /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
659     qw[0] = qw[1] = size = type = 0xdeadbeef;
660     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
661     ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
662     ok(size == 8, "size=%d\n", size);
663     ok(type == REG_BINARY, "type=%d\n", type);
664     ok(qw[0] == 0x12345678 && 
665        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
666     
667     /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
668     type = dw = 0xdeadbeef; size = 4;
669     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
670     ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
671     ok(dw == 0xdeadbeef, "dw=%d\n", dw);
672     ok(size == 8, "size=%d\n", size);
673
674     /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
675     qw[0] = qw[1] = size = type = 0xdeadbeef;
676     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
677     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
678     ok(size == 8, "size=%d\n", size);
679     ok(type == REG_BINARY, "type=%d\n", type);
680     ok(qw[0] == 0x12345678 &&
681        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
682
683     /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
684     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
685     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
686     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
687     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
688     ok(type == REG_SZ, "type=%d\n", type);
689     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
690
691     /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
692     type = 0xdeadbeef; size = 0;
693     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
694     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
695     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
696     ok(size == strlen(sTestpath1)+1 || size == strlen(sTestpath1)+2,
697        "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
698     ok(type == REG_SZ, "type=%d\n", type);
699
700     /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
701     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
702     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
703     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
704     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
705     ok(type == REG_SZ, "type=%d\n", type);
706     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
707
708     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
709     size = 0xbadbeef;
710     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
711     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
712     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
713     ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1),
714         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
715
716     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
717     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
718     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
719     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
720     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
721     ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1), 
722         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
723     ok(type == REG_SZ, "type=%d\n", type);
724     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
725     
726     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
727     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
728     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
729     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
730     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
731     ok(type == REG_EXPAND_SZ, "type=%d\n", type);
732     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
733
734     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
735     size = 0xbadbeef;
736     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
737     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
738     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
739     ok(size == strlen(sTestpath1)+1 || size == strlen(sTestpath1)+2,
740        "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
741
742     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
743     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
744     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
745
746     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
747     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
748     ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
749
750
751 static void test_reg_open_key(void)
752 {
753     DWORD ret = 0;
754     HKEY hkResult = NULL;
755     HKEY hkPreserve = NULL;
756
757     /* successful open */
758     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
759     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
760     ok(hkResult != NULL, "expected hkResult != NULL\n");
761     hkPreserve = hkResult;
762
763     /* these tests fail on Win9x, but we want to be compatible with NT, so
764      * run them if we can */
765     if (!(GetVersion() & 0x80000000))
766     {
767         /* open same key twice */
768         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
769         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
770         ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
771         ok(hkResult != NULL, "hkResult != NULL\n");
772         RegCloseKey(hkResult);
773     
774         /* open nonexistent key
775         * check that hkResult is set to NULL
776         */
777         hkResult = hkPreserve;
778         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
779         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
780         ok(hkResult == NULL, "expected hkResult == NULL\n");
781     
782         /* open the same nonexistent key again to make sure the key wasn't created */
783         hkResult = hkPreserve;
784         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
785         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
786         ok(hkResult == NULL, "expected hkResult == NULL\n");
787     
788         /* send in NULL lpSubKey
789         * check that hkResult receives the value of hKey
790         */
791         hkResult = hkPreserve;
792         ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
793         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
794         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
795     
796         /* send empty-string in lpSubKey */
797         hkResult = hkPreserve;
798         ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
799         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
800         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
801     
802         /* send in NULL lpSubKey and NULL hKey
803         * hkResult is set to NULL
804         */
805         hkResult = hkPreserve;
806         ret = RegOpenKeyA(NULL, NULL, &hkResult);
807         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
808         ok(hkResult == NULL, "expected hkResult == NULL\n");
809     }
810
811     /* only send NULL hKey
812      * the value of hkResult remains unchanged
813      */
814     hkResult = hkPreserve;
815     ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
816     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
817        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
818     ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
819     RegCloseKey(hkResult);
820
821     /* send in NULL hkResult */
822     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
823     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
824
825     /*  beginning backslash character */
826     ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
827        ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
828            ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
829            , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
830 }
831
832 static void test_reg_create_key(void)
833 {
834     LONG ret;
835     HKEY hkey1, hkey2;
836     ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
837     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
838     /* should succeed: all versions of Windows ignore the access rights
839      * to the parent handle */
840     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
841     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
842
843     /* clean up */
844     RegDeleteKey(hkey2, "");
845     RegDeleteKey(hkey1, "");
846
847     /*  beginning backslash character */
848     ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
849     if (!(GetVersion() & 0x80000000))
850         ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
851     else {
852         ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
853         RegDeleteKey(hkey1, NULL);
854     }
855 }
856
857 static void test_reg_close_key(void)
858 {
859     DWORD ret = 0;
860     HKEY hkHandle;
861
862     /* successfully close key
863      * hkHandle remains changed after call to RegCloseKey
864      */
865     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
866     ret = RegCloseKey(hkHandle);
867     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
868
869     /* try to close the key twice */
870     ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
871     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
872        "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
873     
874     /* try to close a NULL handle */
875     ret = RegCloseKey(NULL);
876     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
877        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
878
879     /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
880      * win98 doesn't give a new handle when the same key is opened.
881      * Not re-opening will make some next tests fail.
882      */
883     if (hkey_main == hkHandle)
884     {
885         trace("The main handle is most likely closed, so re-opening\n");
886         RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
887     }
888 }
889
890 static void test_reg_delete_key(void)
891 {
892     DWORD ret;
893
894     ret = RegDeleteKey(hkey_main, NULL);
895
896     /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
897      * there are also no subkeys available it will delete the key pointed to by hkey_main.
898      * Not re-creating will make some next tests fail.
899      */
900     if (ret == ERROR_SUCCESS)
901     {
902         trace("We are probably running on NT4 or W2K as the main key is deleted,"
903             " re-creating the main key\n");
904         setup_main_key();
905     }
906     else
907         ok(ret == ERROR_INVALID_PARAMETER ||
908            ret == ERROR_ACCESS_DENIED ||
909            ret == ERROR_BADKEY, /* Win95 */
910            "ret=%d\n", ret);
911 }
912
913 static void test_reg_save_key(void)
914 {
915     DWORD ret;
916
917     ret = RegSaveKey(hkey_main, "saved_key", NULL);
918     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
919 }
920
921 static void test_reg_load_key(void)
922 {
923     DWORD ret;
924     HKEY hkHandle;
925
926     ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
927     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
928
929     ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
930     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
931
932     RegCloseKey(hkHandle);
933 }
934
935 static void test_reg_unload_key(void)
936 {
937     DWORD ret;
938
939     ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
940     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
941
942     DeleteFile("saved_key");
943     DeleteFile("saved_key.LOG");
944 }
945
946 static BOOL set_privileges(LPCSTR privilege, BOOL set)
947 {
948     TOKEN_PRIVILEGES tp;
949     HANDLE hToken;
950     LUID luid;
951
952     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
953         return FALSE;
954
955     if(!LookupPrivilegeValue(NULL, privilege, &luid))
956     {
957         CloseHandle(hToken);
958         return FALSE;
959     }
960
961     tp.PrivilegeCount = 1;
962     tp.Privileges[0].Luid = luid;
963     
964     if (set)
965         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
966     else
967         tp.Privileges[0].Attributes = 0;
968
969     AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
970     if (GetLastError() != ERROR_SUCCESS)
971     {
972         CloseHandle(hToken);
973         return FALSE;
974     }
975
976     CloseHandle(hToken);
977     return TRUE;
978 }
979
980 /* tests that show that RegConnectRegistry and 
981    OpenSCManager accept computer names without the
982    \\ prefix (what MSDN says).   */
983 static void test_regconnectregistry( void)
984 {
985     CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
986     CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
987     DWORD len = sizeof(compName) ;
988     BOOL ret;
989     LONG retl;
990     HKEY hkey;
991     SC_HANDLE schnd;
992
993     SetLastError(0xdeadbeef);
994     ret = GetComputerNameA(compName, &len);
995     ok( ret, "GetComputerName failed err = %d\n", GetLastError());
996     if( !ret) return;
997
998     lstrcpyA(netwName, "\\\\");
999     lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1000
1001     retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
1002     ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %d\n", retl);
1003     if( !retl) RegCloseKey( hkey);
1004
1005     retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1006     ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %d\n", retl);
1007     if( !retl) RegCloseKey( hkey);
1008
1009     SetLastError(0xdeadbeef);
1010     schnd = OpenSCManagerA( compName, NULL, GENERIC_READ); 
1011     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1012     {
1013         skip("OpenSCManagerA is not implemented\n");
1014         return;
1015     }
1016
1017     ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1018     CloseServiceHandle( schnd);
1019
1020     SetLastError(0xdeadbeef);
1021     schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ); 
1022     ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1023     CloseServiceHandle( schnd);
1024
1025 }
1026
1027 static void test_reg_query_value(void)
1028 {
1029     HKEY subkey;
1030     CHAR val[MAX_PATH];
1031     WCHAR valW[5];
1032     LONG size, ret;
1033
1034     static const WCHAR expected[] = {'d','a','t','a',0};
1035
1036     ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1037     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1038
1039     ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1040     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1041
1042     /* try an invalid hkey */
1043     SetLastError(0xdeadbeef);
1044     size = MAX_PATH;
1045     ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1046     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1047        "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1048     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1049
1050     /* try a NULL hkey */
1051     SetLastError(0xdeadbeef);
1052     size = MAX_PATH;
1053     ret = RegQueryValueA(NULL, "subkey", val, &size);
1054     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1055        "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1056     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1057
1058     /* try a NULL value */
1059     size = MAX_PATH;
1060     ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1061     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1062     ok(size == 5, "Expected 5, got %d\n", size);
1063
1064     /* try a NULL size */
1065     SetLastError(0xdeadbeef);
1066     val[0] = '\0';
1067     ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1068     ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1069     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1070     ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1071
1072     /* try a NULL value and size */
1073     ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1074     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1075
1076     /* try a size too small */
1077     SetLastError(0xdeadbeef);
1078     val[0] = '\0';
1079     size = 1;
1080     ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1081     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1082     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1083     ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1084     ok(size == 5, "Expected 5, got %d\n", size);
1085
1086     /* successfully read the value using 'subkey' */
1087     size = MAX_PATH;
1088     ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1089     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1090     ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1091     ok(size == 5, "Expected 5, got %d\n", size);
1092
1093     /* successfully read the value using the subkey key */
1094     size = MAX_PATH;
1095     ret = RegQueryValueA(subkey, NULL, val, &size);
1096     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1097     ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1098     ok(size == 5, "Expected 5, got %d\n", size);
1099
1100     /* unicode - try size too small */
1101     SetLastError(0xdeadbeef);
1102     valW[0] = '\0';
1103     size = 0;
1104     ret = RegQueryValueW(subkey, NULL, valW, &size);
1105     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1106     {
1107         skip("RegQueryValueW is not implemented\n");
1108         goto cleanup;
1109     }
1110     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1111     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1112     ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1113     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1114
1115     /* unicode - try size in WCHARS */
1116     SetLastError(0xdeadbeef);
1117     size = sizeof(valW) / sizeof(WCHAR);
1118     ret = RegQueryValueW(subkey, NULL, valW, &size);
1119     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1120     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1121     ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1122     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1123
1124     /* unicode - successfully read the value */
1125     size = sizeof(valW);
1126     ret = RegQueryValueW(subkey, NULL, valW, &size);
1127     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1128     ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1129     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1130
1131     /* unicode - set the value without a NULL terminator */
1132     ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1133     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1134
1135     /* unicode - read the unterminated value, value is terminated for us */
1136     memset(valW, 'a', sizeof(valW));
1137     size = sizeof(valW);
1138     ret = RegQueryValueW(subkey, NULL, valW, &size);
1139     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1140     ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1141     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1142
1143 cleanup:
1144     RegDeleteKeyA(subkey, "");
1145     RegCloseKey(subkey);
1146 }
1147
1148 static void test_reg_delete_tree(void)
1149 {
1150     CHAR buffer[MAX_PATH];
1151     HKEY subkey, subkey2;
1152     LONG size, ret;
1153
1154     if(!pRegDeleteTreeA) {
1155         skip("Skipping RegDeleteTreeA tests, function not present\n");
1156         return;
1157     }
1158
1159     ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1160     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1161     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1162     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1163     ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1164     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1165     ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1166     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1167     ret = RegCloseKey(subkey2);
1168     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1169
1170     ret = pRegDeleteTreeA(subkey, "subkey2");
1171     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1172     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1173         "subkey2 was not deleted\n");
1174     size = MAX_PATH;
1175     ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1176         "Default value of subkey not longer present\n");
1177
1178     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1179     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1180     ret = RegCloseKey(subkey2);
1181     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1182     ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1183     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1184     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1185         "subkey2 was not deleted\n");
1186     ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1187         "Default value of subkey not longer present\n");
1188
1189     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1190     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1191     ret = RegCloseKey(subkey2);
1192     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1193     ret = RegCreateKeyA(subkey, "subkey3", &subkey2);
1194     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1195     ret = RegCloseKey(subkey2);
1196     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1197     ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1198     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1199     ret = pRegDeleteTreeA(subkey, NULL);
1200     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1201     ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1202         "subkey was deleted\n");
1203     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1204         "subkey2 was not deleted\n");
1205     ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1206         "subkey3 was not deleted\n");
1207     size = MAX_PATH;
1208     ret = RegQueryValueA(subkey, NULL, buffer, &size);
1209     ok(ret == ERROR_SUCCESS,
1210         "Default value of subkey is not present\n");
1211     ok(!lstrlenA(buffer),
1212         "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1213     size = MAX_PATH;
1214     ok(RegQueryValueA(subkey, "value", buffer, &size),
1215         "Value is still present\n");
1216
1217     ret = pRegDeleteTreeA(hkey_main, "not-here");
1218     ok(ret == ERROR_FILE_NOT_FOUND,
1219         "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1220 }
1221
1222 START_TEST(registry)
1223 {
1224     /* Load pointers for functions that are not available in all Windows versions */
1225     InitFunctionPtrs();
1226
1227     setup_main_key();
1228     test_set_value();
1229     create_test_entries();
1230     test_enum_value();
1231     test_query_value_ex();
1232     test_get_value();
1233     test_reg_open_key();
1234     test_reg_create_key();
1235     test_reg_close_key();
1236     test_reg_delete_key();
1237     test_reg_query_value();
1238
1239     /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
1240     if (set_privileges(SE_BACKUP_NAME, TRUE) &&
1241         set_privileges(SE_RESTORE_NAME, TRUE))
1242     {
1243         test_reg_save_key();
1244         test_reg_load_key();
1245         test_reg_unload_key();
1246
1247         set_privileges(SE_BACKUP_NAME, FALSE);
1248         set_privileges(SE_RESTORE_NAME, FALSE);
1249     }
1250
1251     test_reg_delete_tree();
1252
1253     /* cleanup */
1254     delete_key( hkey_main );
1255     
1256     test_regconnectregistry();
1257 }