test_regex_integration.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2013, 2015 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file regex/test_regex_integration.c
  18. * @brief base test case for regex integration with VPN;
  19. * tests that the regexes generated by the TUN API
  20. * for IP addresses work (for some simple cases)
  21. * @author Christian Grothoff
  22. */
  23. #include "platform.h"
  24. #include "gnunet_applications.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_tun_lib.h"
  27. #include "gnunet_testing_lib.h"
  28. #include "gnunet_regex_service.h"
  29. /**
  30. * How long until we really give up on a particular testcase portion?
  31. */
  32. #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
  33. 600)
  34. /**
  35. * How long until we give up on any particular operation (and retry)?
  36. */
  37. #define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
  38. static struct GNUNET_REGEX_Announcement *a4;
  39. static struct GNUNET_REGEX_Search *s4;
  40. static struct GNUNET_REGEX_Announcement *a6;
  41. static struct GNUNET_REGEX_Search *s6;
  42. static int ok = 1;
  43. static struct GNUNET_SCHEDULER_Task *die_task;
  44. static void
  45. end (void *cls)
  46. {
  47. die_task = NULL;
  48. GNUNET_REGEX_announce_cancel (a4);
  49. a4 = NULL;
  50. GNUNET_REGEX_search_cancel (s4);
  51. s4 = NULL;
  52. GNUNET_REGEX_announce_cancel (a6);
  53. a6 = NULL;
  54. GNUNET_REGEX_search_cancel (s6);
  55. s6 = NULL;
  56. ok = 0;
  57. }
  58. static void
  59. end_badly ()
  60. {
  61. fprintf (stderr, "%s", "Testcase failed (timeout).\n");
  62. end (NULL);
  63. ok = 1;
  64. }
  65. /**
  66. * Search callback function, invoked for every result that was found.
  67. *
  68. * @param cls Closure provided in #GNUNET_REGEX_search().
  69. * @param id Peer providing a regex that matches the string.
  70. * @param get_path Path of the get request.
  71. * @param get_path_length Length of @a get_path.
  72. * @param put_path Path of the put request.
  73. * @param put_path_length Length of the @a put_path.
  74. */
  75. static void
  76. found_cb (void *cls,
  77. const struct GNUNET_PeerIdentity *id,
  78. const struct GNUNET_PeerIdentity *get_path,
  79. unsigned int get_path_length,
  80. const struct GNUNET_PeerIdentity *put_path,
  81. unsigned int put_path_length)
  82. {
  83. const char *str = cls;
  84. static int found;
  85. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  86. "IPv%s-exit found\n",
  87. str);
  88. if (0 == strcmp (str, "4"))
  89. found |= 4;
  90. if (0 == strcmp (str, "6"))
  91. found |= 2;
  92. if ((4 | 2) == found)
  93. {
  94. GNUNET_SCHEDULER_cancel (die_task);
  95. die_task =
  96. GNUNET_SCHEDULER_add_now (&end, NULL);
  97. }
  98. }
  99. static void
  100. run (void *cls,
  101. const struct GNUNET_CONFIGURATION_Handle *cfg,
  102. struct GNUNET_TESTING_Peer *peer)
  103. {
  104. char rxstr4[GNUNET_TUN_IPV4_REGEXLEN];
  105. char rxstr6[GNUNET_TUN_IPV6_REGEXLEN];
  106. char *p4r;
  107. char *p6r;
  108. char *p4;
  109. char *p6;
  110. char *ss4;
  111. char *ss6;
  112. struct in_addr i4;
  113. struct in6_addr i6;
  114. die_task =
  115. GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT,
  116. &end_badly, NULL);
  117. GNUNET_assert (1 ==
  118. inet_pton (AF_INET,
  119. "127.0.0.1",
  120. &i4));
  121. GNUNET_assert (1 ==
  122. inet_pton (AF_INET6,
  123. "::1:5",
  124. &i6));
  125. GNUNET_TUN_ipv4toregexsearch (&i4,
  126. 8080,
  127. rxstr4);
  128. GNUNET_TUN_ipv6toregexsearch (&i6,
  129. 8686,
  130. rxstr6);
  131. GNUNET_asprintf (&ss4,
  132. "%s%s",
  133. GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
  134. rxstr4);
  135. GNUNET_asprintf (&ss6,
  136. "%s%s",
  137. GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
  138. rxstr6);
  139. p4r = GNUNET_TUN_ipv4policy2regex ("0.0.0.0/0:!25;");
  140. p6r = GNUNET_TUN_ipv6policy2regex ("::/0:!25;");
  141. GNUNET_asprintf (&p4,
  142. "%s%s",
  143. GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
  144. p4r);
  145. GNUNET_asprintf (&p6,
  146. "%s%s",
  147. GNUNET_APPLICATION_TYPE_EXIT_REGEX_PREFIX,
  148. p6r);
  149. GNUNET_free (p4r);
  150. GNUNET_free (p6r);
  151. a4 = GNUNET_REGEX_announce (cfg,
  152. p4,
  153. GNUNET_TIME_relative_multiply (
  154. GNUNET_TIME_UNIT_SECONDS,
  155. 5),
  156. 1);
  157. a6 = GNUNET_REGEX_announce (cfg,
  158. p6,
  159. GNUNET_TIME_relative_multiply (
  160. GNUNET_TIME_UNIT_SECONDS,
  161. 5),
  162. 1);
  163. GNUNET_free (p4);
  164. GNUNET_free (p6);
  165. s4 = GNUNET_REGEX_search (cfg,
  166. ss4,
  167. &found_cb, "4");
  168. s6 = GNUNET_REGEX_search (cfg,
  169. ss6,
  170. &found_cb, "6");
  171. GNUNET_free (ss4);
  172. GNUNET_free (ss6);
  173. }
  174. int
  175. main (int argc, char *argv[])
  176. {
  177. if (0 != GNUNET_TESTING_peer_run ("test-regex-integration",
  178. "test_regex_api_data.conf",
  179. &run, NULL))
  180. return 1;
  181. return ok;
  182. }
  183. /* end of test_regex_integration.c */