github.com/kubearmor/cilium@v1.6.12/pkg/alignchecker/alignchecker_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 alignchecker 18 19 import ( 20 "reflect" 21 "testing" 22 23 . "gopkg.in/check.v1" 24 ) 25 26 type AlignCheckerSuite struct{} 27 28 var _ = Suite(&AlignCheckerSuite{}) 29 30 func Test(t *testing.T) { 31 TestingT(t) 32 } 33 34 const path = "testdata/bpf_foo.o" 35 36 type foo struct { 37 ipv6 [4]uint32 `align:"$union0"` 38 misc uint32 `align:"$union1"` 39 f uint8 `align:"family"` 40 pad4 uint8 `align:"pad4"` 41 pad5 uint16 `align:"pad5"` 42 } 43 44 type foo2 struct { 45 foo 46 } 47 48 type fooInvalidSize struct { 49 ipv6 uint32 50 } 51 52 type fooInvalidOffset struct { 53 ipv6 [4]uint32 `align:"$union0"` 54 misc uint32 `align:"$union1"` 55 f uint16 `align:"family"` 56 pad4 uint8 `align:"pad4"` 57 pad5 uint8 `align:"pad5"` 58 } 59 60 type toCheck map[string][]reflect.Type 61 62 func (t *AlignCheckerSuite) TestCheckStructAlignments(c *C) { 63 testCases := []struct { 64 cName string 65 goTypes []reflect.Type 66 err string 67 }{ 68 { 69 "foo", 70 []reflect.Type{ 71 reflect.TypeOf(foo{}), 72 }, 73 "", 74 }, 75 { 76 "foo", 77 []reflect.Type{ 78 reflect.TypeOf(foo2{}), 79 }, 80 "", 81 }, 82 { 83 "foo", 84 []reflect.Type{ 85 reflect.TypeOf(foo{}), 86 reflect.TypeOf(foo2{}), 87 }, 88 "", 89 }, 90 { 91 "foo", 92 []reflect.Type{ 93 reflect.TypeOf(fooInvalidSize{}), 94 }, 95 `*.fooInvalidSize\(4\) size does not match foo\(24\)`, 96 }, 97 { 98 "foo", 99 []reflect.Type{ 100 reflect.TypeOf(fooInvalidOffset{}), 101 }, 102 `*.fooInvalidOffset.pad4 offset\(22\) does not match foo.pad4\(21\)`, 103 }, 104 { 105 "bar", 106 []reflect.Type{ 107 reflect.TypeOf(foo{}), 108 }, 109 "could not find C struct bar", 110 }, 111 } 112 113 for _, tt := range testCases { 114 err := CheckStructAlignments(path, toCheck{tt.cName: tt.goTypes}) 115 if tt.err == "" { 116 c.Assert(err, IsNil) 117 } else { 118 c.Assert(err, ErrorMatches, tt.err) 119 } 120 } 121 }