| 1 | package sys |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "math" |
| 6 | "testing" |
| 7 | "unsafe" |
| 8 | |
| 9 | "github.com/cilium/ebpf/internal/testutils/testmain" |
| 10 | "github.com/cilium/ebpf/internal/unix" |
| 11 | |
| 12 | "github.com/go-quicktest/qt" |
| 13 | ) |
| 14 | |
| 15 | func TestBPF(t *testing.T) { |
| 16 | fd, err := MapCreate(&MapCreateAttr{ |
| 17 | MapType: BPF_MAP_TYPE_HASH, |
| 18 | KeySize: 4, |
| 19 | ValueSize: 4, |
| 20 | MaxEntries: 1, |
| 21 | }) |
| 22 | qt.Assert(t, qt.IsNil(err)) |
| 23 | qt.Assert(t, qt.IsNil(fd.Close())) |
| 24 | } |
| 25 | |
| 26 | func TestBPFAllocations(t *testing.T) { |
| 27 | n := testing.AllocsPerRun(10, func() { |
| 28 | var attr struct { |
| 29 | Foo uint64 |
| 30 | } |
| 31 | |
| 32 | BPF(math.MaxUint32, unsafe.Pointer(&attr), 0) |
| 33 | }) |
| 34 | qt.Assert(t, qt.Equals(n, 0)) |
| 35 | } |
| 36 | |
| 37 | func TestObjName(t *testing.T) { |
| 38 | name := NewObjName("more_than_16_characters_long") |
| 39 | if name[len(name)-1] != 0 { |
| 40 | t.Error("NewBPFObjName doesn't null terminate") |
| 41 | } |
| 42 | if len(name) != BPF_OBJ_NAME_LEN { |
| 43 | t.Errorf("Name is %d instead of %d bytes long", len(name), BPF_OBJ_NAME_LEN) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestWrappedErrno(t *testing.T) { |
| 48 | a := error(wrappedErrno{unix.EINVAL}) |
| 49 | b := error(unix.EINVAL) |
| 50 | |
| 51 | if a == b { |
| 52 | t.Error("wrappedErrno is comparable to plain errno") |
| 53 | } |
| 54 | |
| 55 | if !errors.Is(a, b) { |
| 56 | t.Error("errors.Is(wrappedErrno, errno) returns false") |
| 57 | } |
| 58 | |
| 59 | if errors.Is(a, unix.EAGAIN) { |
| 60 | t.Error("errors.Is(wrappedErrno, EAGAIN) returns true") |
| 61 | } |
| 62 | |
| 63 | notsupp := wrappedErrno{ENOTSUPP} |
| 64 | qt.Assert(t, qt.StringContains(notsupp.Error(), "operation not supported")) |
| 65 | } |
| 66 | |
| 67 | func TestSyscallError(t *testing.T) { |
| 68 | err := errors.New("foo") |
| 69 | foo := Error(err, unix.EINVAL) |
| 70 | |
| 71 | if !errors.Is(foo, unix.EINVAL) { |
| 72 | t.Error("SyscallError is not the wrapped errno") |
| 73 | } |
| 74 | |
| 75 | if !errors.Is(foo, err) { |
| 76 | t.Error("SyscallError is not the wrapped error") |
| 77 | } |
| 78 | |
| 79 | if errors.Is(unix.EINVAL, foo) { |
| 80 | t.Error("Errno is the SyscallError") |
| 81 | } |
| 82 | |
| 83 | if errors.Is(err, foo) { |
| 84 | t.Error("Error is the SyscallError") |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestMain(m *testing.M) { |
| 89 | testmain.Run(m) |
| 90 | } |