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