1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-18 10:26:01 -05:00
v2fly/common/net/ipnet_test.go

45 lines
1.3 KiB
Go
Raw Normal View History

2015-12-08 11:31:31 -05:00
package net_test
import (
"net"
"testing"
2016-08-20 14:55:45 -04:00
. "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
2015-12-08 11:31:31 -05:00
)
func parseCIDR(str string) *net.IPNet {
_, ipNet, err := net.ParseCIDR(str)
2016-05-24 15:55:46 -04:00
if err != nil {
panic(err)
}
2015-12-08 11:31:31 -05:00
return ipNet
}
func TestIPNet(t *testing.T) {
2016-05-24 15:55:46 -04:00
assert := assert.On(t)
2015-12-08 11:31:31 -05:00
ipNet := NewIPNet()
ipNet.Add(parseCIDR(("0.0.0.0/8")))
ipNet.Add(parseCIDR(("10.0.0.0/8")))
ipNet.Add(parseCIDR(("100.64.0.0/10")))
ipNet.Add(parseCIDR(("127.0.0.0/8")))
ipNet.Add(parseCIDR(("169.254.0.0/16")))
ipNet.Add(parseCIDR(("172.16.0.0/12")))
ipNet.Add(parseCIDR(("192.0.0.0/24")))
ipNet.Add(parseCIDR(("192.0.2.0/24")))
ipNet.Add(parseCIDR(("192.168.0.0/16")))
ipNet.Add(parseCIDR(("198.18.0.0/15")))
ipNet.Add(parseCIDR(("198.51.100.0/24")))
ipNet.Add(parseCIDR(("203.0.113.0/24")))
ipNet.Add(parseCIDR(("8.8.8.8/32")))
assert.Bool(ipNet.Contains(net.ParseIP("192.168.1.1"))).IsTrue()
assert.Bool(ipNet.Contains(net.ParseIP("192.0.0.0"))).IsTrue()
assert.Bool(ipNet.Contains(net.ParseIP("192.0.1.0"))).IsFalse()
assert.Bool(ipNet.Contains(net.ParseIP("0.1.0.0"))).IsTrue()
assert.Bool(ipNet.Contains(net.ParseIP("1.0.0.1"))).IsFalse()
assert.Bool(ipNet.Contains(net.ParseIP("8.8.8.7"))).IsFalse()
assert.Bool(ipNet.Contains(net.ParseIP("8.8.8.8"))).IsTrue()
2016-02-04 15:23:51 -05:00
assert.Bool(ipNet.Contains(net.ParseIP("2001:cdba::3257:9652"))).IsFalse()
2015-12-08 11:31:31 -05:00
}