2 This file is part of GNUnet.
3 (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file fs/fs_test_lib.c
23 * @brief library routines for testing FS publishing and downloading;
24 * this code is limited to flat files
25 * and no keywords (those functions can be tested with
26 * single-peer setups; this is for testing routing).
27 * @author Christian Grothoff
31 #include "fs_test_lib.h"
34 #define CONTENT_LIFETIME GNUNET_TIME_UNIT_HOURS
38 * Handle for a publishing operation started for testing FS.
40 struct TestPublishOperation
44 * Handle for the operation to connect to the peer's 'fs' service.
46 struct GNUNET_TESTBED_Operation *fs_op;
49 * Handle to the file sharing context using this daemon.
51 struct GNUNET_FS_Handle *fs;
54 * Function to call when upload is done.
56 GNUNET_FS_TEST_UriContinuation publish_cont;
59 * Closure for publish_cont.
61 void *publish_cont_cls;
64 * Task to abort publishing (timeout).
66 GNUNET_SCHEDULER_TaskIdentifier publish_timeout_task;
69 * Seed for file generation.
71 uint32_t publish_seed;
74 * Context for current publishing operation.
76 struct GNUNET_FS_PublishContext *publish_context;
81 struct GNUNET_FS_Uri *publish_uri;
84 * Name of the temporary file used, or NULL for none.
86 char *publish_tmp_file;
94 * Anonymity level used.
99 * Verbosity level of the current operation.
101 unsigned int verbose;
104 * Are we testing indexing? (YES: index, NO: insert, SYSERR: simulate)
111 * Handle for a download operation started for testing FS.
113 struct TestDownloadOperation
117 * Handle for the operation to connect to the peer's 'fs' service.
119 struct GNUNET_TESTBED_Operation *fs_op;
122 * Handle to the file sharing context using this daemon.
124 struct GNUNET_FS_Handle *fs;
127 * Handle to the daemon via testing.
129 struct GNUNET_TESTING_Daemon *daemon;
132 * Function to call when download is done.
134 GNUNET_SCHEDULER_Task download_cont;
137 * Closure for download_cont.
139 void *download_cont_cls;
144 struct GNUNET_FS_Uri *uri;
147 * Task to abort downloading (timeout).
149 GNUNET_SCHEDULER_TaskIdentifier download_timeout_task;
152 * Context for current download operation.
154 struct GNUNET_FS_DownloadContext *download_context;
162 * Anonymity level used.
167 * Seed for download verification.
169 uint32_t download_seed;
172 * Verbosity level of the current operation.
174 unsigned int verbose;
180 * Task scheduled to report on the completion of our publish operation.
182 * @param cls the publish operation context
183 * @param tc scheduler context (unused)
186 report_uri (void *cls,
187 const struct GNUNET_SCHEDULER_TaskContext *tc)
189 struct TestPublishOperation *po = cls;
191 GNUNET_FS_publish_stop (po->publish_context);
192 GNUNET_TESTBED_operation_done (po->fs_op);
193 po->publish_cont (po->publish_cont_cls,
195 (GNUNET_YES == po->do_index)
196 ? po->publish_tmp_file
198 GNUNET_FS_uri_destroy (po->publish_uri);
199 if (GNUNET_YES != po->do_index)
200 (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
201 GNUNET_free_non_null (po->publish_tmp_file);
207 * Task scheduled to run when publish operation times out.
209 * @param cls the publish operation context
210 * @param tc scheduler context (unused)
213 publish_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
215 struct TestPublishOperation *po = cls;
217 po->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
218 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
219 "Timeout while trying to publish data\n");
220 GNUNET_TESTBED_operation_done (po->fs_op);
221 GNUNET_FS_publish_stop (po->publish_context);
222 po->publish_cont (po->publish_cont_cls, NULL, NULL);
223 (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
224 GNUNET_free_non_null (po->publish_tmp_file);
230 * Progress callback for file-sharing events while publishing.
232 * @param cls the publish operation context
233 * @param info information about the event
236 publish_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
238 struct TestPublishOperation *po = cls;
240 switch (info->status)
242 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
243 GNUNET_SCHEDULER_cancel (po->publish_timeout_task);
244 po->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
246 GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
247 GNUNET_SCHEDULER_add_continuation (&report_uri, po,
248 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
250 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
252 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Publishing at %llu/%llu bytes\n",
253 (unsigned long long) info->value.publish.completed,
254 (unsigned long long) info->value.publish.size);
256 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
258 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
259 (unsigned long long) info->value.download.completed,
260 (unsigned long long) info->value.download.size);
270 * Generate test data for publishing test.
272 * @param cls pointer to uint32_t with publishing seed
273 * @param offset offset to generate data for
274 * @param max maximum number of bytes to generate
275 * @param buf where to write generated data
276 * @param emsg where to store error message (unused)
277 * @return number of bytes written to buf
280 file_generator (void *cls,
286 uint32_t *publish_seed = cls;
295 for (pos = 0; pos < 8; pos++)
296 cbuf[pos] = (uint8_t) (offset >> pos * 8);
297 for (pos = 8; pos < max; pos++)
299 mod = (255 - (offset / 1024 / 32));
302 cbuf[pos] = (uint8_t) ((offset * (*publish_seed)) % mod);
309 * Connect adapter for publishing operation.
311 * @param cls the 'struct TestPublishOperation'
312 * @param cfg configuration of the peer to connect to; will be available until
313 * GNUNET_TESTBED_operation_done() is called on the operation returned
314 * from GNUNET_TESTBED_service_connect()
315 * @return service handle to return in 'op_result', NULL on error
318 publish_connect_adapter (void *cls,
319 const struct GNUNET_CONFIGURATION_Handle *cfg)
321 struct TestPublishOperation *po = cls;
323 return GNUNET_FS_start (cfg,
325 &publish_progress_cb, po,
326 GNUNET_FS_FLAGS_NONE,
327 GNUNET_FS_OPTIONS_END);
332 * Adapter function called to destroy connection to file-sharing service.
334 * @param cls the 'struct GNUNET_FS_Handle'
335 * @param op_result unused (different for publish/download!)
338 fs_disconnect_adapter (void *cls,
341 struct GNUNET_FS_Handle *fs = op_result;
348 * Callback to be called when testbed has connected to the fs service
350 * @param cls the 'struct TestPublishOperation'
351 * @param op the operation that has been finished
352 * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
353 * @param emsg error message in case the operation has failed; will be NULL if
354 * operation has executed successfully.
357 publish_fs_connect_complete_cb (void *cls,
358 struct GNUNET_TESTBED_Operation *op,
362 struct TestPublishOperation *po = cls;
363 struct GNUNET_FS_FileInformation *fi;
364 struct GNUNET_DISK_FileHandle *fh;
367 char buf[DBLOCK_SIZE];
369 struct GNUNET_FS_BlockOptions bo;
371 if (NULL == ca_result)
373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect to FS for publishing: %s\n", emsg);
374 po->publish_cont (po->publish_cont_cls,
376 GNUNET_TESTBED_operation_done (po->fs_op);
382 bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
383 bo.anonymity_level = po->anonymity;
384 bo.content_priority = 42;
385 bo.replication_level = 1;
386 if (GNUNET_YES == po->do_index)
388 po->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
389 GNUNET_assert (po->publish_tmp_file != NULL);
390 fh = GNUNET_DISK_file_open (po->publish_tmp_file,
391 GNUNET_DISK_OPEN_WRITE |
392 GNUNET_DISK_OPEN_CREATE,
393 GNUNET_DISK_PERM_USER_READ |
394 GNUNET_DISK_PERM_USER_WRITE);
395 GNUNET_assert (NULL != fh);
397 while (off < po->size)
399 bsize = GNUNET_MIN (sizeof (buf), po->size - off);
401 GNUNET_assert (bsize == file_generator (&po->publish_seed, off, bsize, buf, &em));
402 GNUNET_assert (em == NULL);
403 GNUNET_assert (bsize == GNUNET_DISK_file_write (fh, buf, bsize));
406 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
407 fi = GNUNET_FS_file_information_create_from_file (po->fs, po,
408 po->publish_tmp_file,
409 NULL, NULL, po->do_index,
414 fi = GNUNET_FS_file_information_create_from_reader (po->fs, po,
416 &file_generator, &po->publish_seed,
420 po->publish_context =
421 GNUNET_FS_publish_start (po->fs, fi, NULL, NULL, NULL,
422 GNUNET_FS_PUBLISH_OPTION_NONE);
427 * Publish a file at the given peer.
429 * @param peer where to publish
430 * @param timeout if this operation cannot be completed within the
431 * given period, call the continuation with an error code
432 * @param anonymity option for publication
433 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
434 * GNUNET_SYSERR for simulation
435 * @param size size of the file to publish
436 * @param seed seed to use for file generation
437 * @param verbose how verbose to be in reporting
438 * @param cont function to call when done
439 * @param cont_cls closure for cont
442 GNUNET_FS_TEST_publish (struct GNUNET_TESTBED_Peer *peer,
443 struct GNUNET_TIME_Relative timeout, uint32_t anonymity,
444 int do_index, uint64_t size, uint32_t seed,
445 unsigned int verbose,
446 GNUNET_FS_TEST_UriContinuation cont, void *cont_cls)
448 struct TestPublishOperation *po;
450 po = GNUNET_malloc (sizeof (struct TestPublishOperation));
451 po->publish_cont = cont;
452 po->publish_cont_cls = cont_cls;
453 po->publish_seed = seed;
454 po->anonymity = anonymity;
456 po->verbose = verbose;
457 po->do_index = do_index;
458 po->fs_op = GNUNET_TESTBED_service_connect (po,
461 &publish_fs_connect_complete_cb,
463 &publish_connect_adapter,
464 &fs_disconnect_adapter,
466 po->publish_timeout_task =
467 GNUNET_SCHEDULER_add_delayed (timeout, &publish_timeout, po);
471 /* ************************** download ************************ */
475 * Task scheduled to run when download operation times out.
477 * @param cls the download operation context
478 * @param tc scheduler context (unused)
481 download_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
483 struct TestDownloadOperation *dop = cls;
485 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
486 "Timeout while trying to download file\n");
487 dop->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
488 GNUNET_FS_download_stop (dop->download_context, GNUNET_YES);
489 GNUNET_SCHEDULER_add_continuation (dop->download_cont,
490 dop->download_cont_cls,
491 GNUNET_SCHEDULER_REASON_TIMEOUT);
492 GNUNET_TESTBED_operation_done (dop->fs_op);
493 GNUNET_FS_uri_destroy (dop->uri);
499 * Task scheduled to report on the completion of our download operation.
501 * @param cls the download operation context
502 * @param tc scheduler context (unused)
505 report_success (void *cls,
506 const struct GNUNET_SCHEDULER_TaskContext *tc)
508 struct TestDownloadOperation *dop = cls;
510 GNUNET_FS_download_stop (dop->download_context, GNUNET_YES);
511 GNUNET_SCHEDULER_add_continuation (dop->download_cont,
512 dop->download_cont_cls,
513 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
514 GNUNET_TESTBED_operation_done (dop->fs_op);
515 GNUNET_FS_uri_destroy (dop->uri);
521 * Progress callback for file-sharing events while downloading.
523 * @param cls the download operation context
524 * @param info information about the event
527 download_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
529 struct TestDownloadOperation *dop = cls;
531 switch (info->status)
533 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
535 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
536 (unsigned long long) info->value.download.completed,
537 (unsigned long long) info->value.download.size);
539 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
540 GNUNET_SCHEDULER_cancel (dop->download_timeout_task);
541 dop->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
542 GNUNET_SCHEDULER_add_continuation (&report_success, dop,
543 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
545 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
546 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
548 /* FIXME: monitor data correctness during download progress */
549 /* FIXME: do performance reports given sufficient verbosity */
550 /* FIXME: advance timeout task to "immediate" on error */
559 * Connect adapter for download operation.
561 * @param cls the 'struct TestDownloadOperation'
562 * @param cfg configuration of the peer to connect to; will be available until
563 * GNUNET_TESTBED_operation_done() is called on the operation returned
564 * from GNUNET_TESTBED_service_connect()
565 * @return service handle to return in 'op_result', NULL on error
568 download_connect_adapter (void *cls,
569 const struct GNUNET_CONFIGURATION_Handle *cfg)
571 struct TestPublishOperation *po = cls;
573 return GNUNET_FS_start (cfg,
575 &download_progress_cb, po,
576 GNUNET_FS_FLAGS_NONE,
577 GNUNET_FS_OPTIONS_END);
582 * Callback to be called when testbed has connected to the fs service
584 * @param cls the 'struct TestPublishOperation'
585 * @param op the operation that has been finished
586 * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
587 * @param emsg error message in case the operation has failed; will be NULL if
588 * operation has executed successfully.
591 download_fs_connect_complete_cb (void *cls,
592 struct GNUNET_TESTBED_Operation *op,
596 struct TestDownloadOperation *dop = cls;
599 GNUNET_assert (NULL != dop->fs);
600 dop->download_context =
601 GNUNET_FS_download_start (dop->fs, dop->uri, NULL, NULL, NULL, 0, dop->size,
602 dop->anonymity, GNUNET_FS_DOWNLOAD_OPTION_NONE,
608 * Perform test download.
610 * @param peer which peer to download from
611 * @param timeout if this operation cannot be completed within the
612 * given period, call the continuation with an error code
613 * @param anonymity option for download
614 * @param seed used for file validation
615 * @param uri URI of file to download (CHK/LOC only)
616 * @param verbose how verbose to be in reporting
617 * @param cont function to call when done
618 * @param cont_cls closure for cont
621 GNUNET_FS_TEST_download (struct GNUNET_TESTBED_Peer *peer,
622 struct GNUNET_TIME_Relative timeout,
623 uint32_t anonymity, uint32_t seed,
624 const struct GNUNET_FS_Uri *uri, unsigned int verbose,
625 GNUNET_SCHEDULER_Task cont, void *cont_cls)
627 struct TestDownloadOperation *dop;
629 dop = GNUNET_malloc (sizeof (struct TestDownloadOperation));
630 dop->uri = GNUNET_FS_uri_dup (uri);
631 dop->size = GNUNET_FS_uri_chk_get_file_size (uri);
632 dop->verbose = verbose;
633 dop->anonymity = anonymity;
634 dop->download_cont = cont;
635 dop->download_cont_cls = cont_cls;
636 dop->download_seed = seed;
638 dop->fs_op = GNUNET_TESTBED_service_connect (dop,
641 &download_fs_connect_complete_cb,
643 &download_connect_adapter,
644 &fs_disconnect_adapter,
646 dop->download_timeout_task =
647 GNUNET_SCHEDULER_add_delayed (timeout, &download_timeout, dop);
651 /* end of fs_test_lib.c */