|
1 From ecb9ce6a781717364697c298e1d4c13e93e1c949 Mon Sep 17 00:00:00 2001 |
|
2 From: Alex Brainman <[email protected]> |
|
3 Date: Fri, 6 Nov 2015 17:29:27 +1100 |
|
4 Subject: [PATCH 41/63] [release-branch.go1.5] net: fix off by one error while |
|
5 counting interfaces on windows |
|
6 |
|
7 Fixes #12301 |
|
8 |
|
9 Change-Id: I8d01ec9551c6cff7e6129e06a7deb36a3be9de41 |
|
10 Reviewed-on: https://go-review.googlesource.com/16751 |
|
11 Reviewed-by: Ian Lance Taylor <[email protected]> |
|
12 Run-TryBot: Ian Lance Taylor <[email protected]> |
|
13 TryBot-Result: Gobot Gobot <[email protected]> |
|
14 Reviewed-on: https://go-review.googlesource.com/16984 |
|
15 Run-TryBot: Austin Clements <[email protected]> |
|
16 Reviewed-by: Alex Brainman <[email protected]> |
|
17 Reviewed-by: Russ Cox <[email protected]> |
|
18 --- |
|
19 src/net/interface_windows.go | 2 +- |
|
20 src/net/net_windows_test.go | 53 ++++++++++++++++++++++++++++++++++++++++++++ |
|
21 2 files changed, 54 insertions(+), 1 deletion(-) |
|
22 |
|
23 diff --git a/src/net/interface_windows.go b/src/net/interface_windows.go |
|
24 index e25c1ed..8cb9d76 100644 |
|
25 --- a/src/net/interface_windows.go |
|
26 +++ b/src/net/interface_windows.go |
|
27 @@ -48,7 +48,7 @@ func getInterfaceInfos() ([]syscall.InterfaceInfo, error) { |
|
28 return nil, os.NewSyscallError("wsaioctl", err) |
|
29 } |
|
30 iilen := ret / uint32(unsafe.Sizeof(iia[0])) |
|
31 - return iia[:iilen-1], nil |
|
32 + return iia[:iilen], nil |
|
33 } |
|
34 |
|
35 func bytesEqualIP(a []byte, b []int8) bool { |
|
36 diff --git a/src/net/net_windows_test.go b/src/net/net_windows_test.go |
|
37 index da03e10..4f6bd45 100644 |
|
38 --- a/src/net/net_windows_test.go |
|
39 +++ b/src/net/net_windows_test.go |
|
40 @@ -6,10 +6,13 @@ package net |
|
41 |
|
42 import ( |
|
43 "bufio" |
|
44 + "bytes" |
|
45 "fmt" |
|
46 "io" |
|
47 "os" |
|
48 "os/exec" |
|
49 + "sort" |
|
50 + "strings" |
|
51 "syscall" |
|
52 "testing" |
|
53 "time" |
|
54 @@ -163,3 +166,53 @@ func TestAcceptIgnoreSomeErrors(t *testing.T) { |
|
55 t.Fatalf(`"%s" received from recv, but "abc" expected`, s) |
|
56 } |
|
57 } |
|
58 + |
|
59 +func isWindowsXP(t *testing.T) bool { |
|
60 + v, err := syscall.GetVersion() |
|
61 + if err != nil { |
|
62 + t.Fatalf("GetVersion failed: %v", err) |
|
63 + } |
|
64 + major := byte(v) |
|
65 + return major < 6 |
|
66 +} |
|
67 + |
|
68 +func listInterfacesWithNetsh() ([]string, error) { |
|
69 + out, err := exec.Command("netsh", "interface", "ip", "show", "config").CombinedOutput() |
|
70 + if err != nil { |
|
71 + return nil, fmt.Errorf("netsh failed: %v: %q", err, string(out)) |
|
72 + } |
|
73 + lines := bytes.Split(out, []byte{'\r', '\n'}) |
|
74 + names := make([]string, 0) |
|
75 + for _, line := range lines { |
|
76 + f := bytes.Split(line, []byte{'"'}) |
|
77 + if len(f) == 3 { |
|
78 + names = append(names, string(f[1])) |
|
79 + } |
|
80 + } |
|
81 + return names, nil |
|
82 +} |
|
83 + |
|
84 +func TestInterfaceList(t *testing.T) { |
|
85 + if isWindowsXP(t) { |
|
86 + t.Skip("Windows XP netsh command does not provide required functionality") |
|
87 + } |
|
88 + ift, err := Interfaces() |
|
89 + if err != nil { |
|
90 + t.Fatal(err) |
|
91 + } |
|
92 + have := make([]string, 0) |
|
93 + for _, ifi := range ift { |
|
94 + have = append(have, ifi.Name) |
|
95 + } |
|
96 + sort.Strings(have) |
|
97 + |
|
98 + want, err := listInterfacesWithNetsh() |
|
99 + if err != nil { |
|
100 + t.Fatal(err) |
|
101 + } |
|
102 + sort.Strings(want) |
|
103 + |
|
104 + if strings.Join(want, "/") != strings.Join(have, "/") { |
|
105 + t.Fatalf("unexpected interface list %q, want %q", have, want) |
|
106 + } |
|
107 +} |
|
108 -- |
|
109 2.6.1 |
|
110 |