From d09d29a581d9e92c95fcd744d4098c5cc3b2d1d8 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 3 Apr 2022 21:13:27 +0200 Subject: [PATCH] tests/unittests: add tests sock_dns_cache --- .../application_layer/sock_dns/dns_cache.h | 11 ++-- tests/unittests/tests-sock_dns_cache/Makefile | 1 + .../tests-sock_dns_cache/Makefile.include | 2 + .../tests-sock_dns_cache.c | 53 +++++++++++++++++++ .../tests-sock_dns_cache.h | 44 +++++++++++++++ 5 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 tests/unittests/tests-sock_dns_cache/Makefile create mode 100644 tests/unittests/tests-sock_dns_cache/Makefile.include create mode 100644 tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.c create mode 100644 tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.h diff --git a/sys/net/application_layer/sock_dns/dns_cache.h b/sys/net/application_layer/sock_dns/dns_cache.h index bd1d67a41e..ffa05eba20 100644 --- a/sys/net/application_layer/sock_dns/dns_cache.h +++ b/sys/net/application_layer/sock_dns/dns_cache.h @@ -54,7 +54,7 @@ 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); @@ -62,12 +62,11 @@ 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) @@ -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; } diff --git a/tests/unittests/tests-sock_dns_cache/Makefile b/tests/unittests/tests-sock_dns_cache/Makefile new file mode 100644 index 0000000000..48422e909a --- /dev/null +++ b/tests/unittests/tests-sock_dns_cache/Makefile @@ -0,0 +1 @@ +include $(RIOTBASE)/Makefile.base diff --git a/tests/unittests/tests-sock_dns_cache/Makefile.include b/tests/unittests/tests-sock_dns_cache/Makefile.include new file mode 100644 index 0000000000..1942dd65b0 --- /dev/null +++ b/tests/unittests/tests-sock_dns_cache/Makefile.include @@ -0,0 +1,2 @@ +USEMODULE += gnrc_ipv6 +USEMODULE += sock_dns_cache diff --git a/tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.c b/tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.c new file mode 100644 index 0000000000..96f3ce9f78 --- /dev/null +++ b/tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.c @@ -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 +#include +#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)); + 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()); +} diff --git a/tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.h b/tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.h new file mode 100644 index 0000000000..e19940513a --- /dev/null +++ b/tests/unittests/tests-sock_dns_cache/tests-sock_dns_cache.h @@ -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 + */ +#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 */ +/** @} */