2 This file is part of GNUnet.
3 (C) 2010, 2011 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 * with multiple peers; 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"
32 #include "gnunet_testing_lib.h"
34 #define CONNECT_ATTEMPTS 4
36 #define CONTENT_LIFETIME GNUNET_TIME_UNIT_HOURS
39 * Handle for a daemon started for testing FS.
41 struct GNUNET_FS_TestDaemon
45 * Global configuration, only stored in first test daemon,
48 struct GNUNET_CONFIGURATION_Handle *gcfg;
51 * Handle to the file sharing context using this daemon.
53 struct GNUNET_FS_Handle *fs;
56 * Handle to the daemon via testing.
58 struct GNUNET_TESTING_Daemon *daemon;
61 * Note that 'group' will be the same value for all of the
62 * daemons started jointly.
64 struct GNUNET_TESTING_PeerGroup *group;
67 * Configuration for accessing this peer.
69 struct GNUNET_CONFIGURATION_Handle *cfg;
74 struct GNUNET_PeerIdentity id;
77 * Function to call when upload is done.
79 GNUNET_FS_TEST_UriContinuation publish_cont;
82 * Closure for publish_cont.
84 void *publish_cont_cls;
87 * Task to abort publishing (timeout).
89 GNUNET_SCHEDULER_TaskIdentifier publish_timeout_task;
92 * Seed for file generation.
94 uint32_t publish_seed;
97 * Context for current publishing operation.
99 struct GNUNET_FS_PublishContext *publish_context;
104 struct GNUNET_FS_Uri *publish_uri;
107 * Name of the temporary file used, or NULL for none.
109 char *publish_tmp_file;
112 * Function to call when download is done.
114 GNUNET_SCHEDULER_Task download_cont;
117 * Closure for download_cont.
119 void *download_cont_cls;
122 * Seed for download verification.
124 uint32_t download_seed;
127 * Task to abort downloading (timeout).
129 GNUNET_SCHEDULER_TaskIdentifier download_timeout_task;
132 * Context for current download operation.
134 struct GNUNET_FS_DownloadContext *download_context;
137 * Verbosity level of the current operation.
145 * Check whether peers successfully shut down.
148 shutdown_callback (void *cls, const char *emsg)
150 struct GNUNET_CONFIGURATION_Handle *gcfg = cls;
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Shutdown of peers failed: %s\n",
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
164 GNUNET_CONFIGURATION_destroy (gcfg);
169 report_uri (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
171 struct GNUNET_FS_TestDaemon *daemon = cls;
172 GNUNET_FS_TEST_UriContinuation cont;
173 struct GNUNET_FS_Uri *uri;
175 GNUNET_FS_publish_stop (daemon->publish_context);
176 daemon->publish_context = NULL;
177 cont = daemon->publish_cont;
178 daemon->publish_cont = NULL;
179 uri = daemon->publish_uri;
180 cont (daemon->publish_cont_cls, uri);
181 GNUNET_FS_uri_destroy (uri);
186 report_success (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
188 struct GNUNET_FS_TestDaemon *daemon = cls;
190 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
191 daemon->download_context = NULL;
192 GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
193 daemon->download_cont_cls,
194 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
195 daemon->download_cont = NULL;
200 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
202 struct GNUNET_FS_TestDaemon *daemon = cls;
204 switch (info->status)
206 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
207 GNUNET_SCHEDULER_cancel (daemon->publish_timeout_task);
208 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
209 daemon->publish_uri =
210 GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
211 GNUNET_SCHEDULER_add_continuation (&report_uri, daemon,
212 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
214 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
216 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Publishing at %llu/%llu bytes\n",
217 (unsigned long long) info->value.publish.completed,
218 (unsigned long long) info->value.publish.size);
220 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
222 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
223 (unsigned long long) info->value.download.completed,
224 (unsigned long long) info->value.download.size);
226 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
227 GNUNET_SCHEDULER_cancel (daemon->download_timeout_task);
228 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
229 GNUNET_SCHEDULER_add_continuation (&report_success, daemon,
230 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
232 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
233 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
235 /* FIXME: monitor data correctness during download progress */
236 /* FIXME: do performance reports given sufficient verbosity */
237 /* FIXME: advance timeout task to "immediate" on error */
247 struct GNUNET_TIME_Relative timeout;
250 struct GNUNET_FS_TestDaemon **daemons;
251 GNUNET_SCHEDULER_Task cont;
253 struct GNUNET_TESTING_PeerGroup *group;
254 struct GNUNET_CONFIGURATION_Handle *cfg;
255 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
260 notify_running (void *cls, const struct GNUNET_PeerIdentity *id,
261 const struct GNUNET_CONFIGURATION_Handle *cfg,
262 struct GNUNET_TESTING_Daemon *d, const char *emsg)
264 struct StartContext *sctx = cls;
269 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Failed to start daemon: %s\n"),
274 while (i < sctx->total)
276 if (GNUNET_TESTING_daemon_get (sctx->group, i) == d)
280 GNUNET_assert (i < sctx->total);
281 GNUNET_assert (sctx->have < sctx->total);
282 GNUNET_assert (sctx->daemons[i]->cfg == NULL);
283 sctx->daemons[i]->cfg = GNUNET_CONFIGURATION_dup (cfg);
284 sctx->daemons[i]->group = sctx->group;
285 sctx->daemons[i]->daemon = d;
286 sctx->daemons[i]->id = *id;
288 if (sctx->have == sctx->total)
290 GNUNET_SCHEDULER_add_continuation (sctx->cont, sctx->cont_cls,
291 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
292 sctx->daemons[0]->gcfg = sctx->cfg;
293 GNUNET_SCHEDULER_cancel (sctx->timeout_task);
294 for (i = 0; i < sctx->total; i++)
296 sctx->daemons[i]->fs =
297 GNUNET_FS_start (sctx->daemons[i]->cfg, "<tester>", &progress_cb,
298 sctx->daemons[i], GNUNET_FS_FLAGS_NONE,
299 GNUNET_FS_OPTIONS_END);
307 start_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
309 struct StartContext *sctx = cls;
312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
313 "Timeout while trying to start daemons\n");
314 GNUNET_TESTING_daemons_stop (sctx->group,
315 GNUNET_TIME_relative_multiply
316 (GNUNET_TIME_UNIT_SECONDS, 30),
317 &shutdown_callback, NULL);
318 for (i = 0; i < sctx->total; i++)
321 GNUNET_CONFIGURATION_destroy (sctx->daemons[i]->cfg);
322 GNUNET_free (sctx->daemons[i]);
323 sctx->daemons[i] = NULL;
325 GNUNET_CONFIGURATION_destroy (sctx->cfg);
326 GNUNET_SCHEDULER_add_continuation (sctx->cont, sctx->cont_cls,
327 GNUNET_SCHEDULER_REASON_TIMEOUT);
333 * Start daemons for testing.
335 * @param template_cfg_file configuration template to use
336 * @param timeout if this operation cannot be completed within the
337 * given period, call the continuation with an error code
338 * @param total number of daemons to start
339 * @param daemons array of 'total' entries to be initialized
340 * (array must already be allocated, will be filled)
341 * @param cont function to call when done
342 * @param cont_cls closure for cont
345 GNUNET_FS_TEST_daemons_start (const char *template_cfg_file,
346 struct GNUNET_TIME_Relative timeout,
348 struct GNUNET_FS_TestDaemon **daemons,
349 GNUNET_SCHEDULER_Task cont, void *cont_cls)
351 struct StartContext *sctx;
354 GNUNET_assert (total > 0);
355 sctx = GNUNET_malloc (sizeof (struct StartContext));
356 sctx->daemons = daemons;
359 sctx->cont_cls = cont_cls;
360 sctx->cfg = GNUNET_CONFIGURATION_create ();
361 if (GNUNET_OK != GNUNET_CONFIGURATION_load (sctx->cfg, template_cfg_file))
364 GNUNET_CONFIGURATION_destroy (sctx->cfg);
366 GNUNET_SCHEDULER_add_continuation (cont, cont_cls,
367 GNUNET_SCHEDULER_REASON_TIMEOUT);
370 for (i = 0; i < total; i++)
371 daemons[i] = GNUNET_malloc (sizeof (struct GNUNET_FS_TestDaemon));
372 sctx->group = GNUNET_TESTING_daemons_start (sctx->cfg, total, total, /* Outstanding connections */
373 total, /* Outstanding ssh connections */
375 ¬ify_running, sctx, NULL, NULL,
378 GNUNET_SCHEDULER_add_delayed (timeout, &start_timeout, sctx);
382 struct GNUNET_FS_TEST_ConnectContext
384 GNUNET_SCHEDULER_Task cont;
386 struct GNUNET_TESTING_ConnectContext *cc;
391 * Prototype of a function that will be called whenever
392 * two daemons are connected by the testing library.
395 * @param first peer id for first daemon
396 * @param second peer id for the second daemon
397 * @param distance distance between the connected peers
398 * @param first_cfg config for the first daemon
399 * @param second_cfg config for the second daemon
400 * @param first_daemon handle for the first daemon
401 * @param second_daemon handle for the second daemon
402 * @param emsg error message (NULL on success)
405 notify_connection (void *cls, const struct GNUNET_PeerIdentity *first,
406 const struct GNUNET_PeerIdentity *second, uint32_t distance,
407 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
408 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
409 struct GNUNET_TESTING_Daemon *first_daemon,
410 struct GNUNET_TESTING_Daemon *second_daemon,
413 struct GNUNET_FS_TEST_ConnectContext *cc = cls;
417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Failed to connect peers: %s\n",
419 GNUNET_SCHEDULER_add_continuation (cc->cont, cc->cont_cls,
421 NULL) ? GNUNET_SCHEDULER_REASON_TIMEOUT :
422 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
428 * Connect two daemons for testing.
430 * @param daemon1 first daemon to connect
431 * @param daemon2 second first daemon to connect
432 * @param timeout if this operation cannot be completed within the
433 * given period, call the continuation with an error code
434 * @param cont function to call when done
435 * @param cont_cls closure for cont
437 struct GNUNET_FS_TEST_ConnectContext *
438 GNUNET_FS_TEST_daemons_connect (struct GNUNET_FS_TestDaemon *daemon1,
439 struct GNUNET_FS_TestDaemon *daemon2,
440 struct GNUNET_TIME_Relative timeout,
441 GNUNET_SCHEDULER_Task cont, void *cont_cls)
443 struct GNUNET_FS_TEST_ConnectContext *ncc;
445 ncc = GNUNET_malloc (sizeof (struct GNUNET_FS_TEST_ConnectContext));
447 ncc->cont_cls = cont_cls;
448 ncc->cc = GNUNET_TESTING_daemons_connect (daemon1->daemon, daemon2->daemon, timeout,
449 CONNECT_ATTEMPTS, GNUNET_YES,
450 ¬ify_connection, ncc);
456 * Cancel connect operation.
458 * @param cc operation to cancel
461 GNUNET_FS_TEST_daemons_connect_cancel (struct GNUNET_FS_TEST_ConnectContext *cc)
463 GNUNET_TESTING_daemons_connect_cancel (cc->cc);
469 * Obtain peer configuration used for testing.
471 * @param daemons array with the daemons
472 * @param off which configuration to get
473 * @return peer configuration
475 const struct GNUNET_CONFIGURATION_Handle *
476 GNUNET_FS_TEST_get_configuration (struct GNUNET_FS_TestDaemon **daemons,
479 return daemons[off]->cfg;
483 * Obtain peer group used for testing.
485 * @param daemons array with the daemons (must contain at least one)
488 struct GNUNET_TESTING_PeerGroup *
489 GNUNET_FS_TEST_get_group (struct GNUNET_FS_TestDaemon **daemons)
491 return daemons[0]->group;
496 * Stop daemons used for testing.
498 * @param total number of daemons to stop
499 * @param daemons array with the daemons (values will be clobbered)
502 GNUNET_FS_TEST_daemons_stop (unsigned int total,
503 struct GNUNET_FS_TestDaemon **daemons)
506 struct GNUNET_TESTING_PeerGroup *pg;
507 struct GNUNET_CONFIGURATION_Handle *gcfg;
508 struct GNUNET_FS_TestDaemon *daemon;
510 GNUNET_assert (total > 0);
511 GNUNET_assert (daemons[0] != NULL);
512 pg = daemons[0]->group;
513 gcfg = daemons[0]->gcfg;
514 for (i = 0; i < total; i++)
517 if (daemon->download_timeout_task != GNUNET_SCHEDULER_NO_TASK)
519 GNUNET_SCHEDULER_cancel (daemon->download_timeout_task);
520 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
522 if (daemon->publish_timeout_task != GNUNET_SCHEDULER_NO_TASK)
524 GNUNET_SCHEDULER_cancel (daemon->publish_timeout_task);
525 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
527 if (NULL != daemon->download_context)
529 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
530 daemon->download_context = NULL;
532 if (daemon->fs != NULL)
533 GNUNET_FS_stop (daemon->fs);
534 if (daemon->cfg != NULL)
535 GNUNET_CONFIGURATION_destroy (daemon->cfg);
536 if (NULL != daemon->publish_tmp_file)
538 GNUNET_break (GNUNET_OK ==
539 GNUNET_DISK_directory_remove (daemon->publish_tmp_file));
540 GNUNET_free (daemon->publish_tmp_file);
541 daemon->publish_tmp_file = NULL;
543 GNUNET_free (daemon);
546 GNUNET_TESTING_daemons_stop (pg,
547 GNUNET_TIME_relative_multiply
548 (GNUNET_TIME_UNIT_SECONDS, 30),
549 &shutdown_callback, gcfg);
554 publish_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
556 struct GNUNET_FS_TestDaemon *daemon = cls;
557 GNUNET_FS_TEST_UriContinuation cont;
559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
560 "Timeout while trying to publish data\n");
561 cont = daemon->publish_cont;
562 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
563 daemon->publish_cont = NULL;
564 GNUNET_FS_publish_stop (daemon->publish_context);
565 daemon->publish_context = NULL;
566 cont (daemon->publish_cont_cls, NULL);
571 file_generator (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
573 struct GNUNET_FS_TestDaemon *daemon = cls;
582 for (pos = 0; pos < 8; pos++)
583 cbuf[pos] = (uint8_t) (offset >> pos * 8);
584 for (pos = 8; pos < max; pos++)
586 mod = (255 - (offset / 1024 / 32));
589 cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);
597 * Publish a file at the given daemon.
599 * @param daemon where to publish
600 * @param timeout if this operation cannot be completed within the
601 * given period, call the continuation with an error code
602 * @param anonymity option for publication
603 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
604 * GNUNET_SYSERR for simulation
605 * @param size size of the file to publish
606 * @param seed seed to use for file generation
607 * @param verbose how verbose to be in reporting
608 * @param cont function to call when done
609 * @param cont_cls closure for cont
612 GNUNET_FS_TEST_publish (struct GNUNET_FS_TestDaemon *daemon,
613 struct GNUNET_TIME_Relative timeout, uint32_t anonymity,
614 int do_index, uint64_t size, uint32_t seed,
615 unsigned int verbose,
616 GNUNET_FS_TEST_UriContinuation cont, void *cont_cls)
618 struct GNUNET_FS_FileInformation *fi;
619 struct GNUNET_DISK_FileHandle *fh;
622 char buf[DBLOCK_SIZE];
624 struct GNUNET_FS_BlockOptions bo;
626 GNUNET_assert (daemon->publish_cont == NULL);
627 daemon->publish_cont = cont;
628 daemon->publish_cont_cls = cont_cls;
629 daemon->publish_seed = seed;
630 daemon->verbose = verbose;
631 bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
632 bo.anonymity_level = anonymity;
633 bo.content_priority = 42;
634 bo.replication_level = 1;
635 if (GNUNET_YES == do_index)
637 GNUNET_assert (daemon->publish_tmp_file == NULL);
638 daemon->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
639 GNUNET_assert (daemon->publish_tmp_file != NULL);
640 fh = GNUNET_DISK_file_open (daemon->publish_tmp_file,
641 GNUNET_DISK_OPEN_WRITE |
642 GNUNET_DISK_OPEN_CREATE,
643 GNUNET_DISK_PERM_USER_READ |
644 GNUNET_DISK_PERM_USER_WRITE);
645 GNUNET_assert (NULL != fh);
649 bsize = GNUNET_MIN (sizeof (buf), size - off);
651 GNUNET_assert (bsize == file_generator (daemon, off, bsize, buf, &emsg));
652 GNUNET_assert (emsg == NULL);
653 GNUNET_assert (bsize == GNUNET_DISK_file_write (fh, buf, bsize));
656 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
657 fi = GNUNET_FS_file_information_create_from_file (daemon->fs, daemon,
658 daemon->publish_tmp_file,
659 NULL, NULL, do_index,
664 fi = GNUNET_FS_file_information_create_from_reader (daemon->fs, daemon,
665 size, &file_generator,
669 daemon->publish_context =
670 GNUNET_FS_publish_start (daemon->fs, fi, NULL, NULL, NULL,
671 GNUNET_FS_PUBLISH_OPTION_NONE);
672 daemon->publish_timeout_task =
673 GNUNET_SCHEDULER_add_delayed (timeout, &publish_timeout, daemon);
678 download_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
680 struct GNUNET_FS_TestDaemon *daemon = cls;
682 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
683 "Timeout while trying to download file\n");
684 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
685 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
686 daemon->download_context = NULL;
687 GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
688 daemon->download_cont_cls,
689 GNUNET_SCHEDULER_REASON_TIMEOUT);
690 daemon->download_cont = NULL;
695 * Perform test download.
697 * @param daemon which peer to download from
698 * @param timeout if this operation cannot be completed within the
699 * given period, call the continuation with an error code
700 * @param anonymity option for download
701 * @param seed used for file validation
702 * @param uri URI of file to download (CHK/LOC only)
703 * @param verbose how verbose to be in reporting
704 * @param cont function to call when done
705 * @param cont_cls closure for cont
708 GNUNET_FS_TEST_download (struct GNUNET_FS_TestDaemon *daemon,
709 struct GNUNET_TIME_Relative timeout,
710 uint32_t anonymity, uint32_t seed,
711 const struct GNUNET_FS_Uri *uri, unsigned int verbose,
712 GNUNET_SCHEDULER_Task cont, void *cont_cls)
716 GNUNET_assert (daemon->download_cont == NULL);
717 size = GNUNET_FS_uri_chk_get_file_size (uri);
718 daemon->verbose = verbose;
719 daemon->download_cont = cont;
720 daemon->download_cont_cls = cont_cls;
721 daemon->download_seed = seed;
722 daemon->download_context =
723 GNUNET_FS_download_start (daemon->fs, uri, NULL, NULL, NULL, 0, size,
724 anonymity, GNUNET_FS_DOWNLOAD_OPTION_NONE, NULL,
726 daemon->download_timeout_task =
727 GNUNET_SCHEDULER_add_delayed (timeout, &download_timeout, daemon);
730 /* end of test_fs_lib.c */