test_dht_api.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2009 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 dht/test_dht_api.c
  19. * @brief base test case for dht api
  20. *
  21. * This test case tests DHT api to DUMMY DHT service communication.
  22. *
  23. */
  24. #include "platform.h"
  25. #include "gnunet_util_lib.h"
  26. #include "gnunet_hello_lib.h"
  27. #include "gnunet_testing_lib.h"
  28. #include "gnunet_dht_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, 600)
  33. /**
  34. * How long until we give up on any particular operation (and retry)?
  35. */
  36. #define BASE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
  37. #define MTYPE 12345
  38. struct RetryContext
  39. {
  40. /**
  41. * When to really abort the operation.
  42. */
  43. struct GNUNET_TIME_Absolute real_timeout;
  44. /**
  45. * What timeout to set for the current attempt (increases)
  46. */
  47. struct GNUNET_TIME_Relative next_timeout;
  48. /**
  49. * The task identifier of the retry task, so it can be cancelled.
  50. */
  51. GNUNET_SCHEDULER_TaskIdentifier retry_task;
  52. };
  53. static struct GNUNET_DHT_Handle *dht_handle;
  54. static struct GNUNET_DHT_GetHandle *get_handle;
  55. struct RetryContext retry_context;
  56. static int ok = 1;
  57. static GNUNET_SCHEDULER_TaskIdentifier die_task;
  58. #if VERBOSE
  59. #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
  60. #else
  61. #define OKPP do { ok++; } while (0)
  62. #endif
  63. static void
  64. end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  65. {
  66. GNUNET_SCHEDULER_cancel (die_task);
  67. die_task = GNUNET_SCHEDULER_NO_TASK;
  68. GNUNET_DHT_disconnect (dht_handle);
  69. dht_handle = NULL;
  70. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  71. "DHT disconnected, returning success!\n");
  72. ok = 0;
  73. }
  74. static void
  75. end_badly ()
  76. {
  77. /* do work here */
  78. FPRINTF (stderr, "%s", "Ending on an unhappy note.\n");
  79. if (get_handle != NULL)
  80. {
  81. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping get request!\n");
  82. GNUNET_DHT_get_stop (get_handle);
  83. }
  84. if (retry_context.retry_task != GNUNET_SCHEDULER_NO_TASK)
  85. GNUNET_SCHEDULER_cancel (retry_context.retry_task);
  86. GNUNET_DHT_disconnect (dht_handle);
  87. dht_handle = NULL;
  88. ok = 1;
  89. }
  90. /**
  91. * Signature of the main function of a task.
  92. *
  93. * @param cls closure
  94. * @param tc context information (why was this task triggered now)
  95. */
  96. static void
  97. test_get_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  98. {
  99. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get_stop!\n");
  100. if ((tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT) != 0)
  101. {
  102. GNUNET_break (0);
  103. GNUNET_SCHEDULER_cancel (die_task);
  104. die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  105. return;
  106. }
  107. GNUNET_assert (dht_handle != NULL);
  108. GNUNET_DHT_get_stop (get_handle);
  109. get_handle = NULL;
  110. GNUNET_SCHEDULER_add_now (&end, NULL);
  111. }
  112. static void
  113. test_get_iterator (void *cls, struct GNUNET_TIME_Absolute exp,
  114. const struct GNUNET_HashCode * key,
  115. const struct GNUNET_PeerIdentity *get_path,
  116. unsigned int get_path_length,
  117. const struct GNUNET_PeerIdentity *put_path,
  118. unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
  119. size_t size, const void *data)
  120. {
  121. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  122. "test_get_iterator called (we got a result), stopping get request!\n");
  123. GNUNET_SCHEDULER_add_continuation (&test_get_stop, NULL,
  124. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  125. }
  126. /**
  127. * Signature of the main function of a task.
  128. *
  129. * @param cls closure
  130. * @param success result of PUT
  131. */
  132. static void
  133. test_get (void *cls, int success)
  134. {
  135. struct GNUNET_HashCode hash;
  136. memset (&hash, 42, sizeof (struct GNUNET_HashCode));
  137. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_get!\n");
  138. GNUNET_assert (dht_handle != NULL);
  139. retry_context.real_timeout = GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT);
  140. retry_context.next_timeout = BASE_TIMEOUT;
  141. get_handle =
  142. GNUNET_DHT_get_start (dht_handle,
  143. GNUNET_BLOCK_TYPE_TEST, &hash, 1,
  144. GNUNET_DHT_RO_NONE, NULL, 0, &test_get_iterator,
  145. NULL);
  146. if (get_handle == NULL)
  147. {
  148. GNUNET_break (0);
  149. GNUNET_SCHEDULER_cancel (die_task);
  150. die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
  151. return;
  152. }
  153. }
  154. static void
  155. run (void *cls,
  156. const struct GNUNET_CONFIGURATION_Handle *cfg,
  157. struct GNUNET_TESTING_Peer *peer)
  158. {
  159. struct GNUNET_HashCode hash;
  160. char *data;
  161. size_t data_size = 42;
  162. GNUNET_assert (ok == 1);
  163. OKPP;
  164. die_task =
  165. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  166. (GNUNET_TIME_UNIT_MINUTES, 1), &end_badly,
  167. NULL);
  168. memset (&hash, 42, sizeof (struct GNUNET_HashCode));
  169. data = GNUNET_malloc (data_size);
  170. memset (data, 43, data_size);
  171. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Called test_put!\n");
  172. dht_handle = GNUNET_DHT_connect (cfg, 100);
  173. GNUNET_assert (dht_handle != NULL);
  174. GNUNET_DHT_put (dht_handle, &hash, 1, GNUNET_DHT_RO_NONE,
  175. GNUNET_BLOCK_TYPE_TEST, data_size, data,
  176. GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT),
  177. TOTAL_TIMEOUT, &test_get, NULL);
  178. GNUNET_free (data);
  179. }
  180. int
  181. main (int argc, char *argv[])
  182. {
  183. if (0 != GNUNET_TESTING_peer_run ("test-dht-api",
  184. "test_dht_api_data.conf",
  185. &run, NULL))
  186. return 1;
  187. return ok;
  188. }
  189. /* end of test_dht_api.c */