Removed W->A from DEFWND_ImmIsUIMessageW.
[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 (!RegCreateKeyExA( HKEY_CURRENT_USER, "Software\\Wine\\Test", 0, NULL,
60                               REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey_main, NULL ));
61 }
62
63 static void create_test_entries(void)
64 {
65     SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
66     SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
67
68     ok(!RegSetValueExA(hkey_main,"Test1",0,REG_EXPAND_SZ, sTestpath1, strlen(sTestpath1)+1), 
69         "RegSetValueExA failed\n");
70     ok(!RegSetValueExA(hkey_main,"Test2",0,REG_SZ, sTestpath1, strlen(sTestpath1)+1), 
71         "RegSetValueExA failed\n");
72     ok(!RegSetValueExA(hkey_main,"Test3",0,REG_EXPAND_SZ, sTestpath2, strlen(sTestpath2)+1), 
73         "RegSetValueExA failed\n");
74 }
75         
76 static void test_enum_value(void)
77 {
78     DWORD res;
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     /* check NULL data with zero length */
87     res = RegSetValueExA( hkey_main, "Test", 0, REG_SZ, NULL, 0 );
88     if (GetVersion() & 0x80000000)
89         ok( res == ERROR_INVALID_PARAMETER, "RegSetValueExA returned %ld\n", res );
90     else
91         ok( !res, "RegSetValueExA returned %ld\n", res );
92     res = RegSetValueExA( hkey_main, "Test", 0, REG_EXPAND_SZ, NULL, 0 );
93     ok( !res, "RegSetValueExA returned %ld\n", res );
94     res = RegSetValueExA( hkey_main, "Test", 0, REG_BINARY, NULL, 0 );
95     ok( !res, "RegSetValueExA returned %ld\n", res );
96
97     res = RegSetValueExA( hkey_main, "Test", 0, REG_SZ, (BYTE *)"foobar", 7 );
98     ok( res == 0, "RegSetValueExA failed error %ld\n", res );
99
100     /* overflow both name and data */
101     val_count = 2;
102     data_count = 2;
103     type = 1234;
104     strcpy( value, "xxxxxxxxxx" );
105     strcpy( data, "xxxxxxxxxx" );
106     res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
107     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
108     ok( val_count == 2, "val_count set to %ld\n", val_count );
109     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
110     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
111     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
112     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
113
114     /* overflow name */
115     val_count = 3;
116     data_count = 20;
117     type = 1234;
118     strcpy( value, "xxxxxxxxxx" );
119     strcpy( data, "xxxxxxxxxx" );
120     res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
121     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
122     /* Win9x returns 2 as specified by MSDN but NT returns 3... */
123     ok( val_count == 2 || val_count == 3, "val_count set to %ld\n", val_count );
124     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
125     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
126 #if 0
127     /* v5.1.2600.0 (XP Home) does not touch value or data in this case */
128     ok( !strcmp( value, "Te" ), "value set to '%s' instead of 'Te'\n", value );
129     ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'\n", data );
130 #endif
131
132     /* overflow empty name */
133     val_count = 0;
134     data_count = 20;
135     type = 1234;
136     strcpy( value, "xxxxxxxxxx" );
137     strcpy( data, "xxxxxxxxxx" );
138     res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
139     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
140     ok( val_count == 0, "val_count set to %ld\n", val_count );
141     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
142     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
143     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
144 #if 0
145     /* v5.1.2600.0 (XP Home) does not touch data in this case */
146     ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'\n", data );
147 #endif
148
149     /* overflow data */
150     val_count = 20;
151     data_count = 2;
152     type = 1234;
153     strcpy( value, "xxxxxxxxxx" );
154     strcpy( data, "xxxxxxxxxx" );
155     res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
156     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
157     ok( val_count == 20, "val_count set to %ld\n", val_count );
158     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
159     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
160     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
161     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
162
163     /* no overflow */
164     val_count = 20;
165     data_count = 20;
166     type = 1234;
167     strcpy( value, "xxxxxxxxxx" );
168     strcpy( data, "xxxxxxxxxx" );
169     res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
170     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
171     ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
172     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
173     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
174     ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
175     ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
176
177     /* Unicode tests */
178
179     SetLastError(0);
180     res = RegSetValueExW( hkey_main, testW, 0, REG_SZ, (BYTE *)foobarW, 7*sizeof(WCHAR) );
181     if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
182         goto CLEANUP;
183     ok( res == 0, "RegSetValueExW failed error %ld\n", res );
184
185     /* overflow both name and data */
186     val_count = 2;
187     data_count = 2;
188     type = 1234;
189     memcpy( valueW, xxxW, sizeof(xxxW) );
190     memcpy( dataW, xxxW, sizeof(xxxW) );
191     res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
192     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
193     ok( val_count == 2, "val_count set to %ld\n", val_count );
194     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
195     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
196     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
197     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
198
199     /* overflow name */
200     val_count = 3;
201     data_count = 20;
202     type = 1234;
203     memcpy( valueW, xxxW, sizeof(xxxW) );
204     memcpy( dataW, xxxW, sizeof(xxxW) );
205     res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
206     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
207     ok( val_count == 3, "val_count set to %ld\n", val_count );
208     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
209     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
210     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
211     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
212
213     /* overflow data */
214     val_count = 20;
215     data_count = 2;
216     type = 1234;
217     memcpy( valueW, xxxW, sizeof(xxxW) );
218     memcpy( dataW, xxxW, sizeof(xxxW) );
219     res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
220     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
221     ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
222     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
223     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
224     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
225     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
226
227     /* no overflow */
228     val_count = 20;
229     data_count = 20;
230     type = 1234;
231     memcpy( valueW, xxxW, sizeof(xxxW) );
232     memcpy( dataW, xxxW, sizeof(xxxW) );
233     res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
234     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
235     ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
236     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
237     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
238     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
239     ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
240
241 CLEANUP:
242     /* cleanup */
243     RegDeleteValueA( hkey_main, "Test" );
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 START_TEST(registry)
259 {
260     setup_main_key();
261     create_test_entries();
262     test_enum_value();
263     test_query_value_ex();
264
265     /* cleanup */
266     delete_key( hkey_main );
267 }