Debian Codegraph @

internal/tracefs/perf_event_test.go

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