feat(dns): add GetNS aliases for DNS records

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-04 07:47:50 +00:00
parent 4e0e5e7be9
commit 887b409623
2 changed files with 64 additions and 0 deletions

View file

@ -158,30 +158,45 @@ func (NSRecord) Type() HSType { return HSTypeNS }
// GetType is an alias for Type.
func (r NSRecord) GetType() HSType { return r.Type() }
// GetNS is an alias for the NS field accessor.
func (r NSRecord) GetNS() string { return r.NS }
// Type returns the DNS record type.
func (GLUE4Record) Type() HSType { return HSTypeGLUE4 }
// GetType is an alias for Type.
func (r GLUE4Record) GetType() HSType { return r.Type() }
// GetNS is an alias for the NS field accessor.
func (r GLUE4Record) GetNS() string { return r.NS }
// Type returns the DNS record type.
func (GLUE6Record) Type() HSType { return HSTypeGLUE6 }
// GetType is an alias for Type.
func (r GLUE6Record) GetType() HSType { return r.Type() }
// GetNS is an alias for the NS field accessor.
func (r GLUE6Record) GetNS() string { return r.NS }
// Type returns the DNS record type.
func (SYNTH4Record) Type() HSType { return HSTypeSYNTH4 }
// GetType is an alias for Type.
func (r SYNTH4Record) GetType() HSType { return r.Type() }
// GetNS is an alias for NS.
func (r SYNTH4Record) GetNS() string { return r.NS() }
// Type returns the DNS record type.
func (SYNTH6Record) Type() HSType { return HSTypeSYNTH6 }
// GetType is an alias for Type.
func (r SYNTH6Record) GetType() HSType { return r.Type() }
// GetNS is an alias for NS.
func (r SYNTH6Record) GetNS() string { return r.NS() }
// Type returns the DNS record type.
func (TXTRecord) Type() HSType { return HSTypeTXT }

View file

@ -83,6 +83,10 @@ func TestResourceEncodeDecodeRoundTrip(t *testing.T) {
t.Fatalf("SYNTH4 NS = %q, want %q", synth4.NS(), "_oopm828._synth.")
}
if synth4.GetNS() != synth4.NS() {
t.Fatalf("SYNTH4 GetNS = %q, want %q", synth4.GetNS(), synth4.NS())
}
synth6, ok := decoded.Records[5].(SYNTH6Record)
if !ok || synth6.Address.String() != "2001:db8::9" {
t.Fatalf("decoded SYNTH6 record = %#v, want 2001:db8::9", decoded.Records[5])
@ -92,6 +96,10 @@ func TestResourceEncodeDecodeRoundTrip(t *testing.T) {
t.Fatalf("SYNTH6 NS = %q, want %q", synth6.NS(), "_400gre00000000000000000014._synth.")
}
if synth6.GetNS() != synth6.NS() {
t.Fatalf("SYNTH6 GetNS = %q, want %q", synth6.GetNS(), synth6.NS())
}
txt, ok := decoded.Records[6].(TXTRecord)
if !ok || len(txt.Entries) != 2 || txt.Entries[0] != "hello" || txt.Entries[1] != "world" {
t.Fatalf("decoded TXT record = %#v, want [hello world]", decoded.Records[6])
@ -293,6 +301,47 @@ func TestResourceRecordTypeAliases(t *testing.T) {
}
}
func TestResourceRecordNSAliases(t *testing.T) {
cases := []struct {
name string
value any
want string
}{
{name: "ns", value: NSRecord{NS: "ns1.example."}, want: "ns1.example."},
{name: "glue4", value: GLUE4Record{NS: "ns1.example."}, want: "ns1.example."},
{name: "glue6", value: GLUE6Record{NS: "ns2.example."}, want: "ns2.example."},
{name: "synth4", value: SYNTH4Record{Address: netip.MustParseAddr("198.51.100.9")}, want: "_oopm828._synth."},
{name: "synth6", value: SYNTH6Record{Address: netip.MustParseAddr("2001:db8::9")}, want: "_400gre00000000000000000014._synth."},
}
for _, tc := range cases {
switch rr := tc.value.(type) {
case NSRecord:
if got := rr.GetNS(); got != tc.want {
t.Fatalf("%s GetNS() = %q, want %q", tc.name, got, tc.want)
}
case GLUE4Record:
if got := rr.GetNS(); got != tc.want {
t.Fatalf("%s GetNS() = %q, want %q", tc.name, got, tc.want)
}
case GLUE6Record:
if got := rr.GetNS(); got != tc.want {
t.Fatalf("%s GetNS() = %q, want %q", tc.name, got, tc.want)
}
case SYNTH4Record:
if got := rr.GetNS(); got != tc.want {
t.Fatalf("%s GetNS() = %q, want %q", tc.name, got, tc.want)
}
case SYNTH6Record:
if got := rr.GetNS(); got != tc.want {
t.Fatalf("%s GetNS() = %q, want %q", tc.name, got, tc.want)
}
default:
t.Fatalf("%s: unexpected record type %T", tc.name, tc.value)
}
}
}
func TestResourceToNSEC(t *testing.T) {
cases := []struct {
name string