Skip to content

Commit

Permalink
testing/nettest: Added two new testcases and a set of public function…
Browse files Browse the repository at this point in the history
…s for packet capture

Add a solinger testcase and a tcpserver error port processing testcase. In addition, a set of public packet capture functions for testing were added.

Signed-off-by: zhangshuai39 <[email protected]>
  • Loading branch information
zs39 committed Jan 3, 2025
1 parent 9d0fb85 commit df3f7f6
Show file tree
Hide file tree
Showing 11 changed files with 722 additions and 39 deletions.
10 changes: 10 additions & 0 deletions testing/nettest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ if(CONFIG_TESTING_NET_TEST)
list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_ipv6.c)
endif()

if(CONFIG_NET_PKT)
list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_rst.c
${CMAKE_CURRENT_LIST_DIR}/utils/nettest_netdump.c)

if(CONFIG_NET_SOLINGER)
list(APPEND SRCS
${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_solinger.c)
endif()
endif()

nuttx_add_application(
NAME
cmocka_net_tcp
Expand Down
11 changes: 10 additions & 1 deletion testing/nettest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ MODULE = $(CONFIG_TESTING_NET_TEST)

ifeq ($(CONFIG_TESTING_NET_TEST),y)

CFLAGS += -I$(NETTEST_UTILS_DIR)
CFLAGS += -I$(NETTEST_UTILS_DIR)

ifeq ($(CONFIG_TESTING_NET_TCP),y)
MAINSRC += tcp/test_tcp.c
Expand All @@ -43,6 +43,15 @@ ifeq ($(CONFIG_NET_IPv6), y)
CSRCS += tcp/test_tcp_connect_ipv6.c
endif

ifeq ($(CONFIG_NET_PKT), y)
CSRCS += tcp/test_tcp_connect_rst.c utils/nettest_netdump.c

ifeq ($(CONFIG_NET_SOLINGER), y)
CSRCS += tcp/test_tcp_connect_solinger.c
endif

endif

endif

ifeq ($(CONFIG_TESTING_NET_OTHERS),y)
Expand Down
10 changes: 10 additions & 0 deletions testing/nettest/tcp/test_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ int main(int argc, FAR char *argv[])
test_tcp_connect_ipv6_setup,
test_tcp_common_teardown),
#endif
#ifdef CONFIG_NET_PKT
#ifdef CONFIG_NET_SOLINGER
cmocka_unit_test_setup_teardown(test_tcp_connect_solinger,
test_tcp_connect_solinger_setup,
test_tcp_common_teardown),
#endif
cmocka_unit_test_setup_teardown(test_tcp_connect_rst,
test_tcp_connect_rst_setup,
test_tcp_common_teardown),
#endif /* CONFIG_NET_PKT */
};

return cmocka_run_group_tests(tcp_tests, test_tcp_group_setup,
Expand Down
44 changes: 42 additions & 2 deletions testing/nettest/tcp/test_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,19 @@

#include <pthread.h>

#include "utils.h"

/****************************************************************************
* Public Types
****************************************************************************/

struct nettest_tcp_state_s
{
int client_fd;
pthread_t server_tid;
int client_fd;
pthread_t server_tid;
#ifdef CONFIG_NET_PKT
struct net_dump_s dump_state;
#endif
};

/****************************************************************************
Expand All @@ -59,6 +64,13 @@ int test_tcp_group_teardown(FAR void **state);

int test_tcp_common_teardown(FAR void **state);

/****************************************************************************
* Name: test_tcp_common_setup
****************************************************************************/

int test_tcp_common_setup(FAR struct nettest_tcp_state_s *tcp_state,
int family, nettest_filter_t filter);

/****************************************************************************
* Name: test_tcp_connect_ipv4_setup
****************************************************************************/
Expand Down Expand Up @@ -86,4 +98,32 @@ int test_tcp_connect_ipv6_setup(FAR void **state);

void test_tcp_connect_ipv6(FAR void **state);
#endif

/****************************************************************************
* Name: test_tcp_connect_solinger_setup
****************************************************************************/

#ifdef CONFIG_NET_PKT
#ifdef CONFIG_NET_SOLINGER
int test_tcp_connect_solinger_setup(FAR void **state);

/****************************************************************************
* Name: test_tcp_connect_solinger
****************************************************************************/

void test_tcp_connect_solinger(FAR void **state);
#endif

/****************************************************************************
* Name: test_tcp_connect_rst_setup
****************************************************************************/

int test_tcp_connect_rst_setup(FAR void **state);

/****************************************************************************
* Name: test_tcp_connect_rst
****************************************************************************/

void test_tcp_connect_rst(FAR void **state);
#endif /* CONFIG_NET_PKT */
#endif /* __APPS_TESTING_NETTEST_TCP_TEST_TCP_H */
59 changes: 59 additions & 0 deletions testing/nettest/tcp/test_tcp_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>

#include <cmocka.h>

#include "test_tcp.h"
#include "utils.h"

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define NETDEV_NAME "lo"
#define TEST_SND_RCV_TIMEOUT 10000

/****************************************************************************
* Public Functions
****************************************************************************/
Expand Down Expand Up @@ -85,5 +94,55 @@ int test_tcp_common_teardown(FAR void **state)
tcp_state->client_fd = -1;
}

#ifdef CONFIG_NET_PKT
nettest_dump_destroy(&tcp_state->dump_state);
#endif

return 0;
}

