]>
git.proxmox.com Git - mirror_lxc.git/blob - src/tests/lxc-test-utils.c
e23ac729c960a81646243744e53cfe536446f63f
2 * lxc: linux Container library
4 * Copyright © 2016 Canonical Ltd.
7 * Christian Brauner <christian.brauner@mailbox.org>
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.
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.
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
36 #include <sys/mount.h>
38 #include <sys/types.h>
41 #include "confile_utils.h"
46 void test_path_simplify(void)
48 char *s
= "/A///B//C/D/E/";
55 lxc_test_assert_abort(strcmp(t
, "/A/B/C/D/E") == 0);
64 lxc_test_assert_abort(strcmp(t
, "/A") == 0);
72 lxc_test_assert_abort(strcmp(t
, "") == 0);
81 lxc_test_assert_abort(strcmp(t
, "/") == 0);
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)
91 int fret
= EXIT_FAILURE
;
92 char path
[__MNTNS_LEN
];
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
;
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",
105 /* This is what we care about. */
106 "24 0 8:2 / / rw - rootfs rootfs rw,size=1004396k,nr_inodes=251099",
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",
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().");
140 init_ns
= open(path
, O_RDONLY
| O_CLOEXEC
);
142 lxc_error("%s\n", "Failed to open initial mount namespace.");
146 if (unshare(CLONE_NEWNS
) < 0) {
147 lxc_error("%s\n", "Could not unshare mount namespace.");
153 if (mount(NULL
, "/", NULL
, MS_REC
| MS_PRIVATE
, 0) < 0) {
154 lxc_error("Failed to remount / private: %s.\n", strerror(errno
));
158 fd1
= lxc_make_tmpfile(tmpf1
, false);
160 lxc_error("%s\n", "Could not create temporary file.");
164 fd2
= lxc_make_tmpfile(tmpf2
, false);
166 lxc_error("%s\n", "Could not create temporary file.");
170 fp1
= fdopen(fd1
, "r+");
172 lxc_error("%s\n", "Could not fdopen() temporary file.");
176 fp2
= fdopen(fd2
, "r+");
178 lxc_error("%s\n", "Could not fdopen() temporary file.");
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
]);
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)
198 if (fprintf(fp2
, "%s\n", mountinfo
[i
]) < 0) {
199 lxc_error("Could not write \"%s\" to temporary file.", mountinfo
[i
]);
207 if (mount(tmpf1
, "/proc/self/mountinfo", NULL
, MS_BIND
, 0) < 0) {
208 lxc_error("%s\n", "Could not overmount \"/proc/self/mountinfo\".");
212 lxc_test_assert_abort(detect_ramfs_rootfs());
214 if (mount(tmpf2
, "/proc/self/mountinfo", NULL
, MS_BIND
, 0) < 0) {
215 lxc_error("%s\n", "Could not overmount \"/proc/self/mountinfo\".");
219 lxc_test_assert_abort(!detect_ramfs_rootfs());
234 if (setns(init_ns
, 0) < 0) {
235 lxc_error("Failed to switch back to initial mount namespace: %s.\n", strerror(errno
));
241 if (fret
== EXIT_SUCCESS
)
247 void test_lxc_safe_uint(void)
251 char numstr
[INTTYPE_TO_STRLEN(uint64_t)];
253 lxc_test_assert_abort((-EINVAL
== lxc_safe_uint(" -123", &n
)));
254 lxc_test_assert_abort((-EINVAL
== lxc_safe_uint("-123", &n
)));
256 ret
= snprintf(numstr
, sizeof(numstr
), "%" PRIu64
, (uint64_t)UINT_MAX
);
257 if (ret
< 0 || (size_t)ret
>= sizeof(numstr
))
260 lxc_test_assert_abort((0 == lxc_safe_uint(numstr
, &n
)) && n
== UINT_MAX
);
262 ret
= snprintf(numstr
, sizeof(numstr
), "%" PRIu64
, (uint64_t)UINT_MAX
+ 1);
263 if (ret
< 0 || (size_t)ret
>= sizeof(numstr
))
266 lxc_test_assert_abort((-ERANGE
== lxc_safe_uint(numstr
, &n
)));
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
)));
283 void test_lxc_safe_int(void)
287 char numstr
[INTTYPE_TO_STRLEN(uint64_t)];
289 ret
= snprintf(numstr
, sizeof(numstr
), "%" PRIu64
, (uint64_t)INT_MAX
);
290 if (ret
< 0 || (size_t)ret
>= sizeof(numstr
))
293 lxc_test_assert_abort((0 == lxc_safe_int(numstr
, &n
)) && n
== INT_MAX
);
295 ret
= snprintf(numstr
, sizeof(numstr
), "%" PRIu64
, (uint64_t)INT_MAX
+ 1);
296 if (ret
< 0 || (size_t)ret
>= sizeof(numstr
))
299 lxc_test_assert_abort((-ERANGE
== lxc_safe_int(numstr
, &n
)));
301 ret
= snprintf(numstr
, sizeof(numstr
), "%" PRId64
, (int64_t)INT_MIN
);
302 if (ret
< 0 || (size_t)ret
>= sizeof(numstr
))
305 lxc_test_assert_abort((0 == lxc_safe_int(numstr
, &n
)) && n
== INT_MIN
);
307 ret
= snprintf(numstr
, sizeof(numstr
), "%" PRId64
, (int64_t)INT_MIN
- 1);
308 if (ret
< 0 || (size_t)ret
>= sizeof(numstr
))
311 lxc_test_assert_abort((-ERANGE
== lxc_safe_int(numstr
, &n
)));
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
)));
330 void test_lxc_safe_long(void)
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
)));
351 void test_lxc_string_replace(void)
355 s
= lxc_string_replace("A", "A", "A");
356 lxc_test_assert_abort(strcmp(s
, "A") == 0);
359 s
= lxc_string_replace("A", "AA", "A");
360 lxc_test_assert_abort(strcmp(s
, "AA") == 0);
363 s
= lxc_string_replace("A", "AA", "BA");
364 lxc_test_assert_abort(strcmp(s
, "BAA") == 0);
367 s
= lxc_string_replace("A", "AA", "BAB");
368 lxc_test_assert_abort(strcmp(s
, "BAAB") == 0);
371 s
= lxc_string_replace("AA", "A", "AA");
372 lxc_test_assert_abort(strcmp(s
, "A") == 0);
375 s
= lxc_string_replace("AA", "A", "BAA");
376 lxc_test_assert_abort(strcmp(s
, "BA") == 0);
379 s
= lxc_string_replace("AA", "A", "BAAB");
380 lxc_test_assert_abort(strcmp(s
, "BAB") == 0);
383 s
= lxc_string_replace("\"A\"A", "\"A\"", "B\"A\"AB");
384 lxc_test_assert_abort(strcmp(s
, "B\"A\"B") == 0);
388 void test_lxc_string_in_array(void)
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
}));
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
}));
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
}));
403 void test_parse_byte_size_string(void)
408 ret
= parse_byte_size_string("0", &n
);
410 lxc_error("%s\n", "Failed to parse \"0\"");
415 lxc_error("%s\n", "Failed to parse \"0\"");
419 ret
= parse_byte_size_string("1", &n
);
421 lxc_error("%s\n", "Failed to parse \"1\"");
426 lxc_error("%s\n", "Failed to parse \"1\"");
430 ret
= parse_byte_size_string("1 ", &n
);
432 lxc_error("%s\n", "Failed to parse \"1 \"");
436 ret
= parse_byte_size_string("1B", &n
);
438 lxc_error("%s\n", "Failed to parse \"1B\"");
443 lxc_error("%s\n", "Failed to parse \"1B\"");
447 ret
= parse_byte_size_string("1kB", &n
);
449 lxc_error("%s\n", "Failed to parse \"1kB\"");
454 lxc_error("%s\n", "Failed to parse \"1kB\"");
458 ret
= parse_byte_size_string("1MB", &n
);
460 lxc_error("%s\n", "Failed to parse \"1MB\"");
465 lxc_error("%s\n", "Failed to parse \"1MB\"");
469 ret
= parse_byte_size_string("1TB", &n
);
471 lxc_error("%s\n", "Failed to parse \"1TB\"");
475 ret
= parse_byte_size_string("1 B", &n
);
477 lxc_error("%s\n", "Failed to parse \"1 B\"");
482 lxc_error("%s\n", "Failed to parse \"1 B\"");
486 ret
= parse_byte_size_string("1 kB", &n
);
488 lxc_error("%s\n", "Failed to parse \"1 kB\"");
493 lxc_error("%s\n", "Failed to parse \"1 kB\"");
497 ret
= parse_byte_size_string("1 MB", &n
);
499 lxc_error("%s\n", "Failed to parse \"1 MB\"");
504 lxc_error("%s\n", "Failed to parse \"1 MB\"");
508 ret
= parse_byte_size_string("1 TB", &n
);
510 lxc_error("%s\n", "Failed to parse \"1 TB\"");
514 ret
= parse_byte_size_string("asdf", &n
);
516 lxc_error("%s\n", "Failed to parse \"asdf\"");
521 void test_lxc_config_net_is_hwaddr(void)
523 if (!lxc_config_net_is_hwaddr("lxc.net.0.hwaddr = 00:16:3e:04:65:b8\n"))
526 if (lxc_config_net_is_hwaddr("lxc.net"))
529 if (lxc_config_net_is_hwaddr("lxc.net."))
532 if (lxc_config_net_is_hwaddr("lxc.net.0."))
536 void test_task_blocks_signal(void)
548 int signals
[] = {SIGBUS
, SIGILL
, SIGSEGV
,
549 SIGWINCH
, SIGQUIT
, SIGUSR1
,
550 SIGUSR2
, SIGRTMIN
+ 3, SIGRTMIN
+ 4};
554 for (i
= 0; (size_t)i
< (sizeof(signals
) / sizeof(signals
[0])); i
++) {
555 ret
= sigaddset(&mask
, signals
[i
]);
560 ret
= pthread_sigmask(SIG_BLOCK
, &mask
, NULL
);
562 lxc_error("%s\n", "Failed to block signals");
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",
575 if (task_blocks_signal(getpid(), SIGKILL
)) {
577 "Falsely detected SIGKILL as blocked signal");
581 if (task_blocks_signal(getpid(), SIGSTOP
)) {
583 "Falsely detected SIGSTOP as blocked signal");
590 ret
= wait_for_pid(pid
);
597 void test_is_in_comm(void)
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 */
606 int main(int argc
, char *argv
[])
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();
614 test_lxc_safe_long();
615 test_parse_byte_size_string();
616 test_lxc_config_net_is_hwaddr();
617 test_task_blocks_signal();