Files
Reims-Go/internal/host/smbios_memory_test.go
T

94 lines
3.0 KiB
Go

package host
import (
"encoding/binary"
"runtime"
"testing"
)
func buildSMBIOSType17Entry(size uint16, memType byte, speed, cfgSpeed uint16) []byte {
b := make([]byte, 0x22)
b[0] = 17
b[1] = byte(len(b))
binary.LittleEndian.PutUint16(b[2:4], 0x0011)
binary.LittleEndian.PutUint16(b[0x0C:0x0E], size)
b[0x12] = memType
binary.LittleEndian.PutUint16(b[0x15:0x17], speed)
binary.LittleEndian.PutUint16(b[0x20:0x22], cfgSpeed)
// Double-NUL string terminator.
return append(b, 0, 0)
}
func wrapWindowsRawSMBIOS(table []byte) []byte {
hdr := make([]byte, 8)
hdr[1] = 3
hdr[2] = 6
binary.LittleEndian.PutUint32(hdr[4:8], uint32(len(table)))
return append(hdr, table...)
}
func TestParseSMBIOSType17DDR5ConfiguredSpeed(t *testing.T) {
var table []byte
// Empty DIMM slot should be ignored even if firmware left bogus fields.
table = append(table, buildSMBIOSType17Entry(0, 26, 2666, 2666)...)
// Populated DDR5-6400 EXPO DIMM (rated 4800, configured 6400).
table = append(table, buildSMBIOSType17Entry(16384, 34, 4800, 6400)...)
table = append(table, buildSMBIOSType17Entry(16384, 34, 4800, 6400)...)
// End-of-table marker.
table = append(table, 127, 4, 0, 0, 0, 0)
spec := ParseWindowsRawSMBIOS(wrapWindowsRawSMBIOS(table))
if spec == nil {
t.Fatal("expected non-nil HostMemorySpec")
}
if spec.SpeedMHz != 6400 || spec.MemoryType != 34 || spec.TypeName != "DDR5" {
t.Fatalf("got %+v, want 6400 MHz DDR5 (type 34)", spec)
}
if got := FormatMemorySpec(spec); got != "6400 MHz DDR5" {
t.Fatalf("FormatMemorySpec = %q, want %q", got, "6400 MHz DDR5")
}
}
func TestParseSMBIOSType17FallsBackToRatedSpeed(t *testing.T) {
// Older SMBIOS 2.3 entry without ConfiguredMemorySpeed (length 0x1B).
entry := make([]byte, 0x1B)
entry[0] = 17
entry[1] = byte(len(entry))
binary.LittleEndian.PutUint16(entry[0x0C:0x0E], 8192)
entry[0x12] = 26 // DDR4
binary.LittleEndian.PutUint16(entry[0x15:0x17], 3200)
entry = append(entry, 'D', 'I', 'M', 'M', '0', 0, 0)
spec := ParseSMBIOSType17Table(entry)
if spec == nil {
t.Fatal("expected non-nil HostMemorySpec")
}
if spec.SpeedMHz != 3200 || spec.MemoryType != 26 || spec.TypeName != "DDR4" {
t.Fatalf("got %+v, want 3200 MHz DDR4", spec)
}
}
func TestParseSMBIOSType17EmptyAndMalformed(t *testing.T) {
if got := ParseWindowsRawSMBIOS(nil); got != nil {
t.Fatalf("nil input: got %+v", got)
}
if got := ParseWindowsRawSMBIOS([]byte{1, 2, 3}); got != nil {
t.Fatalf("short input: got %+v", got)
}
emptySlot := buildSMBIOSType17Entry(0, 26, 3200, 3200)
if got := ParseSMBIOSType17Table(emptySlot); got != nil {
t.Fatalf("empty slot only: got %+v", got)
}
}
func TestProbeHostMemorySpecLiveWindows(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Windows live SMBIOS probe test")
}
spec := probeHostMemorySpec()
t.Logf("live Windows host memory spec: %+v (%q)", spec, FormatMemorySpec(spec))
if spec != nil && (spec.SpeedMHz < 200 || spec.SpeedMHz > 65535) {
t.Fatalf("out-of-range live SpeedMHz: %+v", spec)
}
}