1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/net/application_layer/sock_dns_mock: add module for mocking sock_dns

This commit is contained in:
Hendrik van Essen 2022-03-12 22:57:17 +01:00
parent 6849e33854
commit a9fb3388ca
9 changed files with 116 additions and 75 deletions

View File

@ -143,6 +143,9 @@ endif
ifneq (,$(filter sock_dns,$(USEMODULE)))
DIRS += net/application_layer/sock_dns
endif
ifneq (,$(filter sock_dns_mock,$(USEMODULE)))
DIRS += net/application_layer/sock_dns_mock
endif
ifneq (,$(filter sock_util,$(USEMODULE)))
DIRS += net/sock
endif

View File

@ -0,0 +1,56 @@
/*
* Copyright (C) 2022 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @defgroup net_dns_mock DNS defines
* @ingroup net
* @brief Generic DNS mock values
* @{
*
* @file
* @brief Generic DNS mock values
*
* @author Hendrik van Essen <hendrik.ve@fu-berlin.de>
*/
#ifndef NET_DNS_MOCK_H
#define NET_DNS_MOCK_H
#ifdef __cplusplus
extern "C" {
#endif
#include "net/ipv4/addr.h"
#include "net/ipv6/addr.h"
/**
* @brief Hostname used to query.
*/
#define SOCK_DNS_MOCK_EXAMPLE_COM_HOSTNAME "example.com"
/**
* @brief IPv4 address for @ref SOCK_DNS_MOCK_EXAMPLE_COM_HOSTNAME.
* Address represents "93.184.216.34".
*/
static const ipv4_addr_t sock_dns_mock_example_com_addr_ipv4 = { { 0x5d, 0xb8, 0xd8, 0x22 } };
/**
* @brief IPv6 address for @ref SOCK_DNS_MOCK_EXAMPLE_COM_HOSTNAME.
* Address represents "2606:2800:220:1:248:1893:25c8:1946".
*/
static const ipv6_addr_t sock_dns_mock_example_com_addr_ipv6 = { {
0x26, 0x06, 0x28, 0x00, 0x02, 0x20, 0x00, 0x01,
0x02, 0x48, 0x18, 0x93, 0x25, 0xc8, 0x19, 0x46
}
};
#ifdef __cplusplus
}
#endif
#endif /* NET_DNS_MOCK_H */
/** @} */

View File

@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,44 @@
/*
* Copyright (C) 2022 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup net_dns_mock
* @{
* @file
* @brief sock DNS mock implementation
* @author Hendrik van Essen <hendrik.ve@fu-berlin.de>
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
* @}
*/
#include <errno.h>
#include <string.h>
#include "net/af.h"
#include "net/dns_mock.h"
int sock_dns_query(const char *domain_name, void *addr_out, int family)
{
if (strcmp(domain_name, SOCK_DNS_MOCK_EXAMPLE_COM_HOSTNAME)) {
return -ENOTSUP;
}
switch (family) {
case AF_INET:
memcpy(addr_out, &sock_dns_mock_example_com_addr_ipv4, sizeof(ipv4_addr_t));
return sizeof(ipv4_addr_t);
case AF_UNSPEC:
/* fall-through */
case AF_INET6:
memcpy(addr_out, &sock_dns_mock_example_com_addr_ipv6, sizeof(ipv6_addr_t));
return sizeof(ipv6_addr_t);
default:
return -EAFNOSUPPORT;
}
}

View File

