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