]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-util.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / test / test-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
539ad707 2
618234a5 3#include <errno.h>
618234a5 4#include <string.h>
8852362b 5#include <sys/wait.h>
618234a5 6#include <unistd.h>
539ad707 7
65b3903f
ZJS
8#include "def.h"
9#include "fileio.h"
f4f15635 10#include "fs-util.h"
36dd5ffd 11#include "missing_syscall.h"
d9ab2bcf 12#include "parse-util.h"
dccca82b 13#include "process-util.h"
8869a0b4 14#include "raw-clone.h"
618234a5 15#include "rm-rf.h"
07630cea 16#include "string-util.h"
09c984c6 17#include "tests.h"
618234a5 18#include "util.h"
539ad707 19
625e870b
DH
20static void test_align_power2(void) {
21 unsigned long i, p2;
22
09c984c6
ZJS
23 log_info("/* %s */", __func__);
24
625e870b
DH
25 assert_se(ALIGN_POWER2(0) == 0);
26 assert_se(ALIGN_POWER2(1) == 1);
27 assert_se(ALIGN_POWER2(2) == 2);
28 assert_se(ALIGN_POWER2(3) == 4);
29 assert_se(ALIGN_POWER2(12) == 16);
30
31 assert_se(ALIGN_POWER2(ULONG_MAX) == 0);
32 assert_se(ALIGN_POWER2(ULONG_MAX - 1) == 0);
33 assert_se(ALIGN_POWER2(ULONG_MAX - 1024) == 0);
34 assert_se(ALIGN_POWER2(ULONG_MAX / 2) == ULONG_MAX / 2 + 1);
35 assert_se(ALIGN_POWER2(ULONG_MAX + 1) == 0);
36
37 for (i = 1; i < 131071; ++i) {
38 for (p2 = 1; p2 < i; p2 <<= 1)
39 /* empty */ ;
40
41 assert_se(ALIGN_POWER2(i) == p2);
42 }
43
44 for (i = ULONG_MAX - 1024; i < ULONG_MAX; ++i) {
45 for (p2 = 1; p2 && p2 < i; p2 <<= 1)
46 /* empty */ ;
47
48 assert_se(ALIGN_POWER2(i) == p2);
49 }
50}
51
7242d742
DH
52static void test_max(void) {
53 static const struct {
54 int a;
55 int b[CONST_MAX(10, 100)];
56 } val1 = {
57 .a = CONST_MAX(10, 100),
58 };
59 int d = 0;
84fb2131
FB
60 unsigned long x = 12345;
61 unsigned long y = 54321;
62 const char str[] = "a_string_constant";
63 const unsigned long long arr[] = {9999ULL, 10ULL, 0ULL, 3000ULL, 2000ULL, 1000ULL, 100ULL, 9999999ULL};
64 void *p = (void *)str;
65 void *q = (void *)&str[16];
7242d742 66
09c984c6
ZJS
67 log_info("/* %s */", __func__);
68
7242d742
DH
69 assert_cc(sizeof(val1.b) == sizeof(int) * 100);
70
71 /* CONST_MAX returns (void) instead of a value if the passed arguments
72 * are not of the same type or not constant expressions. */
73 assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int));
7242d742
DH
74 assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void));
75
76 assert_se(val1.a == 100);
77 assert_se(MAX(++d, 0) == 1);
78 assert_se(d == 1);
40a1eebd
DH
79
80 assert_cc(MAXSIZE(char[3], uint16_t) == 3);
81 assert_cc(MAXSIZE(char[3], uint32_t) == 4);
82 assert_cc(MAXSIZE(char, long) == sizeof(long));
667a0377
DH
83
84 assert_se(MAX(-5, 5) == 5);
85 assert_se(MAX(5, 5) == 5);
86 assert_se(MAX(MAX(1, MAX(2, MAX(3, 4))), 5) == 5);
87 assert_se(MAX(MAX(1, MAX(2, MAX(3, 2))), 1) == 3);
88 assert_se(MAX(MIN(1, MIN(2, MIN(3, 4))), 5) == 5);
89 assert_se(MAX(MAX(1, MIN(2, MIN(3, 2))), 1) == 2);
90 assert_se(LESS_BY(8, 4) == 4);
91 assert_se(LESS_BY(8, 8) == 0);
92 assert_se(LESS_BY(4, 8) == 0);
93 assert_se(LESS_BY(16, LESS_BY(8, 4)) == 12);
94 assert_se(LESS_BY(4, LESS_BY(8, 4)) == 0);
ef6e83f0
YW
95 assert_se(CMP(3, 5) == -1);
96 assert_se(CMP(5, 3) == 1);
84fb2131
FB
97 assert_se(CMP(5, 5) == 0);
98 assert_se(CMP(x, y) == -1);
99 assert_se(CMP(y, x) == 1);
100 assert_se(CMP(x, x) == 0);
101 assert_se(CMP(y, y) == 0);
ef6e83f0
YW
102 assert_se(CMP(UINT64_MAX, (uint64_t) 0) == 1);
103 assert_se(CMP((uint64_t) 0, UINT64_MAX) == -1);
84fb2131
FB
104 assert_se(CMP(UINT64_MAX, UINT64_MAX) == 0);
105 assert_se(CMP(INT64_MIN, INT64_MAX) == -1);
106 assert_se(CMP(INT64_MAX, INT64_MIN) == 1);
107 assert_se(CMP(INT64_MAX, INT64_MAX) == 0);
108 assert_se(CMP(INT64_MIN, INT64_MIN) == 0);
ef6e83f0
YW
109 assert_se(CMP(INT64_MAX, (int64_t) 0) == 1);
110 assert_se(CMP((int64_t) 0, INT64_MIN) == 1);
111 assert_se(CMP(INT64_MIN, (int64_t) 0) == -1);
112 assert_se(CMP((int64_t) 0, INT64_MAX) == -1);
84fb2131
FB
113 assert_se(CMP(&str[2], &str[7]) == -1);
114 assert_se(CMP(&str[2], &str[2]) == 0);
115 assert_se(CMP(&str[7], (const char *)str) == 1);
116 assert_se(CMP(str[2], str[7]) == 1);
117 assert_se(CMP(str[7], *str) == 1);
118 assert_se(CMP((const unsigned long long *)arr, &arr[3]) == -1);
119 assert_se(CMP(*arr, arr[3]) == 1);
120 assert_se(CMP(p, q) == -1);
121 assert_se(CMP(q, p) == 1);
122 assert_se(CMP(p, p) == 0);
123 assert_se(CMP(q, q) == 0);
667a0377
DH
124 assert_se(CLAMP(-5, 0, 1) == 0);
125 assert_se(CLAMP(5, 0, 1) == 1);
126 assert_se(CLAMP(5, -10, 1) == 1);
127 assert_se(CLAMP(5, -10, 10) == 5);
128 assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10);
7242d742
DH
129}
130
09571e3f
ZJS
131#pragma GCC diagnostic push
132#ifdef __clang__
133# pragma GCC diagnostic ignored "-Waddress-of-packed-member"
134#endif
135
fb835651
DH
136static void test_container_of(void) {
137 struct mytype {
138 uint8_t pad1[3];
139 uint64_t v1;
140 uint8_t pad2[2];
141 uint32_t v2;
6b68c268 142 } myval = { };
fb835651 143
09c984c6
ZJS
144 log_info("/* %s */", __func__);
145
6b68c268 146 assert_cc(sizeof(myval) >= 17);
fb835651
DH
147 assert_se(container_of(&myval.v1, struct mytype, v1) == &myval);
148 assert_se(container_of(&myval.v2, struct mytype, v2) == &myval);
149 assert_se(container_of(&container_of(&myval.v2,
150 struct mytype,
151 v2)->v1,
152 struct mytype,
153 v1) == &myval);
154}
155
09571e3f
ZJS
156#pragma GCC diagnostic pop
157
180a60bc
DH
158static void test_div_round_up(void) {
159 int div;
160
09c984c6
ZJS
161 log_info("/* %s */", __func__);
162
180a60bc
DH
163 /* basic tests */
164 assert_se(DIV_ROUND_UP(0, 8) == 0);
165 assert_se(DIV_ROUND_UP(1, 8) == 1);
166 assert_se(DIV_ROUND_UP(8, 8) == 1);
167 assert_se(DIV_ROUND_UP(12, 8) == 2);
168 assert_se(DIV_ROUND_UP(16, 8) == 2);
169
170 /* test multiple evaluation */
171 div = 0;
172 assert_se(DIV_ROUND_UP(div++, 8) == 0 && div == 1);
173 assert_se(DIV_ROUND_UP(++div, 8) == 1 && div == 2);
174 assert_se(DIV_ROUND_UP(8, div++) == 4 && div == 3);
175 assert_se(DIV_ROUND_UP(8, ++div) == 2 && div == 4);
176
177 /* overflow test with exact division */
178 assert_se(sizeof(0U) == 4);
179 assert_se(0xfffffffaU % 10U == 0U);
180 assert_se(0xfffffffaU / 10U == 429496729U);
181 assert_se(DIV_ROUND_UP(0xfffffffaU, 10U) == 429496729U);
182 assert_se((0xfffffffaU + 10U - 1U) / 10U == 0U);
183 assert_se(0xfffffffaU / 10U + !!(0xfffffffaU % 10U) == 429496729U);
184
185 /* overflow test with rounded division */
186 assert_se(0xfffffffdU % 10U == 3U);
187 assert_se(0xfffffffdU / 10U == 429496729U);
188 assert_se(DIV_ROUND_UP(0xfffffffdU, 10U) == 429496730U);
189 assert_se((0xfffffffdU + 10U - 1U) / 10U == 0U);
190 assert_se(0xfffffffdU / 10U + !!(0xfffffffdU % 10U) == 429496730U);
191}
192
144e51ec 193static void test_u64log2(void) {
09c984c6
ZJS
194 log_info("/* %s */", __func__);
195
bdf7026e
TA
196 assert_se(u64log2(0) == 0);
197 assert_se(u64log2(8) == 3);
198 assert_se(u64log2(9) == 3);
199 assert_se(u64log2(15) == 3);
200 assert_se(u64log2(16) == 4);
201 assert_se(u64log2(1024*1024) == 20);
202 assert_se(u64log2(1024*1024+5) == 20);
144e51ec
CR
203}
204
2a371001 205static void test_protect_errno(void) {
09c984c6
ZJS
206 log_info("/* %s */", __func__);
207
2a371001
ZJS
208 errno = 12;
209 {
210 PROTECT_ERRNO;
211 errno = 11;
212 }
bdf7026e 213 assert_se(errno == 12);
2a371001
ZJS
214}
215
840f606d
LP
216static void test_unprotect_errno_inner_function(void) {
217 PROTECT_ERRNO;
218
219 errno = 2222;
220}
221
222static void test_unprotect_errno(void) {
223 log_info("/* %s */", __func__);
224
225 errno = 4711;
226
227 PROTECT_ERRNO;
228
229 errno = 815;
230
231 UNPROTECT_ERRNO;
232
233 assert_se(errno == 4711);
234
235 test_unprotect_errno_inner_function();
236
237 assert_se(errno == 4711);
238}
239
cabb7806 240static void test_in_set(void) {
09c984c6
ZJS
241 log_info("/* %s */", __func__);
242
cabb7806
LP
243 assert_se(IN_SET(1, 1));
244 assert_se(IN_SET(1, 1, 2, 3, 4));
245 assert_se(IN_SET(2, 1, 2, 3, 4));
246 assert_se(IN_SET(3, 1, 2, 3, 4));
247 assert_se(IN_SET(4, 1, 2, 3, 4));
248 assert_se(!IN_SET(0, 1));
249 assert_se(!IN_SET(0, 1, 2, 3, 4));
250}
251
8fe90522 252static void test_log2i(void) {
09c984c6
ZJS
253 log_info("/* %s */", __func__);
254
8fe90522
ZJS
255 assert_se(log2i(1) == 0);
256 assert_se(log2i(2) == 1);
257 assert_se(log2i(3) == 1);
258 assert_se(log2i(4) == 2);
259 assert_se(log2i(32) == 5);
260 assert_se(log2i(33) == 5);
261 assert_se(log2i(63) == 5);
262 assert_se(log2i(INT_MAX) == sizeof(int)*8-2);
263}
264
7f6bfc56
ZJS
265static void test_eqzero(void) {
266 const uint32_t zeros[] = {0, 0, 0};
267 const uint32_t ones[] = {1, 1};
268 const uint32_t mixed[] = {0, 1, 0, 0, 0};
269 const uint8_t longer[] = {[55] = 255};
270
271 log_info("/* %s */", __func__);
272
273 assert_se(eqzero(zeros));
274 assert_se(!eqzero(ones));
275 assert_se(!eqzero(mixed));
276 assert_se(!eqzero(longer));
277}
278
ee05e779
ZJS
279static void test_raw_clone(void) {
280 pid_t parent, pid, pid2;
281
09c984c6
ZJS
282 log_info("/* %s */", __func__);
283
ee05e779
ZJS
284 parent = getpid();
285 log_info("before clone: getpid()→"PID_FMT, parent);
286 assert_se(raw_getpid() == parent);
287
8869a0b4 288 pid = raw_clone(0);
e50221bf 289 assert_se(pid >= 0);
ee05e779
ZJS
290
291 pid2 = raw_getpid();
292 log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT,
293 pid, getpid(), pid2);
0289a5bc 294 if (pid == 0) {
e50221bf 295 assert_se(pid2 != parent);
0289a5bc
FB
296 _exit(EXIT_SUCCESS);
297 } else {
298 int status;
299
e50221bf 300 assert_se(pid2 == parent);
0289a5bc
FB
301 waitpid(pid, &status, __WCLONE);
302 assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS);
303 }
96f64eb5
MG
304
305 errno = 0;
306 assert_se(raw_clone(CLONE_FS|CLONE_NEWNS) == -1);
307 assert_se(errno == EINVAL);
ee05e779
ZJS
308}
309
d9ab2bcf
LP
310static void test_physical_memory(void) {
311 uint64_t p;
312 char buf[FORMAT_BYTES_MAX];
313
09c984c6
ZJS
314 log_info("/* %s */", __func__);
315
d9ab2bcf
LP
316 p = physical_memory();
317 assert_se(p > 0);
318 assert_se(p < UINT64_MAX);
319 assert_se(p % page_size() == 0);
320
d8cf2ac7
LP
321 log_info("Memory: %s (%" PRIu64 ")", format_bytes(buf, sizeof(buf), p), p);
322}
323
324static void test_physical_memory_scale(void) {
325 uint64_t p;
326
09c984c6
ZJS
327 log_info("/* %s */", __func__);
328
d8cf2ac7
LP
329 p = physical_memory();
330
331 assert_se(physical_memory_scale(0, 100) == 0);
332 assert_se(physical_memory_scale(100, 100) == p);
333
334 log_info("Memory original: %" PRIu64, physical_memory());
335 log_info("Memory scaled by 50%%: %" PRIu64, physical_memory_scale(50, 100));
336 log_info("Memory divided by 2: %" PRIu64, physical_memory() / 2);
337 log_info("Page size: %zu", page_size());
338
339 /* There might be an uneven number of pages, hence permit these calculations to be half a page off... */
340 assert_se(page_size()/2 + physical_memory_scale(50, 100) - p/2 <= page_size());
341 assert_se(physical_memory_scale(200, 100) == p*2);
342
343 assert_se(physical_memory_scale(0, 1) == 0);
344 assert_se(physical_memory_scale(1, 1) == p);
345 assert_se(physical_memory_scale(2, 1) == p*2);
346
347 assert_se(physical_memory_scale(0, 2) == 0);
348
349 assert_se(page_size()/2 + physical_memory_scale(1, 2) - p/2 <= page_size());
350 assert_se(physical_memory_scale(2, 2) == p);
351 assert_se(physical_memory_scale(4, 2) == p*2);
352
353 assert_se(physical_memory_scale(0, UINT32_MAX) == 0);
354 assert_se(physical_memory_scale(UINT32_MAX, UINT32_MAX) == p);
355
356 /* overflow */
357 assert_se(physical_memory_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX);
83f8e808
LP
358}
359
360static void test_system_tasks_max(void) {
361 uint64_t t;
362
09c984c6
ZJS
363 log_info("/* %s */", __func__);
364
83f8e808
LP
365 t = system_tasks_max();
366 assert_se(t > 0);
367 assert_se(t < UINT64_MAX);
368
369 log_info("Max tasks: %" PRIu64, t);
370}
371
372static void test_system_tasks_max_scale(void) {
373 uint64_t t;
374
09c984c6
ZJS
375 log_info("/* %s */", __func__);
376
83f8e808
LP
377 t = system_tasks_max();
378
379 assert_se(system_tasks_max_scale(0, 100) == 0);
380 assert_se(system_tasks_max_scale(100, 100) == t);
381
382 assert_se(system_tasks_max_scale(0, 1) == 0);
383 assert_se(system_tasks_max_scale(1, 1) == t);
384 assert_se(system_tasks_max_scale(2, 1) == 2*t);
385
386 assert_se(system_tasks_max_scale(0, 2) == 0);
387 assert_se(system_tasks_max_scale(1, 2) == t/2);
388 assert_se(system_tasks_max_scale(2, 2) == t);
389 assert_se(system_tasks_max_scale(3, 2) == (3*t)/2);
390 assert_se(system_tasks_max_scale(4, 2) == t*2);
391
392 assert_se(system_tasks_max_scale(0, UINT32_MAX) == 0);
393 assert_se(system_tasks_max_scale((UINT32_MAX-1)/2, UINT32_MAX-1) == t/2);
394 assert_se(system_tasks_max_scale(UINT32_MAX, UINT32_MAX) == t);
395
396 /* overflow */
d8cf2ac7 397
83f8e808 398 assert_se(system_tasks_max_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX);
d9ab2bcf
LP
399}
400
539ad707 401int main(int argc, char *argv[]) {
09c984c6 402 test_setup_logging(LOG_INFO);
9480794b 403
625e870b 404 test_align_power2();
7242d742 405 test_max();
fb835651 406 test_container_of();
180a60bc 407 test_div_round_up();
144e51ec 408 test_u64log2();
2a371001 409 test_protect_errno();
840f606d 410 test_unprotect_errno();
cabb7806 411 test_in_set();
8fe90522 412 test_log2i();
7f6bfc56 413 test_eqzero();
ee05e779 414 test_raw_clone();
d9ab2bcf 415 test_physical_memory();
d8cf2ac7 416 test_physical_memory_scale();
83f8e808
LP
417 test_system_tasks_max();
418 test_system_tasks_max_scale();
539ad707
TA
419
420 return 0;
421}