| 1 |
|
| 2 |
|
| 3 |
package gen |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"os" |
| 8 |
"path/filepath" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/cilium/ebpf/internal/testutils" |
| 12 |
) |
| 13 |
|
| 14 |
const minimalSocketFilter = `__attribute__((section("socket"), used)) int main() { return 0; }` |
| 15 |
|
| 16 |
func TestCompile(t *testing.T) { |
| 17 |
if testing.Short() { |
| 18 |
t.SkipNow() |
| 19 |
} |
| 20 |
|
| 21 |
dir := t.TempDir() |
| 22 |
mustWriteFile(t, dir, "test.c", minimalSocketFilter) |
| 23 |
|
| 24 |
err := Compile(CompileArgs{ |
| 25 |
CC: testutils.ClangBin(t), |
| 26 |
DisableStripping: true, |
| 27 |
Workdir: dir, |
| 28 |
Source: filepath.Join(dir, "test.c"), |
| 29 |
Dest: filepath.Join(dir, "test.o"), |
| 30 |
}) |
| 31 |
if err != nil { |
| 32 |
t.Fatal("Can't compile:", err) |
| 33 |
} |
| 34 |
|
| 35 |
stat, err := os.Stat(filepath.Join(dir, "test.o")) |
| 36 |
if err != nil { |
| 37 |
t.Fatal("Can't stat output:", err) |
| 38 |
} |
| 39 |
|
| 40 |
if stat.Size() == 0 { |
| 41 |
t.Error("Compilation creates an empty file") |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestReproducibleCompile(t *testing.T) { |
| 46 |
if testing.Short() { |
| 47 |
t.SkipNow() |
| 48 |
} |
| 49 |
|
| 50 |
clangBin := testutils.ClangBin(t) |
| 51 |
dir := t.TempDir() |
| 52 |
mustWriteFile(t, dir, "test.c", minimalSocketFilter) |
| 53 |
|
| 54 |
err := Compile(CompileArgs{ |
| 55 |
CC: clangBin, |
| 56 |
DisableStripping: true, |
| 57 |
Workdir: dir, |
| 58 |
Source: filepath.Join(dir, "test.c"), |
| 59 |
Dest: filepath.Join(dir, "a.o"), |
| 60 |
}) |
| 61 |
if err != nil { |
| 62 |
t.Fatal("Can't compile:", err) |
| 63 |
} |
| 64 |
|
| 65 |
err = Compile(CompileArgs{ |
| 66 |
CC: clangBin, |
| 67 |
DisableStripping: true, |
| 68 |
Workdir: dir, |
| 69 |
Source: filepath.Join(dir, "test.c"), |
| 70 |
Dest: filepath.Join(dir, "b.o"), |
| 71 |
}) |
| 72 |
if err != nil { |
| 73 |
t.Fatal("Can't compile:", err) |
| 74 |
} |
| 75 |
|
| 76 |
aBytes, err := os.ReadFile(filepath.Join(dir, "a.o")) |
| 77 |
if err != nil { |
| 78 |
t.Fatal(err) |
| 79 |
} |
| 80 |
|
| 81 |
bBytes, err := os.ReadFile(filepath.Join(dir, "b.o")) |
| 82 |
if err != nil { |
| 83 |
t.Fatal(err) |
| 84 |
} |
| 85 |
|
| 86 |
if !bytes.Equal(aBytes, bBytes) { |
| 87 |
t.Error("Compiling the same file twice doesn't give the same result") |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func TestTriggerMissingTarget(t *testing.T) { |
| 92 |
if testing.Short() { |
| 93 |
t.SkipNow() |
| 94 |
} |
| 95 |
|
| 96 |
dir := t.TempDir() |
| 97 |
mustWriteFile(t, dir, "test.c", `_Pragma(__BPF_TARGET_MISSING);`) |
| 98 |
|
| 99 |
err := Compile(CompileArgs{ |
| 100 |
CC: testutils.ClangBin(t), |
| 101 |
Workdir: dir, |
| 102 |
Source: filepath.Join(dir, "test.c"), |
| 103 |
Dest: filepath.Join(dir, "a.o"), |
| 104 |
}) |
| 105 |
|
| 106 |
if err == nil { |
| 107 |
t.Fatal("No error when compiling __BPF_TARGET_MISSING") |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
func mustWriteFile(tb testing.TB, dir, name, contents string) { |
| 112 |
tb.Helper() |
| 113 |
tmpFile := filepath.Join(dir, name) |
| 114 |
if err := os.WriteFile(tmpFile, []byte(contents), 0660); err != nil { |
| 115 |
tb.Fatal(err) |
| 116 |
} |
| 117 |
} |