| 1 |
package linux |
| 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 |
|
| 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 |
skipIfNotSupportedOnOS(t, err) |
| 57 |
qt.Assert(t, qt.IsNil(err)) |
| 58 |
} |
| 59 |
|
| 60 |
func TestLinuxVersionCodeEmbedded(t *testing.T) { |
| 61 |
tests := []struct { |
| 62 |
file string |
| 63 |
version uint32 |
| 64 |
}{ |
| 65 |
{ |
| 66 |
"testdata/vdso.bin", |
| 67 |
uint32(328828), |
| 68 |
}, |
| 69 |
{ |
| 70 |
"testdata/vdso_multiple_notes.bin", |
| 71 |
uint32(328875), |
| 72 |
}, |
| 73 |
} |
| 74 |
|
| 75 |
for _, test := range tests { |
| 76 |
t.Run(test.file, func(t *testing.T) { |
| 77 |
vdso, err := os.Open(test.file) |
| 78 |
if err != nil { |
| 79 |
t.Fatal(err) |
| 80 |
} |
| 81 |
defer vdso.Close() |
| 82 |
|
| 83 |
vc, err := vdsoLinuxVersionCode(vdso) |
| 84 |
if err != nil { |
| 85 |
t.Fatal(err) |
| 86 |
} |
| 87 |
|
| 88 |
if vc != test.version { |
| 89 |
t.Errorf("Expected version code %d, got %d", test.version, vc) |
| 90 |
} |
| 91 |
}) |
| 92 |
} |
| 93 |
} |