Fix SHDeleteKey so that it will handle deleting a key with more than
[wine/wine-gecko.git] / dlls / shlwapi / tests / shreg.c
blobedfe97433c4bd78f07146bfadb7b0321ad69c48e
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 <stdlib.h>
22 #include <stdarg.h>
23 #include <stdio.h>
25 #include "wine/test.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "winreg.h"
30 #include "winuser.h"
31 #include "shlwapi.h"
33 /* Keys used for testing */
34 #define REG_TEST_KEY "Software\\Wine\\Test"
35 #define REG_CURRENT_VERSION "Software\\Microsoft\\Windows NT\\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 char sExpTestpath1[MAX_PATH];
47 static char sExpTestpath2[MAX_PATH];
48 static unsigned sExpLen1;
49 static unsigned sExpLen2;
51 static const char * sEmptyBuffer ="0123456789";
53 /* delete key and all its subkeys */
54 static DWORD delete_key( HKEY hkey )
56 WCHAR name[MAX_PATH];
57 DWORD ret;
59 while (!(ret = RegEnumKeyW(hkey, 0, name, sizeof(name))))
61 HKEY tmp;
62 if (!(ret = RegOpenKeyExW( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
64 ret = delete_key( tmp );
65 RegCloseKey( tmp );
67 if (ret) break;
69 if (ret != ERROR_NO_MORE_ITEMS) return ret;
70 RegDeleteKeyA( hkey, NULL );
71 return 0;
74 static HKEY create_test_entries(void)
76 HKEY hKey;
78 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
79 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
81 ok(!RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKey), "RegCreateKeyA failed\n");
83 if (hKey)
85 ok(!RegSetValueExA(hKey,"Test1",0,REG_EXPAND_SZ, sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed\n");
86 ok(!RegSetValueExA(hKey,"Test2",0,REG_SZ, sTestpath1, strlen(sTestpath1)+1), "RegSetValueExA failed\n");
87 ok(!RegSetValueExA(hKey,"Test3",0,REG_EXPAND_SZ, sTestpath2, strlen(sTestpath2)+1), "RegSetValueExA failed\n");
90 sExpLen1 = ExpandEnvironmentStringsA(sTestpath1, sExpTestpath1, sizeof(sExpTestpath1));
91 sExpLen2 = ExpandEnvironmentStringsA(sTestpath2, sExpTestpath2, sizeof(sExpTestpath2));
93 ok(sExpLen1 > 0, "Couldn't expand %s\n", sTestpath1);
94 ok(sExpLen2 > 0, "Couldn't expand %s\n", sTestpath2);
95 return hKey;
98 static void test_SHGetValue(void)
100 DWORD dwSize;
101 DWORD dwType;
102 char buf[MAX_PATH];
104 strcpy(buf, sEmptyBuffer);
105 dwSize = MAX_PATH;
106 dwType = -1;
107 ok(! SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", &dwType, buf, &dwSize), "SHGetValueA failed\n");
108 ok( 0 == strcmp(sExpTestpath1, buf), "(%s,%s)\n", buf, sExpTestpath1);
109 ok( REG_SZ == dwType, "(%lx)\n", dwType);
111 strcpy(buf, sEmptyBuffer);
112 dwSize = MAX_PATH;
113 dwType = -1;
114 ok(! SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", &dwType, buf, &dwSize), "SHGetValueA failed\n");
115 ok( 0 == strcmp(sTestpath1, buf) , "(%s)\n", buf);
116 ok( REG_SZ == dwType , "(%lx)\n", dwType);
119 static void test_SHGetRegPath(void)
121 char buf[MAX_PATH];
123 if (!pSHRegGetPathA)
124 return;
126 strcpy(buf, sEmptyBuffer);
127 ok(! (*pSHRegGetPathA)(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", buf, 0), "SHRegGetPathA failed\n");
128 ok( 0 == strcmp(sExpTestpath1, buf) , "(%s)\n", buf);
131 static void test_SHQUeryValueEx(void)
133 HKEY hKey;
134 DWORD dwSize;
135 DWORD dwType;
136 char buf[MAX_PATH];
137 DWORD dwRet;
138 const char * sTestedFunction = "";
139 DWORD nUsedBuffer1,nUsedBuffer2;
141 ok(! RegOpenKeyExA(HKEY_CURRENT_USER, REG_TEST_KEY, 0, KEY_QUERY_VALUE, &hKey), "test4 RegOpenKey\n");
143 /****** SHQueryValueExA ******/
145 sTestedFunction = "SHQueryValueExA";
146 nUsedBuffer1 = max(strlen(sExpTestpath1)+1, strlen(sTestpath1)+1);
147 nUsedBuffer2 = max(strlen(sExpTestpath2)+1, strlen(sTestpath2)+1);
149 * Case 1.1 All arguments are NULL
151 ok(! SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, NULL), "SHQueryValueExA failed\n");
154 * Case 1.2 dwType is set
156 dwType = -1;
157 ok(! SHQueryValueExA( hKey, "Test1", NULL, &dwType, NULL, NULL), "SHQueryValueExA failed\n");
158 ok( dwType == REG_SZ, "(%lu)\n", dwType);
161 * dwSize is set
162 * dwExpanded < dwUnExpanded
164 dwSize = 6;
165 ok(! SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, &dwSize), "SHQueryValueExA failed\n");
166 ok( dwSize == nUsedBuffer1, "(%lu,%lu)\n", dwSize, nUsedBuffer1);
169 * dwExpanded > dwUnExpanded
171 dwSize = 6;
172 ok(! SHQueryValueExA( hKey, "Test3", NULL, NULL, NULL, &dwSize), "SHQueryValueExA failed\n");
173 ok( dwSize == nUsedBuffer2, "(%lu,%lu)\n", dwSize, nUsedBuffer2);
177 * Case 1 string shrinks during expanding
179 strcpy(buf, sEmptyBuffer);
180 dwSize = 6;
181 dwType = -1;
182 dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, buf, &dwSize);
183 ok( dwRet == ERROR_MORE_DATA, "(%lu)\n", dwRet);
184 ok( 0 == strcmp(sEmptyBuffer, buf), "(%s)\n", buf);
185 ok( dwType == REG_SZ, "(%lu)\n" , dwType);
186 ok( dwSize == nUsedBuffer1, "(%lu,%lu)\n" , dwSize, nUsedBuffer1);
189 * string grows during expanding
191 strcpy(buf, sEmptyBuffer);
192 dwSize = 6;
193 dwType = -1;
194 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
195 ok( ERROR_MORE_DATA == dwRet, "ERROR_MORE_DATA\n");
196 ok( 0 == strcmp(sEmptyBuffer, buf), "(%s)\n", buf);
197 ok( dwSize == nUsedBuffer2, "(%lu,%lu)\n" , dwSize, nUsedBuffer2);
198 ok( dwType == REG_SZ, "(%lu)\n" , dwType);
201 * if the unexpanded string fits into the buffer it can get cut when expanded
203 strcpy(buf, sEmptyBuffer);
204 dwSize = sExpLen2 - 4;
205 dwType = -1;
206 ok( ERROR_MORE_DATA == SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize), "Expected ERROR_MORE_DATA\n");
207 ok( 0 == strncmp(sExpTestpath2, buf, sExpLen2 - 4 - 1), "(%s)\n", buf);
208 ok( sExpLen2 - 4 - 1 == strlen(buf), "(%s)\n", buf);
209 ok( dwSize == nUsedBuffer2, "(%lu,%lu)\n" , dwSize, nUsedBuffer2);
210 ok( dwType == REG_SZ, "(%lu)\n" , dwType);
213 * The buffer is NULL but the size is set
215 strcpy(buf, sEmptyBuffer);
216 dwSize = 6;
217 dwType = -1;
218 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, NULL, &dwSize);
219 ok( ERROR_SUCCESS == dwRet, "(%lu)\n", dwRet);
220 ok( dwSize == nUsedBuffer2, "(%lu,%lu)\n" , dwSize, nUsedBuffer2);
221 ok( dwType == REG_SZ, "(%lu)\n" , dwType);
224 RegCloseKey(hKey);
227 static void test_SHCopyKey(void)
229 HKEY hKeySrc, hKeyDst;
231 /* Delete existing destination sub keys */
232 hKeyDst = NULL;
233 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) && hKeyDst)
235 SHDeleteKeyA(hKeyDst, NULL);
236 RegCloseKey(hKeyDst);
239 hKeyDst = NULL;
240 if (RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) || !hKeyDst)
242 ok(0, "didn't open dest\n");
243 return;
246 hKeySrc = NULL;
247 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, REG_CURRENT_VERSION, &hKeySrc) || !hKeySrc)
249 ok(0, "didn't open source\n");
250 return;
254 if (pSHCopyKeyA)
255 ok (!(*pSHCopyKeyA)(hKeyDst, NULL, hKeySrc, 0), "failed copy\n");
257 RegCloseKey(hKeySrc);
258 RegCloseKey(hKeyDst);
260 /* Check we copied the sub keys, i.e. AeDebug from the default wine registry */
261 hKeyDst = NULL;
262 if (RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination\\AeDebug", &hKeyDst) || !hKeyDst)
264 ok(0, "didn't open copy\n");
265 return;
268 /* And the we copied the values too */
269 ok(!SHQueryValueExA(hKeyDst, "Debugger", NULL, NULL, NULL, NULL), "SHQueryValueExA failed\n");
271 RegCloseKey(hKeyDst);
274 static void test_SHDeleteKey()
276 HKEY hKeyTest;
277 int sysfail=1;
278 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKeyTest))
280 HKEY hKeyS;
281 if (!RegCreateKey(hKeyTest, "ODBC", &hKeyS))
283 HKEY hKeyO;
284 if (!RegCreateKey(hKeyS, "ODBC.INI", &hKeyO))
286 RegCloseKey (hKeyO);
287 if (!RegCreateKey(hKeyS, "ODBCINST.INI", &hKeyO))
289 RegCloseKey (hKeyO);
290 sysfail = 0;
293 RegCloseKey (hKeyS);
295 RegCloseKey (hKeyTest);
297 if (!sysfail)
299 HKEY hKeyS;
300 DWORD dwRet;
301 ok (!SHDeleteKey(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC"), "SHDeleteKey failed\n");
302 ok ((dwRet = RegOpenKey(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC", &hKeyS)) == ERROR_FILE_NOT_FOUND, "SHDeleteKey did not delete\n");
303 if (dwRet == ERROR_SUCCESS)
304 RegCloseKey (hKeyS);
306 else
307 ok (0, "Could not set up SHDeleteKey test\n");
310 START_TEST(shreg)
312 HKEY hkey = create_test_entries();
313 hshlwapi = GetModuleHandleA("shlwapi.dll");
314 if (hshlwapi)
316 pSHCopyKeyA=(SHCopyKeyA_func)GetProcAddress(hshlwapi,"SHCopyKeyA");
317 pSHRegGetPathA=(SHRegGetPathA_func)GetProcAddress(hshlwapi,"SHRegGetPathA");
319 test_SHGetValue();
320 test_SHQUeryValueEx();
321 test_SHGetRegPath();
322 test_SHCopyKey();
323 test_SHDeleteKey();
324 delete_key( hkey );