github.com/fafucoder/cilium@v1.6.11/common/addressing/ip_test.go (about)

     1  // Copyright 2016-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 addressing
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "gopkg.in/check.v1"
    23  )
    24  
    25  // Hook up gocheck into the "go test" runner.
    26  func Test(t *testing.T) {
    27  	TestingT(t)
    28  }
    29  
    30  type AddressingSuite struct{}
    31  
    32  var _ = Suite(&AddressingSuite{})
    33  
    34  func (s *AddressingSuite) TestCiliumIPv6(c *C) {
    35  	ip, err := NewCiliumIPv6("b007::")
    36  	c.Assert(err, IsNil)
    37  	ip2, _ := NewCiliumIPv6("")
    38  	c.Assert(ip2.IsSet(), Equals, false)
    39  	// Lacking a better Equals method, checking if the stringified IP is consistent
    40  	c.Assert(ip.String() == ip2.String(), Equals, false)
    41  
    42  	ip, err = NewCiliumIPv6("b007::aaaa:bbbb:0:0")
    43  	c.Assert(err, IsNil)
    44  	c.Assert(ip.String(), Equals, "b007::aaaa:bbbb:0:0")
    45  	c.Assert(ip.IsSet(), Equals, true)
    46  }
    47  
    48  func (s *AddressingSuite) TestCiliumIPv4(c *C) {
    49  	ip, err := NewCiliumIPv4("10.1.0.0")
    50  	c.Assert(err, IsNil)
    51  	c.Assert(ip.IsSet(), Equals, true)
    52  
    53  	ip2, _ := NewCiliumIPv4("")
    54  	c.Assert(ip2.IsSet(), Equals, false)
    55  	// Lacking a better Equals method, checking if the stringified IP is consistent
    56  	c.Assert(ip.String() == ip2.String(), Equals, false)
    57  
    58  	ip, err = NewCiliumIPv4("b007::")
    59  	c.Assert(err, Not(Equals), nil)
    60  }
    61  
    62  func (s *AddressingSuite) TestCiliumIPv6Negative(c *C) {
    63  	ip, err := NewCiliumIPv6("")
    64  	c.Assert(err, NotNil)
    65  	c.Assert(ip, IsNil)
    66  	c.Assert(ip.String(), Equals, "")
    67  
    68  	ip, err = NewCiliumIPv6("192.168.0.1")
    69  	c.Assert(err, NotNil)
    70  	c.Assert(ip, IsNil)
    71  }
    72  
    73  func (s *AddressingSuite) TestCiliumIPv4Negative(c *C) {
    74  	ip, err := NewCiliumIPv4("")
    75  	c.Assert(err, NotNil)
    76  	c.Assert(ip, IsNil)
    77  	c.Assert(ip.String(), Equals, "")
    78  }