Commit 940d41e3 authored by Yasuhiro Matsumoto's avatar Yasuhiro Matsumoto Committed by Alex Brainman

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: default avatarAlex Brainman <alex.brainman@gmail.com>
parent 0adf6dce
......@@ -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()
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 {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment