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
24 #include "wine/test.h"
31 /* Keys used for testing */
32 #define REG_TEST_KEY "Software\\Wine\\Test"
33 #define REG_CURRENT_VERSION "Software\\Microsoft\\Windows NT\\CurrentVersion"
35 static HMODULE hshlwapi;
36 typedef DWORD (WINAPI *SHCopyKeyA_func)(HKEY,LPCSTR,HKEY,DWORD);
37 static SHCopyKeyA_func pSHCopyKeyA;
38 typedef DWORD (WINAPI *SHRegGetPathA_func)(HKEY,LPCSTR,LPCSTR,LPSTR,DWORD);
39 static SHRegGetPathA_func pSHRegGetPathA;
41 static char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
42 static char * sTestpath2 = "%FOO%\\subdir1";
44 static char sExpTestpath1[MAX_PATH];
45 static char sExpTestpath2[MAX_PATH];
46 static unsigned sExpLen1;
47 static unsigned sExpLen2;
49 static char * sEmptyBuffer ="0123456789";
51 /* delete key and all its subkeys */
52 static DWORD delete_key( HKEY hkey )
57 while (!(ret = RegEnumKeyW(hkey, 0, name, sizeof(name))))
60 if (!(ret = RegOpenKeyExW( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
62 ret = delete_key( tmp );
67 if (ret != ERROR_NO_MORE_ITEMS) return ret;
68 RegDeleteKeyA( hkey, NULL );
72 static HKEY create_test_entries(void)
76 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
77 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
79 ok(!RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKey), "RegCreateKeyA failed");
83 ok(!RegSetValueExA(hKey,"Test1",0,REG_EXPAND_SZ, sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed");
84 ok(!RegSetValueExA(hKey,"Test2",0,REG_SZ, sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed");
85 ok(!RegSetValueExA(hKey,"Test3",0,REG_EXPAND_SZ, sTestpath2, strlen(sTestpath2)+1), "RegSetValueExA failed");
88 sExpLen1 = ExpandEnvironmentStringsA(sTestpath1, sExpTestpath1, sizeof(sExpTestpath1));
89 sExpLen2 = ExpandEnvironmentStringsA(sTestpath2, sExpTestpath2, sizeof(sExpTestpath2));
91 ok(sExpLen1 > 0, "Couldn't expand %s\n", sTestpath1);
92 ok(sExpLen2 > 0, "Couldn't expand %s\n", sTestpath2);
96 static void test_SHGetValue(void)
102 strcpy(buf, sEmptyBuffer);
105 ok(! SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", &dwType, buf, &dwSize), "SHGetValueA failed");
106 ok( 0 == strcmp(sExpTestpath1, buf), "(%s,%s)", buf, sExpTestpath1);
107 ok( REG_SZ == dwType, "(%lx)", dwType);
109 strcpy(buf, sEmptyBuffer);
112 ok(! SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", &dwType, buf, &dwSize), "SHGetValueA failed");
113 ok( 0 == strcmp(sTestpath1, buf) , "(%s)", buf);
114 ok( REG_SZ == dwType , "(%lx)", dwType);
117 static void test_SHGetRegPath(void)
124 strcpy(buf, sEmptyBuffer);
125 ok(! (*pSHRegGetPathA)(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", buf, 0), "SHRegGetPathA failed");
126 ok( 0 == strcmp(sExpTestpath1, buf) , "(%s)", buf);
129 static void test_SHQUeryValueEx(void)
136 char * sTestedFunction = "";
140 ok(! RegOpenKeyExA(HKEY_CURRENT_USER, REG_TEST_KEY, 0, KEY_QUERY_VALUE, &hKey), "test4 RegOpenKey");
142 /****** SHQueryValueExA ******/
144 sTestedFunction = "SHQueryValueExA";
145 nUsedBuffer1 = max(strlen(sExpTestpath1)+1, strlen(sTestpath1)+1);
146 nUsedBuffer2 = max(strlen(sExpTestpath2)+1, strlen(sTestpath2)+1);
148 * Case 1.1 All arguments are NULL
150 ok(! SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, NULL), "SHQueryValueExA failed");
153 * Case 1.2 dwType is set
156 ok(! SHQueryValueExA( hKey, "Test1", NULL, &dwType, NULL, NULL), "SHQueryValueExA failed");
157 ok( dwType == REG_SZ, "(%lu)", dwType);
161 * dwExpanded < dwUnExpanded
164 ok(! SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, &dwSize), "SHQueryValueExA failed");
165 ok( dwSize == nUsedBuffer1, "(%lu,%u)", dwSize, nUsedBuffer1);
168 * dwExpanded > dwUnExpanded
171 ok(! SHQueryValueExA( hKey, "Test3", NULL, NULL, NULL, &dwSize), "SHQueryValueExA failed");
172 ok( dwSize == nUsedBuffer2, "(%lu,%u)", dwSize, nUsedBuffer2);
176 * Case 1 string shrinks during expanding
178 strcpy(buf, sEmptyBuffer);
181 dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, buf, &dwSize);
182 ok( dwRet == ERROR_MORE_DATA, "(%lu)", dwRet);
183 ok( 0 == strcmp(sEmptyBuffer, buf), "(%s)", buf);
184 ok( dwType == REG_SZ, "(%lu)" , dwType);
185 ok( dwSize == nUsedBuffer1, "(%lu,%u)" , dwSize, nUsedBuffer1);
188 * string grows during expanding
190 strcpy(buf, sEmptyBuffer);
193 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
194 ok( ERROR_MORE_DATA == dwRet, "ERROR_MORE_DATA");
195 ok( 0 == strcmp(sEmptyBuffer, buf), "(%s)", buf);
196 ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2);
197 ok( dwType == REG_SZ, "(%lu)" , dwType);
200 * if the unexpanded string fits into the buffer it can get cut when expanded
202 strcpy(buf, sEmptyBuffer);
203 dwSize = sExpLen2 - 4;
205 ok( ERROR_MORE_DATA == SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize), "Expected ERROR_MORE_DATA");
206 ok( 0 == strncmp(sExpTestpath2, buf, sExpLen2 - 4 - 1), "(%s)", buf);
207 ok( sExpLen2 - 4 - 1 == strlen(buf), "(%s)", buf);
208 ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2);
209 ok( dwType == REG_SZ, "(%lu)" , dwType);
212 * The buffer is NULL but the size is set
214 strcpy(buf, sEmptyBuffer);
217 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, NULL, &dwSize);
218 ok( ERROR_SUCCESS == dwRet, "(%lu)", dwRet);
219 ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2);
220 ok( dwType == REG_SZ, "(%lu)" , dwType);
226 static void test_SHCopyKey(void)
228 HKEY hKeySrc, hKeyDst;
230 /* Delete existing destination sub keys */
232 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) && hKeyDst)
234 SHDeleteKeyA(hKeyDst, NULL);
235 RegCloseKey(hKeyDst);
239 if (RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) || !hKeyDst)
241 ok(0, "didn't open dest");
246 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, REG_CURRENT_VERSION, &hKeySrc) || !hKeySrc)
248 ok(0, "didn't open source");
254 ok (!(*pSHCopyKeyA)(hKeyDst, NULL, hKeySrc, 0), "failed copy");
256 RegCloseKey(hKeySrc);
257 RegCloseKey(hKeyDst);
259 /* Check we copied the sub keys, i.e. AeDebug from the default wine registry */
261 if (RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination\\AeDebug", &hKeyDst) || !hKeyDst)
263 ok(0, "didn't open copy");
267 /* And the we copied the values too */
268 ok(!SHQueryValueExA(hKeyDst, "Debugger", NULL, NULL, NULL, NULL), "SHQueryValueExA failed");
270 RegCloseKey(hKeyDst);
276 HKEY hkey = create_test_entries();
277 hshlwapi = GetModuleHandleA("shlwapi.dll");
280 pSHCopyKeyA=(SHCopyKeyA_func)GetProcAddress(hshlwapi,"SHCopyKeyA");
281 pSHRegGetPathA=(SHRegGetPathA_func)GetProcAddress(hshlwapi,"SHRegGetPathA");
284 test_SHQUeryValueEx();