github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/internal/mdns/dns_sd_test.go (about)

     1  package mdns
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  import "github.com/miekg/dns"
     8  
     9  type mockMDNSService struct{}
    10  
    11  func (s *mockMDNSService) Records(q dns.Question) []dns.RR {
    12  	return []dns.RR{
    13  		&dns.PTR{
    14  			Hdr: dns.RR_Header{
    15  				Name:   "fakerecord",
    16  				Rrtype: dns.TypePTR,
    17  				Class:  dns.ClassINET,
    18  				Ttl:    42,
    19  			},
    20  			Ptr: "fake.local.",
    21  		},
    22  	}
    23  }
    24  
    25  func (s *mockMDNSService) Announcement() []dns.RR {
    26  	return []dns.RR{
    27  		&dns.PTR{
    28  			Hdr: dns.RR_Header{
    29  				Name:   "fakeannounce",
    30  				Rrtype: dns.TypePTR,
    31  				Class:  dns.ClassINET,
    32  				Ttl:    42,
    33  			},
    34  			Ptr: "fake.local.",
    35  		},
    36  	}
    37  }
    38  
    39  func TestDNSSDServiceRecords(t *testing.T) {
    40  	s := &DNSSDService{
    41  		MDNSService: &MDNSService{
    42  			serviceAddr: "_foobar._tcp.local.",
    43  			Domain:      "local",
    44  		},
    45  	}
    46  	q := dns.Question{
    47  		Name:   "_services._dns-sd._udp.local.",
    48  		Qtype:  dns.TypePTR,
    49  		Qclass: dns.ClassINET,
    50  	}
    51  	recs := s.Records(q)
    52  	if got, want := len(recs), 1; got != want {
    53  		t.Fatalf("s.Records(%v) returned %v records, want %v", q, got, want)
    54  	}
    55  
    56  	want := dns.RR(&dns.PTR{
    57  		Hdr: dns.RR_Header{
    58  			Name:   "_services._dns-sd._udp.local.",
    59  			Rrtype: dns.TypePTR,
    60  			Class:  dns.ClassINET,
    61  			Ttl:    defaultTTL,
    62  		},
    63  		Ptr: "_foobar._tcp.local.",
    64  	})
    65  	if got := recs[0]; !reflect.DeepEqual(got, want) {
    66  		t.Errorf("s.Records()[0] = %v, want %v", got, want)
    67  	}
    68  }