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;
584 for (pos=0;pos<8;pos++)
585 cbuf[pos] = (uint8_t) (offset >> pos*8);
586 for (pos=8;pos<max;pos++)
588 mod = (255 - (offset / 1024 / 32));
591 cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);
599 * Publish a file at the given daemon.
601 * @param daemon where to publish
602 * @param timeout if this operation cannot be completed within the
603 * given period, call the continuation with an error code
604 * @param anonymity option for publication
605 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
606 * GNUNET_SYSERR for simulation
607 * @param size size of the file to publish
608 * @param seed seed to use for file generation
609 * @param verbose how verbose to be in reporting
610 * @param cont function to call when done
611 * @param cont_cls closure for cont
614 GNUNET_FS_TEST_publish (struct GNUNET_FS_TestDaemon *daemon,
615 struct GNUNET_TIME_Relative timeout,
620 unsigned int verbose,
621 GNUNET_FS_TEST_UriContinuation cont,
624 struct GNUNET_FS_FileInformation *fi;
625 struct GNUNET_DISK_FileHandle *fh;
628 char buf[DBLOCK_SIZE];
631 GNUNET_assert (daemon->publish_cont == NULL);
632 daemon->publish_cont = cont;
633 daemon->publish_cont_cls = cont_cls;
634 daemon->publish_seed = seed;
635 daemon->verbose = verbose;
636 if (GNUNET_YES == do_index)
638 GNUNET_assert (daemon->publish_tmp_file == NULL);
639 daemon->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
640 GNUNET_assert (daemon->publish_tmp_file != NULL);
641 fh = GNUNET_DISK_file_open (daemon->publish_tmp_file,
642 GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
643 GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
644 GNUNET_assert (NULL != fh);
648 bsize = GNUNET_MIN (sizeof (buf),
650 GNUNET_assert (bsize ==
651 file_generator (daemon,
656 GNUNET_assert (emsg == NULL);
657 GNUNET_assert (bsize ==
658 GNUNET_DISK_file_write (fh,
663 GNUNET_assert (GNUNET_OK ==
664 GNUNET_DISK_file_close (fh));
665 fi = GNUNET_FS_file_information_create_from_file (daemon->fs,
667 daemon->publish_tmp_file,
672 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
676 fi = GNUNET_FS_file_information_create_from_reader (daemon->fs,
686 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
688 daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
691 GNUNET_FS_PUBLISH_OPTION_NONE);
692 daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
699 download_timeout (void *cls,
700 const struct GNUNET_SCHEDULER_TaskContext *tc)
702 struct GNUNET_FS_TestDaemon *daemon = cls;
704 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
705 "Timeout while trying to download file\n");
706 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
707 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
708 daemon->download_context = NULL;
709 GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
710 daemon->download_cont_cls,
711 GNUNET_SCHEDULER_REASON_TIMEOUT);
712 daemon->download_cont = NULL;
717 * Perform test download.
719 * @param daemon which peer to download from
720 * @param timeout if this operation cannot be completed within the
721 * given period, call the continuation with an error code
722 * @param anonymity option for download
723 * @param seed used for file validation
724 * @param uri URI of file to download (CHK/LOC only)
725 * @param verbose how verbose to be in reporting
726 * @param cont function to call when done
727 * @param cont_cls closure for cont
730 GNUNET_FS_TEST_download (struct GNUNET_FS_TestDaemon *daemon,
731 struct GNUNET_TIME_Relative timeout,
734 const struct GNUNET_FS_Uri *uri,
735 unsigned int verbose,
736 GNUNET_SCHEDULER_Task cont,
741 GNUNET_assert (daemon->download_cont == NULL);
742 size = GNUNET_FS_uri_chk_get_file_size (uri);
743 daemon->verbose = verbose;
744 daemon->download_cont = cont;
745 daemon->download_cont_cls = cont_cls;
746 daemon->download_seed = seed;
747 daemon->download_context = GNUNET_FS_download_start (daemon->fs,
754 GNUNET_FS_DOWNLOAD_OPTION_NONE,
757 daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
762 /* end of test_fs_lib.c */