fs_test_lib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /*
  2. This file is part of GNUnet.
  3. (C) 2010, 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/fs_test_lib.c
  19. * @brief library routines for testing FS publishing and downloading;
  20. * 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_api.h"
  27. #include "fs_test_lib.h"
  28. #define CONTENT_LIFETIME GNUNET_TIME_UNIT_HOURS
  29. /**
  30. * Handle for a publishing operation started for testing FS.
  31. */
  32. struct TestPublishOperation
  33. {
  34. /**
  35. * Handle for the operation to connect to the peer's 'fs' service.
  36. */
  37. struct GNUNET_TESTBED_Operation *fs_op;
  38. /**
  39. * Handle to the file sharing context using this daemon.
  40. */
  41. struct GNUNET_FS_Handle *fs;
  42. /**
  43. * Function to call when upload is done.
  44. */
  45. GNUNET_FS_TEST_UriContinuation publish_cont;
  46. /**
  47. * Closure for publish_cont.
  48. */
  49. void *publish_cont_cls;
  50. /**
  51. * Task to abort publishing (timeout).
  52. */
  53. GNUNET_SCHEDULER_TaskIdentifier publish_timeout_task;
  54. /**
  55. * Seed for file generation.
  56. */
  57. uint32_t publish_seed;
  58. /**
  59. * Context for current publishing operation.
  60. */
  61. struct GNUNET_FS_PublishContext *publish_context;
  62. /**
  63. * Result URI.
  64. */
  65. struct GNUNET_FS_Uri *publish_uri;
  66. /**
  67. * Name of the temporary file used, or NULL for none.
  68. */
  69. char *publish_tmp_file;
  70. /**
  71. * Size of the file.
  72. */
  73. uint64_t size;
  74. /**
  75. * Anonymity level used.
  76. */
  77. uint32_t anonymity;
  78. /**
  79. * Verbosity level of the current operation.
  80. */
  81. unsigned int verbose;
  82. /**
  83. * Are we testing indexing? (YES: index, NO: insert, SYSERR: simulate)
  84. */
  85. int do_index;
  86. };
  87. /**
  88. * Handle for a download operation started for testing FS.
  89. */
  90. struct TestDownloadOperation
  91. {
  92. /**
  93. * Handle for the operation to connect to the peer's 'fs' service.
  94. */
  95. struct GNUNET_TESTBED_Operation *fs_op;
  96. /**
  97. * Handle to the file sharing context using this daemon.
  98. */
  99. struct GNUNET_FS_Handle *fs;
  100. /**
  101. * Handle to the daemon via testing.
  102. */
  103. struct GNUNET_TESTING_Daemon *daemon;
  104. /**
  105. * Function to call when download is done.
  106. */
  107. GNUNET_SCHEDULER_Task download_cont;
  108. /**
  109. * Closure for download_cont.
  110. */
  111. void *download_cont_cls;
  112. /**
  113. * URI to download.
  114. */
  115. struct GNUNET_FS_Uri *uri;
  116. /**
  117. * Task to abort downloading (timeout).
  118. */
  119. GNUNET_SCHEDULER_TaskIdentifier download_timeout_task;
  120. /**
  121. * Context for current download operation.
  122. */
  123. struct GNUNET_FS_DownloadContext *download_context;
  124. /**
  125. * Size of the file.
  126. */
  127. uint64_t size;
  128. /**
  129. * Anonymity level used.
  130. */
  131. uint32_t anonymity;
  132. /**
  133. * Seed for download verification.
  134. */
  135. uint32_t download_seed;
  136. /**
  137. * Verbosity level of the current operation.
  138. */
  139. unsigned int verbose;
  140. };
  141. /**
  142. * Task scheduled to report on the completion of our publish operation.
  143. *
  144. * @param cls the publish operation context
  145. * @param tc scheduler context (unused)
  146. */
  147. static void
  148. report_uri (void *cls,
  149. const struct GNUNET_SCHEDULER_TaskContext *tc)
  150. {
  151. struct TestPublishOperation *po = cls;
  152. GNUNET_FS_publish_stop (po->publish_context);
  153. GNUNET_TESTBED_operation_done (po->fs_op);
  154. po->publish_cont (po->publish_cont_cls,
  155. po->publish_uri,
  156. (GNUNET_YES == po->do_index)
  157. ? po->publish_tmp_file
  158. : NULL);
  159. GNUNET_FS_uri_destroy (po->publish_uri);
  160. if ( (GNUNET_YES != po->do_index) &&
  161. (NULL != po->publish_tmp_file) )
  162. (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
  163. GNUNET_free_non_null (po->publish_tmp_file);
  164. GNUNET_free (po);
  165. }
  166. /**
  167. * Task scheduled to run when publish operation times out.
  168. *
  169. * @param cls the publish operation context
  170. * @param tc scheduler context (unused)
  171. */
  172. static void
  173. publish_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  174. {
  175. struct TestPublishOperation *po = cls;
  176. po->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
  177. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  178. "Timeout while trying to publish data\n");
  179. GNUNET_TESTBED_operation_done (po->fs_op);
  180. GNUNET_FS_publish_stop (po->publish_context);
  181. po->publish_cont (po->publish_cont_cls, NULL, NULL);
  182. (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
  183. GNUNET_free_non_null (po->publish_tmp_file);
  184. GNUNET_free (po);
  185. }
  186. /**
  187. * Progress callback for file-sharing events while publishing.
  188. *
  189. * @param cls the publish operation context
  190. * @param info information about the event
  191. */
  192. static void *
  193. publish_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
  194. {
  195. struct TestPublishOperation *po = cls;
  196. switch (info->status)
  197. {
  198. case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
  199. GNUNET_SCHEDULER_cancel (po->publish_timeout_task);
  200. po->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
  201. po->publish_uri =
  202. GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
  203. GNUNET_SCHEDULER_add_continuation (&report_uri, po,
  204. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  205. break;
  206. case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
  207. if (po->verbose)
  208. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Publishing at %llu/%llu bytes\n",
  209. (unsigned long long) info->value.publish.completed,
  210. (unsigned long long) info->value.publish.size);
  211. break;
  212. case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
  213. break;
  214. case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
  215. if (po->verbose)
  216. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
  217. (unsigned long long) info->value.download.completed,
  218. (unsigned long long) info->value.download.size);
  219. break;
  220. default:
  221. break;
  222. }
  223. return NULL;
  224. }
  225. /**
  226. * Generate test data for publishing test.
  227. *
  228. * @param cls pointer to uint32_t with publishing seed
  229. * @param offset offset to generate data for
  230. * @param max maximum number of bytes to generate
  231. * @param buf where to write generated data
  232. * @param emsg where to store error message (unused)
  233. * @return number of bytes written to buf
  234. */
  235. static size_t
  236. file_generator (void *cls,
  237. uint64_t offset,
  238. size_t max,
  239. void *buf,
  240. char **emsg)
  241. {
  242. uint32_t *publish_seed = cls;
  243. uint64_t pos;
  244. uint8_t *cbuf = buf;
  245. int mod;
  246. if (emsg != NULL)
  247. *emsg = NULL;
  248. if (buf == NULL)
  249. return 0;
  250. for (pos = 0; pos < 8; pos++)
  251. cbuf[pos] = (uint8_t) (offset >> pos * 8);
  252. for (pos = 8; pos < max; pos++)
  253. {
  254. mod = (255 - (offset / 1024 / 32));
  255. if (mod == 0)
  256. mod = 1;
  257. cbuf[pos] = (uint8_t) ((offset * (*publish_seed)) % mod);
  258. }
  259. return max;
  260. }
  261. /**
  262. * Connect adapter for publishing operation.
  263. *
  264. * @param cls the 'struct TestPublishOperation'
  265. * @param cfg configuration of the peer to connect to; will be available until
  266. * GNUNET_TESTBED_operation_done() is called on the operation returned
  267. * from GNUNET_TESTBED_service_connect()
  268. * @return service handle to return in 'op_result', NULL on error
  269. */
  270. static void *
  271. publish_connect_adapter (void *cls,
  272. const struct GNUNET_CONFIGURATION_Handle *cfg)
  273. {
  274. struct TestPublishOperation *po = cls;
  275. return GNUNET_FS_start (cfg,
  276. "fs-test-publish",
  277. &publish_progress_cb, po,
  278. GNUNET_FS_FLAGS_NONE,
  279. GNUNET_FS_OPTIONS_END);
  280. }
  281. /**
  282. * Adapter function called to destroy connection to file-sharing service.
  283. *
  284. * @param cls the 'struct GNUNET_FS_Handle'
  285. * @param op_result unused (different for publish/download!)
  286. */
  287. static void
  288. fs_disconnect_adapter (void *cls,
  289. void *op_result)
  290. {
  291. struct GNUNET_FS_Handle *fs = op_result;
  292. GNUNET_FS_stop (fs);
  293. }
  294. /**
  295. * Callback to be called when testbed has connected to the fs service
  296. *
  297. * @param cls the 'struct TestPublishOperation'
  298. * @param op the operation that has been finished
  299. * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
  300. * @param emsg error message in case the operation has failed; will be NULL if
  301. * operation has executed successfully.
  302. */
  303. static void
  304. publish_fs_connect_complete_cb (void *cls,
  305. struct GNUNET_TESTBED_Operation *op,
  306. void *ca_result,
  307. const char *emsg)
  308. {
  309. struct TestPublishOperation *po = cls;
  310. struct GNUNET_FS_FileInformation *fi;
  311. struct GNUNET_DISK_FileHandle *fh;
  312. char *em;
  313. uint64_t off;
  314. char buf[DBLOCK_SIZE];
  315. size_t bsize;
  316. struct GNUNET_FS_BlockOptions bo;
  317. if (NULL == ca_result)
  318. {
  319. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect to FS for publishing: %s\n", emsg);
  320. po->publish_cont (po->publish_cont_cls,
  321. NULL, NULL);
  322. GNUNET_TESTBED_operation_done (po->fs_op);
  323. GNUNET_free (po);
  324. return;
  325. }
  326. po->fs = ca_result;
  327. bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
  328. bo.anonymity_level = po->anonymity;
  329. bo.content_priority = 42;
  330. bo.replication_level = 1;
  331. if (GNUNET_YES == po->do_index)
  332. {
  333. po->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
  334. GNUNET_assert (po->publish_tmp_file != NULL);
  335. fh = GNUNET_DISK_file_open (po->publish_tmp_file,
  336. GNUNET_DISK_OPEN_WRITE |
  337. GNUNET_DISK_OPEN_CREATE,
  338. GNUNET_DISK_PERM_USER_READ |
  339. GNUNET_DISK_PERM_USER_WRITE);
  340. GNUNET_assert (NULL != fh);
  341. off = 0;
  342. while (off < po->size)
  343. {
  344. bsize = GNUNET_MIN (sizeof (buf), po->size - off);
  345. emsg = NULL;
  346. GNUNET_assert (bsize == file_generator (&po->publish_seed, off, bsize, buf, &em));
  347. GNUNET_assert (em == NULL);
  348. GNUNET_assert (bsize == GNUNET_DISK_file_write (fh, buf, bsize));
  349. off += bsize;
  350. }
  351. GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
  352. fi = GNUNET_FS_file_information_create_from_file (po->fs, po,
  353. po->publish_tmp_file,
  354. NULL, NULL, po->do_index,
  355. &bo);
  356. GNUNET_assert (NULL != fi);
  357. }
  358. else
  359. {
  360. fi = GNUNET_FS_file_information_create_from_reader (po->fs, po,
  361. po->size,
  362. &file_generator, &po->publish_seed,
  363. NULL, NULL,
  364. po->do_index, &bo);
  365. GNUNET_assert (NULL != fi);
  366. }
  367. po->publish_context =
  368. GNUNET_FS_publish_start (po->fs, fi, NULL, NULL, NULL,
  369. GNUNET_FS_PUBLISH_OPTION_NONE);
  370. }
  371. /**
  372. * Publish a file at the given peer.
  373. *
  374. * @param peer where to publish
  375. * @param timeout if this operation cannot be completed within the
  376. * given period, call the continuation with an error code
  377. * @param anonymity option for publication
  378. * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
  379. * GNUNET_SYSERR for simulation
  380. * @param size size of the file to publish
  381. * @param seed seed to use for file generation
  382. * @param verbose how verbose to be in reporting
  383. * @param cont function to call when done
  384. * @param cont_cls closure for cont
  385. */
  386. void
  387. GNUNET_FS_TEST_publish (struct GNUNET_TESTBED_Peer *peer,
  388. struct GNUNET_TIME_Relative timeout, uint32_t anonymity,
  389. int do_index, uint64_t size, uint32_t seed,
  390. unsigned int verbose,
  391. GNUNET_FS_TEST_UriContinuation cont, void *cont_cls)
  392. {
  393. struct TestPublishOperation *po;
  394. po = GNUNET_new (struct TestPublishOperation);
  395. po->publish_cont = cont;
  396. po->publish_cont_cls = cont_cls;
  397. po->publish_seed = seed;
  398. po->anonymity = anonymity;
  399. po->size = size;
  400. po->verbose = verbose;
  401. po->do_index = do_index;
  402. po->fs_op = GNUNET_TESTBED_service_connect (po,
  403. peer,
  404. "fs",
  405. &publish_fs_connect_complete_cb,
  406. po,
  407. &publish_connect_adapter,
  408. &fs_disconnect_adapter,
  409. po);
  410. po->publish_timeout_task =
  411. GNUNET_SCHEDULER_add_delayed (timeout, &publish_timeout, po);
  412. }
  413. /* ************************** download ************************ */
  414. /**
  415. * Task scheduled to run when download operation times out.
  416. *
  417. * @param cls the download operation context
  418. * @param tc scheduler context (unused)
  419. */
  420. static void
  421. download_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  422. {
  423. struct TestDownloadOperation *dop = cls;
  424. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  425. "Timeout while trying to download file\n");
  426. dop->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
  427. GNUNET_FS_download_stop (dop->download_context, GNUNET_YES);
  428. GNUNET_SCHEDULER_add_continuation (dop->download_cont,
  429. dop->download_cont_cls,
  430. GNUNET_SCHEDULER_REASON_TIMEOUT);
  431. GNUNET_TESTBED_operation_done (dop->fs_op);
  432. GNUNET_FS_uri_destroy (dop->uri);
  433. GNUNET_free (dop);
  434. }
  435. /**
  436. * Task scheduled to report on the completion of our download operation.
  437. *
  438. * @param cls the download operation context
  439. * @param tc scheduler context (unused)
  440. */
  441. static void
  442. report_success (void *cls,
  443. const struct GNUNET_SCHEDULER_TaskContext *tc)
  444. {
  445. struct TestDownloadOperation *dop = cls;
  446. GNUNET_FS_download_stop (dop->download_context, GNUNET_YES);
  447. GNUNET_SCHEDULER_add_continuation (dop->download_cont,
  448. dop->download_cont_cls,
  449. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  450. GNUNET_TESTBED_operation_done (dop->fs_op);
  451. GNUNET_FS_uri_destroy (dop->uri);
  452. GNUNET_free (dop);
  453. }
  454. /**
  455. * Progress callback for file-sharing events while downloading.
  456. *
  457. * @param cls the download operation context
  458. * @param info information about the event
  459. */
  460. static void *
  461. download_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
  462. {
  463. struct TestDownloadOperation *dop = cls;
  464. switch (info->status)
  465. {
  466. case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
  467. if (dop->verbose)
  468. GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
  469. (unsigned long long) info->value.download.completed,
  470. (unsigned long long) info->value.download.size);
  471. break;
  472. case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
  473. GNUNET_SCHEDULER_cancel (dop->download_timeout_task);
  474. dop->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
  475. GNUNET_SCHEDULER_add_continuation (&report_success, dop,
  476. GNUNET_SCHEDULER_REASON_PREREQ_DONE);
  477. break;
  478. case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
  479. case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
  480. break;
  481. /* FIXME: monitor data correctness during download progress */
  482. /* FIXME: do performance reports given sufficient verbosity */
  483. /* FIXME: advance timeout task to "immediate" on error */
  484. default:
  485. break;
  486. }
  487. return NULL;
  488. }
  489. /**
  490. * Connect adapter for download operation.
  491. *
  492. * @param cls the 'struct TestDownloadOperation'
  493. * @param cfg configuration of the peer to connect to; will be available until
  494. * GNUNET_TESTBED_operation_done() is called on the operation returned
  495. * from GNUNET_TESTBED_service_connect()
  496. * @return service handle to return in 'op_result', NULL on error
  497. */
  498. static void *
  499. download_connect_adapter (void *cls,
  500. const struct GNUNET_CONFIGURATION_Handle *cfg)
  501. {
  502. struct TestPublishOperation *po = cls;
  503. return GNUNET_FS_start (cfg,
  504. "fs-test-download",
  505. &download_progress_cb, po,
  506. GNUNET_FS_FLAGS_NONE,
  507. GNUNET_FS_OPTIONS_END);
  508. }
  509. /**
  510. * Callback to be called when testbed has connected to the fs service
  511. *
  512. * @param cls the 'struct TestPublishOperation'
  513. * @param op the operation that has been finished
  514. * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
  515. * @param emsg error message in case the operation has failed; will be NULL if
  516. * operation has executed successfully.
  517. */
  518. static void
  519. download_fs_connect_complete_cb (void *cls,
  520. struct GNUNET_TESTBED_Operation *op,
  521. void *ca_result,
  522. const char *emsg)
  523. {
  524. struct TestDownloadOperation *dop = cls;
  525. dop->fs = ca_result;
  526. GNUNET_assert (NULL != dop->fs);
  527. dop->download_context =
  528. GNUNET_FS_download_start (dop->fs, dop->uri, NULL, NULL, NULL, 0, dop->size,
  529. dop->anonymity, GNUNET_FS_DOWNLOAD_OPTION_NONE,
  530. NULL, NULL);
  531. }
  532. /**
  533. * Perform test download.
  534. *
  535. * @param peer which peer to download from
  536. * @param timeout if this operation cannot be completed within the
  537. * given period, call the continuation with an error code
  538. * @param anonymity option for download
  539. * @param seed used for file validation
  540. * @param uri URI of file to download (CHK/LOC only)
  541. * @param verbose how verbose to be in reporting
  542. * @param cont function to call when done
  543. * @param cont_cls closure for cont
  544. */
  545. void
  546. GNUNET_FS_TEST_download (struct GNUNET_TESTBED_Peer *peer,
  547. struct GNUNET_TIME_Relative timeout,
  548. uint32_t anonymity, uint32_t seed,
  549. const struct GNUNET_FS_Uri *uri, unsigned int verbose,
  550. GNUNET_SCHEDULER_Task cont, void *cont_cls)
  551. {
  552. struct TestDownloadOperation *dop;
  553. dop = GNUNET_new (struct TestDownloadOperation);
  554. dop->uri = GNUNET_FS_uri_dup (uri);
  555. dop->size = GNUNET_FS_uri_chk_get_file_size (uri);
  556. dop->verbose = verbose;
  557. dop->anonymity = anonymity;
  558. dop->download_cont = cont;
  559. dop->download_cont_cls = cont_cls;
  560. dop->download_seed = seed;
  561. dop->fs_op = GNUNET_TESTBED_service_connect (dop,
  562. peer,
  563. "fs",
  564. &download_fs_connect_complete_cb,
  565. dop,
  566. &download_connect_adapter,
  567. &fs_disconnect_adapter,
  568. dop);
  569. dop->download_timeout_task =
  570. GNUNET_SCHEDULER_add_delayed (timeout, &download_timeout, dop);
  571. }
  572. /* end of fs_test_lib.c */