dinput: Silence incorrect warning and move it to a valid place.
[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     size = 4;
585     ret = RegQueryValueExA(hkey_main, "BIN32", NULL, &size, buffer, &size);
586     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
587 }
588
589 static void test_get_value(void)
590 {
591     DWORD ret;
592     DWORD size;
593     DWORD type;
594     DWORD dw, qw[2];
595     CHAR buf[80];
596     CHAR expanded[] = "bar\\subdir1";
597    
598     if(!pRegGetValueA)
599     {
600         skip("RegGetValue not available on this platform\n");
601         return;
602     }
603
604     /* Invalid parameter */
605     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, NULL);
606     ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
607
608     /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
609     size = type = dw = 0xdeadbeef;
610     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
611     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
612     ok(size == 4, "size=%d\n", size);
613     ok(type == REG_DWORD, "type=%d\n", type);
614     ok(dw == 0x12345678, "dw=%d\n", dw);
615
616     /* Query by subkey-name */
617     ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
618     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
619
620     /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
621     size = type = dw = 0xdeadbeef;
622     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
623     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
624     /* Although the function failed all values are retrieved */
625     ok(size == 4, "size=%d\n", size);
626     ok(type == REG_DWORD, "type=%d\n", type);
627     ok(dw == 0x12345678, "dw=%d\n", dw);
628
629     /* Test RRF_ZEROONFAILURE */
630     type = dw = 0xdeadbeef; size = 4;
631     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
632     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
633     /* Again all values are retrieved ... */
634     ok(size == 4, "size=%d\n", size);
635     ok(type == REG_DWORD, "type=%d\n", type);
636     /* ... except the buffer, which is zeroed out */
637     ok(dw == 0, "dw=%d\n", dw);
638
639     /* Test RRF_ZEROONFAILURE with a NULL buffer... */
640     type = size = 0xbadbeef;
641     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, NULL, &size);
642     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
643     ok(size == 4, "size=%d\n", size);
644     ok(type == REG_DWORD, "type=%d\n", type);
645
646     /* Query REG_DWORD using RRF_RT_DWORD (ok) */
647     size = type = dw = 0xdeadbeef;
648     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
649     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
650     ok(size == 4, "size=%d\n", size);
651     ok(type == REG_DWORD, "type=%d\n", type);
652     ok(dw == 0x12345678, "dw=%d\n", dw);
653
654     /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
655     size = type = dw = 0xdeadbeef;
656     ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
657     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
658     ok(size == 4, "size=%d\n", size);
659     ok(type == REG_BINARY, "type=%d\n", type);
660     ok(dw == 0x12345678, "dw=%d\n", dw);
661     
662     /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
663     qw[0] = qw[1] = size = type = 0xdeadbeef;
664     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
665     ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
666     ok(size == 8, "size=%d\n", size);
667     ok(type == REG_BINARY, "type=%d\n", type);
668     ok(qw[0] == 0x12345678 && 
669        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
670     
671     /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
672     type = dw = 0xdeadbeef; size = 4;
673     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
674     ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
675     ok(dw == 0xdeadbeef, "dw=%d\n", dw);
676     ok(size == 8, "size=%d\n", size);
677
678     /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
679     qw[0] = qw[1] = size = type = 0xdeadbeef;
680     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
681     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
682     ok(size == 8, "size=%d\n", size);
683     ok(type == REG_BINARY, "type=%d\n", type);
684     ok(qw[0] == 0x12345678 &&
685        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
686
687     /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
688     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
689     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
690     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
691     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
692     ok(type == REG_SZ, "type=%d\n", type);
693     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
694
695     /* Query REG_SZ using RRF_RT_REG_SZ and no buffer (ok) */
696     type = 0xdeadbeef; size = 0;
697     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, NULL, &size);
698     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
699     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
700     ok(size == strlen(sTestpath1)+1 || size == strlen(sTestpath1)+2,
701        "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
702     ok(type == REG_SZ, "type=%d\n", type);
703
704     /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
705     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
706     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
707     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
708     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
709     ok(type == REG_SZ, "type=%d\n", type);
710     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
711
712     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ and no buffer (ok, expands) */
713     size = 0xbadbeef;
714     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, NULL, NULL, &size);
715     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
716     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
717     ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1),
718         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
719
720     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
721     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
722     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
723     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
724     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
725     ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1), 
726         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
727     ok(type == REG_SZ, "type=%d\n", type);
728     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
729     
730     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
731     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
732     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
733     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
734     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
735     ok(type == REG_EXPAND_SZ, "type=%d\n", type);
736     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
737
738     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND and no buffer (ok, doesn't expand) */
739     size = 0xbadbeef;
740     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, NULL, NULL, &size);
741     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
742     /* v5.2.3790.1830 (2003 SP1) returns sTestpath1 length + 2 here. */
743     ok(size == strlen(sTestpath1)+1 || size == strlen(sTestpath1)+2,
744        "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
745
746     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
747     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
748     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
749
750     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
751     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
752     ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
753
754
755 static void test_reg_open_key(void)
756 {
757     DWORD ret = 0;
758     HKEY hkResult = NULL;
759     HKEY hkPreserve = NULL;
760
761     /* successful open */
762     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
763     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
764     ok(hkResult != NULL, "expected hkResult != NULL\n");
765     hkPreserve = hkResult;
766
767     /* these tests fail on Win9x, but we want to be compatible with NT, so
768      * run them if we can */
769     if (!(GetVersion() & 0x80000000))
770     {
771         /* open same key twice */
772         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
773         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
774         ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
775         ok(hkResult != NULL, "hkResult != NULL\n");
776         RegCloseKey(hkResult);
777     
778         /* open nonexistent key
779         * check that hkResult is set to NULL
780         */
781         hkResult = hkPreserve;
782         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
783         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
784         ok(hkResult == NULL, "expected hkResult == NULL\n");
785     
786         /* open the same nonexistent key again to make sure the key wasn't created */
787         hkResult = hkPreserve;
788         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
789         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
790         ok(hkResult == NULL, "expected hkResult == NULL\n");
791     
792         /* send in NULL lpSubKey
793         * check that hkResult receives the value of hKey
794         */
795         hkResult = hkPreserve;
796         ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
797         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
798         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
799     
800         /* send empty-string in lpSubKey */
801         hkResult = hkPreserve;
802         ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
803         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
804         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
805     
806         /* send in NULL lpSubKey and NULL hKey
807         * hkResult is set to NULL
808         */
809         hkResult = hkPreserve;
810         ret = RegOpenKeyA(NULL, NULL, &hkResult);
811         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
812         ok(hkResult == NULL, "expected hkResult == NULL\n");
813     }
814
815     /* only send NULL hKey
816      * the value of hkResult remains unchanged
817      */
818     hkResult = hkPreserve;
819     ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
820     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
821        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
822     ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
823     RegCloseKey(hkResult);
824
825     /* send in NULL hkResult */
826     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
827     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
828
829     /*  beginning backslash character */
830     ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
831        ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
832            ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
833            , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
834 }
835
836 static void test_reg_create_key(void)
837 {
838     LONG ret;
839     HKEY hkey1, hkey2;
840     ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
841     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
842     /* should succeed: all versions of Windows ignore the access rights
843      * to the parent handle */
844     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
845     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
846
847     /* clean up */
848     RegDeleteKey(hkey2, "");
849     RegDeleteKey(hkey1, "");
850
851     /*  beginning backslash character */
852     ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
853     if (!(GetVersion() & 0x80000000))
854         ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
855     else {
856         ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
857         RegDeleteKey(hkey1, NULL);
858     }
859 }
860
861 static void test_reg_close_key(void)
862 {
863     DWORD ret = 0;
864     HKEY hkHandle;
865
866     /* successfully close key
867      * hkHandle remains changed after call to RegCloseKey
868      */
869     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
870     ret = RegCloseKey(hkHandle);
871     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
872
873     /* try to close the key twice */
874     ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
875     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
876        "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
877     
878     /* try to close a NULL handle */
879     ret = RegCloseKey(NULL);
880     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
881        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
882
883     /* Check to see if we didn't potentially close our main handle, which could happen on win98 as
884      * win98 doesn't give a new handle when the same key is opened.
885      * Not re-opening will make some next tests fail.
886      */
887     if (hkey_main == hkHandle)
888     {
889         trace("The main handle is most likely closed, so re-opening\n");
890         RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main );
891     }
892 }
893
894 static void test_reg_delete_key(void)
895 {
896     DWORD ret;
897
898     ret = RegDeleteKey(hkey_main, NULL);
899
900     /* There is a bug in NT4 and W2K that doesn't check if the subkey is NULL. If
901      * there are also no subkeys available it will delete the key pointed to by hkey_main.
902      * Not re-creating will make some next tests fail.
903      */
904     if (ret == ERROR_SUCCESS)
905     {
906         trace("We are probably running on NT4 or W2K as the main key is deleted,"
907             " re-creating the main key\n");
908         setup_main_key();
909     }
910     else
911         ok(ret == ERROR_INVALID_PARAMETER ||
912            ret == ERROR_ACCESS_DENIED ||
913            ret == ERROR_BADKEY, /* Win95 */
914            "ret=%d\n", ret);
915 }
916
917 static void test_reg_save_key(void)
918 {
919     DWORD ret;
920
921     ret = RegSaveKey(hkey_main, "saved_key", NULL);
922     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
923 }
924
925 static void test_reg_load_key(void)
926 {
927     DWORD ret;
928     HKEY hkHandle;
929
930     ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
931     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
932
933     ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
934     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
935
936     RegCloseKey(hkHandle);
937 }
938
939 static void test_reg_unload_key(void)
940 {
941     DWORD ret;
942
943     ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
944     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
945
946     DeleteFile("saved_key");
947     DeleteFile("saved_key.LOG");
948 }
949
950 static BOOL set_privileges(LPCSTR privilege, BOOL set)
951 {
952     TOKEN_PRIVILEGES tp;
953     HANDLE hToken;
954     LUID luid;
955
956     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
957         return FALSE;
958
959     if(!LookupPrivilegeValue(NULL, privilege, &luid))
960     {
961         CloseHandle(hToken);
962         return FALSE;
963     }
964
965     tp.PrivilegeCount = 1;
966     tp.Privileges[0].Luid = luid;
967     
968     if (set)
969         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
970     else
971         tp.Privileges[0].Attributes = 0;
972
973     AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
974     if (GetLastError() != ERROR_SUCCESS)
975     {
976         CloseHandle(hToken);
977         return FALSE;
978     }
979
980     CloseHandle(hToken);
981     return TRUE;
982 }
983
984 /* tests that show that RegConnectRegistry and 
985    OpenSCManager accept computer names without the
986    \\ prefix (what MSDN says).   */
987 static void test_regconnectregistry( void)
988 {
989     CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
990     CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
991     DWORD len = sizeof(compName) ;
992     BOOL ret;
993     LONG retl;
994     HKEY hkey;
995     SC_HANDLE schnd;
996
997     SetLastError(0xdeadbeef);
998     ret = GetComputerNameA(compName, &len);
999     ok( ret, "GetComputerName failed err = %d\n", GetLastError());
1000     if( !ret) return;
1001
1002     lstrcpyA(netwName, "\\\\");
1003     lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
1004
1005     retl = RegConnectRegistryA( compName, 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     retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
1010     ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %d\n", retl);
1011     if( !retl) RegCloseKey( hkey);
1012
1013     SetLastError(0xdeadbeef);
1014     schnd = OpenSCManagerA( compName, NULL, GENERIC_READ); 
1015     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1016     {
1017         skip("OpenSCManagerA is not implemented\n");
1018         return;
1019     }
1020
1021     ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1022     CloseServiceHandle( schnd);
1023
1024     SetLastError(0xdeadbeef);
1025     schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ); 
1026     ok( schnd != NULL, "OpenSCManagerA failed err = %d\n", GetLastError());
1027     CloseServiceHandle( schnd);
1028
1029 }
1030
1031 static void test_reg_query_value(void)
1032 {
1033     HKEY subkey;
1034     CHAR val[MAX_PATH];
1035     WCHAR valW[5];
1036     LONG size, ret;
1037
1038     static const WCHAR expected[] = {'d','a','t','a',0};
1039
1040     ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1041     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1042
1043     ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1044     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1045
1046     /* try an invalid hkey */
1047     SetLastError(0xdeadbeef);
1048     size = MAX_PATH;
1049     ret = RegQueryValueA((HKEY)0xcafebabe, "subkey", val, &size);
1050     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1051        "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1052     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1053
1054     /* try a NULL hkey */
1055     SetLastError(0xdeadbeef);
1056     size = MAX_PATH;
1057     ret = RegQueryValueA(NULL, "subkey", val, &size);
1058     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 98 returns BADKEY */
1059        "Expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
1060     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1061
1062     /* try a NULL value */
1063     size = MAX_PATH;
1064     ret = RegQueryValueA(hkey_main, "subkey", NULL, &size);
1065     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1066     ok(size == 5, "Expected 5, got %d\n", size);
1067
1068     /* try a NULL size */
1069     SetLastError(0xdeadbeef);
1070     val[0] = '\0';
1071     ret = RegQueryValueA(hkey_main, "subkey", val, NULL);
1072     ok(ret == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", ret);
1073     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1074     ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1075
1076     /* try a NULL value and size */
1077     ret = RegQueryValueA(hkey_main, "subkey", NULL, NULL);
1078     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1079
1080     /* try a size too small */
1081     SetLastError(0xdeadbeef);
1082     val[0] = '\0';
1083     size = 1;
1084     ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1085     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1086     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1087     ok(lstrlenA(val) == 0, "Expected val to be untouched, got %s\n", val);
1088     ok(size == 5, "Expected 5, got %d\n", size);
1089
1090     /* successfully read the value using 'subkey' */
1091     size = MAX_PATH;
1092     ret = RegQueryValueA(hkey_main, "subkey", val, &size);
1093     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1094     ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1095     ok(size == 5, "Expected 5, got %d\n", size);
1096
1097     /* successfully read the value using the subkey key */
1098     size = MAX_PATH;
1099     ret = RegQueryValueA(subkey, NULL, val, &size);
1100     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1101     ok(!lstrcmpA(val, "data"), "Expected 'data', got '%s'\n", val);
1102     ok(size == 5, "Expected 5, got %d\n", size);
1103
1104     /* unicode - try size too small */
1105     SetLastError(0xdeadbeef);
1106     valW[0] = '\0';
1107     size = 0;
1108     ret = RegQueryValueW(subkey, NULL, valW, &size);
1109     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1110     {
1111         skip("RegQueryValueW is not implemented\n");
1112         goto cleanup;
1113     }
1114     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1115     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1116     ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1117     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1118
1119     /* unicode - try size in WCHARS */
1120     SetLastError(0xdeadbeef);
1121     size = sizeof(valW) / sizeof(WCHAR);
1122     ret = RegQueryValueW(subkey, NULL, valW, &size);
1123     ok(ret == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", ret);
1124     ok(GetLastError() == 0xdeadbeef, "Expected 0xdeadbeef, got %d\n", GetLastError());
1125     ok(lstrlenW(valW) == 0, "Expected valW to be untouched\n");
1126     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1127
1128     /* unicode - successfully read the value */
1129     size = sizeof(valW);
1130     ret = RegQueryValueW(subkey, NULL, valW, &size);
1131     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1132     ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1133     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1134
1135     /* unicode - set the value without a NULL terminator */
1136     ret = RegSetValueW(subkey, NULL, REG_SZ, expected, sizeof(expected)-sizeof(WCHAR));
1137     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1138
1139     /* unicode - read the unterminated value, value is terminated for us */
1140     memset(valW, 'a', sizeof(valW));
1141     size = sizeof(valW);
1142     ret = RegQueryValueW(subkey, NULL, valW, &size);
1143     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1144     ok(!lstrcmpW(valW, expected), "Got wrong value\n");
1145     ok(size == sizeof(expected), "Got wrong size: %d\n", size);
1146
1147 cleanup:
1148     RegDeleteKeyA(subkey, "");
1149     RegCloseKey(subkey);
1150 }
1151
1152 static void test_reg_delete_tree(void)
1153 {
1154     CHAR buffer[MAX_PATH];
1155     HKEY subkey, subkey2;
1156     LONG size, ret;
1157
1158     if(!pRegDeleteTreeA) {
1159         skip("Skipping RegDeleteTreeA tests, function not present\n");
1160         return;
1161     }
1162
1163     ret = RegCreateKeyA(hkey_main, "subkey", &subkey);
1164     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1165     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1166     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1167     ret = RegSetValueA(subkey, NULL, REG_SZ, "data", 4);
1168     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1169     ret = RegSetValueA(subkey2, NULL, REG_SZ, "data2", 5);
1170     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1171     ret = RegCloseKey(subkey2);
1172     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1173
1174     ret = pRegDeleteTreeA(subkey, "subkey2");
1175     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1176     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1177         "subkey2 was not deleted\n");
1178     size = MAX_PATH;
1179     ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1180         "Default value of subkey not longer present\n");
1181
1182     ret = RegCreateKeyA(subkey, "subkey2", &subkey2);
1183     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1184     ret = RegCloseKey(subkey2);
1185     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1186     ret = pRegDeleteTreeA(hkey_main, "subkey\\subkey2");
1187     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1188     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1189         "subkey2 was not deleted\n");
1190     ok(!RegQueryValueA(subkey, NULL, buffer, &size),
1191         "Default value of subkey not longer present\n");
1192
1193     ret = RegCreateKeyA(subkey, "subkey2", &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 = RegCreateKeyA(subkey, "subkey3", &subkey2);
1198     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1199     ret = RegCloseKey(subkey2);
1200     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1201     ret = RegSetValueA(subkey, "value", REG_SZ, "data2", 5);
1202     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1203     ret = pRegDeleteTreeA(subkey, NULL);
1204     ok(ret == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", ret);
1205     ok(!RegOpenKeyA(hkey_main, "subkey", &subkey),
1206         "subkey was deleted\n");
1207     ok(RegOpenKeyA(subkey, "subkey2", &subkey2),
1208         "subkey2 was not deleted\n");
1209     ok(RegOpenKeyA(subkey, "subkey3", &subkey2),
1210         "subkey3 was not deleted\n");
1211     size = MAX_PATH;
1212     ret = RegQueryValueA(subkey, NULL, buffer, &size);
1213     ok(ret == ERROR_SUCCESS,
1214         "Default value of subkey is not present\n");
1215     ok(!lstrlenA(buffer),
1216         "Expected length 0 got length %u(%s)\n", lstrlenA(buffer), buffer);
1217     size = MAX_PATH;
1218     ok(RegQueryValueA(subkey, "value", buffer, &size),
1219         "Value is still present\n");
1220
1221     ret = pRegDeleteTreeA(hkey_main, "not-here");
1222     ok(ret == ERROR_FILE_NOT_FOUND,
1223         "Expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
1224 }
1225
1226 START_TEST(registry)
1227 {
1228     /* Load pointers for functions that are not available in all Windows versions */
1229     InitFunctionPtrs();
1230
1231     setup_main_key();
1232     test_set_value();
1233     create_test_entries();
1234     test_enum_value();
1235     test_query_value_ex();
1236     test_get_value();
1237     test_reg_open_key();
1238     test_reg_create_key();
1239     test_reg_close_key();
1240     test_reg_delete_key();
1241     test_reg_query_value();
1242
1243     /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
1244     if (set_privileges(SE_BACKUP_NAME, TRUE) &&
1245         set_privileges(SE_RESTORE_NAME, TRUE))
1246     {
1247         test_reg_save_key();
1248         test_reg_load_key();
1249         test_reg_unload_key();
1250
1251         set_privileges(SE_BACKUP_NAME, FALSE);
1252         set_privileges(SE_RESTORE_NAME, FALSE);
1253     }
1254
1255     test_reg_delete_tree();
1256
1257     /* cleanup */
1258     delete_key( hkey_main );
1259     
1260     test_regconnectregistry();
1261 }