1
0
mirror of https://github.com/golang/go synced 2024-11-23 14:50:07 -07:00

net: make TestInterfaceList work on non-English Windows

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>
This commit is contained in:
Yasuhiro Matsumoto 2015-11-10 11:23:41 +09:00 committed by Alex Brainman
parent 0adf6dce8a
commit 940d41e386

View File

@ -9,6 +9,7 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"sort"
@ -177,10 +178,39 @@ func isWindowsXP(t *testing.T) bool {
}
func listInterfacesWithNetsh() ([]string, error) {
out, err := exec.Command("netsh", "interface", "ip", "show", "config").CombinedOutput()
if err != nil {
return nil, fmt.Errorf("netsh failed: %v: %q", err, string(out))
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, err
}
out = removeUTF8BOM(out)
lines := bytes.Split(out, []byte{'\r', '\n'})
names := make([]string, 0)
for _, line := range lines {