github.com/elfadel/cilium@v1.6.12/pkg/datapath/linux/utils_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 linux 18 19 import ( 20 . "gopkg.in/check.v1" 21 ) 22 23 type DatapathSuite struct{} 24 25 var ( 26 _ = Suite(&DatapathSuite{}) 27 ) 28 29 type formatTestCase struct { 30 input []byte 31 output string 32 } 33 34 func (s *DatapathSuite) TestGoArray2C(c *C) { 35 tests := []formatTestCase{ 36 { 37 input: []byte{0, 0x01, 0x02, 0x03}, 38 output: "0x0, 0x1, 0x2, 0x3", 39 }, 40 { 41 input: []byte{0, 0xFF, 0xFF, 0xFF}, 42 output: "0x0, 0xff, 0xff, 0xff", 43 }, 44 { 45 input: []byte{0xa, 0xbc, 0xde, 0xf1}, 46 output: "0xa, 0xbc, 0xde, 0xf1", 47 }, 48 { 49 input: []byte{0}, 50 output: "0x0", 51 }, 52 { 53 input: []byte{}, 54 output: "", 55 }, 56 } 57 58 for _, test := range tests { 59 c.Assert(goArray2C(test.input), Equals, test.output) 60 } 61 } 62 63 func (s *DatapathSuite) TestdefineIPv6(c *C) { 64 tests := []formatTestCase{ 65 { 66 input: nil, 67 output: "/* BUG: bad ip define foo */\n", 68 }, 69 { 70 input: []byte{}, 71 output: "/* BUG: bad ip define foo */\n", 72 }, 73 { 74 input: []byte{1, 2, 3}, 75 output: "/* BUG: bad ip define foo 0x1, 0x2, 0x3 */\n", 76 }, 77 { 78 input: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}, 79 output: "DEFINE_IPV6(foo, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10);\n", 80 }, 81 } 82 83 for _, test := range tests { 84 c.Assert(defineIPv6("foo", test.input), Equals, test.output) 85 } 86 } 87 88 func (s *DatapathSuite) TestdefineMAC(c *C) { 89 tests := []formatTestCase{ 90 { 91 input: nil, 92 output: "/* BUG: bad mac define foo */\n", 93 }, 94 { 95 input: []byte{}, 96 output: "/* BUG: bad mac define foo */\n", 97 }, 98 { 99 input: []byte{1, 2, 3}, 100 output: "/* BUG: bad mac define foo 0x1, 0x2, 0x3 */\n", 101 }, 102 { 103 input: []byte{1, 2, 3, 4, 5, 6}, 104 output: "DEFINE_MAC(foo, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6);\n" + 105 "#define foo fetch_mac(foo)\n", 106 }, 107 } 108 for _, test := range tests { 109 c.Assert(defineMAC("foo", test.input), Equals, test.output) 110 } 111 }