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
|
#include "safe_tests.h"
#include "moslib/safe.h"
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
TEST_HELPER(malloc_exit) {
safe_malloc(ULLONG_MAX);
}
TEST(test_safe_malloc) {
void *p = safe_malloc(1024);
test_assert(p != NULL, "Failed to malloc 1KiB");
suppress_output(STDERR_FILENO);
test_assert_exit(malloc_exit, EXIT_FAILURE);
unsuppress_outputs();
}
TEST_HELPER(calloc_exit) {
safe_calloc(ULLONG_MAX, ULLONG_MAX);
}
TEST(test_safe_calloc) {
void *p = safe_calloc(16, 16);
test_assert(p != NULL, "Failed to calloc 16 items of 16 bytes");
suppress_output(STDERR_FILENO);
test_assert_exit(calloc_exit, EXIT_FAILURE);
unsuppress_outputs();
}
TEST_HELPER(realloc_exit) {
void *p = NULL;
safe_realloc(p, ULLONG_MAX);
}
TEST(test_safe_realloc) {
void *p = NULL;
p = safe_realloc(p, 1024);
test_assert(p != NULL, "Failed to realloc NULL to 1KiB");
p = safe_realloc(p, 4096);
test_assert(p != NULL, "Failed to realloc 1KiB to 4KiB");
suppress_output(STDERR_FILENO);
test_assert_exit(realloc_exit, EXIT_FAILURE);
unsuppress_outputs();
}
TEST_HELPER(fopen_exit) {
safe_fopen("/non-existing-file-name", "r");
}
TEST(test_safe_fopen) {
FILE *fp = safe_fopen(__FILE__, "r");
test_assert(fp != NULL, "Failed to fopen current file");
suppress_output(STDERR_FILENO);
test_assert_exit(fopen_exit, EXIT_FAILURE);
unsuppress_outputs();
}
TEST(test_safe_tmpfile) {
FILE *fp = safe_tmpfile();
test_assert(fp != NULL, "Failed to create a tmpfile");
}
TEST_HELPER(dup_exit) {
safe_dup(-1);
}
TEST(test_safe_dup) {
int fd = safe_dup(STDOUT_FILENO);
test_assert(fd != -1, "Failed to dup STDOUT_FILENO");
close(fd);
suppress_output(STDERR_FILENO);
test_assert_exit(dup_exit, EXIT_FAILURE);
unsuppress_outputs();
}
TEST_HELPER(dup2_exit) {
safe_dup2(-1, STDOUT_FILENO);
}
TEST(test_safe_dup2) {
int res = dup2(STDOUT_FILENO, STDOUT_FILENO);
test_assert(res != -1, "Failed to dup STDOUT_FILENO 2 itself");
int fd = dup(STDOUT_FILENO);
res = dup2(STDERR_FILENO, STDOUT_FILENO);
test_assert(res != -1, "Failed to dup STDERR_FILENO 2 STDOUT_FILENO");
res = dup2(fd, STDOUT_FILENO);
test_assert(res != -1, "Failed to restore STDOUT_FILENO");
close(fd);
suppress_output(STDERR_FILENO);
test_assert_exit(dup2_exit, EXIT_FAILURE);
unsuppress_outputs();
}
TEST(test_safe_fork) {
pid_t pid = safe_fork();
test_assert(pid != -1, "Failed to create a child process");
if (pid == 0)
exit(EXIT_SUCCESS);
}
TEST(test_safe_pipe) {
int fd[2];
int res = safe_pipe(fd);
test_assert(res != -1, "Failed to create a pipe");
close(fd[0]);
close(fd[1]);
}
TEST(test_safe_mkstemp) {
char f[128];
strcpy(f, ".tmpXXXXXX");
int fd = safe_mkstemp(f);
test_assert(fd != -1, "Failed to create a temporary file");
unlink(f);
close(fd);
}
#define mos_safe_fork() mos_safe_fork_fn(mos_err_info)
#define mos_safe_pipe(fd) mos_safe_pipe_fn(fd, mos_err_info)
#define mos_safe_mkstemp(path) mos_safe_mkstemp_fn(path, mos_err_info)
void safe_tests(Tester *tester) {
TestGroup *group = add_test_group(tester, "moslib/safe.h");
add_test(group, test_safe_malloc);
add_test(group, test_safe_calloc);
add_test(group, test_safe_realloc);
add_test(group, test_safe_fopen);
add_test(group, test_safe_tmpfile);
add_test(group, test_safe_dup);
add_test(group, test_safe_dup2);
add_test(group, test_safe_fork);
add_test(group, test_safe_pipe);
add_test(group, test_safe_mkstemp);
}
|