[KMTESTS/OB]
[reactos.git] / kmtests / ntos_ob / ObType.c
1 /*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: LGPLv2+ - See COPYING.LIB in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite Ob Regressions KM-Test
5 * PROGRAMMER: Aleksey Bragin <aleksey@reactos.org>
6 * Thomas Faber <thfabba@gmx.de>
7 */
8
9 /* TODO: split this into multiple tests! ObLife, ObHandle, ObName, ... */
10
11 #include <kmt_test.h>
12
13 #define NDEBUG
14 #include <debug.h>
15
16 #define CheckObject(Handle, Pointers, Handles) do \
17 { \
18 PUBLIC_OBJECT_BASIC_INFORMATION ObjectInfo; \
19 Status = ZwQueryObject(Handle, ObjectBasicInformation, \
20 &ObjectInfo, sizeof ObjectInfo, NULL); \
21 ok_eq_hex(Status, STATUS_SUCCESS); \
22 ok_eq_ulong(ObjectInfo.PointerCount, Pointers); \
23 ok_eq_ulong(ObjectInfo.HandleCount, Handles); \
24 } while (0)
25
26 #define NUM_OBTYPES 5
27
28 typedef struct _MY_OBJECT1
29 {
30 ULONG Something1;
31 } MY_OBJECT1, *PMY_OBJECT1;
32
33 typedef struct _MY_OBJECT2
34 {
35 ULONG Something1;
36 ULONG SomeLong[10];
37 } MY_OBJECT2, *PMY_OBJECT2;
38
39 static POBJECT_TYPE ObTypes[NUM_OBTYPES];
40 static UNICODE_STRING ObTypeName[NUM_OBTYPES];
41 static UNICODE_STRING ObName[NUM_OBTYPES];
42 static OBJECT_TYPE_INITIALIZER ObTypeInitializer[NUM_OBTYPES];
43 static UNICODE_STRING ObDirectoryName;
44 static OBJECT_ATTRIBUTES ObDirectoryAttributes;
45 static OBJECT_ATTRIBUTES ObAttributes[NUM_OBTYPES];
46 static PVOID ObBody[NUM_OBTYPES];
47 static HANDLE ObHandle1[NUM_OBTYPES];
48 static HANDLE DirectoryHandle;
49
50 typedef struct _COUNTS
51 {
52 USHORT Dump;
53 USHORT Open;
54 USHORT Close;
55 USHORT Delete;
56 USHORT Parse;
57 USHORT OkayToClose;
58 USHORT QueryName;
59 } COUNTS, *PCOUNTS;
60 static COUNTS Counts;
61
62 static
63 VOID
64 NTAPI
65 DumpProc(
66 IN PVOID Object,
67 IN POB_DUMP_CONTROL DumpControl)
68 {
69 DPRINT("DumpProc() called\n");
70 ++Counts.Dump;
71 }
72
73 static
74 NTSTATUS
75 NTAPI
76 OpenProc(
77 IN OB_OPEN_REASON OpenReason,
78 IN PEPROCESS Process,
79 IN PVOID Object,
80 IN ACCESS_MASK GrantedAccess,
81 IN ULONG HandleCount)
82 {
83 DPRINT("OpenProc() 0x%p, OpenReason %d, HandleCount %lu, AccessMask 0x%lX\n",
84 Object, OpenReason, HandleCount, GrantedAccess);
85 ++Counts.Open;
86 return STATUS_SUCCESS;
87 }
88
89 static
90 VOID
91 NTAPI
92 CloseProc(
93 IN PEPROCESS Process,
94 IN PVOID Object,
95 IN ACCESS_MASK GrantedAccess,
96 IN ULONG ProcessHandleCount,
97 IN ULONG SystemHandleCount)
98 {
99 DPRINT("CloseProc() 0x%p, ProcessHandleCount %lu, SystemHandleCount %lu, AccessMask 0x%lX\n",
100 Object, ProcessHandleCount, SystemHandleCount, GrantedAccess);
101 ++Counts.Close;
102 }
103
104 static
105 VOID
106 NTAPI
107 DeleteProc(
108 IN PVOID Object)
109 {
110 DPRINT("DeleteProc() 0x%p\n", Object);
111 ++Counts.Delete;
112 }
113
114 static
115 NTSTATUS
116 NTAPI
117 ParseProc(
118 IN PVOID ParseObject,
119 IN PVOID ObjectType,
120 IN OUT PACCESS_STATE AccessState,
121 IN KPROCESSOR_MODE AccessMode,
122 IN ULONG Attributes,
123 IN OUT PUNICODE_STRING CompleteName,
124 IN OUT PUNICODE_STRING RemainingName,
125 IN OUT PVOID Context OPTIONAL,
126 IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL,
127 OUT PVOID *Object)
128 {
129 DPRINT("ParseProc() called\n");
130 *Object = NULL;
131
132 ++Counts.Parse;
133 return STATUS_OBJECT_NAME_NOT_FOUND;//STATUS_SUCCESS;
134 }
135
136 /* TODO: this does not actually return an NTSTATUS! */
137 static
138 NTSTATUS
139 NTAPI
140 OkayToCloseProc(
141 IN PEPROCESS Process OPTIONAL,
142 IN PVOID Object,
143 IN HANDLE Handle,
144 IN KPROCESSOR_MODE AccessMode)
145 {
146 DPRINT("OkayToCloseProc() 0x%p, Handle 0x%p, AccessMask 0x%lX\n",
147 Object, Handle, AccessMode);
148 ++Counts.OkayToClose;
149 return TRUE;
150 }
151
152 static
153 NTSTATUS
154 NTAPI
155 QueryNameProc(
156 IN PVOID Object,
157 IN BOOLEAN HasObjectName,
158 OUT POBJECT_NAME_INFORMATION ObjectNameInfo,
159 IN ULONG Length,
160 OUT PULONG ReturnLength,
161 IN KPROCESSOR_MODE AccessMode)
162 {
163 DPRINT("QueryNameProc() 0x%p, HasObjectName %d, Len %lu, AccessMask 0x%lX\n",
164 Object, HasObjectName, Length, AccessMode);
165 ++Counts.QueryName;
166
167 ObjectNameInfo = NULL;
168 ReturnLength = 0;
169 return STATUS_OBJECT_NAME_NOT_FOUND;
170 }
171
172 static
173 VOID
174 ObtCreateObjectTypes(VOID)
175 {
176 INT i;
177 NTSTATUS Status;
178 struct
179 {
180 WCHAR DirectoryName[sizeof "\\ObjectTypes\\" - 1];
181 WCHAR TypeName[15];
182 } Name = { L"\\ObjectTypes\\" };
183 OBJECT_ATTRIBUTES ObjectAttributes;
184 HANDLE ObjectTypeHandle;
185 UNICODE_STRING ObjectPath;
186
187 for (i = 0; i < NUM_OBTYPES; ++i)
188 {
189 Status = RtlStringCbPrintfW(Name.TypeName, sizeof Name.TypeName, L"MyObjectType%x", i);
190 ASSERT(NT_SUCCESS(Status));
191 RtlInitUnicodeString(&ObTypeName[i], Name.TypeName);
192 DPRINT("Creating object type %wZ\n", &ObTypeName[i]);
193
194 RtlZeroMemory(&ObTypeInitializer[i], sizeof ObTypeInitializer[i]);
195 ObTypeInitializer[i].Length = sizeof ObTypeInitializer[i];
196 ObTypeInitializer[i].PoolType = NonPagedPool;
197 ObTypeInitializer[i].MaintainHandleCount = TRUE;
198 ObTypeInitializer[i].ValidAccessMask = OBJECT_TYPE_ALL_ACCESS;
199
200 // Test for invalid parameter
201 // FIXME: Make it more exact, to see which params Win2k3 checks
202 // existence of
203 Status = ObCreateObjectType(&ObTypeName[i], &ObTypeInitializer[i], NULL, &ObTypes[i]);
204 ok_eq_hex(Status, STATUS_INVALID_PARAMETER);
205
206 ObTypeInitializer[i].CloseProcedure = CloseProc;
207 ObTypeInitializer[i].DeleteProcedure = DeleteProc;
208 ObTypeInitializer[i].DumpProcedure = DumpProc;
209 ObTypeInitializer[i].OpenProcedure = OpenProc;
210 ObTypeInitializer[i].ParseProcedure = ParseProc;
211 ObTypeInitializer[i].OkayToCloseProcedure = OkayToCloseProc;
212 ObTypeInitializer[i].QueryNameProcedure = QueryNameProc;
213 //ObTypeInitializer[i].SecurityProcedure = SecurityProc;
214
215 Status = ObCreateObjectType(&ObTypeName[i], &ObTypeInitializer[i], NULL, &ObTypes[i]);
216 if (Status == STATUS_OBJECT_NAME_COLLISION)
217 {
218 /* as we cannot delete the object types, get a pointer if they
219 * already exist */
220 RtlInitUnicodeString(&ObjectPath, Name.DirectoryName);
221 InitializeObjectAttributes(&ObjectAttributes, &ObjectPath, OBJ_KERNEL_HANDLE, NULL, NULL);
222 Status = ObOpenObjectByName(&ObjectAttributes, NULL, KernelMode, NULL, 0, NULL, &ObjectTypeHandle);
223 ok_eq_hex(Status, STATUS_SUCCESS);
224 ok(ObjectTypeHandle != NULL, "ObjectTypeHandle = NULL\n");
225 if (!skip(Status == STATUS_SUCCESS && ObjectTypeHandle, "No handle\n"))
226 {
227 Status = ObReferenceObjectByHandle(ObjectTypeHandle, 0, NULL, KernelMode, (PVOID)&ObTypes[i], NULL);
228 ok_eq_hex(Status, STATUS_SUCCESS);
229 if (!skip(Status == STATUS_SUCCESS && ObTypes[i], "blah\n"))
230 {
231 ObTypes[i]->TypeInfo.CloseProcedure = CloseProc;
232 ObTypes[i]->TypeInfo.DeleteProcedure = DeleteProc;
233 ObTypes[i]->TypeInfo.DumpProcedure = DumpProc;
234 ObTypes[i]->TypeInfo.OpenProcedure = OpenProc;
235 ObTypes[i]->TypeInfo.ParseProcedure = ParseProc;
236 ObTypes[i]->TypeInfo.OkayToCloseProcedure = OkayToCloseProc;
237 ObTypes[i]->TypeInfo.QueryNameProcedure = QueryNameProc;
238 }
239 Status = ZwClose(ObjectTypeHandle);
240 }
241 }
242
243 ok_eq_hex(Status, STATUS_SUCCESS);
244 ok(ObTypes[i] != NULL, "ObType = NULL\n");
245 }
246 }
247
248 static
249 VOID
250 ObtCreateDirectory(VOID)
251 {
252 NTSTATUS Status;
253
254 RtlInitUnicodeString(&ObDirectoryName, L"\\ObtDirectory");
255 InitializeObjectAttributes(&ObDirectoryAttributes, &ObDirectoryName, OBJ_KERNEL_HANDLE | OBJ_PERMANENT | OBJ_CASE_INSENSITIVE, NULL, NULL);
256 Status = ZwCreateDirectoryObject(&DirectoryHandle, DELETE, &ObDirectoryAttributes);
257 ok_eq_hex(Status, STATUS_SUCCESS);
258 CheckObject(DirectoryHandle, 3LU, 1LU);
259 }
260
261 #define CheckCounts(OpenCount, CloseCount, DeleteCount, ParseCount, \
262 OkayToCloseCount, QueryNameCount) do \
263 { \
264 ok_eq_uint(Counts.Open, OpenCount); \
265 ok_eq_uint(Counts.Close, CloseCount); \
266 ok_eq_uint(Counts.Delete, DeleteCount); \
267 ok_eq_uint(Counts.Parse, ParseCount); \
268 ok_eq_uint(Counts.OkayToClose, OkayToCloseCount); \
269 ok_eq_uint(Counts.QueryName, QueryNameCount); \
270 } while (0)
271
272 #define SaveCounts(Save) memcpy(&Save, &Counts, sizeof Counts)
273
274 /* TODO: make this the same as NUM_OBTYPES */
275 #define NUM_OBTYPES2 2
276 static
277 VOID
278 ObtCreateObjects(VOID)
279 {
280 NTSTATUS Status;
281 WCHAR Name[NUM_OBTYPES2][MAX_PATH];
282 COUNTS SaveCounts;
283 INT i;
284 ACCESS_MASK Access[NUM_OBTYPES2] = { STANDARD_RIGHTS_ALL, GENERIC_ALL };
285 ULONG ObjectSize[NUM_OBTYPES2] = { sizeof(MY_OBJECT1), sizeof(MY_OBJECT2) };
286
287 // Create two objects
288 for (i = 0; i < NUM_OBTYPES2; ++i)
289 {
290 ASSERT(sizeof Name[i] == MAX_PATH * sizeof(WCHAR));
291 Status = RtlStringCbPrintfW(Name[i], sizeof Name[i], L"\\ObtDirectory\\MyObject%d", i + 1);
292 ASSERT(Status == STATUS_SUCCESS);
293 RtlInitUnicodeString(&ObName[i], Name[i]);
294 InitializeObjectAttributes(&ObAttributes[i], &ObName[i], OBJ_CASE_INSENSITIVE, NULL, NULL);
295 }
296 CheckObject(DirectoryHandle, 3LU, 1LU);
297
298 for (i = 0; i < NUM_OBTYPES2; ++i)
299 {
300 Status = ObCreateObject(KernelMode, ObTypes[i], &ObAttributes[i], KernelMode, NULL, ObjectSize[i], 0L, 0L, &ObBody[i]);
301 ok_eq_hex(Status, STATUS_SUCCESS);
302 }
303
304 SaveCounts(SaveCounts);
305
306 // Insert them
307 for (i = 0; i < NUM_OBTYPES2; ++i)
308 {
309 CheckObject(DirectoryHandle, 3LU + i, 1LU);
310 Status = ObInsertObject(ObBody[i], NULL, Access[i], 0, &ObBody[i], &ObHandle1[i]);
311 ok_eq_hex(Status, STATUS_SUCCESS);
312 ok(ObBody[i] != NULL, "Object body = NULL\n");
313 ok(ObHandle1[i] != NULL, "Handle = NULL\n");
314 CheckObject(ObHandle1[i], 3LU, 1LU);
315 CheckCounts(SaveCounts.Open + 1, SaveCounts.Close, SaveCounts.Delete, SaveCounts.Parse, SaveCounts.OkayToClose, SaveCounts.QueryName);
316 SaveCounts(SaveCounts);
317 CheckObject(DirectoryHandle, 4LU + i, 1LU);
318 }
319
320 //DPRINT1("%d %d %d %d %d %d %d\n", DumpCount, OpenCount, CloseCount, DeleteCount, ParseCount, OkayToCloseCount, QueryNameCount);
321 CheckCounts(SaveCounts.Open, SaveCounts.Close, SaveCounts.Delete, SaveCounts.Parse, SaveCounts.OkayToClose, SaveCounts.QueryName);
322 }
323
324 static
325 VOID
326 ObtClose(
327 BOOLEAN Clean,
328 BOOLEAN AlternativeMethod)
329 {
330 NTSTATUS Status;
331 LONG_PTR Ret;
332 PVOID TypeObject;
333 INT i;
334 UNICODE_STRING ObPathName[NUM_OBTYPES];
335 WCHAR Name[MAX_PATH];
336
337 // Close what we have opened and free what we allocated
338 for (i = 0; i < NUM_OBTYPES2; ++i)
339 {
340 if (!skip(ObBody[i] != NULL, "Nothing to dereference\n"))
341 {
342 if (ObHandle1[i]) CheckObject(ObHandle1[i], 3LU, 1LU);
343 Ret = ObDereferenceObject(ObBody[i]);
344 ok_eq_longptr(Ret, (LONG_PTR)1);
345 if (ObHandle1[i]) CheckObject(ObHandle1[i], 2LU, 1LU);
346 ObBody[i] = NULL;
347 }
348 if (!skip(ObHandle1[i] != NULL, "Nothing to close\n"))
349 {
350 Status = ZwClose(ObHandle1[i]);
351 ok_eq_hex(Status, STATUS_SUCCESS);
352 ObHandle1[i] = NULL;
353 }
354 }
355
356 if (skip(Clean, "Not cleaning up, as requested. Use ObTypeClean to clean up\n"))
357 return;
358
359 // Now we have to get rid of a directory object
360 // Since it is permanent, we have to firstly make it temporary
361 // and only then kill
362 // (this procedure is described in DDK)
363 if (!skip(DirectoryHandle != NULL, "No directory handle\n"))
364 {
365 CheckObject(DirectoryHandle, 3LU, 1LU);
366
367 Status = ZwMakeTemporaryObject(DirectoryHandle);
368 ok_eq_hex(Status, STATUS_SUCCESS);
369 CheckObject(DirectoryHandle, 3LU, 1LU);
370
371 Status = ZwClose(DirectoryHandle);
372 ok_eq_hex(Status, STATUS_SUCCESS);
373 }
374
375 /* we don't delete the object types we created. It makes Windows unstable.
376 * TODO: perhaps make it work in ROS anyway */
377 return;
378 if (!AlternativeMethod)
379 {
380 for (i = 0; i < NUM_OBTYPES; ++i)
381 if (!skip(ObTypes[i] != NULL, "No object type to delete\n"))
382 {
383 Ret = ObDereferenceObject(ObTypes[i]);
384 ok_eq_longptr(Ret, (LONG_PTR)0);
385 ObTypes[i] = NULL;
386 }
387 }
388 else
389 {
390 for (i = 0; i < NUM_OBTYPES; ++i)
391 {
392 if (!skip(ObTypes[i] != NULL, "No object type to delete\n"))
393 {
394 Status = RtlStringCbPrintfW(Name, sizeof Name, L"\\ObjectTypes\\MyObjectType%d", i);
395 RtlInitUnicodeString(&ObPathName[i], Name);
396 Status = ObReferenceObjectByName(&ObPathName[i], OBJ_CASE_INSENSITIVE, NULL, 0L, NULL, KernelMode, NULL, &TypeObject);
397
398 Ret = ObDereferenceObject(TypeObject);
399 ok_eq_longptr(Ret, (LONG_PTR)2);
400 Ret = ObDereferenceObject(TypeObject);
401 ok_eq_longptr(Ret, (LONG_PTR)1);
402 DPRINT("Reference Name %wZ = %p, ObTypes[%d] = %p\n",
403 ObPathName[i], TypeObject, i, ObTypes[i]);
404 ObTypes[i] = NULL;
405 }
406 }
407 }
408 }
409
410 static
411 VOID
412 TestObjectType(
413 IN BOOLEAN Clean)
414 {
415 RtlZeroMemory(&Counts, sizeof Counts);
416
417 ObtCreateObjectTypes();
418 DPRINT("ObtCreateObjectTypes() done\n");
419
420 ObtCreateDirectory();
421 DPRINT("ObtCreateDirectory() done\n");
422
423 if (!skip(ObTypes[0] != NULL, "No object types!\n"))
424 ObtCreateObjects();
425 DPRINT("ObtCreateObjects() done\n");
426
427 ObtClose(Clean, FALSE);
428 }
429
430 START_TEST(ObType)
431 {
432 TestObjectType(TRUE);
433 }
434
435 /* run this to see the objects created in user mode */
436 START_TEST(ObTypeNoClean)
437 {
438 TestObjectType(FALSE);
439 }
440
441 /* run this to clean up after ObTypeNoClean */
442 START_TEST(ObTypeClean)
443 {
444 ObtClose(TRUE, FALSE);
445 }