]> git.proxmox.com Git - mirror_lxc.git/blob - src/tests/lxc-test-utils.c
e23ac729c960a81646243744e53cfe536446f63f
[mirror_lxc.git] / src / tests / lxc-test-utils.c
1 /*
2 * lxc: linux Container library
3 *
4 * Copyright © 2016 Canonical Ltd.
5 *
6 * Authors:
7 * Christian Brauner <christian.brauner@mailbox.org>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "config.h"
25
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <inttypes.h>
29 #include <limits.h>
30 #include <pthread.h>
31 #include <sched.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/mount.h>
37 #include <sys/stat.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40
41 #include "confile_utils.h"
42 #include "lxctest.h"
43 #include "macro.h"
44 #include "utils.h"
45
46 void test_path_simplify(void)
47 {
48 char *s = "/A///B//C/D/E/";
49 char *t;
50
51 t = path_simplify(s);
52 if (!t)
53 exit(EXIT_FAILURE);
54
55 lxc_test_assert_abort(strcmp(t, "/A/B/C/D/E") == 0);
56 free(t);
57
58 s = "/A";
59
60 t = path_simplify(s);
61 if (!t)
62 exit(EXIT_FAILURE);
63
64 lxc_test_assert_abort(strcmp(t, "/A") == 0);
65 free(t);
66
67 s = "";
68 t = path_simplify(s);
69 if (!t)
70 exit(EXIT_FAILURE);
71
72 lxc_test_assert_abort(strcmp(t, "") == 0);
73 free(t);
74
75 s = "//";
76
77 t = path_simplify(s);
78 if (!t)
79 exit(EXIT_FAILURE);
80
81 lxc_test_assert_abort(strcmp(t, "/") == 0);
82 free(t);
83 }
84
85 /* /proc/int_as_str/ns/mnt\0 = (5 + 21 + 7 + 1) */
86 #define __MNTNS_LEN (5 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1)
87 void test_detect_ramfs_rootfs(void)
88 {
89 size_t i;
90 int ret;
91 int fret = EXIT_FAILURE;
92 char path[__MNTNS_LEN];
93 int init_ns = -1;
94 char tmpf1[] = "lxc-test-utils-XXXXXX";
95 char tmpf2[] = "lxc-test-utils-XXXXXX";
96 int fd1 = -1, fd2 = -1;
97 FILE *fp1 = NULL, *fp2 = NULL;
98 char *mountinfo[] = {
99 "18 24 0:17 / /sys rw,nosuid,nodev,noexec,relatime shared:7 - sysfs sysfs rw",
100 "19 24 0:4 / /proc rw,nosuid,nodev,noexec,relatime shared:13 - proc proc rw",
101 "20 24 0:6 / /dev rw,nosuid,relatime shared:2 - devtmpfs udev rw,size=4019884k,nr_inodes=1004971,mode=755",
102 "21 20 0:14 / /dev/pts rw,nosuid,noexec,relatime shared:3 - devpts devpts rw,gid=5,mode=620,ptmxmode=000",
103 "22 24 0:18 / /run rw,nosuid,noexec,relatime shared:5 - tmpfs tmpfs rw,size=807912k,mode=755",
104
105 /* This is what we care about. */
106 "24 0 8:2 / / rw - rootfs rootfs rw,size=1004396k,nr_inodes=251099",
107
108 "25 18 0:12 / /sys/kernel/security rw,nosuid,nodev,noexec,relatime shared:8 - securityfs securityfs rw",
109 "26 20 0:20 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw",
110 "27 22 0:21 / /run/lock rw,nosuid,nodev,noexec,relatime shared:6 - tmpfs tmpfs rw,size=5120k",
111 "28 18 0:22 / /sys/fs/cgroup ro,nosuid,nodev,noexec shared:9 - tmpfs tmpfs ro,mode=755",
112 "29 28 0:23 / /sys/fs/cgroup/systemd rw,nosuid,nodev,noexec,relatime shared:10 - cgroup cgroup rw,xattr,release_agent=/lib/systemd/systemd-cgroups-agent,name=systemd",
113 "30 18 0:24 / /sys/fs/pstore rw,nosuid,nodev,noexec,relatime shared:11 - pstore pstore rw",
114 "31 18 0:25 / /sys/firmware/efi/efivars rw,nosuid,nodev,noexec,relatime shared:12 - efivarfs efivarfs rw",
115 "32 28 0:26 / /sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:14 - cgroup cgroup rw,cpu,cpuacct",
116 "33 28 0:27 / /sys/fs/cgroup/net_cls,net_prio rw,nosuid,nodev,noexec,relatime shared:15 - cgroup cgroup rw,net_cls,net_prio",
117 "34 28 0:28 / /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime shared:16 - cgroup cgroup rw,blkio",
118 "35 28 0:29 / /sys/fs/cgroup/freezer rw,nosuid,nodev,noexec,relatime shared:17 - cgroup cgroup rw,freezer",
119 "36 28 0:30 / /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime shared:18 - cgroup cgroup rw,memory",
120 "37 28 0:31 / /sys/fs/cgroup/hugetlb rw,nosuid,nodev,noexec,relatime shared:19 - cgroup cgroup rw,hugetlb",
121 "38 28 0:32 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:20 - cgroup cgroup rw,cpuset",
122 "39 28 0:33 / /sys/fs/cgroup/devices rw,nosuid,nodev,noexec,relatime shared:21 - cgroup cgroup rw,devices",
123 "40 28 0:34 / /sys/fs/cgroup/pids rw,nosuid,nodev,noexec,relatime shared:22 - cgroup cgroup rw,pids",
124 "41 28 0:35 / /sys/fs/cgroup/perf_event rw,nosuid,nodev,noexec,relatime shared:23 - cgroup cgroup rw,perf_event",
125 "42 19 0:36 / /proc/sys/fs/binfmt_misc rw,relatime shared:24 - autofs systemd-1 rw,fd=32,pgrp=1,timeout=0,minproto=5,maxproto=5,direct",
126 "43 18 0:7 / /sys/kernel/debug rw,relatime shared:25 - debugfs debugfs rw",
127 "44 20 0:37 / /dev/hugepages rw,relatime shared:26 - hugetlbfs hugetlbfs rw",
128 "45 20 0:16 / /dev/mqueue rw,relatime shared:27 - mqueue mqueue rw",
129 "46 43 0:9 / /sys/kernel/debug/tracing rw,relatime shared:28 - tracefs tracefs rw",
130 "76 18 0:38 / /sys/fs/fuse/connections rw,relatime shared:29 - fusectl fusectl rw",
131 "78 24 8:1 / /boot/efi rw,relatime shared:30 - vfat /dev/sda1 rw,fmask=0077,dmask=0077,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro",
132 };
133
134 ret = snprintf(path, __MNTNS_LEN, "/proc/self/ns/mnt");
135 if (ret < 0 || (size_t)ret >= __MNTNS_LEN) {
136 lxc_error("%s\n", "Failed to create path with snprintf().");
137 goto non_test_error;
138 }
139
140 init_ns = open(path, O_RDONLY | O_CLOEXEC);
141 if (init_ns < 0) {
142 lxc_error("%s\n", "Failed to open initial mount namespace.");
143 goto non_test_error;
144 }
145
146 if (unshare(CLONE_NEWNS) < 0) {
147 lxc_error("%s\n", "Could not unshare mount namespace.");
148 close(init_ns);
149 init_ns = -1;
150 goto non_test_error;
151 }
152
153 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0) < 0) {
154 lxc_error("Failed to remount / private: %s.\n", strerror(errno));
155 goto non_test_error;
156 }
157
158 fd1 = lxc_make_tmpfile(tmpf1, false);
159 if (fd1 < 0) {
160 lxc_error("%s\n", "Could not create temporary file.");
161 goto non_test_error;
162 }
163
164 fd2 = lxc_make_tmpfile(tmpf2, false);
165 if (fd2 < 0) {
166 lxc_error("%s\n", "Could not create temporary file.");
167 goto non_test_error;
168 }
169
170 fp1 = fdopen(fd1, "r+");
171 if (!fp1) {
172 lxc_error("%s\n", "Could not fdopen() temporary file.");
173 goto non_test_error;
174 }
175
176 fp2 = fdopen(fd2, "r+");
177 if (!fp2) {
178 lxc_error("%s\n", "Could not fdopen() temporary file.");
179 goto non_test_error;
180 }
181
182 /* Test if it correctly detects - rootfs rootfs */
183 for (i = 0; i < sizeof(mountinfo) / sizeof(mountinfo[0]); i++) {
184 if (fprintf(fp1, "%s\n", mountinfo[i]) < 0) {
185 lxc_error("Could not write \"%s\" to temporary file.", mountinfo[i]);
186 goto non_test_error;
187 }
188 }
189 fclose(fp1);
190 fp1 = NULL;
191 fd1 = -1;
192
193 /* Test if it correctly fails to detect when no - rootfs rootfs */
194 for (i = 0; i < sizeof(mountinfo) / sizeof(mountinfo[0]); i++) {
195 if (strcmp(mountinfo[i], "24 0 8:2 / / rw - rootfs rootfs rw,size=1004396k,nr_inodes=251099") == 0)
196 continue;
197
198 if (fprintf(fp2, "%s\n", mountinfo[i]) < 0) {
199 lxc_error("Could not write \"%s\" to temporary file.", mountinfo[i]);
200 goto non_test_error;
201 }
202 }
203 fclose(fp2);
204 fp2 = NULL;
205 fd2 = -1;
206
207 if (mount(tmpf1, "/proc/self/mountinfo", NULL, MS_BIND, 0) < 0) {
208 lxc_error("%s\n", "Could not overmount \"/proc/self/mountinfo\".");
209 goto non_test_error;
210 }
211
212 lxc_test_assert_abort(detect_ramfs_rootfs());
213
214 if (mount(tmpf2, "/proc/self/mountinfo", NULL, MS_BIND, 0) < 0) {
215 lxc_error("%s\n", "Could not overmount \"/proc/self/mountinfo\".");
216 goto non_test_error;
217 }
218
219 lxc_test_assert_abort(!detect_ramfs_rootfs());
220 fret = EXIT_SUCCESS;
221
222 non_test_error:
223 if (fp1)
224 fclose(fp1);
225 else if (fd1 > 0)
226 close(fd1);
227
228 if (fp2)
229 fclose(fp2);
230 else if (fd2 > 0)
231 close(fd2);
232
233 if (init_ns > 0) {
234 if (setns(init_ns, 0) < 0) {
235 lxc_error("Failed to switch back to initial mount namespace: %s.\n", strerror(errno));
236 fret = EXIT_FAILURE;
237 }
238 close(init_ns);
239 }
240
241 if (fret == EXIT_SUCCESS)
242 return;
243
244 exit(fret);
245 }
246
247 void test_lxc_safe_uint(void)
248 {
249 int ret;
250 unsigned int n;
251 char numstr[INTTYPE_TO_STRLEN(uint64_t)];
252
253 lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" -123", &n)));
254 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("-123", &n)));
255
256 ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)UINT_MAX);
257 if (ret < 0 || (size_t)ret >= sizeof(numstr))
258 exit(EXIT_FAILURE);
259
260 lxc_test_assert_abort((0 == lxc_safe_uint(numstr, &n)) && n == UINT_MAX);
261
262 ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)UINT_MAX + 1);
263 if (ret < 0 || (size_t)ret >= sizeof(numstr))
264 exit(EXIT_FAILURE);
265
266 lxc_test_assert_abort((-ERANGE == lxc_safe_uint(numstr, &n)));
267
268 lxc_test_assert_abort((0 == lxc_safe_uint("1234345", &n)) && n == 1234345);
269 lxc_test_assert_abort((0 == lxc_safe_uint(" 345", &n)) && n == 345);
270 lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" g345", &n)));
271 lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" 3g45", &n)));
272 lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" 345g", &n)));
273 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("g345", &n)));
274 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("3g45", &n)));
275 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("345g", &n)));
276 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("g345 ", &n)));
277 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("3g45 ", &n)));
278 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("345g ", &n)));
279 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("g", &n)));
280 lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" g345", &n)));
281 }
282
283 void test_lxc_safe_int(void)
284 {
285 int ret;
286 signed int n;
287 char numstr[INTTYPE_TO_STRLEN(uint64_t)];
288
289 ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)INT_MAX);
290 if (ret < 0 || (size_t)ret >= sizeof(numstr))
291 exit(EXIT_FAILURE);
292
293 lxc_test_assert_abort((0 == lxc_safe_int(numstr, &n)) && n == INT_MAX);
294
295 ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)INT_MAX + 1);
296 if (ret < 0 || (size_t)ret >= sizeof(numstr))
297 exit(EXIT_FAILURE);
298
299 lxc_test_assert_abort((-ERANGE == lxc_safe_int(numstr, &n)));
300
301 ret = snprintf(numstr, sizeof(numstr), "%" PRId64, (int64_t)INT_MIN);
302 if (ret < 0 || (size_t)ret >= sizeof(numstr))
303 exit(EXIT_FAILURE);
304
305 lxc_test_assert_abort((0 == lxc_safe_int(numstr, &n)) && n == INT_MIN);
306
307 ret = snprintf(numstr, sizeof(numstr), "%" PRId64, (int64_t)INT_MIN - 1);
308 if (ret < 0 || (size_t)ret >= sizeof(numstr))
309 exit(EXIT_FAILURE);
310
311 lxc_test_assert_abort((-ERANGE == lxc_safe_int(numstr, &n)));
312
313 lxc_test_assert_abort((0 == lxc_safe_int("1234345", &n)) && n == 1234345);
314 lxc_test_assert_abort((0 == lxc_safe_int(" 345", &n)) && n == 345);
315 lxc_test_assert_abort((0 == lxc_safe_int("-1234345", &n)) && n == -1234345);
316 lxc_test_assert_abort((0 == lxc_safe_int(" -345", &n)) && n == -345);
317 lxc_test_assert_abort((-EINVAL == lxc_safe_int(" g345", &n)));
318 lxc_test_assert_abort((-EINVAL == lxc_safe_int(" 3g45", &n)));
319 lxc_test_assert_abort((-EINVAL == lxc_safe_int(" 345g", &n)));
320 lxc_test_assert_abort((-EINVAL == lxc_safe_int("g345", &n)));
321 lxc_test_assert_abort((-EINVAL == lxc_safe_int("3g45", &n)));
322 lxc_test_assert_abort((-EINVAL == lxc_safe_int("345g", &n)));
323 lxc_test_assert_abort((-EINVAL == lxc_safe_int("g345 ", &n)));
324 lxc_test_assert_abort((-EINVAL == lxc_safe_int("3g45 ", &n)));
325 lxc_test_assert_abort((-EINVAL == lxc_safe_int("345g ", &n)));
326 lxc_test_assert_abort((-EINVAL == lxc_safe_int("g", &n)));
327 lxc_test_assert_abort((-EINVAL == lxc_safe_int(" g345", &n)));
328 }
329
330 void test_lxc_safe_long(void)
331 {
332 signed long int n;
333
334 lxc_test_assert_abort((0 == lxc_safe_long("1234345", &n)) && n == 1234345);
335 lxc_test_assert_abort((0 == lxc_safe_long(" 345", &n)) && n == 345);
336 lxc_test_assert_abort((0 == lxc_safe_long("-1234345", &n)) && n == -1234345);
337 lxc_test_assert_abort((0 == lxc_safe_long(" -345", &n)) && n == -345);
338 lxc_test_assert_abort((-EINVAL == lxc_safe_long(" g345", &n)));
339 lxc_test_assert_abort((-EINVAL == lxc_safe_long(" 3g45", &n)));
340 lxc_test_assert_abort((-EINVAL == lxc_safe_long(" 345g", &n)));
341 lxc_test_assert_abort((-EINVAL == lxc_safe_long("g345", &n)));
342 lxc_test_assert_abort((-EINVAL == lxc_safe_long("3g45", &n)));
343 lxc_test_assert_abort((-EINVAL == lxc_safe_long("345g", &n)));
344 lxc_test_assert_abort((-EINVAL == lxc_safe_long("g345 ", &n)));
345 lxc_test_assert_abort((-EINVAL == lxc_safe_long("3g45 ", &n)));
346 lxc_test_assert_abort((-EINVAL == lxc_safe_long("345g ", &n)));
347 lxc_test_assert_abort((-EINVAL == lxc_safe_long("g", &n)));
348 lxc_test_assert_abort((-EINVAL == lxc_safe_long(" g345", &n)));
349 }
350
351 void test_lxc_string_replace(void)
352 {
353 char *s;
354
355 s = lxc_string_replace("A", "A", "A");
356 lxc_test_assert_abort(strcmp(s, "A") == 0);
357 free(s);
358
359 s = lxc_string_replace("A", "AA", "A");
360 lxc_test_assert_abort(strcmp(s, "AA") == 0);
361 free(s);
362
363 s = lxc_string_replace("A", "AA", "BA");
364 lxc_test_assert_abort(strcmp(s, "BAA") == 0);
365 free(s);
366
367 s = lxc_string_replace("A", "AA", "BAB");
368 lxc_test_assert_abort(strcmp(s, "BAAB") == 0);
369 free(s);
370
371 s = lxc_string_replace("AA", "A", "AA");
372 lxc_test_assert_abort(strcmp(s, "A") == 0);
373 free(s);
374
375 s = lxc_string_replace("AA", "A", "BAA");
376 lxc_test_assert_abort(strcmp(s, "BA") == 0);
377 free(s);
378
379 s = lxc_string_replace("AA", "A", "BAAB");
380 lxc_test_assert_abort(strcmp(s, "BAB") == 0);
381 free(s);
382
383 s = lxc_string_replace("\"A\"A", "\"A\"", "B\"A\"AB");
384 lxc_test_assert_abort(strcmp(s, "B\"A\"B") == 0);
385 free(s);
386 }
387
388 void test_lxc_string_in_array(void)
389 {
390 lxc_test_assert_abort(lxc_string_in_array("", (const char *[]){"", NULL}));
391 lxc_test_assert_abort(!lxc_string_in_array("A", (const char *[]){"", NULL}));
392 lxc_test_assert_abort(!lxc_string_in_array("AAA", (const char *[]){"", "3472", "jshH", NULL}));
393
394 lxc_test_assert_abort(lxc_string_in_array("A", (const char *[]){"A", NULL}));
395 lxc_test_assert_abort(lxc_string_in_array("A", (const char *[]){"A", "B", "C", NULL}));
396 lxc_test_assert_abort(lxc_string_in_array("A", (const char *[]){"B", "A", "C", NULL}));
397
398 lxc_test_assert_abort(lxc_string_in_array("ABC", (const char *[]){"ASD", "ATR", "ABC", NULL}));
399 lxc_test_assert_abort(lxc_string_in_array("GHJ", (const char *[]){"AZIU", "WRT567B", "879C", "GHJ", "IUZ89", NULL}));
400 lxc_test_assert_abort(lxc_string_in_array("XYZ", (const char *[]){"BERTA", "ARQWE(9", "C8Zhkd", "7U", "XYZ", "UOIZ9", "=)()", NULL}));
401 }
402
403 void test_parse_byte_size_string(void)
404 {
405 int ret;
406 long long int n;
407
408 ret = parse_byte_size_string("0", &n);
409 if (ret < 0) {
410 lxc_error("%s\n", "Failed to parse \"0\"");
411 exit(EXIT_FAILURE);
412 }
413
414 if (n != 0) {
415 lxc_error("%s\n", "Failed to parse \"0\"");
416 exit(EXIT_FAILURE);
417 }
418
419 ret = parse_byte_size_string("1", &n);
420 if (ret < 0) {
421 lxc_error("%s\n", "Failed to parse \"1\"");
422 exit(EXIT_FAILURE);
423 }
424
425 if (n != 1) {
426 lxc_error("%s\n", "Failed to parse \"1\"");
427 exit(EXIT_FAILURE);
428 }
429
430 ret = parse_byte_size_string("1 ", &n);
431 if (ret == 0) {
432 lxc_error("%s\n", "Failed to parse \"1 \"");
433 exit(EXIT_FAILURE);
434 }
435
436 ret = parse_byte_size_string("1B", &n);
437 if (ret < 0) {
438 lxc_error("%s\n", "Failed to parse \"1B\"");
439 exit(EXIT_FAILURE);
440 }
441
442 if (n != 1) {
443 lxc_error("%s\n", "Failed to parse \"1B\"");
444 exit(EXIT_FAILURE);
445 }
446
447 ret = parse_byte_size_string("1kB", &n);
448 if (ret < 0) {
449 lxc_error("%s\n", "Failed to parse \"1kB\"");
450 exit(EXIT_FAILURE);
451 }
452
453 if (n != 1024) {
454 lxc_error("%s\n", "Failed to parse \"1kB\"");
455 exit(EXIT_FAILURE);
456 }
457
458 ret = parse_byte_size_string("1MB", &n);
459 if (ret < 0) {
460 lxc_error("%s\n", "Failed to parse \"1MB\"");
461 exit(EXIT_FAILURE);
462 }
463
464 if (n != 1048576) {
465 lxc_error("%s\n", "Failed to parse \"1MB\"");
466 exit(EXIT_FAILURE);
467 }
468
469 ret = parse_byte_size_string("1TB", &n);
470 if (ret == 0) {
471 lxc_error("%s\n", "Failed to parse \"1TB\"");
472 exit(EXIT_FAILURE);
473 }
474
475 ret = parse_byte_size_string("1 B", &n);
476 if (ret < 0) {
477 lxc_error("%s\n", "Failed to parse \"1 B\"");
478 exit(EXIT_FAILURE);
479 }
480
481 if (n != 1) {
482 lxc_error("%s\n", "Failed to parse \"1 B\"");
483 exit(EXIT_FAILURE);
484 }
485
486 ret = parse_byte_size_string("1 kB", &n);
487 if (ret < 0) {
488 lxc_error("%s\n", "Failed to parse \"1 kB\"");
489 exit(EXIT_FAILURE);
490 }
491
492 if (n != 1024) {
493 lxc_error("%s\n", "Failed to parse \"1 kB\"");
494 exit(EXIT_FAILURE);
495 }
496
497 ret = parse_byte_size_string("1 MB", &n);
498 if (ret < 0) {
499 lxc_error("%s\n", "Failed to parse \"1 MB\"");
500 exit(EXIT_FAILURE);
501 }
502
503 if (n != 1048576) {
504 lxc_error("%s\n", "Failed to parse \"1 MB\"");
505 exit(EXIT_FAILURE);
506 }
507
508 ret = parse_byte_size_string("1 TB", &n);
509 if (ret == 0) {
510 lxc_error("%s\n", "Failed to parse \"1 TB\"");
511 exit(EXIT_FAILURE);
512 }
513
514 ret = parse_byte_size_string("asdf", &n);
515 if (ret == 0) {
516 lxc_error("%s\n", "Failed to parse \"asdf\"");
517 exit(EXIT_FAILURE);
518 }
519 }
520
521 void test_lxc_config_net_is_hwaddr(void)
522 {
523 if (!lxc_config_net_is_hwaddr("lxc.net.0.hwaddr = 00:16:3e:04:65:b8\n"))
524 exit(EXIT_FAILURE);
525
526 if (lxc_config_net_is_hwaddr("lxc.net"))
527 exit(EXIT_FAILURE);
528
529 if (lxc_config_net_is_hwaddr("lxc.net."))
530 exit(EXIT_FAILURE);
531
532 if (lxc_config_net_is_hwaddr("lxc.net.0."))
533 exit(EXIT_FAILURE);
534 }
535
536 void test_task_blocks_signal(void)
537 {
538 int ret;
539 pid_t pid;
540
541 pid = fork();
542 if (pid < 0)
543 _exit(EXIT_FAILURE);
544
545 if (pid == 0) {
546 int i;
547 sigset_t mask;
548 int signals[] = {SIGBUS, SIGILL, SIGSEGV,
549 SIGWINCH, SIGQUIT, SIGUSR1,
550 SIGUSR2, SIGRTMIN + 3, SIGRTMIN + 4};
551
552 sigemptyset(&mask);
553
554 for (i = 0; (size_t)i < (sizeof(signals) / sizeof(signals[0])); i++) {
555 ret = sigaddset(&mask, signals[i]);
556 if (ret < 0)
557 _exit(EXIT_FAILURE);
558 }
559
560 ret = pthread_sigmask(SIG_BLOCK, &mask, NULL);
561 if (ret < 0) {
562 lxc_error("%s\n", "Failed to block signals");
563 _exit(EXIT_FAILURE);
564 }
565
566 for (i = 0; (size_t)i < (sizeof(signals) / sizeof(signals[0])); i++) {
567 if (!task_blocks_signal(getpid(), signals[i])) {
568 lxc_error("Failed to detect blocked signal "
569 "(idx = %d, signal number = %d)\n",
570 i, signals[i]);
571 _exit(EXIT_FAILURE);
572 }
573 }
574
575 if (task_blocks_signal(getpid(), SIGKILL)) {
576 lxc_error("%s\n",
577 "Falsely detected SIGKILL as blocked signal");
578 _exit(EXIT_FAILURE);
579 }
580
581 if (task_blocks_signal(getpid(), SIGSTOP)) {
582 lxc_error("%s\n",
583 "Falsely detected SIGSTOP as blocked signal");
584 _exit(EXIT_FAILURE);
585 }
586
587 _exit(EXIT_SUCCESS);
588 }
589
590 ret = wait_for_pid(pid);
591 if (ret < 0)
592 _exit(EXIT_FAILURE);
593
594 return;
595 }
596
597 void test_is_in_comm(void)
598 {
599 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
600 lxc_test_assert_abort(is_in_comm("fuzz-lxc-") == 0);
601 lxc_test_assert_abort(is_in_comm("lxc-test") == 1);
602 lxc_test_assert_abort(is_in_comm("") == 1);
603 #endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
604 }
605
606 int main(int argc, char *argv[])
607 {
608 test_lxc_string_replace();
609 test_lxc_string_in_array();
610 test_path_simplify();
611 test_detect_ramfs_rootfs();
612 test_lxc_safe_uint();
613 test_lxc_safe_int();
614 test_lxc_safe_long();
615 test_parse_byte_size_string();
616 test_lxc_config_net_is_hwaddr();
617 test_task_blocks_signal();
618 test_is_in_comm();
619
620 exit(EXIT_SUCCESS);
621 }