summaryrefslogtreecommitdiff
path: root/libgo/go/net/interface_test.go
blob: 5d183c56384dfc91e2f8ed2d1fb19c68d6135e5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build !js

package net

import (
	"fmt"
	"reflect"
	"runtime"
	"testing"
)

// loopbackInterface returns an available logical network interface
// for loopback tests. It returns nil if no suitable interface is
// found.
func loopbackInterface() *Interface {
	ift, err := Interfaces()
	if err != nil {
		return nil
	}
	for _, ifi := range ift {
		if ifi.Flags&FlagLoopback != 0 && ifi.Flags&FlagUp != 0 {
			return &ifi
		}
	}
	return nil
}

// ipv6LinkLocalUnicastAddr returns an IPv6 link-local unicast address
// on the given network interface for tests. It returns "" if no
// suitable address is found.
func ipv6LinkLocalUnicastAddr(ifi *Interface) string {
	if ifi == nil {
		return ""
	}
	ifat, err := ifi.Addrs()
	if err != nil {
		return ""
	}
	for _, ifa := range ifat {
		if ifa, ok := ifa.(*IPNet); ok {
			if ifa.IP.To4() == nil && ifa.IP.IsLinkLocalUnicast() {
				return ifa.IP.String()
			}
		}
	}
	return ""
}

func TestInterfaces(t *testing.T) {
	ift, err := Interfaces()
	if err != nil {
		t.Fatal(err)
	}
	for _, ifi := range ift {
		ifxi, err := InterfaceByIndex(ifi.Index)
		if err != nil {
			t.Fatal(err)
		}
		switch runtime.GOOS {
		case "solaris":
			if ifxi.Index != ifi.Index {
				t.Errorf("got %v; want %v", ifxi, ifi)
			}
		default:
			if !reflect.DeepEqual(ifxi, &ifi) {
				t.Errorf("got %v; want %v", ifxi, ifi)
			}
		}
		ifxn, err := InterfaceByName(ifi.Name)
		if err != nil {
			t.Fatal(err)
		}
		if !reflect.DeepEqual(ifxn, &ifi) {
			t.Errorf("got %v; want %v", ifxn, ifi)
		}
		t.Logf("%s: flags=%v index=%d mtu=%d hwaddr=%v", ifi.Name, ifi.Flags, ifi.Index, ifi.MTU, ifi.HardwareAddr)
	}
}

func TestInterfaceAddrs(t *testing.T) {
	ift, err := Interfaces()
	if err != nil {
		t.Fatal(err)
	}
	ifStats := interfaceStats(ift)
	ifat, err := InterfaceAddrs()
	if err != nil {
		t.Fatal(err)
	}
	uniStats, err := validateInterfaceUnicastAddrs(ifat)
	if err != nil {
		t.Fatal(err)
	}
	if err := checkUnicastStats(ifStats, uniStats); err != nil {
		t.Fatal(err)
	}
}

func TestInterfaceUnicastAddrs(t *testing.T) {
	ift, err := Interfaces()
	if err != nil {
		t.Fatal(err)
	}
	ifStats := interfaceStats(ift)
	if err != nil {
		t.Fatal(err)
	}
	var uniStats routeStats
	for _, ifi := range ift {
		ifat, err := ifi.Addrs()
		if err != nil {
			t.Fatal(ifi, err)
		}
		stats, err := validateInterfaceUnicastAddrs(ifat)
		if err != nil {
			t.Fatal(ifi, err)
		}
		uniStats.ipv4 += stats.ipv4
		uniStats.ipv6 += stats.ipv6
	}
	if err := checkUnicastStats(ifStats, &uniStats); err != nil {
		t.Fatal(err)
	}
}

func TestInterfaceMulticastAddrs(t *testing.T) {
	ift, err := Interfaces()
	if err != nil {
		t.Fatal(err)
	}
	ifStats := interfaceStats(ift)
	ifat, err := InterfaceAddrs()
	if err != nil {
		t.Fatal(err)
	}
	uniStats, err := validateInterfaceUnicastAddrs(ifat)
	if err != nil {
		t.Fatal(err)
	}
	var multiStats routeStats
	for _, ifi := range ift {
		ifmat, err := ifi.MulticastAddrs()
		if err != nil {
			t.Fatal(ifi, err)
		}
		stats, err := validateInterfaceMulticastAddrs(ifmat)
		if err != nil {
			t.Fatal(ifi, err)
		}
		multiStats.ipv4 += stats.ipv4
		multiStats.ipv6 += stats.ipv6
	}
	if err := checkMulticastStats(ifStats, uniStats, &multiStats); err != nil {
		t.Fatal(err)
	}
}

