Skip to content

Commit 9125f07

Browse files
jukkarcfriedt
authored andcommitted
tests: net: dns: Add tests for resolving hostname or localhost
Make sure we are able to resolve the localhost or local hostname. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 6797fa6 commit 9125f07

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

tests/net/lib/dns_resolve/src/main.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ LOG_MODULE_REGISTER(net_test, CONFIG_DNS_RESOLVER_LOG_LEVEL);
2323
#include <zephyr/net/net_ip.h>
2424
#include <zephyr/net/net_if.h>
2525
#include <zephyr/net/dns_resolve.h>
26+
#include <zephyr/net/hostname.h>
27+
#include <zephyr/net/socket.h>
2628

2729
#define NET_LOG_ENABLED 1
2830
#include "net_private.h"
@@ -813,4 +815,98 @@ ZTEST(dns_resolve, test_mdns_ipv6_mld_group)
813815
zassert_true(st, "IPv6 mDNS group not joined");
814816
}
815817

818+
#define MAX_HOSTNAME_LEN 64
819+
820+
ZTEST(dns_resolve, test_dns_hostname_resolve_ipv4)
821+
{
822+
Z_TEST_SKIP_IFNDEF(CONFIG_NET_HOSTNAME_ENABLE);
823+
Z_TEST_SKIP_IFNDEF(CONFIG_NET_IPV4);
824+
825+
struct zsock_addrinfo hints = { 0 };
826+
struct zsock_addrinfo *addrinfos;
827+
char hostname[MAX_HOSTNAME_LEN + 1] = { 0 };
828+
int ret;
829+
830+
hints.ai_family = AF_INET;
831+
strncpy(hostname, net_hostname_get(), sizeof(hostname) - 1);
832+
833+
ret = zsock_getaddrinfo(hostname, NULL, &hints, &addrinfos);
834+
zassert_equal(ret, 0, "getaddrinfo failed with %d (%s)",
835+
ret, zsock_gai_strerror(ret));
836+
837+
DBG("Query %s -> address %s\n", hostname,
838+
net_sprint_ipv4_addr(&net_sin(addrinfos[0].ai_addr)->sin_addr));
839+
840+
hostname[strlen(hostname) - 1] = '\0';
841+
ret = zsock_getaddrinfo(hostname, NULL, &hints, &addrinfos);
842+
zassert_not_equal(ret, 0, "getaddrinfo succeed but should have failed");
843+
}
844+
845+
ZTEST(dns_resolve, test_dns_hostname_resolve_ipv6)
846+
{
847+
Z_TEST_SKIP_IFNDEF(CONFIG_NET_HOSTNAME_ENABLE);
848+
Z_TEST_SKIP_IFNDEF(CONFIG_NET_IPV6);
849+
850+
struct zsock_addrinfo hints = { 0 };
851+
struct zsock_addrinfo *addrinfos;
852+
char hostname[MAX_HOSTNAME_LEN + 1] = { 0 };
853+
int ret;
854+
855+
hints.ai_family = AF_INET6;
856+
strncpy(hostname, net_hostname_get(), sizeof(hostname) - 1);
857+
858+
ret = zsock_getaddrinfo(hostname, NULL, &hints, &addrinfos);
859+
zassert_equal(ret, 0, "getaddrinfo failed with %d (%s)",
860+
ret, zsock_gai_strerror(ret));
861+
862+
DBG("Query %s -> address %s\n", hostname,
863+
net_sprint_ipv6_addr(&net_sin6(addrinfos[0].ai_addr)->sin6_addr));
864+
865+
hostname[strlen(hostname) - 1] = '\0';
866+
ret = zsock_getaddrinfo(hostname, NULL, &hints, &addrinfos);
867+
zassert_not_equal(ret, 0, "getaddrinfo succeed but should have failed");
868+
}
869+
870+
ZTEST(dns_resolve, test_dns_localhost_resolve_ipv4)
871+
{
872+
Z_TEST_SKIP_IFNDEF(CONFIG_NET_IPV4);
873+
874+
struct in_addr addr = INADDR_LOOPBACK_INIT;
875+
struct zsock_addrinfo hints = { 0 };
876+
struct zsock_addrinfo *addrinfos;
877+
const char *hostname = "localhost";
878+
int ret;
879+
880+
hints.ai_family = AF_INET;
881+
882+
ret = zsock_getaddrinfo(hostname, NULL, &hints, &addrinfos);
883+
zassert_equal(ret, 0, "getaddrinfo failed with %d (%s)",
884+
ret, zsock_gai_strerror(ret));
885+
886+
zassert_equal(memcmp(&net_sin(addrinfos[0].ai_addr)->sin_addr,
887+
&addr, sizeof(struct in_addr)),
888+
0, "not loopback address");
889+
}
890+
891+
ZTEST(dns_resolve, test_dns_localhost_resolve_ipv6)
892+
{
893+
Z_TEST_SKIP_IFNDEF(CONFIG_NET_IPV6);
894+
895+
struct in6_addr addr = IN6ADDR_LOOPBACK_INIT;
896+
struct zsock_addrinfo hints = { 0 };
897+
struct zsock_addrinfo *addrinfos;
898+
const char *hostname = "localhost";
899+
int ret;
900+
901+
hints.ai_family = AF_INET6;
902+
903+
ret = zsock_getaddrinfo(hostname, NULL, &hints, &addrinfos);
904+
zassert_equal(ret, 0, "getaddrinfo failed with %d (%s)",
905+
ret, zsock_gai_strerror(ret));
906+
907+
zassert_equal(memcmp(&net_sin6(addrinfos[0].ai_addr)->sin6_addr,
908+
&addr, sizeof(struct in6_addr)),
909+
0, "not loopback address");
910+
}
911+
816912
ZTEST_SUITE(dns_resolve, NULL, test_init, NULL, NULL, NULL);

tests/net/lib/dns_resolve/testcase.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ tests:
2121
- CONFIG_MDNS_RESPONDER=n
2222
- CONFIG_NET_IPV6_MLD=y
2323
- CONFIG_NET_IPV4_IGMP=y
24+
net.dns.resolve.hostname:
25+
extra_configs:
26+
- CONFIG_NET_HOSTNAME_ENABLE=y
27+
- CONFIG_NET_HOSTNAME="test-zephyr"
28+
- CONFIG_NET_IPV6=y
29+
- CONFIG_NET_IPV4=y
30+
- CONFIG_NET_SOCKETS_DNS_TIMEOUT=5

0 commit comments

Comments
 (0)