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>
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
13 #ifndef _KMTEST_TEST_H_
14 #define _KMTEST_TEST_H_
16 #include <kmt_platform.h>
18 typedef VOID
KMT_TESTFUNC(VOID
);
19 typedef KMT_TESTFUNC
*PKMT_TESTFUNC
;
24 KMT_TESTFUNC
*TestFunction
;
25 } KMT_TEST
, *PKMT_TEST
;
27 typedef const KMT_TEST CKMT_TEST
, *PCKMT_TEST
;
29 extern const KMT_TEST TestList
[];
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
;
41 #ifndef KMT_STANDALONE_DRIVER
43 /* usermode call-back mechanism */
45 /* list of supported operations */
46 typedef enum _KMT_CALLBACK_INFORMATION_CLASS
49 } KMT_CALLBACK_INFORMATION_CLASS
, *PKMT_CALLBACK_INFORMATION_CLASS
;
51 /* TODO: "response" is a little generic */
52 typedef union _KMT_RESPONSE
54 MEMORY_BASIC_INFORMATION MemInfo
;
55 } KMT_RESPONSE
, *PKMT_RESPONSE
;
57 /* this struct is sent from driver to usermode */
58 typedef struct _KMT_CALLBACK_REQUEST_PACKET
61 KMT_CALLBACK_INFORMATION_CLASS OperationClass
;
63 } KMT_CALLBACK_REQUEST_PACKET
, *PKMT_CALLBACK_REQUEST_PACKET
;
65 PKMT_RESPONSE
KmtUserModeCallback(KMT_CALLBACK_INFORMATION_CLASS Operation
, PVOID Parameters
);
66 VOID
KmtFreeCallbackResponse(PKMT_RESPONSE Response
);
68 //macro to simplify using the mechanism
69 #define Test_NtQueryVirtualMemory(BaseAddress, Size, AllocationType, ProtectionType) \
71 PKMT_RESPONSE NtQueryTest = KmtUserModeCallback(QueryVirtualMemory, BaseAddress); \
72 if (NtQueryTest != NULL) \
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); \
83 #ifdef KMT_STANDALONE_DRIVER
84 #define KMT_KERNEL_MODE
86 typedef NTSTATUS (KMT_IRP_HANDLER
)(
87 IN PDEVICE_OBJECT DeviceObject
,
89 IN PIO_STACK_LOCATION IoStackLocation
);
90 typedef KMT_IRP_HANDLER
*PKMT_IRP_HANDLER
;
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
);
95 typedef NTSTATUS (KMT_MESSAGE_HANDLER
)(
96 IN PDEVICE_OBJECT DeviceObject
,
98 IN PVOID Buffer OPTIONAL
,
100 IN OUT PSIZE_T OutLength
);
101 typedef KMT_MESSAGE_HANDLER
*PKMT_MESSAGE_HANDLER
;
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
);
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 } KMT_TESTENTRY_FLAGS
;
114 NTSTATUS
TestEntry(IN PDRIVER_OBJECT DriverObject
, IN PCUNICODE_STRING RegistryPath
, OUT PCWSTR
*DeviceName
, IN OUT INT
*Flags
);
115 VOID
TestUnload(IN PDRIVER_OBJECT DriverObject
);
116 #endif /* defined KMT_STANDALONE_DRIVER */
118 #ifdef KMT_KERNEL_MODE
119 /* Device Extension layout */
122 PKMT_RESULTBUFFER ResultBuffer
;
124 } KMT_DEVICE_EXTENSION
, *PKMT_DEVICE_EXTENSION
;
126 extern BOOLEAN KmtIsCheckedBuild
;
127 extern BOOLEAN KmtIsMultiProcessorBuild
;
128 extern PCSTR KmtMajorFunctionNames
[];
129 extern PDRIVER_OBJECT KmtDriverObject
;
131 VOID
KmtSetIrql(IN KIRQL NewIrql
);
132 BOOLEAN
KmtAreInterruptsEnabled(VOID
);
133 ULONG
KmtGetPoolTag(PVOID Memory
);
134 USHORT
KmtGetPoolType(PVOID Memory
);
135 #elif defined KMT_USER_MODE
136 DWORD
KmtRunKernelTest(IN PCSTR TestName
);
138 VOID
KmtLoadDriver(IN PCWSTR ServiceName
, IN BOOLEAN RestartIfRunning
);
139 VOID
KmtUnloadDriver(VOID
);
140 VOID
KmtOpenDriver(VOID
);
141 VOID
KmtCloseDriver(VOID
);
143 DWORD
KmtSendToDriver(IN DWORD ControlCode
);
144 DWORD
KmtSendStringToDriver(IN DWORD ControlCode
, IN PCSTR String
);
145 DWORD
KmtSendWStringToDriver(IN DWORD ControlCode
, IN PCWSTR String
);
146 DWORD
KmtSendBufferToDriver(IN DWORD ControlCode
, IN OUT PVOID Buffer OPTIONAL
, IN DWORD InLength
, IN OUT PDWORD OutLength
);
147 #else /* if !defined KMT_KERNEL_MODE && !defined KMT_USER_MODE */
148 #error either KMT_KERNEL_MODE or KMT_USER_MODE must be defined
149 #endif /* !defined KMT_KERNEL_MODE && !defined KMT_USER_MODE */
151 extern PKMT_RESULTBUFFER ResultBuffer
;
154 /* TODO: GCC doesn't understand %wZ :( */
155 #define KMT_FORMAT(type, fmt, first) /*__attribute__((__format__(type, fmt, first)))*/
156 #elif !defined __GNUC__
157 #define KMT_FORMAT(type, fmt, first)
158 #endif /* !defined __GNUC__ */
160 #define START_TEST(name) VOID Test_##name(VOID)
162 #ifndef KMT_STRINGIZE
163 #define KMT_STRINGIZE(x) #x
164 #endif /* !defined KMT_STRINGIZE */
165 #define ok(test, ...) ok_(test, __FILE__, __LINE__, __VA_ARGS__)
166 #define trace(...) trace_( __FILE__, __LINE__, __VA_ARGS__)
167 #define skip(test, ...) skip_(test, __FILE__, __LINE__, __VA_ARGS__)
169 #define ok_(test, file, line, ...) KmtOk(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
170 #define trace_(file, line, ...) KmtTrace( file ":" KMT_STRINGIZE(line), __VA_ARGS__)
171 #define skip_(test, file, line, ...) KmtSkip(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
173 BOOLEAN
KmtVOk(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, va_list Arguments
) KMT_FORMAT(ms_printf
, 3, 0);
174 BOOLEAN
KmtOk(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, ...) KMT_FORMAT(ms_printf
, 3, 4);
175 VOID
KmtVTrace(PCSTR FileAndLine
, PCSTR Format
, va_list Arguments
) KMT_FORMAT(ms_printf
, 2, 0);
176 VOID
KmtTrace(PCSTR FileAndLine
, PCSTR Format
, ...) KMT_FORMAT(ms_printf
, 2, 3);
177 BOOLEAN
KmtVSkip(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, va_list Arguments
) KMT_FORMAT(ms_printf
, 3, 0);
178 BOOLEAN
KmtSkip(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, ...) KMT_FORMAT(ms_printf
, 3, 4);
179 PVOID
KmtAllocateGuarded(SIZE_T SizeRequested
);
180 VOID
KmtFreeGuarded(PVOID Pointer
);
182 #ifdef KMT_KERNEL_MODE
183 #define ok_irql(irql) ok(KeGetCurrentIrql() == irql, "IRQL is %d, expected %d\n", KeGetCurrentIrql(), irql)
184 #endif /* defined KMT_KERNEL_MODE */
185 #define ok_eq_print(value, expected, spec) ok((value) == (expected), #value " = " spec ", expected " spec "\n", value, expected)
186 #define ok_eq_pointer(value, expected) ok_eq_print(value, expected, "%p")
187 #define ok_eq_int(value, expected) ok_eq_print(value, expected, "%d")
188 #define ok_eq_uint(value, expected) ok_eq_print(value, expected, "%u")
189 #define ok_eq_long(value, expected) ok_eq_print(value, expected, "%ld")
190 #define ok_eq_ulong(value, expected) ok_eq_print(value, expected, "%lu")
191 #define ok_eq_longlong(value, expected) ok_eq_print(value, expected, "%I64d")
192 #define ok_eq_ulonglong(value, expected) ok_eq_print(value, expected, "%I64u")
193 #define ok_eq_char(value, expected) ok_eq_print(value, expected, "%c")
194 #define ok_eq_wchar(value, expected) ok_eq_print(value, expected, "%C")
196 #define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%lu")
197 #define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%ld")
198 #define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%lu")
200 #define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%I64u")
201 #define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%I64d")
202 #define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%I64u")
203 #endif /* defined _WIN64 */
204 #define ok_eq_hex(value, expected) ok_eq_print(value, expected, "0x%08lx")
205 #define ok_bool_true(value, desc) ok((value) == TRUE, desc " FALSE, expected TRUE\n")
206 #define ok_bool_false(value, desc) ok((value) == FALSE, desc " TRUE, expected FALSE\n")
207 #define ok_eq_bool(value, expected) ok((value) == (expected), #value " = %s, expected %s\n", \
208 (value) ? "TRUE" : "FALSE", \
209 (expected) ? "TRUE" : "FALSE")
210 #define ok_eq_str(value, expected) ok(!strcmp(value, expected), #value " = \"%s\", expected \"%s\"\n", value, expected)
211 #define ok_eq_wstr(value, expected) ok(!wcscmp(value, expected), #value " = \"%ls\", expected \"%ls\"\n", value, expected)
212 #define ok_eq_tag(value, expected) ok_eq_print(value, expected, "0x%08lx")
214 #define KMT_MAKE_CODE(ControlCode) CTL_CODE(FILE_DEVICE_UNKNOWN, \
215 0xC00 + (ControlCode), \
219 #define MICROSECOND 10
220 #define MILLISECOND (1000 * MICROSECOND)
221 #define SECOND (1000 * MILLISECOND)
223 /* See apitests/include/apitest.h */
224 #define KmtInvalidPointer ((PVOID)0x5555555555555555ULL)
226 #define KmtStartSeh() \
228 NTSTATUS ExceptionStatus = STATUS_SUCCESS; \
232 #define KmtEndSeh(ExpectedStatus) \
234 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) \
236 ExceptionStatus = _SEH2_GetExceptionCode(); \
239 ok_eq_hex(ExceptionStatus, (ExpectedStatus)); \
242 #if defined KMT_DEFINE_TEST_FUNCTIONS
244 #if defined KMT_KERNEL_MODE
245 BOOLEAN KmtIsCheckedBuild
;
246 BOOLEAN KmtIsMultiProcessorBuild
;
247 PCSTR KmtMajorFunctionNames
[] =
259 "QueryVolumeInformation",
260 "SetVolumeInformation",
264 "InternalDeviceControl/Scsi",
279 VOID
KmtSetIrql(IN KIRQL NewIrql
)
281 KIRQL Irql
= KeGetCurrentIrql();
283 KeLowerIrql(NewIrql
);
284 else if (Irql
< NewIrql
)
285 KeRaiseIrql(NewIrql
, &Irql
);
288 BOOLEAN
KmtAreInterruptsEnabled(VOID
)
290 return (__readeflags() & (1 << 9)) != 0;
293 typedef struct _POOL_HEADER
300 USHORT PreviousSize
:8;
305 USHORT PreviousSize
:9;
319 PEPROCESS ProcessBilled
;
325 USHORT AllocatorBackTraceIndex
;
329 } POOL_HEADER
, *PPOOL_HEADER
;
331 ULONG
KmtGetPoolTag(PVOID Memory
)
335 /* it's not so easy for allocations of PAGE_SIZE */
336 if (((ULONG_PTR
)Memory
& (PAGE_SIZE
- 1)) == 0)
342 return Header
->PoolTag
;
345 USHORT
KmtGetPoolType(PVOID Memory
)
349 /* it's not so easy for allocations of PAGE_SIZE */
350 if (((ULONG_PTR
)Memory
& (PAGE_SIZE
- 1)) == 0)
356 return Header
->PoolType
;
359 INT __cdecl
KmtVSNPrintF(PSTR Buffer
, SIZE_T BufferMaxLength
, PCSTR Format
, va_list Arguments
) KMT_FORMAT(ms_printf
, 3, 0);
360 #elif defined KMT_USER_MODE
361 static PKMT_RESULTBUFFER
KmtAllocateResultBuffer(SIZE_T ResultBufferSize
)
363 PKMT_RESULTBUFFER Buffer
= HeapAlloc(GetProcessHeap(), 0, ResultBufferSize
);
367 Buffer
->Successes
= 0;
368 Buffer
->Failures
= 0;
370 Buffer
->LogBufferLength
= 0;
371 Buffer
->LogBufferMaxLength
= (ULONG
)ResultBufferSize
- FIELD_OFFSET(KMT_RESULTBUFFER
, LogBuffer
);
376 static VOID
KmtFreeResultBuffer(PKMT_RESULTBUFFER Buffer
)
378 HeapFree(GetProcessHeap(), 0, Buffer
);
381 #define KmtVSNPrintF vsnprintf
382 #endif /* defined KMT_USER_MODE */
384 PKMT_RESULTBUFFER ResultBuffer
= NULL
;
386 static VOID
KmtAddToLogBuffer(PKMT_RESULTBUFFER Buffer
, PCSTR String
, SIZE_T Length
)
396 OldLength
= Buffer
->LogBufferLength
;
397 NewLength
= OldLength
+ (ULONG
)Length
;
398 if (NewLength
> Buffer
->LogBufferMaxLength
)
400 } while (InterlockedCompareExchange(&Buffer
->LogBufferLength
, NewLength
, OldLength
) != OldLength
);
402 memcpy(&Buffer
->LogBuffer
[OldLength
], String
, Length
);
405 KMT_FORMAT(ms_printf
, 5, 0)
406 static SIZE_T
KmtXVSNPrintF(PSTR Buffer
, SIZE_T BufferMaxLength
, PCSTR FileAndLine
, PCSTR Prepend
, PCSTR Format
, va_list Arguments
)
408 SIZE_T BufferLength
= 0;
414 Slash
= strrchr(FileAndLine
, '\\');
416 FileAndLine
= Slash
+ 1;
417 Slash
= strrchr(FileAndLine
, '/');
419 FileAndLine
= Slash
+ 1;
421 Length
= min(BufferMaxLength
, strlen(FileAndLine
));
422 memcpy(Buffer
, FileAndLine
, Length
);
424 BufferLength
+= Length
;
425 BufferMaxLength
-= Length
;
429 Length
= min(BufferMaxLength
, strlen(Prepend
));
430 memcpy(Buffer
, Prepend
, Length
);
432 BufferLength
+= Length
;
433 BufferMaxLength
-= Length
;
437 Length
= KmtVSNPrintF(Buffer
, BufferMaxLength
, Format
, Arguments
);
438 /* vsnprintf can return more than maxLength, we don't want to do that */
439 BufferLength
+= min(Length
, BufferMaxLength
);
444 KMT_FORMAT(ms_printf
, 5, 6)
445 static SIZE_T
KmtXSNPrintF(PSTR Buffer
, SIZE_T BufferMaxLength
, PCSTR FileAndLine
, PCSTR Prepend
, PCSTR Format
, ...)
449 va_start(Arguments
, Format
);
450 BufferLength
= KmtXVSNPrintF(Buffer
, BufferMaxLength
, FileAndLine
, Prepend
, Format
, Arguments
);
455 VOID
KmtFinishTest(PCSTR TestName
)
457 CHAR MessageBuffer
[512];
458 SIZE_T MessageLength
;
463 MessageLength
= KmtXSNPrintF(MessageBuffer
, sizeof MessageBuffer
, NULL
, NULL
,
464 "%s: %ld tests executed (0 marked as todo, %ld failures), %ld skipped.\n",
466 ResultBuffer
->Successes
+ ResultBuffer
->Failures
,
467 ResultBuffer
->Failures
,
468 ResultBuffer
->Skipped
);
469 KmtAddToLogBuffer(ResultBuffer
, MessageBuffer
, MessageLength
);
472 BOOLEAN
KmtVOk(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, va_list Arguments
)
474 CHAR MessageBuffer
[512];
475 SIZE_T MessageLength
;
478 return Condition
!= 0;
482 InterlockedIncrement(&ResultBuffer
->Successes
);
484 if (0/*KmtReportSuccess*/)
486 MessageLength
= KmtXSNPrintF(MessageBuffer
, sizeof MessageBuffer
, FileAndLine
, ": Test succeeded\n", NULL
);
487 KmtAddToLogBuffer(ResultBuffer
, MessageBuffer
, MessageLength
);
492 InterlockedIncrement(&ResultBuffer
->Failures
);
493 MessageLength
= KmtXVSNPrintF(MessageBuffer
, sizeof MessageBuffer
, FileAndLine
, ": Test failed: ", Format
, Arguments
);
494 KmtAddToLogBuffer(ResultBuffer
, MessageBuffer
, MessageLength
);
497 return Condition
!= 0;
500 BOOLEAN
KmtOk(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, ...)
504 va_start(Arguments
, Format
);
505 Ret
= KmtVOk(Condition
, FileAndLine
, Format
, Arguments
);
510 VOID
KmtVTrace(PCSTR FileAndLine
, PCSTR Format
, va_list Arguments
)
512 CHAR MessageBuffer
[512];
513 SIZE_T MessageLength
;
515 MessageLength
= KmtXVSNPrintF(MessageBuffer
, sizeof MessageBuffer
, FileAndLine
, ": ", Format
, Arguments
);
516 KmtAddToLogBuffer(ResultBuffer
, MessageBuffer
, MessageLength
);
519 VOID
KmtTrace(PCSTR FileAndLine
, PCSTR Format
, ...)
522 va_start(Arguments
, Format
);
523 KmtVTrace(FileAndLine
, Format
, Arguments
);
527 BOOLEAN
KmtVSkip(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, va_list Arguments
)
529 CHAR MessageBuffer
[512];
530 SIZE_T MessageLength
;
537 InterlockedIncrement(&ResultBuffer
->Skipped
);
538 MessageLength
= KmtXVSNPrintF(MessageBuffer
, sizeof MessageBuffer
, FileAndLine
, ": Tests skipped: ", Format
, Arguments
);
539 KmtAddToLogBuffer(ResultBuffer
, MessageBuffer
, MessageLength
);
545 BOOLEAN
KmtSkip(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, ...)
549 va_start(Arguments
, Format
);
550 Ret
= KmtVSkip(Condition
, FileAndLine
, Format
, Arguments
);
555 PVOID
KmtAllocateGuarded(SIZE_T SizeRequested
)
558 SIZE_T Size
= PAGE_ROUND_UP(SizeRequested
+ PAGE_SIZE
);
559 PVOID VirtualMemory
= NULL
;
562 Status
= ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory
, 0, &Size
, MEM_RESERVE
, PAGE_NOACCESS
);
564 if (!NT_SUCCESS(Status
))
568 Status
= ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory
, 0, &Size
, MEM_COMMIT
, PAGE_READWRITE
);
569 if (!NT_SUCCESS(Status
))
572 Status
= ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory
, &Size
, MEM_RELEASE
);
573 ok_eq_hex(Status
, STATUS_SUCCESS
);
577 StartOfBuffer
= VirtualMemory
;
578 StartOfBuffer
+= Size
- SizeRequested
;
580 return StartOfBuffer
;
583 VOID
KmtFreeGuarded(PVOID Pointer
)
586 PVOID VirtualMemory
= (PVOID
)PAGE_ROUND_DOWN((SIZE_T
)Pointer
);
589 Status
= ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory
, &Size
, MEM_RELEASE
);
590 ok_eq_hex(Status
, STATUS_SUCCESS
);
593 #endif /* defined KMT_DEFINE_TEST_FUNCTIONS */
595 #endif /* !defined _KMTEST_TEST_H_ */