[KMTEST]
[reactos.git] / rostests / drivers / kmtest / drvobj_test.c
1 /*
2 * Driver Regression Tests
3 *
4 * Copyright 2009 Michael Martin <martinmnet@hotmail.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; see the file COPYING.LIB.
18 * If not, write to the Free Software Foundation,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 /* INCLUDES *******************************************************************/
23
24 #include "kmtest.h"
25 #include <ddk/ntddk.h>
26 #include <ddk/ntifs.h>
27
28 VOID DriverObjectTest(PDRIVER_OBJECT DriverObject, int DriverStatus)
29 {
30 BOOLEAN CheckThisDispatchRoutine;
31 PVOID FirstMajorFunc;
32 int i;
33
34 ok(DriverObject->Size == sizeof(DRIVER_OBJECT), "Size does not match, got %x",DriverObject->Size);
35 ok(DriverObject->Type == 4, "Type does not match 4. got %d",DriverObject->Type);
36
37 if (DriverStatus == 0)
38 {
39 ok(DriverObject->DeviceObject == NULL, "Expected DeviceObject pointer to be 0, got %p",
40 DriverObject->DeviceObject);
41 ok (DriverObject->Flags == DRVO_LEGACY_DRIVER,
42 "Expected Flags to be DRVO_LEGACY_DRIVER, got %lu",
43 DriverObject->Flags);
44 }
45 else if (DriverStatus == 1)
46 {
47 ok(DriverObject->DeviceObject != NULL, "Expected DeviceObject pointer to non null");
48 ok (DriverObject->Flags == (DRVO_LEGACY_DRIVER | DRVO_INITIALIZED),
49 "Expected Flags to be DRVO_LEGACY_DRIVER | DRVO_INITIALIZED, got %lu",
50 DriverObject->Flags);
51 }
52 else
53 {
54 ok(DriverObject->DeviceObject != NULL, "Expected DeviceObject pointer to non null");
55 ok (DriverObject->Flags == (DRVO_LEGACY_DRIVER | DRVO_INITIALIZED | DRVO_UNLOAD_INVOKED),
56 "Expected Flags to be DRVO_LEGACY_DRIVER | DRVO_INITIALIZED | DRVO_UNLOAD_INVOKED, got %lu",
57 DriverObject->Flags);
58 }
59
60 /* Select a routine that was not changed */
61 FirstMajorFunc = DriverObject->MajorFunction[1];
62 ok(FirstMajorFunc != 0, "Expected MajorFunction[1] to be non NULL");
63
64 if (FirstMajorFunc)
65 {
66 for (i = 0; i <= IRP_MJ_MAXIMUM_FUNCTION; i++)
67 {
68 if (DriverStatus > 0) CheckThisDispatchRoutine = (i > 3) && (i != 14);
69 else CheckThisDispatchRoutine = TRUE;
70
71 if (CheckThisDispatchRoutine)
72 {
73 ok(DriverObject->MajorFunction[i] == FirstMajorFunc, "Expected MajorFunction[%d] to match %p",
74 i, FirstMajorFunc);
75 }
76 }
77 }
78 else
79 {
80 ok(TRUE, "Skipped testing for all MajorFunction");
81 }
82 }
83
84 BOOLEAN ZwLoadTest(PDRIVER_OBJECT DriverObject, PUNICODE_STRING DriverRegistryPath, PWCHAR NewDriverRegPath)
85 {
86 UNICODE_STRING RegPath;
87 NTSTATUS Status;
88
89 /* Try to load ourself */
90 Status = ZwLoadDriver(DriverRegistryPath);
91 ok (Status == STATUS_IMAGE_ALREADY_LOADED, "Expected NTSTATUS STATUS_IMAGE_ALREADY_LOADED, got 0x%lX", Status);
92
93 if (Status != STATUS_IMAGE_ALREADY_LOADED)
94 {
95 DbgPrint("WARNING: Loading this a second time will cause BUGCHECK!\n");
96 }
97
98 /* Try to load with a Registry Path that doesnt exist */
99 RtlInitUnicodeString(&RegPath, L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\deadbeef");
100 Status = ZwLoadDriver(&RegPath);
101 ok (Status == STATUS_OBJECT_NAME_NOT_FOUND, "Expected NTSTATUS STATUS_OBJECT_NAME_NOT_FOUND, got 0x%lX", Status);
102
103 /* Load the driver */
104 RtlInitUnicodeString(&RegPath, NewDriverRegPath);
105 Status = ZwLoadDriver(&RegPath);
106 ok(Status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got 0x%lX", Status);
107
108 if (!NT_SUCCESS(Status))
109 {
110 return FALSE;
111 }
112
113 return TRUE;
114 }
115
116 BOOLEAN ZwUnloadTest(PDRIVER_OBJECT DriverObject, PUNICODE_STRING DriverRegistryPath, PWCHAR NewDriverRegPath)
117 {
118 UNICODE_STRING RegPath;
119 NTSTATUS Status;
120
121 /* Try to unload ourself, which should fail as our Unload routine hasnt been set yet. */
122 Status = ZwUnloadDriver(DriverRegistryPath);
123 ok (Status == STATUS_INVALID_DEVICE_REQUEST, "Expected NTSTATUS STATUS_INVALID_DEVICE_REQUEST, got 0x%lX", Status);
124
125 /* Try to unload with a Registry Path that doesnt exist */
126 RtlInitUnicodeString(&RegPath, L"\\Registry\\Machine\\System\\CurrentControlSet\\Services\\deadbeef");
127 Status = ZwUnloadDriver(&RegPath);
128 ok (Status == STATUS_OBJECT_NAME_NOT_FOUND, "Expected NTSTATUS STATUS_OBJECT_NAME_NOT_FOUND, got 0x%lX", Status);
129
130 /* Unload the driver */
131 RtlInitUnicodeString(&RegPath, NewDriverRegPath);
132 Status = ZwUnloadDriver(&RegPath);
133 ok(Status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got 0x%lX", Status);
134
135 if (!NT_SUCCESS(Status))
136 {
137 return FALSE;
138 }
139
140 return TRUE;
141 }