aboutsummaryrefslogtreecommitdiffstats
path: root/common.h
blob: f1b5f1292fce536f314f3aca4e2025c1ddc0ca7c (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* See LICENSE file for copyright and license details. */
#include "libexec.h"

#include <sys/epoll.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


#if defined(__linux__)
# define STRUCT_OPEN_HOW struct open_how
#else
# define STRUCT_OPEN_HOW void
int libexec_openat2(struct libexec_command *cmd, int fd, int dirfd, const char *file, void *how, size_t size);
#endif


#ifdef TEST
# define TESTED_ELSEWHERE LIBEXEC_CONST__ int main(void) { return 0; }

enum assert_type {
	ASSERT_ENUM,
	ASSERT_UINT,
	ASSERT_INT,
	ASSERT_NULL,
	ASSERT_PTR,
	ASSERT_STR,
	ASSERT_STR_REV
};

enum assert_how {
	ASSERT_EQ,
	ASSERT_NE,
	ASSERT_LT,
	ASSERT_LE,
	ASSERT_GT,
	ASSERT_GE,
	ASSERT_CONTAINS_ALL,
	ASSERT_CONTAINS_ANY,
	ASSERT_CONTAINS_NOT_ALL,
	ASSERT_CONTAINS_NOT_ANY,
	ASSERT_IS_IN,
	ASSERT_NOT_IN,
	ASSERT_NOT_LT,
	ASSERT_NOT_LE,
	ASSERT_NOT_GT,
	ASSERT_NOT_GE
};

void test_assert(const char *file, int line, enum assert_type type, enum assert_how how,
                 const char *have_string, const char *expect_string, const void *have, const void *expect);

#define ASSERT_EQ_ENUM(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_ENUM, ASSERT_EQ, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_NE_ENUM(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_ENUM, ASSERT_NE, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_LT_ENUM(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_ENUM, ASSERT_LT, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_GT_ENUM(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_ENUM, ASSERT_GT, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_LE_ENUM(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_ENUM, ASSERT_LE, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_GE_ENUM(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_ENUM, ASSERT_GE, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_CONTAINS_ALL_OF_ENUM(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_ENUM, ASSERT_CONTAINS_ALL, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_CONTAINS_SOME_OF_ENUM(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_ENUM, ASSERT_CONTAINS_ANY, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_CONTAINS_NONE_OF_ENUM(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_ENUM, ASSERT_CONTAINS_NOT_ANY, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_CONTAINS_NOT_ALL_OF_ENUM(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_ENUM, ASSERT_CONTAINS_NOT_ALL, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_IS_IN_ENUM(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_ENUM, ASSERT_IS_IN, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_NOT_IN_ENUM(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_ENUM, ASSERT_NOT_IN, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_EQ_UINT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_UINT, ASSERT_EQ, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_NE_UINT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_UINT, ASSERT_NE, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_LT_UINT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_UINT, ASSERT_LT, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_GT_UINT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_UINT, ASSERT_GT, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_LE_UINT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_UINT, ASSERT_LE, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_GE_UINT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_UINT, ASSERT_GE, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_CONTAINS_ALL_OF_UINT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_UINT, ASSERT_CONTAINS_ALL, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_CONTAINS_SOME_OF_UINT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_UINT, ASSERT_CONTAINS_ANY, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_CONTAINS_NONE_OF_UINT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_UINT, ASSERT_CONTAINS_NOT_ANY, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_CONTAINS_NOT_ALL_OF_UINT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_UINT, ASSERT_CONTAINS_NOT_ALL, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_IS_IN_UINT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_UINT, ASSERT_IS_IN, #HAVE, #EXPECT,\
	            &(uintmax_t){(uintmax_t)(HAVE)}, &(uintmax_t){(uintmax_t)(EXPECT)})

#define ASSERT_EQ_INT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_INT, ASSERT_EQ, #HAVE, #EXPECT,\
	            &(intmax_t){(intmax_t)(HAVE)}, &(intmax_t){(intmax_t)(EXPECT)})

