Skip to content

Commit

Permalink
tests: net: dns_dispatcher: Add tests for dispatcher
Browse files Browse the repository at this point in the history
Make sure that the socket service is properly unregistered when
dispatcher is unregistered.

Signed-off-by: Jukka Rissanen <[email protected]>
(cherry picked from commit d98fe73)
  • Loading branch information
jukkar authored and github-actions[bot] committed Dec 11, 2024
1 parent f22be22 commit 57abb8f
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/net/lib/dns_dispatcher/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(dns_dispatcher)

target_include_directories(app PRIVATE ${ZEPHYR_BASE}/subsys/net/ip)
FILE(GLOB app_sources src/*.c)
target_sources(app PRIVATE ${app_sources})
25 changes: 25 additions & 0 deletions tests/net/lib/dns_dispatcher/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CONFIG_NETWORKING=y
CONFIG_NET_TEST=y
CONFIG_ENTROPY_GENERATOR=y
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_NET_L2_DUMMY=y
CONFIG_DNS_RESOLVER=y
CONFIG_DNS_RESOLVER_MAX_SERVERS=2
CONFIG_DNS_NUM_CONCUR_QUERIES=1
CONFIG_NET_LOG=y
CONFIG_NET_MGMT=y
CONFIG_NET_MGMT_EVENT=y
CONFIG_NET_MGMT_EVENT_QUEUE_SIZE=5
CONFIG_NET_IPV4=y
CONFIG_NET_IPV6=y
CONFIG_PRINTK=y
CONFIG_ZTEST=y
CONFIG_MAIN_STACK_SIZE=2048
CONFIG_ZVFS_OPEN_MAX=10
CONFIG_ZVFS_POLL_MAX=10
CONFIG_MDNS_RESPONDER=n
CONFIG_MDNS_RESOLVER=n
CONFIG_DNS_SERVER_IP_ADDRESSES=y
CONFIG_DNS_RESOLVER_MAX_SERVERS=2
CONFIG_DNS_SERVER1="192.0.2.1"
CONFIG_DNS_SERVER2="2001:db8::1"
203 changes: 203 additions & 0 deletions tests/net/lib/dns_dispatcher/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/

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

#include <zephyr/types.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <zephyr/sys/printk.h>
#include <zephyr/random/random.h>

#include <zephyr/ztest.h>

#include <zephyr/net/ethernet.h>
#include <zephyr/net/dummy.h>
#include <zephyr/net_buf.h>
#include <zephyr/net/net_ip.h>
#include <zephyr/net/net_if.h>
#include <zephyr/net/dns_resolve.h>
#include <zephyr/net/net_event.h>
#include <zephyr/net/net_mgmt.h>
#include <zephyr/net/socket_service.h>

#define NET_LOG_ENABLED 1
#include "net_private.h"

#if defined(CONFIG_DNS_RESOLVER_LOG_LEVEL_DBG)
#define DBG(fmt, ...) printk(fmt, ##__VA_ARGS__)
#else
#define DBG(fmt, ...)
#endif

#define NAME4 "4.zephyr.test"
#define NAME6 "6.zephyr.test"
#define NAME_IPV4 "192.0.2.1"
#define NAME_IPV6 "2001:db8::1"

#define DNS_NAME_IPV4 "192.0.2.4"
#define DNS2_NAME_IPV4 "192.0.2.5"
#define DNS_NAME_IPV6 "2001:db8::4"

#define DNS_TIMEOUT 500 /* ms */

#if defined(CONFIG_NET_IPV6)
/* Interface 1 addresses */
static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0x1 } } };
#endif

#if defined(CONFIG_NET_IPV4)
/* Interface 1 addresses */
static struct in_addr my_addr2 = { { { 192, 0, 2, 1 } } };
#endif

