ecommunity_test.c 3.8 KB

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