From 4a8e01a70f19aa512e78cc9670d2845c6975fbf2 Mon Sep 17 00:00:00 2001 From: Marco Casaroli Date: Thu, 26 Nov 2026 17:01:31 +0100 Subject: [PATCH] testing/nettest: Test the name of a socket pair. Add two cases to cmocka_net_local_dgram. test_local_dgram_pair_path: F_GETPATH of the ends of a socket pair must give "local:[socketpairN]", with N a decimal number from a counter: the same N for both ends of a pair, and a different N for the next pair. test_local_dgram_pair_noname: connect() and sendto() of a datagram socket to that name must fail with ENOENT, bind() of the name must succeed, and the pair must still work. The cases need CONFIG_FS_IOCTL_FILEPATH; without it they are skipped. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli --- testing/nettest/local/test_local_dgram.c | 2 + testing/nettest/local/test_local_dgram.h | 2 + testing/nettest/local/test_local_dgram_pair.c | 120 ++++++++++++++++++ 3 files changed, 124 insertions(+) diff --git a/testing/nettest/local/test_local_dgram.c b/testing/nettest/local/test_local_dgram.c index ededd6afa93..c183e5a7b47 100644 --- a/testing/nettest/local/test_local_dgram.c +++ b/testing/nettest/local/test_local_dgram.c @@ -140,6 +140,8 @@ int main(int argc, FAR char *argv[]) DGRAM_TEST(test_local_dgram_pair_send), DGRAM_TEST(test_local_dgram_pair_full), DGRAM_TEST(test_local_dgram_pair_close), + DGRAM_TEST(test_local_dgram_pair_path), + DGRAM_TEST(test_local_dgram_pair_noname), DGRAM_TEST(test_local_dgram_poll_pair), DGRAM_TEST(test_local_dgram_recv_intr), DGRAM_TEST(test_local_dgram_stream_intr), diff --git a/testing/nettest/local/test_local_dgram.h b/testing/nettest/local/test_local_dgram.h index b5c4453f335..821e933a833 100644 --- a/testing/nettest/local/test_local_dgram.h +++ b/testing/nettest/local/test_local_dgram.h @@ -376,6 +376,8 @@ void test_local_dgram_fifo_hup(FAR void **state); void test_local_dgram_pair_send(FAR void **state); void test_local_dgram_pair_full(FAR void **state); void test_local_dgram_pair_close(FAR void **state); +void test_local_dgram_pair_path(FAR void **state); +void test_local_dgram_pair_noname(FAR void **state); void test_local_dgram_poll_pair(FAR void **state); void test_local_dgram_pair_iov(FAR void **state); void test_local_dgram_send_iov(FAR void **state); diff --git a/testing/nettest/local/test_local_dgram_pair.c b/testing/nettest/local/test_local_dgram_pair.c index d526f9b3987..fb34a3d75e0 100644 --- a/testing/nettest/local/test_local_dgram_pair.c +++ b/testing/nettest/local/test_local_dgram_pair.c @@ -29,13 +29,16 @@ * Included Files ****************************************************************************/ +#include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -50,6 +53,7 @@ #define LOCAL_DGRAM_NIOV 12 #define LOCAL_DGRAM_IOVLEN 500 #define LOCAL_DGRAM_WHOLE 400 +#define LOCAL_DGRAM_PNAME 16 /**************************************************************************** * Private Functions @@ -68,6 +72,38 @@ static void local_dgram_pair(FAR struct nettest_local_dgram_state_s *st) st->r3 = sv[1]; } +#ifdef CONFIG_FS_IOCTL_FILEPATH +/* F_GETPATH of fd, the end of a pair: check the form + * "local:[socketpairN]" with a decimal N, and return N in name. + */ + +static void local_dgram_pairpath(int fd, FAR char *name, size_t size) +{ + static const char prefix[] = "local:[socketpair"; + char path[PATH_MAX]; + FAR char *num; + size_t len; + size_t i; + int ret; + + memset(path, 0, sizeof(path)); + ret = fcntl(fd, F_GETPATH, path); + assert_return_code(ret, errno); + assert_memory_equal(path, prefix, sizeof(prefix) - 1); + + num = path + sizeof(prefix) - 1; + len = strlen(num); + assert_in_range(len, 2, size); + assert_int_equal(num[len - 1], ']'); + for (i = 0; i < len - 1; i++) + { + assert_true(isdigit(num[i])); + } + + strlcpy(name, num, len); +} +#endif + /* recv() one datagram without a wait into size bytes, and check its length * and data. */ @@ -241,6 +277,90 @@ void test_local_dgram_pair_close(FAR void **state) assert_int_equal(errno, ECONNREFUSED); } +/**************************************************************************** + * Name: test_local_dgram_pair_path + * + * Description: + * F_GETPATH of the ends of a socket pair gives "local:[socketpairN]", + * with N a decimal number from a counter: the same N for both ends of a + * pair, and a different N for the next pair. + * + ****************************************************************************/ + +void test_local_dgram_pair_path(FAR void **state) +{ +#ifdef CONFIG_FS_IOCTL_FILEPATH + FAR struct nettest_local_dgram_state_s *st = *state; + char name[3][LOCAL_DGRAM_PNAME]; + int sv[2]; + int ret; + + ret = socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, sv); + assert_return_code(ret, errno); + st->r2 = sv[0]; + st->r3 = sv[1]; + local_dgram_pairpath(st->r2, name[0], sizeof(name[0])); + local_dgram_pairpath(st->r3, name[1], sizeof(name[1])); + assert_string_equal(name[0], name[1]); + + ret = socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, sv); + assert_return_code(ret, errno); + st->u = sv[0]; + close(sv[1]); + local_dgram_pairpath(st->u, name[2], sizeof(name[2])); + assert_string_not_equal(name[0], name[2]); +#else + skip(); +#endif +} + +/**************************************************************************** + * Name: test_local_dgram_pair_noname + * + * Description: + * The name of a socket pair is internal: connect() and sendto() of a + * datagram socket to it fail with ENOENT, bind() of it succeeds, and the + * pair still works. + * + ****************************************************************************/ + +void test_local_dgram_pair_noname(FAR void **state) +{ +#ifdef CONFIG_FS_IOCTL_FILEPATH + FAR struct nettest_local_dgram_state_s *st = *state; + char num[LOCAL_DGRAM_PNAME]; + char name[LOCAL_DGRAM_PATHLEN]; + int sv[2]; + int fd; + int ret; + + ret = socketpair(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, sv); + assert_return_code(ret, errno); + st->r2 = sv[0]; + st->r3 = sv[1]; + local_dgram_pairpath(st->r2, num, sizeof(num)); + snprintf(name, sizeof(name), "socketpair%s", num); + + errno = 0; + assert_int_equal(local_dgram_connect_to(st->s, name, false), -1); + assert_int_equal(errno, ENOENT); + + errno = 0; + assert_int_equal(local_dgram_sendto(st->s, name, "x", 1, MSG_DONTWAIT), + -1); + assert_int_equal(errno, ENOENT); + + fd = local_dgram_bind(name, false); + assert_return_code(fd, errno); + close(fd); + + assert_int_equal(send(st->r2, "ok", 2, 0), 2); + local_dgram_pairrecv(st->r3, "ok", LOCAL_DGRAM_PAIRBUF); +#else + skip(); +#endif +} + /**************************************************************************** * Name: test_local_dgram_poll_pair *