Skip to content

Commit

Permalink
tests: net: socket: tcp: move to new ztest API
Browse files Browse the repository at this point in the history
Move net/socket/tcp tests to use new ztest API
TEST=twister -T tests/net/socket/tcp

Signed-off-by: Hu Zhenyu <[email protected]>
  • Loading branch information
hunterhu3000 authored and nashif committed Aug 29, 2022
1 parent f123094 commit dcda3ea
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 60 deletions.
1 change: 1 addition & 0 deletions tests/net/socket/tcp/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ CONFIG_NET_TCP_RETRY_COUNT=3
CONFIG_NET_TCP_INIT_RETRANSMISSION_TIMEOUT=120

CONFIG_ZTEST=y
CONFIG_ZTEST_NEW_API=y
CONFIG_ZTEST_STACK_SIZE=2048

CONFIG_NET_CONTEXT_RCVTIMEO=y
Expand Down
112 changes: 52 additions & 60 deletions tests/net/socket/tcp/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ static void test_context_cleanup(void)
}


void test_v4_send_recv(void)
ZTEST_USER(net_socket_tcp, test_v4_send_recv)
{
/* Test if send() and recv() work on a ipv4 stream socket. */
int c_sock;
Expand Down Expand Up @@ -243,7 +243,7 @@ void test_v4_send_recv(void)
k_sleep(TCP_TEARDOWN_TIMEOUT);
}

void test_v6_send_recv(void)
ZTEST_USER(net_socket_tcp, test_v6_send_recv)
{
/* Test if send() and recv() work on a ipv6 stream socket. */
int c_sock;
Expand Down Expand Up @@ -413,7 +413,19 @@ static void restore_packet_loss_ratio(void)
"Error setting packet drop rate");
}

void test_v4_broken_link(void)
ZTEST(net_socket_tcp, test_v4_send_recv_large_1)
{
test_v4_send_recv_large();
}

ZTEST(net_socket_tcp, test_v4_send_recv_large_2)
{
set_packet_loss_ratio();
test_v4_send_recv_large();
restore_packet_loss_ratio();
}