Check notice on line 59 in tests/net/lib/dns_dispatcher/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/net/lib/dns_dispatcher/src/main.c:59 -#define NAME4 "4.zephyr.test" -#define NAME6 "6.zephyr.test" +#define NAME4 "4.zephyr.test" +#define NAME6 "6.zephyr.test" #define NAME_IPV4 "192.0.2.1" #define NAME_IPV6 "2001:db8::1" -#define DNS_NAME_IPV4 "192.0.2.4" +#define DNS_NAME_IPV4 "192.0.2.4" #define DNS2_NAME_IPV4 "192.0.2.5" -#define DNS_NAME_IPV6 "2001:db8::4" +#define DNS_NAME_IPV6 "2001:db8::4" #define DNS_TIMEOUT 500 /* ms */ #if defined(CONFIG_NET_IPV6) /* Interface 1 addresses */ -static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; +static struct in6_addr my_addr1 = { + {{0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}}}; #endif #if defined(CONFIG_NET_IPV4) /* Interface 1 addresses */ -static struct in_addr my_addr2 = { { { 192, 0, 2, 1 } } }; +static struct in_addr my_addr2 = {{{192, 0, 2, 1}}};

Check notice on line 59 in tests/net/lib/dns_dispatcher/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/net/lib/dns_dispatcher/src/main.c:59 -#define NAME4 "4.zephyr.test" -#define NAME6 "6.zephyr.test" +#define NAME4 "4.zephyr.test" +#define NAME6 "6.zephyr.test" #define NAME_IPV4 "192.0.2.1" #define NAME_IPV6 "2001:db8::1" -#define DNS_NAME_IPV4 "192.0.2.4" +#define DNS_NAME_IPV4 "192.0.2.4" #define DNS2_NAME_IPV4 "192.0.2.5" -#define DNS_NAME_IPV6 "2001:db8::4" +#define DNS_NAME_IPV6 "2001:db8::4" #define DNS_TIMEOUT 500 /* ms */ #if defined(CONFIG_NET_IPV6) /* Interface 1 addresses */ -static struct in6_addr my_addr1 = { { { 0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0x1 } } }; +static struct in6_addr my_addr1 = { + {{0x20, 0x01, 0x0d, 0xb8, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1}}}; #endif #if defined(CONFIG_NET_IPV4) /* Interface 1 addresses */ -static struct in_addr my_addr2 = { { { 192, 0, 2, 1 } } }; +static struct in_addr my_addr2 = {{{192, 0, 2, 1}}};

static struct net_if *iface1;

/* this must be higher that the DNS_TIMEOUT */
#define WAIT_TIME K_MSEC((DNS_TIMEOUT + 300) * 3)

struct net_if_test {
uint8_t idx;
uint8_t mac_addr[sizeof(struct net_eth_addr)];
};

static uint8_t *net_iface_get_mac(const struct device *dev)
{
struct net_if_test *data = dev->data;

if (data->mac_addr[2] == 0x00) {
/* 00-00-5E-00-53-xx Documentation RFC 7042 */
data->mac_addr[0] = 0x00;
data->mac_addr[1] = 0x00;
data->mac_addr[2] = 0x5E;
data->mac_addr[3] = 0x00;
data->mac_addr[4] = 0x53;
data->mac_addr[5] = sys_rand8_get();
}

return data->mac_addr;
}

static void net_iface_init(struct net_if *iface)
{
uint8_t *mac = net_iface_get_mac(net_if_get_device(iface));

net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr),
NET_LINK_ETHERNET);
}

Check notice on line 94 in tests/net/lib/dns_dispatcher/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/net/lib/dns_dispatcher/src/main.c:94 - net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr), - NET_LINK_ETHERNET); + net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr), NET_LINK_ETHERNET);

Check notice on line 94 in tests/net/lib/dns_dispatcher/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/net/lib/dns_dispatcher/src/main.c:94 - net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr), - NET_LINK_ETHERNET); + net_if_set_link_addr(iface, mac, sizeof(struct net_eth_addr), NET_LINK_ETHERNET);

static int sender_iface(const struct device *dev, struct net_pkt *pkt)
{
if (!pkt->frags) {
DBG("No data to send!\n");
return -ENODATA;
}

return 0;
}

