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