]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.5] net: fix off by one error while counting interfaces on windows
authorAlex Brainman <alex.brainman@gmail.com>
Fri, 6 Nov 2015 06:29:27 +0000 (17:29 +1100)
committerAustin Clements <austin@google.com>
Tue, 17 Nov 2015 02:24:40 +0000 (02:24 +0000)
Fixes #12301

Change-Id: I8d01ec9551c6cff7e6129e06a7deb36a3be9de41
Reviewed-on: https://go-review.googlesource.com/16751
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-on: https://go-review.googlesource.com/16984
Run-TryBot: Austin Clements <austin@google.com>
Reviewed-by: Alex Brainman <alex.brainman@gmail.com>
Reviewed-by: Russ Cox <rsc@golang.org>
src/net/interface_windows.go
src/net/net_windows_test.go

index e25c1ed560b5f82a24682be0c5028c5dbc6a0157..8cb9d7623747694e9698a2572c12d7b6068ce89b 100644 (file)
@@ -48,7 +48,7 @@ func getInterfaceInfos() ([]syscall.InterfaceInfo, error) {
                return nil, os.NewSyscallError("wsaioctl", err)
        }
        iilen := ret / uint32(unsafe.Sizeof(iia[0]))
-       return iia[:iilen-1], nil
+       return iia[:iilen], nil
 }
 
 func bytesEqualIP(a []byte, b []int8) bool {
index da03e10b3684d281cfdb546711e4214e3d5f5f89..4f6bd45929b8e480629dbfb2d707ba16cb49b453 100644 (file)
@@ -6,10 +6,13 @@ package net
 
 import (
        "bufio"
+       "bytes"
        "fmt"
        "io"
        "os"
        "os/exec"
+       "sort"
+       "strings"
        "syscall"
        "testing"
        "time"
@@ -163,3 +166,53 @@ func TestAcceptIgnoreSomeErrors(t *testing.T) {
                t.Fatalf(`"%s" received from recv, but "abc" expected`, s)
        }
 }
+
+func isWindowsXP(t *testing.T) bool {
+       v, err := syscall.GetVersion()
+       if err != nil {
+               t.Fatalf("GetVersion failed: %v", err)
+       }
+       major := byte(v)
+       return major < 6
+}
+
+func listInterfacesWithNetsh() ([]string, error) {
+       out, err := exec.Command("netsh", "interface", "ip", "show", "config").CombinedOutput()
+       if err != nil {
+               return nil, fmt.Errorf("netsh failed: %v: %q", err, string(out))
+       }
+       lines := bytes.Split(out, []byte{'\r', '\n'})
+       names := make([]string, 0)
+       for _, line := range lines {
+               f := bytes.Split(line, []byte{'"'})
+               if len(f) == 3 {
+                       names = append(names, string(f[1]))
+               }
+       }
+       return names, nil
+}
+
+func TestInterfaceList(t *testing.T) {
+       if isWindowsXP(t) {
+               t.Skip("Windows XP netsh command does not provide required functionality")
+       }
+       ift, err := Interfaces()
+       if err != nil {
+               t.Fatal(err)
+       }
+       have := make([]string, 0)
+       for _, ifi := range ift {
+               have = append(have, ifi.Name)
+       }
+       sort.Strings(have)
+
+       want, err := listInterfacesWithNetsh()
+       if err != nil {
+               t.Fatal(err)
+       }
+       sort.Strings(want)
+
+       if strings.Join(want, "/") != strings.Join(have, "/") {
+               t.Fatalf("unexpected interface list %q, want %q", have, want)
+       }
+}