/****************************************************************************
* Name: test_tcp_common_setup
****************************************************************************/

int test_tcp_common_setup(FAR struct nettest_tcp_state_s *tcp_state,
int family, nettest_filter_t filter)
{
struct timeval tv;
int ret;

assert(family == AF_INET || family == AF_INET6);

tcp_state->client_fd = socket(family, SOCK_STREAM, 0);
assert_return_code(tcp_state->client_fd, errno);

tv.tv_sec = 0;
tv.tv_usec = TEST_SND_RCV_TIMEOUT;
ret = setsockopt(tcp_state->client_fd, SOL_SOCKET, SO_RCVTIMEO,
&tv, sizeof(tv));
if (ret < 0)
{
goto errout;
}

ret = setsockopt(tcp_state->client_fd, SOL_SOCKET, SO_SNDTIMEO,
&tv, sizeof(tv));
if (ret < 0)
{
goto errout;
}

#ifdef CONFIG_NET_PKT
if (filter != NULL)
{
ret = nettest_dump_setup(&tcp_state->dump_state, NETDEV_NAME,
filter, 0);
}
#endif

return ret;

errout:
close(tcp_state->client_fd);
return ret;
}
21 changes: 3 additions & 18 deletions testing/nettest/tcp/test_tcp_connect_ipv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <cmocka.h>
#include <sys/socket.h>
#include <sys/time.h>

#include <cmocka.h>

#include "test_tcp.h"
#include "utils.h"
Expand All @@ -53,23 +53,8 @@
int test_tcp_connect_ipv4_setup(FAR void **state)
{
FAR struct nettest_tcp_state_s *tcp_state = *state;
struct timeval tv;
int ret;

tcp_state->client_fd = socket(PF_INET, SOCK_STREAM, 0);
assert_true(tcp_state->client_fd > 0);

tv.tv_sec = 0;
tv.tv_usec = 10000;
ret = setsockopt(tcp_state->client_fd, SOL_SOCKET,
SO_RCVTIMEO, &tv, sizeof(tv));
assert_return_code(ret, errno);

ret = setsockopt(tcp_state->client_fd, SOL_SOCKET,
SO_SNDTIMEO, &tv, sizeof(tv));
assert_return_code(ret, errno);

return 0;
return test_tcp_common_setup(tcp_state, AF_INET, NULL);
}

/****************************************************************************
Expand Down
19 changes: 2 additions & 17 deletions testing/nettest/tcp/test_tcp_connect_ipv6.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>

#include <cmocka.h>
#include <sys/time.h>

#include "test_tcp.h"
#include "utils.h"
Expand All @@ -52,23 +52,8 @@
int test_tcp_connect_ipv6_setup(FAR void **state)
{
FAR struct nettest_tcp_state_s *tcp_state = *state;
struct timeval tv;
int ret;

tcp_state->client_fd = socket(PF_INET6, SOCK_STREAM, 0);
assert_true(tcp_state->client_fd > 0);

tv.tv_sec = 0;
tv.tv_usec = 10000;
ret = setsockopt(tcp_state->client_fd, SOL_SOCKET,
SO_RCVTIMEO, &tv, sizeof(tv));
assert_return_code(ret, errno);

ret = setsockopt(tcp_state->client_fd, SOL_SOCKET,
SO_SNDTIMEO, &tv, sizeof(tv));
assert_return_code(ret, errno);

return 0;
return test_tcp_common_setup(tcp_state, AF_INET6, NULL);
}

/****************************************************************************
Expand Down
92 changes: 92 additions & 0 deletions testing/nettest/tcp/test_tcp_connect_rst.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/****************************************************************************
* apps/testing/nettest/tcp/test_tcp_connect_rst.c
*
* 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 <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <cmocka.h>

#include <nuttx/net/tcp.h>

#include "test_tcp.h"
#include "utils.h"

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define TEST_WRONG_PORT 9999

/****************************************************************************
* Private Functions
****************************************************************************/

static bool filter(FAR uint8_t *buf)
{
return nettest_filter_tcp_state(NET_LL_LOOPBACK, TCP_RST, buf);
}

/****************************************************************************
* Public Functions
****************************************************************************/

/****************************************************************************
* Name: test_tcp_connect_rst_setup
****************************************************************************/

int test_tcp_connect_rst_setup(FAR void **state)
{
FAR struct nettest_tcp_state_s *tcp_state = *state;

return test_tcp_common_setup(tcp_state, AF_INET, filter);
}

/****************************************************************************
* Name: test_tcp_connect_rst
****************************************************************************/

void test_tcp_connect_rst(FAR void **state)
{
FAR struct nettest_tcp_state_s *tcp_state = *state;
struct sockaddr_in myaddr;
int ret;

assert_false(TEST_WRONG_PORT == CONFIG_TESTING_NET_TCP_PORT);

myaddr.sin_family = AF_INET;
myaddr.sin_port = htons(TEST_WRONG_PORT);
myaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

ret = connect(tcp_state->client_fd, (FAR struct sockaddr *)&myaddr,
sizeof(struct sockaddr_in));
assert_true(ret < 0 && errno == ECONNREFUSED);

close(tcp_state->client_fd);
tcp_state->client_fd = -1;

nettest_netdump_stop(&tcp_state->dump_state);

assert_int_equal(sq_count(&tcp_state->dump_state.data), 1);
}
Loading

0 comments on commit df3f7f6

Please sign in to comment.