Skip to content

Commit

Permalink
tests: net: Add test for Sockets API UDP socket/bind/connect/send/recv
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
  • Loading branch information
pfalcon committed Jun 26, 2017
1 parent 30a5792 commit c2a3d86
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
11 changes: 11 additions & 0 deletions tests/net/socket/udp/Makefile
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
26 changes: 26 additions & 0 deletions tests/net/socket/udp/prj.conf
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
3 changes: 3 additions & 0 deletions tests/net/socket/udp/src/Makefile
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
59 changes: 59 additions & 0 deletions tests/net/socket/udp/src/main.c
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);
}
5 changes: 5 additions & 0 deletions tests/net/socket/udp/testcase.ini
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

0 comments on commit c2a3d86

Please sign in to comment.