Skip to content

Commit

Permalink
tests for CopyIP() and RandomIP()
Browse files Browse the repository at this point in the history
  • Loading branch information
c-robinson committed Jan 2, 2023
1 parent f9d8083 commit 85813ef
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
12 changes: 12 additions & 0 deletions iplib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ package iplib
import (
"math/big"
"net"
"reflect"
"sort"
"testing"
)

func TestCopyIP(t *testing.T) {
ipa := net.ParseIP("192.168.23.5")
ipb := CopyIP(ipa)
if reflect.ValueOf(ipa).Pointer() == reflect.ValueOf(ipb).Pointer() {
t.Errorf("CopyIP() failed to copy (copied IP shares pointer with original)!")
}
if CompareIPs(ipa, ipb) != 0 {
t.Errorf("CopyIP() failed to copy (value of copied IP does not match original)!")
}
}

var IPTests = []struct {
ipaddr net.IP
next net.IP
Expand Down
10 changes: 9 additions & 1 deletion net4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,17 @@ func TestNet4_ContainsNet(t *testing.T) {
}
}

func TestNet4_RandomIP(t *testing.T) {
for i, tt := range containsNet4Tests {
rip := tt.ipn1.RandomIP()
if !tt.ipn1.Contains(rip) {
t.Errorf("[%d] address %s not in %s", i, rip, tt.ipn1)
}
}
}

func TestNet4_Is4in6(t *testing.T) {
nf := Net4FromStr("192.168.0.0./16")
//nf := NewNet4(ForceIP4(net.ParseIP("192.168.0.0")), 16)
if nf.Is4in6() != false {
t.Errorf("should be false for '192.168.0.0/16'")
}
Expand Down
10 changes: 10 additions & 0 deletions net6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,16 @@ func TestNet6_ContainsNet(t *testing.T) {
}
}

func TestNet6_RandomIP(t *testing.T) {
for i, tt := range containsNet6Tests {
_, ipn, _ := ParseCIDR(tt.netblock1)
rip := ipn.(Net6).RandomIP()
if !ipn.Contains(rip) {
t.Errorf("[%d] address %s not in %s", i, rip, ipn)
}
}
}

var controlsTests = []struct {
ipn Net6
addrs map[string]bool
Expand Down

0 comments on commit 85813ef

Please sign in to comment.