2 * Kernel Mode regression Test
5 * Copyright 2004 Filip Navara <xnavara@volny.cz>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; see the file COPYING.LIB.
19 * If not, write to the Free Software Foundation,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 /* INCLUDES *******************************************************************/
25 #include <ddk/ntddk.h>
32 /* PRIVATE FUNCTIONS ***********************************************************/
41 FinishTest(LPSTR TestName
)
43 DbgPrint("%s: %d test executed (0 marked as todo, %d failures), 0 skipped.\n", TestName
, successes
+ failures
, failures
);
46 void kmtest_set_location(const char* file
, int line
)
48 glob_data
.current_file
=strrchr(file
,'/');
49 if (glob_data
.current_file
==NULL
)
50 glob_data
.current_file
=strrchr(file
,'\\');
51 if (glob_data
.current_file
==NULL
)
52 glob_data
.current_file
=file
;
54 glob_data
.current_file
++;
55 glob_data
.current_line
=line
;
61 * - condition - condition to check;
62 * - msg test description;
63 * - file - test application source code file name of the check
64 * - line - test application source code file line number of the check
66 * 0 if condition does not have the expected value, 1 otherwise
68 int kmtest_ok(int condition
, const char *msg
, ... )
77 va_start(valist
, msg
);
78 vsprintf(string
, msg
, valist
);
79 DbgPrint( "%s:%d: Test failed: %s\n",
80 glob_data
.current_file
, glob_data
.current_line
, string
);
85 DbgPrint( "%s:%d: Test failed\n",
86 glob_data
.current_file
, glob_data
.current_line
);
88 InterlockedIncrement(&failures
);
94 fprintf( stdout, "%s:%d: Test succeeded\n",
95 glob_data.current_file, glob_data.current_line);*/
96 InterlockedIncrement(&successes
);
101 /* PUBLIC FUNCTIONS ***********************************************************/
103 PWCHAR
CreateLowerDeviceRegistryKey(PUNICODE_STRING RegistryPath
, PWCHAR NewDriver
);
108 VOID
NtoskrnlIoTests();
109 VOID
NtoskrnlKeTests();
110 VOID
NtoskrnlObTest();
111 VOID
NtoskrnlExecutiveTests();
112 VOID
NtoskrnlPoolsTest();
113 VOID
DriverObjectTest(PDRIVER_OBJECT
, int);
114 VOID
DeviceCreateDeleteTest(PDRIVER_OBJECT
);
115 VOID
DeviceObjectTest(PDEVICE_OBJECT
);
116 BOOLEAN
ZwLoadTest(PDRIVER_OBJECT
, PUNICODE_STRING
, PWCHAR
);
117 BOOLEAN
ZwUnloadTest(PDRIVER_OBJECT
, PUNICODE_STRING
, PWCHAR
);
118 BOOLEAN
DetachDeviceTest(PDEVICE_OBJECT
);
119 BOOLEAN
AttachDeviceTest(PDEVICE_OBJECT
, PWCHAR
);
120 VOID
LowerDeviceKernelAPITest(PDEVICE_OBJECT
, BOOLEAN
);
127 KmtestDispatch(IN PDEVICE_OBJECT DeviceObject
, IN PIRP Irp
)
130 NTSTATUS Status
= STATUS_SUCCESS
;
132 if (AttachDeviceObject
)
134 IoSkipCurrentIrpStackLocation(Irp
);
135 Status
= IoCallDriver(AttachDeviceObject
, Irp
);
139 Irp
->IoStatus
.Status
= Status
;
140 Irp
->IoStatus
.Information
= 0;
141 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
150 KmtestCreateClose(IN PDEVICE_OBJECT DeviceObject
, IN PIRP Irp
)
152 NTSTATUS Status
= STATUS_SUCCESS
;
154 if (AttachDeviceObject
)
156 IoSkipCurrentIrpStackLocation(Irp
);
157 Status
= IoCallDriver(AttachDeviceObject
, Irp
);
161 /* Do DriverObject Test with Driver Initialized */
162 DriverObjectTest(DeviceObject
->DriverObject
, 1);
164 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
165 Irp
->IoStatus
.Information
=0;
167 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
168 return STATUS_SUCCESS
;
176 KmtestUnload(IN PDRIVER_OBJECT DriverObject
)
178 UNICODE_STRING DosDeviceString
;
180 if(AttachDeviceObject
)
182 IoDetachDevice(AttachDeviceObject
);
185 /* Do DriverObject Test for Unload */
186 DriverObjectTest(DriverObject
, 2);
188 if (MainDeviceObject
)
190 RtlInitUnicodeString(&DosDeviceString
, L
"\\DosDevices\\Kmtest");
191 IoDeleteSymbolicLink(&DosDeviceString
);
193 IoDeleteDevice(MainDeviceObject
);
195 FinishTest("Driver Tests");
203 DriverEntry(PDRIVER_OBJECT DriverObject
,
204 PUNICODE_STRING RegistryPath
)
207 PWCHAR LowerDriverRegPath
;
209 DbgPrint("\n===============================================\n");
210 DbgPrint("Kernel Mode Regression Driver Test starting...\n");
211 DbgPrint("===============================================\n");
213 MainDeviceObject
= NULL
;
214 AttachDeviceObject
= NULL
;
215 ThisDriverObject
= DriverObject
;
217 NtoskrnlExecutiveTests();
223 /* Start the tests for the driver routines */
226 /* Do DriverObject Test for Driver Entry */
227 DriverObjectTest(DriverObject
, 0);
228 /* Create and delete device, on return MainDeviceObject has been created */
229 DeviceCreateDeleteTest(DriverObject
);
231 /* Make sure a device object was created */
232 if (MainDeviceObject
)
234 LowerDriverRegPath
= CreateLowerDeviceRegistryKey(RegistryPath
, L
"kmtestassist");
236 if (LowerDriverRegPath
)
238 /* Load driver test and load the lower driver */
239 if (ZwLoadTest(DriverObject
, RegistryPath
, LowerDriverRegPath
))
241 AttachDeviceTest(MainDeviceObject
, L
"kmtestassists");
242 if (AttachDeviceObject
)
244 LowerDeviceKernelAPITest(MainDeviceObject
, FALSE
);
247 /* Unload lower driver without detaching from its device */
248 ZwUnloadTest(DriverObject
, RegistryPath
, LowerDriverRegPath
);
249 LowerDeviceKernelAPITest(MainDeviceObject
, TRUE
);
253 DbgPrint("Failed to load kmtestassist driver\n");
259 return STATUS_UNSUCCESSFUL
;
262 /* Set all MajorFunctions to NULL to verify that kernel fixes them */
263 for (i
= 1; i
<= IRP_MJ_MAXIMUM_FUNCTION
; i
++)
264 DriverObject
->MajorFunction
[i
] = NULL
;
266 /* Set necessary routines */
267 DriverObject
->DriverUnload
= KmtestUnload
;
268 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = KmtestDispatch
;
269 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = KmtestCreateClose
;
270 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = KmtestCreateClose
;
272 return STATUS_SUCCESS
;