blob: 829c848a458be375ca492307382e3c02bc4b910b [file] [log] [blame]
Vincent Béron9a624912002-05-31 23:06:46 +00001/* Unit test suite for SHReg* functions
Juergen Schmiedc74a7912002-05-09 19:48:07 +00002 *
3 * Copyright 2002 Juergen Schmied
4 *
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.
9 *
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.
14 *
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
18 */
19
20#include <assert.h>
21#include <stdlib.h>
22#include <stdio.h>
23
24#include "wine/test.h"
25#include "winbase.h"
Francois Gouget4c314002002-05-23 02:40:07 +000026#include "winerror.h"
Juergen Schmiedc74a7912002-05-09 19:48:07 +000027#include "winreg.h"
28#include "winuser.h"
Juergen Schmiedc74a7912002-05-09 19:48:07 +000029#include "shlwapi.h"
30
Patrik Stridvall7e252cb2002-09-12 20:45:22 +000031/* Keys used for testing */
Jon Griffithsf5b34b52002-09-12 18:02:14 +000032#define REG_TEST_KEY "Software\\Wine\\Test"
33#define REG_CURRENT_VERSION "Software\\Microsoft\\Windows NT\\CurrentVersion"
34
Francois Gouget3a860192002-09-16 22:45:22 +000035static HMODULE hshlwapi;
36typedef DWORD (WINAPI *SHCopyKeyA_func)(HKEY,LPCSTR,HKEY,DWORD);
37static SHCopyKeyA_func pSHCopyKeyA;
38typedef DWORD (WINAPI *SHRegGetPathA_func)(HKEY,LPCSTR,LPCSTR,LPSTR,DWORD);
39static SHRegGetPathA_func pSHRegGetPathA;
40
Eric Pouechf320e742002-06-28 17:35:20 +000041static char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
42static char * sTestpath2 = "%FOO%\\subdir1";
Juergen Schmiedc74a7912002-05-09 19:48:07 +000043
44static char sExpTestpath1[MAX_PATH];
45static char sExpTestpath2[MAX_PATH];
Eric Pouechf320e742002-06-28 17:35:20 +000046static unsigned sExpLen1;
47static unsigned sExpLen2;
Juergen Schmiedc74a7912002-05-09 19:48:07 +000048
49static char * sEmptyBuffer ="0123456789";
50
Alexandre Julliard35079992002-09-12 22:29:58 +000051/* delete key and all its subkeys */
52static DWORD delete_key( HKEY hkey )
53{
54 WCHAR name[MAX_PATH];
55 DWORD ret;
56
57 while (!(ret = RegEnumKeyW(hkey, 0, name, sizeof(name))))
58 {
59 HKEY tmp;
60 if (!(ret = RegOpenKeyExW( hkey, name, 0, KEY_ENUMERATE_SUB_KEYS, &tmp )))
61 {
62 ret = delete_key( tmp );
63 RegCloseKey( tmp );
64 }
65 if (ret) break;
66 }
67 if (ret != ERROR_NO_MORE_ITEMS) return ret;
68 RegDeleteKeyA( hkey, NULL );
69 return 0;
70}
71
72static HKEY create_test_entries(void)
Juergen Schmiedc74a7912002-05-09 19:48:07 +000073{
74 HKEY hKey;
75
Eric Pouechf320e742002-06-28 17:35:20 +000076 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
77 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
78
Jon Griffithsf5b34b52002-09-12 18:02:14 +000079 ok(!RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKey), "RegCreateKeyA failed");
Juergen Schmiedc74a7912002-05-09 19:48:07 +000080
81 if (hKey)
82 {
Alexandre Julliard63d29eb2002-08-09 01:14:23 +000083 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");
Juergen Schmiedc74a7912002-05-09 19:48:07 +000086 }
87
Eric Pouechf320e742002-06-28 17:35:20 +000088 sExpLen1 = ExpandEnvironmentStringsA(sTestpath1, sExpTestpath1, sizeof(sExpTestpath1));
89 sExpLen2 = ExpandEnvironmentStringsA(sTestpath2, sExpTestpath2, sizeof(sExpTestpath2));
90
91 ok(sExpLen1 > 0, "Couldn't expand %s\n", sTestpath1);
92 ok(sExpLen2 > 0, "Couldn't expand %s\n", sTestpath2);
Alexandre Julliard35079992002-09-12 22:29:58 +000093 return hKey;
Juergen Schmiedc74a7912002-05-09 19:48:07 +000094}
95
96static void test_SHGetValue(void)
97{
98 DWORD dwSize;
99 DWORD dwType;
100 char buf[MAX_PATH];
101
102 strcpy(buf, sEmptyBuffer);
103 dwSize = MAX_PATH;
104 dwType = -1;
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000105 ok(! SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", &dwType, buf, &dwSize), "SHGetValueA failed");
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000106 ok( 0 == strcmp(sExpTestpath1, buf), "(%s,%s)", buf, sExpTestpath1);
107 ok( REG_SZ == dwType, "(%lx)", dwType);
108
109 strcpy(buf, sEmptyBuffer);
110 dwSize = MAX_PATH;
111 dwType = -1;
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000112 ok(! SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", &dwType, buf, &dwSize), "SHGetValueA failed");
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000113 ok( 0 == strcmp(sTestpath1, buf) , "(%s)", buf);
114 ok( REG_SZ == dwType , "(%lx)", dwType);
115}
116
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000117static void test_SHGetRegPath(void)
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000118{
119 char buf[MAX_PATH];
120
Francois Gouget3a860192002-09-16 22:45:22 +0000121 if (!pSHRegGetPathA)
122 return;
123
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000124 strcpy(buf, sEmptyBuffer);
Francois Gouget3a860192002-09-16 22:45:22 +0000125 ok(! (*pSHRegGetPathA)(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", buf, 0), "SHRegGetPathA failed");
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000126 ok( 0 == strcmp(sExpTestpath1, buf) , "(%s)", buf);
127}
128
129static void test_SHQUeryValueEx(void)
130{
131 HKEY hKey;
132 DWORD dwSize;
133 DWORD dwType;
134 char buf[MAX_PATH];
135 DWORD dwRet;
136 char * sTestedFunction = "";
137 int nUsedBuffer1;
138 int nUsedBuffer2;
139
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000140 ok(! RegOpenKeyExA(HKEY_CURRENT_USER, REG_TEST_KEY, 0, KEY_QUERY_VALUE, &hKey), "test4 RegOpenKey");
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000141
142 /****** SHQueryValueExA ******/
143
144 sTestedFunction = "SHQueryValueExA";
Alexandre Julliard63d29eb2002-08-09 01:14:23 +0000145 nUsedBuffer1 = max(strlen(sExpTestpath1)+1, strlen(sTestpath1)+1);
146 nUsedBuffer2 = max(strlen(sExpTestpath2)+1, strlen(sTestpath2)+1);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000147 /*
148 * Case 1.1 All arguments are NULL
149 */
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000150 ok(! SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, NULL), "SHQueryValueExA failed");
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000151
152 /*
153 * Case 1.2 dwType is set
154 */
155 dwType = -1;
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000156 ok(! SHQueryValueExA( hKey, "Test1", NULL, &dwType, NULL, NULL), "SHQueryValueExA failed");
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000157 ok( dwType == REG_SZ, "(%lu)", dwType);
158
159 /*
160 * dwSize is set
161 * dwExpanded < dwUnExpanded
162 */
163 dwSize = 6;
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000164 ok(! SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, &dwSize), "SHQueryValueExA failed");
165 ok( dwSize == nUsedBuffer1, "(%lu,%u)", dwSize, nUsedBuffer1);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000166
167 /*
168 * dwExpanded > dwUnExpanded
169 */
170 dwSize = 6;
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000171 ok(! SHQueryValueExA( hKey, "Test3", NULL, NULL, NULL, &dwSize), "SHQueryValueExA failed");
172 ok( dwSize == nUsedBuffer2, "(%lu,%u)", dwSize, nUsedBuffer2);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000173
174
175 /*
176 * Case 1 string shrinks during expanding
177 */
178 strcpy(buf, sEmptyBuffer);
179 dwSize = 6;
180 dwType = -1;
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);
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000185 ok( dwSize == nUsedBuffer1, "(%lu,%u)" , dwSize, nUsedBuffer1);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000186
187 /*
188 * string grows during expanding
Vincent Béron9a624912002-05-31 23:06:46 +0000189 */
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000190 strcpy(buf, sEmptyBuffer);
191 dwSize = 6;
192 dwType = -1;
193 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000194 ok( ERROR_MORE_DATA == dwRet, "ERROR_MORE_DATA");
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000195 ok( 0 == strcmp(sEmptyBuffer, buf), "(%s)", buf);
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000196 ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000197 ok( dwType == REG_SZ, "(%lu)" , dwType);
198
199 /*
200 * if the unexpanded string fits into the buffer it can get cut when expanded
201 */
202 strcpy(buf, sEmptyBuffer);
Eric Pouechf320e742002-06-28 17:35:20 +0000203 dwSize = sExpLen2 - 4;
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000204 dwType = -1;
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000205 ok( ERROR_MORE_DATA == SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize), "Expected ERROR_MORE_DATA");
Eric Pouechf320e742002-06-28 17:35:20 +0000206 ok( 0 == strncmp(sExpTestpath2, buf, sExpLen2 - 4 - 1), "(%s)", buf);
207 ok( sExpLen2 - 4 - 1 == strlen(buf), "(%s)", buf);
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000208 ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000209 ok( dwType == REG_SZ, "(%lu)" , dwType);
210
211 /*
212 * The buffer is NULL but the size is set
213 */
214 strcpy(buf, sEmptyBuffer);
215 dwSize = 6;
216 dwType = -1;
217 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, NULL, &dwSize);
218 ok( ERROR_SUCCESS == dwRet, "(%lu)", dwRet);
Alexandre Julliard75de8fe2002-06-22 00:08:10 +0000219 ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000220 ok( dwType == REG_SZ, "(%lu)" , dwType);
221
222
223 RegCloseKey(hKey);
224}
225
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000226static void test_SHCopyKey(void)
227{
228 HKEY hKeySrc, hKeyDst;
229
Patrik Stridvall7e252cb2002-09-12 20:45:22 +0000230 /* Delete existing destination sub keys */
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000231 hKeyDst = (HKEY)0;
232 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) && hKeyDst)
233 {
234 SHDeleteKeyA(hKeyDst, NULL);
235 RegCloseKey(hKeyDst);
236 }
237
238 hKeyDst = (HKEY)0;
239 if (RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) || !hKeyDst)
240 {
241 ok(0, "didn't open dest");
242 return;
243 }
244
245 hKeySrc = (HKEY)0;
246 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, REG_CURRENT_VERSION, &hKeySrc) || !hKeySrc)
247 {
248 ok(0, "didn't open source");
249 return;
250 }
251
252
Francois Gouget3a860192002-09-16 22:45:22 +0000253 if (pSHCopyKeyA)
254 ok (!(*pSHCopyKeyA)(hKeyDst, NULL, hKeySrc, 0), "failed copy");
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000255
256 RegCloseKey(hKeySrc);
257 RegCloseKey(hKeyDst);
258
259 /* Check we copied the sub keys, i.e. AeDebug from the default wine registry */
260 hKeyDst = (HKEY)0;
261 if (RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination\\AeDebug", &hKeyDst) || !hKeyDst)
262 {
263 ok(0, "didn't open copy");
264 return;
265 }
266
267 /* And the we copied the values too */
268 ok(!SHQueryValueExA(hKeyDst, "Debugger", NULL, NULL, NULL, NULL), "SHQueryValueExA failed");
269
270 RegCloseKey(hKeyDst);
271}
272
273
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000274START_TEST(shreg)
275{
Alexandre Julliard35079992002-09-12 22:29:58 +0000276 HKEY hkey = create_test_entries();
Francois Gouget3a860192002-09-16 22:45:22 +0000277 hshlwapi = GetModuleHandleA("shlwapi.dll");
278 if (hshlwapi)
279 {
280 pSHCopyKeyA=(SHCopyKeyA_func)GetProcAddress(hshlwapi,"SHCopyKeyA");
281 pSHRegGetPathA=(SHRegGetPathA_func)GetProcAddress(hshlwapi,"SHRegGetPathA");
282 }
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000283 test_SHGetValue();
284 test_SHQUeryValueEx();
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000285 test_SHGetRegPath();
286 test_SHCopyKey();
Alexandre Julliard35079992002-09-12 22:29:58 +0000287 delete_key( hkey );
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000288}