|
|
@ -194,7 +194,7 @@ func TestNoAllocs(t *testing.T) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func TestParseIP(t *testing.T) {
|
|
|
|
func TestParseIPSet(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
tests := []struct {
|
|
|
|
host string
|
|
|
|
host string
|
|
|
|
bits int
|
|
|
|
bits int
|
|
|
@ -203,23 +203,33 @@ func TestParseIP(t *testing.T) {
|
|
|
|
}{
|
|
|
|
}{
|
|
|
|
{"8.8.8.8", 24, pfx("8.8.8.8/24"), ""},
|
|
|
|
{"8.8.8.8", 24, pfx("8.8.8.8/24"), ""},
|
|
|
|
{"2601:1234::", 64, pfx("2601:1234::/64"), ""},
|
|
|
|
{"2601:1234::", 64, pfx("2601:1234::/64"), ""},
|
|
|
|
{"8.8.8.8", 33, nil, `invalid CIDR size 33 for host "8.8.8.8"`},
|
|
|
|
{"8.8.8.8", 33, nil, `invalid CIDR size 33 for IP "8.8.8.8"`},
|
|
|
|
{"8.8.8.8", -1, nil, `invalid CIDR size -1 for host "8.8.8.8"`},
|
|
|
|
{"8.8.8.8", -1, pfx("8.8.8.8/32"), ""},
|
|
|
|
{"2601:1234::", 129, nil, `invalid CIDR size 129 for host "2601:1234::"`},
|
|
|
|
{"8.8.8.8", 32, pfx("8.8.8.8/32"), ""},
|
|
|
|
{"0.0.0.0", 24, nil, `ports="0.0.0.0": to allow all IP addresses, use *:port, not 0.0.0.0:port`},
|
|
|
|
{"8.8.8.8/24", -1, nil, "8.8.8.8/24 contains non-network bits set"},
|
|
|
|
{"::", 64, nil, `ports="::": to allow all IP addresses, use *:port, not [::]:port`},
|
|
|
|
{"8.8.8.0/24", 18, pfx("8.8.8.0/24"), ""}, // the 18 is ignored
|
|
|
|
|
|
|
|
{"1.0.0.0-1.255.255.255", 5, pfx("1.0.0.0/8"), ""},
|
|
|
|
|
|
|
|
{"1.0.0.0-2.1.2.3", 5, pfx("1.0.0.0/8", "2.0.0.0/16", "2.1.0.0/23", "2.1.2.0/30"), ""},
|
|
|
|
|
|
|
|
{"1.0.0.2-1.0.0.1", -1, nil, "invalid IP range \"1.0.0.2-1.0.0.1\""},
|
|
|
|
|
|
|
|
{"2601:1234::", 129, nil, `invalid CIDR size 129 for IP "2601:1234::"`},
|
|
|
|
|
|
|
|
{"0.0.0.0", 24, pfx("0.0.0.0/24"), ""},
|
|
|
|
|
|
|
|
{"::", 64, pfx("::/64"), ""},
|
|
|
|
{"*", 24, pfx("0.0.0.0/0", "::/0"), ""},
|
|
|
|
{"*", 24, pfx("0.0.0.0/0", "::/0"), ""},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
for _, tt := range tests {
|
|
|
|
got, err := parseIP(tt.host, tt.bits)
|
|
|
|
var bits *int
|
|
|
|
|
|
|
|
if tt.bits != -1 {
|
|
|
|
|
|
|
|
bits = &tt.bits
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
got, err := parseIPSet(tt.host, bits)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
if err.Error() == tt.wantErr {
|
|
|
|
if err.Error() == tt.wantErr {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Errorf("parseIP(%q, %v) error: %v; want error %q", tt.host, tt.bits, err, tt.wantErr)
|
|
|
|
t.Errorf("parseIPSet(%q, %v) error: %v; want error %q", tt.host, tt.bits, err, tt.wantErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(got, tt.want, cmp.Comparer(func(a, b netaddr.IP) bool { return a == b })); diff != "" {
|
|
|
|
if diff := cmp.Diff(got, tt.want, cmp.Comparer(func(a, b netaddr.IP) bool { return a == b })); diff != "" {
|
|
|
|
t.Errorf("parseIP(%q, %v) = %s; want %s", tt.host, tt.bits, got, tt.want)
|
|
|
|
t.Errorf("parseIPSet(%q, %v) = %s; want %s", tt.host, tt.bits, got, tt.want)
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|