ZTEST(net_socket_tcp, test_v4_broken_link)
{
/* Test if the data stops transmitting after the send returned with a timeout. */
int c_sock;
Expand All @@ -436,6 +448,8 @@ void test_v4_broken_link(void)
int rv;
uint8_t rx_buf[10];

restore_packet_loss_ratio();

prepare_sock_tcp_v4(CONFIG_NET_CONFIG_MY_IPV4_ADDR, ANY_PORT,
&c_sock, &c_saddr);
prepare_sock_tcp_v4(CONFIG_NET_CONFIG_MY_IPV4_ADDR, SERVER_PORT,
Expand Down Expand Up @@ -493,9 +507,11 @@ void test_v4_broken_link(void)
test_close(c_sock);
test_close(new_sock);
test_close(s_sock);

restore_packet_loss_ratio();
}

void test_v4_sendto_recvfrom(void)
ZTEST_USER(net_socket_tcp, test_v4_sendto_recvfrom)
{
int c_sock;
int s_sock;
Expand Down Expand Up @@ -533,7 +549,7 @@ void test_v4_sendto_recvfrom(void)
k_sleep(TCP_TEARDOWN_TIMEOUT);
}

void test_v6_sendto_recvfrom(void)
ZTEST_USER(net_socket_tcp, test_v6_sendto_recvfrom)
{
int c_sock;
int s_sock;
Expand Down Expand Up @@ -572,7 +588,7 @@ void test_v6_sendto_recvfrom(void)
k_sleep(TCP_TEARDOWN_TIMEOUT);
}

void test_v4_sendto_recvfrom_null_dest(void)
ZTEST_USER(net_socket_tcp, test_v4_sendto_recvfrom_null_dest)
{
/* For a stream socket, sendto() should ignore NULL dest address */
int c_sock;
Expand Down Expand Up @@ -607,7 +623,7 @@ void test_v4_sendto_recvfrom_null_dest(void)
k_sleep(TCP_TEARDOWN_TIMEOUT);
}

void test_v6_sendto_recvfrom_null_dest(void)
ZTEST_USER(net_socket_tcp, test_v6_sendto_recvfrom_null_dest)
{
/* For a stream socket, sendto() should ignore NULL dest address */
int c_sock;
Expand Down Expand Up @@ -667,7 +683,7 @@ void _test_recv_enotconn(int c_sock, int s_sock)
k_sleep(TCP_TEARDOWN_TIMEOUT);
}

void test_v4_recv_enotconn(void)
ZTEST_USER(net_socket_tcp, test_v4_recv_enotconn)
{
/* For a stream socket, recv() without connect() or accept()
* should lead to ENOTCONN.
Expand All @@ -685,7 +701,7 @@ void test_v4_recv_enotconn(void)
_test_recv_enotconn(c_sock, s_sock);
}

void test_v6_recv_enotconn(void)
ZTEST_USER(net_socket_tcp, test_v6_recv_enotconn)
{
/* For a stream socket, recv() without connect() or accept()
* should lead to ENOTCONN.
Expand All @@ -703,7 +719,7 @@ void test_v6_recv_enotconn(void)
_test_recv_enotconn(c_sock, s_sock);
}

void test_shutdown_rd_synchronous(void)
ZTEST_USER(net_socket_tcp, test_shutdown_rd_synchronous)
{
/* recv() after shutdown(..., ZSOCK_SHUT_RD) should return 0 (EOF).
*/
Expand Down Expand Up @@ -755,7 +771,7 @@ static void shutdown_work(struct k_work *work)
shutdown(data->fd, data->how);
}

void test_shutdown_rd_while_recv(void)
ZTEST(net_socket_tcp, test_shutdown_rd_while_recv)
{
/* Blocking recv() should return EOF after shutdown(..., ZSOCK_SHUT_RD) is
* called from another thread.
Expand Down Expand Up @@ -799,7 +815,7 @@ void test_shutdown_rd_while_recv(void)
test_context_cleanup();
}

void test_open_close_immediately(void)
ZTEST(net_socket_tcp, test_open_close_immediately)
{
/* Test if socket closing works if done immediately after
* receiving SYN.
Expand Down Expand Up @@ -857,7 +873,7 @@ void test_open_close_immediately(void)
test_context_cleanup();
}

void test_connect_timeout(void)
ZTEST(net_socket_tcp, test_connect_timeout)
{
/* Test if socket connect fails when there is not communication
* possible.
Expand All @@ -868,6 +884,8 @@ void test_connect_timeout(void)
int c_sock;
int rv;

restore_packet_loss_ratio();

prepare_sock_tcp_v4(CONFIG_NET_CONFIG_MY_IPV4_ADDR, ANY_PORT,
&c_sock, &c_saddr);

Expand All @@ -892,11 +910,13 @@ void test_connect_timeout(void)

zassert_equal(count_after, 0,
"net_context still in use");

restore_packet_loss_ratio();
}

#define TCP_CLOSE_FAILURE_TIMEOUT 90000

void test_close_obstructed(void)
ZTEST(net_socket_tcp, test_z_close_obstructed)
{
/* Test if socket closing even when there is not communication
* possible any more
Expand All @@ -912,6 +932,8 @@ void test_close_obstructed(void)
int dropped_packets_before = 0;
int dropped_packets_after = 0;

restore_packet_loss_ratio();

prepare_sock_tcp_v4(CONFIG_NET_CONFIG_MY_IPV4_ADDR, ANY_PORT,
&c_sock, &c_saddr);
prepare_sock_tcp_v4(CONFIG_NET_CONFIG_MY_IPV4_ADDR, SERVER_PORT,
Expand Down Expand Up @@ -964,9 +986,11 @@ void test_close_obstructed(void)

zassert_equal(dropped_packets_before, dropped_packets_after,
"packets after close");

restore_packet_loss_ratio();
}

void test_v4_accept_timeout(void)
ZTEST_USER(net_socket_tcp, test_v4_accept_timeout)
{
/* Test if accept() will timeout properly */
int s_sock;
Expand All @@ -993,7 +1017,7 @@ void test_v4_accept_timeout(void)
k_sleep(TCP_TEARDOWN_TIMEOUT);
}

void test_so_type(void)
ZTEST(net_socket_tcp, test_so_type)
{
struct sockaddr_in bind_addr4;
struct sockaddr_in6 bind_addr6;
Expand Down Expand Up @@ -1028,7 +1052,7 @@ void test_so_type(void)
"Not all TCP contexts properly cleaned up");
}

void test_so_protocol(void)
ZTEST(net_socket_tcp, test_so_protocol)
{
struct sockaddr_in bind_addr4;
struct sockaddr_in6 bind_addr6;
Expand Down Expand Up @@ -1057,7 +1081,7 @@ void test_so_protocol(void)
test_context_cleanup();
}

