diff options
author | Xℹ Ruoyao <xry111@mengyan1223.wang> | 2021-02-16 13:49:15 +0100 |
---|---|---|
committer | Xℹ Ruoyao <xry111@mengyan1223.wang> | 2021-03-03 17:04:36 +0100 |
commit | 54c2459d560283f556e331246f64776cebd6eba6 (patch) | |
tree | b41f23fbd485e474afdc6c5b12f380c5829a42bd /src/test | |
parent | fuzz: add NetworkNamespacePath= into directives.service (diff) | |
download | systemd-54c2459d560283f556e331246f64776cebd6eba6.tar.xz systemd-54c2459d560283f556e331246f64776cebd6eba6.zip |
Refactor network namespace specific functions in generic helpers
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test-namespace.c | 17 | ||||
-rw-r--r-- | src/test/test-stat-util.c | 19 |
2 files changed, 25 insertions, 11 deletions
diff --git a/src/test/test-namespace.c b/src/test/test-namespace.c index b162928482..bf4b87e8a6 100644 --- a/src/test/test-namespace.c +++ b/src/test/test-namespace.c @@ -63,7 +63,7 @@ static void test_tmpdir(const char *id, const char *A, const char *B) { } } -static void test_netns(void) { +static void test_shareable_ns(unsigned long nsflag) { _cleanup_close_pair_ int s[2] = { -1, -1 }; pid_t pid1, pid2, pid3; int r, n = 0; @@ -80,7 +80,7 @@ static void test_netns(void) { assert_se(pid1 >= 0); if (pid1 == 0) { - r = setup_netns(s); + r = setup_shareable_ns(s, nsflag); assert_se(r >= 0); _exit(r); } @@ -89,7 +89,7 @@ static void test_netns(void) { assert_se(pid2 >= 0); if (pid2 == 0) { - r = setup_netns(s); + r = setup_shareable_ns(s, nsflag); assert_se(r >= 0); exit(r); } @@ -98,7 +98,7 @@ static void test_netns(void) { assert_se(pid3 >= 0); if (pid3 == 0) { - r = setup_netns(s); + r = setup_shareable_ns(s, nsflag); assert_se(r >= 0); exit(r); } @@ -121,6 +121,14 @@ static void test_netns(void) { assert_se(n == 1); } +static void test_netns(void) { + test_shareable_ns(CLONE_NEWNET); +} + +static void test_ipcns(void) { + test_shareable_ns(CLONE_NEWIPC); +} + static void test_protect_kernel_logs(void) { int r; pid_t pid; @@ -224,6 +232,7 @@ int main(int argc, char *argv[]) { test_tmpdir("sys-devices-pci0000:00-0000:00:1a.0-usb3-3\\x2d1-3\\x2d1:1.0-bluetooth-hci0.device", z, zz); test_netns(); + test_ipcns(); test_protect_kernel_logs(); return EXIT_SUCCESS; diff --git a/src/test/test-stat-util.c b/src/test/test-stat-util.c index 9aca09c4de..7f03db5fa7 100644 --- a/src/test/test-stat-util.c +++ b/src/test/test-stat-util.c @@ -3,6 +3,7 @@ #include <fcntl.h> #include <linux/magic.h> #include <unistd.h> +#include <sched.h> #include "alloc-util.h" #include "fd-util.h" @@ -67,18 +68,22 @@ static void test_path_is_temporary_fs(void) { assert_se(path_is_temporary_fs("/i-dont-exist") == -ENOENT); } -static void test_fd_is_network_ns(void) { +static void test_fd_is_ns(void) { _cleanup_close_ int fd = -1; - assert_se(fd_is_network_ns(STDIN_FILENO) == 0); - assert_se(fd_is_network_ns(STDERR_FILENO) == 0); - assert_se(fd_is_network_ns(STDOUT_FILENO) == 0); + assert_se(fd_is_ns(STDIN_FILENO, CLONE_NEWNET) == 0); + assert_se(fd_is_ns(STDERR_FILENO, CLONE_NEWNET) == 0); + assert_se(fd_is_ns(STDOUT_FILENO, CLONE_NEWNET) == 0); assert_se((fd = open("/proc/self/ns/mnt", O_CLOEXEC|O_RDONLY)) >= 0); - assert_se(IN_SET(fd_is_network_ns(fd), 0, -EUCLEAN)); + assert_se(IN_SET(fd_is_ns(fd, CLONE_NEWNET), 0, -EUCLEAN)); + fd = safe_close(fd); + + assert_se((fd = open("/proc/self/ns/ipc", O_CLOEXEC|O_RDONLY)) >= 0); + assert_se(IN_SET(fd_is_ns(fd, CLONE_NEWIPC), 1, -EUCLEAN)); fd = safe_close(fd); assert_se((fd = open("/proc/self/ns/net", O_CLOEXEC|O_RDONLY)) >= 0); - assert_se(IN_SET(fd_is_network_ns(fd), 1, -EUCLEAN)); + assert_se(IN_SET(fd_is_ns(fd, CLONE_NEWNET), 1, -EUCLEAN)); } static void test_device_major_minor_valid(void) { @@ -159,7 +164,7 @@ int main(int argc, char *argv[]) { test_is_symlink(); test_path_is_fs_type(); test_path_is_temporary_fs(); - test_fd_is_network_ns(); + test_fd_is_ns(); test_device_major_minor_valid(); test_device_path_make_canonical(); |