]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-namespace.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / test / test-namespace.c
CommitLineData
d8c9d3a4
ZJS
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2013 Zbigniew Jędrzejewski-Szmek
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
613b411c 22#include <sys/socket.h>
d8c9d3a4
ZJS
23
24#include "namespace.h"
0b452006 25#include "process-util.h"
07630cea
LP
26#include "string-util.h"
27#include "util.h"
d8c9d3a4
ZJS
28
29static void test_tmpdir(const char *id, const char *A, const char *B) {
30 _cleanup_free_ char *a, *b;
613b411c
LP
31 struct stat x, y;
32 char *c, *d;
d8c9d3a4 33
613b411c
LP
34 assert_se(setup_tmp_dirs(id, &a, &b) == 0);
35 assert_se(startswith(a, A));
36 assert_se(startswith(b, B));
d8c9d3a4 37
613b411c
LP
38 assert_se(stat(a, &x) >= 0);
39 assert_se(stat(b, &y) >= 0);
d8c9d3a4 40
613b411c
LP
41 assert_se(S_ISDIR(x.st_mode));
42 assert_se(S_ISDIR(y.st_mode));
d8c9d3a4 43
613b411c
LP
44 assert_se((x.st_mode & 01777) == 0700);
45 assert_se((y.st_mode & 01777) == 0700);
46
63c372cb
LP
47 c = strjoina(a, "/tmp");
48 d = strjoina(b, "/tmp");
613b411c
LP
49
50 assert_se(stat(c, &x) >= 0);
51 assert_se(stat(d, &y) >= 0);
52
53 assert_se(S_ISDIR(x.st_mode));
54 assert_se(S_ISDIR(y.st_mode));
55
56 assert_se((x.st_mode & 01777) == 01777);
57 assert_se((y.st_mode & 01777) == 01777);
58
59 assert_se(rmdir(c) >= 0);
60 assert_se(rmdir(d) >= 0);
61
62 assert_se(rmdir(a) >= 0);
63 assert_se(rmdir(b) >= 0);
64}
65
66static void test_netns(void) {
3d94f76c 67 _cleanup_close_pair_ int s[2] = { -1, -1 };
613b411c
LP
68 pid_t pid1, pid2, pid3;
69 int r, n = 0;
70 siginfo_t si;
71
72 if (geteuid() > 0)
73 return;
74
75 assert_se(socketpair(AF_UNIX, SOCK_DGRAM, 0, s) >= 0);
76
77 pid1 = fork();
78 assert_se(pid1 >= 0);
79
80 if (pid1 == 0) {
81 r = setup_netns(s);
82 assert_se(r >= 0);
83 _exit(r);
84 }
85
86 pid2 = fork();
87 assert_se(pid2 >= 0);
88
89 if (pid2 == 0) {
90 r = setup_netns(s);
91 assert_se(r >= 0);
92 exit(r);
93 }
94
95 pid3 = fork();
96 assert_se(pid3 >= 0);
97
98 if (pid3 == 0) {
99 r = setup_netns(s);
100 assert_se(r >= 0);
101 exit(r);
102 }
103
104 r = wait_for_terminate(pid1, &si);
105 assert_se(r >= 0);
106 assert_se(si.si_code == CLD_EXITED);
107 n += si.si_status;
108
109 r = wait_for_terminate(pid2, &si);
110 assert_se(r >= 0);
111 assert_se(si.si_code == CLD_EXITED);
112 n += si.si_status;
113
114 r = wait_for_terminate(pid3, &si);
115 assert_se(r >= 0);
116 assert_se(si.si_code == CLD_EXITED);
117 n += si.si_status;
118
119 assert_se(n == 1);
d8c9d3a4
ZJS
120}
121
122int main(int argc, char *argv[]) {
6b46ea73
LP
123 sd_id128_t bid;
124 char boot_id[SD_ID128_STRING_MAX];
125 _cleanup_free_ char *x = NULL, *y = NULL, *z = NULL, *zz = NULL;
d8c9d3a4 126
6b46ea73
LP
127 assert_se(sd_id128_get_boot(&bid) >= 0);
128 sd_id128_to_string(bid, boot_id);
129
130 x = strjoin("/tmp/systemd-private-", boot_id, "-abcd.service-", NULL);
131 y = strjoin("/var/tmp/systemd-private-", boot_id, "-abcd.service-", NULL);
132 assert_se(x && y);
133
134 test_tmpdir("abcd.service", x, y);
135
136 z = strjoin("/tmp/systemd-private-", boot_id, "-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-", NULL);
137 zz = strjoin("/var/tmp/systemd-private-", boot_id, "-sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device-", NULL);
138
139 assert_se(z && zz);
140
141 test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z, zz);
d8c9d3a4 142
613b411c
LP
143 test_netns();
144
d8c9d3a4
ZJS
145 return 0;
146}