Correctly handle CNAME/DNAMEs

This commit is contained in:
Miek Gieben 2015-08-26 12:19:22 +01:00
parent e1d5f172ae
commit ac493072c7
2 changed files with 14 additions and 13 deletions

View File

@ -44,7 +44,8 @@ func Dedup(rrs []RR) []RR {
} }
// If the length of the result map equals the amount of RRs we got, // If the length of the result map equals the amount of RRs we got,
// it means they were all different. We can then just return the original rrset. // it means they were all different. We can then just return the original rrset.
if len(m) == len(rrs) { // We can only do this when we haven't found a CNAME or DNAME.
if len(m) == len(rrs) && len(cname) == 0 && len(dname) == 0 {
return rrs return rrs
} }
@ -117,22 +118,24 @@ func normalizedString(r RR) (string, int) {
// needsDeletion checks if the RR is masked by either a CNAME or a DNAME. // needsDeletion checks if the RR is masked by either a CNAME or a DNAME.
// If so it return true. // If so it return true.
func needsDeletion(r RR, s string, cname, dname []string) bool { func needsDeletion(r RR, s string, cname, dname []string) bool {
// TODO(miek): fix.
// This is too broad, but we have to be care full not to delete ourselves.
if r.Header().Rrtype == TypeCNAME || r.Header().Rrtype == TypeDNAME {
return false
}
// For CNAME we can do strings.HasPrefix with s. // For CNAME we can do strings.HasPrefix with s.
// For DNAME we can do strings.Contains with s. // For DNAME we can do strings.Contains with s.
// Either signals a removal of this RR. // Either signals a removal of this RR.
for _, c := range cname { for _, c := range cname {
if strings.HasPrefix(s, c) { if strings.HasPrefix(s, c) {
if r.Header().Rrtype == TypeCNAME {
// don't delete yourself
continue
}
return true return true
} }
} }
for _, d := range dname { for _, d := range dname {
if strings.Contains(s, d) { if strings.Contains(s, d) {
if r.Header().Rrtype == TypeDNAME && strings.HasPrefix(s, d) {
// don't delete yourself
continue
}
return true return true
} }
} }

View File

@ -39,15 +39,13 @@ func TestDedup(t *testing.T) {
}, },
} }
T := 0
for rr, expected := range testcases { for rr, expected := range testcases {
out := Dedup([]RR{rr[0], rr[1], rr[2]}) out := Dedup([]RR{rr[0], rr[1], rr[2]})
for i, o := range out { for i, o := range out {
if o.String() != expected[i] { if o.String() != expected[i] {
t.Fatalf("test %d, expected %v, got %v", T, expected[i], o.String()) t.Fatalf("expected %v, got %v", expected[i], o.String())
} }
} }
T++
} }
} }
@ -88,18 +86,18 @@ func TestDedupWithCNAMEDNAME(t *testing.T) {
}: []string{"miek.nl.\t3600\tIN\tDNAME\ta."}, }: []string{"miek.nl.\t3600\tIN\tDNAME\ta."},
} }
T := 0
for rr, expected := range testcases { for rr, expected := range testcases {
out := Dedup([]RR{rr[0], rr[1], rr[2], rr[3]}) out := Dedup([]RR{rr[0], rr[1], rr[2], rr[3]})
for i, o := range out { for i, o := range out {
if o.String() != expected[i] { if o.String() != expected[i] {
t.Fatalf("test %d, expected %v, got %v", T, expected[i], o.String()) t.Fatalf("expected %v, got %v", expected[i], o.String())
} }
} }
T++
} }
} }
// BenchMark test as well TODO(miek)
func TestNormalizedString(t *testing.T) { func TestNormalizedString(t *testing.T) {
tests := map[RR]string{ tests := map[RR]string{
newRR(t, "mIEk.Nl. 3600 IN A 127.0.0.1"): "miek.nl.\tIN\tA\t127.0.0.1", newRR(t, "mIEk.Nl. 3600 IN A 127.0.0.1"): "miek.nl.\tIN\tA\t127.0.0.1",