| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package smbios |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"encoding/binary" |
| 20 |
"fmt" |
| 21 |
"math" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"github.com/google/go-cmp/cmp" |
| 25 |
) |
| 26 |
|
| 27 |
func Test_memoryStream(t *testing.T) { |
| 28 |
tests := []struct { |
| 29 |
name string |
| 30 |
b []byte |
| 31 |
ss []*Structure |
| 32 |
ok bool |
| 33 |
}{ |
| 34 |
{ |
| 35 |
name: "empty", |
| 36 |
b: nil, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "magic before first paragraph", |
| 40 |
b: makeMemory( |
| 41 |
[]byte{'_', 'S', 'M', '_'}, |
| 42 |
nil, |
| 43 |
nil, |
| 44 |
), |
| 45 |
}, |
| 46 |
{ |
| 47 |
name: "magic after last paragraph", |
| 48 |
b: makeMemory( |
| 49 |
nil, |
| 50 |
nil, |
| 51 |
[]byte{'_', 'S', 'M', '_'}, |
| 52 |
), |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "64, OK", |
| 56 |
b: func() []byte { |
| 57 |
|
| 58 |
|
| 59 |
const addr = 0x00f0 |
| 60 |
epb := mustMarshalEntryPoint(&EntryPoint64Bit{ |
| 61 |
StructureTableMaxSize: 512, |
| 62 |
StructureTableAddress: addr, |
| 63 |
}) |
| 64 |
|
| 65 |
|
| 66 |
b := makeMemory( |
| 67 |
nil, |
| 68 |
epb, |
| 69 |
nil, |
| 70 |
) |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
stream := []byte{ |
| 75 |
0x00, 0x05, 0x01, 0x00, |
| 76 |
0xff, |
| 77 |
0x00, |
| 78 |
0x00, |
| 79 |
|
| 80 |
0x01, 0x0c, 0x02, 0x00, |
| 81 |
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef, |
| 82 |
'd', 'e', 'a', 'd', 'b', 'e', 'e', 'f', 0x00, |
| 83 |
0x00, |
| 84 |
|
| 85 |
127, 0x06, 0x03, 0x00, |
| 86 |
0x01, 0x02, |
| 87 |
'a', 'b', 'c', 'd', 0x00, |
| 88 |
'1', '2', '3', '4', 0x00, |
| 89 |
0x00, |
| 90 |
} |
| 91 |
|
| 92 |
copy(b[addr:], stream) |
| 93 |
|
| 94 |
return b |
| 95 |
}(), |
| 96 |
ss: []*Structure{ |
| 97 |
{ |
| 98 |
Header: Header{ |
| 99 |
Type: 0, |
| 100 |
Length: 5, |
| 101 |
Handle: 1, |
| 102 |
}, |
| 103 |
Formatted: []byte{0xff}, |
| 104 |
}, |
| 105 |
{ |
| 106 |
Header: Header{ |
| 107 |
Type: 1, |
| 108 |
Length: 12, |
| 109 |
Handle: 2, |
| 110 |
}, |
| 111 |
Formatted: []byte{0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xbe, 0xef}, |
| 112 |
Strings: []string{"deadbeef"}, |
| 113 |
}, |
| 114 |
{ |
| 115 |
Header: Header{ |
| 116 |
Type: 127, |
| 117 |
Length: 6, |
| 118 |
Handle: 3, |
| 119 |
}, |
| 120 |
Formatted: []byte{0x01, 0x02}, |
| 121 |
Strings: []string{"abcd", "1234"}, |
| 122 |
}, |
| 123 |
}, |
| 124 |
ok: true, |
| 125 |
}, |
| 126 |
} |
| 127 |
|
| 128 |
for _, tt := range tests { |
| 129 |
t.Run(tt.name, func(t *testing.T) { |
| 130 |
rs := bytes.NewReader(tt.b) |
| 131 |
|
| 132 |
rc, _, err := memoryStream(rs, start, end) |
| 133 |
|
| 134 |
if tt.ok && err != nil { |
| 135 |
t.Fatalf("unexpected error: %v", err) |
| 136 |
} |
| 137 |
if !tt.ok && err == nil { |
| 138 |
t.Fatalf("expected an error, but none occurred: %v", err) |
| 139 |
} |
| 140 |
|
| 141 |
if !tt.ok { |
| 142 |
|
| 143 |
t.Logf("OK error: %v", err) |
| 144 |
return |
| 145 |
} |
| 146 |
defer rc.Close() |
| 147 |
|
| 148 |
ss, err := NewDecoder(rc).Decode() |
| 149 |
if err != nil { |
| 150 |
t.Fatalf("failed to decode structures: %v", err) |
| 151 |
} |
| 152 |
|
| 153 |
if diff := cmp.Diff(tt.ss, ss); diff != "" { |
| 154 |
t.Fatalf("unexpected structures (-want +got):\n%s", diff) |
| 155 |
} |
| 156 |
}) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
const ( |
| 162 |
start = 0x0010 |
| 163 |
end = 0xfff0 |
| 164 |
) |
| 165 |
|
| 166 |
func makeMemory(before, in, after []byte) []byte { |
| 167 |
b := make([]byte, math.MaxUint16) |
| 168 |
|
| 169 |
copy(b[0x0000:start], before) |
| 170 |
copy(b[start:0xfff0], in) |
| 171 |
copy(b[end:0xffff], after) |
| 172 |
|
| 173 |
return b |
| 174 |
} |
| 175 |
|
| 176 |
func mustMarshalEntryPoint(ep EntryPoint) []byte { |
| 177 |
switch x := ep.(type) { |
| 178 |
case *EntryPoint64Bit: |
| 179 |
return marshal64(x) |
| 180 |
default: |
| 181 |
|
| 182 |
panic(fmt.Sprintf("entry point marshaling not implemented for %T", ep)) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
func marshal64(ep *EntryPoint64Bit) []byte { |
| 187 |
b := make([]byte, expLen64) |
| 188 |
|
| 189 |
copy(b[0:5], magic64) |
| 190 |
b[6] = expLen64 |
| 191 |
|
| 192 |
b[7] = ep.Major |
| 193 |
b[8] = ep.Minor |
| 194 |
b[9] = ep.Revision |
| 195 |
b[10] = ep.EntryPointRevision |
| 196 |
b[11] = ep.Reserved |
| 197 |
binary.LittleEndian.PutUint32(b[12:16], ep.StructureTableMaxSize) |
| 198 |
binary.LittleEndian.PutUint64(b[16:24], ep.StructureTableAddress) |
| 199 |
|
| 200 |
var chk uint8 |
| 201 |
for i := range b { |
| 202 |
|
| 203 |
if i == chkIndex64 { |
| 204 |
continue |
| 205 |
} |
| 206 |
|
| 207 |
chk += b[i] |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
b[5] = uint8(256 - int(chk)) |
| 212 |
|
| 213 |
return b |
| 214 |
} |