test_dht_api.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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, 60)
  32. static struct GNUNET_DHT_Handle *dht_handle;
  33. static struct GNUNET_DHT_GetHandle *get_handle;
  34. static struct GNUNET_DHT_PutHandle *put_handle;
  35. static int ok = 1;
  36. static struct GNUNET_SCHEDULER_Task *die_task;
  37. static void
  38. do_shutdown (void *cls)
  39. {
  40. if (NULL != die_task)
  41. {
  42. GNUNET_SCHEDULER_cancel (die_task);
  43. die_task = NULL;
  44. }
  45. if (NULL != put_handle)
  46. {
  47. GNUNET_DHT_put_cancel (put_handle);
  48. put_handle = NULL;
  49. }
  50. if (NULL != get_handle)
  51. {
  52. GNUNET_DHT_get_stop (get_handle);
  53. get_handle = NULL;
  54. }
  55. GNUNET_DHT_disconnect (dht_handle);
  56. dht_handle = NULL;
  57. }
  58. static void
  59. end_badly (void *cls)
  60. {
  61. die_task = NULL;
  62. FPRINTF (stderr,
  63. "%s",
  64. "Ending on an unhappy note.\n");
  65. GNUNET_SCHEDULER_shutdown ();
  66. ok = 1;
  67. }
  68. static void
  69. test_get_iterator (void *cls,
  70. struct GNUNET_TIME_Absolute exp,
  71. const struct GNUNET_HashCode *key,
  72. const struct GNUNET_PeerIdentity *get_path,
  73. unsigned int get_path_length,
  74. const struct GNUNET_PeerIdentity *put_path,
  75. unsigned int put_path_length,
  76. enum GNUNET_BLOCK_Type type,
  77. size_t size,
  78. const void *data)
  79. {
  80. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  81. "test_get_iterator called (we got a result), stopping get request!\n");
  82. GNUNET_SCHEDULER_shutdown ();
  83. ok = 0;
  84. }
  85. /**
  86. * Signature of the main function of a task.
  87. *
  88. * @param cls closure
  89. */
  90. static void
  91. test_get (void *cls)
  92. {
  93. struct GNUNET_HashCode hash;
  94. put_handle = NULL;
  95. memset (&hash,
  96. 42,
  97. sizeof (struct GNUNET_HashCode));
  98. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  99. "Called test_get!\n");
  100. GNUNET_assert (dht_handle != NULL);
  101. get_handle = GNUNET_DHT_get_start (dht_handle,
  102. GNUNET_BLOCK_TYPE_TEST,
  103. &hash,
  104. 1,
  105. GNUNET_DHT_RO_NONE,
  106. NULL,
  107. 0,
  108. &test_get_iterator,
  109. NULL);
  110. if (NULL == get_handle)
  111. {
  112. GNUNET_break (0);
  113. ok = 1;
  114. GNUNET_SCHEDULER_shutdown ();
  115. return;
  116. }
  117. }
  118. static void
  119. run (void *cls,
  120. const struct GNUNET_CONFIGURATION_Handle *cfg,
  121. struct GNUNET_TESTING_Peer *peer)
  122. {
  123. struct GNUNET_HashCode hash;
  124. char *data;
  125. size_t data_size = 42;
  126. GNUNET_assert (ok == 1);
  127. GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
  128. NULL);
  129. die_task = GNUNET_SCHEDULER_add_delayed (TOTAL_TIMEOUT,
  130. &end_badly,
  131. NULL);
  132. memset (&hash,
  133. 42,
  134. sizeof (struct GNUNET_HashCode));
  135. data = GNUNET_malloc (data_size);
  136. memset (data, 43, data_size);
  137. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  138. "Called test_put!\n");
  139. dht_handle = GNUNET_DHT_connect (cfg,
  140. 100);
  141. GNUNET_assert (NULL != dht_handle);
  142. put_handle = GNUNET_DHT_put (dht_handle,
  143. &hash,
  144. 1,
  145. GNUNET_DHT_RO_NONE,
  146. GNUNET_BLOCK_TYPE_TEST,
  147. data_size,
  148. data,
  149. GNUNET_TIME_relative_to_absolute (TOTAL_TIMEOUT),
  150. &test_get,
  151. NULL);
  152. GNUNET_free (data);
  153. }
  154. int
  155. main (int argc,
  156. char *argv[])
  157. {
  158. if (0 != GNUNET_TESTING_peer_run ("test-dht-api",
  159. "test_dht_api_data.conf",
  160. &run, NULL))
  161. return 1;
  162. return ok;
  163. }
  164. /* end of test_dht_api.c */