Repository navigation
Expand file tree
/
Copy pathcymru_test.go
More file actions
486 lines (470 loc) · 15.3 KB
/
Copy pathcymru_test.go
File metadata and controls
486 lines (470 loc) · 15.3 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* ZAnnotate Copyright 2026 Regents of the University of Michigan
*
* Licensed under the Apache License, Version 2.0 (the License); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package zannotate
import (
"cmp"
"context"
"net"
"reflect"
"slices"
"strings"
"testing"
)
func TestConvertIPToDNSFormat(t *testing.T) {
tests := []struct {
name string
ip net.IP
expected string
}{
{
name: "ipv4 simple",
ip: net.ParseIP("1.1.1.1"),
expected: "1.1.1.1",
},
{
name: "ipv4 asymmetric",
ip: net.ParseIP("171.67.71.209").To4(),
expected: "209.71.67.171",
},
{
name: "ipv6",
ip: net.ParseIP("2001:abcd:ef12:b002::68").To16(),
expected: "8.6.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.b.2.1.f.e.d.c.b.a.1.0.0.2",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := convertIPToDNSFormat(tt.ip)
if result != tt.expected {
t.Errorf("convertIPToDNSFormat(%v) = %q, want %q", tt.ip, result, tt.expected)
}
})
}
}
func TestPopulateOriginDetails(t *testing.T) {
tests := []struct {
name string
ip net.IP
mockResult []string
mockErr error
expectError bool
validate func(*testing.T, *CymruResult)
}{
{
name: "valid origin result",
ip: net.ParseIP("171.64.1.1"),
mockResult: []string{"32 | 171.64.0.0/14 | US | arin | 1994-08-22"},
mockErr: nil,
validate: func(t *testing.T, result *CymruResult) {
expected := &PrefixResult{
Prefix: "171.64.0.0/14",
CountryCode: "US",
Registry: "arin",
AllocationDate: "1994-08-22",
OriginASNs: []uint32{32},
}
if !reflect.DeepEqual(result.OriginASNs, expected.OriginASNs) {
t.Errorf("expected OriginASNs %v, got %v", expected.OriginASNs, result.OriginASNs)
}
if !reflect.DeepEqual(result.PrefixDetails[0], expected) {
t.Errorf("expected PrefixDetails[171.64.0.0/14] %v, got %v", expected, result.PrefixDetails[0])
}
},
},
{
name: "valid origin result, 2+ origin asns",
ip: net.ParseIP("34.114.10.22"),
mockResult: []string{"15169 19527 | 34.112.0.0/14 | US | arin | 2018-09-28"},
mockErr: nil,
validate: func(t *testing.T, result *CymruResult) {
slices.Sort(result.OriginASNs)
pd := result.PrefixDetails[0]
slices.Sort(pd.OriginASNs)
expected := &PrefixResult{
Prefix: "34.112.0.0/14",
CountryCode: "US",
Registry: "arin",
AllocationDate: "2018-09-28",
OriginASNs: []uint32{15169, 19527},
}
if !reflect.DeepEqual(result.OriginASNs, expected.OriginASNs) {
t.Errorf("expected OriginASNs %v, got %v", expected.OriginASNs, result.OriginASNs)
}
if !reflect.DeepEqual(pd, expected) {
t.Errorf("expected PrefixDetails[34.112.0.0/14] %v, got %v", expected, pd)
}
},
},
{
name: "no results from DNS",
ip: net.ParseIP("10.0.0.1"),
mockResult: []string{},
mockErr: nil,
expectError: true,
},
{
name: "too few fields",
ip: net.ParseIP("10.0.0.1"),
mockResult: []string{"32 | 171.64.0.0/14 | US"},
mockErr: nil,
expectError: true,
},
{
name: "too many fields",
ip: net.ParseIP("10.0.0.1"),
mockResult: []string{"32 | 171.64.0.0/14 | US | arin | 1994-08-22 | extra"},
mockErr: nil,
expectError: true,
},
{
name: "invalid ASN (non-numeric)",
ip: net.ParseIP("10.0.0.1"),
mockResult: []string{"invalid | 171.64.0.0/14 | US | arin | 1994-08-22"},
mockErr: nil,
expectError: true,
},
{
name: "ASN with extra whitespace",
ip: net.ParseIP("171.64.1.1"),
mockResult: []string{" 32 | 171.64.0.0/14 | US | arin | 1994-08-22 "},
mockErr: nil,
validate: func(t *testing.T, result *CymruResult) {
expectedASNs := []uint32{32}
if !reflect.DeepEqual(result.OriginASNs, expectedASNs) {
t.Errorf("expected OriginASNs %v after trimming, got %v", expectedASNs, result.OriginASNs)
}
if result.PrefixDetails[0].CountryCode != "US" {
t.Errorf("expected CountryCode US after trimming, got %s", result.PrefixDetails[0].CountryCode)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := &CymruResult{
asnLookupMap: make(map[uint32]struct{}),
prefixLookupMap: make(map[string]*PrefixResult),
}
lookupFunc := func(ctx context.Context, name string) ([]string, error) {
return tt.mockResult, tt.mockErr
}
err := result.populateOriginDetails(context.Background(), lookupFunc, tt.ip)
if (err != nil) != tt.expectError {
t.Errorf("populateOriginDetails() error = %v, expectError = %v", err, tt.expectError)
return
}
if !tt.expectError && tt.validate != nil {
tt.validate(t, result)
}
})
}
}
func TestPopulatePeerDetails(t *testing.T) {
tests := []struct {
name string
ip net.IP
mockResult []string
mockErr error
expectError bool
validate func(*testing.T, *CymruResult)
}{
{
name: "valid peer result with single ASN",
ip: net.ParseIP("171.64.1.1"),
mockResult: []string{"46749 | 171.64.0.0/14 | US | arin | 1994-08-22"},
mockErr: nil,
validate: func(t *testing.T, result *CymruResult) {
expected := &PrefixResult{
Prefix: "171.64.0.0/14",
CountryCode: "US",
Registry: "arin",
AllocationDate: "1994-08-22",
PeerASNs: []uint32{46749},
}
if !reflect.DeepEqual(result.PeerASNs, expected.PeerASNs) {
t.Errorf("expected PeerASNs %v, got %v", expected.PeerASNs, result.PeerASNs)
}
if !reflect.DeepEqual(result.PrefixDetails[0], expected) {
t.Errorf("expected PrefixDetails[171.64.0.0/14] %v, got %v", expected, result.PrefixDetails[0])
}
},
},
{
name: "valid peer result with multiple ASNs",
ip: net.ParseIP("1.1.1.1"),
mockResult: []string{"2914 6461 6939 13335 23352 | 1.1.1.0/24 | AU | apnic | 2011-08-11"},
mockErr: nil,
validate: func(t *testing.T, result *CymruResult) {
slices.Sort(result.PeerASNs)
pd := result.PrefixDetails[0]
slices.Sort(pd.PeerASNs)
expected := &PrefixResult{
Prefix: "1.1.1.0/24",
CountryCode: "AU",
Registry: "apnic",
AllocationDate: "2011-08-11",
PeerASNs: []uint32{2914, 6461, 6939, 13335, 23352},
}
if !reflect.DeepEqual(result.PeerASNs, expected.PeerASNs) {
t.Errorf("expected PeerASNs %v, got %v", expected.PeerASNs, result.PeerASNs)
}
if !reflect.DeepEqual(pd, expected) {
t.Errorf("expected PrefixDetails[1.1.1.0/24] %v, got %v", expected, pd)
}
},
},
{
name: "no results from DNS",
ip: net.ParseIP("10.0.0.1"),
mockResult: []string{},
mockErr: nil,
expectError: true,
},
{
name: "too few fields",
ip: net.ParseIP("10.0.0.1"),
mockResult: []string{"2914 6461 | 1.1.1.0/24 | AU"},
mockErr: nil,
expectError: true,
},
{
name: "invalid peer ASN (non-numeric)",
ip: net.ParseIP("10.0.0.1"),
mockResult: []string{"2914 invalid 6939 | 1.1.1.0/24 | AU | apnic | 2011-08-11"},
mockErr: nil,
expectError: true,
},
{
name: "peer ASNs with extra whitespace",
ip: net.ParseIP("1.1.1.1"),
mockResult: []string{" 2914 6461 6939 | 1.1.1.0/24 | AU | apnic | 2011-08-11 "},
mockErr: nil,
validate: func(t *testing.T, result *CymruResult) {
slices.Sort(result.PeerASNs)
expected := []uint32{2914, 6461, 6939}
if !reflect.DeepEqual(result.PeerASNs, expected) {
t.Errorf("expected PeerASNs %v after trimming, got %v", expected, result.PeerASNs)
}
},
},
{
name: "ASN value exceeds uint32",
ip: net.ParseIP("10.0.0.1"),
mockResult: []string{"9999999999999 | 1.1.1.0/24 | AU | apnic | 2011-08-11"},
mockErr: nil,
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := &CymruResult{
asnLookupMap: make(map[uint32]struct{}),
prefixLookupMap: make(map[string]*PrefixResult),
}
lookupFunc := func(ctx context.Context, name string) ([]string, error) {
return tt.mockResult, tt.mockErr
}
err := result.populatePeerDetails(context.Background(), lookupFunc, tt.ip)
if (err != nil) != tt.expectError {
t.Errorf("populatePeerDetails() error = %v, expectError = %v", err, tt.expectError)
return
}
if !tt.expectError && tt.validate != nil {
tt.validate(t, result)
}
})
}
}
func TestPopulateASNDetails(t *testing.T) {
tests := []struct {
name string
originASN uint32
mockResult []string
mockErr error
expectError bool
validate func(*testing.T, *CymruResult)
}{
{
name: "valid ASN result",
originASN: 32,
mockResult: []string{"32 | US | arin | 1984-09-24 | STANFORD - Stanford University, US"},
mockErr: nil,
validate: func(t *testing.T, result *CymruResult) {
expected := []*ASNLookup{
{ASN: 32, CountryCode: "US", Registry: "arin", ASNAllocationDate: "1984-09-24", ASNDescription: "STANFORD - Stanford University, US"},
}
if !reflect.DeepEqual(result.ASNLookup, expected) {
t.Errorf("expected ASNLookup %v, got %v", expected, result.ASNLookup)
}
},
},
{
name: "no results from DNS",
originASN: 32,
mockResult: []string{},
mockErr: nil,
expectError: true,
},
{
name: "too few fields",
originASN: 32,
mockResult: []string{"32 | US | arin"},
mockErr: nil,
expectError: true,
},
{
name: "too many fields",
originASN: 32,
mockResult: []string{"32 | US | arin | 1984-09-24 | STANFORD - Stanford University, US | extra"},
mockErr: nil,
expectError: true,
},
{
name: "ASN with extra whitespace",
originASN: 32,
mockResult: []string{" 32 | US | arin | 1984-09-24 | STANFORD - Stanford University, US "},
mockErr: nil,
validate: func(t *testing.T, result *CymruResult) {
expected := []*ASNLookup{
{ASN: 32, CountryCode: "US", Registry: "arin", ASNAllocationDate: "1984-09-24", ASNDescription: "STANFORD - Stanford University, US"},
}
if !reflect.DeepEqual(result.ASNLookup, expected) {
t.Errorf("expected ASNLookup %v after trimming, got %v", expected, result.ASNLookup)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := &CymruResult{
asnLookupMap: make(map[uint32]struct{}),
prefixLookupMap: make(map[string]*PrefixResult),
}
lookupFunc := func(ctx context.Context, name string) ([]string, error) {
return tt.mockResult, tt.mockErr
}
err := result.populateASNDetails(context.Background(), lookupFunc, tt.originASN)
if (err != nil) != tt.expectError {
t.Errorf("populateASNDetails() error = %v, expectError = %v", err, tt.expectError)
return
}
if !tt.expectError && tt.validate != nil {
tt.validate(t, result)
}
})
}
}
func normalizeCymruResult(r *CymruResult) {
if r == nil {
return
}
slices.Sort(r.OriginASNs)
slices.Sort(r.PeerASNs)
slices.SortFunc(r.ASNLookup, func(i, j *ASNLookup) int {
return cmp.Compare(i.ASN, j.ASN)
})
slices.SortFunc(r.PrefixDetails, func(i, j *PrefixResult) int {
return strings.Compare(i.Prefix, j.Prefix)
})
for _, pd := range r.PrefixDetails {
slices.Sort(pd.OriginASNs)
slices.Sort(pd.PeerASNs)
}
r.prefixLookupMap = nil
r.asnLookupMap = nil
}
func TestCymruAnnotate(t *testing.T) {
type lookupResults struct {
originResp string
peerResp string
asnResp string
}
tests := []struct {
testName string // name of the subtest
ip net.IP // input IP address
hardCodedLookupResps lookupResults
expectedResponse *CymruResult
}{
{
testName: "Positive IPv4 Test Case",
ip: net.ParseIP("1.1.1.1"),
hardCodedLookupResps: lookupResults{
originResp: "13335 | 1.1.1.0/24 | AU | apnic | 2011-08-11",
peerResp: "2914 6461 6939 13335 23352 | 1.1.1.0/24 | AU | apnic | 2011-08-11",
asnResp: "13335 | US | arin | 2010-07-14 | CLOUDFLARENET - Cloudflare, Inc., US",
},
expectedResponse: &CymruResult{
OriginASNs: []uint32{13335},
PeerASNs: []uint32{2914, 6461, 6939, 13335, 23352},
// Annotate looks up ASN details for all origin + peer ASNs; the mock
// returns the same Cloudflare record for every query, so each entry
// carries that record's metadata but with the correct ASN key/field.
ASNLookup: []*ASNLookup{
{ASN: 13335, CountryCode: "US", Registry: "arin", ASNAllocationDate: "2010-07-14", ASNDescription: "CLOUDFLARENET - Cloudflare, Inc., US"},
{ASN: 2914, CountryCode: "US", Registry: "arin", ASNAllocationDate: "2010-07-14", ASNDescription: "CLOUDFLARENET - Cloudflare, Inc., US"},
{ASN: 6461, CountryCode: "US", Registry: "arin", ASNAllocationDate: "2010-07-14", ASNDescription: "CLOUDFLARENET - Cloudflare, Inc., US"},
{ASN: 6939, CountryCode: "US", Registry: "arin", ASNAllocationDate: "2010-07-14", ASNDescription: "CLOUDFLARENET - Cloudflare, Inc., US"},
{ASN: 23352, CountryCode: "US", Registry: "arin", ASNAllocationDate: "2010-07-14", ASNDescription: "CLOUDFLARENET - Cloudflare, Inc., US"},
},
PrefixDetails: []*PrefixResult{
{
Prefix: "1.1.1.0/24",
CountryCode: "AU",
Registry: "apnic",
AllocationDate: "2011-08-11",
OriginASNs: []uint32{13335},
PeerASNs: []uint32{2914, 6461, 6939, 13335, 23352},
},
},
},
},
}
for _, test := range tests {
t.Run(test.testName, func(t *testing.T) {
lookupFunc := func(ctx context.Context, name string) ([]string, error) {
if strings.HasSuffix(name, "origin.asn.cymru.com") {
return []string{test.hardCodedLookupResps.originResp}, nil
} else if strings.HasSuffix(name, "peer.asn.cymru.com") {
return []string{test.hardCodedLookupResps.peerResp}, nil
} else if strings.HasSuffix(name, "asn.cymru.com") {
if !strings.HasPrefix(name, "AS") {
t.Fatalf("url is incorrect: %s", name)
}
return []string{test.hardCodedLookupResps.asnResp}, nil
}
t.Fatalf("url is incorrect: %s", name)
return nil, nil
}
var factory CymruAnnotatorFactory
err := factory.Initialize(nil)
if err != nil {
t.Fatalf("failed to initialize Cymru annotator factory: %v", err)
}
annotator := factory.MakeAnnotator(0)
if cymruAnnotator, ok := annotator.(*CymruAnnotator); ok {
cymruAnnotator.lookupFunc = lookupFunc
}
err = annotator.Initialize()
if err != nil {
t.Fatalf("failed to initialize Cymru annotator: %v", err)
}
actual := annotator.Annotate(test.ip).(*CymruResult)
normalizeCymruResult(actual)
normalizeCymruResult(test.expectedResponse)
if !reflect.DeepEqual(actual, test.expectedResponse) {
t.Fatalf("unexpected result: got %v want %v", actual, test.expectedResponse)
}
})
}
}