2 This file is part of GNUnet.
3 (C) 2010 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
37 * Handle for a daemon started for testing FS.
39 struct GNUNET_FS_TestDaemon
43 * Global configuration, only stored in first test daemon,
46 struct GNUNET_CONFIGURATION_Handle *gcfg;
49 * Handle to the file sharing context using this daemon.
51 struct GNUNET_FS_Handle *fs;
54 * Handle to the daemon via testing.
56 struct GNUNET_TESTING_Daemon *daemon;
59 * Note that 'group' will be the same value for all of the
60 * daemons started jointly.
62 struct GNUNET_TESTING_PeerGroup *group;
65 * Configuration for accessing this peer.
67 struct GNUNET_CONFIGURATION_Handle *cfg;
72 struct GNUNET_PeerIdentity id;
75 * Function to call when upload is done.
77 GNUNET_FS_TEST_UriContinuation publish_cont;
80 * Closure for publish_cont.
82 void *publish_cont_cls;
85 * Task to abort publishing (timeout).
87 GNUNET_SCHEDULER_TaskIdentifier publish_timeout_task;
90 * Seed for file generation.
92 uint32_t publish_seed;
95 * Context for current publishing operation.
97 struct GNUNET_FS_PublishContext *publish_context;
102 struct GNUNET_FS_Uri *publish_uri;
105 * Name of the temporary file used, or NULL for none.
107 char *publish_tmp_file;
110 * Function to call when download is done.
112 GNUNET_SCHEDULER_Task download_cont;
115 * Closure for download_cont.
117 void *download_cont_cls;
120 * Seed for download verification.
122 uint32_t download_seed;
125 * Task to abort downloading (timeout).
127 GNUNET_SCHEDULER_TaskIdentifier download_timeout_task;
130 * Context for current download operation.
132 struct GNUNET_FS_DownloadContext *download_context;
135 * Verbosity level of the current operation.
143 * Check whether peers successfully shut down.
146 shutdown_callback (void *cls,
149 struct GNUNET_CONFIGURATION_Handle *gcfg = cls;
153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
154 "Shutdown of peers failed: %s\n",
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "All peers successfully shut down!\n");
165 GNUNET_CONFIGURATION_destroy (gcfg);
170 report_uri (void *cls,
171 const struct GNUNET_SCHEDULER_TaskContext *tc)
173 struct GNUNET_FS_TestDaemon *daemon = cls;
174 GNUNET_FS_TEST_UriContinuation cont;
175 struct GNUNET_FS_Uri *uri;
177 GNUNET_FS_publish_stop (daemon->publish_context);
178 daemon->publish_context = NULL;
179 cont = daemon->publish_cont;
180 daemon->publish_cont = NULL;
181 uri = daemon->publish_uri;
182 cont (daemon->publish_cont_cls,
184 GNUNET_FS_uri_destroy (uri);
189 report_success (void *cls,
190 const struct GNUNET_SCHEDULER_TaskContext *tc)
192 struct GNUNET_FS_TestDaemon *daemon = cls;
194 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
195 daemon->download_context = NULL;
196 GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
197 daemon->download_cont_cls,
198 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
199 daemon->download_cont = NULL;
204 progress_cb (void *cls,
205 const struct GNUNET_FS_ProgressInfo *info)
207 struct GNUNET_FS_TestDaemon *daemon = cls;
209 switch (info->status)
211 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
212 GNUNET_SCHEDULER_cancel (daemon->publish_timeout_task);
213 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
214 daemon->publish_uri = GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
215 GNUNET_SCHEDULER_add_continuation (&report_uri,
217 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
219 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
221 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
222 "Publishing at %llu/%llu bytes\n",
223 (unsigned long long) info->value.publish.completed,
224 (unsigned long long) info->value.publish.size);
226 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
228 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
229 "Download at %llu/%llu bytes\n",
230 (unsigned long long) info->value.download.completed,
231 (unsigned long long) info->value.download.size);
233 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
234 GNUNET_SCHEDULER_cancel (daemon->download_timeout_task);
235 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
236 GNUNET_SCHEDULER_add_continuation (&report_success,
238 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
240 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
241 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
243 /* FIXME: monitor data correctness during download progress */
244 /* FIXME: do performance reports given sufficient verbosity */
245 /* FIXME: advance timeout task to "immediate" on error */
255 struct GNUNET_TIME_Relative timeout;
258 struct GNUNET_FS_TestDaemon **daemons;
259 GNUNET_SCHEDULER_Task cont;
261 struct GNUNET_TESTING_PeerGroup *group;
262 struct GNUNET_CONFIGURATION_Handle *cfg;
263 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
268 notify_running (void *cls,
269 const struct GNUNET_PeerIdentity *id,
270 const struct GNUNET_CONFIGURATION_Handle *cfg,
271 struct GNUNET_TESTING_Daemon *d,
274 struct StartContext *sctx = cls;
279 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
280 _("Failed to start daemon: %s\n"),
285 while (i < sctx->total)
287 if (GNUNET_TESTING_daemon_get (sctx->group,
292 GNUNET_assert (i < sctx->total);
293 GNUNET_assert (sctx->have < sctx->total);
294 GNUNET_assert (sctx->daemons[i]->cfg == NULL);
295 sctx->daemons[i]->cfg = GNUNET_CONFIGURATION_dup (cfg);
296 sctx->daemons[i]->group = sctx->group;
297 sctx->daemons[i]->daemon = d;
298 sctx->daemons[i]->id = *id;
300 if (sctx->have == sctx->total)
302 GNUNET_SCHEDULER_add_continuation (sctx->cont,
304 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
305 sctx->daemons[0]->gcfg = sctx->cfg;
306 GNUNET_SCHEDULER_cancel (sctx->timeout_task);
307 for (i=0;i<sctx->total;i++)
309 sctx->daemons[i]->fs = GNUNET_FS_start (sctx->daemons[i]->cfg,
313 GNUNET_FS_FLAGS_NONE,
314 GNUNET_FS_OPTIONS_END);
322 start_timeout (void *cls,
323 const struct GNUNET_SCHEDULER_TaskContext *tc)
325 struct StartContext *sctx = cls;
328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
329 "Timeout while trying to start daemons\n");
330 GNUNET_TESTING_daemons_stop (sctx->group,
331 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
334 for (i=0;i<sctx->total;i++)
337 GNUNET_CONFIGURATION_destroy (sctx->daemons[i]->cfg);
338 GNUNET_free (sctx->daemons[i]);
339 sctx->daemons[i] = NULL;
341 GNUNET_CONFIGURATION_destroy (sctx->cfg);
342 GNUNET_SCHEDULER_add_continuation (sctx->cont,
344 GNUNET_SCHEDULER_REASON_TIMEOUT);
350 * Start daemons for testing.
352 * @param template_cfg_file configuration template to use
353 * @param timeout if this operation cannot be completed within the
354 * given period, call the continuation with an error code
355 * @param total number of daemons to start
356 * @param daemons array of 'total' entries to be initialized
357 * (array must already be allocated, will be filled)
358 * @param cont function to call when done
359 * @param cont_cls closure for cont
362 GNUNET_FS_TEST_daemons_start (const char *template_cfg_file,
363 struct GNUNET_TIME_Relative timeout,
365 struct GNUNET_FS_TestDaemon **daemons,
366 GNUNET_SCHEDULER_Task cont,
369 struct StartContext *sctx;
372 GNUNET_assert (total > 0);
373 sctx = GNUNET_malloc (sizeof (struct StartContext));
374 sctx->daemons = daemons;
377 sctx->cont_cls = cont_cls;
378 sctx->cfg = GNUNET_CONFIGURATION_create ();
380 GNUNET_CONFIGURATION_load (sctx->cfg,
384 GNUNET_CONFIGURATION_destroy (sctx->cfg);
386 GNUNET_SCHEDULER_add_continuation (cont,
388 GNUNET_SCHEDULER_REASON_TIMEOUT);
391 for (i=0;i<total;i++)
392 daemons[i] = GNUNET_malloc (sizeof (struct GNUNET_FS_TestDaemon));
393 sctx->group = GNUNET_TESTING_daemons_start (sctx->cfg,
402 sctx->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
408 struct ConnectContext
410 GNUNET_SCHEDULER_Task cont;
416 * Prototype of a function that will be called whenever
417 * two daemons are connected by the testing library.
420 * @param first peer id for first daemon
421 * @param second peer id for the second daemon
422 * @param distance distance between the connected peers
423 * @param first_cfg config for the first daemon
424 * @param second_cfg config for the second daemon
425 * @param first_daemon handle for the first daemon
426 * @param second_daemon handle for the second daemon
427 * @param emsg error message (NULL on success)
430 notify_connection (void *cls,
431 const struct GNUNET_PeerIdentity *first,
432 const struct GNUNET_PeerIdentity *second,
434 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
435 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
436 struct GNUNET_TESTING_Daemon *first_daemon,
437 struct GNUNET_TESTING_Daemon *second_daemon,
440 struct ConnectContext *cc = cls;
443 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
444 "Failed to connect peers: %s\n",
446 GNUNET_SCHEDULER_add_continuation (cc->cont,
449 ? GNUNET_SCHEDULER_REASON_TIMEOUT
450 : GNUNET_SCHEDULER_REASON_PREREQ_DONE);
456 * Connect two daemons for testing.
458 * @param daemon1 first daemon to connect
459 * @param daemon2 second first daemon to connect
460 * @param timeout if this operation cannot be completed within the
461 * given period, call the continuation with an error code
462 * @param cont function to call when done
463 * @param cont_cls closure for cont
466 GNUNET_FS_TEST_daemons_connect (struct GNUNET_FS_TestDaemon *daemon1,
467 struct GNUNET_FS_TestDaemon *daemon2,
468 struct GNUNET_TIME_Relative timeout,
469 GNUNET_SCHEDULER_Task cont,
472 struct ConnectContext *ncc;
474 ncc = GNUNET_malloc (sizeof (struct ConnectContext));
476 ncc->cont_cls = cont_cls;
477 GNUNET_TESTING_daemons_connect (daemon1->daemon,
487 * Obtain peer configuration used for testing.
489 * @param daemons array with the daemons
490 * @param off which configuration to get
491 * @return peer configuration
493 const struct GNUNET_CONFIGURATION_Handle *
494 GNUNET_FS_TEST_get_configuration (struct GNUNET_FS_TestDaemon **daemons,
497 return daemons[off]->cfg;
501 * Obtain peer group used for testing.
503 * @param daemons array with the daemons (must contain at least one)
506 struct GNUNET_TESTING_PeerGroup *
507 GNUNET_FS_TEST_get_group (struct GNUNET_FS_TestDaemon **daemons)
509 return daemons[0]->group;
514 * Stop daemons used for testing.
516 * @param total number of daemons to stop
517 * @param daemons array with the daemons (values will be clobbered)
520 GNUNET_FS_TEST_daemons_stop (unsigned int total,
521 struct GNUNET_FS_TestDaemon **daemons)
524 struct GNUNET_TESTING_PeerGroup *pg;
525 struct GNUNET_CONFIGURATION_Handle *gcfg;
527 GNUNET_assert (total > 0);
528 pg = daemons[0]->group;
529 gcfg = daemons[0]->gcfg;
530 for (i=0;i<total;i++)
532 if (daemons[i]->fs != NULL)
533 GNUNET_FS_stop (daemons[i]->fs);
534 if (daemons[i]->cfg != NULL)
535 GNUNET_CONFIGURATION_destroy (daemons[i]->cfg);
536 if (NULL != daemons[i]->publish_tmp_file)
538 GNUNET_DISK_directory_remove (daemons[i]->publish_tmp_file);
539 GNUNET_free (daemons[i]->publish_tmp_file);
541 GNUNET_free (daemons[i]);
544 GNUNET_TESTING_daemons_stop (pg,
545 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
552 publish_timeout (void *cls,
553 const struct GNUNET_SCHEDULER_TaskContext *tc)
555 struct GNUNET_FS_TestDaemon *daemon = cls;
556 GNUNET_FS_TEST_UriContinuation cont;
558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
559 "Timeout while trying to publish data\n");
560 cont = daemon->publish_cont;
561 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
562 daemon->publish_cont = NULL;
563 GNUNET_FS_publish_stop (daemon->publish_context);
564 daemon->publish_context = NULL;
565 cont (daemon->publish_cont_cls,
571 file_generator (void *cls,
577 struct GNUNET_FS_TestDaemon *daemon = cls;
585 for (pos=0;pos<8;pos++)
586 cbuf[pos] = (uint8_t) (offset >> pos*8);
587 for (pos=8;pos<max;pos++)
589 mod = (255 - (offset / 1024 / 32));
592 cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);
600 * Publish a file at the given daemon.
602 * @param daemon where to publish
603 * @param timeout if this operation cannot be completed within the
604 * given period, call the continuation with an error code
605 * @param anonymity option for publication
606 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
607 * GNUNET_SYSERR for simulation
608 * @param size size of the file to publish
609 * @param seed seed to use for file generation
610 * @param verbose how verbose to be in reporting
611 * @param cont function to call when done
612 * @param cont_cls closure for cont
615 GNUNET_FS_TEST_publish (struct GNUNET_FS_TestDaemon *daemon,
616 struct GNUNET_TIME_Relative timeout,
621 unsigned int verbose,
622 GNUNET_FS_TEST_UriContinuation cont,
625 struct GNUNET_FS_FileInformation *fi;
626 struct GNUNET_DISK_FileHandle *fh;
629 char buf[DBLOCK_SIZE];
632 GNUNET_assert (daemon->publish_cont == NULL);
633 daemon->publish_cont = cont;
634 daemon->publish_cont_cls = cont_cls;
635 daemon->publish_seed = seed;
636 daemon->verbose = verbose;
637 if (GNUNET_YES == do_index)
639 GNUNET_assert (daemon->publish_tmp_file == NULL);
640 daemon->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
641 GNUNET_assert (daemon->publish_tmp_file != NULL);
642 fh = GNUNET_DISK_file_open (daemon->publish_tmp_file,
643 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
644 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
645 GNUNET_assert (NULL != fh);
649 bsize = GNUNET_MIN (sizeof (buf),
652 GNUNET_assert (bsize ==
653 file_generator (daemon,
658 GNUNET_assert (emsg == NULL);
659 GNUNET_assert (bsize ==
660 GNUNET_DISK_file_write (fh,
665 GNUNET_assert (GNUNET_OK ==
666 GNUNET_DISK_file_close (fh));
667 fi = GNUNET_FS_file_information_create_from_file (daemon->fs,
669 daemon->publish_tmp_file,
674 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
678 fi = GNUNET_FS_file_information_create_from_reader (daemon->fs,
688 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
690 daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
693 GNUNET_FS_PUBLISH_OPTION_NONE);
694 daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
701 download_timeout (void *cls,
702 const struct GNUNET_SCHEDULER_TaskContext *tc)
704 struct GNUNET_FS_TestDaemon *daemon = cls;
706 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
707 "Timeout while trying to download file\n");
708 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
709 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
710 daemon->download_context = NULL;
711 GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
712 daemon->download_cont_cls,
713 GNUNET_SCHEDULER_REASON_TIMEOUT);
714 daemon->download_cont = NULL;
719 * Perform test download.
721 * @param daemon which peer to download from
722 * @param timeout if this operation cannot be completed within the
723 * given period, call the continuation with an error code
724 * @param anonymity option for download
725 * @param seed used for file validation
726 * @param uri URI of file to download (CHK/LOC only)
727 * @param verbose how verbose to be in reporting
728 * @param cont function to call when done
729 * @param cont_cls closure for cont
732 GNUNET_FS_TEST_download (struct GNUNET_FS_TestDaemon *daemon,
733 struct GNUNET_TIME_Relative timeout,
736 const struct GNUNET_FS_Uri *uri,
737 unsigned int verbose,
738 GNUNET_SCHEDULER_Task cont,
743 GNUNET_assert (daemon->download_cont == NULL);
744 size = GNUNET_FS_uri_chk_get_file_size (uri);
745 daemon->verbose = verbose;
746 daemon->download_cont = cont;
747 daemon->download_cont_cls = cont_cls;
748 daemon->download_seed = seed;
749 daemon->download_context = GNUNET_FS_download_start (daemon->fs,
756 GNUNET_FS_DOWNLOAD_OPTION_NONE,
759 daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
764 /* end of test_fs_lib.c */