Răsfoiți Sursa

Fix address usage check (#125)

Signed-off-by: Anton Kortunov <toshik@yandex-team.ru>

Co-authored-by: Anton Kortunov <toshik@yandex-team.ru>
toshic 4 ani în urmă
părinte
comite
10c40bd4e9

+ 1 - 1
plugins/allocators/bitmap/bitmap_ipv4.go

@@ -69,7 +69,7 @@ func (a *IPv4Allocator) Allocate(hint net.IPNet) (n net.IPNet, err error) {
 
 	var next uint
 	// First try the exact match
-	if a.bitmap.Test(hintOffset) {
+	if !a.bitmap.Test(hintOffset) {
 		next = hintOffset
 	} else {
 		// Then any available address

+ 12 - 3
plugins/allocators/bitmap/bitmap_ipv4_test.go

@@ -20,17 +20,26 @@ func getv4Allocator() *IPv4Allocator {
 func Test4Alloc(t *testing.T) {
 	alloc := getv4Allocator()
 
-	net, err := alloc.Allocate(net.IPNet{})
+	net1, err := alloc.Allocate(net.IPNet{})
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	err = alloc.Free(net)
+	net2, err := alloc.Allocate(net.IPNet{})
 	if err != nil {
 		t.Fatal(err)
 	}
 
-	err = alloc.Free(net)
+	if net1.IP.Equal(net2.IP) {
+		t.Fatal("That address was already allocated")
+	}
+
+	err = alloc.Free(net1)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	err = alloc.Free(net1)
 	if err == nil {
 		t.Fatal("Expected DoubleFree error")
 	}