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