Handle wParam in WM_PAINT properly: if non-null, it is the hdc we are
[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( 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");
99
100     res = RegSetValueExA( hkey_main, "Test", 0, REG_SZ, (BYTE *)"foobar", 7 );
101     ok( res == 0, "RegSetValueExA failed error %ld\n", res );
102
103     /* overflow both name and data */
104     val_count = 2;
105     data_count = 2;
106     type = 1234;
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 );
116
117     /* overflow name */
118     val_count = 3;
119     data_count = 20;
120     type = 1234;
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 );
129 #if 0
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 );
133 #endif
134
135     /* overflow empty name */
136     val_count = 0;
137     data_count = 20;
138     type = 1234;
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 );
147 #if 0
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 );
150 #endif
151
152     /* overflow data */
153     val_count = 20;
154     data_count = 2;
155     type = 1234;
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 );
165
166     /* no overflow */
167     val_count = 20;
168     data_count = 20;
169     type = 1234;
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 );
179
180     /* Unicode tests */
181
182     SetLastError(0);
183     res = RegSetValueExW( hkey_main, testW, 0, REG_SZ, (const BYTE *)foobarW, 7*sizeof(WCHAR) );
184     if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
185         goto CLEANUP;
186     ok( res == 0, "RegSetValueExW failed error %ld\n", res );
187
188     /* overflow both name and data */
189     val_count = 2;
190     data_count = 2;
191     type = 1234;
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" );
201
202     /* overflow name */
203     val_count = 3;
204     data_count = 20;
205     type = 1234;
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" );
215
216     /* overflow data */
217     val_count = 20;
218     data_count = 2;
219     type = 1234;
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" );
229
230     /* no overflow */
231     val_count = 20;
232     data_count = 20;
233     type = 1234;
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" );
243
244 CLEANUP:
245     /* cleanup */
246     RegDeleteValueA( hkey_main, "Test" );
247 }
248
249 static void test_query_value_ex()
250 {
251     DWORD ret;
252     DWORD size;
253     DWORD type;
254     
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);
259 }
260
261 static void test_reg_open_key()
262 {
263     DWORD ret = 0;
264     HKEY hkResult = NULL;
265     HKEY hkPreserve = NULL;
266
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;
272
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);
279
280     /* open nonexistent key
281      * check that hkResult is set to NULL
282      */
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");
287
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");
293
294     /* send in NULL lpSubKey
295      * check that hkResult receives the value of hKey
296      */
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");
301
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");
307
308     /* send in NULL lpSubKey and NULL hKey
309      * hkResult is set to NULL
310      */
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");
315
316     /* only send NULL hKey
317      * the value of hkResult remains unchanged
318      */
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);
325
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);
329 }
330
331 static void test_reg_close_key()
332 {
333     DWORD ret = 0;
334     HKEY hkHandle;
335
336     /* successfully close key
337      * hkHandle remains changed after call to RegCloseKey
338      */
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);
342
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);
346
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);
351 }
352
353 static void test_reg_save_key()
354 {
355     DWORD ret;
356
357     ret = RegSaveKey(hkey_main, "saved_key", NULL);
358     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
359 }
360
361 static void test_reg_load_key()
362 {
363     DWORD ret;
364     HKEY hkHandle;
365
366     ret = RegLoadKey(HKEY_LOCAL_MACHINE, "Test", "saved_key");
367     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
368
369     ret = RegOpenKey(HKEY_LOCAL_MACHINE, "Test", &hkHandle);
370     ok(ret == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", ret);
371
372     delete_key(hkHandle);
373     DeleteFile("saved_key");
374 }
375
376 START_TEST(registry)
377 {
378     setup_main_key();
379     create_test_entries();
380     test_enum_value();
381     test_query_value_ex();
382     test_reg_open_key();
383     test_reg_close_key();
384     test_reg_save_key();
385     test_reg_load_key();
386
387     /* cleanup */
388     delete_key( hkey_main );
389 }