]> Cypherpunks repositories - gostls13.git/commitdiff
net: make TestInterfaceAddrsWithNetsh more robust
authorAlex Brainman <alex.brainman@gmail.com>
Wed, 10 Feb 2016 23:15:58 +0000 (10:15 +1100)
committerRuss Cox <rsc@golang.org>
Tue, 16 Feb 2016 17:16:00 +0000 (17:16 +0000)
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/19441
Reviewed-by: Volker Dobler <dr.volker.dobler@gmail.com>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
src/net/net_windows_test.go

index c26c6a77c91443397eb392697ae8c00617df6996..df390327218e3307a97609ff65f51bc1e505c3b7 100644 (file)
@@ -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)