Release 20050930.
[wine/gsoc-2012-control.git] / dlls / shlwapi / tests / shreg.c
blob28636e453f6677ca64d24d412807f67648056b12
1 /* Unit test suite for SHReg* functions
3 * Copyright 2002 Juergen Schmied
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.
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.
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
20 #include <assert.h>
21 #include <stdarg.h>
22 #include <stdio.h>
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"
32 /* Keys used for testing */
33 #define REG_TEST_KEY "Software\\Wine\\Test"
34 #define REG_CURRENT_VERSION "Software\\Microsoft\\Windows\\CurrentVersion"
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;
42 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
43 static const char * sTestpath2 = "%FOO%\\subdir1";
45 static const char * sEnvvar1 = "bar";
46 static const char * sEnvvar2 = "ImARatherLongButIndeedNeededString";
48 static char sExpTestpath1[MAX_PATH];
49 static char sExpTestpath2[MAX_PATH];
50 static DWORD nExpLen1;
51 static DWORD nExpLen2;
53 static const char * sEmptyBuffer ="0123456789";
55 /* delete key and all its subkeys */
56 static DWORD delete_key( HKEY hkey, LPSTR parent, LPSTR keyname )
58 HKEY parentKey;
59 DWORD ret;
61 RegCloseKey(hkey);
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;
68 ret = SHDeleteKeyA( parentKey, keyname );
69 RegCloseKey(parentKey);
71 return ret;
74 static HKEY create_test_entries(void)
76 HKEY hKey;
77 DWORD ret;
78 DWORD nExpectedLen1, nExpectedLen2;
80 SetEnvironmentVariableA("LONGSYSTEMVAR", sEnvvar1);
81 SetEnvironmentVariableA("FOO", sEnvvar2);
83 ret = RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKey);
84 ok( ERROR_SUCCESS == ret, "RegCreateKeyA failed, ret=%lu\n", ret);
86 if (hKey)
88 ok(!RegSetValueExA(hKey,"Test1",0,REG_EXPAND_SZ, (LPBYTE) sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed\n");
89 ok(!RegSetValueExA(hKey,"Test2",0,REG_SZ, (LPBYTE) sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed\n");
90 ok(!RegSetValueExA(hKey,"Test3",0,REG_EXPAND_SZ, (LPBYTE) sTestpath2, strlen(sTestpath2)+1), "RegSetValueExA failed\n");
93 nExpLen1 = ExpandEnvironmentStringsA(sTestpath1, sExpTestpath1, sizeof(sExpTestpath1));
94 nExpLen2 = ExpandEnvironmentStringsA(sTestpath2, sExpTestpath2, sizeof(sExpTestpath2));
96 nExpectedLen1 = strlen(sTestpath1) - strlen("%LONGSYSTEMVAR%") + strlen(sEnvvar1) + 1;
97 nExpectedLen2 = strlen(sTestpath2) - strlen("%FOO%") + strlen(sEnvvar2) + 1;
98 /* ExpandEnvironmentStringsA on NT4 returns 2x the correct result */
99 trace("sExplen1 = (%ld)\n", nExpLen1);
100 if (nExpectedLen1 != nExpLen1)
101 trace( "Expanding %s failed (expected %ld) - known bug in NT4\n", sTestpath1, nExpectedLen1 );
103 trace("sExplen2 = (%ld)\n", nExpLen2);
104 if (nExpectedLen2 != nExpLen2)
105 trace( "Expanding %s failed (expected %ld) - known bug in NT4\n", sTestpath2, nExpectedLen2 );
107 /* Make sure we carry on with correct values */
108 nExpLen1 = nExpectedLen1;
109 nExpLen2 = nExpectedLen2;
110 return hKey;
113 static void test_SHGetValue(void)
115 DWORD dwSize;
116 DWORD dwType;
117 DWORD dwRet;
118 char buf[MAX_PATH];
120 strcpy(buf, sEmptyBuffer);
121 dwSize = MAX_PATH;
122 dwType = -1;
123 dwRet = SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", &dwType, buf, &dwSize);
124 ok( ERROR_SUCCESS == dwRet, "SHGetValueA failed, ret=%lu\n", dwRet);
125 ok( 0 == strcmp(sExpTestpath1, buf), "Comparing of (%s) with (%s) failed\n", buf, sExpTestpath1);
126 ok( REG_SZ == dwType, "Expected REG_SZ, got (%lu)\n", dwType);
128 strcpy(buf, sEmptyBuffer);
129 dwSize = MAX_PATH;
130 dwType = -1;
131 dwRet = SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", &dwType, buf, &dwSize);
132 ok( ERROR_SUCCESS == dwRet, "SHGetValueA failed, ret=%lu\n", dwRet);
133 ok( 0 == strcmp(sTestpath1, buf) , "Comparing of (%s) with (%s) failed\n", buf, sTestpath1);
134 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
137 static void test_SHGetRegPath(void)
139 char buf[MAX_PATH];
140 DWORD dwRet;
142 if (!pSHRegGetPathA)
143 return;
145 strcpy(buf, sEmptyBuffer);
146 dwRet = (*pSHRegGetPathA)(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", buf, 0);
147 ok( ERROR_SUCCESS == dwRet, "SHRegGetPathA failed, ret=%lu\n", dwRet);
148 ok( 0 == strcmp(sExpTestpath1, buf) , "Comparing (%s) with (%s) failed\n", buf, sExpTestpath1);
151 static void test_SHQUeryValueEx(void)
153 HKEY hKey;
154 DWORD dwSize;
155 DWORD dwType;
156 char buf[MAX_PATH];
157 DWORD dwRet;
158 const char * sTestedFunction = "";
159 DWORD nUsedBuffer1,nUsedBuffer2;
161 sTestedFunction = "RegOpenKeyExA";
162 dwRet = RegOpenKeyExA(HKEY_CURRENT_USER, REG_TEST_KEY, 0, KEY_QUERY_VALUE, &hKey);
163 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
165 /****** SHQueryValueExA ******/
167 sTestedFunction = "SHQueryValueExA";
168 nUsedBuffer1 = max(strlen(sExpTestpath1)+1, strlen(sTestpath1)+1);
169 nUsedBuffer2 = max(strlen(sExpTestpath2)+1, strlen(sTestpath2)+1);
171 * Case 1.1 All arguments are NULL
173 dwRet = SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, NULL);
174 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
177 * Case 1.2 dwType is set
179 dwType = -1;
180 dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, NULL, NULL);
181 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
182 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
185 * dwSize is set
186 * dwExpanded < dwUnExpanded
188 dwSize = 6;
189 dwRet = SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, &dwSize);
190 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
191 ok( dwSize == nUsedBuffer1, "Buffer sizes (%lu) and (%lu) are not equal\n", dwSize, nUsedBuffer1);
194 * dwExpanded > dwUnExpanded
196 dwSize = 6;
197 dwRet = SHQueryValueExA( hKey, "Test3", NULL, NULL, NULL, &dwSize);
198 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
199 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
202 * Case 1 string shrinks during expanding
204 strcpy(buf, sEmptyBuffer);
205 dwSize = 6;
206 dwType = -1;
207 dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, buf, &dwSize);
208 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
209 ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
210 ok( dwSize == nUsedBuffer1, "Buffer sizes (%lu) and (%lu) are not equal\n", dwSize, nUsedBuffer1);
211 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
214 * string grows during expanding
215 * dwSize is smaller then the size of the unexpanded string
217 strcpy(buf, sEmptyBuffer);
218 dwSize = 6;
219 dwType = -1;
220 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
221 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
222 ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
223 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
224 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
227 * string grows during expanding
228 * dwSize is larger then the size of the unexpanded string but smaller than the part before the backslash
229 * if the unexpanded string fits into the buffer it can get cut when expanded
231 strcpy(buf, sEmptyBuffer);
232 dwSize = strlen(sEnvvar2) - 2;
233 dwType = -1;
234 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
235 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
237 todo_wine
239 ok( (0 == strcmp("", buf)) || (0 == strcmp(sTestpath2, buf)),
240 "Expected empty or unexpanded string (win98), got (%s)\n", buf);
243 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
244 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
247 * string grows during expanding
248 * dwSize is larger then the size of the part before the backslash but smaller then the expanded string
249 * if the unexpanded string fits into the buffer it can get cut when expanded
251 strcpy(buf, sEmptyBuffer);
252 dwSize = nExpLen2 - 4;
253 dwType = -1;
254 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
255 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
257 todo_wine
259 ok( (0 == strcmp("", buf)) || (0 == strcmp(sEnvvar2, buf)),
260 "Expected empty or first part of the string \"%s\", got \"%s\"\n", sEnvvar2, buf);
263 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
264 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
267 * The buffer is NULL but the size is set
269 strcpy(buf, sEmptyBuffer);
270 dwSize = 6;
271 dwType = -1;
272 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, NULL, &dwSize);
273 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
274 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
275 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
277 RegCloseKey(hKey);
280 static void test_SHCopyKey(void)
282 HKEY hKeySrc, hKeyDst;
283 DWORD dwRet;
285 /* Delete existing destination sub keys */
286 hKeyDst = NULL;
287 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) && hKeyDst)
289 SHDeleteKeyA(hKeyDst, NULL);
290 RegCloseKey(hKeyDst);
293 hKeyDst = NULL;
294 dwRet = RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst);
295 if (dwRet || !hKeyDst)
297 ok( 0, "Destination couldn't be created, RegCreateKeyA returned (%lu)\n", dwRet);
298 return;
301 hKeySrc = NULL;
302 dwRet = RegOpenKeyA(HKEY_LOCAL_MACHINE, REG_CURRENT_VERSION, &hKeySrc);
303 if (dwRet || !hKeySrc)
305 ok( 0, "Source couldn't be opened, RegOpenKeyA returned (%lu)\n", dwRet);
306 return;
310 if (pSHCopyKeyA)
312 dwRet = (*pSHCopyKeyA)(hKeySrc, NULL, hKeyDst, 0);
313 ok ( ERROR_SUCCESS == dwRet, "Copy failed, ret=(%lu)\n", dwRet);
316 RegCloseKey(hKeySrc);
317 RegCloseKey(hKeyDst);
319 /* Check we copied the sub keys, i.e. something that's on every windows system (including Wine) */
320 hKeyDst = NULL;
321 dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination\\Setup", &hKeyDst);
322 if (dwRet || !hKeyDst)
324 ok ( 0, "Copy couldn't be opened, RegOpenKeyA returned (%lu)\n", dwRet);
325 return;
328 /* And the we copied the values too */
329 ok(!SHQueryValueExA(hKeyDst, "BootDir", NULL, NULL, NULL, NULL), "SHQueryValueExA failed\n");
331 RegCloseKey(hKeyDst);
334 static void test_SHDeleteKey(void)
336 HKEY hKeyTest, hKeyS;
337 DWORD dwRet;
338 int sysfail = 1;
340 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKeyTest))
342 if (!RegCreateKey(hKeyTest, "ODBC", &hKeyS))
344 HKEY hKeyO;
346 if (!RegCreateKey(hKeyS, "ODBC.INI", &hKeyO))
348 RegCloseKey (hKeyO);
350 if (!RegCreateKey(hKeyS, "ODBCINST.INI", &hKeyO))
352 RegCloseKey (hKeyO);
353 sysfail = 0;
356 RegCloseKey (hKeyS);
358 RegCloseKey (hKeyTest);
361 if (!sysfail)
364 dwRet = SHDeleteKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC");
365 ok ( ERROR_SUCCESS == dwRet, "SHDeleteKey failed, ret=(%lu)\n", dwRet);
367 dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC", &hKeyS);
368 ok ( ERROR_FILE_NOT_FOUND == dwRet, "SHDeleteKey did not delete\n");
370 if (dwRet == ERROR_SUCCESS)
371 RegCloseKey (hKeyS);
373 else
374 ok( 0, "Could not set up SHDeleteKey test\n");
377 START_TEST(shreg)
379 HKEY hkey = create_test_entries();
381 if (!hkey) return;
383 hshlwapi = GetModuleHandleA("shlwapi.dll");
384 if (hshlwapi)
386 pSHCopyKeyA=(SHCopyKeyA_func)GetProcAddress(hshlwapi,"SHCopyKeyA");
387 pSHRegGetPathA=(SHRegGetPathA_func)GetProcAddress(hshlwapi,"SHRegGetPathA");
389 test_SHGetValue();
390 test_SHQUeryValueEx();
391 test_SHGetRegPath();
392 test_SHCopyKey();
393 test_SHDeleteKey();
394 delete_key( hkey, "Software\\Wine", "Test" );