...

Source file src/github.com/cilium/ebpf/link/perf_event_test.go

Documentation: github.com/cilium/ebpf/link

     1  package link
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/cilium/ebpf/internal/testutils"
     9  	qt "github.com/frankban/quicktest"
    10  )
    11  
    12  func TestTraceEventTypePMU(t *testing.T) {
    13  	// Requires at least 4.17 (e12f03d7031a "perf/core: Implement the 'perf_kprobe' PMU")
    14  	testutils.SkipOnOldKernel(t, "4.17", "perf_kprobe PMU")
    15  
    16  	c := qt.New(t)
    17  
    18  	et, err := getPMUEventType(kprobeType)
    19  	c.Assert(err, qt.IsNil)
    20  	c.Assert(et, qt.Not(qt.Equals), 0)
    21  
    22  	et, err = getPMUEventType(uprobeType)
    23  	c.Assert(err, qt.IsNil)
    24  	c.Assert(et, qt.Not(qt.Equals), 0)
    25  }
    26  
    27  func TestTraceEventID(t *testing.T) {
    28  	c := qt.New(t)
    29  
    30  	// using sys_enter_mkdir here as it exist in old kernel (< 4.9) as well
    31  	eid, err := getTraceEventID("syscalls", "sys_enter_mkdir")
    32  	c.Assert(err, qt.IsNil)
    33  	c.Assert(eid, qt.Not(qt.Equals), 0)
    34  }
    35  
    36  func TestTraceReadID(t *testing.T) {
    37  	_, err := uint64FromFile("/base/path/", "../escaped")
    38  	if !errors.Is(err, errInvalidInput) {
    39  		t.Errorf("expected error %s, got: %s", errInvalidInput, err)
    40  	}
    41  
    42  	_, err = uint64FromFile("/base/path/not", "../not/escaped")
    43  	if !errors.Is(err, os.ErrNotExist) {
    44  		t.Errorf("expected os.ErrNotExist, got: %s", err)
    45  	}
    46  }
    47  
    48  func TestTraceValidID(t *testing.T) {
    49  	tests := []struct {
    50  		name string
    51  		in   string
    52  		fail bool
    53  	}{
    54  		{"empty string", "", true},
    55  		{"leading number", "1test", true},
    56  		{"underscore first", "__x64_syscall", false},
    57  		{"contains number", "bpf_trace_run1", false},
    58  		{"underscore", "_", false},
    59  		{"contains dash", "-EINVAL", true},
    60  		{"contains number", "all0wed", false},
    61  	}
    62  
    63  	for _, tt := range tests {
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			exp := "pass"
    66  			if tt.fail {
    67  				exp = "fail"
    68  			}
    69  
    70  			if isValidTraceID(tt.in) == tt.fail {
    71  				t.Errorf("expected string '%s' to %s valid ID check", tt.in, exp)
    72  			}
    73  		})
    74  	}
    75  }
    76  
    77  func TestHaveBPFLinkPerfEvent(t *testing.T) {
    78  	testutils.CheckFeatureTest(t, haveBPFLinkPerfEvent)
    79  }
    80  

View as plain text