test_fs_download_persistence.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_download_persistence.c
  19. * @brief simple testcase for persistence of simple download operation
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_testing_lib.h"
  25. #include "gnunet_fs_service.h"
  26. /**
  27. * File-size we use for testing.
  28. */
  29. #define FILESIZE (1024 * 1024 * 2)
  30. /**
  31. * How long until we give up on transmitting the message?
  32. */
  33. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
  34. /**
  35. * How long should our test-content live?
  36. */
  37. #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
  38. static struct GNUNET_TIME_Absolute start;
  39. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  40. static struct GNUNET_FS_Handle *fs;
  41. static struct GNUNET_FS_DownloadContext *download;
  42. static struct GNUNET_FS_PublishContext *publish;
  43. static GNUNET_SCHEDULER_TaskIdentifier timeout_kill;
  44. static char *fn;
  45. static int err;
  46. static void
  47. timeout_kill_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  48. {
  49. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
  50. if (download != NULL)
  51. {
  52. GNUNET_FS_download_stop (download, GNUNET_YES);
  53. download = NULL;
  54. }
  55. else if (publish != NULL)
  56. {
  57. GNUNET_FS_publish_stop (publish);
  58. publish = NULL;
  59. }
  60. timeout_kill = GNUNET_SCHEDULER_NO_TASK;
  61. err = 1;
  62. }
  63. static void
  64. abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  65. {
  66. if (publish != NULL)
  67. {
  68. GNUNET_FS_publish_stop (publish);
  69. publish = NULL;
  70. }
  71. }
  72. static void
  73. abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  74. {
  75. uint64_t size;
  76. if (download != NULL)
  77. {
  78. GNUNET_FS_download_stop (download, GNUNET_YES);
  79. download = NULL;
  80. }
  81. GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
  82. GNUNET_assert (size == FILESIZE);
  83. GNUNET_DISK_directory_remove (fn);
  84. GNUNET_free (fn);
  85. fn = NULL;
  86. GNUNET_SCHEDULER_cancel (timeout_kill);
  87. timeout_kill = GNUNET_SCHEDULER_NO_TASK;
  88. }
  89. static void *
  90. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
  91. static void
  92. restart_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  93. {
  94. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Restarting FS.\n");
  95. GNUNET_FS_stop (fs);
  96. fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
  97. GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
  98. }
  99. /**
  100. * Consider scheduling the restart-task.
  101. * Only runs the restart task once per event
  102. * category.
  103. *
  104. * @param ev type of the event to consider
  105. */
  106. static void
  107. consider_restart (int ev)
  108. {
  109. static int prev[32];
  110. static int off;
  111. int i;
  112. for (i = 0; i < off; i++)
  113. if (prev[i] == ev)
  114. return;
  115. prev[off++] = ev;
  116. GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_URGENT,
  117. &restart_fs_task, NULL);
  118. }
  119. static void *
  120. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
  121. {
  122. switch (event->status)
  123. {
  124. case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
  125. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  126. "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
  127. (unsigned long long) event->value.publish.completed,
  128. (unsigned long long) event->value.publish.size,
  129. event->value.publish.specifics.progress.depth,
  130. (unsigned long long) event->value.publish.specifics.
  131. progress.offset);
  132. break;
  133. case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
  134. break;
  135. case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
  136. printf ("Publishing complete, %llu kbps.\n",
  137. (unsigned long long) (FILESIZE * 1000000LL /
  138. (1 +
  139. GNUNET_TIME_absolute_get_duration
  140. (start).rel_value_us) / 1024LL));
  141. fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
  142. start = GNUNET_TIME_absolute_get ();
  143. GNUNET_assert (download == NULL);
  144. GNUNET_FS_download_start (fs,
  145. event->value.publish.specifics.completed.chk_uri,
  146. NULL, fn, NULL, 0, FILESIZE, 1,
  147. GNUNET_FS_DOWNLOAD_OPTION_NONE, "download", NULL);
  148. break;
  149. case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
  150. consider_restart (event->status);
  151. printf ("Download complete, %llu kbps.\n",
  152. (unsigned long long) (FILESIZE * 1000000LL /
  153. (1 +
  154. GNUNET_TIME_absolute_get_duration
  155. (start).rel_value_us) / 1024LL));
  156. GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
  157. break;
  158. case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
  159. consider_restart (event->status);
  160. GNUNET_assert (download == event->value.download.dc);
  161. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  162. "Download is progressing (%llu/%llu at level %u off %llu)...\n",
  163. (unsigned long long) event->value.download.completed,
  164. (unsigned long long) event->value.download.size,
  165. event->value.download.specifics.progress.depth,
  166. (unsigned long long) event->value.download.specifics.
  167. progress.offset);
  168. break;
  169. case GNUNET_FS_STATUS_PUBLISH_ERROR:
  170. FPRINTF (stderr, "Error publishing file: %s\n",
  171. event->value.publish.specifics.error.message);
  172. GNUNET_break (0);
  173. GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
  174. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  175. break;
  176. case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
  177. FPRINTF (stderr, "Error downloading file: %s\n",
  178. event->value.download.specifics.error.message);
  179. GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
  180. break;
  181. case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
  182. GNUNET_assert (event->value.publish.pc == publish);
  183. publish = NULL;
  184. break;
  185. case GNUNET_FS_STATUS_PUBLISH_RESUME:
  186. GNUNET_assert (NULL == publish);
  187. publish = event->value.publish.pc;
  188. break;
  189. case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
  190. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download suspended.\n");
  191. GNUNET_assert (event->value.download.dc == download);
  192. download = NULL;
  193. break;
  194. case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
  195. GNUNET_assert (NULL == download);
  196. download = event->value.download.dc;
  197. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download resumed.\n");
  198. break;
  199. case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
  200. consider_restart (event->status);
  201. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download active.\n");
  202. break;
  203. case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
  204. consider_restart (event->status);
  205. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download inactive.\n");
  206. break;
  207. case GNUNET_FS_STATUS_PUBLISH_START:
  208. GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
  209. GNUNET_assert (NULL == event->value.publish.pctx);
  210. GNUNET_assert (FILESIZE == event->value.publish.size);
  211. GNUNET_assert (0 == event->value.publish.completed);
  212. GNUNET_assert (1 == event->value.publish.anonymity);
  213. break;
  214. case GNUNET_FS_STATUS_PUBLISH_STOPPED:
  215. GNUNET_assert (publish == event->value.publish.pc);
  216. GNUNET_assert (FILESIZE == event->value.publish.size);
  217. GNUNET_assert (1 == event->value.publish.anonymity);
  218. GNUNET_FS_stop (fs);
  219. fs = NULL;
  220. break;
  221. case GNUNET_FS_STATUS_DOWNLOAD_START:
  222. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download started.\n");
  223. consider_restart (event->status);
  224. GNUNET_assert (download == NULL);
  225. download = event->value.download.dc;
  226. GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
  227. GNUNET_assert (NULL == event->value.download.pctx);
  228. GNUNET_assert (NULL != event->value.download.uri);
  229. GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
  230. GNUNET_assert (FILESIZE == event->value.download.size);
  231. GNUNET_assert (0 == event->value.download.completed);
  232. GNUNET_assert (1 == event->value.download.anonymity);
  233. break;
  234. case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
  235. GNUNET_assert (download == event->value.download.dc);
  236. GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
  237. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  238. download = NULL;
  239. break;
  240. default:
  241. printf ("Unexpected event: %d\n", event->status);
  242. break;
  243. }
  244. return NULL;
  245. }
  246. static void
  247. run (void *cls,
  248. const struct GNUNET_CONFIGURATION_Handle *c,
  249. struct GNUNET_TESTING_Peer *peer)
  250. {
  251. const char *keywords[] = {
  252. "down_foo",
  253. "down_bar",
  254. };
  255. char *buf;
  256. struct GNUNET_CONTAINER_MetaData *meta;
  257. struct GNUNET_FS_Uri *kuri;
  258. struct GNUNET_FS_FileInformation *fi;
  259. size_t i;
  260. struct GNUNET_FS_BlockOptions bo;
  261. cfg = c;
  262. fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
  263. GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
  264. GNUNET_assert (NULL != fs);
  265. buf = GNUNET_malloc (FILESIZE);
  266. for (i = 0; i < FILESIZE; i++)
  267. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  268. meta = GNUNET_CONTAINER_meta_data_create ();
  269. kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
  270. bo.content_priority = 42;
  271. bo.anonymity_level = 1;
  272. bo.replication_level = 0;
  273. bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
  274. fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
  275. FILESIZE, buf, kuri, meta,
  276. GNUNET_NO, &bo);
  277. GNUNET_FS_uri_destroy (kuri);
  278. GNUNET_CONTAINER_meta_data_destroy (meta);
  279. GNUNET_assert (NULL != fi);
  280. timeout_kill =
  281. GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
  282. start = GNUNET_TIME_absolute_get ();
  283. publish =
  284. GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
  285. GNUNET_FS_PUBLISH_OPTION_NONE);
  286. GNUNET_assert (publish != NULL);
  287. }
  288. int
  289. main (int argc, char *argv[])
  290. {
  291. if (0 != GNUNET_TESTING_peer_run ("test-fs-download-persistence",
  292. "test_fs_download_data.conf",
  293. &run, NULL))
  294. return 1;
  295. return err;
  296. }
  297. /* end of test_fs_download_persistence.c */