test_fs_search_with_and.c 7.6 KB

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