[NTDLL_APITEST]
[reactos.git] / rostests / apitests / ntdll / NtDeleteKey.c
1 /*
2 * PROJECT: ReactOS API Tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Test for NtDeleteKey
5 */
6
7 #include <apitest.h>
8
9 #define WIN32_NO_STATUS
10 #include <stdio.h>
11 #include <ndk/rtlfuncs.h>
12 #include <ndk/cmfuncs.h>
13 #include <ndk/obfuncs.h>
14 #include <ndk/setypes.h>
15
16 static
17 NTSTATUS
18 CreateRegistryKeyHandle(PHANDLE KeyHandle,
19 ACCESS_MASK AccessMask,
20 PWCHAR RegistryPath)
21 {
22 UNICODE_STRING KeyName;
23 OBJECT_ATTRIBUTES Attributes;
24
25 RtlInitUnicodeString(&KeyName, RegistryPath);
26 InitializeObjectAttributes(&Attributes,
27 &KeyName,
28 OBJ_CASE_INSENSITIVE,
29 NULL,
30 NULL);
31
32 return NtCreateKey(KeyHandle, AccessMask, &Attributes, 0, NULL, REG_OPTION_VOLATILE, 0);
33 }
34
35 START_TEST(NtDeleteKey)
36 {
37 NTSTATUS Status;
38 HANDLE ParentKey, ChildKey, PetKey;
39 WCHAR Buffer[sizeof("\\Registry\\Machine\\Software\\RosTests\\Child\\Pet") + 5];
40 ULONG i;
41
42 /* Create a registry key */
43 Status = CreateRegistryKeyHandle(&ParentKey, KEY_READ | DELETE, L"\\Registry\\Machine\\Software\\RosTests");
44 ok_ntstatus(Status, STATUS_SUCCESS);
45
46 /* Create a children registry key */
47 Status = CreateRegistryKeyHandle(&ChildKey, KEY_READ | DELETE, L"\\Registry\\Machine\\Software\\RosTests\\Child");
48 ok_ntstatus(Status, STATUS_SUCCESS);
49
50 /* Try deleting the parent one */
51 Status = NtDeleteKey(ParentKey);
52 ok_ntstatus(Status, STATUS_CANNOT_DELETE);
53
54 /* See what happens with Child one */
55 Status = NtDeleteKey(ChildKey);
56 ok_ntstatus(Status, STATUS_SUCCESS);
57
58 /* Retry with parent one */
59 Status = NtDeleteKey(ParentKey);
60 ok_ntstatus(Status, STATUS_SUCCESS);
61
62 /* re-retry */
63 Status = NtDeleteKey(ParentKey);
64 ok_ntstatus(Status, STATUS_SUCCESS);
65
66 /* Close everything */
67 NtClose(ChildKey);
68 NtClose(ParentKey);
69
70 /* Same, but this time close the child handle before trying to delete the parent */
71 /* Create a registry key */
72 Status = CreateRegistryKeyHandle(&ParentKey, KEY_READ | DELETE, L"\\Registry\\Machine\\Software\\RosTests");
73 ok_ntstatus(Status, STATUS_SUCCESS);
74
75 /* Create a children registry key */
76 Status = CreateRegistryKeyHandle(&ChildKey, KEY_READ, L"\\Registry\\Machine\\Software\\RosTests\\Child");
77 ok_ntstatus(Status, STATUS_SUCCESS);
78
79 /* Close the Child */
80 NtClose(ChildKey);
81
82 /* Try deleting the parent one */
83 Status = NtDeleteKey(ParentKey);
84 ok_ntstatus(Status, STATUS_CANNOT_DELETE);
85
86 /* See what happens with Child one */
87 Status = CreateRegistryKeyHandle(&ChildKey, DELETE, L"\\Registry\\Machine\\Software\\RosTests\\Child");
88 ok_ntstatus(Status, STATUS_SUCCESS);
89 Status = NtDeleteKey(ChildKey);
90 ok_ntstatus(Status, STATUS_SUCCESS);
91
92 /* Retry with parent one */
93 Status = NtDeleteKey(ParentKey);
94 ok_ntstatus(Status, STATUS_SUCCESS);
95
96 /* Close everything */
97 NtClose(ChildKey);
98 NtClose(ParentKey);
99
100 /* Stress test key creation */
101 Status = CreateRegistryKeyHandle(&ParentKey, KEY_READ | DELETE, L"\\Registry\\Machine\\Software\\RosTests");
102
103 for (i = 0; i <= 9999; i++) {
104 swprintf(Buffer, L"\\Registry\\Machine\\Software\\RosTests\\Child%04d", i);
105 Status = CreateRegistryKeyHandle(&ChildKey, KEY_READ, Buffer);
106 NtClose(ChildKey);
107
108 swprintf(Buffer, L"\\Registry\\Machine\\Software\\RosTests\\Child%04d\\Pet", i);
109 Status = CreateRegistryKeyHandle(&PetKey, KEY_READ, Buffer);
110 NtClose(PetKey);
111 }
112 ok_ntstatus(Status, STATUS_SUCCESS);
113
114 /* Test hive rerooting */
115 Status = CreateRegistryKeyHandle(&PetKey, DELETE, L"\\Registry\\Machine\\Software\\RosTests\\Child5000\\Pet");
116 ok_ntstatus(Status, STATUS_SUCCESS);
117 Status = NtDeleteKey(PetKey);
118 ok_ntstatus(Status, STATUS_SUCCESS);
119
120 NtClose(PetKey);
121
122 Status = CreateRegistryKeyHandle(&ChildKey, DELETE, L"\\Registry\\Machine\\Software\\RosTests\\Child5000");
123 ok_ntstatus(Status, STATUS_SUCCESS);
124 Status = NtDeleteKey(ChildKey);
125 ok_ntstatus(Status, STATUS_SUCCESS);
126
127 /* Test mass key deletion */
128 for (i = 0; i <= 9999; i++) {
129 if (i != 5000) {
130 swprintf(Buffer, L"\\Registry\\Machine\\Software\\RosTests\\Child%04d\\Pet", i);
131 CreateRegistryKeyHandle(&PetKey, DELETE, Buffer);
132 Status = NtDeleteKey(PetKey);
133 NtClose(PetKey);
134
135 swprintf(Buffer, L"\\Registry\\Machine\\Software\\RosTests\\Child%04d", i);
136 CreateRegistryKeyHandle(&ChildKey, DELETE, Buffer);
137 Status = NtDeleteKey(ChildKey);
138 NtClose(ChildKey);
139 }
140 }
141 ok_ntstatus(Status, STATUS_SUCCESS);
142
143 Status = NtDeleteKey(ParentKey);
144 ok_ntstatus(Status, STATUS_SUCCESS);
145
146 /* Close ParentKey */
147 NtClose(ParentKey);
148 }