2016-09-19 11:26:00 +01:00
|
|
|
package dnsserver
|
|
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
|
|
|
|
|
func TestNormalizeZone(t *testing.T) {
|
|
|
|
|
for i, test := range []struct {
|
|
|
|
|
input string
|
|
|
|
|
expected string
|
|
|
|
|
shouldErr bool
|
|
|
|
|
}{
|
2017-03-13 20:24:37 +00:00
|
|
|
{".", "dns://.:53", false},
|
|
|
|
|
{".:54", "dns://.:54", false},
|
|
|
|
|
{"..", "://:", true},
|
|
|
|
|
{"..", "://:", true},
|
2017-08-07 13:24:09 -07:00
|
|
|
{".:", "://:", true},
|
|
|
|
|
} {
|
|
|
|
|
addr, err := normalizeZone(test.input)
|
|
|
|
|
actual := addr.String()
|
|
|
|
|
if test.shouldErr && err == nil {
|
|
|
|
|
t.Errorf("Test %d: Expected error, but there wasn't any", i)
|
|
|
|
|
}
|
|
|
|
|
if !test.shouldErr && err != nil {
|
|
|
|
|
t.Errorf("Test %d: Expected no error, but there was one: %v", i, err)
|
|
|
|
|
}
|
|
|
|
|
if actual != test.expected {
|
|
|
|
|
t.Errorf("Test %d: Expected %s but got %s", i, test.expected, actual)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNormalizeZoneReverse(t *testing.T) {
|
|
|
|
|
for i, test := range []struct {
|
|
|
|
|
input string
|
|
|
|
|
expected string
|
|
|
|
|
shouldErr bool
|
|
|
|
|
}{
|
2018-01-23 10:58:36 -05:00
|
|
|
{"2003::1/64", "dns://0.0.0.0.0.0.0.0.0.0.0.0.3.0.0.2.ip6.arpa.:53", false},
|
2017-08-07 13:24:09 -07:00
|
|
|
{"2003::1/64.", "dns://2003::1/64.:53", false}, // OK, with closing dot the parse will fail.
|
2018-01-23 10:58:36 -05:00
|
|
|
{"2003::1/64:53", "dns://0.0.0.0.0.0.0.0.0.0.0.0.3.0.0.2.ip6.arpa.:53", false},
|
2017-08-07 13:24:09 -07:00
|
|
|
{"2003::1/64.:53", "dns://2003::1/64.:53", false},
|
|
|
|
|
|
|
|
|
|
{"10.0.0.0/24", "dns://0.0.10.in-addr.arpa.:53", false},
|
|
|
|
|
{"10.0.0.0/24.", "dns://10.0.0.0/24.:53", false},
|
|
|
|
|
{"10.0.0.0/24:53", "dns://0.0.10.in-addr.arpa.:53", false},
|
|
|
|
|
{"10.0.0.0/24.:53", "dns://10.0.0.0/24.:53", false},
|
|
|
|
|
|
|
|
|
|
// non %8==0 netmasks
|
2018-01-23 10:58:36 -05:00
|
|
|
{"2003::53/67", "dns://0.0.0.0.0.0.0.0.0.0.0.0.0.3.0.0.2.ip6.arpa.:53", false},
|
2017-10-24 10:16:03 +01:00
|
|
|
{"10.0.0.0/25.", "dns://10.0.0.0/25.:53", false}, // has dot
|
|
|
|
|
{"10.0.0.0/25", "dns://0.0.10.in-addr.arpa.:53", false},
|
2018-01-23 10:58:36 -05:00
|
|
|
{"fd00:77:30::0/110", "dns://0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.3.0.0.7.7.0.0.0.0.d.f.ip6.arpa.:53", false},
|
2016-09-19 11:26:00 +01:00
|
|
|
} {
|
|
|
|
|
addr, err := normalizeZone(test.input)
|
|
|
|
|
actual := addr.String()
|
|
|
|
|
if test.shouldErr && err == nil {
|
|
|
|
|
t.Errorf("Test %d: Expected error, but there wasn't any", i)
|
|
|
|
|
}
|
|
|
|
|
if !test.shouldErr && err != nil {
|
|
|
|
|
t.Errorf("Test %d: Expected no error, but there was one: %v", i, err)
|
|
|
|
|
}
|
|
|
|
|
if actual != test.expected {
|
|
|
|
|
t.Errorf("Test %d: Expected %s but got %s", i, test.expected, actual)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-14 14:19:32 -05:00
|
|
|
|
|
|
|
|
func TestSplitProtocolHostPort(t *testing.T) {
|
|
|
|
|
for i, test := range []struct {
|
|
|
|
|
input string
|
|
|
|
|
proto string
|
|
|
|
|
ip string
|
|
|
|
|
port string
|
|
|
|
|
shouldErr bool
|
|
|
|
|
}{
|
|
|
|
|
{"dns://:53", "dns", "", "53", false},
|
|
|
|
|
{"dns://127.0.0.1:4005", "dns", "127.0.0.1", "4005", false},
|
|
|
|
|
{"[ffe0:34ab:1]:4005", "", "ffe0:34ab:1", "4005", false},
|
|
|
|
|
|
|
|
|
|
// port part is mandatory
|
|
|
|
|
{"dns://", "dns", "", "", true},
|
|
|
|
|
{"dns://127.0.0.1", "dns", "127.0.0.1", "", true},
|
|
|
|
|
// cannot be empty
|
|
|
|
|
{"", "", "", "", true},
|
|
|
|
|
// invalid format with twice ://
|
|
|
|
|
{"dns://127.0.0.1://53", "", "", "", true},
|
|
|
|
|
} {
|
|
|
|
|
proto, ip, port, err := SplitProtocolHostPort(test.input)
|
|
|
|
|
if test.shouldErr && err == nil {
|
|
|
|
|
t.Errorf("Test %d: (address = %s) expected error, but there wasn't any", i, test.input)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if !test.shouldErr && err != nil {
|
|
|
|
|
t.Errorf("Test %d: (address = %s) expected no error, but there was one: %v", i, test.input, err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err == nil || test.shouldErr {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if proto != test.proto {
|
|
|
|
|
t.Errorf("Test %d: (address = %s) expected protocol with value %s but got %s", i, test.input, test.proto, proto)
|
|
|
|
|
}
|
|
|
|
|
if ip != test.ip {
|
|
|
|
|
t.Errorf("Test %d: (address = %s) expected ip with value %s but got %s", i, test.input, test.ip, ip)
|
|
|
|
|
}
|
|
|
|
|
if port != test.port {
|
|
|
|
|
t.Errorf("Test %d: (address = %s) expected port with value %s but got %s", i, test.input, test.port, port)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|