| 1 |
package btf_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/go-quicktest/qt" |
| 8 |
|
| 9 |
"github.com/cilium/ebpf/btf" |
| 10 |
"github.com/cilium/ebpf/internal/sys" |
| 11 |
"github.com/cilium/ebpf/internal/testutils" |
| 12 |
"github.com/cilium/ebpf/internal/unix" |
| 13 |
) |
| 14 |
|
| 15 |
func TestHandleIterator(t *testing.T) { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
testutils.SkipOnOldKernel(t, "5.11", "vmlinux BTF ID") |
| 20 |
|
| 21 |
it := new(btf.HandleIterator) |
| 22 |
defer it.Handle.Close() |
| 23 |
|
| 24 |
if !it.Next() { |
| 25 |
testutils.SkipIfNotSupportedOnOS(t, it.Err()) |
| 26 |
t.Fatalf("No BTF loaded") |
| 27 |
} |
| 28 |
if it.Handle == nil { |
| 29 |
t.Fatal("Next doesn't assign handle") |
| 30 |
} |
| 31 |
prev := it.ID |
| 32 |
for it.Next() { |
| 33 |
|
| 34 |
if it.Handle == nil { |
| 35 |
t.Fatal("Next doesn't assign handle") |
| 36 |
} |
| 37 |
if it.ID == prev { |
| 38 |
t.Fatal("Iterator doesn't advance ID") |
| 39 |
} |
| 40 |
prev = it.ID |
| 41 |
} |
| 42 |
if err := it.Err(); err != nil { |
| 43 |
t.Fatal("Iteration returned an error:", err) |
| 44 |
} |
| 45 |
|
| 46 |
if it.Handle != nil { |
| 47 |
t.Fatal("Next doesn't clean up handle on last iteration") |
| 48 |
} |
| 49 |
if prev != it.ID { |
| 50 |
t.Fatal("Next changes ID on last iteration") |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func TestParseModuleSplitSpec(t *testing.T) { |
| 55 |
|
| 56 |
testutils.SkipOnOldKernel(t, "5.11", "vmlinux BTF ID") |
| 57 |
|
| 58 |
module, err := btf.FindHandle(func(info *btf.HandleInfo) bool { |
| 59 |
if info.IsModule() { |
| 60 |
t.Log("Using module", info.Name) |
| 61 |
return true |
| 62 |
} |
| 63 |
return false |
| 64 |
}) |
| 65 |
testutils.SkipIfNotSupportedOnOS(t, err) |
| 66 |
if err != nil { |
| 67 |
t.Fatal(err) |
| 68 |
} |
| 69 |
defer module.Close() |
| 70 |
|
| 71 |
vmlinux, err := btf.FindHandle(func(info *btf.HandleInfo) bool { |
| 72 |
return info.IsVmlinux() |
| 73 |
}) |
| 74 |
if err != nil { |
| 75 |
t.Fatal(err) |
| 76 |
} |
| 77 |
defer vmlinux.Close() |
| 78 |
|
| 79 |
base, err := vmlinux.Spec(nil) |
| 80 |
if err != nil { |
| 81 |
t.Fatal(err) |
| 82 |
} |
| 83 |
|
| 84 |
_, err = module.Spec(base) |
| 85 |
if err != nil { |
| 86 |
t.Fatal("Parse module BTF:", err) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
func TestNewHandleFromBTFWithToken(t *testing.T) { |
| 91 |
b, err := btf.NewBuilder([]btf.Type{&btf.Int{"example", 4, btf.Unsigned}}, nil) |
| 92 |
qt.Assert(t, qt.IsNil(err)) |
| 93 |
|
| 94 |
buf, err := b.Marshal(nil, nil) |
| 95 |
qt.Assert(t, qt.IsNil(err)) |
| 96 |
|
| 97 |
testutils.RunWithToken(t, "no-cmd", testutils.Delegated{ |
| 98 |
|
| 99 |
|
| 100 |
Cmds: []sys.Cmd{}, |
| 101 |
Maps: []sys.MapType{sys.BPF_MAP_TYPE_ARRAY}, |
| 102 |
}, func(t *testing.T) { |
| 103 |
h, err := btf.NewHandleFromRawBTF(buf) |
| 104 |
testutils.SkipIfNotSupported(t, err) |
| 105 |
qt.Assert(t, qt.ErrorIs(err, unix.EPERM)) |
| 106 |
h.Close() |
| 107 |
}) |
| 108 |
|
| 109 |
testutils.RunWithToken(t, "success", testutils.Delegated{ |
| 110 |
Cmds: []sys.Cmd{sys.BPF_BTF_LOAD}, |
| 111 |
}, func(t *testing.T) { |
| 112 |
h, err := btf.NewHandleFromRawBTF(buf) |
| 113 |
testutils.SkipIfNotSupported(t, err) |
| 114 |
qt.Assert(t, qt.IsNil(err)) |
| 115 |
h.Close() |
| 116 |
}) |
| 117 |
} |
| 118 |
|
| 119 |
func ExampleHandleIterator() { |
| 120 |
it := new(btf.HandleIterator) |
| 121 |
defer it.Handle.Close() |
| 122 |
|
| 123 |
for it.Next() { |
| 124 |
info, err := it.Handle.Info() |
| 125 |
if err != nil { |
| 126 |
panic(err) |
| 127 |
} |
| 128 |
|
| 129 |
fmt.Printf("Found handle with ID %d and name %s\n", it.ID, info.Name) |
| 130 |
} |
| 131 |
if err := it.Err(); err != nil { |
| 132 |
panic(err) |
| 133 |
} |
| 134 |
} |