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