[KMTESTS]
[reactos.git] / kmtests / ntos_io / IoDriverObject_drv.c
1 /*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite Driver Object Test Driver
5 * PROGRAMMER: Michael Martin <martinmnet@hotmail.com>
6 * Thomas Faber <thfabba@gmx.de>
7 */
8
9 #include <kmt_test.h>
10
11 //#define NDEBUG
12 #include <debug.h>
13
14 typedef enum
15 {
16 DriverEntry,
17 DriverIrp,
18 DriverUnload
19 } DRIVER_STATUS;
20
21 static KMT_IRP_HANDLER TestIrpHandler;
22 static VOID TestDriverObject(IN PDRIVER_OBJECT DriverObject, IN DRIVER_STATUS DriverStatus);
23
24 NTSTATUS
25 TestEntry(
26 IN PDRIVER_OBJECT DriverObject,
27 IN PCUNICODE_STRING RegistryPath,
28 OUT PCWSTR *DeviceName,
29 OUT INT *Flags)
30 {
31 NTSTATUS Status = STATUS_SUCCESS;
32
33 PAGED_CODE();
34
35 UNREFERENCED_PARAMETER(RegistryPath);
36
37 *DeviceName = L"IoDriverObject";
38
39 KmtRegisterIrpHandler(IRP_MJ_CREATE, NULL, TestIrpHandler);
40 KmtRegisterIrpHandler(IRP_MJ_CLOSE, NULL, TestIrpHandler);
41
42 TestDriverObject(DriverObject, DriverEntry);
43
44 return Status;
45 }
46
47 VOID
48 TestUnload(
49 IN PDRIVER_OBJECT DriverObject)
50 {
51 PAGED_CODE();
52
53 TestDriverObject(DriverObject, DriverUnload);
54 }
55
56 static
57 NTSTATUS
58 TestIrpHandler(
59 IN PDEVICE_OBJECT DeviceObject,
60 IN PIRP Irp,
61 IN PIO_STACK_LOCATION IoStackLocation)
62 {
63 NTSTATUS Status = STATUS_SUCCESS;
64
65 TestDriverObject(DeviceObject->DriverObject, DriverIrp);
66
67 Irp->IoStatus.Status = Status;
68 Irp->IoStatus.Information = 0;
69
70 IoCompleteRequest(Irp, IO_NO_INCREMENT);
71
72 return Status;
73 }
74
75 static
76 VOID
77 TestDriverObject(
78 IN PDRIVER_OBJECT DriverObject,
79 IN DRIVER_STATUS DriverStatus)
80 {
81 BOOLEAN CheckThisDispatchRoutine;
82 PVOID FirstMajorFunc;
83 int i;
84
85 ok(DriverObject->Size == sizeof(DRIVER_OBJECT), "Size does not match, got %x",DriverObject->Size);
86 ok(DriverObject->Type == 4, "Type does not match 4. got %d",DriverObject->Type);
87
88 if (DriverStatus == DriverEntry)
89 {
90 ok(DriverObject->DeviceObject == NULL, "Expected DeviceObject pointer to be 0, got %p",
91 DriverObject->DeviceObject);
92 ok (DriverObject->Flags == DRVO_LEGACY_DRIVER,
93 "Expected Flags to be DRVO_LEGACY_DRIVER, got %lu",
94 DriverObject->Flags);
95 }
96 else if (DriverStatus == DriverIrp)
97 {
98 ok(DriverObject->DeviceObject != NULL, "Expected DeviceObject pointer to non null");
99 ok (DriverObject->Flags == (DRVO_LEGACY_DRIVER | DRVO_INITIALIZED),
100 "Expected Flags to be DRVO_LEGACY_DRIVER | DRVO_INITIALIZED, got %lu",
101 DriverObject->Flags);
102 }
103 else if (DriverStatus == DriverUnload)
104 {
105 ok(DriverObject->DeviceObject != NULL, "Expected DeviceObject pointer to non null");
106 ok (DriverObject->Flags == (DRVO_LEGACY_DRIVER | DRVO_INITIALIZED | DRVO_UNLOAD_INVOKED),
107 "Expected Flags to be DRVO_LEGACY_DRIVER | DRVO_INITIALIZED | DRVO_UNLOAD_INVOKED, got %lu",
108 DriverObject->Flags);
109 }
110 else
111 ASSERT(FALSE);
112
113 /* Select a routine that was not changed */
114 FirstMajorFunc = DriverObject->MajorFunction[1];
115 ok(FirstMajorFunc != 0, "Expected MajorFunction[1] to be non NULL");
116
117 if (!skip(FirstMajorFunc != NULL, "First major function not set!\n"))
118 {
119 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
120 {
121 if (DriverStatus > 0) CheckThisDispatchRoutine = (i > 3) && (i != 14);
122 else CheckThisDispatchRoutine = TRUE;
123
124 if (CheckThisDispatchRoutine)
125 {
126 ok(DriverObject->MajorFunction[i] == FirstMajorFunc, "Expected MajorFunction[%d] to match %p",
127 i, FirstMajorFunc);
128 }
129 }
130 }
131 }