#define ASSERT_NE_INT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_INT, ASSERT_NE, #HAVE, #EXPECT,\
	            &(intmax_t){(intmax_t)(HAVE)}, &(intmax_t){(intmax_t)(EXPECT)})

#define ASSERT_LT_INT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_INT, ASSERT_LT, #HAVE, #EXPECT,\
	            &(intmax_t){(intmax_t)(HAVE)}, &(intmax_t){(intmax_t)(EXPECT)})

#define ASSERT_GT_INT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_INT, ASSERT_GT, #HAVE, #EXPECT,\
	            &(intmax_t){(intmax_t)(HAVE)}, &(intmax_t){(intmax_t)(EXPECT)})

#define ASSERT_LE_INT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_INT, ASSERT_LE, #HAVE, #EXPECT,\
	            &(intmax_t){(intmax_t)(HAVE)}, &(intmax_t){(intmax_t)(EXPECT)})

#define ASSERT_GE_INT(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_INT, ASSERT_GE, #HAVE, #EXPECT,\
	            &(intmax_t){(intmax_t)(HAVE)}, &(intmax_t){(intmax_t)(EXPECT)})

#define ASSERT_EQ_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_EQ, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_NE_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_NE, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_STARTS_WITH_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_GE, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_EXTENDS_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_GT, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_TRUNCATES_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_LT, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_BEGINNING_BOUNDED_TO_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_LE, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_ENDS_WITH_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR_REV, ASSERT_GE, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_EXTENDS_END_OF_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR_REV, ASSERT_GT, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_TRUNCATES_END_OF_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR_REV, ASSERT_LT, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_ENDING_BOUNDED_TO_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR_REV, ASSERT_LE, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_DOES_NOT_STARTS_WITH_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_NOT_GE, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_DOES_NOT_EXTENDS_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_NOT_GT, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_DOES_NOT_TRUNCATES_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_NOT_LT, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_BEGINNING_NOT_BOUNDED_TO_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_NOT_LE, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_DOES_NOT_ENDS_WITH_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR_REV, ASSERT_NOT_GE, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_DOES_NOT_EXTENDS_END_OF_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR_REV, ASSERT_NOT_GT, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_DOES_NOT_TRUNCATES_END_OF_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR_REV, ASSERT_NOT_LT, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_ENDING_NOT_BOUNDED_TO_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR_REV, ASSERT_NOT_LE, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_CONTAINS_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_CONTAINS_ALL, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_DOES_NOT_CONTAIN_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_CONTAINS_NOT_ALL, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_IS_IN_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_IS_IN, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_NOT_IN_STR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_STR, ASSERT_NOT_IN, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_IS_PTR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_PTR, ASSERT_EQ, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_NOT_PTR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_PTR, ASSERT_NE, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_BEFORE_PTR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_PTR, ASSERT_LT, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_AFTER_PTR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_PTR, ASSERT_GT, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_BEFORE_OR_IS_PTR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_PTR, ASSERT_LE, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_AFTER_OR_IS_PTR(HAVE, EXPECT)\
	test_assert(__FILE__, __LINE__, ASSERT_PTR, ASSERT_GE, #HAVE, #EXPECT, (HAVE), (EXPECT))

#define ASSERT_IS_NULL(HAVE)\
	test_assert(__FILE__, __LINE__, ASSERT_NULL, ASSERT_EQ, #HAVE, "NULL", (HAVE), NULL)

#define ASSERT_NOT_NULL(HAVE)\
	test_assert(__FILE__, __LINE__, ASSERT_NULL, ASSERT_NE, #HAVE, "NULL", (HAVE), NULL)

#define ASSERT_IS_TRUE(HAVE)\
	ASSERT_NE_INT(HAVE, 0)

#define ASSERT_IS_FALSE(HAVE)\
	ASSERT_EQ_INT(HAVE, 0)

#define ASSERT_ZERO(HAVE)\
	ASSERT_EQ_INT(HAVE, 0)

#endif