[NTDLL_APITEST]
[reactos.git] / rostests / apitests / ntdll / NtQueryKey.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Tests for the NtQueryKey API
5 * PROGRAMMER: Jérôme Gardou <jerome.gardou@reactos.org>
6 */
7
8 #include <apitest.h>
9
10 #define WIN32_NO_STATUS
11 #include <ndk/rtlfuncs.h>
12 #include <ndk/cmfuncs.h>
13 #include <ndk/cmtypes.h>
14 #include <ndk/obfuncs.h>
15
16 static
17 void
18 Test_KeyNameInformation(void)
19 {
20 UNICODE_STRING HKLM_Name = RTL_CONSTANT_STRING(L"\\Registry\\Machine");
21 UNICODE_STRING HKLM_Software_Name = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software");
22 UNICODE_STRING Software_Name = RTL_CONSTANT_STRING(L"Software");
23 UNICODE_STRING InfoName;
24 HANDLE HKLM_Key, HKLM_Software_Key;
25 PKEY_NAME_INFORMATION NameInformation;
26 ULONG InfoLength;
27 OBJECT_ATTRIBUTES ObjectAttributes;
28 NTSTATUS Status;
29
30 /* Open the HKCU key */
31 InitializeObjectAttributes(&ObjectAttributes,
32 &HKLM_Name,
33 OBJ_CASE_INSENSITIVE,
34 NULL,
35 NULL);
36 Status = NtOpenKey(&HKLM_Key, KEY_READ, &ObjectAttributes);
37 ok_ntstatus(Status, STATUS_SUCCESS);
38
39 /* Get the name info length */
40 InfoLength = 0;
41 Status = NtQueryKey(HKLM_Key, KeyNameInformation, NULL, 0, &InfoLength);
42 ok_ntstatus(Status, STATUS_BUFFER_TOO_SMALL);
43 ok_size_t(InfoLength, FIELD_OFFSET(KEY_NAME_INFORMATION, Name[HKLM_Name.Length/sizeof(WCHAR)]));
44
45 /* Get it for real */
46 NameInformation = RtlAllocateHeap(RtlGetProcessHeap(), 0, InfoLength);
47 ok(NameInformation != NULL, "\n");
48
49 Status = NtQueryKey(HKLM_Key, KeyNameInformation, NameInformation, InfoLength, &InfoLength);
50 ok_ntstatus(Status, STATUS_SUCCESS);
51 ok_size_t(InfoLength, FIELD_OFFSET(KEY_NAME_INFORMATION, Name[HKLM_Name.Length/sizeof(WCHAR)]));
52 ok_size_t(NameInformation->NameLength, HKLM_Name.Length);
53
54 RtlInitUnicodeString(&InfoName, NameInformation->Name);
55 InfoName.Length = NameInformation->NameLength;
56 ok(RtlCompareUnicodeString(&InfoName, &HKLM_Name, TRUE) == 0, "%.*S\n",
57 InfoName.Length, InfoName.Buffer);
58
59 RtlFreeHeap(RtlGetProcessHeap(), 0, NameInformation);
60
61 /* Open one subkey */
62 InitializeObjectAttributes(&ObjectAttributes,
63 &Software_Name,
64 OBJ_CASE_INSENSITIVE,
65 HKLM_Key,
66 NULL);
67 Status = NtOpenKey(&HKLM_Software_Key, KEY_READ, &ObjectAttributes);
68 ok_ntstatus(Status, STATUS_SUCCESS);
69
70 /* Get the name info length */
71 InfoLength = 0;
72 Status = NtQueryKey(HKLM_Software_Key, KeyNameInformation, NULL, 0, &InfoLength);
73 ok_ntstatus(Status, STATUS_BUFFER_TOO_SMALL);
74 ok_size_t(InfoLength, FIELD_OFFSET(KEY_NAME_INFORMATION, Name[HKLM_Software_Name.Length/sizeof(WCHAR)]));
75
76 /* Get it for real */
77 NameInformation = RtlAllocateHeap(RtlGetProcessHeap(), 0, InfoLength);
78 ok(NameInformation != NULL, "\n");
79
80 Status = NtQueryKey(HKLM_Software_Key, KeyNameInformation, NameInformation, InfoLength, &InfoLength);
81 ok_ntstatus(Status, STATUS_SUCCESS);
82 ok_size_t(InfoLength, FIELD_OFFSET(KEY_NAME_INFORMATION, Name[HKLM_Software_Name.Length/sizeof(WCHAR)]));
83 ok_size_t(NameInformation->NameLength, HKLM_Software_Name.Length);
84
85 RtlInitUnicodeString(&InfoName, NameInformation->Name);
86 InfoName.Length = NameInformation->NameLength;
87 ok(RtlCompareUnicodeString(&InfoName, &HKLM_Software_Name, TRUE) == 0, "%.*S\n",
88 InfoName.Length, InfoName.Buffer);
89
90 RtlFreeHeap(RtlGetProcessHeap(), 0, NameInformation);
91
92 NtClose(HKLM_Software_Key);
93 NtClose(HKLM_Key);
94 }
95
96 START_TEST(NtQueryKey)
97 {
98 Test_KeyNameInformation();
99 }