File size: 3,378 Bytes
4bcc2be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | // Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package buildinfo
import (
"bytes"
"fmt"
"io"
"testing"
)
type byteExe struct {
b []byte
}
func (x *byteExe) DataReader(addr uint64) (io.ReaderAt, error) {
if addr >= uint64(len(x.b)) {
return nil, fmt.Errorf("ReadData(%d) out of bounds of %d-byte slice", addr, len(x.b))
}
return bytes.NewReader(x.b[addr:]), nil
}
func (x *byteExe) DataStart() (uint64, uint64) {
return 0, uint64(len(x.b))
}
func TestSearchMagic(t *testing.T) {
tests := []struct {
name string
data []byte
want uint64
wantErr error
}{
{
name: "beginning",
data: func() []byte {
b := make([]byte, buildInfoHeaderSize)
copy(b, buildInfoMagic)
return b
}(),
want: 0,
},
{
name: "offset",
data: func() []byte {
b := make([]byte, 512)
copy(b[4*buildInfoAlign:], buildInfoMagic)
return b
}(),
want: 4 * buildInfoAlign,
},
{
name: "second_chunk",
data: func() []byte {
b := make([]byte, 4*searchChunkSize)
copy(b[searchChunkSize+4*buildInfoAlign:], buildInfoMagic)
return b
}(),
want: searchChunkSize + 4*buildInfoAlign,
},
{
name: "second_chunk_short",
data: func() []byte {
// Magic is 64-bytes into the second chunk,
// which is short; only exactly long enough to
// hold the header.
b := make([]byte, searchChunkSize+4*buildInfoAlign+buildInfoHeaderSize)
copy(b[searchChunkSize+4*buildInfoAlign:], buildInfoMagic)
return b
}(),
want: searchChunkSize + 4*buildInfoAlign,
},
{
name: "missing",
data: func() []byte {
b := make([]byte, buildInfoHeaderSize)
return b
}(),
wantErr: errNotGoExe,
},
{
name: "too_short",
data: func() []byte {
// There needs to be space for the entire
// header, not just the magic.
b := make([]byte, len(buildInfoMagic))
copy(b, buildInfoMagic)
return b
}(),
wantErr: errNotGoExe,
},
{
name: "misaligned",
data: func() []byte {
b := make([]byte, 512)
copy(b[7:], buildInfoMagic)
return b
}(),
wantErr: errNotGoExe,
},
{
name: "misaligned_across_chunk",
data: func() []byte {
// Magic crosses chunk boundary. By definition,
// it has to be misaligned.
b := make([]byte, 2*searchChunkSize)
copy(b[searchChunkSize-8:], buildInfoMagic)
return b
}(),
wantErr: errNotGoExe,
},
{
name: "header_across_chunk",
data: func() []byte {
// The magic is aligned within the first chunk,
// but the rest of the 32-byte header crosses
// the chunk boundary.
b := make([]byte, 2*searchChunkSize)
copy(b[searchChunkSize-buildInfoAlign:], buildInfoMagic)
return b
}(),
want: searchChunkSize - buildInfoAlign,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
x := &byteExe{tc.data}
dataAddr, dataSize := x.DataStart()
addr, err := searchMagic(x, dataAddr, dataSize)
if tc.wantErr == nil {
if err != nil {
t.Errorf("searchMagic got err %v want nil", err)
}
if addr != tc.want {
t.Errorf("searchMagic got addr %d want %d", addr, tc.want)
}
} else {
if err != tc.wantErr {
t.Errorf("searchMagic got err %v want %v", err, tc.wantErr)
}
}
})
}
}
|