/
main_test.go
280 lines (248 loc) · 5.87 KB
/
main_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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package main
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strings"
"testing"
qt "github.com/frankban/quicktest"
"github.com/google/go-cmp/cmp"
)
func TestRun(t *testing.T) {
if testing.Short() {
t.Skip("Not compiling with -short")
}
dir := mustWriteTempFile(t, "test.c", minimalSocketFilter)
cwd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
modRoot := filepath.Clean(filepath.Join(cwd, "../.."))
if _, err := os.Stat(filepath.Join(modRoot, "go.mod")); os.IsNotExist(err) {
t.Fatal("No go.mod file in", modRoot)
}
tmpDir, err := os.MkdirTemp("", "bpf2go-module-*")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
execInModule := func(name string, args ...string) {
t.Helper()
cmd := exec.Command(name, args...)
cmd.Dir = tmpDir
if out, err := cmd.CombinedOutput(); err != nil {
if out := string(out); out != "" {
t.Log(out)
}
t.Fatalf("Can't execute %s: %v", name, args)
}
}
execInModule("go", "mod", "init", "bpf2go-test")
execInModule("go", "mod", "edit",
// Require the module. The version doesn't matter due to the replace
// below.
fmt.Sprintf("-require=%s@v0.0.0", ebpfModule),
// Replace the module with the current version.
fmt.Sprintf("-replace=%s=%s", ebpfModule, modRoot),
)
err = run(io.Discard, "foo", tmpDir, []string{
"-cc", clangBin,
"bar",
filepath.Join(dir, "test.c"),
})
if err != nil {
t.Fatal("Can't run:", err)
}
for _, arch := range []string{
"amd64", // little-endian
"s390x", // big-endian
} {
t.Run(arch, func(t *testing.T) {
goBin := exec.Command("go", "build", "-mod=mod")
goBin.Dir = tmpDir
goBin.Env = append(os.Environ(),
"GOOS=linux",
"GOARCH="+arch,
)
out, err := goBin.CombinedOutput()
if err != nil {
if out := string(out); out != "" {
t.Log(out)
}
t.Error("Can't compile package:", err)
}
})
}
}
func TestHelp(t *testing.T) {
var stdout bytes.Buffer
err := run(&stdout, "", "", []string{"-help"})
if err != nil {
t.Fatal("Can't execute -help")
}
if stdout.Len() == 0 {
t.Error("-help doesn't write to stdout")
}
}
func TestDisableStripping(t *testing.T) {
dir := mustWriteTempFile(t, "test.c", minimalSocketFilter)
err := run(io.Discard, "foo", dir, []string{
"-cc", "clang-9",
"-strip", "binary-that-certainly-doesnt-exist",
"-no-strip",
"bar",
filepath.Join(dir, "test.c"),
})
if err != nil {
t.Fatal("Can't run with stripping disabled:", err)
}
}
func TestCollectTargets(t *testing.T) {
clangArches := make(map[string][]string)
linuxArchesLE := make(map[string][]string)
linuxArchesBE := make(map[string][]string)
for arch, archTarget := range targetByGoArch {
clangArches[archTarget.clang] = append(clangArches[archTarget.clang], arch)
if archTarget.clang == "bpfel" {
linuxArchesLE[archTarget.linux] = append(linuxArchesLE[archTarget.linux], arch)
continue
}
linuxArchesBE[archTarget.linux] = append(linuxArchesBE[archTarget.linux], arch)
}
for i := range clangArches {
sort.Strings(clangArches[i])
}
for i := range linuxArchesLE {
sort.Strings(linuxArchesLE[i])
}
for i := range linuxArchesBE {
sort.Strings(linuxArchesBE[i])
}
nativeTarget := make(map[target][]string)
for arch, archTarget := range targetByGoArch {
if arch == runtime.GOARCH {
if archTarget.clang == "bpfel" {
nativeTarget[archTarget] = linuxArchesLE[archTarget.linux]
} else {
nativeTarget[archTarget] = linuxArchesBE[archTarget.linux]
}
break
}
}
tests := []struct {
targets []string
want map[target][]string
}{
{
[]string{"bpf", "bpfel", "bpfeb"},
map[target][]string{
{"bpf", ""}: nil,
{"bpfel", ""}: clangArches["bpfel"],
{"bpfeb", ""}: clangArches["bpfeb"],
},
},
{
[]string{"amd64", "386"},
map[target][]string{
{"bpfel", "x86"}: linuxArchesLE["x86"],
},
},
{
[]string{"amd64", "arm64be"},
map[target][]string{
{"bpfeb", "arm64"}: linuxArchesBE["arm64"],
{"bpfel", "x86"}: linuxArchesLE["x86"],
},
},
{
[]string{"native"},
nativeTarget,
},
}
for _, test := range tests {
name := strings.Join(test.targets, ",")
t.Run(name, func(t *testing.T) {
have, err := collectTargets(test.targets)
if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(test.want, have); diff != "" {
t.Errorf("Result mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestCollectTargetsErrors(t *testing.T) {
tests := []struct {
name string
target string
}{
{"unknown", "frood"},
{"no linux target", "mips64p32le"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := collectTargets([]string{test.target})
if err == nil {
t.Fatal("Function did not return an error")
}
t.Log("Error message:", err)
})
}
}
func TestConvertGOARCH(t *testing.T) {
tmp := mustWriteTempFile(t, "test.c",
`
#ifndef __TARGET_ARCH_x86
#error __TARGET_ARCH_x86 is not defined
#endif`,
)
b2g := bpf2go{
pkg: "test",
stdout: io.Discard,
ident: "test",
cc: clangBin,
disableStripping: true,
sourceFile: tmp + "/test.c",
outputDir: tmp,
}
if err := b2g.convert(targetByGoArch["amd64"], nil); err != nil {
t.Fatal("Can't target GOARCH:", err)
}
}
func TestCTypes(t *testing.T) {
var ct cTypes
valid := []string{
"abcdefghijklmnopqrstuvqxyABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_",
"y",
}
for _, value := range valid {
if err := ct.Set(value); err != nil {
t.Fatalf("Set returned an error for %q: %s", value, err)
}
}
qt.Assert(t, ct, qt.ContentEquals, cTypes(valid))
for _, value := range []string{
"",
" ",
" frood",
"foo\nbar",
".",
",",
"+",
"-",
} {
ct = nil
if err := ct.Set(value); err == nil {
t.Fatalf("Set did not return an error for %q", value)
}
}
ct = nil
qt.Assert(t, ct.Set("foo"), qt.IsNil)
qt.Assert(t, ct.Set("foo"), qt.IsNotNil)
}