74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
|
package ebpf
|
||
|
|
||
|
import (
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestBPF(t *testing.T) {
|
||
|
var mfd MapFD
|
||
|
|
||
|
const beef = 0xbeef0000
|
||
|
const dead = 0xdead0000
|
||
|
|
||
|
t.Run("CreateMap", func(t *testing.T) {
|
||
|
var err error
|
||
|
mfd, err = CreateMap()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
})
|
||
|
|
||
|
var count = 64
|
||
|
|
||
|
t.Run("Add", func(t *testing.T) {
|
||
|
for i := 1; i <= count; i++ {
|
||
|
err := mfd.Add(i, uint64(beef+i))
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
t.Run("GetMap1", func(t *testing.T) {
|
||
|
m, err := mfd.GetMap()
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
} else if len(m) != count {
|
||
|
t.Fatalf("expected len(m) == %d, but got %d: %v", count, len(m), m)
|
||
|
}
|
||
|
for i := 1; i <= count; i++ {
|
||
|
if v, ok := m[i]; !ok {
|
||
|
t.Fatalf("value for index %d not found in map", i)
|
||
|
} else if v != uint64(beef+i) {
|
||
|
t.Fatalf("value for index %d should be %x but found %x", i, beef+i, v)
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
t.Run("Set", func(t *testing.T) {
|
||
|
for i := 1; i <= count; i++ {
|
||
|
err := mfd.Set(i, uint64(dead+i))
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
t.Run("GetMap2", func(t *testing.T) {
|
||
|
m, err := mfd.GetMap()
|
||
|
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
} else if len(m) != count {
|
||
|
t.Fatalf("expected len(m) == %d, but got %d: %v", count, len(m), m)
|
||
|
}
|
||
|
for i := 1; i <= count; i++ {
|
||
|
if v, ok := m[i]; !ok {
|
||
|
t.Fatalf("value for index %d not found in map", i)
|
||
|
} else if v != uint64(dead+i) {
|
||
|
t.Fatalf("value for index %d should be %x but found %x", i, dead+i, v)
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
}
|