aspath_test.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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. #define VT100_RESET "\x1b[0m"
  8. #define VT100_RED "\x1b[31m"
  9. #define VT100_GREEN "\x1b[32m"
  10. #define VT100_YELLOW "\x1b[33m"
  11. #define OK VT100_GREEN "OK" VT100_RESET
  12. #define FAILED VT100_RED "failed" VT100_RESET
  13. /* need these to link in libbgp */
  14. struct zebra_privs_t *bgpd_privs = NULL;
  15. struct thread_master *master = NULL;
  16. static int failed = 0;
  17. /* specification for a test - what the results should be */
  18. struct test_spec
  19. {
  20. const char *shouldbe; /* the string the path should parse to */
  21. const char *shouldbe_delete_confed; /* ditto, but once confeds are deleted */
  22. const unsigned int hops; /* aspath_count_hops result */
  23. const unsigned int confeds; /* aspath_count_confeds */
  24. const int private_as; /* whether the private_as check should pass or fail */
  25. #define NOT_ALL_PRIVATE 0
  26. #define ALL_PRIVATE 1
  27. const as_t does_loop; /* an ASN which should trigger loop-check */
  28. const as_t doesnt_loop; /* one which should not */
  29. const as_t first; /* the first ASN, if there is one */
  30. #define NULL_ASN 0
  31. };
  32. /* test segments to parse and validate, and use for other tests */
  33. static struct test_segment {
  34. const char *name;
  35. const char *desc;
  36. const u_char asdata[1024];
  37. int len;
  38. struct test_spec sp;
  39. } test_segments [] =
  40. {
  41. { /* 0 */
  42. "seq1",
  43. "seq(8466,3,52737,4096)",
  44. { 0x2,0x4, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00 },
  45. 10,
  46. { "8466 3 52737 4096",
  47. "8466 3 52737 4096",
  48. 4, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  49. },
  50. { /* 1 */
  51. "seq2",
  52. "seq(8722) seq(4)",
  53. { 0x2,0x1, 0x22,0x12,
  54. 0x2,0x1, 0x00,0x04 },
  55. 8,
  56. { "8722 4",
  57. "8722 4",
  58. 2, 0, NOT_ALL_PRIVATE, 4, 5, 8722, },
  59. },
  60. { /* 2 */
  61. "seq3",
  62. "seq(8466,3,52737,4096,8722,4)",
  63. { 0x2,0x6, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00,
  64. 0x22,0x12, 0x00,0x04},
  65. 14,
  66. { "8466 3 52737 4096 8722 4",
  67. "8466 3 52737 4096 8722 4",
  68. 6, 0, NOT_ALL_PRIVATE, 3, 5, 8466 },
  69. },
  70. { /* 3 */
  71. "seqset",
  72. "seq(8482,51457) set(5204)",
  73. { 0x2,0x2, 0x21,0x22, 0xc9,0x01,
  74. 0x1,0x1, 0x14,0x54 },
  75. 10,
  76. { "8482 51457 {5204}",
  77. "8482 51457 {5204}",
  78. 3, 0, NOT_ALL_PRIVATE, 5204, 51456, 8482},
  79. },
  80. { /* 4 */
  81. "seqset2",
  82. "seq(8467, 59649) set(4196,48658) set(17322,30745)",
  83. { 0x2,0x2, 0x21,0x13, 0xe9,0x01,
  84. 0x1,0x2, 0x10,0x64, 0xbe,0x12,
  85. 0x1,0x2, 0x43,0xaa, 0x78,0x19 },
  86. 18,
  87. { "8467 59649 {4196,48658} {17322,30745}",
  88. "8467 59649 {4196,48658} {17322,30745}",
  89. 4, 0, NOT_ALL_PRIVATE, 48658, 1, 8467},
  90. },
  91. { /* 5 */
  92. "multi",
  93. "seq(6435,59408,21665) set(2457,61697,4369), seq(1842,41590,51793)",
  94. { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
  95. 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
  96. 0x2,0x3, 0x07,0x32, 0xa2,0x76, 0xca,0x51 },
  97. 24,
  98. { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
  99. "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
  100. 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
  101. },
  102. { /* 6 */
  103. "confed",
  104. "confseq(123,456,789)",
  105. { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15 },
  106. 8,
  107. { "(123 456 789)",
  108. "",
  109. 0, 3, NOT_ALL_PRIVATE, 789, 1, NULL_ASN },
  110. },
  111. { /* 7 */
  112. "confed2",
  113. "confseq(123,456,789) confseq(111,222)",
  114. { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
  115. 0x3,0x2, 0x00,0x6f, 0x00,0xde },
  116. 14,
  117. { "(123 456 789) (111 222)",
  118. "",
  119. 0, 5, NOT_ALL_PRIVATE, 111, 1, NULL_ASN },
  120. },
  121. { /* 8 */
  122. "confset",
  123. "confset(456,123,789)",
  124. { 0x4,0x3, 0x01,0xc8, 0x00,0x7b, 0x03,0x15 },
  125. 8,
  126. { "[123,456,789]",
  127. "[123,456,789]",
  128. 0, 1, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
  129. },
  130. { /* 9 */
  131. "confmulti",
  132. "confseq(123,456,789) confset(222,111) seq(8722) set(4196,48658)",
  133. { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
  134. 0x4,0x2, 0x00,0xde, 0x00,0x6f,
  135. 0x2,0x1, 0x22,0x12,
  136. 0x1,0x2, 0x10,0x64, 0xbe,0x12 },
  137. 24,
  138. { "(123 456 789) [111,222] 8722 {4196,48658}",
  139. "8722 {4196,48658}",
  140. 2, 4, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
  141. },
  142. { /* 10 */
  143. "seq4",
  144. "seq(8466,2,52737,4096,8722,4)",
  145. { 0x2,0x6, 0x21,0x12, 0x00,0x02, 0xce,0x01, 0x10,0x00,
  146. 0x22,0x12, 0x00,0x04},
  147. 14,
  148. { "8466 2 52737 4096 8722 4",
  149. "8466 2 52737 4096 8722 4",
  150. 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
  151. },
  152. { /* 11 */
  153. "tripleseq1",
  154. "seq(8466,2,52737) seq(4096,8722,4) seq(8722)",
  155. { 0x2,0x3, 0x21,0x12, 0x00,0x02, 0xce,0x01,
  156. 0x2,0x3, 0x10,0x00, 0x22,0x12, 0x00,0x04,
  157. 0x2,0x1, 0x22,0x12},
  158. 20,
  159. { "8466 2 52737 4096 8722 4 8722",
  160. "8466 2 52737 4096 8722 4 8722",
  161. 7, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
  162. },
  163. { /* 12 */
  164. "someprivate",
  165. "seq(8466,64512,52737,65535)",
  166. { 0x2,0x4, 0x21,0x12, 0xfc,0x00, 0xce,0x01, 0xff,0xff },
  167. 10,
  168. { "8466 64512 52737 65535",
  169. "8466 64512 52737 65535",
  170. 4, 0, NOT_ALL_PRIVATE, 65535, 4, 8466 },
  171. },
  172. { /* 13 */
  173. "allprivate",
  174. "seq(65534,64512,64513,65535)",
  175. { 0x2,0x4, 0xff,0xfe, 0xfc,0x00, 0xfc,0x01, 0xff,0xff },
  176. 10,
  177. { "65534 64512 64513 65535",
  178. "65534 64512 64513 65535",
  179. 4, 0, ALL_PRIVATE, 65534, 4, 65534 },
  180. },
  181. { /* 14 */
  182. "long",
  183. "seq(8466,3,52737,4096,34285,<repeated 49 more times>)",
  184. { 0x2,0xfa, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  185. 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. 502,
  235. { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  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. 250, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  286. },
  287. { /* 15 */
  288. "seq1extra",
  289. "seq(8466,3,52737,4096,3456)",
  290. { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
  291. 12,
  292. { "8466 3 52737 4096 3456",
  293. "8466 3 52737 4096 3456",
  294. 5, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  295. },
  296. { /* 16 */
  297. "empty",
  298. "<empty>",
  299. {},
  300. 0,
  301. { "", "", 0, 0, 0, 0, 0, 0 },
  302. },
  303. { /* 17 */
  304. "redundantset",
  305. "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153)",
  306. { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
  307. 0x1,0x4, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9 },
  308. 22,
  309. {
  310. /* We shouldn't ever /generate/ such paths. However, we should
  311. * cope with them fine.
  312. */
  313. "8466 3 52737 4096 3456 {7099,8153}",
  314. "8466 3 52737 4096 3456 {7099,8153}",
  315. 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  316. },
  317. { /* 18 */
  318. "reconcile_lead_asp",
  319. "seq(6435,59408,21665) set(23456,23456,23456), seq(23456,23456,23456)",
  320. { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
  321. 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
  322. 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
  323. 24,
  324. { "6435 59408 21665 {23456} 23456 23456 23456",
  325. "6435 59408 21665 {23456} 23456 23456 23456",
  326. 7, 0, NOT_ALL_PRIVATE, 23456, 1, 6435 },
  327. },
  328. { /* 19 */
  329. "reconcile_new_asp",
  330. "set(2457,61697,4369), seq(1842,41591,51793)",
  331. {
  332. 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
  333. 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51 },
  334. 16,
  335. { "{2457,4369,61697} 1842 41591 51793",
  336. "{2457,4369,61697} 1842 41591 51793",
  337. 4, 0, NOT_ALL_PRIVATE, 51793, 1, 2457 },
  338. },
  339. { /* 20 */
  340. "reconcile_confed",
  341. "confseq(123,456,789) confset(456,124,788) seq(6435,59408,21665)"
  342. " set(23456,23456,23456), seq(23456,23456,23456)",
  343. { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
  344. 0x4,0x3, 0x01,0xc8, 0x00,0x7c, 0x03,0x14,
  345. 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
  346. 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
  347. 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
  348. 40,
  349. { "(123 456 789) [124,456,788] 6435 59408 21665"
  350. " {23456} 23456 23456 23456",
  351. "6435 59408 21665 {23456} 23456 23456 23456",
  352. 7, 4, NOT_ALL_PRIVATE, 23456, 1, 6435 },
  353. },
  354. { /* 21 */
  355. "reconcile_start_trans",
  356. "seq(23456,23456,23456) seq(6435,59408,21665)",
  357. { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
  358. 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
  359. 16,
  360. { "23456 23456 23456 6435 59408 21665",
  361. "23456 23456 23456 6435 59408 21665",
  362. 6, 0, NOT_ALL_PRIVATE, 21665, 1, 23456 },
  363. },
  364. { /* 22 */
  365. "reconcile_start_trans4",
  366. "seq(1842,41591,51793) seq(6435,59408,21665)",
  367. { 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51,
  368. 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
  369. 16,
  370. { "1842 41591 51793 6435 59408 21665",
  371. "1842 41591 51793 6435 59408 21665",
  372. 6, 0, NOT_ALL_PRIVATE, 41591, 1, 1842 },
  373. },
  374. { /* 23 */
  375. "reconcile_start_trans_error",
  376. "seq(23456,23456,23456) seq(6435,59408)",
  377. { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
  378. 0x2,0x2, 0x19,0x23, 0xe8,0x10, },
  379. 14,
  380. { "23456 23456 23456 6435 59408",
  381. "23456 23456 23456 6435 59408",
  382. 5, 0, NOT_ALL_PRIVATE, 59408, 1, 23456 },
  383. },
  384. { /* 24 */
  385. "redundantset2",
  386. "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153,7099)",
  387. { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
  388. 0x1,0x5, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9, 0x1b,0xbb,},
  389. 24,
  390. {
  391. /* We should weed out duplicate set members. */
  392. "8466 3 52737 4096 3456 {7099,8153}",
  393. "8466 3 52737 4096 3456 {7099,8153}",
  394. 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  395. },
  396. { /* 25 */
  397. "zero-size overflow",
  398. "#ASNs = 0, data = seq(8466 3 52737 4096 3456)",
  399. { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
  400. 12,
  401. { "", "",
  402. 0, 0, 0, 0, 0, 0 },
  403. },
  404. { /* 26 */
  405. "zero-size overflow + valid segment",
  406. "seq(#AS=0:8466 3 52737),seq(4096 3456)",
  407. { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01,
  408. 0x2,0x2, 0x10,0x00, 0x0d,0x80 },
  409. 14
  410. ,
  411. { "", "",
  412. 0, 0, 0, 0, 0, 0 },
  413. },
  414. { NULL, NULL, {0}, 0, { NULL, 0, 0 } }
  415. };
  416. /* prepending tests */
  417. static struct tests {
  418. const struct test_segment *test1;
  419. const struct test_segment *test2;
  420. struct test_spec sp;
  421. } prepend_tests[] =
  422. {
  423. { &test_segments[0], &test_segments[1],
  424. { "8466 3 52737 4096 8722 4",
  425. "8466 3 52737 4096 8722 4",
  426. 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
  427. },
  428. { &test_segments[1], &test_segments[3],
  429. { "8722 4 8482 51457 {5204}",
  430. "8722 4 8482 51457 {5204}",
  431. 5, 0, NOT_ALL_PRIVATE, 5204, 1, 8722 }
  432. },
  433. { &test_segments[3], &test_segments[4],
  434. { "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
  435. "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
  436. 7, 0, NOT_ALL_PRIVATE, 5204, 1, 8482 },
  437. },
  438. { &test_segments[4], &test_segments[5],
  439. { "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
  440. " {2457,4369,61697} 1842 41590 51793",
  441. "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
  442. " {2457,4369,61697} 1842 41590 51793",
  443. 11, 0, NOT_ALL_PRIVATE, 61697, 1, 8467 }
  444. },
  445. { &test_segments[5], &test_segments[6],
  446. { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793 (123 456 789)",
  447. "6435 59408 21665 {2457,4369,61697} 1842 41590 51793 (123 456 789)",
  448. 7, 3, NOT_ALL_PRIVATE, 123, 1, 6435 },
  449. },
  450. { &test_segments[6], &test_segments[7],
  451. { "(123 456 789) (123 456 789) (111 222)",
  452. "",
  453. 0, 8, NOT_ALL_PRIVATE, 111, 1, 0 }
  454. },
  455. { &test_segments[7], &test_segments[8],
  456. { "(123 456 789) (111 222) [123,456,789]",
  457. "",
  458. 0, 6, NOT_ALL_PRIVATE, 111, 1, 0 }
  459. },
  460. { &test_segments[8], &test_segments[9],
  461. { "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
  462. "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
  463. 2, 5, NOT_ALL_PRIVATE, 456, 1, NULL_ASN },
  464. },
  465. { &test_segments[9], &test_segments[8],
  466. { "(123 456 789) [111,222] 8722 {4196,48658} [123,456,789]",
  467. "8722 {4196,48658} [123,456,789]",
  468. 2, 5, NOT_ALL_PRIVATE, 48658, 1, NULL_ASN },
  469. },
  470. { &test_segments[14], &test_segments[11],
  471. { "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  472. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  473. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  474. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  475. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  476. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  477. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  478. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  479. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  480. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  481. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  482. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  483. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  484. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  485. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  486. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  487. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  488. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  489. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  490. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  491. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  492. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  493. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  494. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  495. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  496. "8466 2 52737 4096 8722 4 8722",
  497. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  498. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  499. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  500. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  501. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  502. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  503. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  504. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  505. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  506. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  507. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  508. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  509. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  510. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  511. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  512. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  513. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  514. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  515. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  516. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  517. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  518. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  519. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  520. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  521. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  522. "8466 2 52737 4096 8722 4 8722",
  523. 257, 0, NOT_ALL_PRIVATE, 4096, 1000, 8466 },
  524. },
  525. { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
  526. };
  527. struct tests reconcile_tests[] =
  528. {
  529. { &test_segments[18], &test_segments[19],
  530. { "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
  531. "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
  532. 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
  533. },
  534. { &test_segments[19], &test_segments[18],
  535. /* AS_PATH (19) has more hops than NEW_AS_PATH,
  536. * so just AS_PATH should be used (though, this practice
  537. * is bad imho).
  538. */
  539. { "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
  540. "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
  541. 11, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
  542. },
  543. { &test_segments[20], &test_segments[19],
  544. { "(123 456 789) [124,456,788] 6435 59408 21665"
  545. " {2457,4369,61697} 1842 41591 51793",
  546. "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
  547. 7, 4, NOT_ALL_PRIVATE, 51793, 1, 6435 },
  548. },
  549. { &test_segments[21], &test_segments[22],
  550. { "1842 41591 51793 6435 59408 21665",
  551. "1842 41591 51793 6435 59408 21665",
  552. 6, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
  553. },
  554. { &test_segments[23], &test_segments[22],
  555. { "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
  556. "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
  557. 11, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
  558. },
  559. { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
  560. };
  561. struct tests aggregate_tests[] =
  562. {
  563. { &test_segments[0], &test_segments[2],
  564. { "8466 3 52737 4096 {4,8722}",
  565. "8466 3 52737 4096 {4,8722}",
  566. 5, 0, NOT_ALL_PRIVATE, 4, 1, 8466 },
  567. },
  568. { &test_segments[2], &test_segments[0],
  569. { "8466 3 52737 4096 {4,8722}",
  570. "8466 3 52737 4096 {4,8722}",
  571. 5, 0, NOT_ALL_PRIVATE, 8722, 1, 8466 },
  572. },
  573. { &test_segments[2], &test_segments[10],
  574. { "8466 {2,3,4,4096,8722,52737}",
  575. "8466 {2,3,4,4096,8722,52737}",
  576. 2, 0, NOT_ALL_PRIVATE, 8722, 5, 8466 },
  577. },
  578. { &test_segments[10], &test_segments[2],
  579. { "8466 {2,3,4,4096,8722,52737}",
  580. "8466 {2,3,4,4096,8722,52737}",
  581. 2, 0, NOT_ALL_PRIVATE, 2, 20000, 8466 },
  582. },
  583. { &test_segments[5], &test_segments[18],
  584. { "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
  585. "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
  586. 4, 0, NOT_ALL_PRIVATE, 41590, 1, 6435 },
  587. },
  588. { NULL, NULL, { NULL, 0, 0} },
  589. };
  590. struct compare_tests
  591. {
  592. int test_index1;
  593. int test_index2;
  594. #define CMP_RES_YES 1
  595. #define CMP_RES_NO 0
  596. char shouldbe_cmp;
  597. char shouldbe_confed;
  598. } left_compare [] =
  599. {
  600. { 0, 1, CMP_RES_NO, CMP_RES_NO },
  601. { 0, 2, CMP_RES_YES, CMP_RES_NO },
  602. { 0, 11, CMP_RES_YES, CMP_RES_NO },
  603. { 0, 15, CMP_RES_YES, CMP_RES_NO },
  604. { 0, 16, CMP_RES_NO, CMP_RES_NO },
  605. { 1, 11, CMP_RES_NO, CMP_RES_NO },
  606. { 6, 7, CMP_RES_NO, CMP_RES_YES },
  607. { 6, 8, CMP_RES_NO, CMP_RES_NO },
  608. { 7, 8, CMP_RES_NO, CMP_RES_NO },
  609. { 1, 9, CMP_RES_YES, CMP_RES_NO },
  610. { 0, 9, CMP_RES_NO, CMP_RES_NO },
  611. { 3, 9, CMP_RES_NO, CMP_RES_NO },
  612. { 0, 6, CMP_RES_NO, CMP_RES_NO },
  613. { 1, 6, CMP_RES_NO, CMP_RES_NO },
  614. { 0, 8, CMP_RES_NO, CMP_RES_NO },
  615. { 1, 8, CMP_RES_NO, CMP_RES_NO },
  616. { 11, 6, CMP_RES_NO, CMP_RES_NO },
  617. { 11, 7, CMP_RES_NO, CMP_RES_NO },
  618. { 11, 8, CMP_RES_NO, CMP_RES_NO },
  619. { 9, 6, CMP_RES_NO, CMP_RES_YES },
  620. { 9, 7, CMP_RES_NO, CMP_RES_YES },
  621. { 9, 8, CMP_RES_NO, CMP_RES_NO },
  622. };
  623. /* make an aspath from a data stream */
  624. static struct aspath *
  625. make_aspath (const u_char *data, size_t len, int use32bit)
  626. {
  627. struct stream *s = NULL;
  628. struct aspath *as;
  629. if (len)
  630. {
  631. s = stream_new (len);
  632. stream_put (s, data, len);
  633. }
  634. as = aspath_parse (s, len, use32bit, 0);
  635. if (s)
  636. stream_free (s);
  637. return as;
  638. }
  639. static void
  640. printbytes (const u_char *bytes, int len)
  641. {
  642. int i = 0;
  643. while (i < len)
  644. {
  645. if (i % 2)
  646. printf ("%02hhx%s", bytes[i], " ");
  647. else
  648. printf ("0x%02hhx", bytes[i]);
  649. i++;
  650. }
  651. printf ("\n");
  652. }
  653. /* validate the given aspath */
  654. static int
  655. validate (struct aspath *as, const struct test_spec *sp)
  656. {
  657. size_t bytes, bytes4;
  658. int fails = 0;
  659. const u_char *out;
  660. static struct stream *s;
  661. struct aspath *asinout, *asconfeddel, *asstr, *as4;
  662. out = aspath_snmp_pathseg (as, &bytes);
  663. asinout = make_aspath (out, bytes, 0);
  664. /* Excercise AS4 parsing a bit, with a dogfood test */
  665. if (!s)
  666. s = stream_new (4096);
  667. bytes4 = aspath_put (s, as, 1);
  668. as4 = make_aspath (STREAM_DATA(s), bytes4, 1);
  669. asstr = aspath_str2aspath (sp->shouldbe);
  670. asconfeddel = aspath_delete_confed_seq (aspath_dup (asinout));
  671. printf ("got: %s\n", aspath_print(as));
  672. /* the parsed path should match the specified 'shouldbe' string.
  673. * We should pass the "eat our own dog food" test, be able to output
  674. * this path and then input it again. Ie the path resulting from:
  675. *
  676. * aspath_parse(aspath_put(as))
  677. *
  678. * should:
  679. *
  680. * - also match the specified 'shouldbe' value
  681. * - hash to same value as original path
  682. * - have same hops and confed counts as original, and as the
  683. * the specified counts
  684. *
  685. * aspath_str2aspath() and shouldbe should match
  686. *
  687. * We do the same for:
  688. *
  689. * aspath_parse(aspath_put(as,USE32BIT))
  690. *
  691. * Confederation related tests:
  692. * - aspath_delete_confed_seq(aspath) should match shouldbe_confed
  693. * - aspath_delete_confed_seq should be idempotent.
  694. */
  695. if (strcmp(aspath_print (as), sp->shouldbe)
  696. /* hash validation */
  697. || (aspath_key_make (as) != aspath_key_make (asinout))
  698. /* by string */
  699. || strcmp(aspath_print (asinout), sp->shouldbe)
  700. /* By 4-byte parsing */
  701. || strcmp(aspath_print (as4), sp->shouldbe)
  702. /* by various path counts */
  703. || (aspath_count_hops (as) != sp->hops)
  704. || (aspath_count_confeds (as) != sp->confeds)
  705. || (aspath_count_hops (asinout) != sp->hops)
  706. || (aspath_count_confeds (asinout) != sp->confeds))
  707. {
  708. failed++;
  709. fails++;
  710. printf ("shouldbe:\n%s\n", sp->shouldbe);
  711. printf ("as4:\n%s\n", aspath_print (as4));
  712. printf ("hash keys: in: %d out->in: %d\n",
  713. aspath_key_make (as), aspath_key_make (asinout));
  714. printf ("hops: %d, counted %d %d\n", sp->hops,
  715. aspath_count_hops (as),
  716. aspath_count_hops (asinout) );
  717. printf ("confeds: %d, counted %d %d\n", sp->confeds,
  718. aspath_count_confeds (as),
  719. aspath_count_confeds (asinout));
  720. printf ("out->in:\n%s\nbytes: ", aspath_print(asinout));
  721. printbytes (out, bytes);
  722. }
  723. /* basic confed related tests */
  724. if ((aspath_print (asconfeddel) == NULL
  725. && sp->shouldbe_delete_confed != NULL)
  726. || (aspath_print (asconfeddel) != NULL
  727. && sp->shouldbe_delete_confed == NULL)
  728. || strcmp(aspath_print (asconfeddel), sp->shouldbe_delete_confed)
  729. /* delete_confed_seq should be idempotent */
  730. || (aspath_key_make (asconfeddel)
  731. != aspath_key_make (aspath_delete_confed_seq (asconfeddel))))
  732. {
  733. failed++;
  734. fails++;
  735. printf ("confed_del: %s\n", aspath_print (asconfeddel));
  736. printf ("should be: %s\n", sp->shouldbe_delete_confed);
  737. }
  738. /* aspath_str2aspath test */
  739. if ((aspath_print (asstr) == NULL && sp->shouldbe != NULL)
  740. || (aspath_print (asstr) != NULL && sp->shouldbe == NULL)
  741. || strcmp(aspath_print (asstr), sp->shouldbe))
  742. {
  743. failed++;
  744. fails++;
  745. printf ("asstr: %s\n", aspath_print (asstr));
  746. }
  747. /* loop, private and first as checks */
  748. if ((sp->does_loop && aspath_loop_check (as, sp->does_loop) == 0)
  749. || (sp->doesnt_loop && aspath_loop_check (as, sp->doesnt_loop) != 0)
  750. || (aspath_private_as_check (as) != sp->private_as)
  751. || (aspath_firstas_check (as,sp->first)
  752. && sp->first == 0))
  753. {
  754. failed++;
  755. fails++;
  756. printf ("firstas: %d, got %d\n", sp->first,
  757. aspath_firstas_check (as,sp->first));
  758. printf ("loop does: %d %d, doesnt: %d %d\n",
  759. sp->does_loop, aspath_loop_check (as, sp->does_loop),
  760. sp->doesnt_loop, aspath_loop_check (as, sp->doesnt_loop));
  761. printf ("private check: %d %d\n", sp->private_as,
  762. aspath_private_as_check (as));
  763. }
  764. aspath_unintern (asinout);
  765. aspath_unintern (as4);
  766. aspath_free (asconfeddel);
  767. aspath_free (asstr);
  768. stream_reset (s);
  769. return fails;
  770. }
  771. static void
  772. empty_get_test ()
  773. {
  774. struct aspath *as = aspath_empty_get ();
  775. struct test_spec sp = { "", "", 0, 0, 0, 0, 0, 0 };
  776. printf ("empty_get_test, as: %s\n",aspath_print (as));
  777. if (!validate (as, &sp))
  778. printf ("%s\n", OK);
  779. else
  780. printf ("%s!\n", FAILED);
  781. printf ("\n");
  782. aspath_free (as);
  783. }
  784. /* basic parsing test */
  785. static void
  786. parse_test (struct test_segment *t)
  787. {
  788. struct aspath *asp;
  789. printf ("%s: %s\n", t->name, t->desc);
  790. asp = make_aspath (t->asdata, t->len, 0);
  791. printf ("aspath: %s\nvalidating...:\n", aspath_print (asp));
  792. if (!validate (asp, &t->sp))
  793. printf (OK "\n");
  794. else
  795. printf (FAILED "\n");
  796. printf ("\n");
  797. aspath_unintern (asp);
  798. }
  799. /* prepend testing */
  800. static void
  801. prepend_test (struct tests *t)
  802. {
  803. struct aspath *asp1, *asp2, *ascratch;
  804. printf ("prepend %s: %s\n", t->test1->name, t->test1->desc);
  805. printf ("to %s: %s\n", t->test2->name, t->test2->desc);
  806. asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
  807. asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
  808. ascratch = aspath_dup (asp2);
  809. aspath_unintern (asp2);
  810. asp2 = aspath_prepend (asp1, ascratch);
  811. printf ("aspath: %s\n", aspath_print (asp2));
  812. if (!validate (asp2, &t->sp))
  813. printf ("%s\n", OK);
  814. else
  815. printf ("%s!\n", FAILED);
  816. printf ("\n");
  817. aspath_unintern (asp1);
  818. aspath_free (asp2);
  819. }
  820. /* empty-prepend testing */
  821. static void
  822. empty_prepend_test (struct test_segment *t)
  823. {
  824. struct aspath *asp1, *asp2, *ascratch;
  825. printf ("empty prepend %s: %s\n", t->name, t->desc);
  826. asp1 = make_aspath (t->asdata, t->len, 0);
  827. asp2 = aspath_empty ();
  828. ascratch = aspath_dup (asp2);
  829. aspath_unintern (asp2);
  830. asp2 = aspath_prepend (asp1, ascratch);
  831. printf ("aspath: %s\n", aspath_print (asp2));
  832. if (!validate (asp2, &t->sp))
  833. printf (OK "\n");
  834. else
  835. printf (FAILED "!\n");
  836. printf ("\n");
  837. aspath_unintern (asp1);
  838. aspath_free (asp2);
  839. }
  840. /* as2+as4 reconciliation testing */
  841. static void
  842. as4_reconcile_test (struct tests *t)
  843. {
  844. struct aspath *asp1, *asp2, *ascratch;
  845. printf ("reconciling %s:\n %s\n", t->test1->name, t->test1->desc);
  846. printf ("with %s:\n %s\n", t->test2->name, t->test2->desc);
  847. asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
  848. asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
  849. ascratch = aspath_reconcile_as4 (asp1, asp2);
  850. if (!validate (ascratch, &t->sp))
  851. printf (OK "\n");
  852. else
  853. printf (FAILED "!\n");
  854. printf ("\n");
  855. aspath_unintern (asp1);
  856. aspath_unintern (asp2);
  857. aspath_free (ascratch);
  858. }
  859. /* aggregation testing */
  860. static void
  861. aggregate_test (struct tests *t)
  862. {
  863. struct aspath *asp1, *asp2, *ascratch;
  864. printf ("aggregate %s: %s\n", t->test1->name, t->test1->desc);
  865. printf ("with %s: %s\n", t->test2->name, t->test2->desc);
  866. asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
  867. asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
  868. ascratch = aspath_aggregate (asp1, asp2);
  869. if (!validate (ascratch, &t->sp))
  870. printf (OK "\n");
  871. else
  872. printf (FAILED "!\n");
  873. printf ("\n");
  874. aspath_unintern (asp1);
  875. aspath_unintern (asp2);
  876. aspath_free (ascratch);
  877. /* aspath_unintern (ascratch);*/
  878. }
  879. /* cmp_left tests */
  880. static void
  881. cmp_test ()
  882. {
  883. unsigned int i;
  884. #define CMP_TESTS_MAX \
  885. (sizeof(left_compare) / sizeof (struct compare_tests))
  886. for (i = 0; i < CMP_TESTS_MAX; i++)
  887. {
  888. struct test_segment *t1 = &test_segments[left_compare[i].test_index1];
  889. struct test_segment *t2 = &test_segments[left_compare[i].test_index2];
  890. struct aspath *asp1, *asp2;
  891. printf ("left cmp %s: %s\n", t1->name, t1->desc);
  892. printf ("and %s: %s\n", t2->name, t2->desc);
  893. asp1 = make_aspath (t1->asdata, t1->len, 0);
  894. asp2 = make_aspath (t2->asdata, t2->len, 0);
  895. if (aspath_cmp_left (asp1, asp2) != left_compare[i].shouldbe_cmp
  896. || aspath_cmp_left (asp2, asp1) != left_compare[i].shouldbe_cmp
  897. || aspath_cmp_left_confed (asp1, asp2)
  898. != left_compare[i].shouldbe_confed
  899. || aspath_cmp_left_confed (asp2, asp1)
  900. != left_compare[i].shouldbe_confed)
  901. {
  902. failed++;
  903. printf (FAILED "\n");
  904. printf ("result should be: cmp: %d, confed: %d\n",
  905. left_compare[i].shouldbe_cmp,
  906. left_compare[i].shouldbe_confed);
  907. printf ("got: cmp %d, cmp_confed: %d\n",
  908. aspath_cmp_left (asp1, asp2),
  909. aspath_cmp_left_confed (asp1, asp2));
  910. printf("path1: %s\npath2: %s\n", aspath_print (asp1),
  911. aspath_print (asp2));
  912. }
  913. else
  914. printf (OK "\n");
  915. printf ("\n");
  916. aspath_unintern (asp1);
  917. aspath_unintern (asp2);
  918. }
  919. }
  920. int
  921. main (void)
  922. {
  923. int i = 0;
  924. aspath_init();
  925. while (test_segments[i].name)
  926. {
  927. parse_test (&test_segments[i]);
  928. empty_prepend_test (&test_segments[i++]);
  929. }
  930. i = 0;
  931. while (prepend_tests[i].test1)
  932. prepend_test (&prepend_tests[i++]);
  933. i = 0;
  934. while (aggregate_tests[i].test1)
  935. aggregate_test (&aggregate_tests[i++]);
  936. i = 0;
  937. while (reconcile_tests[i].test1)
  938. as4_reconcile_test (&reconcile_tests[i++]);
  939. i = 0;
  940. cmp_test();
  941. i = 0;
  942. empty_get_test();
  943. printf ("failures: %d\n", failed);
  944. printf ("aspath count: %ld\n", aspath_count());
  945. return (failed + aspath_count());
  946. }