diff options
| author | Yasuhiro Matsumoto <mattn.jp@gmail.com> | 2015-11-10 11:23:41 +0900 |
|---|---|---|
| committer | Alex Brainman <alex.brainman@gmail.com> | 2015-11-13 01:14:12 +0000 |
| commit | 940d41e386d92bda0f4fb39541df89ed424a012d (patch) | |
| tree | ae70bfb0fb4988e27e510cbc9ea6c04ab63f2306 /src | |
| parent | 0adf6dce8a8895fa7dd6f366ba336d9471b32631 (diff) | |
| download | go-940d41e386d92bda0f4fb39541df89ed424a012d.tar.xz | |
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>
Diffstat (limited to 'src')
| -rw-r--r-- | src/net/net_windows_test.go | 34 |
1 files changed, 32 insertions, 2 deletions
diff --git a/src/net/net_windows_test.go b/src/net/net_windows_test.go index 4f6bd45929..ba81ebbb35 100644 --- a/src/net/net_windows_test.go +++ b/src/net/net_windows_test.go @@ -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 { |
