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 93 typedef KMT_IRP_HANDLER *PKMT_IRP_HANDLER;
98 typedef NTSTATUS (KMT_MESSAGE_HANDLER)(
104 typedef 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 199 #error either KMT_KERNEL_MODE or KMT_USER_MODE must be defined 206 #define KMT_FORMAT(type, fmt, first) 207 #elif !defined __GNUC__ 208 #define KMT_FORMAT(type, fmt, first) 211 #define START_TEST(name) VOID Test_##name(VOID) 213 #ifndef KMT_STRINGIZE 214 #define KMT_STRINGIZE(x) #x 216 #define ok(test, ...) ok_(test, __FILE__, __LINE__, __VA_ARGS__) 217 #define trace(...) trace_( __FILE__, __LINE__, __VA_ARGS__) 218 #define skip(test, ...) skip_(test, __FILE__, __LINE__, __VA_ARGS__) 220 #define ok_(test, file, line, ...) KmtOk(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__) 221 #define trace_(file, line, ...) KmtTrace( file ":" KMT_STRINGIZE(line), __VA_ARGS__) 222 #define skip_(test, file, line, ...) KmtSkip(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__) 233 #ifdef KMT_KERNEL_MODE 234 #define ok_irql(irql) ok(KeGetCurrentIrql() == irql, "IRQL is %d, expected %d\n", KeGetCurrentIrql(), irql) 236 #define ok_eq_print(value, expected, spec) ok((value) == (expected), #value " = " spec ", expected " spec "\n", value, expected) 237 #define ok_eq_pointer(value, expected) ok_eq_print(value, expected, "%p") 238 #define ok_eq_int(value, expected) ok_eq_print(value, expected, "%d") 239 #define ok_eq_uint(value, expected) ok_eq_print(value, expected, "%u") 240 #define ok_eq_long(value, expected) ok_eq_print(value, expected, "%ld") 241 #define ok_eq_ulong(value, expected) ok_eq_print(value, expected, "%lu") 242 #define ok_eq_longlong(value, expected) ok_eq_print(value, expected, "%I64d") 243 #define ok_eq_ulonglong(value, expected) ok_eq_print(value, expected, "%I64u") 244 #define ok_eq_char(value, expected) ok_eq_print(value, expected, "%c") 245 #define ok_eq_wchar(value, expected) ok_eq_print(value, expected, "%C") 247 #define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%lu") 248 #define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%ld") 249 #define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%lu") 251 #define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%I64u") 252 #define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%I64d") 253 #define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%I64u") 255 #define ok_eq_hex(value, expected) ok_eq_print(value, expected, "0x%08lx") 256 #define ok_bool_true(value, desc) ok((value) == TRUE, desc " FALSE, expected TRUE\n") 257 #define ok_bool_false(value, desc) ok((value) == FALSE, desc " TRUE, expected FALSE\n") 258 #define ok_eq_bool(value, expected) ok((value) == (expected), #value " = %s, expected %s\n", \ 259 (value) ? "TRUE" : "FALSE", \ 260 (expected) ? "TRUE" : "FALSE") 261 #define ok_eq_str(value, expected) ok(!strcmp(value, expected), #value " = \"%s\", expected \"%s\"\n", value, expected) 262 #define ok_eq_wstr(value, expected) ok(!wcscmp(value, expected), #value " = \"%ls\", expected \"%ls\"\n", value, expected) 263 #define ok_eq_tag(value, expected) ok_eq_print(value, expected, "0x%08lx") 265 #define KMT_MAKE_CODE(ControlCode) CTL_CODE(FILE_DEVICE_UNKNOWN, \ 266 0xC00 + (ControlCode), \ 270 #define MICROSECOND 10 271 #define MILLISECOND (1000 * MICROSECOND) 272 #define SECOND (1000 * MILLISECOND) 275 #define KmtInvalidPointer ((PVOID)0x5555555555555555ULL) 277 #define KmtStartSeh() \ 279 NTSTATUS ExceptionStatus = STATUS_SUCCESS; \ 283 #define KmtEndSeh(ExpectedStatus) \ 285 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) \ 287 ExceptionStatus = _SEH2_GetExceptionCode(); \ 290 ok_eq_hex(ExceptionStatus, (ExpectedStatus)); \ 293 #define KmtGetSystemOrEmbeddedRoutineAddress(RoutineName) \ 294 p##RoutineName = KmtGetSystemRoutineAddress(L ## #RoutineName); \ 295 if (!p##RoutineName) \ 297 p##RoutineName = RoutineName; \ 298 trace("Using embedded routine for " #RoutineName "\n"); \ 301 trace("Using system routine for " #RoutineName "\n"); 303 #if defined KMT_DEFINE_TEST_FUNCTIONS 305 #if defined KMT_KERNEL_MODE 307 #elif defined KMT_USER_MODE 323 OldLength =
Buffer->LogBufferLength;
341 Slash =
strrchr(FileAndLine,
'\\');
343 FileAndLine = Slash + 1;
344 Slash =
strrchr(FileAndLine,
'/');
346 FileAndLine = Slash + 1;
352 BufferMaxLength -=
Length;
360 BufferMaxLength -=
Length;
384 CHAR MessageBuffer[512];
390 MessageLength = KmtXSNPrintF(MessageBuffer,
sizeof MessageBuffer,
NULL,
NULL,
391 "%s: %ld tests executed (0 marked as todo, %ld failures), %ld skipped.\n",
396 KmtAddToLogBuffer(
ResultBuffer, MessageBuffer, MessageLength);
401 CHAR MessageBuffer[512];
413 MessageLength = KmtXSNPrintF(MessageBuffer,
sizeof MessageBuffer, FileAndLine,
": Test succeeded\n",
NULL);
414 KmtAddToLogBuffer(
ResultBuffer, MessageBuffer, MessageLength);
420 MessageLength = KmtXVSNPrintF(MessageBuffer,
sizeof MessageBuffer, FileAndLine,
": Test failed: ",
Format, Arguments);
421 KmtAddToLogBuffer(
ResultBuffer, MessageBuffer, MessageLength);
439 CHAR MessageBuffer[512];
442 MessageLength = KmtXVSNPrintF(MessageBuffer,
sizeof MessageBuffer, FileAndLine,
": ",
Format, Arguments);
443 KmtAddToLogBuffer(
ResultBuffer, MessageBuffer, MessageLength);
456 CHAR MessageBuffer[512];
465 MessageLength = KmtXVSNPrintF(MessageBuffer,
sizeof MessageBuffer, FileAndLine,
": Tests skipped: ",
Format, Arguments);
466 KmtAddToLogBuffer(
ResultBuffer, MessageBuffer, MessageLength);
504 StartOfBuffer = VirtualMemory;
505 StartOfBuffer +=
Size - SizeRequested;
507 return StartOfBuffer;
BOOLEAN BOOLEAN VOID VOID BOOLEAN BOOLEAN PVOID KmtAllocateGuarded(SIZE_T SizeRequested)
struct KMT_RESULTBUFFER * PKMT_RESULTBUFFER
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG BufferLength
ULONG FLT_INSTANCE_QUERY_TEARDOWN_FLAGS
PCSTR KmtMajorFunctionNames[]
PKTHREAD KmtStartThread(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext OPTIONAL)
_In_ ULONG _In_ ULONG _In_ ULONG Length
DWORD KmtFltRunKernelTest(_In_ HANDLE hPort, _In_z_ PCSTR TestName)
_Must_inspect_result_ _In_ WDFQUEUE _In_opt_ WDFREQUEST _In_opt_ WDFFILEOBJECT _Inout_opt_ PWDF_REQUEST_PARAMETERS Parameters
#define _In_reads_bytes_(s)
ACPI_SIZE strlen(const char *String)
_In_ ULONG _In_opt_ POBJECT_ATTRIBUTES _In_opt_ HANDLE _Out_opt_ PCLIENT_ID _In_ PKSTART_ROUTINE StartRoutine
#define ZwCurrentProcess()
NTSTATUS KmtUnregisterMessageHandler(IN ULONG ControlCode OPTIONAL, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_MESSAGE_HANDLER MessageHandler)
KMT_CALLBACK_INFORMATION_CLASS OperationClass
DWORD KmtFltUnloadDriver(_In_ HANDLE *hPort, _In_ BOOLEAN DisonnectComms)
#define ok_eq_hex(value, expected)
NTSTATUS KmtFilterRegisterCallbacks(_In_ CONST FLT_OPERATION_REGISTRATION *OperationRegistration)
_Must_inspect_result_ _In_ PDRIVER_OBJECT _In_ PCUNICODE_STRING RegistryPath
#define PAGE_ROUND_DOWN(x)
VOID KmtFreeCallbackResponse(PKMT_RESPONSE Response)
#define InterlockedCompareExchange
IN PVOID IN PVOID IN USHORT IN USHORT Size
_Check_return_ _CRTIMP _CONST_RETURN char *__cdecl strrchr(_In_z_ const char *_Str, _In_ int _Ch)
BOOLEAN BOOLEAN VOID VOID BOOLEAN BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format,...) KMT_FORMAT(ms_printf
DWORD KmtSendStringToDriver(IN DWORD ControlCode, IN PCSTR String)
BOOLEAN KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf
DWORD KmtFltCreateService(_In_z_ PCWSTR ServiceName, _In_z_ PCWSTR DisplayName, _Out_ SC_HANDLE *ServiceHandle)
NTSTATUS KmtUnregisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler)
static USHORT USHORT * NewLength
static void TestEntry(const ENTRY *pEntry)
BOOLEAN KmtIsCheckedBuild
_In_ PDEVICE_OBJECT DeviceObject
BOOLEAN BOOLEAN KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format,...) KMT_FORMAT(ms_printf
_KMT_CALLBACK_INFORMATION_CLASS
_Must_inspect_result_ _In_ PWDFDEVICE_INIT _In_opt_ PCUNICODE_STRING DeviceName
#define _Out_writes_bytes_to_opt_(s, c)
_Must_inspect_result_ _In_ WDFDEVICE _In_ WDFSTRING String
MEMORY_BASIC_INFORMATION MemInfo
KSTART_ROUTINE * PKSTART_ROUTINE
DWORD KmtFltDeleteService(_In_opt_z_ PCWSTR ServiceName, _Inout_ SC_HANDLE *ServiceHandle)
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)
VOID KmtSetIrql(IN KIRQL NewIrql)
BOOLEAN KmtIsMultiProcessorBuild
DWORD KmtSendBufferToDriver(IN DWORD ControlCode, IN OUT PVOID Buffer OPTIONAL, IN DWORD InLength, IN OUT PDWORD OutLength)
_IRQL_requires_same_ typedef _In_ ULONG ControlCode
BOOLEAN BOOLEAN VOID VOID BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf
IN ULONG IN UCHAR Condition
struct _KMT_CALLBACK_REQUEST_PACKET * PKMT_CALLBACK_REQUEST_PACKET
EH_STD::__list__< TestClass, eh_allocator(TestClass) > TestList
NTSTATUS KmtRegisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler)
_Must_inspect_result_ _In_ PDRIVER_OBJECT DriverObject
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
_Must_inspect_result_ _In_opt_ PWDF_OBJECT_ATTRIBUTES _In_ _Strict_type_match_ POOL_TYPE _In_opt_ ULONG _In_ _Out_ WDFMEMORY * Memory
_Must_inspect_result_ _Inout_ PFLT_VOLUME _In_ PCUNICODE_STRING Altitude
DWORD KmtFltSendUlongToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ DWORD Value)
enum _KMT_CALLBACK_INFORMATION_CLASS * PKMT_CALLBACK_INFORMATION_CLASS
VOID KmtCloseDriver(VOID)
DWORD KmtFltAddAltitude(_In_z_ LPWSTR Altitude)
_Must_inspect_result_ _In_ ULONG Flags
DWORD KmtRunKernelTest(IN PCSTR TestName)
NTSTATUS KmtRegisterMessageHandler(IN ULONG ControlCode OPTIONAL, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_MESSAGE_HANDLER MessageHandler)
#define NT_SUCCESS(StatCode)
_Must_inspect_result_ _Outptr_ PFLT_PORT _In_ POBJECT_ATTRIBUTES _In_opt_ PVOID _In_ PFLT_CONNECT_NOTIFY _In_ PFLT_DISCONNECT_NOTIFY DisconnectNotifyCallback
NTSTATUS KmtFilterRegisterComms(_In_ PFLT_CONNECT_NOTIFY ConnectNotifyCallback, _In_ PFLT_DISCONNECT_NOTIFY DisconnectNotifyCallback, _In_opt_ PFLT_MESSAGE_NOTIFY MessageNotifyCallback, _In_ LONG MaxClientConnections)
#define KMT_FORMAT(type, fmt, first)
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)
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
DWORD KmtFltDisconnectComms(_In_ HANDLE hPort)
PVOID KmtGetSystemRoutineAddress(IN PCWSTR RoutineName)
VOID TestFilterUnload(IN ULONG Flags)
union _KMT_RESPONSE * PKMT_RESPONSE
DWORD KmtFltSendWStringToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ PCWSTR String)
KMT_TESTFUNC * TestFunction
USHORT KmtGetPoolType(PVOID Memory)
DWORD KmtSendWStringToDriver(IN DWORD ControlCode, IN PCWSTR String)
VOID TestQueryTeardown(_In_ PCFLT_RELATED_OBJECTS FltObjects, _In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags)
#define memcpy(s1, s2, n)
BOOLEAN BOOLEAN VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf
DWORD KmtFltSendToDriver(_In_ HANDLE hPort, _In_ DWORD Message)
_Must_inspect_result_ _Outptr_ PFLT_PORT _In_ POBJECT_ATTRIBUTES _In_opt_ PVOID _In_ PFLT_CONNECT_NOTIFY ConnectNotifyCallback
VOID KmtFinishThread(IN PKTHREAD Thread OPTIONAL, IN PKEVENT Event OPTIONAL)
PKMT_RESPONSE KmtUserModeCallback(KMT_CALLBACK_INFORMATION_CLASS Operation, PVOID Parameters)
BOOLEAN KmtAreInterruptsEnabled(VOID)
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)
DWORD KmtSendToDriver(IN DWORD ControlCode)
enum _KMT_CALLBACK_INFORMATION_CLASS KMT_CALLBACK_INFORMATION_CLASS
VOID KmtUnloadDriver(VOID)
#define InterlockedIncrement
VOID TestUnload(IN PDRIVER_OBJECT DriverObject)
ULONG FLT_INSTANCE_SETUP_FLAGS
ULONG KmtGetPoolTag(PVOID Memory)
DWORD KmtFltLoadDriver(_In_ BOOLEAN EnableDriverLoadPrivlege, _In_ BOOLEAN RestartIfRunning, _In_ BOOLEAN ConnectComms, _Out_ HANDLE *hPort)
DWORD KmtFltConnectComms(_Out_ HANDLE *hPort)
volatile LONG LogBufferLength
_In_ DWORD _In_ DWORD _In_ DWORD _Out_ LPDWORD lpBytesReturned
VOID(FLTAPI * PFLT_DISCONNECT_NOTIFY)(_In_opt_ PVOID ConnectionCookie)
_In_ WDFDEVICE _In_ PVOID _In_opt_ PMDL Mdl
struct KMT_TEST * PKMT_TEST
union _KMT_RESPONSE KMT_RESPONSE
const KMT_TEST * PCKMT_TEST
_Must_inspect_result_ _Inout_opt_ PUNICODE_STRING VolumeName
VOID KmtFreeGuarded(PVOID Pointer)
_In_ FLT_SET_CONTEXT_OPERATION Operation
struct _KMT_CALLBACK_REQUEST_PACKET KMT_CALLBACK_REQUEST_PACKET
KMT_TESTFUNC * PKMT_TESTFUNC
VOID KmtLoadDriver(IN PCWSTR ServiceName, IN BOOLEAN RestartIfRunning)
BOOLEAN BOOLEAN VOID VOID KmtTrace(PCSTR FileAndLine, PCSTR Format,...) KMT_FORMAT(ms_printf
PKMT_RESULTBUFFER ResultBuffer
_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
DWORD KmtSendUlongToDriver(IN DWORD ControlCode, IN DWORD Value)
DWORD KmtFltSendStringToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ PCSTR String)
_In_ WDFMEMORY _Out_opt_ size_t * BufferSize
_Requires_lock_held_ SpinLock _Releases_lock_ SpinLock _In_ _IRQL_restores_ KIRQL NewIrql
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)
PULONG MinorVersion OPTIONAL