| 1 |
package sysenc |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/binary" |
| 6 |
"fmt" |
| 7 |
"math" |
| 8 |
"reflect" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/go-quicktest/qt" |
| 12 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 13 |
|
| 14 |
"github.com/cilium/ebpf/internal" |
| 15 |
) |
| 16 |
|
| 17 |
type testcase struct { |
| 18 |
new func() any |
| 19 |
zeroAllocs bool |
| 20 |
} |
| 21 |
|
| 22 |
type struc struct { |
| 23 |
A uint64 |
| 24 |
B uint32 |
| 25 |
} |
| 26 |
|
| 27 |
type explicitPad struct { |
| 28 |
_ uint32 |
| 29 |
} |
| 30 |
|
| 31 |
func testcases() []testcase { |
| 32 |
return []testcase{ |
| 33 |
{func() any { return new([1]uint64) }, true}, |
| 34 |
{func() any { return new(int16) }, true}, |
| 35 |
{func() any { return new(uint16) }, true}, |
| 36 |
{func() any { return new(int32) }, true}, |
| 37 |
{func() any { return new(uint32) }, true}, |
| 38 |
{func() any { return new(int64) }, true}, |
| 39 |
{func() any { return new(uint64) }, true}, |
| 40 |
{func() any { return make([]byte, 9) }, true}, |
| 41 |
{func() any { return new(explicitPad) }, true}, |
| 42 |
{func() any { return make([]explicitPad, 0) }, false}, |
| 43 |
{func() any { return make([]explicitPad, 1) }, false}, |
| 44 |
{func() any { return make([]explicitPad, 2) }, false}, |
| 45 |
{func() any { return new(struc) }, false}, |
| 46 |
{func() any { return make([]struc, 0) }, false}, |
| 47 |
{func() any { return make([]struc, 1) }, false}, |
| 48 |
{func() any { return make([]struc, 2) }, false}, |
| 49 |
{func() any { return int16(math.MaxInt16) }, false}, |
| 50 |
{func() any { return uint16(math.MaxUint16) }, false}, |
| 51 |
{func() any { return int32(math.MaxInt32) }, false}, |
| 52 |
{func() any { return uint32(math.MaxUint32) }, false}, |
| 53 |
{func() any { return int64(math.MaxInt64) }, false}, |
| 54 |
{func() any { return uint64(math.MaxUint64) }, false}, |
| 55 |
{func() any { return struc{math.MaxUint64, math.MaxUint32} }, false}, |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func TestMarshal(t *testing.T) { |
| 60 |
for _, test := range testcases() { |
| 61 |
value := test.new() |
| 62 |
t.Run(fmt.Sprintf("%T", value), func(t *testing.T) { |
| 63 |
want, err := binary.Append(nil, internal.NativeEndian, value) |
| 64 |
qt.Assert(t, qt.IsNil(err)) |
| 65 |
|
| 66 |
have := make([]byte, len(want)) |
| 67 |
buf, err := Marshal(value, binary.Size(value)) |
| 68 |
if err != nil { |
| 69 |
t.Fatal(err) |
| 70 |
} |
| 71 |
qt.Assert(t, qt.Equals(buf.CopyTo(have), len(want))) |
| 72 |
qt.Assert(t, qt.CmpEquals(have, want, cmpopts.EquateEmpty())) |
| 73 |
}) |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
func TestMarshalAllocations(t *testing.T) { |
| 78 |
allocationsPerMarshal := func(t *testing.T, data any) float64 { |
| 79 |
size := binary.Size(data) |
| 80 |
return testing.AllocsPerRun(5, func() { |
| 81 |
_, err := Marshal(data, size) |
| 82 |
if err != nil { |
| 83 |
t.Fatal(err) |
| 84 |
} |
| 85 |
}) |
| 86 |
} |
| 87 |
|
| 88 |
for _, test := range testcases() { |
| 89 |
if !test.zeroAllocs { |
| 90 |
continue |
| 91 |
} |
| 92 |
|
| 93 |
value := test.new() |
| 94 |
t.Run(fmt.Sprintf("%T", value), func(t *testing.T) { |
| 95 |
qt.Assert(t, qt.Equals(allocationsPerMarshal(t, value), 0)) |
| 96 |
}) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func TestUnmarshal(t *testing.T) { |
| 101 |
for _, test := range testcases() { |
| 102 |
value := test.new() |
| 103 |
if !canUnmarshalInto(value) { |
| 104 |
continue |
| 105 |
} |
| 106 |
|
| 107 |
t.Run(fmt.Sprintf("%T", value), func(t *testing.T) { |
| 108 |
want := test.new() |
| 109 |
buf := randomiseValue(t, want) |
| 110 |
|
| 111 |
qt.Assert(t, qt.IsNil(Unmarshal(value, buf))) |
| 112 |
qt.Assert(t, qt.DeepEquals(value, want)) |
| 113 |
}) |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
func TestUnmarshalAllocations(t *testing.T) { |
| 118 |
allocationsPerUnmarshal := func(t *testing.T, data any, buf []byte) float64 { |
| 119 |
return testing.AllocsPerRun(5, func() { |
| 120 |
err := Unmarshal(data, buf) |
| 121 |
if err != nil { |
| 122 |
t.Fatal(err) |
| 123 |
} |
| 124 |
}) |
| 125 |
} |
| 126 |
|
| 127 |
for _, test := range testcases() { |
| 128 |
value := test.new() |
| 129 |
if !canUnmarshalInto(value) { |
| 130 |
continue |
| 131 |
} |
| 132 |
|
| 133 |
t.Run(fmt.Sprintf("%T", value), func(t *testing.T) { |
| 134 |
buf := make([]byte, binary.Size(value)) |
| 135 |
qt.Assert(t, qt.Equals(allocationsPerUnmarshal(t, value, buf), 0)) |
| 136 |
}) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
func TestUnsafeBackingMemory(t *testing.T) { |
| 141 |
marshalNative := func(t *testing.T, data any) []byte { |
| 142 |
t.Helper() |
| 143 |
|
| 144 |
buf, err := binary.Append(nil, internal.NativeEndian, data) |
| 145 |
qt.Assert(t, qt.IsNil(err)) |
| 146 |
return buf |
| 147 |
} |
| 148 |
|
| 149 |
for _, test := range []struct { |
| 150 |
name string |
| 151 |
value any |
| 152 |
}{ |
| 153 |
{ |
| 154 |
"slice", |
| 155 |
[]uint32{1, 2}, |
| 156 |
}, |
| 157 |
{ |
| 158 |
"pointer to slice", |
| 159 |
&[]uint32{2}, |
| 160 |
}, |
| 161 |
{ |
| 162 |
"pointer to array", |
| 163 |
&[2]uint64{}, |
| 164 |
}, |
| 165 |
{ |
| 166 |
"pointer to int64", |
| 167 |
new(int64), |
| 168 |
}, |
| 169 |
{ |
| 170 |
"pointer to struct", |
| 171 |
&struct { |
| 172 |
A, B uint16 |
| 173 |
C uint32 |
| 174 |
}{}, |
| 175 |
}, |
| 176 |
{ |
| 177 |
"struct with explicit padding", |
| 178 |
&struct{ _ uint64 }{}, |
| 179 |
}, |
| 180 |
} { |
| 181 |
t.Run("valid: "+test.name, func(t *testing.T) { |
| 182 |
want := marshalNative(t, test.value) |
| 183 |
have := unsafeBackingMemory(test.value) |
| 184 |
qt.Assert(t, qt.DeepEquals(have, want)) |
| 185 |
}) |
| 186 |
} |
| 187 |
|
| 188 |
for _, test := range []struct { |
| 189 |
name string |
| 190 |
value any |
| 191 |
}{ |
| 192 |
{ |
| 193 |
"nil", |
| 194 |
nil, |
| 195 |
}, |
| 196 |
{ |
| 197 |
"nil slice", |
| 198 |
([]byte)(nil), |
| 199 |
}, |
| 200 |
{ |
| 201 |
"nil pointer", |
| 202 |
(*uint64)(nil), |
| 203 |
}, |
| 204 |
{ |
| 205 |
"nil pointer to slice", |
| 206 |
(*[]uint32)(nil), |
| 207 |
}, |
| 208 |
{ |
| 209 |
"nil pointer to array", |
| 210 |
(*[2]uint64)(nil), |
| 211 |
}, |
| 212 |
{ |
| 213 |
"unexported field", |
| 214 |
&struct{ a uint64 }{}, |
| 215 |
}, |
| 216 |
{ |
| 217 |
"struct containing pointer", |
| 218 |
&struct{ A *uint64 }{}, |
| 219 |
}, |
| 220 |
{ |
| 221 |
"struct with trailing padding", |
| 222 |
&struc{}, |
| 223 |
}, |
| 224 |
{ |
| 225 |
"struct with interspersed padding", |
| 226 |
&struct { |
| 227 |
B uint32 |
| 228 |
A uint64 |
| 229 |
}{}, |
| 230 |
}, |
| 231 |
{ |
| 232 |
"padding between slice entries", |
| 233 |
&[]struc{{}}, |
| 234 |
}, |
| 235 |
{ |
| 236 |
"padding between array entries", |
| 237 |
&[2]struc{}, |
| 238 |
}, |
| 239 |
} { |
| 240 |
t.Run("invalid: "+test.name, func(t *testing.T) { |
| 241 |
qt.Assert(t, qt.IsNil(unsafeBackingMemory(test.value))) |
| 242 |
}) |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
func BenchmarkMarshal(b *testing.B) { |
| 247 |
for _, test := range testcases() { |
| 248 |
value := test.new() |
| 249 |
b.Run(fmt.Sprintf("%T", value), func(b *testing.B) { |
| 250 |
size := binary.Size(value) |
| 251 |
b.ReportAllocs() |
| 252 |
for b.Loop() { |
| 253 |
_, _ = Marshal(value, size) |
| 254 |
} |
| 255 |
}) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
func BenchmarkUnmarshal(b *testing.B) { |
| 260 |
for _, test := range testcases() { |
| 261 |
value := test.new() |
| 262 |
if !canUnmarshalInto(value) { |
| 263 |
continue |
| 264 |
} |
| 265 |
|
| 266 |
b.Run(fmt.Sprintf("%T", value), func(b *testing.B) { |
| 267 |
size := binary.Size(value) |
| 268 |
buf := make([]byte, size) |
| 269 |
b.ReportAllocs() |
| 270 |
for b.Loop() { |
| 271 |
_ = Unmarshal(value, buf) |
| 272 |
} |
| 273 |
}) |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
func randomiseValue(tb testing.TB, value any) []byte { |
| 278 |
tb.Helper() |
| 279 |
|
| 280 |
size := binary.Size(value) |
| 281 |
if size == -1 { |
| 282 |
tb.Fatalf("Can't unmarshal into %T", value) |
| 283 |
} |
| 284 |
|
| 285 |
buf := make([]byte, size) |
| 286 |
for i := range buf { |
| 287 |
buf[i] = byte(i) |
| 288 |
} |
| 289 |
|
| 290 |
err := binary.Read(bytes.NewReader(buf), internal.NativeEndian, value) |
| 291 |
qt.Assert(tb, qt.IsNil(err)) |
| 292 |
|
| 293 |
return buf |
| 294 |
} |
| 295 |
|
| 296 |
func canUnmarshalInto(data any) bool { |
| 297 |
kind := reflect.TypeOf(data).Kind() |
| 298 |
return kind == reflect.Slice || kind == reflect.Pointer |
| 299 |
} |