| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"fmt" |
| 8 |
"io" |
| 9 |
"os" |
| 10 |
"os/exec" |
| 11 |
"path/filepath" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
|
| 15 |
"github.com/go-quicktest/qt" |
| 16 |
|
| 17 |
"github.com/cilium/ebpf/cmd/bpf2go/gen" |
| 18 |
"github.com/cilium/ebpf/cmd/bpf2go/internal" |
| 19 |
"github.com/cilium/ebpf/internal/testutils" |
| 20 |
) |
| 21 |
|
| 22 |
const minimalSocketFilter = `__attribute__((section("socket"), used)) int main() { return 0; }` |
| 23 |
|
| 24 |
func TestRun(t *testing.T) { |
| 25 |
clangBin := testutils.ClangBin(t) |
| 26 |
dir := t.TempDir() |
| 27 |
mustWriteFile(t, dir, "test.c", minimalSocketFilter) |
| 28 |
|
| 29 |
modRoot, err := filepath.Abs("../..") |
| 30 |
qt.Assert(t, qt.IsNil(err)) |
| 31 |
|
| 32 |
if _, err := os.Stat(filepath.Join(modRoot, "go.mod")); os.IsNotExist(err) { |
| 33 |
t.Fatal("No go.mod file in", modRoot) |
| 34 |
} |
| 35 |
|
| 36 |
modDir := t.TempDir() |
| 37 |
execInModule := func(name string, args ...string) { |
| 38 |
t.Helper() |
| 39 |
|
| 40 |
cmd := exec.Command(name, args...) |
| 41 |
cmd.Dir = modDir |
| 42 |
if out, err := cmd.CombinedOutput(); err != nil { |
| 43 |
if out := string(out); out != "" { |
| 44 |
t.Log(out) |
| 45 |
} |
| 46 |
t.Fatalf("Can't execute %s: %v", name, args) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
module := internal.CurrentModule |
| 51 |
|
| 52 |
execInModule("go", "mod", "init", "bpf2go-test") |
| 53 |
|
| 54 |
execInModule("go", "mod", "edit", |
| 55 |
|
| 56 |
|
| 57 |
fmt.Sprintf("-require=%[email protected]", module), |
| 58 |
|
| 59 |
fmt.Sprintf("-replace=%s=%s", module, modRoot), |
| 60 |
) |
| 61 |
|
| 62 |
goarches := []string{ |
| 63 |
"amd64", |
| 64 |
"arm64", |
| 65 |
"s390x", |
| 66 |
} |
| 67 |
|
| 68 |
err = run(io.Discard, []string{ |
| 69 |
"-go-package", "main", |
| 70 |
"-output-dir", modDir, |
| 71 |
"-cc", clangBin, |
| 72 |
"-target", strings.Join(goarches, ","), |
| 73 |
"bar", |
| 74 |
filepath.Join(dir, "test.c"), |
| 75 |
}) |
| 76 |
|
| 77 |
if err != nil { |
| 78 |
t.Fatal("Can't run:", err) |
| 79 |
} |
| 80 |
|
| 81 |
mustWriteFile(t, modDir, "main.go", |
| 82 |
` |
| 83 |
package main |
| 84 |
|
| 85 |
func main() { |
| 86 |
var obj barObjects |
| 87 |
println(obj.Main) |
| 88 |
}`) |
| 89 |
|
| 90 |
for _, arch := range goarches { |
| 91 |
t.Run(arch, func(t *testing.T) { |
| 92 |
goBuild := exec.Command("go", "build", "-mod=mod", "-o", "/dev/null") |
| 93 |
goBuild.Dir = modDir |
| 94 |
goBuild.Env = append(os.Environ(), |
| 95 |
"GOOS=linux", |
| 96 |
"GOARCH="+arch, |
| 97 |
"GOPROXY=off", |
| 98 |
"GOSUMDB=off", |
| 99 |
) |
| 100 |
out, err := goBuild.CombinedOutput() |
| 101 |
if err != nil { |
| 102 |
if out := string(out); out != "" { |
| 103 |
t.Log(out) |
| 104 |
} |
| 105 |
t.Error("Can't compile package:", err) |
| 106 |
} |
| 107 |
}) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func TestHelp(t *testing.T) { |
| 112 |
var stdout bytes.Buffer |
| 113 |
err := run(&stdout, []string{"-help"}) |
| 114 |
if err != nil { |
| 115 |
t.Fatal("Can't execute -help") |
| 116 |
} |
| 117 |
|
| 118 |
if stdout.Len() == 0 { |
| 119 |
t.Error("-help doesn't write to stdout") |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
func TestErrorMentionsEnvVar(t *testing.T) { |
| 124 |
err := run(io.Discard, nil) |
| 125 |
qt.Assert(t, qt.StringContains(err.Error(), gopackageEnv), qt.Commentf("Error should include name of environment variable")) |
| 126 |
} |
| 127 |
|
| 128 |
func TestDisableStripping(t *testing.T) { |
| 129 |
dir := t.TempDir() |
| 130 |
mustWriteFile(t, dir, "test.c", minimalSocketFilter) |
| 131 |
|
| 132 |
err := run(io.Discard, []string{ |
| 133 |
"-go-package", "foo", |
| 134 |
"-output-dir", dir, |
| 135 |
"-cc", testutils.ClangBin(t), |
| 136 |
"-strip", "binary-that-certainly-doesnt-exist", |
| 137 |
"-no-strip", |
| 138 |
"bar", |
| 139 |
filepath.Join(dir, "test.c"), |
| 140 |
}) |
| 141 |
|
| 142 |
if err != nil { |
| 143 |
t.Fatal("Can't run with stripping disabled:", err) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
func TestConvertGOARCH(t *testing.T) { |
| 148 |
tmp := t.TempDir() |
| 149 |
mustWriteFile(t, tmp, "test.c", |
| 150 |
` |
| 151 |
#ifndef __TARGET_ARCH_x86 |
| 152 |
#error __TARGET_ARCH_x86 is not defined |
| 153 |
#endif`, |
| 154 |
) |
| 155 |
|
| 156 |
b2g := bpf2go{ |
| 157 |
pkg: "test", |
| 158 |
stdout: io.Discard, |
| 159 |
identStem: "test", |
| 160 |
cc: testutils.ClangBin(t), |
| 161 |
disableStripping: true, |
| 162 |
sourceFile: tmp + "/test.c", |
| 163 |
outputDir: tmp, |
| 164 |
} |
| 165 |
|
| 166 |
if err := b2g.convert(gen.TargetsByGoArch()["amd64"], nil); err != nil { |
| 167 |
t.Fatal("Can't target GOARCH:", err) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
func TestCTypes(t *testing.T) { |
| 172 |
var ct cTypes |
| 173 |
valid := []string{ |
| 174 |
"abcdefghijklmnopqrstuvqxyABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_", |
| 175 |
"y", |
| 176 |
} |
| 177 |
for _, value := range valid { |
| 178 |
if err := ct.Set(value); err != nil { |
| 179 |
t.Fatalf("Set returned an error for %q: %s", value, err) |
| 180 |
} |
| 181 |
} |
| 182 |
qt.Assert(t, qt.ContentEquals(ct, valid)) |
| 183 |
|
| 184 |
for _, value := range []string{ |
| 185 |
"", |
| 186 |
" ", |
| 187 |
" frood", |
| 188 |
"foo\nbar", |
| 189 |
".", |
| 190 |
",", |
| 191 |
"+", |
| 192 |
"-", |
| 193 |
} { |
| 194 |
ct = nil |
| 195 |
if err := ct.Set(value); err == nil { |
| 196 |
t.Fatalf("Set did not return an error for %q", value) |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
ct = nil |
| 201 |
qt.Assert(t, qt.IsNil(ct.Set("foo"))) |
| 202 |
qt.Assert(t, qt.IsNotNil(ct.Set("foo"))) |
| 203 |
} |
| 204 |
|
| 205 |
func TestParseArgs(t *testing.T) { |
| 206 |
const ( |
| 207 |
pkg = "eee" |
| 208 |
outputDir = "." |
| 209 |
csource = "testdata/minimal.c" |
| 210 |
stem = "a" |
| 211 |
) |
| 212 |
t.Run("makebase", func(t *testing.T) { |
| 213 |
t.Setenv(gopackageEnv, pkg) |
| 214 |
basePath, _ := filepath.Abs("barfoo") |
| 215 |
args := []string{"-makebase", basePath, stem, csource} |
| 216 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 217 |
qt.Assert(t, qt.IsNil(err)) |
| 218 |
qt.Assert(t, qt.Equals(b2g.makeBase, basePath)) |
| 219 |
}) |
| 220 |
|
| 221 |
t.Run("makebase from env", func(t *testing.T) { |
| 222 |
t.Setenv(gopackageEnv, pkg) |
| 223 |
basePath, _ := filepath.Abs("barfoo") |
| 224 |
args := []string{stem, csource} |
| 225 |
t.Setenv("BPF2GO_MAKEBASE", basePath) |
| 226 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 227 |
qt.Assert(t, qt.IsNil(err)) |
| 228 |
qt.Assert(t, qt.Equals(b2g.makeBase, basePath)) |
| 229 |
}) |
| 230 |
|
| 231 |
t.Run("makebase flag overrides env", func(t *testing.T) { |
| 232 |
t.Setenv(gopackageEnv, pkg) |
| 233 |
basePathFlag, _ := filepath.Abs("barfoo") |
| 234 |
basePathEnv, _ := filepath.Abs("foobar") |
| 235 |
args := []string{"-makebase", basePathFlag, stem, csource} |
| 236 |
t.Setenv("BPF2GO_MAKEBASE", basePathEnv) |
| 237 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 238 |
qt.Assert(t, qt.IsNil(err)) |
| 239 |
qt.Assert(t, qt.Equals(b2g.makeBase, basePathFlag)) |
| 240 |
}) |
| 241 |
|
| 242 |
t.Run("cc defaults to clang", func(t *testing.T) { |
| 243 |
t.Setenv(gopackageEnv, pkg) |
| 244 |
args := []string{stem, csource} |
| 245 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 246 |
qt.Assert(t, qt.IsNil(err)) |
| 247 |
qt.Assert(t, qt.Equals(b2g.cc, "clang")) |
| 248 |
}) |
| 249 |
|
| 250 |
t.Run("cc", func(t *testing.T) { |
| 251 |
t.Setenv(gopackageEnv, pkg) |
| 252 |
args := []string{"-cc", "barfoo", stem, csource} |
| 253 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 254 |
qt.Assert(t, qt.IsNil(err)) |
| 255 |
qt.Assert(t, qt.Equals(b2g.cc, "barfoo")) |
| 256 |
}) |
| 257 |
|
| 258 |
t.Run("cc from env", func(t *testing.T) { |
| 259 |
t.Setenv(gopackageEnv, pkg) |
| 260 |
args := []string{stem, csource} |
| 261 |
t.Setenv("BPF2GO_CC", "barfoo") |
| 262 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 263 |
qt.Assert(t, qt.IsNil(err)) |
| 264 |
qt.Assert(t, qt.Equals(b2g.cc, "barfoo")) |
| 265 |
}) |
| 266 |
|
| 267 |
t.Run("cc flag overrides env", func(t *testing.T) { |
| 268 |
t.Setenv(gopackageEnv, pkg) |
| 269 |
args := []string{"-cc", "barfoo", stem, csource} |
| 270 |
t.Setenv("BPF2GO_CC", "foobar") |
| 271 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 272 |
qt.Assert(t, qt.IsNil(err)) |
| 273 |
qt.Assert(t, qt.Equals(b2g.cc, "barfoo")) |
| 274 |
}) |
| 275 |
|
| 276 |
t.Run("strip defaults to llvm-strip", func(t *testing.T) { |
| 277 |
t.Setenv(gopackageEnv, pkg) |
| 278 |
args := []string{stem, csource} |
| 279 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 280 |
qt.Assert(t, qt.IsNil(err)) |
| 281 |
qt.Assert(t, qt.Equals(b2g.strip, "llvm-strip")) |
| 282 |
}) |
| 283 |
|
| 284 |
t.Run("strip", func(t *testing.T) { |
| 285 |
t.Setenv(gopackageEnv, pkg) |
| 286 |
args := []string{"-strip", "barfoo", stem, csource} |
| 287 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 288 |
qt.Assert(t, qt.IsNil(err)) |
| 289 |
qt.Assert(t, qt.Equals(b2g.strip, "barfoo")) |
| 290 |
}) |
| 291 |
|
| 292 |
t.Run("strip from env", func(t *testing.T) { |
| 293 |
t.Setenv(gopackageEnv, pkg) |
| 294 |
args := []string{stem, csource} |
| 295 |
t.Setenv("BPF2GO_STRIP", "barfoo") |
| 296 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 297 |
qt.Assert(t, qt.IsNil(err)) |
| 298 |
qt.Assert(t, qt.Equals(b2g.strip, "barfoo")) |
| 299 |
}) |
| 300 |
|
| 301 |
t.Run("strip flag overrides env", func(t *testing.T) { |
| 302 |
t.Setenv(gopackageEnv, pkg) |
| 303 |
args := []string{"-strip", "barfoo", stem, csource} |
| 304 |
t.Setenv("BPF2GO_STRIP", "foobar") |
| 305 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 306 |
qt.Assert(t, qt.IsNil(err)) |
| 307 |
qt.Assert(t, qt.Equals(b2g.strip, "barfoo")) |
| 308 |
}) |
| 309 |
|
| 310 |
t.Run("no strip defaults to false", func(t *testing.T) { |
| 311 |
t.Setenv(gopackageEnv, pkg) |
| 312 |
args := []string{stem, csource} |
| 313 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 314 |
qt.Assert(t, qt.IsNil(err)) |
| 315 |
qt.Assert(t, qt.IsFalse(b2g.disableStripping)) |
| 316 |
}) |
| 317 |
|
| 318 |
t.Run("no strip", func(t *testing.T) { |
| 319 |
t.Setenv(gopackageEnv, pkg) |
| 320 |
args := []string{"-no-strip", stem, csource} |
| 321 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 322 |
qt.Assert(t, qt.IsNil(err)) |
| 323 |
qt.Assert(t, qt.IsTrue(b2g.disableStripping)) |
| 324 |
}) |
| 325 |
|
| 326 |
t.Run("cflags flag", func(t *testing.T) { |
| 327 |
t.Setenv(gopackageEnv, pkg) |
| 328 |
args := []string{"-cflags", "x y z", stem, csource} |
| 329 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 330 |
qt.Assert(t, qt.IsNil(err)) |
| 331 |
qt.Assert(t, qt.DeepEquals(b2g.cFlags, []string{"x", "y", "z"})) |
| 332 |
}) |
| 333 |
|
| 334 |
t.Run("cflags multi flag", func(t *testing.T) { |
| 335 |
t.Setenv(gopackageEnv, pkg) |
| 336 |
args := []string{"-cflags", "x y z", "-cflags", "u v", stem, csource} |
| 337 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 338 |
qt.Assert(t, qt.IsNil(err)) |
| 339 |
qt.Assert(t, qt.DeepEquals(b2g.cFlags, []string{"u", "v"})) |
| 340 |
}) |
| 341 |
|
| 342 |
t.Run("cflags flag and args", func(t *testing.T) { |
| 343 |
t.Setenv(gopackageEnv, pkg) |
| 344 |
args := []string{"-cflags", "x y z", "stem", csource, "--", "u", "v"} |
| 345 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 346 |
qt.Assert(t, qt.IsNil(err)) |
| 347 |
qt.Assert(t, qt.DeepEquals(b2g.cFlags, []string{"x", "y", "z", "u", "v"})) |
| 348 |
}) |
| 349 |
|
| 350 |
t.Run("cflags from env", func(t *testing.T) { |
| 351 |
t.Setenv(gopackageEnv, pkg) |
| 352 |
args := []string{stem, csource} |
| 353 |
t.Setenv("BPF2GO_CFLAGS", "x y z") |
| 354 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 355 |
qt.Assert(t, qt.IsNil(err)) |
| 356 |
qt.Assert(t, qt.DeepEquals(b2g.cFlags, []string{"x", "y", "z"})) |
| 357 |
}) |
| 358 |
|
| 359 |
t.Run("cflags flag overrides env", func(t *testing.T) { |
| 360 |
t.Setenv(gopackageEnv, pkg) |
| 361 |
args := []string{"-cflags", "u v", stem, csource} |
| 362 |
t.Setenv("BPF2GO_CFLAGS", "x y z") |
| 363 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 364 |
qt.Assert(t, qt.IsNil(err)) |
| 365 |
qt.Assert(t, qt.DeepEquals(b2g.cFlags, []string{"u", "v"})) |
| 366 |
}) |
| 367 |
|
| 368 |
t.Run("go package overrides env", func(t *testing.T) { |
| 369 |
t.Setenv(gopackageEnv, pkg) |
| 370 |
args := []string{"-go-package", "aaa", stem, csource} |
| 371 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 372 |
qt.Assert(t, qt.IsNil(err)) |
| 373 |
qt.Assert(t, qt.Equals(b2g.pkg, "aaa")) |
| 374 |
}) |
| 375 |
|
| 376 |
t.Run("output dir", func(t *testing.T) { |
| 377 |
t.Setenv(gopackageEnv, pkg) |
| 378 |
args := []string{"-output-dir", outputDir, stem, csource} |
| 379 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 380 |
qt.Assert(t, qt.IsNil(err)) |
| 381 |
qt.Assert(t, qt.Equals(b2g.outputDir, outputDir)) |
| 382 |
}) |
| 383 |
|
| 384 |
t.Run("output suffix default", func(t *testing.T) { |
| 385 |
t.Setenv(gopackageEnv, pkg) |
| 386 |
b2g, err := newB2G(&bytes.Buffer{}, []string{stem, csource}) |
| 387 |
qt.Assert(t, qt.IsNil(err)) |
| 388 |
qt.Assert(t, qt.Equals(b2g.outputSuffix, "")) |
| 389 |
}) |
| 390 |
|
| 391 |
t.Run("output suffix GOFILE=_test", func(t *testing.T) { |
| 392 |
t.Setenv(gopackageEnv, pkg) |
| 393 |
t.Setenv("GOFILE", "foo_test.go") |
| 394 |
b2g, err := newB2G(&bytes.Buffer{}, []string{stem, csource}) |
| 395 |
qt.Assert(t, qt.IsNil(err)) |
| 396 |
qt.Assert(t, qt.Equals(b2g.outputSuffix, "_test")) |
| 397 |
}) |
| 398 |
|
| 399 |
t.Run("output suffix custom", func(t *testing.T) { |
| 400 |
t.Setenv(gopackageEnv, pkg) |
| 401 |
t.Setenv("GOFILE", "foo_test.go") |
| 402 |
args := []string{"-output-suffix", "_custom", stem, csource} |
| 403 |
b2g, err := newB2G(&bytes.Buffer{}, args) |
| 404 |
qt.Assert(t, qt.IsNil(err)) |
| 405 |
qt.Assert(t, qt.Equals(b2g.outputSuffix, "_custom")) |
| 406 |
}) |
| 407 |
} |
| 408 |
|
| 409 |
func mustWriteFile(tb testing.TB, dir, name, contents string) { |
| 410 |
tb.Helper() |
| 411 |
tmpFile := filepath.Join(dir, name) |
| 412 |
if err := os.WriteFile(tmpFile, []byte(contents), 0660); err != nil { |
| 413 |
tb.Fatal(err) |
| 414 |
} |
| 415 |
} |