| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package smbios_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"bytes" |
| 19 |
"testing" |
| 20 |
|
| 21 |
"github.com/digitalocean/go-smbios/smbios" |
| 22 |
"github.com/google/go-cmp/cmp" |
| 23 |
) |
| 24 |
|
| 25 |
func TestParseEntryPoint(t *testing.T) { |
| 26 |
tests := []struct { |
| 27 |
name string |
| 28 |
b []byte |
| 29 |
ep smbios.EntryPoint |
| 30 |
major, minor, revision int |
| 31 |
addr, size int |
| 32 |
ok bool |
| 33 |
}{ |
| 34 |
{ |
| 35 |
name: "short magic", |
| 36 |
b: []byte{0x00}, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "unknown magic", |
| 40 |
b: []byte{0xff, 0xff, 0xff, 0xff}, |
| 41 |
}, |
| 42 |
{ |
| 43 |
name: "32, short entry point", |
| 44 |
b: []byte{ |
| 45 |
'_', 'S', 'M', '_', |
| 46 |
}, |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "32, bad length", |
| 50 |
b: []byte{ |
| 51 |
'_', 'S', 'M', '_', |
| 52 |
0x00, |
| 53 |
0xff, |
| 54 |
0x00, |
| 55 |
0x00, |
| 56 |
0x00, 0x00, |
| 57 |
0x00, |
| 58 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 59 |
'_', 'F', 'O', 'O', '_', |
| 60 |
0x00, |
| 61 |
0x00, 0x00, |
| 62 |
0x00, 0x00, 0x00, 0x00, |
| 63 |
0x00, 0x00, |
| 64 |
0x00, |
| 65 |
}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
name: "32, bad intermediate anchor", |
| 69 |
b: []byte{ |
| 70 |
'_', 'S', 'M', '_', |
| 71 |
0x00, |
| 72 |
31, |
| 73 |
0x00, |
| 74 |
0x00, |
| 75 |
0x00, 0x00, |
| 76 |
0x00, |
| 77 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 78 |
'_', 'F', 'O', 'O', '_', |
| 79 |
0x00, |
| 80 |
0x00, 0x00, |
| 81 |
0x00, 0x00, 0x00, 0x00, |
| 82 |
0x00, 0x00, |
| 83 |
0x00, |
| 84 |
}, |
| 85 |
}, |
| 86 |
{ |
| 87 |
name: "32, bad checksum", |
| 88 |
b: []byte{ |
| 89 |
'_', 'S', 'M', '_', |
| 90 |
0x00, |
| 91 |
31, |
| 92 |
0x00, |
| 93 |
0x00, |
| 94 |
0x00, 0x00, |
| 95 |
0x00, |
| 96 |
0x00, 0x00, 0x00, 0x00, 0x00, |
| 97 |
'_', 'D', 'M', 'I', '_', |
| 98 |
0x00, |
| 99 |
0x00, 0x00, |
| 100 |
0x00, 0x00, 0x00, 0x00, |
| 101 |
0x00, 0x00, |
| 102 |
0x00, |
| 103 |
}, |
| 104 |
}, |
| 105 |
{ |
| 106 |
name: "32, OK", |
| 107 |
b: []byte{ |
| 108 |
'_', 'S', 'M', '_', |
| 109 |
0xa4, |
| 110 |
0x1f, |
| 111 |
0x2, |
| 112 |
0x8, |
| 113 |
0xd4, |
| 114 |
0x1, 0x0, |
| 115 |
0x0, 0x0, 0x0, 0x0, 0x0, |
| 116 |
'_', 'D', 'M', 'I', '_', |
| 117 |
0x95, |
| 118 |
0x5f, 0xf, |
| 119 |
0x0, 0x90, 0xf0, 0x7a, |
| 120 |
0x43, 0x0, |
| 121 |
0x28, |
| 122 |
}, |
| 123 |
ep: &smbios.EntryPoint32Bit{ |
| 124 |
Anchor: "_SM_", |
| 125 |
Checksum: 0xa4, |
| 126 |
Length: 0x1f, |
| 127 |
Major: 0x02, |
| 128 |
Minor: 0x08, |
| 129 |
MaxStructureSize: 0x01d4, |
| 130 |
IntermediateAnchor: "_DMI_", |
| 131 |
IntermediateChecksum: 0x95, |
| 132 |
StructureTableLength: 0x0f5f, |
| 133 |
StructureTableAddress: 0x7af09000, |
| 134 |
NumberStructures: 0x43, |
| 135 |
BCDRevision: 0x28, |
| 136 |
}, |
| 137 |
major: 2, minor: 8, revision: 0, |
| 138 |
addr: 0x7af09000, size: 0x0f5f, |
| 139 |
ok: true, |
| 140 |
}, |
| 141 |
{ |
| 142 |
name: "32, OK, trailing data", |
| 143 |
b: []byte{ |
| 144 |
'_', 'S', 'M', '_', |
| 145 |
0xa4, |
| 146 |
0x20, |
| 147 |
0x2, |
| 148 |
0x8, |
| 149 |
0xd4, |
| 150 |
0x1, 0x0, |
| 151 |
0x0, 0x0, 0x0, 0x0, 0x0, |
| 152 |
'_', 'D', 'M', 'I', '_', |
| 153 |
0x95, |
| 154 |
0x5f, 0xf, |
| 155 |
0x0, 0x90, 0xf0, 0x7a, |
| 156 |
0x43, 0x0, |
| 157 |
0x28, |
| 158 |
0xff, |
| 159 |
}, |
| 160 |
ep: &smbios.EntryPoint32Bit{ |
| 161 |
Anchor: "_SM_", |
| 162 |
Checksum: 0xa4, |
| 163 |
Length: 0x20, |
| 164 |
Major: 0x02, |
| 165 |
Minor: 0x08, |
| 166 |
MaxStructureSize: 0x01d4, |
| 167 |
IntermediateAnchor: "_DMI_", |
| 168 |
IntermediateChecksum: 0x95, |
| 169 |
StructureTableLength: 0x0f5f, |
| 170 |
StructureTableAddress: 0x7af09000, |
| 171 |
NumberStructures: 0x43, |
| 172 |
BCDRevision: 0x28, |
| 173 |
}, |
| 174 |
major: 2, minor: 8, revision: 0, |
| 175 |
addr: 0x7af09000, size: 0x0f5f, |
| 176 |
ok: true, |
| 177 |
}, |
| 178 |
{ |
| 179 |
name: "64, short entry point", |
| 180 |
b: []byte{ |
| 181 |
'_', 'S', 'M', '3', '_', |
| 182 |
}, |
| 183 |
}, |
| 184 |
{ |
| 185 |
name: "64, bad length", |
| 186 |
b: []byte{ |
| 187 |
'_', 'S', 'M', '3', '_', |
| 188 |
0x00, |
| 189 |
0xff, |
| 190 |
0x00, |
| 191 |
0x00, |
| 192 |
0x00, |
| 193 |
0x00, |
| 194 |
0x00, |
| 195 |
0x00, 0x00, 0x00, 0x00, |
| 196 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 197 |
}, |
| 198 |
}, |
| 199 |
{ |
| 200 |
name: "64, bad checksum", |
| 201 |
b: []byte{ |
| 202 |
'_', 'S', 'M', '3', '_', |
| 203 |
0x00, |
| 204 |
0x18, |
| 205 |
0x00, |
| 206 |
0x00, |
| 207 |
0x00, |
| 208 |
0x00, |
| 209 |
0x00, |
| 210 |
0x00, 0x00, 0x00, 0x00, |
| 211 |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 212 |
}, |
| 213 |
}, |
| 214 |
{ |
| 215 |
name: "64, OK", |
| 216 |
b: []byte{ |
| 217 |
'_', 'S', 'M', '3', '_', |
| 218 |
0x86, |
| 219 |
0x18, |
| 220 |
0x3, |
| 221 |
0x0, |
| 222 |
0x0, |
| 223 |
0x1, |
| 224 |
0x0, |
| 225 |
0x53, 0x9, 0x0, 0x0, |
| 226 |
0xb0, 0xb3, 0xe, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 227 |
}, |
| 228 |
ep: &smbios.EntryPoint64Bit{ |
| 229 |
Anchor: "_SM3_", |
| 230 |
Checksum: 0x86, |
| 231 |
Length: 0x18, |
| 232 |
Major: 0x03, |
| 233 |
EntryPointRevision: 0x01, |
| 234 |
StructureTableMaxSize: 0x0953, |
| 235 |
StructureTableAddress: 0x0eb3b0, |
| 236 |
}, |
| 237 |
major: 3, minor: 0, revision: 0, |
| 238 |
addr: 0x0eb3b0, size: 0x0953, |
| 239 |
ok: true, |
| 240 |
}, |
| 241 |
{ |
| 242 |
name: "64, OK, trailing data", |
| 243 |
b: []byte{ |
| 244 |
'_', 'S', 'M', '3', '_', |
| 245 |
0x86, |
| 246 |
0x19, |
| 247 |
0x3, |
| 248 |
0x0, |
| 249 |
0x0, |
| 250 |
0x1, |
| 251 |
0x0, |
| 252 |
0x53, 0x9, 0x0, 0x0, |
| 253 |
0xb0, 0xb3, 0xe, 0x0, 0x0, 0x0, 0x0, 0x0, |
| 254 |
0xff, |
| 255 |
}, |
| 256 |
ep: &smbios.EntryPoint64Bit{ |
| 257 |
Anchor: "_SM3_", |
| 258 |
Checksum: 0x86, |
| 259 |
Length: 0x19, |
| 260 |
Major: 0x03, |
| 261 |
EntryPointRevision: 0x01, |
| 262 |
StructureTableMaxSize: 0x0953, |
| 263 |
StructureTableAddress: 0x0eb3b0, |
| 264 |
}, |
| 265 |
major: 3, minor: 0, revision: 0, |
| 266 |
addr: 0x0eb3b0, size: 0x0953, |
| 267 |
ok: true, |
| 268 |
}, |
| 269 |
} |
| 270 |
|
| 271 |
for _, tt := range tests { |
| 272 |
t.Run(tt.name, func(t *testing.T) { |
| 273 |
ep, err := smbios.ParseEntryPoint(bytes.NewReader(tt.b)) |
| 274 |
|
| 275 |
if tt.ok && err != nil { |
| 276 |
t.Fatalf("unexpected error: %v", err) |
| 277 |
} |
| 278 |
if !tt.ok && err == nil { |
| 279 |
t.Fatalf("expected an error, but none occurred: %v", err) |
| 280 |
} |
| 281 |
|
| 282 |
if !tt.ok { |
| 283 |
|
| 284 |
t.Logf("OK error: %v", err) |
| 285 |
return |
| 286 |
} |
| 287 |
|
| 288 |
if diff := cmp.Diff(tt.ep, ep); diff != "" { |
| 289 |
t.Fatalf("unexpected entry point (-want +got):\n%s", diff) |
| 290 |
} |
| 291 |
|
| 292 |
major, minor, revision := ep.Version() |
| 293 |
wantVersion := []int{tt.major, tt.minor, tt.revision} |
| 294 |
gotVersion := []int{major, minor, revision} |
| 295 |
|
| 296 |
if diff := cmp.Diff(wantVersion, gotVersion); diff != "" { |
| 297 |
t.Fatalf("unexpected SMBIOS version (-want +got):\n%s", diff) |
| 298 |
} |
| 299 |
|
| 300 |
addr, size := ep.Table() |
| 301 |
wantTable := []int{tt.addr, tt.size} |
| 302 |
gotTable := []int{addr, size} |
| 303 |
|
| 304 |
if diff := cmp.Diff(wantTable, gotTable); diff != "" { |
| 305 |
t.Fatalf("unexpected SMBIOS table info (-want +got):\n%s", diff) |
| 306 |
} |
| 307 |
}) |
| 308 |
} |
| 309 |
} |