63 lines
1.0 KiB
Go
63 lines
1.0 KiB
Go
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")
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|