github.com/cilium/ebpf@v0.10.0/link/perf_event_test.go (about)

     1  package link
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/cilium/ebpf/internal/testutils"
     8  	qt "github.com/frankban/quicktest"
     9  )
    10  
    11  func TestTraceEventID(t *testing.T) {
    12  	c := qt.New(t)
    13  
    14  	eid, err := getTraceEventID("syscalls", "sys_enter_mmap")
    15  	c.Assert(err, qt.IsNil)
    16  	c.Assert(eid, qt.Not(qt.Equals), 0)
    17  }
    18  
    19  func TestSanitizePath(t *testing.T) {
    20  	_, err := sanitizePath("/base/path/", "../escaped")
    21  	if !errors.Is(err, errInvalidInput) {
    22  		t.Errorf("expected error %s, got: %s", errInvalidInput, err)
    23  	}
    24  
    25  	_, err = sanitizePath("/base/path/not", "../not/escaped")
    26  	if err != nil {
    27  		t.Errorf("expected no error, got: %s", err)
    28  	}
    29  }
    30  
    31  func TestTraceValidID(t *testing.T) {
    32  	tests := []struct {
    33  		name string
    34  		in   string
    35  		fail bool
    36  	}{
    37  		{"empty string", "", true},
    38  		{"leading number", "1test", true},
    39  		{"underscore first", "__x64_syscall", false},
    40  		{"contains number", "bpf_trace_run1", false},
    41  		{"underscore", "_", false},
    42  		{"contains dash", "-EINVAL", true},
    43  		{"contains number", "all0wed", false},
    44  	}
    45  
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			exp := "pass"
    49  			if tt.fail {
    50  				exp = "fail"
    51  			}
    52  
    53  			if isValidTraceID(tt.in) == tt.fail {
    54  				t.Errorf("expected string '%s' to %s valid ID check", tt.in, exp)
    55  			}
    56  		})
    57  	}
    58  }
    59  
    60  func TestHaveBPFLinkPerfEvent(t *testing.T) {
    61  	testutils.CheckFeatureTest(t, haveBPFLinkPerfEvent)
    62  }