Release 1.1.8.
[wine/multimedia.git] / dlls / shlwapi / tests / shreg.c
blob77a47e2240c747edf8ad82545e965d476d6720d8
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 char sTestpath1[] = "%LONGSYSTEMVAR%\\subdir1";
43 static 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, LPCSTR parent, LPCSTR 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=%u\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 = (%d)\n", nExpLen1);
100 if (nExpectedLen1 != nExpLen1)
101 trace( "Expanding %s failed (expected %d) - known bug in NT4\n", sTestpath1, nExpectedLen1 );
103 trace("sExplen2 = (%d)\n", nExpLen2);
104 if (nExpectedLen2 != nExpLen2)
105 trace( "Expanding %s failed (expected %d) - 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=%u\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 (%u)\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=%u\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 (%u)\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=%u\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=%u\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=%u\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=%u\n", sTestedFunction, dwRet);
182 ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\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=%u\n", sTestedFunction, dwRet);
191 ok( dwSize == nUsedBuffer1, "Buffer sizes (%u) and (%u) 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=%u\n", sTestedFunction, dwRet);
199 ok( dwSize >= nUsedBuffer2, "Buffer size (%u) should be >= (%u)\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 (%u)\n", dwRet);
209 ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
210 ok( dwSize == nUsedBuffer1, "Buffer sizes (%u) and (%u) are not equal\n", dwSize, nUsedBuffer1);
211 ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
214 * string grows during expanding
215 * dwSize is smaller than 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 (%u)\n", dwRet);
222 ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
223 ok( dwSize >= nUsedBuffer2, "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
224 ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
227 * string grows during expanding
228 * dwSize is larger than the size of the unexpanded string, but
229 * smaller than the part before the backslash. If the unexpanded
230 * string fits into the buffer, it can get cut when expanded.
232 strcpy(buf, sEmptyBuffer);
233 dwSize = strlen(sEnvvar2) - 2;
234 dwType = -1;
235 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
236 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%u)\n", dwRet);
238 todo_wine
240 ok( (0 == strcmp("", buf)) || (0 == strcmp(sTestpath2, buf)),
241 "Expected empty or unexpanded string (win98), got (%s)\n", buf);
244 ok( dwSize >= nUsedBuffer2, "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
245 ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
248 * string grows during expanding
249 * dwSize is larger than the size of the part before the backslash,
250 * but smaller than the expanded string. If the unexpanded string fits
251 * into the buffer, it can get cut when expanded.
253 strcpy(buf, sEmptyBuffer);
254 dwSize = nExpLen2 - 4;
255 dwType = -1;
256 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
257 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%u)\n", dwRet);
259 todo_wine
261 ok( (0 == strcmp("", buf)) || (0 == strcmp(sEnvvar2, buf)),
262 "Expected empty or first part of the string \"%s\", got \"%s\"\n", sEnvvar2, buf);
265 ok( dwSize >= nUsedBuffer2, "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
266 ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
269 * The buffer is NULL but the size is set
271 strcpy(buf, sEmptyBuffer);
272 dwSize = 6;
273 dwType = -1;
274 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, NULL, &dwSize);
275 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%u\n", sTestedFunction, dwRet);
276 ok( dwSize >= nUsedBuffer2, "Buffer size (%u) should be >= (%u)\n", dwSize, nUsedBuffer2);
277 ok( REG_SZ == dwType , "Expected REG_SZ, got (%u)\n", dwType);
279 RegCloseKey(hKey);
282 static void test_SHCopyKey(void)
284 HKEY hKeySrc, hKeyDst;
285 DWORD dwRet;
287 /* Delete existing destination sub keys */
288 hKeyDst = NULL;
289 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) && hKeyDst)
291 SHDeleteKeyA(hKeyDst, NULL);
292 RegCloseKey(hKeyDst);
295 hKeyDst = NULL;
296 dwRet = RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst);
297 if (dwRet || !hKeyDst)
299 ok( 0, "Destination couldn't be created, RegCreateKeyA returned (%u)\n", dwRet);
300 return;
303 hKeySrc = NULL;
304 dwRet = RegOpenKeyA(HKEY_LOCAL_MACHINE, REG_CURRENT_VERSION, &hKeySrc);
305 if (dwRet || !hKeySrc)
307 ok( 0, "Source couldn't be opened, RegOpenKeyA returned (%u)\n", dwRet);
308 return;
312 if (pSHCopyKeyA)
314 dwRet = (*pSHCopyKeyA)(hKeySrc, NULL, hKeyDst, 0);
315 ok ( ERROR_SUCCESS == dwRet, "Copy failed, ret=(%u)\n", dwRet);
318 RegCloseKey(hKeySrc);
319 RegCloseKey(hKeyDst);
321 /* Check we copied the sub keys, i.e. something that's on every windows system (including Wine) */
322 hKeyDst = NULL;
323 dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination\\Setup", &hKeyDst);
324 if (dwRet || !hKeyDst)
326 ok ( 0, "Copy couldn't be opened, RegOpenKeyA returned (%u)\n", dwRet);
327 return;
330 /* And the we copied the values too */
331 ok(!SHQueryValueExA(hKeyDst, "BootDir", NULL, NULL, NULL, NULL), "SHQueryValueExA failed\n");
333 RegCloseKey(hKeyDst);
336 static void test_SHDeleteKey(void)
338 HKEY hKeyTest, hKeyS;
339 DWORD dwRet;
340 int sysfail = 1;
342 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKeyTest))
344 if (!RegCreateKey(hKeyTest, "ODBC", &hKeyS))
346 HKEY hKeyO;
348 if (!RegCreateKey(hKeyS, "ODBC.INI", &hKeyO))
350 RegCloseKey (hKeyO);
352 if (!RegCreateKey(hKeyS, "ODBCINST.INI", &hKeyO))
354 RegCloseKey (hKeyO);
355 sysfail = 0;
358 RegCloseKey (hKeyS);
360 RegCloseKey (hKeyTest);
363 if (!sysfail)
366 dwRet = SHDeleteKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC");
367 ok ( ERROR_SUCCESS == dwRet, "SHDeleteKey failed, ret=(%u)\n", dwRet);
369 dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC", &hKeyS);
370 ok ( ERROR_FILE_NOT_FOUND == dwRet, "SHDeleteKey did not delete\n");
372 if (dwRet == ERROR_SUCCESS)
373 RegCloseKey (hKeyS);
375 else
376 ok( 0, "Could not set up SHDeleteKey test\n");
379 START_TEST(shreg)
381 HKEY hkey = create_test_entries();
383 if (!hkey) return;
385 hshlwapi = GetModuleHandleA("shlwapi.dll");
386 pSHCopyKeyA=(SHCopyKeyA_func)GetProcAddress(hshlwapi,"SHCopyKeyA");
387 pSHRegGetPathA=(SHRegGetPathA_func)GetProcAddress(hshlwapi,"SHRegGetPathA");
388 test_SHGetValue();
389 test_SHQUeryValueEx();
390 test_SHGetRegPath();
391 test_SHCopyKey();
392 test_SHDeleteKey();
393 delete_key( hkey, "Software\\Wine", "Test" );