@@ -2,6 +2,7 @@ package dns
22
33import (
44 "net"
5+ "slices"
56 "testing"
67
78 mdns "github.com/miekg/dns"
@@ -114,6 +115,77 @@ func TestQueryUDPSuccessSkipsTCPFallback(t *testing.T) {
114115 }
115116}
116117
118+ func TestParseHTTPS (t * testing.T ) {
119+ hdr := mdns.RR_Header {Name : "example.com." , Rrtype : mdns .TypeHTTPS , Class : mdns .ClassINET , Ttl : 300 }
120+
121+ service := & mdns.HTTPS {SVCB : mdns.SVCB {
122+ Hdr : hdr ,
123+ Priority : 2 ,
124+ Target : "." ,
125+ Value : []mdns.SVCBKeyValue {
126+ & mdns.SVCBAlpn {Alpn : []string {"h3" , "h2" }},
127+ & mdns.SVCBPort {Port : 8443 },
128+ & mdns.SVCBIPv4Hint {Hint : []net.IP {net .ParseIP ("192.0.2.1" )}},
129+ & mdns.SVCBIPv6Hint {Hint : []net.IP {net .ParseIP ("2001:db8::1" )}},
130+ & mdns.SVCBECHConfig {ECH : []byte {0x00 , 0x01 }},
131+ & mdns.SVCBMandatory {Code : []mdns.SVCBKey {mdns .SVCB_ALPN }},
132+ },
133+ }}
134+ alias := & mdns.HTTPS {SVCB : mdns.SVCB {
135+ Hdr : hdr ,
136+ Priority : 0 ,
137+ Target : "svc.example.net." ,
138+ }}
139+
140+ // Pass the higher-priority record first to confirm sorting by priority.
141+ got := parseHTTPS ([]mdns.RR {service , alias })
142+
143+ if len (got ) != 2 {
144+ t .Fatalf ("expected 2 records, got %d" , len (got ))
145+ }
146+
147+ // Alias (priority 0) should sort ahead of the service record.
148+ if got [0 ].Priority != 0 || got [0 ].Target != "svc.example.net" {
149+ t .Errorf ("record[0] = %+v, want alias mode targeting svc.example.net" , got [0 ])
150+ }
151+
152+ svc := got [1 ]
153+ if svc .Priority != 2 {
154+ t .Errorf ("priority = %d, want 2" , svc .Priority )
155+ }
156+ if svc .Target != "." {
157+ t .Errorf ("target = %q, want \" .\" (self)" , svc .Target )
158+ }
159+ if want := []string {"h3" , "h2" }; ! slices .Equal (svc .ALPN , want ) {
160+ t .Errorf ("alpn = %v, want %v" , svc .ALPN , want )
161+ }
162+ if svc .Port != 8443 {
163+ t .Errorf ("port = %d, want 8443" , svc .Port )
164+ }
165+ if want := []string {"192.0.2.1" }; ! slices .Equal (svc .IPv4Hint , want ) {
166+ t .Errorf ("ipv4hint = %v, want %v" , svc .IPv4Hint , want )
167+ }
168+ if want := []string {"2001:db8::1" }; ! slices .Equal (svc .IPv6Hint , want ) {
169+ t .Errorf ("ipv6hint = %v, want %v" , svc .IPv6Hint , want )
170+ }
171+ if svc .ECHConfig != "AAE=" {
172+ t .Errorf ("ech config = %q, want base64 of {0x00,0x01}" , svc .ECHConfig )
173+ }
174+ if want := []string {"mandatory=alpn" }; ! slices .Equal (svc .Params , want ) {
175+ t .Errorf ("params = %v, want %v" , svc .Params , want )
176+ }
177+ }
178+
179+ func TestParseHTTPSIgnoresNonHTTPS (t * testing.T ) {
180+ a := & mdns.A {
181+ Hdr : mdns.RR_Header {Name : "example.com." , Rrtype : mdns .TypeA , Class : mdns .ClassINET , Ttl : 60 },
182+ A : net .ParseIP ("192.0.2.1" ),
183+ }
184+ if got := parseHTTPS ([]mdns.RR {a }); got != nil {
185+ t .Errorf ("expected nil for non-HTTPS records, got %v" , got )
186+ }
187+ }
188+
117189func TestDecodeRNAME (t * testing.T ) {
118190 tests := []struct {
119191 name string
0 commit comments