[KMTestSuite]
[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 IN OUT INT *Flags)
30 {
31 NTSTATUS Status = STATUS_SUCCESS;
32
33 PAGED_CODE();
34
35 UNREFERENCED_PARAMETER(RegistryPath);
36 UNREFERENCED_PARAMETER(Flags);
37
38 *DeviceName = L"IoDriverObject";
39
40 KmtRegisterIrpHandler(IRP_MJ_CREATE, NULL, TestIrpHandler);
41 KmtRegisterIrpHandler(IRP_MJ_CLOSE, NULL, TestIrpHandler);
42
43 TestDriverObject(DriverObject, DriverEntry);
44
45 return Status;
46 }
47
48 VOID
49 TestUnload(
50 IN PDRIVER_OBJECT DriverObject)
51 {
52 PAGED_CODE();
53
54 TestDriverObject(DriverObject, DriverUnload);
55 }
56
57 static
58 NTSTATUS
59 TestIrpHandler(
60 IN PDEVICE_OBJECT DeviceObject,
61 IN PIRP Irp,
62 IN PIO_STACK_LOCATION IoStackLocation)
63 {
64 NTSTATUS Status = STATUS_SUCCESS;
65
66 UNREFERENCED_PARAMETER(IoStackLocation);
67
68 TestDriverObject(DeviceObject->DriverObject, DriverIrp);
69
70 Irp->IoStatus.Status = Status;
71 Irp->IoStatus.Information = 0;
72
73 IoCompleteRequest(Irp, IO_NO_INCREMENT);
74
75 return Status;
76 }
77
78 static
79 VOID
80 TestDriverObject(
81 IN PDRIVER_OBJECT DriverObject,
82 IN DRIVER_STATUS DriverStatus)
83 {
84 BOOLEAN CheckThisDispatchRoutine;
85 PVOID FirstMajorFunc;
86 int i;
87
88 ok(DriverObject->Size == sizeof(DRIVER_OBJECT), "Size does not match, got %x",DriverObject->Size);
89 ok(DriverObject->Type == 4, "Type does not match 4. got %d",DriverObject->Type);
90
91 if (DriverStatus == DriverEntry)
92 {
93 ok(DriverObject->DeviceObject == NULL, "Expected DeviceObject pointer to be 0, got %p",
94 DriverObject->DeviceObject);
95 ok (DriverObject->Flags == DRVO_LEGACY_DRIVER,
96 "Expected Flags to be DRVO_LEGACY_DRIVER, got %lu",
97 DriverObject->Flags);
98 }
99 else if (DriverStatus == DriverIrp)
100 {
101 ok(DriverObject->DeviceObject != NULL, "Expected DeviceObject pointer to non null");
102 ok (DriverObject->Flags == (DRVO_LEGACY_DRIVER | DRVO_INITIALIZED),
103 "Expected Flags to be DRVO_LEGACY_DRIVER | DRVO_INITIALIZED, got %lu",
104 DriverObject->Flags);
105 }
106 else if (DriverStatus == DriverUnload)
107 {
108 ok(DriverObject->DeviceObject != NULL, "Expected DeviceObject pointer to non null");
109 ok (DriverObject->Flags == (DRVO_LEGACY_DRIVER | DRVO_INITIALIZED | DRVO_UNLOAD_INVOKED),
110 "Expected Flags to be DRVO_LEGACY_DRIVER | DRVO_INITIALIZED | DRVO_UNLOAD_INVOKED, got %lu",
111 DriverObject->Flags);
112 }
113 else
114 ASSERT(FALSE);
115
116 /* Select a routine that was not changed */
117 FirstMajorFunc = DriverObject->MajorFunction[1];
118 ok(FirstMajorFunc != 0, "Expected MajorFunction[1] to be non NULL");
119
120 if (!skip(FirstMajorFunc != NULL, "First major function not set!\n"))
121 {
122 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
123 {
124 if (DriverStatus > 0) CheckThisDispatchRoutine = (i > 3) && (i != 14);
125 else CheckThisDispatchRoutine = TRUE;
126
127 if (CheckThisDispatchRoutine)
128 {
129 ok(DriverObject->MajorFunction[i] == FirstMajorFunc, "Expected MajorFunction[%d] to match %p",
130 i, FirstMajorFunc);
131 }
132 }
133 }
134 }