aspath_test.c 42 KB

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