test_fs_download.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2004, 2005, 2006, 2008, 2009, 2011, 2012 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.c
  19. * @brief simple testcase for simple publish + download operation
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_fs_service.h"
  25. #include "gnunet_testing_lib.h"
  26. #include <gauger.h>
  27. /**
  28. * File-size we use for testing.
  29. */
  30. #define FILESIZE (1024 * 1024 * 2)
  31. /**
  32. * How long until we give up on transmitting the message?
  33. */
  34. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
  35. /**
  36. * How long should our test-content live?
  37. */
  38. #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
  39. static unsigned int anonymity_level;
  40. static int indexed;
  41. static struct GNUNET_TIME_Absolute start;
  42. static struct GNUNET_FS_Handle *fs;
  43. static struct GNUNET_FS_DownloadContext *download;
  44. static struct GNUNET_FS_PublishContext *publish;
  45. static GNUNET_SCHEDULER_TaskIdentifier timeout_kill;
  46. static char *fn;
  47. static char *fn1;
  48. static int err;
  49. static void
  50. timeout_kill_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  51. {
  52. if (NULL != download)
  53. {
  54. GNUNET_FS_download_stop (download, GNUNET_YES);
  55. download = NULL;
  56. }
  57. else if (NULL != publish)
  58. {
  59. GNUNET_FS_publish_stop (publish);
  60. publish = NULL;
  61. }
  62. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout downloading file\n");
  63. timeout_kill = GNUNET_SCHEDULER_NO_TASK;
  64. err = 1;
  65. }
  66. static void
  67. abort_publish_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  68. {
  69. if (NULL != publish)
  70. {
  71. GNUNET_FS_publish_stop (publish);
  72. publish = NULL;
  73. }
  74. }
  75. static void
  76. stop_fs_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  77. {
  78. GNUNET_FS_stop (fs);
  79. fs = NULL;
  80. }
  81. static void
  82. abort_download_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  83. {
  84. uint64_t size;
  85. if (NULL != download)
  86. {
  87. GNUNET_FS_download_stop (download, GNUNET_YES);
  88. download = NULL;
  89. }
  90. GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES, GNUNET_NO));
  91. GNUNET_assert (size == FILESIZE);
  92. GNUNET_DISK_directory_remove (fn);
  93. GNUNET_free (fn);
  94. fn = NULL;
  95. GNUNET_SCHEDULER_cancel (timeout_kill);
  96. timeout_kill = GNUNET_SCHEDULER_NO_TASK;
  97. }
  98. static void *
  99. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *event)
  100. {
  101. switch (event->status)
  102. {
  103. case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
  104. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  105. "Publish is progressing (%llu/%llu at level %u off %llu)...\n",
  106. (unsigned long long) event->value.publish.completed,
  107. (unsigned long long) event->value.publish.size,
  108. event->value.publish.specifics.progress.depth,
  109. (unsigned long long) event->value.publish.specifics.
  110. progress.offset);
  111. break;
  112. case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
  113. break;
  114. case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
  115. fprintf (stdout,
  116. "Publishing complete, %llu kb/s.\n",
  117. (unsigned long long) (FILESIZE * 1000000LL /
  118. (1 +
  119. GNUNET_TIME_absolute_get_duration
  120. (start).rel_value_us) / 1024LL));
  121. GAUGER ("FS",
  122. (GNUNET_YES == indexed)
  123. ? "Publishing speed (indexing)"
  124. : "Publishing speed (insertion)",
  125. (unsigned long long) (FILESIZE * 1000000LL /
  126. (1 +
  127. GNUNET_TIME_absolute_get_duration
  128. (start).rel_value_us) / 1024LL), "kb/s");
  129. fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
  130. start = GNUNET_TIME_absolute_get ();
  131. download =
  132. GNUNET_FS_download_start (fs,
  133. event->value.publish.specifics.
  134. completed.chk_uri, NULL, fn, NULL, 0,
  135. FILESIZE, anonymity_level,
  136. GNUNET_FS_DOWNLOAD_OPTION_NONE,
  137. "download", NULL);
  138. GNUNET_assert (download != NULL);
  139. break;
  140. case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
  141. fprintf (stdout,
  142. "Download complete, %llu kb/s.\n",
  143. (unsigned long long) (FILESIZE * 1000000LL /
  144. (1 +
  145. GNUNET_TIME_absolute_get_duration
  146. (start).rel_value_us) / 1024LL));
  147. GAUGER ("FS",
  148. (GNUNET_YES == indexed)
  149. ? "Local download speed (indexed)"
  150. : "Local download speed (inserted)",
  151. (unsigned long long) (FILESIZE * 1000000LL /
  152. (1 +
  153. GNUNET_TIME_absolute_get_duration
  154. (start).rel_value_us) / 1024LL), "kb/s");
  155. GNUNET_SCHEDULER_add_now (&abort_download_task, NULL);
  156. break;
  157. case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
  158. GNUNET_assert (download == event->value.download.dc);
  159. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  160. "Download is progressing (%llu/%llu at level %u off %llu)...\n",
  161. (unsigned long long) event->value.download.completed,
  162. (unsigned long long) event->value.download.size,
  163. event->value.download.specifics.progress.depth,
  164. (unsigned long long) event->value.download.specifics.
  165. progress.offset);
  166. break;
  167. case GNUNET_FS_STATUS_PUBLISH_ERROR:
  168. FPRINTF (stderr, "Error publishing file: %s\n",
  169. event->value.publish.specifics.error.message);
  170. GNUNET_break (0);
  171. GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
  172. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  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_DOWNLOAD_ACTIVE:
  180. case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
  181. break;
  182. case GNUNET_FS_STATUS_PUBLISH_START:
  183. GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
  184. GNUNET_assert (NULL == event->value.publish.pctx);
  185. GNUNET_assert (FILESIZE == event->value.publish.size);
  186. GNUNET_assert (0 == event->value.publish.completed);
  187. GNUNET_assert (1 == event->value.publish.anonymity);
  188. break;
  189. case GNUNET_FS_STATUS_PUBLISH_STOPPED:
  190. GNUNET_assert (publish == event->value.publish.pc);
  191. GNUNET_assert (FILESIZE == event->value.publish.size);
  192. GNUNET_assert (1 == event->value.publish.anonymity);
  193. GNUNET_SCHEDULER_add_now (&stop_fs_task, NULL);
  194. break;
  195. case GNUNET_FS_STATUS_DOWNLOAD_START:
  196. GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
  197. GNUNET_assert (NULL == event->value.download.pctx);
  198. GNUNET_assert (NULL != event->value.download.uri);
  199. GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
  200. GNUNET_assert (FILESIZE == event->value.download.size);
  201. GNUNET_assert (0 == event->value.download.completed);
  202. GNUNET_assert (1 == event->value.download.anonymity);
  203. break;
  204. case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
  205. GNUNET_assert (download == event->value.download.dc);
  206. GNUNET_SCHEDULER_add_continuation (&abort_publish_task, NULL,
  207. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  208. break;
  209. default:
  210. printf ("Unexpected event: %d\n", event->status);
  211. break;
  212. }
  213. return NULL;
  214. }
  215. static void
  216. run (void *cls,
  217. const struct GNUNET_CONFIGURATION_Handle *cfg,
  218. struct GNUNET_TESTING_Peer *peer)
  219. {
  220. const char *binary_name = cls;
  221. const char *keywords[] = {
  222. "down_foo",
  223. "down_bar",
  224. };
  225. char *buf;
  226. struct GNUNET_CONTAINER_MetaData *meta;
  227. struct GNUNET_FS_Uri *kuri;
  228. struct GNUNET_FS_FileInformation *fi;
  229. size_t i;
  230. struct GNUNET_FS_BlockOptions bo;
  231. if (GNUNET_YES ==
  232. GNUNET_CONFIGURATION_get_value_yesno (cfg,
  233. "download-test",
  234. "USE_STREAM"))
  235. anonymity_level = 0;
  236. else
  237. anonymity_level = 1;
  238. fs = GNUNET_FS_start (cfg, binary_name, &progress_cb, NULL,
  239. GNUNET_FS_FLAGS_NONE, GNUNET_FS_OPTIONS_END);
  240. GNUNET_assert (NULL != fs);
  241. buf = GNUNET_malloc (FILESIZE);
  242. for (i = 0; i < FILESIZE; i++)
  243. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  244. meta = GNUNET_CONTAINER_meta_data_create ();
  245. kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
  246. bo.content_priority = 42;
  247. bo.anonymity_level = anonymity_level;
  248. bo.replication_level = 0;
  249. bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
  250. if (GNUNET_YES ==
  251. GNUNET_CONFIGURATION_get_value_yesno (cfg,
  252. "download-test",
  253. "USE_INDEX"))
  254. {
  255. fn1 = GNUNET_DISK_mktemp ("gnunet-download-indexed-test");
  256. GNUNET_assert (FILESIZE ==
  257. GNUNET_DISK_fn_write (fn1, buf, FILESIZE,
  258. GNUNET_DISK_PERM_USER_READ |
  259. GNUNET_DISK_PERM_USER_WRITE));
  260. GNUNET_free (buf);
  261. fi = GNUNET_FS_file_information_create_from_file (fs, "publish-context", fn1,
  262. kuri, meta, GNUNET_YES,
  263. &bo);
  264. indexed = GNUNET_YES;
  265. }
  266. else
  267. {
  268. fi = GNUNET_FS_file_information_create_from_data (fs, "publish-context",
  269. FILESIZE, buf, kuri, meta,
  270. GNUNET_NO, &bo);
  271. /* note: buf will be free'd as part of 'fi' now */
  272. indexed = GNUNET_NO;
  273. }
  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. const char *binary_name;
  289. const char *config_name;
  290. binary_name = "test-fs-download";
  291. config_name = "test_fs_download_data.conf";
  292. if (NULL != strstr (argv[0], "indexed"))
  293. {
  294. binary_name = "test-fs-download-indexed";
  295. config_name = "test_fs_download_indexed.conf";
  296. }
  297. if (NULL != strstr (argv[0], "cadet"))
  298. {
  299. binary_name = "test-fs-download-cadet";
  300. config_name = "test_fs_download_cadet.conf";
  301. }
  302. if (0 != GNUNET_TESTING_peer_run (binary_name,
  303. config_name,
  304. &run, (void *) binary_name))
  305. return 1;
  306. if (NULL != fn1)
  307. {
  308. UNLINK (fn1);
  309. GNUNET_free (fn1);
  310. }
  311. return err;
  312. }
  313. /* end of test_fs_download.c */