Repository navigation
Expand file tree
/
Copy pathtracing_test.go
More file actions
130 lines (113 loc) · 3.39 KB
/
Copy pathtracing_test.go
File metadata and controls
130 lines (113 loc) · 3.39 KB
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
package link
import (
"testing"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
)
func TestFreplace(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.10", "freplace")
testutils.Files(t, testutils.Glob(t, "../testdata/freplace-*.elf"), func(t *testing.T, file string) {
spec, err := ebpf.LoadCollectionSpec(file)
if err != nil {
t.Fatal("Can't parse ELF:", err)
}
if spec.ByteOrder != internal.NativeEndian {
return
}
target, err := ebpf.NewProgram(spec.Programs["sched_process_exec"])
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't create target program:", err)
}
defer target.Close()
// Test attachment specified at load time
spec.Programs["replacement"].AttachTarget = target
replacement, err := ebpf.NewProgram(spec.Programs["replacement"])
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't create replacement program:", err)
}
defer replacement.Close()
freplace, err := AttachFreplace(nil, "", replacement)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't create freplace:", err)
}
testLink(t, freplace, replacement)
})
}
func TestTracing(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.11", "BPF_LINK_TYPE_TRACING")
tests := []struct {
name string
attachTo string
programType ebpf.ProgramType
programAttachType, attachTypeOpt ebpf.AttachType
cookie uint64
}{
{
name: "AttachTraceFEntry",
attachTo: "inet_dgram_connect",
programType: ebpf.Tracing,
programAttachType: ebpf.AttachTraceFEntry,
},
{
name: "AttachTraceFEntry",
attachTo: "inet_dgram_connect",
programType: ebpf.Tracing,
programAttachType: ebpf.AttachTraceFEntry,
attachTypeOpt: ebpf.AttachTraceFEntry,
cookie: 1,
},
{
name: "AttachTraceFEntry",
attachTo: "inet_dgram_connect",
programType: ebpf.Tracing,
programAttachType: ebpf.AttachTraceFEntry,
},
{
name: "AttachTraceFExit",
attachTo: "inet_dgram_connect",
programType: ebpf.Tracing,
programAttachType: ebpf.AttachTraceFExit,
},
{
name: "AttachModifyReturn",
attachTo: "bpf_modify_return_test",
programType: ebpf.Tracing,
programAttachType: ebpf.AttachModifyReturn,
},
{
name: "AttachTraceRawTp",
attachTo: "kfree_skb",
programType: ebpf.Tracing,
programAttachType: ebpf.AttachTraceRawTp,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
prog := mustLoadProgram(t, tt.programType, tt.programAttachType, tt.attachTo)
opts := TracingOptions{Program: prog, AttachType: tt.attachTypeOpt, Cookie: tt.cookie}
link, err := AttachTracing(opts)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
}
testLink(t, link, prog)
if err = link.Close(); err != nil {
t.Fatal(err)
}
})
}
}
func TestLSM(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.11", "BPF_LINK_TYPE_TRACING")
prog := mustLoadProgram(t, ebpf.LSM, ebpf.AttachLSMMac, "file_mprotect")
link, err := AttachLSM(LSMOptions{Program: prog})
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
}
testLink(t, link, prog)
}