[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 #define KmtGetSystemOrEmbeddedRoutineAddress(RoutineName) \
248 p##RoutineName = KmtGetSystemRoutineAddress(L ## #RoutineName); \
249 if (!p##RoutineName) \
250 { \
251 p##RoutineName = RoutineName; \
252 trace("Using embedded routine for " #RoutineName "\n"); \
253 } \
254 else \
255 trace("Using system routine for " #RoutineName "\n");
256
257 #if defined KMT_DEFINE_TEST_FUNCTIONS
258
259 #if defined KMT_KERNEL_MODE
260 #include "kmt_test_kernel.h"
261 #elif defined KMT_USER_MODE
262 #include "kmt_test_user.h"
263 #endif /* defined KMT_USER_MODE */
264
265 PKMT_RESULTBUFFER ResultBuffer = NULL;
266
267 static VOID KmtAddToLogBuffer(PKMT_RESULTBUFFER Buffer, PCSTR String, SIZE_T Length)
268 {
269 LONG OldLength;
270 LONG NewLength;
271
272 if (!Buffer)
273 return;
274
275 do
276 {
277 OldLength = Buffer->LogBufferLength;
278 NewLength = OldLength + (ULONG)Length;
279 if (NewLength > Buffer->LogBufferMaxLength)
280 return;
281 } while (InterlockedCompareExchange(&Buffer->LogBufferLength, NewLength, OldLength) != OldLength);
282
283 memcpy(&Buffer->LogBuffer[OldLength], String, Length);
284 }
285
286 KMT_FORMAT(ms_printf, 5, 0)
287 static SIZE_T KmtXVSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR FileAndLine, PCSTR Prepend, PCSTR Format, va_list Arguments)
288 {
289 SIZE_T BufferLength = 0;
290 SIZE_T Length;
291
292 if (FileAndLine)
293 {
294 PCSTR Slash;
295 Slash = strrchr(FileAndLine, '\\');
296 if (Slash)
297 FileAndLine = Slash + 1;
298 Slash = strrchr(FileAndLine, '/');
299 if (Slash)
300 FileAndLine = Slash + 1;
301
302 Length = min(BufferMaxLength, strlen(FileAndLine));
303 memcpy(Buffer, FileAndLine, Length);
304 Buffer += Length;
305 BufferLength += Length;
306 BufferMaxLength -= Length;
307 }
308 if (Prepend)
309 {
310 Length = min(BufferMaxLength, strlen(Prepend));
311 memcpy(Buffer, Prepend, Length);
312 Buffer += Length;
313 BufferLength += Length;
314 BufferMaxLength -= Length;
315 }
316 if (Format)
317 {
318 Length = KmtVSNPrintF(Buffer, BufferMaxLength, Format, Arguments);
319 /* vsnprintf can return more than maxLength, we don't want to do that */
320 BufferLength += min(Length, BufferMaxLength);
321 }
322 return BufferLength;
323 }
324
325 KMT_FORMAT(ms_printf, 5, 6)
326 static SIZE_T KmtXSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR FileAndLine, PCSTR Prepend, PCSTR Format, ...)
327 {
328 SIZE_T BufferLength;
329 va_list Arguments;
330 va_start(Arguments, Format);
331 BufferLength = KmtXVSNPrintF(Buffer, BufferMaxLength, FileAndLine, Prepend, Format, Arguments);
332 va_end(Arguments);
333 return BufferLength;
334 }
335
336 VOID KmtFinishTest(PCSTR TestName)
337 {
338 CHAR MessageBuffer[512];
339 SIZE_T MessageLength;
340
341 if (!ResultBuffer)
342 return;
343
344 MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, NULL, NULL,
345 "%s: %ld tests executed (0 marked as todo, %ld failures), %ld skipped.\n",
346 TestName,
347 ResultBuffer->Successes + ResultBuffer->Failures,
348 ResultBuffer->Failures,
349 ResultBuffer->Skipped);
350 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
351 }
352
353 BOOLEAN KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments)
354 {
355 CHAR MessageBuffer[512];
356 SIZE_T MessageLength;
357
358 if (!ResultBuffer)
359 return Condition != 0;
360
361 if (Condition)
362 {
363 InterlockedIncrement(&ResultBuffer->Successes);
364
365 if (0/*KmtReportSuccess*/)
366 {
367 MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Test succeeded\n", NULL);
368 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
369 }
370 }
371 else
372 {
373 InterlockedIncrement(&ResultBuffer->Failures);
374 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Test failed: ", Format, Arguments);
375 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
376 }
377
378 return Condition != 0;
379 }
380
381 BOOLEAN KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format, ...)
382 {
383 BOOLEAN Ret;
384 va_list Arguments;
385 va_start(Arguments, Format);
386 Ret = KmtVOk(Condition, FileAndLine, Format, Arguments);
387 va_end(Arguments);
388 return Ret;
389 }
390
391 VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments)
392 {
393 CHAR MessageBuffer[512];
394 SIZE_T MessageLength;
395
396 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": ", Format, Arguments);
397 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
398 }
399
400 VOID KmtTrace(PCSTR FileAndLine, PCSTR Format, ...)
401 {
402 va_list Arguments;
403 va_start(Arguments, Format);
404 KmtVTrace(FileAndLine, Format, Arguments);
405 va_end(Arguments);
406 }
407
408 BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments)
409 {
410 CHAR MessageBuffer[512];
411 SIZE_T MessageLength;
412
413 if (!ResultBuffer)
414 return !Condition;
415
416 if (!Condition)
417 {
418 InterlockedIncrement(&ResultBuffer->Skipped);
419 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Tests skipped: ", Format, Arguments);
420 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
421 }
422
423 return !Condition;
424 }
425
426 BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, ...)
427 {
428 BOOLEAN Ret;
429 va_list Arguments;
430 va_start(Arguments, Format);
431 Ret = KmtVSkip(Condition, FileAndLine, Format, Arguments);
432 va_end(Arguments);
433 return Ret;
434 }
435
436 PVOID KmtAllocateGuarded(SIZE_T SizeRequested)
437 {
438 NTSTATUS Status;
439 SIZE_T Size = PAGE_ROUND_UP(SizeRequested + PAGE_SIZE);
440 PVOID VirtualMemory = NULL;
441 PCHAR StartOfBuffer;
442
443 Status = ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory, 0, &Size, MEM_RESERVE, PAGE_NOACCESS);
444
445 if (!NT_SUCCESS(Status))
446 return NULL;
447
448 Size -= PAGE_SIZE;
449 Status = ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory, 0, &Size, MEM_COMMIT, PAGE_READWRITE);
450 if (!NT_SUCCESS(Status))
451 {
452 Size = 0;
453 Status = ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
454 ok_eq_hex(Status, STATUS_SUCCESS);
455 return NULL;
456 }
457
458 StartOfBuffer = VirtualMemory;
459 StartOfBuffer += Size - SizeRequested;
460
461 return StartOfBuffer;
462 }
463
464 VOID KmtFreeGuarded(PVOID Pointer)
465 {
466 NTSTATUS Status;
467 PVOID VirtualMemory = (PVOID)PAGE_ROUND_DOWN((SIZE_T)Pointer);
468 SIZE_T Size = 0;
469
470 Status = ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
471 ok_eq_hex(Status, STATUS_SUCCESS);
472 }
473
474 #endif /* defined KMT_DEFINE_TEST_FUNCTIONS */
475
476 #endif /* !defined _KMTEST_TEST_H_ */