go: net: TestInterfaceHardwareAddrWithWmic fails

What version of Go are you using (go version)?

go version devel +98443ecd0a Fri Dec 15 21:57:27 2017 +0000 windows/amd64

Does this issue reproduce with the latest release?

Yes, it does.

What operating system and processor architecture are you using (go env)?

set GOARCH=amd64 set GOBIN= set GOCACHE=C:\Users\Alex\AppData\Local\go-build set GOEXE=.exe set GOHOSTARCH=amd64 set GOHOSTOS=windows set GOOS=windows set GOPATH=c:\users\alex\dev set GORACE= set GOROOT=c:\users\alex\dev\go set GOTMPDIR= set GOTOOLDIR=c:\users\alex\dev\go\pkg\tool\windows_amd64 set GCCGO=gccgo set CC=gcc set CXX=g++ set CGO_ENABLED=1 set CGO_CFLAGS=-g -O2 set CGO_CPPFLAGS= set CGO_CXXFLAGS=-g -O2 set CGO_FFLAGS=-g -O2 set CGO_LDFLAGS=-g -O2 set PKG_CONFIG=pkg-config set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Alex\AppData\Local\Temp\go-build981970258=/tmp/go-build -gno-record-gcc-switches

What did you do?

I run go test -run=TestInterfaceHardwareAddrWithWmic net command.

What did you expect to see?

PASS

What did you see instead?

--- FAIL: TestInterfaceHardwareAddrWithWmic (0.31s)
        net_windows_test.go:560: go interface count (3, map[0a:00:27:00:00:06:VirtualBox Host-Only Network #2 44:8a:5b:e8:97:a9:Ethernet 00:00:00:00:00:00:00:e0:Teredo Tunneling Pseudo-Interface]) differs from wmic count (2, map[44:8a:5b:e8:97:a9:[Ethernet] 0a:00:27:00:00:06:[VirtualBox Host-Only Network #2]])
        net_windows_test.go:572: go found interface (name: Teredo Tunneling Pseudo-Interface, mac: 00:00:00:00:00:00:00:e0) not found by wmic (map[44:8a:5b:e8:97:a9:[Ethernet] 0a:00:27:00:00:06:[VirtualBox Host-Only Network #2]])
FAIL
FAIL    net     0.638s

Alex

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 16 (13 by maintainers)

Commits related to this issue

Most upvoted comments

@alexbrainman Good news. wmic command output UTF-16 when the output is a file. It return DBCS when output is a pipe. So writing file should work. Could you please add runWmic?

diff --git a/src/net/net_windows_test.go b/src/net/net_windows_test.go
index ab09cdb28a..ed02ae7e62 100644
--- a/src/net/net_windows_test.go
+++ b/src/net/net_windows_test.go
@@ -18,6 +18,7 @@ import (
 	"syscall"
 	"testing"
 	"time"
+	"unsafe"
 )
 
 func toErrno(err error) (syscall.Errno, bool) {
@@ -512,6 +513,29 @@ func contains(needle string, haystack []string) bool {
 	return false
 }
 
+func runWmic(args ...string) ([]byte, error) {
+	f, err := ioutil.TempFile("", "wmic")
+	if err != nil {
+		return nil, err
+	}
+	defer os.Remove(f.Name())
+	cmd := exec.Command(args[0], args[1:]...)
+	cmd.Stdout = f
+	err = cmd.Run()
+	f.Close()
+	if err != nil {
+		return nil, err
+	}
+	b, err := ioutil.ReadFile(f.Name())
+	if err != nil {
+		return nil, err
+	}
+	if len(b) >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF {
+		b = b[3:]
+	}
+	return []byte(syscall.UTF16ToString(*(*[]uint16)(unsafe.Pointer(&b)))), nil
+}
+
 func TestInterfaceHardwareAddrWithWmic(t *testing.T) {
 	ift, err := Interfaces()
 	if err != nil {
@@ -523,6 +547,10 @@ func TestInterfaceHardwareAddrWithWmic(t *testing.T) {
 			// no MAC address for loopback interfaces
 			continue
 		}
+		if ifi.Flags&FlagUp == 0 {
+			// interface is not up
+			continue
+		}
 		goMacToName[ifi.HardwareAddr.String()] = ifi.Name
 	}
 
@@ -533,7 +561,7 @@ func TestInterfaceHardwareAddrWithWmic(t *testing.T) {
 	//SERVER-2008R2-V,42:01:0A:F0:00:18,Local Area Connection
 	//SERVER-2008R2-V,42:01:0A:F0:00:18,Duplicate Adapter
 	//SERVER-2008R2-V,20:41:53:59:4E:FF,
-	out, err := exec.Command("wmic", "nic", "get", "MACAddress,NetConnectionID", "/format:csv").CombinedOutput()
+	out, err := runWmic("wmic", "nic", "get", "MACAddress,NetConnectionID", "/format:csv")
 	if err != nil {
 		t.Fatal(err)
 	}