-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathemulator_linux_map_array_test.go
103 lines (81 loc) · 1.83 KB
/
emulator_linux_map_array_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package mimic
import (
"testing"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
)
func TestLinuxPerCPUArray(t *testing.T) {
emu := NewLinuxEmulator()
vm := NewVM(VMOptEmulator(emu), VMOptSetvCPUs(2))
m := &LinuxPerCPUArrayMap{
Spec: &ebpf.MapSpec{
Name: "per-cpu-array",
KeySize: 4,
ValueSize: 4,
MaxEntries: 5,
},
}
// Add to emulator, allocating it
err := emu.AddMap("per-cpu-array", m)
if err != nil {
t.Fatal(err)
}
const CPU0 = 0
const CPU1 = 1
ne := GetNativeEndianness()
keyVal := uint32(1)
key := make([]byte, 4)
ne.PutUint32(key, keyVal)
valValCPU0 := uint32(2)
valueCPU0 := make([]byte, 4)
ne.PutUint32(valueCPU0, valValCPU0)
valValCPU1 := uint32(3)
valueCPU1 := make([]byte, 4)
ne.PutUint32(valueCPU1, valValCPU1)
err = m.Update(key, valueCPU0, 0, CPU0)
if err != nil {
t.Fatal(err)
}
err = m.Update(key, valueCPU1, 0, CPU1)
if err != nil {
t.Fatal(err)
}
valPtrCPU0, err := m.Lookup(key, CPU0)
if err != nil {
t.Fatal(err)
}
valPtrCPU1, err := m.Lookup(key, CPU1)
if err != nil {
t.Fatal(err)
}
valEntryCPU0, off, found := vm.MemoryController.GetEntry(valPtrCPU0)
if !found {
t.Fatal("entry not found")
}
vmmem, ok := valEntryCPU0.Object.(VMMem)
if !ok {
t.Fatal("not VM-mem")
}
lookupValCPU0, err := vmmem.Load(off, asm.Word)
if err != nil {
t.Fatal(err)
}
if lookupValCPU0 != uint64(valValCPU0) {
t.Fatalf("expected %d, got %d", valValCPU0, lookupValCPU0)
}
valEntryCPU1, off, found := vm.MemoryController.GetEntry(valPtrCPU1)
if !found {
t.Fatal("entry not found")
}
vmmem, ok = valEntryCPU1.Object.(VMMem)
if !ok {
t.Fatal("not VM-mem")
}
lookupValCPU1, err := vmmem.Load(off, asm.Word)
if err != nil {
t.Fatal(err)
}
if lookupValCPU1 != uint64(valValCPU1) {
t.Fatalf("expected %d, got %d", valValCPU1, lookupValCPU1)
}
}