test_fs_search_probes.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2012 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 fs/test_fs_search_probes.c
  18. * @brief simple testcase for publish + search operation with probes
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testing_lib.h"
  24. #include "gnunet_fs_service.h"
  25. /**
  26. * File-size we use for testing.
  27. */
  28. #define FILESIZE 1024
  29. /**
  30. * How long until we give up on transmitting the message?
  31. */
  32. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
  33. /**
  34. * How long should our test-content live?
  35. */
  36. #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
  37. static struct GNUNET_TIME_Absolute start;
  38. static struct GNUNET_FS_Handle *fs;
  39. static struct GNUNET_FS_SearchContext *search;
  40. static struct GNUNET_FS_PublishContext *publish;
  41. static struct GNUNET_SCHEDULER_Task *timeout_task;
  42. static int err;
  43. static void
  44. abort_error (void *cls)
  45. {
  46. timeout_task = NULL;
  47. fprintf (stderr,
  48. "Timeout\n");
  49. if (NULL != search)
  50. {
  51. GNUNET_FS_search_stop (search);
  52. search = NULL;
  53. }
  54. if (NULL != publish)
  55. {
  56. GNUNET_FS_publish_stop (publish);
  57. publish = NULL;
  58. }
  59. err = 1;
  60. }
  61. static void
  62. abort_publish_task (void *cls)
  63. {
  64. if (NULL != publish)
  65. {
  66. GNUNET_FS_publish_stop (publish);
  67. publish = NULL;
  68. }
  69. if (NULL != timeout_task)
  70. {
  71. GNUNET_SCHEDULER_cancel (timeout_task);
  72. timeout_task = NULL;
  73. }
  74. }
  75. static void
  76. abort_search_task (void *cls)
  77. {
  78. if (search != NULL)
  79. GNUNET_FS_search_stop (search);
  80. search = NULL;
  81. }
  82. static void *
  83. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
  84. {
  85. const char *keywords[] = {
  86. "down_foo"
  87. };
  88. struct GNUNET_FS_Uri *kuri;
  89. switch (event->status)
  90. {
  91. case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
  92. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  93. "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
  94. (unsigned long long) event->value.publish.completed,
  95. (unsigned long long) event->value.publish.size,
  96. event->value.publish.specifics.progress.depth,
  97. (unsigned long long) event->value.publish.specifics.
  98. progress.offset);
  99. break;
  100. case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
  101. break;
  102. case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
  103. kuri = GNUNET_FS_uri_ksk_create_from_args (1, keywords);
  104. start = GNUNET_TIME_absolute_get ();
  105. search =
  106. GNUNET_FS_search_start (fs, kuri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
  107. "search");
  108. GNUNET_FS_uri_destroy (kuri);
  109. GNUNET_assert (search != NULL);
  110. break;
  111. case GNUNET_FS_STATUS_SEARCH_RESULT:
  112. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Search complete.\n");
  113. break;
  114. case GNUNET_FS_STATUS_PUBLISH_ERROR:
  115. fprintf (stderr, "Error publishing file: %s\n",
  116. event->value.publish.specifics.error.message);
  117. GNUNET_break (0);
  118. GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
  119. break;
  120. case GNUNET_FS_STATUS_SEARCH_ERROR:
  121. fprintf (stderr, "Error searching file: %s\n",
  122. event->value.search.specifics.error.message);
  123. GNUNET_SCHEDULER_add_now (&abort_search_task, NULL);
  124. break;
  125. case GNUNET_FS_STATUS_PUBLISH_START:
  126. GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
  127. GNUNET_assert (NULL == event->value.publish.pctx);
  128. GNUNET_assert (FILESIZE == event->value.publish.size);
  129. GNUNET_assert (0 == event->value.publish.completed);
  130. GNUNET_assert (1 == event->value.publish.anonymity);
  131. break;
  132. case GNUNET_FS_STATUS_PUBLISH_STOPPED:
  133. GNUNET_assert (publish == event->value.publish.pc);
  134. GNUNET_assert (FILESIZE == event->value.publish.size);
  135. GNUNET_assert (1 == event->value.publish.anonymity);
  136. GNUNET_FS_stop (fs);
  137. fs = NULL;
  138. break;
  139. case GNUNET_FS_STATUS_SEARCH_UPDATE:
  140. if ((0 < event->value.search.specifics.update.availability_rank) &&
  141. (0 < event->value.search.specifics.update.availability_certainty))
  142. GNUNET_SCHEDULER_add_now (&abort_search_task, NULL);
  143. break;
  144. case GNUNET_FS_STATUS_SEARCH_START:
  145. GNUNET_assert (search == NULL);
  146. GNUNET_assert (0 == strcmp ("search", event->value.search.cctx));
  147. GNUNET_assert (1 == event->value.search.anonymity);
  148. break;
  149. case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
  150. break;
  151. case GNUNET_FS_STATUS_SEARCH_STOPPED:
  152. GNUNET_assert (search == event->value.search.sc);
  153. GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
  154. break;
  155. default:
  156. fprintf (stderr,
  157. "Unexpected event: %d\n",
  158. event->status);
  159. break;
  160. }
  161. return NULL;
  162. }
  163. static void
  164. run (void *cls,
  165. const struct GNUNET_CONFIGURATION_Handle *cfg,
  166. struct GNUNET_TESTING_Peer *peer)
  167. {
  168. const char *keywords[] = {
  169. "down_foo",
  170. "down_bar"
  171. };
  172. char *buf;
  173. struct GNUNET_CONTAINER_MetaData *meta;
  174. struct GNUNET_FS_Uri *kuri;
  175. struct GNUNET_FS_BlockOptions bo;
  176. struct GNUNET_FS_FileInformation *fi;
  177. size_t i;
  178. fs = GNUNET_FS_start (cfg, "test-fs-search", &progress_cb, NULL,
  179. GNUNET_FS_FLAGS_DO_PROBES,
  180. GNUNET_FS_OPTIONS_END);
  181. GNUNET_assert (NULL != fs);
  182. buf = GNUNET_malloc (FILESIZE);
  183. for (i = 0; i < FILESIZE; i++)
  184. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  185. meta = GNUNET_CONTAINER_meta_data_create ();
  186. kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
  187. bo.content_priority = 42;
  188. bo.anonymity_level = 1;
  189. bo.replication_level = 0;
  190. bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
  191. fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
  192. FILESIZE, buf, kuri, meta,
  193. GNUNET_NO, &bo);
  194. GNUNET_FS_uri_destroy (kuri);
  195. GNUNET_CONTAINER_meta_data_destroy (meta);
  196. GNUNET_assert (NULL != fi);
  197. start = GNUNET_TIME_absolute_get ();
  198. publish =
  199. GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
  200. GNUNET_FS_PUBLISH_OPTION_NONE);
  201. GNUNET_assert (publish != NULL);
  202. timeout_task = GNUNET_SCHEDULER_add_delayed (LIFETIME,
  203. &abort_error, NULL);
  204. }
  205. int
  206. main (int argc, char *argv[])
  207. {
  208. if (0 != GNUNET_TESTING_peer_run ("test-fs-search-probes",
  209. "test_fs_search_data.conf",
  210. &run, NULL))
  211. return 1;
  212. return err;
  213. }
  214. /* end of test_fs_search_probes.c */