github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/policy/api/utils_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 api
    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 PolicyAPITestSuite struct{}
    31  
    32  var _ = Suite(&PolicyAPITestSuite{})
    33  
    34  func (s *PolicyAPITestSuite) TestHTTPEqual(c *C) {
    35  	rule1 := PortRuleHTTP{Path: "/foo$", Method: "GET", Headers: []string{"X-Test: Foo"}}
    36  	rule2 := PortRuleHTTP{Path: "/bar$", Method: "GET", Headers: []string{"X-Test: Foo"}}
    37  	rule3 := PortRuleHTTP{Path: "/foo$", Method: "GET", Headers: []string{"X-Test: Bar"}}
    38  
    39  	c.Assert(rule1.Equal(rule1), Equals, true)
    40  	c.Assert(rule1.Equal(rule2), Equals, false)
    41  	c.Assert(rule1.Equal(rule3), Equals, false)
    42  
    43  	rules := L7Rules{
    44  		HTTP: []PortRuleHTTP{rule1, rule2},
    45  	}
    46  
    47  	c.Assert(rule1.Exists(rules), Equals, true)
    48  	c.Assert(rule2.Exists(rules), Equals, true)
    49  	c.Assert(rule3.Exists(rules), Equals, false)
    50  }
    51  
    52  func (s *PolicyAPITestSuite) TestKafkaEqual(c *C) {
    53  	rule1 := PortRuleKafka{APIVersion: "1", APIKey: "foo", Topic: "topic1"}
    54  	rule2 := PortRuleKafka{APIVersion: "1", APIKey: "bar", Topic: "topic1"}
    55  	rule3 := PortRuleKafka{APIVersion: "1", APIKey: "foo", Topic: "topic2"}
    56  
    57  	c.Assert(rule1.Equal(rule1), Equals, true)
    58  	c.Assert(rule1.Equal(rule2), Equals, false)
    59  	c.Assert(rule1.Equal(rule3), Equals, false)
    60  
    61  	rules := L7Rules{
    62  		Kafka: []PortRuleKafka{rule1, rule2},
    63  	}
    64  
    65  	c.Assert(rule1.Exists(rules), Equals, true)
    66  	c.Assert(rule2.Exists(rules), Equals, true)
    67  	c.Assert(rule3.Exists(rules), Equals, false)
    68  }
    69  
    70  func (s *PolicyAPITestSuite) TestL7Equal(c *C) {
    71  	rule1 := PortRuleL7{"Path": "/foo$", "Method": "GET"}
    72  	rule2 := PortRuleL7{"Path": "/bar$", "Method": "GET"}
    73  	rule3 := PortRuleL7{"Path": "/foo$", "Method": "GET", "extra": ""}
    74  
    75  	c.Assert(rule1.Equal(rule1), Equals, true)
    76  	c.Assert(rule2.Equal(rule2), Equals, true)
    77  	c.Assert(rule3.Equal(rule3), Equals, true)
    78  	c.Assert(rule1.Equal(rule2), Equals, false)
    79  	c.Assert(rule2.Equal(rule1), Equals, false)
    80  	c.Assert(rule1.Equal(rule3), Equals, false)
    81  	c.Assert(rule3.Equal(rule1), Equals, false)
    82  	c.Assert(rule2.Equal(rule3), Equals, false)
    83  	c.Assert(rule3.Equal(rule2), Equals, false)
    84  
    85  	rules := L7Rules{
    86  		L7Proto: "testing",
    87  		L7:      []PortRuleL7{rule1, rule2},
    88  	}
    89  
    90  	c.Assert(rule1.Exists(rules), Equals, true)
    91  	c.Assert(rule2.Exists(rules), Equals, true)
    92  	c.Assert(rule3.Exists(rules), Equals, false)
    93  }
    94  
    95  func (s *PolicyAPITestSuite) TestValidateL4Proto(c *C) {
    96  	c.Assert(L4Proto("TCP").Validate(), IsNil)
    97  	c.Assert(L4Proto("UDP").Validate(), IsNil)
    98  	c.Assert(L4Proto("ANY").Validate(), IsNil)
    99  	c.Assert(L4Proto("TCP2").Validate(), Not(IsNil))
   100  	c.Assert(L4Proto("t").Validate(), Not(IsNil))
   101  }
   102  
   103  func (s *PolicyAPITestSuite) TestParseL4Proto(c *C) {
   104  	p, err := ParseL4Proto("tcp")
   105  	c.Assert(p, Equals, ProtoTCP)
   106  	c.Assert(err, IsNil)
   107  
   108  	p, err = ParseL4Proto("Any")
   109  	c.Assert(p, Equals, ProtoAny)
   110  	c.Assert(err, IsNil)
   111  
   112  	p, err = ParseL4Proto("")
   113  	c.Assert(p, Equals, ProtoAny)
   114  	c.Assert(err, IsNil)
   115  
   116  	_, err = ParseL4Proto("foo2")
   117  	c.Assert(err, Not(IsNil))
   118  }