-
Notifications
You must be signed in to change notification settings - Fork 21
/
netroute_test.go
74 lines (67 loc) · 1.5 KB
/
netroute_test.go
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
package netroute
import (
"net"
"strings"
"testing"
)
func TestRoute(t *testing.T) {
r, err := New()
if err != nil {
t.Fatal(err)
}
ifs, err := net.Interfaces()
if err != nil || len(ifs) == 0 {
t.Skip("Can't test routing without access to system interfaces")
}
var localAddr net.IP
var hasV6 bool
addrs, err := ifs[0].Addrs()
if err != nil {
t.Fatal(err)
}
for _, addr := range addrs {
if strings.HasPrefix(addr.Network(), "ip") {
localAddr, _, _ = net.ParseCIDR(addr.String())
break
}
}
for _, addr := range addrs {
if strings.HasPrefix(addr.Network(), "ip") {
_, ipn, _ := net.ParseCIDR(addr.String())
if ipn.IP.To4() == nil &&
!ipn.IP.IsLoopback() &&
!ipn.IP.IsInterfaceLocalMulticast() &&
!ipn.IP.IsLinkLocalUnicast() &&
!ipn.IP.IsLinkLocalMulticast() {
hasV6 = true
break
}
}
}
_, gw, src, err := r.Route(localAddr)
if err != nil {
t.Fatal(err)
}
if gw != nil || !src.Equal(localAddr) {
t.Fatalf("Did not expect gateway for %v->%v: %v", src, localAddr, gw)
}
// Route to somewhere external should.
_, gw, _, err = r.Route(net.IPv4(8, 8, 8, 8))
if err != nil {
t.Fatal(err)
}
if gw == nil {
t.Fatalf("Did not expect direct link to 8.8.8.8. Are you Google?")
}
// Route to v4 and v6 should differ.
if !hasV6 {
return
}
_, v6gw, _, err := r.Route(net.ParseIP("2607:f8b0:400a:809::200e")) // at one point google.
if err != nil {
t.Fatal(err)
}
if v6gw.Equal(gw) {
t.Fatalf("did not expect a v4 gw for a v6 route.")
}
}