github.com/imran-kn/cilium-fork@v1.6.9/pkg/bpf/binary/binary_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 // Copyright 2009 The Go Authors. All rights reserved. 16 // Use of this source code is governed by a BSD-style 17 // license that can be found in the LICENSE file. 18 19 // +build !privileged_tests 20 21 package binary 22 23 import ( 24 "encoding/binary" 25 "math" 26 "reflect" 27 "testing" 28 ) 29 30 type Struct struct { 31 Int8 int8 32 Int16 int16 33 Int32 int32 34 Int64 int64 35 Uint8 uint8 36 Uint16 uint16 37 Uint32 uint32 38 Uint64 uint64 39 Float32 float32 40 Float64 float64 41 Complex64 complex64 42 Complex128 complex128 43 Array [4]uint8 44 Bool bool 45 BoolArray [4]bool 46 } 47 48 var s = Struct{ 49 0x01, 50 0x0203, 51 0x04050607, 52 0x08090a0b0c0d0e0f, 53 0x10, 54 0x1112, 55 0x13141516, 56 0x1718191a1b1c1d1e, 57 58 math.Float32frombits(0x1f202122), 59 math.Float64frombits(0x232425262728292a), 60 complex( 61 math.Float32frombits(0x2b2c2d2e), 62 math.Float32frombits(0x2f303132), 63 ), 64 complex( 65 math.Float64frombits(0x333435363738393a), 66 math.Float64frombits(0x3b3c3d3e3f404142), 67 ), 68 69 [4]uint8{0x43, 0x44, 0x45, 0x46}, 70 71 true, 72 [4]bool{true, false, true, false}, 73 } 74 75 var big = []byte{ 76 1, 77 2, 3, 78 4, 5, 6, 7, 79 8, 9, 10, 11, 12, 13, 14, 15, 80 16, 81 17, 18, 82 19, 20, 21, 22, 83 23, 24, 25, 26, 27, 28, 29, 30, 84 85 31, 32, 33, 34, 86 35, 36, 37, 38, 39, 40, 41, 42, 87 43, 44, 45, 46, 47, 48, 49, 50, 88 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 89 90 67, 68, 69, 70, 91 92 1, 93 1, 0, 1, 0, 94 } 95 96 var little = []byte{ 97 1, 98 3, 2, 99 7, 6, 5, 4, 100 15, 14, 13, 12, 11, 10, 9, 8, 101 16, 102 18, 17, 103 22, 21, 20, 19, 104 30, 29, 28, 27, 26, 25, 24, 23, 105 106 34, 33, 32, 31, 107 42, 41, 40, 39, 38, 37, 36, 35, 108 46, 45, 44, 43, 50, 49, 48, 47, 109 58, 57, 56, 55, 54, 53, 52, 51, 66, 65, 64, 63, 62, 61, 60, 59, 110 111 67, 68, 69, 70, 112 113 1, 114 1, 0, 1, 0, 115 } 116 117 var src = []byte{1, 2, 3, 4, 5, 6, 7, 8} 118 var res = []int32{0x01020304, 0x05060708} 119 120 func checkResult(t *testing.T, dir string, order binary.ByteOrder, err error, have, want interface{}) { 121 if err != nil { 122 t.Errorf("%v %v: %v", dir, order, err) 123 return 124 } 125 if !reflect.DeepEqual(have, want) { 126 t.Errorf("%v %v:\n\thave %+v\n\twant %+v", dir, order, have, want) 127 } 128 } 129 130 func testRead(t *testing.T, order binary.ByteOrder, b []byte, s1 interface{}) { 131 var s2 Struct 132 err := Read(b, order, &s2) 133 checkResult(t, "Read", order, err, s2, s1) 134 } 135 136 func TestLittleEndianRead(t *testing.T) { testRead(t, binary.LittleEndian, little, s) } 137 138 func TestBigEndianRead(t *testing.T) { testRead(t, binary.BigEndian, big, s) } 139 140 func TestReadSlice(t *testing.T) { 141 slice := make([]int32, 2) 142 err := Read(src, binary.BigEndian, slice) 143 checkResult(t, "ReadSlice", binary.BigEndian, err, slice, res) 144 } 145 146 func TestReadBool(t *testing.T) { 147 var res bool 148 var err error 149 err = Read([]byte{0}, binary.BigEndian, &res) 150 checkResult(t, "ReadBool", binary.BigEndian, err, res, false) 151 res = false 152 err = Read([]byte{1}, binary.BigEndian, &res) 153 checkResult(t, "ReadBool", binary.BigEndian, err, res, true) 154 res = false 155 err = Read([]byte{2}, binary.BigEndian, &res) 156 checkResult(t, "ReadBool", binary.BigEndian, err, res, true) 157 } 158 159 func TestReadBoolSlice(t *testing.T) { 160 slice := make([]bool, 4) 161 err := Read([]byte{0, 1, 2, 255}, binary.BigEndian, slice) 162 checkResult(t, "ReadBoolSlice", binary.BigEndian, err, slice, []bool{false, true, true, true}) 163 }