test_fs_search.c 6.7 KB

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