159 lines
4.3 KiB
Go
159 lines
4.3 KiB
Go
package opencore
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestFormatAppleSerialVectors(t *testing.T) {
|
|
imac := lookupSMBIOSModel("iMac20,1")
|
|
got, err := formatAppleSerial(imac, 2020, 30, 1000)
|
|
if err != nil || got != "C02D40VEPN5T" {
|
|
t.Fatalf("iMac20,1 vector: got (%q, %v), want C02D40VEPN5T", got, err)
|
|
}
|
|
pro := lookupSMBIOSModel("imacpro1,1")
|
|
got, err = formatAppleSerial(pro, 2017, 5, 3399)
|
|
if err != nil || got != "C02T5ZZZHX87" {
|
|
t.Fatalf("iMacPro1,1 vector: got (%q, %v), want C02T5ZZZHX87", got, err)
|
|
}
|
|
}
|
|
|
|
func TestGenerateAndValidateSMBIOS(t *testing.T) {
|
|
for _, model := range []string{"iMac20,1", "iMacPro1,1", ""} {
|
|
id, err := GenerateSMBIOS(model)
|
|
if err != nil {
|
|
t.Fatalf("GenerateSMBIOS(%q): %v", model, err)
|
|
}
|
|
if len(id.Serial) != 12 {
|
|
t.Fatalf("serial len = %d (%q)", len(id.Serial), id.Serial)
|
|
}
|
|
if len(id.MLB) != 17 || !MLBChecksumOK(id.MLB) {
|
|
t.Fatalf("invalid MLB %q", id.MLB)
|
|
}
|
|
if _, err := NormalizeAndValidateSMBIOS(id); err != nil {
|
|
t.Fatalf("NormalizeAndValidateSMBIOS failed on generated identity: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestReadAndPatchSMBIOSPlist(t *testing.T) {
|
|
sample := []byte(`<?xml version="1.0" encoding="UTF-8"?>
|
|
<plist version="1.0">
|
|
<dict>
|
|
<key>PlatformInfo</key>
|
|
<dict>
|
|
<key>Automatic</key>
|
|
<true/>
|
|
<key>Generic</key>
|
|
<dict>
|
|
<key>MLB</key>
|
|
<string>C02029200GUPHC11H</string>
|
|
<key>ROM</key>
|
|
<data>
|
|
UlQAEljq
|
|
</data>
|
|
<key>SystemProductName</key>
|
|
<string>iMac20,1</string>
|
|
<key>SystemSerialNumber</key>
|
|
<string>C02D40VEPN5T</string>
|
|
<key>SystemUUID</key>
|
|
<string>11111111-2222-4333-8444-555555555555</string>
|
|
</dict>
|
|
</dict>
|
|
</dict>
|
|
</plist>
|
|
`)
|
|
cur, ok := ReadSMBIOS(sample)
|
|
if !ok {
|
|
t.Fatal("ReadSMBIOS returned ok=false")
|
|
}
|
|
if cur.Model != "iMac20,1" || cur.Serial != "C02D40VEPN5T" || cur.ROM != "52:54:00:12:58:EA" {
|
|
t.Fatalf("ReadSMBIOS = %+v", cur)
|
|
}
|
|
|
|
next, err := GenerateSMBIOS("iMac20,1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
patched, err := PatchSMBIOS(sample, next)
|
|
if err != nil {
|
|
t.Fatalf("PatchSMBIOS: %v", err)
|
|
}
|
|
after, ok := ReadSMBIOS(patched)
|
|
if !ok {
|
|
t.Fatal("ReadSMBIOS after patch failed")
|
|
}
|
|
if after.Serial != next.Serial || after.MLB != next.MLB || after.UUID != next.UUID {
|
|
t.Fatalf("patched identity mismatch: got %+v, want %+v", after, next)
|
|
}
|
|
// ROM must be preserved untouched.
|
|
if after.ROM != "52:54:00:12:58:EA" || !strings.Contains(string(patched), "UlQAEljq") {
|
|
t.Fatalf("ROM was not preserved: %+v", after)
|
|
}
|
|
}
|
|
|
|
func TestPatchImageSMBIOSRoundTrip(t *testing.T) {
|
|
sample := []byte(`<?xml version="1.0" encoding="UTF-8"?>
|
|
<plist version="1.0">
|
|
<dict>
|
|
<key>PlatformInfo</key>
|
|
<dict>
|
|
<key>Automatic</key>
|
|
<true/>
|
|
<key>Generic</key>
|
|
<dict>
|
|
<key>MLB</key>
|
|
<string>C02029200GUPHC11H</string>
|
|
<key>ROM</key>
|
|
<data>UlQAEljq</data>
|
|
<key>SystemProductName</key>
|
|
<string>iMac20,1</string>
|
|
<key>SystemSerialNumber</key>
|
|
<string>C02D40VEPN5T</string>
|
|
<key>SystemUUID</key>
|
|
<string>11111111-2222-4333-8444-555555555555</string>
|
|
</dict>
|
|
</dict>
|
|
</dict>
|
|
</plist>
|
|
`)
|
|
dir := t.TempDir()
|
|
rawBytes := buildSyntheticRawOpenCore(t, sample)
|
|
qcowPath := filepath.Join(dir, "opencore.qcow2")
|
|
if err := os.WriteFile(qcowPath, wrapRawAsQcow2(rawBytes), 0o644); err != nil {
|
|
t.Fatalf("write qcow2: %v", err)
|
|
}
|
|
|
|
orig, err := ReadImageSMBIOS(qcowPath)
|
|
if err != nil {
|
|
t.Fatalf("ReadImageSMBIOS(orig): %v", err)
|
|
}
|
|
if orig.Serial != "C02D40VEPN5T" || orig.ROM != "52:54:00:12:58:EA" {
|
|
t.Fatalf("unexpected orig SMBIOS: %+v", orig)
|
|
}
|
|
|
|
next, err := GenerateSMBIOS("iMac20,1")
|
|
if err != nil {
|
|
t.Fatalf("GenerateSMBIOS: %v", err)
|
|
}
|
|
changed, err := PatchImageSMBIOS(context.Background(), qcowPath, next, "", nil)
|
|
if err != nil || !changed {
|
|
t.Fatalf("PatchImageSMBIOS = (%v, %v), want (true, nil)", changed, err)
|
|
}
|
|
after, err := ReadImageSMBIOS(qcowPath)
|
|
if err != nil {
|
|
t.Fatalf("ReadImageSMBIOS(after): %v", err)
|
|
}
|
|
if after.Serial != next.Serial || after.MLB != next.MLB || after.UUID != next.UUID || after.ROM != orig.ROM {
|
|
t.Fatalf("after patch = %+v, want serial/mlb/uuid=%+v and rom=%s", after, next, orig.ROM)
|
|
}
|
|
// Repeating the same SMBIOS should be a no-op.
|
|
changed2, err := PatchImageSMBIOS(context.Background(), qcowPath, next, "", nil)
|
|
if err != nil || changed2 {
|
|
t.Fatalf("repeat PatchImageSMBIOS = (%v, %v), want (false, nil)", changed2, err)
|
|
}
|
|
}
|