Prevent crash when no URL is specified.
[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., 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, NULL );
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     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};
84
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 );
89     else
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 );
95
96     res = RegSetValueExA( hkey_main, "Test", 0, REG_SZ, (BYTE *)"foobar", 7 );
97     ok( res == 0, "RegSetValueExA failed error %ld\n", res );
98
99     /* overflow both name and data */
100     val_count = 2;
101     data_count = 2;
102     type = 1234;
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 );
112
113     /* overflow name */
114     val_count = 3;
115     data_count = 20;
116     type = 1234;
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 );
125 #if 0
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 );
129 #endif
130
131     /* overflow empty name */
132     val_count = 0;
133     data_count = 20;
134     type = 1234;
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 );
143 #if 0
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 );
146 #endif
147
148     /* overflow data */
149     val_count = 20;
150     data_count = 2;
151     type = 1234;
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 );
161
162     /* no overflow */
163     val_count = 20;
164     data_count = 20;
165     type = 1234;
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 );
175
176     /* Unicode tests */
177
178     SetLastError(0);
179     res = RegSetValueExW( hkey_main, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
180     if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
181         goto CLEANUP;
182     ok( res == 0, "RegSetValueExW failed error %ld\n", res );
183
184     /* overflow both name and data */
185     val_count = 2;
186     data_count = 2;
187     type = 1234;
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" );
197
198     /* overflow name */
199     val_count = 3;
200     data_count = 20;
201     type = 1234;
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" );
211
212     /* overflow data */
213     val_count = 20;
214     data_count = 2;
215     type = 1234;
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" );
225
226     /* no overflow */
227     val_count = 20;
228     data_count = 20;
229     type = 1234;
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" );
239
240 CLEANUP:
241     /* cleanup */
242     RegDeleteValueA( hkey_main, "Test" );
243 }
244
245 static void test_query_value_ex()
246 {
247     DWORD ret;
248     DWORD size;
249     DWORD type;
250     
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);
255 }
256
257 static void test_reg_open_key()
258 {
259     DWORD ret = 0;
260     HKEY hkResult = NULL;
261     HKEY hkPreserve = NULL;
262
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;
268
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);
275
276     /* open nonexistent key
277      * check that hkResult is set to NULL
278      */
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");
283
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");
289
290     /* send in NULL lpSubKey
291      * check that hkResult receives the value of hKey
292      */
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");
297
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");
303
304     /* send in NULL lpSubKey and NULL hKey
305      * hkResult is set to NULL
306      */
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");
311
312     /* only send NULL hKey
313      * the value of hkResult remains unchanged
314      */
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);
320
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);
324 }
325
326 static void test_reg_close_key()
327 {
328     DWORD ret = 0;
329     HKEY hkHandle;
330
331     /* successfully close key
332      * hkHandle remains changed after call to RegCloseKey
333      */
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);
337
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);
341
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);
345 }
346
347 START_TEST(registry)
348 {
349     setup_main_key();
350     create_test_entries();
351     test_enum_value();
352     test_query_value_ex();
353     test_reg_open_key();
354     test_reg_close_key();
355
356     /* cleanup */
357     delete_key( hkey_main );
358 }