Example #1
0
func TestOverlaps(t *testing.T) {
	for i, tt := range overlapsTests {
		p1, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen)
		if err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		}
		var nn []*net.IPNet
		for _, s := range tt.others {
			_, n, err := net.ParseCIDR(s)
			if err != nil {
				t.Fatalf("net.ParseCIDR failed: %v", err)
			}
			nn = append(nn, n)
		}
		var others []ipaddr.Prefix
		for _, n := range nn {
			prefixLen, _ := n.Mask.Size()
			p, err := ipaddr.NewPrefix(n.IP, prefixLen)
			if err != nil {
				t.Fatalf("ipaddr.NewPrefix failed: %v", err)
			}
			others = append(others, p)
		}
		p2 := ipaddr.SummaryPrefix(others)
		if ok := p1.Overlaps(p2); ok != tt.ok {
			t.Fatalf("#%v: got %v; expected %v", i, ok, tt.ok)
		}
		if ok := p2.Overlaps(p1); ok != tt.ok {
			t.Fatalf("#%v: got %v; expected %v", i, ok, tt.ok)
		}
	}
}
Example #2
0
func BenchmarkIPv4Exclude(b *testing.B) {
	p, _ := ipaddr.NewPrefix(net.ParseIP("10.1.0.0"), 16)
	p1, _ := ipaddr.NewPrefix(net.ParseIP("10.1.1.1"), 32)
	for i := 0; i < b.N; i++ {
		p.Exclude(p1)
	}
}
Example #3
0
func BenchmarkIPv4ComparePrefix(b *testing.B) {
	p1, _ := ipaddr.NewPrefix(net.ParseIP("192.168.1.0"), 24)
	p2, _ := ipaddr.NewPrefix(net.ParseIP("192.168.2.0"), 24)
	for i := 0; i < b.N; i++ {
		ipaddr.ComparePrefix(p1, p2)
	}
}
Example #4
0
func BenchmarkIPv6Exclude(b *testing.B) {
	p, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8::"), 64)
	p1, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8::1:1:1:1"), 128)
	for i := 0; i < b.N; i++ {
		p.Exclude(p1)
	}
}
Example #5
0
func BenchmarkIPv4Equal(b *testing.B) {
	p1, _ := ipaddr.NewPrefix(net.ParseIP("192.168.1.0"), 24)
	p2, _ := ipaddr.NewPrefix(net.ParseIP("192.168.2.0"), 24)
	for i := 0; i < b.N; i++ {
		p1.Equal(p2)
	}
}
Example #6
0
func BenchmarkIPv6ComparePrefix(b *testing.B) {
	p1, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8:f001:f002::"), 64)
	p2, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8:f001:f003::"), 64)
	for i := 0; i < b.N; i++ {
		ipaddr.ComparePrefix(p1, p2)
	}
}
Example #7
0
func BenchmarkIPv6Equal(b *testing.B) {
	p1, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8:f001:f002::"), 64)
	p2, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8:f001:f003::"), 64)
	for i := 0; i < b.N; i++ {
		p1.Equal(p2)
	}
}
Example #8
0
func TestExclude(t *testing.T) {
	for i, tt := range excludeTests {
		p, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen)
		if err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		}
		excl, err := ipaddr.NewPrefix(tt.exclAddr, tt.exclPrefixLen)
		if err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		}
		subs := p.Exclude(excl)
		if len(subs) != tt.exclPrefixLen-tt.prefixLen {
			for _, s := range subs {
				t.Logf("subnet: %v", s)
			}
			t.Fatalf("#%v: got %v; expected %v", i, len(subs), tt.exclPrefixLen-tt.prefixLen)
		}
		diff, sum := big.NewInt(0), big.NewInt(0)
		diff.Sub(p.NumAddr(), excl.NumAddr())
		for _, p := range subs {
			sum.Add(sum, p.NumAddr())
		}
		if diff.String() != sum.String() {
			for _, s := range subs {
				t.Logf("subnet: %v", s)
			}
			t.Fatalf("#%v: got %v; expected %v", i, sum.String(), diff.String())
		}
	}
}
Example #9
0
// Overlaps returns a bool stating whether or not the entry
// overlaps with another CIDR block.
func (e Entry) Overlaps(cidr *net.IPNet) bool {
	xMaskSize, _ := e.CIDR.Mask.Size()
	x, _ := ipaddr.NewPrefix(e.CIDR.IP, xMaskSize)
	yMaskSize, _ := cidr.Mask.Size()
	y, _ := ipaddr.NewPrefix(cidr.IP, yMaskSize)
	return x.Overlaps(y)
}
Example #10
0
func makePrefix(network string) (*ipaddr.Prefix, error) {
	_, ipn, err := net.ParseCIDR(network)
	if err != nil {
		return nil, err
	}
	return ipaddr.NewPrefix(ipn), nil
}
Example #11
0
func TestSummaryPrefix(t *testing.T) {
	for i, tt := range summaryPrefixTests {
		var nn []*net.IPNet
		for _, s := range tt.subs {
			_, n, err := net.ParseCIDR(s)
			if err != nil {
				t.Fatalf("net.ParseCIDR failed: %v", err)
			}
			nn = append(nn, n)
		}
		var subs []ipaddr.Prefix
		for _, n := range nn {
			prefixLen, _ := n.Mask.Size()
			p, err := ipaddr.NewPrefix(n.IP, prefixLen)
			if err != nil {
				t.Fatalf("ipaddr.NewPrefix failed: %v", err)
			}
			subs = append(subs, p)
		}
		sum := ipaddr.SummaryPrefix(subs)
		if sum == nil && tt.ok || sum != nil && !tt.ok {
			t.Fatalf("#%v: got %v, %v; expected %v", i, sum, sum != nil, tt.ok)
		}
		if tt.ok {
			for _, s := range subs {
				if !sum.Contains(s.Addr()) {
					t.Fatalf("#%v: %v does not contain %v", i, sum, s)
				}
			}
		}
	}
}
Example #12
0
func ExamplePrefix_subnettingAndAggregation() {
	_, n, err := net.ParseCIDR("192.0.2.0/24")
	if err != nil {
		log.Fatal(err)
	}
	p := ipaddr.NewPrefix(n)
	fmt.Println(p.IP, p.Last(), p.Len(), p.Mask, p.Hostmask())
	fmt.Println()
	ps := p.Subnets(3)
	for _, p := range ps {
		fmt.Println(p)
	}
	fmt.Println()
	fmt.Println(ipaddr.Aggregate(ps))
	fmt.Println(ipaddr.Aggregate(ps[1:7]))
	fmt.Println(ipaddr.Aggregate(ps[:4]))
	fmt.Println(ipaddr.Aggregate(ps[4:8]))
	// Output:
	// 192.0.2.0 192.0.2.255 24 ffffff00 000000ff
	//
	// 192.0.2.0/27
	// 192.0.2.32/27
	// 192.0.2.64/27
	// 192.0.2.96/27
	// 192.0.2.128/27
	// 192.0.2.160/27
	// 192.0.2.192/27
	// 192.0.2.224/27
	//
	// [192.0.2.0/24]
	// [192.0.2.32/27 192.0.2.64/26 192.0.2.64/27 192.0.2.192/27]
	// [192.0.2.0/25]
	// [192.0.2.128/25]
}
Example #13
0
func ExamplePrefix_subnettingAndSupernetting() {
	_, n, err := net.ParseCIDR("203.0.113.0/24")
	if err != nil {
		log.Fatal(err)
	}
	p := ipaddr.NewPrefix(n)
	fmt.Println(p.IP, p.Last(), p.Len(), p.Mask, p.Hostmask())
	fmt.Println()
	ps := p.Subnets(3)
	for _, p := range ps {
		fmt.Println(p)
	}
	fmt.Println()
	fmt.Println(ipaddr.Supernet(ps))
	fmt.Println(ipaddr.Supernet(ps[1:7]))
	fmt.Println(ipaddr.Supernet(ps[:4]))
	fmt.Println(ipaddr.Supernet(ps[4:8]))
	// Output:
	// 203.0.113.0 203.0.113.255 24 ffffff00 000000ff
	//
	// 203.0.113.0/27
	// 203.0.113.32/27
	// 203.0.113.64/27
	// 203.0.113.96/27
	// 203.0.113.128/27
	// 203.0.113.160/27
	// 203.0.113.192/27
	// 203.0.113.224/27
	//
	// 203.0.113.0/24
	// 203.0.113.0/24
	// 203.0.113.0/25
	// 203.0.113.128/25
}
Example #14
0
func BenchmarkIPv4Contains(b *testing.B) {
	p, _ := ipaddr.NewPrefix(net.ParseIP("192.168.255.0"), 24)
	ip := net.ParseIP("192.168.255.255")
	for i := 0; i < b.N; i++ {
		p.Contains(ip)
	}
}
Example #15
0
func BenchmarkIPv6Contains(b *testing.B) {
	p, _ := ipaddr.NewPrefix(net.ParseIP("2001:db8:f001:f002::"), 64)
	ip := net.ParseIP("2001:db8:f001:f002::1")
	for i := 0; i < b.N; i++ {
		p.Contains(ip)
	}
}
Example #16
0
func ExamplePrefix_subnettingAndAggregation() {
	_, n, err := net.ParseCIDR("192.168.0.0/24")
	if err != nil {
		log.Fatal(err)
	}
	p := ipaddr.NewPrefix(n)
	fmt.Println(p.IP, p.Last(), p.Len(), p.Mask, p.Hostmask())
	ps := p.Subnets(2)
	for _, p := range ps {
		fmt.Println(p)
	}
	_, n, err = net.ParseCIDR("192.168.100.0/24")
	if err != nil {
		log.Fatal(err)
	}
	ps = append(ps, *ipaddr.NewPrefix(n))
	fmt.Println(ipaddr.Aggregate(ps[2:]))
	// Output:
	// 192.168.0.0 192.168.0.255 24 ffffff00 000000ff
	// 192.168.0.0/26
	// 192.168.0.64/26
	// 192.168.0.128/26
	// 192.168.0.192/26
	// [192.168.0.128/25 192.168.100.0/24]
}
Example #17
0
func makePrefix(network string) (ipaddr.Prefix, error) {
	_, ipn, err := net.ParseCIDR(network)
	if err != nil {
		return nil, err
	}
	nbits, _ := ipn.Mask.Size()
	return ipaddr.NewPrefix(ipn.IP, nbits)
}
Example #18
0
func TestNumAddr(t *testing.T) {
	for _, tt := range numAddrTests {
		if p, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen); err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		} else if p.NumAddr().String() != tt.naddrs.String() {
			t.Fatalf("%v: got %v; expected %v", p, p.NumAddr().String(), tt.naddrs.String())
		}
	}
}
Example #19
0
func TestContains(t *testing.T) {
	for i, tt := range containsTests {
		if p, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen); err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		} else if ok := p.Contains(tt.sub); ok != tt.ok {
			t.Fatalf("#%v: got %v; expected %v", i, ok, tt.ok)
		}
	}
}
Example #20
0
func TestBits(t *testing.T) {
	for i, tt := range bitsTests {
		if p, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen); err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		} else if bits := p.Bits(tt.pos, tt.nbits); bits != tt.bits {
			t.Fatalf("#%v: got %x; expected %x", i, bits, tt.bits)
		}
	}
}
Example #21
0
func toPrefix(s string) *ipaddr.Prefix {
	if s == "" {
		return nil
	}
	_, n, err := net.ParseCIDR(s)
	if err != nil {
		return nil
	}
	return ipaddr.NewPrefix(n)
}
Example #22
0
func TestHosts(t *testing.T) {
	for i, tt := range hostsTests {
		p, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen)
		if err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		}
		if hosts := p.Hosts(tt.begin); len(hosts) != tt.nhosts {
			t.Errorf("#%v: got %v; expected %v", i, len(hosts), tt.nhosts)
		}
	}
}
Example #23
0
func TestHostIter(t *testing.T) {
	for i, tt := range hostIterTests {
		p, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen)
		if err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		}
		if next := <-p.HostIter(tt.first); next == nil && tt.next != nil {
			t.Errorf("#%v: got %v; expected %v", i, next, tt.next)
		} else if !next.Equal(tt.next) {
			t.Errorf("#%v: got %v; expected %v", i, next, tt.next)
		}
	}
}
Example #24
0
func TestAddr(t *testing.T) {
	for _, tt := range addrTests {
		if p, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen); err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		} else if !p.Addr().Equal(tt.ip) {
			t.Fatalf("%v: got %v; expected %v", p, p.Addr(), tt.ip)
		} else if p.Len() != tt.prefixLen {
			t.Fatalf("%v: got %v; expected %v", p, p.Len(), tt.prefixLen)
		} else if !p.LastAddr().Equal(tt.last) {
			t.Fatalf("%v: got %v; expected %v", p, p.LastAddr(), tt.last)
		}
	}
}
Example #25
0
func TestPrefixHeap(t *testing.T) {
	super, err := ipaddr.NewPrefix(net.ParseIP("2001:db8:f001::"), 48)
	if err != nil {
		t.Fatalf("ipaddr.NewPrefix failed: %v", err)
	}
	h := new(prefixHeap)
	heap.Init(h)
	for _, p := range super.Subnets(6) {
		heap.Push(h, p)
	}
	for h.Len() > 0 {
		heap.Pop(h)
	}
}
Example #26
0
func TestMask(t *testing.T) {
	for _, tt := range maskTests {
		p, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen)
		if err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		} else if bytes.Compare(p.Netmask(), tt.netmask) != 0 {
			t.Fatalf("%v: got %v; expected %v", p, p.Netmask(), tt.netmask)
		}
		hostmask := invmask(tt.netmask)
		if bytes.Compare(p.Hostmask(), hostmask) != 0 {
			t.Fatalf("%v: got %v; expected %v", p, p.Hostmask(), hostmask)
		}
	}
}
Example #27
0
func TestPrefixHeap(t *testing.T) {
	_, n, err := net.ParseCIDR("2001:db8:f001::/48")
	if err != nil {
		t.Fatal(err)
	}
	super := ipaddr.NewPrefix(n)
	h := new(prefixHeap)
	heap.Init(h)
	for _, p := range super.Subnets(6) {
		heap.Push(h, p)
	}
	for h.Len() > 0 {
		heap.Pop(h)
	}
}
Example #28
0
func TestSet(t *testing.T) {
	for _, tt := range setTests {
		p, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen)
		if err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		}
		ip := p.Addr()
		ip[len(ip)-1]++
		if err := p.Set(ip, p.Len()-1); err != nil {
			t.Fatalf("ipaddr.Set failed: %v", err)
		}
		ip[len(ip)-1]--
		if err := p.Set(ip, p.Len()+1); err != nil {
			t.Fatalf("ipaddr.Set failed: %v", err)
		}
		p1, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen)
		if err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		}
		if !p.Equal(p1) {
			t.Fatalf("got %v; expected %v", p, p1)
		}
	}
}
Example #29
0
func TestPrefixTrie(t *testing.T) {
	for _, tt := range prefixTrieTests {
		var ps []ipaddr.Prefix
		for _, st := range tt {
			p, err := ipaddr.NewPrefix(st.addr, st.prefixLen)
			if err != nil {
				t.Fatalf("ipaddr.NewPrefix failed: %v", err)
			}
			ps = append(ps, p)
		}
		sort.Sort(byAddress(ps))
		ni := 1
		ns := prefixTrie(make([]ipv4Node, 0), &ni, ps, 0, 0)
		traverse(t, ns, ps, ns[0])
	}
}
Example #30
0
func TestSubnetIter(t *testing.T) {
	for _, tt := range subnetIterTests {
		p, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen)
		if err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		}
		i := 0
		for s := range p.SubnetIter(tt.nbits) {
			if s.String() != tt.subs[i] {
				t.Errorf("got %v; expected %v", s, tt.subs[i])
			}
			i++
		}
		if i != len(tt.subs) {
			t.Fatalf("got %v; expected %v", i, len(tt.subs))
		}
	}
}