99
1010#include <ztest.h>
1111
12- #include <logging/log.h>
13- LOG_MODULE_REGISTER (test_net_dns_sd , LOG_LEVEL_DBG );
14-
12+ #include <net/dns_sd.h>
1513#include <net/net_context.h>
1614#include <net/net_pkt.h>
1715
@@ -551,7 +549,7 @@ static void test_dns_sd_handle_ptr_query(void)
551549 "dns_sd_handle_ptr_query() failed (%d)" ,
552550 actual_int );
553551
554- zassert_equal (actual_int , expected_int , "" );
552+ zassert_equal (actual_int , expected_int , "act: %d exp: %d" , actual_int , expected_int );
555553
556554 zassert_mem_equal (actual_rsp , expected_rsp ,
557555 MIN (actual_int , expected_int ), "" );
@@ -576,6 +574,58 @@ static void test_dns_sd_handle_ptr_query(void)
576574 sizeof (struct dns_header )), "" );
577575}
578576
577+ /** Test for @ref dns_sd_handle_ptr_query */
578+ static void test_dns_sd_handle_service_type_enum (void )
579+ {
580+ DNS_SD_REGISTER_TCP_SERVICE (chromecast ,
581+ "Chromecast-abcd" ,
582+ "_googlecast" ,
583+ "local" ,
584+ DNS_SD_EMPTY_TXT ,
585+ CONST_PORT );
586+
587+ struct in_addr addr = {
588+ .s_addr = htonl (IP_ADDR (177 , 5 , 240 , 13 )),
589+ };
590+ static uint8_t actual_rsp [512 ];
591+ static uint8_t expected_rsp [] = {
592+ 0x00 , 0x00 , 0x84 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 ,
593+ 0x00 , 0x00 , 0x00 , 0x09 , 0x5f , 0x73 , 0x65 , 0x72 , 0x76 ,
594+ 0x69 , 0x63 , 0x65 , 0x73 , 0x07 , 0x5f , 0x64 , 0x6e , 0x73 ,
595+ 0x2d , 0x73 , 0x64 , 0x04 , 0x5f , 0x75 , 0x64 , 0x70 , 0x05 ,
596+ 0x6c , 0x6f , 0x63 , 0x61 , 0x6c , 0x00 , 0x00 , 0x0c , 0x00 ,
597+ 0x01 , 0x00 , 0x00 , 0x11 , 0x94 , 0x00 , 0x13 , 0x0b , 0x5f ,
598+ 0x67 , 0x6f , 0x6f , 0x67 , 0x6c , 0x65 , 0x63 , 0x61 , 0x73 ,
599+ 0x74 , 0x04 , 0x5f , 0x74 , 0x63 , 0x70 , 0xc0 , 0x23 ,
600+ };
601+ int expected_int = sizeof (expected_rsp );
602+ int actual_int = dns_sd_handle_service_type_enum (& chromecast ,
603+ & addr ,
604+ NULL ,
605+ & actual_rsp [0 ],
606+ sizeof (actual_rsp ) -
607+ sizeof (struct dns_header ));
608+
609+ zassert_true (actual_int > 0 , "dns_sd_handle_service_type_enum() failed (%d)" , actual_int );
610+
611+ zassert_equal (actual_int , expected_int , "act: %d exp: %d" , actual_int , expected_int );
612+
613+ zassert_mem_equal (actual_rsp , expected_rsp , MIN (actual_int , expected_int ), "" );
614+
615+ /* show non-advertisement for uninitialized port */
616+ nonconst_port = 0 ;
617+ zassert_equal (- EHOSTDOWN ,
618+ dns_sd_handle_service_type_enum (& nasxxxxxx_ephemeral , & addr , NULL ,
619+ & actual_rsp [0 ], sizeof (actual_rsp ) - sizeof (struct dns_header )),
620+ "port zero should not "
621+ "produce any DNS-SD query response" );
622+
623+ zassert_equal (- EINVAL ,
624+ dns_sd_handle_service_type_enum (& invalid_dns_sd_record , & addr , NULL ,
625+ & actual_rsp [0 ], sizeof (actual_rsp ) - sizeof (struct dns_header )),
626+ "" );
627+ }
628+
579629/** Test @ref dns_sd_rec_match */
580630static void test_dns_sd_rec_match (void )
581631{
@@ -683,6 +733,104 @@ static void test_setup_dst_addr(void)
683733 setup_dst_addr (ctx_xx , pkt_xx , & dst , & dst_len ), "" );
684734}
685735
736+ /** test for @ref dns_sd_is_service_type_enumeration */
737+ static void test_is_service_type_enumeration (void )
738+ {
739+ static const struct dns_sd_rec filter_ok = {
740+ .instance = "_services" ,
741+ .service = "_dns-sd" ,
742+ .proto = "_udp" ,
743+ /* TODO: support additional service domains */
744+ .domain = "local" ,
745+ .text = dns_sd_empty_txt ,
746+ .text_size = sizeof (dns_sd_empty_txt ),
747+ .port = & dns_sd_port_zero ,
748+ };
749+
750+ zassert_true (dns_sd_is_service_type_enumeration (& filter_ok ), "" );
751+
752+ static const struct dns_sd_rec filter_nok = {
753+ /* not a service_type_enumeration */
754+ .instance = "_serv1c3s" , .service = "_dns-sd" ,
755+ .proto = "_udp" , .domain = "local" ,
756+ .text = dns_sd_empty_txt , .text_size = sizeof (dns_sd_empty_txt ),
757+ .port = & dns_sd_port_zero ,
758+ };
759+
760+ zassert_false (dns_sd_is_service_type_enumeration (& filter_nok ), "" );
761+ }
762+
763+ static void test_extract_service_type_enumeration (void )
764+ {
765+ static const uint8_t query [] = {
766+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x01 , 0x00 , 0x18 , 0x00 , 0x00 , 0x00 , 0x00 , 0x09 , 0x5f ,
767+ 0x73 , 0x65 , 0x72 , 0x76 , 0x69 , 0x63 , 0x65 , 0x73 , 0x07 , 0x5f , 0x64 , 0x6e , 0x73 , 0x2d ,
768+ 0x73 , 0x64 , 0x04 , 0x5f , 0x75 , 0x64 , 0x70 , 0x05 , 0x6c , 0x6f , 0x63 , 0x61 , 0x6c , 0x00 ,
769+ };
770+
771+ struct dns_sd_rec record ;
772+ char instance [DNS_SD_INSTANCE_MAX_SIZE + 1 ];
773+ char service [DNS_SD_SERVICE_MAX_SIZE + 1 ];
774+ char proto [DNS_SD_PROTO_SIZE + 1 ];
775+ char domain [DNS_SD_DOMAIN_MAX_SIZE + 1 ];
776+ char * label [4 ];
777+ size_t size [] = {
778+ ARRAY_SIZE (instance ),
779+ ARRAY_SIZE (service ),
780+ ARRAY_SIZE (proto ),
781+ ARRAY_SIZE (domain ),
782+ };
783+ size_t n = ARRAY_SIZE (label );
784+
785+ BUILD_ASSERT (ARRAY_SIZE (label ) == ARRAY_SIZE (size ), "" );
786+
787+ /*
788+ * work around for bug in compliance scripts which say that the array
789+ * should be static const (incorrect)
790+ */
791+ label [0 ] = instance ;
792+ label [1 ] = service ;
793+ label [2 ] = proto ;
794+ label [3 ] = domain ;
795+
796+ zassert_equal (ARRAY_SIZE (query ),
797+ dns_sd_query_extract (query , ARRAY_SIZE (query ), & record , label , size , & n ),
798+ "failed to extract service type enumeration" );
799+
800+ zassert_true (dns_sd_is_service_type_enumeration (& record ), "" );
801+ }
802+
803+ static void test_wildcard_comparison (void )
804+ {
805+ size_t n_matches = 0 ;
806+ size_t n_records = 0 ;
807+ struct dns_sd_rec filter ;
808+
809+ dns_sd_create_wildcard_filter (& filter );
810+
811+ DNS_SD_FOREACH (record ) {
812+ if (!rec_is_valid (record )) {
813+ continue ;
814+ }
815+
816+ ++ n_records ;
817+ }
818+
819+ DNS_SD_FOREACH (record ) {
820+ if (!rec_is_valid (record )) {
821+ continue ;
822+ }
823+
824+ if (dns_sd_rec_match (record , & filter )) {
825+ ++ n_matches ;
826+ }
827+ }
828+
829+ zassert_true (n_records > 0 , "there must be > 0 records" );
830+ zassert_equal (n_matches , n_records , "wildcard filter does not match "
831+ "all records: n_records: %zu n_matches: %zu" , n_records , n_matches );
832+ }
833+
686834void test_main (void )
687835{
688836 ztest_test_suite (dns_sd_tests ,
@@ -696,7 +844,11 @@ void test_main(void)
696844 ztest_unit_test (test_add_aaaa_record ),
697845 ztest_unit_test (test_dns_sd_handle_ptr_query ),
698846 ztest_unit_test (test_dns_sd_rec_match ),
699- ztest_unit_test (test_setup_dst_addr ));
847+ ztest_unit_test (test_setup_dst_addr ),
848+ ztest_unit_test (test_is_service_type_enumeration ),
849+ ztest_unit_test (test_extract_service_type_enumeration ),
850+ ztest_unit_test (test_wildcard_comparison ),
851+ ztest_unit_test (test_dns_sd_handle_service_type_enum ));
700852
701853 ztest_run_test_suite (dns_sd_tests );
702854}
0 commit comments