aboutsummaryrefslogtreecommitdiff
path: root/source/tests/tests.h
blob: 84135d8287d715dd3d4ae998f6abf8ae7e9e95f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#ifndef TESTS_H_INCLUDED
#define TESTS_H_INCLUDED

#ifndef _RUN_TESTS
#error "_RUN_TESTS has to be defined"
#endif
#include "compat.h"

#include <stdio.h>
#include <libgen.h>


#define MYASSERT_LOGFILE logfile
extern FILE* MYASSERT_LOGFILE;

extern unsigned test_count;
extern unsigned test_faild;

#define ERRPRINT(file, fmt, ...) { \
                        char* __erret_str = strdup(__FILE__); \
                            if (__erret_str != NULL) { \
                                char* __erret_base = basename(__erret_str); \
                                fprintf(file, "%s.%d: " fmt "\n", (__erret_base != NULL ? __erret_base : "NULL"), __LINE__, ##__VA_ARGS__); \
                                free(__erret_str); \
                            } \
                        }
#define ERRPRINT_STDERR(fmt, ...) \
                        ERRPRINT(stderr, fmt, __VA_ARGS__)
#define ERRPRINT_LOGFILE(fmt, ...) \
                        if (MYASSERT_LOGFILE != NULL) { ERRPRINT(MYASSERT_LOGFILE, fmt, __VA_ARGS__); fflush(MYASSERT_LOGFILE); }
#define ERRPRINT_BOTH(fmt, ...) \
                        ERRPRINT(stderr, fmt, __VA_ARGS__) \
                        ERRPRINT_LOGFILE(fmt, __VA_ARGS__)

#define MYASSERT_RETVAL retval
#define MYASSERT_RETVAL_DEF int MYASSERT_RETVAL = 0;
#define MYASSERT_LOG "tests.log"
#define MYASSERT_LOGDEF FILE* MYASSERT_LOGFILE = NULL
#define MYASSERT_INIT \
                        MYASSERT_RETVAL_DEF; \
                        if ((MYASSERT_LOGFILE = fopen(MYASSERT_LOG, "w")) == NULL) { \
                            ERRPRINT_BOTH("Could not open \"%s\" for writing.\n", MYASSERT_LOG); \
                        } else ERRPRINT_BOTH("Logfile-Init: %s", MYASSERT_LOG);

#define MYASSERT_RETURN \
                        fclose(MYASSERT_LOGFILE); \
                        return MYASSERT_RETVAL;

#define MYASSERT(expr) { \
                        ERRPRINT_LOGFILE("MYASSERT: %s", #expr); \
                        if ((expr) != TRUE) { \
                            fprintf_setw(stderr, 50, "%s", #expr); \
                            ERRPRINT_BOTH("FAILED with ERROR: %d", (int)GetLastError()); \
                            MYASSERT_RETVAL++; \
                        } else { \
                            fprintf_setw(stderr, 50, "%s", #expr); \
                            fprintf(stderr, "%s\n", "SUCCEEDED"); \
                        } \
                       }

#define MYASSERT_SILENT(expr) { \
                        ERRPRINT_LOGFILE("MYASSERT_SILENT: %s", #expr); \
                        int outfd = dup(fileno(stdout)); \
                        int stdfd = fileno(stdout); \
                        close(stdfd); \
                        dup2(null_dev, stdfd); \
                        BOOL ret = (expr); \
                        close(stdfd); \
                        dup2(outfd, stdfd); \
                        close(outfd); \
                        if (ret != TRUE) { \
                            fprintf_setw(stderr, 50, "%s", #expr); \
                            ERRPRINT_BOTH("FAILED with ERROR: %d", (int)GetLastError()); \
                        } else { \
                            fprintf_setw(stderr, 50, "%s", #expr); \
                            fprintf(stderr, "%s\n", "SUCCEEDED"); \
                        } \
                       }


#define _ERRETCP(expr)      { test_count++; if ( (expr) != TRUE ) { test_faild++; ERRPRINT_BOTH("(%s) != TRUE", #expr); return FALSE; } }
#define ERRETCP(expr)       { ERRPRINT_LOGFILE("ERRETCP: %s", #expr); _ERRETCP(expr); }
#define ERRETCP_NOLOG(expr) { _ERRETCP(expr); }

#define _ERRETCPDW(expr, val)       { test_count++; if ( (expr) != TRUE ) { test_faild++; ERRPRINT_BOTH("(%s) != TRUE , %s = %lu (0x%lX)", #expr, #val, (unsigned long)val, (unsigned long)val); return FALSE; } }
#define ERRETCPDW(expr, val)        { ERRPRINT_LOGFILE("ERRETCPDW: %s , %s = %lu (0x%lX) (", #expr, #val, (unsigned long)val, (unsigned long)val); _ERRETCPDW(expr, val); }
#define ERRETCPDW_NOLOG(expr, val)  { _ERRETCPDW(expr, val); }

#define _ERRETCPLD(expr, val)       { test_count++; if ( (expr) != TRUE ) { test_faild++; ERRPRINT_BOTH("(%s) != TRUE , %s = %ld (0x%lX)", #expr, #val, (long)val, (unsigned long)val); return FALSE; }   }
#define ERRETCPLD(expr, val)        { ERRPRINT_LOGFILE("ERRETCPL: %s , %s = %ld (0x%lX) (", #expr, #val, (long)val, (unsigned long)val); _ERRETCPDW(expr, val); }
#define ERRETCPLD_NOLOG(expr, val)  { _ERRETCPDW(expr, val); }


extern int null_dev;

char* test_randstring(size_t length);

char* test_randhexstring(size_t length);

int fprintf_setw(FILE *stream, size_t s_maxlen, const char *format, ...);

BOOL test_memmove(void);

BOOL test_realloc(void);

BOOL test_memalign(void);

BOOL test_heap(void);

BOOL test_mem(void);

BOOL test_stdio(void);

BOOL test_math(void);

BOOL test_utils(void);

BOOL test_distorm(void);

BOOL test_aes(void);

BOOL test_crypt(void);

BOOL test_pe(char* filename);

BOOL test_http(void);

#endif // TESTS_H_INCLUDED