-
Notifications
You must be signed in to change notification settings - Fork 7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests: net: Add test for Sockets API UDP socket/bind/connect/send/recv
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
- Loading branch information
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# | ||
# Copyright (c) 2017 Linaro Limited | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
|
||
BOARD ?= qemu_x86 | ||
CONF_FILE ?= prj.conf | ||
|
||
include $(ZEPHYR_BASE)/Makefile.inc | ||
include $(ZEPHYR_BASE)/samples/net/common/Makefile.ipstack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# General config | ||
CONFIG_NEWLIB_LIBC=y | ||
|
||
# Networking config | ||
CONFIG_NETWORKING=y | ||
CONFIG_NET_IPV4=y | ||
CONFIG_NET_IPV6=n | ||
CONFIG_NET_TCP=y | ||
CONFIG_NET_SOCKETS=y | ||
CONFIG_NET_SOCKETS_POSIX_NAMES=y | ||
|
||
# Network driver config | ||
CONFIG_NET_SLIP_TAP=y | ||
CONFIG_TEST_RANDOM_GENERATOR=y | ||
|
||
# Network address config | ||
#CONFIG_NET_APP_SETTINGS=y | ||
#CONFIG_NET_APP_MY_IPV4_ADDR="192.0.2.1" | ||
#CONFIG_NET_APP_PEER_IPV4_ADDR="192.0.2.2" | ||
|
||
# Network debug config | ||
#CONFIG_NET_LOG=y | ||
#CONFIG_NET_DEBUG_SOCKETS=y | ||
#CONFIG_SYS_LOG_NET_LEVEL=4 | ||
|
||
CONFIG_ZTEST=y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
obj-y += main.o | ||
ccflags-y += -I${ZEPHYR_BASE}/tests/include | ||
ccflags-y += -I${ZEPHYR_BASE}/tests/ztest/include |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2017 Linaro Limited | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <ztest_assert.h> | ||
|
||
#include <net/socket.h> | ||
#include <net/net_if.h> | ||
|
||
#define BUF_AND_SIZE(buf) buf, sizeof(buf) - 1 | ||
#define STRLEN(buf) (sizeof(buf) - 1) | ||
|
||
#define TEST_STR_SMALL "test" | ||
|
||
void test_send_recv_2_sock(void) | ||
{ | ||
int sock1, sock2; | ||
struct sockaddr_in bind_addr, conn_addr; | ||
char buf[10]; | ||
int len, cmp; | ||
|
||
sock1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | ||
sock2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); | ||
|
||
bind_addr.sin_family = AF_INET; | ||
bind_addr.sin_addr.s_addr = htonl(INADDR_ANY); | ||
bind_addr.sin_port = htons(55555); | ||
bind(sock1, (struct sockaddr *)&bind_addr, sizeof(bind_addr)); | ||
|
||
conn_addr.sin_family = AF_INET; | ||
conn_addr.sin_addr.s_addr = htonl(0xc0000201); | ||
conn_addr.sin_port = htons(55555); | ||
connect(sock2, (struct sockaddr *)&conn_addr, sizeof(conn_addr)); | ||
|
||
send(sock2, BUF_AND_SIZE(TEST_STR_SMALL), 0); | ||
|
||
len = recv(sock1, buf, sizeof(buf), 0); | ||
zassert_equal(len, 4, "Invalid recv len"); | ||
cmp = memcmp(buf, TEST_STR_SMALL, STRLEN(TEST_STR_SMALL)); | ||
zassert_equal(cmp, 0, "Invalid recv data"); | ||
} | ||
|
||
void test_main(void) | ||
{ | ||
zassert_not_null(net_if_get_default(), "No default netif"); | ||
static struct in_addr in4addr_my = { { {192, 0, 2, 1} } }; | ||
|
||
net_if_ipv4_addr_add(net_if_get_default(), &in4addr_my, | ||
NET_ADDR_MANUAL, 0); | ||
|
||
ztest_test_suite(socket_udp, | ||
ztest_unit_test(test_send_recv_2_sock) | ||
); | ||
|
||
ztest_run_test_suite(socket_udp); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[test] | ||
tags = net | ||
build_only = true | ||
arch_whitelist = x86 | ||
platform_exclude = quark_d2000_crb |