forked from aquasecurity/libbpfgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libbpfgo_test.go
98 lines (89 loc) · 2.48 KB
/
libbpfgo_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
package libbpfgo
import (
"fmt"
"strings"
"testing"
)
func Test_LoadAndAttach(t *testing.T) {
// load non exisiting file, should fail
module, err := NewModuleFromFile("foo.bpf.o")
if err == nil {
t.Errorf("NewModuleFromFile returned nil error on non-existing file")
}
module, err = NewModuleFromFile("selftest/build/libbpfgo_test.bpf.o")
if err != nil {
t.Fatalf("NewModuleFromFile failed: %v", err)
}
defer module.Close()
// load non exisiting program, should fail
if err = module.BPFLoadObject(); err != nil {
t.Fatalf("BPFLoadObject failed: %v", err)
}
// get non exisiting program, should fail
_, err = module.GetProgram("foo")
if err == nil {
t.Errorf("GetProgram returned nil error on non-existing program")
}
attachTests := []struct {
prog string
attachArg string
attachFn func(*BPFProg, string) (*BPFLink, error)
}{
{
prog: "tracepoint__sys_enter_dup",
attachArg: "syscalls:sys_enter_dup",
attachFn: func(prog *BPFProg, name string) (*BPFLink, error) {
tpEvent := strings.Split(name, ":")
if len(tpEvent) != 2 {
return nil, fmt.Errorf("tracepoint must be in 'category:name' format")
}
return prog.AttachTracepoint(tpEvent[0], tpEvent[1])
},
},
{
prog: "raw_tracepoint__sched_switch",
attachArg: "sched_switch",
attachFn: func(prog *BPFProg, name string) (*BPFLink, error) {
return prog.AttachRawTracepoint(name)
},
},
{
prog: "kprobe__get_task_pid",
attachArg: "get_task_pid",
attachFn: func(prog *BPFProg, name string) (*BPFLink, error) {
return prog.AttachKprobe(name)
},
},
{
prog: "kretprobe__get_task_pid",
attachArg: "get_task_pid",
attachFn: func(prog *BPFProg, name string) (*BPFLink, error) {
return prog.AttachKretprobe(name)
},
},
{
prog: "socket_connect",
attachFn: func(prog *BPFProg, name string) (*BPFLink, error) {
if name != "" {
// to make the check for attaching with "foo" happy
return nil, fmt.Errorf("name not empty")
}
return prog.AttachLSM()
},
},
}
for i, test := range attachTests {
prog, err := module.GetProgram(test.prog)
if err != nil {
t.Errorf("test %v: GetProgram(%q) failed: %v", i, test.prog, err)
continue
}
// make sure it handles errors ok
if _, err = test.attachFn(prog, "foo"); err == nil {
t.Errorf("test %v: failure to attach expected", i)
}
if _, err = test.attachFn(prog, test.attachArg); err != nil {
t.Errorf("test %v: attach failed: %v", i, err)
}
}
}