github.com/imran-kn/cilium-fork@v1.6.9/pkg/policy/api/cidr_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 api 18 19 import ( 20 "github.com/cilium/cilium/pkg/checker" 21 "github.com/cilium/cilium/pkg/labels" 22 cidrpkg "github.com/cilium/cilium/pkg/labels/cidr" 23 24 . "gopkg.in/check.v1" 25 ) 26 27 func (s *PolicyAPITestSuite) TestCIDRMatchesAll(c *C) { 28 cidr := CIDR("0.0.0.0/0") 29 c.Assert(cidr.MatchesAll(), Equals, true) 30 31 cidr = CIDR("::/0") 32 c.Assert(cidr.MatchesAll(), Equals, true) 33 34 cidr = CIDR("192.0.2.0/24") 35 c.Assert(cidr.MatchesAll(), Equals, false) 36 cidr = CIDR("192.0.2.3/32") 37 c.Assert(cidr.MatchesAll(), Equals, false) 38 } 39 40 func (s *PolicyAPITestSuite) TestGetAsEndpointSelectors(c *C) { 41 world := labels.ParseLabelArray("reserved:world") 42 43 labelWorld := labels.ParseSelectLabel("reserved:world") 44 esWorld := NewESFromLabels(labelWorld) 45 46 labelAllV4, err := cidrpkg.IPStringToLabel("0.0.0.0/0") 47 c.Assert(err, IsNil) 48 v4World := NewESFromLabels(labelAllV4) 49 50 labelAllV6, err := cidrpkg.IPStringToLabel("::/0") 51 c.Assert(err, IsNil) 52 v6World := NewESFromLabels(labelAllV6) 53 54 labelOtherCIDR, err := cidrpkg.IPStringToLabel("192.168.128.0/24") 55 c.Assert(err, IsNil) 56 esOtherCIDR := NewESFromLabels(labelOtherCIDR) 57 58 cidrs := CIDRSlice{ 59 "0.0.0.0/0", 60 } 61 62 expectedSelectors := EndpointSelectorSlice{ 63 esWorld, 64 v4World, 65 } 66 result := cidrs.GetAsEndpointSelectors() 67 c.Assert(result.Matches(world), Equals, true) 68 c.Assert(result, checker.DeepEquals, expectedSelectors) 69 70 cidrs = CIDRSlice{ 71 "::/0", 72 } 73 expectedSelectors = EndpointSelectorSlice{ 74 esWorld, 75 v6World, 76 } 77 result = cidrs.GetAsEndpointSelectors() 78 c.Assert(result.Matches(world), Equals, true) 79 c.Assert(result, checker.DeepEquals, expectedSelectors) 80 81 cidrs = CIDRSlice{ 82 "0.0.0.0/0", 83 "::/0", 84 "192.168.128.10/24", 85 } 86 expectedSelectors = EndpointSelectorSlice{ 87 esWorld, 88 v4World, 89 v6World, 90 esOtherCIDR, 91 } 92 result = cidrs.GetAsEndpointSelectors() 93 c.Assert(result.Matches(world), Equals, true) 94 c.Assert(result, checker.DeepEquals, expectedSelectors) 95 }