diff options
| author | Christian Brauner <brauner@kernel.org> | 2025-10-29 15:21:10 +0300 |
|---|---|---|
| committer | Christian Brauner <brauner@kernel.org> | 2025-11-03 19:41:23 +0300 |
| commit | 5aec9f455ced4004f758d8619cda69e979734670 (patch) | |
| tree | adc1c59aa9e7975955485c163d1f55f6cc1d632c /tools/testing | |
| parent | c0f06da56860f185268b77cb23c4b3c85518e87a (diff) | |
| download | linux-5aec9f455ced4004f758d8619cda69e979734670.tar.xz | |
selftests/namespaces: third inactive namespace resurrection test
Test SIOCGSKNS with different socket types (TCP, UDP, RAW).
Link: https://patch.msgid.link/20251029-work-namespace-nstree-listns-v4-57-2e6f823ebdc0@kernel.org
Reviewed-by: Jeff Layton <jlayton@kernel.org>
Signed-off-by: Christian Brauner <brauner@kernel.org>
Diffstat (limited to 'tools/testing')
| -rw-r--r-- | tools/testing/selftests/namespaces/siocgskns_test.c | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/tools/testing/selftests/namespaces/siocgskns_test.c b/tools/testing/selftests/namespaces/siocgskns_test.c index 0ad5e39b7e16..02798e59fc11 100644 --- a/tools/testing/selftests/namespaces/siocgskns_test.c +++ b/tools/testing/selftests/namespaces/siocgskns_test.c @@ -163,8 +163,7 @@ TEST(siocgskns_keeps_netns_active) /* Wait for child to exit */ waitpid(pid, &status, 0); ASSERT_TRUE(WIFEXITED(status)); - if (WEXITSTATUS(status) != 0) - SKIP(close(sock_fd); return, "Child failed to create namespace"); + ASSERT_EQ(WEXITSTATUS(status), 0); /* Get network namespace from socket */ netns_fd = ioctl(sock_fd, SIOCGSKNS); @@ -195,4 +194,66 @@ TEST(siocgskns_keeps_netns_active) ASSERT_LT(ioctl(sock_fd, SIOCGSKNS), 0); } +/* + * Test SIOCGSKNS with different socket types (TCP, UDP, RAW). + */ +TEST(siocgskns_socket_types) +{ + int sock_tcp, sock_udp, sock_raw; + int netns_tcp, netns_udp, netns_raw; + struct stat st_tcp, st_udp, st_raw; + + /* TCP socket */ + sock_tcp = socket(AF_INET, SOCK_STREAM, 0); + ASSERT_GE(sock_tcp, 0); + + /* UDP socket */ + sock_udp = socket(AF_INET, SOCK_DGRAM, 0); + ASSERT_GE(sock_udp, 0); + + /* RAW socket (may require privileges) */ + sock_raw = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); + if (sock_raw < 0 && (errno == EPERM || errno == EACCES)) { + sock_raw = -1; /* Skip raw socket test */ + } + + /* Test SIOCGSKNS on TCP */ + netns_tcp = ioctl(sock_tcp, SIOCGSKNS); + if (netns_tcp < 0) { + close(sock_tcp); + close(sock_udp); + if (sock_raw >= 0) close(sock_raw); + if (errno == ENOTTY || errno == EINVAL) + SKIP(return, "SIOCGSKNS not supported"); + ASSERT_GE(netns_tcp, 0); + } + + /* Test SIOCGSKNS on UDP */ + netns_udp = ioctl(sock_udp, SIOCGSKNS); + ASSERT_GE(netns_udp, 0); + + /* Test SIOCGSKNS on RAW (if available) */ + if (sock_raw >= 0) { + netns_raw = ioctl(sock_raw, SIOCGSKNS); + ASSERT_GE(netns_raw, 0); + } + + /* Verify all return the same network namespace */ + ASSERT_EQ(fstat(netns_tcp, &st_tcp), 0); + ASSERT_EQ(fstat(netns_udp, &st_udp), 0); + ASSERT_EQ(st_tcp.st_ino, st_udp.st_ino); + + if (sock_raw >= 0) { + ASSERT_EQ(fstat(netns_raw, &st_raw), 0); + ASSERT_EQ(st_tcp.st_ino, st_raw.st_ino); + close(netns_raw); + close(sock_raw); + } + + close(netns_tcp); + close(netns_udp); + close(sock_tcp); + close(sock_udp); +} + TEST_HARNESS_MAIN |
