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>
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 #elif defined KMT_USER_MODE
135 DWORD
KmtRunKernelTest(IN PCSTR TestName
);
137 VOID
KmtLoadDriver(IN PCWSTR ServiceName
, IN BOOLEAN RestartIfRunning
);
138 VOID
KmtUnloadDriver(VOID
);
139 VOID
KmtOpenDriver(VOID
);
140 VOID
KmtCloseDriver(VOID
);
142 DWORD
KmtSendToDriver(IN DWORD ControlCode
);
143 DWORD
KmtSendStringToDriver(IN DWORD ControlCode
, IN PCSTR String
);
144 DWORD
KmtSendWStringToDriver(IN DWORD ControlCode
, IN PCWSTR String
);
145 DWORD
KmtSendBufferToDriver(IN DWORD ControlCode
, IN OUT PVOID Buffer OPTIONAL
, IN DWORD InLength
, IN OUT PDWORD OutLength
);
146 #else /* if !defined KMT_KERNEL_MODE && !defined KMT_USER_MODE */
147 #error either KMT_KERNEL_MODE or KMT_USER_MODE must be defined
148 #endif /* !defined KMT_KERNEL_MODE && !defined KMT_USER_MODE */
150 extern PKMT_RESULTBUFFER ResultBuffer
;
153 /* TODO: GCC doesn't understand %wZ :( */
154 #define KMT_FORMAT(type, fmt, first) /*__attribute__((__format__(type, fmt, first)))*/
155 #elif !defined __GNUC__
156 #define KMT_FORMAT(type, fmt, first)
157 #endif /* !defined __GNUC__ */
159 #define START_TEST(name) VOID Test_##name(VOID)
161 #ifndef KMT_STRINGIZE
162 #define KMT_STRINGIZE(x) #x
163 #endif /* !defined KMT_STRINGIZE */
164 #define ok(test, ...) ok_(test, __FILE__, __LINE__, __VA_ARGS__)
165 #define trace(...) trace_( __FILE__, __LINE__, __VA_ARGS__)
166 #define skip(test, ...) skip_(test, __FILE__, __LINE__, __VA_ARGS__)
168 #define ok_(test, file, line, ...) KmtOk(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
169 #define trace_(file, line, ...) KmtTrace( file ":" KMT_STRINGIZE(line), __VA_ARGS__)
170 #define skip_(test, file, line, ...) KmtSkip(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
172 BOOLEAN
KmtVOk(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, va_list Arguments
) KMT_FORMAT(ms_printf
, 3, 0);
173 BOOLEAN
KmtOk(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, ...) KMT_FORMAT(ms_printf
, 3, 4);
174 VOID
KmtVTrace(PCSTR FileAndLine
, PCSTR Format
, va_list Arguments
) KMT_FORMAT(ms_printf
, 2, 0);
175 VOID
KmtTrace(PCSTR FileAndLine
, PCSTR Format
, ...) KMT_FORMAT(ms_printf
, 2, 3);
176 BOOLEAN
KmtVSkip(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, va_list Arguments
) KMT_FORMAT(ms_printf
, 3, 0);
177 BOOLEAN
KmtSkip(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, ...) KMT_FORMAT(ms_printf
, 3, 4);
178 PVOID
KmtAllocateGuarded(SIZE_T SizeRequested
);
179 VOID
KmtFreeGuarded(PVOID Pointer
);
181 #ifdef KMT_KERNEL_MODE
182 #define ok_irql(irql) ok(KeGetCurrentIrql() == irql, "IRQL is %d, expected %d\n", KeGetCurrentIrql(), irql)
183 #endif /* defined KMT_KERNEL_MODE */
184 #define ok_eq_print(value, expected, spec) ok((value) == (expected), #value " = " spec ", expected " spec "\n", value, expected)
185 #define ok_eq_pointer(value, expected) ok_eq_print(value, expected, "%p")
186 #define ok_eq_int(value, expected) ok_eq_print(value, expected, "%d")
187 #define ok_eq_uint(value, expected) ok_eq_print(value, expected, "%u")
188 #define ok_eq_long(value, expected) ok_eq_print(value, expected, "%ld")
189 #define ok_eq_ulong(value, expected) ok_eq_print(value, expected, "%lu")
190 #define ok_eq_longlong(value, expected) ok_eq_print(value, expected, "%I64d")
191 #define ok_eq_ulonglong(value, expected) ok_eq_print(value, expected, "%I64u")
193 #define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%lu")
194 #define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%ld")
195 #define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%lu")
197 #define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%I64u")
198 #define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%I64d")
199 #define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%I64u")
200 #endif /* defined _WIN64 */
201 #define ok_eq_hex(value, expected) ok_eq_print(value, expected, "0x%08lx")
202 #define ok_bool_true(value, desc) ok((value) == TRUE, desc " FALSE, expected TRUE\n")
203 #define ok_bool_false(value, desc) ok((value) == FALSE, desc " TRUE, expected FALSE\n")
204 #define ok_eq_bool(value, expected) ok((value) == (expected), #value " = %s, expected %s\n", \
205 (value) ? "TRUE" : "FALSE", \
206 (expected) ? "TRUE" : "FALSE")
207 #define ok_eq_str(value, expected) ok(!strcmp(value, expected), #value " = \"%s\", expected \"%s\"\n", value, expected)
208 #define ok_eq_wstr(value, expected) ok(!wcscmp(value, expected), #value " = \"%ls\", expected \"%ls\"\n", value, expected)
209 #define ok_eq_tag(value, expected) ok_eq_print(value, expected, "0x%08lx")
211 #define KMT_MAKE_CODE(ControlCode) CTL_CODE(FILE_DEVICE_UNKNOWN, \
212 0xC00 + (ControlCode), \
216 #define MICROSECOND 10
217 #define MILLISECOND (1000 * MICROSECOND)
218 #define SECOND (1000 * MILLISECOND)
220 #define KmtInvalidPointer ((PVOID)0x5555555555555555ULL)
222 #define KmtStartSeh() \
223 ExceptionStatus = STATUS_SUCCESS; \
226 #define KmtEndSeh(ExpectedStatus) \
228 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) \
230 ExceptionStatus = _SEH2_GetExceptionCode(); \
232 ok_eq_hex(ExceptionStatus, ExpectedStatus)
234 #if defined KMT_DEFINE_TEST_FUNCTIONS
236 #if defined KMT_KERNEL_MODE
237 BOOLEAN KmtIsCheckedBuild
;
238 BOOLEAN KmtIsMultiProcessorBuild
;
239 PCSTR KmtMajorFunctionNames
[] =
251 "QueryVolumeInformation",
252 "SetVolumeInformation",
256 "InternalDeviceControl/Scsi",
271 VOID
KmtSetIrql(IN KIRQL NewIrql
)
273 KIRQL Irql
= KeGetCurrentIrql();
275 KeLowerIrql(NewIrql
);
276 else if (Irql
< NewIrql
)
277 KeRaiseIrql(NewIrql
, &Irql
);
280 BOOLEAN
KmtAreInterruptsEnabled(VOID
)
282 return (__readeflags() & (1 << 9)) != 0;
285 typedef struct _POOL_HEADER
292 USHORT PreviousSize
:8;
297 USHORT PreviousSize
:9;
311 PEPROCESS ProcessBilled
;
317 USHORT AllocatorBackTraceIndex
;
321 } POOL_HEADER
, *PPOOL_HEADER
;
323 ULONG
KmtGetPoolTag(PVOID Memory
)
327 /* it's not so easy for allocations of PAGE_SIZE */
328 if (((ULONG_PTR
)Memory
& (PAGE_SIZE
- 1)) == 0)
334 return Header
->PoolTag
;
337 INT __cdecl
KmtVSNPrintF(PSTR Buffer
, SIZE_T BufferMaxLength
, PCSTR Format
, va_list Arguments
) KMT_FORMAT(ms_printf
, 3, 0);
338 #elif defined KMT_USER_MODE
339 static PKMT_RESULTBUFFER
KmtAllocateResultBuffer(SIZE_T ResultBufferSize
)
341 PKMT_RESULTBUFFER Buffer
= HeapAlloc(GetProcessHeap(), 0, ResultBufferSize
);
345 Buffer
->Successes
= 0;
346 Buffer
->Failures
= 0;
348 Buffer
->LogBufferLength
= 0;
349 Buffer
->LogBufferMaxLength
= (ULONG
)ResultBufferSize
- FIELD_OFFSET(KMT_RESULTBUFFER
, LogBuffer
);
354 static VOID
KmtFreeResultBuffer(PKMT_RESULTBUFFER Buffer
)
356 HeapFree(GetProcessHeap(), 0, Buffer
);
359 #define KmtVSNPrintF vsnprintf
360 #endif /* defined KMT_USER_MODE */
362 PKMT_RESULTBUFFER ResultBuffer
= NULL
;
364 static VOID
KmtAddToLogBuffer(PKMT_RESULTBUFFER Buffer
, PCSTR String
, SIZE_T Length
)
374 OldLength
= Buffer
->LogBufferLength
;
375 NewLength
= OldLength
+ (ULONG
)Length
;
376 if (NewLength
> Buffer
->LogBufferMaxLength
)
378 } while (InterlockedCompareExchange(&Buffer
->LogBufferLength
, NewLength
, OldLength
) != OldLength
);
380 memcpy(&Buffer
->LogBuffer
[OldLength
], String
, Length
);
383 KMT_FORMAT(ms_printf
, 5, 0)
384 static SIZE_T
KmtXVSNPrintF(PSTR Buffer
, SIZE_T BufferMaxLength
, PCSTR FileAndLine
, PCSTR Prepend
, PCSTR Format
, va_list Arguments
)
386 SIZE_T BufferLength
= 0;
392 Slash
= strrchr(FileAndLine
, '\\');
394 FileAndLine
= Slash
+ 1;
395 Slash
= strrchr(FileAndLine
, '/');
397 FileAndLine
= Slash
+ 1;
399 Length
= min(BufferMaxLength
, strlen(FileAndLine
));
400 memcpy(Buffer
, FileAndLine
, Length
);
402 BufferLength
+= Length
;
403 BufferMaxLength
-= Length
;
407 Length
= min(BufferMaxLength
, strlen(Prepend
));
408 memcpy(Buffer
, Prepend
, Length
);
410 BufferLength
+= Length
;
411 BufferMaxLength
-= Length
;
415 Length
= KmtVSNPrintF(Buffer
, BufferMaxLength
, Format
, Arguments
);
416 /* vsnprintf can return more than maxLength, we don't want to do that */
417 BufferLength
+= min(Length
, BufferMaxLength
);
422 KMT_FORMAT(ms_printf
, 5, 6)
423 static SIZE_T
KmtXSNPrintF(PSTR Buffer
, SIZE_T BufferMaxLength
, PCSTR FileAndLine
, PCSTR Prepend
, PCSTR Format
, ...)
427 va_start(Arguments
, Format
);
428 BufferLength
= KmtXVSNPrintF(Buffer
, BufferMaxLength
, FileAndLine
, Prepend
, Format
, Arguments
);
433 VOID
KmtFinishTest(PCSTR TestName
)
435 CHAR MessageBuffer
[512];
436 SIZE_T MessageLength
;
441 MessageLength
= KmtXSNPrintF(MessageBuffer
, sizeof MessageBuffer
, NULL
, NULL
,
442 "%s: %ld tests executed (0 marked as todo, %ld failures), %ld skipped.\n",
444 ResultBuffer
->Successes
+ ResultBuffer
->Failures
,
445 ResultBuffer
->Failures
,
446 ResultBuffer
->Skipped
);
447 KmtAddToLogBuffer(ResultBuffer
, MessageBuffer
, MessageLength
);
450 BOOLEAN
KmtVOk(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, va_list Arguments
)
452 CHAR MessageBuffer
[512];
453 SIZE_T MessageLength
;
456 return Condition
!= 0;
460 InterlockedIncrement(&ResultBuffer
->Successes
);
462 if (0/*KmtReportSuccess*/)
464 MessageLength
= KmtXSNPrintF(MessageBuffer
, sizeof MessageBuffer
, FileAndLine
, ": Test succeeded\n", NULL
);
465 KmtAddToLogBuffer(ResultBuffer
, MessageBuffer
, MessageLength
);
470 InterlockedIncrement(&ResultBuffer
->Failures
);
471 MessageLength
= KmtXVSNPrintF(MessageBuffer
, sizeof MessageBuffer
, FileAndLine
, ": Test failed: ", Format
, Arguments
);
472 KmtAddToLogBuffer(ResultBuffer
, MessageBuffer
, MessageLength
);
475 return Condition
!= 0;
478 BOOLEAN
KmtOk(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, ...)
482 va_start(Arguments
, Format
);
483 Ret
= KmtVOk(Condition
, FileAndLine
, Format
, Arguments
);
488 VOID
KmtVTrace(PCSTR FileAndLine
, PCSTR Format
, va_list Arguments
)
490 CHAR MessageBuffer
[512];
491 SIZE_T MessageLength
;
493 MessageLength
= KmtXVSNPrintF(MessageBuffer
, sizeof MessageBuffer
, FileAndLine
, ": ", Format
, Arguments
);
494 KmtAddToLogBuffer(ResultBuffer
, MessageBuffer
, MessageLength
);
497 VOID
KmtTrace(PCSTR FileAndLine
, PCSTR Format
, ...)
500 va_start(Arguments
, Format
);
501 KmtVTrace(FileAndLine
, Format
, Arguments
);
505 BOOLEAN
KmtVSkip(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, va_list Arguments
)
507 CHAR MessageBuffer
[512];
508 SIZE_T MessageLength
;
515 InterlockedIncrement(&ResultBuffer
->Skipped
);
516 MessageLength
= KmtXVSNPrintF(MessageBuffer
, sizeof MessageBuffer
, FileAndLine
, ": Tests skipped: ", Format
, Arguments
);
517 KmtAddToLogBuffer(ResultBuffer
, MessageBuffer
, MessageLength
);
523 BOOLEAN
KmtSkip(INT Condition
, PCSTR FileAndLine
, PCSTR Format
, ...)
527 va_start(Arguments
, Format
);
528 Ret
= KmtVSkip(Condition
, FileAndLine
, Format
, Arguments
);
533 PVOID
KmtAllocateGuarded(SIZE_T SizeRequested
)
536 SIZE_T Size
= PAGE_ROUND_UP(SizeRequested
+ PAGE_SIZE
);
537 PVOID VirtualMemory
= NULL
;
540 Status
= ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory
, 0, &Size
, MEM_RESERVE
, PAGE_NOACCESS
);
542 if (!NT_SUCCESS(Status
))
546 Status
= ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory
, 0, &Size
, MEM_COMMIT
, PAGE_READWRITE
);
547 if (!NT_SUCCESS(Status
))
550 Status
= ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory
, &Size
, MEM_RELEASE
);
551 ok_eq_hex(Status
, STATUS_SUCCESS
);
555 StartOfBuffer
= VirtualMemory
;
556 StartOfBuffer
+= Size
- SizeRequested
;
558 return StartOfBuffer
;
561 VOID
KmtFreeGuarded(PVOID Pointer
)
564 PVOID VirtualMemory
= (PVOID
)PAGE_ROUND_DOWN((SIZE_T
)Pointer
);
567 Status
= ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory
, &Size
, MEM_RELEASE
);
568 ok_eq_hex(Status
, STATUS_SUCCESS
);
571 #endif /* defined KMT_DEFINE_TEST_FUNCTIONS */
573 #endif /* !defined _KMTEST_TEST_H_ */