Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(18)

Delta Between Two Patch Sets: src/pkg/net/interface_test.go

Issue 6816116: code review 6816116: net: support IPv6 scoped addressing zone (Closed)
Left Patch Set: diff -r 697f36fec52c https://go.googlecode.com/hg/ Created 12 years, 4 months ago
Right Patch Set: diff -r 0f3086c49268 https://code.google.com/p/go Created 12 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/pkg/net/dial.go ('k') | src/pkg/net/ip.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 » "reflect"
9 "testing" 9 "testing"
10 ) 10 )
11 11
12 // loopbackInterface returns an available logical network interface
13 // for loopback tests. It returns nil if no suitable interface is
14 // found.
12 func loopbackInterface() *Interface { 15 func loopbackInterface() *Interface {
13 ift, err := Interfaces() 16 ift, err := Interfaces()
14 if err != nil { 17 if err != nil {
15 return nil 18 return nil
16 } 19 }
17 for _, ifi := range ift { 20 for _, ifi := range ift {
18 if ifi.Flags&FlagLoopback != 0 && ifi.Flags&FlagUp != 0 { 21 if ifi.Flags&FlagLoopback != 0 && ifi.Flags&FlagUp != 0 {
19 return &ifi 22 return &ifi
20 } 23 }
21 } 24 }
22 return nil 25 return nil
23 } 26 }
24 27
25 func sameInterface(i, j *Interface) bool { 28 // ipv6LinkLocalUnicastAddr returns an IPv6 link-local unicast address
26 » if i == nil || j == nil { 29 // on the given network interface for tests. It returns "" if no
27 » » return false 30 // suitable address is found.
28 » } 31 func ipv6LinkLocalUnicastAddr(ifi *Interface) string {
29 » if i.Index == j.Index && i.Name == j.Name && bytes.Equal(i.HardwareAddr, j.HardwareAddr) { 32 » if ifi == nil {
30 » » return true 33 » » return ""
31 » } 34 » }
32 » return false 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 ""
33 } 52 }
34 53
35 func TestInterfaces(t *testing.T) { 54 func TestInterfaces(t *testing.T) {
36 ift, err := Interfaces() 55 ift, err := Interfaces()
37 if err != nil { 56 if err != nil {
38 t.Fatalf("Interfaces failed: %v", err) 57 t.Fatalf("Interfaces failed: %v", err)
39 } 58 }
40 t.Logf("table: len/cap = %v/%v", len(ift), cap(ift)) 59 t.Logf("table: len/cap = %v/%v", len(ift), cap(ift))
41 60
42 for _, ifi := range ift { 61 for _, ifi := range ift {
43 ifxi, err := InterfaceByIndex(ifi.Index) 62 ifxi, err := InterfaceByIndex(ifi.Index)
44 if err != nil { 63 if err != nil {
45 » » » t.Fatalf("InterfaceByIndex(%q) failed: %v", ifi.Index, e rr) 64 » » » t.Fatalf("InterfaceByIndex(%v) failed: %v", ifi.Index, e rr)
46 » » } 65 » » }
47 » » if !sameInterface(ifxi, &ifi) { 66 » » if !reflect.DeepEqual(ifxi, &ifi) {
48 » » » t.Fatalf("InterfaceByIndex(%q) = %v, want %v", ifi.Index , *ifxi, ifi) 67 » » » t.Fatalf("InterfaceByIndex(%v) = %v, want %v", ifi.Index , ifxi, ifi)
49 } 68 }
50 ifxn, err := InterfaceByName(ifi.Name) 69 ifxn, err := InterfaceByName(ifi.Name)
51 if err != nil { 70 if err != nil {
52 t.Fatalf("InterfaceByName(%q) failed: %v", ifi.Name, err ) 71 t.Fatalf("InterfaceByName(%q) failed: %v", ifi.Name, err )
53 } 72 }
54 » » if !sameInterface(ifxn, &ifi) { 73 » » if !reflect.DeepEqual(ifxn, &ifi) {
55 » » » t.Fatalf("InterfaceByName(%q) = %v, want %v", ifi.Name, *ifxn, ifi) 74 » » » t.Fatalf("InterfaceByName(%q) = %v, want %v", ifi.Name, ifxn, ifi)
56 } 75 }
57 t.Logf("%q: flags %q, ifindex %v, mtu %v", ifi.Name, ifi.Flags.S tring(), ifi.Index, ifi.MTU) 76 t.Logf("%q: flags %q, ifindex %v, mtu %v", ifi.Name, ifi.Flags.S tring(), ifi.Index, ifi.MTU)
58 t.Logf("\thardware address %q", ifi.HardwareAddr.String()) 77 t.Logf("\thardware address %q", ifi.HardwareAddr.String())
59 testInterfaceAddrs(t, &ifi) 78 testInterfaceAddrs(t, &ifi)
60 testInterfaceMulticastAddrs(t, &ifi) 79 testInterfaceMulticastAddrs(t, &ifi)
61 } 80 }
62 } 81 }
63 82
64 func TestInterfaceAddrs(t *testing.T) { 83 func TestInterfaceAddrs(t *testing.T) {
65 ifat, err := InterfaceAddrs() 84 ifat, err := InterfaceAddrs()
(...skipping 15 matching lines...) Expand all
81 func testInterfaceMulticastAddrs(t *testing.T, ifi *Interface) { 100 func testInterfaceMulticastAddrs(t *testing.T, ifi *Interface) {
82 ifmat, err := ifi.MulticastAddrs() 101 ifmat, err := ifi.MulticastAddrs()
83 if err != nil { 102 if err != nil {
84 t.Fatalf("Interface.MulticastAddrs failed: %v", err) 103 t.Fatalf("Interface.MulticastAddrs failed: %v", err)
85 } 104 }
86 testMulticastAddrs(t, ifmat) 105 testMulticastAddrs(t, ifmat)
87 } 106 }
88 107
89 func testAddrs(t *testing.T, ifat []Addr) { 108 func testAddrs(t *testing.T, ifat []Addr) {
90 for _, ifa := range ifat { 109 for _, ifa := range ifat {
91 » » switch ifa.(type) { 110 » » switch ifa := ifa.(type) {
92 case *IPAddr, *IPNet: 111 case *IPAddr, *IPNet:
93 » » » t.Logf("\tinterface address %q", ifa.String()) 112 » » » if ifa == nil {
113 » » » » t.Errorf("\tunexpected value: %v", ifa)
114 » » » } else {
115 » » » » t.Logf("\tinterface address %q", ifa.String())
116 » » » }
94 default: 117 default:
95 t.Errorf("\tunexpected type: %T", ifa) 118 t.Errorf("\tunexpected type: %T", ifa)
96 } 119 }
97 } 120 }
98 } 121 }
99 122
100 func testMulticastAddrs(t *testing.T, ifmat []Addr) { 123 func testMulticastAddrs(t *testing.T, ifmat []Addr) {
101 for _, ifma := range ifmat { 124 for _, ifma := range ifmat {
102 » » switch ifma.(type) { 125 » » switch ifma := ifma.(type) {
103 case *IPAddr: 126 case *IPAddr:
104 » » » t.Logf("\tjoined group address %q", ifma.String()) 127 » » » if ifma == nil {
128 » » » » t.Errorf("\tunexpected value: %v", ifma)
129 » » » } else {
130 » » » » t.Logf("\tjoined group address %q", ifma.String( ))
131 » » » }
105 default: 132 default:
106 t.Errorf("\tunexpected type: %T", ifma) 133 t.Errorf("\tunexpected type: %T", ifma)
107 } 134 }
108 } 135 }
109 } 136 }
137
138 func BenchmarkInterfaces(b *testing.B) {
139 for i := 0; i < b.N; i++ {
140 if _, err := Interfaces(); err != nil {
141 b.Fatalf("Interfaces failed: %v", err)
142 }
143 }
144 }
145
146 func BenchmarkInterfaceByIndex(b *testing.B) {
147 ifi := loopbackInterface()
148 if ifi == nil {
149 b.Skip("loopback interface not found")
150 }
151 for i := 0; i < b.N; i++ {
152 if _, err := InterfaceByIndex(ifi.Index); err != nil {
153 b.Fatalf("InterfaceByIndex failed: %v", err)
154 }
155 }
156 }
157
158 func BenchmarkInterfaceByName(b *testing.B) {
159 ifi := loopbackInterface()
160 if ifi == nil {
161 b.Skip("loopback interface not found")
162 }
163 for i := 0; i < b.N; i++ {
164 if _, err := InterfaceByName(ifi.Name); err != nil {
165 b.Fatalf("InterfaceByName failed: %v", err)
166 }
167 }
168 }
169
170 func BenchmarkInterfaceAddrs(b *testing.B) {
171 for i := 0; i < b.N; i++ {
172 if _, err := InterfaceAddrs(); err != nil {
173 b.Fatalf("InterfaceAddrs failed: %v", err)
174 }
175 }
176 }
177
178 func BenchmarkInterfacesAndAddrs(b *testing.B) {
179 ifi := loopbackInterface()
180 if ifi == nil {
181 b.Skip("loopback interface not found")
182 }
183 for i := 0; i < b.N; i++ {
184 if _, err := ifi.Addrs(); err != nil {
185 b.Fatalf("Interface.Addrs failed: %v", err)
186 }
187 }
188 }
189
190 func BenchmarkInterfacesAndMulticastAddrs(b *testing.B) {
191 ifi := loopbackInterface()
192 if ifi == nil {
193 b.Skip("loopback interface not found")
194 }
195 for i := 0; i < b.N; i++ {
196 if _, err := ifi.MulticastAddrs(); err != nil {
197 b.Fatalf("Interface.MulticastAddrs failed: %v", err)
198 }
199 }
200 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b