This repository was archived by the owner on Jun 9, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +78
-4
lines changed Expand file tree Collapse file tree 2 files changed +78
-4
lines changed Original file line number Diff line number Diff line change @@ -6,13 +6,16 @@ func FilterInvalidPrefixLen(roalist []prefixfile.ROAJson) []prefixfile.ROAJson {
6
6
validROAs := make ([]prefixfile.ROAJson , 0 )
7
7
for _ , roa := range roalist {
8
8
prefix := roa .GetPrefix ()
9
- ones , _ := prefix .Mask .Size ()
10
- if prefix .IP .To4 () != nil && ones <= 24 {
11
- validROAs = append (validROAs , roa )
9
+ prefixLen , _ := prefix .Mask .Size ()
10
+ if prefix .IP .To4 () != nil {
11
+ if prefixLen <= 24 {
12
+ validROAs = append (validROAs , roa )
13
+ }
14
+
12
15
continue
13
16
}
14
17
15
- if prefix . IP . To16 () != nil && ones <= 48 {
18
+ if prefixLen <= 48 {
16
19
validROAs = append (validROAs , roa )
17
20
}
18
21
}
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "testing"
5
+
6
+ "github.com/cloudflare/gortr/prefixfile"
7
+ "github.com/stretchr/testify/assert"
8
+ )
9
+
10
+ func TestFilter (t * testing.T ) {
11
+ tests := []struct {
12
+ name string
13
+ input []prefixfile.ROAJson
14
+ expected []prefixfile.ROAJson
15
+ }{
16
+ {
17
+ name : "Invalid IPv4 prefix" ,
18
+ input : []prefixfile.ROAJson {
19
+ {
20
+ Prefix : "1.1.1.0/25" ,
21
+ ASN : 13335 ,
22
+ Length : 32 ,
23
+ },
24
+ },
25
+ expected : []prefixfile.ROAJson {},
26
+ },
27
+ {
28
+ name : "Invalid IPv6 prefix" ,
29
+ input : []prefixfile.ROAJson {
30
+ {
31
+ Prefix : "2001:db8::/64" ,
32
+ ASN : 13335 ,
33
+ Length : 128 ,
34
+ },
35
+ },
36
+ expected : []prefixfile.ROAJson {},
37
+ },
38
+ {
39
+ name : "All valid" ,
40
+ input : []prefixfile.ROAJson {
41
+ {
42
+ Prefix : "2001:db8::/48" ,
43
+ ASN : 13335 ,
44
+ Length : 48 ,
45
+ },
46
+ {
47
+ Prefix : "1.1.1.0/24" ,
48
+ ASN : 13335 ,
49
+ Length : 32 ,
50
+ },
51
+ },
52
+ expected : []prefixfile.ROAJson {
53
+ {
54
+ Prefix : "2001:db8::/48" ,
55
+ ASN : 13335 ,
56
+ Length : 48 ,
57
+ },
58
+ {
59
+ Prefix : "1.1.1.0/24" ,
60
+ ASN : 13335 ,
61
+ Length : 32 ,
62
+ },
63
+ },
64
+ },
65
+ }
66
+
67
+ for _ , test := range tests {
68
+ res := FilterInvalidPrefixLen (test .input )
69
+ assert .Equal (t , test .expected , res , test .name )
70
+ }
71
+ }
You can’t perform that action at this time.
0 commit comments