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
25 #include "wine/test.h"
33 /* Keys used for testing */
34 #define REG_TEST_KEY "Software\\Wine\\Test"
35 #define REG_CURRENT_VERSION "Software\\Microsoft\\Windows\\CurrentVersion"
37 static HMODULE hshlwapi;
38 typedef DWORD (WINAPI *SHCopyKeyA_func)(HKEY,LPCSTR,HKEY,DWORD);
39 static SHCopyKeyA_func pSHCopyKeyA;
40 typedef DWORD (WINAPI *SHRegGetPathA_func)(HKEY,LPCSTR,LPCSTR,LPSTR,DWORD);
41 static SHRegGetPathA_func pSHRegGetPathA;
43 static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
44 static const char * sTestpath2 = "%FOO%\\subdir1";
46 static const char * sEnvvar1 = "bar";
47 static const char * sEnvvar2 = "ImARatherLongButIndeedNeededString";
49 static char sExpTestpath1[MAX_PATH];
50 static char sExpTestpath2[MAX_PATH];
51 static unsigned sExpLen1;
52 static unsigned sExpLen2;
54 static const char * sEmptyBuffer ="0123456789";
56 /* delete key and all its subkeys */
57 static DWORD delete_key( HKEY hkey, LPSTR parent, LPSTR keyname )
64 /* open the parent of the key to close */
65 ret = RegOpenKeyExA( HKEY_CURRENT_USER, parent, 0, KEY_ALL_ACCESS, &parentKey);
66 if (ret != ERROR_SUCCESS)
69 ret = SHDeleteKeyA( parentKey, keyname );
70 RegCloseKey(parentKey);
75 static HKEY create_test_entries(void)
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);
88 ok(!RegSetValueExA(hKey,"Test1",0,REG_EXPAND_SZ, sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed\n");
89 ok(!RegSetValueExA(hKey,"Test2",0,REG_SZ, sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed\n");
90 ok(!RegSetValueExA(hKey,"Test3",0,REG_EXPAND_SZ, sTestpath2, strlen(sTestpath2)+1), "RegSetValueExA failed\n");
93 sExpLen1 = ExpandEnvironmentStringsA(sTestpath1, sExpTestpath1, sizeof(sExpTestpath1));
94 sExpLen2 = ExpandEnvironmentStringsA(sTestpath2, sExpTestpath2, sizeof(sExpTestpath2));
96 ok(sExpLen1 > 0, "Couldn't expand %s\n", sTestpath1);
97 trace("sExplen1 = (%d)\n", sExpLen1);
98 ok(sExpLen2 > 0, "Couldn't expand %s\n", sTestpath2);
99 trace("sExplen2 = (%d)\n", sExpLen2);
104 static void test_SHGetValue(void)
111 strcpy(buf, sEmptyBuffer);
114 dwRet = SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", &dwType, buf, &dwSize);
115 ok( ERROR_SUCCESS == dwRet, "SHGetValueA failed, ret=%lu\n", dwRet);
116 ok( 0 == strcmp(sExpTestpath1, buf), "Comparing of (%s) with (%s) failed\n", buf, sExpTestpath1);
117 ok( REG_SZ == dwType, "Expected REG_SZ, got (%lu)\n", dwType);
119 strcpy(buf, sEmptyBuffer);
122 dwRet = SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", &dwType, buf, &dwSize);
123 ok( ERROR_SUCCESS == dwRet, "SHGetValueA failed, ret=%lu\n", dwRet);
124 ok( 0 == strcmp(sTestpath1, buf) , "Comparing of (%s) with (%s) failed\n", buf, sTestpath1);
125 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
128 static void test_SHGetRegPath(void)
136 strcpy(buf, sEmptyBuffer);
137 dwRet = (*pSHRegGetPathA)(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", buf, 0);
138 ok( ERROR_SUCCESS == dwRet, "SHRegGetPathA failed, ret=%lu\n", dwRet);
139 ok( 0 == strcmp(sExpTestpath1, buf) , "Comparing (%s) with (%s) failed\n", buf, sExpTestpath1);
142 static void test_SHQUeryValueEx(void)
149 const char * sTestedFunction = "";
150 DWORD nUsedBuffer1,nUsedBuffer2;
152 sTestedFunction = "RegOpenKeyExA";
153 dwRet = RegOpenKeyExA(HKEY_CURRENT_USER, REG_TEST_KEY, 0, KEY_QUERY_VALUE, &hKey);
154 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
156 /****** SHQueryValueExA ******/
158 sTestedFunction = "SHQueryValueExA";
159 nUsedBuffer1 = max(strlen(sExpTestpath1)+1, strlen(sTestpath1)+1);
160 nUsedBuffer2 = max(strlen(sExpTestpath2)+1, strlen(sTestpath2)+1);
162 * Case 1.1 All arguments are NULL
164 dwRet = SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, NULL);
165 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
168 * Case 1.2 dwType is set
171 dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, NULL, NULL);
172 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
173 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
177 * dwExpanded < dwUnExpanded
180 dwRet = SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, &dwSize);
181 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
182 ok( dwSize == nUsedBuffer1, "Buffer sizes (%lu) and (%lu) are not equal\n", dwSize, nUsedBuffer1);
185 * dwExpanded > dwUnExpanded
188 dwRet = SHQueryValueExA( hKey, "Test3", NULL, NULL, NULL, &dwSize);
189 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
190 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
193 * Case 1 string shrinks during expanding
195 strcpy(buf, sEmptyBuffer);
198 dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, buf, &dwSize);
199 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
200 ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
201 ok( dwSize == nUsedBuffer1, "Buffer sizes (%lu) and (%lu) are not equal\n", dwSize, nUsedBuffer1);
202 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
205 * string grows during expanding
206 * dwSize is smaller then the size of the unexpanded string
208 strcpy(buf, sEmptyBuffer);
211 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
212 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
213 ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
214 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
215 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
218 * string grows during expanding
219 * dwSize is larger then the size of the unexpanded string but smaller than the part before the backslash
220 * if the unexpanded string fits into the buffer it can get cut when expanded
222 strcpy(buf, sEmptyBuffer);
223 dwSize = strlen(sEnvvar2) - 2;
225 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
226 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
230 ok( (0 == strcmp("", buf)) | (0 == strcmp(sTestpath2, buf)),
231 "Expected empty or unexpanded string (win98), got (%s)\n", buf);
234 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
235 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
238 * string grows during expanding
239 * dwSize is larger then the size of the part before the backslash but smaller then the expanded string
240 * if the unexpanded string fits into the buffer it can get cut when expanded
242 strcpy(buf, sEmptyBuffer);
243 dwSize = sExpLen2 - 4;
245 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
246 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
250 ok( (0 == strcmp("", buf)) | (0 == strcmp(sEnvvar2, buf)),
251 "Expected empty or first part of the string (win98), got (%s)\n", buf);
254 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
255 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
258 * The buffer is NULL but the size is set
260 strcpy(buf, sEmptyBuffer);
263 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, NULL, &dwSize);
264 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
265 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
266 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
271 static void test_SHCopyKey(void)
273 HKEY hKeySrc, hKeyDst;
276 /* Delete existing destination sub keys */
278 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) && hKeyDst)
280 SHDeleteKeyA(hKeyDst, NULL);
281 RegCloseKey(hKeyDst);
285 dwRet = RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst);
286 if (dwRet || !hKeyDst)
288 ok( 0, "Destination couldn't be created, RegCreateKeyA returned (%lu)\n", dwRet);
293 dwRet = RegOpenKeyA(HKEY_LOCAL_MACHINE, REG_CURRENT_VERSION, &hKeySrc);
294 if (dwRet || !hKeySrc)
296 ok( 0, "Source couldn't be opened, RegOpenKeyA returned (%lu)\n", dwRet);
303 dwRet = (*pSHCopyKeyA)(hKeySrc, NULL, hKeyDst, 0);
304 ok ( ERROR_SUCCESS == dwRet, "Copy failed, ret=(%lu)\n", dwRet);
307 RegCloseKey(hKeySrc);
308 RegCloseKey(hKeyDst);
310 /* Check we copied the sub keys, i.e. something that's on every windows system (including Wine) */
312 dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination\\Setup", &hKeyDst);
313 if (dwRet || !hKeyDst)
315 ok ( 0, "Copy couldn't be opened, RegOpenKeyA returned (%lu)\n", dwRet);
319 /* And the we copied the values too */
320 ok(!SHQueryValueExA(hKeyDst, "BootDir", NULL, NULL, NULL, NULL), "SHQueryValueExA failed\n");
322 RegCloseKey(hKeyDst);
325 static void test_SHDeleteKey()
327 HKEY hKeyTest, hKeyS;
331 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKeyTest))
333 if (!RegCreateKey(hKeyTest, "ODBC", &hKeyS))
337 if (!RegCreateKey(hKeyS, "ODBC.INI", &hKeyO))
341 if (!RegCreateKey(hKeyS, "ODBCINST.INI", &hKeyO))
349 RegCloseKey (hKeyTest);
355 dwRet = SHDeleteKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC");
356 ok ( ERROR_SUCCESS == dwRet, "SHDeleteKey failed, ret=(%lu)\n", dwRet);
358 dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC", &hKeyS);
359 ok ( ERROR_FILE_NOT_FOUND == dwRet, "SHDeleteKey did not delete\n");
361 if (dwRet == ERROR_SUCCESS)
365 ok( 0, "Could not set up SHDeleteKey test\n");
370 HKEY hkey = create_test_entries();
374 hshlwapi = GetModuleHandleA("shlwapi.dll");
377 pSHCopyKeyA=(SHCopyKeyA_func)GetProcAddress(hshlwapi,"SHCopyKeyA");
378 pSHRegGetPathA=(SHRegGetPathA_func)GetProcAddress(hshlwapi,"SHRegGetPathA");
381 test_SHQUeryValueEx();
385 delete_key( hkey, "Software\\Wine", "Test" );