Commit 0c639a8a authored by Jukka Rissanen's avatar Jukka Rissanen
Browse files

tests: net: socket: Add tests for socket family registration



Make sure that socket registration does something sane.

Signed-off-by: default avatarJukka Rissanen <jukka.rissanen@linux.intel.com>
parent e082d989
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.13.1)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(socket_register)

FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
+21 −0
Original line number Diff line number Diff line
CONFIG_ZTEST=y
CONFIG_TEST_USERSPACE=n

# Asserts must be disabled as we are feeding invalid values to various
# functions and want that those do not cause abort.
CONFIG_ASSERT=n

CONFIG_NETWORKING=y
CONFIG_NET_TEST=y
CONFIG_NET_LOOPBACK=y
CONFIG_NET_IPV4=y
CONFIG_NET_IPV6=y
CONFIG_NET_IPV6_DAD=n
CONFIG_NET_IPV6_ND=n
CONFIG_NET_IPV6_MLD=n
CONFIG_NET_IPV6_NBR_CACHE=n
CONFIG_NET_TCP=y
CONFIG_NET_UDP=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y
CONFIG_TEST_RANDOM_GENERATOR=y
+262 −0
Original line number Diff line number Diff line
/*
 * Copyright (c) 2019 Intel Corporation.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <logging/log.h>
LOG_MODULE_REGISTER(net_test, CONFIG_NET_SOCKETS_LOG_LEVEL);

#include <stdio.h>
#include <ztest_assert.h>

#include <net/net_ip.h>
#include <net/ethernet.h>
#include <net/socket.h>
#include <net/socket_can.h>

struct test_case {
	int family;
	int type;
	int proto;
};

static const struct test_result {
	struct test_case test_case;
	int result;
	int error;
} expected_result[] = {
	{
		/* 0 */
		.test_case.family = AF_INET,
		.test_case.type = SOCK_DGRAM,
		.test_case.proto = IPPROTO_UDP,
		.result = 0,
	},
	{
		/* 1 */
		.test_case.family = AF_INET6,
		.test_case.type = SOCK_DGRAM,
		.test_case.proto = IPPROTO_UDP,
		.result = 0,
	},
	{
		/* 2 */
		/* This test will not increase the called func count */
		.test_case.family = AF_UNSPEC,
		.test_case.type = 0,
		.test_case.proto = 0,
		.result = -1,
		.error = EAFNOSUPPORT,
	},
	{
		/* 3 */
		.test_case.family = AF_INET,
		.test_case.type = SOCK_DGRAM,
		.test_case.proto = 0,
		.result = -1,
		.error = EPROTONOSUPPORT,
	},
	{
		/* 4 */
		.test_case.family = AF_INET6,
		.test_case.type = SOCK_DGRAM,
		.test_case.proto = 0,
		.result = -1,
		.error = EPROTONOSUPPORT,
	},
	{
		/* 5 */
		.test_case.family = AF_INET,
		.test_case.type = SOCK_DGRAM,
		.test_case.proto = IPPROTO_UDP,
		.result = 0,
	},
	{
		/* 6 */
		.test_case.family = AF_INET6,
		.test_case.type = SOCK_DGRAM,
		.test_case.proto = IPPROTO_UDP,
		.result = 0,
	},
	{
		/* 7 */
		.test_case.family = AF_INET,
		.test_case.type = SOCK_DGRAM,
		.test_case.proto = IPPROTO_UDP,
		.result = 0,
	},
	{
		/* 8 */
		.test_case.family = AF_INET6,
		.test_case.type = SOCK_DGRAM,
		.test_case.proto = IPPROTO_UDP,
		.result = 0,
	},
	{
		/* 9 */
		.test_case.family = AF_INET6,
		.test_case.type = SOCK_STREAM,
		.test_case.proto = IPPROTO_UDP,
		.result = -1,
		.error = EOPNOTSUPP,
	},
	{
		/* 10 */
		.test_case.family = AF_PACKET,
		.test_case.type = SOCK_RAW,
		.test_case.proto = ETH_P_ALL,
		.result = 0,
	},
	{
		/* 11 */
		.test_case.family = AF_CAN,
		.test_case.type = SOCK_RAW,
		.test_case.proto = CAN_RAW,
		.result = 0,
	},
	{
		/* 12 */
		.test_case.family = AF_INET6,
		.test_case.type = SOCK_STREAM,
		.test_case.proto = IPPROTO_TLS_1_2,
		.result = 0,
	},
	{
		/* 13 */
		.test_case.family = AF_INET,
		.test_case.type = SOCK_DGRAM,
		.test_case.proto = IPPROTO_DTLS_1_0,
		.result = 0,
	},
	{
		/* 14 */
		.test_case.family = AF_CAN,
		.test_case.type = SOCK_RAW,
		.test_case.proto = 255,
		.result = -1,
		.error = EPFNOSUPPORT,
	},
};

