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,
151 struct GNUNET_CONFIGURATION_Handle *gcfg = cls;
155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
156 "Shutdown of peers failed: %s\n",
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163 "All peers successfully shut down!\n");
167 GNUNET_CONFIGURATION_destroy (gcfg);
172 report_uri (void *cls,
173 const struct GNUNET_SCHEDULER_TaskContext *tc)
175 struct GNUNET_FS_TestDaemon *daemon = cls;
176 GNUNET_FS_TEST_UriContinuation cont;
177 struct GNUNET_FS_Uri *uri;
179 GNUNET_FS_publish_stop (daemon->publish_context);
180 daemon->publish_context = NULL;
181 cont = daemon->publish_cont;
182 daemon->publish_cont = NULL;
183 uri = daemon->publish_uri;
184 cont (daemon->publish_cont_cls,
186 GNUNET_FS_uri_destroy (uri);
191 report_success (void *cls,
192 const struct GNUNET_SCHEDULER_TaskContext *tc)
194 struct GNUNET_FS_TestDaemon *daemon = cls;
196 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
197 daemon->download_context = NULL;
198 GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
199 daemon->download_cont_cls,
200 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
201 daemon->download_cont = NULL;
206 progress_cb (void *cls,
207 const struct GNUNET_FS_ProgressInfo *info)
209 struct GNUNET_FS_TestDaemon *daemon = cls;
211 switch (info->status)
213 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
214 GNUNET_SCHEDULER_cancel (daemon->publish_timeout_task);
215 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
216 daemon->publish_uri = GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
217 GNUNET_SCHEDULER_add_continuation (&report_uri,
219 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
221 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
223 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
224 "Publishing at %llu/%llu bytes\n",
225 (unsigned long long) info->value.publish.completed,
226 (unsigned long long) info->value.publish.size);
228 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
231 "Download at %llu/%llu bytes\n",
232 (unsigned long long) info->value.download.completed,
233 (unsigned long long) info->value.download.size);
235 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
236 GNUNET_SCHEDULER_cancel (daemon->download_timeout_task);
237 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
238 GNUNET_SCHEDULER_add_continuation (&report_success,
240 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
242 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
243 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
245 /* FIXME: monitor data correctness during download progress */
246 /* FIXME: do performance reports given sufficient verbosity */
247 /* FIXME: advance timeout task to "immediate" on error */
257 struct GNUNET_TIME_Relative timeout;
260 struct GNUNET_FS_TestDaemon **daemons;
261 GNUNET_SCHEDULER_Task cont;
263 struct GNUNET_TESTING_PeerGroup *group;
264 struct GNUNET_CONFIGURATION_Handle *cfg;
265 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
270 notify_running (void *cls,
271 const struct GNUNET_PeerIdentity *id,
272 const struct GNUNET_CONFIGURATION_Handle *cfg,
273 struct GNUNET_TESTING_Daemon *d,
276 struct StartContext *sctx = cls;
281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282 _("Failed to start daemon: %s\n"),
287 while (i < sctx->total)
289 if (GNUNET_TESTING_daemon_get (sctx->group,
294 GNUNET_assert (i < sctx->total);
295 GNUNET_assert (sctx->have < sctx->total);
296 GNUNET_assert (sctx->daemons[i]->cfg == NULL);
297 sctx->daemons[i]->cfg = GNUNET_CONFIGURATION_dup (cfg);
298 sctx->daemons[i]->group = sctx->group;
299 sctx->daemons[i]->daemon = d;
300 sctx->daemons[i]->id = *id;
302 if (sctx->have == sctx->total)
304 GNUNET_SCHEDULER_add_continuation (sctx->cont,
306 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
307 sctx->daemons[0]->gcfg = sctx->cfg;
308 GNUNET_SCHEDULER_cancel (sctx->timeout_task);
309 for (i=0;i<sctx->total;i++)
311 sctx->daemons[i]->fs = GNUNET_FS_start (sctx->daemons[i]->cfg,
315 GNUNET_FS_FLAGS_NONE,
316 GNUNET_FS_OPTIONS_END);
324 start_timeout (void *cls,
325 const struct GNUNET_SCHEDULER_TaskContext *tc)
327 struct StartContext *sctx = cls;
330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
331 "Timeout while trying to start daemons\n");
332 GNUNET_TESTING_daemons_stop (sctx->group,
333 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
336 for (i=0;i<sctx->total;i++)
339 GNUNET_CONFIGURATION_destroy (sctx->daemons[i]->cfg);
340 GNUNET_free (sctx->daemons[i]);
341 sctx->daemons[i] = NULL;
343 GNUNET_CONFIGURATION_destroy (sctx->cfg);
344 GNUNET_SCHEDULER_add_continuation (sctx->cont,
346 GNUNET_SCHEDULER_REASON_TIMEOUT);
352 * Start daemons for testing.
354 * @param template_cfg_file configuration template to use
355 * @param timeout if this operation cannot be completed within the
356 * given period, call the continuation with an error code
357 * @param total number of daemons to start
358 * @param daemons array of 'total' entries to be initialized
359 * (array must already be allocated, will be filled)
360 * @param cont function to call when done
361 * @param cont_cls closure for cont
364 GNUNET_FS_TEST_daemons_start (const char *template_cfg_file,
365 struct GNUNET_TIME_Relative timeout,
367 struct GNUNET_FS_TestDaemon **daemons,
368 GNUNET_SCHEDULER_Task cont,
371 struct StartContext *sctx;
374 GNUNET_assert (total > 0);
375 sctx = GNUNET_malloc (sizeof (struct StartContext));
376 sctx->daemons = daemons;
379 sctx->cont_cls = cont_cls;
380 sctx->cfg = GNUNET_CONFIGURATION_create ();
382 GNUNET_CONFIGURATION_load (sctx->cfg,
386 GNUNET_CONFIGURATION_destroy (sctx->cfg);
388 GNUNET_SCHEDULER_add_continuation (cont,
390 GNUNET_SCHEDULER_REASON_TIMEOUT);
393 for (i=0;i<total;i++)
394 daemons[i] = GNUNET_malloc (sizeof (struct GNUNET_FS_TestDaemon));
395 sctx->group = GNUNET_TESTING_daemons_start (sctx->cfg,
397 total, /* Outstanding connections */
398 total, /* Outstanding ssh connections */
406 sctx->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
412 struct ConnectContext
414 GNUNET_SCHEDULER_Task cont;
420 * Prototype of a function that will be called whenever
421 * two daemons are connected by the testing library.
424 * @param first peer id for first daemon
425 * @param second peer id for the second daemon
426 * @param distance distance between the connected peers
427 * @param first_cfg config for the first daemon
428 * @param second_cfg config for the second daemon
429 * @param first_daemon handle for the first daemon
430 * @param second_daemon handle for the second daemon
431 * @param emsg error message (NULL on success)
434 notify_connection (void *cls,
435 const struct GNUNET_PeerIdentity *first,
436 const struct GNUNET_PeerIdentity *second,
438 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
439 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
440 struct GNUNET_TESTING_Daemon *first_daemon,
441 struct GNUNET_TESTING_Daemon *second_daemon,
444 struct ConnectContext *cc = cls;
447 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
448 "Failed to connect peers: %s\n",
450 GNUNET_SCHEDULER_add_continuation (cc->cont,
453 ? GNUNET_SCHEDULER_REASON_TIMEOUT
454 : GNUNET_SCHEDULER_REASON_PREREQ_DONE);
460 * Connect two daemons for testing.
462 * @param daemon1 first daemon to connect
463 * @param daemon2 second first daemon to connect
464 * @param timeout if this operation cannot be completed within the
465 * given period, call the continuation with an error code
466 * @param cont function to call when done
467 * @param cont_cls closure for cont
470 GNUNET_FS_TEST_daemons_connect (struct GNUNET_FS_TestDaemon *daemon1,
471 struct GNUNET_FS_TestDaemon *daemon2,
472 struct GNUNET_TIME_Relative timeout,
473 GNUNET_SCHEDULER_Task cont,
476 struct ConnectContext *ncc;
478 ncc = GNUNET_malloc (sizeof (struct ConnectContext));
480 ncc->cont_cls = cont_cls;
481 GNUNET_TESTING_daemons_connect (daemon1->daemon,
492 * Obtain peer configuration used for testing.
494 * @param daemons array with the daemons
495 * @param off which configuration to get
496 * @return peer configuration
498 const struct GNUNET_CONFIGURATION_Handle *
499 GNUNET_FS_TEST_get_configuration (struct GNUNET_FS_TestDaemon **daemons,
502 return daemons[off]->cfg;
506 * Obtain peer group used for testing.
508 * @param daemons array with the daemons (must contain at least one)
511 struct GNUNET_TESTING_PeerGroup *
512 GNUNET_FS_TEST_get_group (struct GNUNET_FS_TestDaemon **daemons)
514 return daemons[0]->group;
519 * Stop daemons used for testing.
521 * @param total number of daemons to stop
522 * @param daemons array with the daemons (values will be clobbered)
525 GNUNET_FS_TEST_daemons_stop (unsigned int total,
526 struct GNUNET_FS_TestDaemon **daemons)
529 struct GNUNET_TESTING_PeerGroup *pg;
530 struct GNUNET_CONFIGURATION_Handle *gcfg;
531 struct GNUNET_FS_TestDaemon *daemon;
533 GNUNET_assert (total > 0);
534 GNUNET_assert (daemons[0] != NULL);
535 pg = daemons[0]->group;
536 gcfg = daemons[0]->gcfg;
537 for (i=0;i<total;i++)
540 if (daemon->download_timeout_task != GNUNET_SCHEDULER_NO_TASK)
542 GNUNET_SCHEDULER_cancel (daemon->download_timeout_task);
543 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
545 if (daemon->publish_timeout_task != GNUNET_SCHEDULER_NO_TASK)
547 GNUNET_SCHEDULER_cancel (daemon->publish_timeout_task);
548 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
550 if (NULL != daemon->download_context)
552 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
553 daemon->download_context = NULL;
555 if (daemon->fs != NULL)
556 GNUNET_FS_stop (daemon->fs);
557 if (daemon->cfg != NULL)
558 GNUNET_CONFIGURATION_destroy (daemon->cfg);
559 if (NULL != daemon->publish_tmp_file)
561 GNUNET_break (GNUNET_OK ==
562 GNUNET_DISK_directory_remove (daemon->publish_tmp_file));
563 GNUNET_free (daemon->publish_tmp_file);
564 daemon->publish_tmp_file = NULL;
566 GNUNET_free (daemon);
569 GNUNET_TESTING_daemons_stop (pg,
570 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
577 publish_timeout (void *cls,
578 const struct GNUNET_SCHEDULER_TaskContext *tc)
580 struct GNUNET_FS_TestDaemon *daemon = cls;
581 GNUNET_FS_TEST_UriContinuation cont;
583 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
584 "Timeout while trying to publish data\n");
585 cont = daemon->publish_cont;
586 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
587 daemon->publish_cont = NULL;
588 GNUNET_FS_publish_stop (daemon->publish_context);
589 daemon->publish_context = NULL;
590 cont (daemon->publish_cont_cls,
596 file_generator (void *cls,
602 struct GNUNET_FS_TestDaemon *daemon = cls;
611 for (pos=0;pos<8;pos++)
612 cbuf[pos] = (uint8_t) (offset >> pos*8);
613 for (pos=8;pos<max;pos++)
615 mod = (255 - (offset / 1024 / 32));
618 cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);
626 * Publish a file at the given daemon.
628 * @param daemon where to publish
629 * @param timeout if this operation cannot be completed within the
630 * given period, call the continuation with an error code
631 * @param anonymity option for publication
632 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
633 * GNUNET_SYSERR for simulation
634 * @param size size of the file to publish
635 * @param seed seed to use for file generation
636 * @param verbose how verbose to be in reporting
637 * @param cont function to call when done
638 * @param cont_cls closure for cont
641 GNUNET_FS_TEST_publish (struct GNUNET_FS_TestDaemon *daemon,
642 struct GNUNET_TIME_Relative timeout,
647 unsigned int verbose,
648 GNUNET_FS_TEST_UriContinuation cont,
651 struct GNUNET_FS_FileInformation *fi;
652 struct GNUNET_DISK_FileHandle *fh;
655 char buf[DBLOCK_SIZE];
657 struct GNUNET_FS_BlockOptions bo;
659 GNUNET_assert (daemon->publish_cont == NULL);
660 daemon->publish_cont = cont;
661 daemon->publish_cont_cls = cont_cls;
662 daemon->publish_seed = seed;
663 daemon->verbose = verbose;
664 bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
665 bo.anonymity_level = anonymity;
666 bo.content_priority = 42;
667 bo.replication_level = 1;
668 if (GNUNET_YES == do_index)
670 GNUNET_assert (daemon->publish_tmp_file == NULL);
671 daemon->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
672 GNUNET_assert (daemon->publish_tmp_file != NULL);
673 fh = GNUNET_DISK_file_open (daemon->publish_tmp_file,
674 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
675 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
676 GNUNET_assert (NULL != fh);
680 bsize = GNUNET_MIN (sizeof (buf),
683 GNUNET_assert (bsize ==
684 file_generator (daemon,
689 GNUNET_assert (emsg == NULL);
690 GNUNET_assert (bsize ==
691 GNUNET_DISK_file_write (fh,
696 GNUNET_assert (GNUNET_OK ==
697 GNUNET_DISK_file_close (fh));
698 fi = GNUNET_FS_file_information_create_from_file (daemon->fs,
700 daemon->publish_tmp_file,
707 fi = GNUNET_FS_file_information_create_from_reader (daemon->fs,
717 daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
720 GNUNET_FS_PUBLISH_OPTION_NONE);
721 daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
728 download_timeout (void *cls,
729 const struct GNUNET_SCHEDULER_TaskContext *tc)
731 struct GNUNET_FS_TestDaemon *daemon = cls;
733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
734 "Timeout while trying to download file\n");
735 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
736 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
737 daemon->download_context = NULL;
738 GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
739 daemon->download_cont_cls,
740 GNUNET_SCHEDULER_REASON_TIMEOUT);
741 daemon->download_cont = NULL;
746 * Perform test download.
748 * @param daemon which peer to download from
749 * @param timeout if this operation cannot be completed within the
750 * given period, call the continuation with an error code
751 * @param anonymity option for download
752 * @param seed used for file validation
753 * @param uri URI of file to download (CHK/LOC only)
754 * @param verbose how verbose to be in reporting
755 * @param cont function to call when done
756 * @param cont_cls closure for cont
759 GNUNET_FS_TEST_download (struct GNUNET_FS_TestDaemon *daemon,
760 struct GNUNET_TIME_Relative timeout,
763 const struct GNUNET_FS_Uri *uri,
764 unsigned int verbose,
765 GNUNET_SCHEDULER_Task cont,
770 GNUNET_assert (daemon->download_cont == NULL);
771 size = GNUNET_FS_uri_chk_get_file_size (uri);
772 daemon->verbose = verbose;
773 daemon->download_cont = cont;
774 daemon->download_cont_cls = cont_cls;
775 daemon->download_seed = seed;
776 daemon->download_context = GNUNET_FS_download_start (daemon->fs,
783 GNUNET_FS_DOWNLOAD_OPTION_NONE,
786 daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
791 /* end of test_fs_lib.c */