github.com/zhyoulun/cilium@v1.6.12/pkg/policy/l3_test.go (about)

     1  // Copyright 2018 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 policy
    18  
    19  import (
    20  	"net"
    21  
    22  	"github.com/cilium/cilium/pkg/checker"
    23  	"github.com/cilium/cilium/pkg/cidr"
    24  	"github.com/cilium/cilium/pkg/labels"
    25  	"github.com/cilium/cilium/pkg/node"
    26  
    27  	. "gopkg.in/check.v1"
    28  )
    29  
    30  func (ds *PolicyTestSuite) SetUpTest(c *C) {
    31  	_, v6node, err := net.ParseCIDR("2001:DB8::/96")
    32  	c.Assert(err, IsNil)
    33  	v4node := cidr.MustParseCIDR("192.0.2.3/24")
    34  	err = node.SetIPv6NodeRange(v6node)
    35  	c.Assert(err, IsNil)
    36  	node.SetIPv4AllocRange(v4node)
    37  }
    38  
    39  func (ds *PolicyTestSuite) TearDownTest(c *C) {
    40  	node.Uninitialize()
    41  }
    42  
    43  func populateMap(m *CIDRPolicyMap) {
    44  	lbls := labels.LabelArray{}
    45  	m.Insert("10.1.1.0/24", lbls)
    46  	m.Insert("10.2.0.0/20", lbls)
    47  	m.Insert("10.3.3.3/32", lbls)
    48  	m.Insert("10.4.4.0/26", lbls)
    49  	m.Insert("10.5.0.0/16", lbls)
    50  }
    51  
    52  func (ds *PolicyTestSuite) TestToBPFData(c *C) {
    53  	cidrPolicy := NewCIDRPolicy()
    54  
    55  	populateMap(&cidrPolicy.Ingress)
    56  	_, s4 := cidrPolicy.ToBPFData()
    57  	exp := []int{32, 26, 24, 20, 16, 0}
    58  	c.Assert(s4, checker.DeepEquals, exp)
    59  
    60  	cidrPolicy = NewCIDRPolicy()
    61  	// 32 represent the host and 0 the world prefixes.
    62  	populateMap(&cidrPolicy.Egress)
    63  	_, s4 = cidrPolicy.ToBPFData()
    64  	exp = []int{32, 26, 24, 20, 16, 0}
    65  	c.Assert(s4, checker.DeepEquals, exp)
    66  }
    67  
    68  func (ds *PolicyTestSuite) TestGetDefaultPrefixLengths(c *C) {
    69  	expected6 := []int{128, 0}
    70  	expected4 := []int{32, 0}
    71  	s6, s4 := GetDefaultPrefixLengths()
    72  
    73  	c.Assert(s6, checker.DeepEquals, expected6)
    74  	c.Assert(s4, checker.DeepEquals, expected4)
    75  }