File: marshal_test.go

package info (click to toggle)
golang-github-cilium-ebpf 0.17.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,684 kB
  • sloc: ansic: 1,259; makefile: 127; python: 113; awk: 29; sh: 24
file content (307 lines) | stat: -rw-r--r-- 6,373 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package sysenc

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"math"
	"reflect"
	"testing"

	"github.com/go-quicktest/qt"
	"github.com/google/go-cmp/cmp/cmpopts"

	"github.com/cilium/ebpf/internal"
)

type testcase struct {
	new        func() any
	zeroAllocs bool
}

type struc struct {
	A uint64
	B uint32
}

type explicitPad struct {
	_ uint32
}

func testcases() []testcase {
	return []testcase{
		{func() any { return new([1]uint64) }, true},
		{func() any { return new(int16) }, true},
		{func() any { return new(uint16) }, true},
		{func() any { return new(int32) }, true},
		{func() any { return new(uint32) }, true},
		{func() any { return new(int64) }, true},
		{func() any { return new(uint64) }, true},
		{func() any { return make([]byte, 9) }, true},
		{func() any { return new(explicitPad) }, true},
		{func() any { return make([]explicitPad, 0) }, false},
		{func() any { return make([]explicitPad, 1) }, false},
		{func() any { return make([]explicitPad, 2) }, false},
		{func() any { return new(struc) }, false},
		{func() any { return make([]struc, 0) }, false},
		{func() any { return make([]struc, 1) }, false},
		{func() any { return make([]struc, 2) }, false},
		{func() any { return int16(math.MaxInt16) }, false},
		{func() any { return uint16(math.MaxUint16) }, false},
		{func() any { return int32(math.MaxInt32) }, false},
		{func() any { return uint32(math.MaxUint32) }, false},
		{func() any { return int64(math.MaxInt64) }, false},
		{func() any { return uint64(math.MaxUint64) }, false},
		{func() any { return struc{math.MaxUint64, math.MaxUint32} }, false},
	}
}

func TestMarshal(t *testing.T) {
	for _, test := range testcases() {
		value := test.new()
		t.Run(fmt.Sprintf("%T", value), func(t *testing.T) {
			var want bytes.Buffer
			if err := binary.Write(&want, internal.NativeEndian, value); err != nil {
				t.Fatal(err)
			}

			have := make([]byte, want.Len())
			buf, err := Marshal(value, binary.Size(value))
			if err != nil {
				t.Fatal(err)
			}
			qt.Assert(t, qt.Equals(buf.CopyTo(have), want.Len()))
			qt.Assert(t, qt.CmpEquals(have, want.Bytes(), cmpopts.EquateEmpty()))
		})
	}
}

func TestMarshalAllocations(t *testing.T) {
	allocationsPerMarshal := func(t *testing.T, data any) float64 {
		size := binary.Size(data)
		return testing.AllocsPerRun(5, func() {
			_, err := Marshal(data, size)
			if err != nil {
				t.Fatal(err)
			}
		})
	}

	for _, test := range testcases() {
		if !test.zeroAllocs {
			continue
		}

		value := test.new()
		t.Run(fmt.Sprintf("%T", value), func(t *testing.T) {
			qt.Assert(t, qt.Equals(allocationsPerMarshal(t, value), 0))
		})
	}
}

func TestUnmarshal(t *testing.T) {
	for _, test := range testcases() {
		value := test.new()
		if !canUnmarshalInto(value) {
			continue
		}

		t.Run(fmt.Sprintf("%T", value), func(t *testing.T) {
			want := test.new()
			buf := randomiseValue(t, want)

			qt.Assert(t, qt.IsNil(Unmarshal(value, buf)))
			qt.Assert(t, qt.DeepEquals(value, want))
		})
	}
}

