2011-06-10 05:34:00 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS kernel-mode tests
|
|
|
|
* LICENSE: GPLv2+ - See COPYING in the top level directory
|
2011-06-19 09:23:03 +00:00
|
|
|
* PURPOSE: Kernel-Mode Test Suite test framework declarations
|
2013-10-12 16:05:54 +00:00
|
|
|
* PROGRAMMER: Thomas Faber <thomas.faber@reactos.org>
|
2011-06-10 05:34:00 +00:00
|
|
|
*/
|
|
|
|
|
2011-06-19 09:23:03 +00:00
|
|
|
/* Inspired by Wine C unit tests, Copyright (C) 2002 Alexandre Julliard
|
|
|
|
* Inspired by ReactOS kernel-mode regression tests,
|
|
|
|
* Copyright (C) Aleksey Bragin, Filip Navara
|
|
|
|
*/
|
|
|
|
|
2011-06-10 05:34:00 +00:00
|
|
|
#ifndef _KMTEST_TEST_H_
|
|
|
|
#define _KMTEST_TEST_H_
|
|
|
|
|
2011-08-01 21:53:52 +00:00
|
|
|
#include <kmt_platform.h>
|
|
|
|
|
2011-06-18 11:03:28 +00:00
|
|
|
typedef VOID KMT_TESTFUNC(VOID);
|
2011-07-03 18:53:26 +00:00
|
|
|
typedef KMT_TESTFUNC *PKMT_TESTFUNC;
|
2011-06-10 05:34:00 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
const char *TestName;
|
|
|
|
KMT_TESTFUNC *TestFunction;
|
|
|
|
} KMT_TEST, *PKMT_TEST;
|
|
|
|
|
|
|
|
typedef const KMT_TEST CKMT_TEST, *PCKMT_TEST;
|
|
|
|
|
|
|
|
extern const KMT_TEST TestList[];
|
|
|
|
|
2011-06-29 08:51:00 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2011-06-13 17:50:07 +00:00
|
|
|
volatile LONG Successes;
|
|
|
|
volatile LONG Failures;
|
2011-06-29 08:51:00 +00:00
|
|
|
volatile LONG Skipped;
|
2011-06-13 17:50:07 +00:00
|
|
|
volatile LONG LogBufferLength;
|
|
|
|
LONG LogBufferMaxLength;
|
|
|
|
CHAR LogBuffer[ANYSIZE_ARRAY];
|
|
|
|
} KMT_RESULTBUFFER, *PKMT_RESULTBUFFER;
|
|
|
|
|
2013-04-27 18:18:17 +00:00
|
|
|
#ifndef KMT_STANDALONE_DRIVER
|
|
|
|
|
|
|
|
/* usermode call-back mechanism */
|
|
|
|
|
|
|
|
/* list of supported operations */
|
|
|
|
typedef enum _KMT_CALLBACK_INFORMATION_CLASS
|
|
|
|
{
|
|
|
|
QueryVirtualMemory
|
|
|
|
} KMT_CALLBACK_INFORMATION_CLASS, *PKMT_CALLBACK_INFORMATION_CLASS;
|
|
|
|
|
|
|
|
/* TODO: "response" is a little generic */
|
|
|
|
typedef union _KMT_RESPONSE
|
|
|
|
{
|
|
|
|
MEMORY_BASIC_INFORMATION MemInfo;
|
|
|
|
} KMT_RESPONSE, *PKMT_RESPONSE;
|
|
|
|
|
|
|
|
/* this struct is sent from driver to usermode */
|
|
|
|
typedef struct _KMT_CALLBACK_REQUEST_PACKET
|
|
|
|
{
|
|
|
|
ULONG RequestId;
|
|
|
|
KMT_CALLBACK_INFORMATION_CLASS OperationClass;
|
|
|
|
PVOID Parameters;
|
|
|
|
} KMT_CALLBACK_REQUEST_PACKET, *PKMT_CALLBACK_REQUEST_PACKET;
|
|
|
|
|
|
|
|
PKMT_RESPONSE KmtUserModeCallback(KMT_CALLBACK_INFORMATION_CLASS Operation, PVOID Parameters);
|
|
|
|
VOID KmtFreeCallbackResponse(PKMT_RESPONSE Response);
|
|
|
|
|
|
|
|
//macro to simplify using the mechanism
|
|
|
|
#define Test_NtQueryVirtualMemory(BaseAddress, Size, AllocationType, ProtectionType) \
|
|
|
|
do { \
|
|
|
|
PKMT_RESPONSE NtQueryTest = KmtUserModeCallback(QueryVirtualMemory, BaseAddress); \
|
|
|
|
if (NtQueryTest != NULL) \
|
|
|
|
{ \
|
|
|
|
ok_eq_hex(NtQueryTest->MemInfo.Protect, ProtectionType); \
|
|
|
|
ok_eq_hex(NtQueryTest->MemInfo.State, AllocationType); \
|
|
|
|
ok_eq_size(NtQueryTest->MemInfo.RegionSize, Size); \
|
|
|
|
KmtFreeCallbackResponse(NtQueryTest); \
|
|
|
|
} \
|
|
|
|
} while (0) \
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2011-07-06 22:23:03 +00:00
|
|
|
#ifdef KMT_STANDALONE_DRIVER
|
|
|
|
#define KMT_KERNEL_MODE
|
|
|
|
|
|
|
|
typedef NTSTATUS (KMT_IRP_HANDLER)(
|
|
|
|
IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
IN PIRP Irp,
|
|
|
|
IN PIO_STACK_LOCATION IoStackLocation);
|
|
|
|
typedef KMT_IRP_HANDLER *PKMT_IRP_HANDLER;
|
|
|
|
|
|
|
|
NTSTATUS KmtRegisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler);
|
|
|
|
NTSTATUS KmtUnregisterIrpHandler(IN UCHAR MajorFunction, IN PDEVICE_OBJECT DeviceObject OPTIONAL, IN PKMT_IRP_HANDLER IrpHandler);
|
|
|
|
|
|
|
|
typedef NTSTATUS (KMT_MESSAGE_HANDLER)(
|
|
|
|
IN PDEVICE_OBJECT DeviceObject,
|
|
|
|
IN ULONG ControlCode,
|
|
|
|
IN PVOID Buffer OPTIONAL,
|
|
|
|
IN SIZE_T InLength,
|
|
|
|
IN OUT PSIZE_T OutLength);
|
|
|
|
typedef KMT_MESSAGE_HANDLER *PKMT_MESSAGE_HANDLER;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
TESTENTRY_NO_CREATE_DEVICE = 1,
|
|
|
|
TESTENTRY_NO_REGISTER_DISPATCH = 2,
|
|
|
|
TESTENTRY_NO_REGISTER_UNLOAD = 4,
|
2013-04-21 18:53:51 +00:00
|
|
|
TESTENTRY_NO_EXCLUSIVE_DEVICE = 8,
|
2011-07-06 22:23:03 +00:00
|
|
|
} KMT_TESTENTRY_FLAGS;
|
|
|
|
|
2011-08-11 11:37:50 +00:00
|
|
|
NTSTATUS TestEntry(IN PDRIVER_OBJECT DriverObject, IN PCUNICODE_STRING RegistryPath, OUT PCWSTR *DeviceName, IN OUT INT *Flags);
|
2011-07-06 22:23:03 +00:00
|
|
|
VOID TestUnload(IN PDRIVER_OBJECT DriverObject);
|
|
|
|
#endif /* defined KMT_STANDALONE_DRIVER */
|
|
|
|
|
2011-07-04 19:47:49 +00:00
|
|
|
#ifdef KMT_KERNEL_MODE
|
|
|
|
/* Device Extension layout */
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
PKMT_RESULTBUFFER ResultBuffer;
|
|
|
|
PMDL Mdl;
|
|
|
|
} KMT_DEVICE_EXTENSION, *PKMT_DEVICE_EXTENSION;
|
2011-07-22 11:51:18 +00:00
|
|
|
|
|
|
|
extern BOOLEAN KmtIsCheckedBuild;
|
|
|
|
extern BOOLEAN KmtIsMultiProcessorBuild;
|
2011-08-18 07:08:59 +00:00
|
|
|
extern PCSTR KmtMajorFunctionNames[];
|
2011-11-12 00:21:57 +00:00
|
|
|
extern PDRIVER_OBJECT KmtDriverObject;
|
2011-07-22 11:51:18 +00:00
|
|
|
|
|
|
|
VOID KmtSetIrql(IN KIRQL NewIrql);
|
2011-07-26 07:38:43 +00:00
|
|
|
BOOLEAN KmtAreInterruptsEnabled(VOID);
|
2012-06-13 21:56:54 +00:00
|
|
|
ULONG KmtGetPoolTag(PVOID Memory);
|
2013-09-16 19:02:28 +00:00
|
|
|
USHORT KmtGetPoolType(PVOID Memory);
|
2014-11-04 20:55:16 +00:00
|
|
|
PKTHREAD KmtStartThread(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext OPTIONAL);
|
|
|
|
VOID KmtFinishThread(IN PKTHREAD Thread OPTIONAL, IN PKEVENT Event OPTIONAL);
|
2011-07-04 19:47:49 +00:00
|
|
|
#elif defined KMT_USER_MODE
|
2011-07-06 18:55:21 +00:00
|
|
|
DWORD KmtRunKernelTest(IN PCSTR TestName);
|
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
VOID KmtLoadDriver(IN PCWSTR ServiceName, IN BOOLEAN RestartIfRunning);
|
|
|
|
VOID KmtUnloadDriver(VOID);
|
|
|
|
VOID KmtOpenDriver(VOID);
|
|
|
|
VOID KmtCloseDriver(VOID);
|
|
|
|
|
|
|
|
DWORD KmtSendToDriver(IN DWORD ControlCode);
|
|
|
|
DWORD KmtSendStringToDriver(IN DWORD ControlCode, IN PCSTR String);
|
2013-04-21 18:53:51 +00:00
|
|
|
DWORD KmtSendWStringToDriver(IN DWORD ControlCode, IN PCWSTR String);
|
2011-07-06 22:23:03 +00:00
|
|
|
DWORD KmtSendBufferToDriver(IN DWORD ControlCode, IN OUT PVOID Buffer OPTIONAL, IN DWORD InLength, IN OUT PDWORD OutLength);
|
2011-08-18 07:08:59 +00:00
|
|
|
#else /* if !defined KMT_KERNEL_MODE && !defined KMT_USER_MODE */
|
|
|
|
#error either KMT_KERNEL_MODE or KMT_USER_MODE must be defined
|
|
|
|
#endif /* !defined KMT_KERNEL_MODE && !defined KMT_USER_MODE */
|
2011-07-03 18:53:26 +00:00
|
|
|
|
2011-06-13 17:50:07 +00:00
|
|
|
extern PKMT_RESULTBUFFER ResultBuffer;
|
|
|
|
|
2011-06-29 08:51:00 +00:00
|
|
|
#ifdef __GNUC__
|
2011-08-19 19:03:46 +00:00
|
|
|
/* TODO: GCC doesn't understand %wZ :( */
|
|
|
|
#define KMT_FORMAT(type, fmt, first) /*__attribute__((__format__(type, fmt, first)))*/
|
2011-06-29 08:51:00 +00:00
|
|
|
#elif !defined __GNUC__
|
|
|
|
#define KMT_FORMAT(type, fmt, first)
|
|
|
|
#endif /* !defined __GNUC__ */
|
|
|
|
|
2011-06-19 18:27:14 +00:00
|
|
|
#define START_TEST(name) VOID Test_##name(VOID)
|
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
#ifndef KMT_STRINGIZE
|
2011-06-19 09:23:03 +00:00
|
|
|
#define KMT_STRINGIZE(x) #x
|
2011-07-03 18:53:26 +00:00
|
|
|
#endif /* !defined KMT_STRINGIZE */
|
|
|
|
#define ok(test, ...) ok_(test, __FILE__, __LINE__, __VA_ARGS__)
|
|
|
|
#define trace(...) trace_( __FILE__, __LINE__, __VA_ARGS__)
|
2011-06-29 08:51:00 +00:00
|
|
|
#define skip(test, ...) skip_(test, __FILE__, __LINE__, __VA_ARGS__)
|
2011-06-19 09:23:03 +00:00
|
|
|
|
2011-07-03 18:53:26 +00:00
|
|
|
#define ok_(test, file, line, ...) KmtOk(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
|
|
|
|
#define trace_(file, line, ...) KmtTrace( file ":" KMT_STRINGIZE(line), __VA_ARGS__)
|
2011-06-29 08:51:00 +00:00
|
|
|
#define skip_(test, file, line, ...) KmtSkip(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
|
2011-06-19 09:23:03 +00:00
|
|
|
|
2012-06-13 21:56:54 +00:00
|
|
|
BOOLEAN KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0);
|
|
|
|
BOOLEAN KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 3, 4);
|
2011-06-29 08:51:00 +00:00
|
|
|
VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 2, 0);
|
|
|
|
VOID KmtTrace(PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 2, 3);
|
|
|
|
BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0);
|
|
|
|
BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 3, 4);
|
2011-09-11 11:22:00 +00:00
|
|
|
PVOID KmtAllocateGuarded(SIZE_T SizeRequested);
|
|
|
|
VOID KmtFreeGuarded(PVOID Pointer);
|
2011-06-19 09:23:03 +00:00
|
|
|
|
2011-06-19 18:27:14 +00:00
|
|
|
#ifdef KMT_KERNEL_MODE
|
|
|
|
#define ok_irql(irql) ok(KeGetCurrentIrql() == irql, "IRQL is %d, expected %d\n", KeGetCurrentIrql(), irql)
|
|
|
|
#endif /* defined KMT_KERNEL_MODE */
|
2011-06-29 08:51:00 +00:00
|
|
|
#define ok_eq_print(value, expected, spec) ok((value) == (expected), #value " = " spec ", expected " spec "\n", value, expected)
|
2011-06-19 18:27:14 +00:00
|
|
|
#define ok_eq_pointer(value, expected) ok_eq_print(value, expected, "%p")
|
|
|
|
#define ok_eq_int(value, expected) ok_eq_print(value, expected, "%d")
|
|
|
|
#define ok_eq_uint(value, expected) ok_eq_print(value, expected, "%u")
|
|
|
|
#define ok_eq_long(value, expected) ok_eq_print(value, expected, "%ld")
|
|
|
|
#define ok_eq_ulong(value, expected) ok_eq_print(value, expected, "%lu")
|
2011-07-11 18:03:19 +00:00
|
|
|
#define ok_eq_longlong(value, expected) ok_eq_print(value, expected, "%I64d")
|
|
|
|
#define ok_eq_ulonglong(value, expected) ok_eq_print(value, expected, "%I64u")
|
2014-04-12 18:37:44 +00:00
|
|
|
#define ok_eq_char(value, expected) ok_eq_print(value, expected, "%c")
|
|
|
|
#define ok_eq_wchar(value, expected) ok_eq_print(value, expected, "%C")
|
2011-08-10 22:26:14 +00:00
|
|
|
#ifndef _WIN64
|
|
|
|
#define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%lu")
|
|
|
|
#define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%ld")
|
|
|
|
#define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%lu")
|
|
|
|
#elif defined _WIN64
|
|
|
|
#define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%I64u")
|
|
|
|
#define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%I64d")
|
|
|
|
#define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%I64u")
|
|
|
|
#endif /* defined _WIN64 */
|
2011-06-19 18:27:14 +00:00
|
|
|
#define ok_eq_hex(value, expected) ok_eq_print(value, expected, "0x%08lx")
|
2011-06-29 08:51:00 +00:00
|
|
|
#define ok_bool_true(value, desc) ok((value) == TRUE, desc " FALSE, expected TRUE\n")
|
|
|
|
#define ok_bool_false(value, desc) ok((value) == FALSE, desc " TRUE, expected FALSE\n")
|
2011-07-03 18:53:26 +00:00
|
|
|
#define ok_eq_bool(value, expected) ok((value) == (expected), #value " = %s, expected %s\n", \
|
|
|
|
(value) ? "TRUE" : "FALSE", \
|
|
|
|
(expected) ? "TRUE" : "FALSE")
|
2011-06-19 18:27:14 +00:00
|
|
|
#define ok_eq_str(value, expected) ok(!strcmp(value, expected), #value " = \"%s\", expected \"%s\"\n", value, expected)
|
|
|
|
#define ok_eq_wstr(value, expected) ok(!wcscmp(value, expected), #value " = \"%ls\", expected \"%ls\"\n", value, expected)
|
2012-06-13 21:56:54 +00:00
|
|
|
#define ok_eq_tag(value, expected) ok_eq_print(value, expected, "0x%08lx")
|
2011-06-19 18:27:14 +00:00
|
|
|
|
2011-07-04 19:47:49 +00:00
|
|
|
#define KMT_MAKE_CODE(ControlCode) CTL_CODE(FILE_DEVICE_UNKNOWN, \
|
2011-07-06 22:23:03 +00:00
|
|
|
0xC00 + (ControlCode), \
|
2011-07-04 19:47:49 +00:00
|
|
|
METHOD_BUFFERED, \
|
|
|
|
FILE_ANY_ACCESS)
|
|
|
|
|
2011-08-10 22:26:14 +00:00
|
|
|
#define MICROSECOND 10
|
|
|
|
#define MILLISECOND (1000 * MICROSECOND)
|
|
|
|
#define SECOND (1000 * MILLISECOND)
|
|
|
|
|
2013-09-22 19:28:21 +00:00
|
|
|
/* See apitests/include/apitest.h */
|
2012-06-14 17:48:14 +00:00
|
|
|
#define KmtInvalidPointer ((PVOID)0x5555555555555555ULL)
|
|
|
|
|
|
|
|
#define KmtStartSeh() \
|
2013-09-22 19:28:21 +00:00
|
|
|
{ \
|
|
|
|
NTSTATUS ExceptionStatus = STATUS_SUCCESS; \
|
2012-06-14 17:48:14 +00:00
|
|
|
_SEH2_TRY \
|
|
|
|
{
|
2013-09-22 19:28:21 +00:00
|
|
|
|
2012-06-14 17:48:14 +00:00
|
|
|
#define KmtEndSeh(ExpectedStatus) \
|
|
|
|
} \
|
|
|
|
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) \
|
|
|
|
{ \
|
|
|
|
ExceptionStatus = _SEH2_GetExceptionCode(); \
|
2013-09-22 19:28:21 +00:00
|
|
|
} \
|
|
|
|
_SEH2_END; \
|
|
|
|
ok_eq_hex(ExceptionStatus, (ExpectedStatus)); \
|
|
|
|
}
|
2012-06-14 17:48:14 +00:00
|
|
|
|
2011-06-13 17:50:07 +00:00
|
|
|
#if defined KMT_DEFINE_TEST_FUNCTIONS
|
|
|
|
|
2011-07-22 11:51:18 +00:00
|
|
|
#if defined KMT_KERNEL_MODE
|
2014-11-04 20:55:16 +00:00
|
|
|
#include "kmt_test_kernel.h"
|
2011-07-22 11:51:18 +00:00
|
|
|
#elif defined KMT_USER_MODE
|
2014-11-04 20:55:16 +00:00
|
|
|
#include "kmt_test_user.h"
|
2011-06-13 17:50:07 +00:00
|
|
|
#endif /* defined KMT_USER_MODE */
|
|
|
|
|
2011-07-22 11:51:18 +00:00
|
|
|
PKMT_RESULTBUFFER ResultBuffer = NULL;
|
|
|
|
|
2011-06-13 17:50:07 +00:00
|
|
|
static VOID KmtAddToLogBuffer(PKMT_RESULTBUFFER Buffer, PCSTR String, SIZE_T Length)
|
|
|
|
{
|
|
|
|
LONG OldLength;
|
|
|
|
LONG NewLength;
|
|
|
|
|
2011-07-04 19:47:49 +00:00
|
|
|
if (!Buffer)
|
|
|
|
return;
|
|
|
|
|
2011-06-13 17:50:07 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
OldLength = Buffer->LogBufferLength;
|
2012-05-01 09:13:19 +00:00
|
|
|
NewLength = OldLength + (ULONG)Length;
|
2011-06-13 17:50:07 +00:00
|
|
|
if (NewLength > Buffer->LogBufferMaxLength)
|
|
|
|
return;
|
|
|
|
} while (InterlockedCompareExchange(&Buffer->LogBufferLength, NewLength, OldLength) != OldLength);
|
|
|
|
|
2011-06-19 18:27:14 +00:00
|
|
|
memcpy(&Buffer->LogBuffer[OldLength], String, Length);
|
2011-06-13 17:50:07 +00:00
|
|
|
}
|
|
|
|
|
2011-06-29 08:51:00 +00:00
|
|
|
KMT_FORMAT(ms_printf, 5, 0)
|
|
|
|
static SIZE_T KmtXVSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR FileAndLine, PCSTR Prepend, PCSTR Format, va_list Arguments)
|
2011-06-19 09:23:03 +00:00
|
|
|
{
|
|
|
|
SIZE_T BufferLength = 0;
|
|
|
|
SIZE_T Length;
|
|
|
|
|
2011-06-29 08:51:00 +00:00
|
|
|
if (FileAndLine)
|
2011-06-19 09:23:03 +00:00
|
|
|
{
|
2011-06-29 08:51:00 +00:00
|
|
|
PCSTR Slash;
|
|
|
|
Slash = strrchr(FileAndLine, '\\');
|
|
|
|
if (Slash)
|
|
|
|
FileAndLine = Slash + 1;
|
|
|
|
Slash = strrchr(FileAndLine, '/');
|
|
|
|
if (Slash)
|
|
|
|
FileAndLine = Slash + 1;
|
|
|
|
|
|
|
|
Length = min(BufferMaxLength, strlen(FileAndLine));
|
|
|
|
memcpy(Buffer, FileAndLine, Length);
|
2011-06-19 09:23:03 +00:00
|
|
|
Buffer += Length;
|
|
|
|
BufferLength += Length;
|
|
|
|
BufferMaxLength -= Length;
|
|
|
|
}
|
2011-06-29 08:51:00 +00:00
|
|
|
if (Prepend)
|
2011-06-19 09:23:03 +00:00
|
|
|
{
|
2011-06-29 08:51:00 +00:00
|
|
|
Length = min(BufferMaxLength, strlen(Prepend));
|
|
|
|
memcpy(Buffer, Prepend, Length);
|
2011-06-19 09:23:03 +00:00
|
|
|
Buffer += Length;
|
|
|
|
BufferLength += Length;
|
|
|
|
BufferMaxLength -= Length;
|
|
|
|
}
|
2011-06-29 08:51:00 +00:00
|
|
|
if (Format)
|
|
|
|
{
|
|
|
|
Length = KmtVSNPrintF(Buffer, BufferMaxLength, Format, Arguments);
|
|
|
|
/* vsnprintf can return more than maxLength, we don't want to do that */
|
|
|
|
BufferLength += min(Length, BufferMaxLength);
|
|
|
|
}
|
2011-06-19 09:23:03 +00:00
|
|
|
return BufferLength;
|
|
|
|
}
|
|
|
|
|
2011-06-29 08:51:00 +00:00
|
|
|
KMT_FORMAT(ms_printf, 5, 6)
|
|
|
|
static SIZE_T KmtXSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR FileAndLine, PCSTR Prepend, PCSTR Format, ...)
|
2011-06-19 09:23:03 +00:00
|
|
|
{
|
|
|
|
SIZE_T BufferLength;
|
|
|
|
va_list Arguments;
|
|
|
|
va_start(Arguments, Format);
|
2011-06-29 08:51:00 +00:00
|
|
|
BufferLength = KmtXVSNPrintF(Buffer, BufferMaxLength, FileAndLine, Prepend, Format, Arguments);
|
2011-06-19 09:23:03 +00:00
|
|
|
va_end(Arguments);
|
|
|
|
return BufferLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID KmtFinishTest(PCSTR TestName)
|
|
|
|
{
|
|
|
|
CHAR MessageBuffer[512];
|
|
|
|
SIZE_T MessageLength;
|
|
|
|
|
2011-07-04 19:47:49 +00:00
|
|
|
if (!ResultBuffer)
|
|
|
|
return;
|
|
|
|
|
2011-06-19 09:23:03 +00:00
|
|
|
MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, NULL, NULL,
|
2011-06-29 08:51:00 +00:00
|
|
|
"%s: %ld tests executed (0 marked as todo, %ld failures), %ld skipped.\n",
|
2011-06-19 09:23:03 +00:00
|
|
|
TestName,
|
|
|
|
ResultBuffer->Successes + ResultBuffer->Failures,
|
2011-06-29 08:51:00 +00:00
|
|
|
ResultBuffer->Failures,
|
|
|
|
ResultBuffer->Skipped);
|
2011-06-19 09:23:03 +00:00
|
|
|
KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
|
|
|
|
}
|
|
|
|
|
2012-06-13 21:56:54 +00:00
|
|
|
BOOLEAN KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments)
|
2011-06-19 09:23:03 +00:00
|
|
|
{
|
|
|
|
CHAR MessageBuffer[512];
|
|
|
|
SIZE_T MessageLength;
|
|
|
|
|
2011-07-04 19:47:49 +00:00
|
|
|
if (!ResultBuffer)
|
2012-06-13 21:56:54 +00:00
|
|
|
return Condition != 0;
|
2011-07-04 19:47:49 +00:00
|
|
|
|
2011-06-19 09:23:03 +00:00
|
|
|
if (Condition)
|
|
|
|
{
|
|
|
|
InterlockedIncrement(&ResultBuffer->Successes);
|
|
|
|
|
|
|
|
if (0/*KmtReportSuccess*/)
|
|
|
|
{
|
2011-06-29 08:51:00 +00:00
|
|
|
MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Test succeeded\n", NULL);
|
2011-06-19 09:23:03 +00:00
|
|
|
KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
InterlockedIncrement(&ResultBuffer->Failures);
|
|
|
|
MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Test failed: ", Format, Arguments);
|
|
|
|
KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
|
|
|
|
}
|
2012-06-13 21:56:54 +00:00
|
|
|
|
|
|
|
return Condition != 0;
|
2011-06-19 09:23:03 +00:00
|
|
|
}
|
|
|
|
|
2012-06-13 21:56:54 +00:00
|
|
|
BOOLEAN KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format, ...)
|
2011-06-19 09:23:03 +00:00
|
|
|
{
|
2012-06-13 21:56:54 +00:00
|
|
|
BOOLEAN Ret;
|
2011-06-19 09:23:03 +00:00
|
|
|
va_list Arguments;
|
|
|
|
va_start(Arguments, Format);
|
2012-06-13 21:56:54 +00:00
|
|
|
Ret = KmtVOk(Condition, FileAndLine, Format, Arguments);
|
2011-06-19 09:23:03 +00:00
|
|
|
va_end(Arguments);
|
2012-06-13 21:56:54 +00:00
|
|
|
return Ret;
|
2011-06-19 09:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments)
|
|
|
|
{
|
|
|
|
CHAR MessageBuffer[512];
|
|
|
|
SIZE_T MessageLength;
|
|
|
|
|
|
|
|
MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": ", Format, Arguments);
|
|
|
|
KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID KmtTrace(PCSTR FileAndLine, PCSTR Format, ...)
|
|
|
|
{
|
|
|
|
va_list Arguments;
|
|
|
|
va_start(Arguments, Format);
|
|
|
|
KmtVTrace(FileAndLine, Format, Arguments);
|
|
|
|
va_end(Arguments);
|
|
|
|
}
|
|
|
|
|
2011-06-29 08:51:00 +00:00
|
|
|
BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments)
|
|
|
|
{
|
|
|
|
CHAR MessageBuffer[512];
|
|
|
|
SIZE_T MessageLength;
|
|
|
|
|
2011-07-04 19:47:49 +00:00
|
|
|
if (!ResultBuffer)
|
|
|
|
return !Condition;
|
|
|
|
|
2011-06-29 08:51:00 +00:00
|
|
|
if (!Condition)
|
|
|
|
{
|
|
|
|
InterlockedIncrement(&ResultBuffer->Skipped);
|
|
|
|
MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Tests skipped: ", Format, Arguments);
|
|
|
|
KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !Condition;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, ...)
|
|
|
|
{
|
|
|
|
BOOLEAN Ret;
|
|
|
|
va_list Arguments;
|
|
|
|
va_start(Arguments, Format);
|
|
|
|
Ret = KmtVSkip(Condition, FileAndLine, Format, Arguments);
|
|
|
|
va_end(Arguments);
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2011-09-11 11:22:00 +00:00
|
|
|
PVOID KmtAllocateGuarded(SIZE_T SizeRequested)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
SIZE_T Size = PAGE_ROUND_UP(SizeRequested + PAGE_SIZE);
|
|
|
|
PVOID VirtualMemory = NULL;
|
|
|
|
PCHAR StartOfBuffer;
|
|
|
|
|
|
|
|
Status = ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory, 0, &Size, MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
Size -= PAGE_SIZE;
|
|
|
|
Status = ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory, 0, &Size, MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
Size = 0;
|
|
|
|
Status = ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
|
|
|
|
ok_eq_hex(Status, STATUS_SUCCESS);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
StartOfBuffer = VirtualMemory;
|
|
|
|
StartOfBuffer += Size - SizeRequested;
|
|
|
|
|
|
|
|
return StartOfBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID KmtFreeGuarded(PVOID Pointer)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
PVOID VirtualMemory = (PVOID)PAGE_ROUND_DOWN((SIZE_T)Pointer);
|
|
|
|
SIZE_T Size = 0;
|
|
|
|
|
|
|
|
Status = ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
|
|
|
|
ok_eq_hex(Status, STATUS_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2011-06-13 17:50:07 +00:00
|
|
|
#endif /* defined KMT_DEFINE_TEST_FUNCTIONS */
|
|
|
|
|
2011-06-10 05:34:00 +00:00
|
|
|
#endif /* !defined _KMTEST_TEST_H_ */
|