type ifStats struct {
	loop  int // # of active loopback interfaces
	other int // # of active other interfaces
}

func interfaceStats(ift []Interface) *ifStats {
	var stats ifStats
	for _, ifi := range ift {
		if ifi.Flags&FlagUp != 0 {
			if ifi.Flags&FlagLoopback != 0 {
				stats.loop++
			} else {
				stats.other++
			}
		}
	}
	return &stats
}

type routeStats struct {
	ipv4, ipv6 int // # of active connected unicast, anycast or multicast routes
}

func validateInterfaceUnicastAddrs(ifat []Addr) (*routeStats, error) {
	// Note: BSD variants allow assigning any IPv4/IPv6 address
	// prefix to IP interface. For example,
	//   - 0.0.0.0/0 through 255.255.255.255/32
	//   - ::/0 through ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128
	// In other words, there is no tightly-coupled combination of
	// interface address prefixes and connected routes.
	stats := new(routeStats)
	for _, ifa := range ifat {
		switch ifa := ifa.(type) {
		case *IPNet:
			if ifa == nil || ifa.IP == nil || ifa.IP.IsMulticast() || ifa.Mask == nil {
				return nil, fmt.Errorf("unexpected value: %#v", ifa)
			}
			if len(ifa.IP) != IPv6len {
				return nil, fmt.Errorf("should be internal representation either IPv6 or IPv4-mapped IPv6 address: %#v", ifa)
			}
			prefixLen, maxPrefixLen := ifa.Mask.Size()
			if ifa.IP.To4() != nil {
				if 0 >= prefixLen || prefixLen > 8*IPv4len || maxPrefixLen != 8*IPv4len {
					return nil, fmt.Errorf("unexpected prefix length: %d/%d for %#v", prefixLen, maxPrefixLen, ifa)
				}
				if ifa.IP.IsLoopback() && prefixLen < 8 { // see RFC 1122
					return nil, fmt.Errorf("unexpected prefix length: %d/%d for %#v", prefixLen, maxPrefixLen, ifa)
				}
				stats.ipv4++
			}
			if ifa.IP.To16() != nil && ifa.IP.To4() == nil {
				if 0 >= prefixLen || prefixLen > 8*IPv6len || maxPrefixLen != 8*IPv6len {
					return nil, fmt.Errorf("unexpected prefix length: %d/%d for %#v", prefixLen, maxPrefixLen, ifa)
				}
				if ifa.IP.IsLoopback() && prefixLen != 8*IPv6len { // see RFC 4291
					return nil, fmt.Errorf("unexpected prefix length: %d/%d for %#v", prefixLen, maxPrefixLen, ifa)
				}
				stats.ipv6++
			}
		case *IPAddr:
			if ifa == nil || ifa.IP == nil || ifa.IP.IsMulticast() {
				return nil, fmt.Errorf("unexpected value: %#v", ifa)
			}
			if len(ifa.IP) != IPv6len {
				return nil, fmt.Errorf("should be internal representation either IPv6 or IPv4-mapped IPv6 address: %#v", ifa)
			}
			if ifa.IP.To4() != nil {
				stats.ipv4++
			}
			if ifa.IP.To16() != nil && ifa.IP.To4() == nil {
				stats.ipv6++
			}
		default:
			return nil, fmt.Errorf("unexpected type: %T", ifa)
		}
	}
	return stats, nil
}

func validateInterfaceMulticastAddrs(ifat []Addr) (*routeStats, error) {
	stats := new(routeStats)
	for _, ifa := range ifat {
		switch ifa := ifa.(type) {
		case *IPAddr:
			if ifa == nil || ifa.IP == nil || ifa.IP.IsUnspecified() || !ifa.IP.IsMulticast() {
				return nil, fmt.Errorf("unexpected value: %#v", ifa)
			}
			if len(ifa.IP) != IPv6len {
				return nil, fmt.Errorf("should be internal representation either IPv6 or IPv4-mapped IPv6 address: %#v", ifa)
			}
			if ifa.IP.To4() != nil {
				stats.ipv4++
			}
			if ifa.IP.To16() != nil && ifa.IP.To4() == nil {
				stats.ipv6++
			}
		default:
			return nil, fmt.Errorf("unexpected type: %T", ifa)
		}
	}
	return stats, nil
}

