Move and reshuffle reactos/regtetsts into rostests. 1/2
[reactos.git] / rostests / winetests / advapi32 / 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 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
65 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
66
67 ok(!RegSetValueExA(hkey_main,"Test1",0,REG_EXPAND_SZ, sTestpath1, strlen(sTestpath1)+1),
68 "RegSetValueExA failed\n");
69 ok(!RegSetValueExA(hkey_main,"Test2",0,REG_SZ, sTestpath1, strlen(sTestpath1)+1),
70 "RegSetValueExA failed\n");
71 ok(!RegSetValueExA(hkey_main,"Test3",0,REG_EXPAND_SZ, sTestpath2, strlen(sTestpath2)+1),
72 "RegSetValueExA failed\n");
73 }
74
75 static void test_enum_value(void)
76 {
77 DWORD res;
78 HKEY test_key;
79 char value[20], data[20];
80 WCHAR valueW[20], dataW[20];
81 DWORD val_count, data_count, type;
82 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
83 static const WCHAR testW[] = {'T','e','s','t',0};
84 static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
85
86 /* create the working key for new 'Test' value */
87 res = RegCreateKeyA( hkey_main, "TestKey", &test_key );
88 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res);
89
90 /* check NULL data with zero length */
91 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, NULL, 0 );
92 if (GetVersion() & 0x80000000)
93 ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %ld\n", res );
94 else
95 ok( !res, "RegSetValueExA returned %ld\n", res );
96 res = RegSetValueExA( test_key, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
97 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %ld\n", res );
98 res = RegSetValueExA( test_key, "Test", 0, REG_BINARY, NULL, 0 );
99 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %ld\n", res );
100
101 res = RegSetValueExA( test_key, "Test", 0, REG_SZ, (BYTE *)"foobar", 7 );
102 ok( res == 0, "RegSetValueExA failed error %ld\n", res );
103
104 /* overflow both name and data */
105 val_count = 2;
106 data_count = 2;
107 type = 1234;
108 strcpy( value, "xxxxxxxxxx" );
109 strcpy( data, "xxxxxxxxxx" );
110 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, data, &data_count );
111 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
112 ok( val_count == 2, "val_count set to %ld\n", val_count );
113 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
114 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
115 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
116 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
117
118 /* overflow name */
119 val_count = 3;
120 data_count = 20;
121 type = 1234;
122 strcpy( value, "xxxxxxxxxx" );
123 strcpy( data, "xxxxxxxxxx" );
124 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, data, &data_count );
125 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
126 /* Win9x returns 2 as specified by MSDN but NT returns 3... */
127 ok( val_count == 2 || val_count == 3, "val_count set to %ld\n", val_count );
128 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
129 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
130 #if 0
131 /* v5.1.2600.0 (XP Home) does not touch value or data in this case */
132 ok( !strcmp( value, "Te" ), "value set to '%s' instead of 'Te'\n", value );
133 ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'\n", data );
134 #endif
135
136 /* overflow empty name */
137 val_count = 0;
138 data_count = 20;
139 type = 1234;
140 strcpy( value, "xxxxxxxxxx" );
141 strcpy( data, "xxxxxxxxxx" );
142 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, data, &data_count );
143 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
144 ok( val_count == 0, "val_count set to %ld\n", val_count );
145 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
146 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
147 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
148 #if 0
149 /* v5.1.2600.0 (XP Home) does not touch data in this case */
150 ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'\n", data );
151 #endif
152
153 /* overflow data */
154 val_count = 20;
155 data_count = 2;
156 type = 1234;
157 strcpy( value, "xxxxxxxxxx" );
158 strcpy( data, "xxxxxxxxxx" );
159 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, data, &data_count );
160 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
161 ok( val_count == 20, "val_count set to %ld\n", val_count );
162 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
163 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
164 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
165 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
166
167 /* no overflow */
168 val_count = 20;
169 data_count = 20;
170 type = 1234;
171 strcpy( value, "xxxxxxxxxx" );
172 strcpy( data, "xxxxxxxxxx" );
173 res = RegEnumValueA( test_key, 0, value, &val_count, NULL, &type, data, &data_count );
174 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
175 ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
176 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
177 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
178 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
179 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
180
181 /* Unicode tests */
182
183 SetLastError(0);
184 res = RegSetValueExW( test_key, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
185 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
186 return;
187 ok( res == 0, "RegSetValueExW failed error %ld\n", res );
188
189 /* overflow both name and data */
190 val_count = 2;
191 data_count = 2;
192 type = 1234;
193 memcpy( valueW, xxxW, sizeof(xxxW) );
194 memcpy( dataW, xxxW, sizeof(xxxW) );
195 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
196 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
197 ok( val_count == 2, "val_count set to %ld\n", val_count );
198 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
199 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
200 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
201 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
202
203 /* overflow name */
204 val_count = 3;
205 data_count = 20;
206 type = 1234;
207 memcpy( valueW, xxxW, sizeof(xxxW) );
208 memcpy( dataW, xxxW, sizeof(xxxW) );
209 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
210 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
211 ok( val_count == 3, "val_count set to %ld\n", val_count );
212 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
213 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
214 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
215 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
216
217 /* overflow data */
218 val_count = 20;
219 data_count = 2;
220 type = 1234;
221 memcpy( valueW, xxxW, sizeof(xxxW) );
222 memcpy( dataW, xxxW, sizeof(xxxW) );
223 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
224 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
225 ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
226 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
227 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
228 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
229 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
230
231 /* no overflow */
232 val_count = 20;
233 data_count = 20;
234 type = 1234;
235 memcpy( valueW, xxxW, sizeof(xxxW) );
236 memcpy( dataW, xxxW, sizeof(xxxW) );
237 res = RegEnumValueW( test_key, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
238 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
239 ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
240 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
241 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
242 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
243 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
244 }
245
246 static void test_query_value_ex()
247 {
248 DWORD ret;
249 DWORD size;
250 DWORD type;
251
252 ret = RegQueryValueExA(hkey_main, "Test2", NULL, &type, NULL, &size);
253 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
254 ok(size == strlen(sTestpath1) + 1, "(%ld,%ld)\n", (DWORD)strlen(sTestpath1) + 1, size);
255 ok(type == REG_SZ, "type %ld is not REG_SZ\n", type);
256 }
257
258 static void test_reg_open_key()
259 {
260 DWORD ret = 0;
261 HKEY hkResult = NULL;
262 HKEY hkPreserve = NULL;
263
264 /* successful open */
265 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
266 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
267 ok(hkResult != NULL, "expected hkResult != NULL\n");
268 hkPreserve = hkResult;
269
270 /* open same key twice */
271 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
272 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
273 ok(hkResult != hkPreserve, "epxected hkResult != hkPreserve\n");
274 ok(hkResult != NULL, "hkResult != NULL\n");
275 RegCloseKey(hkResult);
276
277 /* open nonexistent key
278 * check that hkResult is set to NULL
279 */
280 hkResult = hkPreserve;
281 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
282 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
283 ok(hkResult == NULL, "expected hkResult == NULL\n");
284
285 /* open the same nonexistent key again to make sure the key wasn't created */
286 hkResult = hkPreserve;
287 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
288 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
289 ok(hkResult == NULL, "expected hkResult == NULL\n");
290
291 /* send in NULL lpSubKey
292 * check that hkResult receives the value of hKey
293 */
294 hkResult = hkPreserve;
295 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
296 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
297 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
298
299 /* send empty-string in lpSubKey */
300 hkResult = hkPreserve;
301 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
302 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
303 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
304
305 /* send in NULL lpSubKey and NULL hKey
306 * hkResult is set to NULL
307 */
308 hkResult = hkPreserve;
309 ret = RegOpenKeyA(NULL, NULL, &hkResult);
310 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
311 ok(hkResult == NULL, "expected hkResult == NULL\n");
312
313 /* only send NULL hKey
314 * the value of hkResult remains unchanged
315 */
316 hkResult = hkPreserve;
317 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
318 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
319 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
320 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
321 RegCloseKey(hkResult);
322
323 /* send in NULL hkResult */
324 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
325 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %ld\n", ret);
326 }
327
328 static void test_reg_close_key()
329 {
330 DWORD ret = 0;
331 HKEY hkHandle;
332
333 /* successfully close key
334 * hkHandle remains changed after call to RegCloseKey
335 */
336 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
337 ret = RegCloseKey(hkHandle);
338 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
339
340 /* try to close the key twice */
341 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
342 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_SUCCESS,
343 "expected ERROR_INVALID_HANDLE or ERROR_SUCCESS, got %ld\n", ret);
344
345 /* try to close a NULL handle */
346 ret = RegCloseKey(NULL);
347 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
348 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
349 }
350
351 static void test_reg_delete_key()
352 {
353 DWORD ret;
354 HKEY hkResult = NULL;
355
356 ret = RegDeleteKey(hkey_main, NULL);
357 ok(ret == ERROR_INVALID_PARAMETER || ret == ERROR_ACCESS_DENIED,
358 "expected ERROR_INVALID_PARAMETER or ERROR_ACCESS_DENIED, got %ld\n", ret);
359
360 ret = RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Tost", &hkResult);
361
362 ret = RegDeleteValue(hkResult, "noExists");
363 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
364
365 ret = RegDeleteKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Tost");
366
367 ret = RegCloseKey(hkResult);
368
369 }
370
371 static void test_reg_save_key()
372 {
373 DWORD ret;
374
375 ret = RegSaveKey(hkey_main, "saved_key", NULL);
376 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
377 }
378
379 static void test_reg_load_key()
380 {
381 DWORD ret;
382 HKEY hkHandle;
383
384 ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
385 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
386
387 ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
388 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
389
390 RegCloseKey(hkHandle);
391 }
392
393 static void test_reg_unload_key()
394 {
395 DWORD ret;
396
397 ret = RegUnLoadKey(HKEY_LOCAL_MACHINE, "Test");
398 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
399
400 DeleteFile("saved_key");
401 }
402
403 static BOOL set_privileges(LPCSTR privilege, BOOL set)
404 {
405 TOKEN_PRIVILEGES tp;
406 HANDLE hToken;
407 LUID luid;
408
409 if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken))
410 return FALSE;
411
412 if(!LookupPrivilegeValue(NULL, privilege, &luid))
413 {
414 CloseHandle(hToken);
415 return FALSE;
416 }
417
418 tp.PrivilegeCount = 1;
419 tp.Privileges[0].Luid = luid;
420
421 if (set)
422 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
423 else
424 tp.Privileges[0].Attributes = 0;
425
426 AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL);
427 if (GetLastError() != ERROR_SUCCESS)
428 {
429 CloseHandle(hToken);
430 return FALSE;
431 }
432
433 CloseHandle(hToken);
434 return TRUE;
435 }
436
437 START_TEST(registry)
438 {
439 setup_main_key();
440 create_test_entries();
441 test_enum_value();
442 test_query_value_ex();
443 test_reg_open_key();
444 test_reg_close_key();
445 test_reg_delete_key();
446
447 /* SaveKey/LoadKey require the SE_BACKUP_NAME privilege to be set */
448 if (set_privileges(SE_BACKUP_NAME, TRUE) &&
449 set_privileges(SE_RESTORE_NAME, TRUE))
450 {
451 test_reg_save_key();
452 test_reg_load_key();
453 test_reg_unload_key();
454
455 set_privileges(SE_BACKUP_NAME, FALSE);
456 set_privileges(SE_RESTORE_NAME, FALSE);
457 }
458 /* cleanup */
459 delete_key( hkey_main );
460 }