fixed some signed/unsigned comparison warnings with -Wsign-compare
[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
12 extern void SetupOnce();
13
14 #define _SetupOnce() \
15 void SetupOnce()
16
17 /* Valid values for Command parameter of TestRoutine */
18 #define TESTCMD_RUN 0 /* Buffer contains information about what failed */
19 #define TESTCMD_TESTTYPE 1 /* Buffer contains type of test */
20 #define TESTCMD_TESTNAME 2 /* Buffer contains description of test */
21 #define TESTCMD_TIMEOUT 3 /* Buffer contains timeout for test (DWORD, default is 5000 ms) */
22
23 /* Test types */
24 #define TT_NORMAL 0
25 #define TT_PERFORMANCE 1
26
27 /* Valid values for return values of TestRoutine */
28 #define TS_TIMEDOUT ((DWORD)-2)
29 #define TS_EXCEPTION ((DWORD)-1)
30 #define TS_OK 0
31 #define TS_FAILED 1
32
33 extern int _Result;
34 extern char *_Buffer;
35
36 /* Macros to simplify tests */
37 #define _DispatcherTypeTimeout(FunctionName, TestName, TestType, TimeOut) \
38 void \
39 FunctionName(int Command) \
40 { \
41 switch (Command) \
42 { \
43 case TESTCMD_RUN: \
44 RunTest(); \
45 break; \
46 case TESTCMD_TESTTYPE: \
47 *(PDWORD)_Buffer = (DWORD)TestType; \
48 break; \
49 case TESTCMD_TESTNAME: \
50 strcpy(_Buffer, TestName); \
51 break; \
52 case TESTCMD_TIMEOUT: \
53 *(PDWORD)_Buffer = (DWORD)TimeOut; \
54 break; \
55 default: \
56 _Result = TS_FAILED; \
57 break; \
58 } \
59 }
60
61 #define _DispatcherTimeout(FunctionName, TestName, TimeOut) \
62 _DispatcherTypeTimeout(FunctionName, TestName, TT_NORMAL, TimeOut)
63
64 #define _DispatcherType(FunctionName, TestName, TestType) \
65 _DispatcherTypeTimeout(FunctionName, TestName, TestType, 5000)
66
67 #define _Dispatcher(FunctionName, TestName) \
68 _DispatcherTimeout(FunctionName, TestName, 5000)
69
70 static inline void
71 AppendAssertion(char *message)
72 {
73 if (strlen(_Buffer) != 0)
74 strcat(_Buffer, "\n");
75 strcat(_Buffer, message);
76 _Result = TS_FAILED;
77 }
78
79 #define _AssertTrue(_Condition) \
80 { \
81 if (!(_Condition)) \
82 { \
83 char _message[100]; \
84 sprintf(_message, "Condition was not true at %s:%d", \
85 __FILE__, __LINE__); \
86 AppendAssertion(_message); \
87 } \
88 }
89
90 #define _AssertFalse(_Condition) \
91 { \
92 if (_Condition) \
93 { \
94 char _message[100]; \
95 sprintf(_message, "Condition was not false at %s:%d", \
96 __FILE__, __LINE__); \
97 AppendAssertion(_message); \
98 } \
99 }
100
101 #define _AssertEqualValue(_Expected, _Actual) \
102 { \
103 ULONG __Expected = (ULONG) (_Expected); \
104 ULONG __Actual = (ULONG) (_Actual); \
105 if ((__Expected) != (__Actual)) \
106 { \
107 char _message[100]; \
108 sprintf(_message, "Expected %ld/0x%.08lx was %ld/0x%.08lx at %s:%d", \
109 (__Expected), (__Expected), (__Actual), (__Actual), __FILE__, __LINE__); \
110 AppendAssertion(_message); \
111 } \
112 }
113
114 #define _AssertEqualWideString(_Expected, _Actual) \
115 { \
116 LPWSTR __Expected = (LPWSTR) (_Expected); \
117 LPWSTR __Actual = (LPWSTR) (_Actual); \
118 if (wcscmp((__Expected), (__Actual)) != 0) \
119 { \
120 char _message[100]; \
121 sprintf(_message, "Expected %S was %S at %s:%d", \
122 (__Expected), (__Actual), __FILE__, __LINE__); \
123 AppendAssertion(_message); \
124 } \
125 }
126
127 #define _AssertNotEqualValue(_Expected, _Actual) \
128 { \
129 ULONG __Expected = (ULONG) (_Expected); \
130 ULONG __Actual = (ULONG) (_Actual); \
131 if ((__Expected) == (__Actual)) \
132 { \
133 char _message[100]; \
134 sprintf(_message, "Actual value expected to be different from %ld/0x%.08lx at %s:%d", \
135 (__Expected), (__Expected), __FILE__, __LINE__); \
136 AppendAssertion(_message); \
137 } \
138 }
139
140
141 /*
142 * Test routine prototype
143 * Command - The command to process
144 */
145 typedef void (*TestRoutine)(int Command);
146
147 /*
148 * Test output routine prototype
149 * Buffer - Address of buffer with text to output
150 */
151 typedef void (*TestOutputRoutine)(char *Buffer);
152
153 /*
154 * Test driver entry routine.
155 * OutputRoutine - Output routine.
156 * TestName - If NULL all tests are run. If non-NULL specifies the test to be run
157 */
158 typedef void STDCALL (*TestDriverMain)(TestOutputRoutine OutputRoutine, char *TestName);
159
160 typedef struct __TEST
161 {
162 LIST_ENTRY ListEntry;
163 TestRoutine Routine;
164 } _TEST, *_PTEST;
165
166 extern VOID InitializeTests();
167 extern VOID RegisterTests();
168 extern VOID PerformTests(TestOutputRoutine OutputRoutine, LPSTR TestName);
169
170
171 typedef struct __API_DESCRIPTION
172 {
173 PCHAR FileName;
174 PCHAR FunctionName;
175 PCHAR ForwardedFunctionName;
176 PVOID FunctionAddress;
177 PVOID MockFunctionAddress;
178 } _API_DESCRIPTION, *_PAPI_DESCRIPTION;
179
180 extern _API_DESCRIPTION ExternalDependencies[];
181 extern ULONG MaxExternalDependency;
182
183 HANDLE STDCALL
184 _GetModuleHandleA(LPCSTR lpModuleName);
185
186 PVOID STDCALL
187 _GetProcAddress(HANDLE hModule,
188 LPCSTR lpProcName);
189
190 HANDLE STDCALL
191 _LoadLibraryA(LPCSTR lpLibFileName);
192
193 VOID STDCALL
194 _ExitProcess(UINT uExitCode);
195
196 HANDLE STDCALL
197 _CreateThread(LPSECURITY_ATTRIBUTES lpThreadAttributes, DWORD dwStackSize,
198 LPTHREAD_START_ROUTINE lpStartAddress, LPVOID lpParameter,
199 DWORD dwCreationFlags, LPDWORD lpThreadId);
200
201 WINBOOL STDCALL
202 _TerminateThread(HANDLE hThread, DWORD dwExitCode);
203
204 DWORD STDCALL
205 _WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds);
206
207 DWORD STDCALL
208 _GetLastError();
209
210 VOID STDCALL
211 _CloseHandle(HANDLE handle);
212
213 BOOL STDCALL
214 _GetThreadTimes(HANDLE hThread, LPFILETIME lpCreationTime,
215 LPFILETIME lpExitTime, LPFILETIME lpKernelTime,
216 LPFILETIME lpUserTime);
217
218 BOOL STDCALL
219 _SetPriorityClass(HANDLE hProcess, DWORD dwPriorityClass);
220
221 BOOL STDCALL
222 _SetThreadPriority(HANDLE hThread, int nPriority);
223
224 HANDLE STDCALL
225 _GetCurrentProcess();
226
227 HANDLE STDCALL
228 _GetCurrentThread();
229
230 BOOL STDCALL
231 _GetThreadContext(HANDLE hThread, LPCONTEXT lpContext);
232
233 DWORD STDCALL
234 _SuspendThread(HANDLE hThread);
235
236 DWORD STDCALL
237 _ResumeThread(HANDLE hThread);
238
239 VOID STDCALL
240 _Sleep(DWORD dwMilliseconds);
241
242
243 static inline PCHAR
244 FrameworkGetExportedFunctionNameInternal(_PAPI_DESCRIPTION ApiDescription)
245 {
246 if (ApiDescription->ForwardedFunctionName != NULL)
247 {
248 return ApiDescription->ForwardedFunctionName;
249 }
250 else
251 {
252 return ApiDescription->FunctionName;
253 }
254 }
255
256 static inline PVOID
257 FrameworkGetFunction(_PAPI_DESCRIPTION ApiDescription)
258 {
259 HANDLE hModule;
260 PVOID function = NULL;
261 PCHAR exportedFunctionName;
262
263 exportedFunctionName = FrameworkGetExportedFunctionNameInternal(ApiDescription);
264
265 hModule = _GetModuleHandleA(ApiDescription->FileName);
266 if (hModule != NULL)
267 {
268 function = _GetProcAddress(hModule, exportedFunctionName);
269 }
270 else
271 {
272 hModule = _LoadLibraryA(ApiDescription->FileName);
273 if (hModule != NULL)
274 {
275 function = _GetProcAddress(hModule, exportedFunctionName);
276 //FreeLibrary(hModule);
277 }
278 }
279 return function;
280 }
281
282 static inline PVOID
283 FrameworkGetHookInternal(ULONG index)
284 {
285 PVOID address;
286 PCHAR exportedFunctionName;
287
288 if (index > MaxExternalDependency)
289 return NULL;
290
291 if (ExternalDependencies[index].MockFunctionAddress != NULL)
292 return ExternalDependencies[index].MockFunctionAddress;
293
294 if (ExternalDependencies[index].FunctionAddress != NULL)
295 return ExternalDependencies[index].FunctionAddress;
296
297 exportedFunctionName = FrameworkGetExportedFunctionNameInternal(&ExternalDependencies[index]);
298
299 printf("Calling function '%s' in DLL '%s'.\n",
300 exportedFunctionName,
301 ExternalDependencies[index].FileName);
302
303 address = FrameworkGetFunction(&ExternalDependencies[index]);
304
305 if (address == NULL)
306 {
307 printf("Function '%s' not found in DLL '%s'.\n",
308 exportedFunctionName,
309 ExternalDependencies[index].FileName);
310 }
311 ExternalDependencies[index].FunctionAddress = address;
312
313 return address;
314 }
315
316
317 static inline VOID
318 _SetHook(PCHAR name,
319 PVOID address)
320 {
321 _PAPI_DESCRIPTION api;
322 ULONG index;
323
324 for (index = 0; index <= MaxExternalDependency; index++)
325 {
326 api = &ExternalDependencies[index];
327 if (strcmp(api->FunctionName, name) == 0)
328 {
329 api->MockFunctionAddress = address;
330 return;
331 }
332 }
333 }
334
335 typedef struct __HOOK
336 {
337 PCHAR FunctionName;
338 PVOID FunctionAddress;
339 } _HOOK, *_PHOOK;
340
341 static inline VOID
342 _SetHooks(_PHOOK hookTable)
343 {
344 _PHOOK hook;
345
346 hook = &hookTable[0];
347 while (hook->FunctionName != NULL)
348 {
349 _SetHook(hook->FunctionName,
350 hook->FunctionAddress);
351 hook++;
352 }
353 }
354
355 static inline VOID
356 _UnsetHooks(_PHOOK hookTable)
357 {
358 _PHOOK hook;
359
360 hook = &hookTable[0];
361 while (hook->FunctionName != NULL)
362 {
363 _SetHook(hook->FunctionName,
364 NULL);
365 hook++;
366 }
367 }
368
369 static inline VOID
370 _UnsetAllHooks()
371 {
372 _PAPI_DESCRIPTION api;
373 ULONG index;
374
375 for (index = 0; index <= MaxExternalDependency; index++)
376 {
377 api = &ExternalDependencies[index];
378 api->MockFunctionAddress = NULL;
379 }
380 }