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
|
package tracefs
import (
"errors"
"fmt"
"os"
"strings"
"testing"
"github.com/go-quicktest/qt"
"github.com/cilium/ebpf/internal/testutils"
)
func TestEventID(t *testing.T) {
eid, err := EventID("syscalls", "sys_enter_mmap")
testutils.SkipIfNotSupportedOnOS(t, err)
if err != nil && strings.Contains(err.Error(), "permission denied") {
t.Skipf("Skipping test: %v", err)
}
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Not(qt.Equals(eid, 0)))
}
func TestSanitizePath(t *testing.T) {
_, err := sanitizeTracefsPath("../escaped")
testutils.SkipIfNotSupportedOnOS(t, err)
if !errors.Is(err, ErrInvalidInput) {
t.Errorf("expected error %s, got: %s", ErrInvalidInput, err)
}
_, err = sanitizeTracefsPath("./not/escaped")
if err != nil {
t.Errorf("expected no error, got: %s", err)
}
}
func TestValidIdentifier(t *testing.T) {
tests := []struct {
name string
in string
fail bool
}{
{"empty string", "", true},
{"leading number", "1test", true},
{"underscore first", "__x64_syscall", false},
{"contains number", "bpf_trace_run1", false},
{"underscore", "_", false},
{"contains dash", "-EINVAL", true},
{"contains number", "all0wed", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exp := "pass"
if tt.fail {
exp = "fail"
}
if validIdentifier(tt.in) == tt.fail {
t.Errorf("expected string '%s' to %s valid ID check", tt.in, exp)
}
})
}
}
func TestSanitizeIdentifier(t *testing.T) {
tests := []struct {
symbol string
expected string
}{
{"readline", "readline"},
{"main.Func123", "main_Func123"},
{"a.....a", "a_a"},
{"./;'{}[]a", "_a"},
{"***xx**xx###", "_xx_xx_"},
{`@P#r$i%v^3*+t)i&k++--`, "_P_r_i_v_3_t_i_k_"},
}
for i, tt := range tests {
t.Run(fmt.Sprint(i), func(t *testing.T) {
sanitized := sanitizeIdentifier(tt.symbol)
if tt.expected != sanitized {
t.Errorf("Expected sanitized symbol to be '%s', got '%s'", tt.expected, sanitized)
}
})
}
}
func TestGetTracefsPath(t *testing.T) {
path, err := getTracefsPath()
testutils.SkipIfNotSupportedOnOS(t, err)
qt.Assert(t, qt.IsNil(err))
_, err = os.Stat(path)
qt.Assert(t, qt.IsNil(err))
}
|