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
23 #include "wine/test.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 )
40 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
43 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
45 ret = delete_key( tmp );
50 if (ret != ERROR_NO_MORE_ITEMS) return ret;
51 RegDeleteKeyA( hkey, NULL );
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 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
65 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
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");
75 static void test_enum_value(void)
78 char value[20], data[20];
79 WCHAR valueW[20], dataW[20];
80 DWORD val_count, data_count, type;
81 static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
82 static const WCHAR testW[] = {'T','e','s','t',0};
83 static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
85 /* check NULL data with zero length */
86 res = RegSetValueExA( hkey_main, "Test", 0, REG_SZ, NULL, 0 );
87 if (GetVersion() & 0x80000000)
88 ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %ld\n", res );
90 ok( !res, "RegSetValueExA returned %ld\n", res );
91 res = RegSetValueExA( hkey_main, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
92 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %ld\n", res );
93 if (ERROR_INVALID_PARAMETER == res)
94 trace ("RegSetValueExA() returned ERROR_INVALID_PARAMETER\n");
95 res = RegSetValueExA( hkey_main, "Test", 0, REG_BINARY, NULL, 0 );
96 ok( ERROR_SUCCESS == res || ERROR_INVALID_PARAMETER == res, "RegSetValueExA returned %ld\n", res );
97 if (ERROR_INVALID_PARAMETER == res)
98 trace ("RegSetValueExA() returned ERROR_INVALID_PARAMETER\n");
100 res = RegSetValueExA( hkey_main, "Test", 0, REG_SZ, (BYTE *)"foobar", 7 );
101 ok( res == 0, "RegSetValueExA failed error %ld\n", res );
103 /* overflow both name and data */
107 strcpy( value, "xxxxxxxxxx" );
108 strcpy( data, "xxxxxxxxxx" );
109 res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
110 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
111 ok( val_count == 2, "val_count set to %ld\n", val_count );
112 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
113 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
114 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
115 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
121 strcpy( value, "xxxxxxxxxx" );
122 strcpy( data, "xxxxxxxxxx" );
123 res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
124 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
125 /* Win9x returns 2 as specified by MSDN but NT returns 3... */
126 ok( val_count == 2 || val_count == 3, "val_count set to %ld\n", val_count );
127 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
128 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
130 /* v5.1.2600.0 (XP Home) does not touch value or data in this case */
131 ok( !strcmp( value, "Te" ), "value set to '%s' instead of 'Te'\n", value );
132 ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'\n", data );
135 /* overflow empty name */
139 strcpy( value, "xxxxxxxxxx" );
140 strcpy( data, "xxxxxxxxxx" );
141 res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
142 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
143 ok( val_count == 0, "val_count set to %ld\n", val_count );
144 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
145 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
146 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
148 /* v5.1.2600.0 (XP Home) does not touch data in this case */
149 ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'\n", data );
156 strcpy( value, "xxxxxxxxxx" );
157 strcpy( data, "xxxxxxxxxx" );
158 res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
159 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
160 ok( val_count == 20, "val_count set to %ld\n", val_count );
161 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
162 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
163 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
164 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
170 strcpy( value, "xxxxxxxxxx" );
171 strcpy( data, "xxxxxxxxxx" );
172 res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
173 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
174 ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
175 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
176 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
177 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
178 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
183 res = RegSetValueExW( hkey_main, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
184 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
186 ok( res == 0, "RegSetValueExW failed error %ld\n", res );
188 /* overflow both name and data */
192 memcpy( valueW, xxxW, sizeof(xxxW) );
193 memcpy( dataW, xxxW, sizeof(xxxW) );
194 res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
195 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
196 ok( val_count == 2, "val_count set to %ld\n", val_count );
197 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
198 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
199 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
200 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
206 memcpy( valueW, xxxW, sizeof(xxxW) );
207 memcpy( dataW, xxxW, sizeof(xxxW) );
208 res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
209 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
210 ok( val_count == 3, "val_count set to %ld\n", val_count );
211 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
212 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
213 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
214 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
220 memcpy( valueW, xxxW, sizeof(xxxW) );
221 memcpy( dataW, xxxW, sizeof(xxxW) );
222 res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
223 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
224 ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
225 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
226 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
227 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
228 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
234 memcpy( valueW, xxxW, sizeof(xxxW) );
235 memcpy( dataW, xxxW, sizeof(xxxW) );
236 res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
237 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
238 ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
239 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
240 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
241 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
242 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
246 RegDeleteValueA( hkey_main, "Test" );
249 static void test_query_value_ex()
255 ret = RegQueryValueExA(hkey_main, "Test2", NULL, &type, NULL, &size);
256 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
257 ok(size == strlen(sTestpath1) + 1, "(%ld,%ld)\n", (DWORD)strlen(sTestpath1) + 1, size);
258 ok(type == REG_SZ, "type %ld is not REG_SZ\n", type);
261 static void test_reg_open_key()
264 HKEY hkResult = NULL;
265 HKEY hkPreserve = NULL;
267 /* successful open */
268 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
269 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
270 ok(hkResult != NULL, "expected hkResult != NULL\n");
271 hkPreserve = hkResult;
273 /* open same key twice */
274 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
275 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
276 ok(hkResult != hkPreserve && hkResult != NULL,
277 "expected hkResult != hkPreserve and hkResult != NULL\n");
278 RegCloseKey(hkResult);
280 /* open nonexistent key
281 * check that hkResult is set to NULL
283 hkResult = hkPreserve;
284 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
285 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
286 ok(hkResult == NULL, "expected hkResult == NULL\n");
288 /* open the same nonexistent key again to make sure the key wasn't created */
289 hkResult = hkPreserve;
290 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
291 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
292 ok(hkResult == NULL, "expected hkResult == NULL\n");
294 /* send in NULL lpSubKey
295 * check that hkResult receives the value of hKey
297 hkResult = hkPreserve;
298 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
299 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
300 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
302 /* send empty-string in lpSubKey */
303 hkResult = hkPreserve;
304 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
305 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
306 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
308 /* send in NULL lpSubKey and NULL hKey
309 * hkResult is set to NULL
311 hkResult = hkPreserve;
312 ret = RegOpenKeyA(NULL, NULL, &hkResult);
313 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
314 ok(hkResult == NULL, "expected hkResult == NULL\n");
316 /* only send NULL hKey
317 * the value of hkResult remains unchanged
319 hkResult = hkPreserve;
320 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
321 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
322 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
323 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
324 RegCloseKey(hkResult);
326 /* send in NULL hkResult */
327 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
328 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %ld\n", ret);
331 static void test_reg_close_key()
336 /* successfully close key
337 * hkHandle remains changed after call to RegCloseKey
339 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
340 ret = RegCloseKey(hkHandle);
341 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
343 /* try to close the key twice */
344 ret = RegCloseKey(hkHandle); /* Windows 95 doesn't mind. */
345 trace ("closing key twice, got %ld\n", ret);
347 /* try to close a NULL handle */
348 ret = RegCloseKey(NULL);
349 ok(ret == ERROR_INVALID_HANDLE || ret == ERROR_BADKEY, /* Windows 95 returns BADKEY */
350 "expected ERROR_INVALID_HANDLE or ERROR_BADKEY, got %ld\n", ret);
353 static void test_reg_save_key()
357 ret = RegSaveKey(hkey_main, "saved_key", NULL);
358 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
361 static void test_reg_load_key()
366 ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
367 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
369 ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
370 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
372 delete_key(hkHandle);
373 DeleteFile("saved_key");
379 create_test_entries();
381 test_query_value_ex();
383 test_reg_close_key();
388 delete_key( hkey_main );