func checkUnicastStats(ifStats *ifStats, uniStats *routeStats) error {
	// Test the existence of connected unicast routes for IPv4.
	if supportsIPv4() && ifStats.loop+ifStats.other > 0 && uniStats.ipv4 == 0 {
		return fmt.Errorf("num IPv4 unicast routes = 0; want >0; summary: %+v, %+v", ifStats, uniStats)
	}
	// Test the existence of connected unicast routes for IPv6.
	// We can assume the existence of ::1/128 when at least one
	// loopback interface is installed.
	if supportsIPv6() && ifStats.loop > 0 && uniStats.ipv6 == 0 {
		return fmt.Errorf("num IPv6 unicast routes = 0; want >0; summary: %+v, %+v", ifStats, uniStats)
	}
	return nil
}

func checkMulticastStats(ifStats *ifStats, uniStats, multiStats *routeStats) error {
	switch runtime.GOOS {
	case "dragonfly", "nacl", "netbsd", "openbsd", "plan9", "solaris":
	default:
		// Test the existence of connected multicast route
		// clones for IPv4. Unlike IPv6, IPv4 multicast
		// capability is not a mandatory feature, and so IPv4
		// multicast validation is ignored and we only check
		// IPv6 below.
		//
		// Test the existence of connected multicast route
		// clones for IPv6. Some platform never uses loopback
		// interface as the nexthop for multicast routing.
		// We can assume the existence of connected multicast
		// route clones when at least two connected unicast
		// routes, ::1/128 and other, are installed.
		if supportsIPv6() && ifStats.loop > 0 && uniStats.ipv6 > 1 && multiStats.ipv6 == 0 {
			return fmt.Errorf("num IPv6 multicast route clones = 0; want >0; summary: %+v, %+v, %+v", ifStats, uniStats, multiStats)
		}
	}
	return nil
}

func BenchmarkInterfaces(b *testing.B) {
	testHookUninstaller.Do(uninstallTestHooks)

	for i := 0; i < b.N; i++ {
		if _, err := Interfaces(); err != nil {
			b.Fatal(err)
		}
	}
}

func BenchmarkInterfaceByIndex(b *testing.B) {
	testHookUninstaller.Do(uninstallTestHooks)

	ifi := loopbackInterface()
	if ifi == nil {
		b.Skip("loopback interface not found")
	}
	for i := 0; i < b.N; i++ {
		if _, err := InterfaceByIndex(ifi.Index); err != nil {
			b.Fatal(err)
		}
	}
}

func BenchmarkInterfaceByName(b *testing.B) {
	testHookUninstaller.Do(uninstallTestHooks)

	ifi := loopbackInterface()
	if ifi == nil {
		b.Skip("loopback interface not found")
	}
	for i := 0; i < b.N; i++ {
		if _, err := InterfaceByName(ifi.Name); err != nil {
			b.Fatal(err)
		}
	}
}

func BenchmarkInterfaceAddrs(b *testing.B) {
	testHookUninstaller.Do(uninstallTestHooks)

	for i := 0; i < b.N; i++ {
		if _, err := InterfaceAddrs(); err != nil {
			b.Fatal(err)
		}
	}
}

func BenchmarkInterfacesAndAddrs(b *testing.B) {
	testHookUninstaller.Do(uninstallTestHooks)

	ifi := loopbackInterface()
	if ifi == nil {
		b.Skip("loopback interface not found")
	}
	for i := 0; i < b.N; i++ {
		if _, err := ifi.Addrs(); err != nil {
			b.Fatal(err)
		}
	}
}

func BenchmarkInterfacesAndMulticastAddrs(b *testing.B) {
	testHookUninstaller.Do(uninstallTestHooks)

	ifi := loopbackInterface()
	if ifi == nil {
		b.Skip("loopback interface not found")
	}
	for i := 0; i < b.N; i++ {
		if _, err := ifi.MulticastAddrs(); err != nil {
			b.Fatal(err)
		}
	}
}