]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
539ad707 | 2 | /*** |
539ad707 TA |
3 | Copyright 2010 Lennart Poettering |
4 | Copyright 2013 Thomas H.P. Andersen | |
539ad707 TA |
5 | ***/ |
6 | ||
618234a5 | 7 | #include <errno.h> |
618234a5 | 8 | #include <string.h> |
8852362b | 9 | #include <sys/wait.h> |
618234a5 | 10 | #include <unistd.h> |
539ad707 | 11 | |
65b3903f ZJS |
12 | #include "def.h" |
13 | #include "fileio.h" | |
f4f15635 | 14 | #include "fs-util.h" |
d9ab2bcf | 15 | #include "parse-util.h" |
dccca82b | 16 | #include "process-util.h" |
8869a0b4 | 17 | #include "raw-clone.h" |
618234a5 | 18 | #include "rm-rf.h" |
07630cea | 19 | #include "string-util.h" |
618234a5 | 20 | #include "util.h" |
539ad707 | 21 | |
625e870b DH |
22 | static void test_align_power2(void) { |
23 | unsigned long i, p2; | |
24 | ||
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 |
52 | static 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; | |
60 | ||
61 | assert_cc(sizeof(val1.b) == sizeof(int) * 100); | |
62 | ||
63 | /* CONST_MAX returns (void) instead of a value if the passed arguments | |
64 | * are not of the same type or not constant expressions. */ | |
65 | assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 10)), int)); | |
7242d742 DH |
66 | assert_cc(__builtin_types_compatible_p(typeof(CONST_MAX(1, 1U)), void)); |
67 | ||
68 | assert_se(val1.a == 100); | |
69 | assert_se(MAX(++d, 0) == 1); | |
70 | assert_se(d == 1); | |
40a1eebd DH |
71 | |
72 | assert_cc(MAXSIZE(char[3], uint16_t) == 3); | |
73 | assert_cc(MAXSIZE(char[3], uint32_t) == 4); | |
74 | assert_cc(MAXSIZE(char, long) == sizeof(long)); | |
667a0377 DH |
75 | |
76 | assert_se(MAX(-5, 5) == 5); | |
77 | assert_se(MAX(5, 5) == 5); | |
78 | assert_se(MAX(MAX(1, MAX(2, MAX(3, 4))), 5) == 5); | |
79 | assert_se(MAX(MAX(1, MAX(2, MAX(3, 2))), 1) == 3); | |
80 | assert_se(MAX(MIN(1, MIN(2, MIN(3, 4))), 5) == 5); | |
81 | assert_se(MAX(MAX(1, MIN(2, MIN(3, 2))), 1) == 2); | |
82 | assert_se(LESS_BY(8, 4) == 4); | |
83 | assert_se(LESS_BY(8, 8) == 0); | |
84 | assert_se(LESS_BY(4, 8) == 0); | |
85 | assert_se(LESS_BY(16, LESS_BY(8, 4)) == 12); | |
86 | assert_se(LESS_BY(4, LESS_BY(8, 4)) == 0); | |
87 | assert_se(CLAMP(-5, 0, 1) == 0); | |
88 | assert_se(CLAMP(5, 0, 1) == 1); | |
89 | assert_se(CLAMP(5, -10, 1) == 1); | |
90 | assert_se(CLAMP(5, -10, 10) == 5); | |
91 | assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10); | |
7242d742 DH |
92 | } |
93 | ||
09571e3f ZJS |
94 | #pragma GCC diagnostic push |
95 | #ifdef __clang__ | |
96 | # pragma GCC diagnostic ignored "-Waddress-of-packed-member" | |
97 | #endif | |
98 | ||
fb835651 DH |
99 | static void test_container_of(void) { |
100 | struct mytype { | |
101 | uint8_t pad1[3]; | |
102 | uint64_t v1; | |
103 | uint8_t pad2[2]; | |
104 | uint32_t v2; | |
105 | } _packed_ myval = { }; | |
106 | ||
107 | assert_cc(sizeof(myval) == 17); | |
108 | assert_se(container_of(&myval.v1, struct mytype, v1) == &myval); | |
109 | assert_se(container_of(&myval.v2, struct mytype, v2) == &myval); | |
110 | assert_se(container_of(&container_of(&myval.v2, | |
111 | struct mytype, | |
112 | v2)->v1, | |
113 | struct mytype, | |
114 | v1) == &myval); | |
115 | } | |
116 | ||
09571e3f ZJS |
117 | #pragma GCC diagnostic pop |
118 | ||
180a60bc DH |
119 | static void test_div_round_up(void) { |
120 | int div; | |
121 | ||
122 | /* basic tests */ | |
123 | assert_se(DIV_ROUND_UP(0, 8) == 0); | |
124 | assert_se(DIV_ROUND_UP(1, 8) == 1); | |
125 | assert_se(DIV_ROUND_UP(8, 8) == 1); | |
126 | assert_se(DIV_ROUND_UP(12, 8) == 2); | |
127 | assert_se(DIV_ROUND_UP(16, 8) == 2); | |
128 | ||
129 | /* test multiple evaluation */ | |
130 | div = 0; | |
131 | assert_se(DIV_ROUND_UP(div++, 8) == 0 && div == 1); | |
132 | assert_se(DIV_ROUND_UP(++div, 8) == 1 && div == 2); | |
133 | assert_se(DIV_ROUND_UP(8, div++) == 4 && div == 3); | |
134 | assert_se(DIV_ROUND_UP(8, ++div) == 2 && div == 4); | |
135 | ||
136 | /* overflow test with exact division */ | |
137 | assert_se(sizeof(0U) == 4); | |
138 | assert_se(0xfffffffaU % 10U == 0U); | |
139 | assert_se(0xfffffffaU / 10U == 429496729U); | |
140 | assert_se(DIV_ROUND_UP(0xfffffffaU, 10U) == 429496729U); | |
141 | assert_se((0xfffffffaU + 10U - 1U) / 10U == 0U); | |
142 | assert_se(0xfffffffaU / 10U + !!(0xfffffffaU % 10U) == 429496729U); | |
143 | ||
144 | /* overflow test with rounded division */ | |
145 | assert_se(0xfffffffdU % 10U == 3U); | |
146 | assert_se(0xfffffffdU / 10U == 429496729U); | |
147 | assert_se(DIV_ROUND_UP(0xfffffffdU, 10U) == 429496730U); | |
148 | assert_se((0xfffffffdU + 10U - 1U) / 10U == 0U); | |
149 | assert_se(0xfffffffdU / 10U + !!(0xfffffffdU % 10U) == 429496730U); | |
150 | } | |
151 | ||
144e51ec | 152 | static void test_u64log2(void) { |
bdf7026e TA |
153 | assert_se(u64log2(0) == 0); |
154 | assert_se(u64log2(8) == 3); | |
155 | assert_se(u64log2(9) == 3); | |
156 | assert_se(u64log2(15) == 3); | |
157 | assert_se(u64log2(16) == 4); | |
158 | assert_se(u64log2(1024*1024) == 20); | |
159 | assert_se(u64log2(1024*1024+5) == 20); | |
144e51ec CR |
160 | } |
161 | ||
2a371001 ZJS |
162 | static void test_protect_errno(void) { |
163 | errno = 12; | |
164 | { | |
165 | PROTECT_ERRNO; | |
166 | errno = 11; | |
167 | } | |
bdf7026e | 168 | assert_se(errno == 12); |
2a371001 ZJS |
169 | } |
170 | ||
cabb7806 LP |
171 | static void test_in_set(void) { |
172 | assert_se(IN_SET(1, 1)); | |
173 | assert_se(IN_SET(1, 1, 2, 3, 4)); | |
174 | assert_se(IN_SET(2, 1, 2, 3, 4)); | |
175 | assert_se(IN_SET(3, 1, 2, 3, 4)); | |
176 | assert_se(IN_SET(4, 1, 2, 3, 4)); | |
177 | assert_se(!IN_SET(0, 1)); | |
178 | assert_se(!IN_SET(0, 1, 2, 3, 4)); | |
179 | } | |
180 | ||
8fe90522 ZJS |
181 | static void test_log2i(void) { |
182 | assert_se(log2i(1) == 0); | |
183 | assert_se(log2i(2) == 1); | |
184 | assert_se(log2i(3) == 1); | |
185 | assert_se(log2i(4) == 2); | |
186 | assert_se(log2i(32) == 5); | |
187 | assert_se(log2i(33) == 5); | |
188 | assert_se(log2i(63) == 5); | |
189 | assert_se(log2i(INT_MAX) == sizeof(int)*8-2); | |
190 | } | |
191 | ||
ee05e779 ZJS |
192 | static void test_raw_clone(void) { |
193 | pid_t parent, pid, pid2; | |
194 | ||
195 | parent = getpid(); | |
196 | log_info("before clone: getpid()→"PID_FMT, parent); | |
197 | assert_se(raw_getpid() == parent); | |
198 | ||
8869a0b4 | 199 | pid = raw_clone(0); |
e50221bf | 200 | assert_se(pid >= 0); |
ee05e779 ZJS |
201 | |
202 | pid2 = raw_getpid(); | |
203 | log_info("raw_clone: "PID_FMT" getpid()→"PID_FMT" raw_getpid()→"PID_FMT, | |
204 | pid, getpid(), pid2); | |
0289a5bc | 205 | if (pid == 0) { |
e50221bf | 206 | assert_se(pid2 != parent); |
0289a5bc FB |
207 | _exit(EXIT_SUCCESS); |
208 | } else { | |
209 | int status; | |
210 | ||
e50221bf | 211 | assert_se(pid2 == parent); |
0289a5bc FB |
212 | waitpid(pid, &status, __WCLONE); |
213 | assert_se(WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS); | |
214 | } | |
96f64eb5 MG |
215 | |
216 | errno = 0; | |
217 | assert_se(raw_clone(CLONE_FS|CLONE_NEWNS) == -1); | |
218 | assert_se(errno == EINVAL); | |
ee05e779 ZJS |
219 | } |
220 | ||
d9ab2bcf LP |
221 | static void test_physical_memory(void) { |
222 | uint64_t p; | |
223 | char buf[FORMAT_BYTES_MAX]; | |
224 | ||
225 | p = physical_memory(); | |
226 | assert_se(p > 0); | |
227 | assert_se(p < UINT64_MAX); | |
228 | assert_se(p % page_size() == 0); | |
229 | ||
d8cf2ac7 LP |
230 | log_info("Memory: %s (%" PRIu64 ")", format_bytes(buf, sizeof(buf), p), p); |
231 | } | |
232 | ||
233 | static void test_physical_memory_scale(void) { | |
234 | uint64_t p; | |
235 | ||
236 | p = physical_memory(); | |
237 | ||
238 | assert_se(physical_memory_scale(0, 100) == 0); | |
239 | assert_se(physical_memory_scale(100, 100) == p); | |
240 | ||
241 | log_info("Memory original: %" PRIu64, physical_memory()); | |
242 | log_info("Memory scaled by 50%%: %" PRIu64, physical_memory_scale(50, 100)); | |
243 | log_info("Memory divided by 2: %" PRIu64, physical_memory() / 2); | |
244 | log_info("Page size: %zu", page_size()); | |
245 | ||
246 | /* There might be an uneven number of pages, hence permit these calculations to be half a page off... */ | |
247 | assert_se(page_size()/2 + physical_memory_scale(50, 100) - p/2 <= page_size()); | |
248 | assert_se(physical_memory_scale(200, 100) == p*2); | |
249 | ||
250 | assert_se(physical_memory_scale(0, 1) == 0); | |
251 | assert_se(physical_memory_scale(1, 1) == p); | |
252 | assert_se(physical_memory_scale(2, 1) == p*2); | |
253 | ||
254 | assert_se(physical_memory_scale(0, 2) == 0); | |
255 | ||
256 | assert_se(page_size()/2 + physical_memory_scale(1, 2) - p/2 <= page_size()); | |
257 | assert_se(physical_memory_scale(2, 2) == p); | |
258 | assert_se(physical_memory_scale(4, 2) == p*2); | |
259 | ||
260 | assert_se(physical_memory_scale(0, UINT32_MAX) == 0); | |
261 | assert_se(physical_memory_scale(UINT32_MAX, UINT32_MAX) == p); | |
262 | ||
263 | /* overflow */ | |
264 | assert_se(physical_memory_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX); | |
83f8e808 LP |
265 | } |
266 | ||
267 | static void test_system_tasks_max(void) { | |
268 | uint64_t t; | |
269 | ||
270 | t = system_tasks_max(); | |
271 | assert_se(t > 0); | |
272 | assert_se(t < UINT64_MAX); | |
273 | ||
274 | log_info("Max tasks: %" PRIu64, t); | |
275 | } | |
276 | ||
277 | static void test_system_tasks_max_scale(void) { | |
278 | uint64_t t; | |
279 | ||
280 | t = system_tasks_max(); | |
281 | ||
282 | assert_se(system_tasks_max_scale(0, 100) == 0); | |
283 | assert_se(system_tasks_max_scale(100, 100) == t); | |
284 | ||
285 | assert_se(system_tasks_max_scale(0, 1) == 0); | |
286 | assert_se(system_tasks_max_scale(1, 1) == t); | |
287 | assert_se(system_tasks_max_scale(2, 1) == 2*t); | |
288 | ||
289 | assert_se(system_tasks_max_scale(0, 2) == 0); | |
290 | assert_se(system_tasks_max_scale(1, 2) == t/2); | |
291 | assert_se(system_tasks_max_scale(2, 2) == t); | |
292 | assert_se(system_tasks_max_scale(3, 2) == (3*t)/2); | |
293 | assert_se(system_tasks_max_scale(4, 2) == t*2); | |
294 | ||
295 | assert_se(system_tasks_max_scale(0, UINT32_MAX) == 0); | |
296 | assert_se(system_tasks_max_scale((UINT32_MAX-1)/2, UINT32_MAX-1) == t/2); | |
297 | assert_se(system_tasks_max_scale(UINT32_MAX, UINT32_MAX) == t); | |
298 | ||
299 | /* overflow */ | |
d8cf2ac7 | 300 | |
83f8e808 | 301 | assert_se(system_tasks_max_scale(UINT64_MAX/4, UINT64_MAX) == UINT64_MAX); |
d9ab2bcf LP |
302 | } |
303 | ||
539ad707 | 304 | int main(int argc, char *argv[]) { |
9480794b ZJS |
305 | log_parse_environment(); |
306 | log_open(); | |
307 | ||
625e870b | 308 | test_align_power2(); |
7242d742 | 309 | test_max(); |
fb835651 | 310 | test_container_of(); |
180a60bc | 311 | test_div_round_up(); |
144e51ec | 312 | test_u64log2(); |
2a371001 | 313 | test_protect_errno(); |
cabb7806 | 314 | test_in_set(); |
8fe90522 | 315 | test_log2i(); |
ee05e779 | 316 | test_raw_clone(); |
d9ab2bcf | 317 | test_physical_memory(); |
d8cf2ac7 | 318 | test_physical_memory_scale(); |
83f8e808 LP |
319 | test_system_tasks_max(); |
320 | test_system_tasks_max_scale(); | |
539ad707 TA |
321 | |
322 | return 0; | |
323 | } |