| 1 |
package dns |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"slices" |
| 6 |
"sort" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
type name []string |
| 11 |
|
| 12 |
func (n name) Len() int { return len(n) } |
| 13 |
func (n name) Swap(i, j int) { n[i], n[j] = n[j], n[i] } |
| 14 |
func (n name) Less(i, j int) bool { return CompareName(n[i], n[j]) == -1 } |
| 15 |
|
| 16 |
func TestSort(t *testing.T) { |
| 17 |
testcases := []struct { |
| 18 |
name string |
| 19 |
unsorted name |
| 20 |
sorted name |
| 21 |
}{ |
| 22 |
{ |
| 23 |
"powerdns", |
| 24 |
name{"aaa.powerdns.de.", "bbb.powerdns.net.", "xxx.powerdns.com."}, |
| 25 |
name{"xxx.powerdns.com.", "aaa.powerdns.de.", "bbb.powerdns.net."}, |
| 26 |
}, |
| 27 |
{ |
| 28 |
"rfc4034", |
| 29 |
name{"example.", "a.example.", "yljkjljk.a.example.", "Z.a.example.", "zABC.a.EXAMPLE.", "z.example.", "*.z.example."}, |
| 30 |
name{"example.", "a.example.", "yljkjljk.a.example.", "Z.a.example.", "zABC.a.EXAMPLE.", "z.example.", "*.z.example."}, |
| 31 |
}, |
| 32 |
{ |
| 33 |
"rfc4034-ddd", |
| 34 |
name{"example.", "a.example.", "yljkjljk.a.example.", "Z.a.example.", "zABC.a.EXAMPLE.", "z.example.", "\001.z.example.", "*.z.example.", "\200.z.example."}, |
| 35 |
name{"example.", "a.example.", "yljkjljk.a.example.", "Z.a.example.", "zABC.a.EXAMPLE.", "z.example.", "\001.z.example.", "*.z.example.", "\200.z.example."}, |
| 36 |
}, |
| 37 |
{ |
| 38 |
"root", |
| 39 |
name{".", "nl."}, |
| 40 |
name{".", "nl."}, |
| 41 |
}, |
| 42 |
{ |
| 43 |
"dash", |
| 44 |
name{"dns-ext.nic.cr.", "dns.nic.cr."}, |
| 45 |
name{"dns.nic.cr.", "dns-ext.nic.cr."}, |
| 46 |
}, |
| 47 |
} |
| 48 |
for _, tc := range testcases { |
| 49 |
t.Run(tc.name, func(t *testing.T) { |
| 50 |
sort.Sort(tc.unsorted) |
| 51 |
for i := range len(tc.unsorted) { |
| 52 |
if tc.unsorted[i] != tc.sorted[i] { |
| 53 |
t.Fatalf("expected %v, got %v", tc.sorted, tc.unsorted) |
| 54 |
} |
| 55 |
} |
| 56 |
}) |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func TestSortRRset(t *testing.T) { |
| 61 |
testcases := []struct { |
| 62 |
name string |
| 63 |
unsorted RRset |
| 64 |
sorted RRset |
| 65 |
}{ |
| 66 |
{ |
| 67 |
"miekns", |
| 68 |
RRset([]RR{ |
| 69 |
&NS{Hdr: Header{Name: "miek.nl.", Class: ClassINET, TTL: 600}, Ns: "linode.atoom.net."}, |
| 70 |
&NS{Hdr: Header{Name: "miek.nl.", Class: ClassINET, TTL: 600}, Ns: "omval.tednet.nl"}, |
| 71 |
&NS{Hdr: Header{Name: "miek.nl.", Class: ClassINET, TTL: 600}, Ns: "ns-ext.nlnetlabs.nl."}, |
| 72 |
}), |
| 73 |
RRset([]RR{ |
| 74 |
&NS{Hdr: Header{Name: "miek.nl.", Class: ClassINET, TTL: 600}, Ns: "omval.tednet.nl"}, |
| 75 |
&NS{Hdr: Header{Name: "miek.nl.", Class: ClassINET, TTL: 600}, Ns: "linode.atoom.net."}, |
| 76 |
&NS{Hdr: Header{Name: "miek.nl.", Class: ClassINET, TTL: 600}, Ns: "ns-ext.nlnetlabs.nl."}, |
| 77 |
}), |
| 78 |
}, |
| 79 |
{ |
| 80 |
"zwns", |
| 81 |
RRset([]RR{ |
| 82 |
&NS{Hdr: Header{Name: "zw.", Class: ClassINET, TTL: 172800}, Ns: "zw-ns.anycast.pch.net."}, |
| 83 |
&NS{Hdr: Header{Name: "zw.", Class: ClassINET, TTL: 172800}, Ns: "ns1zim.telone.co.zw."}, |
| 84 |
&NS{Hdr: Header{Name: "zw.", Class: ClassINET, TTL: 172800}, Ns: "ns2zim.telone.co.zw."}, |
| 85 |
&NS{Hdr: Header{Name: "zw.", Class: ClassINET, TTL: 172800}, Ns: "ns1.liquidtelecom.net."}, |
| 86 |
&NS{Hdr: Header{Name: "zw.", Class: ClassINET, TTL: 172800}, Ns: "ns2.liquidtelecom.net."}, |
| 87 |
}), |
| 88 |
RRset([]RR{ |
| 89 |
&NS{Hdr: Header{Name: "zw.", Class: ClassINET, TTL: 172800}, Ns: "ns1.liquidtelecom.net."}, |
| 90 |
&NS{Hdr: Header{Name: "zw.", Class: ClassINET, TTL: 172800}, Ns: "ns2.liquidtelecom.net."}, |
| 91 |
&NS{Hdr: Header{Name: "zw.", Class: ClassINET, TTL: 172800}, Ns: "zw-ns.anycast.pch.net."}, |
| 92 |
&NS{Hdr: Header{Name: "zw.", Class: ClassINET, TTL: 172800}, Ns: "ns1zim.telone.co.zw."}, |
| 93 |
&NS{Hdr: Header{Name: "zw.", Class: ClassINET, TTL: 172800}, Ns: "ns2zim.telone.co.zw."}, |
| 94 |
}), |
| 95 |
}, |
| 96 |
} |
| 97 |
for _, tc := range testcases { |
| 98 |
t.Run(tc.name, func(t *testing.T) { |
| 99 |
sort.Sort(tc.unsorted) |
| 100 |
// check rdata only |
| 101 |
for i := range len(tc.unsorted) { |
| 102 |
switch tc.unsorted[i].(type) { |
| 103 |
case *NS: |
| 104 |
if tc.unsorted[i].(*NS).Ns != tc.sorted[i].(*NS).Ns { |
| 105 |
t.Fatalf("expected %s, got %s", tc.sorted[i].(*NS).Ns, tc.unsorted[i].(*NS).Ns) |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
}) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
func TestCompareSerial(t *testing.T) { |
| 114 |
testcases := []struct { |
| 115 |
name string |
| 116 |
a, b uint32 |
| 117 |
want int |
| 118 |
}{ |
| 119 |
{"equal", 100, 100, 0}, |
| 120 |
{"a-less", 100, 200, -1}, |
| 121 |
{"a-greater", 200, 100, 1}, |
| 122 |
// 4200000000 is numerically > 2009032802 but diff (2190967198) > 2^31: a is behind. |
| 123 |
{"rfc1982-a-behind", 4200000000, 2009032802, -1}, |
| 124 |
// inverse: 2009032802 has wrapped past 4200000000, so it is ahead. |
| 125 |
{"rfc1982-a-ahead", 2009032802, 4200000000, 1}, |
| 126 |
// diff exactly 2^31-1 = MaxSerialIncrement: a < b (old code incorrectly returned 1). |
| 127 |
{"boundary-a-less", 1000000000, 1000000000 + MaxSerialIncrement, -1}, |
| 128 |
} |
| 129 |
for _, tc := range testcases { |
| 130 |
t.Run(tc.name, func(t *testing.T) { |
| 131 |
if got := CompareSerial(tc.a, tc.b); got != tc.want { |
| 132 |
t.Fatalf("expected serial %d, got %d", tc.want, got) |
| 133 |
} |
| 134 |
}) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func TestCompare(t *testing.T) { |
| 139 |
testcases := []struct { |
| 140 |
name string |
| 141 |
a RR |
| 142 |
b RR |
| 143 |
ok bool |
| 144 |
}{ |
| 145 |
{ |
| 146 |
"ok:aaaa", |
| 147 |
dnstestNew("a.example.org. IN AAAA 2a01:7e00::f03c:91ff:fef1:6735"), |
| 148 |
dnstestNew("a.example.org. IN AAAA 2a01:7e00::f03c:91ff:fef1:6735"), |
| 149 |
true, |
| 150 |
}, |
| 151 |
{ |
| 152 |
"diff:aaaa", |
| 153 |
dnstestNew("a.example.org. IN AAAA 2a01:7e00::f03c:91ff:fef1:6735"), |
| 154 |
dnstestNew("a.example.org. IN AAAA 3a01:7e00::f03c:91ff:fef1:6735"), |
| 155 |
false, |
| 156 |
}, |
| 157 |
} |
| 158 |
for _, tc := range testcases { |
| 159 |
t.Run(tc.name, func(t *testing.T) { |
| 160 |
got := Equal(tc.a, tc.b) |
| 161 |
if got != tc.ok { |
| 162 |
t.Fatalf("expected %t, got %t for %q == %q", tc.ok, got, tc.a, tc.b) |
| 163 |
} |
| 164 |
}) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
func TestEqualName(t *testing.T) { |
| 169 |
testcases := []struct { |
| 170 |
a string |
| 171 |
b string |
| 172 |
exp bool |
| 173 |
}{ |
| 174 |
{"example.org.", "example.org.", true}, |
| 175 |
{"example.org.", "eXAMPLe.oRG.", true}, |
| 176 |
} |
| 177 |
for i, tc := range testcases { |
| 178 |
got := EqualName(tc.a, tc.b) |
| 179 |
if got != tc.exp { |
| 180 |
t.Errorf("test %d, expected %t, got %t for %s, %s", i, tc.exp, got, tc.a, tc.b) |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
func ExampleRRset_sort() { |
| 186 |
rrs := RRset([]RR{ |
| 187 |
func() RR { rr, _ := New("miek.nl. IN NS linode.atoom.net."); return rr }(), |
| 188 |
func() RR { rr, _ := New("miek.nl. IN NS omval.tednet.nl."); return rr }(), |
| 189 |
func() RR { rr, _ := New("miek.nl. IN NS ns-ext.nlnetlabs.nl."); return rr }(), |
| 190 |
}) |
| 191 |
sort.Sort(rrs) |
| 192 |
for i := range rrs { |
| 193 |
fmt.Println(rrs[i]) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
func ExampleRRset_compact() { |
| 198 |
rrs := RRset([]RR{ |
| 199 |
func() RR { rr, _ := New("miek.nl. IN NS linode.atoom.net."); return rr }(), |
| 200 |
func() RR { rr, _ := New("miek.nl. IN NS omval.tednet.nl."); return rr }(), |
| 201 |
func() RR { rr, _ := New("miek.nl. IN NS omval.tednet.nl."); return rr }(), |
| 202 |
func() RR { rr, _ := New("miek.nl. IN NS ns-ext.nlnetlabs.nl."); return rr }(), |
| 203 |
}) |
| 204 |
sort.Sort(rrs) |
| 205 |
rrs = slices.CompactFunc(rrs, func(a, b RR) bool { return Equal(a, b) }) |
| 206 |
for i := range rrs { |
| 207 |
fmt.Println(rrs[i]) |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func TestResize(t *testing.T) { |
| 212 |
buf := make([]byte, 10) |
| 213 |
resize(&buf, 9) |
| 214 |
if cap(buf) != 10 { |
| 215 |
t.Fatalf("expected cap to be 10, got %d", cap(buf)) |
| 216 |
} |
| 217 |
resize(&buf, 11) |
| 218 |
if cap(buf) != 11 { |
| 219 |
t.Fatalf("expected cap to be 11, got %d", cap(buf)) |
| 220 |
} |
| 221 |
} |