Skip to content

Commit

Permalink
sys/net/application_layer/sock_dns_mock: add module for mocking sock_dns
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikVE committed Apr 3, 2022
1 parent 6849e33 commit a9fb338
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 75 deletions.
3 changes: 3 additions & 0 deletions sys/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions sys/include/net/dns_mock.h
Original file line number Diff line number Diff line change
@@ -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 */
/** @} */
1 change: 1 addition & 0 deletions sys/net/application_layer/sock_dns_mock/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
44 changes: 44 additions & 0 deletions sys/net/application_layer/sock_dns_mock/dns_mock.c
Original file line number Diff line number Diff line change
@@ -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;
}
}
6 changes: 3 additions & 3 deletions sys/net/netutils/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions sys/net/sock/sock_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down
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_mock.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 a9fb338

Please sign in to comment.