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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 {
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 {
113 * Handle for the operation to connect to the peer's 'fs' service.
115 struct GNUNET_TESTBED_Operation *fs_op;
118 * Handle to the file sharing context using this daemon.
120 struct GNUNET_FS_Handle *fs;
123 * Handle to the daemon via testing.
125 struct GNUNET_TESTING_Daemon *daemon;
128 * Function to call when download is done.
130 GNUNET_SCHEDULER_TaskCallback download_cont;
133 * Closure for download_cont.
135 void *download_cont_cls;
140 struct GNUNET_FS_Uri *uri;
143 * Task to abort downloading (timeout).
145 struct GNUNET_SCHEDULER_Task * download_timeout_task;
148 * Context for current download operation.
150 struct GNUNET_FS_DownloadContext *download_context;
158 * Anonymity level used.
163 * Seed for download verification.
165 uint32_t download_seed;
168 * Verbosity level of the current operation.
170 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,
245 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
247 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Publishing at %llu/%llu bytes\n",
248 (unsigned long long)info->value.publish.completed,
249 (unsigned long long)info->value.publish.size);
252 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);
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,
411 GNUNET_assert(NULL != fi);
415 fi = GNUNET_FS_file_information_create_from_reader(po->fs, po,
417 &file_generator, &po->publish_seed,
420 GNUNET_assert(NULL != fi);
422 po->publish_context =
423 GNUNET_FS_publish_start(po->fs, fi, NULL, NULL, NULL,
424 GNUNET_FS_PUBLISH_OPTION_NONE);
429 * Publish a file at the given peer.
431 * @param peer where to publish
432 * @param timeout if this operation cannot be completed within the
433 * given period, call the continuation with an error code
434 * @param anonymity option for publication
435 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
436 * GNUNET_SYSERR for simulation
437 * @param size size of the file to publish
438 * @param seed seed to use for file generation
439 * @param verbose how verbose to be in reporting
440 * @param cont function to call when done
441 * @param cont_cls closure for cont
444 GNUNET_FS_TEST_publish(struct GNUNET_TESTBED_Peer *peer,
445 struct GNUNET_TIME_Relative timeout, uint32_t anonymity,
446 int do_index, uint64_t size, uint32_t seed,
447 unsigned int verbose,
448 GNUNET_FS_TEST_UriContinuation cont, void *cont_cls)
450 struct TestPublishOperation *po;
452 po = GNUNET_new(struct TestPublishOperation);
453 po->publish_cont = cont;
454 po->publish_cont_cls = cont_cls;
455 po->publish_seed = seed;
456 po->anonymity = anonymity;
458 po->verbose = verbose;
459 po->do_index = do_index;
460 po->fs_op = GNUNET_TESTBED_service_connect(po,
463 &publish_fs_connect_complete_cb,
465 &publish_connect_adapter,
466 &fs_disconnect_adapter,
468 po->publish_timeout_task =
469 GNUNET_SCHEDULER_add_delayed(timeout, &publish_timeout, po);
473 /* ************************** download ************************ */
477 * Task scheduled to run when download operation times out.
479 * @param cls the download operation context
482 download_timeout(void *cls)
484 struct TestDownloadOperation *dop = cls;
486 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
487 "Timeout while trying to download file\n");
488 dop->download_timeout_task = NULL;
489 GNUNET_FS_download_stop(dop->download_context,
491 GNUNET_SCHEDULER_add_now(dop->download_cont,
492 dop->download_cont_cls);
493 GNUNET_TESTBED_operation_done(dop->fs_op);
494 GNUNET_FS_uri_destroy(dop->uri);
500 * Task scheduled to report on the completion of our download operation.
502 * @param cls the download operation context
505 report_success(void *cls)
507 struct TestDownloadOperation *dop = cls;
509 GNUNET_FS_download_stop(dop->download_context,
511 GNUNET_SCHEDULER_add_now(dop->download_cont,
512 dop->download_cont_cls);
513 GNUNET_TESTBED_operation_done(dop->fs_op);
514 GNUNET_FS_uri_destroy(dop->uri);
520 * Progress callback for file-sharing events while downloading.
522 * @param cls the download operation context
523 * @param info information about the event
526 download_progress_cb(void *cls,
527 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,
536 "Download at %llu/%llu bytes\n",
537 (unsigned long long)info->value.download.completed,
538 (unsigned long long)info->value.download.size);
541 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
542 GNUNET_SCHEDULER_cancel(dop->download_timeout_task);
543 dop->download_timeout_task = NULL;
544 GNUNET_SCHEDULER_add_now(&report_success, dop);
547 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
548 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
551 /* FIXME: monitor data correctness during download progress */
552 /* FIXME: do performance reports given sufficient verbosity */
553 /* FIXME: advance timeout task to "immediate" on error */
562 * Connect adapter for download operation.
564 * @param cls the 'struct TestDownloadOperation'
565 * @param cfg configuration of the peer to connect to; will be available until
566 * GNUNET_TESTBED_operation_done() is called on the operation returned
567 * from GNUNET_TESTBED_service_connect()
568 * @return service handle to return in 'op_result', NULL on error
571 download_connect_adapter(void *cls,
572 const struct GNUNET_CONFIGURATION_Handle *cfg)
574 struct TestPublishOperation *po = cls;
576 return GNUNET_FS_start(cfg,
578 &download_progress_cb, po,
579 GNUNET_FS_FLAGS_NONE,
580 GNUNET_FS_OPTIONS_END);
585 * Callback to be called when testbed has connected to the fs service
587 * @param cls the 'struct TestPublishOperation'
588 * @param op the operation that has been finished
589 * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
590 * @param emsg error message in case the operation has failed; will be NULL if
591 * operation has executed successfully.
594 download_fs_connect_complete_cb(void *cls,
595 struct GNUNET_TESTBED_Operation *op,
599 struct TestDownloadOperation *dop = cls;
602 GNUNET_assert(NULL != dop->fs);
603 dop->download_context =
604 GNUNET_FS_download_start(dop->fs, dop->uri, NULL, NULL, NULL, 0, dop->size,
605 dop->anonymity, GNUNET_FS_DOWNLOAD_OPTION_NONE,
611 * Perform test download.
613 * @param peer which peer to download from
614 * @param timeout if this operation cannot be completed within the
615 * given period, call the continuation with an error code
616 * @param anonymity option for download
617 * @param seed used for file validation
618 * @param uri URI of file to download (CHK/LOC only)
619 * @param verbose how verbose to be in reporting
620 * @param cont function to call when done
621 * @param cont_cls closure for cont
624 GNUNET_FS_TEST_download(struct GNUNET_TESTBED_Peer *peer,
625 struct GNUNET_TIME_Relative timeout,
626 uint32_t anonymity, uint32_t seed,
627 const struct GNUNET_FS_Uri *uri, unsigned int verbose,
628 GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls)
630 struct TestDownloadOperation *dop;
632 dop = GNUNET_new(struct TestDownloadOperation);
633 dop->uri = GNUNET_FS_uri_dup(uri);
634 dop->size = GNUNET_FS_uri_chk_get_file_size(uri);
635 dop->verbose = verbose;
636 dop->anonymity = anonymity;
637 dop->download_cont = cont;
638 dop->download_cont_cls = cont_cls;
639 dop->download_seed = seed;
641 dop->fs_op = GNUNET_TESTBED_service_connect(dop,
644 &download_fs_connect_complete_cb,
646 &download_connect_adapter,
647 &fs_disconnect_adapter,
649 dop->download_timeout_task =
650 GNUNET_SCHEDULER_add_delayed(timeout, &download_timeout, dop);
654 /* end of fs_test_lib.c */