Commit 489f65b5 authored by Tim Ebringer's avatar Tim Ebringer Committed by Alex Brainman

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/18711Reviewed-by: default avatarAlex Brainman <alex.brainman@gmail.com>
Run-TryBot: Alex Brainman <alex.brainman@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent b203f88c
......@@ -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 {
......
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