Merge HAL changes 34743, 34812, 34839, 34917, 35515, 35771, 35902, 35904,
[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 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);
120
121 /*
122 * KmtestDispatch
123 */
124 NTSTATUS
125 NTAPI
126 KmtestDispatch(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
127
128 {
129 NTSTATUS Status = STATUS_SUCCESS;
130
131 if (AttachDeviceObject)
132 {
133 IoSkipCurrentIrpStackLocation(Irp);
134 Status = IoCallDriver(AttachDeviceObject, Irp);
135 return Status;
136 }
137
138 Irp->IoStatus.Status = Status;
139 Irp->IoStatus.Information = 0;
140 IoCompleteRequest(Irp, IO_NO_INCREMENT);
141 return Status;
142 }
143
144 /*
145 * KmtestCreateClose
146 */
147 NTSTATUS
148 NTAPI
149 KmtestCreateClose(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp)
150 {
151 NTSTATUS Status = STATUS_SUCCESS;
152
153 if (AttachDeviceObject)
154 {
155 IoSkipCurrentIrpStackLocation(Irp);
156 Status = IoCallDriver(AttachDeviceObject, Irp);
157 return Status;
158 }
159
160 /* Do DriverObject Test with Driver Initialized */
161 DriverObjectTest(DeviceObject->DriverObject, 1);
162
163 Irp->IoStatus.Status = STATUS_SUCCESS;
164 Irp->IoStatus.Information=0;
165
166 IoCompleteRequest(Irp, IO_NO_INCREMENT);
167 return STATUS_SUCCESS;
168 }
169
170 /*
171 * KmtestUnload
172 */
173 VOID
174 NTAPI
175 KmtestUnload(IN PDRIVER_OBJECT DriverObject)
176 {
177 UNICODE_STRING DosDeviceString;
178
179 if(AttachDeviceObject)
180 {
181 IoDetachDevice(AttachDeviceObject);
182 }
183
184 /* Do DriverObject Test for Unload */
185 DriverObjectTest(DriverObject, 2);
186
187 if (MainDeviceObject)
188 {
189 RtlInitUnicodeString(&DosDeviceString, L"\\DosDevices\\Kmtest");
190 IoDeleteSymbolicLink(&DosDeviceString);
191
192 IoDeleteDevice(MainDeviceObject);
193 }
194 FinishTest("Driver Tests");
195 }
196
197 /*
198 * DriverEntry
199 */
200 NTSTATUS
201 NTAPI
202 DriverEntry(PDRIVER_OBJECT DriverObject,
203 PUNICODE_STRING RegistryPath)
204 {
205 int i;
206 PWCHAR LowerDriverRegPath;
207
208 DbgPrint("\n===============================================\n");
209 DbgPrint("Kernel Mode Regression Driver Test starting...\n");
210 DbgPrint("===============================================\n");
211
212 MainDeviceObject = NULL;
213 AttachDeviceObject = NULL;
214 ThisDriverObject = DriverObject;
215
216 NtoskrnlExecutiveTests();
217 NtoskrnlIoTests();
218 NtoskrnlObTest();
219 NtoskrnlPoolsTest();
220
221 /* Start the tests for the driver routines */
222 StartTest();
223
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);
228
229 /* Make sure a device object was created */
230 if (MainDeviceObject)
231 {
232 LowerDriverRegPath = CreateLowerDeviceRegistryKey(RegistryPath, L"kmtestassist");
233
234 if (LowerDriverRegPath)
235 {
236 /* Load driver test and load the lower driver */
237 if (ZwLoadTest(DriverObject, RegistryPath, LowerDriverRegPath))
238 {
239 AttachDeviceTest(MainDeviceObject, L"kmtestassists");
240 if (AttachDeviceObject)
241 {
242 LowerDeviceKernelAPITest(MainDeviceObject, FALSE);
243 }
244
245 /* Unload lower driver without detaching from its device */
246 ZwUnloadTest(DriverObject, RegistryPath, LowerDriverRegPath);
247 LowerDeviceKernelAPITest(MainDeviceObject, TRUE);
248 }
249 else
250 {
251 DbgPrint("Failed to load kmtestassist driver\n");
252 }
253 }
254 }
255 else
256 {
257 return STATUS_UNSUCCESSFUL;
258 }
259
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;
263
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;
269
270 return STATUS_SUCCESS;
271 }