| 1 |
package tracefs |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"os" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/go-quicktest/qt" |
| 10 |
|
| 11 |
"github.com/cilium/ebpf/internal/testutils" |
| 12 |
) |
| 13 |
|
| 14 |
func TestEventID(t *testing.T) { |
| 15 |
eid, err := EventID("syscalls", "sys_enter_mmap") |
| 16 |
testutils.SkipIfNotSupportedOnOS(t, err) |
| 17 |
qt.Assert(t, qt.IsNil(err)) |
| 18 |
qt.Assert(t, qt.Not(qt.Equals(eid, 0))) |
| 19 |
} |
| 20 |
|
| 21 |
func TestSanitizePath(t *testing.T) { |
| 22 |
_, err := sanitizeTracefsPath("../escaped") |
| 23 |
testutils.SkipIfNotSupportedOnOS(t, err) |
| 24 |
if !errors.Is(err, ErrInvalidInput) { |
| 25 |
t.Errorf("expected error %s, got: %s", ErrInvalidInput, err) |
| 26 |
} |
| 27 |
|
| 28 |
_, err = sanitizeTracefsPath("./not/escaped") |
| 29 |
if err != nil { |
| 30 |
t.Errorf("expected no error, got: %s", err) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func TestValidIdentifier(t *testing.T) { |
| 35 |
tests := []struct { |
| 36 |
name string |
| 37 |
in string |
| 38 |
fail bool |
| 39 |
}{ |
| 40 |
{"empty string", "", true}, |
| 41 |
{"leading number", "1test", true}, |
| 42 |
{"underscore first", "__x64_syscall", false}, |
| 43 |
{"contains number", "bpf_trace_run1", false}, |
| 44 |
{"underscore", "_", false}, |
| 45 |
{"leading dash", "-EINVAL", true}, |
| 46 |
{"contains number", "all0wed", false}, |
| 47 |
{"contains dash", "trace-group", false}, |
| 48 |
} |
| 49 |
|
| 50 |
for _, tt := range tests { |
| 51 |
t.Run(tt.name, func(t *testing.T) { |
| 52 |
exp := "pass" |
| 53 |
if tt.fail { |
| 54 |
exp = "fail" |
| 55 |
} |
| 56 |
|
| 57 |
if validIdentifier(tt.in) == tt.fail { |
| 58 |
t.Errorf("expected string '%s' to %s valid ID check", tt.in, exp) |
| 59 |
} |
| 60 |
}) |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
func TestSanitizeIdentifier(t *testing.T) { |
| 65 |
tests := []struct { |
| 66 |
symbol string |
| 67 |
expected string |
| 68 |
}{ |
| 69 |
{"readline", "readline"}, |
| 70 |
{"main.Func123", "main_Func123"}, |
| 71 |
{"a.....a", "a_a"}, |
| 72 |
{"./;'{}[]a", "_a"}, |
| 73 |
{"***xx**xx###", "_xx_xx_"}, |
| 74 |
{`@P#r$i%v^3*+t)i&k++--`, "_P_r_i_v_3_t_i_k_"}, |
| 75 |
} |
| 76 |
|
| 77 |
for i, tt := range tests { |
| 78 |
t.Run(fmt.Sprint(i), func(t *testing.T) { |
| 79 |
sanitized := sanitizeIdentifier(tt.symbol) |
| 80 |
if tt.expected != sanitized { |
| 81 |
t.Errorf("Expected sanitized symbol to be '%s', got '%s'", tt.expected, sanitized) |
| 82 |
} |
| 83 |
}) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func TestGetTracefsPath(t *testing.T) { |
| 88 |
path, err := getTracefsPath() |
| 89 |
testutils.SkipIfNotSupportedOnOS(t, err) |
| 90 |
qt.Assert(t, qt.IsNil(err)) |
| 91 |
_, err = os.Stat(path) |
| 92 |
qt.Assert(t, qt.IsNil(err)) |
| 93 |
} |