github.com/ader1990/go@v0.0.0-20140630135419-8c24447fa791/src/pkg/net/interface_test.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package net 6 7 import ( 8 "reflect" 9 "testing" 10 ) 11 12 // loopbackInterface returns an available logical network interface 13 // for loopback tests. It returns nil if no suitable interface is 14 // found. 15 func loopbackInterface() *Interface { 16 ift, err := Interfaces() 17 if err != nil { 18 return nil 19 } 20 for _, ifi := range ift { 21 if ifi.Flags&FlagLoopback != 0 && ifi.Flags&FlagUp != 0 { 22 return &ifi 23 } 24 } 25 return nil 26 } 27 28 // ipv6LinkLocalUnicastAddr returns an IPv6 link-local unicast address 29 // on the given network interface for tests. It returns "" if no 30 // suitable address is found. 31 func ipv6LinkLocalUnicastAddr(ifi *Interface) string { 32 if ifi == nil { 33 return "" 34 } 35 ifat, err := ifi.Addrs() 36 if err != nil { 37 return "" 38 } 39 for _, ifa := range ifat { 40 switch ifa := ifa.(type) { 41 case *IPAddr: 42 if ifa.IP.To4() == nil && ifa.IP.IsLinkLocalUnicast() { 43 return ifa.IP.String() 44 } 45 case *IPNet: 46 if ifa.IP.To4() == nil && ifa.IP.IsLinkLocalUnicast() { 47 return ifa.IP.String() 48 } 49 } 50 } 51 return "" 52 } 53 54 func TestInterfaces(t *testing.T) { 55 ift, err := Interfaces() 56 if err != nil { 57 t.Fatalf("Interfaces failed: %v", err) 58 } 59 t.Logf("table: len/cap = %v/%v", len(ift), cap(ift)) 60 61 for _, ifi := range ift { 62 ifxi, err := InterfaceByIndex(ifi.Index) 63 if err != nil { 64 t.Fatalf("InterfaceByIndex(%v) failed: %v", ifi.Index, err) 65 } 66 if !reflect.DeepEqual(ifxi, &ifi) { 67 t.Fatalf("InterfaceByIndex(%v) = %v, want %v", ifi.Index, ifxi, ifi) 68 } 69 ifxn, err := InterfaceByName(ifi.Name) 70 if err != nil { 71 t.Fatalf("InterfaceByName(%q) failed: %v", ifi.Name, err) 72 } 73 if !reflect.DeepEqual(ifxn, &ifi) { 74 t.Fatalf("InterfaceByName(%q) = %v, want %v", ifi.Name, ifxn, ifi) 75 } 76 t.Logf("%q: flags %q, ifindex %v, mtu %v", ifi.Name, ifi.Flags.String(), ifi.Index, ifi.MTU) 77 t.Logf("\thardware address %q", ifi.HardwareAddr.String()) 78 testInterfaceAddrs(t, &ifi) 79 testInterfaceMulticastAddrs(t, &ifi) 80 } 81 } 82 83 func TestInterfaceAddrs(t *testing.T) { 84 ifat, err := InterfaceAddrs() 85 if err != nil { 86 t.Fatalf("InterfaceAddrs failed: %v", err) 87 } 88 t.Logf("table: len/cap = %v/%v", len(ifat), cap(ifat)) 89 testAddrs(t, ifat) 90 } 91 92 func testInterfaceAddrs(t *testing.T, ifi *Interface) { 93 ifat, err := ifi.Addrs() 94 if err != nil { 95 t.Fatalf("Interface.Addrs failed: %v", err) 96 } 97 testAddrs(t, ifat) 98 } 99 100 func testInterfaceMulticastAddrs(t *testing.T, ifi *Interface) { 101 ifmat, err := ifi.MulticastAddrs() 102 if err != nil { 103 t.Fatalf("Interface.MulticastAddrs failed: %v", err) 104 } 105 testMulticastAddrs(t, ifmat) 106 } 107 108 func testAddrs(t *testing.T, ifat []Addr) { 109 for _, ifa := range ifat { 110 switch ifa := ifa.(type) { 111 case *IPAddr: 112 if ifa == nil || ifa.IP == nil { 113 t.Errorf("\tunexpected value: %v, %v", ifa, ifa.IP) 114 } else { 115 t.Logf("\tinterface address %q", ifa.String()) 116 } 117 case *IPNet: 118 if ifa == nil || ifa.IP == nil || ifa.Mask == nil { 119 t.Errorf("\tunexpected value: %v, %v, %v", ifa, ifa.IP, ifa.Mask) 120 } else { 121 _, prefixLen := ifa.Mask.Size() 122 if ifa.IP.To4() != nil && prefixLen != 8*IPv4len || ifa.IP.To16() != nil && ifa.IP.To4() == nil && prefixLen != 8*IPv6len { 123 t.Errorf("\tunexpected value: %v, %v, %v, %v", ifa, ifa.IP, ifa.Mask, prefixLen) 124 } else { 125 t.Logf("\tinterface address %q", ifa.String()) 126 } 127 } 128 default: 129 t.Errorf("\tunexpected type: %T", ifa) 130 } 131 } 132 } 133 134 func testMulticastAddrs(t *testing.T, ifmat []Addr) { 135 for _, ifma := range ifmat { 136 switch ifma := ifma.(type) { 137 case *IPAddr: 138 if ifma == nil { 139 t.Errorf("\tunexpected value: %v", ifma) 140 } else { 141 t.Logf("\tjoined group address %q", ifma.String()) 142 } 143 default: 144 t.Errorf("\tunexpected type: %T", ifma) 145 } 146 } 147 } 148 149 func BenchmarkInterfaces(b *testing.B) { 150 for i := 0; i < b.N; i++ { 151 if _, err := Interfaces(); err != nil { 152 b.Fatalf("Interfaces failed: %v", err) 153 } 154 } 155 } 156 157 func BenchmarkInterfaceByIndex(b *testing.B) { 158 ifi := loopbackInterface() 159 if ifi == nil { 160 b.Skip("loopback interface not found") 161 } 162 for i := 0; i < b.N; i++ { 163 if _, err := InterfaceByIndex(ifi.Index); err != nil { 164 b.Fatalf("InterfaceByIndex failed: %v", err) 165 } 166 } 167 } 168 169 func BenchmarkInterfaceByName(b *testing.B) { 170 ifi := loopbackInterface() 171 if ifi == nil { 172 b.Skip("loopback interface not found") 173 } 174 for i := 0; i < b.N; i++ { 175 if _, err := InterfaceByName(ifi.Name); err != nil { 176 b.Fatalf("InterfaceByName failed: %v", err) 177 } 178 } 179 } 180 181 func BenchmarkInterfaceAddrs(b *testing.B) { 182 for i := 0; i < b.N; i++ { 183 if _, err := InterfaceAddrs(); err != nil { 184 b.Fatalf("InterfaceAddrs failed: %v", err) 185 } 186 } 187 } 188 189 func BenchmarkInterfacesAndAddrs(b *testing.B) { 190 ifi := loopbackInterface() 191 if ifi == nil { 192 b.Skip("loopback interface not found") 193 } 194 for i := 0; i < b.N; i++ { 195 if _, err := ifi.Addrs(); err != nil { 196 b.Fatalf("Interface.Addrs failed: %v", err) 197 } 198 } 199 } 200 201 func BenchmarkInterfacesAndMulticastAddrs(b *testing.B) { 202 ifi := loopbackInterface() 203 if ifi == nil { 204 b.Skip("loopback interface not found") 205 } 206 for i := 0; i < b.N; i++ { 207 if _, err := ifi.MulticastAddrs(); err != nil { 208 b.Fatalf("Interface.MulticastAddrs failed: %v", err) 209 } 210 } 211 }