-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-unit.c
More file actions
161 lines (130 loc) · 2.69 KB
/
test-unit.c
File metadata and controls
161 lines (130 loc) · 2.69 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
#include <stdarg.h>
#include <sysexits.h>
#include <fd.h>
#include <util.h>
#include <unit.c>
static int pipefd[2];
static volatile size_t test_num=1;
static void print_test_error(char *err);
static void print_test_failure(char *fail);
static void print_test_message(char *msg);
static void test_args(void);
static void test_signals(void);
static void test_print(void);
void
print_test_error(char *err)
{
dprintf(2, "error\n\t%s", err);
}
void
print_test_failure(char *fail)
{
dprintf(2, "failed\n\t%s", fail);
}
void
print_test_message(char *msg)
{
dprintf(2, "%2zu| %s...", test_num++, msg);
}
void
test_args(void)
{
print_test_message("should parse arguments into configurations");
try(unit_parse_argv(3, argv("", "-n", "3")));
if (unit_opt_test_num != 3) {
print_test_failure(asprintf("expected 3 (instead of %d)"
"from unit_opt_test_num",
unit_opt_test_num));
exit(EX_SOFTWARE);
}
printf("ok\n");
}
void
test_print(void)
{
struct read result[1];
int err;
print_test_message("should print error messages");
unit_opt_error_fd = pipefd[1];
if (!unit_catch()) {
unit_error("hello, world");
}
if (!fd_readable(pipefd[0])) {
print_test_failure("failed to print anything");
return;
}
err = fd_read(result, pipefd[0]);
if (err) {
print_test_error("failed to read from pipe");
exit(EX_OSERR);
}
if (!strncmp(result->buffer, "error\n\thello, world", result->length)) {
char *t = malloc(2 * result->length + 1);
size_t i;
for (i=0; i<result->length; ++i) {
switch (result->buffer[i]) {
case '\n':
t[i++] = '\\';
t[i] = 'n';
continue;
case '\t':
t[i++] = '\\';
t[i] = 't';
continue;
}
t[i] = result->buffer[i];
}
t[i] = 0;
char *s = asprintf("expected \"error\n\thello, world\""
"got \"%s\"", t);
print_test_failure(s);
free(s);
free(t);
return;
}
write_str(2, "ok\n");
}
void
test_signals(void)
{
struct { char *id; int sig; } signals[] = {
{"segfaults", SIGSEGV, },
{"illegal instructions", SIGILL, },
{"bus errors", SIGBUS, },
{"aborts", SIGABRT,},
{"alarms", SIGALRM, },
};
volatile size_t i=0;
int err;
print_test_message("should catch deadly signals");
if (unit_catch()) {
++i;
if (i >= arr_len(signals)) {
write_str(2, "ok\n");
return;
}
}
err = raise(signals[i].sig);
if (err) {
perror("raise failed");
exit(EX_OSERR);
}
}
int
main()
{
int err;
err = pipe(pipefd);
if (err == -1) {
print_test_error("failed to create pipe");
exit(EX_OSERR);
}
err = unit_init();
if (err) {
printf("unit_init failed unexpectedly\n");
exit(EX_SOFTWARE);
}
test_print();
test_signals();
test_args();
}