test_regex_api.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file regex/test_regex_api.c
  19. * @brief base test case for regex api (and DHT functions)
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_testing_lib.h"
  25. #include "gnunet_regex_service.h"
  26. /**
  27. * How long until we really give up on a particular testcase portion?
  28. */
  29. #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 600)
  30. /**
  31. * How long until we give up on any particular operation (and retry)?
  32. */
  33. #define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
  34. static struct GNUNET_REGEX_Announcement *a;
  35. static struct GNUNET_REGEX_Search *s;
  36. static int ok = 1;
  37. static GNUNET_SCHEDULER_TaskIdentifier die_task;
  38. static void
  39. end (void *cls,
  40. const struct GNUNET_SCHEDULER_TaskContext *tc)
  41. {
  42. die_task = GNUNET_SCHEDULER_NO_TASK;
  43. GNUNET_REGEX_announce_cancel (a);
  44. a = NULL;
  45. GNUNET_REGEX_search_cancel (s);
  46. s = NULL;
  47. ok = 0;
  48. }
  49. static void
  50. end_badly ()
  51. {
  52. die_task = GNUNET_SCHEDULER_NO_TASK;
  53. FPRINTF (stderr, "%s", "Testcase failed (timeout).\n");
  54. GNUNET_REGEX_announce_cancel (a);
  55. a = NULL;
  56. GNUNET_REGEX_search_cancel (s);
  57. s = NULL;
  58. ok = 1;
  59. }
  60. /**
  61. * Search callback function, invoked for every result that was found.
  62. *
  63. * @param cls Closure provided in GNUNET_REGEX_search.
  64. * @param id Peer providing a regex that matches the string.
  65. * @param get_path Path of the get request.
  66. * @param get_path_length Lenght of get_path.
  67. * @param put_path Path of the put request.
  68. * @param put_path_length Length of the put_path.
  69. */
  70. static void
  71. found_cb (void *cls,
  72. const struct GNUNET_PeerIdentity *id,
  73. const struct GNUNET_PeerIdentity *get_path,
  74. unsigned int get_path_length,
  75. const struct GNUNET_PeerIdentity *put_path,
  76. unsigned int put_path_length)
  77. {
  78. GNUNET_SCHEDULER_cancel (die_task);
  79. die_task =
  80. GNUNET_SCHEDULER_add_now (&end, NULL);
  81. }
  82. static void
  83. run (void *cls,
  84. const struct GNUNET_CONFIGURATION_Handle *cfg,
  85. struct GNUNET_TESTING_Peer *peer)
  86. {
  87. die_task =
  88. GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT,
  89. &end_badly, NULL);
  90. a = GNUNET_REGEX_announce (cfg,
  91. "my long prefix - hello world(0|1)*",
  92. GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
  93. 5),
  94. 1);
  95. s = GNUNET_REGEX_search (cfg,
  96. "my long prefix - hello world0101",
  97. &found_cb, NULL);
  98. }
  99. int
  100. main (int argc, char *argv[])
  101. {
  102. if (0 != GNUNET_TESTING_peer_run ("test-regex-api",
  103. "test_regex_api_data.conf",
  104. &run, NULL))
  105. return 1;
  106. return ok;
  107. }
  108. /* end of test_regex_api.c */