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