struct net_if_test net_iface1_data;

static struct dummy_api net_iface_api = {
.iface_api.init = net_iface_init,
.send = sender_iface,
};

#define _ETH_L2_LAYER DUMMY_L2
#define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2)

NET_DEVICE_INIT_INSTANCE(net_iface1_test,
"iface1",
iface1,
NULL,
NULL,
&net_iface1_data,
NULL,
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&net_iface_api,
_ETH_L2_LAYER,
_ETH_L2_CTX_TYPE,
127);

Check notice on line 128 in tests/net/lib/dns_dispatcher/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/net/lib/dns_dispatcher/src/main.c:128 -#define _ETH_L2_LAYER DUMMY_L2 +#define _ETH_L2_LAYER DUMMY_L2 #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2) -NET_DEVICE_INIT_INSTANCE(net_iface1_test, - "iface1", - iface1, - NULL, - NULL, - &net_iface1_data, - NULL, - CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &net_iface_api, - _ETH_L2_LAYER, - _ETH_L2_CTX_TYPE, - 127); +NET_DEVICE_INIT_INSTANCE(net_iface1_test, "iface1", iface1, NULL, NULL, &net_iface1_data, NULL, + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &net_iface_api, _ETH_L2_LAYER, + _ETH_L2_CTX_TYPE, 127);

