2023-05-03 02:57:04 +00:00
|
|
|
package iptools
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/netip"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
rangeTestIP1 = netip.AddrFrom4([4]byte{192, 168, 4, 3})
|
|
|
|
rangeTestIP2 = netip.AddrFrom4([4]byte{192, 168, 4, 17})
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasicValidation(t *testing.T) {
|
|
|
|
r1 := &Range{
|
|
|
|
Start: rangeTestIP1,
|
|
|
|
End: rangeTestIP2,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r1.Validate(); err != nil {
|
|
|
|
t.Fatalf("range 1 should be valid: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
r2 := &Range{
|
|
|
|
Start: rangeTestIP2,
|
|
|
|
End: rangeTestIP1,
|
|
|
|
}
|
|
|
|
|
|
|
|
if r2.Validate() == nil {
|
|
|
|
t.Fatal("range 2 should be invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
r3 := &Range{
|
|
|
|
Start: netip.IPv6LinkLocalAllRouters(),
|
|
|
|
End: rangeTestIP1,
|
|
|
|
}
|
|
|
|
|
|
|
|
if r3.Validate() == nil {
|
|
|
|
t.Fatal("range 3 should be invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
r4 := &Range{
|
|
|
|
Start: rangeTestIP2,
|
|
|
|
End: netip.IPv6LinkLocalAllRouters(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if r4.Validate() == nil {
|
|
|
|
t.Fatal("range 4 should be invalid")
|
|
|
|
}
|
|
|
|
}
|
2023-05-06 05:21:27 +00:00
|
|
|
|
|
|
|
func TestNumHosts(t *testing.T) {
|
|
|
|
r := &Range{
|
|
|
|
Start: rangeTestIP1,
|
|
|
|
End: rangeTestIP2,
|
|
|
|
}
|
|
|
|
expected := 15
|
|
|
|
|
|
|
|
numHosts := r.numHosts()
|
|
|
|
if numHosts != expected {
|
|
|
|
t.Fatalf("have %d hosts, expected %d", numHosts, expected)
|
|
|
|
}
|
|
|
|
}
|