@ -20,7 +20,7 @@
#include <string.h>
#include "net/utils.h"
#ifdef MODULE_SOCK_DNS
#if defined(MODULE_SOCK_DNS) || defined(MODULE_SOCK_DNS_MOCK)
#include "net/af.h"
#include "net/sock/dns.h"
#endif
@ -44,7 +44,7 @@ int netutils_get_ipv4(ipv4_addr_t *addr, const char *hostname)
for (size_t i = 0; i < strlen(hostname); i++) {
bool is_not_ipv4 = (hostname[i] < '0' || hostname[i] > '9') && hostname[i] != '.';
#ifdef MODULE_SOCK_DNS
#if defined(MODULE_SOCK_DNS) || defined(MODULE_SOCK_DNS_MOCK)
/* once we see an invalid character for an IPv4 address try to
* resolve the hostname by DNS */
if (is_not_ipv4) {
@ -77,7 +77,7 @@ int netutils_get_ipv6(ipv6_addr_t *addr, netif_t **netif, const char *hostname)
return -EINVAL;
}
#ifdef MODULE_SOCK_DNS
#if defined(MODULE_SOCK_DNS) || defined(MODULE_SOCK_DNS_MOCK)
/* hostname is not an IPv6 address */
if (strchr(hostname, ':') == NULL) {
int res = sock_dns_query(hostname, addr, AF_INET6);

View File

@ -28,7 +28,8 @@
#include "net/sock/udp.h"
#include "net/sock/util.h"
#ifdef MODULE_SOCK_DNS
#if defined(MODULE_SOCK_DNS) || defined(MODULE_SOCK_DNS_MOCK)
#include "net/sock/dns.h"
#endif
@ -264,7 +265,7 @@ int sock_tl_name2ep(struct _sock_tl_ep *ep_out, const char *str)
return 0;
}
#if defined(MODULE_SOCK_DNS)
#if defined(MODULE_SOCK_DNS) || defined(MODULE_SOCK_DNS_MOCK)
int family;
char hostbuf[CONFIG_SOCK_HOSTPORT_MAXLEN];
const char *host;

View File

@ -12,7 +12,6 @@ USEMODULE += gnrc_ipv6
USEMODULE += ipv4_addr
USEMODULE += ipv6_addr
# pretend to include sock_dns
CFLAGS += -DMODULE_SOCK_DNS=1
USEMODULE += sock_dns_mock
include $(RIOTBASE)/Makefile.include

View File

@ -18,6 +18,7 @@
#include "embUnit.h"
#include "net/dns_mock.h"
#include "net/gnrc/netif.h"
#include "net/sock/util.h"
#include "net/utils.h"
@ -101,16 +102,11 @@ static void test_ipv6_addr_from_str__invalid_interface(void)
static void test_ipv6_addr_from_str__success4(void)
{
static const ipv6_addr_t a = { {
0x26, 0x06, 0x28, 0x00, 0x02, 0x20, 0x00, 0x01,
0x02, 0x48, 0x18, 0x93, 0x25, 0xc8, 0x19, 0x46
}
};
ipv6_addr_t address;
netif_t *netif;
TEST_ASSERT_EQUAL_INT(netutils_get_ipv6(&address, &netif, "example.com"), 0);
TEST_ASSERT(ipv6_addr_equal(&a, &address));
TEST_ASSERT_EQUAL_INT(netutils_get_ipv6(&address, &netif, SOCK_DNS_MOCK_EXAMPLE_COM_HOSTNAME), 0);
TEST_ASSERT(ipv6_addr_equal(&sock_dns_mock_example_com_addr_ipv6, &address));
}
static void test_ipv6_addr_from_str__success5(void)
@ -164,11 +160,10 @@ static void test_ipv4_addr_from_str__success(void)
static void test_ipv4_addr_from_str__success2(void)
{
static const ipv4_addr_t a = { { 0x5d, 0xb8, 0xd8, 0x22 } };
ipv4_addr_t address;
TEST_ASSERT_EQUAL_INT(netutils_get_ipv4(&address, "example.com"), 0);
TEST_ASSERT(ipv4_addr_equal(&a, &address));
TEST_ASSERT_EQUAL_INT(netutils_get_ipv4(&address, SOCK_DNS_MOCK_EXAMPLE_COM_HOSTNAME), 0);
TEST_ASSERT(ipv4_addr_equal(&sock_dns_mock_example_com_addr_ipv4, &address));
}
Test *tests_netutils_ipv4_tests(void)

View File

@ -1,58 +0,0 @@
/*
* Copyright (C) 2021 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
*
* @file
* @brief Mock implementation of sock_dns
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/
#include <string.h>
#include "net/af.h"
#include "net/ipv4/addr.h"
#include "net/ipv6/addr.h"
#include "net/sock/dns.h"
int sock_dns_query(const char *domain_name, void *addr_out, int family)
{
const ipv4_addr_t addr_ipv4 = { { 0x5d, 0xb8, 0xd8, 0x22 } };
const ipv6_addr_t addr_ipv6 = { {
0x26, 0x06, 0x28, 0x00, 0x02, 0x20, 0x00, 0x01,
0x02, 0x48, 0x18, 0x93, 0x25, 0xc8, 0x19, 0x46
}
};
if (strcmp(domain_name, "example.com")) {
return -ENOTSUP;
}
if (family == AF_UNSPEC) {
if (IS_USED(SOCK_HAS_IPV4)) {
family = AF_INET;
}
if (IS_USED(SOCK_HAS_IPV6)) {
family = AF_INET6;
}
}
switch (family) {
case AF_INET:
memcpy(addr_out, &addr_ipv4, sizeof(addr_ipv4));
return sizeof(addr_ipv4);
case AF_INET6:
memcpy(addr_out, &addr_ipv6, sizeof(addr_ipv6));
return sizeof(addr_ipv6);
default:
return -EAFNOSUPPORT;
}
}
/** @} */