test_fs_unindex.c 9.2 KB

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