//go:build windows package host import ( "maps" "os" "strings" "testing" "golang.org/x/sys/windows" ) func TestIsActiveDevNodeStatus(t *testing.T) { cases := []struct { name string status uint32 want bool }{ { name: "started healthy adapter", status: windows.DN_NT_DRIVER | windows.DN_NT_ENUMERATOR | windows.DN_DISABLEABLE | windows.DN_STARTED | windows.DN_DRIVER_LOADED, want: true, }, { name: "started multi-function adapter ignores aliased bits", status: windows.DN_STARTED | windows.DN_MF_PARENT | windows.DN_MF_CHILD | windows.DN_NEEDS_LOCKING, want: true, }, { name: "phantom or zero status", status: 0, want: false, }, { name: "driver loaded but not started", status: windows.DN_DRIVER_LOADED | windows.DN_DISABLEABLE, want: false, }, { name: "disabled in device manager", status: windows.DN_DISABLEABLE | windows.DN_HAS_PROBLEM, want: false, }, { name: "started with problem flag", status: windows.DN_STARTED | windows.DN_HAS_PROBLEM, want: false, }, { name: "pending removal", status: windows.DN_STARTED | windows.DN_WILL_BE_REMOVED, want: false, }, } for _, tc := range cases { if got := isActiveDevNodeStatus(tc.status); got != tc.want { t.Errorf("%s: isActiveDevNodeStatus(0x%X) = %v, want %v", tc.name, tc.status, got, tc.want) } } } func TestFilterActiveDisplayAdaptersAndReadRegistryGPUs(t *testing.T) { const ( activeStatus = windows.DN_NT_DRIVER | windows.DN_NT_ENUMERATOR | windows.DN_DISABLEABLE | windows.DN_STARTED | windows.DN_DRIVER_LOADED disabledStatus = windows.DN_DISABLEABLE | windows.DN_HAS_PROBLEM removingStatus = activeStatus | windows.DN_WILL_BE_REMOVED ) cls := func(idx string) string { return `SYSTEM\CurrentControlSet\Control\Class\` + displayClass + `\` + idx } // Simulate registry keys including an unplugged (phantom) NVIDIA GPU at 0001 // whose DriverStore nv-vk64.json still exists on disk, a disabled NVIDIA GPU // at 0002, an ejecting NVIDIA GPU at 0003, a virtual adapter at 0005, and // two real active GPUs at 0007 and 0042 (index >= 32). reg := map[string]map[string]string{ cls("0001"): { "DriverDesc": "NVIDIA GeForce RTX 3080", "VulkanDriverName": `C:\DriverStore\nv_old\nv-vk64.json`, "MatchingDeviceId": `pci\ven_10de&dev_2206`, "DriverVersion": "32.0.15.6636", }, cls("0002"): { "DriverDesc": "NVIDIA GeForce RTX 4070", "VulkanDriverName": `C:\DriverStore\nv_dis\nv-vk64.json`, "MatchingDeviceId": `pci\ven_10de&dev_2786`, "DriverVersion": "32.0.15.6636", }, cls("0003"): { "DriverDesc": "NVIDIA GeForce RTX 4060", "VulkanDriverName": `C:\DriverStore\nv_rem\nv-vk64.json`, "MatchingDeviceId": `pci\ven_10de&dev_2882`, "DriverVersion": "32.0.15.6636", }, cls("0004"): { "DriverDesc": "NVIDIA GeForce GTX 1080", "VulkanDriverName": `C:\DriverStore\nv_err\nv-vk64.json`, "MatchingDeviceId": `pci\ven_10de&dev_1b80`, }, cls("0005"): { "DriverDesc": "Todesk Virtual Display Adapter", "MatchingDeviceId": `Root\tdIdd`, }, cls("0006"): { "DriverDesc": "Intel(R) Arc(TM) A770 Graphics", "VulkanDriverName": `C:\DriverStore\missing\igvk64.json`, "MatchingDeviceId": `pci\ven_8086&dev_56a0`, }, cls("0007"): { "DriverDesc": "Intel(R) UHD Graphics 770", "VulkanDriverName": `C:\DriverStore\intel\igvk64.json`, "MatchingDeviceId": `pci\ven_8086&dev_4680`, "DriverVersion": "31.0.101.5590", }, cls("0042"): { "DriverDesc": "AMD Radeon RX 7900 XTX", "VulkanDriverName": `C:\DriverStore\amd\amd-vulkan64.json`, "MatchingDeviceId": `PCI\VEN_1002&DEV_744C&REV_C8`, "DriverVersion": "32.0.11024.2", }, } existingFiles := map[string]bool{ `C:\DriverStore\nv_old\nv-vk64.json`: true, `C:\DriverStore\nv_dis\nv-vk64.json`: true, `C:\DriverStore\nv_rem\nv-vk64.json`: true, `C:\DriverStore\nv_err\nv-vk64.json`: true, `C:\DriverStore\intel\igvk64.json`: true, `C:\DriverStore\amd\amd-vulkan64.json`: true, } // Only present PnP devices appear here (0001 phantom NVIDIA GPU is absent). records := []displayAdapterRecord{ {instanceID: `PCI\VEN_10DE&DEV_2786\0`, driverKey: displayClass + `\0002`, status: disabledStatus, statusOK: true}, {instanceID: `PCI\VEN_10DE&DEV_2882\0`, driverKey: displayClass + `\0003`, status: removingStatus, statusOK: true}, {instanceID: `PCI\VEN_10DE&DEV_1B80\0`, driverKey: displayClass + `\0004`, status: activeStatus, statusOK: false}, {instanceID: `ROOT\DISPLAY\0000`, driverKey: displayClass + `\0005`, status: activeStatus, statusOK: true}, {instanceID: `PCI\VEN_8086&DEV_56A0\0`, driverKey: displayClass + `\0006`, status: activeStatus, statusOK: true}, {instanceID: `DISPLAY\CUSTOM\0`, driverKey: displayClass + `\0042`, status: activeStatus, statusOK: true}, {instanceID: `PCI\VEN_8086&DEV_4680\0`, driverKey: displayClass + `\0007`, status: activeStatus, statusOK: true}, {instanceID: `PCI\VEN_8086&DEV_4680\0`, driverKey: strings.ToUpper(displayClass) + `\0007`, status: activeStatus, statusOK: true}, {instanceID: `PCI\VEN_10DE&DEV_9999\0`, driverKey: `..\bad`, status: activeStatus, statusOK: true}, } adapters := filterActiveDisplayAdapters(records) gpus := readRegistryGPUs( adapters, func(subKey, valueName string) (string, bool) { v, ok := reg[subKey][valueName] return v, ok }, func(path string) bool { return existingFiles[path] }, ) if len(gpus) != 2 { t.Fatalf("expected 2 active GPUs, got %d: %+v", len(gpus), gpus) } if gpus[0].Name != "Intel(R) UHD Graphics 770" || gpus[0].VendorID != "8086" || gpus[0].DeviceID != "4680" || gpus[0].Kind != GPUIntegrated { t.Fatalf("unexpected gpus[0]: %+v", gpus[0]) } if gpus[1].Name != "AMD Radeon RX 7900 XTX" || gpus[1].VendorID != "1002" || gpus[1].DeviceID != "744c" || gpus[1].Kind != GPUDiscrete { t.Fatalf("unexpected gpus[1]: %+v", gpus[1]) } } func TestLiveGPUsExcludeInactiveDisplayAdapters(t *testing.T) { active := activeDisplayAdapters() activeKeys := make(map[string]bool, len(active)) activePCI := make(map[string]bool, len(active)) for _, a := range active { activeKeys[strings.ToLower(a.driverKey)] = true if a.vendorID != "" && a.deviceID != "" { activePCI[a.vendorID+":"+a.deviceID] = true } } guid, err := windows.GUIDFromString(displayClass) if err != nil { t.Fatalf("GUIDFromString: %v", err) } allDevs, err := windows.SetupDiGetClassDevsEx(&guid, "", 0, 0, 0, "") if err != nil { t.Fatalf("SetupDiGetClassDevsEx(all): %v", err) } defer allDevs.Close() inactiveOnlyPCI := make(map[string]string) for i := 0; ; i++ { dev, err := allDevs.EnumDeviceInfo(i) if err != nil { break } val, _ := allDevs.DeviceRegistryProperty(dev, windows.SPDRP_DRIVER) driverKey, _ := val.(string) driverKey = strings.Trim(strings.TrimSpace(driverKey), `\`) if driverKey != "" && activeKeys[strings.ToLower(driverKey)] { continue } id, _ := allDevs.DeviceInstanceID(dev) ven, devID := ParsePCIIDs(id) if (ven == "" || devID == "") && driverKey != "" && !strings.Contains(driverKey, "..") { matchID, _ := regReadString(hkeyLocalMachine, `SYSTEM\CurrentControlSet\Control\Class\`+driverKey, "MatchingDeviceId") v2, d2 := ParsePCIIDs(matchID) if ven == "" { ven = v2 } if devID == "" { devID = d2 } } if ven == "" || devID == "" { continue } pci := ven + ":" + devID if !activePCI[pci] { inactiveOnlyPCI[pci] = id } } for _, g := range probeActiveRegistryGPUs() { if g.VulkanICD == "" || !isRegularFile(g.VulkanICD) { t.Errorf("active registry GPU %q has invalid VulkanICD %q", g.Name, g.VulkanICD) } } probed := ProbeVulkanGPUs() for _, g := range probed { if g.VendorID == "" || g.DeviceID == "" { continue } pci := strings.ToLower(g.VendorID) + ":" + strings.ToLower(g.DeviceID) if id, bad := inactiveOnlyPCI[pci]; bad { t.Errorf("ProbeVulkanGPUs included inactive/unplugged GPU %q (%s, instance %s): %+v", g.Name, pci, id, g) } } if len(inactiveOnlyPCI) > 0 { t.Logf("verified %d inactive/phantom display adapter PCI ID(s) excluded from ProbeVulkanGPUs (%d active GPUs returned)", len(inactiveOnlyPCI), len(probed)) } } func TestMissingDriverPackages(t *testing.T) { candidates := []adapterManifest{ {"10de:28e0", "nvidia.json"}, {"10de:28e0", "nvidia.json"}, {"8086:1234", "intel.json"}, {"1002:5678", "amd-a.json"}, {"1002:5678", "amd-b.json"}, {"1002:5678", "amd-a.json"}, } visible := []VulkanDeviceProbe{{VendorID: 0x8086, DeviceID: 0x1234}} got := missingDriverPackages(candidates, visible) if !maps.Equal(got, map[string]string{"10de:28e0": "nvidia.json"}) { t.Fatalf("expected only the missing unambiguous device, got %v", got) } } func TestWindowsProcessAffinityAndTopology(t *testing.T) { pid := uint32(os.Getpid()) procMask, sysMask, err := GetProcessAffinityMask(pid) if err != nil { t.Fatalf("GetProcessAffinityMask(self=%d): %v", pid, err) } if procMask == 0 || sysMask == 0 { t.Fatalf("unexpected zero masks: proc=0x%X sys=0x%X", procMask, sysMask) } if procMask&sysMask != procMask { t.Fatalf("procMask 0x%X is not subset of sysMask 0x%X", procMask, sysMask) } rep := Inspect(t.TempDir()) if len(rep.CPUTopology) == 0 { t.Fatalf("expected non-empty CPUTopology on Windows") } if len(rep.GPUs) > 0 { for _, g := range rep.GPUs { if g.Compat == "" { t.Errorf("GPU %q missing Compat grade: %+v", g.Name, g) } t.Logf("probed GPU: name=%q kind=%s pci=%s:%s drv=%s vk=%s compat=%s (%s %s)", g.Name, g.Kind, g.VendorID, g.DeviceID, g.DriverVersion, g.VulkanVersion, g.Compat, g.CompatCode, g.CompatDetail) } } for i, c := range rep.CPUTopology { if i > 0 && c.Index <= rep.CPUTopology[i-1].Index { t.Fatalf("CPUTopology not strictly sorted at %d: %+v", i, rep.CPUTopology) } if c.Tier == "" { t.Fatalf("empty tier for CPU %d", c.Index) } } } func TestProbeDisplaysNoCallbackLeak(t *testing.T) { // Go's Windows syscall callback table holds at most 2000 permanent entries. // Exceeding 2000 iterations verifies probeDisplays reuses a single callback slot. for i := 0; i < 2100; i++ { _ = probeDisplays() } }