Cleanup the tests and add some doc string

This commit is contained in:
Miek Gieben 2015-08-25 08:39:43 +01:00
parent 3daa798e14
commit 4bca364480
2 changed files with 36 additions and 52 deletions

View File

@ -2,7 +2,11 @@ package dns
// Dedup removes identical RRs from rrs. It preserves the original ordering.
// The lowest TTL of any duplicates is used in the remaining one.
// TODO(miek): CNAME, DNAME 'n stuff.
//
// TODO(miek): This function will be extended to also look for CNAMEs and DNAMEs.
// if found, it will prune rrs from the "other data" that can exist. Example:
// if it finds a: a.miek.nl. CNAME foo, all other RRs with the ownername a.miek.nl.
// will be removed.
func Dedup(rrs []RR) []RR {
m := make(map[string]RR)
keys := make([]string, 0, len(rrs))

View File

@ -3,59 +3,39 @@ package dns
import "testing"
func TestDedup(t *testing.T) {
in := []RR{
newRR(t, "miek.nl. IN A 127.0.0.1"),
newRR(t, "miek.nl. IN A 127.0.0.1"),
}
out := Dedup(in)
if len(out) != 1 && out[0].String() != "miek.nl. IN A 127.0.0.1" {
dump(out, t)
t.Errorf("dedup failed, expected %d, got %d", 1, len(out))
testcases := map[[3]RR]string{
[...]RR{
newRR(t, "mIek.nl. IN A 127.0.0.1"),
newRR(t, "mieK.nl. IN A 127.0.0.1"),
newRR(t, "miek.Nl. IN A 127.0.0.1"),
}: "mIek.nl.\t3600\tIN\tA\t127.0.0.1",
[...]RR{
newRR(t, "miEk.nl. 2000 IN A 127.0.0.1"),
newRR(t, "mieK.Nl. 1000 IN A 127.0.0.1"),
newRR(t, "Miek.nL. 500 IN A 127.0.0.1"),
}: "miEk.nl.\t500\tIN\tA\t127.0.0.1",
[...]RR{
newRR(t, "miek.nl. IN A 127.0.0.1"),
newRR(t, "miek.nl. CH A 127.0.0.1"),
newRR(t, "miek.nl. IN A 127.0.0.1"),
}: "miek.nl.\t3600\tIN\tA\t127.0.0.1",
[...]RR{
newRR(t, "miek.nl. CH A 127.0.0.1"),
newRR(t, "miek.nl. IN A 127.0.0.1"),
newRR(t, "miek.nl. IN A 127.0.0.1"),
}: "miek.nl.\t3600\tCH\tA\t127.0.0.1",
}
in = []RR{}
out = Dedup(in)
if len(out) != 0 {
dump(out, t)
t.Errorf("dedup failed, expected %d, got %d", 0, len(out))
}
in = []RR{
newRR(t, "miEk.nl. 2000 IN A 127.0.0.1"),
newRR(t, "mieK.Nl. 1000 IN A 127.0.0.1"),
}
out = Dedup(in)
if len(out) != 1 {
dump(out, t)
t.Errorf("dedup failed, expected %d, got %d", 2, len(out))
}
in = []RR{
newRR(t, "miek.nl. IN A 127.0.0.1"),
newRR(t, "miek.nl. CH A 127.0.0.1"),
}
out = Dedup(in)
if len(out) != 2 {
dump(out, t)
t.Errorf("dedup failed, expected %d, got %d", 2, len(out))
}
in = []RR{
newRR(t, "miek.nl. CH A 127.0.0.1"),
newRR(t, "miek.nl. IN A 127.0.0.1"),
newRR(t, "mIek.Nl. IN A 127.0.0.1"),
}
out = Dedup(in)
if len(out) != 2 {
// TODO(miek): check ordering.
dump(out, t)
t.Errorf("dedup failed, expected %d, got %d", 2, len(out))
}
}
func dump(rrs []RR, t *testing.T) {
t.Logf("********\n")
for _, r := range rrs {
t.Logf("%v\n", r)
for rr, expected := range testcases {
out := Dedup([]RR{rr[0], rr[1], rr[2]})
if len(out) == 0 || len(out) == 3 {
t.Logf("dedup failed, wrong number of RRs returned")
t.Fail()
}
if o := out[0].String(); o != expected {
t.Logf("dedup failed, expected %s, got %s", expected, o)
t.Fail()
}
}
}