EnumThemeColors() and EnumThemeSizes() actually do not return a single
[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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 "winerror.h"
28
29 static HKEY hkey_main;
30
31 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
32 static const char * sTestpath2 = "%FOO%\\subdir1";
33
34 /* delete key and all its subkeys */
35 static DWORD delete_key( HKEY hkey )
36 {
37     char name[MAX_PATH];
38     DWORD ret;
39
40     while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
41     {
42         HKEY tmp;
43         if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
44         {
45             ret = delete_key( tmp );
46             RegCloseKey( tmp );
47         }
48         if (ret) break;
49     }
50     if (ret != ERROR_NO_MORE_ITEMS) return ret;
51     RegDeleteKeyA( hkey, "" );
52     return 0;
53 }
54
55 static void setup_main_key(void)
56 {
57     if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
58
59     assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
60 }
61
62 static void create_test_entries(void)
63 {
64     DWORD qw[2] = { 0x12345678, 0x87654321 };
65
66     SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
67     SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
68
69     ok(!RegSetValueExA(hkey_main,"TP1_EXP_SZ",0,REG_EXPAND_SZ, (LPBYTE)sTestpath1, strlen(sTestpath1)+1), 
70         "RegSetValueExA failed\n");
71     ok(!RegSetValueExA(hkey_main,"TP1_SZ",0,REG_SZ, (LPBYTE)sTestpath1, strlen(sTestpath1)+1), 
72         "RegSetValueExA failed\n");
73     ok(!RegSetValueExA(hkey_main,"TP2_EXP_SZ",0,REG_EXPAND_SZ, (LPBYTE)sTestpath2, strlen(sTestpath2)+1), 
74         "RegSetValueExA failed\n");
75     ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (LPBYTE)qw, 4),
76         "RegSetValueExA failed\n");
77     ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (LPBYTE)qw, 4),
78         "RegSetValueExA failed\n");
79     ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (LPBYTE)qw, 8),
80         "RegSetValueExA failed\n");
81 }
82         
83 static void test_enum_value(void)
84 {
85     DWORD res;
86     HKEY test_key;
87     char value[20], data[20];
88     WCHAR valueW[20], dataW[20];
89     DWORD val_count, data_count, type;
90     static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
91     static const WCHAR testW[] = {'T','e','s','t',0};
92     static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
93
94     /* create the working key for new 'Test' value */
95     res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
96     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res);
97
98     /* check NULL data with zero length */
99     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
100     if (GetVersion() & 0x80000000)
101         ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %ld\n", res );
102     else
103         ok( !res, "RegSetValueExA returned %ld\n", res );
104     res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
105     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %ld\n", res );
106     res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
107     ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %ld\n", res );
108
109     res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (BYTE *)"foobar", 7 );
110     ok( res == 0, "RegSetValueExA failed error %ld\n", res );
111
112     /* overflow both name and data */
113     val_count = 2;
114     data_count = 2;
115     type = 1234;
116     strcpy( value, "xxxxxxxxxx" );
117     strcpy( data, "xxxxxxxxxx" );
118     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
119     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
120     ok( val_count == 2, "val_count set to %ld\n", val_count );
121     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
122     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
123     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
124     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
125
126     /* overflow name */
127     val_count = 3;
128     data_count = 20;
129     type = 1234;
130     strcpy( value, "xxxxxxxxxx" );
131     strcpy( data, "xxxxxxxxxx" );
132     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
133     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
134     /* Win9x returns 2 as specified by MSDN but NT returns 3... */
135     ok( val_count == 2 || val_count == 3, "val_count set to %ld\n", val_count );
136     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
137     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
138 #if 0
139     /* v5.1.2600.0 (XP Home) does not touch value or data in this case */
140     ok( !strcmp( value, "Te" ), "value set to '%s' instead of 'Te'\n", value );
141     ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'\n", data );
142 #endif
143
144     /* overflow empty name */
145     val_count = 0;
146     data_count = 20;
147     type = 1234;
148     strcpy( value, "xxxxxxxxxx" );
149     strcpy( data, "xxxxxxxxxx" );
150     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
151     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
152     ok( val_count == 0, "val_count set to %ld\n", val_count );
153     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
154     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
155     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
156 #if 0
157     /* v5.1.2600.0 (XP Home) does not touch data in this case */
158     ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'\n", data );
159 #endif
160
161     /* overflow data */
162     val_count = 20;
163     data_count = 2;
164     type = 1234;
165     strcpy( value, "xxxxxxxxxx" );
166     strcpy( data, "xxxxxxxxxx" );
167     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
168     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
169     ok( val_count == 20, "val_count set to %ld\n", val_count );
170     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
171     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
172     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
173     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
174
175     /* no overflow */
176     val_count = 20;
177     data_count = 20;
178     type = 1234;
179     strcpy( value, "xxxxxxxxxx" );
180     strcpy( data, "xxxxxxxxxx" );
181     res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, (LPBYTE)data, &data_count );
182     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
183     ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
184     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
185     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
186     ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
187     ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
188
189     /* Unicode tests */
190
191     SetLastError(0);
192     res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
193     if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
194         return;
195     ok( res == 0, "RegSetValueExW failed error %ld\n", res );
196
197     /* overflow both name and data */
198     val_count = 2;
199     data_count = 2;
200     type = 1234;
201     memcpy( valueW, xxxW, sizeof(xxxW) );
202     memcpy( dataW, xxxW, sizeof(xxxW) );
203     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
204     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
205     ok( val_count == 2, "val_count set to %ld\n", val_count );
206     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
207     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
208     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
209     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
210
211     /* overflow name */
212     val_count = 3;
213     data_count = 20;
214     type = 1234;
215     memcpy( valueW, xxxW, sizeof(xxxW) );
216     memcpy( dataW, xxxW, sizeof(xxxW) );
217     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
218     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
219     ok( val_count == 3, "val_count set to %ld\n", val_count );
220     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
221     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
222     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
223     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
224
225     /* overflow data */
226     val_count = 20;
227     data_count = 2;
228     type = 1234;
229     memcpy( valueW, xxxW, sizeof(xxxW) );
230     memcpy( dataW, xxxW, sizeof(xxxW) );
231     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
232     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
233     ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
234     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
235     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
236     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
237     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
238
239     /* no overflow */
240     val_count = 20;
241     data_count = 20;
242     type = 1234;
243     memcpy( valueW, xxxW, sizeof(xxxW) );
244     memcpy( dataW, xxxW, sizeof(xxxW) );
245     res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
246     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
247     ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
248     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
249     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
250     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
251     ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
252 }
253
254 static void test_query_value_ex(void)
255 {
256     DWORD ret;
257     DWORD size;
258     DWORD type;
259     
260     ret = RegQueryValueExA(hkey_main, "TP1_SZ", NULL, &type, NULL, &size);
261     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
262     ok(size == strlen(sTestpath1) + 1, "(%ld,%ld)\n", (DWORD)strlen(sTestpath1) + 1, size);
263     ok(type == REG_SZ, "type %ld is not REG_SZ\n", type);
264 }
265
266 static void test_get_value(void)
267 {
268     HMODULE hadvapi32;
269     DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
270     
271     DWORD ret;
272     DWORD size;
273     DWORD type;
274     DWORD dw, qw[2];
275     CHAR buf[80];
276     CHAR expanded[] = "bar\\subdir1";
277    
278     /* This function was introduced with Windows 2003 SP1 */
279     hadvapi32 = LoadLibraryA("advapi32.dll");
280     if(!hadvapi32) 
281     {
282         ok(0, "error=%ld\n", GetLastError());
283         return;
284     }
285     pRegGetValueA = (PVOID)GetProcAddress(hadvapi32, "RegGetValueA");
286     if(!pRegGetValueA) 
287         return;
288
289     /* Query REG_DWORD using RRF_RT_REG_DWORD (ok) */
290     size = type = dw = 0xdeadbeef;
291     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_DWORD, &type, &dw, &size);
292     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
293     ok(size == 4, "size=%ld\n", size);
294     ok(type == REG_DWORD, "type=%ld\n", type);
295     ok(dw == 0x12345678, "dw=%ld\n", dw);
296
297     /* Query by subkey-name */
298     ret = pRegGetValueA(HKEY_CURRENT_USER, "Software\\Wine\\Test", "DWORD", RRF_RT_REG_DWORD, NULL, NULL, NULL);
299     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
300
301     /* Query REG_DWORD using RRF_RT_REG_BINARY (restricted) */
302     size = type = dw = 0xdeadbeef;
303     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_BINARY, &type, &dw, &size);
304     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
305     /* Although the function failed all values are retrieved */
306     ok(size == 4, "size=%ld\n", size);
307     ok(type == REG_DWORD, "type=%ld\n", type);
308     ok(dw == 0x12345678, "dw=%ld\n", dw);
309
310     /* Test RRF_ZEROONFAILURE */
311     type = dw = 0xdeadbeef; size = 4;
312     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_REG_SZ|RRF_ZEROONFAILURE, &type, &dw, &size);
313     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
314     /* Again all values are retrieved ... */
315     ok(size == 4, "size=%ld\n", size);
316     ok(type == REG_DWORD, "type=%ld\n", type);
317     /* ... except the buffer, which is zeroed out */
318     ok(dw == 0, "dw=%ld\n", dw);
319
320     /* Query REG_DWORD using RRF_RT_DWORD (ok) */
321     size = type = dw = 0xdeadbeef;
322     ret = pRegGetValueA(hkey_main, NULL, "DWORD", RRF_RT_DWORD, &type, &dw, &size);
323     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
324     ok(size == 4, "size=%ld\n", size);
325     ok(type == REG_DWORD, "type=%ld\n", type);
326     ok(dw == 0x12345678, "dw=%ld\n", dw);
327
328     /* Query 32-bit REG_BINARY using RRF_RT_DWORD (ok) */
329     size = type = dw = 0xdeadbeef;
330     ret = pRegGetValueA(hkey_main, NULL, "BIN32", RRF_RT_DWORD, &type, &dw, &size);
331     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
332     ok(size == 4, "size=%ld\n", size);
333     ok(type == REG_BINARY, "type=%ld\n", type);
334     ok(dw == 0x12345678, "dw=%ld\n", dw);
335     
336     /* Query 64-bit REG_BINARY using RRF_RT_DWORD (type mismatch) */
337     qw[0] = qw[1] = size = type = 0xdeadbeef;
338     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_DWORD, &type, qw, &size);
339     ok(ret == ERROR_DATATYPE_MISMATCH, "ret=%ld\n", ret);
340     ok(size == 8, "size=%ld\n", size);
341     ok(type == REG_BINARY, "type=%ld\n", type);
342     ok(qw[0] == 0x12345678 && 
343        qw[1] == 0x87654321, "qw={%ld,%ld}\n", qw[0], qw[1]);
344     
345     /* Query 64-bit REG_BINARY using 32-bit buffer (buffer too small) */
346     type = dw = 0xdeadbeef; size = 4;
347     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_REG_BINARY, &type, &dw, &size);
348     ok(ret == ERROR_MORE_DATA, "ret=%ld\n", ret);
349     ok(dw == 0xdeadbeef, "dw=%ld\n", dw);
350     ok(size == 8, "size=%ld\n", size);
351
352     /* Query 64-bit REG_BINARY using RRF_RT_QWORD (ok) */
353     qw[0] = qw[1] = size = type = 0xdeadbeef;
354     ret = pRegGetValueA(hkey_main, NULL, "BIN64", RRF_RT_QWORD, &type, qw, &size);
355     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
356     ok(size == 8, "size=%ld\n", size);
357     ok(type == REG_BINARY, "type=%ld\n", type);
358     ok(qw[0] == 0x12345678 &&
359        qw[1] == 0x87654321, "qw={%ld,%ld}\n", qw[0], qw[1]);
360
361     /* Query REG_SZ using RRF_RT_REG_SZ (ok) */
362     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
363     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ, &type, buf, &size);
364     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
365     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", strlen(sTestpath1), size);
366     ok(type == REG_SZ, "type=%ld\n", type);
367     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
368
369     /* Query REG_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (ok) */
370     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
371     ret = pRegGetValueA(hkey_main, NULL, "TP1_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, &type, buf, &size);
372     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
373     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", strlen(sTestpath1), size);
374     ok(type == REG_SZ, "type=%ld\n", type);
375     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
376
377     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ (ok, expands) */
378     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
379     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ, &type, buf, &size);
380     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
381 #if 0
382      /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded length + 1 here. */
383      ok(size == strlen(expanded)+1, "strlen(expanded)=%ld size=%ld\n", strlen(expanded), size);
384 #endif
385     ok(type == REG_SZ, "type=%ld\n", type);
386     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
387     
388     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
389     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
390     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
391     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
392     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", strlen(sTestpath1), size);
393     ok(type == REG_EXPAND_SZ, "type=%ld\n", type);
394     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
395     
396     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
397     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
398     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
399
400     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
401     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
402     ok(ret == ERROR_INVALID_PARAMETER, "ret=%ld\n", ret);
403
404
405 static void test_reg_open_key(void)
406 {
407     DWORD ret = 0;
408     HKEY hkResult = NULL;
409     HKEY hkPreserve = NULL;
410
411     /* successful open */
412     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
413     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
414     ok(hkResult != NULL, "expected hkResult != NULL\n");
415     hkPreserve = hkResult;
416
417     /* these tests fail on Win9x, but we want to be compatible with NT, so
418      * run them if we can */
419     if (!(GetVersion() & 0x80000000))
420     {
421         /* open same key twice */
422         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
423         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
424         ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
425         ok(hkResult != NULL, "hkResult != NULL\n");
426         RegCloseKey(hkResult);
427     
428         /* open nonexistent key
429         * check that hkResult is set to NULL
430         */
431         hkResult = hkPreserve;
432         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
433         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
434         ok(hkResult == NULL, "expected hkResult == NULL\n");
435     
436         /* open the same nonexistent key again to make sure the key wasn't created */
437         hkResult = hkPreserve;
438         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
439         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
440         ok(hkResult == NULL, "expected hkResult == NULL\n");
441     
442         /* send in NULL lpSubKey
443         * check that hkResult receives the value of hKey
444         */
445         hkResult = hkPreserve;
446         ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
447         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
448         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
449     
450         /* send empty-string in lpSubKey */
451         hkResult = hkPreserve;
452         ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
453         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
454         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
455     
456         /* send in NULL lpSubKey and NULL hKey
457         * hkResult is set to NULL
458         */
459         hkResult = hkPreserve;
460         ret = RegOpenKeyA(NULL, NULL, &hkResult);
461         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
462         ok(hkResult == NULL, "expected hkResult == NULL\n");
463     }
464
465     /* only send NULL hKey
466      * the value of hkResult remains unchanged
467      */
468     hkResult = hkPreserve;
469     ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
470     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
471        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
472     ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
473     RegCloseKey(hkResult);
474
475     /* send in NULL hkResult */
476     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
477     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %ld\n", ret);
478 }
479
480 static void test_reg_create_key(void)
481 {
482     LONG ret;
483     HKEY hkey1, hkey2;
484     ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
485     ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
486     /* should succeed: all versions of Windows ignore the access rights
487      * to the parent handle */
488     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
489     ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
490
491     /* clean up */
492     RegDeleteKey(hkey2, NULL);
493     RegDeleteKey(hkey1, NULL);
494 }
495
496 static void test_reg_close_key(void)
497 {
498     DWORD ret = 0;
499     HKEY hkHandle;
500
501     /* successfully close key
502      * hkHandle remains changed after call to RegCloseKey
503      */
504     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
505     ret = RegCloseKey(hkHandle);
506     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
507
508     /* try to close the key twice */
509     ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
510     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
511        "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %ld\n", ret);
512     
513     /* try to close a NULL handle */
514     ret = RegCloseKey(NULL);
515     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
516        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
517 }
518
519 static void test_reg_delete_key(void)
520 {
521     DWORD ret;
522
523     ret = RegDeleteKey(hkey_main, NULL);
524     ok(ret == ERROR_INVALID_PARAMETER ||
525        ret == ERROR_ACCESS_DENIED ||
526        ret == ERROR_BADKEY, /* Win95 */
527        "ret=%ld\n", ret);
528 }
529
530 static void test_reg_save_key(void)
531 {
532     DWORD ret;
533
534     ret = RegSaveKey(hkey_main, "saved_key", NULL);
535     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
536 }
537
538 static void test_reg_load_key(void)
539 {
540     DWORD ret;
541     HKEY hkHandle;
542
543     ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
544     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
545
546     ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
547     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
548
549     RegCloseKey(hkHandle);
550 }
551
552 static void test_reg_unload_key(void)
553 {
554     DWORD ret;
555
556     ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
557     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
558
559     DeleteFile("saved_key");
560 }
561
562 static BOOL set_privileges(LPCSTR privilege, BOOL set)
563 {
564     TOKEN_PRIVILEGES tp;
565     HANDLE hToken;
566     LUID luid;
567
568     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
569         return FALSE;
570
571     if(!LookupPrivilegeValue(NULL, privilege, &luid))
572     {
573         CloseHandle(hToken);
574         return FALSE;
575     }
576
577     tp.PrivilegeCount = 1;
578     tp.Privileges[0].Luid = luid;
579     
580     if (set)
581         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
582     else
583         tp.Privileges[0].Attributes = 0;
584
585     AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
586     if (GetLastError() != ERROR_SUCCESS)
587     {
588         CloseHandle(hToken);
589         return FALSE;
590     }
591
592     CloseHandle(hToken);
593     return TRUE;
594 }
595
596 START_TEST(registry)
597 {
598     setup_main_key();
599     create_test_entries();
600     test_enum_value();
601     test_query_value_ex();
602     test_get_value();
603     test_reg_open_key();
604     test_reg_create_key();
605     test_reg_close_key();
606     test_reg_delete_key();
607
608     /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
609     if (set_privileges(SE_BACKUP_NAME, TRUE) &&
610         set_privileges(SE_RESTORE_NAME, TRUE))
611     {
612         test_reg_save_key();
613         test_reg_load_key();
614         test_reg_unload_key();
615
616         set_privileges(SE_BACKUP_NAME, FALSE);
617         set_privileges(SE_RESTORE_NAME, FALSE);
618     }
619
620     /* cleanup */
621     delete_key( hkey_main );
622 }