Skip to content

Commit

Permalink
testing/nettest: Add utils directory and two tcp testcases to the tes…
Browse files Browse the repository at this point in the history
…ting framework

Based on the original directory structure, an additional utils directory is added. It store public functions which used by multiple testcases, such as the tcp server-related functions added this time. In addition, two testcases for testing tcp connections have been added.

Signed-off-by: zhangshuai39 <[email protected]>
  • Loading branch information
zs39 committed Jan 11, 2025
1 parent 46f77a6 commit 0b2fa40
Show file tree
Hide file tree
Showing 10 changed files with 792 additions and 9 deletions.
12 changes: 10 additions & 2 deletions testing/nettest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@

if(CONFIG_TESTING_NET_TEST)
if(CONFIG_TESTING_NET_TCP)
set(SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp.c
${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_common.c)
set(SRCS
${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp.c
${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_common.c
${CMAKE_CURRENT_LIST_DIR}/utils/nettest_tcpserver.c)

if(CONFIG_NET_IPv4)
list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_ipv4.c)
endif()

if(CONFIG_NET_IPv6)
list(APPEND SRCS ${CMAKE_CURRENT_LIST_DIR}/tcp/test_tcp_connect_ipv6.c)
endif()

nuttx_add_application(
NAME
cmocka_net_tcp
Expand All @@ -38,6 +44,8 @@ if(CONFIG_TESTING_NET_TEST)
${CONFIG_TESTING_NET_TEST}
DEPENDS
cmocka
INCLUDE_DIRECTORIES
${CMAKE_CURRENT_LIST_DIR}/utils
SRCS
${SRCS})
endif()
Expand Down
9 changes: 9 additions & 0 deletions testing/nettest/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ config TESTING_NET_TEST_STACKSIZE
config TESTING_NET_TCP
bool "Enable cmocka net tcp test"
depends on NET_TCP && NET_TCPBACKLOG
depends on NET_IPv4 || NET_IPv6
default y

if TESTING_NET_TCP

config TESTING_NET_TCP_PORT
int "TCP port of test server"
default 5471

endif

endif
10 changes: 9 additions & 1 deletion testing/nettest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,29 @@

include $(APPDIR)/Make.defs

NETTEST_UTILS_DIR := $(APPDIR)/testing/nettest/utils

PRIORITY = $(CONFIG_TESTING_NET_TEST_PRIORITY)
STACKSIZE = $(CONFIG_TESTING_NET_TEST_STACKSIZE)
MODULE = $(CONFIG_TESTING_NET_TEST)

ifeq ($(CONFIG_TESTING_NET_TEST),y)

CFLAGS += -I$(NETTEST_UTILS_DIR)

ifeq ($(CONFIG_TESTING_NET_TCP),y)
MAINSRC += tcp/test_tcp.c
PROGNAME += cmocka_net_tcp
CSRCS += tcp/test_tcp_common.c
CSRCS += tcp/test_tcp_common.c utils/nettest_tcpserver.c

ifeq ($(CONFIG_NET_IPv4), y)
CSRCS += tcp/test_tcp_connect_ipv4.c
endif

ifeq ($(CONFIG_NET_IPv6), y)
CSRCS += tcp/test_tcp_connect_ipv6.c
endif

endif

endif
Expand Down
12 changes: 9 additions & 3 deletions testing/nettest/tcp/test_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
* Included Files
****************************************************************************/

#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <setjmp.h>
#include <cmocka.h>

#include "test_tcp.h"
Expand All @@ -39,8 +39,14 @@ int main(int argc, FAR char *argv[])
const struct CMUnitTest tcp_tests[] =
{
#ifdef CONFIG_NET_IPv4
cmocka_unit_test_setup(test_tcp_connect_ipv4,
test_tcp_connect_ipv4_setup),
cmocka_unit_test_setup_teardown(test_tcp_connect_ipv4,
test_tcp_connect_ipv4_setup,
test_tcp_common_teardown),
#endif
#ifdef CONFIG_NET_IPv6
cmocka_unit_test_setup_teardown(test_tcp_connect_ipv6,
test_tcp_connect_ipv6_setup,
test_tcp_common_teardown),
#endif
};

Expand Down
38 changes: 35 additions & 3 deletions testing/nettest/tcp/test_tcp.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,21 @@
* Included Files
****************************************************************************/

#include <nuttx/config.h>
#include <pthread.h>

#include <nuttx/compiler.h>
/****************************************************************************
* Public Types
****************************************************************************/

struct nettest_tcp_state_s
{
int client_fd;
pthread_t server_tid;
};

/****************************************************************************
* Public Function Prototypes
****************************************************************************/

/****************************************************************************
* Name: test_tcp_group_setup
Expand All @@ -41,6 +53,12 @@ int test_tcp_group_setup(FAR void **state);

int test_tcp_group_teardown(FAR void **state);

/****************************************************************************
* Name: test_tcp_common_teardown
****************************************************************************/

int test_tcp_common_teardown(FAR void **state);

/****************************************************************************
* Name: test_tcp_connect_ipv4_setup
****************************************************************************/
Expand All @@ -53,5 +71,19 @@ int test_tcp_connect_ipv4_setup(FAR void **state);
****************************************************************************/

void test_tcp_connect_ipv4(FAR void **state);
#endif /* CONFIG_NET_IPv4 */
#endif

/****************************************************************************
* Name: test_tcp_connect_ipv6_setup
****************************************************************************/

#ifdef CONFIG_NET_IPv6
int test_tcp_connect_ipv6_setup(FAR void **state);

/****************************************************************************
* Name: test_tcp_connect_ipv6
****************************************************************************/

void test_tcp_connect_ipv6(FAR void **state);
#endif
#endif /* __APPS_TESTING_NETTEST_TCP_TEST_TCP_H */
42 changes: 42 additions & 0 deletions testing/nettest/tcp/test_tcp_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,16 @@
* Included Files
****************************************************************************/

#include <netinet/in.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <cmocka.h>

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

/****************************************************************************
* Public Functions
Expand All @@ -34,6 +43,14 @@

int test_tcp_group_setup(FAR void **state)
{
FAR struct nettest_tcp_state_s *tcp_state = zalloc(sizeof(*tcp_state));
assert_non_null(tcp_state);

*state = tcp_state;

tcp_state->server_tid = nettest_create_tcp_lo_server();
assert_return_code(tcp_state->server_tid, errno);

return 0;
}

Expand All @@ -43,5 +60,30 @@ int test_tcp_group_setup(FAR void **state)

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

int ret = nettest_destroy_tcp_lo_server(tcp_state->server_tid);
assert_return_code(ret, errno);

free(tcp_state);

return 0;
}

/****************************************************************************
* Name: test_tcp_common_teardown
****************************************************************************/

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

if (tcp_state->client_fd > 0)
{
shutdown(tcp_state->client_fd, SHUT_RDWR);
close(tcp_state->client_fd);
tcp_state->client_fd = -1;
}

return 0;
}
62 changes: 62 additions & 0 deletions testing/nettest/tcp/test_tcp_connect_ipv4.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,25 @@
* Included Files
****************************************************************************/

#include <netinet/in.h>
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <cmocka.h>
#include <sys/socket.h>
#include <sys/time.h>

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

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

#define TEST_LOOP_CNT 5
#define TEST_BUFFER_SIZE 80
#define TEST_MSG "loopback message"

/****************************************************************************
* Public Functions
Expand All @@ -34,6 +52,23 @@

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;
}

Expand All @@ -43,4 +78,31 @@ int test_tcp_connect_ipv4_setup(FAR void **state)

void test_tcp_connect_ipv4(FAR void **state)
{
FAR struct nettest_tcp_state_s *tcp_state = *state;
struct sockaddr_in myaddr;
char outbuf[TEST_BUFFER_SIZE];
char inbuf[TEST_BUFFER_SIZE];
int addrlen;
int ret;
int len;

addrlen = nettest_lo_addr((FAR struct sockaddr *)&myaddr, AF_INET);

ret = connect(tcp_state->client_fd, (FAR struct sockaddr *)&myaddr,
addrlen);
assert_return_code(ret, errno);

for (int i = 0; i < TEST_LOOP_CNT; i++)
{
strcpy(outbuf, TEST_MSG);
len = strlen(outbuf);
ret = send(tcp_state->client_fd, outbuf, len, 0);
assert_true(ret == len);

ret = recv(tcp_state->client_fd, inbuf, TEST_BUFFER_SIZE, 0);
assert_true(ret == len);

ret = strncmp(inbuf, TEST_MSG, len);
assert_true(ret == 0);
}
}
Loading

0 comments on commit 0b2fa40

Please sign in to comment.