This repository has been archived by the owner on Mar 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathelf_test.go
169 lines (141 loc) · 3.83 KB
/
elf_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package ebpf
import (
"path/filepath"
"reflect"
"testing"
)
func TestLoadCollectionSpec(t *testing.T) {
files, err := filepath.Glob("testdata/loader-*.elf")
if err != nil {
t.Fatal(err)
}
for _, file := range files {
t.Run(file, func(t *testing.T) {
spec, err := LoadCollectionSpec(file)
if err != nil {
t.Fatal("Can't parse ELF:", err)
}
hashMapSpec := &MapSpec{
"hash_map",
Hash,
4,
2,
1,
0,
nil,
}
checkMapSpec(t, spec.Maps, "hash_map", hashMapSpec)
checkMapSpec(t, spec.Maps, "array_of_hash_map", &MapSpec{
"hash_map", ArrayOfMaps, 4, 0, 2, 0, hashMapSpec,
})
hashMap2Spec := &MapSpec{
"",
Hash,
4,
1,
2,
1,
nil,
}
checkMapSpec(t, spec.Maps, "hash_map2", hashMap2Spec)
checkMapSpec(t, spec.Maps, "hash_of_hash_map", &MapSpec{
"", HashOfMaps, 4, 0, 2, 0, hashMap2Spec,
})
checkProgramSpec(t, spec.Programs, "xdp_prog", &ProgramSpec{
Type: XDP,
License: "MIT",
KernelVersion: 0,
})
checkProgramSpec(t, spec.Programs, "no_relocation", &ProgramSpec{
Type: SocketFilter,
License: "MIT",
KernelVersion: 0,
})
t.Log(spec.Programs["xdp_prog"].Instructions)
coll, err := NewCollection(spec)
if err != nil {
t.Fatal(err)
}
defer coll.Close()
hash := coll.DetachMap("hash_map")
if hash == nil {
t.Fatal("Program not returned from DetachMap")
}
defer hash.Close()
if _, ok := coll.Programs["hash_map"]; ok {
t.Error("DetachMap doesn't remove map from Maps")
}
prog := coll.DetachProgram("xdp_prog")
if prog == nil {
t.Fatal("Program not returned from DetachProgram")
}
defer prog.Close()
if _, ok := coll.Programs["xdp_prog"]; ok {
t.Error("DetachProgram doesn't remove program from Programs")
}
})
}
}
func checkMapSpec(t *testing.T, maps map[string]*MapSpec, name string, want *MapSpec) {
t.Helper()
have, ok := maps[name]
if !ok {
t.Errorf("Missing map %s", name)
return
}
mapSpecEqual(t, name, have, want)
}
func mapSpecEqual(t *testing.T, name string, have, want *MapSpec) {
t.Helper()
if have.Type != want.Type {
t.Errorf("%s: expected type %v, got %v", name, want.Type, have.Type)
}
if have.KeySize != want.KeySize {
t.Errorf("%s: expected key size %v, got %v", name, want.KeySize, have.KeySize)
}
if have.ValueSize != want.ValueSize {
t.Errorf("%s: expected value size %v, got %v", name, want.ValueSize, have.ValueSize)
}
if have.MaxEntries != want.MaxEntries {
t.Errorf("%s: expected max entries %v, got %v", name, want.MaxEntries, have.MaxEntries)
}
if have.Flags != want.Flags {
t.Errorf("%s: expected flags %v, got %v", name, want.Flags, have.Flags)
}
switch {
case have.InnerMap != nil && want.InnerMap == nil:
t.Errorf("%s: extraneous InnerMap", name)
case have.InnerMap == nil && want.InnerMap != nil:
t.Errorf("%s: missing InnerMap", name)
case have.InnerMap != nil && want.InnerMap != nil:
mapSpecEqual(t, name+".InnerMap", have.InnerMap, want.InnerMap)
}
}
func checkProgramSpec(t *testing.T, progs map[string]*ProgramSpec, name string, want *ProgramSpec) {
t.Helper()
have, ok := progs[name]
if !ok {
t.Fatalf("Missing program %s", name)
return
}
if have.License != want.License {
t.Errorf("%s: expected %v license, got %v", name, want.License, have.License)
}
if have.Type != want.Type {
t.Errorf("%s: expected %v program, got %v", name, want.Type, have.Type)
}
if want.Instructions != nil && !reflect.DeepEqual(have.Instructions, want.Instructions) {
t.Log("Expected program")
t.Log(want.Instructions)
t.Log("Actual program")
t.Log(want.Instructions)
t.Error("Instructions do not match")
}
}
func TestLoadInvalidMap(t *testing.T) {
_, err := LoadCollectionSpec("testdata/invalid_map.elf")
t.Log(err)
if err == nil {
t.Fatal("should be fail")
}
}