From 7eec67fae79745a9e17ebf690f3cfd3a332bb909 Mon Sep 17 00:00:00 2001 From: Hendrik van Essen Date: Sat, 12 Mar 2022 22:57:17 +0100 Subject: [PATCH] sys/net/application_layer/sock_dns_mock: add module for mocking sock_dns --- sys/Makefile | 3 + sys/include/net/dns_mock.h | 72 +++++++++++++++++++ .../application_layer/sock_dns_mock/Makefile | 1 + .../sock_dns_mock/dns_mock.c | 44 ++++++++++++ sys/net/netutils/util.c | 6 +- sys/net/sock/sock_util.c | 5 +- tests/netutils/Makefile | 3 +- tests/netutils/main.c | 15 ++-- tests/netutils/mock_dns.c | 58 --------------- 9 files changed, 132 insertions(+), 75 deletions(-) create mode 100644 sys/include/net/dns_mock.h create mode 100644 sys/net/application_layer/sock_dns_mock/Makefile create mode 100644 sys/net/application_layer/sock_dns_mock/dns_mock.c delete mode 100644 tests/netutils/mock_dns.c diff --git a/sys/Makefile b/sys/Makefile index 2a008ed9759c9..720333f4481c5 100644 --- a/sys/Makefile +++ b/sys/Makefile @@ -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 diff --git a/sys/include/net/dns_mock.h b/sys/include/net/dns_mock.h new file mode 100644 index 0000000000000..23273ba8be447 --- /dev/null +++ b/sys/include/net/dns_mock.h @@ -0,0 +1,72 @@ +/* + * 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 + */ +#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 when using sock_dns_mock + */ +#define SOCK_DNS_MOCK_EXAMPLE_COM_HOSTNAME "example.com" + +/** + * @brief IPv4 string related to the hostname when using sock_dns_mock + * + * @see @ref SOCK_DNS_MOCK_EXAMPLE_COM_HOSTNAME + */ +#define SOCK_DNS_MOCK_EXAMPLE_COM_IPV4_STRING "93.184.216.34" + +/** + * @brief IPv6 string related to the hostname when using sock_dns_mock + * + * @see @ref SOCK_DNS_MOCK_EXAMPLE_COM_HOSTNAME + */ +#define SOCK_DNS_MOCK_EXAMPLE_COM_IPV6_STRING "2606:2800:220:1:248:1893:25c8:1946" + +/** + * @brief ipv4_addr_t matching the IPv4 string when using sock_dns_mock + * + * @see @ref SOCK_DNS_MOCK_EXAMPLE_COM_IPV4_STRING + */ +static const ipv4_addr_t sock_dns_mock_example_com_addr_ipv4 = { { 0x5d, 0xb8, 0xd8, 0x22 } }; + +/** + * @brief ipv6_addr_t matching the IPv6 string when using sock_dns_mock + * + * @see @ref SOCK_DNS_MOCK_EXAMPLE_COM_IPV6_STRING + */ +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 */ +/** @} */ diff --git a/sys/net/application_layer/sock_dns_mock/Makefile b/sys/net/application_layer/sock_dns_mock/Makefile new file mode 100644 index 0000000000000..48422e909a47d --- /dev/null +++ b/sys/net/application_layer/sock_dns_mock/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/sys/net/application_layer/sock_dns_mock/dns_mock.c b/sys/net/application_layer/sock_dns_mock/dns_mock.c new file mode 100644 index 0000000000000..c1efef3dbad6b --- /dev/null +++ b/sys/net/application_layer/sock_dns_mock/dns_mock.c @@ -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 + * @author Benjamin Valentin + * @} + */ + +#include +#include + +#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; + } +} diff --git a/sys/net/netutils/util.c b/sys/net/netutils/util.c index defc428086c21..c219b1eba9804 100644 --- a/sys/net/netutils/util.c +++ b/sys/net/netutils/util.c @@ -20,7 +20,7 @@ #include #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); diff --git a/sys/net/sock/sock_util.c b/sys/net/sock/sock_util.c index 3bd760c066276..b978bf2e4861a 100644 --- a/sys/net/sock/sock_util.c +++ b/sys/net/sock/sock_util.c @@ -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) char hostbuf[CONFIG_SOCK_HOSTPORT_MAXLEN]; const char *host; char *hostend = strchr(str, ':'); diff --git a/tests/netutils/Makefile b/tests/netutils/Makefile index 1e7efe680218e..8cd48494bd43a 100644 --- a/tests/netutils/Makefile +++ b/tests/netutils/Makefile @@ -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 diff --git a/tests/netutils/main.c b/tests/netutils/main.c index b274ab68f8590..d207e4bf5e5da 100644 --- a/tests/netutils/main.c +++ b/tests/netutils/main.c @@ -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) diff --git a/tests/netutils/mock_dns.c b/tests/netutils/mock_dns.c deleted file mode 100644 index 9bc0101d76b19..0000000000000 --- a/tests/netutils/mock_dns.c +++ /dev/null @@ -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 - */ -#include - -#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; - } -} -/** @} */