forked from zmap/zgrab2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility_test.go
More file actions
413 lines (373 loc) · 12.4 KB
/
utility_test.go
File metadata and controls
413 lines (373 loc) · 12.4 KB
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package zgrab2
import (
"net"
"reflect"
"slices"
"strings"
"testing"
)
func TestParseIPList(t *testing.T) {
tests := []struct {
name string
input string
expected []string
errExpected bool
}{
{
name: "Single IPv4 without port",
input: "8.8.8.8",
expected: []string{"8.8.8.8:53"},
},
{
name: "Single IPv4 with custom port",
input: "8.8.8.8:5353",
expected: []string{"8.8.8.8:5353"},
},
{
name: "Single IPv6 without port",
input: "2001:4860:4860::8888",
expected: []string{"[2001:4860:4860::8888]:53"},
},
{
name: "Single IPv6 with custom port",
input: "[2001:4860:4860::8888]:5353",
expected: []string{"[2001:4860:4860::8888]:5353"},
},
{
name: "Multiple IPv4s mixed ports",
input: "1.1.1.1:5300,8.8.8.8",
expected: []string{"1.1.1.1:5300", "8.8.8.8:53"},
},
{
name: "Mixed IPv4 and IPv6, some with ports",
input: "1.1.1.1:5300,[2001:4860:4860::8888]:5353,8.8.8.8,2001:4860:4860::8844",
expected: []string{
"1.1.1.1:5300",
"[2001:4860:4860::8888]:5353",
"8.8.8.8:53",
"[2001:4860:4860::8844]:53",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := parseCustomDNSString(tt.input)
if (err != nil) != tt.errExpected {
t.Errorf("parseCustomDNSString() error = %v, expected %v", err, tt.errExpected)
}
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("ParseIPList(%q) = %v; want %v", tt.input, result, tt.expected)
}
})
}
}
func TestExtractIPAddresses(t *testing.T) {
tests := []struct {
name string
input string
expected []net.IP
wantErr bool
}{
// Basic single IP
{"Single IP", "1.2.3.4", []net.IP{net.ParseIP("1.2.3.4")}, false},
// Basic 3x IP
{"3x IPs", "1.2.3.4,128.1.1.1,244.1.3.5", []net.IP{
net.ParseIP("1.2.3.4"),
net.ParseIP("128.1.1.1"),
net.ParseIP("244.1.3.5")}, false},
// Basic 3x IP w/ spaces
{"Single IP with spaces", "1.2.3.4, 1.2.3.7, 1.2.3.9", []net.IP{
net.ParseIP("1.2.3.4"),
net.ParseIP("1.2.3.7"),
net.ParseIP("1.2.3.9")}, false},
// Duplicate single IPs
{"Duplicate IPs", "1.2.3.4,1.2.3.4", []net.IP{net.ParseIP("1.2.3.4")}, false},
// IP range
{"IP Range", "1.2.3.4-1.2.3.6", []net.IP{
net.ParseIP("1.2.3.4"), net.ParseIP("1.2.3.5"), net.ParseIP("1.2.3.6"),
}, false},
// IP range with spaces
{"IP Range with spaces", "1.2.3.4 - 1.2.3.6", []net.IP{
net.ParseIP("1.2.3.4"), net.ParseIP("1.2.3.5"), net.ParseIP("1.2.3.6"),
}, false},
// Overlapping IP range
{"Overlapping Range", "1.2.3.4-1.2.3.6,1.2.3.5-1.2.3.7", []net.IP{
net.ParseIP("1.2.3.4"), net.ParseIP("1.2.3.5"), net.ParseIP("1.2.3.6"), net.ParseIP("1.2.3.7"),
}, false},
// CIDR block
{"CIDR Block /30", "192.168.1.0/30", []net.IP{
net.ParseIP("192.168.1.0"),
net.ParseIP("192.168.1.1"),
net.ParseIP("192.168.1.2"),
net.ParseIP("192.168.1.3"),
}, false},
// CIDR block with a single IP
{"CIDR and single IP", "192.168.1.0/30,192.168.1.128", []net.IP{
net.ParseIP("192.168.1.0"),
net.ParseIP("192.168.1.1"),
net.ParseIP("192.168.1.2"),
net.ParseIP("192.168.1.3"),
net.ParseIP("192.168.1.128"),
}, false},
// CIDR block overlapping with a single IP
{"CIDR and single IP", "192.168.1.0/30,192.168.1.2", []net.IP{
net.ParseIP("192.168.1.0"),
net.ParseIP("192.168.1.1"),
net.ParseIP("192.168.1.2"),
net.ParseIP("192.168.1.3"),
}, false},
{"Max IPv4 CIDR", "255.255.255.255/32", []net.IP{
net.ParseIP("255.255.255.255"),
}, false},
{"Max IPv6 CIDR", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128", []net.IP{
net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
}, false},
{"Max IPv4 Range", "255.255.255.254-255.255.255.255", []net.IP{
net.ParseIP("255.255.255.254"),
net.ParseIP("255.255.255.255"),
}, false},
{"Max IPv6 Range", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe - ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", []net.IP{
net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe"),
net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"),
}, false},
// Combination of all formats
{"Mixed formats", "10.0.0.1,10.0.0.2-10.0.0.3,10.0.0.0/30", []net.IP{
net.ParseIP("10.0.0.0"),
net.ParseIP("10.0.0.1"),
net.ParseIP("10.0.0.2"),
net.ParseIP("10.0.0.3"),
}, false},
// Combination of all formats, with spaces
{"Mixed formats with spaces", "10.0.0.1, 10.0.0.2 -10.0.0.3 , 10.0.0.0/30", []net.IP{
net.ParseIP("10.0.0.0"),
net.ParseIP("10.0.0.1"),
net.ParseIP("10.0.0.2"),
net.ParseIP("10.0.0.3"),
}, false},
// Invalid formats
{"Invalid IP", "999.999.999.999", nil, true},
{"Invalid IP in range", "1.2.3.4-258.1.2.3", nil, true},
{"Invalid CIDR - non-numeric", "10.0.0.a/24", nil, true},
{"Invalid CIDR - invalid IP", "10.0.0.256/24", nil, true},
{"Invalid CIDR - invalid mask", "10.1.2.3/33", nil, true},
{"Invalid range, starting IP is greater than ending IP", "1.2.3.4-1.2.3", nil, true},
{"Invalid CIDR", "10.0.0.256/24", nil, true},
{"Random string", "hello world", nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := extractIPAddresses(strings.Split(tt.input, ","))
if (err != nil) != tt.wantErr {
t.Errorf("extractIPAddresses() error = %v, wantErr %v", err, tt.wantErr)
return
}
sortedGot := make([]string, len(got))
for i, ip := range got {
sortedGot[i] = ip.String()
}
slices.Sort(sortedGot)
sortedExpected := make([]string, len(tt.expected))
for i, ip := range tt.expected {
sortedExpected[i] = ip.String()
}
slices.Sort(sortedExpected)
// Ensure deduplication and correct ordering
if !slices.Equal(sortedGot, sortedExpected) {
t.Errorf("extractIPAddresses() = %v, expected %v", got, tt.expected)
}
})
}
}
func TestExtractCIDR(t *testing.T) {
tests := []struct {
name string
input string
expected []net.IPNet
wantErr bool
}{
// Basic single IP
{"Single IP", "1.2.3.4", []net.IPNet{
{
IP: net.ParseIP("1.2.3.4"), Mask: net.CIDRMask(32, 32),
},
}, false},
// Basic 3x IPs (converted to CIDR blocks)
{"3x IPs", "1.2.3.4,128.1.1.1,244.1.3.5", []net.IPNet{
{
IP: net.ParseIP("1.2.3.4"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("128.1.1.1"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("244.1.3.5"), Mask: net.CIDRMask(32, 32),
},
}, false},
// Single IP with spaces (converted to CIDR blocks)
{"Single IP with spaces", "1.2.3.4, 1.2.3.7, 1.2.3.9", []net.IPNet{
{
IP: net.ParseIP("1.2.3.4"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("1.2.3.7"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("1.2.3.9"), Mask: net.CIDRMask(32, 32),
},
}, false},
// Duplicate single IPs (converted to CIDR block)
{"Duplicate IPs", "1.2.3.4,1.2.3.4", []net.IPNet{
{
IP: net.ParseIP("1.2.3.4"), Mask: net.CIDRMask(32, 32),
},
}, false},
// IP range (converted to CIDR blocks)
{"IP Range", "1.2.3.4-1.2.3.6", []net.IPNet{
{
IP: net.ParseIP("1.2.3.4"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("1.2.3.5"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("1.2.3.6"), Mask: net.CIDRMask(32, 32),
}}, false},
// IP range with spaces (converted to CIDR blocks)
{"IP Range with spaces", "1.2.3.4 - 1.2.3.6", []net.IPNet{
{
IP: net.ParseIP("1.2.3.4"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("1.2.3.5"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("1.2.3.6"), Mask: net.CIDRMask(32, 32),
},
}, false},
// Overlapping IP range (converted to CIDR blocks)
{"Overlapping Range", "1.2.3.4-1.2.3.6,1.2.3.5-1.2.3.7", []net.IPNet{
{
IP: net.ParseIP("1.2.3.4"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("1.2.3.5"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("1.2.3.6"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("1.2.3.7"), Mask: net.CIDRMask(32, 32),
},
}, false},
// CIDR block (/30)
{"CIDR Block /30", "192.168.1.0/30", []net.IPNet{
{
IP: net.ParseIP("192.168.1.0"), Mask: net.CIDRMask(30, 32),
},
}, false},
// CIDR block with a single IP (converted to CIDR block)
{"CIDR and single IP", "192.168.1.0/30,192.168.1.128", []net.IPNet{
{
IP: net.ParseIP("192.168.1.0"), Mask: net.CIDRMask(30, 32),
}, {
IP: net.ParseIP("192.168.1.128"), Mask: net.CIDRMask(32, 32),
},
}, false},
// Max IPv4 CIDR (/32)
{"Max IPv4 CIDR", "255.255.255.255/32", []net.IPNet{
{
IP: net.ParseIP("255.255.255.255"), Mask: net.CIDRMask(32, 32),
},
}, false},
// Max IPv6 CIDR (/128)
{"Max IPv6 CIDR", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128", []net.IPNet{
{
IP: net.ParseIP("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), Mask: net.CIDRMask(128, 128),
},
}, false},
// Max IPv4 Range (converted to CIDR blocks)
{"Max IPv4 Range", "255.255.255.254-255.255.255.255", []net.IPNet{
{
IP: net.ParseIP("255.255.255.254"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("255.255.255.255"), Mask: net.CIDRMask(32, 32),
},
}, false},
// Combination of all formats
{"Mixed formats", "10.0.0.1,10.0.0.2-10.0.0.3,10.0.0.0/30", []net.IPNet{
{
IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(30, 32),
}, {
IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(32, 32),
}, {
IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(32, 32),
},
}, false},
// Invalid formats
{"Invalid IP", "999.999.999.999", nil, true},
{"Invalid IP in range", "1.2.3.4-258.1.2.3", nil, true},
{"Invalid CIDR - non-numeric", "10.0.0.a/24", nil, true},
{"Invalid CIDR - invalid IP", "10.0.0.256/24", nil, true},
{"Invalid CIDR - invalid mask", "10.1.2.3/33", nil, true},
{"Invalid range, starting IP is greater than ending IP", "1.2.3.4-1.2.3", nil, true},
{"Invalid CIDR", "10.0.0.256/24", nil, true},
{"Random string", "hello world", nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := extractCIDRRanges(strings.Split(tt.input, ","))
if (err != nil) != tt.wantErr {
t.Errorf("extractCIDRRanges() error = %v, wantErr %v", err, tt.wantErr)
return
}
sortedGot := make([]string, len(got))
for i, ipNet := range got {
sortedGot[i] = ipNet.String()
}
slices.Sort(sortedGot)
sortedExpected := make([]string, len(tt.expected))
for i, ipNet := range tt.expected {
sortedExpected[i] = ipNet.String()
}
slices.Sort(sortedExpected)
// Ensure deduplication and correct ordering
if !slices.Equal(sortedGot, sortedExpected) {
t.Errorf("extractIPAddresses() = %v, expected %v", got, tt.expected)
}
})
}
}
func TestExtractPorts(t *testing.T) {
tests := []struct {
name string
input string
expected []uint16
wantErr bool
}{
// Positive Test Cases
{"Single Port", "80", []uint16{80}, false},
{"Single Port with whitespace", " 80 ", []uint16{80}, false},
{"Multiple ports", "80,443,8080", []uint16{80, 443, 8080}, false},
{"Multiple ports with whitespace", " 80, 443, 8080 ", []uint16{80, 443, 8080}, false},
{"Port range", "80-85", []uint16{80, 81, 82, 83, 84, 85}, false},
{"Port range with whitespace", "80 - 85", []uint16{80, 81, 82, 83, 84, 85}, false},
{"Single port with range", "80, 83-89", []uint16{80, 83, 84, 85, 86, 87, 88, 89}, false},
{"Duplicate port", "80, 80", []uint16{80}, false},
{"Duplicate port with port range", "80, 80-82", []uint16{80, 81, 82}, false},
// Negative Test Cases
{"Invalid port - too large", "65536", nil, true},
{"Invalid port - can't be negative", "-5", nil, true},
{"Invalid port - can't be zero", "0", nil, true},
{"Invalid port - can't be string", "port 80", nil, true},
{"Invalid range - start greater than end", "80-70", nil, true},
{"Invalid range - start can't be zero", "0-70", nil, true},
{"Invalid range - end can't be greater than a valid port", "1-65536", nil, true},
{"Invalid range - end must be integer", "1-port", nil, true},
{"Invalid range - start must be integer", "port-8080", nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := extractPorts(tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("extractPorts() error = %v, wantErr %v", err, tt.wantErr)
return
}
slices.Sort(got)
slices.Sort(tt.expected)
if !slices.Equal(got, tt.expected) {
t.Errorf("extractPorts() = %v, expected %v", got, tt.expected)
}
})
}
}