Skip to content

Commit

Permalink
link: add helper to load test programs
Browse files Browse the repository at this point in the history
The link tests need a program of a certain type. Add a helper function
to cut down on copy paste code.
  • Loading branch information
lmb committed Nov 24, 2021
1 parent 7bb243c commit b937084
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 230 deletions.
2 changes: 1 addition & 1 deletion link/cgroup_test.go
Expand Up @@ -53,7 +53,7 @@ func TestProgAttachCgroupAllowMulti(t *testing.T) {

// It's currently not possible for a program to replace
// itself.
prog2 := mustCgroupEgressProgram(t)
prog2 := mustLoadProgram(t, ebpf.CGroupSKB, ebpf.AttachCGroupInetEgress, "")
testLink(t, link, prog2)
}

Expand Down
37 changes: 6 additions & 31 deletions link/iter_test.go
Expand Up @@ -5,26 +5,13 @@ import (
"testing"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal/testutils"
)

func TestIter(t *testing.T) {
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Type: ebpf.Tracing,
AttachType: ebpf.AttachTraceIter,
AttachTo: "bpf_map",
Instructions: asm.Instructions{
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
},
License: "MIT",
})
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't load program:", err)
}
defer prog.Close()
testutils.SkipOnOldKernel(t, "5.9", "bpf_map iter")

prog := mustLoadProgram(t, ebpf.Tracing, ebpf.AttachTraceIter, "bpf_map")

it, err := AttachIter(IterOptions{
Program: prog,
Expand Down Expand Up @@ -52,21 +39,9 @@ func TestIter(t *testing.T) {
}

func TestIterMapElements(t *testing.T) {
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Type: ebpf.Tracing,
AttachType: ebpf.AttachTraceIter,
AttachTo: "bpf_map_elem",
Instructions: asm.Instructions{
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
},
License: "MIT",
})
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Can't load program:", err)
}
defer prog.Close()
testutils.SkipOnOldKernel(t, "5.9", "bpf_map_elem iter")

prog := mustLoadProgram(t, ebpf.Tracing, ebpf.AttachTraceIter, "bpf_map_elem")

arr, err := ebpf.NewMap(&ebpf.MapSpec{
Type: ebpf.Array,
Expand Down
22 changes: 2 additions & 20 deletions link/kprobe_test.go
Expand Up @@ -13,24 +13,10 @@ import (
"github.com/cilium/ebpf/internal/unix"
)

var kprobeSpec = ebpf.ProgramSpec{
Type: ebpf.Kprobe,
License: "MIT",
Instructions: asm.Instructions{
// set exit code to 0
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
},
}

func TestKprobe(t *testing.T) {
c := qt.New(t)

prog, err := ebpf.NewProgram(&kprobeSpec)
if err != nil {
t.Fatal(err)
}
defer prog.Close()
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "")

k, err := Kprobe("printk", prog)
c.Assert(err, qt.IsNil)
Expand All @@ -48,11 +34,7 @@ func TestKprobe(t *testing.T) {
func TestKretprobe(t *testing.T) {
c := qt.New(t)

prog, err := ebpf.NewProgram(&kprobeSpec)
if err != nil {
t.Fatal(err)
}
defer prog.Close()
prog := mustLoadProgram(t, ebpf.Kprobe, 0, "")

k, err := Kretprobe("printk", prog)
c.Assert(err, qt.IsNil)
Expand Down
55 changes: 31 additions & 24 deletions link/link_test.go
Expand Up @@ -84,30 +84,7 @@ func mustCgroupFixtures(t *testing.T) (*os.File, *ebpf.Program) {

testutils.SkipIfNotSupported(t, haveProgAttach())

return testutils.CreateCgroup(t), mustCgroupEgressProgram(t)
}

func mustCgroupEgressProgram(t *testing.T) *ebpf.Program {
t.Helper()

prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Type: ebpf.CGroupSKB,
AttachType: ebpf.AttachCGroupInetEgress,
License: "MIT",
Instructions: asm.Instructions{
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
},
})
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
prog.Close()
})

return prog
return testutils.CreateCgroup(t), mustLoadProgram(t, ebpf.CGroupSKB, ebpf.AttachCGroupInetEgress, "")
}

