github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/internal/sys/syscall_test.go (about)

     1  package sys
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/cilium/ebpf/internal/unix"
     8  
     9  	"github.com/go-quicktest/qt"
    10  )
    11  
    12  func TestObjName(t *testing.T) {
    13  	name := NewObjName("more_than_16_characters_long")
    14  	if name[len(name)-1] != 0 {
    15  		t.Error("NewBPFObjName doesn't null terminate")
    16  	}
    17  	if len(name) != unix.BPF_OBJ_NAME_LEN {
    18  		t.Errorf("Name is %d instead of %d bytes long", len(name), unix.BPF_OBJ_NAME_LEN)
    19  	}
    20  }
    21  
    22  func TestWrappedErrno(t *testing.T) {
    23  	a := error(wrappedErrno{unix.EINVAL})
    24  	b := error(unix.EINVAL)
    25  
    26  	if a == b {
    27  		t.Error("wrappedErrno is comparable to plain errno")
    28  	}
    29  
    30  	if !errors.Is(a, b) {
    31  		t.Error("errors.Is(wrappedErrno, errno) returns false")
    32  	}
    33  
    34  	if errors.Is(a, unix.EAGAIN) {
    35  		t.Error("errors.Is(wrappedErrno, EAGAIN) returns true")
    36  	}
    37  
    38  	notsupp := wrappedErrno{ENOTSUPP}
    39  	qt.Assert(t, qt.StringContains(notsupp.Error(), "operation not supported"))
    40  }
    41  
    42  func TestSyscallError(t *testing.T) {
    43  	err := errors.New("foo")
    44  	foo := Error(err, unix.EINVAL)
    45  
    46  	if !errors.Is(foo, unix.EINVAL) {
    47  		t.Error("SyscallError is not the wrapped errno")
    48  	}
    49  
    50  	if !errors.Is(foo, err) {
    51  		t.Error("SyscallError is not the wrapped error")
    52  	}
    53  
    54  	if errors.Is(unix.EINVAL, foo) {
    55  		t.Error("Errno is the SyscallError")
    56  	}
    57  
    58  	if errors.Is(err, foo) {
    59  		t.Error("Error is the SyscallError")
    60  	}
    61  }