package probes import ( "strings" "testing" "vetting/internal/spec" ) func TestParseCPUInfoInto(t *testing.T) { sample := `processor : 0 vendor_id : GenuineIntel model name : Intel(R) N95 stepping : 3 cpu cores : 4 flags : fpu vme de pse tsc msr aes sse4_2 vmx processor : 1 vendor_id : GenuineIntel model name : Intel(R) N95 ` var c spec.CPUSpec parseCPUInfoInto(strings.NewReader(sample), &c) if c.Model != "Intel(R) N95" { t.Fatalf("model: got %q", c.Model) } if c.Vendor != "GenuineIntel" { t.Fatalf("vendor: got %q", c.Vendor) } if c.Stepping != "3" { t.Fatalf("stepping: got %q", c.Stepping) } if c.PhysicalCores != 4 { t.Fatalf("physical cores: got %d", c.PhysicalCores) } // Only vmx, aes, sse4_2 should survive the allow-list filter. if len(c.Flags) != 3 { t.Fatalf("flags count: got %v", c.Flags) } } func TestParseOSRelease(t *testing.T) { in := `PRETTY_NAME="Debian GNU/Linux 12 (bookworm)" VERSION_ID="12" NAME=Debian ` dist, ver := parseOSRelease(in) if dist != "Debian GNU/Linux 12 (bookworm)" { t.Fatalf("distribution: got %q", dist) } if ver != "12" { t.Fatalf("version: got %q", ver) } } func TestParseDmidecodeSize(t *testing.T) { cases := []struct { in string want int }{ {"16 GB", 16}, {"32 GiB", 32}, {"8192 MB", 8}, {"No Module Installed", 0}, {"garbage", 0}, } for _, c := range cases { if got := parseDmidecodeSize(c.in); got != c.want { t.Errorf("parseDmidecodeSize(%q) = %d, want %d", c.in, got, c.want) } } } func TestParseMTS(t *testing.T) { if n := parseMTS("3200 MT/s"); n != 3200 { t.Errorf("parseMTS MT/s: got %d", n) } if n := parseMTS("2400 MHz"); n != 2400 { t.Errorf("parseMTS MHz: got %d", n) } if n := parseMTS("Unknown"); n != 0 { t.Errorf("parseMTS unknown: got %d", n) } } func TestCleanDmidecodeValue(t *testing.T) { cases := map[string]string{ "Micron": "Micron", "Not Specified": "", " Unknown ": "", "To Be Filled By O.E.M.": "", "": "", } for in, want := range cases { if got := cleanDmidecodeValue(in); got != want { t.Errorf("cleanDmidecodeValue(%q) = %q, want %q", in, got, want) } } } func TestParseDIMMs(t *testing.T) { // Two populated + two empty slots. Verifies the multi-section parser // produces the expected count and the empty-slot detection fires on // "No Module Installed". sample := `Handle 0x0032, DMI type 17, 84 bytes Memory Device Size: 16 GB Locator: DIMM_A1 Manufacturer: Micron Part Number: 8ATF2G64AZ-3G2E1 Configured Memory Speed: 3200 MT/s Handle 0x0033, DMI type 17, 84 bytes Memory Device Size: No Module Installed Locator: DIMM_A2 Manufacturer: Not Specified Handle 0x0034, DMI type 17, 84 bytes Memory Device Size: 16 GB Locator: DIMM_B1 Manufacturer: Micron Part Number: 8ATF2G64AZ-3G2E1 Speed: 3200 MT/s Handle 0x0035, DMI type 17, 84 bytes Memory Device Size: No Module Installed Locator: DIMM_B2 ` dimms := parseDIMMs(strings.NewReader(sample), Logger{}) if len(dimms) != 4 { t.Fatalf("expected 4 DIMM entries, got %d: %+v", len(dimms), dimms) } populated := 0 for _, d := range dimms { if d.Populated { populated++ } } if populated != 2 { t.Fatalf("expected 2 populated, got %d", populated) } if dimms[0].Slot != "DIMM_A1" || dimms[0].SizeGB != 16 || dimms[0].SpeedMTS != 3200 { t.Fatalf("DIMM_A1 mismatch: %+v", dimms[0]) } if dimms[0].PartNumber != "8ATF2G64AZ-3G2E1" { t.Fatalf("DIMM_A1 part number mismatch: %q", dimms[0].PartNumber) } if dimms[1].Populated || dimms[1].Slot != "DIMM_A2" { t.Fatalf("DIMM_A2 should be empty: %+v", dimms[1]) } } func TestParseLspciVerbose(t *testing.T) { sample := `01:00.0 VGA compatible controller: NVIDIA Corporation Subsystem: ASUSTeK Kernel driver in use: nvidia Memory at f6000000 (32-bit, non-prefetchable) [size=16M] Memory at c0000000 (64-bit, prefetchable) [size=8G] Memory at d0000000 (64-bit, prefetchable) [size=32M] ` vram, driver := parseLspciVerbose(sample) if vram != 8 { t.Errorf("VRAM: got %d, want 8", vram) } if driver != "nvidia" { t.Errorf("driver: got %q", driver) } }