1 /* Unit test suite for Rtl* Registry API functions
3 * Copyright 2003 Thomas Mertes
4 * Copyright 2005 Brad DeMorrow
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * NOTE: I don't test every RelativeTo value because it would be redundant, all calls go through
21 * helper function RTL_GetKeyHandle().--Brad DeMorrow
25 #include "ntdll_test.h"
32 #ifndef __WINE_WINTERNL_H
34 /* RtlQueryRegistryValues structs and defines */
35 #define RTL_REGISTRY_ABSOLUTE 0
36 #define RTL_REGISTRY_SERVICES 1
37 #define RTL_REGISTRY_CONTROL 2
38 #define RTL_REGISTRY_WINDOWS_NT 3
39 #define RTL_REGISTRY_DEVICEMAP 4
40 #define RTL_REGISTRY_USER 5
42 #define RTL_REGISTRY_HANDLE 0x40000000
43 #define RTL_REGISTRY_OPTIONAL 0x80000000
45 #define RTL_QUERY_REGISTRY_SUBKEY 0x00000001
46 #define RTL_QUERY_REGISTRY_TOPKEY 0x00000002
47 #define RTL_QUERY_REGISTRY_REQUIRED 0x00000004
48 #define RTL_QUERY_REGISTRY_NOVALUE 0x00000008
49 #define RTL_QUERY_REGISTRY_NOEXPAND 0x00000010
50 #define RTL_QUERY_REGISTRY_DIRECT 0x00000020
51 #define RTL_QUERY_REGISTRY_DELETE 0x00000040
53 typedef NTSTATUS (WINAPI *PRTL_QUERY_REGISTRY_ROUTINE)( PCWSTR ValueName,
60 typedef struct _RTL_QUERY_REGISTRY_TABLE {
61 PRTL_QUERY_REGISTRY_ROUTINE QueryRoutine;
68 } RTL_QUERY_REGISTRY_TABLE, *PRTL_QUERY_REGISTRY_TABLE;
70 #define InitializeObjectAttributes(p,n,a,r,s) \
72 (p)->Length = sizeof(OBJECT_ATTRIBUTES); \
73 (p)->RootDirectory = r; \
74 (p)->Attributes = a; \
75 (p)->ObjectName = n; \
76 (p)->SecurityDescriptor = s; \
77 (p)->SecurityQualityOfService = NULL; \
82 static NTSTATUS (WINAPI * pRtlCreateUnicodeStringFromAsciiz)(PUNICODE_STRING, LPCSTR);
83 static NTSTATUS (WINAPI * pRtlFreeUnicodeString)(PUNICODE_STRING);
84 static NTSTATUS (WINAPI * pNtDeleteValueKey)(IN HANDLE, IN PUNICODE_STRING);
85 static NTSTATUS (WINAPI * pRtlQueryRegistryValues)(IN ULONG, IN PCWSTR,IN PRTL_QUERY_REGISTRY_TABLE, IN PVOID,IN PVOID);
86 static NTSTATUS (WINAPI * pRtlCheckRegistryKey)(IN ULONG,IN PWSTR);
87 static NTSTATUS (WINAPI * pRtlOpenCurrentUser)(IN ACCESS_MASK, OUT PHKEY);
88 static NTSTATUS (WINAPI * pNtOpenKey)(PHANDLE, IN ACCESS_MASK, IN POBJECT_ATTRIBUTES);
89 static NTSTATUS (WINAPI * pNtClose)(IN HANDLE);
90 static NTSTATUS (WINAPI * pNtDeleteValueKey)(IN HANDLE, IN PUNICODE_STRING);
91 static NTSTATUS (WINAPI * pNtFlushKey)(HKEY);
92 static NTSTATUS (WINAPI * pNtDeleteKey)(HKEY);
93 static NTSTATUS (WINAPI * pNtCreateKey)( PHKEY retkey, ACCESS_MASK access, const OBJECT_ATTRIBUTES *attr,
94 ULONG TitleIndex, const UNICODE_STRING *class, ULONG options,
96 static NTSTATUS (WINAPI * pNtSetValueKey)( PHKEY, const PUNICODE_STRING, ULONG,
97 ULONG, const PVOID, ULONG );
98 static NTSTATUS (WINAPI * pRtlFormatCurrentUserKeyPath)(PUNICODE_STRING);
99 static NTSTATUS (WINAPI * pRtlCreateUnicodeString)( PUNICODE_STRING, LPCWSTR);
100 static NTSTATUS (WINAPI * pRtlReAllocateHeap)(IN PVOID, IN ULONG, IN PVOID, IN ULONG);
101 static NTSTATUS (WINAPI * pRtlAppendUnicodeToString)(PUNICODE_STRING, PCWSTR);
102 static NTSTATUS (WINAPI * pRtlUnicodeStringToAnsiString)(PSTRING, PUNICODE_STRING, BOOL);
103 static NTSTATUS (WINAPI * pRtlFreeHeap)(PVOID, ULONG, PVOID);
104 static NTSTATUS (WINAPI * pRtlAllocateHeap)(PVOID,ULONG,ULONG);
105 static NTSTATUS (WINAPI * pRtlZeroMemory)(PVOID, ULONG);
106 static NTSTATUS (WINAPI * pRtlpNtQueryValueKey)(HANDLE,ULONG*,PBYTE,DWORD*);
108 static HMODULE hntdll = 0;
109 static int CurrentTest = 0;
110 static UNICODE_STRING winetestpath;
112 #define NTDLL_GET_PROC(func) \
113 p ## func = (void*)GetProcAddress(hntdll, #func); \
115 trace("GetProcAddress(%s) failed\n", #func); \
116 FreeLibrary(hntdll); \
120 static BOOL InitFunctionPtrs(void)
122 hntdll = LoadLibraryA("ntdll.dll");
124 trace("Could not load ntdll.dll\n");
129 NTDLL_GET_PROC(RtlCreateUnicodeStringFromAsciiz)
130 NTDLL_GET_PROC(RtlCreateUnicodeString)
131 NTDLL_GET_PROC(RtlFreeUnicodeString)
132 NTDLL_GET_PROC(NtDeleteValueKey)
133 NTDLL_GET_PROC(RtlQueryRegistryValues)
134 NTDLL_GET_PROC(RtlCheckRegistryKey)
135 NTDLL_GET_PROC(RtlOpenCurrentUser)
136 NTDLL_GET_PROC(NtClose)
137 NTDLL_GET_PROC(NtDeleteValueKey)
138 NTDLL_GET_PROC(NtCreateKey)
139 NTDLL_GET_PROC(NtFlushKey)
140 NTDLL_GET_PROC(NtDeleteKey)
141 NTDLL_GET_PROC(NtSetValueKey)
142 NTDLL_GET_PROC(NtOpenKey)
143 NTDLL_GET_PROC(RtlFormatCurrentUserKeyPath)
144 NTDLL_GET_PROC(RtlReAllocateHeap)
145 NTDLL_GET_PROC(RtlAppendUnicodeToString)
146 NTDLL_GET_PROC(RtlUnicodeStringToAnsiString)
147 NTDLL_GET_PROC(RtlFreeHeap)
148 NTDLL_GET_PROC(RtlAllocateHeap)
149 NTDLL_GET_PROC(RtlZeroMemory)
150 NTDLL_GET_PROC(RtlpNtQueryValueKey)
154 #undef NTDLL_GET_PROC
156 static NTSTATUS WINAPI QueryRoutine (IN PCWSTR ValueName, IN ULONG ValueType, IN PVOID ValueData,
157 IN ULONG ValueLength, IN PVOID Context, IN PVOID EntryContext)
159 NTSTATUS ret = STATUS_SUCCESS;
160 int ValueNameLength = 0;
162 trace("**Test %d**\n", CurrentTest);
166 ValueNameLength = lstrlenW(ValueName);
168 ValName = (LPSTR)pRtlAllocateHeap(GetProcessHeap(), 0, ValueNameLength);
170 WideCharToMultiByte(0, 0, ValueName, ValueNameLength+1,ValName, ValueNameLength, 0, 0);
172 trace("ValueName: %s\n", ValName);
175 trace("ValueName: (null)\n");
180 trace("ValueType: REG_NONE\n");
181 trace("ValueData: %d\n", (int)ValueData);
185 trace("ValueType: REG_BINARY\n");
186 trace("ValueData: %d\n", (int)ValueData);
190 trace("ValueType: REG_SZ\n");
191 trace("ValueData: %s\n", (char*)ValueData);
195 trace("ValueType: REG_MULTI_SZ\n");
196 trace("ValueData: %s\n", (char*)ValueData);
200 trace("ValueType: REG_EXPAND_SZ\n");
201 trace("ValueData: %s\n", (char*)ValueData);
205 trace("ValueType: REG_DWORD\n");
206 trace("ValueData: %d\n", (int)ValueData);
209 trace("ValueLength: %d\n", (int)ValueLength);
212 ok(1, "\n"); /*checks that QueryRoutine is called*/
214 ok(!1, "Invalid Test Specified!\n");
219 pRtlFreeHeap(GetProcessHeap(), 0, ValName);
224 static void test_RtlQueryRegistryValues(void)
228 ******************************
230 ******************************
231 *RTL_QUERY_REGISTRY_SUBKEY * Name is the name of a subkey relative to Path
232 *RTL_QUERY_REGISTRY_TOPKEY * Resets location to original RelativeTo and Path
233 *RTL_QUERY_REGISTRY_REQUIRED * Key required. returns STATUS_OBJECT_NAME_NOT_FOUND if not present
234 *RTL_QUERY_REGISTRY_NOVALUE * We just want a call-back
235 *RTL_QUERY_REGISTRY_NOEXPAND * Don't expand the variables!
236 *RTL_QUERY_REGISTRY_DIRECT * Results of query will be stored in EntryContext(QueryRoutine ignored)
237 *RTL_QUERY_REGISTRY_DELETE * Delete value key after query
238 ******************************
241 **Test layout(numbered according to CurrentTest value)**
242 0)NOVALUE Just make sure call-back works
243 1)Null Name See if QueryRoutine is called for every value in current key
244 2)SUBKEY See if we can use SUBKEY to change the current path on the fly
245 3)REQUIRED Test for value that's not there
246 4)NOEXPAND See if it will return multiple strings(no expand should split strings up)
247 5)DIRECT Make it store data directly in EntryContext and not call QueryRoutine
248 6)DefaultType Test return values when key isn't present
249 7)DefaultValue Test Default Value returned with key isn't present(and no REQUIRED flag set)
250 8)DefaultLength Test Default Length with DefaultType = REG_SZ
251 9)DefaultLength Test Default Length with DefaultType = REG_MULTI_SZ
252 10)DefaultLength Test Default Length with DefaultType = REG_EXPAND_SZ
253 11)DefaultData Test whether DefaultData is used while DefaltType = REG_NONE(shouldn't be)
254 12)Delete Try to delete value key
260 PRTL_QUERY_REGISTRY_TABLE QueryTable = NULL;
261 RelativeTo = RTL_REGISTRY_ABSOLUTE;/*Only using absolute - no need to test all relativeto variables*/
263 QueryTable = (PRTL_QUERY_REGISTRY_TABLE)pRtlAllocateHeap(GetProcessHeap(), 0, sizeof(RTL_QUERY_REGISTRY_TABLE)*26);
265 pRtlZeroMemory( QueryTable, sizeof(RTL_QUERY_REGISTRY_TABLE) * 26);
267 QueryTable[0].QueryRoutine = QueryRoutine;
268 QueryTable[0].Flags = RTL_QUERY_REGISTRY_NOVALUE;
269 QueryTable[0].Name = NULL;
270 QueryTable[0].EntryContext = NULL;
271 QueryTable[0].DefaultType = REG_BINARY;
272 QueryTable[0].DefaultData = NULL;
273 QueryTable[0].DefaultLength = 100;
275 QueryTable[1].QueryRoutine = QueryRoutine;
276 QueryTable[1].Flags = 0;
277 QueryTable[1].Name = NULL;
278 QueryTable[1].EntryContext = 0;
279 QueryTable[1].DefaultType = REG_NONE;
280 QueryTable[1].DefaultData = NULL;
281 QueryTable[1].DefaultLength = 0;
283 QueryTable[2].QueryRoutine = NULL;
284 QueryTable[2].Flags = 0;
285 QueryTable[2].Name = NULL;
286 QueryTable[2].EntryContext = 0;
287 QueryTable[2].DefaultType = REG_NONE;
288 QueryTable[2].DefaultData = NULL;
289 QueryTable[2].DefaultLength = 0;
291 status = pRtlQueryRegistryValues(RelativeTo, winetestpath.Buffer, QueryTable, 0, 0);
292 ok(status == STATUS_SUCCESS, "RtlQueryRegistryValues return: 0x%08x\n", status);
294 pRtlFreeHeap(GetProcessHeap(), 0, QueryTable);
297 static void test_NtOpenKey(void)
301 OBJECT_ATTRIBUTES attr;
302 ACCESS_MASK am = KEY_READ;
308 status = pNtOpenKey(NULL, 0, NULL);
309 ok(status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got: 0x%08x\n", status);
311 /* NULL attributes */
312 status = pNtOpenKey(&key, 0, NULL);
313 ok(status == STATUS_ACCESS_VIOLATION /* W2K3/XP/W2K */ || status == STATUS_INVALID_PARAMETER /* NT4 */,
314 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER(NT4), got: 0x%08x\n", status);
317 InitializeObjectAttributes(&attr, &winetestpath, 0, 0, 0);
320 status = pNtOpenKey(NULL, 0, &attr);
322 ok(status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got: 0x%08x\n", status);
324 /* Length > sizeof(OBJECT_ATTRIBUTES) */
326 status = pNtOpenKey(&key, am, &attr);
328 ok(status == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got: 0x%08x\n", status);
331 static void test_NtCreateKey(void)
334 OBJECT_ATTRIBUTES attr;
336 ACCESS_MASK am = GENERIC_ALL;
340 status = pNtCreateKey(NULL, 0, NULL, 0, 0, 0, 0);
341 ok(status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got: 0x%08x\n", status);
344 status = pNtCreateKey(&key, 0, NULL, 0, 0, 0, 0);
345 ok(status == STATUS_ACCESS_VIOLATION /* W2K3/XP/W2K */ || status == STATUS_INVALID_PARAMETER /* NT4 */,
346 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER(NT4), got: 0x%08x\n", status);
348 /* Only accessmask */
349 status = pNtCreateKey(NULL, am, NULL, 0, 0, 0, 0);
350 ok(status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got: 0x%08x\n", status);
352 /* Key and accessmask */
353 status = pNtCreateKey(&key, am, NULL, 0, 0, 0, 0);
354 ok(status == STATUS_ACCESS_VIOLATION /* W2K3/XP/W2K */ || status == STATUS_INVALID_PARAMETER /* NT4 */,
355 "Expected STATUS_ACCESS_VIOLATION or STATUS_INVALID_PARAMETER(NT4), got: 0x%08x\n", status);
357 InitializeObjectAttributes(&attr, &winetestpath, 0, 0, 0);
359 /* Only attributes */
360 status = pNtCreateKey(NULL, 0, &attr, 0, 0, 0, 0);
361 ok(status == STATUS_ACCESS_VIOLATION, "Expected STATUS_ACCESS_VIOLATION, got: 0x%08x\n", status);
363 status = pNtCreateKey(&key, am, &attr, 0, 0, 0, 0);
364 ok(status == STATUS_SUCCESS, "NtCreateKey Failed: 0x%08x\n", status);
366 /* Length > sizeof(OBJECT_ATTRIBUTES) */
368 status = pNtCreateKey(&key, am, &attr, 0, 0, 0, 0);
369 ok(status == STATUS_INVALID_PARAMETER, "Expected STATUS_INVALID_PARAMETER, got: 0x%08x\n", status);
374 static void test_NtSetValueKey(void)
378 OBJECT_ATTRIBUTES attr;
379 ACCESS_MASK am = KEY_WRITE;
380 UNICODE_STRING ValName;
383 pRtlCreateUnicodeStringFromAsciiz(&ValName, "deletetest");
385 InitializeObjectAttributes(&attr, &winetestpath, 0, 0, 0);
386 status = pNtOpenKey(&key, am, &attr);
387 ok(status == STATUS_SUCCESS, "NtOpenKey Failed: 0x%08x\n", status);
389 status = pNtSetValueKey(key, &ValName, 0, REG_DWORD, &data, sizeof(data));
390 ok(status == STATUS_SUCCESS, "NtSetValueKey Failed: 0x%08x\n", status);
392 pRtlFreeUnicodeString(&ValName);
396 static void test_RtlOpenCurrentUser(void)
400 status=pRtlOpenCurrentUser(KEY_READ, &handle);
401 ok(status == STATUS_SUCCESS, "RtlOpenCurrentUser Failed: 0x%08x\n", status);
405 static void test_RtlCheckRegistryKey(void)
409 status = pRtlCheckRegistryKey(RTL_REGISTRY_ABSOLUTE, winetestpath.Buffer);
410 ok(status == STATUS_SUCCESS, "RtlCheckRegistryKey with RTL_REGISTRY_ABSOLUTE: 0x%08x\n", status);
412 status = pRtlCheckRegistryKey((RTL_REGISTRY_ABSOLUTE | RTL_REGISTRY_OPTIONAL), winetestpath.Buffer);
413 ok(status == STATUS_SUCCESS, "RtlCheckRegistryKey with RTL_REGISTRY_ABSOLUTE and RTL_REGISTRY_OPTIONAL: 0x%08x\n", status);
416 static void test_NtFlushKey(void)
420 OBJECT_ATTRIBUTES attr;
421 ACCESS_MASK am = KEY_ALL_ACCESS;
423 status = pNtFlushKey(NULL);
424 ok(status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got: 0x%08x\n", status);
426 InitializeObjectAttributes(&attr, &winetestpath, 0, 0, 0);
427 pNtOpenKey(&hkey, am, &attr);
429 status = pNtFlushKey(hkey);
430 ok(status == STATUS_SUCCESS, "NtDeleteKey Failed: 0x%08x\n", status);
435 static void test_NtDeleteKey(void)
439 OBJECT_ATTRIBUTES attr;
440 ACCESS_MASK am = KEY_ALL_ACCESS;
442 status = pNtDeleteKey(NULL);
443 ok(status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got: 0x%08x\n", status);
445 InitializeObjectAttributes(&attr, &winetestpath, 0, 0, 0);
446 status = pNtOpenKey(&hkey, am, &attr);
448 status = pNtDeleteKey(hkey);
449 ok(status == STATUS_SUCCESS, "NtDeleteKey Failed: 0x%08x\n", status);
452 static void test_RtlpNtQueryValueKey(void)
456 status = pRtlpNtQueryValueKey(NULL, NULL, NULL, NULL);
457 ok(status == STATUS_INVALID_HANDLE, "Expected STATUS_INVALID_HANDLE, got: 0x%08x\n", status);
462 static const WCHAR winetest[] = {'\\','W','i','n','e','T','e','s','t','\\',0};
463 if(!InitFunctionPtrs())
465 pRtlFormatCurrentUserKeyPath(&winetestpath);
466 winetestpath.Buffer = (PWSTR)pRtlReAllocateHeap(GetProcessHeap(), HEAP_ZERO_MEMORY, winetestpath.Buffer,
467 winetestpath.MaximumLength + sizeof(winetest)*sizeof(WCHAR));
468 winetestpath.MaximumLength = winetestpath.MaximumLength + sizeof(winetest)*sizeof(WCHAR);
470 pRtlAppendUnicodeToString(&winetestpath, winetest);
474 test_NtSetValueKey();
475 test_RtlCheckRegistryKey();
476 test_RtlOpenCurrentUser();
477 test_RtlQueryRegistryValues();
478 test_RtlpNtQueryValueKey();
482 pRtlFreeUnicodeString(&winetestpath);