wined3d: Remove stray '\' at end of lines.
[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 "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winreg.h"
27 #include "winsvc.h"
28 #include "winerror.h"
29
30 static HKEY hkey_main;
31 static DWORD GLE;
32
33 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
34 static const char * sTestpath2 = "%FOO%\\subdir1";
35
36 /* delete key and all its subkeys */
37 static DWORD delete_key( HKEY hkey )
38 {
39     char name[MAX_PATH];
40     DWORD ret;
41
42     while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
43     {
44         HKEY tmp;
45         if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
46         {
47             ret = delete_key( tmp );
48             RegCloseKey( tmp );
49         }
50         if (ret) break;
51     }
52     if (ret != ERROR_NO_MORE_ITEMS) return ret;
53     RegDeleteKeyA( hkey, "" );
54     return 0;
55 }
56
57 static void setup_main_key(void)
58 {
59     if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
60
61     assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
62 }
63
64 static void test_hkey_main_Value_A(LPCSTR name, LPCSTR string)
65 {
66     DWORD ret, type, cbData;
67     DWORD str_byte_len, full_byte_len;
68
69     ret = RegQueryValueExA(hkey_main, name, NULL, &type, NULL, &cbData);
70     GLE = GetLastError();
71     ok(ret == ERROR_SUCCESS, "RegQueryValueExA failed: %d, GLE=%d\n", ret, GLE);
72     if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
73
74     str_byte_len = lstrlenA(string) + 1;
75     full_byte_len = sizeof(string);
76     ok(type == REG_SZ, "RegQueryValueExA returned type %d\n", type);
77     ok(cbData == full_byte_len || cbData == str_byte_len,
78         "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
79 }
80
81 static void test_hkey_main_Value_W(LPCWSTR name, LPCWSTR string)
82 {
83     DWORD ret, type, cbData;
84     DWORD str_byte_len, full_byte_len;
85
86     ret = RegQueryValueExW(hkey_main, name, NULL, &type, NULL, &cbData);
87     GLE = GetLastError();
88     ok(ret == ERROR_SUCCESS, "RegQueryValueExW failed: %d, GLE=%d\n", ret, GLE);
89     if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return;
90
91     str_byte_len = (lstrlenW(string) + 1) * sizeof(WCHAR);
92     full_byte_len = sizeof(string);
93     ok(type == REG_SZ, "RegQueryValueExW returned type %d\n", type);
94     ok(cbData == full_byte_len || cbData == str_byte_len,
95         "cbData=%d instead of %d or %d\n", cbData, full_byte_len, str_byte_len);
96 }
97
98 static void test_set_value(void)
99 {
100     DWORD ret;
101
102     static const WCHAR name1W[] =   {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
103     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};
104     static const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
105     static const WCHAR string2W[] = {'T','h','i','s', 0 ,'B','r','e','a','k','s', 0 , 0 ,'A', 0 , 0 , 0 , 0 ,'L','o','t', 0 , 0 , 0 , 0};
106
107     static const char name1A[] =   "CleanSingleString";
108     static const char name2A[] =   "SomeIntraZeroedString";
109     static const char string1A[] = "ThisNeverBreaks";
110     static const char string2A[] = "This\0Breaks\0\0A\0\0\0Lot\0\0\0\0";
111
112     /* test RegSetValueExA with normal string */
113     ret = RegSetValueExA(hkey_main, name1A, 0, REG_SZ, (const BYTE *)string1A, sizeof(string1A));
114     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
115     test_hkey_main_Value_A(name1A, string1A);
116     test_hkey_main_Value_W(name1W, string1W);
117
118     /* test RegSetValueExA with intrazeroed string */
119     ret = RegSetValueExA(hkey_main, name2A, 0, REG_SZ, (const BYTE *)string2A, sizeof(string2A));
120     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %d, GLE=%d\n", ret, GetLastError());
121     test_hkey_main_Value_A(name1A, string1A);
122     test_hkey_main_Value_W(name1W, string1W);
123
124     /* 9x doesn't support W-calls, so don't test them then */
125     if(GLE == ERROR_CALL_NOT_IMPLEMENTED) return; 
126
127     /* test RegSetValueExW with normal string */
128     ret = RegSetValueExW(hkey_main, name1W, 0, REG_SZ, (const BYTE *)string1W, sizeof(string1W));
129     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
130     test_hkey_main_Value_A(name1A, string1A);
131     test_hkey_main_Value_W(name1W, string1W);
132
133     /* test RegSetValueExW with intrazeroed string */
134     ret = RegSetValueExW(hkey_main, name2W, 0, REG_SZ, (const BYTE *)string2W, sizeof(string2W));
135     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %d, GLE=%d\n", ret, GetLastError());
136     test_hkey_main_Value_A(name1A, string1A);
137     test_hkey_main_Value_W(name1W, string1W);
138 }
139
140 static void create_test_entries(void)
141 {
142     static const DWORD qw[2] = { 0x12345678, 0x87654321 };
143
144     SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
145     SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
146
147     ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1), 
148         "RegSetValueExA failed\n");
149     ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (const BYTE *)sTestpath1, strlen(sTestpath1)+1), 
150         "RegSetValueExA failed\n");
151     ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (const BYTE *)sTestpath2, strlen(sTestpath2)+1), 
152         "RegSetValueExA failed\n");
153     ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (const BYTE *)qw, 4),
154         "RegSetValueExA failed\n");
155     ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (const BYTE *)qw, 4),
156         "RegSetValueExA failed\n");
157     ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (const BYTE *)qw, 8),
158         "RegSetValueExA failed\n");
159 }
160         
161 static void test_enum_value(void)
162 {
163     DWORD res;
164     HKEY test_key;
165     char value[20], data[20];
166     WCHAR valueW[20], dataW[20];
167     DWORD val_count, data_count, type;
168     static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
169     static const WCHAR testW[] = {'T','e','s','t',0};
170     static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
171
172     /* create the working key for new 'Test' value */
173     res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
174     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res);
175
176     /* check NULL data with zero length */
177     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
178     if (GetVersion() & 0x80000000)
179         ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %d\n", res );
180     else
181         ok( !res, "RegSetValueExA returned %d\n", res );
182     res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
183     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
184     res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
185     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %d\n", res );
186
187     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (const BYTE *)"foobar", 7 );
188     ok( res == 0, "RegSetValueExA failed error %d\n", res );
189
190     /* overflow both name and data */
191     val_count = 2;
192     data_count = 2;
193     type = 1234;
194     strcpy( value, "xxxxxxxxxx" );
195     strcpy( data, "xxxxxxxxxx" );
196     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
197     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
198     ok( val_count == 2, "val_count set to %d\n", val_count );
199     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
200     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
201     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
202     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
203
204     /* overflow name */
205     val_count = 3;
206     data_count = 20;
207     type = 1234;
208     strcpy( value, "xxxxxxxxxx" );
209     strcpy( data, "xxxxxxxxxx" );
210     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
211     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
212     /* Win9x returns 2 as specified by MSDN but NT returns 3... */
213     ok( val_count == 2 || val_count == 3, "val_count set to %d\n", val_count );
214     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
215     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
216     /* v5.1.2600.0 (XP Home and Proffesional) does not touch value or data in this case */
217     ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ), 
218         "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
219     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ), 
220         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
221
222     /* overflow empty name */
223     val_count = 0;
224     data_count = 20;
225     type = 1234;
226     strcpy( value, "xxxxxxxxxx" );
227     strcpy( data, "xxxxxxxxxx" );
228     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
229     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
230     ok( val_count == 0, "val_count set to %d\n", val_count );
231     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
232     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
233     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
234     /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
235     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ), 
236         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
237
238     /* overflow data */
239     val_count = 20;
240     data_count = 2;
241     type = 1234;
242     strcpy( value, "xxxxxxxxxx" );
243     strcpy( data, "xxxxxxxxxx" );
244     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
245     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
246     ok( val_count == 20, "val_count set to %d\n", val_count );
247     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
248     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
249     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
250     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
251
252     /* no overflow */
253     val_count = 20;
254     data_count = 20;
255     type = 1234;
256     strcpy( value, "xxxxxxxxxx" );
257     strcpy( data, "xxxxxxxxxx" );
258     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
259     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
260     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
261     ok( data_count == 7, "data_count set to %d instead of 7\n", data_count );
262     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
263     ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
264     ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
265
266     /* Unicode tests */
267
268     SetLastError(0);
269     res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
270     if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
271         return;
272     ok( res == 0, "RegSetValueExW failed error %d\n", res );
273
274     /* overflow both name and data */
275     val_count = 2;
276     data_count = 2;
277     type = 1234;
278     memcpy( valueW, xxxW, sizeof(xxxW) );
279     memcpy( dataW, xxxW, sizeof(xxxW) );
280     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
281     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
282     ok( val_count == 2, "val_count set to %d\n", val_count );
283     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
284     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
285     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
286     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
287
288     /* overflow name */
289     val_count = 3;
290     data_count = 20;
291     type = 1234;
292     memcpy( valueW, xxxW, sizeof(xxxW) );
293     memcpy( dataW, xxxW, sizeof(xxxW) );
294     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
295     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
296     ok( val_count == 3, "val_count set to %d\n", val_count );
297     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
298     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
299     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
300     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
301
302     /* overflow data */
303     val_count = 20;
304     data_count = 2;
305     type = 1234;
306     memcpy( valueW, xxxW, sizeof(xxxW) );
307     memcpy( dataW, xxxW, sizeof(xxxW) );
308     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
309     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %d\n", res );
310     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
311     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
312     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
313     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
314     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
315
316     /* no overflow */
317     val_count = 20;
318     data_count = 20;
319     type = 1234;
320     memcpy( valueW, xxxW, sizeof(xxxW) );
321     memcpy( dataW, xxxW, sizeof(xxxW) );
322     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
323     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", res );
324     ok( val_count == 4, "val_count set to %d instead of 4\n", val_count );
325     ok( data_count == 7*sizeof(WCHAR), "data_count set to %d instead of 7*sizeof(WCHAR)\n", data_count );
326     ok( type == REG_SZ, "type %d is not REG_SZ\n", type );
327     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
328     ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
329 }
330
331 static void test_query_value_ex(void)
332 {
333     DWORD ret;
334     DWORD size;
335     DWORD type;
336     BYTE buffer[10];
337     
338     ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
339     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
340     ok(size == strlen(sTestpath1) + 1, "(%d,%d)\n", (DWORD)strlen(sTestpath1) + 1, size);
341     ok(type == REG_SZ, "type %d is not REG_SZ\n", type);
342
343     type = 0xdeadbeef;
344     size = 0xdeadbeef;
345     ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, NULL, &size);
346     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
347     ok(size == 0, "size should have been set to 0 instead of %d\n", size);
348     /* the type parameter is cleared on Win9x, but is set to a random value on
349      * NT, so don't do this test there */
350     if (GetVersion() & 0x80000000)
351         ok(type == 0, "type should have been set to 0 instead of 0x%x\n", type);
352     else
353         trace("test_query_value_ex: type set to: 0x%08x\n", type);
354
355     size = sizeof(buffer);
356     ret = RegQueryValueExA(HKEY_CLASSES_ROOT, "Nonexistent Value", NULL, &type, buffer, &size);
357     ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
358     ok(size == sizeof(buffer), "size shouldn't have been changed to %d\n", size);
359 }
360
361 static void test_get_value(void)
362 {
363     HMODULE hadvapi32;
364     DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
365     
366     DWORD ret;
367     DWORD size;
368     DWORD type;
369     DWORD dw, qw[2];
370     CHAR buf[80];
371     CHAR expanded[] = "bar\\subdir1";
372    
373     /* This function was introduced with Windows 2003 SP1 */
374     hadvapi32 = LoadLibraryA("advapi32.dll");
375     if(!hadvapi32) 
376     {
377         ok(0, "error=%d\n", GetLastError());
378         return;
379     }
380     pRegGetValueA = (PVOID)GetProcAddress(hadvapi32, "RegGetValueA");
381     if(!pRegGetValueA) 
382         return;
383
384     /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
385     size = type = dw = 0xdeadbeef;
386     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
387     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
388     ok(size == 4, "size=%d\n", size);
389     ok(type == REG_DWORD, "type=%d\n", type);
390     ok(dw == 0x12345678, "dw=%d\n", dw);
391
392     /* Query by subkey-name */
393     ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
394     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
395
396     /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
397     size = type = dw = 0xdeadbeef;
398     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
399     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
400     /* Although the function failed all values are retrieved */
401     ok(size == 4, "size=%d\n", size);
402     ok(type == REG_DWORD, "type=%d\n", type);
403     ok(dw == 0x12345678, "dw=%d\n", dw);
404
405     /* Test RRF_ZEROONFAILURE */
406     type = dw = 0xdeadbeef; size = 4;
407     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
408     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
409     /* Again all values are retrieved ... */
410     ok(size == 4, "size=%d\n", size);
411     ok(type == REG_DWORD, "type=%d\n", type);
412     /* ... except the buffer, which is zeroed out */
413     ok(dw == 0, "dw=%d\n", dw);
414
415     /* Query REG_DWORD using RRF_RT_DWORD (ok) */
416     size = type = dw = 0xdeadbeef;
417     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
418     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
419     ok(size == 4, "size=%d\n", size);
420     ok(type == REG_DWORD, "type=%d\n", type);
421     ok(dw == 0x12345678, "dw=%d\n", dw);
422
423     /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
424     size = type = dw = 0xdeadbeef;
425     ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
426     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
427     ok(size == 4, "size=%d\n", size);
428     ok(type == REG_BINARY, "type=%d\n", type);
429     ok(dw == 0x12345678, "dw=%d\n", dw);
430     
431     /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
432     qw[0] = qw[1] = size = type = 0xdeadbeef;
433     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
434     ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%d\n", ret);
435     ok(size == 8, "size=%d\n", size);
436     ok(type == REG_BINARY, "type=%d\n", type);
437     ok(qw[0] == 0x12345678 && 
438        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
439     
440     /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
441     type = dw = 0xdeadbeef; size = 4;
442     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
443     ok(ret == ERROR_MORE_DATA, "ret=%d\n", ret);
444     ok(dw == 0xdeadbeef, "dw=%d\n", dw);
445     ok(size == 8, "size=%d\n", size);
446
447     /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
448     qw[0] = qw[1] = size = type = 0xdeadbeef;
449     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
450     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
451     ok(size == 8, "size=%d\n", size);
452     ok(type == REG_BINARY, "type=%d\n", type);
453     ok(qw[0] == 0x12345678 &&
454        qw[1] == 0x87654321, "qw={%d,%d}\n", qw[0], qw[1]);
455
456     /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
457     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
458     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
459     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
460     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
461     ok(type == REG_SZ, "type=%d\n", type);
462     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
463
464     /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
465     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
466     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
467     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
468     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
469     ok(type == REG_SZ, "type=%d\n", type);
470     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
471
472     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
473     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
474     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
475     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
476     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
477     ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1), 
478         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%d\n", lstrlenA(expanded), lstrlenA(sTestpath1), size);
479     ok(type == REG_SZ, "type=%d\n", type);
480     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
481     
482     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
483     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
484     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
485     ok(ret == ERROR_SUCCESS, "ret=%d\n", ret);
486     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%d\n", lstrlenA(sTestpath1), size);
487     ok(type == REG_EXPAND_SZ, "type=%d\n", type);
488     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
489     
490     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
491     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
492     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%d\n", ret);
493
494     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
495     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
496     ok(ret == ERROR_INVALID_PARAMETER, "ret=%d\n", ret);
497
498
499 static void test_reg_open_key(void)
500 {
501     DWORD ret = 0;
502     HKEY hkResult = NULL;
503     HKEY hkPreserve = NULL;
504
505     /* successful open */
506     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
507     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
508     ok(hkResult != NULL, "expected hkResult != NULL\n");
509     hkPreserve = hkResult;
510
511     /* these tests fail on Win9x, but we want to be compatible with NT, so
512      * run them if we can */
513     if (!(GetVersion() & 0x80000000))
514     {
515         /* open same key twice */
516         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
517         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
518         ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
519         ok(hkResult != NULL, "hkResult != NULL\n");
520         RegCloseKey(hkResult);
521     
522         /* open nonexistent key
523         * check that hkResult is set to NULL
524         */
525         hkResult = hkPreserve;
526         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
527         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
528         ok(hkResult == NULL, "expected hkResult == NULL\n");
529     
530         /* open the same nonexistent key again to make sure the key wasn't created */
531         hkResult = hkPreserve;
532         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
533         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", ret);
534         ok(hkResult == NULL, "expected hkResult == NULL\n");
535     
536         /* send in NULL lpSubKey
537         * check that hkResult receives the value of hKey
538         */
539         hkResult = hkPreserve;
540         ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
541         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
542         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
543     
544         /* send empty-string in lpSubKey */
545         hkResult = hkPreserve;
546         ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
547         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
548         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
549     
550         /* send in NULL lpSubKey and NULL hKey
551         * hkResult is set to NULL
552         */
553         hkResult = hkPreserve;
554         ret = RegOpenKeyA(NULL, NULL, &hkResult);
555         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
556         ok(hkResult == NULL, "expected hkResult == NULL\n");
557     }
558
559     /* only send NULL hKey
560      * the value of hkResult remains unchanged
561      */
562     hkResult = hkPreserve;
563     ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
564     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
565        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
566     ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
567     RegCloseKey(hkResult);
568
569     /* send in NULL hkResult */
570     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
571     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", ret);
572
573     /*  beginning backslash character */
574     ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
575        ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
576            ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
577            , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %d\n", ret);
578 }
579
580 static void test_reg_create_key(void)
581 {
582     LONG ret;
583     HKEY hkey1, hkey2;
584     ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
585     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
586     /* should succeed: all versions of Windows ignore the access rights
587      * to the parent handle */
588     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
589     ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
590
591     /* clean up */
592     RegDeleteKey(hkey2, "");
593     RegDeleteKey(hkey1, "");
594
595     /*  beginning backslash character */
596     ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
597     if (!(GetVersion() & 0x80000000))
598         ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %d\n", ret);
599     else {
600         ok(!ret, "RegCreateKeyExA failed with error %d\n", ret);
601         RegDeleteKey(hkey1, NULL);
602     }
603 }
604
605 static void test_reg_close_key(void)
606 {
607     DWORD ret = 0;
608     HKEY hkHandle;
609
610     /* successfully close key
611      * hkHandle remains changed after call to RegCloseKey
612      */
613     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
614     ret = RegCloseKey(hkHandle);
615     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
616
617     /* try to close the key twice */
618     ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
619     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
620        "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %d\n", ret);
621     
622     /* try to close a NULL handle */
623     ret = RegCloseKey(NULL);
624     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
625        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %d\n", ret);
626 }
627
628 static void test_reg_delete_key(void)
629 {
630     DWORD ret;
631
632     ret = RegDeleteKey(hkey_main, NULL);
633     ok(ret == ERROR_INVALID_PARAMETER ||
634        ret == ERROR_ACCESS_DENIED ||
635        ret == ERROR_BADKEY, /* Win95 */
636        "ret=%d\n", ret);
637 }
638
639 static void test_reg_save_key(void)
640 {
641     DWORD ret;
642
643     ret = RegSaveKey(hkey_main, "saved_key", NULL);
644     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
645 }
646
647 static void test_reg_load_key(void)
648 {
649     DWORD ret;
650     HKEY hkHandle;
651
652     ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
653     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
654
655     ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
656     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
657
658     RegCloseKey(hkHandle);
659 }
660
661 static void test_reg_unload_key(void)
662 {
663     DWORD ret;
664
665     ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
666     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", ret);
667
668     DeleteFile("saved_key");
669     DeleteFile("saved_key.LOG");
670 }
671
672 static BOOL set_privileges(LPCSTR privilege, BOOL set)
673 {
674     TOKEN_PRIVILEGES tp;
675     HANDLE hToken;
676     LUID luid;
677
678     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
679         return FALSE;
680
681     if(!LookupPrivilegeValue(NULL, privilege, &luid))
682     {
683         CloseHandle(hToken);
684         return FALSE;
685     }
686
687     tp.PrivilegeCount = 1;
688     tp.Privileges[0].Luid = luid;
689     
690     if (set)
691         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
692     else
693         tp.Privileges[0].Attributes = 0;
694
695     AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
696     if (GetLastError() != ERROR_SUCCESS)
697     {
698         CloseHandle(hToken);
699         return FALSE;
700     }
701
702     CloseHandle(hToken);
703     return TRUE;
704 }
705
706 /* tests that show that RegConnectRegistry and 
707    OpenSCManager accept computer names without the
708    \\ prefix (what MSDN says).   */
709 static void test_regconnectregistry( void)
710 {
711     CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
712     CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
713     DWORD len = sizeof(compName) ;
714     BOOL ret;
715     LONG retl;
716     HKEY hkey;
717     SC_HANDLE schnd;
718     DWORD GLE;
719
720     ret = GetComputerNameA(compName, &len);
721     ok( ret, "GetComputerName failed err = %d\n", GetLastError());
722     if( !ret) return;
723
724     lstrcpyA(netwName, "\\\\");
725     lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
726
727     retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
728     ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %d\n", retl);
729     if( !retl) RegCloseKey( hkey);
730
731     retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
732     ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %d\n", retl);
733     if( !retl) RegCloseKey( hkey);
734
735     schnd = OpenSCManagerA( compName, NULL, GENERIC_READ); 
736     GLE = GetLastError();
737     ok( schnd != NULL || GLE==ERROR_CALL_NOT_IMPLEMENTED, 
738         "OpenSCManagerA failed err = %d\n", GLE);
739     CloseServiceHandle( schnd);
740
741     schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ); 
742     GLE = GetLastError();
743     ok( schnd != NULL || GLE==ERROR_CALL_NOT_IMPLEMENTED, 
744         "OpenSCManagerA failed err = %d\n", GLE);
745     CloseServiceHandle( schnd);
746
747 }
748
749 START_TEST(registry)
750 {
751     setup_main_key();
752     test_set_value();
753     create_test_entries();
754     test_enum_value();
755     test_query_value_ex();
756     test_get_value();
757     test_reg_open_key();
758     test_reg_create_key();
759     test_reg_close_key();
760     test_reg_delete_key();
761
762     /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
763     if (set_privileges(SE_BACKUP_NAME, TRUE) &&
764         set_privileges(SE_RESTORE_NAME, TRUE))
765     {
766         test_reg_save_key();
767         test_reg_load_key();
768         test_reg_unload_key();
769
770         set_privileges(SE_BACKUP_NAME, FALSE);
771         set_privileges(SE_RESTORE_NAME, FALSE);
772     }
773
774     /* cleanup */
775     delete_key( hkey_main );
776     
777     test_regconnectregistry();
778 }