aspath_test.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. #include <zebra.h>
  2. #include "vty.h"
  3. #include "stream.h"
  4. #include "privs.h"
  5. #include "bgpd/bgpd.h"
  6. #include "bgpd/bgp_aspath.h"
  7. #include "bgpd/bgp_attr.h"
  8. #define VT100_RESET "\x1b[0m"
  9. #define VT100_RED "\x1b[31m"
  10. #define VT100_GREEN "\x1b[32m"
  11. #define VT100_YELLOW "\x1b[33m"
  12. #define OK VT100_GREEN "OK" VT100_RESET
  13. #define FAILED VT100_RED "failed" VT100_RESET
  14. /* need these to link in libbgp */
  15. struct zebra_privs_t *bgpd_privs = NULL;
  16. struct thread_master *master = NULL;
  17. static int failed = 0;
  18. /* specification for a test - what the results should be */
  19. struct test_spec
  20. {
  21. const char *shouldbe; /* the string the path should parse to */
  22. const char *shouldbe_delete_confed; /* ditto, but once confeds are deleted */
  23. const unsigned int hops; /* aspath_count_hops result */
  24. const unsigned int confeds; /* aspath_count_confeds */
  25. const int private_as; /* whether the private_as check should pass or fail */
  26. #define NOT_ALL_PRIVATE 0
  27. #define ALL_PRIVATE 1
  28. const as_t does_loop; /* an ASN which should trigger loop-check */
  29. const as_t doesnt_loop; /* one which should not */
  30. const as_t first; /* the first ASN, if there is one */
  31. #define NULL_ASN 0
  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, 51456, 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. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  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. 250, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  287. },
  288. { /* 15 */
  289. "seq1extra",
  290. "seq(8466,3,52737,4096,3456)",
  291. { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
  292. 12,
  293. { "8466 3 52737 4096 3456",
  294. "8466 3 52737 4096 3456",
  295. 5, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  296. },
  297. { /* 16 */
  298. "empty",
  299. "<empty>",
  300. {},
  301. 0,
  302. { "", "", 0, 0, 0, 0, 0, 0 },
  303. },
  304. { /* 17 */
  305. "redundantset",
  306. "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153)",
  307. { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
  308. 0x1,0x4, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9 },
  309. 22,
  310. {
  311. /* We shouldn't ever /generate/ such paths. However, we should
  312. * cope with them fine.
  313. */
  314. "8466 3 52737 4096 3456 {7099,8153}",
  315. "8466 3 52737 4096 3456 {7099,8153}",
  316. 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  317. },
  318. { /* 18 */
  319. "reconcile_lead_asp",
  320. "seq(6435,59408,21665) set(23456,23456,23456), seq(23456,23456,23456)",
  321. { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
  322. 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
  323. 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
  324. 24,
  325. { "6435 59408 21665 {23456} 23456 23456 23456",
  326. "6435 59408 21665 {23456} 23456 23456 23456",
  327. 7, 0, NOT_ALL_PRIVATE, 23456, 1, 6435 },
  328. },
  329. { /* 19 */
  330. "reconcile_new_asp",
  331. "set(2457,61697,4369), seq(1842,41591,51793)",
  332. {
  333. 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
  334. 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51 },
  335. 16,
  336. { "{2457,4369,61697} 1842 41591 51793",
  337. "{2457,4369,61697} 1842 41591 51793",
  338. 4, 0, NOT_ALL_PRIVATE, 51793, 1, 2457 },
  339. },
  340. { /* 20 */
  341. "reconcile_confed",
  342. "confseq(123,456,789) confset(456,124,788) seq(6435,59408,21665)"
  343. " set(23456,23456,23456), seq(23456,23456,23456)",
  344. { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
  345. 0x4,0x3, 0x01,0xc8, 0x00,0x7c, 0x03,0x14,
  346. 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
  347. 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
  348. 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
  349. 40,
  350. { "(123 456 789) [124,456,788] 6435 59408 21665"
  351. " {23456} 23456 23456 23456",
  352. "6435 59408 21665 {23456} 23456 23456 23456",
  353. 7, 4, NOT_ALL_PRIVATE, 23456, 1, 6435 },
  354. },
  355. { /* 21 */
  356. "reconcile_start_trans",
  357. "seq(23456,23456,23456) seq(6435,59408,21665)",
  358. { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
  359. 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
  360. 16,
  361. { "23456 23456 23456 6435 59408 21665",
  362. "23456 23456 23456 6435 59408 21665",
  363. 6, 0, NOT_ALL_PRIVATE, 21665, 1, 23456 },
  364. },
  365. { /* 22 */
  366. "reconcile_start_trans4",
  367. "seq(1842,41591,51793) seq(6435,59408,21665)",
  368. { 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51,
  369. 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
  370. 16,
  371. { "1842 41591 51793 6435 59408 21665",
  372. "1842 41591 51793 6435 59408 21665",
  373. 6, 0, NOT_ALL_PRIVATE, 41591, 1, 1842 },
  374. },
  375. { /* 23 */
  376. "reconcile_start_trans_error",
  377. "seq(23456,23456,23456) seq(6435,59408)",
  378. { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
  379. 0x2,0x2, 0x19,0x23, 0xe8,0x10, },
  380. 14,
  381. { "23456 23456 23456 6435 59408",
  382. "23456 23456 23456 6435 59408",
  383. 5, 0, NOT_ALL_PRIVATE, 59408, 1, 23456 },
  384. },
  385. { /* 24 */
  386. "redundantset2",
  387. "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153,7099)",
  388. { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
  389. 0x1,0x5, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9, 0x1b,0xbb,},
  390. 24,
  391. {
  392. /* We should weed out duplicate set members. */
  393. "8466 3 52737 4096 3456 {7099,8153}",
  394. "8466 3 52737 4096 3456 {7099,8153}",
  395. 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  396. },
  397. { /* 25 */
  398. "zero-size overflow",
  399. "#ASNs = 0, data = seq(8466 3 52737 4096 3456)",
  400. { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
  401. 12,
  402. { NULL, NULL,
  403. 0, 0, 0, 0, 0, 0 },
  404. },
  405. { /* 26 */
  406. "zero-size overflow + valid segment",
  407. "seq(#AS=0:8466 3 52737),seq(4096 3456)",
  408. { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01,
  409. 0x2,0x2, 0x10,0x00, 0x0d,0x80 },
  410. 14
  411. ,
  412. { NULL, NULL,
  413. 0, 0, 0, 0, 0, 0 },
  414. },
  415. { /* 27 */
  416. "invalid segment type",
  417. "type=8(4096 3456)",
  418. { 0x8,0x2, 0x10,0x00, 0x0d,0x80 },
  419. 14
  420. ,
  421. { NULL, NULL,
  422. 0, 0, 0, 0, 0, 0 },
  423. }, { NULL, NULL, {0}, 0, { NULL, 0, 0 } }
  424. };
  425. /* */
  426. static struct aspath_tests {
  427. const char *desc;
  428. const struct test_segment *segment;
  429. const char *shouldbe; /* String it should evaluate to */
  430. const enum as4 { AS4_DATA, AS2_DATA }
  431. as4; /* whether data should be as4 or not (ie as2) */
  432. const int result; /* expected result for bgp_attr_parse */
  433. const int cap; /* capabilities to set for peer */
  434. const char attrheader [1024];
  435. size_t len;
  436. const struct test_segment *old_segment;
  437. } aspath_tests [] =
  438. {
  439. /* 0 */
  440. {
  441. "basic test",
  442. &test_segments[0],
  443. "8466 3 52737 4096",
  444. AS2_DATA, 0,
  445. 0,
  446. { BGP_ATTR_FLAG_TRANS,
  447. BGP_ATTR_AS_PATH,
  448. 10,
  449. },
  450. 3,
  451. },
  452. /* 1 */
  453. {
  454. "length too short",
  455. &test_segments[0],
  456. "8466 3 52737 4096",
  457. AS2_DATA, -1,
  458. 0,
  459. { BGP_ATTR_FLAG_TRANS,
  460. BGP_ATTR_AS_PATH,
  461. 8,
  462. },
  463. 3,
  464. },
  465. /* 2 */
  466. {
  467. "length too long",
  468. &test_segments[0],
  469. "8466 3 52737 4096",
  470. AS2_DATA, -1,
  471. 0,
  472. { BGP_ATTR_FLAG_TRANS,
  473. BGP_ATTR_AS_PATH,
  474. 12,
  475. },
  476. 3,
  477. },
  478. /* 3 */
  479. {
  480. "incorrect flag",
  481. &test_segments[0],
  482. "8466 3 52737 4096",
  483. AS2_DATA, -1,
  484. 0,
  485. { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
  486. BGP_ATTR_AS_PATH,
  487. 10,
  488. },
  489. 3,
  490. },
  491. /* 4 */
  492. {
  493. "as4_path, with as2 format data",
  494. &test_segments[0],
  495. "8466 3 52737 4096",
  496. AS2_DATA, -1,
  497. 0,
  498. { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
  499. BGP_ATTR_AS4_PATH,
  500. 10,
  501. },
  502. 3,
  503. },
  504. /* 5 */
  505. {
  506. "as4, with incorrect attr length",
  507. &test_segments[0],
  508. "8466 3 52737 4096",
  509. AS4_DATA, -1,
  510. PEER_CAP_AS4_RCV,
  511. { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
  512. BGP_ATTR_AS4_PATH,
  513. 10,
  514. },
  515. 3,
  516. },
  517. /* 6 */
  518. {
  519. "basic 4-byte as-path",
  520. &test_segments[0],
  521. "8466 3 52737 4096",
  522. AS4_DATA, 0,
  523. PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
  524. { BGP_ATTR_FLAG_TRANS,
  525. BGP_ATTR_AS_PATH,
  526. 18,
  527. },
  528. 3,
  529. },
  530. /* 7 */
  531. {
  532. "4b AS_PATH: too short",
  533. &test_segments[0],
  534. "8466 3 52737 4096",
  535. AS4_DATA, -1,
  536. PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
  537. { BGP_ATTR_FLAG_TRANS,
  538. BGP_ATTR_AS_PATH,
  539. 16,
  540. },
  541. 3,
  542. },
  543. /* 8 */
  544. {
  545. "4b AS_PATH: too long",
  546. &test_segments[0],
  547. "8466 3 52737 4096",
  548. AS4_DATA, -1,
  549. PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
  550. { BGP_ATTR_FLAG_TRANS,
  551. BGP_ATTR_AS_PATH,
  552. 20,
  553. },
  554. 3,
  555. },
  556. /* 9 */
  557. {
  558. "4b AS_PATH: too long2",
  559. &test_segments[0],
  560. "8466 3 52737 4096",
  561. AS4_DATA, -1,
  562. PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
  563. { BGP_ATTR_FLAG_TRANS,
  564. BGP_ATTR_AS_PATH,
  565. 22,
  566. },
  567. 3,
  568. },
  569. /* 10 */
  570. {
  571. "4b AS_PATH: bad flags",
  572. &test_segments[0],
  573. "8466 3 52737 4096",
  574. AS4_DATA, -1,
  575. PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
  576. { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
  577. BGP_ATTR_AS_PATH,
  578. 18,
  579. },
  580. 3,
  581. },
  582. /* 11 */
  583. {
  584. "4b AS4_PATH w/o AS_PATH",
  585. &test_segments[6],
  586. NULL,
  587. AS4_DATA, 0,
  588. PEER_CAP_AS4_ADV,
  589. { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
  590. BGP_ATTR_AS4_PATH,
  591. 14,
  592. },
  593. 3,
  594. },
  595. /* 12 */
  596. {
  597. "4b AS4_PATH: confed",
  598. &test_segments[6],
  599. "8466 3 52737 4096 (123 456 789)",
  600. AS4_DATA, 0,
  601. PEER_CAP_AS4_ADV,
  602. { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
  603. BGP_ATTR_AS4_PATH,
  604. 14,
  605. },
  606. 3,
  607. &test_segments[0],
  608. },
  609. { NULL, NULL, NULL, 0, 0, 0, { 0 }, 0 },
  610. };
  611. /* prepending tests */
  612. static struct tests {
  613. const struct test_segment *test1;
  614. const struct test_segment *test2;
  615. struct test_spec sp;
  616. } prepend_tests[] =
  617. {
  618. /* 0 */
  619. { &test_segments[0], &test_segments[1],
  620. { "8466 3 52737 4096 8722 4",
  621. "8466 3 52737 4096 8722 4",
  622. 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
  623. },
  624. /* 1 */
  625. { &test_segments[1], &test_segments[3],
  626. { "8722 4 8482 51457 {5204}",
  627. "8722 4 8482 51457 {5204}",
  628. 5, 0, NOT_ALL_PRIVATE, 5204, 1, 8722 }
  629. },
  630. /* 2 */
  631. { &test_segments[3], &test_segments[4],
  632. { "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
  633. "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
  634. 7, 0, NOT_ALL_PRIVATE, 5204, 1, 8482 },
  635. },
  636. /* 3 */
  637. { &test_segments[4], &test_segments[5],
  638. { "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
  639. " {2457,4369,61697} 1842 41590 51793",
  640. "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
  641. " {2457,4369,61697} 1842 41590 51793",
  642. 11, 0, NOT_ALL_PRIVATE, 61697, 1, 8467 }
  643. },
  644. /* 4 */
  645. { &test_segments[5], &test_segments[6],
  646. { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
  647. "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
  648. 7, 0, NOT_ALL_PRIVATE, 1842, 1, 6435 },
  649. },
  650. /* 5 */
  651. { &test_segments[6], &test_segments[7],
  652. { "(123 456 789) (123 456 789) (111 222)",
  653. "",
  654. 0, 8, NOT_ALL_PRIVATE, 111, 1, 0 }
  655. },
  656. { &test_segments[7], &test_segments[8],
  657. { "(123 456 789) (111 222) [123,456,789]",
  658. "",
  659. 0, 6, NOT_ALL_PRIVATE, 111, 1, 0 }
  660. },
  661. { &test_segments[8], &test_segments[9],
  662. { "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
  663. "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
  664. 2, 5, NOT_ALL_PRIVATE, 456, 1, NULL_ASN },
  665. },
  666. { &test_segments[9], &test_segments[8],
  667. { "(123 456 789) [111,222] 8722 {4196,48658} [123,456,789]",
  668. "8722 {4196,48658} [123,456,789]",
  669. 2, 5, NOT_ALL_PRIVATE, 48658, 1, NULL_ASN },
  670. },
  671. { &test_segments[14], &test_segments[11],
  672. { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  673. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  674. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  675. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  676. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  677. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  678. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  679. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  680. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  681. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  682. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  683. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  684. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  685. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  686. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  687. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  688. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  689. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  690. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  691. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  692. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  693. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  694. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  695. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  696. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  697. "8466 2 52737 4096 8722 4 8722",
  698. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  699. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  700. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  701. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  702. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  703. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  704. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  705. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  706. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  707. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  708. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  709. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  710. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  711. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  712. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  713. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  714. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  715. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  716. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  717. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  718. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  719. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  720. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  721. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  722. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  723. "8466 2 52737 4096 8722 4 8722",
  724. 257, 0, NOT_ALL_PRIVATE, 4096, 1000, 8466 },
  725. },
  726. { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
  727. };
  728. struct tests reconcile_tests[] =
  729. {
  730. { &test_segments[18], &test_segments[19],
  731. { "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
  732. "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
  733. 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
  734. },
  735. { &test_segments[19], &test_segments[18],
  736. /* AS_PATH (19) has more hops than NEW_AS_PATH,
  737. * so just AS_PATH should be used (though, this practice
  738. * is bad imho).
  739. */
  740. { "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
  741. "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
  742. 11, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
  743. },
  744. { &test_segments[20], &test_segments[19],
  745. { "(123 456 789) [124,456,788] 6435 59408 21665"
  746. " {2457,4369,61697} 1842 41591 51793",
  747. "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
  748. 7, 4, NOT_ALL_PRIVATE, 51793, 1, 6435 },
  749. },
  750. { &test_segments[21], &test_segments[22],
  751. { "1842 41591 51793 6435 59408 21665",
  752. "1842 41591 51793 6435 59408 21665",
  753. 6, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
  754. },
  755. { &test_segments[23], &test_segments[22],
  756. { "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
  757. "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
  758. 11, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
  759. },
  760. { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
  761. };
  762. struct tests aggregate_tests[] =
  763. {
  764. { &test_segments[0], &test_segments[2],
  765. { "8466 3 52737 4096 {4,8722}",
  766. "8466 3 52737 4096 {4,8722}",
  767. 5, 0, NOT_ALL_PRIVATE, 4, 1, 8466 },
  768. },
  769. { &test_segments[2], &test_segments[0],
  770. { "8466 3 52737 4096 {4,8722}",
  771. "8466 3 52737 4096 {4,8722}",
  772. 5, 0, NOT_ALL_PRIVATE, 8722, 1, 8466 },
  773. },
  774. { &test_segments[2], &test_segments[10],
  775. { "8466 {2,3,4,4096,8722,52737}",
  776. "8466 {2,3,4,4096,8722,52737}",
  777. 2, 0, NOT_ALL_PRIVATE, 8722, 5, 8466 },
  778. },
  779. { &test_segments[10], &test_segments[2],
  780. { "8466 {2,3,4,4096,8722,52737}",
  781. "8466 {2,3,4,4096,8722,52737}",
  782. 2, 0, NOT_ALL_PRIVATE, 2, 20000, 8466 },
  783. },
  784. { &test_segments[5], &test_segments[18],
  785. { "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
  786. "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
  787. 4, 0, NOT_ALL_PRIVATE, 41590, 1, 6435 },
  788. },
  789. { NULL, NULL, { NULL, 0, 0} },
  790. };
  791. struct compare_tests
  792. {
  793. int test_index1;
  794. int test_index2;
  795. #define CMP_RES_YES 1
  796. #define CMP_RES_NO 0
  797. char shouldbe_cmp;
  798. char shouldbe_confed;
  799. } left_compare [] =
  800. {
  801. { 0, 1, CMP_RES_NO, CMP_RES_NO },
  802. { 0, 2, CMP_RES_YES, CMP_RES_NO },
  803. { 0, 11, CMP_RES_YES, CMP_RES_NO },
  804. { 0, 15, CMP_RES_YES, CMP_RES_NO },
  805. { 0, 16, CMP_RES_NO, CMP_RES_NO },
  806. { 1, 11, CMP_RES_NO, CMP_RES_NO },
  807. { 6, 7, CMP_RES_NO, CMP_RES_YES },
  808. { 6, 8, CMP_RES_NO, CMP_RES_NO },
  809. { 7, 8, CMP_RES_NO, CMP_RES_NO },
  810. { 1, 9, CMP_RES_YES, CMP_RES_NO },
  811. { 0, 9, CMP_RES_NO, CMP_RES_NO },
  812. { 3, 9, CMP_RES_NO, CMP_RES_NO },
  813. { 0, 6, CMP_RES_NO, CMP_RES_NO },
  814. { 1, 6, CMP_RES_NO, CMP_RES_NO },
  815. { 0, 8, CMP_RES_NO, CMP_RES_NO },
  816. { 1, 8, CMP_RES_NO, CMP_RES_NO },
  817. { 11, 6, CMP_RES_NO, CMP_RES_NO },
  818. { 11, 7, CMP_RES_NO, CMP_RES_NO },
  819. { 11, 8, CMP_RES_NO, CMP_RES_NO },
  820. { 9, 6, CMP_RES_NO, CMP_RES_YES },
  821. { 9, 7, CMP_RES_NO, CMP_RES_YES },
  822. { 9, 8, CMP_RES_NO, CMP_RES_NO },
  823. };
  824. /* make an aspath from a data stream */
  825. static struct aspath *
  826. make_aspath (const u_char *data, size_t len, int use32bit)
  827. {
  828. struct stream *s = NULL;
  829. struct aspath *as;
  830. if (len)
  831. {
  832. s = stream_new (len);
  833. stream_put (s, data, len);
  834. }
  835. as = aspath_parse (s, len, use32bit);
  836. if (s)
  837. stream_free (s);
  838. return as;
  839. }
  840. static void
  841. printbytes (const u_char *bytes, int len)
  842. {
  843. int i = 0;
  844. while (i < len)
  845. {
  846. if (i % 2)
  847. printf ("%02hhx%s", bytes[i], " ");
  848. else
  849. printf ("0x%02hhx", bytes[i]);
  850. i++;
  851. }
  852. printf ("\n");
  853. }
  854. /* validate the given aspath */
  855. static int
  856. validate (struct aspath *as, const struct test_spec *sp)
  857. {
  858. size_t bytes, bytes4;
  859. int fails = 0;
  860. const u_char *out;
  861. static struct stream *s;
  862. struct aspath *asinout, *asconfeddel, *asstr, *as4;
  863. if (as == NULL && sp->shouldbe == NULL)
  864. {
  865. printf ("Correctly failed to parse\n");
  866. return fails;
  867. }
  868. out = aspath_snmp_pathseg (as, &bytes);
  869. asinout = make_aspath (out, bytes, 0);
  870. /* Excercise AS4 parsing a bit, with a dogfood test */
  871. if (!s)
  872. s = stream_new (4096);
  873. bytes4 = aspath_put (s, as, 1);
  874. as4 = make_aspath (STREAM_DATA(s), bytes4, 1);
  875. asstr = aspath_str2aspath (sp->shouldbe);
  876. asconfeddel = aspath_delete_confed_seq (aspath_dup (asinout));
  877. printf ("got: %s\n", aspath_print(as));
  878. /* the parsed path should match the specified 'shouldbe' string.
  879. * We should pass the "eat our own dog food" test, be able to output
  880. * this path and then input it again. Ie the path resulting from:
  881. *
  882. * aspath_parse(aspath_put(as))
  883. *
  884. * should:
  885. *
  886. * - also match the specified 'shouldbe' value
  887. * - hash to same value as original path
  888. * - have same hops and confed counts as original, and as the
  889. * the specified counts
  890. *
  891. * aspath_str2aspath() and shouldbe should match
  892. *
  893. * We do the same for:
  894. *
  895. * aspath_parse(aspath_put(as,USE32BIT))
  896. *
  897. * Confederation related tests:
  898. * - aspath_delete_confed_seq(aspath) should match shouldbe_confed
  899. * - aspath_delete_confed_seq should be idempotent.
  900. */
  901. if (strcmp(aspath_print (as), sp->shouldbe)
  902. /* hash validation */
  903. || (aspath_key_make (as) != aspath_key_make (asinout))
  904. /* by string */
  905. || strcmp(aspath_print (asinout), sp->shouldbe)
  906. /* By 4-byte parsing */
  907. || strcmp(aspath_print (as4), sp->shouldbe)
  908. /* by various path counts */
  909. || (aspath_count_hops (as) != sp->hops)
  910. || (aspath_count_confeds (as) != sp->confeds)
  911. || (aspath_count_hops (asinout) != sp->hops)
  912. || (aspath_count_confeds (asinout) != sp->confeds))
  913. {
  914. failed++;
  915. fails++;
  916. printf ("shouldbe:\n%s\n", sp->shouldbe);
  917. printf ("as4:\n%s\n", aspath_print (as4));
  918. printf ("hash keys: in: %d out->in: %d\n",
  919. aspath_key_make (as), aspath_key_make (asinout));
  920. printf ("hops: %d, counted %d %d\n", sp->hops,
  921. aspath_count_hops (as),
  922. aspath_count_hops (asinout) );
  923. printf ("confeds: %d, counted %d %d\n", sp->confeds,
  924. aspath_count_confeds (as),
  925. aspath_count_confeds (asinout));
  926. printf ("out->in:\n%s\nbytes: ", aspath_print(asinout));
  927. printbytes (out, bytes);
  928. }
  929. /* basic confed related tests */
  930. if ((aspath_print (asconfeddel) == NULL
  931. && sp->shouldbe_delete_confed != NULL)
  932. || (aspath_print (asconfeddel) != NULL
  933. && sp->shouldbe_delete_confed == NULL)
  934. || strcmp(aspath_print (asconfeddel), sp->shouldbe_delete_confed)
  935. /* delete_confed_seq should be idempotent */
  936. || (aspath_key_make (asconfeddel)
  937. != aspath_key_make (aspath_delete_confed_seq (asconfeddel))))
  938. {
  939. failed++;
  940. fails++;
  941. printf ("confed_del: %s\n", aspath_print (asconfeddel));
  942. printf ("should be: %s\n", sp->shouldbe_delete_confed);
  943. }
  944. /* aspath_str2aspath test */
  945. if ((aspath_print (asstr) == NULL && sp->shouldbe != NULL)
  946. || (aspath_print (asstr) != NULL && sp->shouldbe == NULL)
  947. || strcmp(aspath_print (asstr), sp->shouldbe))
  948. {
  949. failed++;
  950. fails++;
  951. printf ("asstr: %s\n", aspath_print (asstr));
  952. }
  953. /* loop, private and first as checks */
  954. if ((sp->does_loop && aspath_loop_check (as, sp->does_loop) == 0)
  955. || (sp->doesnt_loop && aspath_loop_check (as, sp->doesnt_loop) != 0)
  956. || (aspath_private_as_check (as) != sp->private_as)
  957. || (aspath_firstas_check (as,sp->first)
  958. && sp->first == 0))
  959. {
  960. failed++;
  961. fails++;
  962. printf ("firstas: %d, got %d\n", sp->first,
  963. aspath_firstas_check (as,sp->first));
  964. printf ("loop does: %d %d, doesnt: %d %d\n",
  965. sp->does_loop, aspath_loop_check (as, sp->does_loop),
  966. sp->doesnt_loop, aspath_loop_check (as, sp->doesnt_loop));
  967. printf ("private check: %d %d\n", sp->private_as,
  968. aspath_private_as_check (as));
  969. }
  970. aspath_unintern (&asinout);
  971. aspath_unintern (&as4);
  972. aspath_free (asconfeddel);
  973. aspath_free (asstr);
  974. stream_reset (s);
  975. return fails;
  976. }
  977. static void
  978. empty_get_test ()
  979. {
  980. struct aspath *as = aspath_empty_get ();
  981. struct test_spec sp = { "", "", 0, 0, 0, 0, 0, 0 };
  982. printf ("empty_get_test, as: %s\n",aspath_print (as));
  983. if (!validate (as, &sp))
  984. printf ("%s\n", OK);
  985. else
  986. printf ("%s!\n", FAILED);
  987. printf ("\n");
  988. aspath_free (as);
  989. }
  990. /* basic parsing test */
  991. static void
  992. parse_test (struct test_segment *t)
  993. {
  994. struct aspath *asp;
  995. printf ("%s: %s\n", t->name, t->desc);
  996. asp = make_aspath (t->asdata, t->len, 0);
  997. printf ("aspath: %s\nvalidating...:\n", aspath_print (asp));
  998. if (!validate (asp, &t->sp))
  999. printf (OK "\n");
  1000. else
  1001. printf (FAILED "\n");
  1002. printf ("\n");
  1003. if (asp)
  1004. aspath_unintern (&asp);
  1005. }
  1006. /* prepend testing */
  1007. static void
  1008. prepend_test (struct tests *t)
  1009. {
  1010. struct aspath *asp1, *asp2, *ascratch;
  1011. printf ("prepend %s: %s\n", t->test1->name, t->test1->desc);
  1012. printf ("to %s: %s\n", t->test2->name, t->test2->desc);
  1013. asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
  1014. asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
  1015. ascratch = aspath_dup (asp2);
  1016. aspath_unintern (&asp2);
  1017. asp2 = aspath_prepend (asp1, ascratch);
  1018. printf ("aspath: %s\n", aspath_print (asp2));
  1019. if (!validate (asp2, &t->sp))
  1020. printf ("%s\n", OK);
  1021. else
  1022. printf ("%s!\n", FAILED);
  1023. printf ("\n");
  1024. aspath_unintern (&asp1);
  1025. aspath_free (asp2);
  1026. }
  1027. /* empty-prepend testing */
  1028. static void
  1029. empty_prepend_test (struct test_segment *t)
  1030. {
  1031. struct aspath *asp1, *asp2, *ascratch;
  1032. printf ("empty prepend %s: %s\n", t->name, t->desc);
  1033. asp1 = make_aspath (t->asdata, t->len, 0);
  1034. asp2 = aspath_empty ();
  1035. ascratch = aspath_dup (asp2);
  1036. aspath_unintern (&asp2);
  1037. asp2 = aspath_prepend (asp1, ascratch);
  1038. printf ("aspath: %s\n", aspath_print (asp2));
  1039. if (!validate (asp2, &t->sp))
  1040. printf (OK "\n");
  1041. else
  1042. printf (FAILED "!\n");
  1043. printf ("\n");
  1044. if (asp1)
  1045. aspath_unintern (&asp1);
  1046. aspath_free (asp2);
  1047. }
  1048. /* as2+as4 reconciliation testing */
  1049. static void
  1050. as4_reconcile_test (struct tests *t)
  1051. {
  1052. struct aspath *asp1, *asp2, *ascratch;
  1053. printf ("reconciling %s:\n %s\n", t->test1->name, t->test1->desc);
  1054. printf ("with %s:\n %s\n", t->test2->name, t->test2->desc);
  1055. asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
  1056. asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
  1057. ascratch = aspath_reconcile_as4 (asp1, asp2);
  1058. if (!validate (ascratch, &t->sp))
  1059. printf (OK "\n");
  1060. else
  1061. printf (FAILED "!\n");
  1062. printf ("\n");
  1063. aspath_unintern (&asp1);
  1064. aspath_unintern (&asp2);
  1065. aspath_free (ascratch);
  1066. }
  1067. /* aggregation testing */
  1068. static void
  1069. aggregate_test (struct tests *t)
  1070. {
  1071. struct aspath *asp1, *asp2, *ascratch;
  1072. printf ("aggregate %s: %s\n", t->test1->name, t->test1->desc);
  1073. printf ("with %s: %s\n", t->test2->name, t->test2->desc);
  1074. asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
  1075. asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
  1076. ascratch = aspath_aggregate (asp1, asp2);
  1077. if (!validate (ascratch, &t->sp))
  1078. printf (OK "\n");
  1079. else
  1080. printf (FAILED "!\n");
  1081. printf ("\n");
  1082. aspath_unintern (&asp1);
  1083. aspath_unintern (&asp2);
  1084. aspath_free (ascratch);
  1085. /* aspath_unintern (ascratch);*/
  1086. }
  1087. /* cmp_left tests */
  1088. static void
  1089. cmp_test ()
  1090. {
  1091. unsigned int i;
  1092. #define CMP_TESTS_MAX \
  1093. (sizeof(left_compare) / sizeof (struct compare_tests))
  1094. for (i = 0; i < CMP_TESTS_MAX; i++)
  1095. {
  1096. struct test_segment *t1 = &test_segments[left_compare[i].test_index1];
  1097. struct test_segment *t2 = &test_segments[left_compare[i].test_index2];
  1098. struct aspath *asp1, *asp2;
  1099. printf ("left cmp %s: %s\n", t1->name, t1->desc);
  1100. printf ("and %s: %s\n", t2->name, t2->desc);
  1101. asp1 = make_aspath (t1->asdata, t1->len, 0);
  1102. asp2 = make_aspath (t2->asdata, t2->len, 0);
  1103. if (aspath_cmp_left (asp1, asp2) != left_compare[i].shouldbe_cmp
  1104. || aspath_cmp_left (asp2, asp1) != left_compare[i].shouldbe_cmp
  1105. || aspath_cmp_left_confed (asp1, asp2)
  1106. != left_compare[i].shouldbe_confed
  1107. || aspath_cmp_left_confed (asp2, asp1)
  1108. != left_compare[i].shouldbe_confed)
  1109. {
  1110. failed++;
  1111. printf (FAILED "\n");
  1112. printf ("result should be: cmp: %d, confed: %d\n",
  1113. left_compare[i].shouldbe_cmp,
  1114. left_compare[i].shouldbe_confed);
  1115. printf ("got: cmp %d, cmp_confed: %d\n",
  1116. aspath_cmp_left (asp1, asp2),
  1117. aspath_cmp_left_confed (asp1, asp2));
  1118. printf("path1: %s\npath2: %s\n", aspath_print (asp1),
  1119. aspath_print (asp2));
  1120. }
  1121. else
  1122. printf (OK "\n");
  1123. printf ("\n");
  1124. aspath_unintern (&asp1);
  1125. aspath_unintern (&asp2);
  1126. }
  1127. }
  1128. static int
  1129. handle_attr_test (struct aspath_tests *t)
  1130. {
  1131. struct bgp bgp = { 0 };
  1132. struct peer peer = { 0 };
  1133. struct attr attr = { 0 };
  1134. int ret;
  1135. int initfail = failed;
  1136. struct aspath *asp;
  1137. size_t datalen;
  1138. asp = make_aspath (t->segment->asdata, t->segment->len, 0);
  1139. peer.ibuf = stream_new (BGP_MAX_PACKET_SIZE);
  1140. peer.obuf = stream_fifo_new ();
  1141. peer.bgp = &bgp;
  1142. peer.host = (char *)"none";
  1143. peer.fd = -1;
  1144. peer.cap = t->cap;
  1145. stream_write (peer.ibuf, t->attrheader, t->len);
  1146. datalen = aspath_put (peer.ibuf, asp, t->as4 == AS4_DATA);
  1147. if (t->old_segment)
  1148. {
  1149. char dummyaspath[] = { BGP_ATTR_FLAG_TRANS, BGP_ATTR_AS_PATH,
  1150. t->old_segment->len };
  1151. stream_write (peer.ibuf, dummyaspath, sizeof (dummyaspath));
  1152. stream_write (peer.ibuf, t->old_segment->asdata, t->old_segment->len);
  1153. datalen += sizeof (dummyaspath) + t->old_segment->len;
  1154. }
  1155. ret = bgp_attr_parse (&peer, &attr, t->len + datalen, NULL, NULL);
  1156. if (ret != t->result)
  1157. {
  1158. printf ("bgp_attr_parse returned %d, expected %d\n", ret, t->result);
  1159. printf ("datalen %zd\n", datalen);
  1160. failed++;
  1161. }
  1162. if (ret != 0)
  1163. goto out;
  1164. if (t->shouldbe && attr.aspath == NULL)
  1165. {
  1166. printf ("aspath is NULL, but should be: %s\n", t->shouldbe);
  1167. failed++;
  1168. }
  1169. if (t->shouldbe && attr.aspath && strcmp (attr.aspath->str, t->shouldbe))
  1170. {
  1171. printf ("attr str and 'shouldbe' mismatched!\n"
  1172. "attr str: %s\n"
  1173. "shouldbe: %s\n",
  1174. attr.aspath->str, t->shouldbe);
  1175. failed++;
  1176. }
  1177. if (!t->shouldbe && attr.aspath)
  1178. {
  1179. printf ("aspath should be NULL, but is: %s\n", attr.aspath->str);
  1180. failed++;
  1181. }
  1182. out:
  1183. if (attr.aspath)
  1184. aspath_unintern (&attr.aspath);
  1185. if (asp)
  1186. aspath_unintern (&asp);
  1187. return failed - initfail;
  1188. }
  1189. static void
  1190. attr_test (struct aspath_tests *t)
  1191. {
  1192. printf ("%s\n", t->desc);
  1193. printf ("%s\n\n", handle_attr_test (t) ? FAILED : OK);
  1194. }
  1195. int
  1196. main (void)
  1197. {
  1198. int i = 0;
  1199. bgp_master_init ();
  1200. master = bm->master;
  1201. bgp_option_set (BGP_OPT_NO_LISTEN);
  1202. bgp_attr_init ();
  1203. while (test_segments[i].name)
  1204. {
  1205. printf ("test %u\n", i);
  1206. parse_test (&test_segments[i]);
  1207. empty_prepend_test (&test_segments[i++]);
  1208. }
  1209. i = 0;
  1210. while (prepend_tests[i].test1)
  1211. {
  1212. printf ("prepend test %u\n", i);
  1213. prepend_test (&prepend_tests[i++]);
  1214. }
  1215. i = 0;
  1216. while (aggregate_tests[i].test1)
  1217. {
  1218. printf ("aggregate test %u\n", i);
  1219. aggregate_test (&aggregate_tests[i++]);
  1220. }
  1221. i = 0;
  1222. while (reconcile_tests[i].test1)
  1223. {
  1224. printf ("reconcile test %u\n", i);
  1225. as4_reconcile_test (&reconcile_tests[i++]);
  1226. }
  1227. i = 0;
  1228. cmp_test();
  1229. i = 0;
  1230. empty_get_test();
  1231. i = 0;
  1232. while (aspath_tests[i].desc)
  1233. {
  1234. printf ("aspath_attr test %d\n", i);
  1235. attr_test (&aspath_tests[i++]);
  1236. }
  1237. printf ("failures: %d\n", failed);
  1238. printf ("aspath count: %ld\n", aspath_count());
  1239. return (failed + aspath_count());
  1240. }