Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions internal/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,25 @@ func Name(s string) string {
// (#189), while the addresses behind a CDN alias rotate with every TTL and
// would make every CDN host look changed.
func RecordsChanged(prev, cur []dns.Record, types []string) bool {
compared := func(t string) bool { return len(types) == 0 || slices.Contains(types, t) }
norm := make([]string, 0, len(types))
for _, t := range types {
if t = strings.ToUpper(strings.TrimSpace(t)); t != "" {
norm = append(norm, t)
}
}
compared := func(t string) bool { return len(norm) == 0 || slices.Contains(norm, t) }
set := func(rs []dns.Record, only string) map[string]struct{} {
s := map[string]struct{}{}
for _, r := range rs {
t := strings.ToUpper(r.Type)
t := strings.ToUpper(strings.TrimSpace(r.Type))
if (only != "" && t != only) || !compared(t) {
continue
}
s[t+" "+strings.ToLower(strings.TrimSuffix(r.Value, "."))] = struct{}{}
v := r.Value
if t != "TXT" {
v = strings.ToLower(strings.TrimSuffix(v, "."))
}
s[t+" "+v] = struct{}{}
}
return s
}
Expand Down
4 changes: 4 additions & 0 deletions internal/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func TestRecordsChanged(t *testing.T) {
{"without CNAME lookups addresses decide", []dns.Record{cname("x.example"), a("192.0.2.1")}, []dns.Record{a("192.0.2.1")}, []string{"A", "AAAA"}, false},
{"no types compares every type", []dns.Record{a("192.0.2.1"), {Type: "TXT", Value: "v=spf1 -all"}}, []dns.Record{a("192.0.2.1")}, nil, true},
{"no types still prefers the alias", []dns.Record{cname("app.cdn.example"), a("192.0.2.1")}, []dns.Record{cname("app.cdn.example"), a("192.0.2.7")}, nil, false},
{"lower-case types", []dns.Record{a("192.0.2.1")}, []dns.Record{a("192.0.2.2")}, []string{"a"}, true},
{"space-padded types", []dns.Record{a("192.0.2.1")}, []dns.Record{a("192.0.2.2")}, []string{" a ", " CNAME"}, true},
{"lower-case cname still prefers the alias", []dns.Record{cname("app.cdn.example"), a("192.0.2.1")}, []dns.Record{cname("app.cdn.example"), a("192.0.2.7")}, []string{"a", "cname"}, false},
{"TXT case change", []dns.Record{{Type: "TXT", Value: "token=AbC"}}, []dns.Record{{Type: "TXT", Value: "token=abc"}}, []string{"txt"}, true},
} {
if got := RecordsChanged(c.prev, c.cur, c.types); got != c.want {
t.Errorf("%s: changed = %v, want %v", c.name, got, c.want)
Expand Down
18 changes: 18 additions & 0 deletions pkg/subenum/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ func TestDiffTypes(t *testing.T) {
}
}

func TestDiffTypesCaseInsensitive(t *testing.T) {
prev := []subenum.Result{{Name: "a.example.com", Records: []subenum.Record{{Type: "A", Value: "192.0.2.1"}}}}
cur := []subenum.Result{{Name: "a.example.com", Records: []subenum.Record{{Type: "A", Value: "192.0.2.2"}}}}
for _, types := range [][]string{{"A"}, {"a"}, {" a "}} {
if got := subenum.Diff(prev, cur, types); len(got.Changed) != 1 {
t.Errorf("types %q: Changed = %+v, want a.example.com", types, got.Changed)
}
}
}

func TestDiffTXTCase(t *testing.T) {
prev := []subenum.Result{{Name: "t.example.com", Records: []subenum.Record{{Type: "TXT", Value: "token=AbC"}}}}
cur := []subenum.Result{{Name: "t.example.com", Records: []subenum.Record{{Type: "TXT", Value: "token=abc"}}}}
if got := subenum.Diff(prev, cur, []string{"TXT"}); len(got.Changed) != 1 {
t.Errorf("Changed = %+v, want t.example.com", got.Changed)
}
}

func TestDiffEmpty(t *testing.T) {
got := subenum.Diff(nil, nil, nil)
if got.Added != nil || got.Removed != nil || got.Changed != nil {
Expand Down