From: Tim Ebringer Date: Mon, 18 Jan 2016 10:33:25 +0000 (+1100) Subject: net: improve netsh usage in Windows unit tests X-Git-Tag: go1.6rc1~79 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=489f65b52ad28c2a6f4f2dc58a3d939e9a77be47;p=gostls13.git net: improve netsh usage in Windows unit tests The TestInterfaceAddrsWithNetsh Windows unit test parses and compares the output of the "netsh" command against more low level Windows API calls. In at least two cases, some quirks of netsh cause these comparisons to fail. One example appears to be wi-fi adapters. After a reboot, before it has been allowed to connect to a network, netsh for IPv4 will not show an address, whereas netsh for IPv6 will. If the interface is allowed to connect, and then disconnected, netsh for IPv4 now shows an address and the test will pass. The fix is to not compare netsh output if the interface is down. A related issue is that the IPv6 version of "netsh" can return an IPv4-embedded IPv6 address where the IPv4 component of the address is in decimal form, whilst the test is expecting hexadecimal form. For example, output might be: Address fe80::5efe:192.168.1.7%6 Parameters ... Whilst this is valid notation, the fix is to recognise this format in the "netsh" output and re-parse the address into the all-hexadecimal representation that the test is expecting. Fixes #13981 Change-Id: Ie8366673f4d43d07bad80d6d5d1d6e33f654b6cc Reviewed-on: https://go-review.googlesource.com/18711 Reviewed-by: Alex Brainman Run-TryBot: Alex Brainman TryBot-Result: Gobot Gobot --- diff --git a/src/net/net_windows_test.go b/src/net/net_windows_test.go index e59dcd52d3..095a339e02 100644 --- a/src/net/net_windows_test.go +++ b/src/net/net_windows_test.go @@ -12,6 +12,7 @@ import ( "io/ioutil" "os" "os/exec" + "regexp" "sort" "strings" "syscall" @@ -374,14 +375,20 @@ func netshInterfaceIPv6ShowAddress(name string) ([]string, error) { } // remove scope ID if present f = bytes.Split(f[1], []byte{'%'}) + + // netsh can create IPv4-embedded IPv6 addresses, like fe80::5efe:192.168.140.1. + // Convert these to all hexadecimal fe80::5efe:c0a8:8c01 for later string comparisons. + ipv4Tail := regexp.MustCompile(`:\d+\.\d+\.\d+\.\d+$`) + if ipv4Tail.Match(f[0]) { + f[0] = []byte(ParseIP(string(f[0])).String()) + } + addrs = append(addrs, string(bytes.ToLower(bytes.TrimSpace(f[0])))) } return addrs, nil } func TestInterfaceAddrsWithNetsh(t *testing.T) { - t.Skip("see https://golang.org/issue/13981") - if isWindowsXP(t) { t.Skip("Windows XP netsh command does not provide required functionality") } @@ -393,6 +400,10 @@ func TestInterfaceAddrsWithNetsh(t *testing.T) { t.Fatal(err) } for _, ifi := range ift { + // Skip the interface if it's down. + if (ifi.Flags & FlagUp) == 0 { + continue + } have := make([]string, 0) addrs, err := ifi.Addrs() if err != nil {