test_fs_search_with_and.c 7.3 KB

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