Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sock_dns: implement DNS cache #17680

Merged
merged 2 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
tests/unittests: add tests sock_dns_cache
  • Loading branch information
benpicco committed Jul 14, 2022
commit d09d29a581d9e92c95fcd744d4098c5cc3b2d1d8
11 changes: 5 additions & 6 deletions sys/net/application_layer/sock_dns/dns_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,19 @@ extern "C" {
* @param[in] family Either AF_INET, AF_INET6 or AF_UNSPEC
*
* @return the size of the resolved address on success
* @return < 0 otherwise
* @return <= 0 otherwise
*/
int sock_dns_cache_query(const char *domain_name, void *addr_out, int family);

/**
* @brief Add an IP address for a DNS name to the DNS cache
*
* @param[in] domain_name DNS name to resolve into address
* @param[in] addr_out buffer to write result into
* @param[in] addr buffer containing the address
* @param[in] addr_len length of the address in bytes
* @param[in] ttl lifetime of the entry in seconds
*/
void sock_dns_cache_add(const char *domain_name, const void *addr_out,
int addr_len, uint32_t ttl);
void sock_dns_cache_add(const char *domain_name, const void *addr, int addr_len, uint32_t ttl);
#else
static inline int sock_dns_cache_query(const char *domain_name,
void *addr_out, int family)
Expand All @@ -78,11 +77,11 @@ static inline int sock_dns_cache_query(const char *domain_name,
return 0;
}

static inline void sock_dns_cache_add(const char *domain_name, const void *addr_out,
static inline void sock_dns_cache_add(const char *domain_name, const void *addr,
int addr_len, uint32_t ttl)
{
(void)domain_name;
(void)addr_out;
(void)addr;
(void)addr_len;
(void)ttl;
}
Expand Down
1 change: 1 addition & 0 deletions tests/unittests/tests-sock_dns_cache/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
2 changes: 2 additions & 0 deletions tests/unittests/tests-sock_dns_cache/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
USEMODULE += gnrc_ipv6
USEMODULE += sock_dns_cache
53 changes: 53 additions & 0 deletions tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2022 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.
*/

#include <stdint.h>
#include <string.h>
#include "net/af.h"
#include "net/ipv6.h"
#include "ztimer.h"

#include "tests-sock_dns_cache.h"
#include "../net/application_layer/sock_dns/dns_cache.h"

static void test_dns_cache_add(void)
{
ipv6_addr_t addr_in = IPV6_ADDR_ALL_NODES_IF_LOCAL;
ipv6_addr_t addr_out;

TEST_ASSERT_EQUAL_INT(0, sock_dns_cache_query("example.com", &addr_out, AF_INET6));

/* add DNS entry, set it to expire in 1s */
sock_dns_cache_add("example.com", &addr_in, sizeof(addr_in), 1);
TEST_ASSERT_EQUAL_INT(sizeof(addr_out), sock_dns_cache_query("example.com", &addr_out, AF_INET6));
benpicco marked this conversation as resolved.
Show resolved Hide resolved
TEST_ASSERT_EQUAL_INT(0, memcmp(&addr_in, &addr_out, sizeof(addr_in)));

TEST_ASSERT_EQUAL_INT(0, sock_dns_cache_query("example.com", &addr_out, AF_INET));
TEST_ASSERT_EQUAL_INT(0, sock_dns_cache_query("alt.example.com", &addr_out, AF_INET6));
TEST_ASSERT_EQUAL_INT(0, sock_dns_cache_query("example.comm", &addr_out, AF_INET6));
TEST_ASSERT_EQUAL_INT(0, sock_dns_cache_query("example.co", &addr_out, AF_INET6));

ztimer_sleep(ZTIMER_USEC, 2000000);
TEST_ASSERT_EQUAL_INT(0, sock_dns_cache_query("example.com", &addr_out, AF_INET6));
}

Test *tests_sock_dns_cache_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_dns_cache_add),
};

EMB_UNIT_TESTCALLER(sock_dns_cache_tests, NULL, NULL, fixtures);

return (Test *)&sock_dns_cache_tests;
}

void tests_sock_dns_cache(void)
{
TESTS_RUN(tests_sock_dns_cache_tests());
}
44 changes: 44 additions & 0 deletions tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2022 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.
*/

/**
* @addtogroup unittests
* @{
*
* @file
* @brief Unittests for the ``sock_dns_cache`` module
*
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
*/
#ifndef TESTS_SOCK_DNS_CACHE_H
#define TESTS_SOCK_DNS_CACHE_H

#include "embUnit.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief The entry point of this test suite.
*/
void tests_sock_dns_cache(void);

/**
* @brief Generates tests for sock_dns_cache
*
* @return embUnit tests if successful, NULL if not.
*/
Test *tests_sock_dns_cache_tests(void);

#ifdef __cplusplus
}
#endif

#endif /* TESTS_SOCK_DNS_CACHE_H */
/** @} */