wined3d: Add the bulk of the GLSL string generation functions.
[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
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     /* v5.1.2600.0 (XP Home and Proffesional) does not touch value or data in this case */
140     ok( !strcmp( value, "Te" ) || !strcmp( value, "xxxxxxxxxx" ), 
141         "value set to '%s' instead of 'Te' or 'xxxxxxxxxx'\n", value );
142     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ), 
143         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
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     /* v5.1.2600.0 (XP Home and Professional) does not touch data in this case */
158     ok( !strcmp( data, "foobar" ) || !strcmp( data, "xxxxxxx" ), 
159         "data set to '%s' instead of 'foobar' or 'xxxxxxx'\n", data );
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     /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded sTestpath1 length + 1 here. */
382     ok((size == strlen(expanded)+1) || (size == strlen(sTestpath1)+1), 
383         "strlen(expanded)=%d, strlen(sTestpath1)=%d, size=%ld\n", strlen(expanded), strlen(sTestpath1), size);
384     ok(type == REG_SZ, "type=%ld\n", type);
385     ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
386     
387     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND (ok, doesn't expand) */
388     buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
389     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
390     ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
391     ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", strlen(sTestpath1), size);
392     ok(type == REG_EXPAND_SZ, "type=%ld\n", type);
393     ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
394     
395     /* Query REG_EXPAND_SZ using RRF_RT_REG_SZ|RRF_NOEXPAND (type mismatch) */
396     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
397     ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
398
399     /* Query REG_EXPAND_SZ using RRF_RT_REG_EXPAND_SZ (not allowed without RRF_NOEXPAND) */
400     ret = pRegGetValueA(hkey_main, NULL, "TP1_EXP_SZ", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
401     ok(ret == ERROR_INVALID_PARAMETER, "ret=%ld\n", ret);
402
403
404 static void test_reg_open_key(void)
405 {
406     DWORD ret = 0;
407     HKEY hkResult = NULL;
408     HKEY hkPreserve = NULL;
409
410     /* successful open */
411     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
412     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
413     ok(hkResult != NULL, "expected hkResult != NULL\n");
414     hkPreserve = hkResult;
415
416     /* these tests fail on Win9x, but we want to be compatible with NT, so
417      * run them if we can */
418     if (!(GetVersion() & 0x80000000))
419     {
420         /* open same key twice */
421         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
422         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
423         ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
424         ok(hkResult != NULL, "hkResult != NULL\n");
425         RegCloseKey(hkResult);
426     
427         /* open nonexistent key
428         * check that hkResult is set to NULL
429         */
430         hkResult = hkPreserve;
431         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
432         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
433         ok(hkResult == NULL, "expected hkResult == NULL\n");
434     
435         /* open the same nonexistent key again to make sure the key wasn't created */
436         hkResult = hkPreserve;
437         ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
438         ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
439         ok(hkResult == NULL, "expected hkResult == NULL\n");
440     
441         /* send in NULL lpSubKey
442         * check that hkResult receives the value of hKey
443         */
444         hkResult = hkPreserve;
445         ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
446         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
447         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
448     
449         /* send empty-string in lpSubKey */
450         hkResult = hkPreserve;
451         ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
452         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
453         ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
454     
455         /* send in NULL lpSubKey and NULL hKey
456         * hkResult is set to NULL
457         */
458         hkResult = hkPreserve;
459         ret = RegOpenKeyA(NULL, NULL, &hkResult);
460         ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
461         ok(hkResult == NULL, "expected hkResult == NULL\n");
462     }
463
464     /* only send NULL hKey
465      * the value of hkResult remains unchanged
466      */
467     hkResult = hkPreserve;
468     ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
469     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
470        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
471     ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
472     RegCloseKey(hkResult);
473
474     /* send in NULL hkResult */
475     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
476     ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %ld\n", ret);
477
478     /*  beginning backslash character */
479     ret = RegOpenKeyA(HKEY_CURRENT_USER, "\\Software\\Wine\\Test", &hkResult);
480        ok(ret == ERROR_BAD_PATHNAME || /* NT/2k/XP */
481            ret == ERROR_FILE_NOT_FOUND /* Win9x,ME */
482            , "expected ERROR_BAD_PATHNAME or ERROR_FILE_NOT_FOUND, got %ld\n", ret);
483 }
484
485 static void test_reg_create_key(void)
486 {
487     LONG ret;
488     HKEY hkey1, hkey2;
489     ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
490     ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
491     /* should succeed: all versions of Windows ignore the access rights
492      * to the parent handle */
493     ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
494     ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
495
496     /* clean up */
497     RegDeleteKey(hkey2, NULL);
498     RegDeleteKey(hkey1, NULL);
499
500     /*  beginning backslash character */
501     ret = RegCreateKeyExA(hkey_main, "\\Subkey3", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
502     if (!(GetVersion() & 0x80000000))
503         ok(ret == ERROR_BAD_PATHNAME, "expected ERROR_BAD_PATHNAME, got %ld\n", ret);
504     else {
505         ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
506         RegDeleteKey(hkey1, NULL);
507     }
508 }
509
510 static void test_reg_close_key(void)
511 {
512     DWORD ret = 0;
513     HKEY hkHandle;
514
515     /* successfully close key
516      * hkHandle remains changed after call to RegCloseKey
517      */
518     ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
519     ret = RegCloseKey(hkHandle);
520     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
521
522     /* try to close the key twice */
523     ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
524     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
525        "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %ld\n", ret);
526     
527     /* try to close a NULL handle */
528     ret = RegCloseKey(NULL);
529     ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
530        "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
531 }
532
533 static void test_reg_delete_key(void)
534 {
535     DWORD ret;
536
537     ret = RegDeleteKey(hkey_main, NULL);
538     ok(ret == ERROR_INVALID_PARAMETER ||
539        ret == ERROR_ACCESS_DENIED ||
540        ret == ERROR_BADKEY, /* Win95 */
541        "ret=%ld\n", ret);
542 }
543
544 static void test_reg_save_key(void)
545 {
546     DWORD ret;
547
548     ret = RegSaveKey(hkey_main, "saved_key", NULL);
549     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
550 }
551
552 static void test_reg_load_key(void)
553 {
554     DWORD ret;
555     HKEY hkHandle;
556
557     ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
558     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
559
560     ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
561     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
562
563     RegCloseKey(hkHandle);
564 }
565
566 static void test_reg_unload_key(void)
567 {
568     DWORD ret;
569
570     ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
571     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
572
573     DeleteFile("saved_key");
574     DeleteFile("saved_key.LOG");
575 }
576
577 static BOOL set_privileges(LPCSTR privilege, BOOL set)
578 {
579     TOKEN_PRIVILEGES tp;
580     HANDLE hToken;
581     LUID luid;
582
583     if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
584         return FALSE;
585
586     if(!LookupPrivilegeValue(NULL, privilege, &luid))
587     {
588         CloseHandle(hToken);
589         return FALSE;
590     }
591
592     tp.PrivilegeCount = 1;
593     tp.Privileges[0].Luid = luid;
594     
595     if (set)
596         tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
597     else
598         tp.Privileges[0].Attributes = 0;
599
600     AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
601     if (GetLastError() != ERROR_SUCCESS)
602     {
603         CloseHandle(hToken);
604         return FALSE;
605     }
606
607     CloseHandle(hToken);
608     return TRUE;
609 }
610
611 /* tests that show that RegConnectRegistry and 
612    OpenSCManager accept computer names without the
613    \\ prefix (what MSDN says).   */
614 static void test_regconnectregistry( void)
615 {
616     CHAR compName[MAX_COMPUTERNAME_LENGTH + 1];
617     CHAR netwName[MAX_COMPUTERNAME_LENGTH + 3]; /* 2 chars for double backslash */
618     DWORD len = sizeof(compName) ;
619     BOOL ret;
620     LONG retl;
621     HKEY hkey;
622     SC_HANDLE schnd;
623     DWORD GLE;
624
625     ret = GetComputerNameA(compName, &len);
626     ok( ret, "GetComputerName failed err = %ld\n", GetLastError());
627     if( !ret) return;
628
629     lstrcpyA(netwName, "\\\\");
630     lstrcpynA(netwName+2, compName, MAX_COMPUTERNAME_LENGTH + 1);
631
632     retl = RegConnectRegistryA( compName, HKEY_LOCAL_MACHINE, &hkey);
633     ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %ld\n", retl);
634     if( !retl) RegCloseKey( hkey);
635
636     retl = RegConnectRegistryA( netwName, HKEY_LOCAL_MACHINE, &hkey);
637     ok( !retl || retl == ERROR_DLL_INIT_FAILED, "RegConnectRegistryA failed err = %ld\n", retl);
638     if( !retl) RegCloseKey( hkey);
639
640     schnd = OpenSCManagerA( compName, NULL, GENERIC_READ); 
641     GLE = GetLastError();
642     ok( schnd != NULL || GLE==ERROR_CALL_NOT_IMPLEMENTED, 
643         "OpenSCManagerA failed err = %ld\n", GLE);
644     CloseServiceHandle( schnd);
645
646     schnd = OpenSCManagerA( netwName, NULL, GENERIC_READ); 
647     GLE = GetLastError();
648     ok( schnd != NULL || GLE==ERROR_CALL_NOT_IMPLEMENTED, 
649         "OpenSCManagerA failed err = %ld\n", GLE);
650     CloseServiceHandle( schnd);
651
652 }
653
654 START_TEST(registry)
655 {
656     setup_main_key();
657     create_test_entries();
658     test_enum_value();
659     test_query_value_ex();
660     test_get_value();
661     test_reg_open_key();
662     test_reg_create_key();
663     test_reg_close_key();
664     test_reg_delete_key();
665
666     /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
667     if (set_privileges(SE_BACKUP_NAME, TRUE) &&
668         set_privileges(SE_RESTORE_NAME, TRUE))
669     {
670         test_reg_save_key();
671         test_reg_load_key();
672         test_reg_unload_key();
673
674         set_privileges(SE_BACKUP_NAME, FALSE);
675         set_privileges(SE_RESTORE_NAME, FALSE);
676     }
677
678     /* cleanup */
679     delete_key( hkey_main );
680     
681     test_regconnectregistry();
682 }