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
25 #include "wine/test.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
)
59 while (!(ret
= RegEnumKeyW(hkey
, 0, name
, sizeof(name
))))
62 if (!(ret
= RegOpenKeyExW( hkey
, name
, 0, KEY_ENUMERATE_SUB_KEYS
, &tmp
)))
64 ret
= delete_key( tmp
);
69 if (ret
!= ERROR_NO_MORE_ITEMS
) return ret
;
70 RegDeleteKeyA( hkey
, NULL
);
74 static HKEY
create_test_entries(void)
78 SetEnvironmentVariableA("LONGSYSTEMVAR", "bar");
79 SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString");
81 ok(!RegCreateKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
, &hKey
), "RegCreateKeyA failed\n");
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
);
98 static void test_SHGetValue(void)
104 strcpy(buf
, sEmptyBuffer
);
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
);
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)
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)
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
157 ok(! SHQueryValueExA( hKey
, "Test1", NULL
, &dwType
, NULL
, NULL
), "SHQueryValueExA failed\n");
158 ok( dwType
== REG_SZ
, "(%lu)\n", dwType
);
162 * dwExpanded < dwUnExpanded
165 ok(! SHQueryValueExA( hKey
, "Test1", NULL
, NULL
, NULL
, &dwSize
), "SHQueryValueExA failed\n");
166 ok( dwSize
== nUsedBuffer1
, "(%lu,%lu)\n", dwSize
, nUsedBuffer1
);
169 * dwExpanded > dwUnExpanded
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
);
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
);
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;
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
);
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
);
227 static void test_SHCopyKey(void)
229 HKEY hKeySrc
, hKeyDst
;
231 /* Delete existing destination sub keys */
233 if (!RegOpenKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\CopyDestination", &hKeyDst
) && hKeyDst
)
235 SHDeleteKeyA(hKeyDst
, NULL
);
236 RegCloseKey(hKeyDst
);
240 if (RegCreateKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\CopyDestination", &hKeyDst
) || !hKeyDst
)
242 ok(0, "didn't open dest\n");
247 if (RegOpenKeyA(HKEY_LOCAL_MACHINE
, REG_CURRENT_VERSION
, &hKeySrc
) || !hKeySrc
)
249 ok(0, "didn't open source\n");
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 */
262 if (RegOpenKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
"\\CopyDestination\\AeDebug", &hKeyDst
) || !hKeyDst
)
264 ok(0, "didn't open copy\n");
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()
278 if (!RegOpenKeyA(HKEY_CURRENT_USER
, REG_TEST_KEY
, &hKeyTest
))
281 if (!RegCreateKey(hKeyTest
, "ODBC", &hKeyS
))
284 if (!RegCreateKey(hKeyS
, "ODBC.INI", &hKeyO
))
287 if (!RegCreateKey(hKeyS
, "ODBCINST.INI", &hKeyO
))
295 RegCloseKey (hKeyTest
);
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
)
307 ok (0, "Could not set up SHDeleteKey test\n");
312 HKEY hkey
= create_test_entries();
313 hshlwapi
= GetModuleHandleA("shlwapi.dll");
316 pSHCopyKeyA
=(SHCopyKeyA_func
)GetProcAddress(hshlwapi
,"SHCopyKeyA");
317 pSHRegGetPathA
=(SHRegGetPathA_func
)GetProcAddress(hshlwapi
,"SHRegGetPathA");
320 test_SHQUeryValueEx();