]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-util.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / test / test-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <string.h>
5 #include <sys/wait.h>
6 #include <unistd.h>
7
8 #include "def.h"
9 #include "fileio.h"
10 #include "fs-util.h"
11 #include "parse-util.h"
12 #include "process-util.h"
13 #include "raw-clone.h"
14 #include "rm-rf.h"
15 #include "string-util.h"
16 #include "util.h"
17
18 static void test_align_power2(void) {
19 unsigned long i, p2;
20
21 assert_se(ALIGN_POWER2(0) == 0);
22 assert_se(ALIGN_POWER2(1) == 1);
23 assert_se(ALIGN_POWER2(2) == 2);
24 assert_se(ALIGN_POWER2(3) == 4);
25 assert_se(ALIGN_POWER2(12) == 16);
26
27 assert_se(ALIGN_POWER2(ULONG_MAX) == 0);
28 assert_se(ALIGN_POWER2(ULONG_MAX - 1) == 0);
29 assert_se(ALIGN_POWER2(ULONG_MAX - 1024) == 0);
30 assert_se(ALIGN_POWER2(ULONG_MAX / 2) == ULONG_MAX / 2 + 1);
31 assert_se(ALIGN_POWER2(ULONG_MAX + 1) == 0);
32
33 for (i = 1; i < 131071; ++i) {
34 for (p2 = 1; p2 < i; p2 <<= 1)
35 /* empty */ ;
36
37 assert_se(ALIGN_POWER2(i) == p2);
38 }
39
40 for (i = ULONG_MAX - 1024; i < ULONG_MAX; ++i) {
41 for (p2 = 1; p2 && p2 < i; p2 <<= 1)
42 /* empty */ ;
43
44 assert_se(ALIGN_POWER2(i) == p2);
45 }
46 }
47
48 static void test_max(void) {
49 static const struct {
50 int a;
51 int b[CONST_MAX(10, 100)];
52 } val1 = {
53 .a = CONST_MAX(10, 100),
54 };
55 int d = 0;
56 unsigned long x = 12345;
57 unsigned long y = 54321;
58 const char str[] = "a_string_constant";
59 const unsigned long long arr[] = {9999ULL, 10ULL, 0ULL, 3000ULL, 2000ULL, 1000ULL, 100ULL, 9999999ULL};
60 void *p = (void *)str;
61 void *q = (void *)&str[16];
62
63 assert_cc(sizeof(val1.b) == sizeof(int) * 100);
64
65 /* CONST_MAX returns (void) instead of a value if the passed arguments
66 * are not of the same type or not constant expressions. */
67 assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int));
68 assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void));
69
70 assert_se(val1.a == 100);
71 assert_se(MAX(++d, 0) == 1);
72 assert_se(d == 1);
73
74 assert_cc(MAXSIZE(char[3], uint16_t) == 3);
75 assert_cc(MAXSIZE(char[3], uint32_t) == 4);
76 assert_cc(MAXSIZE(char, long) == sizeof(long));
77
78 assert_se(MAX(-5, 5) == 5);
79 assert_se(MAX(5, 5) == 5);
80 assert_se(MAX(MAX(1, MAX(2, MAX(3, 4))), 5) == 5);
81 assert_se(MAX(MAX(1, MAX(2, MAX(3, 2))), 1) == 3);
82 assert_se(MAX(MIN(1, MIN(2, MIN(3, 4))), 5) == 5);
83 assert_se(MAX(MAX(1, MIN(2, MIN(3, 2))), 1) == 2);
84 assert_se(LESS_BY(8, 4) == 4);
85 assert_se(LESS_BY(8, 8) == 0);
86 assert_se(LESS_BY(4, 8) == 0);
87 assert_se(LESS_BY(16, LESS_BY(8, 4)) == 12);
88 assert_se(LESS_BY(4, LESS_BY(8, 4)) == 0);
89 assert_se(CMP(3, 5) == -1);
90 assert_se(CMP(5, 3) == 1);
91 assert_se(CMP(5, 5) == 0);
92 assert_se(CMP(x, y) == -1);
93 assert_se(CMP(y, x) == 1);
94 assert_se(CMP(x, x) == 0);
95 assert_se(CMP(y, y) == 0);
96 assert_se(CMP(UINT64_MAX, (uint64_t) 0) == 1);
97 assert_se(CMP((uint64_t) 0, UINT64_MAX) == -1);
98 assert_se(CMP(UINT64_MAX, UINT64_MAX) == 0);
99 assert_se(CMP(INT64_MIN, INT64_MAX) == -1);
100 assert_se(CMP(INT64_MAX, INT64_MIN) == 1);
101 assert_se(CMP(INT64_MAX, INT64_MAX) == 0);
102 assert_se(CMP(INT64_MIN, INT64_MIN) == 0);
103 assert_se(CMP(INT64_MAX, (int64_t) 0) == 1);
104 assert_se(CMP((int64_t) 0, INT64_MIN) == 1);
105 assert_se(CMP(INT64_MIN, (int64_t) 0) == -1);
106 assert_se(CMP((int64_t) 0, INT64_MAX) == -1);
107 assert_se(CMP(&str[2], &str[7]) == -1);
108 assert_se(CMP(&str[2], &str[2]) == 0);
109 assert_se(CMP(&str[7], (const char *)str) == 1);
110 assert_se(CMP(str[2], str[7]) == 1);
111 assert_se(CMP(str[7], *str) == 1);
112 assert_se(CMP((const unsigned long long *)arr, &arr[3]) == -1);
113 assert_se(CMP(*arr, arr[3]) == 1);
114 assert_se(CMP(p, q) == -1);
115 assert_se(CMP(q, p) == 1);
116 assert_se(CMP(p, p) == 0);
117 assert_se(CMP(q, q) == 0);
118 assert_se(CLAMP(-5, 0, 1) == 0);
119 assert_se(CLAMP(5, 0, 1) == 1);
120 assert_se(CLAMP(5, -10, 1) == 1);
121 assert_se(CLAMP(5, -10, 10) == 5);
122 assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10);
123 }
124
125 #pragma GCC diagnostic push
126 #ifdef __clang__
127 # pragma GCC diagnostic ignored "-Waddress-of-packed-member"
128 #endif
129
130 static void test_container_of(void) {
131 struct mytype {
132 uint8_t pad1[3];
133 uint64_t v1;
134 uint8_t pad2[2];
135 uint32_t v2;
136 } _packed_ myval = { };
137
138 assert_cc(sizeof(myval) == 17);
139 assert_se(container_of(&myval.v1, struct mytype, v1) == &myval);
140 assert_se(container_of(&myval.v2, struct mytype, v2) == &myval);
141 assert_se(container_of(&container_of(&myval.v2,
142 struct mytype,
143 v2)->v1,
144 struct mytype,
145 v1) == &myval);
146 }
147
148 #pragma GCC diagnostic pop
149
150 static void test_div_round_up(void) {
151 int div;
152
153 /* basic tests */
154 assert_se(DIV_ROUND_UP(0, 8) == 0);
155 assert_se(DIV_ROUND_UP(1, 8) == 1);
156 assert_se(DIV_ROUND_UP(8, 8) == 1);
157 assert_se(DIV_ROUND_UP(12, 8) == 2);
158 assert_se(DIV_ROUND_UP(16, 8) == 2);
159
160 /* test multiple evaluation */
161 div = 0;
162 assert_se(DIV_ROUND_UP(div++, 8) == 0 && div == 1);
163 assert_se(DIV_ROUND_UP(++div, 8) == 1 && div == 2);
164 assert_se(DIV_ROUND_UP(8, div++) == 4 && div == 3);
165 assert_se(DIV_ROUND_UP(8, ++div) == 2 && div == 4);
166
167 /* overflow test with exact division */
168 assert_se(sizeof(0U) == 4);
169 assert_se(0xfffffffaU % 10U == 0U);
170 assert_se(0xfffffffaU / 10U == 429496729U);
171 assert_se(DIV_ROUND_UP(0xfffffffaU, 10U) == 429496729U);
172 assert_se((0xfffffffaU + 10U - 1U) / 10U == 0U);
173 assert_se(0xfffffffaU / 10U + !!(0xfffffffaU % 10U) == 429496729U);
174
175 /* overflow test with rounded division */
176 assert_se(0xfffffffdU % 10U == 3U);
177 assert_se(0xfffffffdU / 10U == 429496729U);
178 assert_se(DIV_ROUND_UP(0xfffffffdU, 10U) == 429496730U);
179 assert_se((0xfffffffdU + 10U - 1U) / 10U == 0U);
180 assert_se(0xfffffffdU / 10U + !!(0xfffffffdU % 10U) == 429496730U);
181 }
182
183 static void test_u64log2(void) {
184 assert_se(u64log2(0) == 0);
185 assert_se(u64log2(8) == 3);
186 assert_se(u64log2(9) == 3);
187 assert_se(u64log2(15) == 3);
188 assert_se(u64log2(16) == 4);
189 assert_se(u64log2(1024*1024) == 20);
190 assert_se(u64log2(1024*1024+5) == 20);
191 }
192
193 static void test_protect_errno(void) {
194 errno = 12;
195 {
196 PROTECT_ERRNO;
197 errno = 11;
198 }
199 assert_se(errno == 12);
200 }
201
202 static void test_in_set(void) {
203 assert_se(IN_SET(1, 1));
204 assert_se(IN_SET(1, 1, 2, 3, 4));
205 assert_se(IN_SET(2, 1, 2, 3, 4));
206 assert_se(IN_SET(3, 1, 2, 3, 4));
207 assert_se(IN_SET(4, 1, 2, 3, 4));
208 assert_se(!IN_SET(0, 1));
209 assert_se(!IN_SET(0, 1, 2, 3, 4));
210 }
211
212 static void test_log2i(void) {
213 assert_se(log2i(1) == 0);
214 assert_se(log2i(2) == 1);
215 assert_se(log2i(3) == 1);
216 assert_se(log2i(4) == 2);
217 assert_se(log2i(32) == 5);
218 assert_se(log2i(33) == 5);
219 assert_se(log2i(63) == 5);
220 assert_se(log2i(INT_MAX) == sizeof(int)*8-2);
221 }
222
223 static void test_raw_clone(void) {
224 pid_t parent, pid, pid2;
225
226 parent = getpid();
227 log_info("before clone: getpid()→"PID_FMT, parent);
228 assert_se(raw_getpid() == parent);
229
230 pid = raw_clone(0);
231 assert_se(pid >= 0);
232
233 pid2 = raw_getpid();
234 log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT,
235 pid, getpid(), pid2);
236 if (pid == 0) {
237 assert_se(pid2 != parent);
238 _exit(EXIT_SUCCESS);
239 } else {
240 int status;
241
242 assert_se(pid2 == parent);
243 waitpid(pid, &status, __WCLONE);
244 assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
245 }
246
247 errno = 0;
248 assert_se(raw_clone(CLONE_FS|CLONE_NEWNS) == -1);
249 assert_se(errno == EINVAL);
250 }
251
252 static void test_physical_memory(void) {
253 uint64_t p;
254 char buf[FORMAT_BYTES_MAX];
255
256 p = physical_memory();
257 assert_se(p > 0);
258 assert_se(p < UINT64_MAX);
259 assert_se(p % page_size() == 0);
260
261 log_info("Memory: %s (%" PRIu64 ")", format_bytes(buf, sizeof(buf), p), p);
262 }
263
264 static void test_physical_memory_scale(void) {
265 uint64_t p;
266
267 p = physical_memory();
268
269 assert_se(physical_memory_scale(0, 100) == 0);
270 assert_se(physical_memory_scale(100, 100) == p);
271
272 log_info("Memory original: %" PRIu64, physical_memory());
273 log_info("Memory scaled by 50%%: %" PRIu64, physical_memory_scale(50, 100));
274 log_info("Memory divided by 2: %" PRIu64, physical_memory() / 2);
275 log_info("Page size: %zu", page_size());
276
277 /* There might be an uneven number of pages, hence permit these calculations to be half a page off... */
278 assert_se(page_size()/2 + physical_memory_scale(50, 100) - p/2 <= page_size());
279 assert_se(physical_memory_scale(200, 100) == p*2);
280
281 assert_se(physical_memory_scale(0, 1) == 0);
282 assert_se(physical_memory_scale(1, 1) == p);
283 assert_se(physical_memory_scale(2, 1) == p*2);
284
285 assert_se(physical_memory_scale(0, 2) == 0);
286
287 assert_se(page_size()/2 + physical_memory_scale(1, 2) - p/2 <= page_size());
288 assert_se(physical_memory_scale(2, 2) == p);
289 assert_se(physical_memory_scale(4, 2) == p*2);
290
291 assert_se(physical_memory_scale(0, UINT32_MAX) == 0);
292 assert_se(physical_memory_scale(UINT32_MAX, UINT32_MAX) == p);
293
294 /* overflow */
295 assert_se(physical_memory_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX);
296 }
297
298 static void test_system_tasks_max(void) {
299 uint64_t t;
300
301 t = system_tasks_max();
302 assert_se(t > 0);
303 assert_se(t < UINT64_MAX);
304
305 log_info("Max tasks: %" PRIu64, t);
306 }
307
308 static void test_system_tasks_max_scale(void) {
309 uint64_t t;
310
311 t = system_tasks_max();
312
313 assert_se(system_tasks_max_scale(0, 100) == 0);
314 assert_se(system_tasks_max_scale(100, 100) == t);
315
316 assert_se(system_tasks_max_scale(0, 1) == 0);
317 assert_se(system_tasks_max_scale(1, 1) == t);
318 assert_se(system_tasks_max_scale(2, 1) == 2*t);
319
320 assert_se(system_tasks_max_scale(0, 2) == 0);
321 assert_se(system_tasks_max_scale(1, 2) == t/2);
322 assert_se(system_tasks_max_scale(2, 2) == t);
323 assert_se(system_tasks_max_scale(3, 2) == (3*t)/2);
324 assert_se(system_tasks_max_scale(4, 2) == t*2);
325
326 assert_se(system_tasks_max_scale(0, UINT32_MAX) == 0);
327 assert_se(system_tasks_max_scale((UINT32_MAX-1)/2, UINT32_MAX-1) == t/2);
328 assert_se(system_tasks_max_scale(UINT32_MAX, UINT32_MAX) == t);
329
330 /* overflow */
331
332 assert_se(system_tasks_max_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX);
333 }
334
335 int main(int argc, char *argv[]) {
336 log_parse_environment();
337 log_open();
338
339 test_align_power2();
340 test_max();
341 test_container_of();
342 test_div_round_up();
343 test_u64log2();
344 test_protect_errno();
345 test_in_set();
346 test_log2i();
347 test_raw_clone();
348 test_physical_memory();
349 test_physical_memory_scale();
350 test_system_tasks_max();
351 test_system_tasks_max_scale();
352
353 return 0;
354 }