github.com/cilium/ebpf@v0.16.0/cmd/bpf2go/gen/compile_test.go (about)

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