test_fs_search.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2004, 2005, 2006, 2008, 2009 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.c
  19. * @brief simple testcase for simple publish + 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 void
  54. abort_publish_task (void *cls,
  55. 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,
  62. const struct GNUNET_SCHEDULER_TaskContext *tc)
  63. {
  64. if (search != NULL)
  65. GNUNET_FS_search_stop (search);
  66. search = NULL;
  67. }
  68. static void *
  69. progress_cb (void *cls,
  70. const struct GNUNET_FS_ProgressInfo *event)
  71. {
  72. const char *keywords[] = {
  73. "down_foo"
  74. };
  75. struct GNUNET_FS_Uri *kuri;
  76. switch (event->status)
  77. {
  78. case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
  79. #if VERBOSE
  80. printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
  81. (unsigned long long) event->value.publish.completed,
  82. (unsigned long long) event->value.publish.size,
  83. event->value.publish.specifics.progress.depth,
  84. (unsigned long long) event->value.publish.specifics.progress.offset);
  85. #endif
  86. break;
  87. case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
  88. kuri = GNUNET_FS_uri_ksk_create_from_args (1, keywords);
  89. start = GNUNET_TIME_absolute_get ();
  90. search = GNUNET_FS_search_start (fs,
  91. kuri,
  92. 1,
  93. GNUNET_FS_SEARCH_OPTION_NONE,
  94. "search");
  95. GNUNET_FS_uri_destroy (kuri);
  96. GNUNET_assert (search != NULL);
  97. break;
  98. case GNUNET_FS_STATUS_SEARCH_RESULT:
  99. #if VERBOSE
  100. printf ("Search complete.\n");
  101. #endif
  102. GNUNET_SCHEDULER_add_continuation (&abort_search_task,
  103. NULL,
  104. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  105. break;
  106. case GNUNET_FS_STATUS_PUBLISH_ERROR:
  107. fprintf (stderr,
  108. "Error publishing file: %s\n",
  109. event->value.publish.specifics.error.message);
  110. GNUNET_break (0);
  111. GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
  112. NULL,
  113. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  114. break;
  115. case GNUNET_FS_STATUS_SEARCH_ERROR:
  116. fprintf (stderr,
  117. "Error searching file: %s\n",
  118. event->value.search.specifics.error.message);
  119. GNUNET_SCHEDULER_add_continuation (&abort_search_task,
  120. NULL,
  121. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  122. break;
  123. case GNUNET_FS_STATUS_PUBLISH_START:
  124. GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
  125. GNUNET_assert (NULL == event->value.publish.pctx);
  126. GNUNET_assert (FILESIZE == event->value.publish.size);
  127. GNUNET_assert (0 == event->value.publish.completed);
  128. GNUNET_assert (1 == event->value.publish.anonymity);
  129. break;
  130. case GNUNET_FS_STATUS_PUBLISH_STOPPED:
  131. GNUNET_assert (publish == event->value.publish.pc);
  132. GNUNET_assert (FILESIZE == event->value.publish.size);
  133. GNUNET_assert (1 == event->value.publish.anonymity);
  134. GNUNET_FS_stop (fs);
  135. fs = NULL;
  136. break;
  137. case GNUNET_FS_STATUS_SEARCH_START:
  138. GNUNET_assert (search == NULL);
  139. GNUNET_assert (0 == strcmp ("search", event->value.search.cctx));
  140. GNUNET_assert (1 == event->value.search.anonymity);
  141. break;
  142. case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
  143. break;
  144. case GNUNET_FS_STATUS_SEARCH_STOPPED:
  145. GNUNET_assert (search == event->value.search.sc);
  146. GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
  147. NULL,
  148. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  149. break;
  150. default:
  151. fprintf (stderr,
  152. "Unexpected event: %d\n",
  153. event->status);
  154. break;
  155. }
  156. return NULL;
  157. }
  158. static void
  159. setup_peer (struct PeerContext *p, const char *cfgname)
  160. {
  161. p->cfg = GNUNET_CONFIGURATION_create ();
  162. #if START_ARM
  163. p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
  164. "gnunet-service-arm",
  165. #if VERBOSE
  166. "-L", "DEBUG",
  167. #endif
  168. "-c", cfgname, NULL);
  169. #endif
  170. GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
  171. }
  172. static void
  173. stop_arm (struct PeerContext *p)
  174. {
  175. #if START_ARM
  176. if (NULL != p->arm_proc)
  177. {
  178. if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
  179. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  180. if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
  181. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
  182. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  183. "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
  184. GNUNET_OS_process_close (p->arm_proc);
  185. p->arm_proc = NULL;
  186. }
  187. #endif
  188. GNUNET_CONFIGURATION_destroy (p->cfg);
  189. }
  190. static void
  191. run (void *cls,
  192. char *const *args,
  193. const char *cfgfile,
  194. const struct GNUNET_CONFIGURATION_Handle *cfg)
  195. {
  196. const char *keywords[] = {
  197. "down_foo",
  198. "down_bar"
  199. };
  200. char *buf;
  201. struct GNUNET_CONTAINER_MetaData *meta;
  202. struct GNUNET_FS_Uri *kuri;
  203. struct GNUNET_FS_BlockOptions bo;
  204. struct GNUNET_FS_FileInformation *fi;
  205. size_t i;
  206. setup_peer (&p1, "test_fs_search_data.conf");
  207. fs = GNUNET_FS_start (cfg,
  208. "test-fs-search",
  209. &progress_cb,
  210. NULL,
  211. GNUNET_FS_FLAGS_NONE,
  212. GNUNET_FS_OPTIONS_END);
  213. GNUNET_assert (NULL != fs);
  214. buf = GNUNET_malloc (FILESIZE);
  215. for (i = 0; i < FILESIZE; i++)
  216. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  217. meta = GNUNET_CONTAINER_meta_data_create ();
  218. kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
  219. bo.content_priority = 42;
  220. bo.anonymity_level = 1;
  221. bo.replication_level = 0;
  222. bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
  223. fi = GNUNET_FS_file_information_create_from_data (fs,
  224. "publish-context",
  225. FILESIZE,
  226. buf,
  227. kuri,
  228. meta,
  229. GNUNET_NO,
  230. &bo);
  231. GNUNET_FS_uri_destroy (kuri);
  232. GNUNET_CONTAINER_meta_data_destroy (meta);
  233. GNUNET_assert (NULL != fi);
  234. start = GNUNET_TIME_absolute_get ();
  235. publish = GNUNET_FS_publish_start (fs,
  236. fi,
  237. NULL, NULL, NULL,
  238. GNUNET_FS_PUBLISH_OPTION_NONE);
  239. GNUNET_assert (publish != NULL);
  240. }
  241. int
  242. main (int argc, char *argv[])
  243. {
  244. char *const argvx[] = {
  245. "test-fs-search",
  246. "-c",
  247. "test_fs_search_data.conf",
  248. #if VERBOSE
  249. "-L", "DEBUG",
  250. #endif
  251. NULL
  252. };
  253. struct GNUNET_GETOPT_CommandLineOption options[] = {
  254. GNUNET_GETOPT_OPTION_END
  255. };
  256. GNUNET_log_setup ("test_fs_search",
  257. #if VERBOSE
  258. "DEBUG",
  259. #else
  260. "WARNING",
  261. #endif
  262. NULL);
  263. GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
  264. argvx, "test-fs-search",
  265. "nohelp", options, &run, NULL);
  266. stop_arm (&p1);
  267. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-search/");
  268. return 0;
  269. }
  270. /* end of test_fs_search.c */