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
NtoskrnlObTest();
110 VOID
NtoskrnlExecutiveTests();
111 VOID
NtoskrnlPoolsTest();
112 VOID
DriverObjectTest(PDRIVER_OBJECT
, int);
113 VOID
DeviceCreateDeleteTest(PDRIVER_OBJECT
);
114 VOID
DeviceObjectTest(PDEVICE_OBJECT
);
115 BOOLEAN
ZwLoadTest(PDRIVER_OBJECT
, PUNICODE_STRING
, PWCHAR
);
116 BOOLEAN
ZwUnloadTest(PDRIVER_OBJECT
, PUNICODE_STRING
, PWCHAR
);
117 BOOLEAN
DetachDeviceTest(PDEVICE_OBJECT
);
118 BOOLEAN
AttachDeviceTest(PDEVICE_OBJECT
, PWCHAR
);
119 VOID
LowerDeviceKernelAPITest(PDEVICE_OBJECT
, BOOLEAN
);
126 KmtestDispatch(IN PDEVICE_OBJECT DeviceObject
, IN PIRP Irp
)
129 NTSTATUS Status
= STATUS_SUCCESS
;
131 if (AttachDeviceObject
)
133 IoSkipCurrentIrpStackLocation(Irp
);
134 Status
= IoCallDriver(AttachDeviceObject
, Irp
);
138 Irp
->IoStatus
.Status
= Status
;
139 Irp
->IoStatus
.Information
= 0;
140 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
149 KmtestCreateClose(IN PDEVICE_OBJECT DeviceObject
, IN PIRP Irp
)
151 NTSTATUS Status
= STATUS_SUCCESS
;
153 if (AttachDeviceObject
)
155 IoSkipCurrentIrpStackLocation(Irp
);
156 Status
= IoCallDriver(AttachDeviceObject
, Irp
);
160 /* Do DriverObject Test with Driver Initialized */
161 DriverObjectTest(DeviceObject
->DriverObject
, 1);
163 Irp
->IoStatus
.Status
= STATUS_SUCCESS
;
164 Irp
->IoStatus
.Information
=0;
166 IoCompleteRequest(Irp
, IO_NO_INCREMENT
);
167 return STATUS_SUCCESS
;
175 KmtestUnload(IN PDRIVER_OBJECT DriverObject
)
177 UNICODE_STRING DosDeviceString
;
179 if(AttachDeviceObject
)
181 IoDetachDevice(AttachDeviceObject
);
184 /* Do DriverObject Test for Unload */
185 DriverObjectTest(DriverObject
, 2);
187 if (MainDeviceObject
)
189 RtlInitUnicodeString(&DosDeviceString
, L
"\\DosDevices\\Kmtest");
190 IoDeleteSymbolicLink(&DosDeviceString
);
192 IoDeleteDevice(MainDeviceObject
);
194 FinishTest("Driver Tests");
202 DriverEntry(PDRIVER_OBJECT DriverObject
,
203 PUNICODE_STRING RegistryPath
)
206 PWCHAR LowerDriverRegPath
;
208 DbgPrint("\n===============================================\n");
209 DbgPrint("Kernel Mode Regression Driver Test starting...\n");
210 DbgPrint("===============================================\n");
212 MainDeviceObject
= NULL
;
213 AttachDeviceObject
= NULL
;
214 ThisDriverObject
= DriverObject
;
216 NtoskrnlExecutiveTests();
221 /* Start the tests for the driver routines */
224 /* Do DriverObject Test for Driver Entry */
225 DriverObjectTest(DriverObject
, 0);
226 /* Create and delete device, on return MainDeviceObject has been created */
227 DeviceCreateDeleteTest(DriverObject
);
229 /* Make sure a device object was created */
230 if (MainDeviceObject
)
232 LowerDriverRegPath
= CreateLowerDeviceRegistryKey(RegistryPath
, L
"kmtestassist");
234 if (LowerDriverRegPath
)
236 /* Load driver test and load the lower driver */
237 if (ZwLoadTest(DriverObject
, RegistryPath
, LowerDriverRegPath
))
239 AttachDeviceTest(MainDeviceObject
, L
"kmtestassists");
240 if (AttachDeviceObject
)
242 LowerDeviceKernelAPITest(MainDeviceObject
, FALSE
);
245 /* Unload lower driver without detaching from its device */
246 ZwUnloadTest(DriverObject
, RegistryPath
, LowerDriverRegPath
);
247 LowerDeviceKernelAPITest(MainDeviceObject
, TRUE
);
251 DbgPrint("Failed to load kmtestassist driver\n");
257 return STATUS_UNSUCCESSFUL
;
260 /* Set all MajorFunctions to NULL to verify that kernel fixes them */
261 for (i
= 1; i
<= IRP_MJ_MAXIMUM_FUNCTION
; i
++)
262 DriverObject
->MajorFunction
[i
] = NULL
;
264 /* Set necessary routines */
265 DriverObject
->DriverUnload
= KmtestUnload
;
266 DriverObject
->MajorFunction
[IRP_MJ_DEVICE_CONTROL
] = KmtestDispatch
;
267 DriverObject
->MajorFunction
[IRP_MJ_CREATE
] = KmtestCreateClose
;
268 DriverObject
->MajorFunction
[IRP_MJ_CLOSE
] = KmtestCreateClose
;
270 return STATUS_SUCCESS
;