-
Notifications
You must be signed in to change notification settings - Fork 729
/
Copy pathhelpers_test.go
310 lines (253 loc) · 6.29 KB
/
helpers_test.go
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
308
309
310
package ebpf
import (
"errors"
"sync"
"testing"
"github.com/go-quicktest/qt"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal/platform"
"github.com/cilium/ebpf/internal/testutils"
)
var haveTestmod = sync.OnceValues(func() (bool, error) {
if platform.IsWindows {
return false, nil
}
// See https://github.com/torvalds/linux/commit/290248a5b7d829871b3ea3c62578613a580a1744
testmod, err := btf.FindHandle(func(info *btf.HandleInfo) bool {
return info.IsModule() && info.Name == "bpf_testmod"
})
if err != nil && !errors.Is(err, btf.ErrNotFound) {
return false, err
}
testmod.Close()
return testmod != nil, nil
})
func requireTestmod(tb testing.TB) {
tb.Helper()
testutils.SkipOnOldKernel(tb, "5.11", "bpf_testmod")
testmod, err := haveTestmod()
if err != nil {
tb.Fatal(err)
}
if !testmod {
tb.Skip("bpf_testmod not loaded")
}
}
func newMap(tb testing.TB, spec *MapSpec, opts *MapOptions) (*Map, error) {
tb.Helper()
spec = fixupMapSpec(spec)
if opts == nil {
opts = new(MapOptions)
}
m, err := NewMapWithOptions(spec, *opts)
testutils.SkipIfNotSupportedOnOS(tb, err)
if err != nil {
return nil, err
}
tb.Cleanup(func() { m.Close() })
return m, nil
}
func mustNewMap(tb testing.TB, spec *MapSpec, opts *MapOptions) *Map {
tb.Helper()
m, err := newMap(tb, spec, opts)
qt.Assert(tb, qt.IsNil(err))
return m
}
func createMap(tb testing.TB, typ MapType, maxEntries uint32) *Map {
tb.Helper()
return mustNewMap(tb, &MapSpec{
Name: "test",
Type: typ,
KeySize: 4,
ValueSize: 4,
MaxEntries: maxEntries,
}, nil)
}
func createMapInMap(tb testing.TB, outer, inner MapType) *Map {
tb.Helper()
return mustNewMap(tb, &MapSpec{
Type: outer,
KeySize: 4,
MaxEntries: 2,
InnerMap: &MapSpec{
Type: inner,
KeySize: 4,
ValueSize: 4,
MaxEntries: 2,
},
}, nil)
}
func newProgram(tb testing.TB, spec *ProgramSpec, opts *ProgramOptions) (*Program, error) {
tb.Helper()
if opts == nil {
opts = new(ProgramOptions)
}
spec = fixupProgramSpec(spec)
prog, err := NewProgramWithOptions(spec, *opts)
testutils.SkipIfNotSupportedOnOS(tb, err)
if err != nil {
return nil, err
}
tb.Cleanup(func() { prog.Close() })
return prog, nil
}
func mustNewProgram(tb testing.TB, spec *ProgramSpec, opts *ProgramOptions) *Program {
tb.Helper()
prog, err := newProgram(tb, spec, opts)
qt.Assert(tb, qt.IsNil(err))
return prog
}
func createProgram(tb testing.TB, typ ProgramType, retval int64) *Program {
tb.Helper()
return mustNewProgram(tb, &ProgramSpec{
Name: "test",
Type: typ,
Instructions: asm.Instructions{
asm.LoadImm(asm.R0, retval, asm.DWord),
asm.Return(),
},
License: "MIT",
}, nil)
}
var basicProgramSpec = &ProgramSpec{
Name: "test",
Type: SocketFilter,
Instructions: asm.Instructions{
asm.LoadImm(asm.R0, 2, asm.DWord),
asm.Return(),
},
License: "MIT",
}
// createBasicProgram returns a program of an unspecified type which returns
// a non-zero value when executed.
func createBasicProgram(tb testing.TB) *Program {
return mustNewProgram(tb, basicProgramSpec, nil)
}
func newCollection(tb testing.TB, spec *CollectionSpec, opts *CollectionOptions) (*Collection, error) {
tb.Helper()
spec = fixupCollectionSpec(spec)
if opts == nil {
opts = new(CollectionOptions)
}
c, err := NewCollectionWithOptions(spec, *opts)
testutils.SkipIfNotSupportedOnOS(tb, err)
if err != nil {
return nil, err
}
tb.Cleanup(func() { c.Close() })
return c, nil
}
func mustNewCollection(tb testing.TB, spec *CollectionSpec, opts *CollectionOptions) *Collection {
tb.Helper()
c, err := newCollection(tb, spec, opts)
qt.Assert(tb, qt.IsNil(err))
return c
}
func loadAndAssign(tb testing.TB, spec *CollectionSpec, to any, opts *CollectionOptions) error {
tb.Helper()
spec = fixupCollectionSpec(spec)
err := spec.LoadAndAssign(to, opts)
testutils.SkipIfNotSupported(tb, err)
return err
}
func mustLoadAndAssign(tb testing.TB, spec *CollectionSpec, to any, opts *CollectionOptions) {
qt.Assert(tb, qt.IsNil(loadAndAssign(tb, spec, to, opts)))
}
// The functions below translate Linux types to their Windows equivalents, if
// possible. This allows running most tests on Windows without modification.
func fixupMapType(typ MapType) MapType {
if !platform.IsWindows {
return typ
}
switch typ {
case Array:
return WindowsArray
case Hash:
return WindowsHash
case ProgramArray:
return WindowsProgramArray
case PerCPUHash:
return WindowsPerCPUHash
case PerCPUArray:
return WindowsPerCPUArray
case LRUHash:
return WindowsLRUHash
case LRUCPUHash:
return WindowsLRUCPUHash
case ArrayOfMaps:
return WindowsArrayOfMaps
case HashOfMaps:
return WindowsHashOfMaps
case LPMTrie:
return WindowsLPMTrie
case Queue:
return WindowsQueue
case Stack:
return WindowsStack
case RingBuf:
return WindowsRingBuf
default:
return typ
}
}
func fixupMapSpec(spec *MapSpec) *MapSpec {
if !platform.IsWindows {
return spec
}
spec = spec.Copy()
spec.Type = fixupMapType(spec.Type)
if spec.InnerMap != nil {
spec.InnerMap.Type = fixupMapType(spec.InnerMap.Type)
}
return spec
}
func fixupProgramType(typ ProgramType) ProgramType {
if !platform.IsWindows {
return typ
}
switch typ {
case SocketFilter:
return WindowsXDPTest
case XDP:
return WindowsXDPTest
default:
return typ
}
}
func fixupProgramSpec(spec *ProgramSpec) *ProgramSpec {
if !platform.IsWindows {
return spec
}
spec = spec.Copy()
spec.Type = fixupProgramType(spec.Type)
for i, ins := range spec.Instructions {
if ins.IsBuiltinCall() {
switch asm.BuiltinFunc(ins.Constant) {
case asm.FnMapUpdateElem:
spec.Instructions[i].Constant = int64(asm.WindowsFnMapUpdateElem)
case asm.FnMapLookupElem:
spec.Instructions[i].Constant = int64(asm.WindowsFnMapLookupElem)
case asm.FnTailCall:
spec.Instructions[i].Constant = int64(asm.WindowsFnTailCall)
}
}
}
return spec
}
func fixupCollectionSpec(spec *CollectionSpec) *CollectionSpec {
if !platform.IsWindows {
return spec
}
spec = spec.Copy()
for name := range spec.Maps {
spec.Maps[name] = fixupMapSpec(spec.Maps[name])
}
for name := range spec.Programs {
spec.Programs[name] = fixupProgramSpec(spec.Programs[name])
}
for _, varSpec := range spec.Variables {
varSpec.m = spec.Maps[varSpec.m.Name]
}
return spec
}