Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 1 | /* Unit test suite for SHReg* functions |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 2 | * |
| 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 Gouget | 4c31400 | 2002-05-23 02:40:07 +0000 | [diff] [blame] | 26 | #include "winerror.h" |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 27 | #include "winreg.h" |
| 28 | #include "winuser.h" |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 29 | #include "shlwapi.h" |
| 30 | |
Patrik Stridvall | 7e252cb | 2002-09-12 20:45:22 +0000 | [diff] [blame] | 31 | /* Keys used for testing */ |
Jon Griffiths | f5b34b5 | 2002-09-12 18:02:14 +0000 | [diff] [blame] | 32 | #define REG_TEST_KEY "Software\\Wine\\Test" |
| 33 | #define REG_CURRENT_VERSION "Software\\Microsoft\\Windows NT\\CurrentVersion" |
| 34 | |
Francois Gouget | 3a86019 | 2002-09-16 22:45:22 +0000 | [diff] [blame^] | 35 | static HMODULE hshlwapi; |
| 36 | typedef DWORD (WINAPI *SHCopyKeyA_func)(HKEY,LPCSTR,HKEY,DWORD); |
| 37 | static SHCopyKeyA_func pSHCopyKeyA; |
| 38 | typedef DWORD (WINAPI *SHRegGetPathA_func)(HKEY,LPCSTR,LPCSTR,LPSTR,DWORD); |
| 39 | static SHRegGetPathA_func pSHRegGetPathA; |
| 40 | |
Eric Pouech | f320e74 | 2002-06-28 17:35:20 +0000 | [diff] [blame] | 41 | static char * sTestpath1 = "%LONGSYSTEMVAR%\\subdir1"; |
| 42 | static char * sTestpath2 = "%FOO%\\subdir1"; |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 43 | |
| 44 | static char sExpTestpath1[MAX_PATH]; |
| 45 | static char sExpTestpath2[MAX_PATH]; |
Eric Pouech | f320e74 | 2002-06-28 17:35:20 +0000 | [diff] [blame] | 46 | static unsigned sExpLen1; |
| 47 | static unsigned sExpLen2; |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 48 | |
| 49 | static char * sEmptyBuffer ="0123456789"; |
| 50 | |
Alexandre Julliard | 3507999 | 2002-09-12 22:29:58 +0000 | [diff] [blame] | 51 | /* delete key and all its subkeys */ |
| 52 | static 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 | |
| 72 | static HKEY create_test_entries(void) |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 73 | { |
| 74 | HKEY hKey; |
| 75 | |
Eric Pouech | f320e74 | 2002-06-28 17:35:20 +0000 | [diff] [blame] | 76 | SetEnvironmentVariableA("LONGSYSTEMVAR", "bar"); |
| 77 | SetEnvironmentVariableA("FOO", "ImARatherLongButIndeedNeededString"); |
| 78 | |
Jon Griffiths | f5b34b5 | 2002-09-12 18:02:14 +0000 | [diff] [blame] | 79 | ok(!RegCreateKeyA(HKEY_CURRENT_USER, REG_TEST_KEY, &hKey), "RegCreateKeyA failed"); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 80 | |
| 81 | if (hKey) |
| 82 | { |
Alexandre Julliard | 63d29eb | 2002-08-09 01:14:23 +0000 | [diff] [blame] | 83 | 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 Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Eric Pouech | f320e74 | 2002-06-28 17:35:20 +0000 | [diff] [blame] | 88 | 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 Julliard | 3507999 | 2002-09-12 22:29:58 +0000 | [diff] [blame] | 93 | return hKey; |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static 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 Griffiths | f5b34b5 | 2002-09-12 18:02:14 +0000 | [diff] [blame] | 105 | ok(! SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", &dwType, buf, &dwSize), "SHGetValueA failed"); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 106 | 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 Griffiths | f5b34b5 | 2002-09-12 18:02:14 +0000 | [diff] [blame] | 112 | ok(! SHGetValueA(HKEY_CURRENT_USER, REG_TEST_KEY, "Test2", &dwType, buf, &dwSize), "SHGetValueA failed"); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 113 | ok( 0 == strcmp(sTestpath1, buf) , "(%s)", buf); |
| 114 | ok( REG_SZ == dwType , "(%lx)", dwType); |
| 115 | } |
| 116 | |
Jon Griffiths | f5b34b5 | 2002-09-12 18:02:14 +0000 | [diff] [blame] | 117 | static void test_SHGetRegPath(void) |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 118 | { |
| 119 | char buf[MAX_PATH]; |
| 120 | |
Francois Gouget | 3a86019 | 2002-09-16 22:45:22 +0000 | [diff] [blame^] | 121 | if (!pSHRegGetPathA) |
| 122 | return; |
| 123 | |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 124 | strcpy(buf, sEmptyBuffer); |
Francois Gouget | 3a86019 | 2002-09-16 22:45:22 +0000 | [diff] [blame^] | 125 | ok(! (*pSHRegGetPathA)(HKEY_CURRENT_USER, REG_TEST_KEY, "Test1", buf, 0), "SHRegGetPathA failed"); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 126 | ok( 0 == strcmp(sExpTestpath1, buf) , "(%s)", buf); |
| 127 | } |
| 128 | |
| 129 | static 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 Griffiths | f5b34b5 | 2002-09-12 18:02:14 +0000 | [diff] [blame] | 140 | ok(! RegOpenKeyExA(HKEY_CURRENT_USER, REG_TEST_KEY, 0, KEY_QUERY_VALUE, &hKey), "test4 RegOpenKey"); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 141 | |
| 142 | /****** SHQueryValueExA ******/ |
| 143 | |
| 144 | sTestedFunction = "SHQueryValueExA"; |
Alexandre Julliard | 63d29eb | 2002-08-09 01:14:23 +0000 | [diff] [blame] | 145 | nUsedBuffer1 = max(strlen(sExpTestpath1)+1, strlen(sTestpath1)+1); |
| 146 | nUsedBuffer2 = max(strlen(sExpTestpath2)+1, strlen(sTestpath2)+1); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 147 | /* |
| 148 | * Case 1.1 All arguments are NULL |
| 149 | */ |
Alexandre Julliard | 75de8fe | 2002-06-22 00:08:10 +0000 | [diff] [blame] | 150 | ok(! SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, NULL), "SHQueryValueExA failed"); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Case 1.2 dwType is set |
| 154 | */ |
| 155 | dwType = -1; |
Alexandre Julliard | 75de8fe | 2002-06-22 00:08:10 +0000 | [diff] [blame] | 156 | ok(! SHQueryValueExA( hKey, "Test1", NULL, &dwType, NULL, NULL), "SHQueryValueExA failed"); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 157 | ok( dwType == REG_SZ, "(%lu)", dwType); |
| 158 | |
| 159 | /* |
| 160 | * dwSize is set |
| 161 | * dwExpanded < dwUnExpanded |
| 162 | */ |
| 163 | dwSize = 6; |
Alexandre Julliard | 75de8fe | 2002-06-22 00:08:10 +0000 | [diff] [blame] | 164 | ok(! SHQueryValueExA( hKey, "Test1", NULL, NULL, NULL, &dwSize), "SHQueryValueExA failed"); |
| 165 | ok( dwSize == nUsedBuffer1, "(%lu,%u)", dwSize, nUsedBuffer1); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 166 | |
| 167 | /* |
| 168 | * dwExpanded > dwUnExpanded |
| 169 | */ |
| 170 | dwSize = 6; |
Alexandre Julliard | 75de8fe | 2002-06-22 00:08:10 +0000 | [diff] [blame] | 171 | ok(! SHQueryValueExA( hKey, "Test3", NULL, NULL, NULL, &dwSize), "SHQueryValueExA failed"); |
| 172 | ok( dwSize == nUsedBuffer2, "(%lu,%u)", dwSize, nUsedBuffer2); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 173 | |
| 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 Julliard | 75de8fe | 2002-06-22 00:08:10 +0000 | [diff] [blame] | 185 | ok( dwSize == nUsedBuffer1, "(%lu,%u)" , dwSize, nUsedBuffer1); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * string grows during expanding |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 189 | */ |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 190 | strcpy(buf, sEmptyBuffer); |
| 191 | dwSize = 6; |
| 192 | dwType = -1; |
| 193 | dwRet = SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize); |
Alexandre Julliard | 75de8fe | 2002-06-22 00:08:10 +0000 | [diff] [blame] | 194 | ok( ERROR_MORE_DATA == dwRet, "ERROR_MORE_DATA"); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 195 | ok( 0 == strcmp(sEmptyBuffer, buf), "(%s)", buf); |
Alexandre Julliard | 75de8fe | 2002-06-22 00:08:10 +0000 | [diff] [blame] | 196 | ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 197 | 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 Pouech | f320e74 | 2002-06-28 17:35:20 +0000 | [diff] [blame] | 203 | dwSize = sExpLen2 - 4; |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 204 | dwType = -1; |
Alexandre Julliard | 75de8fe | 2002-06-22 00:08:10 +0000 | [diff] [blame] | 205 | ok( ERROR_MORE_DATA == SHQueryValueExA( hKey, "Test3", NULL, &dwType, buf, &dwSize), "Expected ERROR_MORE_DATA"); |
Eric Pouech | f320e74 | 2002-06-28 17:35:20 +0000 | [diff] [blame] | 206 | ok( 0 == strncmp(sExpTestpath2, buf, sExpLen2 - 4 - 1), "(%s)", buf); |
| 207 | ok( sExpLen2 - 4 - 1 == strlen(buf), "(%s)", buf); |
Alexandre Julliard | 75de8fe | 2002-06-22 00:08:10 +0000 | [diff] [blame] | 208 | ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 209 | 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 Julliard | 75de8fe | 2002-06-22 00:08:10 +0000 | [diff] [blame] | 219 | ok( dwSize == nUsedBuffer2, "(%lu,%u)" , dwSize, nUsedBuffer2); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 220 | ok( dwType == REG_SZ, "(%lu)" , dwType); |
| 221 | |
| 222 | |
| 223 | RegCloseKey(hKey); |
| 224 | } |
| 225 | |
Jon Griffiths | f5b34b5 | 2002-09-12 18:02:14 +0000 | [diff] [blame] | 226 | static void test_SHCopyKey(void) |
| 227 | { |
| 228 | HKEY hKeySrc, hKeyDst; |
| 229 | |
Patrik Stridvall | 7e252cb | 2002-09-12 20:45:22 +0000 | [diff] [blame] | 230 | /* Delete existing destination sub keys */ |
Jon Griffiths | f5b34b5 | 2002-09-12 18:02:14 +0000 | [diff] [blame] | 231 | 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 Gouget | 3a86019 | 2002-09-16 22:45:22 +0000 | [diff] [blame^] | 253 | if (pSHCopyKeyA) |
| 254 | ok (!(*pSHCopyKeyA)(hKeyDst, NULL, hKeySrc, 0), "failed copy"); |
Jon Griffiths | f5b34b5 | 2002-09-12 18:02:14 +0000 | [diff] [blame] | 255 | |
| 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 Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 274 | START_TEST(shreg) |
| 275 | { |
Alexandre Julliard | 3507999 | 2002-09-12 22:29:58 +0000 | [diff] [blame] | 276 | HKEY hkey = create_test_entries(); |
Francois Gouget | 3a86019 | 2002-09-16 22:45:22 +0000 | [diff] [blame^] | 277 | hshlwapi = GetModuleHandleA("shlwapi.dll"); |
| 278 | if (hshlwapi) |
| 279 | { |
| 280 | pSHCopyKeyA=(SHCopyKeyA_func)GetProcAddress(hshlwapi,"SHCopyKeyA"); |
| 281 | pSHRegGetPathA=(SHRegGetPathA_func)GetProcAddress(hshlwapi,"SHRegGetPathA"); |
| 282 | } |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 283 | test_SHGetValue(); |
| 284 | test_SHQUeryValueEx(); |
Jon Griffiths | f5b34b5 | 2002-09-12 18:02:14 +0000 | [diff] [blame] | 285 | test_SHGetRegPath(); |
| 286 | test_SHCopyKey(); |
Alexandre Julliard | 3507999 | 2002-09-12 22:29:58 +0000 | [diff] [blame] | 287 | delete_key( hkey ); |
Juergen Schmied | c74a791 | 2002-05-09 19:48:07 +0000 | [diff] [blame] | 288 | } |