Merge from amd64 branch:
[reactos.git] / rostests / drivers / kmtest / devobj_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 #include "ntddser.h"
28 #include "ntndk.h"
29
30 VOID LowerDeviceKernelAPITest(PDEVICE_OBJECT DeviceObject, BOOLEAN UnLoading)
31 {
32 PDEVICE_OBJECT RetObject;
33
34 RetObject = IoGetLowerDeviceObject(DeviceObject);
35
36 if (UnLoading)
37 {
38 ok(RetObject == 0,
39 "Expected no Lower DeviceObject, got %p", RetObject);
40 }
41 else
42 {
43 ok(RetObject == AttachDeviceObject,
44 "Expected an Attached DeviceObject %p, got %p", AttachDeviceObject, RetObject);
45 }
46
47 if (RetObject)
48 {
49 ObDereferenceObject(RetObject);
50 }
51
52 RetObject = IoGetDeviceAttachmentBaseRef(DeviceObject);
53 ok(RetObject == DeviceObject,
54 "Expected an Attached DeviceObject %p, got %p", DeviceObject, RetObject);
55
56 if (RetObject)
57 {
58 ObDereferenceObject(RetObject);
59 }
60
61 }
62 VOID DeviceCreatedTest(PDEVICE_OBJECT DeviceObject, BOOLEAN ExclusiveAccess)
63 {
64 PEXTENDED_DEVOBJ_EXTENSION extdev;
65
66 /*Check the device object members */
67 ok(DeviceObject->Type==3, "Expected Type = 3, got %x", DeviceObject->Type);
68 ok(DeviceObject->Size = 0xb8, "Expected Size = 0xba, got %x", DeviceObject->Size);
69 ok(DeviceObject->ReferenceCount == 0, "Expected ReferenceCount = 0, got %lu",
70 DeviceObject->ReferenceCount);
71 ok(DeviceObject->DriverObject == ThisDriverObject,
72 "Expected DriverObject member to match this DriverObject %p, got %p",
73 ThisDriverObject, DeviceObject->DriverObject);
74 ok(DeviceObject->NextDevice == NULL, "Expected NextDevice to be NULL, got %p", DeviceObject->NextDevice);
75 ok(DeviceObject->AttachedDevice == NULL, "Expected AttachDevice to be NULL, got %p", DeviceObject->AttachedDevice);
76 ok(DeviceObject->Characteristics == 0, "Expected Characteristics to be 0");
77 if (ExclusiveAccess)
78 {
79 ok((DeviceObject->Flags == (DO_DEVICE_HAS_NAME | DO_DEVICE_INITIALIZING | DO_EXCLUSIVE)),
80 "Expected Flags DO_DEVICE_HAS_NAME | DO_DEVICE_INITIALIZING | DO_EXCLUSIVE, got %lu", DeviceObject->Flags);
81 }
82 else
83 {
84 ok((DeviceObject->Flags == (DO_DEVICE_HAS_NAME | DO_DEVICE_INITIALIZING)),
85 "Expected Flags DO_DEVICE_HAS_NAME | DO_DEVICE_INITIALIZING, got %lu", DeviceObject->Flags);
86 }
87 ok(DeviceObject->DeviceType == FILE_DEVICE_UNKNOWN,
88 "Expected DeviceType to match creation parameter FILE_DEVICE_UNKNWOWN, got %lu",
89 DeviceObject->DeviceType);
90 ok(DeviceObject->ActiveThreadCount == 0, "Expected ActiveThreadCount = 0, got %lu\n", DeviceObject->ActiveThreadCount);
91
92 /*Check the extended extension */
93 extdev = (PEXTENDED_DEVOBJ_EXTENSION)DeviceObject->DeviceObjectExtension;
94 ok(extdev->ExtensionFlags == 0, "Expected Extended ExtensionFlags to be 0, got %lu", extdev->ExtensionFlags);
95 ok (extdev->Type == 13, "Expected Type of 13, got %d", extdev->Type);
96 ok (extdev->Size == 0, "Expected Size of 0, got %d", extdev->Size);
97 ok (extdev->DeviceObject == DeviceObject, "Expected DeviceOject to match newly created device %p, got %p",
98 DeviceObject, extdev->DeviceObject);
99 ok(extdev->AttachedTo == NULL, "Expected AttachTo to be NULL, got %p", extdev->AttachedTo);
100 ok(extdev->StartIoCount == 0, "Expected StartIoCount = 0, got %lu", extdev->StartIoCount);
101 ok(extdev->StartIoKey == 0, "Expected StartIoKey = 0, got %lu", extdev->StartIoKey);
102 ok(extdev->StartIoFlags == 0, "Expected StartIoFlags = 0, got %lu", extdev->StartIoFlags);
103 }
104
105 VOID DeviceDeletionTest(PDEVICE_OBJECT DeviceObject, BOOLEAN Lower)
106 {
107 PEXTENDED_DEVOBJ_EXTENSION extdev;
108
109 /*Check the device object members */
110 ok(DeviceObject->Type==3, "Expected Type = 3, got %d", DeviceObject->Type);
111 ok(DeviceObject->Size = 0xb8, "Expected Size = 0xba, got %d", DeviceObject->Size);
112 ok(DeviceObject->ReferenceCount == 0, "Expected ReferenceCount = 0, got %lu",
113 DeviceObject->ReferenceCount);
114 if (!Lower)
115 {
116 ok(DeviceObject->DriverObject == ThisDriverObject,
117 "Expected DriverObject member to match this DriverObject %p, got %p",
118 ThisDriverObject, DeviceObject->DriverObject);
119 }
120 ok(DeviceObject->NextDevice == NULL, "Expected NextDevice to be NULL, got %p", DeviceObject->NextDevice);
121
122 if (Lower)
123 {
124 ok(DeviceObject->AttachedDevice == MainDeviceObject,
125 "Expected AttachDevice to be %p, got %p", MainDeviceObject, DeviceObject->AttachedDevice);
126 }
127 else
128 {
129 ok(DeviceObject->AttachedDevice == NULL, "Expected AttachDevice to be NULL, got %p", DeviceObject->AttachedDevice);
130 }
131
132 ok(DeviceObject->Flags ==FILE_VIRTUAL_VOLUME,
133 "Expected Flags FILE_VIRTUAL_VOLUME, got %lu", DeviceObject->Flags);
134 ok(DeviceObject->DeviceType == FILE_DEVICE_UNKNOWN,
135 "Expected DeviceType to match creation parameter FILE_DEVICE_UNKNWOWN, got %lu",
136 DeviceObject->DeviceType);
137 ok(DeviceObject->ActiveThreadCount == 0, "Expected ActiveThreadCount = 0, got %lu\n", DeviceObject->ActiveThreadCount);
138
139 /*Check the extended extension */
140 extdev = (PEXTENDED_DEVOBJ_EXTENSION)DeviceObject->DeviceObjectExtension;
141 ok(extdev->ExtensionFlags == DOE_UNLOAD_PENDING,
142 "Expected Extended ExtensionFlags to be DOE_UNLOAD_PENDING, got %lu", extdev->ExtensionFlags);
143 ok (extdev->Type == 13, "Expected Type of 13, got %d", extdev->Type);
144 ok (extdev->Size == 0, "Expected Size of 0, got %d", extdev->Size);
145 ok (extdev->DeviceObject == DeviceObject, "Expected DeviceOject to match newly created device %p, got %p",
146 DeviceObject, extdev->DeviceObject);
147 if (Lower)
148 {
149 /* Skip this for now */
150 //ok(extdev->AttachedTo == MainDeviceObject, "Expected AttachTo to %p, got %p", MainDeviceObject, extdev->AttachedTo);
151 }
152 else
153 {
154 ok(extdev->AttachedTo == NULL, "Expected AttachTo to be NULL, got %p", extdev->AttachedTo);
155 }
156 ok(extdev->StartIoCount == 0, "Expected StartIoCount = 0, got %lu", extdev->StartIoCount);
157 ok(extdev->StartIoKey == 0, "Expected StartIoKey = 0, got %lu", extdev->StartIoKey);
158 ok(extdev->StartIoFlags == 0, "Expected StartIoFlags = 0, got %lu", extdev->StartIoFlags);
159 }
160
161 VOID DeviceCreateDeleteTest(PDRIVER_OBJECT DriverObject)
162 {
163 NTSTATUS Status;
164 UNICODE_STRING DeviceString;
165 UNICODE_STRING DosDeviceString;
166 PDEVICE_OBJECT DeviceObject;
167
168 /* Create using wrong directory */
169 RtlInitUnicodeString(&DeviceString, L"\\Device1\\Kmtest");
170 Status = IoCreateDevice(DriverObject,
171 0,
172 &DeviceString,
173 FILE_DEVICE_UNKNOWN,
174 0,
175 FALSE,
176 &DeviceObject);
177 ok(Status == STATUS_OBJECT_PATH_NOT_FOUND, "Expected STATUS_OBJECT_PATH_NOT_FOUND, got 0x%lX", Status);
178
179 /* Create using correct params with exlusice access */
180 RtlInitUnicodeString(&DeviceString, L"\\Device\\Kmtest");
181 Status = IoCreateDevice(DriverObject,
182 0,
183 &DeviceString,
184 FILE_DEVICE_UNKNOWN,
185 0,
186 TRUE,
187 &DeviceObject);
188 ok(Status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got 0x%lX", Status);
189
190 DeviceCreatedTest(DeviceObject, TRUE);
191
192 /* Delete the device */
193 if (NT_SUCCESS(Status))
194 {
195 IoDeleteDevice(DeviceObject);
196 ok(DriverObject->DeviceObject == 0, "Expected DriverObject->DeviceObject to be NULL, got %p",
197 DriverObject->DeviceObject);
198 }
199
200 /* Create using correct params with exlusice access */
201 Status = IoCreateDevice(DriverObject,
202 0,
203 &DeviceString,
204 FILE_DEVICE_UNKNOWN,
205 0,
206 FALSE,
207 &DeviceObject);
208 ok(Status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got 0x%lX", Status);
209
210 DeviceCreatedTest(DeviceObject, FALSE);
211
212 /* Delete the device */
213 if (NT_SUCCESS(Status))
214 {
215 IoDeleteDevice(DeviceObject);
216 ok(DriverObject->DeviceObject == 0, "Expected DriverObject->DeviceObject to be NULL, got %p",
217 DriverObject->DeviceObject);
218 }
219
220 /* Recreate device */
221 Status = IoCreateDevice(DriverObject,
222 0,
223 &DeviceString,
224 FILE_DEVICE_UNKNOWN,
225 0,
226 FALSE,
227 &DeviceObject);
228 ok(Status == STATUS_SUCCESS, "Expected STATUS_SUCCESS, got 0x%lX", Status);
229
230 RtlInitUnicodeString(&DosDeviceString, L"\\DosDevices\\kmtest");
231 Status = IoCreateSymbolicLink(&DosDeviceString, &DeviceString);
232
233 if (!NT_SUCCESS(Status))
234 {
235 /* Delete device object if not successful */
236 IoDeleteDevice(DeviceObject);
237 return;
238 }
239
240 MainDeviceObject = DeviceObject;
241
242 return;
243 }
244
245 BOOLEAN AttachDeviceTest(PDEVICE_OBJECT DeviceObject, PWCHAR NewDriverRegPath)
246 {
247 NTSTATUS Status;
248 UNICODE_STRING LowerDeviceName;
249
250 RtlInitUnicodeString(&LowerDeviceName, NewDriverRegPath);
251 Status = IoAttachDevice(DeviceObject, &LowerDeviceName, &AttachDeviceObject);
252
253 /* TODO: Add more tests */
254
255 return TRUE;
256 }
257
258 BOOLEAN DetachDeviceTest(PDEVICE_OBJECT AttachedDevice)
259 {
260
261 IoDetachDevice(AttachedDevice);
262
263 /* TODO: Add more tests */
264
265 return TRUE;
266 }