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"
32 /* Keys used for testing */
33 #define REG_TEST_KEY "Software\\Wine\\Test"
34 #define REG_CURRENT_VERSION "Software\\Microsoft\\Windows\\CurrentVersion"
36 static HMODULE hshlwapi
;
37 typedef DWORD (WINAPI
*SHCopyKeyA_func
)(HKEY
,LPCSTR
,HKEY
,DWORD
);
38 static SHCopyKeyA_func pSHCopyKeyA
;
39 typedef DWORD (WINAPI
*SHRegGetPathA_func
)(HKEY
,LPCSTR
,LPCSTR
,LPSTR
,DWORD
);
40 static SHRegGetPathA_func pSHRegGetPathA
;
42 static const char * sTestpath1
= "%LONGSYSTEMVAR%\\subdir1";
43 static const char * sTestpath2
= "%FOO%\\subdir1";
45 static const char * sEnvvar1
= "bar";
46 static const char * sEnvvar2
= "ImARatherLongButIndeedNeededString";
48 static char sExpTestpath1
[MAX_PATH
];
49 static char sExpTestpath2
[MAX_PATH
];
50 static unsigned sExpLen1
;
51 static unsigned sExpLen2
;
53 static const char * sEmptyBuffer
="0123456789";
55 /* delete key and all its subkeys */
56 static DWORD
delete_key( HKEY hkey
, LPSTR parent
, LPSTR keyname
)
63 /* open the parent of the key to close */
64 ret
= RegOpenKeyExA( HKEY_CURRENT_USER
, parent
, 0, KEY_ALL_ACCESS
, &parentKey
);
65 if (ret
!= ERROR_SUCCESS
)
68 ret
= SHDeleteKeyA( parentKey
, keyname
);
69 RegCloseKey(parentKey
);
74 static HKEY
create_test_entries(void)
79 SetEnvironmentVariableA("LONGSYSTEMVAR", sEnvvar1
);
80 SetEnvironmentVariableA("FOO", sEnvvar2
);
82 ret
= RegCreateKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
, &hKey
);
83 ok( ERROR_SUCCESS
== ret
, "RegCreateKeyA failed, ret=%lu\n", ret
);
87 ok(!RegSetValueExA(hKey
,"Test1",0,REG_EXPAND_SZ
, sTestpath1
, strlen(sTestpath1
)+1), "RegSetValueExA failed\n");
88 ok(!RegSetValueExA(hKey
,"Test2",0,REG_SZ
, sTestpath1
, strlen(sTestpath1
)+1), "RegSetValueExA failed\n");
89 ok(!RegSetValueExA(hKey
,"Test3",0,REG_EXPAND_SZ
, sTestpath2
, strlen(sTestpath2
)+1), "RegSetValueExA failed\n");
92 sExpLen1
= ExpandEnvironmentStringsA(sTestpath1
, sExpTestpath1
, sizeof(sExpTestpath1
));
93 sExpLen2
= ExpandEnvironmentStringsA(sTestpath2
, sExpTestpath2
, sizeof(sExpTestpath2
));
95 ok(sExpLen1
> 0, "Couldn't expand %s\n", sTestpath1
);
96 trace("sExplen1 = (%d)\n", sExpLen1
);
97 ok(sExpLen2
> 0, "Couldn't expand %s\n", sTestpath2
);
98 trace("sExplen2 = (%d)\n", sExpLen2
);
103 static void test_SHGetValue(void)
110 strcpy(buf
, sEmptyBuffer
);
113 dwRet
= SHGetValueA(HKEY_CURRENT_USER
, REG_TEST_KEY
, "Test1", &dwType
, buf
, &dwSize
);
114 ok( ERROR_SUCCESS
== dwRet
, "SHGetValueA failed, ret=%lu\n", dwRet
);
115 ok( 0 == strcmp(sExpTestpath1
, buf
), "Comparing of (%s) with (%s) failed\n", buf
, sExpTestpath1
);
116 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%lu)\n", dwType
);
118 strcpy(buf
, sEmptyBuffer
);
121 dwRet
= SHGetValueA(HKEY_CURRENT_USER
, REG_TEST_KEY
, "Test2", &dwType
, buf
, &dwSize
);
122 ok( ERROR_SUCCESS
== dwRet
, "SHGetValueA failed, ret=%lu\n", dwRet
);
123 ok( 0 == strcmp(sTestpath1
, buf
) , "Comparing of (%s) with (%s) failed\n", buf
, sTestpath1
);
124 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%lu)\n", dwType
);
127 static void test_SHGetRegPath(void)
135 strcpy(buf
, sEmptyBuffer
);
136 dwRet
= (*pSHRegGetPathA
)(HKEY_CURRENT_USER
, REG_TEST_KEY
, "Test1", buf
, 0);
137 ok( ERROR_SUCCESS
== dwRet
, "SHRegGetPathA failed, ret=%lu\n", dwRet
);
138 ok( 0 == strcmp(sExpTestpath1
, buf
) , "Comparing (%s) with (%s) failed\n", buf
, sExpTestpath1
);
141 static void test_SHQUeryValueEx(void)
148 const char * sTestedFunction
= "";
149 DWORD nUsedBuffer1
,nUsedBuffer2
;
151 sTestedFunction
= "RegOpenKeyExA";
152 dwRet
= RegOpenKeyExA(HKEY_CURRENT_USER
, REG_TEST_KEY
, 0, KEY_QUERY_VALUE
, &hKey
);
153 ok( ERROR_SUCCESS
== dwRet
, "%s failed, ret=%lu\n", sTestedFunction
, dwRet
);
155 /****** SHQueryValueExA ******/
157 sTestedFunction
= "SHQueryValueExA";
158 nUsedBuffer1
= max(strlen(sExpTestpath1
)+1, strlen(sTestpath1
)+1);
159 nUsedBuffer2
= max(strlen(sExpTestpath2
)+1, strlen(sTestpath2
)+1);
161 * Case 1.1 All arguments are NULL
163 dwRet
= SHQueryValueExA( hKey
, "Test1", NULL
, NULL
, NULL
, NULL
);
164 ok( ERROR_SUCCESS
== dwRet
, "%s failed, ret=%lu\n", sTestedFunction
, dwRet
);
167 * Case 1.2 dwType is set
170 dwRet
= SHQueryValueExA( hKey
, "Test1", NULL
, &dwType
, NULL
, NULL
);
171 ok( ERROR_SUCCESS
== dwRet
, "%s failed, ret=%lu\n", sTestedFunction
, dwRet
);
172 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%lu)\n", dwType
);
176 * dwExpanded < dwUnExpanded
179 dwRet
= SHQueryValueExA( hKey
, "Test1", NULL
, NULL
, NULL
, &dwSize
);
180 ok( ERROR_SUCCESS
== dwRet
, "%s failed, ret=%lu\n", sTestedFunction
, dwRet
);
181 ok( dwSize
== nUsedBuffer1
, "Buffer sizes (%lu) and (%lu) are not equal\n", dwSize
, nUsedBuffer1
);
184 * dwExpanded > dwUnExpanded
187 dwRet
= SHQueryValueExA( hKey
, "Test3", NULL
, NULL
, NULL
, &dwSize
);
188 ok( ERROR_SUCCESS
== dwRet
, "%s failed, ret=%lu\n", sTestedFunction
, dwRet
);
189 ok( dwSize
>= nUsedBuffer2
, "Buffer size (%lu) should be >= (%lu)\n", dwSize
, nUsedBuffer2
);
192 * Case 1 string shrinks during expanding
194 strcpy(buf
, sEmptyBuffer
);
197 dwRet
= SHQueryValueExA( hKey
, "Test1", NULL
, &dwType
, buf
, &dwSize
);
198 ok( ERROR_MORE_DATA
== dwRet
, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet
);
199 ok( 0 == strcmp(sEmptyBuffer
, buf
) , "Comparing (%s) with (%s) failed\n", buf
, sEmptyBuffer
);
200 ok( dwSize
== nUsedBuffer1
, "Buffer sizes (%lu) and (%lu) are not equal\n", dwSize
, nUsedBuffer1
);
201 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%lu)\n", dwType
);
204 * string grows during expanding
205 * dwSize is smaller then the size of the unexpanded string
207 strcpy(buf
, sEmptyBuffer
);
210 dwRet
= SHQueryValueExA( hKey
, "Test3", NULL
, &dwType
, buf
, &dwSize
);
211 ok( ERROR_MORE_DATA
== dwRet
, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet
);
212 ok( 0 == strcmp(sEmptyBuffer
, buf
) , "Comparing (%s) with (%s) failed\n", buf
, sEmptyBuffer
);
213 ok( dwSize
>= nUsedBuffer2
, "Buffer size (%lu) should be >= (%lu)\n", dwSize
, nUsedBuffer2
);
214 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%lu)\n", dwType
);
217 * string grows during expanding
218 * dwSize is larger then the size of the unexpanded string but smaller than the part before the backslash
219 * if the unexpanded string fits into the buffer it can get cut when expanded
221 strcpy(buf
, sEmptyBuffer
);
222 dwSize
= strlen(sEnvvar2
) - 2;
224 dwRet
= SHQueryValueExA( hKey
, "Test3", NULL
, &dwType
, buf
, &dwSize
);
225 ok( ERROR_MORE_DATA
== dwRet
, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet
);
229 ok( (0 == strcmp("", buf
)) | (0 == strcmp(sTestpath2
, buf
)),
230 "Expected empty or unexpanded string (win98), got (%s)\n", buf
);
233 ok( dwSize
>= nUsedBuffer2
, "Buffer size (%lu) should be >= (%lu)\n", dwSize
, nUsedBuffer2
);
234 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%lu)\n", dwType
);
237 * string grows during expanding
238 * dwSize is larger then the size of the part before the backslash but smaller then the expanded string
239 * if the unexpanded string fits into the buffer it can get cut when expanded
241 strcpy(buf
, sEmptyBuffer
);
242 dwSize
= sExpLen2
- 4;
244 dwRet
= SHQueryValueExA( hKey
, "Test3", NULL
, &dwType
, buf
, &dwSize
);
245 ok( ERROR_MORE_DATA
== dwRet
, "Expected ERROR_MORE_DATA, got (%lu)\n", dwRet
);
249 ok( (0 == strcmp("", buf
)) | (0 == strcmp(sEnvvar2
, buf
)),
250 "Expected empty or first part of the string \"%s\", got \"%s\"\n", sEnvvar2
, buf
);
253 ok( dwSize
>= nUsedBuffer2
, "Buffer size (%lu) should be >= (%lu)\n", dwSize
, nUsedBuffer2
);
254 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%lu)\n", dwType
);
257 * The buffer is NULL but the size is set
259 strcpy(buf
, sEmptyBuffer
);
262 dwRet
= SHQueryValueExA( hKey
, "Test3", NULL
, &dwType
, NULL
, &dwSize
);
263 ok( ERROR_SUCCESS
== dwRet
, "%s failed, ret=%lu\n", sTestedFunction
, dwRet
);
264 ok( dwSize
>= nUsedBuffer2
, "Buffer size (%lu) should be >= (%lu)\n", dwSize
, nUsedBuffer2
);
265 ok( REG_SZ
== dwType
, "Expected REG_SZ, got (%lu)\n", dwType
);
270 static void test_SHCopyKey(void)
272 HKEY hKeySrc
, hKeyDst
;
275 /* Delete existing destination sub keys */
277 if (!RegOpenKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\CopyDestination", &hKeyDst
) && hKeyDst
)
279 SHDeleteKeyA(hKeyDst
, NULL
);
280 RegCloseKey(hKeyDst
);
284 dwRet
= RegCreateKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\CopyDestination", &hKeyDst
);
285 if (dwRet
|| !hKeyDst
)
287 ok( 0, "Destination couldn't be created, RegCreateKeyA returned (%lu)\n", dwRet
);
292 dwRet
= RegOpenKeyA(HKEY_LOCAL_MACHINE
, REG_CURRENT_VERSION
, &hKeySrc
);
293 if (dwRet
|| !hKeySrc
)
295 ok( 0, "Source couldn't be opened, RegOpenKeyA returned (%lu)\n", dwRet
);
302 dwRet
= (*pSHCopyKeyA
)(hKeySrc
, NULL
, hKeyDst
, 0);
303 ok ( ERROR_SUCCESS
== dwRet
, "Copy failed, ret=(%lu)\n", dwRet
);
306 RegCloseKey(hKeySrc
);
307 RegCloseKey(hKeyDst
);
309 /* Check we copied the sub keys, i.e. something that's on every windows system (including Wine) */
311 dwRet
= RegOpenKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\CopyDestination\\Setup", &hKeyDst
);
312 if (dwRet
|| !hKeyDst
)
314 ok ( 0, "Copy couldn't be opened, RegOpenKeyA returned (%lu)\n", dwRet
);
318 /* And the we copied the values too */
319 ok(!SHQueryValueExA(hKeyDst
, "BootDir", NULL
, NULL
, NULL
, NULL
), "SHQueryValueExA failed\n");
321 RegCloseKey(hKeyDst
);
324 static void test_SHDeleteKey()
326 HKEY hKeyTest
, hKeyS
;
330 if (!RegOpenKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
, &hKeyTest
))
332 if (!RegCreateKey(hKeyTest
, "ODBC", &hKeyS
))
336 if (!RegCreateKey(hKeyS
, "ODBC.INI", &hKeyO
))
340 if (!RegCreateKey(hKeyS
, "ODBCINST.INI", &hKeyO
))
348 RegCloseKey (hKeyTest
);
354 dwRet
= SHDeleteKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\ODBC");
355 ok ( ERROR_SUCCESS
== dwRet
, "SHDeleteKey failed, ret=(%lu)\n", dwRet
);
357 dwRet
= RegOpenKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\ODBC", &hKeyS
);
358 ok ( ERROR_FILE_NOT_FOUND
== dwRet
, "SHDeleteKey did not delete\n");
360 if (dwRet
== ERROR_SUCCESS
)
364 ok( 0, "Could not set up SHDeleteKey test\n");
369 HKEY hkey
= create_test_entries();
373 hshlwapi
= GetModuleHandleA("shlwapi.dll");
376 pSHCopyKeyA
=(SHCopyKeyA_func
)GetProcAddress(hshlwapi
,"SHCopyKeyA");
377 pSHRegGetPathA
=(SHRegGetPathA_func
)GetProcAddress(hshlwapi
,"SHRegGetPathA");
380 test_SHQUeryValueEx();
384 delete_key( hkey
, "Software\\Wine", "Test" );