void test_so_rcvbuf(void)
ZTEST(net_socket_tcp, test_so_rcvbuf)
{
struct sockaddr_in bind_addr4;
struct sockaddr_in6 bind_addr6;
Expand Down Expand Up @@ -1099,7 +1123,7 @@ void test_so_rcvbuf(void)
test_context_cleanup();
}

void test_so_sndbuf(void)
ZTEST(net_socket_tcp, test_so_sndbuf)
{
struct sockaddr_in bind_addr4;
struct sockaddr_in6 bind_addr6;
Expand Down Expand Up @@ -1141,7 +1165,7 @@ void test_so_sndbuf(void)
test_context_cleanup();
}

void test_v4_so_rcvtimeo(void)
ZTEST(net_socket_tcp, test_v4_so_rcvtimeo)
{
int c_sock;
int s_sock;
Expand Down Expand Up @@ -1210,7 +1234,7 @@ void test_v4_so_rcvtimeo(void)
test_context_cleanup();
}

void test_v6_so_rcvtimeo(void)
ZTEST(net_socket_tcp, test_v6_so_rcvtimeo)
{
int c_sock;
int s_sock;
Expand Down Expand Up @@ -1301,7 +1325,7 @@ static void test_msg_waitall_tx_work_handler(struct k_work *work)
}
}

void test_v4_msg_waitall(void)
ZTEST(net_socket_tcp, test_v4_msg_waitall)
{
struct test_msg_waitall_data test_data = {
.data = TEST_STR_SMALL,
Expand Down Expand Up @@ -1378,7 +1402,7 @@ void test_v4_msg_waitall(void)
test_context_cleanup();
}

void test_v6_msg_waitall(void)
ZTEST(net_socket_tcp, test_v6_msg_waitall)
{
struct test_msg_waitall_data test_data = {
.data = TEST_STR_SMALL,
Expand Down Expand Up @@ -1476,7 +1500,7 @@ static void spawn_child(int sock)
}
#endif

void test_socket_permission(void)
ZTEST(net_socket_tcp, test_socket_permission)
{
#ifdef CONFIG_USERSPACE
int sock;
Expand Down Expand Up @@ -1514,7 +1538,7 @@ void test_socket_permission(void)
#endif /* CONFIG_USERSPACE */
}

void test_main(void)
static void *setup(void)
{
#ifdef CONFIG_USERSPACE
/* ztest thread inherit permissions from main */
Expand All @@ -1528,39 +1552,7 @@ void test_main(void)
k_thread_priority_set(k_current_get(), K_PRIO_PREEMPT(8));
}

ztest_test_suite(
socket_tcp,
ztest_user_unit_test(test_v4_send_recv),
ztest_user_unit_test(test_v6_send_recv),
ztest_user_unit_test(test_v4_sendto_recvfrom),
ztest_user_unit_test(test_v6_sendto_recvfrom),
ztest_user_unit_test(test_v4_sendto_recvfrom_null_dest),
ztest_user_unit_test(test_v6_sendto_recvfrom_null_dest),
ztest_user_unit_test(test_v4_recv_enotconn),
ztest_user_unit_test(test_v6_recv_enotconn),
ztest_user_unit_test(test_shutdown_rd_synchronous),
ztest_unit_test(test_shutdown_rd_while_recv),
ztest_unit_test(test_open_close_immediately),
ztest_unit_test_setup_teardown(test_connect_timeout,
restore_packet_loss_ratio, restore_packet_loss_ratio),
ztest_unit_test_setup_teardown(test_close_obstructed,
restore_packet_loss_ratio, restore_packet_loss_ratio),
ztest_user_unit_test(test_v4_accept_timeout),
ztest_unit_test(test_so_type),
ztest_unit_test(test_so_protocol),
ztest_unit_test(test_so_rcvbuf),
ztest_unit_test(test_so_sndbuf),
ztest_unit_test(test_v4_so_rcvtimeo),
ztest_unit_test(test_v6_so_rcvtimeo),
ztest_unit_test(test_v4_msg_waitall),
ztest_unit_test(test_v6_msg_waitall),
ztest_user_unit_test(test_socket_permission),
ztest_unit_test(test_v4_send_recv_large),
ztest_unit_test_setup_teardown(test_v4_send_recv_large,
set_packet_loss_ratio, restore_packet_loss_ratio),
ztest_unit_test_setup_teardown(test_v4_broken_link,
restore_packet_loss_ratio, restore_packet_loss_ratio)
);

ztest_run_test_suite(socket_tcp);
return NULL;
}

ZTEST_SUITE(net_socket_tcp, NULL, setup, NULL, NULL, NULL);

0 comments on commit dcda3ea

Please sign in to comment.