Added unit tests for RegGetValue.
[wine/gsoc_dplay.git] / dlls / advapi32 / tests / registry.c
blob0ae4233a2fdfba1d7b7dbed5c4900f787cdc942c
1 /*
2 * Unit tests for registry functions
4 * Copyright (c) 2002 Alexandre Julliard
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.
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.
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
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"
29 static HKEY hkey_main;
31 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
32 static const char * sTestpath2 = "%FOO%\\subdir1";
34 /* delete key and all its subkeys */
35 static DWORD delete_key( HKEY hkey )
37 char name[MAX_PATH];
38 DWORD ret;
40 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
42 HKEY tmp;
43 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
45 ret = delete_key( tmp );
46 RegCloseKey( tmp );
48 if (ret) break;
50 if (ret != ERROR_NO_MORE_ITEMS) return ret;
51 RegDeleteKeyA( hkey, "" );
52 return 0;
55 static void setup_main_key(void)
57 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
59 assert (!RegCreateKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main ));
62 static void create_test_entries(void)
64 DWORD qw[2] = { 0x12345678, 0x87654321 };
66 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
67 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
69 ok(!RegSetValueExA(hkey_main,"Test1",0,REG_EXPAND_SZ, sTestpath1, strlen(sTestpath1)+1),
70 "RegSetValueExA failed\n");
71 ok(!RegSetValueExA(hkey_main,"Test2",0,REG_SZ, sTestpath1, strlen(sTestpath1)+1),
72 "RegSetValueExA failed\n");
73 ok(!RegSetValueExA(hkey_main,"Test3",0,REG_EXPAND_SZ, sTestpath2, strlen(sTestpath2)+1),
74 "RegSetValueExA failed\n");
75 ok(!RegSetValueExA(hkey_main,"DWORD",0,REG_DWORD, (PVOID)qw, 4),
76 "RegSetValueExA failed\n");
77 ok(!RegSetValueExA(hkey_main,"BIN32",0,REG_BINARY, (PVOID)qw, 4),
78 "RegSetValueExA failed\n");
79 ok(!RegSetValueExA(hkey_main,"BIN64",0,REG_BINARY, (PVOID)qw, 8),
80 "RegSetValueExA failed\n");
83 static void test_enum_value(void)
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};
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);
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 );
109 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (BYTE *)"foobar", 7 );
110 ok( res == 0, "RegSetValueExA failed error %ld\n", res );
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, 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 );
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, 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
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, 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
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, 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 );
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, 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 );
189 /* Unicode tests */
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 );
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" );
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" );
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" );
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" );
254 static void test_query_value_ex(void)
256 DWORD ret;
257 DWORD size;
258 DWORD type;
260 ret = RegQueryValueExA(hkey_main, "Test2", 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);
266 static void test_get_value(void)
268 HMODULE hadvapi32;
269 DWORD (WINAPI *pRegGetValueA)(HKEY,LPCSTR,LPCSTR,DWORD,LPDWORD,PVOID,LPDWORD);
271 DWORD ret;
272 DWORD size;
273 DWORD type;
274 DWORD dw, qw[2];
275 CHAR buf[80];
276 CHAR expanded[] = "bar\\subdir1";
278 /* This function was introduced with Windows 2003 SP1 */
279 hadvapi32 = LoadLibraryA("advapi32.dll");
280 if(!hadvapi32)
282 ok(0, "error=%ld\n", GetLastError());
283 return;
285 pRegGetValueA = (PVOID)GetProcAddress(hadvapi32, "RegGetValueA");
286 if(!pRegGetValueA)
287 return;
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);
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);
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);
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);
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);
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);
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]);
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);
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]);
361 /* This silly behaviour is not documented but since expanding REG_EXPAND_SZ
362 * values is one the two major features (with type restrictions being the
363 * other one) of this function and it doesn't work as one might expect we
364 * better test it... */
366 /* RRF_RT_REG_EXPAND_SZ without RRF_NOEXPAND is not allowed */
367 ret = pRegGetValueA(hkey_main, NULL, "Test1", RRF_RT_REG_EXPAND_SZ, NULL, NULL, NULL);
368 ok(ret == ERROR_INVALID_PARAMETER, "ret=%ld\n", ret);
369 /* Instead you have to use RRF_RT_REG_SZ to expand a REG_EXPAND_SZ */
370 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
371 ret = pRegGetValueA(hkey_main, NULL, "Test1", RRF_RT_REG_SZ, &type, buf, &size);
372 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
373 #if 0
374 /* At least v5.2.3790.1830 (2003 SP1) returns the unexpanded length + 1 here. */
375 ok(size == strlen(expanded)+1, "strlen(expanded)=%ld size=%ld\n", strlen(expanded), size);
376 #endif
377 ok(type == REG_SZ, "type=%ld\n", type);
378 ok(!strcmp(expanded, buf), "expanded=\"%s\" buf=\"%s\"\n", expanded, buf);
380 /* Query REG_SZ using RRF_RT_REG_SZ (should not expand it) */
381 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
382 ret = pRegGetValueA(hkey_main, NULL, "Test2", RRF_RT_REG_SZ, &type, buf, &size);
383 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
384 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", strlen(sTestpath1), size);
385 ok(type == REG_SZ, "type=%ld\n", type);
386 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
388 /* With RRF_RT_REG_SZ however, RRF_NOEXPAND is not allowed */
389 ret = pRegGetValueA(hkey_main, NULL, "Test1", RRF_RT_REG_SZ|RRF_NOEXPAND, NULL, NULL, NULL);
390 ok(ret == ERROR_UNSUPPORTED_TYPE, "ret=%ld\n", ret);
391 /* So we use RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND to get the unexpanded REG_EXPAND_SZ */
392 buf[0] = 0; type = 0xdeadbeef; size = sizeof(buf);
393 ret = pRegGetValueA(hkey_main, NULL, "Test1", RRF_RT_REG_EXPAND_SZ|RRF_NOEXPAND, &type, buf, &size);
394 ok(ret == ERROR_SUCCESS, "ret=%ld\n", ret);
395 ok(size == strlen(sTestpath1)+1, "strlen(sTestpath1)=%d size=%ld\n", strlen(sTestpath1), size);
396 ok(type == REG_EXPAND_SZ, "type=%ld\n", type);
397 ok(!strcmp(sTestpath1, buf), "sTestpath=\"%s\" buf=\"%s\"\n", sTestpath1, buf);
400 static void test_reg_open_key(void)
402 DWORD ret = 0;
403 HKEY hkResult = NULL;
404 HKEY hkPreserve = NULL;
406 /* successful open */
407 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
408 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
409 ok(hkResult != NULL, "expected hkResult != NULL\n");
410 hkPreserve = hkResult;
412 /* these tests fail on Win9x, but we want to be compatible with NT, so
413 * run them if we can */
414 if (!(GetVersion() & 0x80000000))
416 /* open same key twice */
417 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
418 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
419 ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
420 ok(hkResult != NULL, "hkResult != NULL\n");
421 RegCloseKey(hkResult);
423 /* open nonexistent key
424 * check that hkResult is set to NULL
426 hkResult = hkPreserve;
427 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
428 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
429 ok(hkResult == NULL, "expected hkResult == NULL\n");
431 /* open the same nonexistent key again to make sure the key wasn't created */
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");
437 /* send in NULL lpSubKey
438 * check that hkResult receives the value of hKey
440 hkResult = hkPreserve;
441 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
442 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
443 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
445 /* send empty-string in lpSubKey */
446 hkResult = hkPreserve;
447 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &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");
451 /* send in NULL lpSubKey and NULL hKey
452 * hkResult is set to NULL
454 hkResult = hkPreserve;
455 ret = RegOpenKeyA(NULL, NULL, &hkResult);
456 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
457 ok(hkResult == NULL, "expected hkResult == NULL\n");
460 /* only send NULL hKey
461 * the value of hkResult remains unchanged
463 hkResult = hkPreserve;
464 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
465 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
466 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
467 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
468 RegCloseKey(hkResult);
470 /* send in NULL hkResult */
471 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
472 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %ld\n", ret);
475 static void test_reg_create_key(void)
477 LONG ret;
478 HKEY hkey1, hkey2;
479 ret = RegCreateKeyExA(hkey_main, "Subkey1", 0, NULL, 0, KEY_NOTIFY, NULL, &hkey1, NULL);
480 ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
481 /* should succeed: all versions of Windows ignore the access rights
482 * to the parent handle */
483 ret = RegCreateKeyExA(hkey1, "Subkey2", 0, NULL, 0, KEY_SET_VALUE, NULL, &hkey2, NULL);
484 ok(!ret, "RegCreateKeyExA failed with error %ld\n", ret);
486 /* clean up */
487 RegDeleteKey(hkey2, NULL);
488 RegDeleteKey(hkey1, NULL);
491 static void test_reg_close_key(void)
493 DWORD ret = 0;
494 HKEY hkHandle;
496 /* successfully close key
497 * hkHandle remains changed after call to RegCloseKey
499 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
500 ret = RegCloseKey(hkHandle);
501 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
503 /* try to close the key twice */
504 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
505 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
506 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %ld\n", ret);
508 /* try to close a NULL handle */
509 ret = RegCloseKey(NULL);
510 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
511 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
514 static void test_reg_delete_key(void)
516 DWORD ret;
518 ret = RegDeleteKey(hkey_main, NULL);
519 ok(ret == ERROR_INVALID_PARAMETER || ret == ERROR_ACCESS_DENIED,
520 "expected ERROR_INVALID_PARAMETER or ERROR_ACCESS_DENIED, got %ld\n", ret);
523 static void test_reg_save_key(void)
525 DWORD ret;
527 ret = RegSaveKey(hkey_main, "saved_key", NULL);
528 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
531 static void test_reg_load_key(void)
533 DWORD ret;
534 HKEY hkHandle;
536 ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
537 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
539 ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
540 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
542 RegCloseKey(hkHandle);
545 static void test_reg_unload_key(void)
547 DWORD ret;
549 ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
550 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
552 DeleteFile("saved_key");
555 static BOOL set_privileges(LPCSTR privilege, BOOL set)
557 TOKEN_PRIVILEGES tp;
558 HANDLE hToken;
559 LUID luid;
561 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
562 return FALSE;
564 if(!LookupPrivilegeValue(NULL, privilege, &luid))
566 CloseHandle(hToken);
567 return FALSE;
570 tp.PrivilegeCount = 1;
571 tp.Privileges[0].Luid = luid;
573 if (set)
574 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
575 else
576 tp.Privileges[0].Attributes = 0;
578 AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
579 if (GetLastError() != ERROR_SUCCESS)
581 CloseHandle(hToken);
582 return FALSE;
585 CloseHandle(hToken);
586 return TRUE;
589 START_TEST(registry)
591 setup_main_key();
592 create_test_entries();
593 test_enum_value();
594 test_query_value_ex();
595 test_get_value();
596 test_reg_open_key();
597 test_reg_create_key();
598 test_reg_close_key();
599 test_reg_delete_key();
601 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
602 if (set_privileges(SE_BACKUP_NAME, TRUE) &&
603 set_privileges(SE_RESTORE_NAME, TRUE))
605 test_reg_save_key();
606 test_reg_load_key();
607 test_reg_unload_key();
609 set_privileges(SE_BACKUP_NAME, FALSE);
610 set_privileges(SE_RESTORE_NAME, FALSE);
613 /* cleanup */
614 delete_key( hkey_main );