blob: 1a650e048087674e0b9cd6061f4fd97cd840c45f [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>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000022#include <stdarg.h>
Juergen Schmiedc74a7912002-05-09 19:48:07 +000023#include <stdio.h>
24
25#include "wine/test.h"
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000026#include "windef.h"
Juergen Schmiedc74a7912002-05-09 19:48:07 +000027#include "winbase.h"
Francois Gouget4c314002002-05-23 02:40:07 +000028#include "winerror.h"
Juergen Schmiedc74a7912002-05-09 19:48:07 +000029#include "winreg.h"
30#include "winuser.h"
Juergen Schmiedc74a7912002-05-09 19:48:07 +000031#include "shlwapi.h"
32
Patrik Stridvall7e252cb2002-09-12 20:45:22 +000033/* Keys used for testing */
Jon Griffithsf5b34b52002-09-12 18:02:14 +000034#define REG_TEST_KEY "Software\\Wine\\Test"
Paul Vriens361ee292005-01-17 15:45:44 +000035#define REG_CURRENT_VERSION "Software\\Microsoft\\Windows\\CurrentVersion"
Jon Griffithsf5b34b52002-09-12 18:02:14 +000036
Francois Gouget3a860192002-09-16 22:45:22 +000037static HMODULE hshlwapi;
38typedef DWORD (WINAPI *SHCopyKeyA_func)(HKEY,LPCSTR,HKEY,DWORD);
39static SHCopyKeyA_func pSHCopyKeyA;
40typedef DWORD (WINAPI *SHRegGetPathA_func)(HKEY,LPCSTR,LPCSTR,LPSTR,DWORD);
41static SHRegGetPathA_func pSHRegGetPathA;
42
Daniel Marmier7e0ddbc2003-10-07 22:54:17 +000043static const char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1";
44static const char * sTestpath2 = "%FOO%\\subdir1";
Juergen Schmiedc74a7912002-05-09 19:48:07 +000045
46static char sExpTestpath1[MAX_PATH];
47static char sExpTestpath2[MAX_PATH];
Eric Pouechf320e742002-06-28 17:35:20 +000048static unsigned sExpLen1;
49static unsigned sExpLen2;
Juergen Schmiedc74a7912002-05-09 19:48:07 +000050
Daniel Marmier7e0ddbc2003-10-07 22:54:17 +000051static const char * sEmptyBuffer ="0123456789";
Juergen Schmiedc74a7912002-05-09 19:48:07 +000052
Alexandre Julliard35079992002-09-12 22:29:58 +000053/* delete key and all its subkeys */
James Hawkins0fec65d2004-11-10 01:30:59 +000054static DWORD delete_key( HKEY hkey, LPSTR parent, LPSTR keyname )
Alexandre Julliard35079992002-09-12 22:29:58 +000055{
James Hawkins0fec65d2004-11-10 01:30:59 +000056 HKEY parentKey;
Alexandre Julliard35079992002-09-12 22:29:58 +000057 DWORD ret;
58
James Hawkins0fec65d2004-11-10 01:30:59 +000059 RegCloseKey(hkey);
60
61 /* open the parent of the key to close */
Jon Griffiths1f79d572004-12-15 10:50:01 +000062 ret = RegOpenKeyExA( HKEY_CURRENT_USER, parent, 0, KEY_ALL_ACCESS, &parentKey);
James Hawkins0fec65d2004-11-10 01:30:59 +000063 if (ret != ERROR_SUCCESS)
64 return ret;
65
Jon Griffiths1f79d572004-12-15 10:50:01 +000066 ret = SHDeleteKeyA( parentKey, keyname );
James Hawkins0fec65d2004-11-10 01:30:59 +000067 RegCloseKey(parentKey);
68
69 return ret;
Alexandre Julliard35079992002-09-12 22:29:58 +000070}
71
72static HKEY create_test_entries(void)
Juergen Schmiedc74a7912002-05-09 19:48:07 +000073{
74 HKEY hKey;
Paul Vriens14b96352005-01-21 16:18:00 +000075 DWORD ret;
Juergen Schmiedc74a7912002-05-09 19:48:07 +000076
Eric Pouechf320e742002-06-28 17:35:20 +000077 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
78 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
79
Paul Vriens14b96352005-01-21 16:18:00 +000080 ret = RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKey);
81 ok( ERROR_SUCCESS == ret, "RegCreateKeyA failed, ret=%lu\n", ret);
Juergen Schmiedc74a7912002-05-09 19:48:07 +000082
83 if (hKey)
84 {
Francois Gougetce7c3b52004-01-27 20:13:03 +000085 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");
Juergen Schmiedc74a7912002-05-09 19:48:07 +000088 }
89
Eric Pouechf320e742002-06-28 17:35:20 +000090 sExpLen1 = ExpandEnvironmentStringsA(sTestpath1, sExpTestpath1, sizeof(sExpTestpath1));
91 sExpLen2 = ExpandEnvironmentStringsA(sTestpath2, sExpTestpath2, sizeof(sExpTestpath2));
92
93 ok(sExpLen1 > 0, "Couldn't expand %s\n", sTestpath1);
94 ok(sExpLen2 > 0, "Couldn't expand %s\n", sTestpath2);
Alexandre Julliard35079992002-09-12 22:29:58 +000095 return hKey;
Juergen Schmiedc74a7912002-05-09 19:48:07 +000096}
97
98static void test_SHGetValue(void)
99{
100 DWORD dwSize;
101 DWORD dwType;
Paul Vriens14b96352005-01-21 16:18:00 +0000102 DWORD dwRet;
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000103 char buf[MAX_PATH];
104
105 strcpy(buf, sEmptyBuffer);
106 dwSize = MAX_PATH;
107 dwType = -1;
Paul Vriens14b96352005-01-21 16:18:00 +0000108 dwRet = SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", &dwType, buf, &dwSize);
109 ok( ERROR_SUCCESS == dwRet, "SHGetValueA failed, ret=%lu\n", dwRet);
110 ok( 0 == strcmp(sExpTestpath1, buf), "Comparing of (%s) with (%s) failed\n", buf, sExpTestpath1);
111 ok( REG_SZ == dwType, "Expected REG_SZ, got (%lu)\n", dwType);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000112
113 strcpy(buf, sEmptyBuffer);
114 dwSize = MAX_PATH;
115 dwType = -1;
Paul Vriens14b96352005-01-21 16:18:00 +0000116 dwRet = SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", &dwType, buf, &dwSize);
117 ok( ERROR_SUCCESS == dwRet, "SHGetValueA failed, ret=%lu\n", dwRet);
118 ok( 0 == strcmp(sTestpath1, buf) , "Comparing of (%s) with (%s) failed\n", buf, sTestpath1);
119 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000120}
121
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000122static void test_SHGetRegPath(void)
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000123{
124 char buf[MAX_PATH];
Paul Vriens14b96352005-01-21 16:18:00 +0000125 DWORD dwRet;
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000126
Francois Gouget3a860192002-09-16 22:45:22 +0000127 if (!pSHRegGetPathA)
128 return;
129
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000130 strcpy(buf, sEmptyBuffer);
Paul Vriens14b96352005-01-21 16:18:00 +0000131 dwRet = (*pSHRegGetPathA)(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", buf, 0);
132 ok( ERROR_SUCCESS == dwRet, "SHRegGetPathA failed, ret=%lu\n", dwRet);
133 ok( 0 == strcmp(sExpTestpath1, buf) , "Comparing (%s) with (%s) failed\n", buf, sExpTestpath1);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000134}
135
136static void test_SHQUeryValueEx(void)
137{
138 HKEY hKey;
139 DWORD dwSize;
140 DWORD dwType;
141 char buf[MAX_PATH];
142 DWORD dwRet;
Daniel Marmier7e0ddbc2003-10-07 22:54:17 +0000143 const char * sTestedFunction = "";
Francois Gouget23aa0f62002-12-18 20:50:49 +0000144 DWORD nUsedBuffer1,nUsedBuffer2;
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000145
Paul Vriens14b96352005-01-21 16:18:00 +0000146 sTestedFunction = "RegOpenKeyExA";
147 dwRet = RegOpenKeyExA(HKEY_CURRENT_USER, REG_TEST_KEY, 0, KEY_QUERY_VALUE, &hKey);
148 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000149
150 /****** SHQueryValueExA ******/
151
152 sTestedFunction = "SHQueryValueExA";
Alexandre Julliard63d29eb2002-08-09 01:14:23 +0000153 nUsedBuffer1 = max(strlen(sExpTestpath1)+1, strlen(sTestpath1)+1);
154 nUsedBuffer2 = max(strlen(sExpTestpath2)+1, strlen(sTestpath2)+1);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000155 /*
156 * Case 1.1 All arguments are NULL
157 */
Paul Vriens14b96352005-01-21 16:18:00 +0000158 dwRet = SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, NULL);
159 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000160
161 /*
162 * Case 1.2 dwType is set
163 */
164 dwType = -1;
Paul Vriens14b96352005-01-21 16:18:00 +0000165 dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, NULL, NULL);
166 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
167 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000168
169 /*
170 * dwSize is set
171 * dwExpanded < dwUnExpanded
172 */
173 dwSize = 6;
Paul Vriens14b96352005-01-21 16:18:00 +0000174 dwRet = SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, &dwSize);
175 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
176 ok( dwSize == nUsedBuffer1, "Buffer sizes (%lu) and (%lu) are not equal\n", dwSize, nUsedBuffer1);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000177
178 /*
179 * dwExpanded > dwUnExpanded
180 */
181 dwSize = 6;
Paul Vriens14b96352005-01-21 16:18:00 +0000182 dwRet = SHQueryValueExA( hKey, "Test3", NULL, NULL, NULL, &dwSize);
183 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
184 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000185
186 /*
187 * Case 1 string shrinks during expanding
188 */
189 strcpy(buf, sEmptyBuffer);
190 dwSize = 6;
191 dwType = -1;
192 dwRet = SHQueryValueExA( hKey, "Test1", NULL, &dwType, buf, &dwSize);
Paul Vriens14b96352005-01-21 16:18:00 +0000193 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
194 ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
195 ok( dwSize == nUsedBuffer1, "Buffer sizes (%lu) and (%lu) are not equal\n", dwSize, nUsedBuffer1);
196 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000197
198 /*
199 * string grows during expanding
Vincent BĂ©ron9a624912002-05-31 23:06:46 +0000200 */
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000201 strcpy(buf, sEmptyBuffer);
202 dwSize = 6;
203 dwType = -1;
204 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
Paul Vriens14b96352005-01-21 16:18:00 +0000205 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
206 ok( 0 == strcmp(sEmptyBuffer, buf) , "Comparing (%s) with (%s) failed\n", buf, sEmptyBuffer);
207 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
208 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000209
210 /*
211 * if the unexpanded string fits into the buffer it can get cut when expanded
212 */
213 strcpy(buf, sEmptyBuffer);
Eric Pouechf320e742002-06-28 17:35:20 +0000214 dwSize = sExpLen2 - 4;
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000215 dwType = -1;
Paul Vriens14b96352005-01-21 16:18:00 +0000216 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize);
217 ok( ERROR_MORE_DATA == dwRet, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet);
218 ok( 0 == strncmp(sExpTestpath2, buf, sExpLen2 - 4 - 1), "Comparing the first (%d) bytes of (%s) and (%s) failed\n", sExpLen2 - 4 - 1, buf, sExpTestpath2);
219 ok( sExpLen2 - 4 - 1 == strlen(buf), "Expected strlen(%s) to be %d instead of %d\n", buf, sExpLen2 - 4 - 1, strlen(buf));
220 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
221 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000222
223 /*
224 * The buffer is NULL but the size is set
225 */
226 strcpy(buf, sEmptyBuffer);
227 dwSize = 6;
228 dwType = -1;
229 dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, NULL, &dwSize);
Paul Vriens14b96352005-01-21 16:18:00 +0000230 ok( ERROR_SUCCESS == dwRet, "%s failed, ret=%lu\n", sTestedFunction, dwRet);
231 ok( dwSize >= nUsedBuffer2, "Buffer size (%lu) should be >= (%lu)\n", dwSize, nUsedBuffer2);
232 ok( REG_SZ == dwType , "Expected REG_SZ, got (%lu)\n", dwType);
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000233
234 RegCloseKey(hKey);
235}
236
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000237static void test_SHCopyKey(void)
238{
239 HKEY hKeySrc, hKeyDst;
240
Patrik Stridvall7e252cb2002-09-12 20:45:22 +0000241 /* Delete existing destination sub keys */
Francois Gougetd2667a42002-12-02 18:10:57 +0000242 hKeyDst = NULL;
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000243 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) && hKeyDst)
244 {
245 SHDeleteKeyA(hKeyDst, NULL);
246 RegCloseKey(hKeyDst);
247 }
248
Francois Gougetd2667a42002-12-02 18:10:57 +0000249 hKeyDst = NULL;
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000250 if (RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination", &hKeyDst) || !hKeyDst)
251 {
Francois Gougetce7c3b52004-01-27 20:13:03 +0000252 ok(0, "didn't open dest\n");
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000253 return;
254 }
255
Francois Gougetd2667a42002-12-02 18:10:57 +0000256 hKeySrc = NULL;
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000257 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, REG_CURRENT_VERSION, &hKeySrc) || !hKeySrc)
258 {
Francois Gougetce7c3b52004-01-27 20:13:03 +0000259 ok(0, "didn't open source\n");
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000260 return;
261 }
262
263
Francois Gouget3a860192002-09-16 22:45:22 +0000264 if (pSHCopyKeyA)
Paul Vriens7e8c0962005-01-17 15:33:00 +0000265 ok (!(*pSHCopyKeyA)(hKeySrc, NULL, hKeyDst, 0), "failed copy\n");
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000266
267 RegCloseKey(hKeySrc);
268 RegCloseKey(hKeyDst);
269
Paul Vriens361ee292005-01-17 15:45:44 +0000270 /* Check we copied the sub keys, i.e. something that's on every windows system (including Wine) */
Francois Gougetd2667a42002-12-02 18:10:57 +0000271 hKeyDst = NULL;
Paul Vriens361ee292005-01-17 15:45:44 +0000272 if (RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\CopyDestination\\Setup", &hKeyDst) || !hKeyDst)
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000273 {
Francois Gougetce7c3b52004-01-27 20:13:03 +0000274 ok(0, "didn't open copy\n");
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000275 return;
276 }
277
278 /* And the we copied the values too */
Paul Vriens361ee292005-01-17 15:45:44 +0000279 ok(!SHQueryValueExA(hKeyDst, "BootDir", NULL, NULL, NULL, NULL), "SHQueryValueExA failed\n");
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000280
281 RegCloseKey(hKeyDst);
282}
283
Bill Medland3f3e2922004-10-14 00:25:29 +0000284static void test_SHDeleteKey()
285{
286 HKEY hKeyTest;
287 int sysfail=1;
288 if (!RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKeyTest))
289 {
290 HKEY hKeyS;
291 if (!RegCreateKey(hKeyTest, "ODBC", &hKeyS))
292 {
293 HKEY hKeyO;
294 if (!RegCreateKey(hKeyS, "ODBC.INI", &hKeyO))
295 {
296 RegCloseKey (hKeyO);
297 if (!RegCreateKey(hKeyS, "ODBCINST.INI", &hKeyO))
298 {
299 RegCloseKey (hKeyO);
300 sysfail = 0;
301 }
302 }
303 RegCloseKey (hKeyS);
304 }
305 RegCloseKey (hKeyTest);
306 }
307 if (!sysfail)
308 {
309 HKEY hKeyS;
310 DWORD dwRet;
Jon Griffithsbcf43922005-01-03 14:36:47 +0000311 ok (!SHDeleteKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC"), "SHDeleteKey failed\n");
312 ok ((dwRet = RegOpenKeyA(HKEY_CURRENT_USER, REG_TEST_KEY "\\ODBC", &hKeyS)) == ERROR_FILE_NOT_FOUND, "SHDeleteKey did not delete\n");
Bill Medland3f3e2922004-10-14 00:25:29 +0000313 if (dwRet == ERROR_SUCCESS)
314 RegCloseKey (hKeyS);
315 }
316 else
317 ok (0, "Could not set up SHDeleteKey test\n");
318}
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000319
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000320START_TEST(shreg)
321{
Alexandre Julliard35079992002-09-12 22:29:58 +0000322 HKEY hkey = create_test_entries();
Paul Vriens14b96352005-01-21 16:18:00 +0000323
324 if (!hkey) return;
325
Francois Gouget3a860192002-09-16 22:45:22 +0000326 hshlwapi = GetModuleHandleA("shlwapi.dll");
327 if (hshlwapi)
328 {
329 pSHCopyKeyA=(SHCopyKeyA_func)GetProcAddress(hshlwapi,"SHCopyKeyA");
330 pSHRegGetPathA=(SHRegGetPathA_func)GetProcAddress(hshlwapi,"SHRegGetPathA");
331 }
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000332 test_SHGetValue();
333 test_SHQUeryValueEx();
Jon Griffithsf5b34b52002-09-12 18:02:14 +0000334 test_SHGetRegPath();
335 test_SHCopyKey();
Bill Medland3f3e2922004-10-14 00:25:29 +0000336 test_SHDeleteKey();
James Hawkins0fec65d2004-11-10 01:30:59 +0000337 delete_key( hkey, "Software\\Wine", "Test" );
Juergen Schmiedc74a7912002-05-09 19:48:07 +0000338}