112 lines
3 KiB
Go
112 lines
3 KiB
Go
package checker
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
func TestSerialLess(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
a, b uint32
|
|
want bool
|
|
}{
|
|
{"equal", 100, 100, false},
|
|
{"a<b small", 100, 200, true},
|
|
{"a>b small", 200, 100, false},
|
|
{"wrap b ahead", 0xFFFFFFFE, 1, true},
|
|
{"wrap a ahead", 1, 0xFFFFFFFE, false},
|
|
{"zero<one", 0, 1, true},
|
|
{"max distance same direction", 0, 1<<31 - 1, true},
|
|
{"max distance other direction", 1<<31 - 1, 0, false},
|
|
{"undefined boundary equal-half", 0, 1 << 31, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := serialLess(tt.a, tt.b); got != tt.want {
|
|
t.Errorf("serialLess(%d,%d) = %v, want %v", tt.a, tt.b, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestUnionStrings(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
a, b []string
|
|
wantOut []string
|
|
}{
|
|
{"both empty", nil, nil, nil},
|
|
{"only a", []string{"x", "a"}, nil, []string{"a", "x"}},
|
|
{"only b", nil, []string{"b", "a"}, []string{"a", "b"}},
|
|
{"overlap", []string{"a", "b"}, []string{"b", "c"}, []string{"a", "b", "c"}},
|
|
{"duplicates within a", []string{"a", "a"}, []string{"b"}, []string{"a", "b"}},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := unionStrings(tt.a, tt.b)
|
|
if len(got) == 0 && len(tt.wantOut) == 0 {
|
|
return
|
|
}
|
|
if !reflect.DeepEqual(got, tt.wantOut) {
|
|
t.Errorf("got %v, want %v", got, tt.wantOut)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDiffStringSets(t *testing.T) {
|
|
want := []string{"ns1.example.com.", "ns2.example.com.", "NS3.Example.com"}
|
|
got := []string{"ns2.example.com", "ns3.example.com.", "ns4.example.com"}
|
|
missing, extra := diffStringSets(want, got)
|
|
if !reflect.DeepEqual(missing, []string{"ns1.example.com"}) {
|
|
t.Errorf("missing = %v, want [ns1.example.com]", missing)
|
|
}
|
|
if !reflect.DeepEqual(extra, []string{"ns4.example.com"}) {
|
|
t.Errorf("extra = %v, want [ns4.example.com]", extra)
|
|
}
|
|
}
|
|
|
|
func TestDiffStringSets_Equal(t *testing.T) {
|
|
missing, extra := diffStringSets(
|
|
[]string{"a.example.", "b.example."},
|
|
[]string{"A.example", "b.EXAMPLE."},
|
|
)
|
|
if len(missing) != 0 || len(extra) != 0 {
|
|
t.Errorf("equal sets should produce no diff, got missing=%v extra=%v", missing, extra)
|
|
}
|
|
}
|
|
|
|
func TestNormalizeNSList(t *testing.T) {
|
|
in := []*dns.NS{
|
|
{Ns: "NS2.Example.COM"},
|
|
nil,
|
|
{Ns: "ns1.example.com."},
|
|
{Ns: "NS1.example.com"},
|
|
}
|
|
got := normalizeNSList(in)
|
|
want := []string{"ns1.example.com.", "ns1.example.com.", "ns2.example.com."}
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Errorf("got %v, want %v", got, want)
|
|
}
|
|
}
|
|
|
|
func TestHostPort(t *testing.T) {
|
|
tests := []struct {
|
|
host, port, want string
|
|
}{
|
|
{"192.0.2.1", "53", "192.0.2.1:53"},
|
|
{"2001:db8::1", "53", "[2001:db8::1]:53"},
|
|
{"ns.example.com.", "53", "ns.example.com:53"},
|
|
{"ns.example.com", "5353", "ns.example.com:5353"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.host, func(t *testing.T) {
|
|
if got := hostPort(tt.host, tt.port); got != tt.want {
|
|
t.Errorf("hostPort(%q,%q) = %q, want %q", tt.host, tt.port, got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|