@@ -23,6 +23,8 @@ LOG_MODULE_REGISTER(net_test, CONFIG_DNS_RESOLVER_LOG_LEVEL);
23
23
#include <zephyr/net/net_ip.h>
24
24
#include <zephyr/net/net_if.h>
25
25
#include <zephyr/net/dns_resolve.h>
26
+ #include <zephyr/net/hostname.h>
27
+ #include <zephyr/net/socket.h>
26
28
27
29
#define NET_LOG_ENABLED 1
28
30
#include "net_private.h"
@@ -813,4 +815,98 @@ ZTEST(dns_resolve, test_mdns_ipv6_mld_group)
813
815
zassert_true (st , "IPv6 mDNS group not joined" );
814
816
}
815
817
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
+
816
912
ZTEST_SUITE (dns_resolve , NULL , test_init , NULL , NULL , NULL );
0 commit comments