2 This file is part of GNUnet.
3 Copyright (C) 2010, 2011, 2012 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
17 * @file fs/fs_test_lib.c
18 * @brief library routines for testing FS publishing and downloading;
19 * this code is limited to flat files
20 * and no keywords (those functions can be tested with
21 * single-peer setups; this is for testing routing).
22 * @author Christian Grothoff
26 #include "fs_test_lib.h"
29 #define CONTENT_LIFETIME GNUNET_TIME_UNIT_HOURS
33 * Handle for a publishing operation started for testing FS.
35 struct TestPublishOperation
39 * Handle for the operation to connect to the peer's 'fs' service.
41 struct GNUNET_TESTBED_Operation *fs_op;
44 * Handle to the file sharing context using this daemon.
46 struct GNUNET_FS_Handle *fs;
49 * Function to call when upload is done.
51 GNUNET_FS_TEST_UriContinuation publish_cont;
54 * Closure for publish_cont.
56 void *publish_cont_cls;
59 * Task to abort publishing (timeout).
61 struct GNUNET_SCHEDULER_Task * publish_timeout_task;
64 * Seed for file generation.
66 uint32_t publish_seed;
69 * Context for current publishing operation.
71 struct GNUNET_FS_PublishContext *publish_context;
76 struct GNUNET_FS_Uri *publish_uri;
79 * Name of the temporary file used, or NULL for none.
81 char *publish_tmp_file;
89 * Anonymity level used.
94 * Verbosity level of the current operation.
99 * Are we testing indexing? (YES: index, NO: insert, SYSERR: simulate)
106 * Handle for a download operation started for testing FS.
108 struct TestDownloadOperation
112 * Handle for the operation to connect to the peer's 'fs' service.
114 struct GNUNET_TESTBED_Operation *fs_op;
117 * Handle to the file sharing context using this daemon.
119 struct GNUNET_FS_Handle *fs;
122 * Handle to the daemon via testing.
124 struct GNUNET_TESTING_Daemon *daemon;
127 * Function to call when download is done.
129 GNUNET_SCHEDULER_TaskCallback download_cont;
132 * Closure for download_cont.
134 void *download_cont_cls;
139 struct GNUNET_FS_Uri *uri;
142 * Task to abort downloading (timeout).
144 struct GNUNET_SCHEDULER_Task * download_timeout_task;
147 * Context for current download operation.
149 struct GNUNET_FS_DownloadContext *download_context;
157 * Anonymity level used.
162 * Seed for download verification.
164 uint32_t download_seed;
167 * Verbosity level of the current operation.
169 unsigned int verbose;
175 * Task scheduled to report on the completion of our publish operation.
177 * @param cls the publish operation context
178 * @param tc scheduler context (unused)
181 report_uri (void *cls)
183 struct TestPublishOperation *po = cls;
185 GNUNET_FS_publish_stop (po->publish_context);
186 GNUNET_TESTBED_operation_done (po->fs_op);
187 po->publish_cont (po->publish_cont_cls,
189 (GNUNET_YES == po->do_index)
190 ? po->publish_tmp_file
192 GNUNET_FS_uri_destroy (po->publish_uri);
193 if ( (GNUNET_YES != po->do_index) &&
194 (NULL != po->publish_tmp_file) )
195 (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
196 GNUNET_free_non_null (po->publish_tmp_file);
202 * Task scheduled to run when publish operation times out.
204 * @param cls the publish operation context
207 publish_timeout (void *cls)
209 struct TestPublishOperation *po = cls;
211 po->publish_timeout_task = NULL;
212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
213 "Timeout while trying to publish data\n");
214 GNUNET_TESTBED_operation_done (po->fs_op);
215 GNUNET_FS_publish_stop (po->publish_context);
216 po->publish_cont (po->publish_cont_cls, NULL, NULL);
217 (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
218 GNUNET_free_non_null (po->publish_tmp_file);
224 * Progress callback for file-sharing events while publishing.
226 * @param cls the publish operation context
227 * @param info information about the event
230 publish_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
232 struct TestPublishOperation *po = cls;
234 switch (info->status)
236 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
237 GNUNET_SCHEDULER_cancel (po->publish_timeout_task);
238 po->publish_timeout_task = NULL;
240 GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
241 GNUNET_SCHEDULER_add_now (&report_uri,
244 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
246 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Publishing at %llu/%llu bytes\n",
247 (unsigned long long) info->value.publish.completed,
248 (unsigned long long) info->value.publish.size);
250 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
252 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
254 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
255 (unsigned long long) info->value.download.completed,
256 (unsigned long long) info->value.download.size);
266 * Generate test data for publishing test.
268 * @param cls pointer to uint32_t with publishing seed
269 * @param offset offset to generate data for
270 * @param max maximum number of bytes to generate
271 * @param buf where to write generated data
272 * @param emsg where to store error message (unused)
273 * @return number of bytes written to buf
276 file_generator (void *cls,
282 uint32_t *publish_seed = cls;
291 for (pos = 0; pos < 8; pos++)
292 cbuf[pos] = (uint8_t) (offset >> pos * 8);
293 for (pos = 8; pos < max; pos++)
295 mod = (255 - (offset / 1024 / 32));
298 cbuf[pos] = (uint8_t) ((offset * (*publish_seed)) % mod);
305 * Connect adapter for publishing operation.
307 * @param cls the 'struct TestPublishOperation'
308 * @param cfg configuration of the peer to connect to; will be available until
309 * GNUNET_TESTBED_operation_done() is called on the operation returned
310 * from GNUNET_TESTBED_service_connect()
311 * @return service handle to return in 'op_result', NULL on error
314 publish_connect_adapter (void *cls,
315 const struct GNUNET_CONFIGURATION_Handle *cfg)
317 struct TestPublishOperation *po = cls;
319 return GNUNET_FS_start (cfg,
321 &publish_progress_cb, po,
322 GNUNET_FS_FLAGS_NONE,
323 GNUNET_FS_OPTIONS_END);
328 * Adapter function called to destroy connection to file-sharing service.
330 * @param cls the 'struct GNUNET_FS_Handle'
331 * @param op_result unused (different for publish/download!)
334 fs_disconnect_adapter (void *cls,
337 struct GNUNET_FS_Handle *fs = op_result;
344 * Callback to be called when testbed has connected to the fs service
346 * @param cls the 'struct TestPublishOperation'
347 * @param op the operation that has been finished
348 * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
349 * @param emsg error message in case the operation has failed; will be NULL if
350 * operation has executed successfully.
353 publish_fs_connect_complete_cb (void *cls,
354 struct GNUNET_TESTBED_Operation *op,
358 struct TestPublishOperation *po = cls;
359 struct GNUNET_FS_FileInformation *fi;
360 struct GNUNET_DISK_FileHandle *fh;
363 char buf[DBLOCK_SIZE];
365 struct GNUNET_FS_BlockOptions bo;
367 if (NULL == ca_result)
369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect to FS for publishing: %s\n", emsg);
370 po->publish_cont (po->publish_cont_cls,
372 GNUNET_TESTBED_operation_done (po->fs_op);
378 bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
379 bo.anonymity_level = po->anonymity;
380 bo.content_priority = 42;
381 bo.replication_level = 1;
382 if (GNUNET_YES == po->do_index)
384 po->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
385 GNUNET_assert (po->publish_tmp_file != NULL);
386 fh = GNUNET_DISK_file_open (po->publish_tmp_file,
387 GNUNET_DISK_OPEN_WRITE |
388 GNUNET_DISK_OPEN_CREATE,
389 GNUNET_DISK_PERM_USER_READ |
390 GNUNET_DISK_PERM_USER_WRITE);
391 GNUNET_assert (NULL != fh);
393 while (off < po->size)
395 bsize = GNUNET_MIN (sizeof (buf), po->size - off);
397 GNUNET_assert (bsize == file_generator (&po->publish_seed, off, bsize, buf, &em));
398 GNUNET_assert (em == NULL);
399 GNUNET_assert (bsize == GNUNET_DISK_file_write (fh, buf, bsize));
402 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
403 fi = GNUNET_FS_file_information_create_from_file (po->fs, po,
404 po->publish_tmp_file,
405 NULL, NULL, po->do_index,
407 GNUNET_assert (NULL != fi);
411 fi = GNUNET_FS_file_information_create_from_reader (po->fs, po,
413 &file_generator, &po->publish_seed,
416 GNUNET_assert (NULL != fi);
418 po->publish_context =
419 GNUNET_FS_publish_start (po->fs, fi, NULL, NULL, NULL,
420 GNUNET_FS_PUBLISH_OPTION_NONE);
425 * Publish a file at the given peer.
427 * @param peer where to publish
428 * @param timeout if this operation cannot be completed within the
429 * given period, call the continuation with an error code
430 * @param anonymity option for publication
431 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
432 * GNUNET_SYSERR for simulation
433 * @param size size of the file to publish
434 * @param seed seed to use for file generation
435 * @param verbose how verbose to be in reporting
436 * @param cont function to call when done
437 * @param cont_cls closure for cont
440 GNUNET_FS_TEST_publish (struct GNUNET_TESTBED_Peer *peer,
441 struct GNUNET_TIME_Relative timeout, uint32_t anonymity,
442 int do_index, uint64_t size, uint32_t seed,
443 unsigned int verbose,
444 GNUNET_FS_TEST_UriContinuation cont, void *cont_cls)
446 struct TestPublishOperation *po;
448 po = GNUNET_new (struct TestPublishOperation);
449 po->publish_cont = cont;
450 po->publish_cont_cls = cont_cls;
451 po->publish_seed = seed;
452 po->anonymity = anonymity;
454 po->verbose = verbose;
455 po->do_index = do_index;
456 po->fs_op = GNUNET_TESTBED_service_connect (po,
459 &publish_fs_connect_complete_cb,
461 &publish_connect_adapter,
462 &fs_disconnect_adapter,
464 po->publish_timeout_task =
465 GNUNET_SCHEDULER_add_delayed (timeout, &publish_timeout, po);
469 /* ************************** download ************************ */
473 * Task scheduled to run when download operation times out.
475 * @param cls the download operation context
478 download_timeout (void *cls)
480 struct TestDownloadOperation *dop = cls;
482 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
483 "Timeout while trying to download file\n");
484 dop->download_timeout_task = NULL;
485 GNUNET_FS_download_stop (dop->download_context,
487 GNUNET_SCHEDULER_add_now (dop->download_cont,
488 dop->download_cont_cls);
489 GNUNET_TESTBED_operation_done (dop->fs_op);
490 GNUNET_FS_uri_destroy (dop->uri);
496 * Task scheduled to report on the completion of our download operation.
498 * @param cls the download operation context
501 report_success (void *cls)
503 struct TestDownloadOperation *dop = cls;
505 GNUNET_FS_download_stop (dop->download_context,
507 GNUNET_SCHEDULER_add_now (dop->download_cont,
508 dop->download_cont_cls);
509 GNUNET_TESTBED_operation_done (dop->fs_op);
510 GNUNET_FS_uri_destroy (dop->uri);
516 * Progress callback for file-sharing events while downloading.
518 * @param cls the download operation context
519 * @param info information about the event
522 download_progress_cb (void *cls,
523 const struct GNUNET_FS_ProgressInfo *info)
525 struct TestDownloadOperation *dop = cls;
527 switch (info->status)
529 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
531 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
532 "Download at %llu/%llu bytes\n",
533 (unsigned long long) info->value.download.completed,
534 (unsigned long long) info->value.download.size);
536 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
537 GNUNET_SCHEDULER_cancel (dop->download_timeout_task);
538 dop->download_timeout_task = NULL;
539 GNUNET_SCHEDULER_add_now (&report_success, dop);
541 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
542 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
544 /* FIXME: monitor data correctness during download progress */
545 /* FIXME: do performance reports given sufficient verbosity */
546 /* FIXME: advance timeout task to "immediate" on error */
555 * Connect adapter for download operation.
557 * @param cls the 'struct TestDownloadOperation'
558 * @param cfg configuration of the peer to connect to; will be available until
559 * GNUNET_TESTBED_operation_done() is called on the operation returned
560 * from GNUNET_TESTBED_service_connect()
561 * @return service handle to return in 'op_result', NULL on error
564 download_connect_adapter (void *cls,
565 const struct GNUNET_CONFIGURATION_Handle *cfg)
567 struct TestPublishOperation *po = cls;
569 return GNUNET_FS_start (cfg,
571 &download_progress_cb, po,
572 GNUNET_FS_FLAGS_NONE,
573 GNUNET_FS_OPTIONS_END);
578 * Callback to be called when testbed has connected to the fs service
580 * @param cls the 'struct TestPublishOperation'
581 * @param op the operation that has been finished
582 * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
583 * @param emsg error message in case the operation has failed; will be NULL if
584 * operation has executed successfully.
587 download_fs_connect_complete_cb (void *cls,
588 struct GNUNET_TESTBED_Operation *op,
592 struct TestDownloadOperation *dop = cls;
595 GNUNET_assert (NULL != dop->fs);
596 dop->download_context =
597 GNUNET_FS_download_start (dop->fs, dop->uri, NULL, NULL, NULL, 0, dop->size,
598 dop->anonymity, GNUNET_FS_DOWNLOAD_OPTION_NONE,
604 * Perform test download.
606 * @param peer which peer to download from
607 * @param timeout if this operation cannot be completed within the
608 * given period, call the continuation with an error code
609 * @param anonymity option for download
610 * @param seed used for file validation
611 * @param uri URI of file to download (CHK/LOC only)
612 * @param verbose how verbose to be in reporting
613 * @param cont function to call when done
614 * @param cont_cls closure for cont
617 GNUNET_FS_TEST_download (struct GNUNET_TESTBED_Peer *peer,
618 struct GNUNET_TIME_Relative timeout,
619 uint32_t anonymity, uint32_t seed,
620 const struct GNUNET_FS_Uri *uri, unsigned int verbose,
621 GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls)
623 struct TestDownloadOperation *dop;
625 dop = GNUNET_new (struct TestDownloadOperation);
626 dop->uri = GNUNET_FS_uri_dup (uri);
627 dop->size = GNUNET_FS_uri_chk_get_file_size (uri);
628 dop->verbose = verbose;
629 dop->anonymity = anonymity;
630 dop->download_cont = cont;
631 dop->download_cont_cls = cont_cls;
632 dop->download_seed = seed;
634 dop->fs_op = GNUNET_TESTBED_service_connect (dop,
637 &download_fs_connect_complete_cb,
639 &download_connect_adapter,
640 &fs_disconnect_adapter,
642 dop->download_timeout_task =
643 GNUNET_SCHEDULER_add_delayed (timeout, &download_timeout, dop);
647 /* end of fs_test_lib.c */