-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbgpstuff_test.go
370 lines (349 loc) · 7.63 KB
/
bgpstuff_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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package bgpstuff_test
import (
"fmt"
"net"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/mellowdrifter/go-bgpstuff.net"
)
func TestRoute(t *testing.T) {
t.Parallel()
tests := []struct {
ip string
want string
wantExists bool
wantErr bool
}{
{
ip: "1.1.1.1",
want: "1.1.1.0/24",
wantExists: true,
},
{
ip: "10.1.1.1",
wantErr: true,
},
{
ip: "2600::",
want: "2600::/48",
wantExists: true,
},
{
ip: "19.1.1.1",
wantExists: false,
},
{
ip: "🥺",
wantErr: true,
},
}
// TODO: Interface?
c := bgpstuff.NewBGPClient(true)
for _, tc := range tests {
t.Run(tc.ip, func(t *testing.T) {
got, err := c.GetRoute(tc.ip)
if tc.wantExists && got == nil {
t.Errorf("Prefix should exist, but exist returned false")
}
if !tc.wantExists && got != nil {
t.Errorf("Prefix should not exist, but exist returned true")
}
if tc.wantErr && err == nil {
t.Error("Expected error, but no error returned")
}
if !tc.wantErr && err != nil {
t.Errorf("No error expected, but got error: %v", err)
}
_, want, _ := net.ParseCIDR(tc.want)
if !cmp.Equal(got, want) {
t.Errorf("Got: %s, Want: %s", got, want)
}
})
}
}
func TestOrigin(t *testing.T) {
t.Parallel()
tests := []struct {
ip string
want int
wantExists bool
wantErr bool
}{
{
ip: "1.1.1.1",
want: 13335,
wantExists: true,
},
{
ip: "10.1.1.1",
wantErr: true,
},
{
ip: "2600::",
want: 1239,
wantExists: true,
},
{
ip: "19.1.1.1",
wantExists: false,
},
{
ip: "🥺",
wantErr: true,
},
}
c := bgpstuff.NewBGPClient(true)
for _, tc := range tests {
t.Run(tc.ip, func(t *testing.T) {
got, err := c.GetOrigin(tc.ip)
if tc.wantExists && got == 0 {
t.Errorf("Origin should exist, but exist returned false")
}
if !tc.wantExists && got != 0 {
t.Errorf("Origin should not exist, but exist returned true")
}
if tc.wantErr && err == nil {
t.Error("Expected error, but no error returned")
}
if !tc.wantErr && err != nil {
t.Errorf("No error expected, but got error: %v", err)
}
if got != tc.want {
t.Errorf("Got: %d, Want: %d", got, tc.want)
}
})
}
}
func TestASPath(t *testing.T) {
t.Parallel()
tests := []struct {
ip string
wantSrc int
wantExists bool
wantErr bool
}{
{
ip: "1.1.1.1",
wantSrc: 13335,
wantExists: true,
},
{
ip: "10.1.1.1",
wantErr: true,
},
{
ip: "2600::",
wantSrc: 1239,
wantExists: true,
},
{
ip: "19.1.1.1",
wantExists: false,
},
{
ip: "🥺",
wantErr: true,
},
}
c := bgpstuff.NewBGPClient(true)
for _, tc := range tests {
t.Run(tc.ip, func(t *testing.T) {
got, _, err := c.GetASPath(tc.ip)
if tc.wantExists && got == nil {
t.Errorf("AS-path should exist, but exist returned false")
}
if !tc.wantExists && got != nil {
t.Errorf("AS-path should not exist, but exist returned true")
}
if tc.wantErr && err == nil {
t.Error("Expected error, but no error returned")
}
if !tc.wantErr && err != nil {
t.Errorf("No error expected, but got error: %v", err)
}
if tc.wantExists {
if len(got) < 2 {
t.Errorf("ASPath is only %d long: %v", len(got), got)
}
if got[len(got)-1] != tc.wantSrc {
t.Errorf("Got: %d, Want: %d", got, tc.wantSrc)
}
}
})
}
}
func TestROA(t *testing.T) {
t.Parallel()
tests := []struct {
ip string
want string
wantExists bool
wantErr bool
}{
{
ip: "1.1.1.1",
want: "VALID",
wantExists: true,
},
{
ip: "10.1.1.1",
wantErr: true,
},
{
ip: "2600::",
want: "UNKNOWN",
wantExists: true,
},
{
ip: "19.1.1.1",
wantExists: false,
},
{
ip: "🥺",
wantErr: true,
},
}
c := bgpstuff.NewBGPClient(true)
for _, tc := range tests {
t.Run(tc.ip, func(t *testing.T) {
got, err := c.GetROA(tc.ip)
if tc.wantExists && got == "" {
t.Errorf("ROA should exist, but exist returned false")
}
if !tc.wantExists && got != "" {
t.Errorf("ROA should not exist, but exist returned true: %v", got)
}
if tc.wantErr && err == nil {
t.Error("Expected error, but no error returned")
}
if !tc.wantErr && err != nil {
t.Errorf("No error expected, but got error: %v", err)
}
if tc.wantExists {
if got != tc.want {
t.Errorf("Got: %s, Want: %s", got, tc.want)
}
}
})
}
}
func TestASName(t *testing.T) {
t.Parallel()
tests := []struct {
asn int
want string
wantExists bool
wantErr bool
}{
{
asn: 3356,
want: "LEVEL3",
wantExists: true,
},
{
asn: 0,
wantErr: true,
},
{
asn: 4199999999,
},
}
c := bgpstuff.NewBGPClient(true)
c.GetASNames()
for _, tc := range tests {
t.Run(fmt.Sprint(tc.asn), func(t *testing.T) {
name, err := c.GetASName(tc.asn)
if tc.wantExists && name == "" {
t.Errorf("AS name should exist, but exist returned false")
}
if !tc.wantExists && name != "" {
t.Errorf("AS name should not exist, but exist returned true")
}
if tc.wantErr && err == nil {
t.Error("Expected error, but no error returned")
}
if !tc.wantErr && err != nil {
t.Errorf("No error expected, but got error: %v", err)
}
if tc.wantExists {
if name != tc.want {
t.Errorf("Got: %s, Want: %s", name, tc.want)
}
}
})
}
}
func TestASNames(t *testing.T) {
c := bgpstuff.NewBGPClient(true)
if err := c.GetASNames(); err != nil {
t.Errorf("got error: %v", err)
}
if len(c.ASNames) < 100000 {
t.Errorf("wanted at least 100k prefixes, but got %d", len(c.ASNames))
}
if c.ASNames[3356] != "LEVEL3" {
t.Errorf("expected LEVEL3, got %s", c.ASNames[3356])
}
}
func TestInvalids(t *testing.T) {
c := bgpstuff.NewBGPClient(true)
if err := c.GetInvalids(); err != nil {
t.Errorf("got error: %v", err)
}
if len(c.Invalids) == 0 {
t.Errorf("Should have some invalids, but seeing %d invalids", len(c.Invalids))
}
if len(c.Invalids[13335]) != 3 {
t.Errorf("cloudflare advertises three invalid prefixes, but seeing %d: %v", len(c.Invalids[13335]), c.Invalids[13335])
}
}
func TestInvalid(t *testing.T) {
c := bgpstuff.NewBGPClient(true)
_, err := c.GetInvalid(13335)
if err == nil {
t.Errorf("expected error, but got none")
}
c.GetInvalids()
prefixes, err := c.GetInvalid(13335)
if err != nil {
t.Fatal(err)
}
if len(prefixes) == 0 {
t.Fatalf("wanted some invalids, but returned %d", len(prefixes))
}
if len(prefixes) != 3 {
t.Errorf("cloudflare advertises three invalid prefixes, but seeing %d: %v", len(c.Invalids[13335]), c.Invalids[13335])
}
}
func TestSourced(t *testing.T) {
c := bgpstuff.NewBGPClient(true)
prefixes, v4, v6, err := c.GetSourced(15169)
if err != nil {
t.Fatal(err)
}
if (v4 == 0) || (v6 == 0) {
t.Errorf("AS15169 should be advertising more than zero IPv4 and IPv6 addresses, but got IPv4: %d and IPv6: %d", v4, v6)
}
_, dns, _ := net.ParseCIDR("8.8.8.0/24")
if !containsSubnet(dns, prefixes) {
t.Error("Expected to see 8.8.8.0/24, but not found")
}
}
func containsSubnet(prefix *net.IPNet, prefixes []*net.IPNet) bool {
for _, v := range prefixes {
if v.String() == prefix.String() {
return true
}
}
return false
}
func TestTotals(t *testing.T) {
c := bgpstuff.NewBGPClient(true)
ipv4, ipv6, err := c.GetTotals()
if err != nil {
t.Fatal(err)
}
if ipv4 < 800000 || ipv6 < 100000 {
t.Errorf("Expecting a certain amount of prefixes, but got %d IPv4 and %d IPv6", ipv4, ipv6)
}
}