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
/
abi_test.go
226 lines (193 loc) · 4.5 KB
/
abi_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package ebpf
import (
"testing"
)
func TestCollectionABI(t *testing.T) {
cabi := &CollectionABI{
Maps: map[string]*MapABI{
"a": {
Type: ArrayOfMaps,
KeySize: 4,
ValueSize: 2,
MaxEntries: 3,
InnerMap: &MapABI{
Type: Array,
},
},
},
Programs: map[string]*ProgramABI{
"1": {Type: SocketFilter},
},
}
if err := cabi.CheckSpec(abiFixtureCollectionSpec()); err != nil {
t.Error("ABI check found error:", err)
}
cs := abiFixtureCollectionSpec()
delete(cs.Maps, "a")
if err := cabi.CheckSpec(cs); err == nil {
t.Error("Did not detect missing map")
}
cs = abiFixtureCollectionSpec()
delete(cs.Programs, "1")
if err := cabi.CheckSpec(cs); err == nil {
t.Error("Did not detect missing program")
}
if err := cabi.Check(abiFixtureCollection()); err != nil {
t.Error("ABI check found error:", err)
}
coll := abiFixtureCollection()
coll.Maps["a"].abi.KeySize = 12
if err := cabi.Check(coll); err == nil {
t.Error("Check not check map ABI")
}
delete(coll.Maps, "a")
if err := cabi.Check(coll); err == nil {
t.Error("Check did not detect missing map")
}
coll = abiFixtureCollection()
coll.Programs["1"].abi.Type = TracePoint
if err := cabi.Check(coll); err == nil {
t.Error("Did not check program ABI")
}
delete(coll.Programs, "1")
if err := cabi.Check(coll); err == nil {
t.Error("Check did not detect missing program")
}
}
func TestMapABI(t *testing.T) {
mabi := &MapABI{
Type: ArrayOfMaps,
KeySize: 4,
ValueSize: 2,
MaxEntries: 3,
InnerMap: &MapABI{
Type: Array,
},
}
if err := mabi.Check(abiFixtureMap()); err != nil {
t.Error("ABI check found error:", err)
}
fm := abiFixtureMap()
fm.abi.Type = Hash
if err := mabi.Check(fm); err == nil {
t.Error("Did not detect incorrect type")
}
fm = abiFixtureMap()
fm.abi.KeySize = 3
if err := mabi.Check(fm); err == nil {
t.Error("Did not detect incorrect key size")
}
fm = abiFixtureMap()
fm.abi.ValueSize = 23
if err := mabi.Check(fm); err == nil {
t.Error("Did not detect incorrect value size")
}
fm = abiFixtureMap()
fm.abi.MaxEntries = 23
if err := mabi.Check(fm); err == nil {
t.Error("Did not detect incorrect max entries")
}
fm = abiFixtureMap()
mabi.InnerMap.Type = Hash
if err := mabi.Check(fm); err == nil {
t.Error("Did not detect incorrect inner map type")
}
fm = abiFixtureMap()
mabi.InnerMap = nil
if err := mabi.Check(fm); err == nil {
t.Error("Did not detect missing inner map ABI")
}
}
func TestProgramABI(t *testing.T) {
fabi := &ProgramABI{Type: SocketFilter}
if err := fabi.Check(abiFixtureProgram()); err != nil {
t.Error("ABI check found error:", err)
}
fp := abiFixtureProgram()
fp.abi.Type = TracePoint
if err := fabi.Check(fp); err == nil {
t.Error("Did not detect incorrect type")
}
}
func abiFixtureCollectionSpec() *CollectionSpec {
return &CollectionSpec{
Maps: map[string]*MapSpec{
"a": abiFixtureMapSpec(),
},
Programs: map[string]*ProgramSpec{
"1": abiFixtureProgramSpec(),
},
}
}
func abiFixtureCollection() *Collection {
return &Collection{
Maps: map[string]*Map{
"a": abiFixtureMap(),
},
Programs: map[string]*Program{
"1": abiFixtureProgram(),
},
}
}
func abiFixtureMapSpec() *MapSpec {
return &MapSpec{
Type: ArrayOfMaps,
KeySize: 4,
ValueSize: 2,
MaxEntries: 3,
InnerMap: &MapSpec{
Type: Array,
KeySize: 2,
},
}
}
func abiFixtureMap() *Map {
return &Map{
abi: *newMapABIFromSpec(abiFixtureMapSpec()),
}
}
func abiFixtureProgramSpec() *ProgramSpec {
return &ProgramSpec{
Type: SocketFilter,
}
}
func abiFixtureProgram() *Program {
return &Program{
abi: *newProgramABIFromSpec(abiFixtureProgramSpec()),
}
}
func ExampleCollectionABI() {
abi := CollectionABI{
Maps: map[string]*MapABI{
"a": {
Type: Array,
// Members which aren't specified are not checked
},
// Use an empty ABI if you just want to make sure
// something is present.
"b": {},
},
Programs: map[string]*ProgramABI{
"1": {Type: XDP},
},
}
spec, err := LoadCollectionSpec("from-somewhere.elf")
if err != nil {
panic(err)
}
// CheckSpec only makes sure that all entries of the ABI
// are present. It doesn't check whether the ABI is correct.
// See below for how to do that.
if err := abi.CheckSpec(spec); err != nil {
panic(err)
}
coll, err := NewCollection(spec)
if err != nil {
panic(err)
}
// Check finally compares the ABI and the collection, and
// makes sure that they match.
if err := abi.Check(coll); err != nil {
panic(err)
}
}