static int current_test;
static int func_called;
static int failed_family = 2; /* The number of tests that are checked
			       * by generic socket code like the test
			       * number 3
			       */

static int socket_test(int family, int type, int proto)
{
	struct net_context *ctx;
	int ret;

	func_called++;

	ret = net_context_get(family, type, proto, &ctx);
	if (ret < 0) {
		errno = -ret;
		return -1;
	}

	return 0;
}

static int socket_test_ok(int family, int type, int proto)
{
	func_called++;
	return 0;
}

static bool is_tls(int family, int type, int proto)
{
	if ((family == AF_INET || family == AF_INET6) &&
	    (((proto >= IPPROTO_TLS_1_0) && (proto <= IPPROTO_TLS_1_2)) ||
	     (proto >= IPPROTO_DTLS_1_0 && proto <= IPPROTO_DTLS_1_2))) {
		return true;
	}

	return false;
}

static bool is_packet(int family, int type, int proto)
{
	if (type != SOCK_RAW || proto != ETH_P_ALL) {
		return false;
	}

	return true;
}

static bool is_can(int family, int type, int proto)
{
	if (type != SOCK_RAW || proto != CAN_RAW) {
		return false;
	}

	return true;
}

static bool is_ip(int family, int type, int proto)
{
	if (family != AF_INET && family != AF_INET6) {
		return false;
	}

	return true;
}

NET_SOCKET_REGISTER(af_inet,   AF_INET,   is_ip,      socket_test);
NET_SOCKET_REGISTER(af_inet6,  AF_INET6,  is_ip,      socket_test);
NET_SOCKET_REGISTER(af_can2,   AF_CAN,    is_ip,      socket_test);

/* For these socket families, we return ok always for now */
NET_SOCKET_REGISTER(tls,       AF_UNSPEC, is_tls,    socket_test_ok);
NET_SOCKET_REGISTER(af_packet, AF_PACKET, is_packet, socket_test_ok);
NET_SOCKET_REGISTER(af_can,    AF_CAN,    is_can,    socket_test_ok);

void test_create_sockets(void)
{
	int i, fd, ok_tests = 0, failed_tests = 0;

	for (i = 0; i < ARRAY_SIZE(expected_result); i++, current_test++) {
		errno = 0;

		fd = socket(expected_result[i].test_case.family,
			    expected_result[i].test_case.type,
			    expected_result[i].test_case.proto);

		if (errno == EPROTONOSUPPORT) {
			func_called--;
			continue;
		}

		zassert_equal(fd, expected_result[i].result,
			      "[%d] Invalid result (expecting %d got %d, "
			      "errno %d)", i, expected_result[i].result, fd,
			      errno);
		if (expected_result[i].result < 0) {
			zassert_equal(errno, expected_result[i].error,
				      "[%d] Invalid errno (%d vs %d)", i,
				      errno, expected_result[i].error);
		}

		if (expected_result[i].result == 0) {
			ok_tests++;
		} else {
			failed_tests++;
		}
	}

	zassert_equal(ok_tests + failed_tests - failed_family, func_called,
		      "Invalid num of tests failed (%d vs %d)",
		      ok_tests + failed_tests - failed_family, func_called);
}

void test_main(void)
{
	ztest_test_suite(socket_register,
			 ztest_user_unit_test(test_create_sockets));

	ztest_run_test_suite(socket_register);
}
+19 −0
Original line number Diff line number Diff line
common:
  depends_on: netif
  tags: net socket socket.register
tests:
  net.socket.register:
    extra_configs:
      - CONFIG_NET_SOCKETS_SOCKOPT_TLS=n
  net.socket.register.tls:
    extra_configs:
      - CONFIG_MBEDTLS=y
      - CONFIG_MBEDTLS_BUILTIN=y
      - CONFIG_MBEDTLS_ENABLE_HEAP=y
      - CONFIG_MBEDTLS_HEAP_SIZE=60000
      - CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=2048
      - CONFIG_NET_SOCKETS_SOCKOPT_TLS=y
      - CONFIG_NET_SOCKETS_TLS_MAX_CONTEXTS=6
      - CONFIG_NET_SOCKETS_ENABLE_DTLS=y
      - CONFIG_NET_SOCKETS_DTLS_TIMEOUT=30000
      - CONFIG_MAIN_STACK_SIZE=2048