27 lines
926 B
Go
27 lines
926 B
Go
package db
|
|||
|
|
|
||
|
|
import "testing"
|
||
|
|
|
||
|
|
func TestSubnetMatcher(t *testing.T) {
|
||
|
|
nested := []Subnet{{CIDR: "10.0.0.0/8", Label: "wide"}, {CIDR: "10.1.0.0/16", Label: "narrow"}, {CIDR: "2001:db8::/32"}}
|
||
|
|
for _, c := range []struct {
|
||
|
|
name string
|
||
|
|
list []Subnet
|
||
|
|
ip string
|
||
|
|
cidr string
|
||
|
|
label string
|
||
|
|
}{
|
||
|
|
{"most specific", nested, "10.1.2.3", "10.1.0.0/16", "narrow"},
|
||
|
|
{"outer only", nested, "10.2.0.1", "10.0.0.0/8", "wide"},
|
||
|
|
{"outside", nested, "192.0.2.1", SubnetOutside, ""},
|
||
|
|
{"garbage", nested, "garbage", SubnetOutside, ""},
|
||
|
|
{"ipv6 in list", nested, "2001:db8::1", "2001:db8::/32", ""},
|
||
|
|
{"empty list v4", nil, "203.0.113.77", "203.0.113.0/24", ""},
|
||
|
|
{"empty list v6", nil, "2001:db8:1:2:3::1", "2001:db8:1:2::/64", ""},
|
||
|
|
} {
|
||
|
|
cidr, label := NewSubnetMatcher(c.list).Lookup(c.ip)
|
||
|
|
if cidr != c.cidr || label != c.label {
|
||
|
|
t.Errorf("%s: got %q/%q, want %q/%q", c.name, cidr, label, c.cidr, c.label)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|