From 902e8bb68eefc2ab657b2915b6d8fd3b5994ac8e Mon Sep 17 00:00:00 2001 From: Thomas Faber Date: Tue, 4 Nov 2014 20:55:16 +0000 Subject: [PATCH] [KMTESTS] - Move out definitions for user/kernel test utility functions into their own header (still not pretty, but at least the one header won't get as huge - Make KmtStartThread/KmtFinishThread available to all tests svn path=/trunk/; revision=65256 --- rostests/kmtests/include/kmt_test.h | 140 +--------------- rostests/kmtests/include/kmt_test_kernel.h | 184 +++++++++++++++++++++ rostests/kmtests/include/kmt_test_user.h | 37 +++++ rostests/kmtests/npfs/NpfsHelpers.c | 59 ------- rostests/kmtests/npfs/npfs.h | 11 -- 5 files changed, 225 insertions(+), 206 deletions(-) create mode 100644 rostests/kmtests/include/kmt_test_kernel.h create mode 100644 rostests/kmtests/include/kmt_test_user.h diff --git a/rostests/kmtests/include/kmt_test.h b/rostests/kmtests/include/kmt_test.h index a0e6139818c..0c6adaa6260 100644 --- a/rostests/kmtests/include/kmt_test.h +++ b/rostests/kmtests/include/kmt_test.h @@ -132,6 +132,8 @@ VOID KmtSetIrql(IN KIRQL NewIrql); BOOLEAN KmtAreInterruptsEnabled(VOID); ULONG KmtGetPoolTag(PVOID Memory); USHORT KmtGetPoolType(PVOID Memory); +PKTHREAD KmtStartThread(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext OPTIONAL); +VOID KmtFinishThread(IN PKTHREAD Thread OPTIONAL, IN PKEVENT Event OPTIONAL); #elif defined KMT_USER_MODE DWORD KmtRunKernelTest(IN PCSTR TestName); @@ -242,143 +244,9 @@ VOID KmtFreeGuarded(PVOID Pointer); #if defined KMT_DEFINE_TEST_FUNCTIONS #if defined KMT_KERNEL_MODE -BOOLEAN KmtIsCheckedBuild; -BOOLEAN KmtIsMultiProcessorBuild; -PCSTR KmtMajorFunctionNames[] = -{ - "Create", - "CreateNamedPipe", - "Close", - "Read", - "Write", - "QueryInformation", - "SetInformation", - "QueryEa", - "SetEa", - "FlushBuffers", - "QueryVolumeInformation", - "SetVolumeInformation", - "DirectoryControl", - "FileSystemControl", - "DeviceControl", - "InternalDeviceControl/Scsi", - "Shutdown", - "LockControl", - "Cleanup", - "CreateMailslot", - "QuerySecurity", - "SetSecurity", - "Power", - "SystemControl", - "DeviceChange", - "QueryQuota", - "SetQuota", - "Pnp/PnpPower" -}; - -VOID KmtSetIrql(IN KIRQL NewIrql) -{ - KIRQL Irql = KeGetCurrentIrql(); - if (Irql > NewIrql) - KeLowerIrql(NewIrql); - else if (Irql < NewIrql) - KeRaiseIrql(NewIrql, &Irql); -} - -BOOLEAN KmtAreInterruptsEnabled(VOID) -{ - return (__readeflags() & (1 << 9)) != 0; -} - -typedef struct _POOL_HEADER -{ - union - { - struct - { -#ifdef _M_AMD64 - USHORT PreviousSize:8; - USHORT PoolIndex:8; - USHORT BlockSize:8; - USHORT PoolType:8; -#else - USHORT PreviousSize:9; - USHORT PoolIndex:7; - USHORT BlockSize:9; - USHORT PoolType:7; -#endif - }; - ULONG Ulong1; - }; -#ifdef _M_AMD64 - ULONG PoolTag; -#endif - union - { -#ifdef _M_AMD64 - PEPROCESS ProcessBilled; -#else - ULONG PoolTag; -#endif - struct - { - USHORT AllocatorBackTraceIndex; - USHORT PoolTagHash; - }; - }; -} POOL_HEADER, *PPOOL_HEADER; - -ULONG KmtGetPoolTag(PVOID Memory) -{ - PPOOL_HEADER Header; - - /* it's not so easy for allocations of PAGE_SIZE */ - if (((ULONG_PTR)Memory & (PAGE_SIZE - 1)) == 0) - return 'TooL'; - - Header = Memory; - Header--; - - return Header->PoolTag; -} - -USHORT KmtGetPoolType(PVOID Memory) -{ - PPOOL_HEADER Header; - - /* it's not so easy for allocations of PAGE_SIZE */ - if (((ULONG_PTR)Memory & (PAGE_SIZE - 1)) == 0) - return 0; - - Header = Memory; - Header--; - - return Header->PoolType; -} - -INT __cdecl KmtVSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0); +#include "kmt_test_kernel.h" #elif defined KMT_USER_MODE -static PKMT_RESULTBUFFER KmtAllocateResultBuffer(SIZE_T ResultBufferSize) -{ - PKMT_RESULTBUFFER Buffer = HeapAlloc(GetProcessHeap(), 0, ResultBufferSize); - if (!Buffer) - return NULL; - - Buffer->Successes = 0; - Buffer->Failures = 0; - Buffer->Skipped = 0; - Buffer->LogBufferLength = 0; - Buffer->LogBufferMaxLength = (ULONG)ResultBufferSize - FIELD_OFFSET(KMT_RESULTBUFFER, LogBuffer); - - return Buffer; -} - -static VOID KmtFreeResultBuffer(PKMT_RESULTBUFFER Buffer) -{ - HeapFree(GetProcessHeap(), 0, Buffer); -} - -#define KmtVSNPrintF vsnprintf +#include "kmt_test_user.h" #endif /* defined KMT_USER_MODE */ PKMT_RESULTBUFFER ResultBuffer = NULL; diff --git a/rostests/kmtests/include/kmt_test_kernel.h b/rostests/kmtests/include/kmt_test_kernel.h new file mode 100644 index 00000000000..aa0a84bd21a --- /dev/null +++ b/rostests/kmtests/include/kmt_test_kernel.h @@ -0,0 +1,184 @@ +/* + * PROJECT: ReactOS kernel-mode tests + * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory + * PURPOSE: Kernel-Mode Test Suite test framework declarations + * PROGRAMMER: Thomas Faber + */ + +#ifndef _KMTEST_TEST_KERNEL_H_ +#define _KMTEST_TEST_KERNEL_H_ + +#if !defined _KMTEST_TEST_H_ +#error include kmt_test.h instead of including kmt_test_kernel.h +#endif /* !defined _KMTEST_TEST_H_ */ + +BOOLEAN KmtIsCheckedBuild; +BOOLEAN KmtIsMultiProcessorBuild; +PCSTR KmtMajorFunctionNames[] = +{ + "Create", + "CreateNamedPipe", + "Close", + "Read", + "Write", + "QueryInformation", + "SetInformation", + "QueryEa", + "SetEa", + "FlushBuffers", + "QueryVolumeInformation", + "SetVolumeInformation", + "DirectoryControl", + "FileSystemControl", + "DeviceControl", + "InternalDeviceControl/Scsi", + "Shutdown", + "LockControl", + "Cleanup", + "CreateMailslot", + "QuerySecurity", + "SetSecurity", + "Power", + "SystemControl", + "DeviceChange", + "QueryQuota", + "SetQuota", + "Pnp/PnpPower" +}; + +VOID KmtSetIrql(IN KIRQL NewIrql) +{ + KIRQL Irql = KeGetCurrentIrql(); + if (Irql > NewIrql) + KeLowerIrql(NewIrql); + else if (Irql < NewIrql) + KeRaiseIrql(NewIrql, &Irql); +} + +BOOLEAN KmtAreInterruptsEnabled(VOID) +{ + return (__readeflags() & (1 << 9)) != 0; +} + +typedef struct _POOL_HEADER +{ + union + { + struct + { +#ifdef _M_AMD64 + USHORT PreviousSize:8; + USHORT PoolIndex:8; + USHORT BlockSize:8; + USHORT PoolType:8; +#else + USHORT PreviousSize:9; + USHORT PoolIndex:7; + USHORT BlockSize:9; + USHORT PoolType:7; +#endif + }; + ULONG Ulong1; + }; +#ifdef _M_AMD64 + ULONG PoolTag; +#endif + union + { +#ifdef _M_AMD64 + PEPROCESS ProcessBilled; +#else + ULONG PoolTag; +#endif + struct + { + USHORT AllocatorBackTraceIndex; + USHORT PoolTagHash; + }; + }; +} POOL_HEADER, *PPOOL_HEADER; + +ULONG KmtGetPoolTag(PVOID Memory) +{ + PPOOL_HEADER Header; + + /* it's not so easy for allocations of PAGE_SIZE */ + if (((ULONG_PTR)Memory & (PAGE_SIZE - 1)) == 0) + return 'TooL'; + + Header = Memory; + Header--; + + return Header->PoolTag; +} + +USHORT KmtGetPoolType(PVOID Memory) +{ + PPOOL_HEADER Header; + + /* it's not so easy for allocations of PAGE_SIZE */ + if (((ULONG_PTR)Memory & (PAGE_SIZE - 1)) == 0) + return 0; + + Header = Memory; + Header--; + + return Header->PoolType; +} + +PKTHREAD KmtStartThread(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext OPTIONAL) +{ + NTSTATUS Status; + OBJECT_ATTRIBUTES ObjectAttributes; + HANDLE ThreadHandle; + PVOID ThreadObject = NULL; + + InitializeObjectAttributes(&ObjectAttributes, + NULL, + OBJ_KERNEL_HANDLE, + NULL, + NULL); + ThreadHandle = INVALID_HANDLE_VALUE; + Status = PsCreateSystemThread(&ThreadHandle, + SYNCHRONIZE, + &ObjectAttributes, + NULL, + NULL, + StartRoutine, + StartContext); + ok_eq_hex(Status, STATUS_SUCCESS); + if (!skip(NT_SUCCESS(Status) && ThreadHandle != NULL && ThreadHandle != INVALID_HANDLE_VALUE, "No thread\n")) + { + Status = ObReferenceObjectByHandle(ThreadHandle, + SYNCHRONIZE, + *PsThreadType, + KernelMode, + &ThreadObject, + NULL); + ok_eq_hex(Status, STATUS_SUCCESS); + ObCloseHandle(ThreadHandle, KernelMode); + } + return ThreadObject; +} + +VOID KmtFinishThread(IN PKTHREAD Thread OPTIONAL, IN PKEVENT Event OPTIONAL) +{ + NTSTATUS Status; + + if (skip(Thread != NULL, "No thread\n")) + return; + + if (Event) + KeSetEvent(Event, IO_NO_INCREMENT, TRUE); + Status = KeWaitForSingleObject(Thread, + Executive, + KernelMode, + FALSE, + NULL); + ok_eq_hex(Status, STATUS_SUCCESS); + ObDereferenceObject(Thread); +} + +INT __cdecl KmtVSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0); + +#endif /* !defined _KMTEST_TEST_KERNEL_H_ */ diff --git a/rostests/kmtests/include/kmt_test_user.h b/rostests/kmtests/include/kmt_test_user.h new file mode 100644 index 00000000000..580dc755e27 --- /dev/null +++ b/rostests/kmtests/include/kmt_test_user.h @@ -0,0 +1,37 @@ +/* + * PROJECT: ReactOS kernel-mode tests + * LICENSE: LGPLv2.1+ - See COPYING.LIB in the top level directory + * PURPOSE: Kernel-Mode Test Suite test framework declarations + * PROGRAMMER: Thomas Faber + */ + +#ifndef _KMTEST_TEST_USER_H_ +#define _KMTEST_TEST_USER_H_ + +#if !defined _KMTEST_TEST_H_ +#error include kmt_test.h instead of including kmt_test_user.h +#endif /* !defined _KMTEST_TEST_H_ */ + +static PKMT_RESULTBUFFER KmtAllocateResultBuffer(SIZE_T ResultBufferSize) +{ + PKMT_RESULTBUFFER Buffer = HeapAlloc(GetProcessHeap(), 0, ResultBufferSize); + if (!Buffer) + return NULL; + + Buffer->Successes = 0; + Buffer->Failures = 0; + Buffer->Skipped = 0; + Buffer->LogBufferLength = 0; + Buffer->LogBufferMaxLength = (ULONG)ResultBufferSize - FIELD_OFFSET(KMT_RESULTBUFFER, LogBuffer); + + return Buffer; +} + +static VOID KmtFreeResultBuffer(PKMT_RESULTBUFFER Buffer) +{ + HeapFree(GetProcessHeap(), 0, Buffer); +} + +#define KmtVSNPrintF vsnprintf + +#endif /* !defined _KMTEST_TEST_USER_H_ */ diff --git a/rostests/kmtests/npfs/NpfsHelpers.c b/rostests/kmtests/npfs/NpfsHelpers.c index f4cd46e945a..ec0d3669e96 100644 --- a/rostests/kmtests/npfs/NpfsHelpers.c +++ b/rostests/kmtests/npfs/NpfsHelpers.c @@ -713,62 +713,3 @@ TriggerWork( KeSetEvent(&Context->StartWorkEvent, IO_NO_INCREMENT, TRUE); return WaitForWork(Context, MilliSeconds); } - -PKTHREAD -KmtStartThread( - IN PKSTART_ROUTINE StartRoutine, - IN PVOID StartContext OPTIONAL) -{ - NTSTATUS Status; - OBJECT_ATTRIBUTES ObjectAttributes; - HANDLE ThreadHandle; - PVOID ThreadObject = NULL; - - InitializeObjectAttributes(&ObjectAttributes, - NULL, - OBJ_KERNEL_HANDLE, - NULL, - NULL); - ThreadHandle = INVALID_HANDLE_VALUE; - Status = PsCreateSystemThread(&ThreadHandle, - SYNCHRONIZE, - &ObjectAttributes, - NULL, - NULL, - StartRoutine, - StartContext); - ok_eq_hex(Status, STATUS_SUCCESS); - if (!skip(NT_SUCCESS(Status) && ThreadHandle != NULL && ThreadHandle != INVALID_HANDLE_VALUE, "No thread\n")) - { - Status = ObReferenceObjectByHandle(ThreadHandle, - SYNCHRONIZE, - *PsThreadType, - KernelMode, - &ThreadObject, - NULL); - ok_eq_hex(Status, STATUS_SUCCESS); - ObCloseHandle(ThreadHandle, KernelMode); - } - return ThreadObject; -} - -VOID -KmtFinishThread( - IN PKTHREAD Thread OPTIONAL, - IN PKEVENT Event OPTIONAL) -{ - NTSTATUS Status; - - if (skip(Thread != NULL, "No thread\n")) - return; - - if (Event) - KeSetEvent(Event, IO_NO_INCREMENT, TRUE); - Status = KeWaitForSingleObject(Thread, - Executive, - KernelMode, - FALSE, - NULL); - ok_eq_hex(Status, STATUS_SUCCESS); - ObDereferenceObject(Thread); -} diff --git a/rostests/kmtests/npfs/npfs.h b/rostests/kmtests/npfs/npfs.h index dd28c839120..b7fa62c2b5a 100644 --- a/rostests/kmtests/npfs/npfs.h +++ b/rostests/kmtests/npfs/npfs.h @@ -211,15 +211,4 @@ TriggerWork( IN PTHREAD_CONTEXT Context, IN ULONG MilliSeconds); - -PKTHREAD -KmtStartThread( - IN PKSTART_ROUTINE StartRoutine, - IN PVOID StartContext OPTIONAL); - -VOID -KmtFinishThread( - IN PKTHREAD Thread OPTIONAL, - IN PKEVENT Event OPTIONAL); - #endif /* !defined _KMTEST_NPFS_H_ */