func testLink(t *testing.T, link Link, prog *ebpf.Program) {
Expand Down Expand Up @@ -168,3 +145,33 @@ func testLink(t *testing.T, link Link, prog *ebpf.Program) {
t.Fatalf("%T.Close returns an error: %s", link, err)
}
}

func mustLoadProgram(tb testing.TB, typ ebpf.ProgramType, attachType ebpf.AttachType, attachTo string) *ebpf.Program {
tb.Helper()

license := "MIT"
switch typ {
case ebpf.RawTracepoint, ebpf.LSM:
license = "GPL"
}

prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Type: typ,
AttachType: attachType,
AttachTo: attachTo,
License: license,
Instructions: asm.Instructions{
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
},
})
if err != nil {
tb.Fatal(err)
}

tb.Cleanup(func() {
prog.Close()
})

return prog
}
14 changes: 1 addition & 13 deletions link/netns_test.go
Expand Up @@ -12,7 +12,7 @@ import (
func TestSkLookup(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.8", "sk_lookup program")

prog := mustCreateSkLookupProgram(t)
prog := mustLoadProgram(t, ebpf.SkLookup, ebpf.AttachSkLookup, "")

netns, err := os.Open("/proc/self/ns/net")
if err != nil {
Expand All @@ -33,18 +33,6 @@ func TestSkLookup(t *testing.T) {
testLink(t, link, prog)
}

func mustCreateSkLookupProgram(tb testing.TB) *ebpf.Program {
tb.Helper()

prog, err := createSkLookupProgram()
if err != nil {
tb.Fatal(err)
}
tb.Cleanup(func() { prog.Close() })

return prog
}

func createSkLookupProgram() (*ebpf.Program, error) {
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Type: ebpf.SkLookup,
Expand Down
18 changes: 2 additions & 16 deletions link/program_test.go
Expand Up @@ -4,30 +4,16 @@ import (
"testing"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal/testutils"
)

func TestProgramAlter(t *testing.T) {
testutils.SkipOnOldKernel(t, "4.13", "SkSKB type")

var err error
var prog *ebpf.Program
prog, err = ebpf.NewProgram(&ebpf.ProgramSpec{
Type: ebpf.SkSKB,
Instructions: asm.Instructions{
asm.LoadImm(asm.R0, 0, asm.DWord),
asm.Return(),
},
License: "MIT",
})
if err != nil {
t.Fatal(err)
}
defer prog.Close()
prog := mustLoadProgram(t, ebpf.SkSKB, 0, "")

var sockMap *ebpf.Map
sockMap, err = ebpf.NewMap(&ebpf.MapSpec{
sockMap, err := ebpf.NewMap(&ebpf.MapSpec{
Type: ebpf.MapType(15), // BPF_MAP_TYPE_SOCKMAP
KeySize: 4,
ValueSize: 4,
Expand Down
29 changes: 3 additions & 26 deletions link/raw_tracepoint_test.go
Expand Up @@ -4,26 +4,13 @@ import (
"testing"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal/testutils"
)

func TestRawTracepoint(t *testing.T) {
testutils.SkipOnOldKernel(t, "4.17", "BPF_RAW_TRACEPOINT API")

prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Type: ebpf.RawTracepoint,
AttachType: ebpf.AttachNone,
Instructions: asm.Instructions{
asm.LoadImm(asm.R0, 0, asm.DWord),
asm.Return(),
},
License: "GPL",
})
if err != nil {
t.Fatal(err)
}
defer prog.Close()
prog := mustLoadProgram(t, ebpf.RawTracepoint, 0, "")

link, err := AttachRawTracepoint(RawTracepointOptions{
Name: "cgroup_mkdir",
Expand All @@ -39,18 +26,8 @@ func TestRawTracepoint(t *testing.T) {
func TestRawTracepoint_writable(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.2", "BPF_RAW_TRACEPOINT_WRITABLE API")

prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Type: ebpf.RawTracepointWritable,
AttachType: ebpf.AttachNone,
Instructions: asm.Instructions{
asm.LoadImm(asm.R0, 0, asm.DWord),
asm.Return(),
},
License: "GPL",
})
if err != nil {
t.Fatal(err)
}
prog := mustLoadProgram(t, ebpf.RawTracepoint, 0, "")

defer prog.Close()

link, err := AttachRawTracepoint(RawTracepointOptions{
Expand Down
28 changes: 3 additions & 25 deletions link/tracepoint_test.go
Expand Up @@ -6,35 +6,17 @@ import (
"testing"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal/testutils"
"github.com/cilium/ebpf/internal/unix"

qt "github.com/frankban/quicktest"
)

var (
tracepointSpec = ebpf.ProgramSpec{
Type: ebpf.TracePoint,
License: "MIT",
Instructions: asm.Instructions{
// set exit code to 0
asm.Mov.Imm(asm.R0, 0),
asm.Return(),
},
}
)

func TestTracepoint(t *testing.T) {

// Requires at least 4.7 (98b5c2c65c29 "perf, bpf: allow bpf programs attach to tracepoints")
testutils.SkipOnOldKernel(t, "4.7", "tracepoint support")

prog, err := ebpf.NewProgram(&tracepointSpec)
if err != nil {
t.Fatal(err)
}
defer prog.Close()
prog := mustLoadProgram(t, ebpf.TracePoint, 0, "")

// printk is guaranteed to be present.
// Kernels before 4.14 don't support attaching to syscall tracepoints.
Expand All @@ -52,13 +34,9 @@ func TestTracepointMissing(t *testing.T) {
// Requires at least 4.7 (98b5c2c65c29 "perf, bpf: allow bpf programs attach to tracepoints")
testutils.SkipOnOldKernel(t, "4.7", "tracepoint support")

prog, err := ebpf.NewProgram(&tracepointSpec)
if err != nil {
t.Fatal(err)
}
defer prog.Close()
prog := mustLoadProgram(t, ebpf.TracePoint, 0, "")

_, err = Tracepoint("missing", "foobazbar", prog)
_, err := Tracepoint("missing", "foobazbar", prog)
if !errors.Is(err, os.ErrNotExist) {
t.Error("Expected os.ErrNotExist, got", err)
}
Expand Down
31 changes: 2 additions & 29 deletions link/tracing_test.go
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
)
Expand Down Expand Up @@ -83,20 +82,7 @@ func TestTracing(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Type: tt.programType,
AttachType: tt.attachType,
AttachTo: tt.attachTo,
Instructions: asm.Instructions{
asm.LoadImm(asm.R0, 0, asm.DWord),
asm.Return(),
},
License: "MIT",
})
if err != nil {
t.Fatal(err)
}
defer prog.Close()
prog := mustLoadProgram(t, tt.programType, tt.attachType, tt.attachTo)

link, err := AttachTracing(TracingOptions{Program: prog})
if err != nil {
Expand All @@ -111,20 +97,7 @@ func TestTracing(t *testing.T) {
func TestLSM(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.11", "BPF_LINK_TYPE_TRACING")

prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Type: ebpf.LSM,
AttachType: ebpf.AttachLSMMac,
AttachTo: "file_mprotect",
Instructions: asm.Instructions{
asm.LoadImm(asm.R0, 0, asm.DWord),
asm.Return(),
},
License: "GPL",
})
if err != nil {
t.Fatal(err)
}
defer prog.Close()
prog := mustLoadProgram(t, ebpf.LSM, ebpf.AttachLSMMac, "file_mprotect")

link, err := AttachLSM(LSMOptions{Program: prog})
if err != nil {
Expand Down

0 comments on commit b937084

Please sign in to comment.