github.com/osrg/gobgp/v3@v3.30.0/internal/pkg/table/roa_test.go (about) 1 package table 2 3 import ( 4 "net" 5 "strconv" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/osrg/gobgp/v3/pkg/config/oc" 11 "github.com/osrg/gobgp/v3/pkg/packet/bgp" 12 "github.com/stretchr/testify/assert" 13 ) 14 15 func strToASParam(str string) *bgp.PathAttributeAsPath { 16 toList := func(asstr, sep string) []uint32 { 17 as := make([]uint32, 0) 18 l := strings.Split(asstr, sep) 19 for _, s := range l { 20 v, _ := strconv.ParseUint(s, 10, 32) 21 as = append(as, uint32(v)) 22 } 23 return as 24 } 25 var atype uint8 26 var as []uint32 27 if strings.HasPrefix(str, "{") { 28 atype = bgp.BGP_ASPATH_ATTR_TYPE_SET 29 as = toList(str[1:len(str)-1], ",") 30 } else if strings.HasPrefix(str, "(") { 31 atype = bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET 32 as = toList(str[1:len(str)-1], " ") 33 } else { 34 atype = bgp.BGP_ASPATH_ATTR_TYPE_SEQ 35 as = toList(str, " ") 36 } 37 38 return bgp.NewPathAttributeAsPath([]bgp.AsPathParamInterface{bgp.NewAs4PathParam(atype, as)}) 39 } 40 41 func validateOne(rt *ROATable, cidr, aspathStr string) oc.RpkiValidationResultType { 42 var nlri bgp.AddrPrefixInterface 43 ip, r, _ := net.ParseCIDR(cidr) 44 length, _ := r.Mask.Size() 45 if ip.To4() == nil { 46 nlri = bgp.NewIPv6AddrPrefix(uint8(length), ip.String()) 47 } else { 48 nlri = bgp.NewIPAddrPrefix(uint8(length), ip.String()) 49 } 50 attrs := []bgp.PathAttributeInterface{strToASParam(aspathStr)} 51 path := NewPath(&PeerInfo{LocalAS: 65500}, nlri, false, attrs, time.Now(), false) 52 ret := rt.Validate(path) 53 return ret.Status 54 } 55 56 func TestValidate0(t *testing.T) { 57 assert := assert.New(t) 58 59 table := NewROATable(logger) 60 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("192.168.0.0").To4(), 24, 32, 100, "")) 61 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("192.168.0.0").To4(), 24, 24, 200, "")) 62 63 r := validateOne(table, "192.168.0.0/24", "100") 64 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_VALID) 65 66 r = validateOne(table, "192.168.0.0/24", "100 200") 67 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_VALID) 68 69 r = validateOne(table, "192.168.0.0/24", "300") 70 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_INVALID) 71 72 r = validateOne(table, "192.168.0.0/25", "100") 73 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_VALID) 74 75 r = validateOne(table, "192.168.0.0/25", "200") 76 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_INVALID) 77 78 r = validateOne(table, "192.168.0.0/25", "300") 79 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_INVALID) 80 } 81 82 func TestValidate1(t *testing.T) { 83 assert := assert.New(t) 84 85 table := NewROATable(logger) 86 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) 87 88 r := validateOne(table, "10.0.0.0/16", "65000") 89 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_VALID) 90 91 r = validateOne(table, "10.0.0.0/16", "65001") 92 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_INVALID) 93 } 94 95 func TestValidate2(t *testing.T) { 96 assert := assert.New(t) 97 98 table := NewROATable(logger) 99 100 var r oc.RpkiValidationResultType 101 102 r = validateOne(table, "10.0.0.0/16", "65000") 103 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND) 104 105 r = validateOne(table, "10.0.0.0/16", "65001") 106 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND) 107 } 108 109 func TestValidate3(t *testing.T) { 110 assert := assert.New(t) 111 112 table := NewROATable(logger) 113 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) 114 115 r := validateOne(table, "10.0.0.0/8", "65000") 116 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND) 117 118 r = validateOne(table, "10.0.0.0/17", "65000") 119 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_INVALID) 120 121 table = NewROATable(logger) 122 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) 123 124 r = validateOne(table, "10.0.0.0/17", "65000") 125 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_VALID) 126 } 127 128 func TestValidate4(t *testing.T) { 129 assert := assert.New(t) 130 131 table := NewROATable(logger) 132 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65000, "")) 133 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 16, 65001, "")) 134 135 r := validateOne(table, "10.0.0.0/16", "65000") 136 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_VALID) 137 138 r = validateOne(table, "10.0.0.0/16", "65001") 139 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_VALID) 140 } 141 142 func TestValidate5(t *testing.T) { 143 assert := assert.New(t) 144 145 table := NewROATable(logger) 146 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 17, 17, 65000, "")) 147 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.128.0").To4(), 17, 17, 65000, "")) 148 149 r := validateOne(table, "10.0.0.0/16", "65000") 150 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND) 151 } 152 153 func TestValidate6(t *testing.T) { 154 assert := assert.New(t) 155 156 table := NewROATable(logger) 157 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 8, 32, 0, "")) 158 159 r := validateOne(table, "10.0.0.0/7", "65000") 160 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND) 161 162 r = validateOne(table, "10.0.0.0/8", "65000") 163 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_INVALID) 164 165 r = validateOne(table, "10.0.0.0/24", "65000") 166 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_INVALID) 167 } 168 169 func TestValidate7(t *testing.T) { 170 assert := assert.New(t) 171 172 table := NewROATable(logger) 173 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) 174 175 r := validateOne(table, "10.0.0.0/24", "{65000}") 176 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND) 177 178 r = validateOne(table, "10.0.0.0/24", "{65001}") 179 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND) 180 181 r = validateOne(table, "10.0.0.0/24", "{65000,65001}") 182 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND) 183 } 184 185 func TestValidate8(t *testing.T) { 186 assert := assert.New(t) 187 188 table := NewROATable(logger) 189 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 0, "")) 190 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65000, "")) 191 192 r := validateOne(table, "10.0.0.0/24", "65000") 193 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_VALID) 194 195 r = validateOne(table, "10.0.0.0/24", "65001") 196 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_INVALID) 197 } 198 199 func TestValidate9(t *testing.T) { 200 assert := assert.New(t) 201 202 table := NewROATable(logger) 203 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 24, 24, 65000, "")) 204 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65001, "")) 205 206 r := validateOne(table, "10.0.0.0/24", "65000") 207 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_VALID) 208 209 r = validateOne(table, "10.0.0.0/24", "65001") 210 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_VALID) 211 } 212 213 func TestValidate10(t *testing.T) { 214 assert := assert.New(t) 215 216 table := NewROATable(logger) 217 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 24, 24, 0, "")) 218 table.Add(NewROA(bgp.AFI_IP, net.ParseIP("10.0.0.0").To4(), 16, 24, 65001, "")) 219 220 r := validateOne(table, "10.0.0.0/24", "65000") 221 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_INVALID) 222 223 r = validateOne(table, "10.0.0.0/24", "65001") 224 assert.Equal(r, oc.RPKI_VALIDATION_RESULT_TYPE_VALID) 225 }