github.com/jjyr/docker@v1.5.0-rc2/daemon/networkdriver/ipallocator/allocator_test.go (about)

     1  package ipallocator
     2  
     3  import (
     4  	"fmt"
     5  	"math/big"
     6  	"net"
     7  	"testing"
     8  )
     9  
    10  func reset() {
    11  	allocatedIPs = networkSet{}
    12  }
    13  
    14  func TestConversion(t *testing.T) {
    15  	ip := net.ParseIP("127.0.0.1")
    16  	i := ipToBigInt(ip)
    17  	if i.Cmp(big.NewInt(0x7f000001)) != 0 {
    18  		t.Fatal("incorrect conversion")
    19  	}
    20  	conv := bigIntToIP(i)
    21  	if !ip.Equal(conv) {
    22  		t.Error(conv.String())
    23  	}
    24  }
    25  
    26  func TestConversionIPv6(t *testing.T) {
    27  	ip := net.ParseIP("2a00:1450::1")
    28  	ip2 := net.ParseIP("2a00:1450::2")
    29  	ip3 := net.ParseIP("2a00:1450::1:1")
    30  	i := ipToBigInt(ip)
    31  	val, success := big.NewInt(0).SetString("2a001450000000000000000000000001", 16)
    32  	if !success {
    33  		t.Fatal("Hex-String to BigInt conversion failed.")
    34  	}
    35  	if i.Cmp(val) != 0 {
    36  		t.Fatal("incorrent conversion")
    37  	}
    38  
    39  	conv := bigIntToIP(i)
    40  	conv2 := bigIntToIP(big.NewInt(0).Add(i, big.NewInt(1)))
    41  	conv3 := bigIntToIP(big.NewInt(0).Add(i, big.NewInt(0x10000)))
    42  
    43  	if !ip.Equal(conv) {
    44  		t.Error("2a00:1450::1 should be equal to " + conv.String())
    45  	}
    46  	if !ip2.Equal(conv2) {
    47  		t.Error("2a00:1450::2 should be equal to " + conv2.String())
    48  	}
    49  	if !ip3.Equal(conv3) {
    50  		t.Error("2a00:1450::1:1 should be equal to " + conv3.String())
    51  	}
    52  }
    53  
    54  func TestRequestNewIps(t *testing.T) {
    55  	defer reset()
    56  	network := &net.IPNet{
    57  		IP:   []byte{192, 168, 0, 1},
    58  		Mask: []byte{255, 255, 255, 0},
    59  	}
    60  
    61  	var ip net.IP
    62  	var err error
    63  
    64  	for i := 2; i < 10; i++ {
    65  		ip, err = RequestIP(network, nil)
    66  		if err != nil {
    67  			t.Fatal(err)
    68  		}
    69  
    70  		if expected := fmt.Sprintf("192.168.0.%d", i); ip.String() != expected {
    71  			t.Fatalf("Expected ip %s got %s", expected, ip.String())
    72  		}
    73  	}
    74  	value := bigIntToIP(big.NewInt(0).Add(ipToBigInt(ip), big.NewInt(1))).String()
    75  	if err := ReleaseIP(network, ip); err != nil {
    76  		t.Fatal(err)
    77  	}
    78  	ip, err = RequestIP(network, nil)
    79  	if err != nil {
    80  		t.Fatal(err)
    81  	}
    82  	if ip.String() != value {
    83  		t.Fatalf("Expected to receive the next ip %s got %s", value, ip.String())
    84  	}
    85  }
    86  
    87  func TestRequestNewIpV6(t *testing.T) {
    88  	defer reset()
    89  	network := &net.IPNet{
    90  		IP:   []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    91  		Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask
    92  	}
    93  
    94  	var ip net.IP
    95  	var err error
    96  	for i := 1; i < 10; i++ {
    97  		ip, err = RequestIP(network, nil)
    98  		if err != nil {
    99  			t.Fatal(err)
   100  		}
   101  
   102  		if expected := fmt.Sprintf("2a00:1450::%d", i); ip.String() != expected {
   103  			t.Fatalf("Expected ip %s got %s", expected, ip.String())
   104  		}
   105  	}
   106  	value := bigIntToIP(big.NewInt(0).Add(ipToBigInt(ip), big.NewInt(1))).String()
   107  	if err := ReleaseIP(network, ip); err != nil {
   108  		t.Fatal(err)
   109  	}
   110  	ip, err = RequestIP(network, nil)
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	if ip.String() != value {
   115  		t.Fatalf("Expected to receive the next ip %s got %s", value, ip.String())
   116  	}
   117  }
   118  
   119  func TestReleaseIp(t *testing.T) {
   120  	defer reset()
   121  	network := &net.IPNet{
   122  		IP:   []byte{192, 168, 0, 1},
   123  		Mask: []byte{255, 255, 255, 0},
   124  	}
   125  
   126  	ip, err := RequestIP(network, nil)
   127  	if err != nil {
   128  		t.Fatal(err)
   129  	}
   130  
   131  	if err := ReleaseIP(network, ip); err != nil {
   132  		t.Fatal(err)
   133  	}
   134  }
   135  
   136  func TestReleaseIpV6(t *testing.T) {
   137  	defer reset()
   138  	network := &net.IPNet{
   139  		IP:   []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   140  		Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask
   141  	}
   142  
   143  	ip, err := RequestIP(network, nil)
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  
   148  	if err := ReleaseIP(network, ip); err != nil {
   149  		t.Fatal(err)
   150  	}
   151  }
   152  
   153  func TestGetReleasedIp(t *testing.T) {
   154  	defer reset()
   155  	network := &net.IPNet{
   156  		IP:   []byte{192, 168, 0, 1},
   157  		Mask: []byte{255, 255, 255, 0},
   158  	}
   159  
   160  	ip, err := RequestIP(network, nil)
   161  	if err != nil {
   162  		t.Fatal(err)
   163  	}
   164  
   165  	value := ip.String()
   166  	if err := ReleaseIP(network, ip); err != nil {
   167  		t.Fatal(err)
   168  	}
   169  
   170  	for i := 0; i < 252; i++ {
   171  		_, err = RequestIP(network, nil)
   172  		if err != nil {
   173  			t.Fatal(err)
   174  		}
   175  		err = ReleaseIP(network, ip)
   176  		if err != nil {
   177  			t.Fatal(err)
   178  		}
   179  	}
   180  
   181  	ip, err = RequestIP(network, nil)
   182  	if err != nil {
   183  		t.Fatal(err)
   184  	}
   185  
   186  	if ip.String() != value {
   187  		t.Fatalf("Expected to receive same ip %s got %s", value, ip.String())
   188  	}
   189  }
   190  
   191  func TestGetReleasedIpV6(t *testing.T) {
   192  	defer reset()
   193  	network := &net.IPNet{
   194  		IP:   []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   195  		Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0},
   196  	}
   197  
   198  	ip, err := RequestIP(network, nil)
   199  	if err != nil {
   200  		t.Fatal(err)
   201  	}
   202  
   203  	value := ip.String()
   204  	if err := ReleaseIP(network, ip); err != nil {
   205  		t.Fatal(err)
   206  	}
   207  
   208  	for i := 0; i < 253; i++ {
   209  		_, err = RequestIP(network, nil)
   210  		if err != nil {
   211  			t.Fatal(err)
   212  		}
   213  		err = ReleaseIP(network, ip)
   214  		if err != nil {
   215  			t.Fatal(err)
   216  		}
   217  	}
   218  
   219  	ip, err = RequestIP(network, nil)
   220  	if err != nil {
   221  		t.Fatal(err)
   222  	}
   223  
   224  	if ip.String() != value {
   225  		t.Fatalf("Expected to receive same ip %s got %s", value, ip.String())
   226  	}
   227  }
   228  
   229  func TestRequestSpecificIp(t *testing.T) {
   230  	defer reset()
   231  	network := &net.IPNet{
   232  		IP:   []byte{192, 168, 0, 1},
   233  		Mask: []byte{255, 255, 255, 224},
   234  	}
   235  
   236  	ip := net.ParseIP("192.168.0.5")
   237  
   238  	// Request a "good" IP.
   239  	if _, err := RequestIP(network, ip); err != nil {
   240  		t.Fatal(err)
   241  	}
   242  
   243  	// Request the same IP again.
   244  	if _, err := RequestIP(network, ip); err != ErrIPAlreadyAllocated {
   245  		t.Fatalf("Got the same IP twice: %#v", err)
   246  	}
   247  
   248  	// Request an out of range IP.
   249  	if _, err := RequestIP(network, net.ParseIP("192.168.0.42")); err != ErrIPOutOfRange {
   250  		t.Fatalf("Got an out of range IP: %#v", err)
   251  	}
   252  }
   253  
   254  func TestRequestSpecificIpV6(t *testing.T) {
   255  	defer reset()
   256  	network := &net.IPNet{
   257  		IP:   []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   258  		Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask
   259  	}
   260  
   261  	ip := net.ParseIP("2a00:1450::5")
   262  
   263  	// Request a "good" IP.
   264  	if _, err := RequestIP(network, ip); err != nil {
   265  		t.Fatal(err)
   266  	}
   267  
   268  	// Request the same IP again.
   269  	if _, err := RequestIP(network, ip); err != ErrIPAlreadyAllocated {
   270  		t.Fatalf("Got the same IP twice: %#v", err)
   271  	}
   272  
   273  	// Request an out of range IP.
   274  	if _, err := RequestIP(network, net.ParseIP("2a00:1500::1")); err != ErrIPOutOfRange {
   275  		t.Fatalf("Got an out of range IP: %#v", err)
   276  	}
   277  }
   278  
   279  func TestIPAllocator(t *testing.T) {
   280  	expectedIPs := []net.IP{
   281  		0: net.IPv4(127, 0, 0, 2),
   282  		1: net.IPv4(127, 0, 0, 3),
   283  		2: net.IPv4(127, 0, 0, 4),
   284  		3: net.IPv4(127, 0, 0, 5),
   285  		4: net.IPv4(127, 0, 0, 6),
   286  	}
   287  
   288  	gwIP, n, _ := net.ParseCIDR("127.0.0.1/29")
   289  
   290  	network := &net.IPNet{IP: gwIP, Mask: n.Mask}
   291  	// Pool after initialisation (f = free, u = used)
   292  	// 2(f) - 3(f) - 4(f) - 5(f) - 6(f)
   293  	//  ↑
   294  
   295  	// Check that we get 5 IPs, from 127.0.0.2–127.0.0.6, in that
   296  	// order.
   297  	for i := 0; i < 5; i++ {
   298  		ip, err := RequestIP(network, nil)
   299  		if err != nil {
   300  			t.Fatal(err)
   301  		}
   302  
   303  		assertIPEquals(t, expectedIPs[i], ip)
   304  	}
   305  	// Before loop begin
   306  	// 2(f) - 3(f) - 4(f) - 5(f) - 6(f)
   307  	//  ↑
   308  
   309  	// After i = 0
   310  	// 2(u) - 3(f) - 4(f) - 5(f) - 6(f)
   311  	//         ↑
   312  
   313  	// After i = 1
   314  	// 2(u) - 3(u) - 4(f) - 5(f) - 6(f)
   315  	//                ↑
   316  
   317  	// After i = 2
   318  	// 2(u) - 3(u) - 4(u) - 5(f) - 6(f)
   319  	//                       ↑
   320  
   321  	// After i = 3
   322  	// 2(u) - 3(u) - 4(u) - 5(u) - 6(f)
   323  	//                              ↑
   324  
   325  	// After i = 4
   326  	// 2(u) - 3(u) - 4(u) - 5(u) - 6(u)
   327  	//  ↑
   328  
   329  	// Check that there are no more IPs
   330  	ip, err := RequestIP(network, nil)
   331  	if err == nil {
   332  		t.Fatalf("There shouldn't be any IP addresses at this point, got %s\n", ip)
   333  	}
   334  
   335  	// Release some IPs in non-sequential order
   336  	if err := ReleaseIP(network, expectedIPs[3]); err != nil {
   337  		t.Fatal(err)
   338  	}
   339  	// 2(u) - 3(u) - 4(u) - 5(f) - 6(u)
   340  	//                       ↑
   341  
   342  	if err := ReleaseIP(network, expectedIPs[2]); err != nil {
   343  		t.Fatal(err)
   344  	}
   345  	// 2(u) - 3(u) - 4(f) - 5(f) - 6(u)
   346  	//                       ↑
   347  
   348  	if err := ReleaseIP(network, expectedIPs[4]); err != nil {
   349  		t.Fatal(err)
   350  	}
   351  	// 2(u) - 3(u) - 4(f) - 5(f) - 6(f)
   352  	//                       ↑
   353  
   354  	// Make sure that IPs are reused in sequential order, starting
   355  	// with the first released IP
   356  	newIPs := make([]net.IP, 3)
   357  	for i := 0; i < 3; i++ {
   358  		ip, err := RequestIP(network, nil)
   359  		if err != nil {
   360  			t.Fatal(err)
   361  		}
   362  
   363  		newIPs[i] = ip
   364  	}
   365  	assertIPEquals(t, expectedIPs[2], newIPs[0])
   366  	assertIPEquals(t, expectedIPs[3], newIPs[1])
   367  	assertIPEquals(t, expectedIPs[4], newIPs[2])
   368  
   369  	_, err = RequestIP(network, nil)
   370  	if err == nil {
   371  		t.Fatal("There shouldn't be any IP addresses at this point")
   372  	}
   373  }
   374  
   375  func TestAllocateFirstIP(t *testing.T) {
   376  	defer reset()
   377  	network := &net.IPNet{
   378  		IP:   []byte{192, 168, 0, 0},
   379  		Mask: []byte{255, 255, 255, 0},
   380  	}
   381  
   382  	firstIP := network.IP.To4().Mask(network.Mask)
   383  	first := big.NewInt(0).Add(ipToBigInt(firstIP), big.NewInt(1))
   384  
   385  	ip, err := RequestIP(network, nil)
   386  	if err != nil {
   387  		t.Fatal(err)
   388  	}
   389  	allocated := ipToBigInt(ip)
   390  
   391  	if allocated == first {
   392  		t.Fatalf("allocated ip should not equal first ip: %d == %d", first, allocated)
   393  	}
   394  }
   395  
   396  func TestAllocateAllIps(t *testing.T) {
   397  	defer reset()
   398  	network := &net.IPNet{
   399  		IP:   []byte{192, 168, 0, 1},
   400  		Mask: []byte{255, 255, 255, 0},
   401  	}
   402  
   403  	var (
   404  		current, first net.IP
   405  		err            error
   406  		isFirst        = true
   407  	)
   408  
   409  	for err == nil {
   410  		current, err = RequestIP(network, nil)
   411  		if isFirst {
   412  			first = current
   413  			isFirst = false
   414  		}
   415  	}
   416  
   417  	if err != ErrNoAvailableIPs {
   418  		t.Fatal(err)
   419  	}
   420  
   421  	if _, err := RequestIP(network, nil); err != ErrNoAvailableIPs {
   422  		t.Fatal(err)
   423  	}
   424  
   425  	if err := ReleaseIP(network, first); err != nil {
   426  		t.Fatal(err)
   427  	}
   428  
   429  	again, err := RequestIP(network, nil)
   430  	if err != nil {
   431  		t.Fatal(err)
   432  	}
   433  
   434  	assertIPEquals(t, first, again)
   435  
   436  	// ensure that alloc.last == alloc.begin won't result in dead loop
   437  	if _, err := RequestIP(network, nil); err != ErrNoAvailableIPs {
   438  		t.Fatal(err)
   439  	}
   440  
   441  	// Test by making alloc.last the only free ip and ensure we get it back
   442  	// #1. first of the range, (alloc.last == ipToInt(first) already)
   443  	if err := ReleaseIP(network, first); err != nil {
   444  		t.Fatal(err)
   445  	}
   446  
   447  	ret, err := RequestIP(network, nil)
   448  	if err != nil {
   449  		t.Fatal(err)
   450  	}
   451  
   452  	assertIPEquals(t, first, ret)
   453  
   454  	// #2. last of the range, note that current is the last one
   455  	last := net.IPv4(192, 168, 0, 254)
   456  	setLastTo(t, network, last)
   457  
   458  	ret, err = RequestIP(network, nil)
   459  	if err != nil {
   460  		t.Fatal(err)
   461  	}
   462  
   463  	assertIPEquals(t, last, ret)
   464  
   465  	// #3. middle of the range
   466  	mid := net.IPv4(192, 168, 0, 7)
   467  	setLastTo(t, network, mid)
   468  
   469  	ret, err = RequestIP(network, nil)
   470  	if err != nil {
   471  		t.Fatal(err)
   472  	}
   473  
   474  	assertIPEquals(t, mid, ret)
   475  }
   476  
   477  // make sure the pool is full when calling setLastTo.
   478  // we don't cheat here
   479  func setLastTo(t *testing.T, network *net.IPNet, ip net.IP) {
   480  	if err := ReleaseIP(network, ip); err != nil {
   481  		t.Fatal(err)
   482  	}
   483  
   484  	ret, err := RequestIP(network, nil)
   485  	if err != nil {
   486  		t.Fatal(err)
   487  	}
   488  
   489  	assertIPEquals(t, ip, ret)
   490  
   491  	if err := ReleaseIP(network, ip); err != nil {
   492  		t.Fatal(err)
   493  	}
   494  }
   495  
   496  func TestAllocateDifferentSubnets(t *testing.T) {
   497  	defer reset()
   498  	network1 := &net.IPNet{
   499  		IP:   []byte{192, 168, 0, 1},
   500  		Mask: []byte{255, 255, 255, 0},
   501  	}
   502  	network2 := &net.IPNet{
   503  		IP:   []byte{127, 0, 0, 1},
   504  		Mask: []byte{255, 255, 255, 0},
   505  	}
   506  	network3 := &net.IPNet{
   507  		IP:   []byte{0x2a, 0x00, 0x14, 0x50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   508  		Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask
   509  	}
   510  	network4 := &net.IPNet{
   511  		IP:   []byte{0x2a, 0x00, 0x16, 0x32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
   512  		Mask: []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0}, // /64 netmask
   513  	}
   514  	expectedIPs := []net.IP{
   515  		0: net.IPv4(192, 168, 0, 2),
   516  		1: net.IPv4(192, 168, 0, 3),
   517  		2: net.IPv4(127, 0, 0, 2),
   518  		3: net.IPv4(127, 0, 0, 3),
   519  		4: net.ParseIP("2a00:1450::1"),
   520  		5: net.ParseIP("2a00:1450::2"),
   521  		6: net.ParseIP("2a00:1450::3"),
   522  		7: net.ParseIP("2a00:1632::1"),
   523  		8: net.ParseIP("2a00:1632::2"),
   524  	}
   525  
   526  	ip11, err := RequestIP(network1, nil)
   527  	if err != nil {
   528  		t.Fatal(err)
   529  	}
   530  	ip12, err := RequestIP(network1, nil)
   531  	if err != nil {
   532  		t.Fatal(err)
   533  	}
   534  	ip21, err := RequestIP(network2, nil)
   535  	if err != nil {
   536  		t.Fatal(err)
   537  	}
   538  	ip22, err := RequestIP(network2, nil)
   539  	if err != nil {
   540  		t.Fatal(err)
   541  	}
   542  	ip31, err := RequestIP(network3, nil)
   543  	if err != nil {
   544  		t.Fatal(err)
   545  	}
   546  	ip32, err := RequestIP(network3, nil)
   547  	if err != nil {
   548  		t.Fatal(err)
   549  	}
   550  	ip33, err := RequestIP(network3, nil)
   551  	if err != nil {
   552  		t.Fatal(err)
   553  	}
   554  	ip41, err := RequestIP(network4, nil)
   555  	if err != nil {
   556  		t.Fatal(err)
   557  	}
   558  	ip42, err := RequestIP(network4, nil)
   559  	if err != nil {
   560  		t.Fatal(err)
   561  	}
   562  	assertIPEquals(t, expectedIPs[0], ip11)
   563  	assertIPEquals(t, expectedIPs[1], ip12)
   564  	assertIPEquals(t, expectedIPs[2], ip21)
   565  	assertIPEquals(t, expectedIPs[3], ip22)
   566  	assertIPEquals(t, expectedIPs[4], ip31)
   567  	assertIPEquals(t, expectedIPs[5], ip32)
   568  	assertIPEquals(t, expectedIPs[6], ip33)
   569  	assertIPEquals(t, expectedIPs[7], ip41)
   570  	assertIPEquals(t, expectedIPs[8], ip42)
   571  }
   572  
   573  func TestRegisterBadTwice(t *testing.T) {
   574  	defer reset()
   575  	network := &net.IPNet{
   576  		IP:   []byte{192, 168, 1, 1},
   577  		Mask: []byte{255, 255, 255, 0},
   578  	}
   579  	subnet := &net.IPNet{
   580  		IP:   []byte{192, 168, 1, 8},
   581  		Mask: []byte{255, 255, 255, 248},
   582  	}
   583  
   584  	if err := RegisterSubnet(network, subnet); err != nil {
   585  		t.Fatal(err)
   586  	}
   587  	subnet = &net.IPNet{
   588  		IP:   []byte{192, 168, 1, 16},
   589  		Mask: []byte{255, 255, 255, 248},
   590  	}
   591  	if err := RegisterSubnet(network, subnet); err != ErrNetworkAlreadyRegistered {
   592  		t.Fatalf("Expecteded ErrNetworkAlreadyRegistered error, got %v", err)
   593  	}
   594  }
   595  
   596  func TestRegisterBadRange(t *testing.T) {
   597  	defer reset()
   598  	network := &net.IPNet{
   599  		IP:   []byte{192, 168, 1, 1},
   600  		Mask: []byte{255, 255, 255, 0},
   601  	}
   602  	subnet := &net.IPNet{
   603  		IP:   []byte{192, 168, 1, 1},
   604  		Mask: []byte{255, 255, 0, 0},
   605  	}
   606  	if err := RegisterSubnet(network, subnet); err != ErrBadSubnet {
   607  		t.Fatalf("Expected ErrBadSubnet error, got %v", err)
   608  	}
   609  }
   610  
   611  func TestAllocateFromRange(t *testing.T) {
   612  	defer reset()
   613  	network := &net.IPNet{
   614  		IP:   []byte{192, 168, 0, 1},
   615  		Mask: []byte{255, 255, 255, 0},
   616  	}
   617  	// 192.168.1.9 - 192.168.1.14
   618  	subnet := &net.IPNet{
   619  		IP:   []byte{192, 168, 0, 8},
   620  		Mask: []byte{255, 255, 255, 248},
   621  	}
   622  
   623  	if err := RegisterSubnet(network, subnet); err != nil {
   624  		t.Fatal(err)
   625  	}
   626  	expectedIPs := []net.IP{
   627  		0: net.IPv4(192, 168, 0, 9),
   628  		1: net.IPv4(192, 168, 0, 10),
   629  		2: net.IPv4(192, 168, 0, 11),
   630  		3: net.IPv4(192, 168, 0, 12),
   631  		4: net.IPv4(192, 168, 0, 13),
   632  		5: net.IPv4(192, 168, 0, 14),
   633  	}
   634  	for _, ip := range expectedIPs {
   635  		rip, err := RequestIP(network, nil)
   636  		if err != nil {
   637  			t.Fatal(err)
   638  		}
   639  		assertIPEquals(t, ip, rip)
   640  	}
   641  
   642  	if _, err := RequestIP(network, nil); err != ErrNoAvailableIPs {
   643  		t.Fatalf("Expected ErrNoAvailableIPs error, got %v", err)
   644  	}
   645  	for _, ip := range expectedIPs {
   646  		ReleaseIP(network, ip)
   647  		rip, err := RequestIP(network, nil)
   648  		if err != nil {
   649  			t.Fatal(err)
   650  		}
   651  		assertIPEquals(t, ip, rip)
   652  	}
   653  }
   654  
   655  func assertIPEquals(t *testing.T, ip1, ip2 net.IP) {
   656  	if !ip1.Equal(ip2) {
   657  		t.Fatalf("Expected IP %s, got %s", ip1, ip2)
   658  	}
   659  }
   660  
   661  func BenchmarkRequestIP(b *testing.B) {
   662  	network := &net.IPNet{
   663  		IP:   []byte{192, 168, 0, 1},
   664  		Mask: []byte{255, 255, 255, 0},
   665  	}
   666  	b.ResetTimer()
   667  	for i := 0; i < b.N; i++ {
   668  		for j := 0; j < 253; j++ {
   669  			_, err := RequestIP(network, nil)
   670  			if err != nil {
   671  				b.Fatal(err)
   672  			}
   673  		}
   674  		reset()
   675  	}
   676  }