func TestUnmarshalAllocations(t *testing.T) {
	allocationsPerUnmarshal := func(t *testing.T, data any, buf []byte) float64 {
		return testing.AllocsPerRun(5, func() {
			err := Unmarshal(data, buf)
			if err != nil {
				t.Fatal(err)
			}
		})
	}

	for _, test := range testcases() {
		if !test.zeroAllocs {
			continue
		}

		value := test.new()
		if !canUnmarshalInto(value) {
			continue
		}

		t.Run(fmt.Sprintf("%T", value), func(t *testing.T) {
			buf := make([]byte, binary.Size(value))
			qt.Assert(t, qt.Equals(allocationsPerUnmarshal(t, value, buf), 0))
		})
	}
}

func TestUnsafeBackingMemory(t *testing.T) {
	marshalNative := func(t *testing.T, data any) []byte {
		t.Helper()

		var buf bytes.Buffer
		qt.Assert(t, qt.IsNil(binary.Write(&buf, internal.NativeEndian, data)))
		return buf.Bytes()
	}

	for _, test := range []struct {
		name  string
		value any
	}{
		{
			"slice",
			[]uint32{1, 2},
		},
		{
			"pointer to slice",
			&[]uint32{2},
		},
		{
			"pointer to array",
			&[2]uint64{},
		},
		{
			"pointer to int64",
			new(int64),
		},
		{
			"pointer to struct",
			&struct {
				A, B uint16
				C    uint32
			}{},
		},
		{
			"struct with explicit padding",
			&struct{ _ uint64 }{},
		},
	} {
		t.Run("valid: "+test.name, func(t *testing.T) {
			want := marshalNative(t, test.value)
			have := unsafeBackingMemory(test.value)
			qt.Assert(t, qt.DeepEquals(have, want))
		})
	}

	for _, test := range []struct {
		name  string
		value any
	}{
		{
			"nil",
			nil,
		},
		{
			"nil slice",
			([]byte)(nil),
		},
		{
			"nil pointer",
			(*uint64)(nil),
		},
		{
			"nil pointer to slice",
			(*[]uint32)(nil),
		},
		{
			"nil pointer to array",
			(*[2]uint64)(nil),
		},
		{
			"unexported field",
			&struct{ a uint64 }{},
		},
		{
			"struct containing pointer",
			&struct{ A *uint64 }{},
		},
		{
			"struct with trailing padding",
			&struc{},
		},
		{
			"struct with interspersed padding",
			&struct {
				B uint32
				A uint64
			}{},
		},
		{
			"padding between slice entries",
			&[]struc{{}},
		},
		{
			"padding between array entries",
			&[2]struc{},
		},
	} {
		t.Run("invalid: "+test.name, func(t *testing.T) {
			qt.Assert(t, qt.IsNil(unsafeBackingMemory(test.value)))
		})
	}
}

func BenchmarkMarshal(b *testing.B) {
	for _, test := range testcases() {
		value := test.new()
		b.Run(fmt.Sprintf("%T", value), func(b *testing.B) {
			size := binary.Size(value)
			b.ResetTimer()
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				_, _ = Marshal(value, size)
			}
		})
	}
}

func BenchmarkUnmarshal(b *testing.B) {
	for _, test := range testcases() {
		value := test.new()
		if !canUnmarshalInto(value) {
			continue
		}

		b.Run(fmt.Sprintf("%T", value), func(b *testing.B) {
			size := binary.Size(value)
			buf := make([]byte, size)
			b.ResetTimer()
			b.ReportAllocs()
			for i := 0; i < b.N; i++ {
				_ = Unmarshal(value, buf)
			}
		})
	}
}

func randomiseValue(tb testing.TB, value any) []byte {
	tb.Helper()

	size := binary.Size(value)
	if size == -1 {
		tb.Fatalf("Can't unmarshal into %T", value)
	}

	buf := make([]byte, size)
	for i := range buf {
		buf[i] = byte(i)
	}

	err := binary.Read(bytes.NewReader(buf), internal.NativeEndian, value)
	qt.Assert(tb, qt.IsNil(err))

	return buf
}

func canUnmarshalInto(data any) bool {
	kind := reflect.TypeOf(data).Kind()
	return kind == reflect.Slice || kind == reflect.Pointer
}