Skip to content

Commit

Permalink
sys/net/application_layer/sock_dns: add pseudomodule for mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikVE committed Mar 26, 2022
1 parent a20d8f6 commit 5d63635
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 70 deletions.
1 change: 1 addition & 0 deletions makefiles/pseudomodules.inc.mk
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ PSEUDOMODULES += sock_async
PSEUDOMODULES += sock_aux_local
PSEUDOMODULES += sock_aux_rssi
PSEUDOMODULES += sock_aux_timestamp
PSEUDOMODULES += sock_dns_mock
PSEUDOMODULES += sock_dtls
PSEUDOMODULES += sock_ip
PSEUDOMODULES += sock_tcp
Expand Down
4 changes: 4 additions & 0 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,10 @@ ifneq (,$(filter sock_async,$(USEMODULE)))
endif
endif

ifneq (,$(filter sock_dns_mock,$(USEMODULE)))
USEMODULE += sock_dns
endif

ifneq (,$(filter sock_dns,$(USEMODULE)))
USEMODULE += dns_msg
USEMODULE += sock_udp
Expand Down
43 changes: 43 additions & 0 deletions sys/include/net/dns.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
* @brief Generic DNS values
*
* @author Martine Lenders <m.lenders@fu-berlin.de>
* @author Hendrik van Essen <hendrik.ve@fu-berlin.de>
*/
#ifndef NET_DNS_H
#define NET_DNS_H

#include "kernel_defines.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -43,6 +46,46 @@ extern "C" {
#define RR_RDLENGTH_LENGTH (2U)
/** @} */

#if IS_USED(MODULE_SOCK_DNS_MOCK) || DOXYGEN

/**
* @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
}
};
#endif /* IS_USED(MODULE_SOCK_DNS_MOCK)|| DOXYGEN */

#ifdef __cplusplus
}
#endif
Expand Down
24 changes: 24 additions & 0 deletions sys/net/application_layer/sock_dns/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @brief sock DNS client implementation
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Hendrik van Essen <hendrik.ve@fu-berlin.de>
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
* @}
*/

Expand All @@ -21,8 +22,11 @@

#include <arpa/inet.h>

#include "net/af.h"
#include "net/dns.h"
#include "net/dns/msg.h"
#include "net/ipv4/addr.h"
#include "net/ipv6/addr.h"
#include "net/sock/udp.h"
#include "net/sock/dns.h"

Expand Down Expand Up @@ -67,6 +71,25 @@ void auto_init_sock_dns(void)

int sock_dns_query(const char *domain_name, void *addr_out, int family)
{
#ifdef MODULE_SOCK_DNS_MOCK
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;
}
#else
static uint8_t dns_buf[CONFIG_DNS_MSG_LEN];

if (sock_dns_server.port == 0) {
Expand Down Expand Up @@ -109,4 +132,5 @@ int sock_dns_query(const char *domain_name, void *addr_out, int family)
out:
sock_udp_close(&sock_dns);
return res;
#endif /* MODULE_SOCK_DNS_MOCK */
}
3 changes: 1 addition & 2 deletions tests/netutils/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
15 changes: 5 additions & 10 deletions tests/netutils/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "embUnit.h"

#include "net/dns.h"
#include "net/gnrc/netif.h"
#include "net/sock/util.h"
#include "net/utils.h"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
58 changes: 0 additions & 58 deletions tests/netutils/mock_dns.c

This file was deleted.

0 comments on commit 5d63635

Please sign in to comment.