2
0

test_fs_download.c 10 KB

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