Skip to content

Commit aebef29

Browse files
Fix #397: subenum.Diff reports no changes when Types are lower case; TX…
Closes #397. Prepared by AgentLab (an AI agent system).
1 parent dd5b156 commit aebef29

3 files changed

Lines changed: 35 additions & 3 deletions

File tree

‎internal/diff/diff.go‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,25 @@ func Name(s string) string {
2323
// (#189), while the addresses behind a CDN alias rotate with every TTL and
2424
// would make every CDN host look changed.
2525
func RecordsChanged(prev, cur []dns.Record, types []string) bool {
26-
compared := func(t string) bool { return len(types) == 0 || slices.Contains(types, t) }
26+
norm := make([]string, 0, len(types))
27+
for _, t := range types {
28+
if t = strings.ToUpper(strings.TrimSpace(t)); t != "" {
29+
norm = append(norm, t)
30+
}
31+
}
32+
compared := func(t string) bool { return len(norm) == 0 || slices.Contains(norm, t) }
2733
set := func(rs []dns.Record, only string) map[string]struct{} {
2834
s := map[string]struct{}{}
2935
for _, r := range rs {
30-
t := strings.ToUpper(r.Type)
36+
t := strings.ToUpper(strings.TrimSpace(r.Type))
3137
if (only != "" && t != only) || !compared(t) {
3238
continue
3339
}
34-
s[t+" "+strings.ToLower(strings.TrimSuffix(r.Value, "."))] = struct{}{}
40+
v := r.Value
41+
if t != "TXT" {
42+
v = strings.ToLower(strings.TrimSuffix(v, "."))
43+
}
44+
s[t+" "+v] = struct{}{}
3545
}
3646
return s
3747
}

‎internal/diff/diff_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func TestRecordsChanged(t *testing.T) {
2828
{"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},
2929
{"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},
3030
{"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},
31+
{"lower-case types", []dns.Record{a("192.0.2.1")}, []dns.Record{a("192.0.2.2")}, []string{"a"}, true},
32+
{"space-padded types", []dns.Record{a("192.0.2.1")}, []dns.Record{a("192.0.2.2")}, []string{" a ", " CNAME"}, true},
33+
{"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},
34+
{"TXT case change", []dns.Record{{Type: "TXT", Value: "token=AbC"}}, []dns.Record{{Type: "TXT", Value: "token=abc"}}, []string{"txt"}, true},
3135
} {
3236
if got := RecordsChanged(c.prev, c.cur, c.types); got != c.want {
3337
t.Errorf("%s: changed = %v, want %v", c.name, got, c.want)

‎pkg/subenum/diff_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,24 @@ func TestDiffTypes(t *testing.T) {
4848
}
4949
}
5050

51+
func TestDiffTypesCaseInsensitive(t *testing.T) {
52+
prev := []subenum.Result{{Name: "a.example.com", Records: []subenum.Record{{Type: "A", Value: "192.0.2.1"}}}}
53+
cur := []subenum.Result{{Name: "a.example.com", Records: []subenum.Record{{Type: "A", Value: "192.0.2.2"}}}}
54+
for _, types := range [][]string{{"A"}, {"a"}, {" a "}} {
55+
if got := subenum.Diff(prev, cur, types); len(got.Changed) != 1 {
56+
t.Errorf("types %q: Changed = %+v, want a.example.com", types, got.Changed)
57+
}
58+
}
59+
}
60+
61+
func TestDiffTXTCase(t *testing.T) {
62+
prev := []subenum.Result{{Name: "t.example.com", Records: []subenum.Record{{Type: "TXT", Value: "token=AbC"}}}}
63+
cur := []subenum.Result{{Name: "t.example.com", Records: []subenum.Record{{Type: "TXT", Value: "token=abc"}}}}
64+
if got := subenum.Diff(prev, cur, []string{"TXT"}); len(got.Changed) != 1 {
65+
t.Errorf("Changed = %+v, want t.example.com", got.Changed)
66+
}
67+
}
68+
5169
func TestDiffEmpty(t *testing.T) {
5270
got := subenum.Diff(nil, nil, nil)
5371
if got.Added != nil || got.Removed != nil || got.Changed != nil {

0 commit comments

Comments
 (0)