Skip to content

Commit

Permalink
net: make TestInterfaceList work on non-English Windows
Browse files Browse the repository at this point in the history
Fixes #13198

The output of netsh is encoded with ANSI encoding. So doesn't match with UTF-8 strings.
Write output as UTF-8 using powershell.

Change-Id: I6c7e93c590ed407f24ae847601d71df9523e028c
Reviewed-on: https://go-review.googlesource.com/16756
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
  • Loading branch information
mattn authored and alexbrainman committed Nov 13, 2015
1 parent 0adf6dc commit 940d41e
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/net/net_windows_test.go
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"sort"
Expand Down Expand Up @@ -177,10 +178,39 @@ func isWindowsXP(t *testing.T) bool {
}

func listInterfacesWithNetsh() ([]string, error) {
out, err := exec.Command("netsh", "interface", "ip", "show", "config").CombinedOutput()
removeUTF8BOM := func(b []byte) []byte {
if len(b) >= 3 && b[0] == 0xEF && b[1] == 0xBB && b[2] == 0xBF {
return b[3:]
}
return b
}
f, err := ioutil.TempFile("", "netsh")
if err != nil {
return nil, err
}
f.Close()
defer os.Remove(f.Name())
cmd := fmt.Sprintf(`netsh interface ip show config | Out-File "%s" -encoding UTF8`, f.Name())
out, err := exec.Command("powershell", "-Command", cmd).CombinedOutput()
if err != nil {
if len(out) != 0 {
return nil, fmt.Errorf("netsh failed: %v: %q", err, string(removeUTF8BOM(out)))
}
var err2 error
out, err2 = ioutil.ReadFile(f.Name())
if err2 != nil {
return nil, err2
}
if len(out) != 0 {
return nil, fmt.Errorf("netsh failed: %v: %q", err, string(removeUTF8BOM(out)))
}
return nil, fmt.Errorf("netsh failed: %v", err)
}
out, err = ioutil.ReadFile(f.Name())
if err != nil {
return nil, fmt.Errorf("netsh failed: %v: %q", err, string(out))
return nil, err
}
out = removeUTF8BOM(out)
lines := bytes.Split(out, []byte{'\r', '\n'})
names := make([]string, 0)
for _, line := range lines {
Expand Down

0 comments on commit 940d41e

Please sign in to comment.