[KMTEST]
[reactos.git] / rostests / drivers / kmtest / kmtest.c
1 /*
2 * Kernel Mode regression Test
3 * Driver Core
4 *
5 * Copyright 2004 Filip Navara <xnavara@volny.cz>
6 *
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.
11 *
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.
16 *
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.
21 */
22
23 /* INCLUDES *******************************************************************/
24
25 #include <ddk/ntddk.h>
26 #include "kmtest.h"
27
28 LONG successes;
29 LONG failures;
30 tls_data glob_data;
31
32 /* PRIVATE FUNCTIONS ***********************************************************/
33 VOID
34 StartTest()
35 {
36 successes = 0;
37 failures = 0;
38 }
39
40 VOID
41 FinishTest(LPSTR TestName)
42 {
43 DbgPrint("%s: %d test executed (0 marked as todo, %d failures), 0 skipped.\n", TestName, successes + failures, failures);
44 }
45
46 void kmtest_set_location(const char* file, int line)
47 {
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;
53 else
54 glob_data.current_file++;
55 glob_data.current_line=line;
56 }
57
58 /*
59 * Checks condition.
60 * Parameters:
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
65 * Return:
66 * 0 if condition does not have the expected value, 1 otherwise
67 */
68 int kmtest_ok(int condition, const char *msg, ... )
69 {
70 va_list valist;
71
72 if (!condition)
73 {
74 if (msg[0])
75 {
76 char string[1024];
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 );
81 va_end(valist);
82 }
83 else
84 {
85 DbgPrint( "%s:%d: Test failed\n",
86 glob_data.current_file, glob_data.current_line );
87 }
88 InterlockedIncrement(&failures);
89 return 0;
90 }
91 else
92 {/*
93 if (report_success)
94 fprintf( stdout, "%s:%d: Test succeeded\n",
95 glob_data.current_file, glob_data.current_line);*/
96 InterlockedIncrement(&successes);
97 }
98 return 1;
99 }
100
101 /* PUBLIC FUNCTIONS ***********************************************************/
102
103 PWCHAR CreateLowerDeviceRegistryKey(PUNICODE_STRING RegistryPath, PWCHAR NewDriver);
104
105 /*
106 * Test Declarations
107 */
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);
121
122 /*
123 * KmtestDispatch
124 */
125 NTSTATUS
126 NTAPI
127 KmtestDispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
128
129 {
130 NTSTATUS Status = STATUS_SUCCESS;
131
132 if (AttachDeviceObject)
133 {
134 IoSkipCurrentIrpStackLocation(Irp);
135 Status = IoCallDriver(AttachDeviceObject, Irp);
136 return Status;
137 }
138
139 Irp->IoStatus.Status = Status;
140 Irp->IoStatus.Information = 0;
141 IoCompleteRequest(Irp, IO_NO_INCREMENT);
142 return Status;
143 }
144
145 /*
146 * KmtestCreateClose
147 */
148 NTSTATUS
149 NTAPI
150 KmtestCreateClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
151 {
152 NTSTATUS Status = STATUS_SUCCESS;
153
154 if (AttachDeviceObject)
155 {
156 IoSkipCurrentIrpStackLocation(Irp);
157 Status = IoCallDriver(AttachDeviceObject, Irp);
158 return Status;
159 }
160
161 /* Do DriverObject Test with Driver Initialized */
162 DriverObjectTest(DeviceObject->DriverObject, 1);
163
164 Irp->IoStatus.Status = STATUS_SUCCESS;
165 Irp->IoStatus.Information=0;
166
167 IoCompleteRequest(Irp, IO_NO_INCREMENT);
168 return STATUS_SUCCESS;
169 }
170
171 /*
172 * KmtestUnload
173 */
174 VOID
175 NTAPI
176 KmtestUnload(IN PDRIVER_OBJECT DriverObject)
177 {
178 UNICODE_STRING DosDeviceString;
179
180 if(AttachDeviceObject)
181 {
182 IoDetachDevice(AttachDeviceObject);
183 }
184
185 /* Do DriverObject Test for Unload */
186 DriverObjectTest(DriverObject, 2);
187
188 if (MainDeviceObject)
189 {
190 RtlInitUnicodeString(&DosDeviceString, L"\\DosDevices\\Kmtest");
191 IoDeleteSymbolicLink(&DosDeviceString);
192
193 IoDeleteDevice(MainDeviceObject);
194 }
195 FinishTest("Driver Tests");
196 }
197
198 /*
199 * DriverEntry
200 */
201 NTSTATUS
202 NTAPI
203 DriverEntry(PDRIVER_OBJECT DriverObject,
204 PUNICODE_STRING RegistryPath)
205 {
206 int i;
207 PWCHAR LowerDriverRegPath;
208
209 DbgPrint("\n===============================================\n");
210 DbgPrint("Kernel Mode Regression Driver Test starting...\n");
211 DbgPrint("===============================================\n");
212
213 MainDeviceObject = NULL;
214 AttachDeviceObject = NULL;
215 ThisDriverObject = DriverObject;
216
217 NtoskrnlExecutiveTests();
218 NtoskrnlKeTests();
219 NtoskrnlIoTests();
220 NtoskrnlObTest();
221 NtoskrnlPoolsTest();
222
223 /* Start the tests for the driver routines */
224 StartTest();
225
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);
230
231 /* Make sure a device object was created */
232 if (MainDeviceObject)
233 {
234 LowerDriverRegPath = CreateLowerDeviceRegistryKey(RegistryPath, L"kmtestassist");
235
236 if (LowerDriverRegPath)
237 {
238 /* Load driver test and load the lower driver */
239 if (ZwLoadTest(DriverObject, RegistryPath, LowerDriverRegPath))
240 {
241 AttachDeviceTest(MainDeviceObject, L"kmtestassists");
242 if (AttachDeviceObject)
243 {
244 LowerDeviceKernelAPITest(MainDeviceObject, FALSE);
245 }
246
247 /* Unload lower driver without detaching from its device */
248 ZwUnloadTest(DriverObject, RegistryPath, LowerDriverRegPath);
249 LowerDeviceKernelAPITest(MainDeviceObject, TRUE);
250 }
251 else
252 {
253 DbgPrint("Failed to load kmtestassist driver\n");
254 }
255 }
256 }
257 else
258 {
259 return STATUS_UNSUCCESSFUL;
260 }
261
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;
265
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;
271
272 return STATUS_SUCCESS;
273 }