github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/internal/vdso_test.go (about)

     1  package internal
     2  
     3  import (
     4  	"encoding/binary"
     5  	"errors"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/go-quicktest/qt"
    10  )
    11  
    12  func TestAuxvVDSOMemoryAddress(t *testing.T) {
    13  	for _, testcase := range []struct {
    14  		source  string
    15  		is32bit bool
    16  		address uint64
    17  	}{
    18  		{"auxv64le.bin", false, 0x7ffd377e5000},
    19  		{"auxv32le.bin", true, 0xb7fc3000},
    20  	} {
    21  		t.Run(testcase.source, func(t *testing.T) {
    22  			av, err := newAuxFileReader("testdata/"+testcase.source, binary.LittleEndian, testcase.is32bit)
    23  			if err != nil {
    24  				t.Fatal(err)
    25  			}
    26  			t.Cleanup(func() { av.Close() })
    27  
    28  			addr, err := vdsoMemoryAddress(av)
    29  			if err != nil {
    30  				t.Fatal(err)
    31  			}
    32  
    33  			if uint64(addr) != testcase.address {
    34  				t.Errorf("Expected vDSO memory address %x, got %x", testcase.address, addr)
    35  			}
    36  		})
    37  	}
    38  }
    39  
    40  func TestAuxvNoVDSO(t *testing.T) {
    41  	// Copy of auxv.bin with the vDSO pointer removed.
    42  	av, err := newAuxFileReader("testdata/auxv64le_no_vdso.bin", binary.LittleEndian, false)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	t.Cleanup(func() { av.Close() })
    47  
    48  	_, err = vdsoMemoryAddress(av)
    49  	if want, got := errAuxvNoVDSO, err; !errors.Is(got, want) {
    50  		t.Fatalf("expected error '%v', got: %v", want, got)
    51  	}
    52  }
    53  
    54  func TestVDSOVersion(t *testing.T) {
    55  	_, err := vdsoVersion()
    56  	qt.Assert(t, qt.IsNil(err))
    57  }
    58  
    59  func TestLinuxVersionCodeEmbedded(t *testing.T) {
    60  	tests := []struct {
    61  		file    string
    62  		version uint32
    63  	}{
    64  		{
    65  			"testdata/vdso.bin",
    66  			uint32(328828), // 5.4.124
    67  		},
    68  		{
    69  			"testdata/vdso_multiple_notes.bin",
    70  			uint32(328875), // Container Optimized OS v85 with a 5.4.x kernel
    71  		},
    72  	}
    73  
    74  	for _, test := range tests {
    75  		t.Run(test.file, func(t *testing.T) {
    76  			vdso, err := os.Open(test.file)
    77  			if err != nil {
    78  				t.Fatal(err)
    79  			}
    80  			defer vdso.Close()
    81  
    82  			vc, err := vdsoLinuxVersionCode(vdso)
    83  			if err != nil {
    84  				t.Fatal(err)
    85  			}
    86  
    87  			if vc != test.version {
    88  				t.Errorf("Expected version code %d, got %d", test.version, vc)
    89  			}
    90  		})
    91  	}
    92  }