ecommunity_test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <zebra.h>
  2. #include "vty.h"
  3. #include "stream.h"
  4. #include "privs.h"
  5. #include "memory.h"
  6. #include "bgpd/bgpd.h"
  7. #include "bgpd/bgp_ecommunity.h"
  8. /* need these to link in libbgp */
  9. struct zebra_privs_t *bgpd_privs = NULL;
  10. struct thread_master *master = NULL;
  11. static int failed = 0;
  12. /* specification for a test - what the results should be */
  13. struct test_spec
  14. {
  15. const char *shouldbe; /* the string the path should parse to */
  16. };
  17. /* test segments to parse and validate, and use for other tests */
  18. static struct test_segment {
  19. const char *name;
  20. const char *desc;
  21. const u_int8_t data[1024];
  22. int len;
  23. struct test_spec sp;
  24. } test_segments [] =
  25. {
  26. { /* 0 */
  27. "ipaddr",
  28. "rt 1.2.3.4:257",
  29. { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_ROUTE_TARGET,
  30. 0x1,0x2,0x3,0x4, 0x1,0x1 },
  31. 8,
  32. { "rt 1.2.3.4:257" }
  33. },
  34. { /* 1 */
  35. "ipaddr-so",
  36. "soo 1.2.3.4:257",
  37. { ECOMMUNITY_ENCODE_IP, ECOMMUNITY_SITE_ORIGIN,
  38. 0x1,0x2,0x3,0x4, 0x1,0x1},
  39. 8,
  40. { "soo 1.2.3.4:257" }
  41. },
  42. { /* 2 */
  43. "asn",
  44. "rt 23456:987654321",
  45. { ECOMMUNITY_ENCODE_AS, ECOMMUNITY_SITE_ORIGIN,
  46. 0x5b,0xa0, 0x3a,0xde,0x68,0xb1 },
  47. 8,
  48. { "soo 23456:987654321" }
  49. },
  50. { /* 3 */
  51. "asn4",
  52. "rt 168450976:4321",
  53. { ECOMMUNITY_ENCODE_AS4, ECOMMUNITY_SITE_ORIGIN,
  54. 0xa,0xa,0x5b,0xa0, 0x10,0xe1 },
  55. 8,
  56. { "soo 168450976:4321" }
  57. },
  58. { NULL, NULL, {0}, 0, { NULL } }
  59. };
  60. /* validate the given aspath */
  61. static int
  62. validate (struct ecommunity *ecom, const struct test_spec *sp)
  63. {
  64. int fails = 0;
  65. struct ecommunity *etmp;
  66. char *str1, *str2;
  67. printf ("got:\n %s\n", ecommunity_str (ecom));
  68. str1 = ecommunity_ecom2str (ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
  69. etmp = ecommunity_str2com (str1, 0, 1);
  70. if (etmp)
  71. str2 = ecommunity_ecom2str (etmp, ECOMMUNITY_FORMAT_COMMUNITY_LIST);
  72. else
  73. str2 = NULL;
  74. if (strcmp (sp->shouldbe, str1))
  75. {
  76. failed++;
  77. fails++;
  78. printf ("shouldbe: %s\n%s\n", str1, sp->shouldbe);
  79. }
  80. if (!etmp || strcmp (str1, str2))
  81. {
  82. failed++;
  83. fails++;
  84. printf ("dogfood: in %s\n"
  85. " in->out %s\n",
  86. str1,
  87. (etmp && str2) ? str2 : "NULL");
  88. }
  89. ecommunity_free (&etmp);
  90. XFREE (MTYPE_ECOMMUNITY_STR, str1);
  91. XFREE (MTYPE_ECOMMUNITY_STR, str2);
  92. return fails;
  93. }
  94. /* basic parsing test */
  95. static void
  96. parse_test (struct test_segment *t)
  97. {
  98. struct ecommunity *ecom;
  99. printf ("%s: %s\n", t->name, t->desc);
  100. ecom = ecommunity_parse (t->data, t->len);
  101. printf ("ecom: %s\nvalidating...:\n", ecommunity_str (ecom));
  102. if (!validate (ecom, &t->sp))
  103. printf ("OK\n");
  104. else
  105. printf ("failed\n");
  106. printf ("\n");
  107. ecommunity_unintern (&ecom);
  108. }
  109. int
  110. main (void)
  111. {
  112. int i = 0;
  113. ecommunity_init();
  114. while (test_segments[i].name)
  115. parse_test (&test_segments[i++]);
  116. printf ("failures: %d\n", failed);
  117. //printf ("aspath count: %ld\n", aspath_count());
  118. return failed;
  119. //return (failed + aspath_count());
  120. }