github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/maps/policymap/policymap_privileged_test.go (about) 1 // Copyright 2018-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 policymap 18 19 import ( 20 "fmt" 21 "os" 22 "syscall" 23 "testing" 24 25 "github.com/cilium/cilium/pkg/bpf" 26 "github.com/cilium/cilium/pkg/checker" 27 "github.com/cilium/cilium/pkg/policy/trafficdirection" 28 "github.com/cilium/cilium/pkg/u8proto" 29 30 . "gopkg.in/check.v1" 31 ) 32 33 func Test(t *testing.T) { 34 TestingT(t) 35 } 36 37 type PolicyMapTestSuite struct{} 38 39 var ( 40 _ = Suite(&PolicyMapTestSuite{}) 41 42 testMap = newMap("cilium_policy_test") 43 ) 44 45 func runTests(m *testing.M) (int, error) { 46 bpf.CheckOrMountFS("") 47 if err := bpf.ConfigureResourceLimits(); err != nil { 48 return 1, fmt.Errorf("Failed to configure rlimit") 49 } 50 51 _ = os.RemoveAll(bpf.MapPath("cilium_policy_test")) 52 _, err := testMap.OpenOrCreate() 53 if err != nil { 54 return 1, fmt.Errorf("Failed to create map") 55 } 56 defer func() { 57 path, _ := testMap.Path() 58 os.Remove(path) 59 }() 60 defer testMap.Close() 61 62 return m.Run(), nil 63 } 64 65 func TestMain(m *testing.M) { 66 exitCode, err := runTests(m) 67 if err != nil { 68 log.Fatal(err) 69 } 70 os.Exit(exitCode) 71 } 72 73 func (pm *PolicyMapTestSuite) TestPolicyMapDumpToSlice(c *C) { 74 c.Assert(testMap, NotNil) 75 76 fooEntry := newKey(1, 1, 1, 1) 77 err := testMap.AllowKey(fooEntry, 0) 78 c.Assert(err, IsNil) 79 80 dump, err := testMap.DumpToSlice() 81 c.Assert(err, IsNil) 82 c.Assert(len(dump), Equals, 1) 83 84 // FIXME: It's weird that AllowKey() does the implicit byteorder 85 // conversion above. But not really a bug, so work around it. 86 fooEntry = fooEntry.ToNetwork() 87 c.Assert(dump[0].Key, checker.DeepEquals, fooEntry) 88 89 // Special case: allow-all entry 90 barEntry := newKey(0, 0, 0, 0) 91 err = testMap.AllowKey(barEntry, 0) 92 c.Assert(err, IsNil) 93 94 dump, err = testMap.DumpToSlice() 95 c.Assert(err, IsNil) 96 c.Assert(len(dump), Equals, 2) 97 } 98 99 func (pm *PolicyMapTestSuite) TestDeleteNonexistentKey(c *C) { 100 key := newKey(27, 80, u8proto.ANY, trafficdirection.Ingress) 101 err, errno := testMap.Map.DeleteWithErrno(&key) 102 c.Assert(err, Not(IsNil)) 103 c.Assert(errno, Equals, syscall.ENOENT) 104 }