This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git
commit 5ff96668e6c0b6520d25dd80f8bbd653f887d89c Author: Marco Casaroli <[email protected]> AuthorDate: Sat Nov 21 17:10:57 2026 +0100 testing/nettest: Check the peer port of IPv6 TCP and UDP sockets. test_tcp_peername_ipv6: on a TCP connection over ::1, getpeername() of the client gives the port of the listener, and getpeername() of the accepted socket gives the port of the client. test_udp_family_connect: after connect(), getpeername() gives the port of the connect() address. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- testing/nettest/CMakeLists.txt | 3 +- testing/nettest/Makefile | 2 +- testing/nettest/tcp/test_tcp.c | 3 + testing/nettest/tcp/test_tcp.h | 18 ++++ testing/nettest/tcp/test_tcp_peername.c | 164 ++++++++++++++++++++++++++++++++ testing/nettest/udp/test_udp_family.c | 7 ++ 6 files changed, 195 insertions(+), 2 deletions(-) diff --git a/testing/nettest/CMakeLists.txt b/testing/nettest/CMakeLists.txt index 957d035a3..28438a714 100644 --- a/testing/nettest/CMakeLists.txt +++ b/testing/nettest/CMakeLists.txt @@ -36,7 +36,8 @@ if(CONFIG_TESTING_NET_TEST) endif() if(CONFIG_NET_IPv6) - list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_ipv6.c) + list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_ipv6.c + ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_peername.c) endif() if(CONFIG_NET_IPv4) diff --git a/testing/nettest/Makefile b/testing/nettest/Makefile index 3c3bf3736..6b44529ea 100644 --- a/testing/nettest/Makefile +++ b/testing/nettest/Makefile @@ -45,7 +45,7 @@ CSRCS += tcp/test_tcp_close.c endif ifeq ($(CONFIG_NET_IPv6), y) -CSRCS += tcp/test_tcp_connect_ipv6.c +CSRCS += tcp/test_tcp_connect_ipv6.c tcp/test_tcp_peername.c endif ifeq ($(CONFIG_NET_IPv4), y) diff --git a/testing/nettest/tcp/test_tcp.c b/testing/nettest/tcp/test_tcp.c index cfa755266..66de7982b 100644 --- a/testing/nettest/tcp/test_tcp.c +++ b/testing/nettest/tcp/test_tcp.c @@ -92,6 +92,9 @@ int main(int argc, FAR char *argv[]) cmocka_unit_test_setup_teardown(test_tcp_connect_ipv6, test_tcp_connect_ipv6_setup, test_tcp_common_teardown), + cmocka_unit_test_setup_teardown(test_tcp_peername_ipv6, + test_tcp_peername_setup, + test_tcp_peername_teardown), #endif #ifdef CONFIG_NET_IPv4 cmocka_unit_test_setup_teardown(test_tcp_reset_recv, diff --git a/testing/nettest/tcp/test_tcp.h b/testing/nettest/tcp/test_tcp.h index e6dc268c2..c3e6dc449 100644 --- a/testing/nettest/tcp/test_tcp.h +++ b/testing/nettest/tcp/test_tcp.h @@ -207,6 +207,24 @@ int test_tcp_connect_ipv6_setup(FAR void **state); ****************************************************************************/ void test_tcp_connect_ipv6(FAR void **state); + +/**************************************************************************** + * Name: test_tcp_peername_setup + ****************************************************************************/ + +int test_tcp_peername_setup(FAR void **state); + +/**************************************************************************** + * Name: test_tcp_peername_teardown + ****************************************************************************/ + +int test_tcp_peername_teardown(FAR void **state); + +/**************************************************************************** + * Name: test_tcp_peername_ipv6 + ****************************************************************************/ + +void test_tcp_peername_ipv6(FAR void **state); #endif #ifdef CONFIG_NET_IPv4 diff --git a/testing/nettest/tcp/test_tcp_peername.c b/testing/nettest/tcp/test_tcp_peername.c new file mode 100644 index 000000000..085e2bc7a --- /dev/null +++ b/testing/nettest/tcp/test_tcp_peername.c @@ -0,0 +1,164 @@ +/**************************************************************************** + * apps/testing/nettest/tcp/test_tcp_peername.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <errno.h> +#include <netinet/in.h> +#include <poll.h> +#include <setjmp.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <sys/socket.h> +#include <cmocka.h> + +#include "test_tcp.h" +#include "utils.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define TEST_POLL 1000 /* ms, loopback answers at once */ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct tcp_peername_state_s +{ + int fds[3]; +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: test_tcp_peername_setup + ****************************************************************************/ + +int test_tcp_peername_setup(FAR void **state) +{ + FAR struct tcp_peername_state_s *st = malloc(sizeof(*st)); + + assert_non_null(st); + st->fds[0] = -1; + st->fds[1] = -1; + st->fds[2] = -1; + *state = st; + return 0; +} + +/**************************************************************************** + * Name: test_tcp_peername_teardown + ****************************************************************************/ + +int test_tcp_peername_teardown(FAR void **state) +{ + FAR struct tcp_peername_state_s *st = *state; + int i; + + for (i = 0; i < 3; i++) + { + if (st->fds[i] >= 0) + { + close(st->fds[i]); + } + } + + free(st); + return 0; +} + +/**************************************************************************** + * Name: test_tcp_peername_ipv6 + * + * Description: + * On a connection over ::1, getpeername() of each end gives the address + * and the port that getsockname() of the other end gives. + * + ****************************************************************************/ + +void test_tcp_peername_ipv6(FAR void **state) +{ + FAR struct tcp_peername_state_s *st = *state; + struct sockaddr_in6 listen_addr; + struct sockaddr_in6 local; + struct sockaddr_in6 peer; + socklen_t addrlen; + struct pollfd pfd; + + st->fds[0] = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0); + assert_return_code(st->fds[0], errno); + nettest_lo_addr((FAR struct sockaddr *)&listen_addr, AF_INET6); + listen_addr.sin6_port = 0; + assert_return_code(bind(st->fds[0], (FAR struct sockaddr *)&listen_addr, + sizeof(listen_addr)), errno); + assert_return_code(listen(st->fds[0], 1), errno); + addrlen = sizeof(listen_addr); + assert_return_code(getsockname(st->fds[0], + (FAR struct sockaddr *)&listen_addr, + &addrlen), errno); + + st->fds[1] = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0); + assert_return_code(st->fds[1], errno); + assert_return_code(connect(st->fds[1], + (FAR struct sockaddr *)&listen_addr, + sizeof(listen_addr)), errno); + pfd.fd = st->fds[0]; + pfd.events = POLLIN; + pfd.revents = 0; + assert_int_equal(poll(&pfd, 1, TEST_POLL), 1); + st->fds[2] = accept4(st->fds[0], NULL, NULL, SOCK_CLOEXEC); + assert_return_code(st->fds[2], errno); + + /* The client: its peer is the listener's address and port */ + + addrlen = sizeof(local); + assert_return_code(getsockname(st->fds[1], (FAR struct sockaddr *)&local, + &addrlen), errno); + addrlen = sizeof(peer); + memset(&peer, 0, sizeof(peer)); + assert_return_code(getpeername(st->fds[1], (FAR struct sockaddr *)&peer, + &addrlen), errno); + assert_int_equal(addrlen, sizeof(peer)); + assert_int_equal(peer.sin6_family, AF_INET6); + assert_true(IN6_IS_ADDR_LOOPBACK(&peer.sin6_addr)); + assert_int_equal(ntohs(peer.sin6_port), ntohs(listen_addr.sin6_port)); + assert_int_not_equal(ntohs(peer.sin6_port), ntohs(local.sin6_port)); + + /* The accepted end: its peer is the client's port */ + + addrlen = sizeof(peer); + memset(&peer, 0, sizeof(peer)); + assert_return_code(getpeername(st->fds[2], (FAR struct sockaddr *)&peer, + &addrlen), errno); + assert_true(IN6_IS_ADDR_LOOPBACK(&peer.sin6_addr)); + assert_int_equal(ntohs(peer.sin6_port), ntohs(local.sin6_port)); +} diff --git a/testing/nettest/udp/test_udp_family.c b/testing/nettest/udp/test_udp_family.c index d3f0dbffc..60e4a1614 100644 --- a/testing/nettest/udp/test_udp_family.c +++ b/testing/nettest/udp/test_udp_family.c @@ -134,6 +134,7 @@ void test_udp_family_v6addr(FAR void **state) * Description: * connect() to [::]:p connects to ::1, as connect() to 0.0.0.0 connects * to 127.0.0.1: a UDP socket of AF_INET6 on [::]:p gets the datagram. + * getpeername() gives the port p. * ****************************************************************************/ @@ -141,6 +142,7 @@ void test_udp_family_connect(FAR void **state) { struct sockaddr_in6 from; struct sockaddr_in6 addr; + socklen_t addrlen; int port = TEST_UDP_PORT + 13; int ret; int err; @@ -158,6 +160,11 @@ void test_udp_family_connect(FAR void **state) addr.sin6_addr = in6addr_any; ret = connect(sd, (FAR struct sockaddr *)&addr, sizeof(addr)); assert_return_code(ret, errno); + addrlen = sizeof(from); + memset(&from, 0, sizeof(from)); + ret = getpeername(sd, (FAR struct sockaddr *)&from, &addrlen); + assert_return_code(ret, errno); + assert_int_equal(ntohs(from.sin6_port), port); ret = send(sd, "v6", 2, 0); assert_return_code(ret, errno); usleep(TEST_UDP_WAIT_US);
