@@ -6,10 +6,13 @@ package net
6
6
7
7
import (
8
8
"bufio"
9
+ "bytes"
9
10
"fmt"
10
11
"io"
11
12
"os"
12
13
"os/exec"
14
+ "sort"
15
+ "strings"
13
16
"syscall"
14
17
"testing"
15
18
"time"
@@ -163,3 +166,53 @@ func TestAcceptIgnoreSomeErrors(t *testing.T) {
163
166
t .Fatalf (`"%s" received from recv, but "abc" expected` , s )
164
167
}
165
168
}
169
+
170
+ func isWindowsXP (t * testing.T ) bool {
171
+ v , err := syscall .GetVersion ()
172
+ if err != nil {
173
+ t .Fatalf ("GetVersion failed: %v" , err )
174
+ }
175
+ major := byte (v )
176
+ return major < 6
177
+ }
178
+
179
+ func listInterfacesWithNetsh () ([]string , error ) {
180
+ out , err := exec .Command ("netsh" , "interface" , "ip" , "show" , "config" ).CombinedOutput ()
181
+ if err != nil {
182
+ return nil , fmt .Errorf ("netsh failed: %v: %q" , err , string (out ))
183
+ }
184
+ lines := bytes .Split (out , []byte {'\r' , '\n' })
185
+ names := make ([]string , 0 )
186
+ for _ , line := range lines {
187
+ f := bytes .Split (line , []byte {'"' })
188
+ if len (f ) == 3 {
189
+ names = append (names , string (f [1 ]))
190
+ }
191
+ }
192
+ return names , nil
193
+ }
194
+
195
+ func TestInterfaceList (t * testing.T ) {
196
+ if isWindowsXP (t ) {
197
+ t .Skip ("Windows XP netsh command does not provide required functionality" )
198
+ }
199
+ ift , err := Interfaces ()
200
+ if err != nil {
201
+ t .Fatal (err )
202
+ }
203
+ have := make ([]string , 0 )
204
+ for _ , ifi := range ift {
205
+ have = append (have , ifi .Name )
206
+ }
207
+ sort .Strings (have )
208
+
209
+ want , err := listInterfacesWithNetsh ()
210
+ if err != nil {
211
+ t .Fatal (err )
212
+ }
213
+ sort .Strings (want )
214
+
215
+ if strings .Join (want , "/" ) != strings .Join (have , "/" ) {
216
+ t .Fatalf ("unexpected interface list %q, want %q" , have , want )
217
+ }
218
+ }
0 commit comments