forked from cloudflare/xdpcap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook_test.go
90 lines (74 loc) · 1.77 KB
/
hook_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
package xdpcap
import (
"testing"
"github.com/cilium/ebpf"
)
const (
elf = "testdata/xdp_hook.c.elf"
hookSymbol = "xdpcap_hook"
)
// Test loading an elf with a hook map using ebpf.NewCollection().
func TestHookNewCollection(t *testing.T) {
hook, err := NewHook("foo")
if err != nil {
t.Fatal(err)
}
defer hook.Close()
spec := mustPatchSpec(t, hook)
coll, err := ebpf.NewCollection(spec)
if err != nil {
t.Fatal(err)
}
defer coll.Close()
}
// Test loading an elf with a hook map using ebpf.CollectionSpec.LoadAndReplace(),
// which didn't always work: https://github.com/cilium/ebpf/commit/04b5c2a901f3bcfa7d7a13c59f7c1c556f2f3d5f
func TestHookLoadAndReplace(t *testing.T) {
test := func(t *testing.T, hook *Hook) {
spec := mustPatchSpec(t, hook)
var objs struct {
// Works for both programs that do and don't use the hook.
Hook *ebpf.Program `ebpf:"xdp_hook"`
NoHook *ebpf.Program `ebpf:"xdp_nohook"`
}
if err := spec.LoadAndAssign(&objs, nil); err != nil {
t.Fatal(err)
}
defer objs.Hook.Close()
defer objs.NoHook.Close()
}
t.Run("nil", func(t *testing.T) {
test(t, nil)
})
t.Run("not-nil", func(t *testing.T) {
hook, err := NewHook("foo")
if err != nil {
t.Fatal(err)
}
defer hook.Close()
test(t, hook)
})
}
// Test loading an elf that uses a hook without an explicit hook map
func TestNoHook(t *testing.T) {
spec, err := ebpf.LoadCollectionSpec(elf)
if err != nil {
t.Fatal(err)
}
coll, err := ebpf.NewCollection(spec)
if err != nil {
t.Fatal(err)
}
defer coll.Close()
}
func mustPatchSpec(tb testing.TB, hook *Hook) *ebpf.CollectionSpec {
spec, err := ebpf.LoadCollectionSpec(elf)
if err != nil {
tb.Fatal(err)
}
err = hook.Patch(spec, hookSymbol)
if err != nil {
tb.Fatal(err)
}
return spec
}