Add support for setupapi.SetupDiEnumDeviceInfo()

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman
2019-02-01 12:17:09 +01:00
committed by Jason A. Donenfeld
parent 25e18d01e6
commit 955d8dfe04
4 changed files with 83 additions and 18 deletions

View File

@@ -12,7 +12,7 @@ import (
"golang.org/x/sys/windows"
)
var guidDeviceClassNet = windows.GUID{0x4d36e972, 0xe325, 0x11ce, [8]byte{0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}}
var deviceClassNetGUID = windows.GUID{0x4d36e972, 0xe325, 0x11ce, [8]byte{0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18}}
var computerName string
func init() {
@@ -20,16 +20,16 @@ func init() {
}
func TestSetupDiGetClassDevsEx(t *testing.T) {
dev_info_list, err := SetupDiGetClassDevsEx(&guidDeviceClassNet, "PCI", 0, DIGCF_PRESENT, DevInfo(0), computerName)
devInfoList, err := SetupDiGetClassDevsEx(&deviceClassNetGUID, "PCI", 0, DIGCF_PRESENT, DevInfo(0), computerName)
if err == nil {
dev_info_list.Close()
devInfoList.Close()
} else {
t.Errorf("Error calling SetupDiGetClassDevsEx: %s", err.Error())
}
dev_info_list, err = SetupDiGetClassDevsEx(nil, "", 0, DIGCF_PRESENT, DevInfo(0), "")
devInfoList, err = SetupDiGetClassDevsEx(nil, "", 0, DIGCF_PRESENT, DevInfo(0), "")
if err == nil {
dev_info_list.Close()
devInfoList.Close()
t.Errorf("SetupDiGetClassDevsEx(nil, ...) should fail")
} else {
if errWin, ok := err.(syscall.Errno); !ok || errWin != 87 /*ERROR_INVALID_PARAMETER*/ {
@@ -39,18 +39,18 @@ func TestSetupDiGetClassDevsEx(t *testing.T) {
}
func TestSetupDiGetDeviceInfoListDetailLocal(t *testing.T) {
dev_info_list, err := SetupDiGetClassDevsEx(&guidDeviceClassNet, "", 0, DIGCF_PRESENT, DevInfo(0), "")
devInfoList, err := SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, DIGCF_PRESENT, DevInfo(0), "")
if err != nil {
t.Errorf("Error calling SetupDiGetClassDevsEx: %s", err.Error())
}
defer SetupDiDestroyDeviceInfoList(dev_info_list)
defer SetupDiDestroyDeviceInfoList(devInfoList)
data, err := SetupDiGetDeviceInfoListDetail(dev_info_list)
data, err := SetupDiGetDeviceInfoListDetail(devInfoList)
if err != nil {
t.Errorf("Error calling SetupDiGetDeviceInfoListDetail: %s", err.Error())
}
if data.ClassGUID != guidDeviceClassNet {
if data.ClassGUID != deviceClassNetGUID {
t.Error("SetupDiGetDeviceInfoListDetail returned different class GUID")
}
@@ -64,18 +64,18 @@ func TestSetupDiGetDeviceInfoListDetailLocal(t *testing.T) {
}
func TestSetupDiGetDeviceInfoListDetailRemote(t *testing.T) {
dev_info_list, err := SetupDiGetClassDevsEx(&guidDeviceClassNet, "", 0, DIGCF_PRESENT, DevInfo(0), computerName)
devInfoList, err := SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, DIGCF_PRESENT, DevInfo(0), computerName)
if err != nil {
t.Errorf("Error calling SetupDiGetClassDevsEx: %s", err.Error())
}
defer SetupDiDestroyDeviceInfoList(dev_info_list)
defer SetupDiDestroyDeviceInfoList(devInfoList)
data, err := SetupDiGetDeviceInfoListDetail(dev_info_list)
data, err := SetupDiGetDeviceInfoListDetail(devInfoList)
if err != nil {
t.Errorf("Error calling SetupDiGetDeviceInfoListDetail: %s", err.Error())
}
if data.ClassGUID != guidDeviceClassNet {
if data.ClassGUID != deviceClassNetGUID {
t.Error("SetupDiGetDeviceInfoListDetail returned different class GUID")
}
@@ -87,3 +87,25 @@ func TestSetupDiGetDeviceInfoListDetailRemote(t *testing.T) {
t.Error("SetupDiGetDeviceInfoListDetail returned different remote machine name")
}
}
func TestSetupDiEnumDeviceInfo(t *testing.T) {
devInfoList, err := SetupDiGetClassDevsEx(&deviceClassNetGUID, "", 0, DIGCF_PRESENT, DevInfo(0), "")
if err != nil {
t.Errorf("Error calling SetupDiGetClassDevsEx: %s", err.Error())
}
defer SetupDiDestroyDeviceInfoList(devInfoList)
for i := 0; true; i++ {
data, err := SetupDiEnumDeviceInfo(devInfoList, i)
if err != nil {
if errWin, ok := err.(syscall.Errno); ok && errWin == 259 /*ERROR_NO_MORE_ITEMS*/ {
break
}
continue
}
if data.ClassGUID != deviceClassNetGUID {
t.Error("SetupDiEnumDeviceInfo returned different class GUID")
}
}
}