-
Notifications
You must be signed in to change notification settings - Fork 53
/
sockaddrs_test.go
352 lines (324 loc) · 8.36 KB
/
sockaddrs_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
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
package sockaddr_test
import (
crand "crypto/rand"
"fmt"
"math"
"math/big"
"math/rand"
"testing"
"time"
sockaddr "github.com/hashicorp/go-sockaddr"
)
func init() {
seedMathRand()
}
// seedMathRand provides weak, but guaranteed seeding, which is better than
// running with Go's default seed of 1.
func seedMathRand() {
n, err := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
if err != nil {
rand.Seed(time.Now().UTC().UnixNano())
return
}
rand.Seed(n.Int64())
}
// NOTE: A number of these code paths are exercised in template/ and
// cmd/sockaddr/
// sockAddrStringInputs allows for easy test creation by developers.
// Parallel arrays of string inputs are converted to their SockAddr
// equivalents for use by unit tests.
type sockAddrStringInputs []struct {
inputAddrs []string
sortedAddrs []string
sortedTypes []sockaddr.SockAddrType
sortFuncs []sockaddr.CmpAddrFunc
numIPv4Inputs int
numIPv6Inputs int
numUnixInputs int
}
func convertToSockAddrs(t *testing.T, inputs []string) sockaddr.SockAddrs {
sockAddrs := make(sockaddr.SockAddrs, 0, len(inputs))
for i, input := range inputs {
sa, err := sockaddr.NewSockAddr(input)
if err != nil {
t.Fatalf("[%d] Invalid SockAddr input for %+q: %v", i, input, err)
}
sockAddrs = append(sockAddrs, sa)
}
return sockAddrs
}
// shuffleStrings randomly shuffles the list of strings
func shuffleStrings(list []string) {
for i := range list {
j := rand.Intn(i + 1)
list[i], list[j] = list[j], list[i]
}
}
func TestSockAddr_SockAddrs_AscAddress(t *testing.T) {
testInputs := sockAddrStringInputs{
{ // testNum: 0
sortFuncs: []sockaddr.CmpAddrFunc{
sockaddr.AscAddress,
},
numIPv4Inputs: 9,
numIPv6Inputs: 1,
numUnixInputs: 0,
inputAddrs: []string{
"10.0.0.0/8",
"172.16.1.3/12",
"128.95.120.2:53",
"128.95.120.2/32",
"192.168.0.0/16",
"128.95.120.1/32",
"192.168.1.10/24",
"128.95.120.2:8600",
"240.0.0.1/4",
"::",
},
sortedAddrs: []string{
"10.0.0.0/8",
"128.95.120.1/32",
"128.95.120.2:53",
"128.95.120.2/32",
"128.95.120.2:8600",
"172.16.1.3/12",
"192.168.0.0/16",
"192.168.1.10/24",
"240.0.0.1/4",
"::",
},
},
}
for idx, test := range testInputs {
t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) {
shuffleStrings(test.inputAddrs)
inputSockAddrs := convertToSockAddrs(t, test.inputAddrs)
sas := convertToSockAddrs(t, test.sortedAddrs)
sortedIPv4Addrs, nonIPv4Addrs := sas.FilterByType(sockaddr.TypeIPv4)
if l := len(sortedIPv4Addrs); l != test.numIPv4Inputs {
t.Fatalf("[%d] Missing IPv4Addrs: expected %d, received %d", idx, test.numIPv4Inputs, l)
}
if len(nonIPv4Addrs) != test.numIPv6Inputs+test.numUnixInputs {
t.Fatalf("[%d] Non-IPv4 Address in input", idx)
}
// Copy inputAddrs so we can manipulate it. wtb const.
sockAddrs := append(sockaddr.SockAddrs(nil), inputSockAddrs...)
filteredAddrs, _ := sockAddrs.FilterByType(sockaddr.TypeIPv4)
sockaddr.OrderedAddrBy(test.sortFuncs...).Sort(filteredAddrs)
ipv4SockAddrs, nonIPv4s := filteredAddrs.FilterByType(sockaddr.TypeIPv4)
if len(nonIPv4s) != 0 {
t.Fatalf("[%d] bad", idx)
}
for i, ipv4SockAddr := range ipv4SockAddrs {
ipv4Addr := sockaddr.ToIPv4Addr(ipv4SockAddr)
sortedIPv4Addr := sockaddr.ToIPv4Addr(sortedIPv4Addrs[i])
if ipv4Addr.Address != sortedIPv4Addr.Address {
t.Errorf("[%d/%d] Sort equality failed: expected %s, received %s", idx, i, sortedIPv4Addrs[i], ipv4Addr)
}
}
})
}
}
func TestSockAddr_SockAddrs_AscPrivate(t *testing.T) {
testInputs := []struct {
sortFuncs []sockaddr.CmpAddrFunc
inputAddrs []string
sortedAddrs []string
}{
{ // testNum: 0
sortFuncs: []sockaddr.CmpAddrFunc{
sockaddr.AscType,
sockaddr.AscPrivate,
sockaddr.AscAddress,
sockaddr.AscType,
sockaddr.AscAddress,
sockaddr.AscPort,
},
inputAddrs: []string{
"10.0.0.0/8",
"172.16.1.3/12",
"192.168.0.0/16",
"192.168.0.0/16",
"192.168.1.10/24",
"128.95.120.1/32",
"128.95.120.2/32",
"128.95.120.2:53",
"128.95.120.2:8600",
"240.0.0.1/4",
"::",
},
sortedAddrs: []string{
"10.0.0.0/8",
"172.16.1.3/12",
"192.168.0.0/16",
"192.168.0.0/16",
"192.168.1.10/24",
"240.0.0.1/4",
"128.95.120.1/32",
"128.95.120.2/32",
// "128.95.120.2:53",
// "128.95.120.2:8600",
// "::",
},
},
{
sortFuncs: []sockaddr.CmpAddrFunc{
sockaddr.AscType,
sockaddr.AscPrivate,
sockaddr.AscAddress,
},
inputAddrs: []string{
"1.2.3.4:53",
"192.168.1.2",
"/tmp/foo",
"[cc::1]:8600",
"[::1]:53",
},
sortedAddrs: []string{
"/tmp/foo",
"192.168.1.2",
"1.2.3.4:53",
"[::1]:53",
"[cc::1]:8600",
},
},
{
sortFuncs: []sockaddr.CmpAddrFunc{
sockaddr.AscType,
sockaddr.AscPrivate,
sockaddr.AscAddress,
},
inputAddrs: []string{
"/tmp/foo",
"/tmp/bar",
"1.2.3.4",
},
sortedAddrs: []string{
"/tmp/bar",
"/tmp/foo",
"1.2.3.4",
},
},
}
for idx, test := range testInputs {
t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) {
sortedAddrs := convertToSockAddrs(t, test.sortedAddrs)
inputAddrs := append([]string(nil), test.inputAddrs...)
shuffleStrings(inputAddrs)
inputSockAddrs := convertToSockAddrs(t, inputAddrs)
sockaddr.OrderedAddrBy(test.sortFuncs...).Sort(inputSockAddrs)
for i, sockAddr := range sortedAddrs {
if !sockAddr.Equal(inputSockAddrs[i]) {
t.Logf("Input Addrs:\t%+v", inputAddrs)
t.Logf("Sorted Addrs:\t%+v", inputSockAddrs)
t.Logf("Expected Addrs:\t%+v", test.sortedAddrs)
t.Fatalf("[%d/%d] Sort AscType/AscAddress failed: expected %+q, received %+q", idx, i, sockAddr, inputSockAddrs[i])
}
}
})
}
}
func TestSockAddr_SockAddrs_AscPort(t *testing.T) {
testInputs := []struct {
name string
sortFuncs []sockaddr.CmpAddrFunc
inputAddrs []string
sortedAddrs []string
}{
{
name: "simple port test",
sortFuncs: []sockaddr.CmpAddrFunc{
sockaddr.AscPort,
sockaddr.AscType,
},
inputAddrs: []string{
"1.2.3.4:53",
"/tmp/foo",
"[::1]:53",
},
sortedAddrs: []string{
"/tmp/foo",
"1.2.3.4:53",
"[::1]:53",
},
},
{
name: "simple port test",
sortFuncs: []sockaddr.CmpAddrFunc{
sockaddr.AscPort,
sockaddr.AscType,
},
inputAddrs: []string{
"1.2.3.4:53",
"/tmp/foo",
},
sortedAddrs: []string{
"/tmp/foo",
"1.2.3.4:53",
},
},
}
for idx, test := range testInputs {
t.Run(test.name, func(t *testing.T) {
sortedAddrs := convertToSockAddrs(t, test.sortedAddrs)
inputAddrs := append([]string(nil), test.inputAddrs...)
shuffleStrings(inputAddrs)
inputSockAddrs := convertToSockAddrs(t, inputAddrs)
sockaddr.OrderedAddrBy(test.sortFuncs...).Sort(inputSockAddrs)
for i, sockAddr := range sortedAddrs {
if !sockAddr.Equal(inputSockAddrs[i]) {
t.Logf("Input Addrs:\t%+v", inputAddrs)
t.Logf("Sorted Addrs:\t%+v", inputSockAddrs)
t.Logf("Expected Addrs:\t%+v", test.sortedAddrs)
t.Fatalf("[%d/%d] Sort AscType/AscAddress failed: expected %+q, received %+q", idx, i, sockAddr, inputSockAddrs[i])
}
}
})
}
}
func TestSockAddr_SockAddrs_AscType(t *testing.T) {
testInputs := sockAddrStringInputs{
{ // testNum: 0
sortFuncs: []sockaddr.CmpAddrFunc{
sockaddr.AscType,
},
inputAddrs: []string{
"10.0.0.0/8",
"172.16.1.3/12",
"128.95.120.2:53",
"::",
"128.95.120.2/32",
"192.168.0.0/16",
"128.95.120.1/32",
"192.168.1.10/24",
"128.95.120.2:8600",
"240.0.0.1/4",
},
sortedTypes: []sockaddr.SockAddrType{
sockaddr.TypeIPv4,
sockaddr.TypeIPv4,
sockaddr.TypeIPv4,
sockaddr.TypeIPv4,
sockaddr.TypeIPv4,
sockaddr.TypeIPv4,
sockaddr.TypeIPv4,
sockaddr.TypeIPv4,
sockaddr.TypeIPv4,
sockaddr.TypeIPv6,
},
},
}
for idx, test := range testInputs {
t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) {
shuffleStrings(test.inputAddrs)
inputSockAddrs := convertToSockAddrs(t, test.inputAddrs)
sortedAddrs := convertToSockAddrs(t, test.sortedAddrs)
sockaddr.OrderedAddrBy(test.sortFuncs...).Sort(inputSockAddrs)
for i, sockAddr := range sortedAddrs {
if sockAddr.Type() != sortedAddrs[i].Type() {
t.Errorf("[%d/%d] Sort AscType failed: expected %+q, received %+q", idx, i, sortedAddrs[i], sockAddr)
}
}
})
}
}