-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathc_test.go
70 lines (58 loc) · 1.47 KB
/
c_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
package cbpfc
import (
"bytes"
"testing"
"github.com/cilium/ebpf"
"golang.org/x/net/bpf"
)
func TestFunctionName(t *testing.T) {
checkName := func(t *testing.T, name string, valid bool) {
t.Helper()
_, err := ToC([]bpf.Instruction{bpf.RetA{}}, COpts{
FunctionName: name,
})
if valid && err != nil {
t.Fatalf("valid function name %s rejected: %v", name, err)
}
if !valid {
requireError(t, err, "invalid FunctionName")
}
}
checkName(t, "", false)
checkName(t, "0foo", false)
checkName(t, "0foo\nfoo", false)
checkName(t, "foo_bar2", true)
checkName(t, "a2", true)
}
func TestNoInline(t *testing.T) {
elf, err := buildC([]bpf.Instruction{
bpf.RetConstant{Val: 1},
}, entryPoint, COpts{
FunctionName: "filter",
NoInline: true,
})
if err != nil {
t.Fatal(err)
}
spec, err := ebpf.LoadCollectionSpecFromReader(bytes.NewReader(elf))
if err != nil {
t.Fatal(err)
}
if res := testProg(t, spec.Programs[entryPoint], []byte{1}); res != match {
t.Fatalf("expected match, got %v", res)
}
}
const entryPoint = "xdp_filter"
// cBackend compiles classic BPF to C, which is compiled with clang
func cBackend(tb testing.TB, insns []bpf.Instruction, in []byte) result {
elf, err := buildC(insns, entryPoint, COpts{FunctionName: "filter"})
if err != nil {
tb.Fatal(err)
}
// load ELF
spec, err := ebpf.LoadCollectionSpecFromReader(bytes.NewReader(elf))
if err != nil {
tb.Fatal(err)
}
return testProg(tb, spec.Programs[entryPoint], in)
}