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.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file fs/fs_test_lib.c
21 * @brief library routines for testing FS publishing and downloading;
22 * this code is limited to flat files
23 * and no keywords (those functions can be tested with
24 * single-peer setups; this is for testing routing).
25 * @author Christian Grothoff
29 #include "fs_test_lib.h"
32 #define CONTENT_LIFETIME GNUNET_TIME_UNIT_HOURS
36 * Handle for a publishing operation started for testing FS.
38 struct TestPublishOperation
42 * Handle for the operation to connect to the peer's 'fs' service.
44 struct GNUNET_TESTBED_Operation *fs_op;
47 * Handle to the file sharing context using this daemon.
49 struct GNUNET_FS_Handle *fs;
52 * Function to call when upload is done.
54 GNUNET_FS_TEST_UriContinuation publish_cont;
57 * Closure for publish_cont.
59 void *publish_cont_cls;
62 * Task to abort publishing (timeout).
64 struct GNUNET_SCHEDULER_Task * publish_timeout_task;
67 * Seed for file generation.
69 uint32_t publish_seed;
72 * Context for current publishing operation.
74 struct GNUNET_FS_PublishContext *publish_context;
79 struct GNUNET_FS_Uri *publish_uri;
82 * Name of the temporary file used, or NULL for none.
84 char *publish_tmp_file;
92 * Anonymity level used.
97 * Verbosity level of the current operation.
102 * Are we testing indexing? (YES: index, NO: insert, SYSERR: simulate)
109 * Handle for a download operation started for testing FS.
111 struct TestDownloadOperation
115 * Handle for the operation to connect to the peer's 'fs' service.
117 struct GNUNET_TESTBED_Operation *fs_op;
120 * Handle to the file sharing context using this daemon.
122 struct GNUNET_FS_Handle *fs;
125 * Handle to the daemon via testing.
127 struct GNUNET_TESTING_Daemon *daemon;
130 * Function to call when download is done.
132 GNUNET_SCHEDULER_TaskCallback download_cont;
135 * Closure for download_cont.
137 void *download_cont_cls;
142 struct GNUNET_FS_Uri *uri;
145 * Task to abort downloading (timeout).
147 struct GNUNET_SCHEDULER_Task * download_timeout_task;
150 * Context for current download operation.
152 struct GNUNET_FS_DownloadContext *download_context;
160 * Anonymity level used.
165 * Seed for download verification.
167 uint32_t download_seed;
170 * Verbosity level of the current operation.
172 unsigned int verbose;
178 * Task scheduled to report on the completion of our publish operation.
180 * @param cls the publish operation context
181 * @param tc scheduler context (unused)
184 report_uri (void *cls)
186 struct TestPublishOperation *po = cls;
188 GNUNET_FS_publish_stop (po->publish_context);
189 GNUNET_TESTBED_operation_done (po->fs_op);
190 po->publish_cont (po->publish_cont_cls,
192 (GNUNET_YES == po->do_index)
193 ? po->publish_tmp_file
195 GNUNET_FS_uri_destroy (po->publish_uri);
196 if ( (GNUNET_YES != po->do_index) &&
197 (NULL != po->publish_tmp_file) )
198 (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
199 GNUNET_free_non_null (po->publish_tmp_file);
205 * Task scheduled to run when publish operation times out.
207 * @param cls the publish operation context
210 publish_timeout (void *cls)
212 struct TestPublishOperation *po = cls;
214 po->publish_timeout_task = NULL;
215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
216 "Timeout while trying to publish data\n");
217 GNUNET_TESTBED_operation_done (po->fs_op);
218 GNUNET_FS_publish_stop (po->publish_context);
219 po->publish_cont (po->publish_cont_cls, NULL, NULL);
220 (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
221 GNUNET_free_non_null (po->publish_tmp_file);
227 * Progress callback for file-sharing events while publishing.
229 * @param cls the publish operation context
230 * @param info information about the event
233 publish_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
235 struct TestPublishOperation *po = cls;
237 switch (info->status)
239 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
240 GNUNET_SCHEDULER_cancel (po->publish_timeout_task);
241 po->publish_timeout_task = NULL;
243 GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
244 GNUNET_SCHEDULER_add_now (&report_uri,
247 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
249 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Publishing at %llu/%llu bytes\n",
250 (unsigned long long) info->value.publish.completed,
251 (unsigned long long) info->value.publish.size);
253 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
255 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
257 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
258 (unsigned long long) info->value.download.completed,
259 (unsigned long long) info->value.download.size);
269 * Generate test data for publishing test.
271 * @param cls pointer to uint32_t with publishing seed
272 * @param offset offset to generate data for
273 * @param max maximum number of bytes to generate
274 * @param buf where to write generated data
275 * @param emsg where to store error message (unused)
276 * @return number of bytes written to buf
279 file_generator (void *cls,
285 uint32_t *publish_seed = cls;
294 for (pos = 0; pos < 8; pos++)
295 cbuf[pos] = (uint8_t) (offset >> pos * 8);
296 for (pos = 8; pos < max; pos++)
298 mod = (255 - (offset / 1024 / 32));
301 cbuf[pos] = (uint8_t) ((offset * (*publish_seed)) % mod);
308 * Connect adapter for publishing operation.
310 * @param cls the 'struct TestPublishOperation'
311 * @param cfg configuration of the peer to connect to; will be available until
312 * GNUNET_TESTBED_operation_done() is called on the operation returned
313 * from GNUNET_TESTBED_service_connect()
314 * @return service handle to return in 'op_result', NULL on error
317 publish_connect_adapter (void *cls,
318 const struct GNUNET_CONFIGURATION_Handle *cfg)
320 struct TestPublishOperation *po = cls;
322 return GNUNET_FS_start (cfg,
324 &publish_progress_cb, po,
325 GNUNET_FS_FLAGS_NONE,
326 GNUNET_FS_OPTIONS_END);
331 * Adapter function called to destroy connection to file-sharing service.
333 * @param cls the 'struct GNUNET_FS_Handle'
334 * @param op_result unused (different for publish/download!)
337 fs_disconnect_adapter (void *cls,
340 struct GNUNET_FS_Handle *fs = op_result;
347 * Callback to be called when testbed has connected to the fs service
349 * @param cls the 'struct TestPublishOperation'
350 * @param op the operation that has been finished
351 * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
352 * @param emsg error message in case the operation has failed; will be NULL if
353 * operation has executed successfully.
356 publish_fs_connect_complete_cb (void *cls,
357 struct GNUNET_TESTBED_Operation *op,
361 struct TestPublishOperation *po = cls;
362 struct GNUNET_FS_FileInformation *fi;
363 struct GNUNET_DISK_FileHandle *fh;
366 char buf[DBLOCK_SIZE];
368 struct GNUNET_FS_BlockOptions bo;
370 if (NULL == ca_result)
372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect to FS for publishing: %s\n", emsg);
373 po->publish_cont (po->publish_cont_cls,
375 GNUNET_TESTBED_operation_done (po->fs_op);
381 bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
382 bo.anonymity_level = po->anonymity;
383 bo.content_priority = 42;
384 bo.replication_level = 1;
385 if (GNUNET_YES == po->do_index)
387 po->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
388 GNUNET_assert (po->publish_tmp_file != NULL);
389 fh = GNUNET_DISK_file_open (po->publish_tmp_file,
390 GNUNET_DISK_OPEN_WRITE |
391 GNUNET_DISK_OPEN_CREATE,
392 GNUNET_DISK_PERM_USER_READ |
393 GNUNET_DISK_PERM_USER_WRITE);
394 GNUNET_assert (NULL != fh);
396 while (off < po->size)
398 bsize = GNUNET_MIN (sizeof (buf), po->size - off);
400 GNUNET_assert (bsize == file_generator (&po->publish_seed, off, bsize, buf, &em));
401 GNUNET_assert (em == NULL);
402 GNUNET_assert (bsize == GNUNET_DISK_file_write (fh, buf, bsize));
405 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
406 fi = GNUNET_FS_file_information_create_from_file (po->fs, po,
407 po->publish_tmp_file,
408 NULL, NULL, po->do_index,
410 GNUNET_assert (NULL != fi);
414 fi = GNUNET_FS_file_information_create_from_reader (po->fs, po,
416 &file_generator, &po->publish_seed,
419 GNUNET_assert (NULL != fi);
421 po->publish_context =
422 GNUNET_FS_publish_start (po->fs, fi, NULL, NULL, NULL,
423 GNUNET_FS_PUBLISH_OPTION_NONE);
428 * Publish a file at the given peer.
430 * @param peer where to publish
431 * @param timeout if this operation cannot be completed within the
432 * given period, call the continuation with an error code
433 * @param anonymity option for publication
434 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
435 * GNUNET_SYSERR for simulation
436 * @param size size of the file to publish
437 * @param seed seed to use for file generation
438 * @param verbose how verbose to be in reporting
439 * @param cont function to call when done
440 * @param cont_cls closure for cont
443 GNUNET_FS_TEST_publish (struct GNUNET_TESTBED_Peer *peer,
444 struct GNUNET_TIME_Relative timeout, uint32_t anonymity,
445 int do_index, uint64_t size, uint32_t seed,
446 unsigned int verbose,
447 GNUNET_FS_TEST_UriContinuation cont, void *cont_cls)
449 struct TestPublishOperation *po;
451 po = GNUNET_new (struct TestPublishOperation);
452 po->publish_cont = cont;
453 po->publish_cont_cls = cont_cls;
454 po->publish_seed = seed;
455 po->anonymity = anonymity;
457 po->verbose = verbose;
458 po->do_index = do_index;
459 po->fs_op = GNUNET_TESTBED_service_connect (po,
462 &publish_fs_connect_complete_cb,
464 &publish_connect_adapter,
465 &fs_disconnect_adapter,
467 po->publish_timeout_task =
468 GNUNET_SCHEDULER_add_delayed (timeout, &publish_timeout, po);
472 /* ************************** download ************************ */
476 * Task scheduled to run when download operation times out.
478 * @param cls the download operation context
481 download_timeout (void *cls)
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 = NULL;
488 GNUNET_FS_download_stop (dop->download_context,
490 GNUNET_SCHEDULER_add_now (dop->download_cont,
491 dop->download_cont_cls);
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
504 report_success (void *cls)
506 struct TestDownloadOperation *dop = cls;
508 GNUNET_FS_download_stop (dop->download_context,
510 GNUNET_SCHEDULER_add_now (dop->download_cont,
511 dop->download_cont_cls);
512 GNUNET_TESTBED_operation_done (dop->fs_op);
513 GNUNET_FS_uri_destroy (dop->uri);
519 * Progress callback for file-sharing events while downloading.
521 * @param cls the download operation context
522 * @param info information about the event
525 download_progress_cb (void *cls,
526 const struct GNUNET_FS_ProgressInfo *info)
528 struct TestDownloadOperation *dop = cls;
530 switch (info->status)
532 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
534 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
535 "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 = NULL;
542 GNUNET_SCHEDULER_add_now (&report_success, dop);
544 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
545 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
547 /* FIXME: monitor data correctness during download progress */
548 /* FIXME: do performance reports given sufficient verbosity */
549 /* FIXME: advance timeout task to "immediate" on error */
558 * Connect adapter for download operation.
560 * @param cls the 'struct TestDownloadOperation'
561 * @param cfg configuration of the peer to connect to; will be available until
562 * GNUNET_TESTBED_operation_done() is called on the operation returned
563 * from GNUNET_TESTBED_service_connect()
564 * @return service handle to return in 'op_result', NULL on error
567 download_connect_adapter (void *cls,
568 const struct GNUNET_CONFIGURATION_Handle *cfg)
570 struct TestPublishOperation *po = cls;
572 return GNUNET_FS_start (cfg,
574 &download_progress_cb, po,
575 GNUNET_FS_FLAGS_NONE,
576 GNUNET_FS_OPTIONS_END);
581 * Callback to be called when testbed has connected to the fs service
583 * @param cls the 'struct TestPublishOperation'
584 * @param op the operation that has been finished
585 * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
586 * @param emsg error message in case the operation has failed; will be NULL if
587 * operation has executed successfully.
590 download_fs_connect_complete_cb (void *cls,
591 struct GNUNET_TESTBED_Operation *op,
595 struct TestDownloadOperation *dop = cls;
598 GNUNET_assert (NULL != dop->fs);
599 dop->download_context =
600 GNUNET_FS_download_start (dop->fs, dop->uri, NULL, NULL, NULL, 0, dop->size,
601 dop->anonymity, GNUNET_FS_DOWNLOAD_OPTION_NONE,
607 * Perform test download.
609 * @param peer which peer to download from
610 * @param timeout if this operation cannot be completed within the
611 * given period, call the continuation with an error code
612 * @param anonymity option for download
613 * @param seed used for file validation
614 * @param uri URI of file to download (CHK/LOC only)
615 * @param verbose how verbose to be in reporting
616 * @param cont function to call when done
617 * @param cont_cls closure for cont
620 GNUNET_FS_TEST_download (struct GNUNET_TESTBED_Peer *peer,
621 struct GNUNET_TIME_Relative timeout,
622 uint32_t anonymity, uint32_t seed,
623 const struct GNUNET_FS_Uri *uri, unsigned int verbose,
624 GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls)
626 struct TestDownloadOperation *dop;
628 dop = GNUNET_new (struct TestDownloadOperation);
629 dop->uri = GNUNET_FS_uri_dup (uri);
630 dop->size = GNUNET_FS_uri_chk_get_file_size (uri);
631 dop->verbose = verbose;
632 dop->anonymity = anonymity;
633 dop->download_cont = cont;
634 dop->download_cont_cls = cont_cls;
635 dop->download_seed = seed;
637 dop->fs_op = GNUNET_TESTBED_service_connect (dop,
640 &download_fs_connect_complete_cb,
642 &download_connect_adapter,
643 &fs_disconnect_adapter,
645 dop->download_timeout_task =
646 GNUNET_SCHEDULER_add_delayed (timeout, &download_timeout, dop);
650 /* end of fs_test_lib.c */