]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/lxc-test-utils.c
tests: Kill containers (don't wait for shutdown)
[mirror_lxc.git] / src / tests / lxc-test-utils.c
CommitLineData
bed0d08f
CB
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
1a514c15 24#define _GNU_SOURCE
cb9df6d4 25#define __STDC_FORMAT_MACROS
24b9970e
CB
26#include <errno.h>
27#include <fcntl.h>
cb9df6d4 28#include <inttypes.h>
57c7adc3 29#include <limits.h>
cb9df6d4 30#include <sched.h>
bed0d08f
CB
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
24b9970e
CB
34#include <unistd.h>
35#include <sys/mount.h>
36#include <sys/stat.h>
37#include <sys/types.h>
bed0d08f
CB
38
39#include "lxctest.h"
40#include "utils.h"
41
1a514c15
CB
42void test_lxc_deslashify(void)
43{
44 char *s = strdup("/A///B//C/D/E/");
45 if (!s)
46 exit(EXIT_FAILURE);
47 lxc_test_assert_abort(lxc_deslashify(&s));
48 lxc_test_assert_abort(strcmp(s, "/A/B/C/D/E") == 0);
49 free(s);
50
51 s = strdup("/A");
52 if (!s)
53 exit(EXIT_FAILURE);
54 lxc_test_assert_abort(lxc_deslashify(&s));
55 lxc_test_assert_abort(strcmp(s, "/A") == 0);
56 free(s);
57
58 s = strdup("");
59 if (!s)
60 exit(EXIT_FAILURE);
61 lxc_test_assert_abort(lxc_deslashify(&s));
62 lxc_test_assert_abort(strcmp(s, "") == 0);
63 free(s);
64
65 s = strdup("//");
66 if (!s)
67 exit(EXIT_FAILURE);
68 lxc_test_assert_abort(lxc_deslashify(&s));
69 lxc_test_assert_abort(strcmp(s, "/") == 0);
70 free(s);
71}
72
de3c491b 73/* /proc/int_as_str/ns/mnt\0 = (5 + 21 + 7 + 1) */
eab15c1e 74#define __MNTNS_LEN (5 + (LXC_NUMSTRLEN64) + 7 + 1)
24b9970e
CB
75void test_detect_ramfs_rootfs(void)
76{
77 size_t i;
78 int ret;
79 int fret = EXIT_FAILURE;
de3c491b 80 char path[__MNTNS_LEN];
24b9970e
CB
81 int init_ns = -1;
82 char tmpf1[] = "lxc-test-utils-XXXXXX";
83 char tmpf2[] = "lxc-test-utils-XXXXXX";
84 int fd1 = -1, fd2 = -1;
85 FILE *fp1 = NULL, *fp2 = NULL;
86 char *mountinfo[] = {
87 "18 24 0:17 / /sys rw,nosuid,nodev,noexec,relatime shared:7 - sysfs sysfs rw",
88 "19 24 0:4 / /proc rw,nosuid,nodev,noexec,relatime shared:13 - proc proc rw",
89 "20 24 0:6 / /dev rw,nosuid,relatime shared:2 - devtmpfs udev rw,size=4019884k,nr_inodes=1004971,mode=755",
90 "21 20 0:14 / /dev/pts rw,nosuid,noexec,relatime shared:3 - devpts devpts rw,gid=5,mode=620,ptmxmode=000",
91 "22 24 0:18 / /run rw,nosuid,noexec,relatime shared:5 - tmpfs tmpfs rw,size=807912k,mode=755",
92
93 /* This is what we care about. */
94 "24 0 8:2 / / rw - rootfs rootfs rw,size=1004396k,nr_inodes=251099",
95
96 "25 18 0:12 / /sys/kernel/security rw,nosuid,nodev,noexec,relatime shared:8 - securityfs securityfs rw",
97 "26 20 0:20 / /dev/shm rw,nosuid,nodev shared:4 - tmpfs tmpfs rw",
98 "27 22 0:21 / /run/lock rw,nosuid,nodev,noexec,relatime shared:6 - tmpfs tmpfs rw,size=5120k",
99 "28 18 0:22 / /sys/fs/cgroup ro,nosuid,nodev,noexec shared:9 - tmpfs tmpfs ro,mode=755",
100 "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",
101 "30 18 0:24 / /sys/fs/pstore rw,nosuid,nodev,noexec,relatime shared:11 - pstore pstore rw",
102 "31 18 0:25 / /sys/firmware/efi/efivars rw,nosuid,nodev,noexec,relatime shared:12 - efivarfs efivarfs rw",
103 "32 28 0:26 / /sys/fs/cgroup/cpu,cpuacct rw,nosuid,nodev,noexec,relatime shared:14 - cgroup cgroup rw,cpu,cpuacct",
104 "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",
105 "34 28 0:28 / /sys/fs/cgroup/blkio rw,nosuid,nodev,noexec,relatime shared:16 - cgroup cgroup rw,blkio",
106 "35 28 0:29 / /sys/fs/cgroup/freezer rw,nosuid,nodev,noexec,relatime shared:17 - cgroup cgroup rw,freezer",
107 "36 28 0:30 / /sys/fs/cgroup/memory rw,nosuid,nodev,noexec,relatime shared:18 - cgroup cgroup rw,memory",
108 "37 28 0:31 / /sys/fs/cgroup/hugetlb rw,nosuid,nodev,noexec,relatime shared:19 - cgroup cgroup rw,hugetlb",
109 "38 28 0:32 / /sys/fs/cgroup/cpuset rw,nosuid,nodev,noexec,relatime shared:20 - cgroup cgroup rw,cpuset",
110 "39 28 0:33 / /sys/fs/cgroup/devices rw,nosuid,nodev,noexec,relatime shared:21 - cgroup cgroup rw,devices",
111 "40 28 0:34 / /sys/fs/cgroup/pids rw,nosuid,nodev,noexec,relatime shared:22 - cgroup cgroup rw,pids",
112 "41 28 0:35 / /sys/fs/cgroup/perf_event rw,nosuid,nodev,noexec,relatime shared:23 - cgroup cgroup rw,perf_event",
113 "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",
114 "43 18 0:7 / /sys/kernel/debug rw,relatime shared:25 - debugfs debugfs rw",
115 "44 20 0:37 / /dev/hugepages rw,relatime shared:26 - hugetlbfs hugetlbfs rw",
116 "45 20 0:16 / /dev/mqueue rw,relatime shared:27 - mqueue mqueue rw",
117 "46 43 0:9 / /sys/kernel/debug/tracing rw,relatime shared:28 - tracefs tracefs rw",
118 "76 18 0:38 / /sys/fs/fuse/connections rw,relatime shared:29 - fusectl fusectl rw",
119 "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",
120 };
121
de3c491b
CB
122 ret = snprintf(path, __MNTNS_LEN, "/proc/self/ns/mnt");
123 if (ret < 0 || (size_t)ret >= __MNTNS_LEN) {
24b9970e
CB
124 lxc_error("%s\n", "Failed to create path with snprintf().");
125 goto non_test_error;
126 }
127
128 init_ns = open(path, O_RDONLY | O_CLOEXEC);
129 if (init_ns < 0) {
130 lxc_error("%s\n", "Failed to open initial mount namespace.");
131 goto non_test_error;
132 }
133
134 if (unshare(CLONE_NEWNS) < 0) {
135 lxc_error("%s\n", "Could not unshare mount namespace.");
136 close(init_ns);
137 init_ns = -1;
138 goto non_test_error;
139 }
140
141 if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, 0) < 0) {
142 lxc_error("Failed to remount / private: %s.\n", strerror(errno));
143 goto non_test_error;
144 }
145
146 fd1 = mkstemp(tmpf1);
147 if (fd1 < 0) {
148 lxc_error("%s\n", "Could not create temporary file.");
149 goto non_test_error;
150 }
151
152 fd2 = mkstemp(tmpf2);
153 if (fd2 < 0) {
154 lxc_error("%s\n", "Could not create temporary file.");
155 goto non_test_error;
156 }
157
158 fp1 = fdopen(fd1, "r+");
159 if (!fp1) {
160 lxc_error("%s\n", "Could not fdopen() temporary file.");
161 goto non_test_error;
162 }
163
164 fp2 = fdopen(fd2, "r+");
165 if (!fp2) {
166 lxc_error("%s\n", "Could not fdopen() temporary file.");
167 goto non_test_error;
168 }
169
170 /* Test if it correctly detects - rootfs rootfs */
171 for (i = 0; i < sizeof(mountinfo) / sizeof(mountinfo[0]); i++) {
172 if (fprintf(fp1, "%s\n", mountinfo[i]) < 0) {
173 lxc_error("Could not write \"%s\" to temporary file.", mountinfo[i]);
174 goto non_test_error;
175 }
176 }
177 fclose(fp1);
178 fp1 = NULL;
179
180 /* Test if it correctly fails to detect when no - rootfs rootfs */
181 for (i = 0; i < sizeof(mountinfo) / sizeof(mountinfo[0]); i++) {
182 if (strcmp(mountinfo[i], "24 0 8:2 / / rw - rootfs rootfs rw,size=1004396k,nr_inodes=251099") == 0)
183 continue;
184 if (fprintf(fp2, "%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(fp2);
190 fp2 = NULL;
191
192 if (mount(tmpf1, "/proc/self/mountinfo", NULL, MS_BIND, 0) < 0) {
193 lxc_error("%s\n", "Could not overmount \"/proc/self/mountinfo\".");
194 goto non_test_error;
195 }
196
197 lxc_test_assert_abort(detect_ramfs_rootfs());
198
199 if (mount(tmpf2, "/proc/self/mountinfo", NULL, MS_BIND, 0) < 0) {
200 lxc_error("%s\n", "Could not overmount \"/proc/self/mountinfo\".");
201 goto non_test_error;
202 }
203
204 lxc_test_assert_abort(!detect_ramfs_rootfs());
205 fret = EXIT_SUCCESS;
206
207non_test_error:
208 if (fp1)
209 fclose(fp1);
210 else if (fd1 > 0)
211 close(fd1);
212 if (fp2)
213 fclose(fp2);
214 else if (fd2 > 0)
215 close(fd2);
216
217 if (init_ns > 0) {
218 if (setns(init_ns, 0) < 0) {
219 lxc_error("Failed to switch back to initial mount namespace: %s.\n", strerror(errno));
220 fret = EXIT_FAILURE;
221 }
222 close(init_ns);
223 }
224 if (fret == EXIT_SUCCESS)
225 return;
226 exit(fret);
aa75cefc 227}
57c7adc3
CB
228
229void test_lxc_safe_uint(void)
230{
57c7adc3 231 unsigned int n;
57c7adc3
CB
232
233 lxc_test_assert_abort((0 == lxc_safe_uint("1234345", &n)) && n == 1234345);
234 lxc_test_assert_abort((0 == lxc_safe_uint(" 345", &n)) && n == 345);
235 lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" g345", &n)));
236 lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" 3g45", &n)));
237 lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" 345g", &n)));
238 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("g345", &n)));
239 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("3g45", &n)));
240 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("345g", &n)));
241 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("g345 ", &n)));
242 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("3g45 ", &n)));
243 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("345g ", &n)));
244 lxc_test_assert_abort((-EINVAL == lxc_safe_uint("g", &n)));
245 lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" g345", &n)));
24b9970e
CB
246}
247
aa75cefc
CB
248void test_lxc_safe_int(void)
249{
aa75cefc 250 signed int n;
aa75cefc
CB
251
252 lxc_test_assert_abort((0 == lxc_safe_int("1234345", &n)) && n == 1234345);
253 lxc_test_assert_abort((0 == lxc_safe_int(" 345", &n)) && n == 345);
254 lxc_test_assert_abort((0 == lxc_safe_int("-1234345", &n)) && n == -1234345);
255 lxc_test_assert_abort((0 == lxc_safe_int(" -345", &n)) && n == -345);
256 lxc_test_assert_abort((-EINVAL == lxc_safe_int(" g345", &n)));
257 lxc_test_assert_abort((-EINVAL == lxc_safe_int(" 3g45", &n)));
258 lxc_test_assert_abort((-EINVAL == lxc_safe_int(" 345g", &n)));
259 lxc_test_assert_abort((-EINVAL == lxc_safe_int("g345", &n)));
260 lxc_test_assert_abort((-EINVAL == lxc_safe_int("3g45", &n)));
261 lxc_test_assert_abort((-EINVAL == lxc_safe_int("345g", &n)));
262 lxc_test_assert_abort((-EINVAL == lxc_safe_int("g345 ", &n)));
263 lxc_test_assert_abort((-EINVAL == lxc_safe_int("3g45 ", &n)));
264 lxc_test_assert_abort((-EINVAL == lxc_safe_int("345g ", &n)));
265 lxc_test_assert_abort((-EINVAL == lxc_safe_int("g", &n)));
266 lxc_test_assert_abort((-EINVAL == lxc_safe_int(" g345", &n)));
aa75cefc
CB
267}
268
cb9df6d4
CB
269void test_lxc_safe_long(void)
270{
cb9df6d4 271 signed long int n;
cb9df6d4
CB
272
273 lxc_test_assert_abort((0 == lxc_safe_long("1234345", &n)) && n == 1234345);
274 lxc_test_assert_abort((0 == lxc_safe_long(" 345", &n)) && n == 345);
275 lxc_test_assert_abort((0 == lxc_safe_long("-1234345", &n)) && n == -1234345);
276 lxc_test_assert_abort((0 == lxc_safe_long(" -345", &n)) && n == -345);
277 lxc_test_assert_abort((-EINVAL == lxc_safe_long(" g345", &n)));
278 lxc_test_assert_abort((-EINVAL == lxc_safe_long(" 3g45", &n)));
279 lxc_test_assert_abort((-EINVAL == lxc_safe_long(" 345g", &n)));
280 lxc_test_assert_abort((-EINVAL == lxc_safe_long("g345", &n)));
281 lxc_test_assert_abort((-EINVAL == lxc_safe_long("3g45", &n)));
282 lxc_test_assert_abort((-EINVAL == lxc_safe_long("345g", &n)));
283 lxc_test_assert_abort((-EINVAL == lxc_safe_long("g345 ", &n)));
284 lxc_test_assert_abort((-EINVAL == lxc_safe_long("3g45 ", &n)));
285 lxc_test_assert_abort((-EINVAL == lxc_safe_long("345g ", &n)));
286 lxc_test_assert_abort((-EINVAL == lxc_safe_long("g", &n)));
287 lxc_test_assert_abort((-EINVAL == lxc_safe_long(" g345", &n)));
cb9df6d4
CB
288}
289
29c57e4e 290void test_lxc_string_replace(void)
bed0d08f
CB
291{
292 char *s;
293
294 s = lxc_string_replace("A", "A", "A");
295 lxc_test_assert_abort(strcmp(s, "A") == 0);
296 free(s);
297
298 s = lxc_string_replace("A", "AA", "A");
299 lxc_test_assert_abort(strcmp(s, "AA") == 0);
300 free(s);
301
302 s = lxc_string_replace("A", "AA", "BA");
303 lxc_test_assert_abort(strcmp(s, "BAA") == 0);
304 free(s);
305
306 s = lxc_string_replace("A", "AA", "BAB");
307 lxc_test_assert_abort(strcmp(s, "BAAB") == 0);
308 free(s);
309
310 s = lxc_string_replace("AA", "A", "AA");
311 lxc_test_assert_abort(strcmp(s, "A") == 0);
312 free(s);
313
314 s = lxc_string_replace("AA", "A", "BAA");
315 lxc_test_assert_abort(strcmp(s, "BA") == 0);
316 free(s);
317
318 s = lxc_string_replace("AA", "A", "BAAB");
319 lxc_test_assert_abort(strcmp(s, "BAB") == 0);
320 free(s);
321
322 s = lxc_string_replace("\"A\"A", "\"A\"", "B\"A\"AB");
323 lxc_test_assert_abort(strcmp(s, "B\"A\"B") == 0);
324 free(s);
325}
326
29c57e4e
CB
327void test_lxc_string_in_array(void)
328{
329 lxc_test_assert_abort(lxc_string_in_array("", (const char *[]){"", NULL}));
330 lxc_test_assert_abort(!lxc_string_in_array("A", (const char *[]){"", NULL}));
331 lxc_test_assert_abort(!lxc_string_in_array("AAA", (const char *[]){"", "3472", "jshH", NULL}));
332
333 lxc_test_assert_abort(lxc_string_in_array("A", (const char *[]){"A", NULL}));
334 lxc_test_assert_abort(lxc_string_in_array("A", (const char *[]){"A", "B", "C", NULL}));
335 lxc_test_assert_abort(lxc_string_in_array("A", (const char *[]){"B", "A", "C", NULL}));
336
337 lxc_test_assert_abort(lxc_string_in_array("ABC", (const char *[]){"ASD", "ATR", "ABC", NULL}));
338 lxc_test_assert_abort(lxc_string_in_array("GHJ", (const char *[]){"AZIU", "WRT567B", "879C", "GHJ", "IUZ89", NULL}));
339 lxc_test_assert_abort(lxc_string_in_array("XYZ", (const char *[]){"BERTA", "ARQWE(9", "C8Zhkd", "7U", "XYZ", "UOIZ9", "=)()", NULL}));
340}
341
bed0d08f
CB
342int main(int argc, char *argv[])
343{
29c57e4e
CB
344 test_lxc_string_replace();
345 test_lxc_string_in_array();
1a514c15 346 test_lxc_deslashify();
24b9970e 347 test_detect_ramfs_rootfs();
57c7adc3 348 test_lxc_safe_uint();
aa75cefc 349 test_lxc_safe_int();
cb9df6d4 350 test_lxc_safe_long();
bed0d08f
CB
351
352 exit(EXIT_SUCCESS);
353}