github.phpd.cn/cilium/cilium@v1.6.12/pkg/ip/ip_test.go (about)

     1  // Copyright 2017 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build !privileged_tests
    16  
    17  package ip
    18  
    19  import (
    20  	"math/rand"
    21  	"net"
    22  	"sort"
    23  	"testing"
    24  
    25  	"github.com/cilium/cilium/pkg/checker"
    26  
    27  	. "gopkg.in/check.v1"
    28  )
    29  
    30  // Hook up gocheck into the "go test" runner.
    31  type IPTestSuite struct{}
    32  
    33  var _ = Suite(&IPTestSuite{})
    34  
    35  func Test(t *testing.T) {
    36  	TestingT(t)
    37  }
    38  
    39  func (s *IPTestSuite) TestCountIPs(c *C) {
    40  	tests := map[string]int{
    41  		"192.168.0.1/32": 1,
    42  		"192.168.0.1/31": 1,
    43  		"192.168.0.1/30": 3,
    44  		"192.168.0.1/24": 255,
    45  		"192.168.0.1/16": 65535,
    46  		"::1/128":        1,
    47  		"::1/120":        255,
    48  	}
    49  	for cidr, nIPs := range tests {
    50  		_, ipnet, err := net.ParseCIDR(cidr)
    51  		c.Assert(err, IsNil)
    52  		count := CountIPsInCIDR(ipnet)
    53  		c.Assert(count, Equals, nIPs)
    54  	}
    55  }
    56  
    57  func (s *IPTestSuite) TestFirstIP(c *C) {
    58  	// Test IPv4.
    59  	desiredIPv4_1 := net.IP{0xa, 0, 0, 0}
    60  	testNetv4_1 := net.IPNet{IP: net.ParseIP("10.0.0.5"), Mask: net.CIDRMask(8, 32)}
    61  	ipNetv4_1 := getNetworkPrefix(&testNetv4_1)
    62  	for k := range *ipNetv4_1 {
    63  		c.Assert((*ipNetv4_1)[k], Equals, desiredIPv4_1[k])
    64  	}
    65  	testNetv4_2 := net.IPNet{IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)}
    66  	ipNetv4_2 := getNetworkPrefix(&testNetv4_2)
    67  	for k := range *ipNetv4_2 {
    68  		c.Assert((*ipNetv4_2)[k], Equals, desiredIPv4_1[k])
    69  	}
    70  
    71  	// Test IPv6
    72  	desiredIPv6_1, testNetv6_1, _ := net.ParseCIDR("fd44:7089:ff32:712b::/64")
    73  
    74  	ipNetv6_1 := getNetworkPrefix(testNetv6_1)
    75  	for k := range *ipNetv6_1 {
    76  		c.Assert((*ipNetv6_1)[k], Equals, desiredIPv6_1[k])
    77  	}
    78  }
    79  
    80  func (s *IPTestSuite) testIPNetsEqual(created, expected []*net.IPNet, c *C) {
    81  	for index := range created {
    82  		c.Assert(created[index].String(), Equals, expected[index].String())
    83  		c.Assert(created[index].Mask.String(), Equals, expected[index].Mask.String())
    84  	}
    85  }
    86  
    87  func (s *IPTestSuite) testIPsEqual(created, expected net.IP, c *C) {
    88  	for k := range created {
    89  		c.Assert(created[k], Equals, expected[k])
    90  	}
    91  }
    92  
    93  func createIPNet(address string, maskSize int, bitLen int) *net.IPNet {
    94  	return &net.IPNet{IP: net.ParseIP(address), Mask: net.CIDRMask(maskSize, bitLen)}
    95  }
    96  
    97  func createIPRange(first string, last string) *netWithRange {
    98  	firstIP := net.ParseIP(first)
    99  	lastIP := net.ParseIP(last)
   100  	return &netWithRange{First: &firstIP, Last: &lastIP}
   101  }
   102  
   103  func (s *IPTestSuite) TestRemoveCIDRs(c *C) {
   104  	allowCIDRs := []*net.IPNet{createIPNet("10.0.0.0", 8, int(ipv4BitLen))}
   105  	removeCIDRs := []*net.IPNet{createIPNet("10.96.0.0", 12, int(ipv4BitLen)),
   106  		createIPNet("10.112.0.0", 13, int(ipv4BitLen)),
   107  	}
   108  	expectedCIDRs := []*net.IPNet{createIPNet("10.128.0.0", 9, int(ipv4BitLen)),
   109  		createIPNet("10.0.0.0", 10, int(ipv4BitLen)),
   110  		createIPNet("10.64.0.0", 11, int(ipv4BitLen)),
   111  		createIPNet("10.120.0.0", 13, int(ipv4BitLen))}
   112  
   113  	allowedCIDRs, err := RemoveCIDRs(allowCIDRs, removeCIDRs)
   114  	c.Assert(err, IsNil)
   115  
   116  	s.testIPNetsEqual(allowedCIDRs, expectedCIDRs, c)
   117  
   118  	allowCIDRs = []*net.IPNet{createIPNet("10.0.0.0", 8, int(ipv4BitLen))}
   119  	removeCIDRs = []*net.IPNet{createIPNet("10.96.0.0", 12, int(ipv4BitLen)),
   120  		createIPNet("10.112.0.0", 13, int(ipv4BitLen)),
   121  		createIPNet("10.62.0.33", 32, int(ipv4BitLen)),
   122  		createIPNet("10.93.0.4", 30, int(ipv4BitLen)),
   123  		createIPNet("10.63.0.5", 13, int(ipv4BitLen)),
   124  	}
   125  
   126  	expectedCIDRs = []*net.IPNet{createIPNet("10.128.0.0", 9, int(ipv4BitLen)),
   127  		createIPNet("10.0.0.0", 11, int(ipv4BitLen)),
   128  		createIPNet("10.32.0.0", 12, int(ipv4BitLen)),
   129  		createIPNet("10.48.0.0", 13, int(ipv4BitLen)),
   130  		createIPNet("10.120.0.0", 13, int(ipv4BitLen)),
   131  		createIPNet("10.64.0.0", 12, int(ipv4BitLen)),
   132  		createIPNet("10.80.0.0", 13, int(ipv4BitLen)),
   133  		createIPNet("10.88.0.0", 14, int(ipv4BitLen)),
   134  		createIPNet("10.94.0.0", 15, int(ipv4BitLen)),
   135  		createIPNet("10.92.0.0", 16, int(ipv4BitLen)),
   136  		createIPNet("10.93.128.0", 17, int(ipv4BitLen)),
   137  		createIPNet("10.93.64.0", 18, int(ipv4BitLen)),
   138  		createIPNet("10.93.32.0", 19, int(ipv4BitLen)),
   139  		createIPNet("10.93.16.0", 20, int(ipv4BitLen)),
   140  		createIPNet("10.93.8.0", 21, int(ipv4BitLen)),
   141  		createIPNet("10.93.4.0", 22, int(ipv4BitLen)),
   142  		createIPNet("10.93.2.0", 23, int(ipv4BitLen)),
   143  		createIPNet("10.93.1.0", 24, int(ipv4BitLen)),
   144  		createIPNet("10.93.0.128", 25, int(ipv4BitLen)),
   145  		createIPNet("10.93.0.64", 26, int(ipv4BitLen)),
   146  		createIPNet("10.93.0.32", 27, int(ipv4BitLen)),
   147  		createIPNet("10.93.0.16", 28, int(ipv4BitLen)),
   148  		createIPNet("10.93.0.8", 29, int(ipv4BitLen)),
   149  		createIPNet("10.93.0.0", 30, int(ipv4BitLen)),
   150  	}
   151  
   152  	allowedCIDRs, err = RemoveCIDRs(allowCIDRs, removeCIDRs)
   153  	c.Assert(err, IsNil)
   154  	s.testIPNetsEqual(allowedCIDRs, expectedCIDRs, c)
   155  
   156  	// Cannot remove CIDRs that are of a different address family.
   157  	removeCIDRs = []*net.IPNet{createIPNet("fd44:7089:ff32:712b::", 66, int(ipv6BitLen))}
   158  	_, err = RemoveCIDRs(allowCIDRs, removeCIDRs)
   159  	c.Assert(err, NotNil)
   160  
   161  	//IPv6 tests
   162  	allowCIDRs = []*net.IPNet{createIPNet("fd44:7089:ff32:712b:ff00::", 64, int(ipv6BitLen))}
   163  	allowedCIDRs, err = RemoveCIDRs(allowCIDRs, removeCIDRs)
   164  
   165  	c.Assert(err, IsNil)
   166  	expectedCIDRs = []*net.IPNet{createIPNet("fd44:7089:ff32:712b:8000::", 65, int(ipv6BitLen)),
   167  		createIPNet("fd44:7089:ff32:712b:4000::", 66, int(ipv6BitLen))}
   168  	s.testIPNetsEqual(allowedCIDRs, expectedCIDRs, c)
   169  
   170  }
   171  
   172  func (s *IPTestSuite) TestByteFunctions(c *C) {
   173  
   174  	//getByteIndexofBit
   175  	byteNum := getByteIndexOfBit(0)
   176  	c.Assert(byteNum, Equals, uint(15))
   177  	byteNum = getByteIndexOfBit(1)
   178  	c.Assert(byteNum, Equals, uint(15))
   179  	byteNum = getByteIndexOfBit(8)
   180  	c.Assert(byteNum, Equals, uint(14))
   181  	byteNum = getByteIndexOfBit(9)
   182  	c.Assert(byteNum, Equals, uint(14))
   183  
   184  	//getNthBit
   185  	testNet := net.IPNet{IP: net.ParseIP("10.96.0.0"), Mask: net.CIDRMask(12, int(ipv4BitLen))}
   186  	bit := getNthBit(&(testNet.IP), 20)
   187  	c.Assert(bit, Equals, uint8(0))
   188  	bit = getNthBit(&(testNet.IP), 22)
   189  	c.Assert(bit, Equals, uint8(1))
   190  
   191  	//flipNthBit
   192  	testBytes := []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0x0, 0x0}
   193  	newBytes := flipNthBit(&testBytes, 10)
   194  	expectedBytes := []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xff, 0x0, 0x0, 0x4, 0x0}
   195  	for k := range expectedBytes {
   196  		c.Assert(expectedBytes[k], Equals, (*newBytes)[k])
   197  	}
   198  
   199  	newBytes = flipNthBit(&testBytes, 32)
   200  	expectedBytes = []byte{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0xfe, 0x0, 0x0, 0x0, 0x0}
   201  	for k := range expectedBytes {
   202  		c.Assert(expectedBytes[k], Equals, (*newBytes)[k])
   203  	}
   204  
   205  }
   206  
   207  func (s *IPTestSuite) TestIPNetToRange(c *C) {
   208  
   209  	testRange := ipNetToRange(*createIPNet("192.0.128.0", 24, int(ipv4BitLen)))
   210  	var expectedFirst, expectedLast []byte
   211  	expectedFirst = append(expectedFirst, v4Mappedv6Prefix...)
   212  	expectedFirst = append(expectedFirst, []byte{192, 0, 128, 0}...)
   213  	expectedFirstIP := net.IP(expectedFirst)
   214  
   215  	expectedLast = append(expectedLast, v4Mappedv6Prefix...)
   216  	expectedLast = append(expectedLast, []byte{192, 0, 128, 255}...)
   217  	expectedLastIP := net.IP(expectedLast)
   218  	expectedRange := netWithRange{First: &expectedFirstIP, Last: &expectedLastIP}
   219  
   220  	s.checkRangesEqual(&expectedRange, &testRange, c)
   221  
   222  	// Check that all bits are masked correctly.
   223  	testRange = ipNetToRange(*createIPNet("192.0.128.255", 24, int(ipv4BitLen)))
   224  	s.checkRangesEqual(&expectedRange, &testRange, c)
   225  
   226  	testRange = ipNetToRange(*createIPNet("fd44:7089:ff32:712b:ff00::", 64, int(ipv6BitLen)))
   227  	testRange = ipNetToRange(*createIPNet("::ffff:0", 128, int(ipv6BitLen)))
   228  
   229  }
   230  
   231  func (s *IPTestSuite) checkRangesEqual(range1, range2 *netWithRange, c *C) {
   232  	for l := range *range1.First {
   233  		c.Assert((*range1.First)[l], Equals, (*range2.First)[l])
   234  	}
   235  	for l := range *range1.Last {
   236  		c.Assert((*range1.Last)[l], Equals, (*range2.Last)[l])
   237  	}
   238  }
   239  
   240  func (s *IPTestSuite) TestNetsByRange(c *C) {
   241  	ranges := []*netWithRange{}
   242  
   243  	// Check sorting by last IP first
   244  	cidrs := []*net.IPNet{createIPNet("10.0.0.0", 8, int(ipv4BitLen)),
   245  		createIPNet("10.0.0.0", 10, int(ipv4BitLen)),
   246  		createIPNet("10.64.0.0", 11, int(ipv4BitLen)),
   247  		createIPNet("10.112.0.0", 12, int(ipv4BitLen))}
   248  
   249  	for _, network := range cidrs {
   250  		newNetToRange := ipNetToRange(*network)
   251  		ranges = append(ranges, &newNetToRange)
   252  	}
   253  
   254  	expectedRanges := []*netWithRange{
   255  		createIPRange("10.0.0.0", "10.63.255.255"),
   256  		createIPRange("10.64.0.0", "10.95.255.255"),
   257  		createIPRange("10.112.0.0", "10.127.255.255"),
   258  		createIPRange("10.0.0.0", "10.255.255.255")}
   259  	sort.Sort(NetsByRange(ranges))
   260  	// Ensure that length of ranges isn't modified first.
   261  	c.Assert(len(ranges), Equals, len(expectedRanges))
   262  	for k := range ranges {
   263  		s.checkRangesEqual(ranges[k], expectedRanges[k], c)
   264  	}
   265  
   266  	ranges = []*netWithRange{createIPRange("10.0.0.0", "10.255.255.255"),
   267  		createIPRange("10.255.255.254", "10.255.255.255")}
   268  	expectedRanges = []*netWithRange{createIPRange("10.0.0.0", "10.255.255.255"),
   269  		createIPRange("10.255.255.254", "10.255.255.255")}
   270  	sort.Sort(NetsByRange(ranges))
   271  	// Ensure that length of ranges isn't modified first.
   272  	c.Assert(len(ranges), Equals, len(expectedRanges))
   273  	for k := range ranges {
   274  		s.checkRangesEqual(ranges[k], expectedRanges[k], c)
   275  	}
   276  
   277  }
   278  
   279  func (s *IPTestSuite) TestCoalesceCIDRs(c *C) {
   280  
   281  	cidrs := []*net.IPNet{createIPNet("192.0.128.0", 24, int(ipv4BitLen)),
   282  		createIPNet("192.0.129.0", 24, int(ipv4BitLen))}
   283  	expected := []*net.IPNet{createIPNet("192.0.128.0", 23, int(ipv4BitLen))}
   284  	mergedV4CIDRs, mergedV6CIDRs := CoalesceCIDRs(cidrs)
   285  	c.Assert(len(mergedV6CIDRs), Equals, 0)
   286  	s.testIPNetsEqual(mergedV4CIDRs, expected, c)
   287  
   288  	cidrs = []*net.IPNet{createIPNet("192.0.129.0", 24, int(ipv4BitLen)),
   289  		createIPNet("192.0.130.0", 24, int(ipv4BitLen))}
   290  	expected = []*net.IPNet{createIPNet("192.0.129.0", 24, int(ipv4BitLen)),
   291  		createIPNet("192.0.130.0", 24, int(ipv4BitLen))}
   292  	mergedV4CIDRs, mergedV6CIDRs = CoalesceCIDRs(cidrs)
   293  	c.Assert(len(mergedV6CIDRs), Equals, 0)
   294  	s.testIPNetsEqual(mergedV4CIDRs, expected, c)
   295  
   296  	cidrs = []*net.IPNet{createIPNet("192.0.2.112", 30, int(ipv4BitLen)),
   297  		createIPNet("192.0.2.116", 31, int(ipv4BitLen)),
   298  		createIPNet("192.0.2.118", 31, int(ipv4BitLen))}
   299  	expected = []*net.IPNet{createIPNet("192.0.2.112", 29, int(ipv4BitLen))}
   300  	mergedV4CIDRs, mergedV6CIDRs = CoalesceCIDRs(cidrs)
   301  	c.Assert(len(mergedV6CIDRs), Equals, 0)
   302  	s.testIPNetsEqual(mergedV4CIDRs, expected, c)
   303  
   304  	cidrs = []*net.IPNet{createIPNet("192.0.2.112", 30, int(ipv4BitLen)),
   305  		createIPNet("192.0.2.116", 32, int(ipv4BitLen)),
   306  		createIPNet("192.0.2.118", 31, int(ipv4BitLen))}
   307  	expected = []*net.IPNet{createIPNet("192.0.2.112", 30, int(ipv4BitLen)),
   308  		createIPNet("192.0.2.116", 32, int(ipv4BitLen)),
   309  		createIPNet("192.0.2.118", 31, int(ipv4BitLen))}
   310  	mergedV4CIDRs, mergedV6CIDRs = CoalesceCIDRs(cidrs)
   311  	c.Assert(len(mergedV6CIDRs), Equals, 0)
   312  	s.testIPNetsEqual(mergedV4CIDRs, expected, c)
   313  
   314  	cidrs = []*net.IPNet{createIPNet("192.0.2.112", 31, int(ipv4BitLen)),
   315  		createIPNet("192.0.2.116", 31, int(ipv4BitLen)),
   316  		createIPNet("192.0.2.118", 31, int(ipv4BitLen))}
   317  	expected = []*net.IPNet{createIPNet("192.0.2.112", 31, int(ipv4BitLen)),
   318  		createIPNet("192.0.2.116", 30, int(ipv4BitLen))}
   319  	mergedV4CIDRs, mergedV6CIDRs = CoalesceCIDRs(cidrs)
   320  	c.Assert(len(mergedV6CIDRs), Equals, 0)
   321  	s.testIPNetsEqual(mergedV4CIDRs, expected, c)
   322  
   323  	cidrs = []*net.IPNet{createIPNet("192.0.1.254", 31, int(ipv4BitLen)),
   324  		createIPNet("192.0.2.0", 28, int(ipv4BitLen)),
   325  		createIPNet("192.0.2.16", 28, int(ipv4BitLen)),
   326  		createIPNet("192.0.2.32", 28, int(ipv4BitLen)),
   327  		createIPNet("192.0.2.48", 28, int(ipv4BitLen)),
   328  		createIPNet("192.0.2.64", 28, int(ipv4BitLen)),
   329  		createIPNet("192.0.2.80", 28, int(ipv4BitLen)),
   330  		createIPNet("192.0.2.96", 28, int(ipv4BitLen)),
   331  		createIPNet("192.0.2.112", 28, int(ipv4BitLen)),
   332  		createIPNet("192.0.2.128", 28, int(ipv4BitLen)),
   333  		createIPNet("192.0.2.144", 28, int(ipv4BitLen)),
   334  		createIPNet("192.0.2.160", 28, int(ipv4BitLen)),
   335  		createIPNet("192.0.2.176", 28, int(ipv4BitLen)),
   336  		createIPNet("192.0.2.192", 28, int(ipv4BitLen)),
   337  		createIPNet("192.0.2.208", 28, int(ipv4BitLen)),
   338  		createIPNet("192.0.2.224", 28, int(ipv4BitLen)),
   339  		createIPNet("192.0.2.240", 28, int(ipv4BitLen)),
   340  		createIPNet("192.0.3.0", 28, int(ipv4BitLen)),
   341  	}
   342  
   343  	expected = []*net.IPNet{createIPNet("192.0.1.254", 31, int(ipv4BitLen)),
   344  		createIPNet("192.0.2.0", 24, int(ipv4BitLen)),
   345  		createIPNet("192.0.3.0", 28, int(ipv4BitLen))}
   346  	mergedV4CIDRs, mergedV6CIDRs = CoalesceCIDRs(cidrs)
   347  	c.Assert(len(mergedV6CIDRs), Equals, 0)
   348  	s.testIPNetsEqual(mergedV4CIDRs, expected, c)
   349  
   350  	cidrs = []*net.IPNet{createIPNet("::", 0, int(ipv6BitLen)),
   351  		createIPNet("fe80::1", 128, int(ipv6BitLen))}
   352  	expected = []*net.IPNet{createIPNet("::", 0, int(ipv6BitLen))}
   353  	_, mergedV6CIDRs = CoalesceCIDRs(cidrs)
   354  	s.testIPNetsEqual(mergedV6CIDRs, expected, c)
   355  
   356  	// assert cidr_merge(['::/0', '::192.0.2.0/124', 'ff00::101']) == [IPNetwork('::/0')]
   357  	cidrs = []*net.IPNet{createIPNet("::", 0, int(ipv6BitLen)),
   358  		createIPNet("::192.0.2.0", 124, int(ipv6BitLen)),
   359  		createIPNet("ff00::101", 128, int(ipv6BitLen))}
   360  	_, mergedV6CIDRs = CoalesceCIDRs(cidrs)
   361  	s.testIPNetsEqual(mergedV6CIDRs, expected, c)
   362  }
   363  
   364  func (s *IPTestSuite) TestRangeToCIDRs(c *C) {
   365  	// IPv4 worst case.
   366  	ipNets := rangeToCIDRs(net.ParseIP("0.0.0.1"), net.ParseIP("255.255.255.254"))
   367  	expected := []*net.IPNet{createIPNet("0.0.0.1", 32, int(ipv4BitLen)),
   368  		createIPNet("0.0.0.2", 31, int(ipv4BitLen)),
   369  		createIPNet("0.0.0.4", 30, int(ipv4BitLen)),
   370  		createIPNet("0.0.0.8", 29, int(ipv4BitLen)),
   371  		createIPNet("0.0.0.16", 28, int(ipv4BitLen)),
   372  		createIPNet("0.0.0.32", 27, int(ipv4BitLen)),
   373  		createIPNet("0.0.0.64", 26, int(ipv4BitLen)),
   374  		createIPNet("0.0.0.128", 25, int(ipv4BitLen)),
   375  		createIPNet("0.0.1.0", 24, int(ipv4BitLen)),
   376  		createIPNet("0.0.2.0", 23, int(ipv4BitLen)),
   377  		createIPNet("0.0.4.0", 22, int(ipv4BitLen)),
   378  		createIPNet("0.0.8.0", 21, int(ipv4BitLen)),
   379  		createIPNet("0.0.16.0", 20, int(ipv4BitLen)),
   380  		createIPNet("0.0.32.0", 19, int(ipv4BitLen)),
   381  		createIPNet("0.0.64.0", 18, int(ipv4BitLen)),
   382  		createIPNet("0.0.128.0", 17, int(ipv4BitLen)),
   383  		createIPNet("0.1.0.0", 16, int(ipv4BitLen)),
   384  		createIPNet("0.2.0.0", 15, int(ipv4BitLen)),
   385  		createIPNet("0.4.0.0", 14, int(ipv4BitLen)),
   386  		createIPNet("0.8.0.0", 13, int(ipv4BitLen)),
   387  		createIPNet("0.16.0.0", 12, int(ipv4BitLen)),
   388  		createIPNet("0.32.0.0", 11, int(ipv4BitLen)),
   389  		createIPNet("0.64.0.0", 10, int(ipv4BitLen)),
   390  		createIPNet("0.128.0.0", 9, int(ipv4BitLen)),
   391  		createIPNet("1.0.0.0", 8, int(ipv4BitLen)),
   392  		createIPNet("2.0.0.0", 7, int(ipv4BitLen)),
   393  		createIPNet("4.0.0.0", 6, int(ipv4BitLen)),
   394  		createIPNet("8.0.0.0", 5, int(ipv4BitLen)),
   395  		createIPNet("16.0.0.0", 4, int(ipv4BitLen)),
   396  		createIPNet("32.0.0.0", 3, int(ipv4BitLen)),
   397  		createIPNet("64.0.0.0", 2, int(ipv4BitLen)),
   398  		createIPNet("128.0.0.0", 2, int(ipv4BitLen)),
   399  		createIPNet("192.0.0.0", 3, int(ipv4BitLen)),
   400  		createIPNet("224.0.0.0", 4, int(ipv4BitLen)),
   401  		createIPNet("240.0.0.0", 5, int(ipv4BitLen)),
   402  		createIPNet("248.0.0.0", 6, int(ipv4BitLen)),
   403  		createIPNet("252.0.0.0", 7, int(ipv4BitLen)),
   404  		createIPNet("254.0.0.0", 8, int(ipv4BitLen)),
   405  		createIPNet("255.0.0.0", 9, int(ipv4BitLen)),
   406  		createIPNet("255.128.0.0", 10, int(ipv4BitLen)),
   407  		createIPNet("255.192.0.0", 11, int(ipv4BitLen)),
   408  		createIPNet("255.224.0.0", 12, int(ipv4BitLen)),
   409  		createIPNet("255.240.0.0", 13, int(ipv4BitLen)),
   410  		createIPNet("255.248.0.0", 14, int(ipv4BitLen)),
   411  		createIPNet("255.252.0.0", 15, int(ipv4BitLen)),
   412  		createIPNet("255.254.0.0", 16, int(ipv4BitLen)),
   413  		createIPNet("255.255.0.0", 17, int(ipv4BitLen)),
   414  		createIPNet("255.255.128.0", 18, int(ipv4BitLen)),
   415  		createIPNet("255.255.192.0", 19, int(ipv4BitLen)),
   416  		createIPNet("255.255.224.0", 20, int(ipv4BitLen)),
   417  		createIPNet("255.255.240.0", 21, int(ipv4BitLen)),
   418  		createIPNet("255.255.249.0", 22, int(ipv4BitLen)),
   419  		createIPNet("255.255.252.0", 23, int(ipv4BitLen)),
   420  		createIPNet("255.255.254.0", 24, int(ipv4BitLen)),
   421  		createIPNet("255.255.255.0", 25, int(ipv4BitLen)),
   422  		createIPNet("255.255.255.128", 26, int(ipv4BitLen)),
   423  		createIPNet("255.255.255.192", 27, int(ipv4BitLen)),
   424  		createIPNet("255.255.255.224", 28, int(ipv4BitLen)),
   425  		createIPNet("255.255.255.240", 29, int(ipv4BitLen)),
   426  		createIPNet("255.255.255.248", 30, int(ipv4BitLen)),
   427  		createIPNet("255.255.255.252", 31, int(ipv4BitLen)),
   428  		createIPNet("255.255.255.254", 32, int(ipv4BitLen)),
   429  	}
   430  
   431  	// Sort both so we can compare easily
   432  	sort.Sort(NetsByMask(expected))
   433  	sort.Sort(NetsByMask(ipNets))
   434  	c.Assert(len(ipNets), Equals, len(expected))
   435  }
   436  
   437  func (s *IPTestSuite) TestPreviousIP(c *C) {
   438  	ip := net.ParseIP("10.0.0.0")
   439  	expectedPrev := net.ParseIP("9.255.255.255")
   440  	prevIP := getPreviousIP(ip)
   441  	s.testIPsEqual(prevIP, expectedPrev, c)
   442  
   443  	// Check that underflow does not occur.
   444  	ip = net.ParseIP("0.0.0.0")
   445  	prevIP = getPreviousIP(ip)
   446  	expectedPrev = ip
   447  	s.testIPsEqual(prevIP, expectedPrev, c)
   448  
   449  	ip = net.ParseIP("::")
   450  	prevIP = getPreviousIP(ip)
   451  	expectedPrev = ip
   452  	s.testIPsEqual(prevIP, expectedPrev, c)
   453  
   454  	ip = net.ParseIP("10.0.0.1")
   455  	prevIP = getPreviousIP(ip)
   456  	expectedPrev = net.ParseIP("10.0.0.0")
   457  	s.testIPsEqual(prevIP, expectedPrev, c)
   458  
   459  	ip = net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
   460  	expectedPrev = net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe")
   461  	prevIP = getPreviousIP(ip)
   462  	s.testIPsEqual(prevIP, expectedPrev, c)
   463  }
   464  
   465  func (s *IPTestSuite) TestNextIP(c *C) {
   466  	expectedNext := net.ParseIP("10.0.0.0")
   467  	ip := net.ParseIP("9.255.255.255")
   468  	nextIP := GetNextIP(ip)
   469  	c.Assert(nextIP, checker.DeepEquals, expectedNext)
   470  
   471  	// Check that overflow does not occur.
   472  	ip = net.ParseIP("255.255.255.255")
   473  	nextIP = GetNextIP(ip)
   474  	expectedNext = ip
   475  	c.Assert(nextIP, checker.DeepEquals, expectedNext)
   476  
   477  	ip = net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
   478  	nextIP = GetNextIP(ip)
   479  	expectedNext = ip
   480  	c.Assert(nextIP, checker.DeepEquals, expectedNext)
   481  
   482  	ip = net.IP([]byte{0xa, 0, 0, 0})
   483  	nextIP = GetNextIP(ip)
   484  	expectedNext = net.IP([]byte{0xa, 0, 0, 1})
   485  	c.Assert(nextIP, checker.DeepEquals, expectedNext)
   486  
   487  	ip = net.IP([]byte{0xff, 0xff, 0xff, 0xff})
   488  	nextIP = GetNextIP(ip)
   489  	expectedNext = net.IP([]byte{0xff, 0xff, 0xff, 0xff})
   490  	c.Assert(nextIP, checker.DeepEquals, expectedNext)
   491  
   492  	ip = net.ParseIP("10.0.0.0")
   493  	nextIP = GetNextIP(ip)
   494  	expectedNext = net.ParseIP("10.0.0.1")
   495  	c.Assert(nextIP, checker.DeepEquals, expectedNext)
   496  
   497  	ip = net.ParseIP("0:0:0:0:ffff:ffff:ffff:ffff")
   498  	nextIP = GetNextIP(ip)
   499  	expectedNext = net.ParseIP("0:0:0:1:0:0:0:0")
   500  	c.Assert(nextIP, checker.DeepEquals, expectedNext)
   501  
   502  	ip = net.ParseIP("ffff:ffff:ffff:fffe:ffff:ffff:ffff:ffff")
   503  	nextIP = GetNextIP(ip)
   504  	expectedNext = net.ParseIP("ffff:ffff:ffff:ffff:0:0:0:0")
   505  	c.Assert(nextIP, checker.DeepEquals, expectedNext)
   506  
   507  	ip = net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe")
   508  	nextIP = GetNextIP(ip)
   509  	expectedNext = net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff")
   510  	c.Assert(nextIP, checker.DeepEquals, expectedNext)
   511  }
   512  
   513  func (s *IPTestSuite) TestCreateSpanningCIDR(c *C) {
   514  	netRange := createIPRange("10.0.0.0", "10.255.255.255")
   515  	expectedSpanningCIDR := createIPNet("10.0.0.0", 8, int(ipv4BitLen))
   516  	spanningCIDR := createSpanningCIDR(*netRange)
   517  	s.testIPNetsEqual([]*net.IPNet{expectedSpanningCIDR}, []*net.IPNet{&spanningCIDR}, c)
   518  
   519  	netRange = createIPRange("10.0.0.0", "10.255.255.254")
   520  	expectedSpanningCIDR = createIPNet("10.0.0.0", 8, int(ipv4BitLen))
   521  	spanningCIDR = createSpanningCIDR(*netRange)
   522  	s.testIPNetsEqual([]*net.IPNet{expectedSpanningCIDR}, []*net.IPNet{&spanningCIDR}, c)
   523  
   524  	netRange = createIPRange("10.0.0.1", "10.0.0.1")
   525  	expectedSpanningCIDR = createIPNet("10.0.0.1", 32, int(ipv4BitLen))
   526  	spanningCIDR = createSpanningCIDR(*netRange)
   527  	s.testIPNetsEqual([]*net.IPNet{expectedSpanningCIDR}, []*net.IPNet{&spanningCIDR}, c)
   528  
   529  	netRange = createIPRange("10.0.0.1", "10.0.0.2")
   530  	expectedSpanningCIDR = createIPNet("10.0.0.0", 30, int(ipv4BitLen))
   531  	spanningCIDR = createSpanningCIDR(*netRange)
   532  	s.testIPNetsEqual([]*net.IPNet{expectedSpanningCIDR}, []*net.IPNet{&spanningCIDR}, c)
   533  
   534  	netRange = createIPRange("9.0.0.0", "10.0.0.0")
   535  	expectedSpanningCIDR = createIPNet("8.0.0.0", 6, int(ipv4BitLen))
   536  	spanningCIDR = createSpanningCIDR(*netRange)
   537  	s.testIPNetsEqual([]*net.IPNet{expectedSpanningCIDR}, []*net.IPNet{&spanningCIDR}, c)
   538  
   539  	netRange = createIPRange("FD44:7089:FF32:712B:FF00:0000:0000:0000", "FD44:7089:FF32:712B:FFFF:FFFF:FFFF:FFFF")
   540  	expectedSpanningCIDR = createIPNet("fd44:7089:ff32:712b:ff00::", 72, int(ipv6BitLen))
   541  	spanningCIDR = createSpanningCIDR(*netRange)
   542  	s.testIPNetsEqual([]*net.IPNet{expectedSpanningCIDR}, []*net.IPNet{&spanningCIDR}, c)
   543  
   544  }
   545  
   546  func (s *IPTestSuite) TestPartitionCIDR(c *C) {
   547  	targetCIDR := createIPNet("10.0.0.0", 8, int(ipv4BitLen))
   548  	excludeCIDR := createIPNet("10.255.255.255", 32, int(ipv4BitLen))
   549  	left, exclude, right := partitionCIDR(*targetCIDR, *excludeCIDR)
   550  	// Exclude should just contain exclude CIDR
   551  	s.testIPNetsEqual([]*net.IPNet{excludeCIDR}, exclude, c)
   552  	// Nothing should be in right list.
   553  	c.Assert(len(right), Equals, 0)
   554  	expectedLeft := []*net.IPNet{createIPNet("10.0.0.0", 9, int(ipv4BitLen)),
   555  		createIPNet("10.128.0.0", 10, int(ipv4BitLen)),
   556  		createIPNet("10.192.0.0", 11, int(ipv4BitLen)),
   557  		createIPNet("10.224.0.0", 12, int(ipv4BitLen)),
   558  		createIPNet("10.240.0.0", 13, int(ipv4BitLen)),
   559  		createIPNet("10.248.0.0", 14, int(ipv4BitLen)),
   560  		createIPNet("10.252.0.0", 15, int(ipv4BitLen)),
   561  		createIPNet("10.254.0.0", 16, int(ipv4BitLen)),
   562  		createIPNet("10.255.0.0", 17, int(ipv4BitLen)),
   563  		createIPNet("10.255.128.0", 18, int(ipv4BitLen)),
   564  		createIPNet("10.255.192.0", 19, int(ipv4BitLen)),
   565  		createIPNet("10.255.224.0", 20, int(ipv4BitLen)),
   566  		createIPNet("10.255.240.0", 21, int(ipv4BitLen)),
   567  		createIPNet("10.255.248.0", 22, int(ipv4BitLen)),
   568  		createIPNet("10.255.252.0", 23, int(ipv4BitLen)),
   569  		createIPNet("10.255.254.0", 24, int(ipv4BitLen)),
   570  		createIPNet("10.255.255.0", 25, int(ipv4BitLen)),
   571  		createIPNet("10.255.255.128", 26, int(ipv4BitLen)),
   572  		createIPNet("10.255.255.192", 27, int(ipv4BitLen)),
   573  		createIPNet("10.255.255.224", 28, int(ipv4BitLen)),
   574  		createIPNet("10.255.255.240", 29, int(ipv4BitLen)),
   575  		createIPNet("10.255.255.248", 30, int(ipv4BitLen)),
   576  		createIPNet("10.255.255.252", 31, int(ipv4BitLen)),
   577  		createIPNet("10.255.255.254", 32, int(ipv4BitLen)),
   578  	}
   579  	s.testIPNetsEqual(expectedLeft, left, c)
   580  
   581  	targetCIDR = createIPNet("10.0.0.0", 8, int(ipv4BitLen))
   582  	excludeCIDR = createIPNet("10.0.0.0", 32, int(ipv4BitLen))
   583  	left, exclude, right = partitionCIDR(*targetCIDR, *excludeCIDR)
   584  	// Exclude should just contain exclude CIDR
   585  	s.testIPNetsEqual([]*net.IPNet{excludeCIDR}, exclude, c)
   586  	// Nothing should be in left list.
   587  	c.Assert(len(left), Equals, 0)
   588  	expectedRight := []*net.IPNet{createIPNet("10.128.0.0", 9, int(ipv4BitLen)),
   589  		createIPNet("10.64.0.0", 10, int(ipv4BitLen)),
   590  		createIPNet("10.32.0.0", 11, int(ipv4BitLen)),
   591  		createIPNet("10.16.0.0", 12, int(ipv4BitLen)),
   592  		createIPNet("10.8.0.0", 13, int(ipv4BitLen)),
   593  		createIPNet("10.4.0.0", 14, int(ipv4BitLen)),
   594  		createIPNet("10.2.0.0", 15, int(ipv4BitLen)),
   595  		createIPNet("10.1.0.0", 16, int(ipv4BitLen)),
   596  		createIPNet("10.0.128.0", 17, int(ipv4BitLen)),
   597  		createIPNet("10.0.64.0", 18, int(ipv4BitLen)),
   598  		createIPNet("10.0.32.0", 19, int(ipv4BitLen)),
   599  		createIPNet("10.0.16.0", 20, int(ipv4BitLen)),
   600  		createIPNet("10.0.8.0", 21, int(ipv4BitLen)),
   601  		createIPNet("10.0.4.0", 22, int(ipv4BitLen)),
   602  		createIPNet("10.0.2.0", 23, int(ipv4BitLen)),
   603  		createIPNet("10.0.1.0", 24, int(ipv4BitLen)),
   604  		createIPNet("10.0.0.128", 25, int(ipv4BitLen)),
   605  		createIPNet("10.0.0.64", 26, int(ipv4BitLen)),
   606  		createIPNet("10.0.0.32", 27, int(ipv4BitLen)),
   607  		createIPNet("10.0.0.16", 28, int(ipv4BitLen)),
   608  		createIPNet("10.0.0.8", 29, int(ipv4BitLen)),
   609  		createIPNet("10.0.0.4", 30, int(ipv4BitLen)),
   610  		createIPNet("10.0.0.2", 31, int(ipv4BitLen)),
   611  		createIPNet("10.0.0.1", 32, int(ipv4BitLen)),
   612  	}
   613  	s.testIPNetsEqual(expectedRight, right, c)
   614  
   615  	// exclude is not in target CIDR and is to left.
   616  	targetCIDR = createIPNet("10.0.0.0", 8, int(ipv4BitLen))
   617  	excludeCIDR = createIPNet("9.0.0.255", 32, int(ipv4BitLen))
   618  	left, exclude, right = partitionCIDR(*targetCIDR, *excludeCIDR)
   619  	c.Assert(len(left), Equals, 0)
   620  	c.Assert(len(exclude), Equals, 0)
   621  	s.testIPNetsEqual([]*net.IPNet{targetCIDR}, right, c)
   622  
   623  	// exclude is not in target CIDR and is to right.
   624  	targetCIDR = createIPNet("10.255.255.254", 32, int(ipv4BitLen))
   625  	excludeCIDR = createIPNet("10.255.255.255", 32, int(ipv4BitLen))
   626  	left, exclude, right = partitionCIDR(*targetCIDR, *excludeCIDR)
   627  	c.Assert(len(right), Equals, 0)
   628  	c.Assert(len(exclude), Equals, 0)
   629  	s.testIPNetsEqual([]*net.IPNet{targetCIDR}, left, c)
   630  
   631  	// exclude CIDR larger than target CIDR
   632  	targetCIDR = createIPNet("10.96.0.0", 12, int(ipv4BitLen))
   633  	excludeCIDR = createIPNet("10.0.0.0", 8, int(ipv4BitLen))
   634  	left, exclude, right = partitionCIDR(*targetCIDR, *excludeCIDR)
   635  	c.Assert(len(left), Equals, 0)
   636  	c.Assert(len(right), Equals, 0)
   637  	s.testIPNetsEqual([]*net.IPNet{targetCIDR}, exclude, c)
   638  
   639  	targetCIDR = createIPNet("fd44:7089:ff32:712b:ff00::", 64, int(ipv6BitLen))
   640  	excludeCIDR = createIPNet("fd44:7089:ff32:712b::", 66, int(ipv6BitLen))
   641  
   642  	_, exclude, right = partitionCIDR(*targetCIDR, *excludeCIDR)
   643  
   644  	expectedCIDRs := []*net.IPNet{createIPNet("fd44:7089:ff32:712b:8000::", 65, int(ipv6BitLen)),
   645  		createIPNet("fd44:7089:ff32:712b:4000::", 66, int(ipv6BitLen))}
   646  	s.testIPNetsEqual(expectedCIDRs, right, c)
   647  	s.testIPNetsEqual([]*net.IPNet{excludeCIDR}, exclude, c)
   648  }
   649  
   650  // TestKeepUniqueIPs tests that KeepUniqueIPs returns a slice with only the unique IPs
   651  func (s *IPTestSuite) TestKeepUniqueIPs(c *C) {
   652  	// test nil/empty handling
   653  	ips := KeepUniqueIPs(nil)
   654  	c.Assert(len(ips), Equals, 0, Commentf("Non-empty slice returned with empty input"))
   655  
   656  	// test all duplicate
   657  	ips = KeepUniqueIPs([]net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("1.1.1.1"), net.ParseIP("1.1.1.1")})
   658  	c.Assert(len(ips), Equals, 1, Commentf("Too many IPs returned for only 1 unique"))
   659  	c.Assert(ips[0].String(), Equals, "1.1.1.1", Commentf("Incorrect unique IP returned"))
   660  
   661  	// test all unique
   662  	ipSource := []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("2.2.2.2"), net.ParseIP("3.3.3.3")}
   663  	ips = KeepUniqueIPs(ipSource)
   664  	c.Assert(len(ips), Equals, 3, Commentf("Too few IPs returned for only 3 uniques"))
   665  	for i := range ipSource {
   666  		c.Assert(ips[i].String(), Equals, ipSource[i].String(), Commentf("Incorrect unique IP returned"))
   667  	}
   668  
   669  	// test mixed
   670  	ipSource = []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("2.2.2.2"), net.ParseIP("3.3.3.3"), net.ParseIP("2.2.2.2")}
   671  	ips = KeepUniqueIPs(ipSource)
   672  	c.Assert(len(ips), Equals, 3, Commentf("Too few IPs returned for only 3 uniques"))
   673  	for i := range ipSource[:3] {
   674  		c.Assert(ips[i].String(), Equals, ipSource[i].String(), Commentf("Incorrect unique IP returned"))
   675  	}
   676  }
   677  
   678  // Note: each "op" works on size things
   679  func (s *IPTestSuite) BenchmarkKeepUniqueIPs(c *C) {
   680  	size := 1000
   681  	ipsOrig := make([]net.IP, 0, size)
   682  	for i := 0; i < size; i++ {
   683  		ipsOrig = append(ipsOrig, net.IPv4(byte(i>>24), byte(i>>16), byte(i>>8), byte(i>>0)))
   684  	}
   685  	ips := make([]net.IP, 0, len(ipsOrig))
   686  
   687  	copy(ips, ipsOrig)
   688  	for i := 0; i < c.N; i++ {
   689  		c.StopTimer()
   690  		rand.Shuffle(len(ips), func(i, j int) {
   691  			ips[i], ips[j] = ips[j], ips[i]
   692  		})
   693  		c.StartTimer()
   694  
   695  		KeepUniqueIPs(ips)
   696  	}
   697  }