Skip to content

Commit

Permalink
apps/nettest: add network test
Browse files Browse the repository at this point in the history
add network test
getaddrinfo, gethostbyname, getnameinfo...
  • Loading branch information
pillip8282 authored and an4967 committed Oct 15, 2021
1 parent e4c6018 commit e054817
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 30 deletions.
2 changes: 1 addition & 1 deletion apps/examples/nettest/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ THREADEXEC = TASH_EXECMD_ASYNC
# transport layer (TCP / UDP) / IP multicast functionality test example

ASRCS =
CSRCS = nettest_stress.c
CSRCS = nettest_stress.c test_main.c
MAINSRC = nettest.c

AOBJS = $(ASRCS:.S=$(OBJEXT))
Expand Down
12 changes: 9 additions & 3 deletions apps/examples/nettest/nettest.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ uint32_t total_data;

#define NETTEST_SERVER_MODE 1
#define NETTEST_CLIENT_MODE 2
#define NETTEST_INTERNAL_MODE 3
#define NETTEST_PROTO_TCP "tcp"
#define NETTEST_PROTO_UDP "udp"
#define NETTEST_PROTO_BROADCAST "brc"
Expand All @@ -170,6 +171,7 @@ static void show_usage(void)
printf("MODE:\n");
printf("\t1: server\n");
printf("\t2: client\n");
printf("\t3: internal\n");

printf("PROTOCOL\n");
printf("\ttcp: TCP\n");
Expand Down Expand Up @@ -477,7 +479,7 @@ void ipmcast_receiver_thread(int num_packets, const char *intf)
printf("[MCASTSERV] fail: adding multicast group %d\n", errno);
goto out_with_socket;
}

if (bind(sd, (struct sockaddr *)&localSock, sizeof(localSock))) {
printf("[MCASTSERV] ERR: binding datagram socket\n");
goto out_with_socket;
Expand Down Expand Up @@ -832,6 +834,7 @@ void tcp_client_thread(int num_packets, uint32_t sleep_time)
}

extern void nettest_stress(char *addr, int port);
extern int network_internal_test(void);

/* Sample App to test Transport Layer (TCP / UDP) / IP Multicast Functionality */
#ifdef CONFIG_BUILD_KERNEL
Expand All @@ -847,12 +850,16 @@ int nettest_main(int argc, char *argv[])
/* pps - packet per second, default value 1 */
uint32_t pps = 1;

if (argc == 2 && atoi(argv[1]) == NETTEST_INTERNAL_MODE) {
network_internal_test();
return 0;
}
if (argc < 5) {
goto err_with_input;
}

mode = atoi(argv[1]);
if (mode != NETTEST_SERVER_MODE && mode != NETTEST_CLIENT_MODE) {
if (mode != NETTEST_SERVER_MODE && mode != NETTEST_CLIENT_MODE && mode != NETTEST_INTERNAL_MODE) {
goto err_with_input;
}

Expand Down Expand Up @@ -930,4 +937,3 @@ int nettest_main(int argc, char *argv[])
show_usage();
return -1;
}

264 changes: 264 additions & 0 deletions apps/examples/nettest/test_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
/****************************************************************************
*
* Copyright 2021 Samsung Electronics All Rights Reserved.
*
* Licensed 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.
*
****************************************************************************/
#include <lwip/sockets.h>
#include <netdb.h>
#include <tinyara/config.h>
#include <stress_tool/st_perf.h>

#define NT_TEST_TRIAL 2
#define NT_MAXHOST 1024 /* refer NI_MAXHOST */
#define NT_MAXSERV 64 /* refer tmpserv in lwip_getnameinfo */
#define TC_HOST_NAME "www.google.com"
#define TC_SERVICE "80"
#define NT_STR_DNS_ADDR "8.8.8.8"

char g_hostname[NT_MAXHOST]; // stack overflow can be happened if set this value local variable.
char g_serv[NT_MAXSERV];

