16#ifndef _KMTEST_TEST_H_
17#define _KMTEST_TEST_H_
44#ifndef KMT_STANDALONE_DRIVER
72#define Test_NtQueryVirtualMemory(BaseAddress, Size, AllocationType, ProtectionType) \
74 PKMT_RESPONSE NtQueryTest = KmtUserModeCallback(QueryVirtualMemory, BaseAddress); \
75 if (NtQueryTest != NULL) \
77 ok_eq_hex(NtQueryTest->MemInfo.Protect, ProtectionType); \
78 ok_eq_hex(NtQueryTest->MemInfo.State, AllocationType); \
79 ok_eq_size(NtQueryTest->MemInfo.RegionSize, Size); \
80 KmtFreeCallbackResponse(NtQueryTest); \
86#ifdef KMT_STANDALONE_DRIVER
87#define KMT_KERNEL_MODE
93typedef KMT_IRP_HANDLER *PKMT_IRP_HANDLER;
98typedef NTSTATUS (KMT_MESSAGE_HANDLER)(
104typedef KMT_MESSAGE_HANDLER *PKMT_MESSAGE_HANDLER;
111 TESTENTRY_NO_CREATE_DEVICE = 1,
112 TESTENTRY_NO_REGISTER_DISPATCH = 2,
113 TESTENTRY_NO_REGISTER_UNLOAD = 4,
114 TESTENTRY_NO_EXCLUSIVE_DEVICE = 8,
115 TESTENTRY_NO_READONLY_DEVICE = 16,
116 TESTENTRY_BUFFERED_IO_DEVICE = 32,
117} KMT_TESTENTRY_FLAGS;
123#ifdef KMT_FILTER_DRIVER
124#ifndef KMT_KERNEL_MODE
125#define KMT_KERNEL_MODE
132 TESTENTRY_NO_REGISTER_FILTER = 0x01,
133 TESTENTRY_NO_CREATE_COMMS_PORT = 0x02,
134 TESTENTRY_NO_START_FILTERING = 0x04,
135 TESTENTRY_NO_INSTANCE_SETUP = 0x08,
136 TESTENTRY_NO_QUERY_TEARDOWN = 0x10,
137 TESTENTRY_NO_ALL = 0xFF
138} KMT_MINIFILTER_FLAGS;
149#ifdef KMT_KERNEL_MODE
155} KMT_DEVICE_EXTENSION, *PKMT_DEVICE_EXTENSION;
169#elif defined KMT_USER_MODE
201#error either KMT_KERNEL_MODE or KMT_USER_MODE must be defined
208#define KMT_FORMAT(type, fmt, first)
209#elif !defined __GNUC__
210#define KMT_FORMAT(type, fmt, first)
213#define START_TEST(name) VOID Test_##name(VOID)
216#define KMT_STRINGIZE(x) #x
218#define ok(test, ...) ok_(test, __FILE__, __LINE__, __VA_ARGS__)
219#define trace(...) trace_( __FILE__, __LINE__, __VA_ARGS__)
220#define skip(test, ...) skip_(test, __FILE__, __LINE__, __VA_ARGS__)
222#define ok_(test, file, line, ...) KmtOk(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
223#define trace_(file, line, ...) KmtTrace( file ":" KMT_STRINGIZE(line), __VA_ARGS__)
224#define skip_(test, file, line, ...) KmtSkip(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
235#ifdef KMT_KERNEL_MODE
236#define ok_irql(irql) ok(KeGetCurrentIrql() == irql, "IRQL is %d, expected %d\n", KeGetCurrentIrql(), irql)
238#define ok_eq_print(value, expected, spec) ok((value) == (expected), #value " = " spec ", expected " spec "\n", value, expected)
239#define ok_eq_pointer(value, expected) ok_eq_print(value, expected, "%p")
240#define ok_eq_int(value, expected) ok_eq_print(value, expected, "%d")
241#define ok_eq_uint(value, expected) ok_eq_print(value, expected, "%u")
242#define ok_eq_long(value, expected) ok_eq_print(value, expected, "%ld")
243#define ok_eq_ulong(value, expected) ok_eq_print(value, expected, "%lu")
244#define ok_eq_longlong(value, expected) ok_eq_print(value, expected, "%I64d")
245#define ok_eq_ulonglong(value, expected) ok_eq_print(value, expected, "%I64u")
246#define ok_eq_char(value, expected) ok_eq_print(value, expected, "%c")
247#define ok_eq_wchar(value, expected) ok_eq_print(value, expected, "%C")
249#define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%lu")
250#define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%ld")
251#define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%lu")
253#define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%I64u")
254#define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%I64d")
255#define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%I64u")
257#define ok_eq_hex(value, expected) ok_eq_print(value, expected, "0x%08lx")
258#define ok_bool_true(value, desc) ok((value) == TRUE, desc " FALSE, expected TRUE\n")
259#define ok_bool_false(value, desc) ok((value) == FALSE, desc " TRUE, expected FALSE\n")
260#define ok_eq_bool(value, expected) ok((value) == (expected), #value " = %s, expected %s\n", \
261 (value) ? "TRUE" : "FALSE", \
262 (expected) ? "TRUE" : "FALSE")
263#define ok_eq_str(value, expected) ok(!strcmp(value, expected), #value " = \"%s\", expected \"%s\"\n", value, expected)
264#define ok_eq_wstr(value, expected) ok(!wcscmp(value, expected), #value " = \"%ls\", expected \"%ls\"\n", value, expected)
265#define ok_eq_tag(value, expected) ok_eq_print(value, expected, "0x%08lx")
267#define ok_eq_hex64(value, expected) ok_eq_print(value, expected, "%I64x")
268#define ok_eq_xmm(value, expected) ok((value).Low == (expected).Low, #value " = %I64x'%08I64x, expected %I64x'%08I64x\n", (value).Low, (value).High, (expected).Low, (expected).High)
270#define KMT_MAKE_CODE(ControlCode) CTL_CODE(FILE_DEVICE_UNKNOWN, \
271 0xC00 + (ControlCode), \
275#define MICROSECOND 10
276#define MILLISECOND (1000 * MICROSECOND)
277#define SECOND (1000 * MILLISECOND)
280#define KmtInvalidPointer ((PVOID)0x5555555555555555ULL)
282#define KmtStartSeh() \
284 NTSTATUS ExceptionStatus = STATUS_SUCCESS; \
288#define KmtEndSeh(ExpectedStatus) \
290 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) \
292 ExceptionStatus = _SEH2_GetExceptionCode(); \
295 ok_eq_hex(ExceptionStatus, (ExpectedStatus)); \
298#define KmtGetSystemOrEmbeddedRoutineAddress(RoutineName) \
299 p##RoutineName = KmtGetSystemRoutineAddress(L ## #RoutineName); \
300 if (!p##RoutineName) \
302 p##RoutineName = RoutineName; \
303 trace("Using embedded routine for " #RoutineName "\n"); \
306 trace("Using system routine for " #RoutineName "\n");
308#if defined KMT_DEFINE_TEST_FUNCTIONS
310#if defined KMT_KERNEL_MODE
312#elif defined KMT_USER_MODE
328 OldLength =
Buffer->LogBufferLength;
346 Slash =
strrchr(FileAndLine,
'\\');
348 FileAndLine = Slash + 1;
349 Slash =
strrchr(FileAndLine,
'/');
351 FileAndLine = Slash + 1;
357 BufferMaxLength -=
Length;
365 BufferMaxLength -=
Length;
389 CHAR MessageBuffer[512];
395 MessageLength = KmtXSNPrintF(MessageBuffer,
sizeof MessageBuffer,
NULL,
NULL,
396 "%s: %ld tests executed (0 marked as todo, %ld failures), %ld skipped.\n",
401 KmtAddToLogBuffer(
ResultBuffer, MessageBuffer, MessageLength);
406 CHAR MessageBuffer[512];
418 MessageLength = KmtXSNPrintF(MessageBuffer,
sizeof MessageBuffer, FileAndLine,
": Test succeeded\n",
NULL);
419 KmtAddToLogBuffer(
ResultBuffer, MessageBuffer, MessageLength);
425 MessageLength = KmtXVSNPrintF(MessageBuffer,
sizeof MessageBuffer, FileAndLine,
": Test failed: ",
Format, Arguments);
426 KmtAddToLogBuffer(
ResultBuffer, MessageBuffer, MessageLength);
444 CHAR MessageBuffer[512];
447 MessageLength = KmtXVSNPrintF(MessageBuffer,
sizeof MessageBuffer, FileAndLine,
": ",
Format, Arguments);
448 KmtAddToLogBuffer(
ResultBuffer, MessageBuffer, MessageLength);
461 CHAR MessageBuffer[512];
470 MessageLength = KmtXVSNPrintF(MessageBuffer,
sizeof MessageBuffer, FileAndLine,
": Tests skipped: ",
Format, Arguments);
471 KmtAddToLogBuffer(
ResultBuffer, MessageBuffer, MessageLength);
509 StartOfBuffer = VirtualMemory;
510 StartOfBuffer +=
Size - SizeRequested;
512 return StartOfBuffer;
VOID TestUnload(IN PDRIVER_OBJECT DriverObject)
static USHORT USHORT * NewLength
ACPI_SIZE strlen(const char *String)
#define InterlockedIncrement
static WCHAR ServiceName[]
#define NT_SUCCESS(StatCode)
static const WCHAR Message[]
_Must_inspect_result_ _Inout_opt_ PUNICODE_STRING VolumeName
ULONG FLT_INSTANCE_SETUP_FLAGS
VOID(FLTAPI * PFLT_DISCONNECT_NOTIFY)(_In_opt_ PVOID ConnectionCookie)
NTSTATUS(FLTAPI * PFLT_MESSAGE_NOTIFY)(_In_opt_ PVOID PortCookie, _In_reads_bytes_opt_(InputBufferLength) PVOID InputBuffer, _In_ ULONG InputBufferLength, _Out_writes_bytes_to_opt_(OutputBufferLength, *ReturnOutputBufferLength) PVOID OutputBuffer, _In_ ULONG OutputBufferLength, _Out_ PULONG ReturnOutputBufferLength)
_Must_inspect_result_ _Outptr_ PFLT_PORT _In_ POBJECT_ATTRIBUTES _In_opt_ PVOID _In_ PFLT_CONNECT_NOTIFY _In_ PFLT_DISCONNECT_NOTIFY _In_opt_ PFLT_MESSAGE_NOTIFY MessageNotifyCallback
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
NTSTATUS(FLTAPI * PFLT_CONNECT_NOTIFY)(_In_ PFLT_PORT ClientPort, _In_opt_ PVOID ServerPortCookie, _In_reads_bytes_opt_(SizeOfContext) PVOID ConnectionContext, _In_ ULONG SizeOfContext, _Outptr_result_maybenull_ PVOID *ConnectionPortCookie)
ULONG FLT_INSTANCE_QUERY_TEARDOWN_FLAGS
_Must_inspect_result_ _Inout_ PFLT_VOLUME _In_ PCUNICODE_STRING Altitude
_Must_inspect_result_ _Outptr_ PFLT_PORT _In_ POBJECT_ATTRIBUTES _In_opt_ PVOID _In_ PFLT_CONNECT_NOTIFY ConnectNotifyCallback
_Must_inspect_result_ _Outptr_ PFLT_PORT _In_ POBJECT_ATTRIBUTES _In_opt_ PVOID _In_ PFLT_CONNECT_NOTIFY _In_ PFLT_DISCONNECT_NOTIFY DisconnectNotifyCallback
VOID TestQueryTeardown(_In_ PCFLT_RELATED_OBJECTS FltObjects, _In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags)
NTSTATUS TestInstanceSetup(_In_ PCFLT_RELATED_OBJECTS FltObjects, _In_ FLT_INSTANCE_SETUP_FLAGS Flags, _In_ DEVICE_TYPE VolumeDeviceType, _In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType, _In_ PUNICODE_STRING VolumeName, _In_ ULONG SectorSize, _In_ ULONG ReportedSectorSize)
VOID TestFilterUnload(IN ULONG Flags)
DWORD KmtFltSendToDriver(_In_ HANDLE hPort, _In_ DWORD Message)
DWORD KmtFltCreateService(_In_z_ PCWSTR ServiceName, _In_z_ PCWSTR DisplayName, _Out_ SC_HANDLE *ServiceHandle)
DWORD KmtFltRunKernelTest(_In_ HANDLE hPort, _In_z_ PCSTR TestName)
DWORD KmtFltSendUlongToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ DWORD Value)
DWORD KmtFltDisconnectComms(_In_ HANDLE hPort)
DWORD KmtFltSendBufferToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_reads_bytes_(BufferSize) LPVOID InBuffer, _In_ DWORD BufferSize, _Out_writes_bytes_to_opt_(OutBufferSize, *BytesReturned) LPVOID OutBuffer, _In_ DWORD OutBufferSize, _Out_opt_ LPDWORD BytesReturned)
DWORD KmtFltSendStringToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ PCSTR String)
DWORD KmtFltLoadDriver(_In_ BOOLEAN EnableDriverLoadPrivilege, _In_ BOOLEAN RestartIfRunning, _In_ BOOLEAN ConnectComms, _Out_ HANDLE *hPort)
DWORD KmtFltAddAltitude(_In_z_ LPWSTR Altitude)
DWORD KmtFltUnloadDriver(_In_ HANDLE *hPort, _In_ BOOLEAN DisonnectComms)
DWORD KmtFltSendWStringToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ PCWSTR String)
DWORD KmtFltConnectComms(_Out_ HANDLE *hPort)
DWORD KmtFltDeleteService(_In_opt_z_ PCWSTR ServiceName, _Inout_ SC_HANDLE *ServiceHandle)
#define InterlockedCompareExchange
union _KMT_RESPONSE KMT_RESPONSE
PKMT_RESPONSE KmtUserModeCallback(KMT_CALLBACK_INFORMATION_CLASS Operation, PVOID Parameters)
#define ok_eq_hex(value, expected)
BOOLEAN BOOLEAN VOID VOID KmtTrace(PCSTR FileAndLine, PCSTR Format,...) KMT_FORMAT(ms_printf
const KMT_TEST * PCKMT_TEST
VOID KmtFreeGuarded(PVOID Pointer)
BOOLEAN BOOLEAN VOID VOID BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf
union _KMT_RESPONSE * PKMT_RESPONSE
BOOLEAN BOOLEAN KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format,...) KMT_FORMAT(ms_printf
BOOLEAN BOOLEAN VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf
BOOLEAN KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf
struct KMT_RESULTBUFFER * PKMT_RESULTBUFFER
struct _KMT_CALLBACK_REQUEST_PACKET * PKMT_CALLBACK_REQUEST_PACKET
struct _KMT_CALLBACK_REQUEST_PACKET KMT_CALLBACK_REQUEST_PACKET
BOOLEAN BOOLEAN VOID VOID BOOLEAN BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format,...) KMT_FORMAT(ms_printf
_KMT_CALLBACK_INFORMATION_CLASS
VOID KmtFreeCallbackResponse(PKMT_RESPONSE Response)
enum _KMT_CALLBACK_INFORMATION_CLASS KMT_CALLBACK_INFORMATION_CLASS
KMT_TESTFUNC * PKMT_TESTFUNC
PKMT_RESULTBUFFER ResultBuffer
#define KMT_FORMAT(type, fmt, first)
enum _KMT_CALLBACK_INFORMATION_CLASS * PKMT_CALLBACK_INFORMATION_CLASS
BOOLEAN BOOLEAN VOID VOID BOOLEAN BOOLEAN PVOID KmtAllocateGuarded(SIZE_T SizeRequested)
struct KMT_TEST * PKMT_TEST
PKTHREAD KmtStartThread(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext OPTIONAL)
PCSTR KmtMajorFunctionNames[]
PVOID KmtGetSystemRoutineAddress(IN PCWSTR RoutineName)
USHORT KmtGetPoolType(PVOID Memory)
ULONG KmtGetPoolTag(PVOID Memory)
VOID KmtSetIrql(IN KIRQL NewIrql)
BOOLEAN KmtAreInterruptsEnabled(VOID)
BOOLEAN KmtIsCheckedBuild
BOOLEAN KmtIsMultiProcessorBuild
VOID KmtFinishThread(IN PKTHREAD Thread OPTIONAL, IN PKEVENT Event OPTIONAL)
NTSTATUS KmtFilterRegisterCallbacks(_In_ CONST FLT_OPERATION_REGISTRATION *OperationRegistration)
NTSTATUS KmtFilterRegisterComms(_In_ PFLT_CONNECT_NOTIFY ConnectNotifyCallback, _In_ PFLT_DISCONNECT_NOTIFY DisconnectNotifyCallback, _In_opt_ PFLT_MESSAGE_NOTIFY MessageNotifyCallback, _In_ LONG MaxClientConnections)
NTSTATUS KmtRegisterMessageHandler(IN ULONG ControlCode OPTIONAL, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_MESSAGE_HANDLER MessageHandler)
NTSTATUS KmtUnregisterMessageHandler(IN ULONG ControlCode OPTIONAL, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_MESSAGE_HANDLER MessageHandler)
NTSTATUS KmtUnregisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler)
NTSTATUS KmtRegisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler)
#define memcpy(s1, s2, n)
static void TestEntry(const ENTRY *pEntry)
#define PAGE_ROUND_DOWN(x)
#define _In_reads_bytes_(s)
#define _Out_writes_bytes_to_opt_(s, c)
IN ULONG IN UCHAR Condition
_In_ ULONG _In_ ULONG _In_ ULONG Length
_CRT_RESTORE_GCC_WARNINGS _CRT_DISABLE_GCC_WARNINGS _Check_return_ _CRTIMP _CONST_RETURN char *__cdecl strrchr(_In_z_ const char *_Str, _In_ int _Ch)
_Requires_lock_held_ SpinLock _Releases_lock_ SpinLock _In_ _IRQL_restores_ KIRQL NewIrql
PULONG MinorVersion OPTIONAL
volatile LONG LogBufferLength
KMT_TESTFUNC * TestFunction
KMT_CALLBACK_INFORMATION_CLASS OperationClass
DWORD KmtRunKernelTest(IN PCSTR TestName)
DWORD KmtLoadAndOpenDriver(IN PCWSTR ServiceName, IN BOOLEAN RestartIfRunning)
DWORD KmtOpenDriver(VOID)
VOID KmtCloseDriver(VOID)
DWORD KmtSendBufferToDriver(IN DWORD ControlCode, IN OUT PVOID Buffer OPTIONAL, IN DWORD InLength, IN OUT PDWORD OutLength)
DWORD KmtSendUlongToDriver(IN DWORD ControlCode, IN DWORD Value)
DWORD KmtSendStringToDriver(IN DWORD ControlCode, IN PCSTR String)
DWORD KmtSendToDriver(IN DWORD ControlCode)
DWORD KmtLoadDriver(IN PCWSTR ServiceName, IN BOOLEAN RestartIfRunning)
VOID KmtUnloadDriverKeepService(VOID)
DWORD KmtSendWStringToDriver(IN DWORD ControlCode, IN PCWSTR String)
VOID KmtUnloadDriver(VOID)
EH_STD::__list__< TestClass, eh_allocator(TestClass) > TestList
MEMORY_BASIC_INFORMATION MemInfo
_In_ PDEVICE_OBJECT DeviceObject
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG BufferLength
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
_Must_inspect_result_ _In_ PWDFDEVICE_INIT _In_opt_ PCUNICODE_STRING DeviceName
_In_ WDFDEVICE _In_ PVOID _In_opt_ PMDL Mdl
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
_Must_inspect_result_ _In_ WDFQUEUE _In_opt_ WDFREQUEST _In_opt_ WDFFILEOBJECT _Inout_opt_ PWDF_REQUEST_PARAMETERS Parameters
_Must_inspect_result_ _In_opt_ PWDF_OBJECT_ATTRIBUTES _In_ _Strict_type_match_ POOL_TYPE _In_opt_ ULONG _In_ _Out_ WDFMEMORY * Memory
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
_In_ DWORD _In_ DWORD _In_ DWORD _Out_ LPDWORD lpBytesReturned
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
_Must_inspect_result_ _In_ ULONG Flags
KSTART_ROUTINE * PKSTART_ROUTINE
_In_ ULONG _In_opt_ POBJECT_ATTRIBUTES _In_opt_ HANDLE _Out_opt_ PCLIENT_ID _In_ PKSTART_ROUTINE StartRoutine
#define ZwCurrentProcess()