Check notice on line 128 in tests/net/lib/dns_dispatcher/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/net/lib/dns_dispatcher/src/main.c:128 -#define _ETH_L2_LAYER DUMMY_L2 +#define _ETH_L2_LAYER DUMMY_L2 #define _ETH_L2_CTX_TYPE NET_L2_GET_CTX_TYPE(DUMMY_L2) -NET_DEVICE_INIT_INSTANCE(net_iface1_test, - "iface1", - iface1, - NULL, - NULL, - &net_iface1_data, - NULL, - CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &net_iface_api, - _ETH_L2_LAYER, - _ETH_L2_CTX_TYPE, - 127); +NET_DEVICE_INIT_INSTANCE(net_iface1_test, "iface1", iface1, NULL, NULL, &net_iface1_data, NULL, + CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &net_iface_api, _ETH_L2_LAYER, + _ETH_L2_CTX_TYPE, 127);
static void *test_init(void)
{
struct net_if_addr *ifaddr;

iface1 = net_if_get_by_index(0);
zassert_is_null(iface1, "iface1");

iface1 = net_if_get_by_index(1);

((struct net_if_test *) net_if_get_device(iface1)->data)->idx =
net_if_get_by_iface(iface1);

#if defined(CONFIG_NET_IPV6)
ifaddr = net_if_ipv6_addr_add(iface1, &my_addr1,
NET_ADDR_MANUAL, 0);
if (!ifaddr) {
DBG("Cannot add IPv6 address %s\n",
net_sprint_ipv6_addr(&my_addr1));
zassert_not_null(ifaddr, "addr1");

Check notice on line 147 in tests/net/lib/dns_dispatcher/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/net/lib/dns_dispatcher/src/main.c:147 - ((struct net_if_test *) net_if_get_device(iface1)->data)->idx = - net_if_get_by_iface(iface1); + ((struct net_if_test *)net_if_get_device(iface1)->data)->idx = net_if_get_by_iface(iface1); #if defined(CONFIG_NET_IPV6) - ifaddr = net_if_ipv6_addr_add(iface1, &my_addr1, - NET_ADDR_MANUAL, 0); + ifaddr = net_if_ipv6_addr_add(iface1, &my_addr1, NET_ADDR_MANUAL, 0); if (!ifaddr) { - DBG("Cannot add IPv6 address %s\n", - net_sprint_ipv6_addr(&my_addr1)); + DBG("Cannot add IPv6 address %s\n", net_sprint_ipv6_addr(&my_addr1));

Check notice on line 147 in tests/net/lib/dns_dispatcher/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/net/lib/dns_dispatcher/src/main.c:147 - ((struct net_if_test *) net_if_get_device(iface1)->data)->idx = - net_if_get_by_iface(iface1); + ((struct net_if_test *)net_if_get_device(iface1)->data)->idx = net_if_get_by_iface(iface1); #if defined(CONFIG_NET_IPV6) - ifaddr = net_if_ipv6_addr_add(iface1, &my_addr1, - NET_ADDR_MANUAL, 0); + ifaddr = net_if_ipv6_addr_add(iface1, &my_addr1, NET_ADDR_MANUAL, 0); if (!ifaddr) { - DBG("Cannot add IPv6 address %s\n", - net_sprint_ipv6_addr(&my_addr1)); + DBG("Cannot add IPv6 address %s\n", net_sprint_ipv6_addr(&my_addr1));

return NULL;
}

/* For testing purposes we need to set the adddresses preferred */
ifaddr->addr_state = NET_ADDR_PREFERRED;
#endif

#if defined(CONFIG_NET_IPV4)
ifaddr = net_if_ipv4_addr_add(iface1, &my_addr2,
NET_ADDR_MANUAL, 0);
if (!ifaddr) {
DBG("Cannot add IPv4 address %s\n",
net_sprint_ipv4_addr(&my_addr2));
zassert_not_null(ifaddr, "addr2");

Check notice on line 162 in tests/net/lib/dns_dispatcher/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/net/lib/dns_dispatcher/src/main.c:162 - ifaddr = net_if_ipv4_addr_add(iface1, &my_addr2, - NET_ADDR_MANUAL, 0); + ifaddr = net_if_ipv4_addr_add(iface1, &my_addr2, NET_ADDR_MANUAL, 0); if (!ifaddr) { - DBG("Cannot add IPv4 address %s\n", - net_sprint_ipv4_addr(&my_addr2)); + DBG("Cannot add IPv4 address %s\n", net_sprint_ipv4_addr(&my_addr2));

Check notice on line 162 in tests/net/lib/dns_dispatcher/src/main.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

tests/net/lib/dns_dispatcher/src/main.c:162 - ifaddr = net_if_ipv4_addr_add(iface1, &my_addr2, - NET_ADDR_MANUAL, 0); + ifaddr = net_if_ipv4_addr_add(iface1, &my_addr2, NET_ADDR_MANUAL, 0); if (!ifaddr) { - DBG("Cannot add IPv4 address %s\n", - net_sprint_ipv4_addr(&my_addr2)); + DBG("Cannot add IPv4 address %s\n", net_sprint_ipv4_addr(&my_addr2));

return NULL;
}

ifaddr->addr_state = NET_ADDR_PREFERRED;
#endif

net_if_up(iface1);

return NULL;
}

ZTEST(dns_dispatcher, test_dns_dispatcher)
{
struct dns_resolve_context *ctx;
int sock1, sock2 = -1;

ctx = dns_resolve_get_default();

dns_resolve_init_default(ctx);

sock1 = ctx->servers[0].sock;

for (int i = 0; i < ctx->servers[0].dispatcher.fds_len; i++) {
if (ctx->servers[0].dispatcher.fds[i].fd == sock1) {
sock2 = i;
break;
}
}

zassert_not_equal(sock2, -1, "Cannot find socket");

k_sleep(K_MSEC(10));

dns_resolve_close(ctx);

zassert_equal(ctx->servers[0].dispatcher.fds[sock2].fd, -1, "Socket not closed");
zassert_equal(ctx->servers[0].dispatcher.sock, -1, "Dispatcher still registered");
}

ZTEST_SUITE(dns_dispatcher, NULL, test_init, NULL, NULL, NULL);
13 changes: 13 additions & 0 deletions tests/net/lib/dns_dispatcher/testcase.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
common:
tags:
- dns
- net
depends_on: netif
min_ram: 21
timeout: 600
platform_exclude:
- native_posix
- native_posix/native/64
tests:
net.dns.dispatch:
min_ram: 21

0 comments on commit 57abb8f

Please sign in to comment.