Пример #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)
		}
	}
}
Пример #2
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)
				}
			}
		}
	}
}
Пример #3
0
func BenchmarkIPv4SummaryPrefix(b *testing.B) {
	var nn []*net.IPNet
	for _, ns := range []string{"172.16.141.0/24", "172.16.142.0/24", "172.16.143.0/24"} {
		_, n, err := net.ParseCIDR(ns)
		if err != nil {
			b.Fatalf("net.ParseCIDR failed: %v", err)
		}
		nn = append(nn, n)
	}
	var subs []ipaddr.Prefix
	for _, n := range nn {
		l, _ := n.Mask.Size()
		p, _ := ipaddr.NewPrefix(n.IP, l)
		subs = append(subs, p)
	}
	for i := 0; i < b.N; i++ {
		ipaddr.SummaryPrefix(subs)
	}
}
Пример #4
0
func TestSubnets(t *testing.T) {
	for i, tt := range subnetsTests {
		p, err := ipaddr.NewPrefix(tt.addr, tt.prefixLen)
		if err != nil {
			t.Fatalf("ipaddr.NewPrefix failed: %v", err)
		}
		subs := p.Subnets(tt.nbits)
		if len(subs) != 1<<uint(tt.nbits) {
			t.Fatalf("%v: got %v; expected %v", p, len(subs), 1<<uint(tt.nbits))
		}
		for _, s := range subs {
			if s.Len() != tt.prefixLen+tt.nbits {
				t.Errorf("%v: got %v; expected %v", p, s.Len(), tt.prefixLen+tt.nbits)
			}
		}
		if sum := ipaddr.SummaryPrefix(subs); sum == nil {
			for _, s := range subs {
				t.Logf("subnet: %v", s)
			}
			t.Fatalf("#%v: got %v; expected %v", i, sum, p)
		}
	}
}