c45c4e40f7a9149d2543b3a050196888d478bba6
[reactos.git] / rostests / kmtests / kmtest / support.c
1 /*
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>
6 */
7
8 #include <kmt_test.h>
9
10 #include "kmtest.h"
11 #include <kmt_public.h>
12
13 #include <assert.h>
14
15 extern HANDLE KmtestHandle;
16
17 /**
18 * @name KmtRunKernelTest
19 *
20 * Run the specified kernel-mode test part
21 *
22 * @param TestName
23 * Name of the test to run
24 *
25 * @return Win32 error code as returned by DeviceIoControl
26 */
27 DWORD
28 KmtRunKernelTest(
29 IN PCSTR TestName)
30 {
31 DWORD Error = ERROR_SUCCESS;
32 DWORD BytesRead;
33
34 if (!DeviceIoControl(KmtestHandle, IOCTL_KMTEST_RUN_TEST, (PVOID)TestName, (DWORD)strlen(TestName), NULL, 0, &BytesRead, NULL))
35 error(Error);
36
37 return Error;
38 }
39
40 static WCHAR TestServiceName[MAX_PATH];
41 static SC_HANDLE TestServiceHandle;
42 static HANDLE TestDeviceHandle;
43
44 /**
45 * @name KmtLoadDriver
46 *
47 * Load the specified special-purpose driver (create/start the service)
48 *
49 * @param ServiceName
50 * Name of the driver service (Kmtest- prefix will be added automatically)
51 * @param RestartIfRunning
52 * TRUE to stop and restart the service if it is already running
53 */
54 VOID
55 KmtLoadDriver(
56 IN PCWSTR ServiceName,
57 IN BOOLEAN RestartIfRunning)
58 {
59 DWORD Error = ERROR_SUCCESS;
60 WCHAR ServicePath[MAX_PATH];
61
62 StringCbCopy(ServicePath, sizeof ServicePath, ServiceName);
63 StringCbCat(ServicePath, sizeof ServicePath, L"_drv.sys");
64
65 StringCbCopy(TestServiceName, sizeof TestServiceName, L"Kmtest-");
66 StringCbCat(TestServiceName, sizeof TestServiceName, ServiceName);
67
68 Error = KmtCreateAndStartService(TestServiceName, ServicePath, NULL, &TestServiceHandle, RestartIfRunning);
69
70 if (Error)
71 {
72 // TODO
73 __debugbreak();
74 }
75 }
76
77 /**
78 * @name KmtUnloadDriver
79 *
80 * Unload special-purpose driver (stop the service)
81 */
82 VOID
83 KmtUnloadDriver(VOID)
84 {
85 DWORD Error = ERROR_SUCCESS;
86
87 Error = KmtStopService(TestServiceName, &TestServiceHandle);
88
89 if (Error)
90 {
91 // TODO
92 __debugbreak();
93 }
94 }
95
96 /**
97 * @name KmtOpenDriver
98 *
99 * Open special-purpose driver (acquire a device handle)
100 */
101 VOID
102 KmtOpenDriver(VOID)
103 {
104 DWORD Error = ERROR_SUCCESS;
105 WCHAR DevicePath[MAX_PATH];
106
107 StringCbCopy(DevicePath, sizeof DevicePath, L"\\\\.\\Global\\GLOBALROOT\\Device\\");
108 StringCbCat(DevicePath, sizeof DevicePath, TestServiceName);
109
110 TestDeviceHandle = CreateFile(DevicePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
111 if (TestDeviceHandle == INVALID_HANDLE_VALUE)
112 error(Error);
113
114 if (Error)
115 {
116 // TODO
117 __debugbreak();
118 }
119
120 }
121
122 /**
123 * @name KmtCloseDriver
124 *
125 * Close special-purpose driver (close device handle)
126 */
127 VOID
128 KmtCloseDriver(VOID)
129 {
130 DWORD Error = ERROR_SUCCESS;
131
132 if (TestDeviceHandle && !CloseHandle(TestDeviceHandle))
133 error(Error);
134
135 if (Error)
136 {
137 // TODO
138 __debugbreak();
139 }
140 }
141
142 /**
143 * @name KmtSendToDriver
144 *
145 * Unload special-purpose driver (stop the service)
146 *
147 * @param ControlCode
148 *
149 * @return Win32 error code as returned by DeviceIoControl
150 */
151 DWORD
152 KmtSendToDriver(
153 IN DWORD ControlCode)
154 {
155 DWORD BytesRead;
156
157 assert(ControlCode < 0x400);
158
159 if (!DeviceIoControl(TestDeviceHandle, KMT_MAKE_CODE(ControlCode), NULL, 0, NULL, 0, &BytesRead, NULL))
160 return GetLastError();
161
162 return ERROR_SUCCESS;
163 }
164
165 /**
166 * @name KmtSendStringToDriver
167 *
168 * Unload special-purpose driver (stop the service)
169 *
170 * @param ControlCode
171 * @param String
172 *
173 * @return Win32 error code as returned by DeviceIoControl
174 */
175 DWORD
176 KmtSendStringToDriver(
177 IN DWORD ControlCode,
178 IN PCSTR String)
179 {
180 DWORD BytesRead;
181
182 assert(ControlCode < 0x400);
183
184 if (!DeviceIoControl(TestDeviceHandle, KMT_MAKE_CODE(ControlCode), (PVOID)String, (DWORD)strlen(String), NULL, 0, &BytesRead, NULL))
185 return GetLastError();
186
187 return ERROR_SUCCESS;
188 }
189
190 /**
191 * @name KmtSendStringToDriver
192 *
193 * Unload special-purpose driver (stop the service)
194 *
195 * @param ControlCode
196 * @param String
197 *
198 * @return Win32 error code as returned by DeviceIoControl
199 */
200 DWORD
201 KmtSendWStringToDriver(
202 IN DWORD ControlCode,
203 IN PCWSTR String)
204 {
205 DWORD BytesRead;
206
207 assert(ControlCode < 0x400);
208
209 if (!DeviceIoControl(TestDeviceHandle, KMT_MAKE_CODE(ControlCode), (PVOID)String, (DWORD)wcslen(String) * sizeof(WCHAR), NULL, 0, &BytesRead, NULL))
210 return GetLastError();
211
212 return ERROR_SUCCESS;
213 }
214
215 /**
216 * @name KmtSendBufferToDriver
217 *
218 * @param ControlCode
219 * @param Buffer
220 * @param InLength
221 * @param OutLength
222 *
223 * @return Win32 error code as returned by DeviceIoControl
224 */
225 DWORD
226 KmtSendBufferToDriver(
227 IN DWORD ControlCode,
228 IN OUT PVOID Buffer OPTIONAL,
229 IN DWORD InLength,
230 IN OUT PDWORD OutLength)
231 {
232 assert(OutLength);
233 assert(Buffer || (!InLength && !*OutLength));
234 assert(ControlCode < 0x400);
235
236 if (!DeviceIoControl(TestDeviceHandle, KMT_MAKE_CODE(ControlCode), Buffer, InLength, Buffer, *OutLength, OutLength, NULL))
237 return GetLastError();
238
239 return ERROR_SUCCESS;
240 }