50bb10298804a67591279a103e02eb5b6de10f25
[reactos.git] / kmtests / include / kmt_test.h
1 /*
2 * PROJECT: ReactOS kernel-mode tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Kernel-Mode Test Suite test framework declarations
5 * PROGRAMMER: Thomas Faber <thfabba@gmx.de>
6 */
7
8 /* Inspired by Wine C unit tests, Copyright (C) 2002 Alexandre Julliard
9 * Inspired by ReactOS kernel-mode regression tests,
10 * Copyright (C) Aleksey Bragin, Filip Navara
11 */
12
13 #ifndef _KMTEST_TEST_H_
14 #define _KMTEST_TEST_H_
15
16 #include <stdarg.h>
17
18 typedef VOID KMT_TESTFUNC(VOID);
19 typedef KMT_TESTFUNC *PKMT_TESTFUNC;
20
21 typedef struct
22 {
23 const char *TestName;
24 KMT_TESTFUNC *TestFunction;
25 } KMT_TEST, *PKMT_TEST;
26
27 typedef const KMT_TEST CKMT_TEST, *PCKMT_TEST;
28
29 extern const KMT_TEST TestList[];
30
31 typedef struct
32 {
33 volatile LONG Successes;
34 volatile LONG Failures;
35 volatile LONG Skipped;
36 volatile LONG LogBufferLength;
37 LONG LogBufferMaxLength;
38 CHAR LogBuffer[ANYSIZE_ARRAY];
39 } KMT_RESULTBUFFER, *PKMT_RESULTBUFFER;
40
41 #ifdef KMT_STANDALONE_DRIVER
42 #define KMT_KERNEL_MODE
43
44 typedef NTSTATUS (KMT_IRP_HANDLER)(
45 IN PDEVICE_OBJECT DeviceObject,
46 IN PIRP Irp,
47 IN PIO_STACK_LOCATION IoStackLocation);
48 typedef KMT_IRP_HANDLER *PKMT_IRP_HANDLER;
49
50 NTSTATUS KmtRegisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler);
51 NTSTATUS KmtUnregisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler);
52
53 typedef NTSTATUS (KMT_MESSAGE_HANDLER)(
54 IN PDEVICE_OBJECT DeviceObject,
55 IN ULONG ControlCode,
56 IN PVOID Buffer OPTIONAL,
57 IN SIZE_T InLength,
58 IN OUT PSIZE_T OutLength);
59 typedef KMT_MESSAGE_HANDLER *PKMT_MESSAGE_HANDLER;
60
61 NTSTATUS KmtRegisterMessageHandler(IN ULONG ControlCode OPTIONAL, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_MESSAGE_HANDLER MessageHandler);
62 NTSTATUS KmtUnregisterMessageHandler(IN ULONG ControlCode OPTIONAL, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_MESSAGE_HANDLER MessageHandler);
63
64 typedef enum
65 {
66 TESTENTRY_NO_CREATE_DEVICE = 1,
67 TESTENTRY_NO_REGISTER_DISPATCH = 2,
68 TESTENTRY_NO_REGISTER_UNLOAD = 4,
69 } KMT_TESTENTRY_FLAGS;
70
71 NTSTATUS TestEntry(IN PDRIVER_OBJECT DriverObject, IN PCUNICODE_STRING RegistryPath, OUT PCWSTR *DeviceName, OUT INT *Flags);
72 VOID TestUnload(IN PDRIVER_OBJECT DriverObject);
73 #endif /* defined KMT_STANDALONE_DRIVER */
74
75 #ifdef KMT_KERNEL_MODE
76 /* Device Extension layout */
77 typedef struct
78 {
79 PKMT_RESULTBUFFER ResultBuffer;
80 PMDL Mdl;
81 } KMT_DEVICE_EXTENSION, *PKMT_DEVICE_EXTENSION;
82
83 extern BOOLEAN KmtIsCheckedBuild;
84 extern BOOLEAN KmtIsMultiProcessorBuild;
85
86 VOID KmtSetIrql(IN KIRQL NewIrql);
87 #elif defined KMT_USER_MODE
88 DWORD KmtRunKernelTest(IN PCSTR TestName);
89
90 VOID KmtLoadDriver(IN PCWSTR ServiceName, IN BOOLEAN RestartIfRunning);
91 VOID KmtUnloadDriver(VOID);
92 VOID KmtOpenDriver(VOID);
93 VOID KmtCloseDriver(VOID);
94
95 DWORD KmtSendToDriver(IN DWORD ControlCode);
96 DWORD KmtSendStringToDriver(IN DWORD ControlCode, IN PCSTR String);
97 DWORD KmtSendBufferToDriver(IN DWORD ControlCode, IN OUT PVOID Buffer OPTIONAL, IN DWORD InLength, IN OUT PDWORD OutLength);
98 #endif /* defined KMT_USER_MODE */
99
100 extern PKMT_RESULTBUFFER ResultBuffer;
101
102 #ifdef __GNUC__
103 #define KMT_FORMAT(type, fmt, first) __attribute__((__format__(type, fmt, first)))
104 #elif !defined __GNUC__
105 #define KMT_FORMAT(type, fmt, first)
106 #endif /* !defined __GNUC__ */
107
108 #define START_TEST(name) VOID Test_##name(VOID)
109
110 #ifndef KMT_STRINGIZE
111 #define KMT_STRINGIZE(x) #x
112 #endif /* !defined KMT_STRINGIZE */
113 #define ok(test, ...) ok_(test, __FILE__, __LINE__, __VA_ARGS__)
114 #define trace(...) trace_( __FILE__, __LINE__, __VA_ARGS__)
115 #define skip(test, ...) skip_(test, __FILE__, __LINE__, __VA_ARGS__)
116
117 #define ok_(test, file, line, ...) KmtOk(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
118 #define trace_(file, line, ...) KmtTrace( file ":" KMT_STRINGIZE(line), __VA_ARGS__)
119 #define skip_(test, file, line, ...) KmtSkip(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
120
121 VOID KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0);
122 VOID KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 3, 4);
123 VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 2, 0);
124 VOID KmtTrace(PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 2, 3);
125 BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0);
126 BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 3, 4);
127
128 #ifdef KMT_KERNEL_MODE
129 #define ok_irql(irql) ok(KeGetCurrentIrql() == irql, "IRQL is %d, expected %d\n", KeGetCurrentIrql(), irql)
130 #endif /* defined KMT_KERNEL_MODE */
131 #define ok_eq_print(value, expected, spec) ok((value) == (expected), #value " = " spec ", expected " spec "\n", value, expected)
132 #define ok_eq_pointer(value, expected) ok_eq_print(value, expected, "%p")
133 #define ok_eq_int(value, expected) ok_eq_print(value, expected, "%d")
134 #define ok_eq_uint(value, expected) ok_eq_print(value, expected, "%u")
135 #define ok_eq_long(value, expected) ok_eq_print(value, expected, "%ld")
136 #define ok_eq_ulong(value, expected) ok_eq_print(value, expected, "%lu")
137 #define ok_eq_longlong(value, expected) ok_eq_print(value, expected, "%I64d")
138 #define ok_eq_ulonglong(value, expected) ok_eq_print(value, expected, "%I64u")
139 #define ok_eq_hex(value, expected) ok_eq_print(value, expected, "0x%08lx")
140 #define ok_bool_true(value, desc) ok((value) == TRUE, desc " FALSE, expected TRUE\n")
141 #define ok_bool_false(value, desc) ok((value) == FALSE, desc " TRUE, expected FALSE\n")
142 #define ok_eq_bool(value, expected) ok((value) == (expected), #value " = %s, expected %s\n", \
143 (value) ? "TRUE" : "FALSE", \
144 (expected) ? "TRUE" : "FALSE")
145 #define ok_eq_str(value, expected) ok(!strcmp(value, expected), #value " = \"%s\", expected \"%s\"\n", value, expected)
146 #define ok_eq_wstr(value, expected) ok(!wcscmp(value, expected), #value " = \"%ls\", expected \"%ls\"\n", value, expected)
147
148 #define KMT_MAKE_CODE(ControlCode) CTL_CODE(FILE_DEVICE_UNKNOWN, \
149 0xC00 + (ControlCode), \
150 METHOD_BUFFERED, \
151 FILE_ANY_ACCESS)
152
153 #if defined KMT_DEFINE_TEST_FUNCTIONS
154
155 #if defined KMT_KERNEL_MODE
156 BOOLEAN KmtIsCheckedBuild;
157 BOOLEAN KmtIsMultiProcessorBuild;
158
159 VOID KmtSetIrql(IN KIRQL NewIrql)
160 {
161 KIRQL Irql = KeGetCurrentIrql();
162 if (Irql > NewIrql)
163 KeLowerIrql(NewIrql);
164 else if (Irql < NewIrql)
165 KeRaiseIrql(NewIrql, &Irql);
166 }
167
168 INT __cdecl KmtVSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0);
169 #elif defined KMT_USER_MODE
170 static PKMT_RESULTBUFFER KmtAllocateResultBuffer(SIZE_T LogBufferMaxLength)
171 {
172 PKMT_RESULTBUFFER Buffer = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(KMT_RESULTBUFFER, LogBuffer[LogBufferMaxLength]));
173
174 Buffer->Successes = 0;
175 Buffer->Failures = 0;
176 Buffer->Skipped = 0;
177 Buffer->LogBufferLength = 0;
178 Buffer->LogBufferMaxLength = LogBufferMaxLength;
179
180 return Buffer;
181 }
182
183 static VOID KmtFreeResultBuffer(PKMT_RESULTBUFFER Buffer)
184 {
185 HeapFree(GetProcessHeap(), 0, Buffer);
186 }
187
188 #define KmtVSNPrintF vsnprintf
189 #endif /* defined KMT_USER_MODE */
190
191 PKMT_RESULTBUFFER ResultBuffer = NULL;
192
193 static VOID KmtAddToLogBuffer(PKMT_RESULTBUFFER Buffer, PCSTR String, SIZE_T Length)
194 {
195 LONG OldLength;
196 LONG NewLength;
197
198 if (!Buffer)
199 return;
200
201 do
202 {
203 OldLength = Buffer->LogBufferLength;
204 NewLength = OldLength + Length;
205 if (NewLength > Buffer->LogBufferMaxLength)
206 {
207 /* TODO: indicate failure somehow */
208 __debugbreak();
209 return;
210 }
211 } while (InterlockedCompareExchange(&Buffer->LogBufferLength, NewLength, OldLength) != OldLength);
212
213 memcpy(&Buffer->LogBuffer[OldLength], String, Length);
214 }
215
216 KMT_FORMAT(ms_printf, 5, 0)
217 static SIZE_T KmtXVSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR FileAndLine, PCSTR Prepend, PCSTR Format, va_list Arguments)
218 {
219 SIZE_T BufferLength = 0;
220 SIZE_T Length;
221
222 if (FileAndLine)
223 {
224 PCSTR Slash;
225 Slash = strrchr(FileAndLine, '\\');
226 if (Slash)
227 FileAndLine = Slash + 1;
228 Slash = strrchr(FileAndLine, '/');
229 if (Slash)
230 FileAndLine = Slash + 1;
231
232 Length = min(BufferMaxLength, strlen(FileAndLine));
233 memcpy(Buffer, FileAndLine, Length);
234 Buffer += Length;
235 BufferLength += Length;
236 BufferMaxLength -= Length;
237 }
238 if (Prepend)
239 {
240 Length = min(BufferMaxLength, strlen(Prepend));
241 memcpy(Buffer, Prepend, Length);
242 Buffer += Length;
243 BufferLength += Length;
244 BufferMaxLength -= Length;
245 }
246 if (Format)
247 {
248 Length = KmtVSNPrintF(Buffer, BufferMaxLength, Format, Arguments);
249 /* vsnprintf can return more than maxLength, we don't want to do that */
250 BufferLength += min(Length, BufferMaxLength);
251 }
252 return BufferLength;
253 }
254
255 KMT_FORMAT(ms_printf, 5, 6)
256 static SIZE_T KmtXSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR FileAndLine, PCSTR Prepend, PCSTR Format, ...)
257 {
258 SIZE_T BufferLength;
259 va_list Arguments;
260 va_start(Arguments, Format);
261 BufferLength = KmtXVSNPrintF(Buffer, BufferMaxLength, FileAndLine, Prepend, Format, Arguments);
262 va_end(Arguments);
263 return BufferLength;
264 }
265
266 VOID KmtFinishTest(PCSTR TestName)
267 {
268 CHAR MessageBuffer[512];
269 SIZE_T MessageLength;
270
271 if (!ResultBuffer)
272 return;
273
274 MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, NULL, NULL,
275 "%s: %ld tests executed (0 marked as todo, %ld failures), %ld skipped.\n",
276 TestName,
277 ResultBuffer->Successes + ResultBuffer->Failures,
278 ResultBuffer->Failures,
279 ResultBuffer->Skipped);
280 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
281 }
282
283 VOID KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments)
284 {
285 CHAR MessageBuffer[512];
286 SIZE_T MessageLength;
287
288 if (!ResultBuffer)
289 return;
290
291 if (Condition)
292 {
293 InterlockedIncrement(&ResultBuffer->Successes);
294
295 if (0/*KmtReportSuccess*/)
296 {
297 MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Test succeeded\n", NULL);
298 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
299 }
300 }
301 else
302 {
303 InterlockedIncrement(&ResultBuffer->Failures);
304 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Test failed: ", Format, Arguments);
305 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
306 }
307 }
308
309 VOID KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format, ...)
310 {
311 va_list Arguments;
312 va_start(Arguments, Format);
313 KmtVOk(Condition, FileAndLine, Format, Arguments);
314 va_end(Arguments);
315 }
316
317 VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments)
318 {
319 CHAR MessageBuffer[512];
320 SIZE_T MessageLength;
321
322 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": ", Format, Arguments);
323 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
324 }
325
326 VOID KmtTrace(PCSTR FileAndLine, PCSTR Format, ...)
327 {
328 va_list Arguments;
329 va_start(Arguments, Format);
330 KmtVTrace(FileAndLine, Format, Arguments);
331 va_end(Arguments);
332 }
333
334 BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments)
335 {
336 CHAR MessageBuffer[512];
337 SIZE_T MessageLength;
338
339 if (!ResultBuffer)
340 return !Condition;
341
342 if (!Condition)
343 {
344 InterlockedIncrement(&ResultBuffer->Skipped);
345 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Tests skipped: ", Format, Arguments);
346 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
347 }
348
349 return !Condition;
350 }
351
352 BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, ...)
353 {
354 BOOLEAN Ret;
355 va_list Arguments;
356 va_start(Arguments, Format);
357 Ret = KmtVSkip(Condition, FileAndLine, Format, Arguments);
358 va_end(Arguments);
359 return Ret;
360 }
361
362 #endif /* defined KMT_DEFINE_TEST_FUNCTIONS */
363
364 #endif /* !defined _KMTEST_TEST_H_ */