Skip to content

Commit b650fa8

Browse files
pfalconjukkar
authored andcommitted
tests: net: Add test for Sockets API UDP socket/bind/connect/send/recv
Signed-off-by: Paul Sokolovsky <[email protected]>
1 parent d950ec2 commit b650fa8

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

tests/net/socket/udp/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#
2+
# Copyright (c) 2017 Linaro Limited
3+
#
4+
# SPDX-License-Identifier: Apache-2.0
5+
#
6+
7+
BOARD ?= qemu_x86
8+
CONF_FILE ?= prj.conf
9+
10+
include $(ZEPHYR_BASE)/Makefile.inc
11+
include $(ZEPHYR_BASE)/samples/net/common/Makefile.ipstack

tests/net/socket/udp/prj.conf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# General config
2+
CONFIG_NEWLIB_LIBC=y
3+
4+
# Networking config
5+
CONFIG_NETWORKING=y
6+
CONFIG_NET_IPV4=y
7+
CONFIG_NET_IPV6=n
8+
CONFIG_NET_TCP=y
9+
CONFIG_NET_SOCKETS=y
10+
CONFIG_NET_SOCKETS_POSIX_NAMES=y
11+
12+
# Network driver config
13+
CONFIG_NET_SLIP_TAP=y
14+
CONFIG_TEST_RANDOM_GENERATOR=y
15+
16+
# Network address config
17+
#CONFIG_NET_APP_SETTINGS=y
18+
#CONFIG_NET_APP_MY_IPV4_ADDR="192.0.2.1"
19+
#CONFIG_NET_APP_PEER_IPV4_ADDR="192.0.2.2"
20+
21+
# Network debug config
22+
#CONFIG_NET_LOG=y
23+
#CONFIG_NET_DEBUG_SOCKETS=y
24+
#CONFIG_SYS_LOG_NET_LEVEL=4
25+
26+
CONFIG_ZTEST=y

tests/net/socket/udp/src/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
obj-y += main.o
2+
ccflags-y += -I${ZEPHYR_BASE}/tests/include
3+
ccflags-y += -I${ZEPHYR_BASE}/tests/ztest/include

tests/net/socket/udp/src/main.c

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2017 Linaro Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdio.h>
8+
#include <ztest_assert.h>
9+
10+
#include <net/socket.h>
11+
#include <net/net_if.h>
12+
13+
#define BUF_AND_SIZE(buf) buf, sizeof(buf) - 1
14+
#define STRLEN(buf) (sizeof(buf) - 1)
15+
16+
#define TEST_STR_SMALL "test"
17+
18+
void test_send_recv_2_sock(void)
19+
{
20+
int sock1, sock2;
21+
struct sockaddr_in bind_addr, conn_addr;
22+
char buf[10];
23+
int len, cmp;
24+
25+
sock1 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
26+
sock2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
27+
28+
bind_addr.sin_family = AF_INET;
29+
bind_addr.sin_addr.s_addr = htonl(INADDR_ANY);
30+
bind_addr.sin_port = htons(55555);
31+
bind(sock1, (struct sockaddr *)&bind_addr, sizeof(bind_addr));
32+
33+
conn_addr.sin_family = AF_INET;
34+
conn_addr.sin_addr.s_addr = htonl(0xc0000201);
35+
conn_addr.sin_port = htons(55555);
36+
connect(sock2, (struct sockaddr *)&conn_addr, sizeof(conn_addr));
37+
38+
send(sock2, BUF_AND_SIZE(TEST_STR_SMALL), 0);
39+
40+
len = recv(sock1, buf, sizeof(buf), 0);
41+
zassert_equal(len, 4, "Invalid recv len");
42+
cmp = memcmp(buf, TEST_STR_SMALL, STRLEN(TEST_STR_SMALL));
43+
zassert_equal(cmp, 0, "Invalid recv data");
44+
}
45+
46+
void test_main(void)
47+
{
48+
zassert_not_null(net_if_get_default(), "No default netif");
49+
static struct in_addr in4addr_my = { { {192, 0, 2, 1} } };
50+
51+
net_if_ipv4_addr_add(net_if_get_default(), &in4addr_my,
52+
NET_ADDR_MANUAL, 0);
53+
54+
ztest_test_suite(socket_udp,
55+
ztest_unit_test(test_send_recv_2_sock)
56+
);
57+
58+
ztest_run_test_suite(socket_udp);
59+
}

tests/net/socket/udp/testcase.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[test]
2+
tags = net
3+
build_only = true
4+
arch_whitelist = x86
5+
platform_exclude = quark_d2000_crb

0 commit comments

Comments
 (0)