github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/ebpf/asm_test.go (about)

     1  package ebpf
     2  
     3  import (
     4  	"bytes"
     5  	"embed"
     6  	"testing"
     7  )
     8  
     9  //go:embed asm_test.bpfasm
    10  var assembly embed.FS
    11  
    12  const filename = "asm_test.bpfasm"
    13  
    14  // This test ensures that the format accepted by the dissasembler matches the output of decoded instructions.
    15  // This test doesn't include dissasembly specific features like labels and number formatting.
    16  func TestDecodeEncodeSymmetry(t *testing.T) {
    17  	fileContents, err := assembly.ReadFile(filename)
    18  	if err != nil {
    19  		t.Error(err)
    20  	}
    21  
    22  	disInst, err := AssemblyToInstructions(filename, bytes.NewReader(fileContents))
    23  	if err != nil {
    24  		t.Error(err)
    25  	}
    26  
    27  	var disassembled string
    28  	for _, inst := range disInst {
    29  		disassembled += inst.String() + "\n"
    30  	}
    31  
    32  	encoded, err := Encode(disInst)
    33  	if err != nil {
    34  		t.Error(err)
    35  	}
    36  
    37  	decInst, err := Decode(encoded)
    38  	if err != nil {
    39  		t.Error(err)
    40  	}
    41  
    42  	var decoded string
    43  	for _, inst := range decInst {
    44  		decoded += inst.String() + "\n"
    45  	}
    46  
    47  	if string(fileContents) != decoded {
    48  		t.Error("Encoding and decoding not symetric")
    49  	}
    50  }