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