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