Commit a2618c0d authored by Jordan Rife's avatar Jordan Rife Committed by Alexei Starovoitov

selftests/bpf: Handle SYSCALL_EPERM and SYSCALL_ENOTSUPP test cases

In preparation to move test cases from bpf/test_sock_addr.c that expect
system calls to return ENOTSUPP or EPERM, this patch propagates errno
from relevant system calls up to test_sock_addr() where the result can
be checked.
Signed-off-by: default avatarJordan Rife <jrife@google.com>
Link: https://lore.kernel.org/r/20240510190246.3247730-6-jrife@google.comSigned-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 5a047b22
...@@ -19,6 +19,10 @@ ...@@ -19,6 +19,10 @@
#include "getpeername_unix_prog.skel.h" #include "getpeername_unix_prog.skel.h"
#include "network_helpers.h" #include "network_helpers.h"
#ifndef ENOTSUPP
# define ENOTSUPP 524
#endif
#define TEST_NS "sock_addr" #define TEST_NS "sock_addr"
#define TEST_IF_PREFIX "test_sock_addr" #define TEST_IF_PREFIX "test_sock_addr"
#define TEST_IPV4 "127.0.0.4" #define TEST_IPV4 "127.0.0.4"
...@@ -43,6 +47,8 @@ ...@@ -43,6 +47,8 @@
#define SERVUN_REWRITE_ADDRESS "bpf_cgroup_unix_test_rewrite" #define SERVUN_REWRITE_ADDRESS "bpf_cgroup_unix_test_rewrite"
#define SRCUN_ADDRESS "bpf_cgroup_unix_test_src" #define SRCUN_ADDRESS "bpf_cgroup_unix_test_src"
#define save_errno_do(op) ({ int __save = errno; op; errno = __save; })
enum sock_addr_test_type { enum sock_addr_test_type {
SOCK_ADDR_TEST_BIND, SOCK_ADDR_TEST_BIND,
SOCK_ADDR_TEST_CONNECT, SOCK_ADDR_TEST_CONNECT,
...@@ -98,6 +104,7 @@ static int run_bpf_prog(const char *prog_name, void *ctx, int ctx_size) ...@@ -98,6 +104,7 @@ static int run_bpf_prog(const char *prog_name, void *ctx, int ctx_size)
goto err; goto err;
err = topts.retval; err = topts.retval;
errno = -topts.retval;
goto out; goto out;
err: err:
err = -1; err = -1;
...@@ -221,8 +228,7 @@ int kernel_connect_to_addr(int type, const struct sockaddr_storage *addr, sockle ...@@ -221,8 +228,7 @@ int kernel_connect_to_addr(int type, const struct sockaddr_storage *addr, sockle
"kernel_init_sock")) "kernel_init_sock"))
goto err; goto err;
if (!ASSERT_OK(kernel_connect((struct sockaddr *)addr, addrlen), if (kernel_connect((struct sockaddr *)addr, addrlen) < 0)
"kernel_connect"))
goto err; goto err;
/* Test code expects a "file descriptor" on success. */ /* Test code expects a "file descriptor" on success. */
...@@ -230,7 +236,7 @@ int kernel_connect_to_addr(int type, const struct sockaddr_storage *addr, sockle ...@@ -230,7 +236,7 @@ int kernel_connect_to_addr(int type, const struct sockaddr_storage *addr, sockle
goto out; goto out;
err: err:
err = -1; err = -1;
ASSERT_OK(kernel_close_sock(0), "kernel_close_sock"); save_errno_do(ASSERT_OK(kernel_close_sock(0), "kernel_close_sock"));
out: out:
return err; return err;
} }
...@@ -248,8 +254,7 @@ int kernel_start_server(int family, int type, const char *addr_str, __u16 port, ...@@ -248,8 +254,7 @@ int kernel_start_server(int family, int type, const char *addr_str, __u16 port,
if (make_sockaddr(family, addr_str, port, &addr, &addrlen)) if (make_sockaddr(family, addr_str, port, &addr, &addrlen))
goto err; goto err;
if (!ASSERT_OK(kernel_bind(0, (struct sockaddr *)&addr, addrlen), if (kernel_bind(0, (struct sockaddr *)&addr, addrlen) < 0)
"kernel_bind"))
goto err; goto err;
if (type == SOCK_STREAM) { if (type == SOCK_STREAM) {
...@@ -262,7 +267,7 @@ int kernel_start_server(int family, int type, const char *addr_str, __u16 port, ...@@ -262,7 +267,7 @@ int kernel_start_server(int family, int type, const char *addr_str, __u16 port,
goto out; goto out;
err: err:
err = -1; err = -1;
ASSERT_OK(kernel_close_sock(0), "kernel_close_sock"); save_errno_do(ASSERT_OK(kernel_close_sock(0), "kernel_close_sock"));
out: out:
return err; return err;
} }
...@@ -1066,7 +1071,7 @@ static void unload_sock_addr_kern(void) ...@@ -1066,7 +1071,7 @@ static void unload_sock_addr_kern(void)
sock_addr_kern__destroy(skel); sock_addr_kern__destroy(skel);
} }
static void test_bind(struct sock_addr_test *test) static int test_bind(struct sock_addr_test *test)
{ {
struct sockaddr_storage expected_addr; struct sockaddr_storage expected_addr;
socklen_t expected_addr_len = sizeof(struct sockaddr_storage); socklen_t expected_addr_len = sizeof(struct sockaddr_storage);
...@@ -1075,8 +1080,10 @@ static void test_bind(struct sock_addr_test *test) ...@@ -1075,8 +1080,10 @@ static void test_bind(struct sock_addr_test *test)
serv = test->ops->start_server(test->socket_family, test->socket_type, serv = test->ops->start_server(test->socket_family, test->socket_type,
test->requested_addr, test->requested_addr,
test->requested_port, 0); test->requested_port, 0);
if (!ASSERT_GE(serv, 0, "start_server")) if (serv < 0) {
goto cleanup; err = errno;
goto err;
}
err = make_sockaddr(test->socket_family, err = make_sockaddr(test->socket_family,
test->expected_addr, test->expected_port, test->expected_addr, test->expected_port,
...@@ -1095,13 +1102,17 @@ static void test_bind(struct sock_addr_test *test) ...@@ -1095,13 +1102,17 @@ static void test_bind(struct sock_addr_test *test)
goto cleanup; goto cleanup;
cleanup: cleanup:
err = 0;
err:
if (client != -1) if (client != -1)
close(client); close(client);
if (serv != -1) if (serv != -1)
test->ops->close(serv); test->ops->close(serv);
return err;
} }
static void test_connect(struct sock_addr_test *test) static int test_connect(struct sock_addr_test *test)
{ {
struct sockaddr_storage addr, expected_addr, expected_src_addr; struct sockaddr_storage addr, expected_addr, expected_src_addr;
socklen_t addr_len = sizeof(struct sockaddr_storage), socklen_t addr_len = sizeof(struct sockaddr_storage),
...@@ -1121,8 +1132,10 @@ static void test_connect(struct sock_addr_test *test) ...@@ -1121,8 +1132,10 @@ static void test_connect(struct sock_addr_test *test)
client = test->ops->connect_to_addr(test->socket_type, &addr, addr_len, client = test->ops->connect_to_addr(test->socket_type, &addr, addr_len,
NULL); NULL);
if (!ASSERT_GE(client, 0, "connect_to_addr")) if (client < 0) {
goto cleanup; err = errno;
goto err;
}
err = make_sockaddr(test->socket_family, test->expected_addr, test->expected_port, err = make_sockaddr(test->socket_family, test->expected_addr, test->expected_port,
&expected_addr, &expected_addr_len); &expected_addr, &expected_addr_len);
...@@ -1149,13 +1162,17 @@ static void test_connect(struct sock_addr_test *test) ...@@ -1149,13 +1162,17 @@ static void test_connect(struct sock_addr_test *test)
goto cleanup; goto cleanup;
} }
cleanup: cleanup:
err = 0;
err:
if (client != -1) if (client != -1)
test->ops->close(client); test->ops->close(client);
if (serv != -1) if (serv != -1)
close(serv); close(serv);
return err;
} }
static void test_xmsg(struct sock_addr_test *test) static int test_xmsg(struct sock_addr_test *test)
{ {
struct sockaddr_storage addr, src_addr; struct sockaddr_storage addr, src_addr;
socklen_t addr_len = sizeof(struct sockaddr_storage), socklen_t addr_len = sizeof(struct sockaddr_storage),
...@@ -1196,6 +1213,11 @@ static void test_xmsg(struct sock_addr_test *test) ...@@ -1196,6 +1213,11 @@ static void test_xmsg(struct sock_addr_test *test)
if (test->socket_type == SOCK_DGRAM) { if (test->socket_type == SOCK_DGRAM) {
err = test->ops->sendmsg(client, (struct sockaddr *)&addr, err = test->ops->sendmsg(client, (struct sockaddr *)&addr,
addr_len, &data, sizeof(data)); addr_len, &data, sizeof(data));
if (err < 0) {
err = errno;
goto err;
}
if (!ASSERT_EQ(err, sizeof(data), "sendmsg")) if (!ASSERT_EQ(err, sizeof(data), "sendmsg"))
goto cleanup; goto cleanup;
} else { } else {
...@@ -1245,13 +1267,17 @@ static void test_xmsg(struct sock_addr_test *test) ...@@ -1245,13 +1267,17 @@ static void test_xmsg(struct sock_addr_test *test)
} }
cleanup: cleanup:
err = 0;
err:
if (client != -1) if (client != -1)
test->ops->close(client); test->ops->close(client);
if (serv != -1) if (serv != -1)
close(serv); close(serv);
return err;
} }
static void test_getsockname(struct sock_addr_test *test) static int test_getsockname(struct sock_addr_test *test)
{ {
struct sockaddr_storage expected_addr; struct sockaddr_storage expected_addr;
socklen_t expected_addr_len = sizeof(struct sockaddr_storage); socklen_t expected_addr_len = sizeof(struct sockaddr_storage);
...@@ -1275,9 +1301,11 @@ static void test_getsockname(struct sock_addr_test *test) ...@@ -1275,9 +1301,11 @@ static void test_getsockname(struct sock_addr_test *test)
cleanup: cleanup:
if (serv != -1) if (serv != -1)
test->ops->close(serv); test->ops->close(serv);
return 0;
} }
static void test_getpeername(struct sock_addr_test *test) static int test_getpeername(struct sock_addr_test *test)
{ {
struct sockaddr_storage addr, expected_addr; struct sockaddr_storage addr, expected_addr;
socklen_t addr_len = sizeof(struct sockaddr_storage), socklen_t addr_len = sizeof(struct sockaddr_storage),
...@@ -1314,6 +1342,8 @@ static void test_getpeername(struct sock_addr_test *test) ...@@ -1314,6 +1342,8 @@ static void test_getpeername(struct sock_addr_test *test)
test->ops->close(client); test->ops->close(client);
if (serv != -1) if (serv != -1)
close(serv); close(serv);
return 0;
} }
static int setup_test_env(struct nstoken **tok) static int setup_test_env(struct nstoken **tok)
...@@ -1369,6 +1399,7 @@ void test_sock_addr(void) ...@@ -1369,6 +1399,7 @@ void test_sock_addr(void)
for (size_t i = 0; i < ARRAY_SIZE(tests); ++i) { for (size_t i = 0; i < ARRAY_SIZE(tests); ++i) {
struct sock_addr_test *test = &tests[i]; struct sock_addr_test *test = &tests[i];
int err;
if (!test__start_subtest(test->name)) if (!test__start_subtest(test->name))
continue; continue;
...@@ -1385,26 +1416,33 @@ void test_sock_addr(void) ...@@ -1385,26 +1416,33 @@ void test_sock_addr(void)
* the future. * the future.
*/ */
case SOCK_ADDR_TEST_BIND: case SOCK_ADDR_TEST_BIND:
test_bind(test); err = test_bind(test);
break; break;
case SOCK_ADDR_TEST_CONNECT: case SOCK_ADDR_TEST_CONNECT:
test_connect(test); err = test_connect(test);
break; break;
case SOCK_ADDR_TEST_SENDMSG: case SOCK_ADDR_TEST_SENDMSG:
case SOCK_ADDR_TEST_RECVMSG: case SOCK_ADDR_TEST_RECVMSG:
test_xmsg(test); err = test_xmsg(test);
break; break;
case SOCK_ADDR_TEST_GETSOCKNAME: case SOCK_ADDR_TEST_GETSOCKNAME:
test_getsockname(test); err = test_getsockname(test);
break; break;
case SOCK_ADDR_TEST_GETPEERNAME: case SOCK_ADDR_TEST_GETPEERNAME:
test_getpeername(test); err = test_getpeername(test);
break; break;
default: default:
ASSERT_TRUE(false, "Unknown sock addr test type"); ASSERT_TRUE(false, "Unknown sock addr test type");
break; break;
} }
if (test->expected_result == SYSCALL_EPERM)
ASSERT_EQ(err, EPERM, "socket operation returns EPERM");
else if (test->expected_result == SYSCALL_ENOTSUPP)
ASSERT_EQ(err, ENOTSUPP, "socket operation returns ENOTSUPP");
else if (test->expected_result == SUCCESS)
ASSERT_OK(err, "socket operation succeeds");
test->destroyfn(skel); test->destroyfn(skel);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment