You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/common/net/ipnet_test.go

44 lines
1.3 KiB

9 years ago
package net_test
import (
"net"
"testing"
8 years ago
"v2ray.com/core/common"
. "v2ray.com/core/common/net"
. "v2ray.com/ext/assert"
9 years ago
)
func parseCIDR(str string) *net.IPNet {
_, ipNet, err := net.ParseCIDR(str)
8 years ago
common.Must(err)
9 years ago
return ipNet
}
func TestIPNet(t *testing.T) {
assert := With(t)
9 years ago
ipNet := NewIPNetTable()
9 years ago
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(ipNet.Contains(ParseIP("192.168.1.1")), IsTrue)
assert(ipNet.Contains(ParseIP("192.0.0.0")), IsTrue)
assert(ipNet.Contains(ParseIP("192.0.1.0")), IsFalse)
assert(ipNet.Contains(ParseIP("0.1.0.0")), IsTrue)
assert(ipNet.Contains(ParseIP("1.0.0.1")), IsFalse)
assert(ipNet.Contains(ParseIP("8.8.8.7")), IsFalse)
assert(ipNet.Contains(ParseIP("8.8.8.8")), IsTrue)
assert(ipNet.Contains(ParseIP("2001:cdba::3257:9652")), IsFalse)
9 years ago
}