[KMTEST] Initial usermode support for testing FS mini-filters (#81)
[reactos.git] / modules / rostests / kmtests / include / kmt_test.h
1 /*
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 <thomas.faber@reactos.org>
6 */
7
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
11 */
12
13 #ifndef _KMTEST_TEST_H_
14 #define _KMTEST_TEST_H_
15
16 #include <kmt_platform.h>
17
18 typedef VOID KMT_TESTFUNC(VOID);
19 typedef KMT_TESTFUNC *PKMT_TESTFUNC;
20
21 typedef struct
22 {
23 const char *TestName;
24 KMT_TESTFUNC *TestFunction;
25 } KMT_TEST, *PKMT_TEST;
26
27 typedef const KMT_TEST CKMT_TEST, *PCKMT_TEST;
28
29 extern const KMT_TEST TestList[];
30
31 typedef struct
32 {
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;
40
41 #ifndef KMT_STANDALONE_DRIVER
42
43 /* usermode call-back mechanism */
44
45 /* list of supported operations */
46 typedef enum _KMT_CALLBACK_INFORMATION_CLASS
47 {
48 QueryVirtualMemory
49 } KMT_CALLBACK_INFORMATION_CLASS, *PKMT_CALLBACK_INFORMATION_CLASS;
50
51 /* TODO: "response" is a little generic */
52 typedef union _KMT_RESPONSE
53 {
54 MEMORY_BASIC_INFORMATION MemInfo;
55 } KMT_RESPONSE, *PKMT_RESPONSE;
56
57 /* this struct is sent from driver to usermode */
58 typedef struct _KMT_CALLBACK_REQUEST_PACKET
59 {
60 ULONG RequestId;
61 KMT_CALLBACK_INFORMATION_CLASS OperationClass;
62 PVOID Parameters;
63 } KMT_CALLBACK_REQUEST_PACKET, *PKMT_CALLBACK_REQUEST_PACKET;
64
65 PKMT_RESPONSE KmtUserModeCallback(KMT_CALLBACK_INFORMATION_CLASS Operation, PVOID Parameters);
66 VOID KmtFreeCallbackResponse(PKMT_RESPONSE Response);
67
68 //macro to simplify using the mechanism
69 #define Test_NtQueryVirtualMemory(BaseAddress, Size, AllocationType, ProtectionType) \
70 do { \
71 PKMT_RESPONSE NtQueryTest = KmtUserModeCallback(QueryVirtualMemory, BaseAddress); \
72 if (NtQueryTest != NULL) \
73 { \
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); \
78 } \
79 } while (0) \
80
81 #endif
82
83 #ifdef KMT_STANDALONE_DRIVER
84 #define KMT_KERNEL_MODE
85
86 typedef NTSTATUS (KMT_IRP_HANDLER)(
87 IN PDEVICE_OBJECT DeviceObject,
88 IN PIRP Irp,
89 IN PIO_STACK_LOCATION IoStackLocation);
90 typedef KMT_IRP_HANDLER *PKMT_IRP_HANDLER;
91
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);
94
95 typedef NTSTATUS (KMT_MESSAGE_HANDLER)(
96 IN PDEVICE_OBJECT DeviceObject,
97 IN ULONG ControlCode,
98 IN PVOID Buffer OPTIONAL,
99 IN SIZE_T InLength,
100 IN OUT PSIZE_T OutLength);
101 typedef KMT_MESSAGE_HANDLER *PKMT_MESSAGE_HANDLER;
102
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);
105
106 typedef enum
107 {
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 TESTENTRY_NO_READONLY_DEVICE = 16,
113 TESTENTRY_BUFFERED_IO_DEVICE = 32,
114 } KMT_TESTENTRY_FLAGS;
115
116 NTSTATUS TestEntry(IN PDRIVER_OBJECT DriverObject, IN PCUNICODE_STRING RegistryPath, OUT PCWSTR *DeviceName, IN OUT INT *Flags);
117 VOID TestUnload(IN PDRIVER_OBJECT DriverObject);
118 #endif /* defined KMT_STANDALONE_DRIVER */
119
120 #ifdef KMT_FILTER_DRIVER
121 #ifndef KMT_KERNEL_MODE
122 #define KMT_KERNEL_MODE
123 #endif
124
125 NTSTATUS KmtFilterRegisterCallbacks(_In_ CONST FLT_OPERATION_REGISTRATION *OperationRegistration);
126
127 typedef enum
128 {
129 TESTENTRY_NO_REGISTER_FILTER = 1,
130 TESTENTRY_NO_CREATE_COMMS_PORT = 2,
131 TESTENTRY_NO_START_FILTERING = 4,
132 } KMT_MINIFILTER_FLAGS;
133
134 VOID TestFilterUnload(_In_ ULONG Flags);
135 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 RealSectorSize, _In_ ULONG SectorSize);
136 VOID TestQueryTeardown(_In_ PCFLT_RELATED_OBJECTS FltObjects, _In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags);
137
138 NTSTATUS KmtFilterRegisterComms(_In_ PFLT_CONNECT_NOTIFY ConnectNotifyCallback, _In_ PFLT_DISCONNECT_NOTIFY DisconnectNotifyCallback, _In_opt_ PFLT_MESSAGE_NOTIFY MessageNotifyCallback, _In_ LONG MaxClientConnections);
139
140 #endif/* defined KMT_FILTER_DRIVER */
141
142
143 #ifdef KMT_KERNEL_MODE
144 /* Device Extension layout */
145 typedef struct
146 {
147 PKMT_RESULTBUFFER ResultBuffer;
148 PMDL Mdl;
149 } KMT_DEVICE_EXTENSION, *PKMT_DEVICE_EXTENSION;
150
151 extern BOOLEAN KmtIsCheckedBuild;
152 extern BOOLEAN KmtIsMultiProcessorBuild;
153 extern PCSTR KmtMajorFunctionNames[];
154 extern PDRIVER_OBJECT KmtDriverObject;
155
156 VOID KmtSetIrql(IN KIRQL NewIrql);
157 BOOLEAN KmtAreInterruptsEnabled(VOID);
158 ULONG KmtGetPoolTag(PVOID Memory);
159 USHORT KmtGetPoolType(PVOID Memory);
160 PVOID KmtGetSystemRoutineAddress(IN PCWSTR RoutineName);
161 PKTHREAD KmtStartThread(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext OPTIONAL);
162 VOID KmtFinishThread(IN PKTHREAD Thread OPTIONAL, IN PKEVENT Event OPTIONAL);
163 #elif defined KMT_USER_MODE
164 DWORD KmtRunKernelTest(IN PCSTR TestName);
165
166 VOID KmtLoadDriver(IN PCWSTR ServiceName, IN BOOLEAN RestartIfRunning);
167 VOID KmtUnloadDriver(VOID);
168 VOID KmtOpenDriver(VOID);
169 VOID KmtCloseDriver(VOID);
170
171 DWORD KmtSendToDriver(IN DWORD ControlCode);
172 DWORD KmtSendStringToDriver(IN DWORD ControlCode, IN PCSTR String);
173 DWORD KmtSendWStringToDriver(IN DWORD ControlCode, IN PCWSTR String);
174 DWORD KmtSendUlongToDriver(IN DWORD ControlCode, IN DWORD Value);
175 DWORD KmtSendBufferToDriver(IN DWORD ControlCode, IN OUT PVOID Buffer OPTIONAL, IN DWORD InLength, IN OUT PDWORD OutLength);
176
177 DWORD KmtFltLoadDriver(_In_z_ PCWSTR ServiceName, _In_ BOOLEAN RestartIfRunning, _In_ BOOLEAN ConnectComms, _Out_ HANDLE *hPort);
178 DWORD KmtFltUnloadDriver(_In_ HANDLE *hPort, _In_ BOOLEAN DisonnectComms);
179 DWORD KmtFltRunKernelTest(_In_ HANDLE hPort, _In_z_ PCSTR TestName);
180 DWORD KmtFltSendToDriver(_In_ HANDLE hPort, _In_ DWORD Message);
181 DWORD KmtFltSendStringToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ PCSTR String);
182 DWORD KmtFltSendWStringToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ PCWSTR String);
183 DWORD KmtFltSendUlongToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_ DWORD Value);
184 DWORD KmtFltSendBufferToDriver(_In_ HANDLE hPort, _In_ DWORD Message, _In_reads_bytes_(BufferSize) LPVOID Buffer, _In_ DWORD BufferSize, _Out_writes_bytes_to_opt_(dwOutBufferSize, *lpBytesReturned) LPVOID lpOutBuffer, _In_ DWORD dwOutBufferSize, _Out_opt_ LPDWORD lpBytesReturned);
185
186 #else /* if !defined KMT_KERNEL_MODE && !defined KMT_USER_MODE */
187 #error either KMT_KERNEL_MODE or KMT_USER_MODE must be defined
188 #endif /* !defined KMT_KERNEL_MODE && !defined KMT_USER_MODE */
189
190 extern PKMT_RESULTBUFFER ResultBuffer;
191
192 #ifdef __GNUC__
193 /* TODO: GCC doesn't understand %wZ :( */
194 #define KMT_FORMAT(type, fmt, first) /*__attribute__((__format__(type, fmt, first)))*/
195 #elif !defined __GNUC__
196 #define KMT_FORMAT(type, fmt, first)
197 #endif /* !defined __GNUC__ */
198
199 #define START_TEST(name) VOID Test_##name(VOID)
200
201 #ifndef KMT_STRINGIZE
202 #define KMT_STRINGIZE(x) #x
203 #endif /* !defined KMT_STRINGIZE */
204 #define ok(test, ...) ok_(test, __FILE__, __LINE__, __VA_ARGS__)
205 #define trace(...) trace_( __FILE__, __LINE__, __VA_ARGS__)
206 #define skip(test, ...) skip_(test, __FILE__, __LINE__, __VA_ARGS__)
207
208 #define ok_(test, file, line, ...) KmtOk(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
209 #define trace_(file, line, ...) KmtTrace( file ":" KMT_STRINGIZE(line), __VA_ARGS__)
210 #define skip_(test, file, line, ...) KmtSkip(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
211
212 BOOLEAN KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0);
213 BOOLEAN KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 3, 4);
214 VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 2, 0);
215 VOID KmtTrace(PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 2, 3);
216 BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0);
217 BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 3, 4);
218 PVOID KmtAllocateGuarded(SIZE_T SizeRequested);
219 VOID KmtFreeGuarded(PVOID Pointer);
220
221 #ifdef KMT_KERNEL_MODE
222 #define ok_irql(irql) ok(KeGetCurrentIrql() == irql, "IRQL is %d, expected %d\n", KeGetCurrentIrql(), irql)
223 #endif /* defined KMT_KERNEL_MODE */
224 #define ok_eq_print(value, expected, spec) ok((value) == (expected), #value " = " spec ", expected " spec "\n", value, expected)
225 #define ok_eq_pointer(value, expected) ok_eq_print(value, expected, "%p")
226 #define ok_eq_int(value, expected) ok_eq_print(value, expected, "%d")
227 #define ok_eq_uint(value, expected) ok_eq_print(value, expected, "%u")
228 #define ok_eq_long(value, expected) ok_eq_print(value, expected, "%ld")
229 #define ok_eq_ulong(value, expected) ok_eq_print(value, expected, "%lu")
230 #define ok_eq_longlong(value, expected) ok_eq_print(value, expected, "%I64d")
231 #define ok_eq_ulonglong(value, expected) ok_eq_print(value, expected, "%I64u")
232 #define ok_eq_char(value, expected) ok_eq_print(value, expected, "%c")
233 #define ok_eq_wchar(value, expected) ok_eq_print(value, expected, "%C")
234 #ifndef _WIN64
235 #define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%lu")
236 #define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%ld")
237 #define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%lu")
238 #elif defined _WIN64
239 #define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%I64u")
240 #define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%I64d")
241 #define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%I64u")
242 #endif /* defined _WIN64 */
243 #define ok_eq_hex(value, expected) ok_eq_print(value, expected, "0x%08lx")
244 #define ok_bool_true(value, desc) ok((value) == TRUE, desc " FALSE, expected TRUE\n")
245 #define ok_bool_false(value, desc) ok((value) == FALSE, desc " TRUE, expected FALSE\n")
246 #define ok_eq_bool(value, expected) ok((value) == (expected), #value " = %s, expected %s\n", \
247 (value) ? "TRUE" : "FALSE", \
248 (expected) ? "TRUE" : "FALSE")
249 #define ok_eq_str(value, expected) ok(!strcmp(value, expected), #value " = \"%s\", expected \"%s\"\n", value, expected)
250 #define ok_eq_wstr(value, expected) ok(!wcscmp(value, expected), #value " = \"%ls\", expected \"%ls\"\n", value, expected)
251 #define ok_eq_tag(value, expected) ok_eq_print(value, expected, "0x%08lx")
252
253 #define KMT_MAKE_CODE(ControlCode) CTL_CODE(FILE_DEVICE_UNKNOWN, \
254 0xC00 + (ControlCode), \
255 METHOD_BUFFERED, \
256 FILE_ANY_ACCESS)
257
258 #define MICROSECOND 10
259 #define MILLISECOND (1000 * MICROSECOND)
260 #define SECOND (1000 * MILLISECOND)
261
262 /* See apitests/include/apitest.h */
263 #define KmtInvalidPointer ((PVOID)0x5555555555555555ULL)
264
265 #define KmtStartSeh() \
266 { \
267 NTSTATUS ExceptionStatus = STATUS_SUCCESS; \
268 _SEH2_TRY \
269 {
270
271 #define KmtEndSeh(ExpectedStatus) \
272 } \
273 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) \
274 { \
275 ExceptionStatus = _SEH2_GetExceptionCode(); \
276 } \
277 _SEH2_END; \
278 ok_eq_hex(ExceptionStatus, (ExpectedStatus)); \
279 }
280
281 #define KmtGetSystemOrEmbeddedRoutineAddress(RoutineName) \
282 p##RoutineName = KmtGetSystemRoutineAddress(L ## #RoutineName); \
283 if (!p##RoutineName) \
284 { \
285 p##RoutineName = RoutineName; \
286 trace("Using embedded routine for " #RoutineName "\n"); \
287 } \
288 else \
289 trace("Using system routine for " #RoutineName "\n");
290
291 #if defined KMT_DEFINE_TEST_FUNCTIONS
292
293 #if defined KMT_KERNEL_MODE
294 #include "kmt_test_kernel.h"
295 #elif defined KMT_USER_MODE
296 #include "kmt_test_user.h"
297 #endif /* defined KMT_USER_MODE */
298
299 PKMT_RESULTBUFFER ResultBuffer = NULL;
300
301 static VOID KmtAddToLogBuffer(PKMT_RESULTBUFFER Buffer, PCSTR String, SIZE_T Length)
302 {
303 LONG OldLength;
304 LONG NewLength;
305
306 if (!Buffer)
307 return;
308
309 do
310 {
311 OldLength = Buffer->LogBufferLength;
312 NewLength = OldLength + (ULONG)Length;
313 if (NewLength > Buffer->LogBufferMaxLength)
314 return;
315 } while (InterlockedCompareExchange(&Buffer->LogBufferLength, NewLength, OldLength) != OldLength);
316
317 memcpy(&Buffer->LogBuffer[OldLength], String, Length);
318 }
319
320 KMT_FORMAT(ms_printf, 5, 0)
321 static SIZE_T KmtXVSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR FileAndLine, PCSTR Prepend, PCSTR Format, va_list Arguments)
322 {
323 SIZE_T BufferLength = 0;
324 SIZE_T Length;
325
326 if (FileAndLine)
327 {
328 PCSTR Slash;
329 Slash = strrchr(FileAndLine, '\\');
330 if (Slash)
331 FileAndLine = Slash + 1;
332 Slash = strrchr(FileAndLine, '/');
333 if (Slash)
334 FileAndLine = Slash + 1;
335
336 Length = min(BufferMaxLength, strlen(FileAndLine));
337 memcpy(Buffer, FileAndLine, Length);
338 Buffer += Length;
339 BufferLength += Length;
340 BufferMaxLength -= Length;
341 }
342 if (Prepend)
343 {
344 Length = min(BufferMaxLength, strlen(Prepend));
345 memcpy(Buffer, Prepend, Length);
346 Buffer += Length;
347 BufferLength += Length;
348 BufferMaxLength -= Length;
349 }
350 if (Format)
351 {
352 Length = KmtVSNPrintF(Buffer, BufferMaxLength, Format, Arguments);
353 /* vsnprintf can return more than maxLength, we don't want to do that */
354 BufferLength += min(Length, BufferMaxLength);
355 }
356 return BufferLength;
357 }
358
359 KMT_FORMAT(ms_printf, 5, 6)
360 static SIZE_T KmtXSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR FileAndLine, PCSTR Prepend, PCSTR Format, ...)
361 {
362 SIZE_T BufferLength;
363 va_list Arguments;
364 va_start(Arguments, Format);
365 BufferLength = KmtXVSNPrintF(Buffer, BufferMaxLength, FileAndLine, Prepend, Format, Arguments);
366 va_end(Arguments);
367 return BufferLength;
368 }
369
370 VOID KmtFinishTest(PCSTR TestName)
371 {
372 CHAR MessageBuffer[512];
373 SIZE_T MessageLength;
374
375 if (!ResultBuffer)
376 return;
377
378 MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, NULL, NULL,
379 "%s: %ld tests executed (0 marked as todo, %ld failures), %ld skipped.\n",
380 TestName,
381 ResultBuffer->Successes + ResultBuffer->Failures,
382 ResultBuffer->Failures,
383 ResultBuffer->Skipped);
384 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
385 }
386
387 BOOLEAN KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments)
388 {
389 CHAR MessageBuffer[512];
390 SIZE_T MessageLength;
391
392 if (!ResultBuffer)
393 return Condition != 0;
394
395 if (Condition)
396 {
397 InterlockedIncrement(&ResultBuffer->Successes);
398
399 if (0/*KmtReportSuccess*/)
400 {
401 MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Test succeeded\n", NULL);
402 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
403 }
404 }
405 else
406 {
407 InterlockedIncrement(&ResultBuffer->Failures);
408 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Test failed: ", Format, Arguments);
409 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
410 }
411
412 return Condition != 0;
413 }
414
415 BOOLEAN KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format, ...)
416 {
417 BOOLEAN Ret;
418 va_list Arguments;
419 va_start(Arguments, Format);
420 Ret = KmtVOk(Condition, FileAndLine, Format, Arguments);
421 va_end(Arguments);
422 return Ret;
423 }
424
425 VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments)
426 {
427 CHAR MessageBuffer[512];
428 SIZE_T MessageLength;
429
430 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": ", Format, Arguments);
431 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
432 }
433
434 VOID KmtTrace(PCSTR FileAndLine, PCSTR Format, ...)
435 {
436 va_list Arguments;
437 va_start(Arguments, Format);
438 KmtVTrace(FileAndLine, Format, Arguments);
439 va_end(Arguments);
440 }
441
442 BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments)
443 {
444 CHAR MessageBuffer[512];
445 SIZE_T MessageLength;
446
447 if (!ResultBuffer)
448 return !Condition;
449
450 if (!Condition)
451 {
452 InterlockedIncrement(&ResultBuffer->Skipped);
453 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Tests skipped: ", Format, Arguments);
454 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
455 }
456
457 return !Condition;
458 }
459
460 BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, ...)
461 {
462 BOOLEAN Ret;
463 va_list Arguments;
464 va_start(Arguments, Format);
465 Ret = KmtVSkip(Condition, FileAndLine, Format, Arguments);
466 va_end(Arguments);
467 return Ret;
468 }
469
470 PVOID KmtAllocateGuarded(SIZE_T SizeRequested)
471 {
472 NTSTATUS Status;
473 SIZE_T Size = PAGE_ROUND_UP(SizeRequested + PAGE_SIZE);
474 PVOID VirtualMemory = NULL;
475 PCHAR StartOfBuffer;
476
477 Status = ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory, 0, &Size, MEM_RESERVE, PAGE_NOACCESS);
478
479 if (!NT_SUCCESS(Status))
480 return NULL;
481
482 Size -= PAGE_SIZE;
483 Status = ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory, 0, &Size, MEM_COMMIT, PAGE_READWRITE);
484 if (!NT_SUCCESS(Status))
485 {
486 Size = 0;
487 Status = ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
488 ok_eq_hex(Status, STATUS_SUCCESS);
489 return NULL;
490 }
491
492 StartOfBuffer = VirtualMemory;
493 StartOfBuffer += Size - SizeRequested;
494
495 return StartOfBuffer;
496 }
497
498 VOID KmtFreeGuarded(PVOID Pointer)
499 {
500 NTSTATUS Status;
501 PVOID VirtualMemory = (PVOID)PAGE_ROUND_DOWN((SIZE_T)Pointer);
502 SIZE_T Size = 0;
503
504 Status = ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
505 ok_eq_hex(Status, STATUS_SUCCESS);
506 }
507
508 #endif /* defined KMT_DEFINE_TEST_FUNCTIONS */
509
510 #endif /* !defined _KMTEST_TEST_H_ */