[KMTEST]
[reactos.git] / rostests / 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 <thomas.faber@reactos.org>
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 <kmt_platform.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 #ifndef KMT_STANDALONE_DRIVER
42
43 /* usermode call-back mechanism */
44
45 /* list of supported operations */
46 typedef enum _KMT_CALLBACK_INFORMATION_CLASS
47 {
48 QueryVirtualMemory
49 } KMT_CALLBACK_INFORMATION_CLASS, *PKMT_CALLBACK_INFORMATION_CLASS;
50
51 /* TODO: "response" is a little generic */
52 typedef union _KMT_RESPONSE
53 {
54 MEMORY_BASIC_INFORMATION MemInfo;
55 } KMT_RESPONSE, *PKMT_RESPONSE;
56
57 /* this struct is sent from driver to usermode */
58 typedef struct _KMT_CALLBACK_REQUEST_PACKET
59 {
60 ULONG RequestId;
61 KMT_CALLBACK_INFORMATION_CLASS OperationClass;
62 PVOID Parameters;
63 } KMT_CALLBACK_REQUEST_PACKET, *PKMT_CALLBACK_REQUEST_PACKET;
64
65 PKMT_RESPONSE KmtUserModeCallback(KMT_CALLBACK_INFORMATION_CLASS Operation, PVOID Parameters);
66 VOID KmtFreeCallbackResponse(PKMT_RESPONSE Response);
67
68 //macro to simplify using the mechanism
69 #define Test_NtQueryVirtualMemory(BaseAddress, Size, AllocationType, ProtectionType) \
70 do { \
71 PKMT_RESPONSE NtQueryTest = KmtUserModeCallback(QueryVirtualMemory, BaseAddress); \
72 if (NtQueryTest != NULL) \
73 { \
74 ok_eq_hex(NtQueryTest->MemInfo.Protect, ProtectionType); \
75 ok_eq_hex(NtQueryTest->MemInfo.State, AllocationType); \
76 ok_eq_size(NtQueryTest->MemInfo.RegionSize, Size); \
77 KmtFreeCallbackResponse(NtQueryTest); \
78 } \
79 } while (0) \
80
81 #endif
82
83 #ifdef KMT_STANDALONE_DRIVER
84 #define KMT_KERNEL_MODE
85
86 typedef NTSTATUS (KMT_IRP_HANDLER)(
87 IN PDEVICE_OBJECT DeviceObject,
88 IN PIRP Irp,
89 IN PIO_STACK_LOCATION IoStackLocation);
90 typedef KMT_IRP_HANDLER *PKMT_IRP_HANDLER;
91
92 NTSTATUS KmtRegisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler);
93 NTSTATUS KmtUnregisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler);
94
95 typedef NTSTATUS (KMT_MESSAGE_HANDLER)(
96 IN PDEVICE_OBJECT DeviceObject,
97 IN ULONG ControlCode,
98 IN PVOID Buffer OPTIONAL,
99 IN SIZE_T InLength,
100 IN OUT PSIZE_T OutLength);
101 typedef KMT_MESSAGE_HANDLER *PKMT_MESSAGE_HANDLER;
102
103 NTSTATUS KmtRegisterMessageHandler(IN ULONG ControlCode OPTIONAL, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_MESSAGE_HANDLER MessageHandler);
104 NTSTATUS KmtUnregisterMessageHandler(IN ULONG ControlCode OPTIONAL, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_MESSAGE_HANDLER MessageHandler);
105
106 typedef enum
107 {
108 TESTENTRY_NO_CREATE_DEVICE = 1,
109 TESTENTRY_NO_REGISTER_DISPATCH = 2,
110 TESTENTRY_NO_REGISTER_UNLOAD = 4,
111 TESTENTRY_NO_EXCLUSIVE_DEVICE = 8,
112 TESTENTRY_NO_READONLY_DEVICE = 16,
113 TESTENTRY_BUFFERED_IO_DEVICE = 32,
114 } KMT_TESTENTRY_FLAGS;
115
116 NTSTATUS TestEntry(IN PDRIVER_OBJECT DriverObject, IN PCUNICODE_STRING RegistryPath, OUT PCWSTR *DeviceName, IN OUT INT *Flags);
117 VOID TestUnload(IN PDRIVER_OBJECT DriverObject);
118 #endif /* defined KMT_STANDALONE_DRIVER */
119
120 #ifdef KMT_KERNEL_MODE
121 /* Device Extension layout */
122 typedef struct
123 {
124 PKMT_RESULTBUFFER ResultBuffer;
125 PMDL Mdl;
126 } KMT_DEVICE_EXTENSION, *PKMT_DEVICE_EXTENSION;
127
128 extern BOOLEAN KmtIsCheckedBuild;
129 extern BOOLEAN KmtIsMultiProcessorBuild;
130 extern PCSTR KmtMajorFunctionNames[];
131 extern PDRIVER_OBJECT KmtDriverObject;
132
133 VOID KmtSetIrql(IN KIRQL NewIrql);
134 BOOLEAN KmtAreInterruptsEnabled(VOID);
135 ULONG KmtGetPoolTag(PVOID Memory);
136 USHORT KmtGetPoolType(PVOID Memory);
137 PVOID KmtGetSystemRoutineAddress(IN PCWSTR RoutineName);
138 PKTHREAD KmtStartThread(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext OPTIONAL);
139 VOID KmtFinishThread(IN PKTHREAD Thread OPTIONAL, IN PKEVENT Event OPTIONAL);
140 #elif defined KMT_USER_MODE
141 DWORD KmtRunKernelTest(IN PCSTR TestName);
142
143 VOID KmtLoadDriver(IN PCWSTR ServiceName, IN BOOLEAN RestartIfRunning);
144 VOID KmtUnloadDriver(VOID);
145 VOID KmtOpenDriver(VOID);
146 VOID KmtCloseDriver(VOID);
147
148 DWORD KmtSendToDriver(IN DWORD ControlCode);
149 DWORD KmtSendStringToDriver(IN DWORD ControlCode, IN PCSTR String);
150 DWORD KmtSendWStringToDriver(IN DWORD ControlCode, IN PCWSTR String);
151 DWORD KmtSendBufferToDriver(IN DWORD ControlCode, IN OUT PVOID Buffer OPTIONAL, IN DWORD InLength, IN OUT PDWORD OutLength);
152 #else /* if !defined KMT_KERNEL_MODE && !defined KMT_USER_MODE */
153 #error either KMT_KERNEL_MODE or KMT_USER_MODE must be defined
154 #endif /* !defined KMT_KERNEL_MODE && !defined KMT_USER_MODE */
155
156 extern PKMT_RESULTBUFFER ResultBuffer;
157
158 #ifdef __GNUC__
159 /* TODO: GCC doesn't understand %wZ :( */
160 #define KMT_FORMAT(type, fmt, first) /*__attribute__((__format__(type, fmt, first)))*/
161 #elif !defined __GNUC__
162 #define KMT_FORMAT(type, fmt, first)
163 #endif /* !defined __GNUC__ */
164
165 #define START_TEST(name) VOID Test_##name(VOID)
166
167 #ifndef KMT_STRINGIZE
168 #define KMT_STRINGIZE(x) #x
169 #endif /* !defined KMT_STRINGIZE */
170 #define ok(test, ...) ok_(test, __FILE__, __LINE__, __VA_ARGS__)
171 #define trace(...) trace_( __FILE__, __LINE__, __VA_ARGS__)
172 #define skip(test, ...) skip_(test, __FILE__, __LINE__, __VA_ARGS__)
173
174 #define ok_(test, file, line, ...) KmtOk(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
175 #define trace_(file, line, ...) KmtTrace( file ":" KMT_STRINGIZE(line), __VA_ARGS__)
176 #define skip_(test, file, line, ...) KmtSkip(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
177
178 BOOLEAN KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0);
179 BOOLEAN KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 3, 4);
180 VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 2, 0);
181 VOID KmtTrace(PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 2, 3);
182 BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0);
183 BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 3, 4);
184 PVOID KmtAllocateGuarded(SIZE_T SizeRequested);
185 VOID KmtFreeGuarded(PVOID Pointer);
186
187 #ifdef KMT_KERNEL_MODE
188 #define ok_irql(irql) ok(KeGetCurrentIrql() == irql, "IRQL is %d, expected %d\n", KeGetCurrentIrql(), irql)
189 #endif /* defined KMT_KERNEL_MODE */
190 #define ok_eq_print(value, expected, spec) ok((value) == (expected), #value " = " spec ", expected " spec "\n", value, expected)
191 #define ok_eq_pointer(value, expected) ok_eq_print(value, expected, "%p")
192 #define ok_eq_int(value, expected) ok_eq_print(value, expected, "%d")
193 #define ok_eq_uint(value, expected) ok_eq_print(value, expected, "%u")
194 #define ok_eq_long(value, expected) ok_eq_print(value, expected, "%ld")
195 #define ok_eq_ulong(value, expected) ok_eq_print(value, expected, "%lu")
196 #define ok_eq_longlong(value, expected) ok_eq_print(value, expected, "%I64d")
197 #define ok_eq_ulonglong(value, expected) ok_eq_print(value, expected, "%I64u")
198 #define ok_eq_char(value, expected) ok_eq_print(value, expected, "%c")
199 #define ok_eq_wchar(value, expected) ok_eq_print(value, expected, "%C")
200 #ifndef _WIN64
201 #define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%lu")
202 #define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%ld")
203 #define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%lu")
204 #elif defined _WIN64
205 #define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%I64u")
206 #define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%I64d")
207 #define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%I64u")
208 #endif /* defined _WIN64 */
209 #define ok_eq_hex(value, expected) ok_eq_print(value, expected, "0x%08lx")
210 #define ok_bool_true(value, desc) ok((value) == TRUE, desc " FALSE, expected TRUE\n")
211 #define ok_bool_false(value, desc) ok((value) == FALSE, desc " TRUE, expected FALSE\n")
212 #define ok_eq_bool(value, expected) ok((value) == (expected), #value " = %s, expected %s\n", \
213 (value) ? "TRUE" : "FALSE", \
214 (expected) ? "TRUE" : "FALSE")
215 #define ok_eq_str(value, expected) ok(!strcmp(value, expected), #value " = \"%s\", expected \"%s\"\n", value, expected)
216 #define ok_eq_wstr(value, expected) ok(!wcscmp(value, expected), #value " = \"%ls\", expected \"%ls\"\n", value, expected)
217 #define ok_eq_tag(value, expected) ok_eq_print(value, expected, "0x%08lx")
218
219 #define KMT_MAKE_CODE(ControlCode) CTL_CODE(FILE_DEVICE_UNKNOWN, \
220 0xC00 + (ControlCode), \
221 METHOD_BUFFERED, \
222 FILE_ANY_ACCESS)
223
224 #define MICROSECOND 10
225 #define MILLISECOND (1000 * MICROSECOND)
226 #define SECOND (1000 * MILLISECOND)
227
228 /* See apitests/include/apitest.h */
229 #define KmtInvalidPointer ((PVOID)0x5555555555555555ULL)
230
231 #define KmtStartSeh() \
232 { \
233 NTSTATUS ExceptionStatus = STATUS_SUCCESS; \
234 _SEH2_TRY \
235 {
236
237 #define KmtEndSeh(ExpectedStatus) \
238 } \
239 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) \
240 { \
241 ExceptionStatus = _SEH2_GetExceptionCode(); \
242 } \
243 _SEH2_END; \
244 ok_eq_hex(ExceptionStatus, (ExpectedStatus)); \
245 }
246
247 #if defined KMT_DEFINE_TEST_FUNCTIONS
248
249 #if defined KMT_KERNEL_MODE
250 #include "kmt_test_kernel.h"
251 #elif defined KMT_USER_MODE
252 #include "kmt_test_user.h"
253 #endif /* defined KMT_USER_MODE */
254
255 PKMT_RESULTBUFFER ResultBuffer = NULL;
256
257 static VOID KmtAddToLogBuffer(PKMT_RESULTBUFFER Buffer, PCSTR String, SIZE_T Length)
258 {
259 LONG OldLength;
260 LONG NewLength;
261
262 if (!Buffer)
263 return;
264
265 do
266 {
267 OldLength = Buffer->LogBufferLength;
268 NewLength = OldLength + (ULONG)Length;
269 if (NewLength > Buffer->LogBufferMaxLength)
270 return;
271 } while (InterlockedCompareExchange(&Buffer->LogBufferLength, NewLength, OldLength) != OldLength);
272
273 memcpy(&Buffer->LogBuffer[OldLength], String, Length);
274 }
275
276 KMT_FORMAT(ms_printf, 5, 0)
277 static SIZE_T KmtXVSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR FileAndLine, PCSTR Prepend, PCSTR Format, va_list Arguments)
278 {
279 SIZE_T BufferLength = 0;
280 SIZE_T Length;
281
282 if (FileAndLine)
283 {
284 PCSTR Slash;
285 Slash = strrchr(FileAndLine, '\\');
286 if (Slash)
287 FileAndLine = Slash + 1;
288 Slash = strrchr(FileAndLine, '/');
289 if (Slash)
290 FileAndLine = Slash + 1;
291
292 Length = min(BufferMaxLength, strlen(FileAndLine));
293 memcpy(Buffer, FileAndLine, Length);
294 Buffer += Length;
295 BufferLength += Length;
296 BufferMaxLength -= Length;
297 }
298 if (Prepend)
299 {
300 Length = min(BufferMaxLength, strlen(Prepend));
301 memcpy(Buffer, Prepend, Length);
302 Buffer += Length;
303 BufferLength += Length;
304 BufferMaxLength -= Length;
305 }
306 if (Format)
307 {
308 Length = KmtVSNPrintF(Buffer, BufferMaxLength, Format, Arguments);
309 /* vsnprintf can return more than maxLength, we don't want to do that */
310 BufferLength += min(Length, BufferMaxLength);
311 }
312 return BufferLength;
313 }
314
315 KMT_FORMAT(ms_printf, 5, 6)
316 static SIZE_T KmtXSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR FileAndLine, PCSTR Prepend, PCSTR Format, ...)
317 {
318 SIZE_T BufferLength;
319 va_list Arguments;
320 va_start(Arguments, Format);
321 BufferLength = KmtXVSNPrintF(Buffer, BufferMaxLength, FileAndLine, Prepend, Format, Arguments);
322 va_end(Arguments);
323 return BufferLength;
324 }
325
326 VOID KmtFinishTest(PCSTR TestName)
327 {
328 CHAR MessageBuffer[512];
329 SIZE_T MessageLength;
330
331 if (!ResultBuffer)
332 return;
333
334 MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, NULL, NULL,
335 "%s: %ld tests executed (0 marked as todo, %ld failures), %ld skipped.\n",
336 TestName,
337 ResultBuffer->Successes + ResultBuffer->Failures,
338 ResultBuffer->Failures,
339 ResultBuffer->Skipped);
340 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
341 }
342
343 BOOLEAN KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments)
344 {
345 CHAR MessageBuffer[512];
346 SIZE_T MessageLength;
347
348 if (!ResultBuffer)
349 return Condition != 0;
350
351 if (Condition)
352 {
353 InterlockedIncrement(&ResultBuffer->Successes);
354
355 if (0/*KmtReportSuccess*/)
356 {
357 MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Test succeeded\n", NULL);
358 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
359 }
360 }
361 else
362 {
363 InterlockedIncrement(&ResultBuffer->Failures);
364 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Test failed: ", Format, Arguments);
365 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
366 }
367
368 return Condition != 0;
369 }
370
371 BOOLEAN KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format, ...)
372 {
373 BOOLEAN Ret;
374 va_list Arguments;
375 va_start(Arguments, Format);
376 Ret = KmtVOk(Condition, FileAndLine, Format, Arguments);
377 va_end(Arguments);
378 return Ret;
379 }
380
381 VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments)
382 {
383 CHAR MessageBuffer[512];
384 SIZE_T MessageLength;
385
386 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": ", Format, Arguments);
387 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
388 }
389
390 VOID KmtTrace(PCSTR FileAndLine, PCSTR Format, ...)
391 {
392 va_list Arguments;
393 va_start(Arguments, Format);
394 KmtVTrace(FileAndLine, Format, Arguments);
395 va_end(Arguments);
396 }
397
398 BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments)
399 {
400 CHAR MessageBuffer[512];
401 SIZE_T MessageLength;
402
403 if (!ResultBuffer)
404 return !Condition;
405
406 if (!Condition)
407 {
408 InterlockedIncrement(&ResultBuffer->Skipped);
409 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Tests skipped: ", Format, Arguments);
410 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
411 }
412
413 return !Condition;
414 }
415
416 BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, ...)
417 {
418 BOOLEAN Ret;
419 va_list Arguments;
420 va_start(Arguments, Format);
421 Ret = KmtVSkip(Condition, FileAndLine, Format, Arguments);
422 va_end(Arguments);
423 return Ret;
424 }
425
426 PVOID KmtAllocateGuarded(SIZE_T SizeRequested)
427 {
428 NTSTATUS Status;
429 SIZE_T Size = PAGE_ROUND_UP(SizeRequested + PAGE_SIZE);
430 PVOID VirtualMemory = NULL;
431 PCHAR StartOfBuffer;
432
433 Status = ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory, 0, &Size, MEM_RESERVE, PAGE_NOACCESS);
434
435 if (!NT_SUCCESS(Status))
436 return NULL;
437
438 Size -= PAGE_SIZE;
439 Status = ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory, 0, &Size, MEM_COMMIT, PAGE_READWRITE);
440 if (!NT_SUCCESS(Status))
441 {
442 Size = 0;
443 Status = ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
444 ok_eq_hex(Status, STATUS_SUCCESS);
445 return NULL;
446 }
447
448 StartOfBuffer = VirtualMemory;
449 StartOfBuffer += Size - SizeRequested;
450
451 return StartOfBuffer;
452 }
453
454 VOID KmtFreeGuarded(PVOID Pointer)
455 {
456 NTSTATUS Status;
457 PVOID VirtualMemory = (PVOID)PAGE_ROUND_DOWN((SIZE_T)Pointer);
458 SIZE_T Size = 0;
459
460 Status = ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
461 ok_eq_hex(Status, STATUS_SUCCESS);
462 }
463
464 #endif /* defined KMT_DEFINE_TEST_FUNCTIONS */
465
466 #endif /* !defined _KMTEST_TEST_H_ */