From d2da43f7e8b40aaf2d8d49da04ef18d877999548 Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 08:44:58 +0000 Subject: [PATCH] feat(dns): add record accessors --- pkg/dns/nsec.go | 20 ++++++++++++++++++++ pkg/dns/nsec_test.go | 22 ++++++++++++++++++++++ pkg/dns/resource.go | 18 ++++++++++++++++++ pkg/dns/resource_test.go | 8 ++++++++ 4 files changed, 68 insertions(+) diff --git a/pkg/dns/nsec.go b/pkg/dns/nsec.go index 3625910..3f76b1d 100644 --- a/pkg/dns/nsec.go +++ b/pkg/dns/nsec.go @@ -16,6 +16,26 @@ type NSECRecord struct { TTL int } +// GetName returns the owner name. +func (r NSECRecord) GetName() string { + return r.Name +} + +// GetNextDomain returns the canonical successor name. +func (r NSECRecord) GetNextDomain() string { + return r.NextDomain +} + +// GetTypeBitmap returns a copy of the NSEC type bitmap. +func (r NSECRecord) GetTypeBitmap() []byte { + return append([]byte(nil), r.TypeBitmap...) +} + +// GetTTL returns the record TTL. +func (r NSECRecord) GetTTL() int { + return r.TTL +} + // Create constructs a reference NSEC record container. // // name := Create(".", NextName("."), TYPE_MAP_ROOT) diff --git a/pkg/dns/nsec_test.go b/pkg/dns/nsec_test.go index a789644..4802245 100644 --- a/pkg/dns/nsec_test.go +++ b/pkg/dns/nsec_test.go @@ -33,10 +33,32 @@ func TestNSECRecordHelpers(t *testing.T) { t.Fatalf("Create bitmap = %x, want %x", rr.TypeBitmap, bitmap) } + if rr.GetName() != rr.Name { + t.Fatalf("GetName() = %q, want %q", rr.GetName(), rr.Name) + } + + if rr.GetNextDomain() != rr.NextDomain { + t.Fatalf("GetNextDomain() = %q, want %q", rr.GetNextDomain(), rr.NextDomain) + } + + if rr.GetTTL() != rr.TTL { + t.Fatalf("GetTTL() = %d, want %d", rr.GetTTL(), rr.TTL) + } + + if !bytes.Equal(rr.GetTypeBitmap(), rr.TypeBitmap) { + t.Fatalf("GetTypeBitmap() = %x, want %x", rr.GetTypeBitmap(), rr.TypeBitmap) + } + bitmap[0] = 0xff if rr.TypeBitmap[0] != 0x01 { t.Fatal("Create should copy the type bitmap") } + + clone := rr.GetTypeBitmap() + clone[0] = 0xff + if rr.TypeBitmap[0] != 0x01 { + t.Fatal("GetTypeBitmap should return a copy") + } } func TestNextName(t *testing.T) { diff --git a/pkg/dns/resource.go b/pkg/dns/resource.go index 3b5964f..e389cbc 100644 --- a/pkg/dns/resource.go +++ b/pkg/dns/resource.go @@ -30,6 +30,24 @@ type Resource struct { Records []ResourceRecord } +// GetTTL returns the resource TTL. +func (r *Resource) GetTTL() int { + if r == nil { + return 0 + } + + return r.TTL +} + +// GetRecords returns the resource records. +func (r *Resource) GetRecords() []ResourceRecord { + if r == nil { + return nil + } + + return r.Records +} + // ResourceJSON represents the JSON form of a DNS resource. type ResourceJSON struct { Records []json.RawMessage `json:"records"` diff --git a/pkg/dns/resource_test.go b/pkg/dns/resource_test.go index 25ef1e9..6bb4a83 100644 --- a/pkg/dns/resource_test.go +++ b/pkg/dns/resource_test.go @@ -248,6 +248,14 @@ func TestResourceTypeHelpers(t *testing.T) { if !resource.HasDS() || !resource.GetHasDS() { t.Fatal("HasDS should report DS records") } + + if got := resource.GetTTL(); got != DEFAULT_TTL { + t.Fatalf("GetTTL() = %d, want %d", got, DEFAULT_TTL) + } + + if got := resource.GetRecords(); len(got) != len(resource.Records) { + t.Fatalf("GetRecords() = %d records, want %d", len(got), len(resource.Records)) + } } func TestResourceRecordTypeAliases(t *testing.T) {