Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / advapi32 / tests / registry.c
blob4e49cd940fcdc7b641e259763f613a601c5d8241
1 /*
2 * Unit tests for registry functions
4 * Copyright (c) 2002 Alexandre Julliard
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.
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.
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
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"
29 static HKEY hkey_main;
31 /* delete key and all its subkeys */
32 static DWORD delete_key( HKEY hkey )
34 char name[MAX_PATH];
35 DWORD ret;
37 while (!(ret = RegEnumKeyA(hkey, 0, name, sizeof(name))))
39 HKEY tmp;
40 if (!(ret = RegOpenKeyExA( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
42 ret = delete_key( tmp );
43 RegCloseKey( tmp );
45 if (ret) break;
47 if (ret != ERROR_NO_MORE_ITEMS) return ret;
48 RegDeleteKeyA( hkey, NULL );
49 return 0;
52 static void setup_main_key(void)
54 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Test", &hkey_main )) delete_key( hkey_main );
56 assert (!RegCreateKeyExA( HKEY_CURRENT_USER, "Software\\Wine\\Test", 0, NULL,
57 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey_main, NULL ));
60 static void test_enum_value(void)
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};
70 res = RegSetValueExA( hkey_main, "Test", 0, REG_SZ, (BYTE *)"foobar", 7 );
71 ok( res == 0, "RegSetValueExA failed error %ld", res );
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", res );
81 ok( val_count == 2, "val_count set to %ld", val_count );
82 ok( data_count == 7, "data_count set to %ld instead of 7", data_count );
83 ok( type == REG_SZ, "type %ld is not REG_SZ", type );
84 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'", value );
85 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'", data );
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", 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", val_count );
97 ok( data_count == 7, "data_count set to %ld instead of 7", data_count );
98 ok( type == REG_SZ, "type %ld is not REG_SZ", type );
99 ok( !strcmp( value, "Te" ), "value set to '%s' instead of 'Te'", value );
100 ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'", data );
102 /* overflow empty name */
103 val_count = 0;
104 data_count = 20;
105 type = 1234;
106 strcpy( value, "xxxxxxxxxx" );
107 strcpy( data, "xxxxxxxxxx" );
108 res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
109 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld", res );
110 ok( val_count == 0, "val_count set to %ld", val_count );
111 ok( data_count == 7, "data_count set to %ld instead of 7", data_count );
112 ok( type == REG_SZ, "type %ld is not REG_SZ", type );
113 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'", value );
114 ok( !strcmp( data, "foobar" ), "data set to '%s' instead of 'foobar'", data );
116 /* overflow data */
117 val_count = 20;
118 data_count = 2;
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", res );
124 ok( val_count == 20, "val_count set to %ld", val_count );
125 ok( data_count == 7, "data_count set to %ld instead of 7", data_count );
126 ok( type == REG_SZ, "type %ld is not REG_SZ", type );
127 ok( !strcmp( value, "xxxxxxxxxx" ), "value set to '%s'", value );
128 ok( !strcmp( data, "xxxxxxxxxx" ), "data set to '%s'", data );
130 /* no overflow */
131 val_count = 20;
132 data_count = 20;
133 type = 1234;
134 strcpy( value, "xxxxxxxxxx" );
135 strcpy( data, "xxxxxxxxxx" );
136 res = RegEnumValueA( hkey_main, 0, value, &val_count, NULL, &type, data, &data_count );
137 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld", res );
138 ok( val_count == 4, "val_count set to %ld instead of 4", val_count );
139 ok( data_count == 7, "data_count set to %ld instead of 7", data_count );
140 ok( type == REG_SZ, "type %ld is not REG_SZ", type );
141 ok( !strcmp( value, "Test" ), "value is '%s' instead of Test", value );
142 ok( !strcmp( data, "foobar" ), "data is '%s' instead of foobar", data );
144 /* Unicode tests */
146 SetLastError(0);
147 res = RegSetValueExW( hkey_main, testW, 0, REG_SZ, (BYTE *)foobarW, 7*sizeof(WCHAR) );
148 if (res==0 && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
149 goto CLEANUP;
150 ok( res == 0, "RegSetValueExW failed error %ld", res );
152 /* overflow both name and data */
153 val_count = 2;
154 data_count = 2;
155 type = 1234;
156 memcpy( valueW, xxxW, sizeof(xxxW) );
157 memcpy( dataW, xxxW, sizeof(xxxW) );
158 res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
159 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld", res );
160 ok( val_count == 2, "val_count set to %ld", val_count );
161 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)", data_count );
162 ok( type == REG_SZ, "type %ld is not REG_SZ", type );
163 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified" );
164 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified" );
166 /* overflow name */
167 val_count = 3;
168 data_count = 20;
169 type = 1234;
170 memcpy( valueW, xxxW, sizeof(xxxW) );
171 memcpy( dataW, xxxW, sizeof(xxxW) );
172 res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
173 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld", res );
174 ok( val_count == 3, "val_count set to %ld", val_count );
175 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)", data_count );
176 ok( type == REG_SZ, "type %ld is not REG_SZ", type );
177 ok( !memcmp( valueW, xxxW, sizeof(xxxW) ), "value modified" );
178 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified" );
180 /* overflow data */
181 val_count = 20;
182 data_count = 2;
183 type = 1234;
184 memcpy( valueW, xxxW, sizeof(xxxW) );
185 memcpy( dataW, xxxW, sizeof(xxxW) );
186 res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
187 ok( res == ERROR_MORE_DATA, "expected ERROR_MORE_DATA, got %ld", res );
188 ok( val_count == 4, "val_count set to %ld instead of 4", val_count );
189 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)", data_count );
190 ok( type == REG_SZ, "type %ld is not REG_SZ", type );
191 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'" );
192 ok( !memcmp( dataW, xxxW, sizeof(xxxW) ), "data modified" );
194 /* no overflow */
195 val_count = 20;
196 data_count = 20;
197 type = 1234;
198 memcpy( valueW, xxxW, sizeof(xxxW) );
199 memcpy( dataW, xxxW, sizeof(xxxW) );
200 res = RegEnumValueW( hkey_main, 0, valueW, &val_count, NULL, &type, (BYTE*)dataW, &data_count );
201 ok( res == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %ld", res );
202 ok( val_count == 4, "val_count set to %ld instead of 4", val_count );
203 ok( data_count == 7*sizeof(WCHAR), "data_count set to %ld instead of 7*sizeof(WCHAR)", data_count );
204 ok( type == REG_SZ, "type %ld is not REG_SZ", type );
205 ok( !memcmp( valueW, testW, sizeof(testW) ), "value is not 'Test'" );
206 ok( !memcmp( dataW, foobarW, sizeof(foobarW) ), "data is not 'foobar'" );
208 CLEANUP:
209 /* cleanup */
210 RegDeleteValueA( hkey_main, "Test" );
213 START_TEST(registry)
215 setup_main_key();
216 test_enum_value();
218 /* cleanup */
219 delete_key( hkey_main );