ecommunity_test.c 3.7 KB

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