whatcanGOwrong
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"unsafe"
|
||||
|
||||
"github.com/cilium/ebpf/internal/testutils"
|
||||
)
|
||||
|
||||
func TestLoadingSpec(t *testing.T) {
|
||||
spec, err := loadTest()
|
||||
testutils.SkipIfNotSupported(t, err)
|
||||
if err != nil {
|
||||
t.Fatal("Can't load spec:", err)
|
||||
}
|
||||
|
||||
if spec == nil {
|
||||
t.Fatal("Got a nil spec")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadingObjects(t *testing.T) {
|
||||
var objs testObjects
|
||||
err := loadTestObjects(&objs, nil)
|
||||
testutils.SkipIfNotSupported(t, err)
|
||||
if err != nil {
|
||||
t.Fatal("Can't load objects:", err)
|
||||
}
|
||||
defer objs.Close()
|
||||
|
||||
if objs.Filter == nil {
|
||||
t.Error("Loading returns an object with nil programs")
|
||||
}
|
||||
|
||||
if objs.Map1 == nil {
|
||||
t.Error("Loading returns an object with nil maps")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTypes(t *testing.T) {
|
||||
if testEHOOPY != 0 {
|
||||
t.Error("Expected testEHOOPY to be 0, got", testEHOOPY)
|
||||
}
|
||||
if testEFROOD != 1 {
|
||||
t.Error("Expected testEFROOD to be 0, got", testEFROOD)
|
||||
}
|
||||
|
||||
e := testE(0)
|
||||
if size := unsafe.Sizeof(e); size != 4 {
|
||||
t.Error("Expected size of exampleE to be 4, got", size)
|
||||
}
|
||||
|
||||
bf := testBarfoo{}
|
||||
if size := unsafe.Sizeof(bf); size != 16 {
|
||||
t.Error("Expected size of exampleE to be 16, got", size)
|
||||
}
|
||||
if reflect.TypeOf(bf.Bar).Kind() != reflect.Int64 {
|
||||
t.Error("Expected testBarfoo.Bar to be int64")
|
||||
}
|
||||
if reflect.TypeOf(bf.Baz).Kind() != reflect.Bool {
|
||||
t.Error("Expected testBarfoo.Baz to be bool")
|
||||
}
|
||||
if reflect.TypeOf(bf.Boo) != reflect.TypeOf(e) {
|
||||
t.Error("Expected testBarfoo.Boo to be exampleE")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// Package test checks that the code generated by bpf2go conforms to a
|
||||
// specific API.
|
||||
package test
|
||||
|
||||
// $BPF_CLANG and $BPF_CFLAGS are set by the Makefile.
|
||||
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -cc $BPF_CLANG test ../testdata/minimal.c
|
||||
@@ -0,0 +1,133 @@
|
||||
// Code generated by bpf2go; DO NOT EDIT.
|
||||
//go:build arm64be || armbe || mips || mips64 || mips64p32 || ppc64 || s390 || s390x || sparc || sparc64
|
||||
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/cilium/ebpf"
|
||||
)
|
||||
|
||||
type testBarfoo struct {
|
||||
Bar int64
|
||||
Baz bool
|
||||
_ [3]byte
|
||||
Boo testE
|
||||
}
|
||||
|
||||
type testE uint32
|
||||
|
||||
const (
|
||||
testEHOOPY testE = 0
|
||||
testEFROOD testE = 1
|
||||
)
|
||||
|
||||
// loadTest returns the embedded CollectionSpec for test.
|
||||
func loadTest() (*ebpf.CollectionSpec, error) {
|
||||
reader := bytes.NewReader(_TestBytes)
|
||||
spec, err := ebpf.LoadCollectionSpecFromReader(reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't load test: %w", err)
|
||||
}
|
||||
|
||||
return spec, err
|
||||
}
|
||||
|
||||
// loadTestObjects loads test and converts it into a struct.
|
||||
//
|
||||
// The following types are suitable as obj argument:
|
||||
//
|
||||
// *testObjects
|
||||
// *testPrograms
|
||||
// *testMaps
|
||||
//
|
||||
// See ebpf.CollectionSpec.LoadAndAssign documentation for details.
|
||||
func loadTestObjects(obj interface{}, opts *ebpf.CollectionOptions) error {
|
||||
spec, err := loadTest()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return spec.LoadAndAssign(obj, opts)
|
||||
}
|
||||
|
||||
// testSpecs contains maps and programs before they are loaded into the kernel.
|
||||
//
|
||||
// It can be passed ebpf.CollectionSpec.Assign.
|
||||
type testSpecs struct {
|
||||
testProgramSpecs
|
||||
testMapSpecs
|
||||
}
|
||||
|
||||
// testSpecs contains programs before they are loaded into the kernel.
|
||||
//
|
||||
// It can be passed ebpf.CollectionSpec.Assign.
|
||||
type testProgramSpecs struct {
|
||||
Filter *ebpf.ProgramSpec `ebpf:"filter"`
|
||||
}
|
||||
|
||||
// testMapSpecs contains maps before they are loaded into the kernel.
|
||||
//
|
||||
// It can be passed ebpf.CollectionSpec.Assign.
|
||||
type testMapSpecs struct {
|
||||
Map1 *ebpf.MapSpec `ebpf:"map1"`
|
||||
}
|
||||
|
||||
// testObjects contains all objects after they have been loaded into the kernel.
|
||||
//
|
||||
// It can be passed to loadTestObjects or ebpf.CollectionSpec.LoadAndAssign.
|
||||
type testObjects struct {
|
||||
testPrograms
|
||||
testMaps
|
||||
}
|
||||
|
||||
func (o *testObjects) Close() error {
|
||||
return _TestClose(
|
||||
&o.testPrograms,
|
||||
&o.testMaps,
|
||||
)
|
||||
}
|
||||
|
||||
// testMaps contains all maps after they have been loaded into the kernel.
|
||||
//
|
||||
// It can be passed to loadTestObjects or ebpf.CollectionSpec.LoadAndAssign.
|
||||
type testMaps struct {
|
||||
Map1 *ebpf.Map `ebpf:"map1"`
|
||||
}
|
||||
|
||||
func (m *testMaps) Close() error {
|
||||
return _TestClose(
|
||||
m.Map1,
|
||||
)
|
||||
}
|
||||
|
||||
// testPrograms contains all programs after they have been loaded into the kernel.
|
||||
//
|
||||
// It can be passed to loadTestObjects or ebpf.CollectionSpec.LoadAndAssign.
|
||||
type testPrograms struct {
|
||||
Filter *ebpf.Program `ebpf:"filter"`
|
||||
}
|
||||
|
||||
func (p *testPrograms) Close() error {
|
||||
return _TestClose(
|
||||
p.Filter,
|
||||
)
|
||||
}
|
||||
|
||||
func _TestClose(closers ...io.Closer) error {
|
||||
for _, closer := range closers {
|
||||
if err := closer.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Do not access this directly.
|
||||
//
|
||||
//go:embed test_bpfeb.o
|
||||
var _TestBytes []byte
|
||||
Binary file not shown.
@@ -0,0 +1,133 @@
|
||||
// Code generated by bpf2go; DO NOT EDIT.
|
||||
//go:build 386 || amd64 || amd64p32 || arm || arm64 || loong64 || mips64le || mips64p32le || mipsle || ppc64le || riscv64
|
||||
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/cilium/ebpf"
|
||||
)
|
||||
|
||||
type testBarfoo struct {
|
||||
Bar int64
|
||||
Baz bool
|
||||
_ [3]byte
|
||||
Boo testE
|
||||
}
|
||||
|
||||
type testE uint32
|
||||
|
||||
const (
|
||||
testEHOOPY testE = 0
|
||||
testEFROOD testE = 1
|
||||
)
|
||||
|
||||
// loadTest returns the embedded CollectionSpec for test.
|
||||
func loadTest() (*ebpf.CollectionSpec, error) {
|
||||
reader := bytes.NewReader(_TestBytes)
|
||||
spec, err := ebpf.LoadCollectionSpecFromReader(reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't load test: %w", err)
|
||||
}
|
||||
|
||||
return spec, err
|
||||
}
|
||||
|
||||
// loadTestObjects loads test and converts it into a struct.
|
||||
//
|
||||
// The following types are suitable as obj argument:
|
||||
//
|
||||
// *testObjects
|
||||
// *testPrograms
|
||||
// *testMaps
|
||||
//
|
||||
// See ebpf.CollectionSpec.LoadAndAssign documentation for details.
|
||||
func loadTestObjects(obj interface{}, opts *ebpf.CollectionOptions) error {
|
||||
spec, err := loadTest()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return spec.LoadAndAssign(obj, opts)
|
||||
}
|
||||
|
||||
// testSpecs contains maps and programs before they are loaded into the kernel.
|
||||
//
|
||||
// It can be passed ebpf.CollectionSpec.Assign.
|
||||
type testSpecs struct {
|
||||
testProgramSpecs
|
||||
testMapSpecs
|
||||
}
|
||||
|
||||
// testSpecs contains programs before they are loaded into the kernel.
|
||||
//
|
||||
// It can be passed ebpf.CollectionSpec.Assign.
|
||||
type testProgramSpecs struct {
|
||||
Filter *ebpf.ProgramSpec `ebpf:"filter"`
|
||||
}
|
||||
|
||||
// testMapSpecs contains maps before they are loaded into the kernel.
|
||||
//
|
||||
// It can be passed ebpf.CollectionSpec.Assign.
|
||||
type testMapSpecs struct {
|
||||
Map1 *ebpf.MapSpec `ebpf:"map1"`
|
||||
}
|
||||
|
||||
// testObjects contains all objects after they have been loaded into the kernel.
|
||||
//
|
||||
// It can be passed to loadTestObjects or ebpf.CollectionSpec.LoadAndAssign.
|
||||
type testObjects struct {
|
||||
testPrograms
|
||||
testMaps
|
||||
}
|
||||
|
||||
func (o *testObjects) Close() error {
|
||||
return _TestClose(
|
||||
&o.testPrograms,
|
||||
&o.testMaps,
|
||||
)
|
||||
}
|
||||
|
||||
// testMaps contains all maps after they have been loaded into the kernel.
|
||||
//
|
||||
// It can be passed to loadTestObjects or ebpf.CollectionSpec.LoadAndAssign.
|
||||
type testMaps struct {
|
||||
Map1 *ebpf.Map `ebpf:"map1"`
|
||||
}
|
||||
|
||||
func (m *testMaps) Close() error {
|
||||
return _TestClose(
|
||||
m.Map1,
|
||||
)
|
||||
}
|
||||
|
||||
// testPrograms contains all programs after they have been loaded into the kernel.
|
||||
//
|
||||
// It can be passed to loadTestObjects or ebpf.CollectionSpec.LoadAndAssign.
|
||||
type testPrograms struct {
|
||||
Filter *ebpf.Program `ebpf:"filter"`
|
||||
}
|
||||
|
||||
func (p *testPrograms) Close() error {
|
||||
return _TestClose(
|
||||
p.Filter,
|
||||
)
|
||||
}
|
||||
|
||||
func _TestClose(closers ...io.Closer) error {
|
||||
for _, closer := range closers {
|
||||
if err := closer.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Do not access this directly.
|
||||
//
|
||||
//go:embed test_bpfel.o
|
||||
var _TestBytes []byte
|
||||
Binary file not shown.
Reference in New Issue
Block a user