paul | 1f377b0 | 2005-09-10 18:00:11 +0000 | [diff] [blame^] | 1 | #include <zebra.h> |
| 2 | |
| 3 | #include "vty.h" |
| 4 | #include "stream.h" |
| 5 | #include "privs.h" |
| 6 | |
| 7 | #include "bgpd/bgpd.h" |
| 8 | #include "bgpd/bgp_aspath.h" |
| 9 | |
| 10 | /* need these to link in libbgp */ |
| 11 | struct zebra_privs_t *bgpd_privs = NULL; |
| 12 | struct thread_master *master = NULL; |
| 13 | |
| 14 | static int failed = 0; |
| 15 | |
| 16 | /* specification for a test - what the results should be */ |
| 17 | struct test_spec |
| 18 | { |
| 19 | const char *shouldbe; /* the string the path should parse to */ |
| 20 | const char *shouldbe_delete_confed; /* ditto, but once confeds are deleted */ |
| 21 | const int hops; /* aspath_count_hops result */ |
| 22 | const int confeds; /* aspath_count_confeds */ |
| 23 | const int private_as; /* whether the private_as check should pass or fail */ |
| 24 | #define NOT_ALL_PRIVATE 0 |
| 25 | #define ALL_PRIVATE 1 |
| 26 | const as_t does_loop; /* an ASN which should trigger loop-check */ |
| 27 | const as_t doesnt_loop; /* one which should not */ |
| 28 | const as_t first; /* the first ASN, if there is one */ |
| 29 | #define NULL_ASN 0 |
| 30 | }; |
| 31 | |
| 32 | |
| 33 | /* test segments to parse and validate, and use for other tests */ |
| 34 | static struct test_segment { |
| 35 | const char *name; |
| 36 | const char *desc; |
| 37 | const u_char asdata[1024]; |
| 38 | int len; |
| 39 | struct test_spec sp; |
| 40 | } test_segments [] = |
| 41 | { |
| 42 | { /* 0 */ |
| 43 | "seq1", |
| 44 | "seq(8466,3,52737,4096)", |
| 45 | { 0x2,0x4, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00 }, |
| 46 | 10, |
| 47 | { "8466 3 52737 4096", |
| 48 | "8466 3 52737 4096", |
| 49 | 4, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 }, |
| 50 | }, |
| 51 | { /* 1 */ |
| 52 | "seq2", |
| 53 | "seq(8722) seq(4)", |
| 54 | { 0x2,0x1, 0x22,0x12, |
| 55 | 0x2,0x1, 0x00,0x04 }, |
| 56 | 8, |
| 57 | { "8722 4", |
| 58 | "8722 4", |
| 59 | 2, 0, NOT_ALL_PRIVATE, 4, 5, 8722, }, |
| 60 | }, |
| 61 | { /* 2 */ |
| 62 | "seq3", |
| 63 | "seq(8466,3,52737,4096,8722,4)", |
| 64 | { 0x2,0x6, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, |
| 65 | 0x22,0x12, 0x00,0x04}, |
| 66 | 14, |
| 67 | { "8466 3 52737 4096 8722 4", |
| 68 | "8466 3 52737 4096 8722 4", |
| 69 | 6, 0, NOT_ALL_PRIVATE, 3, 5, 8466 }, |
| 70 | }, |
| 71 | { /* 3 */ |
| 72 | "seqset", |
| 73 | "seq(8482,51457) set(5204)", |
| 74 | { 0x2,0x2, 0x21,0x22, 0xc9,0x01, |
| 75 | 0x1,0x1, 0x14,0x54 }, |
| 76 | 10, |
| 77 | { "8482 51457 {5204}", |
| 78 | "8482 51457 {5204}", |
| 79 | 3, 0, NOT_ALL_PRIVATE, 5204, 51457, 8482}, |
| 80 | }, |
| 81 | { /* 4 */ |
| 82 | "seqset2", |
| 83 | "seq(8467, 59649) set(4196,48658) set(17322,30745)", |
| 84 | { 0x2,0x2, 0x21,0x13, 0xe9,0x01, |
| 85 | 0x1,0x2, 0x10,0x64, 0xbe,0x12, |
| 86 | 0x1,0x2, 0x43,0xaa, 0x78,0x19 }, |
| 87 | 18, |
| 88 | { "8467 59649 {4196,48658} {17322,30745}", |
| 89 | "8467 59649 {4196,48658} {17322,30745}", |
| 90 | 4, 0, NOT_ALL_PRIVATE, 48658, 1, 8467}, |
| 91 | }, |
| 92 | { /* 5 */ |
| 93 | "multi", |
| 94 | "seq(6435,59408,21665) set(2457,61697,4369), seq(1842,41590,51793)", |
| 95 | { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, |
| 96 | 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11, |
| 97 | 0x2,0x3, 0x07,0x32, 0xa2,0x76, 0xca,0x51 }, |
| 98 | 24, |
| 99 | { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793", |
| 100 | "6435 59408 21665 {2457,4369,61697} 1842 41590 51793", |
| 101 | 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 }, |
| 102 | }, |
| 103 | { /* 6 */ |
| 104 | "confed", |
| 105 | "confseq(123,456,789)", |
| 106 | { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15 }, |
| 107 | 8, |
| 108 | { "(123 456 789)", |
| 109 | "", |
| 110 | 0, 3, NOT_ALL_PRIVATE, 789, 1, NULL_ASN }, |
| 111 | }, |
| 112 | { /* 7 */ |
| 113 | "confed2", |
| 114 | "confseq(123,456,789) confseq(111,222)", |
| 115 | { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15, |
| 116 | 0x3,0x2, 0x00,0x6f, 0x00,0xde }, |
| 117 | 14, |
| 118 | { "(123 456 789) (111 222)", |
| 119 | "", |
| 120 | 0, 5, NOT_ALL_PRIVATE, 111, 1, NULL_ASN }, |
| 121 | }, |
| 122 | { /* 8 */ |
| 123 | "confset", |
| 124 | "confset(456,123,789)", |
| 125 | { 0x4,0x3, 0x01,0xc8, 0x00,0x7b, 0x03,0x15 }, |
| 126 | 8, |
| 127 | { "[123,456,789]", |
| 128 | "[123,456,789]", |
| 129 | 0, 1, NOT_ALL_PRIVATE, 123, 1, NULL_ASN }, |
| 130 | }, |
| 131 | { /* 9 */ |
| 132 | "confmulti", |
| 133 | "confseq(123,456,789) confset(222,111) seq(8722) set(4196,48658)", |
| 134 | { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15, |
| 135 | 0x4,0x2, 0x00,0xde, 0x00,0x6f, |
| 136 | 0x2,0x1, 0x22,0x12, |
| 137 | 0x1,0x2, 0x10,0x64, 0xbe,0x12 }, |
| 138 | 24, |
| 139 | { "(123 456 789) [111,222] 8722 {4196,48658}", |
| 140 | "8722 {4196,48658}", |
| 141 | 2, 4, NOT_ALL_PRIVATE, 123, 1, NULL_ASN }, |
| 142 | }, |
| 143 | { /* 10 */ |
| 144 | "seq4", |
| 145 | "seq(8466,2,52737,4096,8722,4)", |
| 146 | { 0x2,0x6, 0x21,0x12, 0x00,0x02, 0xce,0x01, 0x10,0x00, |
| 147 | 0x22,0x12, 0x00,0x04}, |
| 148 | 14, |
| 149 | { "8466 2 52737 4096 8722 4", |
| 150 | "8466 2 52737 4096 8722 4", |
| 151 | 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 }, |
| 152 | }, |
| 153 | { /* 11 */ |
| 154 | "tripleseq1", |
| 155 | "seq(8466,2,52737) seq(4096,8722,4) seq(8722)", |
| 156 | { 0x2,0x3, 0x21,0x12, 0x00,0x02, 0xce,0x01, |
| 157 | 0x2,0x3, 0x10,0x00, 0x22,0x12, 0x00,0x04, |
| 158 | 0x2,0x1, 0x22,0x12}, |
| 159 | 20, |
| 160 | { "8466 2 52737 4096 8722 4 8722", |
| 161 | "8466 2 52737 4096 8722 4 8722", |
| 162 | 7, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 }, |
| 163 | }, |
| 164 | { /* 12 */ |
| 165 | "someprivate", |
| 166 | "seq(8466,64512,52737,65535)", |
| 167 | { 0x2,0x4, 0x21,0x12, 0xfc,0x00, 0xce,0x01, 0xff,0xff }, |
| 168 | 10, |
| 169 | { "8466 64512 52737 65535", |
| 170 | "8466 64512 52737 65535", |
| 171 | 4, 0, NOT_ALL_PRIVATE, 65535, 4, 8466 }, |
| 172 | }, |
| 173 | { /* 13 */ |
| 174 | "allprivate", |
| 175 | "seq(65534,64512,64513,65535)", |
| 176 | { 0x2,0x4, 0xff,0xfe, 0xfc,0x00, 0xfc,0x01, 0xff,0xff }, |
| 177 | 10, |
| 178 | { "65534 64512 64513 65535", |
| 179 | "65534 64512 64513 65535", |
| 180 | 4, 0, ALL_PRIVATE, 65534, 4, 65534 }, |
| 181 | }, |
| 182 | { /* 14 */ |
| 183 | "long", |
| 184 | "seq(8466,3,52737,4096,34285,<repeated 49 more times>)", |
| 185 | { 0x2,0xfa, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 186 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 187 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 188 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 189 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 190 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 191 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 192 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 193 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 194 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 195 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 196 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 197 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 198 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 199 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 200 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 201 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 202 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 203 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 204 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 205 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 206 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 207 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 208 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 209 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 210 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 211 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 212 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 213 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 214 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 215 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 216 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 217 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 218 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 219 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 220 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 221 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 222 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 223 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 224 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 225 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 226 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 227 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 228 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 229 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 230 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 231 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 232 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 233 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, |
| 234 | 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, }, |
| 235 | 502, |
| 236 | { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 237 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 238 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 239 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 240 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 241 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 242 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 243 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 244 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 245 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 246 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 247 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 248 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 249 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 250 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 251 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 252 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 253 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 254 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 255 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 256 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 257 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 258 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 259 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 260 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285", |
| 261 | |
| 262 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 263 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 264 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 265 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 266 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 267 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 268 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 269 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 270 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 271 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 272 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 273 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 274 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 275 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 276 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 277 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 278 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 279 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 280 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 281 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 282 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 283 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 284 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 285 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 286 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285", |
| 287 | 250, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 }, |
| 288 | }, |
| 289 | { NULL, NULL, {0}, 0, { NULL, 0, 0 } } |
| 290 | }; |
| 291 | |
| 292 | /* prepending tests */ |
| 293 | static struct tests { |
| 294 | const struct test_segment *test1; |
| 295 | const struct test_segment *test2; |
| 296 | struct test_spec sp; |
| 297 | } prepend_tests[] = |
| 298 | { |
| 299 | { &test_segments[0], &test_segments[1], |
| 300 | { "8466 3 52737 4096 8722 4", |
| 301 | "8466 3 52737 4096 8722 4", |
| 302 | 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 }, |
| 303 | }, |
| 304 | { &test_segments[1], &test_segments[3], |
| 305 | { "8722 4 8482 51457 {5204}", |
| 306 | "8722 4 8482 51457 {5204}", |
| 307 | 5, 0, NOT_ALL_PRIVATE, 5204, 1, 8722 } |
| 308 | }, |
| 309 | { &test_segments[3], &test_segments[4], |
| 310 | { "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}", |
| 311 | "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}", |
| 312 | 7, 0, NOT_ALL_PRIVATE, 5204, 1, 8482 }, |
| 313 | }, |
| 314 | { &test_segments[4], &test_segments[5], |
| 315 | { "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665" |
| 316 | " {2457,4369,61697} 1842 41590 51793", |
| 317 | "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665" |
| 318 | " {2457,4369,61697} 1842 41590 51793", |
| 319 | 11, 0, NOT_ALL_PRIVATE, 61697, 1, 8467 } |
| 320 | }, |
| 321 | { &test_segments[5], &test_segments[6], |
| 322 | { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793 (123 456 789)", |
| 323 | "6435 59408 21665 {2457,4369,61697} 1842 41590 51793 (123 456 789)", |
| 324 | 7, 3, NOT_ALL_PRIVATE, 123, 1, 6435 }, |
| 325 | }, |
| 326 | { &test_segments[6], &test_segments[7], |
| 327 | { "(123 456 789) (123 456 789) (111 222)", |
| 328 | "", |
| 329 | 0, 8, NOT_ALL_PRIVATE, 111, 1, 0 } |
| 330 | }, |
| 331 | { &test_segments[7], &test_segments[8], |
| 332 | { "(123 456 789) (111 222) [123,456,789]", |
| 333 | "", |
| 334 | 0, 6, NOT_ALL_PRIVATE, 111, 1, 0 } |
| 335 | }, |
| 336 | { &test_segments[8], &test_segments[9], |
| 337 | { "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}", |
| 338 | "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}", |
| 339 | 2, 5, NOT_ALL_PRIVATE, 456, 1, NULL_ASN }, |
| 340 | }, |
| 341 | { &test_segments[9], &test_segments[8], |
| 342 | { "(123 456 789) [111,222] 8722 {4196,48658} [123,456,789]", |
| 343 | "8722 {4196,48658} [123,456,789]", |
| 344 | 2, 5, NOT_ALL_PRIVATE, 48658, 1, NULL_ASN }, |
| 345 | }, |
| 346 | { &test_segments[14], &test_segments[11], |
| 347 | { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 348 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 349 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 350 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 351 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 352 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 353 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 354 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 355 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 356 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 357 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 358 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 359 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 360 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 361 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 362 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 363 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 364 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 365 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 366 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 367 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 368 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 369 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 370 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 371 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 372 | "8466 2 52737 4096 8722 4 8722", |
| 373 | |
| 374 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 375 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 376 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 377 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 378 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 379 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 380 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 381 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 382 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 383 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 384 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 385 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 386 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 387 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 388 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 389 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 390 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 391 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 392 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 393 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 394 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 395 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 396 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 397 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 398 | "8466 3 52737 4096 34285 8466 3 52737 4096 34285 " |
| 399 | "8466 2 52737 4096 8722 4 8722", |
| 400 | 257, 0, NOT_ALL_PRIVATE, 4096, 1000, 8466 }, |
| 401 | }, |
| 402 | { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } }, |
| 403 | }; |
| 404 | |
| 405 | struct tests aggregate_tests[] = |
| 406 | { |
| 407 | { &test_segments[0], &test_segments[1], |
| 408 | { "{3,4,4096,8466,8722,52737}", |
| 409 | "{3,4,4096,8466,8722,52737}", |
| 410 | 1, 0, NOT_ALL_PRIVATE, 52737, 1, NULL_ASN }, |
| 411 | }, |
| 412 | { &test_segments[0], &test_segments[2], |
| 413 | { "8466 3 52737 4096 {4,8722}", |
| 414 | "8466 3 52737 4096 {4,8722}", |
| 415 | 5, 0, NOT_ALL_PRIVATE, 4, 1, 8466 }, |
| 416 | }, |
| 417 | { &test_segments[2], &test_segments[0], |
| 418 | { "8466 3 52737 4096 {4,8722}", |
| 419 | "8466 3 52737 4096 {4,8722}", |
| 420 | 5, 0, NOT_ALL_PRIVATE, 8722, 1, 8466 }, |
| 421 | }, |
| 422 | { &test_segments[2], &test_segments[10], |
| 423 | { "8466 {2,3,4,4096,8722,52737}", |
| 424 | "8466 {2,3,4,4096,8722,52737}", |
| 425 | 2, 0, NOT_ALL_PRIVATE, 8722, 5, 8466 }, |
| 426 | }, |
| 427 | { &test_segments[10], &test_segments[2], |
| 428 | { "8466 {2,3,4,4096,8722,52737}", |
| 429 | "8466 {2,3,4,4096,8722,52737}", |
| 430 | 2, 0, NOT_ALL_PRIVATE, 2, 20000, 8466 }, |
| 431 | }, |
| 432 | { NULL, NULL, { NULL, 0, 0} }, |
| 433 | }; |
| 434 | |
| 435 | struct compare_tests |
| 436 | { |
| 437 | int test_index1; |
| 438 | int test_index2; |
| 439 | #define CMP_RES_YES 1 |
| 440 | #define CMP_RES_NO 0 |
| 441 | char shouldbe_cmp; |
| 442 | char shouldbe_confed; |
| 443 | } left_compare [] = |
| 444 | { |
| 445 | { 0, 1, CMP_RES_NO, CMP_RES_NO }, |
| 446 | { 0, 2, CMP_RES_YES, CMP_RES_NO }, |
| 447 | { 0, 11, CMP_RES_YES, CMP_RES_NO }, |
| 448 | { 1, 11, CMP_RES_NO, CMP_RES_NO }, |
| 449 | { 6, 7, CMP_RES_NO, CMP_RES_YES }, |
| 450 | { 6, 8, CMP_RES_NO, CMP_RES_NO }, |
| 451 | { 7, 8, CMP_RES_NO, CMP_RES_NO }, |
| 452 | { 1, 9, CMP_RES_YES, CMP_RES_NO }, |
| 453 | { 0, 9, CMP_RES_NO, CMP_RES_NO }, |
| 454 | { 3, 9, CMP_RES_NO, CMP_RES_NO }, |
| 455 | { 0, 6, CMP_RES_NO, CMP_RES_NO }, |
| 456 | { 1, 6, CMP_RES_NO, CMP_RES_NO }, |
| 457 | { 0, 8, CMP_RES_NO, CMP_RES_NO }, |
| 458 | { 1, 8, CMP_RES_NO, CMP_RES_NO }, |
| 459 | { 11, 6, CMP_RES_NO, CMP_RES_NO }, |
| 460 | { 11, 7, CMP_RES_NO, CMP_RES_NO }, |
| 461 | { 11, 8, CMP_RES_NO, CMP_RES_NO }, |
| 462 | { 9, 6, CMP_RES_NO, CMP_RES_YES }, |
| 463 | { 9, 7, CMP_RES_NO, CMP_RES_YES }, |
| 464 | { 9, 8, CMP_RES_NO, CMP_RES_NO }, |
| 465 | }; |
| 466 | |
| 467 | /* make an aspath from a data stream */ |
| 468 | static struct aspath * |
| 469 | make_aspath (const u_char *data, size_t len) |
| 470 | { |
| 471 | struct stream *s = NULL; |
| 472 | struct aspath *as; |
| 473 | |
| 474 | if (len) |
| 475 | { |
| 476 | s = stream_new (len); |
| 477 | stream_put (s, data, len); |
| 478 | } |
| 479 | as = aspath_parse (s, len); |
| 480 | stream_free (s); |
| 481 | |
| 482 | return as; |
| 483 | } |
| 484 | |
| 485 | static void |
| 486 | printbytes (const u_char *bytes, int len) |
| 487 | { |
| 488 | int i = 0; |
| 489 | while (i < len) |
| 490 | { |
| 491 | if (i % 2) |
| 492 | printf ("%02hhx%s", bytes[i], " "); |
| 493 | else |
| 494 | printf ("0x%02hhx", bytes[i]); |
| 495 | i++; |
| 496 | } |
| 497 | printf ("\n"); |
| 498 | } |
| 499 | |
| 500 | /* validate the given aspath */ |
| 501 | static int |
| 502 | validate (struct aspath *as, const struct test_spec *sp) |
| 503 | { |
| 504 | size_t bytes; |
| 505 | int fails = 0; |
| 506 | const u_char *out; |
| 507 | struct aspath *asinout, *asconfeddel, *asstr; |
| 508 | |
| 509 | out = aspath_snmp_pathseg (as, &bytes); |
| 510 | asinout = make_aspath (out, bytes); |
| 511 | |
| 512 | asstr = aspath_str2aspath (sp->shouldbe); |
| 513 | |
| 514 | asconfeddel = aspath_delete_confed_seq (aspath_dup (asinout)); |
| 515 | |
| 516 | /* the parsed path should match the specified 'shouldbe' string. |
| 517 | * We should pass the "eat our own dog food" test, be able to output |
| 518 | * this path and then input it again. Ie the path resulting from: |
| 519 | * |
| 520 | * aspath_parse(aspath_put(as)) |
| 521 | * |
| 522 | * should: |
| 523 | * |
| 524 | * - also match the specified 'shouldbe' value |
| 525 | * - hash to same value as original path |
| 526 | * - have same hops and confed counts as original, and as the |
| 527 | * the specified counts |
| 528 | * |
| 529 | * aspath_str2aspath() and shouldbe should match |
| 530 | * |
| 531 | * Confederation related tests: |
| 532 | * - aspath_delete_confed_seq(aspath) should match shouldbe_confed |
| 533 | * - aspath_delete_confed_seq should be idempotent. |
| 534 | */ |
| 535 | if (strcmp(aspath_print (as), sp->shouldbe) |
| 536 | /* hash validation */ |
| 537 | || (aspath_key_make (as) != aspath_key_make (asinout)) |
| 538 | /* by string */ |
| 539 | || strcmp(aspath_print (asinout), sp->shouldbe) |
| 540 | /* by various path counts */ |
| 541 | || (aspath_count_hops (as) != sp->hops) |
| 542 | || (aspath_count_confeds (as) != sp->confeds) |
| 543 | || (aspath_count_hops (asinout) != sp->hops) |
| 544 | || (aspath_count_confeds (asinout) != sp->confeds)) |
| 545 | { |
| 546 | failed++; |
| 547 | fails++; |
| 548 | printf ("shouldbe:\n%s\n", sp->shouldbe); |
| 549 | printf ("got:\n%s\n", aspath_print(as)); |
| 550 | printf ("hash keys: in: %d out->in: %d\n", |
| 551 | aspath_key_make (as), aspath_key_make (asinout)); |
| 552 | printf ("hops: %d, counted %d %d\n", sp->hops, |
| 553 | aspath_count_hops (as), |
| 554 | aspath_count_hops (asinout) ); |
| 555 | printf ("confeds: %d, counted %d %d\n", sp->confeds, |
| 556 | aspath_count_confeds (as), |
| 557 | aspath_count_confeds (asinout)); |
| 558 | printf ("out->in:\n%s\nbytes: ", aspath_print(asinout)); |
| 559 | printbytes (out, bytes); |
| 560 | } |
| 561 | /* basic confed related tests */ |
| 562 | if ((aspath_print (asconfeddel) == NULL |
| 563 | && sp->shouldbe_delete_confed != NULL) |
| 564 | || (aspath_print (asconfeddel) != NULL |
| 565 | && sp->shouldbe_delete_confed == NULL) |
| 566 | || strcmp(aspath_print (asconfeddel), sp->shouldbe_delete_confed) |
| 567 | /* delete_confed_seq should be idempotent */ |
| 568 | || (aspath_key_make (asconfeddel) |
| 569 | != aspath_key_make (aspath_delete_confed_seq (asconfeddel)))) |
| 570 | { |
| 571 | failed++; |
| 572 | fails++; |
| 573 | printf ("confed_del: %s\n", aspath_print (asconfeddel)); |
| 574 | printf ("should be: %s\n", sp->shouldbe_delete_confed); |
| 575 | } |
| 576 | /* aspath_str2aspath test */ |
| 577 | if ((aspath_print (asstr) == NULL && sp->shouldbe != NULL) |
| 578 | || (aspath_print (asstr) != NULL && sp->shouldbe == NULL) |
| 579 | || strcmp(aspath_print (asstr), sp->shouldbe)) |
| 580 | { |
| 581 | failed++; |
| 582 | fails++; |
| 583 | printf ("asstr: %s\n", aspath_print (asstr)); |
| 584 | } |
| 585 | |
| 586 | /* loop, private and first as checks */ |
| 587 | if (aspath_loop_check (as, sp->does_loop) |
| 588 | || aspath_loop_check (as, sp->doesnt_loop) |
| 589 | || (aspath_private_as_check (as) != sp->private_as) |
| 590 | || (aspath_firstas_check (as,sp->first) |
| 591 | && sp->first == 0)) |
| 592 | { |
| 593 | failed++; |
| 594 | fails++; |
| 595 | printf ("firstas: %d, got %d\n", sp->first, |
| 596 | aspath_firstas_check (as,sp->first)); |
| 597 | printf ("loop does: %d %d, doesnt: %d %d\n", |
| 598 | sp->does_loop, aspath_loop_check (as, sp->does_loop), |
| 599 | sp->doesnt_loop, aspath_loop_check (as, sp->doesnt_loop)); |
| 600 | printf ("private check: %d %d\n", sp->private_as, |
| 601 | aspath_private_as_check (as)); |
| 602 | } |
| 603 | aspath_unintern (asinout); |
| 604 | /* aspath_unintern (asconfeddel);*/ |
| 605 | return fails; |
| 606 | |
| 607 | } |
| 608 | |
| 609 | static void |
| 610 | empty_get_test () |
| 611 | { |
| 612 | struct aspath *as = aspath_empty_get (); |
| 613 | struct test_spec sp = { "", "", 0, 0, 0, 0, 0, 0 }; |
| 614 | |
| 615 | printf ("empty_get_test, as: %s\n",aspath_print (as)); |
| 616 | if (!validate (as, &sp)) |
| 617 | printf ("OK\n"); |
| 618 | else |
| 619 | printf ("failed!\n"); |
| 620 | |
| 621 | printf ("\n"); |
| 622 | } |
| 623 | |
| 624 | /* basic parsing test */ |
| 625 | static void |
| 626 | parse_test (struct test_segment *t) |
| 627 | { |
| 628 | struct aspath *asp; |
| 629 | |
| 630 | printf ("%s: %s\n", t->name, t->desc); |
| 631 | |
| 632 | asp = make_aspath (t->asdata, t->len); |
| 633 | |
| 634 | printf ("aspath: %s\nvalidating...:\n", aspath_print (asp)); |
| 635 | |
| 636 | if (!validate (asp, &t->sp)) |
| 637 | printf ("OK\n"); |
| 638 | else |
| 639 | printf ("failed\n"); |
| 640 | |
| 641 | printf ("\n"); |
| 642 | aspath_unintern (asp); |
| 643 | } |
| 644 | |
| 645 | /* prepend testing */ |
| 646 | void |
| 647 | prepend_test (struct tests *t) |
| 648 | { |
| 649 | struct aspath *asp1, *asp2, *ascratch; |
| 650 | |
| 651 | printf ("prepend %s: %s\n", t->test1->name, t->test1->desc); |
| 652 | printf ("to %s: %s\n", t->test2->name, t->test2->desc); |
| 653 | |
| 654 | asp1 = make_aspath (t->test1->asdata, t->test1->len); |
| 655 | asp2 = make_aspath (t->test2->asdata, t->test2->len); |
| 656 | |
| 657 | ascratch = aspath_dup (asp2); |
| 658 | aspath_unintern (asp2); |
| 659 | |
| 660 | asp2 = aspath_prepend (asp1, ascratch); |
| 661 | |
| 662 | printf ("aspath: %s\n", aspath_print (asp2)); |
| 663 | |
| 664 | if (!validate (asp2, &t->sp)) |
| 665 | printf ("OK\n"); |
| 666 | else |
| 667 | printf ("failed!\n"); |
| 668 | |
| 669 | printf ("\n"); |
| 670 | aspath_unintern (asp1); |
| 671 | aspath_free (asp2); |
| 672 | } |
| 673 | |
| 674 | /* empty-prepend testing */ |
| 675 | void |
| 676 | empty_prepend_test (struct test_segment *t) |
| 677 | { |
| 678 | struct aspath *asp1, *asp2, *ascratch; |
| 679 | |
| 680 | printf ("empty prepend %s: %s\n", t->name, t->desc); |
| 681 | |
| 682 | asp1 = make_aspath (t->asdata, t->len); |
| 683 | asp2 = aspath_empty (); |
| 684 | |
| 685 | ascratch = aspath_dup (asp2); |
| 686 | aspath_unintern (asp2); |
| 687 | |
| 688 | asp2 = aspath_prepend (asp1, ascratch); |
| 689 | |
| 690 | printf ("aspath: %s\n", aspath_print (asp2)); |
| 691 | |
| 692 | if (!validate (asp2, &t->sp)) |
| 693 | printf ("OK\n"); |
| 694 | else |
| 695 | printf ("failed!\n"); |
| 696 | |
| 697 | printf ("\n"); |
| 698 | aspath_unintern (asp1); |
| 699 | aspath_free (asp2); |
| 700 | } |
| 701 | |
| 702 | /* aggregation testing */ |
| 703 | void |
| 704 | aggregate_test (struct tests *t) |
| 705 | { |
| 706 | struct aspath *asp1, *asp2, *ascratch; |
| 707 | |
| 708 | printf ("aggregate %s: %s\n", t->test1->name, t->test1->desc); |
| 709 | printf ("with %s: %s\n", t->test2->name, t->test2->desc); |
| 710 | |
| 711 | asp1 = make_aspath (t->test1->asdata, t->test1->len); |
| 712 | asp2 = make_aspath (t->test2->asdata, t->test2->len); |
| 713 | |
| 714 | ascratch = aspath_aggregate (asp1, asp2); |
| 715 | |
| 716 | printf ("aspath: %s\n", aspath_print (ascratch)); |
| 717 | |
| 718 | if (!validate (ascratch, &t->sp)) |
| 719 | printf ("OK\n"); |
| 720 | else |
| 721 | printf ("failed!\n"); |
| 722 | |
| 723 | printf ("\n"); |
| 724 | aspath_unintern (asp1); |
| 725 | aspath_unintern (asp2); |
| 726 | aspath_free (ascratch); |
| 727 | /* aspath_unintern (ascratch);*/ |
| 728 | } |
| 729 | |
| 730 | /* cmp_left tests */ |
| 731 | static void |
| 732 | cmp_test () |
| 733 | { |
| 734 | int i; |
| 735 | #define CMP_TESTS_MAX \ |
| 736 | (sizeof(left_compare) / sizeof (struct compare_tests)) |
| 737 | |
| 738 | for (i = 0; i < CMP_TESTS_MAX; i++) |
| 739 | { |
| 740 | struct test_segment *t1 = &test_segments[left_compare[i].test_index1]; |
| 741 | struct test_segment *t2 = &test_segments[left_compare[i].test_index2]; |
| 742 | struct aspath *asp1, *asp2; |
| 743 | |
| 744 | printf ("left cmp %s: %s\n", t1->name, t1->desc); |
| 745 | printf ("and %s: %s\n", t2->name, t2->desc); |
| 746 | |
| 747 | asp1 = make_aspath (t1->asdata, t1->len); |
| 748 | asp2 = make_aspath (t2->asdata, t2->len); |
| 749 | |
| 750 | if (aspath_cmp_left (asp1, asp2) != left_compare[i].shouldbe_cmp |
| 751 | || aspath_cmp_left (asp2, asp1) != left_compare[i].shouldbe_cmp |
| 752 | || aspath_cmp_left_confed (asp1, asp2) |
| 753 | != left_compare[i].shouldbe_confed |
| 754 | || aspath_cmp_left_confed (asp2, asp1) |
| 755 | != left_compare[i].shouldbe_confed) |
| 756 | { |
| 757 | printf ("failed\n"); |
| 758 | printf ("result should be: cmp: %d, confed: %d\n", |
| 759 | left_compare[i].shouldbe_cmp, |
| 760 | left_compare[i].shouldbe_confed); |
| 761 | printf ("got: cmp %d, cmp_confed: %d\n", |
| 762 | aspath_cmp_left (asp1, asp2), |
| 763 | aspath_cmp_left_confed (asp1, asp2)); |
| 764 | printf("path1: %s\npath2: %s\n", aspath_print (asp1), |
| 765 | aspath_print (asp2)); |
| 766 | failed++; |
| 767 | } |
| 768 | else |
| 769 | printf ("OK\n"); |
| 770 | |
| 771 | printf ("\n"); |
| 772 | aspath_unintern (asp1); |
| 773 | aspath_unintern (asp2); |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | int |
| 778 | main (void) |
| 779 | { |
| 780 | int i = 0; |
| 781 | aspath_init(); |
| 782 | while (test_segments[i].name) |
| 783 | { |
| 784 | parse_test (&test_segments[i]); |
| 785 | empty_prepend_test (&test_segments[i++]); |
| 786 | } |
| 787 | |
| 788 | i = 0; |
| 789 | while (prepend_tests[i].test1) |
| 790 | prepend_test (&prepend_tests[i++]); |
| 791 | |
| 792 | i = 0; |
| 793 | while (aggregate_tests[i].test1) |
| 794 | aggregate_test (&aggregate_tests[i++]); |
| 795 | |
| 796 | cmp_test(); |
| 797 | |
| 798 | i = 0; |
| 799 | |
| 800 | empty_get_test(); |
| 801 | |
| 802 | printf ("failures: %d\n", failed); |
| 803 | printf ("aspath count: %ld\n", aspath_count()); |
| 804 | |
| 805 | return (failed + aspath_count()); |
| 806 | } |