]> Cypherpunks repositories - gostls13.git/commitdiff
net: improve netsh usage in Windows unit tests
authorTim Ebringer <tim.ebringer@gmail.com>
Mon, 18 Jan 2016 10:33:25 +0000 (21:33 +1100)
committerAlex Brainman <alex.brainman@gmail.com>
Thu, 21 Jan 2016 23:01:56 +0000 (23:01 +0000)
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 <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/net_windows_test.go

index e59dcd52d3dc7537afbbd85c4a94ec9214d7d8c2..095a339e02c8e69a8a258526733ee94d8a92bd1e 100644 (file)
@@ -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 {