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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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 struct GNUNET_SCHEDULER_Task * 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_TaskCallback 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 struct GNUNET_SCHEDULER_Task * 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)
188 struct TestPublishOperation *po = cls;
190 GNUNET_FS_publish_stop (po->publish_context);
191 GNUNET_TESTBED_operation_done (po->fs_op);
192 po->publish_cont (po->publish_cont_cls,
194 (GNUNET_YES == po->do_index)
195 ? po->publish_tmp_file
197 GNUNET_FS_uri_destroy (po->publish_uri);
198 if ( (GNUNET_YES != po->do_index) &&
199 (NULL != po->publish_tmp_file) )
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
212 publish_timeout (void *cls)
214 struct TestPublishOperation *po = cls;
216 po->publish_timeout_task = NULL;
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 "Timeout while trying to publish data\n");
219 GNUNET_TESTBED_operation_done (po->fs_op);
220 GNUNET_FS_publish_stop (po->publish_context);
221 po->publish_cont (po->publish_cont_cls, NULL, NULL);
222 (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
223 GNUNET_free_non_null (po->publish_tmp_file);
229 * Progress callback for file-sharing events while publishing.
231 * @param cls the publish operation context
232 * @param info information about the event
235 publish_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
237 struct TestPublishOperation *po = cls;
239 switch (info->status)
241 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
242 GNUNET_SCHEDULER_cancel (po->publish_timeout_task);
243 po->publish_timeout_task = NULL;
245 GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
246 GNUNET_SCHEDULER_add_now (&report_uri,
249 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
251 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Publishing at %llu/%llu bytes\n",
252 (unsigned long long) info->value.publish.completed,
253 (unsigned long long) info->value.publish.size);
255 case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
257 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
259 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
260 (unsigned long long) info->value.download.completed,
261 (unsigned long long) info->value.download.size);
271 * Generate test data for publishing test.
273 * @param cls pointer to uint32_t with publishing seed
274 * @param offset offset to generate data for
275 * @param max maximum number of bytes to generate
276 * @param buf where to write generated data
277 * @param emsg where to store error message (unused)
278 * @return number of bytes written to buf
281 file_generator (void *cls,
287 uint32_t *publish_seed = cls;
296 for (pos = 0; pos < 8; pos++)
297 cbuf[pos] = (uint8_t) (offset >> pos * 8);
298 for (pos = 8; pos < max; pos++)
300 mod = (255 - (offset / 1024 / 32));
303 cbuf[pos] = (uint8_t) ((offset * (*publish_seed)) % mod);
310 * Connect adapter for publishing operation.
312 * @param cls the 'struct TestPublishOperation'
313 * @param cfg configuration of the peer to connect to; will be available until
314 * GNUNET_TESTBED_operation_done() is called on the operation returned
315 * from GNUNET_TESTBED_service_connect()
316 * @return service handle to return in 'op_result', NULL on error
319 publish_connect_adapter (void *cls,
320 const struct GNUNET_CONFIGURATION_Handle *cfg)
322 struct TestPublishOperation *po = cls;
324 return GNUNET_FS_start (cfg,
326 &publish_progress_cb, po,
327 GNUNET_FS_FLAGS_NONE,
328 GNUNET_FS_OPTIONS_END);
333 * Adapter function called to destroy connection to file-sharing service.
335 * @param cls the 'struct GNUNET_FS_Handle'
336 * @param op_result unused (different for publish/download!)
339 fs_disconnect_adapter (void *cls,
342 struct GNUNET_FS_Handle *fs = op_result;
349 * Callback to be called when testbed has connected to the fs service
351 * @param cls the 'struct TestPublishOperation'
352 * @param op the operation that has been finished
353 * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
354 * @param emsg error message in case the operation has failed; will be NULL if
355 * operation has executed successfully.
358 publish_fs_connect_complete_cb (void *cls,
359 struct GNUNET_TESTBED_Operation *op,
363 struct TestPublishOperation *po = cls;
364 struct GNUNET_FS_FileInformation *fi;
365 struct GNUNET_DISK_FileHandle *fh;
368 char buf[DBLOCK_SIZE];
370 struct GNUNET_FS_BlockOptions bo;
372 if (NULL == ca_result)
374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect to FS for publishing: %s\n", emsg);
375 po->publish_cont (po->publish_cont_cls,
377 GNUNET_TESTBED_operation_done (po->fs_op);
383 bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
384 bo.anonymity_level = po->anonymity;
385 bo.content_priority = 42;
386 bo.replication_level = 1;
387 if (GNUNET_YES == po->do_index)
389 po->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
390 GNUNET_assert (po->publish_tmp_file != NULL);
391 fh = GNUNET_DISK_file_open (po->publish_tmp_file,
392 GNUNET_DISK_OPEN_WRITE |
393 GNUNET_DISK_OPEN_CREATE,
394 GNUNET_DISK_PERM_USER_READ |
395 GNUNET_DISK_PERM_USER_WRITE);
396 GNUNET_assert (NULL != fh);
398 while (off < po->size)
400 bsize = GNUNET_MIN (sizeof (buf), po->size - off);
402 GNUNET_assert (bsize == file_generator (&po->publish_seed, off, bsize, buf, &em));
403 GNUNET_assert (em == NULL);
404 GNUNET_assert (bsize == GNUNET_DISK_file_write (fh, buf, bsize));
407 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
408 fi = GNUNET_FS_file_information_create_from_file (po->fs, po,
409 po->publish_tmp_file,
410 NULL, NULL, po->do_index,
412 GNUNET_assert (NULL != fi);
416 fi = GNUNET_FS_file_information_create_from_reader (po->fs, po,
418 &file_generator, &po->publish_seed,
421 GNUNET_assert (NULL != fi);
423 po->publish_context =
424 GNUNET_FS_publish_start (po->fs, fi, NULL, NULL, NULL,
425 GNUNET_FS_PUBLISH_OPTION_NONE);
430 * Publish a file at the given peer.
432 * @param peer where to publish
433 * @param timeout if this operation cannot be completed within the
434 * given period, call the continuation with an error code
435 * @param anonymity option for publication
436 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
437 * GNUNET_SYSERR for simulation
438 * @param size size of the file to publish
439 * @param seed seed to use for file generation
440 * @param verbose how verbose to be in reporting
441 * @param cont function to call when done
442 * @param cont_cls closure for cont
445 GNUNET_FS_TEST_publish (struct GNUNET_TESTBED_Peer *peer,
446 struct GNUNET_TIME_Relative timeout, uint32_t anonymity,
447 int do_index, uint64_t size, uint32_t seed,
448 unsigned int verbose,
449 GNUNET_FS_TEST_UriContinuation cont, void *cont_cls)
451 struct TestPublishOperation *po;
453 po = GNUNET_new (struct TestPublishOperation);
454 po->publish_cont = cont;
455 po->publish_cont_cls = cont_cls;
456 po->publish_seed = seed;
457 po->anonymity = anonymity;
459 po->verbose = verbose;
460 po->do_index = do_index;
461 po->fs_op = GNUNET_TESTBED_service_connect (po,
464 &publish_fs_connect_complete_cb,
466 &publish_connect_adapter,
467 &fs_disconnect_adapter,
469 po->publish_timeout_task =
470 GNUNET_SCHEDULER_add_delayed (timeout, &publish_timeout, po);
474 /* ************************** download ************************ */
478 * Task scheduled to run when download operation times out.
480 * @param cls the download operation context
483 download_timeout (void *cls)
485 struct TestDownloadOperation *dop = cls;
487 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
488 "Timeout while trying to download file\n");
489 dop->download_timeout_task = NULL;
490 GNUNET_FS_download_stop (dop->download_context,
492 GNUNET_SCHEDULER_add_now (dop->download_cont,
493 dop->download_cont_cls);
494 GNUNET_TESTBED_operation_done (dop->fs_op);
495 GNUNET_FS_uri_destroy (dop->uri);
501 * Task scheduled to report on the completion of our download operation.
503 * @param cls the download operation context
506 report_success (void *cls)
508 struct TestDownloadOperation *dop = cls;
510 GNUNET_FS_download_stop (dop->download_context,
512 GNUNET_SCHEDULER_add_now (dop->download_cont,
513 dop->download_cont_cls);
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,
528 const struct GNUNET_FS_ProgressInfo *info)
530 struct TestDownloadOperation *dop = cls;
532 switch (info->status)
534 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
536 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
537 "Download at %llu/%llu bytes\n",
538 (unsigned long long) info->value.download.completed,
539 (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);
546 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
547 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
549 /* FIXME: monitor data correctness during download progress */
550 /* FIXME: do performance reports given sufficient verbosity */
551 /* FIXME: advance timeout task to "immediate" on error */
560 * Connect adapter for download operation.
562 * @param cls the 'struct TestDownloadOperation'
563 * @param cfg configuration of the peer to connect to; will be available until
564 * GNUNET_TESTBED_operation_done() is called on the operation returned
565 * from GNUNET_TESTBED_service_connect()
566 * @return service handle to return in 'op_result', NULL on error
569 download_connect_adapter (void *cls,
570 const struct GNUNET_CONFIGURATION_Handle *cfg)
572 struct TestPublishOperation *po = cls;
574 return GNUNET_FS_start (cfg,
576 &download_progress_cb, po,
577 GNUNET_FS_FLAGS_NONE,
578 GNUNET_FS_OPTIONS_END);
583 * Callback to be called when testbed has connected to the fs service
585 * @param cls the 'struct TestPublishOperation'
586 * @param op the operation that has been finished
587 * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
588 * @param emsg error message in case the operation has failed; will be NULL if
589 * operation has executed successfully.
592 download_fs_connect_complete_cb (void *cls,
593 struct GNUNET_TESTBED_Operation *op,
597 struct TestDownloadOperation *dop = cls;
600 GNUNET_assert (NULL != dop->fs);
601 dop->download_context =
602 GNUNET_FS_download_start (dop->fs, dop->uri, NULL, NULL, NULL, 0, dop->size,
603 dop->anonymity, GNUNET_FS_DOWNLOAD_OPTION_NONE,
609 * Perform test download.
611 * @param peer which peer to download from
612 * @param timeout if this operation cannot be completed within the
613 * given period, call the continuation with an error code
614 * @param anonymity option for download
615 * @param seed used for file validation
616 * @param uri URI of file to download (CHK/LOC only)
617 * @param verbose how verbose to be in reporting
618 * @param cont function to call when done
619 * @param cont_cls closure for cont
622 GNUNET_FS_TEST_download (struct GNUNET_TESTBED_Peer *peer,
623 struct GNUNET_TIME_Relative timeout,
624 uint32_t anonymity, uint32_t seed,
625 const struct GNUNET_FS_Uri *uri, unsigned int verbose,
626 GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls)
628 struct TestDownloadOperation *dop;
630 dop = GNUNET_new (struct TestDownloadOperation);
631 dop->uri = GNUNET_FS_uri_dup (uri);
632 dop->size = GNUNET_FS_uri_chk_get_file_size (uri);
633 dop->verbose = verbose;
634 dop->anonymity = anonymity;
635 dop->download_cont = cont;
636 dop->download_cont_cls = cont_cls;
637 dop->download_seed = seed;
639 dop->fs_op = GNUNET_TESTBED_service_connect (dop,
642 &download_fs_connect_complete_cb,
644 &download_connect_adapter,
645 &fs_disconnect_adapter,
647 dop->download_timeout_task =
648 GNUNET_SCHEDULER_add_delayed (timeout, &download_timeout, dop);
652 /* end of fs_test_lib.c */