diff --git a/tests/netutils/Makefile b/tests/netutils/Makefile new file mode 100644 index 0000000000..10c661b057 --- /dev/null +++ b/tests/netutils/Makefile @@ -0,0 +1,14 @@ +include ../Makefile.tests_common + +USEMODULE += netutils +USEMODULE += netif +USEMODULE += embunit + +# make sure we have an implementation of sock_types.h +USEMODULE += gnrc_sock_udp +USEMODULE += gnrc_ipv6 + +# pretend to include sock_dns +CFLAGS += -DMODULE_SOCK_DNS=1 + +include $(RIOTBASE)/Makefile.include diff --git a/tests/netutils/Makefile.ci b/tests/netutils/Makefile.ci new file mode 100644 index 0000000000..9f30089d46 --- /dev/null +++ b/tests/netutils/Makefile.ci @@ -0,0 +1,28 @@ +BOARD_INSUFFICIENT_MEMORY := \ + arduino-duemilanove \ + arduino-leonardo \ + arduino-mega2560 \ + arduino-nano \ + arduino-uno \ + atmega328p-xplained-mini \ + atmega328p \ + atxmega-a1u-xpro \ + bluepill-stm32f030c8 \ + i-nucleo-lrwan1 \ + msb-430 \ + msb-430h \ + nucleo-f030r8 \ + nucleo-f031k6 \ + nucleo-f042k6 \ + nucleo-l011k4 \ + nucleo-l031k6 \ + nucleo-l053r8 \ + samd10-xmini \ + slstk3400a \ + stk3200 \ + stm32f030f4-demo \ + stm32f0discovery \ + stm32l0538-disco \ + telosb \ + waspmote-pro \ + # diff --git a/tests/netutils/main.c b/tests/netutils/main.c new file mode 100644 index 0000000000..728427f8f4 --- /dev/null +++ b/tests/netutils/main.c @@ -0,0 +1,157 @@ +/* + * Copyright (C) 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 + * + * @author Benjamin Valentin + */ +#include +#include + +#include "embUnit.h" + +#include "net/gnrc/netif.h" +#include "net/utils.h" + +static gnrc_netif_t dummy_netif[2]; + +static void test_ipv6_addr_from_str__one_colon_start(void) +{ + ipv6_addr_t address; + netif_t *netif; + + TEST_ASSERT_EQUAL_INT(netutils_get_ipv6(&address, &netif, ":ff::1"), -EINVAL); +} + +static void test_ipv6_addr_from_str__three_colons(void) +{ + ipv6_addr_t address; + netif_t *netif; + + TEST_ASSERT_EQUAL_INT(netutils_get_ipv6(&address, &netif, "ff02:::1"), -EINVAL); +} + +static void test_ipv6_addr_from_str__illegal_chars(void) +{ + ipv6_addr_t address; + netif_t *netif; + + TEST_ASSERT_EQUAL_INT(netutils_get_ipv6(&address, &netif, ":-D"), -EINVAL); +} + +static void test_ipv6_addr_from_str__addr_NULL(void) +{ + ipv6_addr_t address; + netif_t *netif; + + TEST_ASSERT_EQUAL_INT(netutils_get_ipv6(&address, &netif, NULL), -EINVAL); +} + +static void test_ipv6_addr_from_str__address_NULL(void) +{ + TEST_ASSERT_NULL(ipv6_addr_from_str(NULL, "::")); +} + +static void test_ipv6_addr_from_str__success(void) +{ + static const ipv6_addr_t a = { { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f + } + }; + ipv6_addr_t address; + netif_t *netif; + + TEST_ASSERT_EQUAL_INT(netutils_get_ipv6(&address, &netif, "1:203:405:607:809:a0b:c0d:e0f"), 0); + TEST_ASSERT(ipv6_addr_equal(&a, &address)); + TEST_ASSERT_NULL(netif); +} + +static void test_ipv6_addr_from_str__success2(void) +{ + static const ipv6_addr_t a = { { + 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff + } + }; + ipv6_addr_t address; + netif_t *netif; + + TEST_ASSERT_EQUAL_INT(netutils_get_ipv6(&address, &netif, "fe80::f8f9:fafb:fcfd:feff%1"), 0); + TEST_ASSERT_EQUAL_INT((uintptr_t)netif, (uintptr_t)&dummy_netif[1]); + TEST_ASSERT(ipv6_addr_equal(&a, &address)); +} + +static void test_ipv6_addr_from_str__invalid_interface(void) +{ + ipv6_addr_t address; + netif_t *netif; + + TEST_ASSERT_EQUAL_INT(netutils_get_ipv6(&address, &netif, "fe80::f8f9:fafb:fcfd:feff%3"), -EINVAL); +} + +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)); +} + +static void test_ipv6_addr_from_str__success5(void) +{ + static const ipv6_addr_t a = IPV6_ADDR_LOOPBACK; + ipv6_addr_t address; + + TEST_ASSERT_NOT_NULL(ipv6_addr_from_str(&address, "::1")); + TEST_ASSERT(ipv6_addr_equal(&a, &address)); +} + +Test *tests_netutils_tests(void) +{ + for (unsigned i = 0; i < ARRAY_SIZE(dummy_netif); ++i) { + netif_register(&dummy_netif[i].netif); + dummy_netif[i].pid = i; + } + + EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_ipv6_addr_from_str__one_colon_start), + new_TestFixture(test_ipv6_addr_from_str__three_colons), + new_TestFixture(test_ipv6_addr_from_str__illegal_chars), + new_TestFixture(test_ipv6_addr_from_str__addr_NULL), + new_TestFixture(test_ipv6_addr_from_str__address_NULL), + new_TestFixture(test_ipv6_addr_from_str__success), + new_TestFixture(test_ipv6_addr_from_str__success2), + new_TestFixture(test_ipv6_addr_from_str__invalid_interface), + new_TestFixture(test_ipv6_addr_from_str__success4), + new_TestFixture(test_ipv6_addr_from_str__success5), + }; + + EMB_UNIT_TESTCALLER(ipv6_addr_tests, NULL, NULL, fixtures); + + return (Test *)&ipv6_addr_tests; +} + +int main(void) +{ + TESTS_START(); + TESTS_RUN(tests_netutils_tests()); + TESTS_END(); + + return 0; +} +/** @} */ diff --git a/tests/netutils/mock_dns.c b/tests/netutils/mock_dns.c new file mode 100644 index 0000000000..e8eb37ffb3 --- /dev/null +++ b/tests/netutils/mock_dns.c @@ -0,0 +1,43 @@ +/* + * 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/ipv6/addr.h" +#include "net/sock/dns.h" + +int sock_dns_query(const char *domain_name, void *addr_out, int family) +{ + const ipv6_addr_t a = { { + 0x26, 0x06, 0x28, 0x00, 0x02, 0x20, 0x00, 0x01, + 0x02, 0x48, 0x18, 0x93, 0x25, 0xc8, 0x19, 0x46 + } + }; + + + if (family != AF_INET6) { + return -EAFNOSUPPORT; + } + + if (strcmp(domain_name, "example.com")) { + return -ENOTSUP; + } + + memcpy(addr_out, &a, sizeof(a)); + return 0; +} +/** @} */ diff --git a/tests/netutils/tests/01-run.py b/tests/netutils/tests/01-run.py new file mode 100755 index 0000000000..8b4e23f2e7 --- /dev/null +++ b/tests/netutils/tests/01-run.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2017 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. + +import sys +from testrunner import run_check_unittests + + +if __name__ == "__main__": + sys.exit(run_check_unittests())