package ip import ( "net" "testing" ) func TestIPInCidr(t *testing.T) { ip := NewIP() HandleIP(t, ip, "123.2.3.5", false) HandleIP(t, ip, "10.10.0.0", true) HandleIP(t, ip, "10.10.10.10", true) HandleIP(t, ip, "10.255.255.255", true) HandleIP(t, ip, "11.0.0.0", false) HandleIP(t, ip, "10.10.10.432", false) HandleIP(t, ip, "127.0.0.1", true) HandleIP(t, ip, "127.255.255.255", true) HandleIP(t, ip, "128.0.0.1", false) HandleIP(t, ip, "126.255.255.255", false) HandleIP(t, ip, "192.167.255.255", false) HandleIP(t, ip, "192.168.0.0", true) HandleIP(t, ip, "192.168.122.1", true) HandleIP(t, ip, "192.168.255.255", true) HandleIP(t, ip, "192.169.0.0", false) HandleIP(t, ip, "172.15.255.255", false) HandleIP(t, ip, "172.16.0.0", true) HandleIP(t, ip, "172.16.1.7", true) HandleIP(t, ip, "172.31.255.255", true) HandleIP(t, ip, "172.15.255.255", false) HandleIP(t, ip, "fcff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", false) HandleIP(t, ip, "fd00::1", true) HandleIP(t, ip, "fd02::1", true) HandleIP(t, ip, "fdff::1", true) HandleIP(t, ip, "fe00::1", false) } func TestNetwork(t *testing.T) { ip := NewIP() HandleNetwork(t, ip, "127.0.0.1", "127.0.0.0/8", true) HandleNetwork(t, ip, "127.244.0.1", "127.0.0.1/8", true) HandleNetwork(t, ip, "128.244.0.1", "127.0.0.1/8", false) HandleNetwork(t, ip, "10.145.32.11", "10.0.0.0/8", true) HandleNetwork(t, ip, "172.16.0.1", "172.16.0.0/12", true) HandleNetwork(t, ip, "192.168.21.44", "192.168.0.0/16", true) } func HandleNetwork(t *testing.T, privIP *IP, ip string, n string, assert bool) { _, network, _ := net.ParseCIDR(n) addr := net.ParseIP(ip) res := privIP.Network(addr) chk := (res != nil && res.String() == network.String()) if chk != assert { t.Errorf("Assertion (have: %t, should be %t) failed on IP %s", chk, assert, ip) } } func HandleIP(t *testing.T, ip *IP, addr string, assert bool) { res := ip.IsPrivate(net.ParseIP(addr)) if res != assert { t.Errorf("Assertion (have: %t, should be %t) failed on IP %s", res, assert, addr) } }