LEFT | RIGHT |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package net | 5 package net |
6 | 6 |
7 import ( | 7 import ( |
| 8 "bytes" |
8 "testing" | 9 "testing" |
9 ) | 10 ) |
| 11 |
| 12 func sameInterface(i, j *Interface) bool { |
| 13 if i == nil || j == nil { |
| 14 return false |
| 15 } |
| 16 if i.Index == j.Index && i.Name == j.Name && bytes.Equal(i.HardwareAddr,
j.HardwareAddr) { |
| 17 return true |
| 18 } |
| 19 return false |
| 20 } |
| 21 |
| 22 func interfaceFlagsString(ifi *Interface) string { |
| 23 fs := "<" |
| 24 if ifi.IsUp() { |
| 25 fs += "UP," |
| 26 } |
| 27 if ifi.CanBroadcast() { |
| 28 fs += "BROADCAST," |
| 29 } |
| 30 if ifi.IsLoopback() { |
| 31 fs += "LOOPBACK," |
| 32 } |
| 33 if ifi.IsPointToPoint() { |
| 34 fs += "POINTOPOINT," |
| 35 } |
| 36 if ifi.CanMulticast() { |
| 37 fs += "MULTICAST," |
| 38 } |
| 39 if len(fs) > 1 { |
| 40 fs = fs[:len(fs)-1] |
| 41 } |
| 42 fs += ">" |
| 43 return fs |
| 44 } |
10 | 45 |
11 func TestInterfaces(t *testing.T) { | 46 func TestInterfaces(t *testing.T) { |
12 ift, err := Interfaces() | 47 ift, err := Interfaces() |
13 if err != nil { | 48 if err != nil { |
14 t.Fatalf("Interfaces() failed: %v", err) | 49 t.Fatalf("Interfaces() failed: %v", err) |
15 } | 50 } |
16 t.Logf("table: len/cap = %v/%v\n", len(ift), cap(ift)) | 51 t.Logf("table: len/cap = %v/%v\n", len(ift), cap(ift)) |
17 | 52 |
18 for _, ifi := range ift { | 53 for _, ifi := range ift { |
19 » » index, err := InterfaceIndex(ifi.Name) | 54 » » ifxi, err := InterfaceByIndex(ifi.Index) |
20 if err != nil { | 55 if err != nil { |
21 » » » t.Fatalf("InterfaceIndex(%#q) failed: %v", ifi.Name, err
) | 56 » » » t.Fatalf("InterfaceByIndex(%#q) failed: %v", ifi.Index,
err) |
22 } | 57 } |
23 » » if index != ifi.Index { | 58 » » if !sameInterface(ifxi, &ifi) { |
24 » » » t.Fatalf("InterfaceIndex(%#q) = %v, want %v", ifi.Name,
index, ifi.Index) | 59 » » » t.Fatalf("InterfaceByIndex(%#q) = %v, want %v", ifi.Inde
x, *ifxi, ifi) |
25 } | 60 } |
26 | 61 » » ifxn, err := InterfaceByName(ifi.Name) |
27 » » name, err := InterfaceName(ifi.Index) | |
28 if err != nil { | 62 if err != nil { |
29 » » » t.Fatalf("InterfaceName(%#q) failed: %v", ifi.Index, err
) | 63 » » » t.Fatalf("InterfaceByName(%#q) failed: %v", ifi.Name, er
r) |
30 } | 64 } |
31 » » if name != ifi.Name { | 65 » » if !sameInterface(ifxn, &ifi) { |
32 » » » t.Fatalf("InterfaceName(%#q) = %v, want %v", ifi.Index,
name, ifi.Name) | 66 » » » t.Fatalf("InterfaceByName(%#q) = %v, want %v", ifi.Name,
*ifxn, ifi) |
33 } | 67 } |
34 | 68 » » ifat, err := ifi.Addrs() |
35 » » ifat, err := ifi.Addr() | |
36 if err != nil { | 69 if err != nil { |
37 » » » t.Fatalf("Interface.Addr() failed: %v", err) | 70 » » » t.Fatalf("Interface.Addrs() failed: %v", err) |
38 } | 71 } |
39 | 72 » » t.Logf("%s: flags %s, ifindex %v, mtu %v\n", ifi.Name, interface
FlagsString(&ifi), ifi.Index, ifi.MTU) |
40 » » t.Logf("%s: flags %#x, ifindex %v, mtu %v\n", ifi.Name, ifi.Flag
s, ifi.Index, ifi.MTU) | |
41 for _, ifa := range ifat { | 73 for _, ifa := range ifat { |
42 t.Logf("\tinterface address %s\n", ifa.String()) | 74 t.Logf("\tinterface address %s\n", ifa.String()) |
43 } | 75 } |
44 t.Logf("\thardware address %v", ifi.HardwareAddr.String()) | 76 t.Logf("\thardware address %v", ifi.HardwareAddr.String()) |
45 } | 77 } |
46 } | 78 } |
47 | 79 |
48 func TestInterfaceAddrs(t *testing.T) { | 80 func TestInterfaceAddrs(t *testing.T) { |
49 ifat, err := InterfaceAddrs() | 81 ifat, err := InterfaceAddrs() |
50 if err != nil { | 82 if err != nil { |
51 t.Fatalf("InterfaceAddrs() failed: %v", err) | 83 t.Fatalf("InterfaceAddrs() failed: %v", err) |
52 return | |
53 } | 84 } |
54 t.Logf("table: len/cap = %v/%v\n", len(ifat), cap(ifat)) | 85 t.Logf("table: len/cap = %v/%v\n", len(ifat), cap(ifat)) |
55 | 86 |
56 for _, ifa := range ifat { | 87 for _, ifa := range ifat { |
57 t.Logf("interface address %s\n", ifa.String()) | 88 t.Logf("interface address %s\n", ifa.String()) |
58 } | 89 } |
59 } | 90 } |
LEFT | RIGHT |