test_fs_download_indexed.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2004, 2005, 2006, 2008, 2009, 2011 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_indexed.c
  19. * @brief simple testcase for downloading of indexed file
  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. #include <gauger.h>
  27. #define VERBOSE GNUNET_NO
  28. #define START_ARM GNUNET_YES
  29. /**
  30. * File-size we use for testing.
  31. */
  32. #define FILESIZE (1024 * 1024 * 2)
  33. /**
  34. * How long until we give up on transmitting the message?
  35. */
  36. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
  37. /**
  38. * How long should our test-content live?
  39. */
  40. #define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
  41. struct PeerContext
  42. {
  43. struct GNUNET_CONFIGURATION_Handle *cfg;
  44. #if START_ARM
  45. struct GNUNET_OS_Process *arm_proc;
  46. #endif
  47. };
  48. static struct PeerContext p1;
  49. static struct GNUNET_TIME_Absolute start;
  50. static struct GNUNET_FS_Handle *fs;
  51. static struct GNUNET_FS_DownloadContext *download;
  52. static struct GNUNET_FS_PublishContext *publish;
  53. static GNUNET_SCHEDULER_TaskIdentifier timeout_kill;
  54. static char *fn;
  55. static char *fn1;
  56. static int err;
  57. static void
  58. timeout_kill_task (void *cls,
  59. const struct GNUNET_SCHEDULER_TaskContext *tc)
  60. {
  61. if (download != NULL)
  62. {
  63. GNUNET_FS_download_stop (download, GNUNET_YES);
  64. download = NULL;
  65. }
  66. else if (publish != NULL)
  67. {
  68. GNUNET_FS_publish_stop (publish);
  69. publish = NULL;
  70. }
  71. timeout_kill = GNUNET_SCHEDULER_NO_TASK;
  72. err = 1;
  73. }
  74. static void
  75. abort_publish_task (void *cls,
  76. const struct GNUNET_SCHEDULER_TaskContext *tc)
  77. {
  78. if (publish != NULL)
  79. {
  80. GNUNET_FS_publish_stop (publish);
  81. publish = NULL;
  82. }
  83. }
  84. static void
  85. stop_fs_task (void *cls,
  86. const struct GNUNET_SCHEDULER_TaskContext *tc)
  87. {
  88. GNUNET_FS_stop (fs);
  89. fs = NULL;
  90. }
  91. static void
  92. abort_download_task (void *cls,
  93. const struct GNUNET_SCHEDULER_TaskContext *tc)
  94. {
  95. uint64_t size;
  96. if (download != NULL)
  97. {
  98. GNUNET_FS_download_stop (download, GNUNET_YES);
  99. download = NULL;
  100. }
  101. GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES));
  102. GNUNET_assert (size == FILESIZE);
  103. GNUNET_DISK_directory_remove (fn);
  104. GNUNET_free (fn);
  105. fn = NULL;
  106. GNUNET_SCHEDULER_cancel (timeout_kill);
  107. timeout_kill = GNUNET_SCHEDULER_NO_TASK;
  108. }
  109. static void *
  110. progress_cb (void *cls,
  111. const struct GNUNET_FS_ProgressInfo *event)
  112. {
  113. switch (event->status)
  114. {
  115. case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
  116. #if VERBOSE
  117. printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
  118. (unsigned long long) event->value.publish.completed,
  119. (unsigned long long) event->value.publish.size,
  120. event->value.publish.specifics.progress.depth,
  121. (unsigned long long) event->value.publish.specifics.progress.offset);
  122. #endif
  123. break;
  124. case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
  125. printf ("Publishing complete, %llu kbps.\n",
  126. (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).rel_value) / 1024LL));
  127. GAUGER ("FS",
  128. "Publishing speed (indexing)",
  129. (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).rel_value) / 1024LL),
  130. "kb/s");
  131. fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
  132. start = GNUNET_TIME_absolute_get ();
  133. download = GNUNET_FS_download_start (fs,
  134. event->value.publish.specifics.completed.chk_uri,
  135. NULL,
  136. fn, NULL,
  137. 0,
  138. FILESIZE,
  139. 1,
  140. GNUNET_FS_DOWNLOAD_OPTION_NONE,
  141. "download",
  142. NULL);
  143. GNUNET_assert (download != NULL);
  144. break;
  145. case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
  146. printf ("Download complete, %llu kbps.\n",
  147. (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).rel_value) / 1024LL));
  148. GAUGER ("FS",
  149. "Local download speed (indexed)",
  150. (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).rel_value) / 1024LL),
  151. "kb/s");
  152. GNUNET_SCHEDULER_add_now (&abort_download_task,
  153. NULL);
  154. break;
  155. case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
  156. GNUNET_assert (download == event->value.download.dc);
  157. #if VERBOSE
  158. printf ("Download is progressing (%llu/%llu at level %u off %llu)...\n",
  159. (unsigned long long) event->value.download.completed,
  160. (unsigned long long) event->value.download.size,
  161. event->value.download.specifics.progress.depth,
  162. (unsigned long long) event->value.download.specifics.progress.offset);
  163. #endif
  164. break;
  165. case GNUNET_FS_STATUS_PUBLISH_ERROR:
  166. fprintf (stderr,
  167. "Error publishing file: %s\n",
  168. event->value.publish.specifics.error.message);
  169. GNUNET_break (0);
  170. GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
  171. NULL,
  172. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  173. break;
  174. case GNUNET_FS_STATUS_DOWNLOAD_ERROR:
  175. fprintf (stderr,
  176. "Error downloading file: %s\n",
  177. event->value.download.specifics.error.message);
  178. GNUNET_SCHEDULER_add_now (&abort_download_task,
  179. NULL);
  180. break;
  181. case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
  182. case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
  183. break;
  184. case GNUNET_FS_STATUS_PUBLISH_START:
  185. GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
  186. GNUNET_assert (NULL == event->value.publish.pctx);
  187. GNUNET_assert (FILESIZE == event->value.publish.size);
  188. GNUNET_assert (0 == event->value.publish.completed);
  189. GNUNET_assert (1 == event->value.publish.anonymity);
  190. break;
  191. case GNUNET_FS_STATUS_PUBLISH_STOPPED:
  192. GNUNET_assert (publish == event->value.publish.pc);
  193. GNUNET_assert (FILESIZE == event->value.publish.size);
  194. GNUNET_assert (1 == event->value.publish.anonymity);
  195. GNUNET_SCHEDULER_add_now (&stop_fs_task,
  196. NULL);
  197. break;
  198. case GNUNET_FS_STATUS_DOWNLOAD_START:
  199. GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
  200. GNUNET_assert (NULL == event->value.download.pctx);
  201. GNUNET_assert (NULL != event->value.download.uri);
  202. GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
  203. GNUNET_assert (FILESIZE == event->value.download.size);
  204. GNUNET_assert (0 == event->value.download.completed);
  205. GNUNET_assert (1 == event->value.download.anonymity);
  206. break;
  207. case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
  208. GNUNET_assert (download == event->value.download.dc);
  209. GNUNET_SCHEDULER_add_continuation (&abort_publish_task,
  210. NULL,
  211. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  212. break;
  213. default:
  214. printf ("Unexpected event: %d\n",
  215. event->status);
  216. break;
  217. }
  218. return NULL;
  219. }
  220. static void
  221. setup_peer (struct PeerContext *p, const char *cfgname)
  222. {
  223. p->cfg = GNUNET_CONFIGURATION_create ();
  224. #if START_ARM
  225. p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
  226. "gnunet-service-arm",
  227. #if VERBOSE
  228. "-L", "DEBUG",
  229. #endif
  230. "-c", cfgname, NULL);
  231. #endif
  232. GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
  233. }
  234. static void
  235. stop_arm (struct PeerContext *p)
  236. {
  237. #if START_ARM
  238. if (NULL != p->arm_proc)
  239. {
  240. if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
  241. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
  242. if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
  243. GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
  244. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  245. "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
  246. GNUNET_OS_process_close (p->arm_proc);
  247. p->arm_proc = NULL;
  248. }
  249. #endif
  250. GNUNET_CONFIGURATION_destroy (p->cfg);
  251. }
  252. static void
  253. run (void *cls,
  254. char *const *args,
  255. const char *cfgfile,
  256. const struct GNUNET_CONFIGURATION_Handle *cfg)
  257. {
  258. const char *keywords[] = {
  259. "down_foo",
  260. "down_bar",
  261. };
  262. char *buf;
  263. struct GNUNET_CONTAINER_MetaData *meta;
  264. struct GNUNET_FS_Uri *kuri;
  265. struct GNUNET_FS_FileInformation *fi;
  266. struct GNUNET_FS_BlockOptions bo;
  267. size_t i;
  268. setup_peer (&p1, "test_fs_download_data.conf");
  269. fs = GNUNET_FS_start (cfg,
  270. "test-fs-download-indexed",
  271. &progress_cb,
  272. NULL,
  273. GNUNET_FS_FLAGS_NONE,
  274. GNUNET_FS_OPTIONS_END);
  275. GNUNET_assert (NULL != fs);
  276. fn1 = GNUNET_DISK_mktemp ("gnunet-download-indexed-test");
  277. buf = GNUNET_malloc (FILESIZE);
  278. for (i = 0; i < FILESIZE; i++)
  279. buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
  280. GNUNET_assert (FILESIZE ==
  281. GNUNET_DISK_fn_write (fn1,
  282. buf,
  283. FILESIZE,
  284. GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE));
  285. GNUNET_free (buf);
  286. meta = GNUNET_CONTAINER_meta_data_create ();
  287. kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
  288. bo.content_priority = 42;
  289. bo.anonymity_level = 1;
  290. bo.replication_level = 0;
  291. bo.expiration_time = GNUNET_TIME_relative_to_absolute (LIFETIME);
  292. fi = GNUNET_FS_file_information_create_from_file (fs,
  293. "publish-context",
  294. fn1,
  295. kuri,
  296. meta,
  297. GNUNET_YES,
  298. &bo);
  299. GNUNET_FS_uri_destroy (kuri);
  300. GNUNET_CONTAINER_meta_data_destroy (meta);
  301. GNUNET_assert (NULL != fi);
  302. timeout_kill = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
  303. &timeout_kill_task,
  304. NULL);
  305. start = GNUNET_TIME_absolute_get ();
  306. publish = GNUNET_FS_publish_start (fs,
  307. fi,
  308. NULL, NULL, NULL,
  309. GNUNET_FS_PUBLISH_OPTION_NONE);
  310. GNUNET_assert (publish != NULL);
  311. }
  312. int
  313. main (int argc, char *argv[])
  314. {
  315. char *const argvx[] = {
  316. "test-fs-download-indexed",
  317. "-c",
  318. "test_fs_download_data.conf",
  319. #if VERBOSE
  320. "-L", "DEBUG",
  321. #endif
  322. NULL
  323. };
  324. struct GNUNET_GETOPT_CommandLineOption options[] = {
  325. GNUNET_GETOPT_OPTION_END
  326. };
  327. GNUNET_log_setup ("test_fs_download_indexed",
  328. #if VERBOSE
  329. "DEBUG",
  330. #else
  331. "WARNING",
  332. #endif
  333. NULL);
  334. GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
  335. argvx, "test-fs-download-indexed",
  336. "nohelp", options, &run, NULL);
  337. stop_arm (&p1);
  338. if (fn1 != NULL)
  339. {
  340. GNUNET_DISK_directory_remove (fn1);
  341. GNUNET_free (fn1);
  342. }
  343. if (fn != NULL)
  344. {
  345. GNUNET_DISK_directory_remove (fn);
  346. GNUNET_free (fn);
  347. }
  348. GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-download/");
  349. return err;
  350. }
  351. /* end of test_fs_download_indexed.c */