gcc:
[official-gcc.git] / libgo / go / net / interface_test.go
blobefabb5f3c25daafeed4082067bb83368cd181912
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.
5 package net
7 import (
8 "reflect"
9 "testing"
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
20 for _, ifi := range ift {
21 if ifi.Flags&FlagLoopback != 0 && ifi.Flags&FlagUp != 0 {
22 return &ifi
25 return nil
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 ""
35 ifat, err := ifi.Addrs()
36 if err != nil {
37 return ""
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()
45 case *IPNet:
46 if ifa.IP.To4() == nil && ifa.IP.IsLinkLocalUnicast() {
47 return ifa.IP.String()
51 return ""
54 func TestInterfaces(t *testing.T) {
55 ift, err := Interfaces()
56 if err != nil {
57 t.Fatalf("Interfaces failed: %v", err)
59 t.Logf("table: len/cap = %v/%v", len(ift), cap(ift))
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)
66 if !reflect.DeepEqual(ifxi, &ifi) {
67 t.Fatalf("InterfaceByIndex(%v) = %v, want %v", ifi.Index, ifxi, ifi)
69 ifxn, err := InterfaceByName(ifi.Name)
70 if err != nil {
71 t.Fatalf("InterfaceByName(%q) failed: %v", ifi.Name, err)
73 if !reflect.DeepEqual(ifxn, &ifi) {
74 t.Fatalf("InterfaceByName(%q) = %v, want %v", ifi.Name, ifxn, ifi)
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)
83 func TestInterfaceAddrs(t *testing.T) {
84 ifat, err := InterfaceAddrs()
85 if err != nil {
86 t.Fatalf("InterfaceAddrs failed: %v", err)
88 t.Logf("table: len/cap = %v/%v", len(ifat), cap(ifat))
89 testAddrs(t, ifat)
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)
97 testAddrs(t, ifat)
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)
105 testMulticastAddrs(t, ifmat)
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())
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())
128 default:
129 t.Errorf("\tunexpected type: %T", ifa)
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())
143 default:
144 t.Errorf("\tunexpected type: %T", ifma)
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)
157 func BenchmarkInterfaceByIndex(b *testing.B) {
158 ifi := loopbackInterface()
159 if ifi == nil {
160 b.Skip("loopback interface not found")
162 for i := 0; i < b.N; i++ {
163 if _, err := InterfaceByIndex(ifi.Index); err != nil {
164 b.Fatalf("InterfaceByIndex failed: %v", err)
169 func BenchmarkInterfaceByName(b *testing.B) {
170 ifi := loopbackInterface()
171 if ifi == nil {
172 b.Skip("loopback interface not found")
174 for i := 0; i < b.N; i++ {
175 if _, err := InterfaceByName(ifi.Name); err != nil {
176 b.Fatalf("InterfaceByName failed: %v", err)
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)
189 func BenchmarkInterfacesAndAddrs(b *testing.B) {
190 ifi := loopbackInterface()
191 if ifi == nil {
192 b.Skip("loopback interface not found")
194 for i := 0; i < b.N; i++ {
195 if _, err := ifi.Addrs(); err != nil {
196 b.Fatalf("Interface.Addrs failed: %v", err)
201 func BenchmarkInterfacesAndMulticastAddrs(b *testing.B) {
202 ifi := loopbackInterface()
203 if ifi == nil {
204 b.Skip("loopback interface not found")
206 for i := 0; i < b.N; i++ {
207 if _, err := ifi.MulticastAddrs(); err != nil {
208 b.Fatalf("Interface.MulticastAddrs failed: %v", err)