[KMTESTS]
[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 static
137 BOOLEAN
138 NTAPI
139 OkayToCloseProc(
140 IN PEPROCESS Process OPTIONAL,
141 IN PVOID Object,
142 IN HANDLE Handle,
143 IN KPROCESSOR_MODE AccessMode)
144 {
145 DPRINT("OkayToCloseProc() 0x%p, Handle 0x%p, AccessMask 0x%lX\n",
146 Object, Handle, AccessMode);
147 ++Counts.OkayToClose;
148 return TRUE;
149 }
150
151 static
152 NTSTATUS
153 NTAPI
154 QueryNameProc(
155 IN PVOID Object,
156 IN BOOLEAN HasObjectName,
157 OUT POBJECT_NAME_INFORMATION ObjectNameInfo,
158 IN ULONG Length,
159 OUT PULONG ReturnLength,
160 IN KPROCESSOR_MODE AccessMode)
161 {
162 DPRINT("QueryNameProc() 0x%p, HasObjectName %d, Len %lu, AccessMask 0x%lX\n",
163 Object, HasObjectName, Length, AccessMode);
164 ++Counts.QueryName;
165
166 ObjectNameInfo = NULL;
167 ReturnLength = 0;
168 return STATUS_OBJECT_NAME_NOT_FOUND;
169 }
170
171 static
172 VOID
173 ObtCreateObjectTypes(VOID)
174 {
175 INT i;
176 NTSTATUS Status;
177 struct
178 {
179 WCHAR DirectoryName[sizeof "\\ObjectTypes\\" - 1];
180 WCHAR TypeName[15];
181 } Name = { L"\\ObjectTypes\\" };
182 OBJECT_ATTRIBUTES ObjectAttributes;
183 HANDLE ObjectTypeHandle;
184 UNICODE_STRING ObjectPath;
185
186 for (i = 0; i < NUM_OBTYPES; ++i)
187 {
188 Status = RtlStringCbPrintfW(Name.TypeName, sizeof Name.TypeName, L"MyObjectType%x", i);
189 ASSERT(NT_SUCCESS(Status));
190 RtlInitUnicodeString(&ObTypeName[i], Name.TypeName);
191 DPRINT("Creating object type %wZ\n", &ObTypeName[i]);
192
193 RtlZeroMemory(&ObTypeInitializer[i], sizeof ObTypeInitializer[i]);
194 ObTypeInitializer[i].Length = sizeof ObTypeInitializer[i];
195 ObTypeInitializer[i].PoolType = NonPagedPool;
196 ObTypeInitializer[i].MaintainHandleCount = TRUE;
197 ObTypeInitializer[i].ValidAccessMask = OBJECT_TYPE_ALL_ACCESS;
198
199 // Test for invalid parameter
200 // FIXME: Make it more exact, to see which params Win2k3 checks
201 // existence of
202 Status = ObCreateObjectType(&ObTypeName[i], &ObTypeInitializer[i], NULL, &ObTypes[i]);
203 ok_eq_hex(Status, STATUS_INVALID_PARAMETER);
204
205 ObTypeInitializer[i].CloseProcedure = CloseProc;
206 ObTypeInitializer[i].DeleteProcedure = DeleteProc;
207 ObTypeInitializer[i].DumpProcedure = DumpProc;
208 ObTypeInitializer[i].OpenProcedure = OpenProc;
209 ObTypeInitializer[i].ParseProcedure = ParseProc;
210 ObTypeInitializer[i].OkayToCloseProcedure = OkayToCloseProc;
211 ObTypeInitializer[i].QueryNameProcedure = QueryNameProc;
212 //ObTypeInitializer[i].SecurityProcedure = SecurityProc;
213
214 Status = ObCreateObjectType(&ObTypeName[i], &ObTypeInitializer[i], NULL, &ObTypes[i]);
215 if (Status == STATUS_OBJECT_NAME_COLLISION)
216 {
217 /* as we cannot delete the object types, get a pointer if they
218 * already exist */
219 RtlInitUnicodeString(&ObjectPath, Name.DirectoryName);
220 InitializeObjectAttributes(&ObjectAttributes, &ObjectPath, OBJ_KERNEL_HANDLE, NULL, NULL);
221 Status = ObOpenObjectByName(&ObjectAttributes, NULL, KernelMode, NULL, 0, NULL, &ObjectTypeHandle);
222 ok_eq_hex(Status, STATUS_SUCCESS);
223 ok(ObjectTypeHandle != NULL, "ObjectTypeHandle = NULL\n");
224 if (!skip(Status == STATUS_SUCCESS && ObjectTypeHandle, "No handle\n"))
225 {
226 Status = ObReferenceObjectByHandle(ObjectTypeHandle, 0, NULL, KernelMode, (PVOID)&ObTypes[i], NULL);
227 ok_eq_hex(Status, STATUS_SUCCESS);
228 if (!skip(Status == STATUS_SUCCESS && ObTypes[i], "blah\n"))
229 {
230 ObTypes[i]->TypeInfo.CloseProcedure = CloseProc;
231 ObTypes[i]->TypeInfo.DeleteProcedure = DeleteProc;
232 ObTypes[i]->TypeInfo.DumpProcedure = DumpProc;
233 ObTypes[i]->TypeInfo.OpenProcedure = OpenProc;
234 ObTypes[i]->TypeInfo.ParseProcedure = ParseProc;
235 ObTypes[i]->TypeInfo.OkayToCloseProcedure = OkayToCloseProc;
236 ObTypes[i]->TypeInfo.QueryNameProcedure = QueryNameProc;
237 }
238 Status = ZwClose(ObjectTypeHandle);
239 }
240 }
241
242 ok_eq_hex(Status, STATUS_SUCCESS);
243 ok(ObTypes[i] != NULL, "ObType = NULL\n");
244 }
245 }
246
247 static
248 VOID
249 ObtCreateDirectory(VOID)
250 {
251 NTSTATUS Status;
252
253 RtlInitUnicodeString(&ObDirectoryName, L"\\ObtDirectory");
254 InitializeObjectAttributes(&ObDirectoryAttributes, &ObDirectoryName, OBJ_KERNEL_HANDLE | OBJ_PERMANENT | OBJ_CASE_INSENSITIVE, NULL, NULL);
255 Status = ZwCreateDirectoryObject(&DirectoryHandle, DELETE, &ObDirectoryAttributes);
256 ok_eq_hex(Status, STATUS_SUCCESS);
257 CheckObject(DirectoryHandle, 3LU, 1LU);
258 }
259
260 #define CheckCounts(OpenCount, CloseCount, DeleteCount, ParseCount, \
261 OkayToCloseCount, QueryNameCount) do \
262 { \
263 ok_eq_uint(Counts.Open, OpenCount); \
264 ok_eq_uint(Counts.Close, CloseCount); \
265 ok_eq_uint(Counts.Delete, DeleteCount); \
266 ok_eq_uint(Counts.Parse, ParseCount); \
267 ok_eq_uint(Counts.OkayToClose, OkayToCloseCount); \
268 ok_eq_uint(Counts.QueryName, QueryNameCount); \
269 } while (0)
270
271 #define SaveCounts(Save) memcpy(&Save, &Counts, sizeof Counts)
272
273 /* TODO: make this the same as NUM_OBTYPES */
274 #define NUM_OBTYPES2 2
275 static
276 VOID
277 ObtCreateObjects(VOID)
278 {
279 NTSTATUS Status;
280 WCHAR Name[NUM_OBTYPES2][MAX_PATH];
281 COUNTS SaveCounts;
282 INT i;
283 ACCESS_MASK Access[NUM_OBTYPES2] = { STANDARD_RIGHTS_ALL, GENERIC_ALL };
284 ULONG ObjectSize[NUM_OBTYPES2] = { sizeof(MY_OBJECT1), sizeof(MY_OBJECT2) };
285
286 // Create two objects
287 for (i = 0; i < NUM_OBTYPES2; ++i)
288 {
289 ASSERT(sizeof Name[i] == MAX_PATH * sizeof(WCHAR));
290 Status = RtlStringCbPrintfW(Name[i], sizeof Name[i], L"\\ObtDirectory\\MyObject%d", i + 1);
291 ASSERT(Status == STATUS_SUCCESS);
292 RtlInitUnicodeString(&ObName[i], Name[i]);
293 InitializeObjectAttributes(&ObAttributes[i], &ObName[i], OBJ_CASE_INSENSITIVE, NULL, NULL);
294 }
295 CheckObject(DirectoryHandle, 3LU, 1LU);
296
297 for (i = 0; i < NUM_OBTYPES2; ++i)
298 {
299 Status = ObCreateObject(KernelMode, ObTypes[i], &ObAttributes[i], KernelMode, NULL, ObjectSize[i], 0L, 0L, &ObBody[i]);
300 ok_eq_hex(Status, STATUS_SUCCESS);
301 }
302
303 SaveCounts(SaveCounts);
304
305 // Insert them
306 for (i = 0; i < NUM_OBTYPES2; ++i)
307 {
308 CheckObject(DirectoryHandle, 3LU + i, 1LU);
309 Status = ObInsertObject(ObBody[i], NULL, Access[i], 0, &ObBody[i], &ObHandle1[i]);
310 ok_eq_hex(Status, STATUS_SUCCESS);
311 ok(ObBody[i] != NULL, "Object body = NULL\n");
312 ok(ObHandle1[i] != NULL, "Handle = NULL\n");
313 CheckObject(ObHandle1[i], 3LU, 1LU);
314 CheckCounts(SaveCounts.Open + 1, SaveCounts.Close, SaveCounts.Delete, SaveCounts.Parse, SaveCounts.OkayToClose, SaveCounts.QueryName);
315 SaveCounts(SaveCounts);
316 CheckObject(DirectoryHandle, 4LU + i, 1LU);
317 }
318
319 //DPRINT1("%d %d %d %d %d %d %d\n", DumpCount, OpenCount, CloseCount, DeleteCount, ParseCount, OkayToCloseCount, QueryNameCount);
320 CheckCounts(SaveCounts.Open, SaveCounts.Close, SaveCounts.Delete, SaveCounts.Parse, SaveCounts.OkayToClose, SaveCounts.QueryName);
321 }
322
323 static
324 VOID
325 ObtClose(
326 BOOLEAN Clean,
327 BOOLEAN AlternativeMethod)
328 {
329 NTSTATUS Status;
330 LONG_PTR Ret;
331 PVOID TypeObject;
332 INT i;
333 UNICODE_STRING ObPathName[NUM_OBTYPES];
334 WCHAR Name[MAX_PATH];
335
336 // Close what we have opened and free what we allocated
337 for (i = 0; i < NUM_OBTYPES2; ++i)
338 {
339 if (!skip(ObBody[i] != NULL, "Nothing to dereference\n"))
340 {
341 if (ObHandle1[i]) CheckObject(ObHandle1[i], 3LU, 1LU);
342 Ret = ObDereferenceObject(ObBody[i]);
343 ok_eq_longptr(Ret, (LONG_PTR)1);
344 if (ObHandle1[i]) CheckObject(ObHandle1[i], 2LU, 1LU);
345 ObBody[i] = NULL;
346 }
347 if (!skip(ObHandle1[i] != NULL, "Nothing to close\n"))
348 {
349 Status = ZwClose(ObHandle1[i]);
350 ok_eq_hex(Status, STATUS_SUCCESS);
351 ObHandle1[i] = NULL;
352 }
353 }
354
355 if (skip(Clean, "Not cleaning up, as requested. Use ObTypeClean to clean up\n"))
356 return;
357
358 // Now we have to get rid of a directory object
359 // Since it is permanent, we have to firstly make it temporary
360 // and only then kill
361 // (this procedure is described in DDK)
362 if (!skip(DirectoryHandle != NULL, "No directory handle\n"))
363 {
364 CheckObject(DirectoryHandle, 3LU, 1LU);
365
366 Status = ZwMakeTemporaryObject(DirectoryHandle);
367 ok_eq_hex(Status, STATUS_SUCCESS);
368 CheckObject(DirectoryHandle, 3LU, 1LU);
369
370 Status = ZwClose(DirectoryHandle);
371 ok_eq_hex(Status, STATUS_SUCCESS);
372 }
373
374 /* we don't delete the object types we created. It makes Windows unstable.
375 * TODO: perhaps make it work in ROS anyway */
376 return;
377 if (!AlternativeMethod)
378 {
379 for (i = 0; i < NUM_OBTYPES; ++i)
380 if (!skip(ObTypes[i] != NULL, "No object type to delete\n"))
381 {
382 Ret = ObDereferenceObject(ObTypes[i]);
383 ok_eq_longptr(Ret, (LONG_PTR)0);
384 ObTypes[i] = NULL;
385 }
386 }
387 else
388 {
389 for (i = 0; i < NUM_OBTYPES; ++i)
390 {
391 if (!skip(ObTypes[i] != NULL, "No object type to delete\n"))
392 {
393 Status = RtlStringCbPrintfW(Name, sizeof Name, L"\\ObjectTypes\\MyObjectType%d", i);
394 RtlInitUnicodeString(&ObPathName[i], Name);
395 Status = ObReferenceObjectByName(&ObPathName[i], OBJ_CASE_INSENSITIVE, NULL, 0L, NULL, KernelMode, NULL, &TypeObject);
396
397 Ret = ObDereferenceObject(TypeObject);
398 ok_eq_longptr(Ret, (LONG_PTR)2);
399 Ret = ObDereferenceObject(TypeObject);
400 ok_eq_longptr(Ret, (LONG_PTR)1);
401 DPRINT("Reference Name %wZ = %p, ObTypes[%d] = %p\n",
402 ObPathName[i], TypeObject, i, ObTypes[i]);
403 ObTypes[i] = NULL;
404 }
405 }
406 }
407 }
408
409 static
410 VOID
411 TestObjectType(
412 IN BOOLEAN Clean)
413 {
414 RtlZeroMemory(&Counts, sizeof Counts);
415
416 ObtCreateObjectTypes();
417 DPRINT("ObtCreateObjectTypes() done\n");
418
419 ObtCreateDirectory();
420 DPRINT("ObtCreateDirectory() done\n");
421
422 if (!skip(ObTypes[0] != NULL, "No object types!\n"))
423 ObtCreateObjects();
424 DPRINT("ObtCreateObjects() done\n");
425
426 ObtClose(Clean, FALSE);
427 }
428
429 START_TEST(ObType)
430 {
431 TestObjectType(TRUE);
432 }
433
434 /* run this to see the objects created in user mode */
435 START_TEST(ObTypeNoClean)
436 {
437 TestObjectType(FALSE);
438 }
439
440 /* run this to clean up after ObTypeNoClean */
441 START_TEST(ObTypeClean)
442 {
443 ObtClose(TRUE, FALSE);
444 }