advapi32: Test RegSetValueEx[AW] for setting some sequential strings as one.
[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: %ld, GLE=%ld\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 %ld\n", type);
77     ok(cbData == full_byte_len || cbData == str_byte_len,
78         "cbData=%ld instead of %ld or %ld\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: %ld, GLE=%ld\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 %ld\n", type);
94     ok(cbData == full_byte_len || cbData == str_byte_len,
95         "cbData=%ld instead of %ld or %ld\n", cbData, full_byte_len, str_byte_len);
96 }
97
98 static void test_set_value(void)
99 {
100     DWORD ret;
101
102     const WCHAR name1W[] =   {'C','l','e','a','n','S','i','n','g','l','e','S','t','r','i','n','g', 0};
103     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     const WCHAR string1W[] = {'T','h','i','s','N','e','v','e','r','B','r','e','a','k','s', 0};
105     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     const char name1A[] =   "CleanSingleString";
108     const char name2A[] =   "SomeIntraZeroedString";
109     const char string1A[] = "ThisNeverBreaks";
110     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, (LPBYTE)string1A, sizeof(string1A));
114     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %ld, GLE=%ld\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, (LPBYTE)string2A, sizeof(string2A));
120     ok(ret == ERROR_SUCCESS, "RegSetValueExA failed: %ld, GLE=%ld\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, (LPBYTE)string1W, sizeof(string1W));
129     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %ld, GLE=%ld\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, (LPBYTE)string2W, sizeof(string2W));
135     ok(ret == ERROR_SUCCESS, "RegSetValueExW failed: %ld, GLE=%ld\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     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, (LPBYTE)sTestpath1, strlen(sTestpath1)+1), 
148         "RegSetValueExA failed\n");
149     ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (LPBYTE)sTestpath1, strlen(sTestpath1)+1), 
150         "RegSetValueExA failed\n");
151     ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (LPBYTE)sTestpath2, strlen(sTestpath2)+1), 
152         "RegSetValueExA failed\n");
153     ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (LPBYTE)qw, 4),
154         "RegSetValueExA failed\n");
155     ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (LPBYTE)qw, 4),
156         "RegSetValueExA failed\n");
157     ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (LPBYTE)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 %ld\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 %ld\n", res );
180     else
181         ok( !res, "RegSetValueExA returned %ld\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 %ld\n", res );
184     res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
185     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %ld\n", res );
186
187     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (BYTE *)"foobar", 7 );
188     ok( res == 0, "RegSetValueExA failed error %ld\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 %ld\n", res );
198     ok( val_count == 2, "val_count set to %ld\n", val_count );
199     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
200     ok( type == REG_SZ, "type %ld 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 %ld\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 %ld\n", val_count );
214     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
215     ok( type == REG_SZ, "type %ld 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 %ld\n", res );
230     ok( val_count == 0, "val_count set to %ld\n", val_count );
231     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
232     ok( type == REG_SZ, "type %ld 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 %ld\n", res );
246     ok( val_count == 20, "val_count set to %ld\n", val_count );
247     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
248     ok( type == REG_SZ, "type %ld 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 %ld\n", res );
260     ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
261     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
262     ok( type == REG_SZ, "type %ld 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 %ld\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 %ld\n", res );
282     ok( val_count == 2, "val_count set to %ld\n", val_count );
283     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
284     ok( type == REG_SZ, "type %ld 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 %ld\n", res );
296     ok( val_count == 3, "val_count set to %ld\n", val_count );
297     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
298     ok( type == REG_SZ, "type %ld 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 %ld\n", res );
310     ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
311     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
312     ok( type == REG_SZ, "type %ld 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 %ld\n", res );
324     ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
325     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
326     ok( type == REG_SZ, "type %ld 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     
337     ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
338     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
339     ok(size == strlen(sTestpath1) + 1, "(%ld,%ld)\n", (DWORD)strlen(sTestpath1) + 1, size);
340     ok(type == REG_SZ, "type %ld is not REG_SZ\n", type);
341 }
342
343 static void test_get_value(void)
344 {
345     HMODULE hadvapi32;
346     DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
347     
348     DWORD ret;
349     DWORD size;
350     DWORD type;
351     DWORD dw, qw[2];
352     CHAR buf[80];
353     CHAR expanded[] = "bar\\subdir1";
354    
355     /* This function was introduced with Windows 2003 SP1 */
356     hadvapi32 = LoadLibraryA("advapi32.dll");
357     if(!hadvapi32) 
358     {
359         ok(0, "error=%ld\n", GetLastError());
360         return;
361     }
362     pRegGetValueA = (PVOID)GetProcAddress(hadvapi32, "RegGetValueA");
363     if(!pRegGetValueA) 
364         return;
365
366     /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
367     size = type = dw = 0xdeadbeef;
368     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
369     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
370     ok(size == 4, "size=%ld\n", size);
371     ok(type == REG_DWORD, "type=%ld\n", type);
372     ok(dw == 0x12345678, "dw=%ld\n", dw);
373
374     /* Query by subkey-name */
375     ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
376     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
377
378     /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
379     size = type = dw = 0xdeadbeef;
380     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
381     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
382     /* Although the function failed all values are retrieved */
383     ok(size == 4, "size=%ld\n", size);
384     ok(type == REG_DWORD, "type=%ld\n", type);
385     ok(dw == 0x12345678, "dw=%ld\n", dw);
386
387     /* Test RRF_ZEROONFAILURE */
388     type = dw = 0xdeadbeef; size = 4;
389     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
390     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
391     /* Again all values are retrieved ... */
392     ok(size == 4, "size=%ld\n", size);
393     ok(type == REG_DWORD, "type=%ld\n", type);
394     /* ... except the buffer, which is zeroed out */
395     ok(dw == 0, "dw=%ld\n", dw);
396
397     /* Query REG_DWORD using RRF_RT_DWORD (ok) */
398     size = type = dw = 0xdeadbeef;
399     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
400     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
401     ok(size == 4, "size=%ld\n", size);
402     ok(type == REG_DWORD, "type=%ld\n", type);
403     ok(dw == 0x12345678, "dw=%ld\n", dw);
404
405     /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
406     size = type = dw = 0xdeadbeef;
407     ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
408     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
409     ok(size == 4, "size=%ld\n", size);
410     ok(type == REG_BINARY, "type=%ld\n", type);
411     ok(dw == 0x12345678, "dw=%ld\n", dw);
412     
413     /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
414     qw[0] = qw[1] = size = type = 0xdeadbeef;
415     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
416     ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%ld\n", ret);
417     ok(size == 8, "size=%ld\n", size);
418     ok(type == REG_BINARY, "type=%ld\n", type);
419     ok(qw[0] == 0x12345678 && 
420        qw[1] == 0x87654321, "qw={%ld,%ld}\n", qw[0], qw[1]);
421     
422     /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
423     type = dw = 0xdeadbeef; size = 4;
424     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
425     ok(ret == ERROR_MORE_DATA, "ret=%ld\n", ret);
426     ok(dw == 0xdeadbeef, "dw=%ld\n", dw);
427     ok(size == 8, "size=%ld\n", size);
428
429     /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
430     qw[0] = qw[1] = size = type = 0xdeadbeef;
431     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
432     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
433     ok(size == 8, "size=%ld\n", size);
434     ok(type == REG_BINARY, "type=%ld\n", type);
435     ok(qw[0] == 0x12345678 &&
436        qw[1] == 0x87654321, "qw={%ld,%ld}\n", qw[0], qw[1]);
437
438     /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
439     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
440     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
441     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
442     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", strlen(sTestpath1), size);
443     ok(type == REG_SZ, "type=%ld\n", type);
444     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
445
446     /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
447     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
448     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
449     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
450     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", strlen(sTestpath1), size);
451     ok(type == REG_SZ, "type=%ld\n", type);
452     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
453
454     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
455     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
456     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
457     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
458     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
459     ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1), 
460         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%ld\n", strlen(expanded), strlen(sTestpath1), size);
461     ok(type == REG_SZ, "type=%ld\n", type);
462     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
463     
464     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
465     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
466     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
467     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
468     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", strlen(sTestpath1), size);
469     ok(type == REG_EXPAND_SZ, "type=%ld\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|RRF_NOEXPAND (type mismatch) */
473     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
474     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
475
476     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
477     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
478     ok(ret == ERROR_INVALID_PARAMETER, "ret=%ld\n", ret);
479
480
481 static void test_reg_open_key(void)
482 {
483     DWORD ret = 0;
484     HKEY hkResult = NULL;
485     HKEY hkPreserve = NULL;
486
487     /* successful open */
488     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
489     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
490     ok(hkResult != NULL, "expected hkResult != NULL\n");
491     hkPreserve = hkResult;
492
493     /* these tests fail on Win9x, but we want to be compatible with NT, so
494      * run them if we can */
495     if (!(GetVersion() & 0x80000000))
496     {
497         /* open same key twice */
498         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
499         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
500         ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
501         ok(hkResult != NULL, "hkResult != NULL\n");
502         RegCloseKey(hkResult);
503     
504         /* open nonexistent key
505         * check that hkResult is set to NULL
506         */
507         hkResult = hkPreserve;
508         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
509         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
510         ok(hkResult == NULL, "expected hkResult == NULL\n");
511     
512         /* open the same nonexistent key again to make sure the key wasn't created */
513         hkResult = hkPreserve;
514         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
515         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
516         ok(hkResult == NULL, "expected hkResult == NULL\n");
517     
518         /* send in NULL lpSubKey
519         * check that hkResult receives the value of hKey
520         */
521         hkResult = hkPreserve;
522         ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
523         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
524         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
525     
526         /* send empty-string in lpSubKey */
527         hkResult = hkPreserve;
528         ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
529         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
530         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
531     
532         /* send in NULL lpSubKey and NULL hKey
533         * hkResult is set to NULL
534         */
535         hkResult = hkPreserve;
536         ret = RegOpenKeyA(NULL, NULL, &hkResult);
537         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
538         ok(hkResult == NULL, "expected hkResult == NULL\n");
539     }
540
541     /* only send NULL hKey
542      * the value of hkResult remains unchanged
543      */
544     hkResult = hkPreserve;
545     ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
546     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
547        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
548     ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
549     RegCloseKey(hkResult);
550
551     /* send in NULL hkResult */
552     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
553     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %ld\n", ret);
554
555     /*  beginning backslash character */
556     ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
557        ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
558            ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
559            , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %ld\n", ret);
560 }
561
562 static void test_reg_create_key(void)
563 {
564     LONG ret;
565     HKEY hkey1, hkey2;
566     ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
567     ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
568     /* should succeed: all versions of Windows ignore the access rights
569      * to the parent handle */
570     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
571     ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
572
573     /* clean up */
574     RegDeleteKey(hkey2, NULL);
575     RegDeleteKey(hkey1, NULL);
576
577     /*  beginning backslash character */
578     ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
579     if (!(GetVersion() & 0x80000000))
580         ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %ld\n", ret);
581     else {
582         ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
583         RegDeleteKey(hkey1, NULL);
584     }
585 }
586
587 static void test_reg_close_key(void)
588 {
589     DWORD ret = 0;
590     HKEY hkHandle;
591
592     /* successfully close key
593      * hkHandle remains changed after call to RegCloseKey
594      */
595     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
596     ret = RegCloseKey(hkHandle);
597     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
598
599     /* try to close the key twice */
600     ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
601     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
602        "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %ld\n", ret);
603     
604     /* try to close a NULL handle */
605     ret = RegCloseKey(NULL);
606     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
607        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
608 }
609
610 static void test_reg_delete_key(void)
611 {
612     DWORD ret;
613
614     ret = RegDeleteKey(hkey_main, NULL);
615     ok(ret == ERROR_INVALID_PARAMETER ||
616        ret == ERROR_ACCESS_DENIED ||
617        ret == ERROR_BADKEY, /* Win95 */
618        "ret=%ld\n", ret);
619 }
620
621 static void test_reg_save_key(void)
622 {
623     DWORD ret;
624
625     ret = RegSaveKey(hkey_main, "saved_key", NULL);
626     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
627 }
628
629 static void test_reg_load_key(void)
630 {
631     DWORD ret;
632     HKEY hkHandle;
633
634     ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
635     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
636
637     ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
638     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
639
640     RegCloseKey(hkHandle);
641 }
642
643 static void test_reg_unload_key(void)
644 {
645     DWORD ret;
646
647     ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
648     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
649
650     DeleteFile("saved_key");
651     DeleteFile("saved_key.LOG");
652 }
653
654 static BOOL set_privileges(LPCSTR privilege, BOOL set)
655 {
656     TOKEN_PRIVILEGES tp;
657     HANDLE hToken;
658     LUID luid;
659
660     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
661         return FALSE;
662
663     if(!LookupPrivilegeValue(NULL, privilege, &luid))
664     {
665         CloseHandle(hToken);
666         return FALSE;
667     }
668
669     tp.PrivilegeCount = 1;
670     tp.Privileges[0].Luid = luid;
671     
672     if (set)
673         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
674     else
675         tp.Privileges[0].Attributes = 0;
676
677     AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
678     if (GetLastError() != ERROR_SUCCESS)
679     {
680         CloseHandle(hToken);
681         return FALSE;
682     }
683
684     CloseHandle(hToken);
685     return TRUE;
686 }
687
688 /* tests that show that RegConnectRegistry and 
689    OpenSCManager accept computer names without the
690    \\ prefix (what MSDN says).   */
691 static void test_regconnectregistry( void)
692 {
693     CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
694     CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
695     DWORD len = sizeof(compName) ;
696     BOOL ret;
697     LONG retl;
698     HKEY hkey;
699     SC_HANDLE schnd;
700     DWORD GLE;
701
702     ret = GetComputerNameA(compName, &len);
703     ok( ret, "GetComputerName failed err = %ld\n", GetLastError());
704     if( !ret) return;
705
706     lstrcpyA(netwName, "\\\\");
707     lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
708
709     retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
710     ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %ld\n", retl);
711     if( !retl) RegCloseKey( hkey);
712
713     retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
714     ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %ld\n", retl);
715     if( !retl) RegCloseKey( hkey);
716
717     schnd = OpenSCManagerA( compName, NULL, GENERIC_READ); 
718     GLE = GetLastError();
719     ok( schnd != NULL || GLE==ERROR_CALL_NOT_IMPLEMENTED, 
720         "OpenSCManagerA failed err = %ld\n", GLE);
721     CloseServiceHandle( schnd);
722
723     schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ); 
724     GLE = GetLastError();
725     ok( schnd != NULL || GLE==ERROR_CALL_NOT_IMPLEMENTED, 
726         "OpenSCManagerA failed err = %ld\n", GLE);
727     CloseServiceHandle( schnd);
728
729 }
730
731 START_TEST(registry)
732 {
733     setup_main_key();
734     test_set_value();
735     create_test_entries();
736     test_enum_value();
737     test_query_value_ex();
738     test_get_value();
739     test_reg_open_key();
740     test_reg_create_key();
741     test_reg_close_key();
742     test_reg_delete_key();
743
744     /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
745     if (set_privileges(SE_BACKUP_NAME, TRUE) &&
746         set_privileges(SE_RESTORE_NAME, TRUE))
747     {
748         test_reg_save_key();
749         test_reg_load_key();
750         test_reg_unload_key();
751
752         set_privileges(SE_BACKUP_NAME, FALSE);
753         set_privileges(SE_RESTORE_NAME, FALSE);
754     }
755
756     /* cleanup */
757     delete_key( hkey_main );
758     
759     test_regconnectregistry();
760 }