github.com/fafucoder/cilium@v1.6.11/pkg/cidr/cidr_test.go (about)

     1  // Copyright 2019 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 cidr
    18  
    19  import (
    20  	"net"
    21  	"testing"
    22  
    23  	"github.com/cilium/cilium/pkg/checker"
    24  
    25  	"gopkg.in/check.v1"
    26  )
    27  
    28  // Hook up gocheck into the "go test" runner.
    29  type CidrTestSuite struct{}
    30  
    31  var _ = check.Suite(&CidrTestSuite{})
    32  
    33  func Test(t *testing.T) {
    34  	check.TestingT(t)
    35  }
    36  
    37  func (t *CidrTestSuite) TestNilDeepCopy(c *check.C) {
    38  	var c1 *CIDR
    39  	c.Assert(c1.DeepCopy(), check.IsNil)
    40  }
    41  
    42  func (t *CidrTestSuite) TestDeepCopy(c *check.C) {
    43  	_, ipnet, err := net.ParseCIDR("1.1.1.1/8")
    44  	c.Assert(err, check.IsNil)
    45  	c1 := NewCIDR(ipnet)
    46  	c.Assert(c1, check.Not(check.IsNil))
    47  
    48  	c2 := c1.DeepCopy()
    49  	c.Assert(c1, checker.DeepEquals, c2)
    50  }
    51  
    52  func (t *CidrTestSuite) TestNewCIDRNil(c *check.C) {
    53  	c.Assert(NewCIDR(nil), check.IsNil)
    54  }
    55  
    56  func (t *CidrTestSuite) TestIllegalParseCIDR(c *check.C) {
    57  	c1, err := ParseCIDR("Illegal")
    58  	c.Assert(c1, check.IsNil)
    59  	c.Assert(err, check.Not(check.IsNil))
    60  }
    61  
    62  func (t *CidrTestSuite) TestIllegalMustParseCIDR(c *check.C) {
    63  	defer func() {
    64  		if r := recover(); r == nil {
    65  			c.Errorf("MustParseCIDR did not panic on illegal CIDR")
    66  		}
    67  	}()
    68  	c1 := MustParseCIDR("Illegal")
    69  	c.Assert(c1, check.IsNil)
    70  }