[KMTESTS]
[reactos.git] / 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 } KMT_TESTENTRY_FLAGS;
113
114 NTSTATUS TestEntry(IN PDRIVER_OBJECT DriverObject, IN PCUNICODE_STRING RegistryPath, OUT PCWSTR *DeviceName, IN OUT INT *Flags);
115 VOID TestUnload(IN PDRIVER_OBJECT DriverObject);
116 #endif /* defined KMT_STANDALONE_DRIVER */
117
118 #ifdef KMT_KERNEL_MODE
119 /* Device Extension layout */
120 typedef struct
121 {
122 PKMT_RESULTBUFFER ResultBuffer;
123 PMDL Mdl;
124 } KMT_DEVICE_EXTENSION, *PKMT_DEVICE_EXTENSION;
125
126 extern BOOLEAN KmtIsCheckedBuild;
127 extern BOOLEAN KmtIsMultiProcessorBuild;
128 extern PCSTR KmtMajorFunctionNames[];
129 extern PDRIVER_OBJECT KmtDriverObject;
130
131 VOID KmtSetIrql(IN KIRQL NewIrql);
132 BOOLEAN KmtAreInterruptsEnabled(VOID);
133 ULONG KmtGetPoolTag(PVOID Memory);
134 USHORT KmtGetPoolType(PVOID Memory);
135 PVOID KmtGetSystemRoutineAddress(IN PCWSTR RoutineName);
136 PKTHREAD KmtStartThread(IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext OPTIONAL);
137 VOID KmtFinishThread(IN PKTHREAD Thread OPTIONAL, IN PKEVENT Event OPTIONAL);
138 #elif defined KMT_USER_MODE
139 DWORD KmtRunKernelTest(IN PCSTR TestName);
140
141 VOID KmtLoadDriver(IN PCWSTR ServiceName, IN BOOLEAN RestartIfRunning);
142 VOID KmtUnloadDriver(VOID);
143 VOID KmtOpenDriver(VOID);
144 VOID KmtCloseDriver(VOID);
145
146 DWORD KmtSendToDriver(IN DWORD ControlCode);
147 DWORD KmtSendStringToDriver(IN DWORD ControlCode, IN PCSTR String);
148 DWORD KmtSendWStringToDriver(IN DWORD ControlCode, IN PCWSTR String);
149 DWORD KmtSendBufferToDriver(IN DWORD ControlCode, IN OUT PVOID Buffer OPTIONAL, IN DWORD InLength, IN OUT PDWORD OutLength);
150 #else /* if !defined KMT_KERNEL_MODE && !defined KMT_USER_MODE */
151 #error either KMT_KERNEL_MODE or KMT_USER_MODE must be defined
152 #endif /* !defined KMT_KERNEL_MODE && !defined KMT_USER_MODE */
153
154 extern PKMT_RESULTBUFFER ResultBuffer;
155
156 #ifdef __GNUC__
157 /* TODO: GCC doesn't understand %wZ :( */
158 #define KMT_FORMAT(type, fmt, first) /*__attribute__((__format__(type, fmt, first)))*/
159 #elif !defined __GNUC__
160 #define KMT_FORMAT(type, fmt, first)
161 #endif /* !defined __GNUC__ */
162
163 #define START_TEST(name) VOID Test_##name(VOID)
164
165 #ifndef KMT_STRINGIZE
166 #define KMT_STRINGIZE(x) #x
167 #endif /* !defined KMT_STRINGIZE */
168 #define ok(test, ...) ok_(test, __FILE__, __LINE__, __VA_ARGS__)
169 #define trace(...) trace_( __FILE__, __LINE__, __VA_ARGS__)
170 #define skip(test, ...) skip_(test, __FILE__, __LINE__, __VA_ARGS__)
171
172 #define ok_(test, file, line, ...) KmtOk(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
173 #define trace_(file, line, ...) KmtTrace( file ":" KMT_STRINGIZE(line), __VA_ARGS__)
174 #define skip_(test, file, line, ...) KmtSkip(test, file ":" KMT_STRINGIZE(line), __VA_ARGS__)
175
176 BOOLEAN KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0);
177 BOOLEAN KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 3, 4);
178 VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 2, 0);
179 VOID KmtTrace(PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 2, 3);
180 BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments) KMT_FORMAT(ms_printf, 3, 0);
181 BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, ...) KMT_FORMAT(ms_printf, 3, 4);
182 PVOID KmtAllocateGuarded(SIZE_T SizeRequested);
183 VOID KmtFreeGuarded(PVOID Pointer);
184
185 #ifdef KMT_KERNEL_MODE
186 #define ok_irql(irql) ok(KeGetCurrentIrql() == irql, "IRQL is %d, expected %d\n", KeGetCurrentIrql(), irql)
187 #endif /* defined KMT_KERNEL_MODE */
188 #define ok_eq_print(value, expected, spec) ok((value) == (expected), #value " = " spec ", expected " spec "\n", value, expected)
189 #define ok_eq_pointer(value, expected) ok_eq_print(value, expected, "%p")
190 #define ok_eq_int(value, expected) ok_eq_print(value, expected, "%d")
191 #define ok_eq_uint(value, expected) ok_eq_print(value, expected, "%u")
192 #define ok_eq_long(value, expected) ok_eq_print(value, expected, "%ld")
193 #define ok_eq_ulong(value, expected) ok_eq_print(value, expected, "%lu")
194 #define ok_eq_longlong(value, expected) ok_eq_print(value, expected, "%I64d")
195 #define ok_eq_ulonglong(value, expected) ok_eq_print(value, expected, "%I64u")
196 #define ok_eq_char(value, expected) ok_eq_print(value, expected, "%c")
197 #define ok_eq_wchar(value, expected) ok_eq_print(value, expected, "%C")
198 #ifndef _WIN64
199 #define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%lu")
200 #define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%ld")
201 #define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%lu")
202 #elif defined _WIN64
203 #define ok_eq_size(value, expected) ok_eq_print(value, (SIZE_T)(expected), "%I64u")
204 #define ok_eq_longptr(value, expected) ok_eq_print(value, (LONG_PTR)(expected), "%I64d")
205 #define ok_eq_ulongptr(value, expected) ok_eq_print(value, (ULONG_PTR)(expected), "%I64u")
206 #endif /* defined _WIN64 */
207 #define ok_eq_hex(value, expected) ok_eq_print(value, expected, "0x%08lx")
208 #define ok_bool_true(value, desc) ok((value) == TRUE, desc " FALSE, expected TRUE\n")
209 #define ok_bool_false(value, desc) ok((value) == FALSE, desc " TRUE, expected FALSE\n")
210 #define ok_eq_bool(value, expected) ok((value) == (expected), #value " = %s, expected %s\n", \
211 (value) ? "TRUE" : "FALSE", \
212 (expected) ? "TRUE" : "FALSE")
213 #define ok_eq_str(value, expected) ok(!strcmp(value, expected), #value " = \"%s\", expected \"%s\"\n", value, expected)
214 #define ok_eq_wstr(value, expected) ok(!wcscmp(value, expected), #value " = \"%ls\", expected \"%ls\"\n", value, expected)
215 #define ok_eq_tag(value, expected) ok_eq_print(value, expected, "0x%08lx")
216
217 #define KMT_MAKE_CODE(ControlCode) CTL_CODE(FILE_DEVICE_UNKNOWN, \
218 0xC00 + (ControlCode), \
219 METHOD_BUFFERED, \
220 FILE_ANY_ACCESS)
221
222 #define MICROSECOND 10
223 #define MILLISECOND (1000 * MICROSECOND)
224 #define SECOND (1000 * MILLISECOND)
225
226 /* See apitests/include/apitest.h */
227 #define KmtInvalidPointer ((PVOID)0x5555555555555555ULL)
228
229 #define KmtStartSeh() \
230 { \
231 NTSTATUS ExceptionStatus = STATUS_SUCCESS; \
232 _SEH2_TRY \
233 {
234
235 #define KmtEndSeh(ExpectedStatus) \
236 } \
237 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER) \
238 { \
239 ExceptionStatus = _SEH2_GetExceptionCode(); \
240 } \
241 _SEH2_END; \
242 ok_eq_hex(ExceptionStatus, (ExpectedStatus)); \
243 }
244
245 #if defined KMT_DEFINE_TEST_FUNCTIONS
246
247 #if defined KMT_KERNEL_MODE
248 #include "kmt_test_kernel.h"
249 #elif defined KMT_USER_MODE
250 #include "kmt_test_user.h"
251 #endif /* defined KMT_USER_MODE */
252
253 PKMT_RESULTBUFFER ResultBuffer = NULL;
254
255 static VOID KmtAddToLogBuffer(PKMT_RESULTBUFFER Buffer, PCSTR String, SIZE_T Length)
256 {
257 LONG OldLength;
258 LONG NewLength;
259
260 if (!Buffer)
261 return;
262
263 do
264 {
265 OldLength = Buffer->LogBufferLength;
266 NewLength = OldLength + (ULONG)Length;
267 if (NewLength > Buffer->LogBufferMaxLength)
268 return;
269 } while (InterlockedCompareExchange(&Buffer->LogBufferLength, NewLength, OldLength) != OldLength);
270
271 memcpy(&Buffer->LogBuffer[OldLength], String, Length);
272 }
273
274 KMT_FORMAT(ms_printf, 5, 0)
275 static SIZE_T KmtXVSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR FileAndLine, PCSTR Prepend, PCSTR Format, va_list Arguments)
276 {
277 SIZE_T BufferLength = 0;
278 SIZE_T Length;
279
280 if (FileAndLine)
281 {
282 PCSTR Slash;
283 Slash = strrchr(FileAndLine, '\\');
284 if (Slash)
285 FileAndLine = Slash + 1;
286 Slash = strrchr(FileAndLine, '/');
287 if (Slash)
288 FileAndLine = Slash + 1;
289
290 Length = min(BufferMaxLength, strlen(FileAndLine));
291 memcpy(Buffer, FileAndLine, Length);
292 Buffer += Length;
293 BufferLength += Length;
294 BufferMaxLength -= Length;
295 }
296 if (Prepend)
297 {
298 Length = min(BufferMaxLength, strlen(Prepend));
299 memcpy(Buffer, Prepend, Length);
300 Buffer += Length;
301 BufferLength += Length;
302 BufferMaxLength -= Length;
303 }
304 if (Format)
305 {
306 Length = KmtVSNPrintF(Buffer, BufferMaxLength, Format, Arguments);
307 /* vsnprintf can return more than maxLength, we don't want to do that */
308 BufferLength += min(Length, BufferMaxLength);
309 }
310 return BufferLength;
311 }
312
313 KMT_FORMAT(ms_printf, 5, 6)
314 static SIZE_T KmtXSNPrintF(PSTR Buffer, SIZE_T BufferMaxLength, PCSTR FileAndLine, PCSTR Prepend, PCSTR Format, ...)
315 {
316 SIZE_T BufferLength;
317 va_list Arguments;
318 va_start(Arguments, Format);
319 BufferLength = KmtXVSNPrintF(Buffer, BufferMaxLength, FileAndLine, Prepend, Format, Arguments);
320 va_end(Arguments);
321 return BufferLength;
322 }
323
324 VOID KmtFinishTest(PCSTR TestName)
325 {
326 CHAR MessageBuffer[512];
327 SIZE_T MessageLength;
328
329 if (!ResultBuffer)
330 return;
331
332 MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, NULL, NULL,
333 "%s: %ld tests executed (0 marked as todo, %ld failures), %ld skipped.\n",
334 TestName,
335 ResultBuffer->Successes + ResultBuffer->Failures,
336 ResultBuffer->Failures,
337 ResultBuffer->Skipped);
338 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
339 }
340
341 BOOLEAN KmtVOk(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments)
342 {
343 CHAR MessageBuffer[512];
344 SIZE_T MessageLength;
345
346 if (!ResultBuffer)
347 return Condition != 0;
348
349 if (Condition)
350 {
351 InterlockedIncrement(&ResultBuffer->Successes);
352
353 if (0/*KmtReportSuccess*/)
354 {
355 MessageLength = KmtXSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Test succeeded\n", NULL);
356 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
357 }
358 }
359 else
360 {
361 InterlockedIncrement(&ResultBuffer->Failures);
362 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Test failed: ", Format, Arguments);
363 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
364 }
365
366 return Condition != 0;
367 }
368
369 BOOLEAN KmtOk(INT Condition, PCSTR FileAndLine, PCSTR Format, ...)
370 {
371 BOOLEAN Ret;
372 va_list Arguments;
373 va_start(Arguments, Format);
374 Ret = KmtVOk(Condition, FileAndLine, Format, Arguments);
375 va_end(Arguments);
376 return Ret;
377 }
378
379 VOID KmtVTrace(PCSTR FileAndLine, PCSTR Format, va_list Arguments)
380 {
381 CHAR MessageBuffer[512];
382 SIZE_T MessageLength;
383
384 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": ", Format, Arguments);
385 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
386 }
387
388 VOID KmtTrace(PCSTR FileAndLine, PCSTR Format, ...)
389 {
390 va_list Arguments;
391 va_start(Arguments, Format);
392 KmtVTrace(FileAndLine, Format, Arguments);
393 va_end(Arguments);
394 }
395
396 BOOLEAN KmtVSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, va_list Arguments)
397 {
398 CHAR MessageBuffer[512];
399 SIZE_T MessageLength;
400
401 if (!ResultBuffer)
402 return !Condition;
403
404 if (!Condition)
405 {
406 InterlockedIncrement(&ResultBuffer->Skipped);
407 MessageLength = KmtXVSNPrintF(MessageBuffer, sizeof MessageBuffer, FileAndLine, ": Tests skipped: ", Format, Arguments);
408 KmtAddToLogBuffer(ResultBuffer, MessageBuffer, MessageLength);
409 }
410
411 return !Condition;
412 }
413
414 BOOLEAN KmtSkip(INT Condition, PCSTR FileAndLine, PCSTR Format, ...)
415 {
416 BOOLEAN Ret;
417 va_list Arguments;
418 va_start(Arguments, Format);
419 Ret = KmtVSkip(Condition, FileAndLine, Format, Arguments);
420 va_end(Arguments);
421 return Ret;
422 }
423
424 PVOID KmtAllocateGuarded(SIZE_T SizeRequested)
425 {
426 NTSTATUS Status;
427 SIZE_T Size = PAGE_ROUND_UP(SizeRequested + PAGE_SIZE);
428 PVOID VirtualMemory = NULL;
429 PCHAR StartOfBuffer;
430
431 Status = ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory, 0, &Size, MEM_RESERVE, PAGE_NOACCESS);
432
433 if (!NT_SUCCESS(Status))
434 return NULL;
435
436 Size -= PAGE_SIZE;
437 Status = ZwAllocateVirtualMemory(ZwCurrentProcess(), &VirtualMemory, 0, &Size, MEM_COMMIT, PAGE_READWRITE);
438 if (!NT_SUCCESS(Status))
439 {
440 Size = 0;
441 Status = ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
442 ok_eq_hex(Status, STATUS_SUCCESS);
443 return NULL;
444 }
445
446 StartOfBuffer = VirtualMemory;
447 StartOfBuffer += Size - SizeRequested;
448
449 return StartOfBuffer;
450 }
451
452 VOID KmtFreeGuarded(PVOID Pointer)
453 {
454 NTSTATUS Status;
455 PVOID VirtualMemory = (PVOID)PAGE_ROUND_DOWN((SIZE_T)Pointer);
456 SIZE_T Size = 0;
457
458 Status = ZwFreeVirtualMemory(ZwCurrentProcess(), &VirtualMemory, &Size, MEM_RELEASE);
459 ok_eq_hex(Status, STATUS_SUCCESS);
460 }
461
462 #endif /* defined KMT_DEFINE_TEST_FUNCTIONS */
463
464 #endif /* !defined _KMTEST_TEST_H_ */