Commit e136cac4 authored by Alex Brainman's avatar Alex Brainman Committed by Russ Cox

net: make TestInterfaceAddrsWithNetsh more robust

TestInterfaceAddrsWithNetsh invokes Windows netsh command passing
it a particular interface name. This approach somehow does not work
on some computers (see issue for details). Change that to call netsh
without specifying any interface name. This provides output for all
interfaces available. So we can achieve same goal parsing this output.
Also makes test faster because we only need to invoke netsh once.

Fixes #14130.

Change-Id: I7911692ca64e372af1e1f9d6acb718c67071de67
Reviewed-on: https://go-review.googlesource.com/19441Reviewed-by: default avatarVolker Dobler <dr.volker.dobler@gmail.com>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 0c02bc00
......@@ -314,20 +314,43 @@ func TestInterfacesWithNetsh(t *testing.T) {
}
}
func netshInterfaceIPv4ShowAddress(name string) ([]string, error) {
out, err := runCmd("netsh", "interface", "ipv4", "show", "address", "name=\""+name+"\"")
if err != nil {
return nil, err
}
func netshInterfaceIPv4ShowAddress(name string, netshOutput []byte) []string {
// adress information is listed like:
//
//Configuration for interface "Local Area Connection"
// DHCP enabled: Yes
// IP Address: 10.0.0.2
// Subnet Prefix: 10.0.0.0/24 (mask 255.255.255.0)
// IP Address: 10.0.0.3
// Subnet Prefix: 10.0.0.0/24 (mask 255.255.255.0)
// Default Gateway: 10.0.0.254
// Gateway Metric: 0
// InterfaceMetric: 10
//
//Configuration for interface "Loopback Pseudo-Interface 1"
// DHCP enabled: No
// IP Address: 127.0.0.1
// Subnet Prefix: 127.0.0.0/8 (mask 255.0.0.0)
// InterfaceMetric: 50
//
addrs := make([]string, 0)
var addr, subnetprefix string
lines := bytes.Split(out, []byte{'\r', '\n'})
var processingOurInterface bool
lines := bytes.Split(netshOutput, []byte{'\r', '\n'})
for _, line := range lines {
if !processingOurInterface {
if !bytes.HasPrefix(line, []byte("Configuration for interface")) {
continue
}
if !bytes.Contains(line, []byte(`"`+name+`"`)) {
continue
}
processingOurInterface = true
continue
}
if len(line) == 0 {
break
}
if bytes.Contains(line, []byte("Subnet Prefix:")) {
f := bytes.Split(line, []byte{':'})
if len(f) == 2 {
......@@ -351,18 +374,50 @@ func netshInterfaceIPv4ShowAddress(name string) ([]string, error) {
}
}
}
return addrs, nil
return addrs
}
func netshInterfaceIPv6ShowAddress(name string) ([]string, error) {
func netshInterfaceIPv6ShowAddress(name string, netshOutput []byte) []string {
// adress information is listed like:
//
//Address ::1 Parameters
//---------------------------------------------------------
//Interface Luid : Loopback Pseudo-Interface 1
//Scope Id : 0.0
//Valid Lifetime : infinite
//Preferred Lifetime : infinite
//DAD State : Preferred
//Address Type : Other
//Skip as Source : false
//
//Address XXXX::XXXX:XXXX:XXXX:XXXX%11 Parameters
//---------------------------------------------------------
//Interface Luid : Local Area Connection
//Scope Id : 0.11
//Valid Lifetime : infinite
//Preferred Lifetime : infinite
//DAD State : Preferred
//Address Type : Other
//Skip as Source : false
//
// TODO: need to test ipv6 netmask too, but netsh does not outputs it
out, err := runCmd("netsh", "interface", "ipv6", "show", "address", "interface=\""+name+"\"")
if err != nil {
return nil, err
}
var addr string
addrs := make([]string, 0)
lines := bytes.Split(out, []byte{'\r', '\n'})
lines := bytes.Split(netshOutput, []byte{'\r', '\n'})
for _, line := range lines {
if addr != "" {
if len(line) == 0 {
addr = ""
continue
}
if string(line) != "Interface Luid : "+name {
continue
}
addrs = append(addrs, addr)
addr = ""
continue
}
if !bytes.HasPrefix(line, []byte("Address")) {
continue
}
......@@ -383,9 +438,9 @@ func netshInterfaceIPv6ShowAddress(name string) ([]string, error) {
f[0] = []byte(ParseIP(string(f[0])).String())
}
addrs = append(addrs, string(bytes.ToLower(bytes.TrimSpace(f[0]))))
addr = string(bytes.ToLower(bytes.TrimSpace(f[0])))
}
return addrs, nil
return addrs
}
func TestInterfaceAddrsWithNetsh(t *testing.T) {
......@@ -395,6 +450,16 @@ func TestInterfaceAddrsWithNetsh(t *testing.T) {
if !isEnglishOS(t) {
t.Skip("English version of OS required for this test")
}
outIPV4, err := runCmd("netsh", "interface", "ipv4", "show", "address")
if err != nil {
t.Fatal(err)
}
outIPV6, err := runCmd("netsh", "interface", "ipv6", "show", "address", "level=verbose")
if err != nil {
t.Fatal(err)
}
ift, err := Interfaces()
if err != nil {
t.Fatal(err)
......@@ -431,14 +496,8 @@ func TestInterfaceAddrsWithNetsh(t *testing.T) {
}
sort.Strings(have)
want, err := netshInterfaceIPv4ShowAddress(ifi.Name)
if err != nil {
t.Fatal(err)
}
wantIPv6, err := netshInterfaceIPv6ShowAddress(ifi.Name)
if err != nil {
t.Fatal(err)
}
want := netshInterfaceIPv4ShowAddress(ifi.Name, outIPV4)
wantIPv6 := netshInterfaceIPv6ShowAddress(ifi.Name, outIPV6)
want = append(want, wantIPv6...)
sort.Strings(want)
......
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