test_fs_search_persistence.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 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_persistence.c
  18. * @brief simple testcase for persistence of 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 const struct GNUNET_CONFIGURATION_Handle *cfg;
  42. static struct GNUNET_SCHEDULER_Task *timeout_task;
  43. static int err;
  44. static void
  45. abort_error (void *cls)
  46. {
  47. timeout_task = NULL;
  48. fprintf (stderr,
  49. "Timeout\n");
  50. if (NULL != search)
  51. {
  52. GNUNET_FS_search_stop (search);
  53. search = NULL;
  54. }
  55. if (NULL != publish)
  56. {
  57. GNUNET_FS_publish_stop (publish);
  58. publish = NULL;
  59. }
  60. err = 1;
  61. }
  62. static void
  63. abort_publish_task (void *cls)
  64. {
  65. if (NULL != publish)
  66. {
  67. GNUNET_FS_publish_stop (publish);
  68. publish = NULL;
  69. }
  70. if (NULL != timeout_task)
  71. {
  72. GNUNET_SCHEDULER_cancel (timeout_task);
  73. timeout_task = NULL;
  74. }
  75. }
  76. static void
  77. abort_search_task (void *cls)
  78. {
  79. if (NULL != search)
  80. {
  81. GNUNET_FS_search_stop (search);
  82. search = NULL;
  83. }
  84. }
  85. static void *
  86. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
  87. static void
  88. restart_fs_task (void *cls)
  89. {
  90. GNUNET_FS_stop (fs);
  91. fs = GNUNET_FS_start (cfg, "test-fs-search-persistence", &progress_cb, NULL,
  92. GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
  93. }
  94. /**
  95. * Consider scheduling the restart-task.
  96. * Only runs the restart task once per event
  97. * category.
  98. *
  99. * @param ev type of the event to consider
  100. */
  101. static void
  102. consider_restart (int ev)
  103. {
  104. static int prev[32];
  105. static int off;
  106. int i;
  107. for (i = 0; i < off; i++)
  108. if (prev[i] == ev)
  109. return;
  110. prev[off++] = ev;
  111. GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
  112. &restart_fs_task, NULL);
  113. }
  114. static void *
  115. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
  116. {
  117. const char *keywords[] = {
  118. "down_foo"
  119. };
  120. struct GNUNET_FS_Uri *kuri;
  121. switch (event->status)
  122. {
  123. case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
  124. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  125. "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
  126. (unsigned long long) event->value.publish.completed,
  127. (unsigned long long) event->value.publish.size,
  128. event->value.publish.specifics.progress.depth,
  129. (unsigned long long) event->value.publish.specifics.
  130. progress.offset);
  131. break;
  132. case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
  133. break;
  134. case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
  135. kuri = GNUNET_FS_uri_ksk_create_from_args (1, keywords);
  136. start = GNUNET_TIME_absolute_get ();
  137. GNUNET_FS_search_start (fs, kuri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
  138. "search");
  139. GNUNET_FS_uri_destroy (kuri);
  140. GNUNET_assert (search != NULL);
  141. break;
  142. case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
  143. if (event->value.publish.pc == publish)
  144. publish = NULL;
  145. break;
  146. case GNUNET_FS_STATUS_PUBLISH_RESUME:
  147. if (NULL == publish)
  148. publish = event->value.publish.pc;
  149. break;
  150. case GNUNET_FS_STATUS_SEARCH_RESULT:
  151. /* FIXME: consider_restart (event->status); cannot be tested with
  152. * search result since we exit here after the first one... */
  153. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  154. "Search complete.\n");
  155. GNUNET_SCHEDULER_add_now (&abort_search_task, NULL);
  156. break;
  157. case GNUNET_FS_STATUS_PUBLISH_ERROR:
  158. fprintf (stderr, "Error publishing file: %s\n",
  159. event->value.publish.specifics.error.message);
  160. GNUNET_break (0);
  161. GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
  162. break;
  163. case GNUNET_FS_STATUS_SEARCH_ERROR:
  164. fprintf (stderr, "Error searching file: %s\n",
  165. event->value.search.specifics.error.message);
  166. GNUNET_SCHEDULER_add_now (&abort_search_task, NULL);
  167. break;
  168. case GNUNET_FS_STATUS_SEARCH_SUSPEND:
  169. if (event->value.search.sc == search)
  170. search = NULL;
  171. break;
  172. case GNUNET_FS_STATUS_SEARCH_RESUME:
  173. if (NULL == search)
  174. {
  175. search = event->value.search.sc;
  176. return "search";
  177. }
  178. break;
  179. case GNUNET_FS_STATUS_PUBLISH_START:
  180. GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
  181. GNUNET_assert (NULL == event->value.publish.pctx);
  182. GNUNET_assert (FILESIZE == event->value.publish.size);
  183. GNUNET_assert (0 == event->value.publish.completed);
  184. GNUNET_assert (1 == event->value.publish.anonymity);
  185. break;
  186. case GNUNET_FS_STATUS_PUBLISH_STOPPED:
  187. GNUNET_assert (publish == event->value.publish.pc);
  188. GNUNET_assert (FILESIZE == event->value.publish.size);
  189. GNUNET_assert (1 == event->value.publish.anonymity);
  190. GNUNET_FS_stop (fs);
  191. fs = NULL;
  192. break;
  193. case GNUNET_FS_STATUS_SEARCH_START:
  194. consider_restart (event->status);
  195. GNUNET_assert (search == NULL);
  196. search = event->value.search.sc;
  197. GNUNET_assert (0 == strcmp ("search", event->value.search.cctx));
  198. GNUNET_assert (1 == event->value.search.anonymity);
  199. break;
  200. case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
  201. break;
  202. case GNUNET_FS_STATUS_SEARCH_STOPPED:
  203. GNUNET_assert (search == event->value.search.sc);
  204. GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
  205. search = NULL;
  206. break;
  207. default:
  208. fprintf (stderr, "Unexpected event: %d\n", event->status);
  209. break;
  210. }
  211. return NULL;
  212. }
  213. static void
  214. run (void *cls,
  215. const struct GNUNET_CONFIGURATION_Handle *c,
  216. struct GNUNET_TESTING_Peer *peer)
  217. {
  218. const char *keywords[] = {
  219. "down_foo",
  220. "down_bar"
  221. };
  222. char *buf;
  223. struct GNUNET_CONTAINER_MetaData *meta;
  224. struct GNUNET_FS_Uri *kuri;
  225. struct GNUNET_FS_FileInformation *fi;
  226. size_t i;
  227. struct GNUNET_FS_BlockOptions bo;
  228. cfg = c;
  229. fs = GNUNET_FS_start (cfg, "test-fs-search-persistence", &progress_cb, NULL,
  230. GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
  231. GNUNET_assert (NULL != fs);
  232. buf = GNUNET_malloc (FILESIZE);
  233. for (i = 0; i < FILESIZE; i++)
  234. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  235. meta = GNUNET_CONTAINER_meta_data_create ();
  236. kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
  237. bo.content_priority = 42;
  238. bo.anonymity_level = 1;
  239. bo.replication_level = 0;
  240. bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
  241. fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
  242. FILESIZE, buf, kuri, meta,
  243. GNUNET_NO, &bo);
  244. GNUNET_FS_uri_destroy (kuri);
  245. GNUNET_CONTAINER_meta_data_destroy (meta);
  246. GNUNET_assert (NULL != fi);
  247. start = GNUNET_TIME_absolute_get ();
  248. publish =
  249. GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
  250. GNUNET_FS_PUBLISH_OPTION_NONE);
  251. GNUNET_assert (publish != NULL);
  252. timeout_task = GNUNET_SCHEDULER_add_delayed (LIFETIME,
  253. &abort_error, NULL);
  254. }
  255. int
  256. main (int argc, char *argv[])
  257. {
  258. if (0 != GNUNET_TESTING_peer_run ("test-fs-search-persistence",
  259. "test_fs_search_data.conf",
  260. &run, NULL))
  261. return 1;
  262. return err;
  263. }
  264. /* end of test_fs_search_persistence.c */