static void _print_addrinfo(struct addrinfo *addr)
{
struct addrinfo *rp;
char buf[64];
printf("=====================================\n");
printf("family\tsocket type\tprotocol\taddress\n");
printf("-------------------------------------\n");
for (rp = addr; rp != NULL; rp = rp->ai_next) {
printf("%d\t%d\t%d\t", rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (rp->ai_family == AF_INET) {
inet_ntop(rp->ai_family, &(((struct sockaddr_in *)(rp->ai_addr))->sin_addr), buf, sizeof(buf));
printf("%s\n", buf);
} else if (rp->ai_family == AF_INET6) {
inet_ntop(rp->ai_family, &(((struct sockaddr_in6 *)(rp->ai_addr))->sin6_addr), buf, sizeof(buf));
printf("%s\n", buf);
} else {
printf("\n");
}
}
printf("=====================================\n\n");
}

static void _print_serv_info(const char *host, const char *service)
{
printf("=====================================\n");
printf("host\tservice\n");
printf("-------------------------------------\n");
printf("%s\t%s\n", host, service);
printf("=====================================\n\n");
}

static void _print_host(const struct hostent *he)
{
struct in_addr **addr_list;
printf("=====================================\n");
printf("Official name is: %s\n", he->h_name);
printf("IP address: %s\n", inet_ntoa(*(struct in_addr *)he->h_addr));
printf("All addresses: ");
addr_list = (struct in_addr **)he->h_addr_list;
for (int i = 0; addr_list[i] != NULL; i++) {
printf("%s ", inet_ntoa(*addr_list[i]));
}
printf("\n=====================================\n\n");
}

/*
* Description: get socket information
*/
START_TEST_F(getaddrinfo_p)
{
struct addrinfo hints;
struct addrinfo *servinfo;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;

int res = getaddrinfo(TC_HOST_NAME, TC_SERVICE, &hints, &servinfo);
ST_EXPECT_EQ(0, res);
if (res == 0) {
_print_addrinfo(servinfo);
freeaddrinfo(servinfo);
}
}
END_TEST_F

/*
* Description: verify getaddrinfo negative case
*/
START_TEST_F(getaddrinfo_n)
{
ST_EXPECT_NEQ(0, getaddrinfo(NULL, NULL, NULL, NULL));
}
END_TEST_F

/*
* description: getnameinfo positive
*/
START_TEST_F(getnameinfo_p)
{
struct addrinfo hints;
struct addrinfo *servinfo;
int res = 0;

memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
res = getaddrinfo(TC_HOST_NAME, TC_SERVICE, &hints, &servinfo);
ST_EXPECT_EQ(0, res);

res = getnameinfo(servinfo->ai_addr, sizeof(struct sockaddr),
g_hostname, sizeof(g_hostname),
g_serv, sizeof(g_serv),
NI_NUMERICSERV | NI_NUMERICHOST);
ST_EXPECT_EQ(0, res);
if (res == 0) {
_print_serv_info(g_hostname, g_serv);
}
freeaddrinfo(servinfo);
}
END_TEST_F

/*
* description: getnameinfo negative
*/
START_TEST_F(getnameinfo_n)
{
int res = getnameinfo(NULL, 0,
g_hostname, sizeof(g_hostname),
g_serv, sizeof(g_serv),
NI_NUMERICSERV | NI_NUMERICHOST);
ST_EXPECT_NEQ(0, res);
}
END_TEST_F

/*
* description: gethostbyname positive
*/
START_TEST_F(gethostbyname_p)
{
struct hostent *shost;
shost = gethostbyname(TC_HOST_NAME);
ST_EXPECT_NEQ(NULL, shost);
if (shost != NULL) {
_print_host(shost);
}
}
END_TEST_F

/*
* description: gethostbyname negative
*/
START_TEST_F(gethostbyname_n)
{
struct hostent *shost;
shost = gethostbyname(NULL);
ST_EXPECT_EQ(NULL, shost);
}
END_TEST_F

/*
* description: get socket information
*/
START_TEST_F(netmon_sock)
{
char *str_sock = NULL;
int ret = netlib_netmon_sock(str_sock);
ST_EXPECT_EQ(0, ret);
if (ret == 0) {
printf("%s\n", str_sock);
free(str_sock);
}
}
END_TEST_F

/*
* description: get network stack statistics info
*/
START_TEST_F(netmon_stats)
{
char *str_stats = NULL;
int ret = netlib_getstats((void *)str_stats);
ST_EXPECT_EQ(0, ret);
if (ret == 0) {
printf("%s\n", str_stats);
free(str_stats);
}
}
END_TEST_F

/*
* Description: get NIC statistics
*/
START_TEST_F(netmon_dev_stats)
{
char *str_stats = NULL;
int ret = netlib_netmon_devstats("wlan0", (void **)&str_stats);
ST_EXPECT_EQ(0, ret);
if (ret == 0) {
printf("%s\n", str_stats);
free(str_stats);
}
}
END_TEST_F

/*
* description: set dns server
*/
START_TEST_F(set_dns_p)
{
struct sockaddr_in dns_addr;
memset(&dns_addr, 0x0, sizeof(struct sockaddr_in));
dns_addr.sin_family = AF_INET;
inet_pton(AF_INET, NT_STR_DNS_ADDR, (void *)&dns_addr.sin_addr);
int res = netlib_setdnsserver((struct sockaddr *)&dns_addr, -1);
ST_EXPECT_EQ(0, res);
}
END_TEST_F

/*
* description: set dns server negative
*/
START_TEST_F(set_dns_n)
{
int res = netlib_setdnsserver(NULL, -1);
ST_EXPECT_NEQ(0, res);
}
END_TEST_F

int network_internal_test(void)
{
ST_SET_PACK(nettest);

ST_SET_SMOKE1(nettest, NT_TEST_TRIAL, ST_NO_TIMELIMIT, "getaddrinfo p", getaddrinfo_p);
ST_SET_SMOKE1(nettest, NT_TEST_TRIAL, ST_NO_TIMELIMIT, "getaddrinfo n", getaddrinfo_n);
ST_SET_SMOKE1(nettest, NT_TEST_TRIAL, ST_NO_TIMELIMIT, "gethostbyname p", gethostbyname_p);
ST_SET_SMOKE1(nettest, NT_TEST_TRIAL, ST_NO_TIMELIMIT, "gethostbyname n", gethostbyname_n);
ST_SET_SMOKE1(nettest, NT_TEST_TRIAL, ST_NO_TIMELIMIT, "getnameinfo p", getnameinfo_p);
ST_SET_SMOKE1(nettest, NT_TEST_TRIAL, ST_NO_TIMELIMIT, "getnameinfo n", getnameinfo_n);

ST_SET_SMOKE1(nettest, NT_TEST_TRIAL, ST_NO_TIMELIMIT, "get sock info from netmon", netmon_sock);
ST_SET_SMOKE1(nettest, NT_TEST_TRIAL, ST_NO_TIMELIMIT, "get statistics from netmon", netmon_stats);
ST_SET_SMOKE1(nettest, NT_TEST_TRIAL, ST_NO_TIMELIMIT, "get NIC statistics", netmon_dev_stats);

ST_SET_SMOKE1(nettest, NT_TEST_TRIAL, ST_NO_TIMELIMIT, "set dns positive", set_dns_p);
ST_SET_SMOKE1(nettest, NT_TEST_TRIAL, ST_NO_TIMELIMIT, "set dns negative", set_dns_n);

ST_RUN_TEST(nettest);
ST_RESULT_TEST(nettest);

return 0;
}
9 changes: 5 additions & 4 deletions apps/system/utils/netcmd_netmon.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,22 @@ int cmd_netmon(int argc, char **argv)
/* Get socket information: SIOCGETSOCK */
char *buf = NULL;
ret = netlib_netmon_sock(buf);
if (ret != 0) {
NETCMD_LOGE(NTAG, "Failed to fetch socket info.\n");
} else {
if (!ret) {
_print_sock(buf);
free(buf);
} else {
NETCMD_LOGE(NTAG, "Failed to fetch socket info.\n");
}
} else if (!(strncmp(argv[1], "wifi", strlen("wifi") + 1))) {
return _print_wifi_info();
} else if (!(strncmp(argv[1], "netstats", strlen("netstats") + 1))) {
return -1;
} else {
char *buf = NULL;
ret = netlib_netmon_devstats(argv[1], buf);
ret = netlib_netmon_devstats(argv[1], (void **)&buf);
if (!ret) {
_print_devstats(buf);
free(buf);
} else {
NETCMD_LOGE(NTAG, "No device interface %s\n", argv[1]);
return ERROR;
Expand Down
2 changes: 1 addition & 1 deletion external/include/netutils/netlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ void netlib_freeifaddrs(struct ifaddrs *ifa);

#ifdef CONFIG_NET_NETMON
int netlib_netmon_sock(void *arg);
int netlib_netmon_devstats(const char *ifname, void *arg);
int netlib_netmon_devstats(const char *ifname, void **arg);
#endif

/* HTTP support */
Expand Down
1 change: 1 addition & 0 deletions external/include/stress_tool/st_perf.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#define ST_ELOG printf
#endif

#define ST_NO_TIMELIMIT 0
/*
* Initializer Macro
*/
Expand Down
Loading

0 comments on commit e054817

Please sign in to comment.