fs_test_lib.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2010, 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/fs_test_lib.c
  19. * @brief library routines for testing FS publishing and downloading
  20. * with multiple peers; this code is limited to flat files
  21. * and no keywords (those functions can be tested with
  22. * single-peer setups; this is for testing routing).
  23. * @author Christian Grothoff
  24. */
  25. #include "platform.h"
  26. #include "fs.h"
  27. #include "fs_test_lib.h"
  28. #include "gnunet_testing_lib.h"
  29. #define CONNECT_ATTEMPTS 4
  30. #define CONTENT_LIFETIME GNUNET_TIME_UNIT_HOURS
  31. /**
  32. * Handle for a daemon started for testing FS.
  33. */
  34. struct GNUNET_FS_TestDaemon
  35. {
  36. /**
  37. * Global configuration, only stored in first test daemon,
  38. * otherwise NULL.
  39. */
  40. struct GNUNET_CONFIGURATION_Handle *gcfg;
  41. /**
  42. * Handle to the file sharing context using this daemon.
  43. */
  44. struct GNUNET_FS_Handle *fs;
  45. /**
  46. * Handle to the daemon via testing.
  47. */
  48. struct GNUNET_TESTING_Daemon *daemon;
  49. /**
  50. * Note that 'group' will be the same value for all of the
  51. * daemons started jointly.
  52. */
  53. struct GNUNET_TESTING_PeerGroup *group;
  54. /**
  55. * Configuration for accessing this peer.
  56. */
  57. struct GNUNET_CONFIGURATION_Handle *cfg;
  58. /**
  59. * ID of this peer.
  60. */
  61. struct GNUNET_PeerIdentity id;
  62. /**
  63. * Function to call when upload is done.
  64. */
  65. GNUNET_FS_TEST_UriContinuation publish_cont;
  66. /**
  67. * Closure for publish_cont.
  68. */
  69. void *publish_cont_cls;
  70. /**
  71. * Task to abort publishing (timeout).
  72. */
  73. GNUNET_SCHEDULER_TaskIdentifier publish_timeout_task;
  74. /**
  75. * Seed for file generation.
  76. */
  77. uint32_t publish_seed;
  78. /**
  79. * Context for current publishing operation.
  80. */
  81. struct GNUNET_FS_PublishContext *publish_context;
  82. /**
  83. * Result URI.
  84. */
  85. struct GNUNET_FS_Uri *publish_uri;
  86. /**
  87. * Name of the temporary file used, or NULL for none.
  88. */
  89. char *publish_tmp_file;
  90. /**
  91. * Function to call when download is done.
  92. */
  93. GNUNET_SCHEDULER_Task download_cont;
  94. /**
  95. * Closure for download_cont.
  96. */
  97. void *download_cont_cls;
  98. /**
  99. * Seed for download verification.
  100. */
  101. uint32_t download_seed;
  102. /**
  103. * Task to abort downloading (timeout).
  104. */
  105. GNUNET_SCHEDULER_TaskIdentifier download_timeout_task;
  106. /**
  107. * Context for current download operation.
  108. */
  109. struct GNUNET_FS_DownloadContext *download_context;
  110. /**
  111. * Verbosity level of the current operation.
  112. */
  113. int verbose;
  114. };
  115. /**
  116. * Check whether peers successfully shut down.
  117. */
  118. static void
  119. shutdown_callback (void *cls, const char *emsg)
  120. {
  121. struct GNUNET_CONFIGURATION_Handle *gcfg = cls;
  122. if (emsg != NULL)
  123. {
  124. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Shutdown of peers failed: %s\n",
  125. emsg);
  126. }
  127. else
  128. {
  129. #if VERBOSE
  130. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
  131. #endif
  132. }
  133. if (gcfg != NULL)
  134. GNUNET_CONFIGURATION_destroy (gcfg);
  135. }
  136. static void
  137. report_uri (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  138. {
  139. struct GNUNET_FS_TestDaemon *daemon = cls;
  140. GNUNET_FS_TEST_UriContinuation cont;
  141. struct GNUNET_FS_Uri *uri;
  142. GNUNET_FS_publish_stop (daemon->publish_context);
  143. daemon->publish_context = NULL;
  144. cont = daemon->publish_cont;
  145. daemon->publish_cont = NULL;
  146. uri = daemon->publish_uri;
  147. cont (daemon->publish_cont_cls, uri);
  148. GNUNET_FS_uri_destroy (uri);
  149. }
  150. static void
  151. report_success (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  152. {
  153. struct GNUNET_FS_TestDaemon *daemon = cls;
  154. GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
  155. daemon->download_context = NULL;
  156. GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
  157. daemon->download_cont_cls,
  158. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  159. daemon->download_cont = NULL;
  160. }
  161. static void *
  162. progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
  163. {
  164. struct GNUNET_FS_TestDaemon *daemon = cls;
  165. switch (info->status)
  166. {
  167. case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
  168. GNUNET_SCHEDULER_cancel (daemon->publish_timeout_task);
  169. daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
  170. daemon->publish_uri =
  171. GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
  172. GNUNET_SCHEDULER_add_continuation (&report_uri, daemon,
  173. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  174. break;
  175. case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
  176. if (daemon->verbose)
  177. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Publishing at %llu/%llu bytes\n",
  178. (unsigned long long) info->value.publish.completed,
  179. (unsigned long long) info->value.publish.size);
  180. break;
  181. case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
  182. if (daemon->verbose)
  183. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
  184. (unsigned long long) info->value.download.completed,
  185. (unsigned long long) info->value.download.size);
  186. break;
  187. case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
  188. GNUNET_SCHEDULER_cancel (daemon->download_timeout_task);
  189. daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
  190. GNUNET_SCHEDULER_add_continuation (&report_success, daemon,
  191. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  192. break;
  193. case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
  194. case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
  195. break;
  196. /* FIXME: monitor data correctness during download progress */
  197. /* FIXME: do performance reports given sufficient verbosity */
  198. /* FIXME: advance timeout task to "immediate" on error */
  199. default:
  200. break;
  201. }
  202. return NULL;
  203. }
  204. struct StartContext
  205. {
  206. struct GNUNET_TIME_Relative timeout;
  207. unsigned int total;
  208. unsigned int have;
  209. struct GNUNET_FS_TestDaemon **daemons;
  210. GNUNET_SCHEDULER_Task cont;
  211. void *cont_cls;
  212. struct GNUNET_TESTING_PeerGroup *group;
  213. struct GNUNET_CONFIGURATION_Handle *cfg;
  214. GNUNET_SCHEDULER_TaskIdentifier timeout_task;
  215. };
  216. static void
  217. notify_running (void *cls, const struct GNUNET_PeerIdentity *id,
  218. const struct GNUNET_CONFIGURATION_Handle *cfg,
  219. struct GNUNET_TESTING_Daemon *d, const char *emsg)
  220. {
  221. struct StartContext *sctx = cls;
  222. unsigned int i;
  223. if (emsg != NULL)
  224. {
  225. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start daemon: %s\n"),
  226. emsg);
  227. return;
  228. }
  229. i = 0;
  230. while (i < sctx->total)
  231. {
  232. if (GNUNET_TESTING_daemon_get (sctx->group, i) == d)
  233. break;
  234. i++;
  235. }
  236. GNUNET_assert (i < sctx->total);
  237. GNUNET_assert (sctx->have < sctx->total);
  238. GNUNET_assert (sctx->daemons[i]->cfg == NULL);
  239. sctx->daemons[i]->cfg = GNUNET_CONFIGURATION_dup (cfg);
  240. sctx->daemons[i]->group = sctx->group;
  241. sctx->daemons[i]->daemon = d;
  242. sctx->daemons[i]->id = *id;
  243. sctx->have++;
  244. if (sctx->have == sctx->total)
  245. {
  246. GNUNET_SCHEDULER_add_continuation (sctx->cont, sctx->cont_cls,
  247. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  248. sctx->daemons[0]->gcfg = sctx->cfg;
  249. GNUNET_SCHEDULER_cancel (sctx->timeout_task);
  250. for (i = 0; i < sctx->total; i++)
  251. {
  252. sctx->daemons[i]->fs =
  253. GNUNET_FS_start (sctx->daemons[i]->cfg, "<tester>", &progress_cb,
  254. sctx->daemons[i], GNUNET_FS_FLAGS_NONE,
  255. GNUNET_FS_OPTIONS_END);
  256. }
  257. GNUNET_free (sctx);
  258. }
  259. }
  260. static void
  261. start_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  262. {
  263. struct StartContext *sctx = cls;
  264. unsigned int i;
  265. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  266. "Timeout while trying to start daemons\n");
  267. GNUNET_TESTING_daemons_stop (sctx->group,
  268. GNUNET_TIME_relative_multiply
  269. (GNUNET_TIME_UNIT_SECONDS, 30),
  270. &shutdown_callback, NULL);
  271. for (i = 0; i < sctx->total; i++)
  272. {
  273. if (i < sctx->have)
  274. GNUNET_CONFIGURATION_destroy (sctx->daemons[i]->cfg);
  275. GNUNET_free (sctx->daemons[i]);
  276. sctx->daemons[i] = NULL;
  277. }
  278. GNUNET_CONFIGURATION_destroy (sctx->cfg);
  279. GNUNET_SCHEDULER_add_continuation (sctx->cont, sctx->cont_cls,
  280. GNUNET_SCHEDULER_REASON_TIMEOUT);
  281. GNUNET_free (sctx);
  282. }
  283. /**
  284. * Start daemons for testing.
  285. *
  286. * @param template_cfg_file configuration template to use
  287. * @param timeout if this operation cannot be completed within the
  288. * given period, call the continuation with an error code
  289. * @param total number of daemons to start
  290. * @param daemons array of 'total' entries to be initialized
  291. * (array must already be allocated, will be filled)
  292. * @param cont function to call when done
  293. * @param cont_cls closure for cont
  294. */
  295. void
  296. GNUNET_FS_TEST_daemons_start (const char *template_cfg_file,
  297. struct GNUNET_TIME_Relative timeout,
  298. unsigned int total,
  299. struct GNUNET_FS_TestDaemon **daemons,
  300. GNUNET_SCHEDULER_Task cont, void *cont_cls)
  301. {
  302. struct StartContext *sctx;
  303. unsigned int i;
  304. GNUNET_assert (total > 0);
  305. sctx = GNUNET_malloc (sizeof (struct StartContext));
  306. sctx->daemons = daemons;
  307. sctx->total = total;
  308. sctx->cont = cont;
  309. sctx->cont_cls = cont_cls;
  310. sctx->cfg = GNUNET_CONFIGURATION_create ();
  311. if (GNUNET_OK != GNUNET_CONFIGURATION_load (sctx->cfg, template_cfg_file))
  312. {
  313. GNUNET_break (0);
  314. GNUNET_CONFIGURATION_destroy (sctx->cfg);
  315. GNUNET_free (sctx);
  316. GNUNET_SCHEDULER_add_continuation (cont, cont_cls,
  317. GNUNET_SCHEDULER_REASON_TIMEOUT);
  318. return;
  319. }
  320. for (i = 0; i < total; i++)
  321. daemons[i] = GNUNET_malloc (sizeof (struct GNUNET_FS_TestDaemon));
  322. sctx->group = GNUNET_TESTING_daemons_start (sctx->cfg, total, total, /* Outstanding connections */
  323. total, /* Outstanding ssh connections */
  324. timeout, NULL, NULL,
  325. &notify_running, sctx, NULL, NULL,
  326. NULL);
  327. sctx->timeout_task =
  328. GNUNET_SCHEDULER_add_delayed (timeout, &start_timeout, sctx);
  329. }
  330. struct ConnectContext
  331. {
  332. GNUNET_SCHEDULER_Task cont;
  333. void *cont_cls;
  334. };
  335. /**
  336. * Prototype of a function that will be called whenever
  337. * two daemons are connected by the testing library.
  338. *
  339. * @param cls closure
  340. * @param first peer id for first daemon
  341. * @param second peer id for the second daemon
  342. * @param distance distance between the connected peers
  343. * @param first_cfg config for the first daemon
  344. * @param second_cfg config for the second daemon
  345. * @param first_daemon handle for the first daemon
  346. * @param second_daemon handle for the second daemon
  347. * @param emsg error message (NULL on success)
  348. */
  349. static void
  350. notify_connection (void *cls, const struct GNUNET_PeerIdentity *first,
  351. const struct GNUNET_PeerIdentity *second, uint32_t distance,
  352. const struct GNUNET_CONFIGURATION_Handle *first_cfg,
  353. const struct GNUNET_CONFIGURATION_Handle *second_cfg,
  354. struct GNUNET_TESTING_Daemon *first_daemon,
  355. struct GNUNET_TESTING_Daemon *second_daemon,
  356. const char *emsg)
  357. {
  358. struct ConnectContext *cc = cls;
  359. if (emsg != NULL)
  360. GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to connect peers: %s\n",
  361. emsg);
  362. GNUNET_SCHEDULER_add_continuation (cc->cont, cc->cont_cls,
  363. (emsg !=
  364. NULL) ? GNUNET_SCHEDULER_REASON_TIMEOUT :
  365. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  366. GNUNET_free (cc);
  367. }
  368. /**
  369. * Connect two daemons for testing.
  370. *
  371. * @param daemon1 first daemon to connect
  372. * @param daemon2 second first daemon to connect
  373. * @param timeout if this operation cannot be completed within the
  374. * given period, call the continuation with an error code
  375. * @param cont function to call when done
  376. * @param cont_cls closure for cont
  377. */
  378. void
  379. GNUNET_FS_TEST_daemons_connect (struct GNUNET_FS_TestDaemon *daemon1,
  380. struct GNUNET_FS_TestDaemon *daemon2,
  381. struct GNUNET_TIME_Relative timeout,
  382. GNUNET_SCHEDULER_Task cont, void *cont_cls)
  383. {
  384. struct ConnectContext *ncc;
  385. ncc = GNUNET_malloc (sizeof (struct ConnectContext));
  386. ncc->cont = cont;
  387. ncc->cont_cls = cont_cls;
  388. GNUNET_TESTING_daemons_connect (daemon1->daemon, daemon2->daemon, timeout,
  389. CONNECT_ATTEMPTS, GNUNET_YES,
  390. &notify_connection, ncc);
  391. }
  392. /**
  393. * Obtain peer configuration used for testing.
  394. *
  395. * @param daemons array with the daemons
  396. * @param off which configuration to get
  397. * @return peer configuration
  398. */
  399. const struct GNUNET_CONFIGURATION_Handle *
  400. GNUNET_FS_TEST_get_configuration (struct GNUNET_FS_TestDaemon **daemons,
  401. unsigned int off)
  402. {
  403. return daemons[off]->cfg;
  404. }
  405. /**
  406. * Obtain peer group used for testing.
  407. *
  408. * @param daemons array with the daemons (must contain at least one)
  409. * @return peer group
  410. */
  411. struct GNUNET_TESTING_PeerGroup *
  412. GNUNET_FS_TEST_get_group (struct GNUNET_FS_TestDaemon **daemons)
  413. {
  414. return daemons[0]->group;
  415. }
  416. /**
  417. * Stop daemons used for testing.
  418. *
  419. * @param total number of daemons to stop
  420. * @param daemons array with the daemons (values will be clobbered)
  421. */
  422. void
  423. GNUNET_FS_TEST_daemons_stop (unsigned int total,
  424. struct GNUNET_FS_TestDaemon **daemons)
  425. {
  426. unsigned int i;
  427. struct GNUNET_TESTING_PeerGroup *pg;
  428. struct GNUNET_CONFIGURATION_Handle *gcfg;
  429. struct GNUNET_FS_TestDaemon *daemon;
  430. GNUNET_assert (total > 0);
  431. GNUNET_assert (daemons[0] != NULL);
  432. pg = daemons[0]->group;
  433. gcfg = daemons[0]->gcfg;
  434. for (i = 0; i < total; i++)
  435. {
  436. daemon = daemons[i];
  437. if (daemon->download_timeout_task != GNUNET_SCHEDULER_NO_TASK)
  438. {
  439. GNUNET_SCHEDULER_cancel (daemon->download_timeout_task);
  440. daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
  441. }
  442. if (daemon->publish_timeout_task != GNUNET_SCHEDULER_NO_TASK)
  443. {
  444. GNUNET_SCHEDULER_cancel (daemon->publish_timeout_task);
  445. daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
  446. }
  447. if (NULL != daemon->download_context)
  448. {
  449. GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
  450. daemon->download_context = NULL;
  451. }
  452. if (daemon->fs != NULL)
  453. GNUNET_FS_stop (daemon->fs);
  454. if (daemon->cfg != NULL)
  455. GNUNET_CONFIGURATION_destroy (daemon->cfg);
  456. if (NULL != daemon->publish_tmp_file)
  457. {
  458. GNUNET_break (GNUNET_OK ==
  459. GNUNET_DISK_directory_remove (daemon->publish_tmp_file));
  460. GNUNET_free (daemon->publish_tmp_file);
  461. daemon->publish_tmp_file = NULL;
  462. }
  463. GNUNET_free (daemon);
  464. daemons[i] = NULL;
  465. }
  466. GNUNET_TESTING_daemons_stop (pg,
  467. GNUNET_TIME_relative_multiply
  468. (GNUNET_TIME_UNIT_SECONDS, 30),
  469. &shutdown_callback, gcfg);
  470. }
  471. static void
  472. publish_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  473. {
  474. struct GNUNET_FS_TestDaemon *daemon = cls;
  475. GNUNET_FS_TEST_UriContinuation cont;
  476. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  477. "Timeout while trying to publish data\n");
  478. cont = daemon->publish_cont;
  479. daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
  480. daemon->publish_cont = NULL;
  481. GNUNET_FS_publish_stop (daemon->publish_context);
  482. daemon->publish_context = NULL;
  483. cont (daemon->publish_cont_cls, NULL);
  484. }
  485. static size_t
  486. file_generator (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
  487. {
  488. struct GNUNET_FS_TestDaemon *daemon = cls;
  489. uint64_t pos;
  490. uint8_t *cbuf = buf;
  491. int mod;
  492. if (emsg != NULL)
  493. *emsg = NULL;
  494. if (buf == NULL)
  495. return 0;
  496. for (pos = 0; pos < 8; pos++)
  497. cbuf[pos] = (uint8_t) (offset >> pos * 8);
  498. for (pos = 8; pos < max; pos++)
  499. {
  500. mod = (255 - (offset / 1024 / 32));
  501. if (mod == 0)
  502. mod = 1;
  503. cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);
  504. }
  505. return max;
  506. }
  507. /**
  508. * Publish a file at the given daemon.
  509. *
  510. * @param daemon where to publish
  511. * @param timeout if this operation cannot be completed within the
  512. * given period, call the continuation with an error code
  513. * @param anonymity option for publication
  514. * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
  515. * GNUNET_SYSERR for simulation
  516. * @param size size of the file to publish
  517. * @param seed seed to use for file generation
  518. * @param verbose how verbose to be in reporting
  519. * @param cont function to call when done
  520. * @param cont_cls closure for cont
  521. */
  522. void
  523. GNUNET_FS_TEST_publish (struct GNUNET_FS_TestDaemon *daemon,
  524. struct GNUNET_TIME_Relative timeout, uint32_t anonymity,
  525. int do_index, uint64_t size, uint32_t seed,
  526. unsigned int verbose,
  527. GNUNET_FS_TEST_UriContinuation cont, void *cont_cls)
  528. {
  529. struct GNUNET_FS_FileInformation *fi;
  530. struct GNUNET_DISK_FileHandle *fh;
  531. char *emsg;
  532. uint64_t off;
  533. char buf[DBLOCK_SIZE];
  534. size_t bsize;
  535. struct GNUNET_FS_BlockOptions bo;
  536. GNUNET_assert (daemon->publish_cont == NULL);
  537. daemon->publish_cont = cont;
  538. daemon->publish_cont_cls = cont_cls;
  539. daemon->publish_seed = seed;
  540. daemon->verbose = verbose;
  541. bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
  542. bo.anonymity_level = anonymity;
  543. bo.content_priority = 42;
  544. bo.replication_level = 1;
  545. if (GNUNET_YES == do_index)
  546. {
  547. GNUNET_assert (daemon->publish_tmp_file == NULL);
  548. daemon->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
  549. GNUNET_assert (daemon->publish_tmp_file != NULL);
  550. fh = GNUNET_DISK_file_open (daemon->publish_tmp_file,
  551. GNUNET_DISK_OPEN_WRITE |
  552. GNUNET_DISK_OPEN_CREATE,
  553. GNUNET_DISK_PERM_USER_READ |
  554. GNUNET_DISK_PERM_USER_WRITE);
  555. GNUNET_assert (NULL != fh);
  556. off = 0;
  557. while (off < size)
  558. {
  559. bsize = GNUNET_MIN (sizeof (buf), size - off);
  560. emsg = NULL;
  561. GNUNET_assert (bsize == file_generator (daemon, off, bsize, buf, &emsg));
  562. GNUNET_assert (emsg == NULL);
  563. GNUNET_assert (bsize == GNUNET_DISK_file_write (fh, buf, bsize));
  564. off += bsize;
  565. }
  566. GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
  567. fi = GNUNET_FS_file_information_create_from_file (daemon->fs, daemon,
  568. daemon->publish_tmp_file,
  569. NULL, NULL, do_index,
  570. &bo);
  571. }
  572. else
  573. {
  574. fi = GNUNET_FS_file_information_create_from_reader (daemon->fs, daemon,
  575. size, &file_generator,
  576. daemon, NULL, NULL,
  577. do_index, &bo);
  578. }
  579. daemon->publish_context =
  580. GNUNET_FS_publish_start (daemon->fs, fi, NULL, NULL, NULL,
  581. GNUNET_FS_PUBLISH_OPTION_NONE);
  582. daemon->publish_timeout_task =
  583. GNUNET_SCHEDULER_add_delayed (timeout, &publish_timeout, daemon);
  584. }
  585. static void
  586. download_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  587. {
  588. struct GNUNET_FS_TestDaemon *daemon = cls;
  589. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  590. "Timeout while trying to download file\n");
  591. daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
  592. GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
  593. daemon->download_context = NULL;
  594. GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
  595. daemon->download_cont_cls,
  596. GNUNET_SCHEDULER_REASON_TIMEOUT);
  597. daemon->download_cont = NULL;
  598. }
  599. /**
  600. * Perform test download.
  601. *
  602. * @param daemon which peer to download from
  603. * @param timeout if this operation cannot be completed within the
  604. * given period, call the continuation with an error code
  605. * @param anonymity option for download
  606. * @param seed used for file validation
  607. * @param uri URI of file to download (CHK/LOC only)
  608. * @param verbose how verbose to be in reporting
  609. * @param cont function to call when done
  610. * @param cont_cls closure for cont
  611. */
  612. void
  613. GNUNET_FS_TEST_download (struct GNUNET_FS_TestDaemon *daemon,
  614. struct GNUNET_TIME_Relative timeout,
  615. uint32_t anonymity, uint32_t seed,
  616. const struct GNUNET_FS_Uri *uri, unsigned int verbose,
  617. GNUNET_SCHEDULER_Task cont, void *cont_cls)
  618. {
  619. uint64_t size;
  620. GNUNET_assert (daemon->download_cont == NULL);
  621. size = GNUNET_FS_uri_chk_get_file_size (uri);
  622. daemon->verbose = verbose;
  623. daemon->download_cont = cont;
  624. daemon->download_cont_cls = cont_cls;
  625. daemon->download_seed = seed;
  626. daemon->download_context =
  627. GNUNET_FS_download_start (daemon->fs, uri, NULL, NULL, NULL, 0, size,
  628. anonymity, GNUNET_FS_DOWNLOAD_OPTION_NONE, NULL,
  629. NULL);
  630. daemon->download_timeout_task =
  631. GNUNET_SCHEDULER_add_delayed (timeout, &download_timeout, daemon);
  632. }
  633. /* end of test_fs_lib.c */