This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
host_test.go
240 lines (235 loc) · 5.31 KB
/
host_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
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
package ice
import (
"net"
"testing"
"gortc.io/ice/gather"
)
func TestProcessDualStack(t *testing.T) {
const maxCount = 100
tt := make([]struct {
V4, V6 int
}, 0, maxCount*maxCount)
// Not checking v4=0 and v6=0 because that case is invalid for
// the processDualStack function.
for v4 := 1; v4 <= maxCount; v4++ {
for v6 := 1; v6 <= maxCount; v6++ {
tt = append(tt, struct{ V4, V6 int }{V4: v4, V6: v6})
}
}
for _, tc := range tt {
var v4, v6, all []gather.Addr
for i := 0; i < tc.V4; i++ {
a := gather.Addr{
IP: make(net.IP, net.IPv4len),
}
// "marking" IP so we can count unique ip's.
bin.PutUint32(a.IP, uint32(i))
v4 = append(v4, a)
all = append(all, a)
}
for i := 0; i < tc.V6; i++ {
a := gather.Addr{
IP: make(net.IP, net.IPv6len),
}
bin.PutUint32(a.IP, uint32(i))
v6 = append(v6, a)
all = append(all, a)
}
// Checking that output length is equal to total length.
result := processDualStack(all, v4, v6)
if len(result) != len(all) {
t.Errorf("v4: %d, v6: %d: expected %d, got %d", tc.V4, tc.V6, len(all), len(result))
}
// Checking unique IP count.
gotV4 := make(map[uint32]bool)
gotV6 := make(map[uint32]bool)
for _, r := range result {
if r.IP.To4() == nil {
gotV6[bin.Uint32(r.IP)] = true
} else {
gotV4[bin.Uint32(r.IP)] = true
}
}
if len(gotV4) != len(v4) {
t.Errorf("v4: %d, v6: %d: v4 expected %d, got %d", tc.V4, tc.V6, len(v4), len(gotV4))
}
if len(gotV6) != len(v6) {
t.Errorf("v4: %d, v6: %d: v6 expected %d, got %d", tc.V4, tc.V6, len(v6), len(gotV6))
}
}
}
func TestGatherHostAddresses(t *testing.T) {
type outputRow struct {
IP string
Preference int
}
for _, tc := range []struct {
Name string
Input []string
Output []outputRow
}{
{
Name: "blank",
},
{
Name: "loopback",
Input: []string{
"127.0.0.1",
},
},
{
Name: "Single IPv4",
Input: []string{
"1.1.1.1",
},
Output: []outputRow{
{"1.1.1.1", 65535},
},
},
{
Name: "IPv4",
Input: []string{
"1.1.1.1",
"1.1.1.2",
},
Output: []outputRow{
{"1.1.1.1", 2},
{"1.1.1.2", 1},
},
},
{
Name: "Single IPv6",
Input: []string{
"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa1",
},
Output: []outputRow{
{"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa1", 65535},
},
},
{
Name: "IPv6",
Input: []string{
"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa1",
"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa2",
},
Output: []outputRow{
{"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa1", 2},
{"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa2", 1},
},
},
{
// If a host has two IPv4 addresses and six IPv6 addresses, it will
// insert an IPv4 address after four IPv6 addresses by choosing the
// appropriate local preference values when calculating the pair
// priorities.
Name: "2xIPv4 and 6xIPv6",
Input: []string{
"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa1",
"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa2",
"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa3",
"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa4",
"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa5",
"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa6",
"1.1.1.1",
"1.1.1.2",
},
Output: []outputRow{
{"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa1", 8},
{"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa2", 7},
{"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa3", 6},
{"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa4", 5},
{"1.1.1.1", 4},
{"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa5", 3},
{"2a03:e2c0:60f:52:cfe1:fdd:daf7:7fa6", 2},
{"1.1.1.2", 1},
},
},
} {
t.Run(tc.Name, func(t *testing.T) {
gatherAddrs := make([]gather.Addr, len(tc.Input))
for i, ip := range tc.Input {
gatherAddrs[i] = gather.Addr{
IP: net.ParseIP(ip),
}
}
expected := make([]HostAddr, len(tc.Output))
for i, row := range tc.Output {
expected[i] = HostAddr{
IP: net.ParseIP(row.IP),
LocalPreference: row.Preference,
}
}
gotAddr, err := HostAddresses(gatherAddrs)
if err != nil {
t.Fatal(err)
}
if len(gotAddr) != len(expected) {
t.Fatalf("bad length: %d (got) != %d (expected)",
len(gotAddr), len(expected),
)
}
for i := range gotAddr {
got := gotAddr[i]
exp := expected[i]
if got.LocalPreference != exp.LocalPreference || !got.IP.Equal(exp.IP) {
t.Errorf("[%d]: %s, %d (got) != %s, %d (expected)",
i, got.IP, got.LocalPreference, exp.IP, exp.LocalPreference,
)
}
}
})
}
}
func TestIsValidHostIP(t *testing.T) {
for _, tc := range []struct {
Name string
IP net.IP
V6 bool
Valid bool
}{
{
Name: "blank",
},
{
Name: "127.0.0.1",
IP: localIP,
},
{
Name: "v4",
IP: net.IPv4(10, 0, 0, 1),
Valid: true,
},
{
Name: "v4 for v6 only",
IP: net.IPv4(10, 0, 0, 1),
V6: true,
},
{
Name: "Site-local ipv6",
IP: net.ParseIP("FEC0::ff:aa"),
V6: true,
},
{
Name: "link-local ipv6",
IP: net.ParseIP("fe80::50da:9baa:ef96:15c8"),
},
{
Name: "ipv4-mapped",
IP: net.IPv4(10, 0, 0, 1).To16(),
Valid: true,
},
{
Name: "ipv4-mapped for v6 only",
IP: net.IPv4(10, 0, 0, 1).To16(),
V6: true,
},
} {
t.Run(tc.Name, func(t *testing.T) {
if v := IsHostIPValid(tc.IP, tc.V6); v != tc.Valid {
t.Errorf("valid(%s, v6=%v) %v (got) != %v (expected)",
tc.IP, tc.V6, v, tc.Valid,
)
}
})
}
}