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
30 #include "fs_test_lib.h"
31 #include "gnunet_testing_lib.h"
33 #define CONNECT_ATTEMPTS 4
36 * Handle for a daemon started for testing FS.
38 struct GNUNET_FS_TestDaemon
42 * Global configuration, only stored in first test daemon,
45 struct GNUNET_CONFIGURATION_Handle *gcfg;
48 * Handle to the file sharing context using this daemon.
50 struct GNUNET_FS_Handle *fs;
53 * Handle to the daemon via testing.
55 struct GNUNET_TESTING_Daemon *daemon;
58 * Note that 'group' will be the same value for all of the
59 * daemons started jointly.
61 struct GNUNET_TESTING_PeerGroup *group;
64 * Configuration for accessing this peer.
66 struct GNUNET_CONFIGURATION_Handle *cfg;
71 struct GNUNET_PeerIdentity id;
74 * Scheduler to use (for publish_cont).
76 struct GNUNET_SCHEDULER_Handle *publish_sched;
79 * Function to call when upload is done.
81 GNUNET_FS_TEST_UriContinuation publish_cont;
84 * Closure for publish_cont.
86 void *publish_cont_cls;
89 * Task to abort publishing (timeout).
91 GNUNET_SCHEDULER_TaskIdentifier publish_timeout_task;
94 * Seed for file generation.
96 uint32_t publish_seed;
99 * Context for current publishing operation.
101 struct GNUNET_FS_PublishContext *publish_context;
106 struct GNUNET_FS_Uri *publish_uri;
109 * Scheduler to use (for download_cont).
111 struct GNUNET_SCHEDULER_Handle *download_sched;
114 * Function to call when download is done.
116 GNUNET_SCHEDULER_Task download_cont;
119 * Closure for download_cont.
121 void *download_cont_cls;
124 * Seed for download verification.
126 uint32_t download_seed;
129 * Task to abort downloading (timeout).
131 GNUNET_SCHEDULER_TaskIdentifier download_timeout_task;
134 * Context for current download operation.
136 struct GNUNET_FS_DownloadContext *download_context;
139 * Verbosity level of the current operation.
147 * Check whether peers successfully shut down.
150 shutdown_callback (void *cls,
153 struct GNUNET_CONFIGURATION_Handle *gcfg = cls;
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
158 "Shutdown of peers failed: %s\n",
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "All peers successfully shut down!\n");
169 GNUNET_CONFIGURATION_destroy (gcfg);
174 report_uri (void *cls,
175 const struct GNUNET_SCHEDULER_TaskContext *tc)
177 struct GNUNET_FS_TestDaemon *daemon = cls;
178 GNUNET_FS_TEST_UriContinuation cont;
179 struct GNUNET_FS_Uri *uri;
181 GNUNET_FS_publish_stop (daemon->publish_context);
182 daemon->publish_context = NULL;
183 daemon->publish_sched = NULL;
184 cont = daemon->publish_cont;
185 daemon->publish_cont = NULL;
186 uri = daemon->publish_uri;
187 cont (daemon->publish_cont_cls,
189 GNUNET_FS_uri_destroy (uri);
194 report_success (void *cls,
195 const struct GNUNET_SCHEDULER_TaskContext *tc)
197 struct GNUNET_FS_TestDaemon *daemon = cls;
199 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
200 daemon->download_context = NULL;
201 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
202 daemon->download_cont,
203 daemon->download_cont_cls,
204 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
205 daemon->download_cont = NULL;
206 daemon->download_sched = NULL;
211 progress_cb (void *cls,
212 const struct GNUNET_FS_ProgressInfo *info)
214 struct GNUNET_FS_TestDaemon *daemon = cls;
216 switch (info->status)
218 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
219 GNUNET_SCHEDULER_cancel (daemon->publish_sched,
220 daemon->publish_timeout_task);
221 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
222 daemon->publish_uri = GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
223 GNUNET_SCHEDULER_add_continuation (daemon->publish_sched,
226 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
228 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
231 "Publishing at %llu/%llu bytes\n",
232 (unsigned long long) info->value.publish.completed,
233 (unsigned long long) info->value.publish.size);
235 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
237 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
238 "Download at %llu/%llu bytes\n",
239 (unsigned long long) info->value.download.completed,
240 (unsigned long long) info->value.download.size);
242 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
243 GNUNET_SCHEDULER_cancel (daemon->download_sched,
244 daemon->download_timeout_task);
245 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
246 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
249 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
251 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
252 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
254 /* FIXME: monitor data correctness during download progress */
255 /* FIXME: do performance reports given sufficient verbosity */
256 /* FIXME: advance timeout task to "immediate" on error */
266 struct GNUNET_SCHEDULER_Handle *sched;
267 struct GNUNET_TIME_Relative timeout;
270 struct GNUNET_FS_TestDaemon **daemons;
271 GNUNET_SCHEDULER_Task cont;
273 struct GNUNET_TESTING_PeerGroup *group;
274 struct GNUNET_CONFIGURATION_Handle *cfg;
275 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
280 notify_running (void *cls,
281 const struct GNUNET_PeerIdentity *id,
282 const struct GNUNET_CONFIGURATION_Handle *cfg,
283 struct GNUNET_TESTING_Daemon *d,
286 struct StartContext *sctx = cls;
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292 _("Failed to start daemon: %s\n"),
297 while (i < sctx->total)
299 if (GNUNET_TESTING_daemon_get (sctx->group,
304 GNUNET_assert (i < sctx->total);
305 GNUNET_assert (sctx->have < sctx->total);
306 GNUNET_assert (sctx->daemons[i]->cfg == NULL);
307 sctx->daemons[i]->cfg = GNUNET_CONFIGURATION_dup (cfg);
308 sctx->daemons[i]->group = sctx->group;
309 sctx->daemons[i]->daemon = d;
310 sctx->daemons[i]->id = *id;
312 if (sctx->have == sctx->total)
314 GNUNET_SCHEDULER_add_continuation (sctx->sched,
317 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
318 sctx->daemons[0]->gcfg = sctx->cfg;
319 GNUNET_SCHEDULER_cancel (sctx->sched,
321 for (i=0;i<sctx->total;i++)
323 sctx->daemons[i]->fs = GNUNET_FS_start (sctx->sched,
324 sctx->daemons[i]->cfg,
328 GNUNET_FS_FLAGS_NONE,
329 GNUNET_FS_OPTIONS_END);
337 start_timeout (void *cls,
338 const struct GNUNET_SCHEDULER_TaskContext *tc)
340 struct StartContext *sctx = cls;
343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
344 "Timeout while trying to start daemons\n");
345 GNUNET_TESTING_daemons_stop (sctx->group,
346 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
349 for (i=0;i<sctx->total;i++)
352 GNUNET_CONFIGURATION_destroy (sctx->daemons[i]->cfg);
353 GNUNET_free (sctx->daemons[i]);
354 sctx->daemons[i] = NULL;
356 GNUNET_CONFIGURATION_destroy (sctx->cfg);
357 GNUNET_SCHEDULER_add_continuation (sctx->sched,
360 GNUNET_SCHEDULER_REASON_TIMEOUT);
366 * Start daemons for testing.
368 * @param sched scheduler to use
369 * @param template_cfg_file configuration template to use
370 * @param timeout if this operation cannot be completed within the
371 * given period, call the continuation with an error code
372 * @param total number of daemons to start
373 * @param daemons array of 'total' entries to be initialized
374 * (array must already be allocated, will be filled)
375 * @param cont function to call when done
376 * @param cont_cls closure for cont
379 GNUNET_FS_TEST_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
380 const char *template_cfg_file,
381 struct GNUNET_TIME_Relative timeout,
383 struct GNUNET_FS_TestDaemon **daemons,
384 GNUNET_SCHEDULER_Task cont,
387 struct StartContext *sctx;
390 GNUNET_assert (total > 0);
391 sctx = GNUNET_malloc (sizeof (struct StartContext));
393 sctx->daemons = daemons;
396 sctx->cont_cls = cont_cls;
397 sctx->cfg = GNUNET_CONFIGURATION_create ();
399 GNUNET_CONFIGURATION_load (sctx->cfg,
403 GNUNET_CONFIGURATION_destroy (sctx->cfg);
405 GNUNET_SCHEDULER_add_continuation (sched,
408 GNUNET_SCHEDULER_REASON_TIMEOUT);
411 for (i=0;i<total;i++)
412 daemons[i] = GNUNET_malloc (sizeof (struct GNUNET_FS_TestDaemon));
413 sctx->group = GNUNET_TESTING_daemons_start (sched,
423 sctx->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
430 struct ConnectContext
432 struct GNUNET_SCHEDULER_Handle *sched;
433 GNUNET_SCHEDULER_Task cont;
439 * Prototype of a function that will be called whenever
440 * two daemons are connected by the testing library.
443 * @param first peer id for first daemon
444 * @param second peer id for the second daemon
445 * @param distance distance between the connected peers
446 * @param first_cfg config for the first daemon
447 * @param second_cfg config for the second daemon
448 * @param first_daemon handle for the first daemon
449 * @param second_daemon handle for the second daemon
450 * @param emsg error message (NULL on success)
453 notify_connection (void *cls,
454 const struct GNUNET_PeerIdentity *first,
455 const struct GNUNET_PeerIdentity *second,
457 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
458 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
459 struct GNUNET_TESTING_Daemon *first_daemon,
460 struct GNUNET_TESTING_Daemon *second_daemon,
463 struct ConnectContext *cc = cls;
466 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
467 "Failed to connect peers: %s\n",
469 GNUNET_SCHEDULER_add_continuation (cc->sched,
473 ? GNUNET_SCHEDULER_REASON_TIMEOUT
474 : GNUNET_SCHEDULER_REASON_PREREQ_DONE);
480 * Connect two daemons for testing.
482 * @param sched scheduler to use
483 * @param daemon1 first daemon to connect
484 * @param daemon2 second first daemon to connect
485 * @param timeout if this operation cannot be completed within the
486 * given period, call the continuation with an error code
487 * @param cont function to call when done
488 * @param cont_cls closure for cont
491 GNUNET_FS_TEST_daemons_connect (struct GNUNET_SCHEDULER_Handle *sched,
492 struct GNUNET_FS_TestDaemon *daemon1,
493 struct GNUNET_FS_TestDaemon *daemon2,
494 struct GNUNET_TIME_Relative timeout,
495 GNUNET_SCHEDULER_Task cont,
498 struct ConnectContext *ncc;
500 ncc = GNUNET_malloc (sizeof (struct ConnectContext));
503 ncc->cont_cls = cont_cls;
504 GNUNET_TESTING_daemons_connect (daemon1->daemon,
514 * Obtain peer configuration used for testing.
516 * @param daemons array with the daemons
517 * @param off which configuration to get
518 * @return peer configuration
520 const struct GNUNET_CONFIGURATION_Handle *
521 GNUNET_FS_TEST_get_configuration (struct GNUNET_FS_TestDaemon **daemons,
524 return daemons[off]->cfg;
528 * Obtain peer group used for testing.
530 * @param daemons array with the daemons (must contain at least one)
533 struct GNUNET_TESTING_PeerGroup *
534 GNUNET_FS_TEST_get_group (struct GNUNET_FS_TestDaemon **daemons)
536 return daemons[0]->group;
541 * Stop daemons used for testing.
543 * @param sched scheduler to use
544 * @param total number of daemons to stop
545 * @param daemons array with the daemons (values will be clobbered)
548 GNUNET_FS_TEST_daemons_stop (struct GNUNET_SCHEDULER_Handle *sched,
550 struct GNUNET_FS_TestDaemon **daemons)
553 struct GNUNET_TESTING_PeerGroup *pg;
554 struct GNUNET_CONFIGURATION_Handle *gcfg;
556 GNUNET_assert (total > 0);
557 pg = daemons[0]->group;
558 gcfg = daemons[0]->gcfg;
559 for (i=0;i<total;i++)
561 if (daemons[i]->fs != NULL)
562 GNUNET_FS_stop (daemons[i]->fs);
563 if (daemons[i]->cfg != NULL)
564 GNUNET_CONFIGURATION_destroy (daemons[i]->cfg);
565 GNUNET_free (daemons[i]);
568 GNUNET_TESTING_daemons_stop (pg,
569 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
576 publish_timeout (void *cls,
577 const struct GNUNET_SCHEDULER_TaskContext *tc)
579 struct GNUNET_FS_TestDaemon *daemon = cls;
580 GNUNET_FS_TEST_UriContinuation cont;
582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
583 "Timeout while trying to publish data\n");
584 cont = daemon->publish_cont;
585 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
586 daemon->publish_cont = NULL;
587 GNUNET_FS_publish_stop (daemon->publish_context);
588 daemon->publish_context = NULL;
589 cont (daemon->publish_cont_cls,
595 file_generator (void *cls,
601 struct GNUNET_FS_TestDaemon *daemon = cls;
608 for (pos=0;pos<8;pos++)
609 cbuf[pos] = (uint8_t) (offset >> pos*8);
610 for (pos=8;pos<max;pos++)
612 mod = (255 - (offset / 1024 / 32));
615 cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);
623 * Publish a file at the given daemon.
625 * @param sched scheduler to use
626 * @param daemon where to publish
627 * @param timeout if this operation cannot be completed within the
628 * given period, call the continuation with an error code
629 * @param anonymity option for publication
630 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
631 * GNUNET_SYSERR for simulation
632 * @param size size of the file to publish
633 * @param seed seed to use for file generation
634 * @param verbose how verbose to be in reporting
635 * @param cont function to call when done
636 * @param cont_cls closure for cont
639 GNUNET_FS_TEST_publish (struct GNUNET_SCHEDULER_Handle *sched,
640 struct GNUNET_FS_TestDaemon *daemon,
641 struct GNUNET_TIME_Relative timeout,
646 unsigned int verbose,
647 GNUNET_FS_TEST_UriContinuation cont,
650 GNUNET_assert (daemon->publish_cont == NULL);
651 struct GNUNET_FS_FileInformation *fi;
653 daemon->publish_cont = cont;
654 daemon->publish_cont_cls = cont_cls;
655 daemon->publish_seed = seed;
656 daemon->verbose = verbose;
657 daemon->publish_sched = sched;
658 fi = GNUNET_FS_file_information_create_from_reader (daemon->fs,
668 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
669 daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
672 GNUNET_FS_PUBLISH_OPTION_NONE);
673 daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
681 download_timeout (void *cls,
682 const struct GNUNET_SCHEDULER_TaskContext *tc)
684 struct GNUNET_FS_TestDaemon *daemon = cls;
686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
687 "Timeout while trying to download file\n");
688 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
689 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
690 daemon->download_context = NULL;
691 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
692 daemon->download_cont,
693 daemon->download_cont_cls,
694 GNUNET_SCHEDULER_REASON_TIMEOUT);
695 daemon->download_cont = NULL;
696 daemon->download_sched = NULL;
701 * Perform test download.
703 * @param sched scheduler to use
704 * @param daemon which peer to download from
705 * @param timeout if this operation cannot be completed within the
706 * given period, call the continuation with an error code
707 * @param anonymity option for download
708 * @param seed used for file validation
709 * @param uri URI of file to download (CHK/LOC only)
710 * @param verbose how verbose to be in reporting
711 * @param cont function to call when done
712 * @param cont_cls closure for cont
715 GNUNET_FS_TEST_download (struct GNUNET_SCHEDULER_Handle *sched,
716 struct GNUNET_FS_TestDaemon *daemon,
717 struct GNUNET_TIME_Relative timeout,
720 const struct GNUNET_FS_Uri *uri,
721 unsigned int verbose,
722 GNUNET_SCHEDULER_Task cont,
727 GNUNET_assert (daemon->download_cont == NULL);
728 size = GNUNET_FS_uri_chk_get_file_size (uri);
729 daemon->verbose = verbose;
730 daemon->download_sched = sched;
731 daemon->download_cont = cont;
732 daemon->download_cont_cls = cont_cls;
733 daemon->download_seed = seed;
734 daemon->download_context = GNUNET_FS_download_start (daemon->fs,
741 GNUNET_FS_DOWNLOAD_OPTION_NONE,
744 daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
750 /* end of test_fs_lib.c */