* The Shell.. for a long time we dreamed of having a compatible, properly working...
[reactos.git] / rostests / kmtests / ntos_se / SeHelpers.c
1 /*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite Helper functions for Se tests
5 * PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
6 */
7
8 #include <kmt_test.h>
9 #include "se.h"
10
11 NTSTATUS
12 RtlxAddAuditAccessAceEx(
13 _Inout_ PACL Acl,
14 _In_ ULONG Revision,
15 _In_ ULONG Flags,
16 _In_ ACCESS_MASK AccessMask,
17 _In_ PSID Sid,
18 _In_ BOOLEAN Success,
19 _In_ BOOLEAN Failure)
20 {
21 NTSTATUS Status;
22 USHORT AceSize;
23 PSYSTEM_AUDIT_ACE Ace;
24
25 if (Success) Flags |= SUCCESSFUL_ACCESS_ACE_FLAG;
26 if (Failure) Flags |= FAILED_ACCESS_ACE_FLAG;
27
28 AceSize = FIELD_OFFSET(SYSTEM_AUDIT_ACE, SidStart) + RtlLengthSid(Sid);
29 Ace = ExAllocatePoolWithTag(PagedPool, AceSize, 'cAmK');
30 if (!Ace)
31 return STATUS_INSUFFICIENT_RESOURCES;
32 Ace->Header.AceType = SYSTEM_AUDIT_ACE_TYPE;
33 Ace->Header.AceFlags = Flags;
34 Ace->Header.AceSize = AceSize;
35 Ace->Mask = AccessMask;
36 Status = RtlCopySid(AceSize - FIELD_OFFSET(SYSTEM_AUDIT_ACE, SidStart),
37 (PSID)&Ace->SidStart,
38 Sid);
39 ASSERT(NT_SUCCESS(Status));
40 if (NT_SUCCESS(Status))
41 {
42 Status = RtlAddAce(Acl,
43 Revision,
44 MAXULONG,
45 Ace,
46 AceSize);
47 }
48 ExFreePoolWithTag(Ace, 'cAmK');
49 return Status;
50 }
51
52 VOID
53 CheckSid__(
54 _In_ PSID Sid,
55 _In_ ULONG SidSize,
56 _In_ PISID ExpectedSid,
57 _In_ PCSTR FileAndLine)
58 {
59 BOOLEAN Okay;
60 ULONG Length;
61
62 KmtOk(Sid != NULL, FileAndLine, "Sid is NULL\n");
63 if (KmtSkip(Sid != NULL, FileAndLine, "No Sid\n"))
64 return;
65 if (KmtSkip(SidSize >= sizeof(ULONG), FileAndLine, "Sid too small: %lu\n", SidSize))
66 return;
67 Okay = RtlValidSid(Sid);
68 KmtOk(Okay == TRUE, FileAndLine, "Invalid Sid\n");
69 if (KmtSkip(Okay, FileAndLine, "Invalid Sid\n"))
70 return;
71
72 Length = RtlLengthSid(Sid);
73 KmtOk(SidSize >= Length, FileAndLine, "SidSize %lu too small, need %lu\n", SidSize, Length);
74 if (KmtSkip(SidSize >= Length, FileAndLine, "Sid too small\n"))
75 return;
76 Okay = RtlEqualSid(Sid, ExpectedSid);
77 KmtOk(Okay, FileAndLine, "Sids %p and %p not equal\n", Sid, ExpectedSid);
78 if (!Okay)
79 {
80 WCHAR Buffer1[128];
81 WCHAR Buffer2[128];
82 UNICODE_STRING SidString1, SidString2;
83 RtlInitEmptyUnicodeString(&SidString1, Buffer1, sizeof(Buffer1));
84 RtlInitEmptyUnicodeString(&SidString2, Buffer2, sizeof(Buffer2));
85 (void)RtlConvertSidToUnicodeString(&SidString1, Sid, FALSE);
86 (void)RtlConvertSidToUnicodeString(&SidString2, ExpectedSid, FALSE);
87 KmtOk(0, FileAndLine, "Got %wZ, expected %wZ\n", &SidString1, &SidString2);
88 }
89 }
90
91 VOID
92 VCheckAcl__(
93 _In_ PACL Acl,
94 _In_ ULONG AceCount,
95 _In_ PCSTR FileAndLine,
96 _In_ va_list Arguments)
97 {
98 ULONG i;
99 ULONG Offset;
100 PACE_HEADER AceHeader;
101 INT AceType;
102 INT AceFlags;
103 ACCESS_MASK Mask;
104 PISID Sid;
105 PACCESS_ALLOWED_ACE AllowedAce;
106 PACCESS_DENIED_ACE DeniedAce;
107 PSYSTEM_AUDIT_ACE AuditAce;
108
109 KmtOk(Acl != NULL, FileAndLine, "Acl is NULL\n");
110 if (KmtSkip(Acl != NULL, FileAndLine, "No ACL\n"))
111 return;
112 KmtOk((ULONG_PTR)Acl % sizeof(ULONG) == 0, FileAndLine, "Unaligned ACL %p\n", Acl);
113 KmtOk(Acl->AclRevision == ACL_REVISION, FileAndLine, "AclRevision is %u\n", Acl->AclRevision);
114 KmtOk(Acl->Sbz1 == 0, FileAndLine, "Sbz1 is %u\n", Acl->Sbz1);
115 KmtOk(Acl->Sbz2 == 0, FileAndLine, "Sbz2 is %u\n", Acl->Sbz2);
116 KmtOk(Acl->AclSize >= sizeof(*Acl), FileAndLine, "AclSize too small: %u\n", Acl->AclSize);
117 KmtOk(Acl->AceCount == AceCount, FileAndLine, "AceCount is %u, expected %lu\n", Acl->AceCount, AceCount);
118 Offset = sizeof(*Acl);
119 for (i = 0; i < Acl->AceCount; i++)
120 {
121 KmtOk(Acl->AclSize >= Offset + sizeof(*AceHeader), FileAndLine, "AclSize too small (%u) at Offset %lu, ACE #%lu\n", Acl->AclSize, Offset, i);
122 if (Acl->AclSize < Offset + sizeof(*AceHeader))
123 break;
124 AceHeader = (PACE_HEADER)((PUCHAR)Acl + Offset);
125 KmtOk((ULONG_PTR)AceHeader % sizeof(ULONG) == 0, FileAndLine, "[%lu] Unaligned ACE %p\n", i, AceHeader);
126 KmtOk(AceHeader->AceSize % sizeof(ULONG) == 0, FileAndLine, "[%lu] Unaligned ACE size %u\n", i, AceHeader->AceSize);
127 KmtOk(Acl->AclSize >= Offset + AceHeader->AceSize, FileAndLine, "[%lu] AclSize too small (%u) at Offset %lu\n", i, Acl->AclSize, Offset);
128 if (Acl->AclSize < Offset + AceHeader->AceSize)
129 break;
130 Offset += AceHeader->AceSize;
131 if (i >= AceCount)
132 continue;
133 AceType = va_arg(Arguments, INT);
134 AceFlags = va_arg(Arguments, INT);
135 KmtOk(AceHeader->AceType == AceType, FileAndLine, "[%lu] AceType is %u, expected %u\n", i, AceHeader->AceType, AceType);
136 KmtOk(AceHeader->AceFlags == AceFlags, FileAndLine, "[%lu] AceFlags is 0x%x, expected 0x%x\n", i, AceHeader->AceFlags, AceFlags);
137 if (AceType == ACCESS_ALLOWED_ACE_TYPE)
138 {
139 Sid = va_arg(Arguments, PSID);
140 Mask = va_arg(Arguments, INT);
141 KmtOk(AceHeader->AceSize >= sizeof(*AllowedAce), FileAndLine, "[%lu] AllowedAce AceSize too small: %u\n", i, AceHeader->AceSize);
142 if (AceHeader->AceSize < sizeof(*AllowedAce))
143 continue;
144 AllowedAce = (PACCESS_ALLOWED_ACE)AceHeader;
145 KmtOk(AllowedAce->Mask == Mask, FileAndLine, "[%lu] AllowedAce Mask is 0x%lx, expected 0x%lx\n", i, AllowedAce->Mask, Mask);
146 CheckSid__((PSID)&AllowedAce->SidStart,
147 AceHeader->AceSize - FIELD_OFFSET(ACCESS_ALLOWED_ACE, SidStart),
148 Sid,
149 FileAndLine);
150 }
151 else if (AceType == ACCESS_DENIED_ACE_TYPE)
152 {
153 Sid = va_arg(Arguments, PSID);
154 Mask = va_arg(Arguments, INT);
155 KmtOk(AceHeader->AceSize >= sizeof(*DeniedAce), FileAndLine, "[%lu] DeniedAce AceSize too small: %u\n", i, AceHeader->AceSize);
156 if (AceHeader->AceSize < sizeof(*DeniedAce))
157 continue;
158 DeniedAce = (PACCESS_DENIED_ACE)AceHeader;
159 KmtOk(DeniedAce->Mask == Mask, FileAndLine, "[%lu] DeniedAce Mask is 0x%lx, expected 0x%lx\n", i, DeniedAce->Mask, Mask);
160 CheckSid__((PSID)&DeniedAce->SidStart,
161 AceHeader->AceSize - FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart),
162 Sid,
163 FileAndLine);
164 }
165 else if (AceType == SYSTEM_AUDIT_ACE_TYPE)
166 {
167 Sid = va_arg(Arguments, PSID);
168 Mask = va_arg(Arguments, INT);
169 KmtOk(AceHeader->AceSize >= sizeof(*AuditAce), FileAndLine, "[%lu] AuditAce AceSize too small: %u\n", i, AceHeader->AceSize);
170 if (AceHeader->AceSize < sizeof(*AuditAce))
171 continue;
172 AuditAce = (PSYSTEM_AUDIT_ACE)AceHeader;
173 KmtOk(AuditAce->Mask == Mask, FileAndLine, "[%lu] AuditAce Mask is 0x%lx, expected 0x%lx\n", i, AuditAce->Mask, Mask);
174 CheckSid__((PSID)&AuditAce->SidStart,
175 AceHeader->AceSize - FIELD_OFFSET(ACCESS_DENIED_ACE, SidStart),
176 Sid,
177 FileAndLine);
178 }
179 }
180 }
181
182 VOID
183 CheckAcl__(
184 _In_ PACL Acl,
185 _In_ ULONG AceCount,
186 _In_ PCSTR FileAndLine,
187 ...)
188 {
189 va_list Arguments;
190
191 va_start(Arguments, FileAndLine);
192 VCheckAcl__(Acl, AceCount, FileAndLine, Arguments);
193 va_end(Arguments);
194 }