again dump the .cvsignore
[reactos.git] / reactos / regtests / winetests / shlwapi / shreg.c
1 /* Unit test suite for SHReg* functions
2 *
3 * Copyright 2002 Juergen Schmied
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20 #include <assert.h>
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #include "wine/test.h"
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winerror.h"
28 #include "winreg.h"
29 #include "winuser.h"
30 #include "shlwapi.h"
31
32 /* Keys used for testing */
33 #define REG_TEST_KEY "Software\\Wine\\Test"
34 #define REG_CURRENT_VERSION "Software\\Microsoft\\Windows\\CurrentVersion"
35
36 static HMODULE hshlwapi;
37 typedef DWORD (WINAPI *SHCopyKeyA_func)(HKEY,LPCSTR,HKEY,DWORD);
38 static SHCopyKeyA_func pSHCopyKeyA;
39 typedef DWORD (WINAPI *SHRegGetPathA_func)(HKEY,LPCSTR,LPCSTR,LPSTR,DWORD);
40 static SHRegGetPathA_func pSHRegGetPathA;
41
42 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
43 static const char * sTestpath2 = "%FOO%\\subdir1";
44
45 static const char * sEnvvar1 = "bar";
46 static const char * sEnvvar2 = "ImARatherLongButIndeedNeededString";
47
48 static char sExpTestpath1[MAX_PATH];
49 static char sExpTestpath2[MAX_PATH];
50 static unsigned sExpLen1;
51 static unsigned sExpLen2;
52
53 static const char * sEmptyBuffer ="0123456789";
54
55 /* delete key and all its subkeys */
56 static DWORD delete_key( HKEY hkey, LPSTR parent, LPSTR keyname )
57 {
58 HKEY parentKey;
59 DWORD ret;
60
61 RegCloseKey(hkey);
62
63 /* open the parent of the key to close */
64 ret = RegOpenKeyExA( HKEY_CURRENT_USER, parent, 0, KEY_ALL_ACCESS, &parentKey);
65 if (ret != ERROR_SUCCESS)
66 return ret;
67
68 ret = SHDeleteKeyA( parentKey, keyname );
69 RegCloseKey(parentKey);
70
71 return ret;
72 }
73
74 static HKEY create_test_entries(void)
75 {
76 HKEY hKey;
77 DWORD ret;
78
79 SetEnvironmentVariableA("LONGSYSTEMVAR", sEnvvar1);
80 SetEnvironmentVariableA("FOO", sEnvvar2);
81
82 ret = RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKey);
83 ok( ERROR_SUCCESS == ret, "RegCreateKeyA failed, ret=%lu\n", ret);
84
85 if (hKey)
86 {
87 ok(!RegSetValueExA(hKey,"Test1",0,REG_EXPAND_SZ, (LPBYTE) sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed\n");
88 ok(!RegSetValueExA(hKey,"Test2",0,REG_SZ, (LPBYTE) sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed\n");
89 ok(!RegSetValueExA(hKey,"Test3",0,REG_EXPAND_SZ, (LPBYTE) sTestpath2, strlen(sTestpath2)+1), "RegSetValueExA failed\n");
90 }
91
92 sExpLen1 = ExpandEnvironmentStringsA(sTestpath1, sExpTestpath1, sizeof(sExpTestpath1));
93 sExpLen2 = ExpandEnvironmentStringsA(sTestpath2, sExpTestpath2, sizeof(sExpTestpath2));
94
95 ok(sExpLen1 > 0, "Couldn't expand %s\n", sTestpath1);
96 trace("sExplen1 = (%d)\n", sExpLen1);
97 ok(sExpLen2 > 0, "Couldn't expand %s\n", sTestpath2);
98 trace("sExplen2 = (%d)\n", sExpLen2);
99
100 return hKey;
101 }
102
103 static void test_SHGetValue(void)
104 {
105 DWORD dwSize;
106 DWORD dwType;
107 DWORD dwRet;
108 char buf[MAX_PATH];
109
110 strcpy(buf, sEmptyBuffer);
111 dwSize = MAX_PATH;
112 dwType = -1;
113 dwRet = SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", &dwType, buf, &dwSize);
114 ok( ERROR_SUCCESS == dwRet, "SHGetValueA failed, ret=%lu\n", dwRet);
115 ok( 0 == strcmp(sExpTestpath1, buf), "Comparing of (%s) with (%s) failed\n", buf, sExpTestpath1);
116 ok( REG_SZ == dwType, "Expected REG_SZ, got (%lu)\n", dwType);
117
118 strcpy(buf, sEmptyBuffer);
119 dwSize = MAX_PATH;
120 dwType = -1;
121 dwRet = SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", &dwType, buf, &dwSize);
122 ok( ERROR_SUCCESS == dwRet, "SHGetValueA failed, ret=%lu\n", dwRet);
123 ok( 0 == strcmp(sTestpath1, buf) , "Comparing of (%s) with (%s) failed\n", buf, sTestpath1);
124 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
125 }
126
127 static void test_SHGetRegPath(void)
128 {
129 char buf[MAX_PATH];
130 DWORD dwRet;
131
132 if (!pSHRegGetPathA)
133 return;
134
135 strcpy(buf, sEmptyBuffer);
136 dwRet = (*pSHRegGetPathA)(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", buf, 0);
137 ok( ERROR_SUCCESS == dwRet, "SHRegGetPathA failed, ret=%lu\n", dwRet);
138 ok( 0 == strcmp(sExpTestpath1, buf) , "Comparing (%s) with (%s) failed\n", buf, sExpTestpath1);
139 }
140
141 static void test_SHQUeryValueEx(void)
142 {
143 HKEY hKey;
144 DWORD dwSize;
145 DWORD dwType;
146 char buf[MAX_PATH];
147 DWORD dwRet;
148 const char * sTestedFunction = "";
149 DWORD nUsedBuffer1,nUsedBuffer2;
150
151 sTestedFunction = "RegOpenKeyExA";
152 dwRet = RegOpenKeyExA(HKEY_CURRENT_USER, REG_TEST_KEY, 0, KEY_QUERY_VALUE, &hKey);
153 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
154
155 /****** SHQueryValueExA ******/
156
157 sTestedFunction = "SHQueryValueExA";
158 nUsedBuffer1 = max(strlen(sExpTestpath1)+1, strlen(sTestpath1)+1);
159 nUsedBuffer2 = max(strlen(sExpTestpath2)+1, strlen(sTestpath2)+1);
160 /*
161 * Case 1.1 All arguments are NULL
162 */
163 dwRet = SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, NULL);
164 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
165
166 /*
167 * Case 1.2 dwType is set
168 */
169 dwType = -1;
170 dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, NULL, NULL);
171 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
172 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
173
174 /*
175 * dwSize is set
176 * dwExpanded < dwUnExpanded
177 */
178 dwSize = 6;
179 dwRet = SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, &dwSize);
180 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
181 ok( dwSize == nUsedBuffer1, "Buffer sizes (%lu) and (%lu) are not equal\n", dwSize, nUsedBuffer1);
182
183 /*
184 * dwExpanded > dwUnExpanded
185 */
186 dwSize = 6;
187 dwRet = SHQueryValueExA( hKey, "Test3", NULL, NULL, NULL, &dwSize);
188 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
189 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
190
191 /*
192 * Case 1 string shrinks during expanding
193 */
194 strcpy(buf, sEmptyBuffer);
195 dwSize = 6;
196 dwType = -1;
197 dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, buf, &dwSize);
198 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
199 ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
200 ok( dwSize == nUsedBuffer1, "Buffer sizes (%lu) and (%lu) are not equal\n", dwSize, nUsedBuffer1);
201 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
202
203 /*
204 * string grows during expanding
205 * dwSize is smaller then the size of the unexpanded string
206 */
207 strcpy(buf, sEmptyBuffer);
208 dwSize = 6;
209 dwType = -1;
210 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
211 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
212 ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
213 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
214 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
215
216 /*
217 * string grows during expanding
218 * dwSize is larger then the size of the unexpanded string but smaller than the part before the backslash
219 * if the unexpanded string fits into the buffer it can get cut when expanded
220 */
221 strcpy(buf, sEmptyBuffer);
222 dwSize = strlen(sEnvvar2) - 2;
223 dwType = -1;
224 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
225 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
226
227 todo_wine
228 {
229 ok( (0 == strcmp("", buf)) | (0 == strcmp(sTestpath2, buf)),
230 "Expected empty or unexpanded string (win98), got (%s)\n", buf);
231 }
232
233 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
234 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
235
236 /*
237 * string grows during expanding
238 * dwSize is larger then the size of the part before the backslash but smaller then the expanded string
239 * if the unexpanded string fits into the buffer it can get cut when expanded
240 */
241 strcpy(buf, sEmptyBuffer);
242 dwSize = sExpLen2 - 4;
243 dwType = -1;
244 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
245 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
246
247 todo_wine
248 {
249 ok( (0 == strcmp("", buf)) | (0 == strcmp(sEnvvar2, buf)),
250 "Expected empty or first part of the string \"%s\", got \"%s\"\n", sEnvvar2, buf);
251 }
252
253 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
254 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
255
256 /*
257 * The buffer is NULL but the size is set
258 */
259 strcpy(buf, sEmptyBuffer);
260 dwSize = 6;
261 dwType = -1;
262 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, NULL, &dwSize);
263 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
264 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
265 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
266
267 RegCloseKey(hKey);
268 }
269
270 static void test_SHCopyKey(void)
271 {
272 HKEY hKeySrc, hKeyDst;
273 DWORD dwRet;
274
275 /* Delete existing destination sub keys */
276 hKeyDst = NULL;
277 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) && hKeyDst)
278 {
279 SHDeleteKeyA(hKeyDst, NULL);
280 RegCloseKey(hKeyDst);
281 }
282
283 hKeyDst = NULL;
284 dwRet = RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst);
285 if (dwRet || !hKeyDst)
286 {
287 ok( 0, "Destination couldn't be created, RegCreateKeyA returned (%lu)\n", dwRet);
288 return;
289 }
290
291 hKeySrc = NULL;
292 dwRet = RegOpenKeyA(HKEY_LOCAL_MACHINE, REG_CURRENT_VERSION, &hKeySrc);
293 if (dwRet || !hKeySrc)
294 {
295 ok( 0, "Source couldn't be opened, RegOpenKeyA returned (%lu)\n", dwRet);
296 return;
297 }
298
299
300 if (pSHCopyKeyA)
301 {
302 dwRet = (*pSHCopyKeyA)(hKeySrc, NULL, hKeyDst, 0);
303 ok ( ERROR_SUCCESS == dwRet, "Copy failed, ret=(%lu)\n", dwRet);
304 }
305
306 RegCloseKey(hKeySrc);
307 RegCloseKey(hKeyDst);
308
309 /* Check we copied the sub keys, i.e. something that's on every windows system (including Wine) */
310 hKeyDst = NULL;
311 dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination\\Setup", &hKeyDst);
312 if (dwRet || !hKeyDst)
313 {
314 ok ( 0, "Copy couldn't be opened, RegOpenKeyA returned (%lu)\n", dwRet);
315 return;
316 }
317
318 /* And the we copied the values too */
319 ok(!SHQueryValueExA(hKeyDst, "BootDir", NULL, NULL, NULL, NULL), "SHQueryValueExA failed\n");
320
321 RegCloseKey(hKeyDst);
322 }
323
324 static void test_SHDeleteKey(void)
325 {
326 HKEY hKeyTest, hKeyS;
327 DWORD dwRet;
328 int sysfail = 1;
329
330 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKeyTest))
331 {
332 if (!RegCreateKey(hKeyTest, "ODBC", &hKeyS))
333 {
334 HKEY hKeyO;
335
336 if (!RegCreateKey(hKeyS, "ODBC.INI", &hKeyO))
337 {
338 RegCloseKey (hKeyO);
339
340 if (!RegCreateKey(hKeyS, "ODBCINST.INI", &hKeyO))
341 {
342 RegCloseKey (hKeyO);
343 sysfail = 0;
344 }
345 }
346 RegCloseKey (hKeyS);
347 }
348 RegCloseKey (hKeyTest);
349 }
350
351 if (!sysfail)
352 {
353
354 dwRet = SHDeleteKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC");
355 ok ( ERROR_SUCCESS == dwRet, "SHDeleteKey failed, ret=(%lu)\n", dwRet);
356
357 dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC", &hKeyS);
358 ok ( ERROR_FILE_NOT_FOUND == dwRet, "SHDeleteKey did not delete\n");
359
360 if (dwRet == ERROR_SUCCESS)
361 RegCloseKey (hKeyS);
362 }
363 else
364 ok( 0, "Could not set up SHDeleteKey test\n");
365 }
366
367 START_TEST(shreg)
368 {
369 HKEY hkey = create_test_entries();
370
371 if (!hkey) return;
372
373 hshlwapi = GetModuleHandleA("shlwapi.dll");
374 if (hshlwapi)
375 {
376 pSHCopyKeyA=(SHCopyKeyA_func)GetProcAddress(hshlwapi,"SHCopyKeyA");
377 pSHRegGetPathA=(SHRegGetPathA_func)GetProcAddress(hshlwapi,"SHRegGetPathA");
378 }
379 test_SHGetValue();
380 test_SHQUeryValueEx();
381 test_SHGetRegPath();
382 test_SHCopyKey();
383 test_SHDeleteKey();
384 delete_key( hkey, "Software\\Wine", "Test" );
385 }