]>
Commit | Line | Data |
---|---|---|
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> |
573ad77f | 30 | #include <pthread.h> |
cb9df6d4 | 31 | #include <sched.h> |
573ad77f | 32 | #include <signal.h> |
bed0d08f CB |
33 | #include <stdio.h> |
34 | #include <stdlib.h> | |
35 | #include <string.h> | |
24b9970e CB |
36 | #include <sys/mount.h> |
37 | #include <sys/stat.h> | |
38 | #include <sys/types.h> | |
573ad77f | 39 | #include <unistd.h> |
bed0d08f | 40 | |
d0afbad9 | 41 | #include "confile_utils.h" |
bed0d08f | 42 | #include "lxctest.h" |
84226232 | 43 | #include "macro.h" |
bed0d08f CB |
44 | #include "utils.h" |
45 | ||
1a514c15 CB |
46 | void test_lxc_deslashify(void) |
47 | { | |
eda0afd4 CB |
48 | char *s = "/A///B//C/D/E/"; |
49 | char *t; | |
50 | ||
51 | t = lxc_deslashify(s); | |
52 | if (!t) | |
1a514c15 | 53 | exit(EXIT_FAILURE); |
b477d830 | 54 | |
eda0afd4 CB |
55 | lxc_test_assert_abort(strcmp(t, "/A/B/C/D/E") == 0); |
56 | free(t); | |
57 | ||
58 | s = "/A"; | |
1a514c15 | 59 | |
eda0afd4 CB |
60 | t = lxc_deslashify(s); |
61 | if (!t) | |
1a514c15 | 62 | exit(EXIT_FAILURE); |
b477d830 | 63 | |
eda0afd4 CB |
64 | lxc_test_assert_abort(strcmp(t, "/A") == 0); |
65 | free(t); | |
1a514c15 | 66 | |
eda0afd4 CB |
67 | s = ""; |
68 | t = lxc_deslashify(s); | |
69 | if (!t) | |
1a514c15 | 70 | exit(EXIT_FAILURE); |
b477d830 | 71 | |
eda0afd4 CB |
72 | lxc_test_assert_abort(strcmp(t, "") == 0); |
73 | free(t); | |
74 | ||
75 | s = "//"; | |
1a514c15 | 76 | |
eda0afd4 CB |
77 | t = lxc_deslashify(s); |
78 | if (!t) | |
1a514c15 | 79 | exit(EXIT_FAILURE); |
b477d830 | 80 | |
eda0afd4 CB |
81 | lxc_test_assert_abort(strcmp(t, "/") == 0); |
82 | free(t); | |
1a514c15 CB |
83 | } |
84 | ||
de3c491b | 85 | /* /proc/int_as_str/ns/mnt\0 = (5 + 21 + 7 + 1) */ |
84226232 | 86 | #define __MNTNS_LEN (5 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1) |
24b9970e CB |
87 | void test_detect_ramfs_rootfs(void) |
88 | { | |
89 | size_t i; | |
90 | int ret; | |
91 | int fret = EXIT_FAILURE; | |
de3c491b | 92 | char path[__MNTNS_LEN]; |
24b9970e CB |
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 | ||
de3c491b CB |
134 | ret = snprintf(path, __MNTNS_LEN, "/proc/self/ns/mnt"); |
135 | if (ret < 0 || (size_t)ret >= __MNTNS_LEN) { | |
24b9970e CB |
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 | ||
0311439f | 158 | fd1 = lxc_make_tmpfile(tmpf1, false); |
24b9970e CB |
159 | if (fd1 < 0) { |
160 | lxc_error("%s\n", "Could not create temporary file."); | |
161 | goto non_test_error; | |
162 | } | |
163 | ||
0311439f | 164 | fd2 = lxc_make_tmpfile(tmpf2, false); |
24b9970e CB |
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; | |
060adc61 | 191 | fd1 = -1; |
24b9970e CB |
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; | |
b477d830 | 197 | |
24b9970e CB |
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; | |
060adc61 | 205 | fd2 = -1; |
24b9970e CB |
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); | |
b477d830 | 227 | |
24b9970e CB |
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 | } | |
b477d830 | 240 | |
24b9970e CB |
241 | if (fret == EXIT_SUCCESS) |
242 | return; | |
b477d830 | 243 | |
24b9970e | 244 | exit(fret); |
aa75cefc | 245 | } |
57c7adc3 CB |
246 | |
247 | void test_lxc_safe_uint(void) | |
248 | { | |
15fe2189 | 249 | int ret; |
57c7adc3 | 250 | unsigned int n; |
84226232 | 251 | char numstr[INTTYPE_TO_STRLEN(uint64_t)]; |
15fe2189 CB |
252 | |
253 | lxc_test_assert_abort((-EINVAL == lxc_safe_uint(" -123", &n))); | |
254 | lxc_test_assert_abort((-EINVAL == lxc_safe_uint("-123", &n))); | |
255 | ||
979a0d93 CB |
256 | ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)UINT_MAX); |
257 | if (ret < 0 || ret >= sizeof(numstr)) | |
15fe2189 | 258 | exit(EXIT_FAILURE); |
b477d830 | 259 | |
15fe2189 CB |
260 | lxc_test_assert_abort((0 == lxc_safe_uint(numstr, &n)) && n == UINT_MAX); |
261 | ||
979a0d93 CB |
262 | ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)UINT_MAX + 1); |
263 | if (ret < 0 || ret >= sizeof(numstr)) | |
15fe2189 | 264 | exit(EXIT_FAILURE); |
b477d830 | 265 | |
15fe2189 | 266 | lxc_test_assert_abort((-ERANGE == lxc_safe_uint(numstr, &n))); |
57c7adc3 CB |
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))); | |
24b9970e CB |
281 | } |
282 | ||
aa75cefc CB |
283 | void test_lxc_safe_int(void) |
284 | { | |
15fe2189 | 285 | int ret; |
aa75cefc | 286 | signed int n; |
84226232 | 287 | char numstr[INTTYPE_TO_STRLEN(uint64_t)]; |
15fe2189 | 288 | |
979a0d93 CB |
289 | ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)INT_MAX); |
290 | if (ret < 0 || ret >= sizeof(numstr)) | |
15fe2189 | 291 | exit(EXIT_FAILURE); |
b477d830 | 292 | |
15fe2189 CB |
293 | lxc_test_assert_abort((0 == lxc_safe_int(numstr, &n)) && n == INT_MAX); |
294 | ||
979a0d93 CB |
295 | ret = snprintf(numstr, sizeof(numstr), "%" PRIu64, (uint64_t)INT_MAX + 1); |
296 | if (ret < 0 || ret >= sizeof(numstr)) | |
15fe2189 | 297 | exit(EXIT_FAILURE); |
b477d830 | 298 | |
15fe2189 CB |
299 | lxc_test_assert_abort((-ERANGE == lxc_safe_int(numstr, &n))); |
300 | ||
979a0d93 CB |
301 | ret = snprintf(numstr, sizeof(numstr), "%" PRId64, (int64_t)INT_MIN); |
302 | if (ret < 0 || ret >= sizeof(numstr)) | |
15fe2189 | 303 | exit(EXIT_FAILURE); |
b477d830 | 304 | |
15fe2189 CB |
305 | lxc_test_assert_abort((0 == lxc_safe_int(numstr, &n)) && n == INT_MIN); |
306 | ||
979a0d93 CB |
307 | ret = snprintf(numstr, sizeof(numstr), "%" PRId64, (int64_t)INT_MIN - 1); |
308 | if (ret < 0 || ret >= sizeof(numstr)) | |
15fe2189 | 309 | exit(EXIT_FAILURE); |
b477d830 | 310 | |
15fe2189 | 311 | lxc_test_assert_abort((-ERANGE == lxc_safe_int(numstr, &n))); |
aa75cefc CB |
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))); | |
aa75cefc CB |
328 | } |
329 | ||
cb9df6d4 CB |
330 | void test_lxc_safe_long(void) |
331 | { | |
cb9df6d4 | 332 | signed long int n; |
cb9df6d4 CB |
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))); | |
cb9df6d4 CB |
349 | } |
350 | ||
29c57e4e | 351 | void test_lxc_string_replace(void) |
bed0d08f CB |
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 | ||
29c57e4e CB |
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 | ||
e3db0162 CB |
403 | void test_parse_byte_size_string(void) |
404 | { | |
405 | int ret; | |
4c5479d2 | 406 | long long int n; |
e3db0162 CB |
407 | |
408 | ret = parse_byte_size_string("0", &n); | |
99eafdd4 CB |
409 | if (ret < 0) { |
410 | lxc_error("%s\n", "Failed to parse \"0\""); | |
e3db0162 | 411 | exit(EXIT_FAILURE); |
99eafdd4 | 412 | } |
b477d830 | 413 | |
99eafdd4 CB |
414 | if (n != 0) { |
415 | lxc_error("%s\n", "Failed to parse \"0\""); | |
e3db0162 | 416 | exit(EXIT_FAILURE); |
99eafdd4 | 417 | } |
e3db0162 CB |
418 | |
419 | ret = parse_byte_size_string("1", &n); | |
99eafdd4 CB |
420 | if (ret < 0) { |
421 | lxc_error("%s\n", "Failed to parse \"1\""); | |
e3db0162 | 422 | exit(EXIT_FAILURE); |
99eafdd4 | 423 | } |
b477d830 | 424 | |
99eafdd4 CB |
425 | if (n != 1) { |
426 | lxc_error("%s\n", "Failed to parse \"1\""); | |
e3db0162 | 427 | exit(EXIT_FAILURE); |
99eafdd4 | 428 | } |
e3db0162 CB |
429 | |
430 | ret = parse_byte_size_string("1 ", &n); | |
99eafdd4 CB |
431 | if (ret == 0) { |
432 | lxc_error("%s\n", "Failed to parse \"1 \""); | |
e3db0162 | 433 | exit(EXIT_FAILURE); |
99eafdd4 | 434 | } |
e3db0162 CB |
435 | |
436 | ret = parse_byte_size_string("1B", &n); | |
99eafdd4 CB |
437 | if (ret < 0) { |
438 | lxc_error("%s\n", "Failed to parse \"1B\""); | |
e3db0162 | 439 | exit(EXIT_FAILURE); |
99eafdd4 | 440 | } |
b477d830 | 441 | |
99eafdd4 CB |
442 | if (n != 1) { |
443 | lxc_error("%s\n", "Failed to parse \"1B\""); | |
e3db0162 | 444 | exit(EXIT_FAILURE); |
99eafdd4 | 445 | } |
e3db0162 CB |
446 | |
447 | ret = parse_byte_size_string("1kB", &n); | |
99eafdd4 CB |
448 | if (ret < 0) { |
449 | lxc_error("%s\n", "Failed to parse \"1kB\""); | |
e3db0162 | 450 | exit(EXIT_FAILURE); |
99eafdd4 | 451 | } |
b477d830 | 452 | |
99eafdd4 CB |
453 | if (n != 1024) { |
454 | lxc_error("%s\n", "Failed to parse \"1kB\""); | |
e3db0162 | 455 | exit(EXIT_FAILURE); |
99eafdd4 | 456 | } |
e3db0162 CB |
457 | |
458 | ret = parse_byte_size_string("1MB", &n); | |
99eafdd4 CB |
459 | if (ret < 0) { |
460 | lxc_error("%s\n", "Failed to parse \"1MB\""); | |
e3db0162 | 461 | exit(EXIT_FAILURE); |
99eafdd4 | 462 | } |
b477d830 | 463 | |
99eafdd4 CB |
464 | if (n != 1048576) { |
465 | lxc_error("%s\n", "Failed to parse \"1MB\""); | |
e3db0162 | 466 | exit(EXIT_FAILURE); |
99eafdd4 | 467 | } |
e3db0162 | 468 | |
e3db0162 | 469 | ret = parse_byte_size_string("1TB", &n); |
99eafdd4 CB |
470 | if (ret == 0) { |
471 | lxc_error("%s\n", "Failed to parse \"1TB\""); | |
e3db0162 | 472 | exit(EXIT_FAILURE); |
99eafdd4 | 473 | } |
e3db0162 CB |
474 | |
475 | ret = parse_byte_size_string("1 B", &n); | |
99eafdd4 CB |
476 | if (ret < 0) { |
477 | lxc_error("%s\n", "Failed to parse \"1 B\""); | |
e3db0162 | 478 | exit(EXIT_FAILURE); |
99eafdd4 | 479 | } |
b477d830 | 480 | |
99eafdd4 CB |
481 | if (n != 1) { |
482 | lxc_error("%s\n", "Failed to parse \"1 B\""); | |
e3db0162 | 483 | exit(EXIT_FAILURE); |
99eafdd4 | 484 | } |
e3db0162 CB |
485 | |
486 | ret = parse_byte_size_string("1 kB", &n); | |
99eafdd4 CB |
487 | if (ret < 0) { |
488 | lxc_error("%s\n", "Failed to parse \"1 kB\""); | |
e3db0162 | 489 | exit(EXIT_FAILURE); |
99eafdd4 | 490 | } |
b477d830 | 491 | |
99eafdd4 CB |
492 | if (n != 1024) { |
493 | lxc_error("%s\n", "Failed to parse \"1 kB\""); | |
e3db0162 | 494 | exit(EXIT_FAILURE); |
99eafdd4 | 495 | } |
e3db0162 CB |
496 | |
497 | ret = parse_byte_size_string("1 MB", &n); | |
99eafdd4 CB |
498 | if (ret < 0) { |
499 | lxc_error("%s\n", "Failed to parse \"1 MB\""); | |
e3db0162 | 500 | exit(EXIT_FAILURE); |
99eafdd4 | 501 | } |
b477d830 | 502 | |
99eafdd4 CB |
503 | if (n != 1048576) { |
504 | lxc_error("%s\n", "Failed to parse \"1 MB\""); | |
e3db0162 | 505 | exit(EXIT_FAILURE); |
99eafdd4 | 506 | } |
e3db0162 | 507 | |
e3db0162 | 508 | ret = parse_byte_size_string("1 TB", &n); |
99eafdd4 CB |
509 | if (ret == 0) { |
510 | lxc_error("%s\n", "Failed to parse \"1 TB\""); | |
e3db0162 | 511 | exit(EXIT_FAILURE); |
99eafdd4 | 512 | } |
e3db0162 CB |
513 | |
514 | ret = parse_byte_size_string("asdf", &n); | |
99eafdd4 CB |
515 | if (ret == 0) { |
516 | lxc_error("%s\n", "Failed to parse \"asdf\""); | |
e3db0162 | 517 | exit(EXIT_FAILURE); |
99eafdd4 | 518 | } |
e3db0162 CB |
519 | } |
520 | ||
3db41a6c | 521 | void test_lxc_config_net_is_hwaddr(void) |
44047b2b | 522 | { |
3db41a6c | 523 | if (!lxc_config_net_is_hwaddr("lxc.net.0.hwaddr = 00:16:3e:04:65:b8\n")) |
44047b2b | 524 | exit(EXIT_FAILURE); |
44047b2b | 525 | |
3db41a6c | 526 | if (lxc_config_net_is_hwaddr("lxc.net")) |
44047b2b | 527 | exit(EXIT_FAILURE); |
b477d830 | 528 | |
3db41a6c | 529 | if (lxc_config_net_is_hwaddr("lxc.net.")) |
44047b2b | 530 | exit(EXIT_FAILURE); |
b477d830 | 531 | |
3db41a6c | 532 | if (lxc_config_net_is_hwaddr("lxc.net.0.")) |
44047b2b | 533 | exit(EXIT_FAILURE); |
44047b2b FA |
534 | } |
535 | ||
573ad77f CB |
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; 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; 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 | ||
bed0d08f CB |
597 | int main(int argc, char *argv[]) |
598 | { | |
29c57e4e CB |
599 | test_lxc_string_replace(); |
600 | test_lxc_string_in_array(); | |
1a514c15 | 601 | test_lxc_deslashify(); |
24b9970e | 602 | test_detect_ramfs_rootfs(); |
57c7adc3 | 603 | test_lxc_safe_uint(); |
aa75cefc | 604 | test_lxc_safe_int(); |
cb9df6d4 | 605 | test_lxc_safe_long(); |
e3db0162 | 606 | test_parse_byte_size_string(); |
3db41a6c | 607 | test_lxc_config_net_is_hwaddr(); |
573ad77f | 608 | test_task_blocks_signal(); |
bed0d08f CB |
609 | |
610 | exit(EXIT_SUCCESS); | |
611 | } |