test_fs_search_persistence.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2004, 2005, 2006, 2008, 2009, 2010 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_persistence.c
  19. * @brief simple testcase for persistence of search operation
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_arm_service.h"
  25. #include "gnunet_fs_service.h"
  26. #define VERBOSE GNUNET_NO
  27. #define START_ARM GNUNET_YES
  28. /**
  29. * File-size we use for testing.
  30. */
  31. #define FILESIZE 1024
  32. /**
  33. * How long until we give up on transmitting the message?
  34. */
  35. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
  36. /**
  37. * How long should our test-content live?
  38. */
  39. #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
  40. struct PeerContext
  41. {
  42. struct GNUNET_CONFIGURATION_Handle *cfg;
  43. struct GNUNET_PeerIdentity id;
  44. #if START_ARM
  45. struct GNUNET_OS_Process *arm_proc;
  46. #endif
  47. };
  48. static struct PeerContext p1;
  49. static struct GNUNET_TIME_Absolute start;
  50. static struct GNUNET_FS_Handle *fs;
  51. static struct GNUNET_FS_SearchContext *search;
  52. static struct GNUNET_FS_PublishContext *publish;
  53. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  54. static void
  55. abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  56. {
  57. GNUNET_FS_publish_stop (publish);
  58. publish = NULL;
  59. }
  60. static void
  61. abort_search_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  62. {
  63. if (search != NULL)
  64. GNUNET_FS_search_stop (search);
  65. search = NULL;
  66. }
  67. static void *
  68. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
  69. static void
  70. restart_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  71. {
  72. GNUNET_FS_stop (fs);
  73. fs = GNUNET_FS_start (cfg, "test-fs-search-persistence", &progress_cb, NULL,
  74. GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
  75. }
  76. /**
  77. * Consider scheduling the restart-task.
  78. * Only runs the restart task once per event
  79. * category.
  80. *
  81. * @param ev type of the event to consider
  82. */
  83. static void
  84. consider_restart (int ev)
  85. {
  86. static int prev[32];
  87. static int off;
  88. int i;
  89. for (i = 0; i < off; i++)
  90. if (prev[i] == ev)
  91. return;
  92. prev[off++] = ev;
  93. GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
  94. &restart_fs_task, NULL);
  95. }
  96. static void *
  97. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
  98. {
  99. const char *keywords[] = {
  100. "down_foo"
  101. };
  102. struct GNUNET_FS_Uri *kuri;
  103. switch (event->status)
  104. {
  105. case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
  106. #if VERBOSE
  107. printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
  108. (unsigned long long) event->value.publish.completed,
  109. (unsigned long long) event->value.publish.size,
  110. event->value.publish.specifics.progress.depth,
  111. (unsigned long long) event->value.publish.specifics.
  112. progress.offset);
  113. #endif
  114. break;
  115. case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
  116. kuri = GNUNET_FS_uri_ksk_create_from_args (1, keywords);
  117. start = GNUNET_TIME_absolute_get ();
  118. GNUNET_FS_search_start (fs, kuri, 1, GNUNET_FS_SEARCH_OPTION_NONE,
  119. "search");
  120. GNUNET_FS_uri_destroy (kuri);
  121. GNUNET_assert (search != NULL);
  122. break;
  123. case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
  124. if (event->value.publish.pc == publish)
  125. publish = NULL;
  126. break;
  127. case GNUNET_FS_STATUS_PUBLISH_RESUME:
  128. if (NULL == publish)
  129. publish = event->value.publish.pc;
  130. break;
  131. case GNUNET_FS_STATUS_SEARCH_RESULT:
  132. /* FIXME: consider_restart (event->status); cannot be tested with
  133. * search result since we exit here after the first one... */
  134. #if VERBOSE
  135. printf ("Search complete.\n");
  136. #endif
  137. GNUNET_SCHEDULER_add_continuation (&abort_search_task, NULL,
  138. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  139. break;
  140. case GNUNET_FS_STATUS_PUBLISH_ERROR:
  141. fprintf (stderr, "Error publishing file: %s\n",
  142. event->value.publish.specifics.error.message);
  143. GNUNET_break (0);
  144. GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
  145. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  146. break;
  147. case GNUNET_FS_STATUS_SEARCH_ERROR:
  148. fprintf (stderr, "Error searching file: %s\n",
  149. event->value.search.specifics.error.message);
  150. GNUNET_SCHEDULER_add_continuation (&abort_search_task, NULL,
  151. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  152. break;
  153. case GNUNET_FS_STATUS_SEARCH_SUSPEND:
  154. if (event->value.search.sc == search)
  155. search = NULL;
  156. break;
  157. case GNUNET_FS_STATUS_SEARCH_RESUME:
  158. if (NULL == search)
  159. {
  160. search = event->value.search.sc;
  161. return "search";
  162. }
  163. break;
  164. case GNUNET_FS_STATUS_PUBLISH_START:
  165. GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
  166. GNUNET_assert (NULL == event->value.publish.pctx);
  167. GNUNET_assert (FILESIZE == event->value.publish.size);
  168. GNUNET_assert (0 == event->value.publish.completed);
  169. GNUNET_assert (1 == event->value.publish.anonymity);
  170. break;
  171. case GNUNET_FS_STATUS_PUBLISH_STOPPED:
  172. GNUNET_assert (publish == event->value.publish.pc);
  173. GNUNET_assert (FILESIZE == event->value.publish.size);
  174. GNUNET_assert (1 == event->value.publish.anonymity);
  175. GNUNET_FS_stop (fs);
  176. fs = NULL;
  177. break;
  178. case GNUNET_FS_STATUS_SEARCH_START:
  179. consider_restart (event->status);
  180. GNUNET_assert (search == NULL);
  181. search = event->value.search.sc;
  182. GNUNET_assert (0 == strcmp ("search", event->value.search.cctx));
  183. GNUNET_assert (1 == event->value.search.anonymity);
  184. break;
  185. case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
  186. break;
  187. case GNUNET_FS_STATUS_SEARCH_STOPPED:
  188. GNUNET_assert (search == event->value.search.sc);
  189. GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
  190. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  191. search = NULL;
  192. break;
  193. default:
  194. fprintf (stderr, "Unexpected event: %d\n", event->status);
  195. break;
  196. }
  197. return NULL;
  198. }
  199. static void
  200. setup_peer (struct PeerContext *p, const char *cfgname)
  201. {
  202. p->cfg = GNUNET_CONFIGURATION_create ();
  203. #if START_ARM
  204. p->arm_proc =
  205. GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
  206. "gnunet-service-arm",
  207. #if VERBOSE
  208. "-L", "DEBUG",
  209. #endif
  210. "-c", cfgname, NULL);
  211. #endif
  212. GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
  213. }
  214. static void
  215. stop_arm (struct PeerContext *p)
  216. {
  217. #if START_ARM
  218. if (NULL != p->arm_proc)
  219. {
  220. if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
  221. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  222. if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
  223. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
  224. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
  225. GNUNET_OS_process_get_pid (p->arm_proc));
  226. GNUNET_OS_process_close (p->arm_proc);
  227. p->arm_proc = NULL;
  228. }
  229. #endif
  230. GNUNET_CONFIGURATION_destroy (p->cfg);
  231. }
  232. static void
  233. run (void *cls, char *const *args, const char *cfgfile,
  234. const struct GNUNET_CONFIGURATION_Handle *c)
  235. {
  236. const char *keywords[] = {
  237. "down_foo",
  238. "down_bar"
  239. };
  240. char *buf;
  241. struct GNUNET_CONTAINER_MetaData *meta;
  242. struct GNUNET_FS_Uri *kuri;
  243. struct GNUNET_FS_FileInformation *fi;
  244. size_t i;
  245. struct GNUNET_FS_BlockOptions bo;
  246. cfg = c;
  247. setup_peer (&p1, "test_fs_search_data.conf");
  248. fs = GNUNET_FS_start (cfg, "test-fs-search-persistence", &progress_cb, NULL,
  249. GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
  250. GNUNET_assert (NULL != fs);
  251. buf = GNUNET_malloc (FILESIZE);
  252. for (i = 0; i < FILESIZE; i++)
  253. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  254. meta = GNUNET_CONTAINER_meta_data_create ();
  255. kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
  256. bo.content_priority = 42;
  257. bo.anonymity_level = 1;
  258. bo.replication_level = 0;
  259. bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
  260. fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
  261. FILESIZE, buf, kuri, meta,
  262. GNUNET_NO, &bo);
  263. GNUNET_FS_uri_destroy (kuri);
  264. GNUNET_CONTAINER_meta_data_destroy (meta);
  265. GNUNET_assert (NULL != fi);
  266. start = GNUNET_TIME_absolute_get ();
  267. publish =
  268. GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
  269. GNUNET_FS_PUBLISH_OPTION_NONE);
  270. GNUNET_assert (publish != NULL);
  271. }
  272. int
  273. main (int argc, char *argv[])
  274. {
  275. char *const argvx[] = {
  276. "test-fs-search-persistence",
  277. "-c",
  278. "test_fs_search_data.conf",
  279. #if VERBOSE
  280. "-L", "DEBUG",
  281. #endif
  282. NULL
  283. };
  284. struct GNUNET_GETOPT_CommandLineOption options[] = {
  285. GNUNET_GETOPT_OPTION_END
  286. };
  287. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-search/");
  288. GNUNET_log_setup ("test_fs_search_persistence",
  289. #if VERBOSE
  290. "DEBUG",
  291. #else
  292. "WARNING",
  293. #endif
  294. NULL);
  295. GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1, argvx,
  296. "test-fs-search-persistence", "nohelp", options, &run,
  297. NULL);
  298. stop_arm (&p1);
  299. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-search/");
  300. return 0;
  301. }
  302. /* end of test_fs_search_persistence.c */