tests: net: socket: Test that non-blocking accept() works

Mark socket non-blocking and make sure that accept() will return
immediately.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2019-09-12 16:47:59 +03:00
commit 8e6aaaf919

View file

@ -8,6 +8,7 @@
LOG_MODULE_REGISTER(net_test, CONFIG_NET_SOCKETS_LOG_LEVEL);
#include <ztest_assert.h>
#include <fcntl.h>
#include <net/socket.h>
#include "../../socket_helpers.h"
@ -66,6 +67,21 @@ static void test_accept(int sock, int *new_sock, struct sockaddr *addr,
zassert_true(*new_sock >= 0, "accept failed");
}
static void test_accept_timeout(int sock, int *new_sock, struct sockaddr *addr,
socklen_t *addrlen)
{
zassert_not_null(new_sock, "null newsock");
*new_sock = accept(sock, addr, addrlen);
zassert_equal(*new_sock, -1, "accept succeed");
zassert_equal(errno, EAGAIN, "");
}
static void test_fcntl(int sock, int cmd, int val)
{
zassert_equal(fcntl(sock, cmd, val), 0, "fcntl failed");
}
static void test_recv(int sock, int flags)
{
ssize_t recved = 0;
@ -403,6 +419,33 @@ void test_open_close_immediately(void)
k_sleep(TCP_TEARDOWN_TIMEOUT);
}
void test_v4_accept_timeout(void)
{
/* Test if accept() will timeout properly */
int s_sock;
int new_sock;
u32_t tstamp;
struct sockaddr_in s_saddr;
struct sockaddr addr;
socklen_t addrlen = sizeof(addr);
prepare_sock_tcp_v4(CONFIG_NET_CONFIG_MY_IPV4_ADDR, SERVER_PORT,
&s_sock, &s_saddr);
test_bind(s_sock, (struct sockaddr *)&s_saddr, sizeof(s_saddr));
test_listen(s_sock);
test_fcntl(s_sock, F_SETFL, O_NONBLOCK);
tstamp = k_uptime_get_32();
test_accept_timeout(s_sock, &new_sock, &addr, &addrlen);
zassert_true(k_uptime_get_32() - tstamp <= 100, "");
test_close(s_sock);
k_sleep(TCP_TEARDOWN_TIMEOUT);
}
void test_main(void)
{
ztest_test_suite(
@ -413,7 +456,8 @@ void test_main(void)
ztest_user_unit_test(test_v6_sendto_recvfrom),
ztest_user_unit_test(test_v4_sendto_recvfrom_null_dest),
ztest_user_unit_test(test_v6_sendto_recvfrom_null_dest),
ztest_unit_test(test_open_close_immediately));
ztest_unit_test(test_open_close_immediately),
ztest_user_unit_test(test_v4_accept_timeout));
ztest_run_test_suite(socket_tcp);
}