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