-
Notifications
You must be signed in to change notification settings - Fork 4
/
resolve_test.go
168 lines (159 loc) · 4.9 KB
/
resolve_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
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
// Copyright 2014 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.
package nett
import (
"net"
"reflect"
"strings"
"testing"
"time"
)
type testAddr struct {
net, addr string
ips []net.IP
ipv4, ipv6 bool
err error
}
var testTCPAddrs, testUDPAddrs, testIPAddrs []testAddr
func init() {
ipv4 := net.IP{127, 0, 0, 1}
ipv6 := net.IPv6loopback
ips := []net.IP{ipv4, ipv6}
testTCPAddrs = []testAddr{
{"tcp", "foo.com:http", ips, true, true, nil},
{"tcp", "foo.com:80", ips, true, true, nil},
{"tcp4", "foo.com:80", ips, true, false, nil},
{"tcp4", "foo.com:80", ips, false, true, ErrNoSuitableAddress},
{"tcp6", "foo.com:80", ips, false, true, nil},
{"tcp6", "foo.com:80", ips, true, false, ErrNoSuitableAddress},
{"tcp", "127.0.0.1:80", nil, true, true, nil},
{"tcp6", "127.0.0.1:80", nil, false, true, ErrNoSuitableAddress},
{"tcp", "[::1%911]:80", nil, false, true, nil},
{"tcp", "[::1%911]:80", nil, true, false, ErrNoSuitableAddress},
}
testUDPAddrs = make([]testAddr, len(testTCPAddrs))
testIPAddrs = make([]testAddr, len(testTCPAddrs))
for i, ta := range testTCPAddrs {
ta.net = strings.Replace(ta.net, "tcp", "udp", -1)
testUDPAddrs[i] = ta
ta.net = strings.Replace(ta.net, "udp", "ip", -1)
ta.addr, _, _ = net.SplitHostPort(ta.addr) // TODO: shitty, but ok now
testIPAddrs[i] = ta
}
}
func TestResolveTCP(t *testing.T) {
defer func(fn func(string) ([]net.IP, error), ipv4, ipv6 bool) {
lookupIPs = fn
supportsIPv4 = ipv4
supportsIPv6 = ipv6
}(lookupIPs, supportsIPv4, supportsIPv6)
var ips []net.IP
lookupIPs = func(host string) ([]net.IP, error) {
clone := make([]net.IP, len(ips))
copy(clone, ips)
return clone, nil
}
for i, ta := range testTCPAddrs {
ips = ta.ips
supportsIPv4 = ta.ipv4
supportsIPv6 = ta.ipv6
addrs, err := resolveAddrList(nil, nil, ta.net, ta.addr)
if err != ta.err {
t.Errorf("test %d: expecting error: %v\ngot: error: %v\n", i, ta.err, err)
} else if err == nil && addrs.Len() == 0 {
t.Errorf("test %d: net: %s; addr: %s\nno addresses\n", i, ta.net, ta.addr)
}
}
}
func TestResolveUDP(t *testing.T) {
defer func(fn func(string) ([]net.IP, error), ipv4, ipv6 bool) {
lookupIPs = fn
supportsIPv4 = ipv4
supportsIPv6 = ipv6
}(lookupIPs, supportsIPv4, supportsIPv6)
var ips []net.IP
lookupIPs = func(host string) ([]net.IP, error) {
clone := make([]net.IP, len(ips))
copy(clone, ips)
return clone, nil
}
for _, ta := range testUDPAddrs {
ips = ta.ips
supportsIPv4 = ta.ipv4
supportsIPv6 = ta.ipv6
addrs, err := resolveAddrList(nil, nil, ta.net, ta.addr)
if err != ta.err {
t.Errorf("test: %#v\nexpecting error: %v\ngot error: %v\n", ta, ta.err, err)
} else if err == nil && addrs.Len() == 0 {
t.Errorf("test: %#v\nnet: %s; addr: %s\nno addresses\n", ta, ta.net, ta.addr)
}
}
}
func TestResolveIP(t *testing.T) {
defer func(fn func(string) ([]net.IP, error), ipv4, ipv6 bool) {
lookupIPs = fn
supportsIPv4 = ipv4
supportsIPv6 = ipv6
}(lookupIPs, supportsIPv4, supportsIPv6)
var ips []net.IP
lookupIPs = func(host string) ([]net.IP, error) {
clone := make([]net.IP, len(ips))
copy(clone, ips)
return clone, nil
}
for _, ta := range testIPAddrs {
ips = ta.ips
supportsIPv4 = ta.ipv4
supportsIPv6 = ta.ipv6
addrs, err := resolveAddrList(nil, nil, ta.net, ta.addr)
if err != ta.err {
t.Errorf("test: %#v\nexpecting error: %v\ngot error: %v\n", ta, ta.err, err)
} else if err == nil && addrs.Len() == 0 {
t.Errorf("test: %#v\nnet: %s; addr: %s\nno addresses\n", ta, ta.net, ta.addr)
}
}
}
func TestCacheResolver(t *testing.T) {
defer func(lookupFn func(string) ([]net.IP, error), timeFn func() time.Time) {
lookupIPs = lookupFn
timeNow = timeFn
}(lookupIPs, timeNow)
lookups := 0
ips := []net.IP{net.IPv6loopback}
lookupIPs = func(string) ([]net.IP, error) {
lookups++
return ips, nil
}
start := time.Now()
now := start
ttl := time.Second
timeNow = func() time.Time { return now }
resolver := &CacheResolver{TTL: ttl}
validate := func(host string, expLookups int) {
ips0, err := resolver.Resolve(host)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if lookups != expLookups {
t.Fatalf("lookups: expected %d; got %d", expLookups, lookups)
}
if !reflect.DeepEqual(ips, ips0) {
t.Fatalf("ips: expected %v; got %v", ips, ips0)
}
ips0[0] = nil
if reflect.DeepEqual(ips, ips0) {
t.Fatal("ips: expected copy; got same")
}
}
validate("foo.com", 1) // lookup foo.com
now = start.Add(ttl / 2) //
validate("bar.net", 2) // lookup bar.net
validate("foo.com", 2) // cached
now = start.Add(ttl) // expire foo.com
validate("foo.com", 3) // lookup foo.com
validate("bar.net", 3) // cached
now = start.Add(ttl + ttl/2) // expire bar.net
validate("foo.com", 3) // cached
validate("bar.net", 4) // lookup bar.net
}