|
|
|
@ -90,3 +90,89 @@ func MustParseAddr(s string) netip.Addr {
|
|
|
|
|
} |
|
|
|
|
return addr |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func MustParsePrefix(s string) netip.Prefix { |
|
|
|
|
subnet, err := netip.ParsePrefix(s) |
|
|
|
|
if err != nil { |
|
|
|
|
panic(err) |
|
|
|
|
} |
|
|
|
|
return subnet |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestPrefixToRangeValid(t *testing.T) { |
|
|
|
|
cases := []struct { |
|
|
|
|
subnet netip.Prefix |
|
|
|
|
from netip.Addr |
|
|
|
|
to netip.Addr |
|
|
|
|
}{ |
|
|
|
|
{ |
|
|
|
|
MustParsePrefix("127.0.0.1/32"), |
|
|
|
|
MustParseAddr("127.0.0.1"), |
|
|
|
|
MustParseAddr("127.0.0.1"), |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
MustParsePrefix("127.0.0.1/24"), |
|
|
|
|
MustParseAddr("127.0.0.0"), |
|
|
|
|
MustParseAddr("127.0.0.255"), |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
MustParsePrefix("127.0.1.2/16"), |
|
|
|
|
MustParseAddr("127.0.0.0"), |
|
|
|
|
MustParseAddr("127.0.255.255"), |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
MustParsePrefix("127.1.2.3/8"), |
|
|
|
|
MustParseAddr("127.0.0.0"), |
|
|
|
|
MustParseAddr("127.255.255.255"), |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
MustParsePrefix("10.20.30.40/12"), |
|
|
|
|
MustParseAddr("10.16.0.0"), |
|
|
|
|
MustParseAddr("10.31.255.255"), |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
MustParsePrefix("10.20.30.40/20"), |
|
|
|
|
MustParseAddr("10.20.16.0"), |
|
|
|
|
MustParseAddr("10.20.31.255"), |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
MustParsePrefix("10.0.0.0/12"), |
|
|
|
|
MustParseAddr("10.0.0.0"), |
|
|
|
|
MustParseAddr("10.15.255.255"), |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
MustParsePrefix("10.16.0.0/12"), |
|
|
|
|
MustParseAddr("10.16.0.0"), |
|
|
|
|
MustParseAddr("10.31.255.255"), |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
MustParsePrefix("10.32.0.0/12"), |
|
|
|
|
MustParseAddr("10.32.0.0"), |
|
|
|
|
MustParseAddr("10.47.255.255"), |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
MustParsePrefix("10.48.0.0/12"), |
|
|
|
|
MustParseAddr("10.48.0.0"), |
|
|
|
|
MustParseAddr("10.63.255.255"), |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, tc := range cases { |
|
|
|
|
from, to, ok := PrefixToRange(tc.subnet) |
|
|
|
|
if ok && from.IsValid() && to.IsValid() && |
|
|
|
|
from.Compare(tc.from) == 0 && |
|
|
|
|
to.Compare(tc.to) == 0 { |
|
|
|
|
//
|
|
|
|
|
t.Logf("[%v/%v]: %s → %s - %s", |
|
|
|
|
i, len(cases), |
|
|
|
|
tc.subnet, |
|
|
|
|
from, to) |
|
|
|
|
} else { |
|
|
|
|
t.Errorf("ERROR: [%v/%v]: %q → %s - %s %v (expected %s - %s %v)", |
|
|
|
|
i, len(cases), |
|
|
|
|
tc.subnet, |
|
|
|
|
from, to, ok, |
|
|
|
|
tc.from, tc.to, true) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|