test_fs_download_persistence.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your 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. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file fs/test_fs_download_persistence.c
  18. * @brief simple testcase for persistence of simple download operation
  19. * @author Christian Grothoff
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testing_lib.h"
  24. #include "gnunet_fs_service.h"
  25. /**
  26. * File-size we use for testing.
  27. */
  28. #define FILESIZE (1024 * 1024 * 2)
  29. /**
  30. * How long until we give up on transmitting the message?
  31. */
  32. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
  33. /**
  34. * How long should our test-content live?
  35. */
  36. #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
  37. static struct GNUNET_TIME_Absolute start;
  38. static const struct GNUNET_CONFIGURATION_Handle *cfg;
  39. static struct GNUNET_FS_Handle *fs;
  40. static struct GNUNET_FS_DownloadContext *download;
  41. static struct GNUNET_FS_PublishContext *publish;
  42. static struct GNUNET_SCHEDULER_Task *timeout_kill;
  43. static char *fn;
  44. static int err;
  45. static void
  46. timeout_kill_task (void *cls)
  47. {
  48. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
  49. if (download != NULL)
  50. {
  51. GNUNET_FS_download_stop (download, GNUNET_YES);
  52. download = NULL;
  53. }
  54. else if (publish != NULL)
  55. {
  56. GNUNET_FS_publish_stop (publish);
  57. publish = NULL;
  58. }
  59. timeout_kill = NULL;
  60. err = 1;
  61. }
  62. static void
  63. abort_publish_task (void *cls)
  64. {
  65. if (publish != NULL)
  66. {
  67. GNUNET_FS_publish_stop (publish);
  68. publish = NULL;
  69. }
  70. }
  71. static void
  72. abort_download_task (void *cls)
  73. {
  74. uint64_t size;
  75. if (download != NULL)
  76. {
  77. GNUNET_FS_download_stop (download, GNUNET_YES);
  78. download = NULL;
  79. }
  80. GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES,
  81. 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 = NULL;
  88. }
  89. static void *
  90. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event);
  91. static void
  92. restart_fs_task (void *cls)
  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. printf ("Download complete, %llu kbps.\n",
  151. (unsigned long long) (FILESIZE * 1000000LL
  152. / (1
  153. + GNUNET_TIME_absolute_get_duration
  154. (start).rel_value_us) / 1024LL));
  155. GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
  156. break;
  157. case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
  158. consider_restart (event->status);
  159. GNUNET_assert (download == event->value.download.dc);
  160. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  161. "Download is progressing (%llu/%llu at level %u off %llu)...\n",
  162. (unsigned long long) event->value.download.completed,
  163. (unsigned long long) event->value.download.size,
  164. event->value.download.specifics.progress.depth,
  165. (unsigned long long) event->value.download.specifics.
  166. progress.offset);
  167. break;
  168. case GNUNET_FS_STATUS_PUBLISH_ERROR:
  169. fprintf (stderr, "Error publishing file: %s\n",
  170. event->value.publish.specifics.error.message);
  171. GNUNET_break (0);
  172. GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
  173. break;
  174. case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
  175. fprintf (stderr, "Error downloading file: %s\n",
  176. event->value.download.specifics.error.message);
  177. GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
  178. break;
  179. case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
  180. GNUNET_assert (event->value.publish.pc == publish);
  181. publish = NULL;
  182. break;
  183. case GNUNET_FS_STATUS_PUBLISH_RESUME:
  184. GNUNET_assert (NULL == publish);
  185. publish = event->value.publish.pc;
  186. break;
  187. case GNUNET_FS_STATUS_DOWNLOAD_SUSPEND:
  188. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download suspended.\n");
  189. GNUNET_assert (event->value.download.dc == download);
  190. download = NULL;
  191. break;
  192. case GNUNET_FS_STATUS_DOWNLOAD_RESUME:
  193. GNUNET_assert (NULL == download);
  194. download = event->value.download.dc;
  195. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download resumed.\n");
  196. break;
  197. case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
  198. consider_restart (event->status);
  199. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download active.\n");
  200. break;
  201. case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
  202. consider_restart (event->status);
  203. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download inactive.\n");
  204. break;
  205. case GNUNET_FS_STATUS_PUBLISH_START:
  206. GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
  207. GNUNET_assert (NULL == event->value.publish.pctx);
  208. GNUNET_assert (FILESIZE == event->value.publish.size);
  209. GNUNET_assert (0 == event->value.publish.completed);
  210. GNUNET_assert (1 == event->value.publish.anonymity);
  211. break;
  212. case GNUNET_FS_STATUS_PUBLISH_STOPPED:
  213. GNUNET_assert (publish == event->value.publish.pc);
  214. GNUNET_assert (FILESIZE == event->value.publish.size);
  215. GNUNET_assert (1 == event->value.publish.anonymity);
  216. GNUNET_FS_stop (fs);
  217. fs = NULL;
  218. break;
  219. case GNUNET_FS_STATUS_DOWNLOAD_START:
  220. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Download started.\n");
  221. consider_restart (event->status);
  222. GNUNET_assert (download == NULL);
  223. download = event->value.download.dc;
  224. GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
  225. GNUNET_assert (NULL == event->value.download.pctx);
  226. GNUNET_assert (NULL != event->value.download.uri);
  227. GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
  228. GNUNET_assert (FILESIZE == event->value.download.size);
  229. GNUNET_assert (0 == event->value.download.completed);
  230. GNUNET_assert (1 == event->value.download.anonymity);
  231. break;
  232. case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
  233. GNUNET_assert (download == event->value.download.dc);
  234. GNUNET_SCHEDULER_add_now (&abort_publish_task, NULL);
  235. download = NULL;
  236. break;
  237. default:
  238. printf ("Unexpected event: %d\n", event->status);
  239. break;
  240. }
  241. return NULL;
  242. }
  243. static void
  244. run (void *cls,
  245. const struct GNUNET_CONFIGURATION_Handle *c,
  246. struct GNUNET_TESTING_Peer *peer)
  247. {
  248. const char *keywords[] = {
  249. "down_foo",
  250. "down_bar",
  251. };
  252. char *buf;
  253. struct GNUNET_CONTAINER_MetaData *meta;
  254. struct GNUNET_FS_Uri *kuri;
  255. struct GNUNET_FS_FileInformation *fi;
  256. size_t i;
  257. struct GNUNET_FS_BlockOptions bo;
  258. cfg = c;
  259. fs = GNUNET_FS_start (cfg, "test-fs-download-persistence", &progress_cb, NULL,
  260. GNUNET_FS_FLAGS_PERSISTENCE, GNUNET_FS_OPTIONS_END);
  261. GNUNET_assert (NULL != fs);
  262. buf = GNUNET_malloc (FILESIZE);
  263. for (i = 0; i < FILESIZE; i++)
  264. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  265. meta = GNUNET_CONTAINER_meta_data_create ();
  266. kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
  267. bo.content_priority = 42;
  268. bo.anonymity_level = 1;
  269. bo.replication_level = 0;
  270. bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
  271. fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
  272. FILESIZE, buf, kuri, meta,
  273. GNUNET_NO, &bo);
  274. GNUNET_FS_uri_destroy (kuri);
  275. GNUNET_CONTAINER_meta_data_destroy (meta);
  276. GNUNET_assert (NULL != fi);
  277. timeout_kill =
  278. GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_kill_task, NULL);
  279. start = GNUNET_TIME_absolute_get ();
  280. publish =
  281. GNUNET_FS_publish_start (fs, fi, NULL, NULL, NULL,
  282. GNUNET_FS_PUBLISH_OPTION_NONE);
  283. GNUNET_assert (publish != NULL);
  284. }
  285. int
  286. main (int argc, char *argv[])
  287. {
  288. if (0 != GNUNET_TESTING_peer_run ("test-fs-download-persistence",
  289. "test_fs_download_data.conf",
  290. &run, NULL))
  291. return 1;
  292. return err;
  293. }
  294. /* end of test_fs_download_persistence.c */