-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinytest.c
More file actions
165 lines (141 loc) · 4.38 KB
/
tinytest.c
File metadata and controls
165 lines (141 loc) · 4.38 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <dlfcn.h>
#include <fcntl.h>
#include <gelf.h>
#include <libelf.h>
#include <memory.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#ifndef TINYTEST_CNF__NO_COLOR
#define ANSI_STYLE_BOLD "\033[1m"
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_RESET "\x1b[0m"
#else
#define ANSI_STYLE_BOLD ""
#define ANSI_COLOR_RED ""
#define ANSI_COLOR_GREEN ""
#define ANSI_COLOR_RESET ""
#endif
typedef void (*tinytest_test_f)(void);
jmp_buf test_jumpback_buffer;
void test_signal_handler(int signum) { longjmp(test_jumpback_buffer, signum); }
typedef struct tinytest_test {
const char *test_name;
const char *func_name;
tinytest_test_f test_f;
} tinytest_test;
int main(int argc, char **argv) {
size_t test_counter = 0;
size_t test_capacity = 16;
tinytest_test *tests =
(tinytest_test *)malloc(sizeof(tinytest_test) * test_capacity);
elf_version(EV_CURRENT);
int fd = open(argv[1], O_RDONLY);
if (!fd)
exit(66);
Elf *elf = elf_begin(fd, ELF_C_READ, NULL);
Elf_Scn *section = NULL;
GElf_Shdr shdr;
while ((section = elf_nextscn(elf, section)) != NULL) {
gelf_getshdr(section, &shdr);
if (shdr.sh_type == SHT_SYMTAB)
// Symbol table located
break;
}
Elf_Data *data;
data = elf_getdata(section, NULL);
if (data == NULL)
exit(65);
int count = shdr.sh_size / shdr.sh_entsize;
int ii;
for (ii = 0; ii < count; ++ii) {
GElf_Sym symbol;
gelf_getsym(data, ii, &symbol);
const char *prefix = "tinytest_test__";
int prefix_len = strlen(prefix);
const char *funcname = elf_strptr(elf, shdr.sh_link, symbol.st_name);
if (strlen(funcname) >= strlen(prefix))
if (memcmp(funcname, prefix, strlen(prefix)) == 0) {
if (test_counter >= test_capacity) {
test_capacity *= 2;
tests =
(tinytest_test *)realloc(tests, sizeof(tests) * test_capacity);
}
int test_name_len = strlen(funcname + prefix_len) - 2;
char *test_name = (char *)malloc(sizeof(char) * test_name_len);
strncpy(test_name, funcname + prefix_len, test_name_len);
tests[test_counter].test_name = test_name;
char *func_name = (char *)malloc(sizeof(char) * strlen(funcname));
strcpy(func_name, funcname);
tests[test_counter].func_name = func_name;
++test_counter;
}
}
if (test_counter == 0)
return 0;
void *dl = dlopen(argv[1], RTLD_NOW | RTLD_GLOBAL);
if (!dl)
exit(66);
for (int i = 0; i < test_counter; i++)
tests[i].test_f = (tinytest_test_f)dlsym(dl, tests[i].func_name);
int signals[5] = {SIGILL, SIGTRAP, SIGABRT, SIGFPE, SIGSEGV};
for (int i = 0; i < sizeof signals / sizeof(int); i++) {
// TODO: replace this with sigaction
signal(signals[i], test_signal_handler);
}
int tests_total = 0;
int tests_passed = 0;
for (int i = 0; i < test_counter; i++) {
tests_total++;
int signal_recved = 0;
if ((signal_recved = setjmp(test_jumpback_buffer)) == 0) {
printf("%.3d: began executing %s\n", i + 1, tests[i].test_name);
tests[i].test_f();
printf(" %s " ANSI_COLOR_GREEN "passed" ANSI_COLOR_RESET "...\n",
tests[i].test_name);
tests_passed++;
} else {
switch (signal_recved) {
case SIGILL:
printf(" caught illegal instruction\n");
break;
case SIGTRAP:
printf(" assertion or breakpoint raised\n");
break;
case SIGABRT:
printf(" abort signal was triggered\n");
break;
case SIGFPE:
printf(" division by zero\n");
break;
case SIGSEGV:
printf(" segmentation violation\n");
}
printf(" %s " ANSI_COLOR_RED "failed" ANSI_COLOR_RESET "...\n",
tests[i].test_name);
}
}
float passing_rate = (float)(tests_passed) / (float)(tests_total);
printf("Results: " ANSI_STYLE_BOLD);
if (tests_passed == tests_total)
printf(ANSI_COLOR_GREEN);
else
printf(ANSI_COLOR_RED);
printf("%d/%d", tests_passed, tests_total);
printf(ANSI_COLOR_RESET);
printf(". Passing rate: " ANSI_STYLE_BOLD);
if (tests_passed == tests_total)
printf(ANSI_COLOR_GREEN);
else
printf(ANSI_COLOR_RED);
printf("%.3f", passing_rate);
printf(ANSI_COLOR_RESET "\n");
elf_end(elf);
close(fd);
return 0;
}