Fix subclassing to support nested messages.
[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 /* delete key and all its subkeys */
32 static DWORD delete_key( HKEY hkey )
33 {
34     char name[MAX_PATH];
35     DWORD ret;
36
37     while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
38     {
39         HKEY tmp;
40         if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
41         {
42             ret = delete_key( tmp );
43             RegCloseKey( tmp );
44         }
45         if (ret) break;
46     }
47     if (ret != ERROR_NO_MORE_ITEMS) return ret;
48     RegDeleteKeyA( hkey, NULL );
49     return 0;
50 }
51
52 static void setup_main_key(void)
53 {
54     if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
55
56     assert (!RegCreateKeyExA( HKEY_CURRENT_USER, "Software\\Wine\\Test", 0, NULL,
57                               REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey_main, NULL ));
58 }
59
60 static void test_enum_value(void)
61 {
62     DWORD res;
63     char value[20], data[20];
64     WCHAR valueW[20], dataW[20];
65     DWORD val_count, data_count, type;
66     static const WCHAR foobarW[] = {'f','o','o','b','a','r',0};
67     static const WCHAR testW[] = {'T','e','s','t',0};
68     static const WCHAR xxxW[] = {'x','x','x','x','x','x','x','x',0};
69
70     res = RegSetValueExA( hkey_main, "Test", 0, REG_SZ, (BYTE *)"foobar", 7 );
71     ok( res == 0, "RegSetValueExA failed error %ld\n", res );
72
73     /* overflow both name and data */
74     val_count = 2;
75     data_count = 2;
76     type = 1234;
77     strcpy( value, "xxxxxxxxxx" );
78     strcpy( data, "xxxxxxxxxx" );
79     res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
80     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
81     ok( val_count == 2, "val_count set to %ld\n", val_count );
82     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
83     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
84     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
85     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
86
87     /* overflow name */
88     val_count = 3;
89     data_count = 20;
90     type = 1234;
91     strcpy( value, "xxxxxxxxxx" );
92     strcpy( data, "xxxxxxxxxx" );
93     res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
94     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
95     /* Win9x returns 2 as specified by MSDN but NT returns 3... */
96     ok( val_count == 2 || val_count == 3, "val_count set to %ld\n", val_count );
97     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
98     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
99 #if 0
100     /* v5.1.2600.0 (XP Home) does not touch value or data in this case */
101     ok( !strcmp( value, "Te" ), "value set to '%s' instead of 'Te'\n", value );
102     ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'\n", data );
103 #endif
104
105     /* overflow empty name */
106     val_count = 0;
107     data_count = 20;
108     type = 1234;
109     strcpy( value, "xxxxxxxxxx" );
110     strcpy( data, "xxxxxxxxxx" );
111     res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
112     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
113     ok( val_count == 0, "val_count set to %ld\n", val_count );
114     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
115     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
116     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
117 #if 0
118     /* v5.1.2600.0 (XP Home) does not touch data in this case */
119     ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'\n", data );
120 #endif
121
122     /* overflow data */
123     val_count = 20;
124     data_count = 2;
125     type = 1234;
126     strcpy( value, "xxxxxxxxxx" );
127     strcpy( data, "xxxxxxxxxx" );
128     res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
129     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
130     ok( val_count == 20, "val_count set to %ld\n", val_count );
131     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
132     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
133     ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'\n", value );
134     ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'\n", data );
135
136     /* no overflow */
137     val_count = 20;
138     data_count = 20;
139     type = 1234;
140     strcpy( value, "xxxxxxxxxx" );
141     strcpy( data, "xxxxxxxxxx" );
142     res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
143     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
144     ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
145     ok( data_count == 7, "data_count set to %ld instead of 7\n", data_count );
146     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
147     ok( !strcmp( value, "Test" ), "value is '%s' instead of Test\n", value );
148     ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar\n", data );
149
150     /* Unicode tests */
151
152     SetLastError(0);
153     res = RegSetValueExW( hkey_main, testW, 0, REG_SZ, (BYTE *)foobarW, 7*sizeof(WCHAR) );
154     if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
155         goto CLEANUP;
156     ok( res == 0, "RegSetValueExW failed error %ld\n", res );
157
158     /* overflow both name and data */
159     val_count = 2;
160     data_count = 2;
161     type = 1234;
162     memcpy( valueW, xxxW, sizeof(xxxW) );
163     memcpy( dataW, xxxW, sizeof(xxxW) );
164     res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
165     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
166     ok( val_count == 2, "val_count set to %ld\n", val_count );
167     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
168     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
169     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
170     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
171
172     /* overflow name */
173     val_count = 3;
174     data_count = 20;
175     type = 1234;
176     memcpy( valueW, xxxW, sizeof(xxxW) );
177     memcpy( dataW, xxxW, sizeof(xxxW) );
178     res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
179     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
180     ok( val_count == 3, "val_count set to %ld\n", val_count );
181     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
182     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
183     ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified\n" );
184     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
185
186     /* overflow data */
187     val_count = 20;
188     data_count = 2;
189     type = 1234;
190     memcpy( valueW, xxxW, sizeof(xxxW) );
191     memcpy( dataW, xxxW, sizeof(xxxW) );
192     res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
193     ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld\n", res );
194     ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
195     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
196     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
197     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
198     ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified\n" );
199
200     /* no overflow */
201     val_count = 20;
202     data_count = 20;
203     type = 1234;
204     memcpy( valueW, xxxW, sizeof(xxxW) );
205     memcpy( dataW, xxxW, sizeof(xxxW) );
206     res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
207     ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld\n", res );
208     ok( val_count == 4, "val_count set to %ld instead of 4\n", val_count );
209     ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)\n", data_count );
210     ok( type == REG_SZ, "type %ld is not REG_SZ\n", type );
211     ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'\n" );
212     ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'\n" );
213
214 CLEANUP:
215     /* cleanup */
216     RegDeleteValueA( hkey_main, "Test" );
217 }
218
219 START_TEST(registry)
220 {
221     setup_main_key();
222     test_enum_value();
223
224     /* cleanup */
225     delete_key( hkey_main );
226 }