aspath_test.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. /*
  2. * Copyright (C) 2005 Sun Microsystems, Inc.
  3. *
  4. * This file is part of Quagga.
  5. *
  6. * Quagga is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * Quagga is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Quagga; see the file COPYING. If not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. */
  21. #include <zebra.h>
  22. #include "vty.h"
  23. #include "stream.h"
  24. #include "privs.h"
  25. #include "bgpd/bgpd.h"
  26. #include "bgpd/bgp_aspath.h"
  27. #include "bgpd/bgp_attr.h"
  28. #define VT100_RESET "\x1b[0m"
  29. #define VT100_RED "\x1b[31m"
  30. #define VT100_GREEN "\x1b[32m"
  31. #define VT100_YELLOW "\x1b[33m"
  32. #define OK VT100_GREEN "OK" VT100_RESET
  33. #define FAILED VT100_RED "failed" VT100_RESET
  34. /* need these to link in libbgp */
  35. struct zebra_privs_t *bgpd_privs = NULL;
  36. struct thread_master *master = NULL;
  37. static int failed = 0;
  38. /* specification for a test - what the results should be */
  39. struct test_spec
  40. {
  41. const char *shouldbe; /* the string the path should parse to */
  42. const char *shouldbe_delete_confed; /* ditto, but once confeds are deleted */
  43. const unsigned int hops; /* aspath_count_hops result */
  44. const unsigned int confeds; /* aspath_count_confeds */
  45. const int private_as; /* whether the private_as check should pass or fail */
  46. #define NOT_ALL_PRIVATE 0
  47. #define ALL_PRIVATE 1
  48. const as_t does_loop; /* an ASN which should trigger loop-check */
  49. const as_t doesnt_loop; /* one which should not */
  50. const as_t first; /* the first ASN, if there is one */
  51. #define NULL_ASN 0
  52. };
  53. /* test segments to parse and validate, and use for other tests */
  54. static struct test_segment {
  55. const char *name;
  56. const char *desc;
  57. const u_char asdata[1024];
  58. int len;
  59. struct test_spec sp;
  60. } test_segments [] =
  61. {
  62. { /* 0 */
  63. "seq1",
  64. "seq(8466,3,52737,4096)",
  65. { 0x2,0x4, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00 },
  66. 10,
  67. { "8466 3 52737 4096",
  68. "8466 3 52737 4096",
  69. 4, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  70. },
  71. { /* 1 */
  72. "seq2",
  73. "seq(8722) seq(4)",
  74. { 0x2,0x1, 0x22,0x12,
  75. 0x2,0x1, 0x00,0x04 },
  76. 8,
  77. { "8722 4",
  78. "8722 4",
  79. 2, 0, NOT_ALL_PRIVATE, 4, 5, 8722, },
  80. },
  81. { /* 2 */
  82. "seq3",
  83. "seq(8466,3,52737,4096,8722,4)",
  84. { 0x2,0x6, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00,
  85. 0x22,0x12, 0x00,0x04},
  86. 14,
  87. { "8466 3 52737 4096 8722 4",
  88. "8466 3 52737 4096 8722 4",
  89. 6, 0, NOT_ALL_PRIVATE, 3, 5, 8466 },
  90. },
  91. { /* 3 */
  92. "seqset",
  93. "seq(8482,51457) set(5204)",
  94. { 0x2,0x2, 0x21,0x22, 0xc9,0x01,
  95. 0x1,0x1, 0x14,0x54 },
  96. 10,
  97. { "8482 51457 {5204}",
  98. "8482 51457 {5204}",
  99. 3, 0, NOT_ALL_PRIVATE, 5204, 51456, 8482},
  100. },
  101. { /* 4 */
  102. "seqset2",
  103. "seq(8467, 59649) set(4196,48658) set(17322,30745)",
  104. { 0x2,0x2, 0x21,0x13, 0xe9,0x01,
  105. 0x1,0x2, 0x10,0x64, 0xbe,0x12,
  106. 0x1,0x2, 0x43,0xaa, 0x78,0x19 },
  107. 18,
  108. { "8467 59649 {4196,48658} {17322,30745}",
  109. "8467 59649 {4196,48658} {17322,30745}",
  110. 4, 0, NOT_ALL_PRIVATE, 48658, 1, 8467},
  111. },
  112. { /* 5 */
  113. "multi",
  114. "seq(6435,59408,21665) set(2457,61697,4369), seq(1842,41590,51793)",
  115. { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
  116. 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
  117. 0x2,0x3, 0x07,0x32, 0xa2,0x76, 0xca,0x51 },
  118. 24,
  119. { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
  120. "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
  121. 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
  122. },
  123. { /* 6 */
  124. "confed",
  125. "confseq(123,456,789)",
  126. { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15 },
  127. 8,
  128. { "(123 456 789)",
  129. "",
  130. 0, 3, NOT_ALL_PRIVATE, 789, 1, NULL_ASN },
  131. },
  132. { /* 7 */
  133. "confed2",
  134. "confseq(123,456,789) confseq(111,222)",
  135. { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
  136. 0x3,0x2, 0x00,0x6f, 0x00,0xde },
  137. 14,
  138. { "(123 456 789) (111 222)",
  139. "",
  140. 0, 5, NOT_ALL_PRIVATE, 111, 1, NULL_ASN },
  141. },
  142. { /* 8 */
  143. "confset",
  144. "confset(456,123,789)",
  145. { 0x4,0x3, 0x01,0xc8, 0x00,0x7b, 0x03,0x15 },
  146. 8,
  147. { "[123,456,789]",
  148. "[123,456,789]",
  149. 0, 1, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
  150. },
  151. { /* 9 */
  152. "confmulti",
  153. "confseq(123,456,789) confset(222,111) seq(8722) set(4196,48658)",
  154. { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
  155. 0x4,0x2, 0x00,0xde, 0x00,0x6f,
  156. 0x2,0x1, 0x22,0x12,
  157. 0x1,0x2, 0x10,0x64, 0xbe,0x12 },
  158. 24,
  159. { "(123 456 789) [111,222] 8722 {4196,48658}",
  160. "8722 {4196,48658}",
  161. 2, 4, NOT_ALL_PRIVATE, 123, 1, NULL_ASN },
  162. },
  163. { /* 10 */
  164. "seq4",
  165. "seq(8466,2,52737,4096,8722,4)",
  166. { 0x2,0x6, 0x21,0x12, 0x00,0x02, 0xce,0x01, 0x10,0x00,
  167. 0x22,0x12, 0x00,0x04},
  168. 14,
  169. { "8466 2 52737 4096 8722 4",
  170. "8466 2 52737 4096 8722 4",
  171. 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
  172. },
  173. { /* 11 */
  174. "tripleseq1",
  175. "seq(8466,2,52737) seq(4096,8722,4) seq(8722)",
  176. { 0x2,0x3, 0x21,0x12, 0x00,0x02, 0xce,0x01,
  177. 0x2,0x3, 0x10,0x00, 0x22,0x12, 0x00,0x04,
  178. 0x2,0x1, 0x22,0x12},
  179. 20,
  180. { "8466 2 52737 4096 8722 4 8722",
  181. "8466 2 52737 4096 8722 4 8722",
  182. 7, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
  183. },
  184. { /* 12 */
  185. "someprivate",
  186. "seq(8466,64512,52737,65535)",
  187. { 0x2,0x4, 0x21,0x12, 0xfc,0x00, 0xce,0x01, 0xff,0xff },
  188. 10,
  189. { "8466 64512 52737 65535",
  190. "8466 64512 52737 65535",
  191. 4, 0, NOT_ALL_PRIVATE, 65535, 4, 8466 },
  192. },
  193. { /* 13 */
  194. "allprivate",
  195. "seq(65534,64512,64513,65535)",
  196. { 0x2,0x4, 0xff,0xfe, 0xfc,0x00, 0xfc,0x01, 0xff,0xff },
  197. 10,
  198. { "65534 64512 64513 65535",
  199. "65534 64512 64513 65535",
  200. 4, 0, ALL_PRIVATE, 65534, 4, 65534 },
  201. },
  202. { /* 14 */
  203. "long",
  204. "seq(8466,3,52737,4096,34285,<repeated 49 more times>)",
  205. { 0x2,0xfa, 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. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  236. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  237. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  238. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  239. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  240. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  241. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  242. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  243. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  244. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  245. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  246. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  247. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  248. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  249. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  250. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  251. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  252. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  253. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed,
  254. 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x85,0xed, },
  255. 502,
  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. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  287. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  288. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  289. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  290. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  291. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  292. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  293. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  294. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  295. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  296. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  297. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  298. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  299. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  300. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  301. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  302. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  303. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  304. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  305. "8466 3 52737 4096 34285 8466 3 52737 4096 34285",
  306. 250, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  307. },
  308. { /* 15 */
  309. "seq1extra",
  310. "seq(8466,3,52737,4096,3456)",
  311. { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
  312. 12,
  313. { "8466 3 52737 4096 3456",
  314. "8466 3 52737 4096 3456",
  315. 5, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  316. },
  317. { /* 16 */
  318. "empty",
  319. "<empty>",
  320. {},
  321. 0,
  322. { "", "", 0, 0, 0, 0, 0, 0 },
  323. },
  324. { /* 17 */
  325. "redundantset",
  326. "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153)",
  327. { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
  328. 0x1,0x4, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9 },
  329. 22,
  330. {
  331. /* We shouldn't ever /generate/ such paths. However, we should
  332. * cope with them fine.
  333. */
  334. "8466 3 52737 4096 3456 {7099,8153}",
  335. "8466 3 52737 4096 3456 {7099,8153}",
  336. 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  337. },
  338. { /* 18 */
  339. "reconcile_lead_asp",
  340. "seq(6435,59408,21665) set(23456,23456,23456), seq(23456,23456,23456)",
  341. { 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
  342. 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
  343. 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
  344. 24,
  345. { "6435 59408 21665 {23456} 23456 23456 23456",
  346. "6435 59408 21665 {23456} 23456 23456 23456",
  347. 7, 0, NOT_ALL_PRIVATE, 23456, 1, 6435 },
  348. },
  349. { /* 19 */
  350. "reconcile_new_asp",
  351. "set(2457,61697,4369), seq(1842,41591,51793)",
  352. {
  353. 0x1,0x3, 0x09,0x99, 0xf1,0x01, 0x11,0x11,
  354. 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51 },
  355. 16,
  356. { "{2457,4369,61697} 1842 41591 51793",
  357. "{2457,4369,61697} 1842 41591 51793",
  358. 4, 0, NOT_ALL_PRIVATE, 51793, 1, 2457 },
  359. },
  360. { /* 20 */
  361. "reconcile_confed",
  362. "confseq(123,456,789) confset(456,124,788) seq(6435,59408,21665)"
  363. " set(23456,23456,23456), seq(23456,23456,23456)",
  364. { 0x3,0x3, 0x00,0x7b, 0x01,0xc8, 0x03,0x15,
  365. 0x4,0x3, 0x01,0xc8, 0x00,0x7c, 0x03,0x14,
  366. 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1,
  367. 0x1,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
  368. 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0 },
  369. 40,
  370. { "(123 456 789) [124,456,788] 6435 59408 21665"
  371. " {23456} 23456 23456 23456",
  372. "6435 59408 21665 {23456} 23456 23456 23456",
  373. 7, 4, NOT_ALL_PRIVATE, 23456, 1, 6435 },
  374. },
  375. { /* 21 */
  376. "reconcile_start_trans",
  377. "seq(23456,23456,23456) seq(6435,59408,21665)",
  378. { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
  379. 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
  380. 16,
  381. { "23456 23456 23456 6435 59408 21665",
  382. "23456 23456 23456 6435 59408 21665",
  383. 6, 0, NOT_ALL_PRIVATE, 21665, 1, 23456 },
  384. },
  385. { /* 22 */
  386. "reconcile_start_trans4",
  387. "seq(1842,41591,51793) seq(6435,59408,21665)",
  388. { 0x2,0x3, 0x07,0x32, 0xa2,0x77, 0xca,0x51,
  389. 0x2,0x3, 0x19,0x23, 0xe8,0x10, 0x54,0xa1, },
  390. 16,
  391. { "1842 41591 51793 6435 59408 21665",
  392. "1842 41591 51793 6435 59408 21665",
  393. 6, 0, NOT_ALL_PRIVATE, 41591, 1, 1842 },
  394. },
  395. { /* 23 */
  396. "reconcile_start_trans_error",
  397. "seq(23456,23456,23456) seq(6435,59408)",
  398. { 0x2,0x3, 0x5b,0xa0, 0x5b,0xa0, 0x5b,0xa0,
  399. 0x2,0x2, 0x19,0x23, 0xe8,0x10, },
  400. 14,
  401. { "23456 23456 23456 6435 59408",
  402. "23456 23456 23456 6435 59408",
  403. 5, 0, NOT_ALL_PRIVATE, 59408, 1, 23456 },
  404. },
  405. { /* 24 */
  406. "redundantset2",
  407. "seq(8466,3,52737,4096,3456) set(7099,8153,8153,8153,7099)",
  408. { 0x2,0x5, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80,
  409. 0x1,0x5, 0x1b,0xbb, 0x1f,0xd9, 0x1f,0xd9, 0x1f,0xd9, 0x1b,0xbb,},
  410. 24,
  411. {
  412. /* We should weed out duplicate set members. */
  413. "8466 3 52737 4096 3456 {7099,8153}",
  414. "8466 3 52737 4096 3456 {7099,8153}",
  415. 6, 0, NOT_ALL_PRIVATE, 4096, 4, 8466 },
  416. },
  417. { /* 25 */
  418. "zero-size overflow",
  419. "#ASNs = 0, data = seq(8466 3 52737 4096 3456)",
  420. { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01, 0x10,0x00, 0x0d,0x80 },
  421. 12,
  422. { NULL, NULL,
  423. 0, 0, 0, 0, 0, 0 },
  424. },
  425. { /* 26 */
  426. "zero-size overflow + valid segment",
  427. "seq(#AS=0:8466 3 52737),seq(4096 3456)",
  428. { 0x2,0x0, 0x21,0x12, 0x00,0x03, 0xce,0x01,
  429. 0x2,0x2, 0x10,0x00, 0x0d,0x80 },
  430. 14
  431. ,
  432. { NULL, NULL,
  433. 0, 0, 0, 0, 0, 0 },
  434. },
  435. { /* 27 */
  436. "invalid segment type",
  437. "type=8(4096 3456)",
  438. { 0x8,0x2, 0x10,0x00, 0x0d,0x80 },
  439. 14
  440. ,
  441. { NULL, NULL,
  442. 0, 0, 0, 0, 0, 0 },
  443. }, { NULL, NULL, {0}, 0, { NULL, 0, 0 } }
  444. };
  445. /* */
  446. static struct aspath_tests {
  447. const char *desc;
  448. const struct test_segment *segment;
  449. const char *shouldbe; /* String it should evaluate to */
  450. const enum as4 { AS4_DATA, AS2_DATA }
  451. as4; /* whether data should be as4 or not (ie as2) */
  452. const int result; /* expected result for bgp_attr_parse */
  453. const int cap; /* capabilities to set for peer */
  454. const char attrheader [1024];
  455. size_t len;
  456. const struct test_segment *old_segment;
  457. } aspath_tests [] =
  458. {
  459. /* 0 */
  460. {
  461. "basic test",
  462. &test_segments[0],
  463. "8466 3 52737 4096",
  464. AS2_DATA, 0,
  465. 0,
  466. { BGP_ATTR_FLAG_TRANS,
  467. BGP_ATTR_AS_PATH,
  468. 10,
  469. },
  470. 3,
  471. },
  472. /* 1 */
  473. {
  474. "length too short",
  475. &test_segments[0],
  476. "8466 3 52737 4096",
  477. AS2_DATA, -1,
  478. 0,
  479. { BGP_ATTR_FLAG_TRANS,
  480. BGP_ATTR_AS_PATH,
  481. 8,
  482. },
  483. 3,
  484. },
  485. /* 2 */
  486. {
  487. "length too long",
  488. &test_segments[0],
  489. "8466 3 52737 4096",
  490. AS2_DATA, -1,
  491. 0,
  492. { BGP_ATTR_FLAG_TRANS,
  493. BGP_ATTR_AS_PATH,
  494. 12,
  495. },
  496. 3,
  497. },
  498. /* 3 */
  499. {
  500. "incorrect flag",
  501. &test_segments[0],
  502. "8466 3 52737 4096",
  503. AS2_DATA, -1,
  504. 0,
  505. { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
  506. BGP_ATTR_AS_PATH,
  507. 10,
  508. },
  509. 3,
  510. },
  511. /* 4 */
  512. {
  513. "as4_path, with as2 format data",
  514. &test_segments[0],
  515. "8466 3 52737 4096",
  516. AS2_DATA, -1,
  517. 0,
  518. { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
  519. BGP_ATTR_AS4_PATH,
  520. 10,
  521. },
  522. 3,
  523. },
  524. /* 5 */
  525. {
  526. "as4, with incorrect attr length",
  527. &test_segments[0],
  528. "8466 3 52737 4096",
  529. AS4_DATA, -1,
  530. PEER_CAP_AS4_RCV,
  531. { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
  532. BGP_ATTR_AS4_PATH,
  533. 10,
  534. },
  535. 3,
  536. },
  537. /* 6 */
  538. {
  539. "basic 4-byte as-path",
  540. &test_segments[0],
  541. "8466 3 52737 4096",
  542. AS4_DATA, 0,
  543. PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
  544. { BGP_ATTR_FLAG_TRANS,
  545. BGP_ATTR_AS_PATH,
  546. 18,
  547. },
  548. 3,
  549. },
  550. /* 7 */
  551. {
  552. "4b AS_PATH: too short",
  553. &test_segments[0],
  554. "8466 3 52737 4096",
  555. AS4_DATA, -1,
  556. PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
  557. { BGP_ATTR_FLAG_TRANS,
  558. BGP_ATTR_AS_PATH,
  559. 16,
  560. },
  561. 3,
  562. },
  563. /* 8 */
  564. {
  565. "4b AS_PATH: too long",
  566. &test_segments[0],
  567. "8466 3 52737 4096",
  568. AS4_DATA, -1,
  569. PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
  570. { BGP_ATTR_FLAG_TRANS,
  571. BGP_ATTR_AS_PATH,
  572. 20,
  573. },
  574. 3,
  575. },
  576. /* 9 */
  577. {
  578. "4b AS_PATH: too long2",
  579. &test_segments[0],
  580. "8466 3 52737 4096",
  581. AS4_DATA, -1,
  582. PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
  583. { BGP_ATTR_FLAG_TRANS,
  584. BGP_ATTR_AS_PATH,
  585. 22,
  586. },
  587. 3,
  588. },
  589. /* 10 */
  590. {
  591. "4b AS_PATH: bad flags",
  592. &test_segments[0],
  593. "8466 3 52737 4096",
  594. AS4_DATA, -1,
  595. PEER_CAP_AS4_RCV|PEER_CAP_AS4_ADV,
  596. { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
  597. BGP_ATTR_AS_PATH,
  598. 18,
  599. },
  600. 3,
  601. },
  602. /* 11 */
  603. {
  604. "4b AS4_PATH w/o AS_PATH",
  605. &test_segments[6],
  606. NULL,
  607. AS4_DATA, 0,
  608. PEER_CAP_AS4_ADV,
  609. { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
  610. BGP_ATTR_AS4_PATH,
  611. 14,
  612. },
  613. 3,
  614. },
  615. /* 12 */
  616. {
  617. "4b AS4_PATH: confed",
  618. &test_segments[6],
  619. "8466 3 52737 4096 (123 456 789)",
  620. AS4_DATA, 0,
  621. PEER_CAP_AS4_ADV,
  622. { BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL,
  623. BGP_ATTR_AS4_PATH,
  624. 14,
  625. },
  626. 3,
  627. &test_segments[0],
  628. },
  629. { NULL, NULL, NULL, 0, 0, 0, { 0 }, 0 },
  630. };
  631. /* prepending tests */
  632. static struct tests {
  633. const struct test_segment *test1;
  634. const struct test_segment *test2;
  635. struct test_spec sp;
  636. } prepend_tests[] =
  637. {
  638. /* 0 */
  639. { &test_segments[0], &test_segments[1],
  640. { "8466 3 52737 4096 8722 4",
  641. "8466 3 52737 4096 8722 4",
  642. 6, 0, NOT_ALL_PRIVATE, 4096, 1, 8466 },
  643. },
  644. /* 1 */
  645. { &test_segments[1], &test_segments[3],
  646. { "8722 4 8482 51457 {5204}",
  647. "8722 4 8482 51457 {5204}",
  648. 5, 0, NOT_ALL_PRIVATE, 5204, 1, 8722 }
  649. },
  650. /* 2 */
  651. { &test_segments[3], &test_segments[4],
  652. { "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
  653. "8482 51457 {5204} 8467 59649 {4196,48658} {17322,30745}",
  654. 7, 0, NOT_ALL_PRIVATE, 5204, 1, 8482 },
  655. },
  656. /* 3 */
  657. { &test_segments[4], &test_segments[5],
  658. { "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
  659. " {2457,4369,61697} 1842 41590 51793",
  660. "8467 59649 {4196,48658} {17322,30745} 6435 59408 21665"
  661. " {2457,4369,61697} 1842 41590 51793",
  662. 11, 0, NOT_ALL_PRIVATE, 61697, 1, 8467 }
  663. },
  664. /* 4 */
  665. { &test_segments[5], &test_segments[6],
  666. { "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
  667. "6435 59408 21665 {2457,4369,61697} 1842 41590 51793",
  668. 7, 0, NOT_ALL_PRIVATE, 1842, 1, 6435 },
  669. },
  670. /* 5 */
  671. { &test_segments[6], &test_segments[7],
  672. { "(123 456 789) (123 456 789) (111 222)",
  673. "",
  674. 0, 8, NOT_ALL_PRIVATE, 111, 1, 0 }
  675. },
  676. { &test_segments[7], &test_segments[8],
  677. { "(123 456 789) (111 222) [123,456,789]",
  678. "",
  679. 0, 6, NOT_ALL_PRIVATE, 111, 1, 0 }
  680. },
  681. { &test_segments[8], &test_segments[9],
  682. { "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
  683. "[123,456,789] (123 456 789) [111,222] 8722 {4196,48658}",
  684. 2, 5, NOT_ALL_PRIVATE, 456, 1, NULL_ASN },
  685. },
  686. { &test_segments[9], &test_segments[8],
  687. { "(123 456 789) [111,222] 8722 {4196,48658} [123,456,789]",
  688. "8722 {4196,48658} [123,456,789]",
  689. 2, 5, NOT_ALL_PRIVATE, 48658, 1, NULL_ASN },
  690. },
  691. { &test_segments[14], &test_segments[11],
  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 3 52737 4096 34285 8466 3 52737 4096 34285 "
  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 2 52737 4096 8722 4 8722",
  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 3 52737 4096 34285 8466 3 52737 4096 34285 "
  724. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  725. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  726. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  727. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  728. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  729. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  730. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  731. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  732. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  733. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  734. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  735. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  736. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  737. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  738. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  739. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  740. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  741. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  742. "8466 3 52737 4096 34285 8466 3 52737 4096 34285 "
  743. "8466 2 52737 4096 8722 4 8722",
  744. 257, 0, NOT_ALL_PRIVATE, 4096, 1000, 8466 },
  745. },
  746. { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
  747. };
  748. struct tests reconcile_tests[] =
  749. {
  750. { &test_segments[18], &test_segments[19],
  751. { "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
  752. "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
  753. 7, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
  754. },
  755. { &test_segments[19], &test_segments[18],
  756. /* AS_PATH (19) has more hops than NEW_AS_PATH,
  757. * so just AS_PATH should be used (though, this practice
  758. * is bad imho).
  759. */
  760. { "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
  761. "{2457,4369,61697} 1842 41591 51793 6435 59408 21665 {23456} 23456 23456 23456",
  762. 11, 0, NOT_ALL_PRIVATE, 51793, 1, 6435 },
  763. },
  764. { &test_segments[20], &test_segments[19],
  765. { "(123 456 789) [124,456,788] 6435 59408 21665"
  766. " {2457,4369,61697} 1842 41591 51793",
  767. "6435 59408 21665 {2457,4369,61697} 1842 41591 51793",
  768. 7, 4, NOT_ALL_PRIVATE, 51793, 1, 6435 },
  769. },
  770. { &test_segments[21], &test_segments[22],
  771. { "1842 41591 51793 6435 59408 21665",
  772. "1842 41591 51793 6435 59408 21665",
  773. 6, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
  774. },
  775. { &test_segments[23], &test_segments[22],
  776. { "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
  777. "23456 23456 23456 6435 59408 1842 41591 51793 6435 59408 21665",
  778. 11, 0, NOT_ALL_PRIVATE, 51793, 1, 1842 },
  779. },
  780. { NULL, NULL, { NULL, 0, 0, 0, 0, 0, 0, } },
  781. };
  782. struct tests aggregate_tests[] =
  783. {
  784. { &test_segments[0], &test_segments[2],
  785. { "8466 3 52737 4096 {4,8722}",
  786. "8466 3 52737 4096 {4,8722}",
  787. 5, 0, NOT_ALL_PRIVATE, 4, 1, 8466 },
  788. },
  789. { &test_segments[2], &test_segments[0],
  790. { "8466 3 52737 4096 {4,8722}",
  791. "8466 3 52737 4096 {4,8722}",
  792. 5, 0, NOT_ALL_PRIVATE, 8722, 1, 8466 },
  793. },
  794. { &test_segments[2], &test_segments[10],
  795. { "8466 {2,3,4,4096,8722,52737}",
  796. "8466 {2,3,4,4096,8722,52737}",
  797. 2, 0, NOT_ALL_PRIVATE, 8722, 5, 8466 },
  798. },
  799. { &test_segments[10], &test_segments[2],
  800. { "8466 {2,3,4,4096,8722,52737}",
  801. "8466 {2,3,4,4096,8722,52737}",
  802. 2, 0, NOT_ALL_PRIVATE, 2, 20000, 8466 },
  803. },
  804. { &test_segments[5], &test_segments[18],
  805. { "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
  806. "6435 59408 21665 {1842,2457,4369,23456,41590,51793,61697}",
  807. 4, 0, NOT_ALL_PRIVATE, 41590, 1, 6435 },
  808. },
  809. { NULL, NULL, { NULL, 0, 0} },
  810. };
  811. struct compare_tests
  812. {
  813. int test_index1;
  814. int test_index2;
  815. #define CMP_RES_YES 1
  816. #define CMP_RES_NO 0
  817. char shouldbe_cmp;
  818. char shouldbe_confed;
  819. } left_compare [] =
  820. {
  821. { 0, 1, CMP_RES_NO, CMP_RES_NO },
  822. { 0, 2, CMP_RES_YES, CMP_RES_NO },
  823. { 0, 11, CMP_RES_YES, CMP_RES_NO },
  824. { 0, 15, CMP_RES_YES, CMP_RES_NO },
  825. { 0, 16, CMP_RES_NO, CMP_RES_NO },
  826. { 1, 11, CMP_RES_NO, CMP_RES_NO },
  827. { 6, 7, CMP_RES_NO, CMP_RES_YES },
  828. { 6, 8, CMP_RES_NO, CMP_RES_NO },
  829. { 7, 8, CMP_RES_NO, CMP_RES_NO },
  830. { 1, 9, CMP_RES_YES, CMP_RES_NO },
  831. { 0, 9, CMP_RES_NO, CMP_RES_NO },
  832. { 3, 9, CMP_RES_NO, CMP_RES_NO },
  833. { 0, 6, CMP_RES_NO, CMP_RES_NO },
  834. { 1, 6, CMP_RES_NO, CMP_RES_NO },
  835. { 0, 8, CMP_RES_NO, CMP_RES_NO },
  836. { 1, 8, CMP_RES_NO, CMP_RES_NO },
  837. { 11, 6, CMP_RES_NO, CMP_RES_NO },
  838. { 11, 7, CMP_RES_NO, CMP_RES_NO },
  839. { 11, 8, CMP_RES_NO, CMP_RES_NO },
  840. { 9, 6, CMP_RES_NO, CMP_RES_YES },
  841. { 9, 7, CMP_RES_NO, CMP_RES_YES },
  842. { 9, 8, CMP_RES_NO, CMP_RES_NO },
  843. };
  844. /* make an aspath from a data stream */
  845. static struct aspath *
  846. make_aspath (const u_char *data, size_t len, int use32bit)
  847. {
  848. struct stream *s = NULL;
  849. struct aspath *as;
  850. if (len)
  851. {
  852. s = stream_new (len);
  853. stream_put (s, data, len);
  854. }
  855. as = aspath_parse (s, len, use32bit);
  856. if (s)
  857. stream_free (s);
  858. return as;
  859. }
  860. static void
  861. printbytes (const u_char *bytes, int len)
  862. {
  863. int i = 0;
  864. while (i < len)
  865. {
  866. if (i % 2)
  867. printf ("%02hhx%s", bytes[i], " ");
  868. else
  869. printf ("0x%02hhx", bytes[i]);
  870. i++;
  871. }
  872. printf ("\n");
  873. }
  874. /* validate the given aspath */
  875. static int
  876. validate (struct aspath *as, const struct test_spec *sp)
  877. {
  878. size_t bytes, bytes4;
  879. int fails = 0;
  880. const u_char *out;
  881. static struct stream *s;
  882. struct aspath *asinout, *asconfeddel, *asstr, *as4;
  883. if (as == NULL && sp->shouldbe == NULL)
  884. {
  885. printf ("Correctly failed to parse\n");
  886. return fails;
  887. }
  888. out = aspath_snmp_pathseg (as, &bytes);
  889. asinout = make_aspath (out, bytes, 0);
  890. /* Excercise AS4 parsing a bit, with a dogfood test */
  891. if (!s)
  892. s = stream_new (4096);
  893. bytes4 = aspath_put (s, as, 1);
  894. as4 = make_aspath (STREAM_DATA(s), bytes4, 1);
  895. asstr = aspath_str2aspath (sp->shouldbe);
  896. asconfeddel = aspath_delete_confed_seq (aspath_dup (asinout));
  897. printf ("got: %s\n", aspath_print(as));
  898. /* the parsed path should match the specified 'shouldbe' string.
  899. * We should pass the "eat our own dog food" test, be able to output
  900. * this path and then input it again. Ie the path resulting from:
  901. *
  902. * aspath_parse(aspath_put(as))
  903. *
  904. * should:
  905. *
  906. * - also match the specified 'shouldbe' value
  907. * - hash to same value as original path
  908. * - have same hops and confed counts as original, and as the
  909. * the specified counts
  910. *
  911. * aspath_str2aspath() and shouldbe should match
  912. *
  913. * We do the same for:
  914. *
  915. * aspath_parse(aspath_put(as,USE32BIT))
  916. *
  917. * Confederation related tests:
  918. * - aspath_delete_confed_seq(aspath) should match shouldbe_confed
  919. * - aspath_delete_confed_seq should be idempotent.
  920. */
  921. if (strcmp(aspath_print (as), sp->shouldbe)
  922. /* hash validation */
  923. || (aspath_key_make (as) != aspath_key_make (asinout))
  924. /* by string */
  925. || strcmp(aspath_print (asinout), sp->shouldbe)
  926. /* By 4-byte parsing */
  927. || strcmp(aspath_print (as4), sp->shouldbe)
  928. /* by various path counts */
  929. || (aspath_count_hops (as) != sp->hops)
  930. || (aspath_count_confeds (as) != sp->confeds)
  931. || (aspath_count_hops (asinout) != sp->hops)
  932. || (aspath_count_confeds (asinout) != sp->confeds))
  933. {
  934. failed++;
  935. fails++;
  936. printf ("shouldbe:\n%s\n", sp->shouldbe);
  937. printf ("as4:\n%s\n", aspath_print (as4));
  938. printf ("hash keys: in: %d out->in: %d\n",
  939. aspath_key_make (as), aspath_key_make (asinout));
  940. printf ("hops: %d, counted %d %d\n", sp->hops,
  941. aspath_count_hops (as),
  942. aspath_count_hops (asinout) );
  943. printf ("confeds: %d, counted %d %d\n", sp->confeds,
  944. aspath_count_confeds (as),
  945. aspath_count_confeds (asinout));
  946. printf ("out->in:\n%s\nbytes: ", aspath_print(asinout));
  947. printbytes (out, bytes);
  948. }
  949. /* basic confed related tests */
  950. if ((aspath_print (asconfeddel) == NULL
  951. && sp->shouldbe_delete_confed != NULL)
  952. || (aspath_print (asconfeddel) != NULL
  953. && sp->shouldbe_delete_confed == NULL)
  954. || strcmp(aspath_print (asconfeddel), sp->shouldbe_delete_confed)
  955. /* delete_confed_seq should be idempotent */
  956. || (aspath_key_make (asconfeddel)
  957. != aspath_key_make (aspath_delete_confed_seq (asconfeddel))))
  958. {
  959. failed++;
  960. fails++;
  961. printf ("confed_del: %s\n", aspath_print (asconfeddel));
  962. printf ("should be: %s\n", sp->shouldbe_delete_confed);
  963. }
  964. /* aspath_str2aspath test */
  965. if ((aspath_print (asstr) == NULL && sp->shouldbe != NULL)
  966. || (aspath_print (asstr) != NULL && sp->shouldbe == NULL)
  967. || strcmp(aspath_print (asstr), sp->shouldbe))
  968. {
  969. failed++;
  970. fails++;
  971. printf ("asstr: %s\n", aspath_print (asstr));
  972. }
  973. /* loop, private and first as checks */
  974. if ((sp->does_loop && aspath_loop_check (as, sp->does_loop) == 0)
  975. || (sp->doesnt_loop && aspath_loop_check (as, sp->doesnt_loop) != 0)
  976. || (aspath_private_as_check (as) != sp->private_as)
  977. || (aspath_firstas_check (as,sp->first)
  978. && sp->first == 0))
  979. {
  980. failed++;
  981. fails++;
  982. printf ("firstas: %d, got %d\n", sp->first,
  983. aspath_firstas_check (as,sp->first));
  984. printf ("loop does: %d %d, doesnt: %d %d\n",
  985. sp->does_loop, aspath_loop_check (as, sp->does_loop),
  986. sp->doesnt_loop, aspath_loop_check (as, sp->doesnt_loop));
  987. printf ("private check: %d %d\n", sp->private_as,
  988. aspath_private_as_check (as));
  989. }
  990. aspath_unintern (&asinout);
  991. aspath_unintern (&as4);
  992. aspath_free (asconfeddel);
  993. aspath_free (asstr);
  994. stream_reset (s);
  995. return fails;
  996. }
  997. static void
  998. empty_get_test ()
  999. {
  1000. struct aspath *as = aspath_empty_get ();
  1001. struct test_spec sp = { "", "", 0, 0, 0, 0, 0, 0 };
  1002. printf ("empty_get_test, as: %s\n",aspath_print (as));
  1003. if (!validate (as, &sp))
  1004. printf ("%s\n", OK);
  1005. else
  1006. printf ("%s!\n", FAILED);
  1007. printf ("\n");
  1008. aspath_free (as);
  1009. }
  1010. /* basic parsing test */
  1011. static void
  1012. parse_test (struct test_segment *t)
  1013. {
  1014. struct aspath *asp;
  1015. printf ("%s: %s\n", t->name, t->desc);
  1016. asp = make_aspath (t->asdata, t->len, 0);
  1017. printf ("aspath: %s\nvalidating...:\n", aspath_print (asp));
  1018. if (!validate (asp, &t->sp))
  1019. printf (OK "\n");
  1020. else
  1021. printf (FAILED "\n");
  1022. printf ("\n");
  1023. if (asp)
  1024. aspath_unintern (&asp);
  1025. }
  1026. /* prepend testing */
  1027. static void
  1028. prepend_test (struct tests *t)
  1029. {
  1030. struct aspath *asp1, *asp2, *ascratch;
  1031. printf ("prepend %s: %s\n", t->test1->name, t->test1->desc);
  1032. printf ("to %s: %s\n", t->test2->name, t->test2->desc);
  1033. asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
  1034. asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
  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 ("%s\n", OK);
  1041. else
  1042. printf ("%s!\n", FAILED);
  1043. printf ("\n");
  1044. aspath_unintern (&asp1);
  1045. aspath_free (asp2);
  1046. }
  1047. /* empty-prepend testing */
  1048. static void
  1049. empty_prepend_test (struct test_segment *t)
  1050. {
  1051. struct aspath *asp1, *asp2, *ascratch;
  1052. printf ("empty prepend %s: %s\n", t->name, t->desc);
  1053. asp1 = make_aspath (t->asdata, t->len, 0);
  1054. asp2 = aspath_empty ();
  1055. ascratch = aspath_dup (asp2);
  1056. aspath_unintern (&asp2);
  1057. asp2 = aspath_prepend (asp1, ascratch);
  1058. printf ("aspath: %s\n", aspath_print (asp2));
  1059. if (!validate (asp2, &t->sp))
  1060. printf (OK "\n");
  1061. else
  1062. printf (FAILED "!\n");
  1063. printf ("\n");
  1064. if (asp1)
  1065. aspath_unintern (&asp1);
  1066. aspath_free (asp2);
  1067. }
  1068. /* as2+as4 reconciliation testing */
  1069. static void
  1070. as4_reconcile_test (struct tests *t)
  1071. {
  1072. struct aspath *asp1, *asp2, *ascratch;
  1073. printf ("reconciling %s:\n %s\n", t->test1->name, t->test1->desc);
  1074. printf ("with %s:\n %s\n", t->test2->name, t->test2->desc);
  1075. asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
  1076. asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
  1077. ascratch = aspath_reconcile_as4 (asp1, asp2);
  1078. if (!validate (ascratch, &t->sp))
  1079. printf (OK "\n");
  1080. else
  1081. printf (FAILED "!\n");
  1082. printf ("\n");
  1083. aspath_unintern (&asp1);
  1084. aspath_unintern (&asp2);
  1085. aspath_free (ascratch);
  1086. }
  1087. /* aggregation testing */
  1088. static void
  1089. aggregate_test (struct tests *t)
  1090. {
  1091. struct aspath *asp1, *asp2, *ascratch;
  1092. printf ("aggregate %s: %s\n", t->test1->name, t->test1->desc);
  1093. printf ("with %s: %s\n", t->test2->name, t->test2->desc);
  1094. asp1 = make_aspath (t->test1->asdata, t->test1->len, 0);
  1095. asp2 = make_aspath (t->test2->asdata, t->test2->len, 0);
  1096. ascratch = aspath_aggregate (asp1, asp2);
  1097. if (!validate (ascratch, &t->sp))
  1098. printf (OK "\n");
  1099. else
  1100. printf (FAILED "!\n");
  1101. printf ("\n");
  1102. aspath_unintern (&asp1);
  1103. aspath_unintern (&asp2);
  1104. aspath_free (ascratch);
  1105. /* aspath_unintern (ascratch);*/
  1106. }
  1107. /* cmp_left tests */
  1108. static void
  1109. cmp_test ()
  1110. {
  1111. unsigned int i;
  1112. #define CMP_TESTS_MAX \
  1113. (sizeof(left_compare) / sizeof (struct compare_tests))
  1114. for (i = 0; i < CMP_TESTS_MAX; i++)
  1115. {
  1116. struct test_segment *t1 = &test_segments[left_compare[i].test_index1];
  1117. struct test_segment *t2 = &test_segments[left_compare[i].test_index2];
  1118. struct aspath *asp1, *asp2;
  1119. printf ("left cmp %s: %s\n", t1->name, t1->desc);
  1120. printf ("and %s: %s\n", t2->name, t2->desc);
  1121. asp1 = make_aspath (t1->asdata, t1->len, 0);
  1122. asp2 = make_aspath (t2->asdata, t2->len, 0);
  1123. if (aspath_cmp_left (asp1, asp2) != left_compare[i].shouldbe_cmp
  1124. || aspath_cmp_left (asp2, asp1) != left_compare[i].shouldbe_cmp
  1125. || aspath_cmp_left_confed (asp1, asp2)
  1126. != left_compare[i].shouldbe_confed
  1127. || aspath_cmp_left_confed (asp2, asp1)
  1128. != left_compare[i].shouldbe_confed)
  1129. {
  1130. failed++;
  1131. printf (FAILED "\n");
  1132. printf ("result should be: cmp: %d, confed: %d\n",
  1133. left_compare[i].shouldbe_cmp,
  1134. left_compare[i].shouldbe_confed);
  1135. printf ("got: cmp %d, cmp_confed: %d\n",
  1136. aspath_cmp_left (asp1, asp2),
  1137. aspath_cmp_left_confed (asp1, asp2));
  1138. printf("path1: %s\npath2: %s\n", aspath_print (asp1),
  1139. aspath_print (asp2));
  1140. }
  1141. else
  1142. printf (OK "\n");
  1143. printf ("\n");
  1144. aspath_unintern (&asp1);
  1145. aspath_unintern (&asp2);
  1146. }
  1147. }
  1148. static int
  1149. handle_attr_test (struct aspath_tests *t)
  1150. {
  1151. struct bgp bgp = { 0 };
  1152. struct peer peer = { 0 };
  1153. struct attr attr = { 0 };
  1154. int ret;
  1155. int initfail = failed;
  1156. struct aspath *asp;
  1157. size_t datalen;
  1158. asp = make_aspath (t->segment->asdata, t->segment->len, 0);
  1159. peer.ibuf = stream_new (BGP_MAX_PACKET_SIZE);
  1160. peer.obuf = stream_fifo_new ();
  1161. peer.bgp = &bgp;
  1162. peer.host = (char *)"none";
  1163. peer.fd = -1;
  1164. peer.cap = t->cap;
  1165. stream_write (peer.ibuf, t->attrheader, t->len);
  1166. datalen = aspath_put (peer.ibuf, asp, t->as4 == AS4_DATA);
  1167. if (t->old_segment)
  1168. {
  1169. char dummyaspath[] = { BGP_ATTR_FLAG_TRANS, BGP_ATTR_AS_PATH,
  1170. t->old_segment->len };
  1171. stream_write (peer.ibuf, dummyaspath, sizeof (dummyaspath));
  1172. stream_write (peer.ibuf, t->old_segment->asdata, t->old_segment->len);
  1173. datalen += sizeof (dummyaspath) + t->old_segment->len;
  1174. }
  1175. ret = bgp_attr_parse (&peer, &attr, t->len + datalen, NULL, NULL);
  1176. if (ret != t->result)
  1177. {
  1178. printf ("bgp_attr_parse returned %d, expected %d\n", ret, t->result);
  1179. printf ("datalen %zd\n", datalen);
  1180. failed++;
  1181. }
  1182. if (ret != 0)
  1183. goto out;
  1184. if (t->shouldbe && attr.aspath == NULL)
  1185. {
  1186. printf ("aspath is NULL, but should be: %s\n", t->shouldbe);
  1187. failed++;
  1188. }
  1189. if (t->shouldbe && attr.aspath && strcmp (attr.aspath->str, t->shouldbe))
  1190. {
  1191. printf ("attr str and 'shouldbe' mismatched!\n"
  1192. "attr str: %s\n"
  1193. "shouldbe: %s\n",
  1194. attr.aspath->str, t->shouldbe);
  1195. failed++;
  1196. }
  1197. if (!t->shouldbe && attr.aspath)
  1198. {
  1199. printf ("aspath should be NULL, but is: %s\n", attr.aspath->str);
  1200. failed++;
  1201. }
  1202. out:
  1203. if (attr.aspath)
  1204. aspath_unintern (&attr.aspath);
  1205. if (asp)
  1206. aspath_unintern (&asp);
  1207. return failed - initfail;
  1208. }
  1209. static void
  1210. attr_test (struct aspath_tests *t)
  1211. {
  1212. printf ("%s\n", t->desc);
  1213. printf ("%s\n\n", handle_attr_test (t) ? FAILED : OK);
  1214. }
  1215. int
  1216. main (void)
  1217. {
  1218. int i = 0;
  1219. bgp_master_init ();
  1220. master = bm->master;
  1221. bgp_option_set (BGP_OPT_NO_LISTEN);
  1222. bgp_attr_init ();
  1223. while (test_segments[i].name)
  1224. {
  1225. printf ("test %u\n", i);
  1226. parse_test (&test_segments[i]);
  1227. empty_prepend_test (&test_segments[i++]);
  1228. }
  1229. i = 0;
  1230. while (prepend_tests[i].test1)
  1231. {
  1232. printf ("prepend test %u\n", i);
  1233. prepend_test (&prepend_tests[i++]);
  1234. }
  1235. i = 0;
  1236. while (aggregate_tests[i].test1)
  1237. {
  1238. printf ("aggregate test %u\n", i);
  1239. aggregate_test (&aggregate_tests[i++]);
  1240. }
  1241. i = 0;
  1242. while (reconcile_tests[i].test1)
  1243. {
  1244. printf ("reconcile test %u\n", i);
  1245. as4_reconcile_test (&reconcile_tests[i++]);
  1246. }
  1247. i = 0;
  1248. cmp_test();
  1249. i = 0;
  1250. empty_get_test();
  1251. i = 0;
  1252. while (aspath_tests[i].desc)
  1253. {
  1254. printf ("aspath_attr test %d\n", i);
  1255. attr_test (&aspath_tests[i++]);
  1256. }
  1257. printf ("failures: %d\n", failed);
  1258. printf ("aspath count: %ld\n", aspath_count());
  1259. return (failed + aspath_count());
  1260. }