test_dht_api.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2015, 2017 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 dht/test_dht_api.c
  18. * @brief base test case for dht api
  19. * @author Christian Grothoff
  20. *
  21. * This test case tests DHT api to DUMMY DHT service communication.
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_hello_lib.h"
  26. #include "gnunet_testing_lib.h"
  27. #include "gnunet_dht_service.h"
  28. /**
  29. * How long until we really give up on a particular testcase portion?
  30. */
  31. #define TOTAL_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, \
  32. 60)
  33. static struct GNUNET_DHT_Handle *dht_handle;
  34. static struct GNUNET_DHT_GetHandle *get_handle;
  35. static struct GNUNET_DHT_PutHandle *put_handle;
  36. static int ok = 1;
  37. static struct GNUNET_SCHEDULER_Task *die_task;
  38. static void
  39. do_shutdown (void *cls)
  40. {
  41. if (NULL != die_task)
  42. {
  43. GNUNET_SCHEDULER_cancel (die_task);
  44. die_task = NULL;
  45. }
  46. if (NULL != put_handle)
  47. {
  48. GNUNET_DHT_put_cancel (put_handle);
  49. put_handle = NULL;
  50. }
  51. if (NULL != get_handle)
  52. {
  53. GNUNET_DHT_get_stop (get_handle);
  54. get_handle = NULL;
  55. }
  56. GNUNET_DHT_disconnect (dht_handle);
  57. dht_handle = NULL;
  58. }
  59. static void
  60. end_badly (void *cls)
  61. {
  62. die_task = NULL;
  63. fprintf (stderr,
  64. "%s",
  65. "Ending on an unhappy note.\n");
  66. GNUNET_SCHEDULER_shutdown ();
  67. ok = 1;
  68. }
  69. static void
  70. test_get_iterator (void *cls,
  71. struct GNUNET_TIME_Absolute exp,
  72. const struct GNUNET_HashCode *key,
  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. enum GNUNET_BLOCK_Type type,
  78. size_t size,
  79. const void *data)
  80. {
  81. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  82. "test_get_iterator called (we got a result), stopping get request!\n");
  83. GNUNET_SCHEDULER_shutdown ();
  84. ok = 0;
  85. }
  86. /**
  87. * Signature of the main function of a task.
  88. *
  89. * @param cls closure
  90. */
  91. static void
  92. test_get (void *cls)
  93. {
  94. struct GNUNET_HashCode hash;
  95. put_handle = NULL;
  96. memset (&hash,
  97. 42,
  98. sizeof(struct GNUNET_HashCode));
  99. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  100. "Called test_get!\n");
  101. GNUNET_assert (dht_handle != NULL);
  102. get_handle = GNUNET_DHT_get_start (dht_handle,
  103. GNUNET_BLOCK_TYPE_TEST,
  104. &hash,
  105. 1,
  106. GNUNET_DHT_RO_NONE,
  107. NULL,
  108. 0,
  109. &test_get_iterator,
  110. NULL);
  111. if (NULL == get_handle)
  112. {
  113. GNUNET_break (0);
  114. ok = 1;
  115. GNUNET_SCHEDULER_shutdown ();
  116. return;
  117. }
  118. }
  119. static void
  120. run (void *cls,
  121. const struct GNUNET_CONFIGURATION_Handle *cfg,
  122. struct GNUNET_TESTING_Peer *peer)
  123. {
  124. struct GNUNET_HashCode hash;
  125. char *data;
  126. size_t data_size = 42;
  127. GNUNET_assert (ok == 1);
  128. GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  129. NULL);
  130. die_task = GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT,
  131. &end_badly,
  132. NULL);
  133. memset (&hash,
  134. 42,
  135. sizeof(struct GNUNET_HashCode));
  136. data = GNUNET_malloc (data_size);
  137. memset (data, 43, data_size);
  138. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  139. "Called test_put!\n");
  140. dht_handle = GNUNET_DHT_connect (cfg,
  141. 100);
  142. GNUNET_assert (NULL != dht_handle);
  143. put_handle = GNUNET_DHT_put (dht_handle,
  144. &hash,
  145. 1,
  146. GNUNET_DHT_RO_NONE,
  147. GNUNET_BLOCK_TYPE_TEST,
  148. data_size,
  149. data,
  150. GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT),
  151. &test_get,
  152. NULL);
  153. GNUNET_free (data);
  154. }
  155. int
  156. main (int argc,
  157. char *argv[])
  158. {
  159. if (0 != GNUNET_TESTING_peer_run ("test-dht-api",
  160. "test_dht_api_data.conf",
  161. &run, NULL))
  162. return 1;
  163. return ok;
  164. }
  165. /* end of test_dht_api.c */