[NTDLL_APITEST]
[reactos.git] / rostests / apitests / ntdll / NtCreateKey.c
1 /*
2 * PROJECT: ReactOS API tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Test for NtCreateKey
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8 #include <apitest.h>
9
10 #include <winreg.h>
11 #include <ndk/cmfuncs.h>
12 #include <ndk/obfuncs.h>
13
14 static
15 VOID
16 VerifyAccess_(
17 _In_ HANDLE Handle,
18 _In_ ACCESS_MASK ExpectedAccess,
19 _In_ PCSTR File,
20 _In_ INT Line)
21 {
22 NTSTATUS Status;
23 OBJECT_BASIC_INFORMATION BasicInfo;
24 ULONG Length;
25
26 Status = NtQueryObject(Handle,
27 ObjectBasicInformation,
28 &BasicInfo,
29 sizeof(BasicInfo),
30 &Length);
31 ok_(File, Line)(Status == STATUS_SUCCESS, "NtQueryObject returned 0x%lx\n", Status);
32 ok_(File, Line)(BasicInfo.GrantedAccess == ExpectedAccess,
33 "GrantedAccess is 0x%lx, expected 0x%lx\n",
34 BasicInfo.GrantedAccess, ExpectedAccess);
35 }
36 #define VerifyAccess(h, e) VerifyAccess_(h, e, __FILE__, __LINE__)
37
38 static
39 VOID
40 TestCreateOpen_(
41 _In_ ACCESS_MASK DesiredAccess,
42 _In_ ACCESS_MASK ExpectedAccess,
43 _In_ NTSTATUS ExpectedStatus,
44 _In_ PCSTR File,
45 _In_ INT Line)
46 {
47 NTSTATUS Status;
48 HANDLE KeyHandle;
49 UNICODE_STRING KeyName = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software");
50 OBJECT_ATTRIBUTES ObjectAttributes;
51
52 InitializeObjectAttributes(&ObjectAttributes,
53 &KeyName,
54 OBJ_CASE_INSENSITIVE,
55 NULL,
56 NULL);
57 Status = NtCreateKey(&KeyHandle,
58 DesiredAccess,
59 &ObjectAttributes,
60 0,
61 NULL,
62 REG_OPTION_NON_VOLATILE,
63 NULL);
64 ok_(File, Line)(Status == ExpectedStatus,
65 "NtCreateKey returned 0x%lx, expected 0x%lx\n",
66 Status, ExpectedStatus);
67 if (NT_SUCCESS(Status))
68 {
69 VerifyAccess_(KeyHandle, ExpectedAccess, File, Line);
70 Status = NtClose(KeyHandle);
71 ok_(File, Line)(Status == STATUS_SUCCESS,
72 "NtClose from NtCreateKey returned 0x%lx\n",
73 Status);
74 }
75 else if (NT_SUCCESS(ExpectedStatus))
76 {
77 skip_(File, Line)("NtCreateKey failed, skipping\n");
78 }
79
80 Status = NtOpenKey(&KeyHandle,
81 DesiredAccess,
82 &ObjectAttributes);
83 ok_(File, Line)(Status == ExpectedStatus,
84 "NtOpenKey returned 0x%lx, expected 0x%lx\n",
85 Status, ExpectedStatus);
86 if (NT_SUCCESS(Status))
87 {
88 VerifyAccess_(KeyHandle, ExpectedAccess, File, Line);
89 Status = NtClose(KeyHandle);
90 ok_(File, Line)(Status == STATUS_SUCCESS,
91 "NtClose from NtOpenKey returned 0x%lx\n",
92 Status);
93 }
94 else if (NT_SUCCESS(ExpectedStatus))
95 {
96 skip_(File, Line)("NtOpenKey failed, skipping\n");
97 }
98 }
99 #define TestCreateOpen(d, ea, es) TestCreateOpen_(d, ea, es, __FILE__, __LINE__)
100
101 START_TEST(NtCreateKey)
102 {
103 TestCreateOpen(0, 0, STATUS_ACCESS_DENIED);
104 TestCreateOpen(KEY_WOW64_32KEY, 0, STATUS_ACCESS_DENIED);
105 TestCreateOpen(KEY_WOW64_64KEY, 0, STATUS_ACCESS_DENIED);
106 TestCreateOpen(KEY_WOW64_32KEY | KEY_WOW64_64KEY, 0, STATUS_ACCESS_DENIED); // STATUS_INVALID_PARAMETER on Win7
107 TestCreateOpen(READ_CONTROL, READ_CONTROL, STATUS_SUCCESS);
108 TestCreateOpen(READ_CONTROL | KEY_WOW64_32KEY, READ_CONTROL, STATUS_SUCCESS);
109 TestCreateOpen(READ_CONTROL | KEY_WOW64_64KEY, READ_CONTROL, STATUS_SUCCESS);
110 TestCreateOpen(READ_CONTROL | KEY_WOW64_32KEY | KEY_WOW64_64KEY, READ_CONTROL, STATUS_SUCCESS); // STATUS_INVALID_PARAMETER on Win7
111 }