| 1 |
package tracefs |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"os" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/go-quicktest/qt" |
| 9 |
|
| 10 |
"github.com/cilium/ebpf/internal/testutils" |
| 11 |
) |
| 12 |
|
| 13 |
// Global symbol, present on all tested kernels. |
| 14 |
const ksym = "vprintk" |
| 15 |
|
| 16 |
func TestKprobeTraceFSGroup(t *testing.T) { |
| 17 |
// Expect <prefix>_<16 random hex chars>. |
| 18 |
g, err := RandomGroup("ebpftest") |
| 19 |
qt.Assert(t, qt.IsNil(err)) |
| 20 |
qt.Assert(t, qt.Matches(g, `ebpftest_[a-f0-9]{16}`)) |
| 21 |
|
| 22 |
// Expect error when the generator's output exceeds 63 characters. |
| 23 |
p := make([]byte, 47) // 63 - 17 (length of the random suffix and underscore) + 1 |
| 24 |
for i := range p { |
| 25 |
p[i] = byte('a') |
| 26 |
} |
| 27 |
_, err = RandomGroup(string(p)) |
| 28 |
qt.Assert(t, qt.Not(qt.IsNil(err))) |
| 29 |
|
| 30 |
// Reject non-alphanumeric characters. |
| 31 |
_, err = RandomGroup("/") |
| 32 |
qt.Assert(t, qt.Not(qt.IsNil(err))) |
| 33 |
} |
| 34 |
|
| 35 |
func TestKprobeToken(t *testing.T) { |
| 36 |
tests := []struct { |
| 37 |
args ProbeArgs |
| 38 |
expected string |
| 39 |
}{ |
| 40 |
{ProbeArgs{Symbol: "symbol"}, "symbol"}, |
| 41 |
{ProbeArgs{Symbol: "symbol", Offset: 1}, "symbol+0x1"}, |
| 42 |
{ProbeArgs{Symbol: "symbol", Offset: 65535}, "symbol+0xffff"}, |
| 43 |
{ProbeArgs{Symbol: "symbol", Offset: 65536}, "symbol+0x10000"}, |
| 44 |
} |
| 45 |
|
| 46 |
for i, tt := range tests { |
| 47 |
t.Run(fmt.Sprint(i), func(t *testing.T) { |
| 48 |
po := KprobeToken(tt.args) |
| 49 |
if tt.expected != po { |
| 50 |
t.Errorf("Expected symbol+offset to be '%s', got '%s'", tt.expected, po) |
| 51 |
} |
| 52 |
}) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func TestNewEvent(t *testing.T) { |
| 57 |
for _, args := range []ProbeArgs{ |
| 58 |
{Type: Kprobe, Symbol: ksym}, |
| 59 |
{Type: Kprobe, Symbol: ksym, Ret: true}, |
| 60 |
{Type: Uprobe, Path: "/bin/bash", Symbol: "main"}, |
| 61 |
{Type: Uprobe, Path: "/bin/bash", Symbol: "main", Ret: true}, |
| 62 |
} { |
| 63 |
name := fmt.Sprintf("%s ret=%v", args.Type, args.Ret) |
| 64 |
t.Run(name, func(t *testing.T) { |
| 65 |
args.Group, _ = RandomGroup("ebpftest") |
| 66 |
|
| 67 |
evt, err := NewEvent(args) |
| 68 |
testutils.SkipIfNotSupportedOnOS(t, err) |
| 69 |
qt.Assert(t, qt.IsNil(err)) |
| 70 |
defer evt.Close() |
| 71 |
|
| 72 |
_, err = NewEvent(args) |
| 73 |
qt.Assert(t, qt.ErrorIs(err, os.ErrExist), |
| 74 |
qt.Commentf("expected consecutive event creation to contain os.ErrExist")) |
| 75 |
|
| 76 |
qt.Assert(t, qt.IsNil(evt.Close())) |
| 77 |
qt.Assert(t, qt.ErrorIs(evt.Close(), os.ErrClosed)) |
| 78 |
}) |
| 79 |
} |
| 80 |
} |