Debian Codegraph @

internal/tracefs/perf_event_test.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package tracefs
2
3 import (
4 "errors"
5 "fmt"
6 "os"
7 "path/filepath"
•8 "strings"
9 "testing"
10
11 "github.com/go-quicktest/qt"
12
13 "github.com/cilium/ebpf/internal/mountinfo"
14 "github.com/cilium/ebpf/internal/testutils"
15 )
16
17 func TestEventID(t *testing.T) {
18 eid, err := EventID("syscalls", "sys_enter_mmap")
19 testutils.SkipIfNotSupportedOnOS(t, err)
•20 if err != nil && strings.Contains(err.Error(), "permission denied") {
21 t.Skipf("Skipping test: %v", err)
22 }
23 qt.Assert(t, qt.IsNil(err))
24 qt.Assert(t, qt.Not(qt.Equals(eid, 0)))
25 }
26
27 func TestSanitizePath(t *testing.T) {
28 _, err := sanitizeTracefsPath("../escaped")
29 testutils.SkipIfNotSupportedOnOS(t, err)
30 if !errors.Is(err, ErrInvalidInput) {
31 t.Errorf("expected error %s, got: %s", ErrInvalidInput, err)
32 }
33
34 _, err = sanitizeTracefsPath("./not/escaped")
35 if err != nil {
36 t.Errorf("expected no error, got: %s", err)
37 }
38 }
39
40 func TestValidIdentifier(t *testing.T) {
41 tests := []struct {
42 name string
43 in string
44 fail bool
45 }{
46 {"empty string", "", true},
47 {"leading number", "1test", true},
48 {"underscore first", "__x64_syscall", false},
49 {"contains number", "bpf_trace_run1", false},
50 {"underscore", "_", false},
51 {"leading dash", "-EINVAL", true},
52 {"contains number", "all0wed", false},
53 {"contains dash", "trace-group", false},
54 }
55
56 for _, tt := range tests {
57 t.Run(tt.name, func(t *testing.T) {
58 exp := "pass"
59 if tt.fail {
60 exp = "fail"
61 }
62
63 if validIdentifier(tt.in) == tt.fail {
64 t.Errorf("expected string '%s' to %s valid ID check", tt.in, exp)
65 }
66 })
67 }
68 }
69
70 func TestSanitizeIdentifier(t *testing.T) {
71 tests := []struct {
72 symbol string
73 expected string
74 }{
75 {"readline", "readline"},
76 {"main.Func123", "main_Func123"},
77 {"a.....a", "a_a"},
78 {"./;'{}[]a", "_a"},
79 {"***xx**xx###", "_xx_xx_"},
80 {`@P#r$i%v^3*+t)i&k++--`, "_P_r_i_v_3_t_i_k_"},
81 }
82
83 for i, tt := range tests {
84 t.Run(fmt.Sprint(i), func(t *testing.T) {
85 sanitized := sanitizeIdentifier(tt.symbol)
86 if tt.expected != sanitized {
87 t.Errorf("Expected sanitized symbol to be '%s', got '%s'", tt.expected, sanitized)
88 }
89 })
90 }
91 }
92
93 func TestGetTracefsPath(t *testing.T) {
94 path, err := getTracefsPath()
95 testutils.SkipIfNotSupportedOnOS(t, err)
96 qt.Assert(t, qt.IsNil(err))
97 _, err = os.Stat(path)
98 qt.Assert(t, qt.IsNil(err))
99 }
100
101 func TestFindTracefsInEntries(t *testing.T) {
102 tmpDebugWithTracing := t.TempDir()
103 qt.Assert(t, qt.IsNil(os.Mkdir(filepath.Join(tmpDebugWithTracing, "tracing"), 0o755)))
104 tmpDebugBare := t.TempDir()
105
106 t.Run("tracefs entry returns its mount point", func(t *testing.T) {
107 got := findTracefsInEntries([]mountinfo.Entry{
108 {MountPoint: "/host/sys/kernel/tracing", Root: "/", FSType: "tracefs"},
109 })
110 qt.Assert(t, qt.Equals(got, "/host/sys/kernel/tracing"))
111 })
112
113 t.Run("first tracefs entry wins when multiple", func(t *testing.T) {
114 got := findTracefsInEntries([]mountinfo.Entry{
115 {MountPoint: "/sys/kernel/tracing", Root: "/", FSType: "tracefs"},
116 {MountPoint: "/host/sys/kernel/tracing", Root: "/", FSType: "tracefs"},
117 })
118 qt.Assert(t, qt.Equals(got, "/sys/kernel/tracing"))
119 })
120
121 t.Run("tracefs preferred over debugfs", func(t *testing.T) {
122 got := findTracefsInEntries([]mountinfo.Entry{
123 {MountPoint: tmpDebugWithTracing, Root: "/", FSType: "debugfs"},
124 {MountPoint: "/sys/kernel/tracing", Root: "/", FSType: "tracefs"},
125 })
126 qt.Assert(t, qt.Equals(got, "/sys/kernel/tracing"))
127 })
128
129 t.Run("debugfs entry with tracing subdir returns the subdir", func(t *testing.T) {
130 got := findTracefsInEntries([]mountinfo.Entry{
131 {MountPoint: tmpDebugWithTracing, Root: "/", FSType: "debugfs"},
132 })
133 qt.Assert(t, qt.Equals(got, filepath.Join(tmpDebugWithTracing, "tracing")))
134 })
135
136 t.Run("debugfs entry without tracing subdir falls through", func(t *testing.T) {
137 got := findTracefsInEntries([]mountinfo.Entry{
138 {MountPoint: tmpDebugBare, Root: "/", FSType: "debugfs"},
139 })
140 qt.Assert(t, qt.Equals(got, ""))
141 })
142
143 t.Run("no tracefs and no usable debugfs returns empty", func(t *testing.T) {
144 got := findTracefsInEntries([]mountinfo.Entry{
145 {MountPoint: "/", Root: "/", FSType: "overlay"},
146 {MountPoint: "/proc", Root: "/", FSType: "proc"},
147 })
148 qt.Assert(t, qt.Equals(got, ""))
149 })
150
151 t.Run("nil entries returns empty", func(t *testing.T) {
152 got := findTracefsInEntries(nil)
153 qt.Assert(t, qt.Equals(got, ""))
154 })
155
156 t.Run("subdirectory bind mount is skipped in favor of real tracefs", func(t *testing.T) {
157 // /weird/tracing-events is a bind mount of tracefs's events/ subdir;
158 // it should not be picked even though it appears first.
159 got := findTracefsInEntries([]mountinfo.Entry{
160 {MountPoint: "/weird/tracing-events", Root: "/events", FSType: "tracefs"},
161 {MountPoint: "/sys/kernel/tracing", Root: "/", FSType: "tracefs"},
162 })
163 qt.Assert(t, qt.Equals(got, "/sys/kernel/tracing"))
164 })
165
166 t.Run("only subdirectory tracefs mounts means no usable tracefs", func(t *testing.T) {
167 got := findTracefsInEntries([]mountinfo.Entry{
168 {MountPoint: "/weird/tracing-events", Root: "/events", FSType: "tracefs"},
169 })
170 qt.Assert(t, qt.Equals(got, ""))
171 })
172
173 t.Run("subdirectory debugfs bind mount is skipped", func(t *testing.T) {
174 got := findTracefsInEntries([]mountinfo.Entry{
175 {MountPoint: tmpDebugWithTracing, Root: "/tracing", FSType: "debugfs"},
176 })
177 qt.Assert(t, qt.Equals(got, ""))
178 })
179 }