package pciids import ( "os" "path/filepath" "sync" "testing" ) func TestLookupByDescription(t *testing.T) { dir := t.TempDir() idsPath := filepath.Join(dir, "pci.ids") content := "" + "fff0 Example Devices\n" + "\t0001 Widget 100 [W100]\n" + "\t0002 Twin Port Adapter\n" + "\t0003 Twin Port Adapter\n" + "fff1 Example Devices, Inc. [EXD]\n" + "\t0010 Gadget\n" if err := os.WriteFile(idsPath, []byte(content), 0o644); err != nil { t.Fatalf("write pci.ids: %v", err) } t.Setenv("LOGPILE_PCI_IDS_PATH", idsPath) reset := func() { loadOnce = sync.Once{} reverseOnce = sync.Once{} vendors = nil devices = nil } reset() t.Cleanup(reset) cases := []struct { name string description string wantOK bool wantVendor int wantDevice int wantName string wantModel string }{ { name: "unique device name", description: "Example Devices Widget 100 [W100]", wantOK: true, wantVendor: 0xfff0, wantDevice: 0x0001, wantName: "Example Devices", wantModel: "Widget 100 [W100]", }, { name: "longest vendor name wins", description: "Example Devices, Inc. [EXD] Gadget", wantOK: true, wantVendor: 0xfff1, wantDevice: 0x0010, wantName: "Example Devices, Inc. [EXD]", wantModel: "Gadget", }, { name: "lspci placeholder carries the device id", description: "Example Devices Device 0abc", wantOK: true, wantVendor: 0xfff0, wantDevice: 0x0abc, wantName: "Example Devices", wantModel: "Device 0abc", }, { name: "ambiguous device name keeps the id unknown", description: "Example Devices Twin Port Adapter", wantOK: true, wantVendor: 0xfff0, wantDevice: 0, wantName: "Example Devices", wantModel: "Twin Port Adapter", }, { name: "vendor name must end at a word boundary", description: "Example DevicesX Widget", wantOK: false, wantModel: "Example DevicesX Widget", }, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { vendorID, deviceID, vendorName, deviceName, ok := LookupByDescription(tc.description) if ok != tc.wantOK || vendorID != tc.wantVendor || deviceID != tc.wantDevice || vendorName != tc.wantName || deviceName != tc.wantModel { t.Fatalf("LookupByDescription(%q) = %04x:%04x %q %q ok=%v", tc.description, vendorID, deviceID, vendorName, deviceName, ok) } }) } }