1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-25 06:23:53 +01:00

tests/netutils: add test for sock_tl_name2ep()

This commit is contained in:
Benjamin Valentin 2022-01-26 16:20:45 +01:00 committed by Benjamin Valentin
parent 1ef6da7de4
commit 4db814d94c
3 changed files with 65 additions and 2 deletions

View File

@ -3,6 +3,7 @@ include ../Makefile.tests_common
USEMODULE += netutils
USEMODULE += netif
USEMODULE += embunit
USEMODULE += sock_util
# make sure we have an implementation of sock_types.h
USEMODULE += gnrc_sock_udp

View File

@ -19,6 +19,7 @@
#include "embUnit.h"
#include "net/gnrc/netif.h"
#include "net/sock/util.h"
#include "net/utils.h"
static gnrc_netif_t dummy_netif[2];
@ -187,6 +188,55 @@ Test *tests_netutils_ipv4_tests(void)
return (Test *)&ipv4_addr_tests;
}
static void test_sock_tl_name2ep__ip_if_port(void)
{
static const ipv6_addr_t a = { {
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
}
};
struct _sock_tl_ep ep;
int res = sock_tl_name2ep(&ep, "[fe80::f8f9:fafb:fcfd:feff%1]:1234");
TEST_ASSERT_EQUAL_INT(res, 0);
TEST_ASSERT_EQUAL_INT(AF_INET6, ep.family);
TEST_ASSERT_EQUAL_INT(1234, ep.port);
TEST_ASSERT_EQUAL_INT(1, ep.netif);
TEST_ASSERT(ipv6_addr_equal(&a, (ipv6_addr_t *)&ep.addr.ipv6));
}
static void test_sock_tl_name2ep__name_port(void)
{
static const ipv6_addr_t a = { {
0x26, 0x06, 0x28, 0x00, 0x02, 0x20, 0x00, 0x01,
0x02, 0x48, 0x18, 0x93, 0x25, 0xc8, 0x19, 0x46
}
};
struct _sock_tl_ep ep;
int res = sock_tl_name2ep(&ep, "example.com:1234");
TEST_ASSERT_EQUAL_INT(0, res);
TEST_ASSERT_EQUAL_INT(AF_INET6, ep.family);
TEST_ASSERT_EQUAL_INT(1234, ep.port);
TEST_ASSERT(ipv6_addr_equal(&a, (ipv6_addr_t *)&ep.addr.ipv6));
}
static void test_sock_tl_name2ep__name_only(void)
{
static const ipv6_addr_t a = { {
0x26, 0x06, 0x28, 0x00, 0x02, 0x20, 0x00, 0x01,
0x02, 0x48, 0x18, 0x93, 0x25, 0xc8, 0x19, 0x46
}
};
struct _sock_tl_ep ep;
int res = sock_tl_name2ep(&ep, "example.com");
TEST_ASSERT_EQUAL_INT(0, res);
TEST_ASSERT_EQUAL_INT(AF_INET6, ep.family);
TEST_ASSERT_EQUAL_INT(0, ep.port);
TEST_ASSERT(ipv6_addr_equal(&a, (ipv6_addr_t *)&ep.addr.ipv6));
}
Test *tests_netutils_ipv6_tests(void)
{
for (unsigned i = 0; i < ARRAY_SIZE(dummy_netif); ++i) {
@ -205,6 +255,9 @@ Test *tests_netutils_ipv6_tests(void)
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),
new_TestFixture(test_sock_tl_name2ep__ip_if_port),
new_TestFixture(test_sock_tl_name2ep__name_port),
new_TestFixture(test_sock_tl_name2ep__name_only),
};
EMB_UNIT_TESTCALLER(ipv6_addr_tests, NULL, NULL, fixtures);

View File

@ -35,13 +35,22 @@ int sock_dns_query(const char *domain_name, void *addr_out, int family)
return -ENOTSUP;
}
if (family == AF_UNSPEC) {
if (IS_USED(SOCK_HAS_IPV4)) {
family = AF_INET;
}
if (IS_USED(SOCK_HAS_IPV6)) {
family = AF_INET6;
}
}
switch (family) {
case AF_INET:
memcpy(addr_out, &addr_ipv4, sizeof(addr_ipv4));
return 0;
return sizeof(addr_ipv4);
case AF_INET6:
memcpy(addr_out, &addr_ipv6, sizeof(addr_ipv6));
return 0;
return sizeof(addr_ipv6);
default:
return -EAFNOSUPPORT;
}