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