2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite Driver
5 * PROGRAMMER: Thomas Faber <thfabba@gmx.de>
9 #define WIN32_LEAN_AND_MEAN
17 #include <kmt_public.h>
23 // nothing to do here. All tests create the service if needed
28 SC_HANDLE Handle
= NULL
;
29 DWORD Error
= KmtDeleteService(L
"Kmtest", &Handle
);
31 ok_eq_hex(Error
, (DWORD
)ERROR_SUCCESS
);
36 // nothing to do here. All tests start the service
41 // TODO: requiring the service to be started for this is... bad,
42 // especially when it's marked for deletion and won't start ;)
43 SC_HANDLE Handle
= NULL
;
44 DWORD Error
= KmtStopService(L
"Kmtest", &Handle
);
46 ok_eq_hex(Error
, (DWORD
)ERROR_SUCCESS
);
47 Error
= KmtCloseService(&Handle
);
48 ok_eq_hex(Error
, (DWORD
)ERROR_SUCCESS
);
51 /* test support functions for special-purpose drivers */
53 extern HANDLE KmtestHandle
;
56 * @name KmtRunKernelTest
58 * Run the specified kernel-mode test part
61 * Name of the test to run
63 * @return Win32 error code as returned by DeviceIoControl
69 DWORD Error
= ERROR_SUCCESS
;
72 if (!DeviceIoControl(KmtestHandle
, IOCTL_KMTEST_RUN_TEST
, (PVOID
)TestName
, strlen(TestName
), NULL
, 0, &BytesRead
, NULL
))
78 static WCHAR TestServiceName
[MAX_PATH
];
79 static SC_HANDLE TestServiceHandle
;
80 static HANDLE TestDeviceHandle
;
85 * Load the specified special-purpose driver (create/start the service)
88 * Name of the driver service (Kmtest- prefix will be added automatically)
89 * @param RestartIfRunning
90 * TRUE to stop and restart the service if it is already running
94 IN PCWSTR ServiceName
,
95 IN BOOLEAN RestartIfRunning
)
97 DWORD Error
= ERROR_SUCCESS
;
98 WCHAR ServicePath
[MAX_PATH
];
100 StringCbCopy(ServicePath
, sizeof ServicePath
, ServiceName
);
101 StringCbCat(ServicePath
, sizeof ServicePath
, L
"_drv.sys");
103 StringCbCopy(TestServiceName
, sizeof TestServiceName
, L
"Kmtest-");
104 StringCbCat(TestServiceName
, sizeof TestServiceName
, ServiceName
);
106 Error
= KmtCreateAndStartService(TestServiceName
, ServicePath
, NULL
, &TestServiceHandle
, RestartIfRunning
);
116 * @name KmtUnloadDriver
118 * Unload special-purpose driver (stop the service)
121 KmtUnloadDriver(VOID
)
123 DWORD Error
= ERROR_SUCCESS
;
125 Error
= KmtStopService(TestServiceName
, &TestServiceHandle
);
135 * @name KmtOpenDriver
137 * Open special-purpose driver (acquire a device handle)
142 DWORD Error
= ERROR_SUCCESS
;
143 WCHAR DevicePath
[MAX_PATH
];
145 StringCbCopy(DevicePath
, sizeof DevicePath
, L
"\\\\.\\Global\\GLOBALROOT\\Device\\");
146 StringCbCat(DevicePath
, sizeof DevicePath
, TestServiceName
);
148 TestDeviceHandle
= CreateFile(DevicePath
, GENERIC_READ
| GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, 0, NULL
);
149 if (TestDeviceHandle
== INVALID_HANDLE_VALUE
)
161 * @name KmtCloseDriver
163 * Close special-purpose driver (close device handle)
168 DWORD Error
= ERROR_SUCCESS
;
170 if (TestDeviceHandle
&& !CloseHandle(TestDeviceHandle
))
181 * @name KmtSendToDriver
183 * Unload special-purpose driver (stop the service)
187 * @return Win32 error code as returned by DeviceIoControl
191 IN DWORD ControlCode
)
195 if (!DeviceIoControl(TestDeviceHandle
, KMT_MAKE_CODE(ControlCode
), NULL
, 0, NULL
, 0, &BytesRead
, NULL
))
196 return GetLastError();
198 return ERROR_SUCCESS
;
202 * @name KmtSendStringToDriver
204 * Unload special-purpose driver (stop the service)
209 * @return Win32 error code as returned by DeviceIoControl
212 KmtSendStringToDriver(
213 IN DWORD ControlCode
,
218 if (!DeviceIoControl(TestDeviceHandle
, KMT_MAKE_CODE(ControlCode
), (PVOID
)String
, strlen(String
), NULL
, 0, &BytesRead
, NULL
))
219 return GetLastError();
221 return ERROR_SUCCESS
;
225 * @name KmtSendBufferToDriver
231 * @return Win32 error code as returned by DeviceIoControl
234 KmtSendBufferToDriver(
235 IN DWORD ControlCode
,
237 IN OUT PDWORD Length
)
241 if (!DeviceIoControl(TestDeviceHandle
, KMT_MAKE_CODE(ControlCode
), Buffer
, *Length
, NULL
, 0, Length
, NULL
))
242 return GetLastError();
244 return ERROR_SUCCESS
;