Skip to content

Commit 5ff9666

Browse files
casaroliacassis
authored andcommitted
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 <marco.casaroli@gmail.com>
1 parent 3eff818 commit 5ff9666

6 files changed

Lines changed: 195 additions & 2 deletions

File tree

‎testing/nettest/CMakeLists.txt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ if(CONFIG_TESTING_NET_TEST)
3636
endif()
3737

3838
if(CONFIG_NET_IPv6)
39-
list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_ipv6.c)
39+
list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_ipv6.c
40+
${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_peername.c)
4041
endif()
4142

4243
if(CONFIG_NET_IPv4)

‎testing/nettest/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ CSRCS += tcp/test_tcp_close.c
4545
endif
4646

4747
ifeq ($(CONFIG_NET_IPv6), y)
48-
CSRCS += tcp/test_tcp_connect_ipv6.c
48+
CSRCS += tcp/test_tcp_connect_ipv6.c tcp/test_tcp_peername.c
4949
endif
5050

5151
ifeq ($(CONFIG_NET_IPv4), y)

‎testing/nettest/tcp/test_tcp.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ int main(int argc, FAR char *argv[])
9292
cmocka_unit_test_setup_teardown(test_tcp_connect_ipv6,
9393
test_tcp_connect_ipv6_setup,
9494
test_tcp_common_teardown),
95+
cmocka_unit_test_setup_teardown(test_tcp_peername_ipv6,
96+
test_tcp_peername_setup,
97+
test_tcp_peername_teardown),
9598
#endif
9699
#ifdef CONFIG_NET_IPv4
97100
cmocka_unit_test_setup_teardown(test_tcp_reset_recv,

‎testing/nettest/tcp/test_tcp.h‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,24 @@ int test_tcp_connect_ipv6_setup(FAR void **state);
207207
****************************************************************************/
208208

209209
void test_tcp_connect_ipv6(FAR void **state);
210+
211+
/****************************************************************************
212+
* Name: test_tcp_peername_setup
213+
****************************************************************************/
214+
215+
int test_tcp_peername_setup(FAR void **state);
216+
217+
/****************************************************************************
218+
* Name: test_tcp_peername_teardown
219+
****************************************************************************/
220+
221+
int test_tcp_peername_teardown(FAR void **state);
222+
223+
/****************************************************************************
224+
* Name: test_tcp_peername_ipv6
225+
****************************************************************************/
226+
227+
void test_tcp_peername_ipv6(FAR void **state);
210228
#endif
211229

212230
#ifdef CONFIG_NET_IPv4
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/****************************************************************************
2+
* apps/testing/nettest/tcp/test_tcp_peername.c
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed to the Apache Software Foundation (ASF) under one or more
7+
* contributor license agreements. See the NOTICE file distributed with
8+
* this work for additional information regarding copyright ownership. The
9+
* ASF licenses this file to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance with the
11+
* License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18+
* License for the specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
****************************************************************************/
22+
23+
/****************************************************************************
24+
* Included Files
25+
****************************************************************************/
26+
27+
#include <errno.h>
28+
#include <netinet/in.h>
29+
#include <poll.h>
30+
#include <setjmp.h>
31+
#include <stdarg.h>
32+
#include <stddef.h>
33+
#include <stdint.h>
34+
#include <stdlib.h>
35+
#include <string.h>
36+
#include <unistd.h>
37+
#include <sys/socket.h>
38+
#include <cmocka.h>
39+
40+
#include "test_tcp.h"
41+
#include "utils.h"
42+
43+
/****************************************************************************
44+
* Pre-processor Definitions
45+
****************************************************************************/
46+
47+
#define TEST_POLL 1000 /* ms, loopback answers at once */
48+
49+
/****************************************************************************
50+
* Private Types
51+
****************************************************************************/
52+
53+
struct tcp_peername_state_s
54+
{
55+
int fds[3];
56+
};
57+
58+
/****************************************************************************
59+
* Public Functions
60+
****************************************************************************/
61+
62+
/****************************************************************************
63+
* Name: test_tcp_peername_setup
64+
****************************************************************************/
65+
66+
int test_tcp_peername_setup(FAR void **state)
67+
{
68+
FAR struct tcp_peername_state_s *st = malloc(sizeof(*st));
69+
70+
assert_non_null(st);
71+
st->fds[0] = -1;
72+
st->fds[1] = -1;
73+
st->fds[2] = -1;
74+
*state = st;
75+
return 0;
76+
}
77+
78+
/****************************************************************************
79+
* Name: test_tcp_peername_teardown
80+
****************************************************************************/
81+
82+
int test_tcp_peername_teardown(FAR void **state)
83+
{
84+
FAR struct tcp_peername_state_s *st = *state;
85+
int i;
86+
87+
for (i = 0; i < 3; i++)
88+
{
89+
if (st->fds[i] >= 0)
90+
{
91+
close(st->fds[i]);
92+
}
93+
}
94+
95+
free(st);
96+
return 0;
97+
}
98+
99+
/****************************************************************************
100+
* Name: test_tcp_peername_ipv6
101+
*
102+
* Description:
103+
* On a connection over ::1, getpeername() of each end gives the address
104+
* and the port that getsockname() of the other end gives.
105+
*
106+
****************************************************************************/
107+
108+
void test_tcp_peername_ipv6(FAR void **state)
109+
{
110+
FAR struct tcp_peername_state_s *st = *state;
111+
struct sockaddr_in6 listen_addr;
112+
struct sockaddr_in6 local;
113+
struct sockaddr_in6 peer;
114+
socklen_t addrlen;
115+
struct pollfd pfd;
116+
117+
st->fds[0] = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
118+
assert_return_code(st->fds[0], errno);
119+
nettest_lo_addr((FAR struct sockaddr *)&listen_addr, AF_INET6);
120+
listen_addr.sin6_port = 0;
121+
assert_return_code(bind(st->fds[0], (FAR struct sockaddr *)&listen_addr,
122+
sizeof(listen_addr)), errno);
123+
assert_return_code(listen(st->fds[0], 1), errno);
124+
addrlen = sizeof(listen_addr);
125+
assert_return_code(getsockname(st->fds[0],
126+
(FAR struct sockaddr *)&listen_addr,
127+
&addrlen), errno);
128+
129+
st->fds[1] = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
130+
assert_return_code(st->fds[1], errno);
131+
assert_return_code(connect(st->fds[1],
132+
(FAR struct sockaddr *)&listen_addr,
133+
sizeof(listen_addr)), errno);
134+
pfd.fd = st->fds[0];
135+
pfd.events = POLLIN;
136+
pfd.revents = 0;
137+
assert_int_equal(poll(&pfd, 1, TEST_POLL), 1);
138+
st->fds[2] = accept4(st->fds[0], NULL, NULL, SOCK_CLOEXEC);
139+
assert_return_code(st->fds[2], errno);
140+
141+
/* The client: its peer is the listener's address and port */
142+
143+
addrlen = sizeof(local);
144+
assert_return_code(getsockname(st->fds[1], (FAR struct sockaddr *)&local,
145+
&addrlen), errno);
146+
addrlen = sizeof(peer);
147+
memset(&peer, 0, sizeof(peer));
148+
assert_return_code(getpeername(st->fds[1], (FAR struct sockaddr *)&peer,
149+
&addrlen), errno);
150+
assert_int_equal(addrlen, sizeof(peer));
151+
assert_int_equal(peer.sin6_family, AF_INET6);
152+
assert_true(IN6_IS_ADDR_LOOPBACK(&peer.sin6_addr));
153+
assert_int_equal(ntohs(peer.sin6_port), ntohs(listen_addr.sin6_port));
154+
assert_int_not_equal(ntohs(peer.sin6_port), ntohs(local.sin6_port));
155+
156+
/* The accepted end: its peer is the client's port */
157+
158+
addrlen = sizeof(peer);
159+
memset(&peer, 0, sizeof(peer));
160+
assert_return_code(getpeername(st->fds[2], (FAR struct sockaddr *)&peer,
161+
&addrlen), errno);
162+
assert_true(IN6_IS_ADDR_LOOPBACK(&peer.sin6_addr));
163+
assert_int_equal(ntohs(peer.sin6_port), ntohs(local.sin6_port));
164+
}

‎testing/nettest/udp/test_udp_family.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,15 @@ void test_udp_family_v6addr(FAR void **state)
134134
* Description:
135135
* connect() to [::]:p connects to ::1, as connect() to 0.0.0.0 connects
136136
* to 127.0.0.1: a UDP socket of AF_INET6 on [::]:p gets the datagram.
137+
* getpeername() gives the port p.
137138
*
138139
****************************************************************************/
139140

140141
void test_udp_family_connect(FAR void **state)
141142
{
142143
struct sockaddr_in6 from;
143144
struct sockaddr_in6 addr;
145+
socklen_t addrlen;
144146
int port = TEST_UDP_PORT + 13;
145147
int ret;
146148
int err;
@@ -158,6 +160,11 @@ void test_udp_family_connect(FAR void **state)
158160
addr.sin6_addr = in6addr_any;
159161
ret = connect(sd, (FAR struct sockaddr *)&addr, sizeof(addr));
160162
assert_return_code(ret, errno);
163+
addrlen = sizeof(from);
164+
memset(&from, 0, sizeof(from));
165+
ret = getpeername(sd, (FAR struct sockaddr *)&from, &addrlen);
166+
assert_return_code(ret, errno);
167+
assert_int_equal(ntohs(from.sin6_port), port);
161168
ret = send(sd, "v6", 2, 0);
162169
assert_return_code(ret, errno);
163170
usleep(TEST_UDP_WAIT_US);

0 commit comments

Comments
 (0)