03620c04b58664e97cbb6d7a299ea31c4daed34a
[reactos.git] / reactos / regtests / shared / regtests.h
1 /*
2 * PROJECT: ReactOS kernel
3 * FILE: regtests/shared/regtests.h
4 * PURPOSE: Regression testing
5 * PROGRAMMER: Casper S. Hornstrup (chorns@users.sourceforge.net)
6 * UPDATE HISTORY:
7 * 06-07-2003 CSH Created
8 */
9 #include <stdio.h>
10 #include <string.h>
11 #include <windows.h>
12
13 /* Valid values for Command parameter of TestRoutine */
14 #define TESTCMD_RUN 0 /* Buffer contains information about what failed */
15 #define TESTCMD_TESTNAME 1 /* Buffer contains description of test */
16
17 /* Valid values for return values of TestRoutine */
18 #define TS_EXCEPTION -1
19 #define TS_OK 0
20 #define TS_FAILED 1
21
22 /* Macros to simplify tests */
23 #define DISPATCHER(FunctionName, TestName) \
24 int \
25 FunctionName(int Command, \
26 char *Buffer) \
27 { \
28 switch (Command) \
29 { \
30 case TESTCMD_RUN: \
31 return RunTest(Buffer); \
32 case TESTCMD_TESTNAME: \
33 strcpy(Buffer, TestName); \
34 return TS_OK; \
35 default: \
36 break; \
37 } \
38 return TS_FAILED; \
39 }
40
41 #define FAIL(ErrorMessage) \
42 sprintf(Buffer, "%s\n", ErrorMessage); \
43 return TS_FAILED;
44
45 #define FAIL_IF_NULL(GivenValue, ErrorMessage) if (GivenValue == NULL) { FAIL(ErrorMessage); }
46 #define FAIL_IF_TRUE(GivenValue, ErrorMessage) if (GivenValue == TRUE) { FAIL(ErrorMessage); }
47 #define FAIL_IF_FALSE(GivenValue, ErrorMessage) if (GivenValue == FALSE) { FAIL(ErrorMessage); }
48 #define FAIL_IF_EQUAL(GivenValue, FailValue, ErrorMessage) if (GivenValue == FailValue) { FAIL(ErrorMessage); }
49 #define FAIL_IF_NOT_EQUAL(GivenValue, FailValue, ErrorMessage) if (GivenValue != FailValue) { FAIL(ErrorMessage); }
50 #define FAIL_IF_LESS_EQUAL(GivenValue, FailValue, ErrorMessage) if (GivenValue <= FailValue) { FAIL(ErrorMessage); }
51 #define FAIL_IF_GREATER_EQUAL(GivenValue, FailValue, ErrorMessage) if (GivenValue >= FailValue) { FAIL(ErrorMessage); }
52
53 /*
54 * Test routine prototype
55 * Command - The command to process
56 * Buffer - Pointer to buffer in which to return context information
57 */
58 typedef int (*TestRoutine)(int Command, char *Buffer);
59
60 /*
61 * Test output routine prototype
62 * Buffer - Address of buffer with text to output
63 */
64 typedef void (*TestOutputRoutine)(char *Buffer);
65
66 /*
67 * Test driver entry routine.
68 * OutputRoutine - Output routine.
69 * TestName - If NULL all tests are run. If non-NULL specifies the test to be run
70 */
71 typedef void STDCALL (*TestDriverMain)(TestOutputRoutine OutputRoutine, char *TestName);
72
73 typedef struct _ROS_TEST
74 {
75 LIST_ENTRY ListEntry;
76 TestRoutine Routine;
77 } ROS_TEST, *PROS_TEST;
78
79 extern LIST_ENTRY AllTests;
80
81 extern VOID InitializeTests();
82 extern VOID RegisterTests();
83 extern VOID PerformTests(TestOutputRoutine OutputRoutine, LPSTR TestName);
84
85 /* Routines provided by the driver */
86 extern PVOID AllocateMemory(ULONG Size);
87 extern VOID FreeMemory(PVOID Base);
88
89
90 typedef struct _API_DESCRIPTION
91 {
92 PCHAR FileName;
93 PCHAR FunctionName;
94 PVOID FunctionAddress;
95 PVOID MockFunctionAddress;
96 } API_DESCRIPTION, *PAPI_DESCRIPTION;
97
98 extern API_DESCRIPTION ExternalDependencies[];
99 extern ULONG MaxExternalDependency;
100
101 static inline PVOID
102 FrameworkGetFunction(PAPI_DESCRIPTION ApiDescription)
103 {
104 HMODULE hModule;
105 PVOID Function;
106
107 hModule = GetModuleHandleA(ApiDescription->FileName);
108 if (hModule != NULL)
109 {
110 Function = GetProcAddress(hModule, ApiDescription->FunctionName);
111 }
112 else
113 {
114 hModule = LoadLibraryA(ApiDescription->FileName);
115 if (hModule != NULL)
116 {
117 Function = GetProcAddress(hModule, ApiDescription->FunctionName);
118 //FreeLibrary(hModule);
119 }
120 }
121 return Function;
122 }
123
124 static inline PVOID STDCALL
125 FrameworkGetHookInternal(ULONG index)
126 {
127 PVOID address;
128
129 if (index > MaxExternalDependency)
130 return NULL;
131
132 if (ExternalDependencies[index].MockFunctionAddress != NULL)
133 return ExternalDependencies[index].MockFunctionAddress;
134
135 if (ExternalDependencies[index].FunctionAddress != NULL)
136 return ExternalDependencies[index].FunctionAddress;
137
138 address = FrameworkGetFunction(&ExternalDependencies[index]);
139 ExternalDependencies[index].FunctionAddress = address;
140
141 return address;
142 }
143
144
145 static inline VOID
146 _SetHook(PCHAR name,
147 PVOID address)
148 {
149 PAPI_DESCRIPTION api;
150 ULONG index;
151
152 for (index = 0; index <= MaxExternalDependency; index++)
153 {
154 api = &ExternalDependencies[index];
155 if (strcmp(api->FunctionName, name) == 0)
156 {
157 api->FunctionAddress = address;
158 return;
159 }
160 }
161 }
162
163 typedef struct _HOOK
164 {
165 PCHAR FunctionName;
166 PVOID FunctionAddress;
167 } HOOK, *PHOOK;
168
169 static inline VOID
170 _SetHooks(PHOOK hookTable)
171 {
172 PHOOK hook;
173
174 hook = &hookTable[0];
175 _SetHook(hook->FunctionName,
176 hook->FunctionAddress);
177 }
178
179 static inline VOID
180 _UnsetHooks(PHOOK hookTable)
181 {
182 PHOOK hook;
183
184 hook = &hookTable[0];
185 _SetHook(hook->FunctionName,
186 NULL);
187 }