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( !res, "RegSetValueExA returned %ld\n", res );
93 res = RegSetValueExA( hkey_main, "Test", 0, REG_BINARY, NULL, 0 );
94 ok( !res, "RegSetValueExA returned %ld\n", res );
96 res = RegSetValueExA( hkey_main, "Test", 0, REG_SZ, (BYTE *)"foobar", 7 );
97 ok( res == 0, "RegSetValueExA failed error %ld\n", res );
99 /* overflow both name and data */
103 strcpy( value, "xxxxxxxxxx" );
104 strcpy( data, "xxxxxxxxxx" );
105 res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
106 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
107 ok( val_count == 2, "val_count set to %ld\n", val_count );
108 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
109 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
110 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
111 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
117 strcpy( value, "xxxxxxxxxx" );
118 strcpy( data, "xxxxxxxxxx" );
119 res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
120 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
121 /* Win9x returns 2 as specified by MSDN but NT returns 3... */
122 ok( val_count == 2 || val_count == 3, "val_count set to %ld\n", val_count );
123 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
124 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
126 /* v5.1.2600.0 (XP Home) does not touch value or data in this case */
127 ok( !strcmp( value, "Te" ), "value set to '%s' instead of 'Te'\n", value );
128 ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'\n", data );
131 /* overflow empty name */
135 strcpy( value, "xxxxxxxxxx" );
136 strcpy( data, "xxxxxxxxxx" );
137 res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
138 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
139 ok( val_count == 0, "val_count set to %ld\n", val_count );
140 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
141 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
142 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
144 /* v5.1.2600.0 (XP Home) does not touch data in this case */
145 ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'\n", data );
152 strcpy( value, "xxxxxxxxxx" );
153 strcpy( data, "xxxxxxxxxx" );
154 res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
155 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
156 ok( val_count == 20, "val_count set to %ld\n", val_count );
157 ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
158 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
159 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
160 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
166 strcpy( value, "xxxxxxxxxx" );
167 strcpy( data, "xxxxxxxxxx" );
168 res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
169 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
170 ok( val_count == 4, "val_count set to %ld instead of 4\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, "Test" ), "value is '%s' instead of Test\n", value );
174 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
179 res = RegSetValueExW( hkey_main, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
180 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
182 ok( res == 0, "RegSetValueExW failed error %ld\n", res );
184 /* overflow both name and data */
188 memcpy( valueW, xxxW, sizeof(xxxW) );
189 memcpy( dataW, xxxW, sizeof(xxxW) );
190 res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
191 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
192 ok( val_count == 2, "val_count set to %ld\n", val_count );
193 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
194 ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
195 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
196 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
202 memcpy( valueW, xxxW, sizeof(xxxW) );
203 memcpy( dataW, xxxW, sizeof(xxxW) );
204 res = RegEnumValueW( hkey_main, 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 == 3, "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" );
216 memcpy( valueW, xxxW, sizeof(xxxW) );
217 memcpy( dataW, xxxW, sizeof(xxxW) );
218 res = RegEnumValueW( hkey_main, 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 == 4, "val_count set to %ld instead of 4\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, testW, sizeof(testW) ), "value is not 'Test'\n" );
224 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
230 memcpy( valueW, xxxW, sizeof(xxxW) );
231 memcpy( dataW, xxxW, sizeof(xxxW) );
232 res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
233 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, 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, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
242 RegDeleteValueA( hkey_main, "Test" );
245 static void test_query_value_ex()
251 ret = RegQueryValueExA(hkey_main, "Test2", NULL, &type, NULL, &size);
252 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
253 ok(size == strlen(sTestpath1) + 1, "(%ld,%ld)\n", (DWORD)strlen(sTestpath1) + 1, size);
254 ok(type == REG_SZ, "type %ld is not REG_SZ\n", type);
257 static void test_reg_open_key()
260 HKEY hkResult = NULL;
261 HKEY hkPreserve = NULL;
263 /* successful open */
264 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
265 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
266 ok(hkResult != NULL, "expected hkResult != NULL\n");
267 hkPreserve = hkResult;
269 /* open same key twice */
270 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkResult);
271 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
272 ok(hkResult != hkPreserve && hkResult != NULL,
273 "expected hkResult != hkPreserve and hkResult != NULL\n");
274 RegCloseKey(hkResult);
276 /* open nonexistent key
277 * check that hkResult is set to NULL
279 hkResult = hkPreserve;
280 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
281 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
282 ok(hkResult == NULL, "expected hkResult == NULL\n");
284 /* open the same nonexistent key again to make sure the key wasn't created */
285 hkResult = hkPreserve;
286 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Nonexistent", &hkResult);
287 ok(ret == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %ld\n", ret);
288 ok(hkResult == NULL, "expected hkResult == NULL\n");
290 /* send in NULL lpSubKey
291 * check that hkResult receives the value of hKey
293 hkResult = hkPreserve;
294 ret = RegOpenKeyA(HKEY_CURRENT_USER, NULL, &hkResult);
295 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
296 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
298 /* send empty-string in lpSubKey */
299 hkResult = hkPreserve;
300 ret = RegOpenKeyA(HKEY_CURRENT_USER, "", &hkResult);
301 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
302 ok(hkResult == HKEY_CURRENT_USER, "expected hkResult == HKEY_CURRENT_USER\n");
304 /* send in NULL lpSubKey and NULL hKey
305 * hkResult is set to NULL
307 hkResult = hkPreserve;
308 ret = RegOpenKeyA(NULL, NULL, &hkResult);
309 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
310 ok(hkResult == NULL, "expected hkResult == NULL\n");
312 /* only send NULL hKey
313 * the value of hkResult remains unchanged
315 hkResult = hkPreserve;
316 ret = RegOpenKeyA(NULL, "Software\\Wine\\Test", &hkResult);
317 ok(ret == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %ld\n", ret);
318 ok(hkResult == hkPreserve, "expected hkResult == hkPreserve\n");
319 RegCloseKey(hkResult);
321 /* send in NULL hkResult */
322 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", NULL);
323 ok(ret == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %ld\n", ret);
326 static void test_reg_close_key()
331 /* successfully close key
332 * hkHandle remains changed after call to RegCloseKey
334 ret = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkHandle);
335 ret = RegCloseKey(hkHandle);
336 ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
338 /* try to close the key twice */
339 ret = RegCloseKey(hkHandle);
340 ok(ret == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %ld\n", ret);
342 /* try to close a NULL handle */
343 ret = RegCloseKey(NULL);
344 ok(ret == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %ld\n", ret);
350 create_test_entries();
352 test_query_value_ex();
354 test_reg_close_key();
357 delete_key( hkey_main );