Debian Codegraph @

internal/tracefs/kprobe_test.go

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