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 * Handle to the file sharing context using this daemon.
44 struct GNUNET_FS_Handle *fs;
47 * Handle to the daemon via testing.
49 struct GNUNET_TESTING_Daemon *daemon;
52 * Note that 'group' will be the same value for all of the
53 * daemons started jointly.
55 struct GNUNET_TESTING_PeerGroup *group;
58 * Configuration for accessing this peer.
60 struct GNUNET_CONFIGURATION_Handle *cfg;
65 struct GNUNET_PeerIdentity id;
68 * Scheduler to use (for publish_cont).
70 struct GNUNET_SCHEDULER_Handle *publish_sched;
73 * Function to call when upload is done.
75 GNUNET_FS_TEST_UriContinuation publish_cont;
78 * Closure for publish_cont.
80 void *publish_cont_cls;
83 * Task to abort publishing (timeout).
85 GNUNET_SCHEDULER_TaskIdentifier publish_timeout_task;
88 * Seed for file generation.
90 uint32_t publish_seed;
93 * Context for current publishing operation.
95 struct GNUNET_FS_PublishContext *publish_context;
100 struct GNUNET_FS_Uri *publish_uri;
103 * Scheduler to use (for download_cont).
105 struct GNUNET_SCHEDULER_Handle *download_sched;
108 * Function to call when download is done.
110 GNUNET_SCHEDULER_Task download_cont;
113 * Closure for download_cont.
115 void *download_cont_cls;
118 * Seed for download verification.
120 uint32_t download_seed;
123 * Task to abort downloading (timeout).
125 GNUNET_SCHEDULER_TaskIdentifier download_timeout_task;
128 * Context for current download operation.
130 struct GNUNET_FS_DownloadContext *download_context;
133 * Verbosity level of the current operation.
141 * Check whether peers successfully shut down.
143 void shutdown_callback (void *cls,
149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
150 "Shutdown of peers failed!\n");
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157 "All peers successfully shut down!\n");
163 report_uri (void *cls,
164 const struct GNUNET_SCHEDULER_TaskContext *tc)
166 struct GNUNET_FS_TestDaemon *daemon = cls;
167 GNUNET_FS_TEST_UriContinuation cont;
168 struct GNUNET_FS_Uri *uri;
170 GNUNET_FS_publish_stop (daemon->publish_context);
171 daemon->publish_context = NULL;
172 daemon->publish_sched = NULL;
173 cont = daemon->publish_cont;
174 daemon->publish_cont = NULL;
175 uri = daemon->publish_uri;
176 cont (daemon->publish_cont_cls,
178 GNUNET_FS_uri_destroy (uri);
183 report_success (void *cls,
184 const struct GNUNET_SCHEDULER_TaskContext *tc)
186 struct GNUNET_FS_TestDaemon *daemon = cls;
188 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
189 daemon->download_context = NULL;
190 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
191 daemon->download_cont,
192 daemon->download_cont_cls,
193 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
194 daemon->download_cont = NULL;
195 daemon->download_sched = NULL;
199 progress_cb (void *cls,
200 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_sched,
208 daemon->publish_timeout_task);
209 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
210 daemon->publish_uri = GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
211 GNUNET_SCHEDULER_add_continuation (daemon->publish_sched,
214 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
216 case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
218 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
219 "Publishing at %llu/%llu bytes\n",
220 (unsigned long long) info->value.publish.completed,
221 (unsigned long long) info->value.publish.size);
223 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
225 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
226 "Download at %llu/%llu bytes\n",
227 (unsigned long long) info->value.download.completed,
228 (unsigned long long) info->value.download.size);
230 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
231 GNUNET_SCHEDULER_cancel (daemon->download_sched,
232 daemon->download_timeout_task);
233 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
234 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
237 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
239 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
240 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
242 /* FIXME: monitor data correctness during download progress */
243 /* FIXME: do performance reports given sufficient verbosity */
244 /* FIXME: advance timeout task to "immediate" on error */
254 struct GNUNET_SCHEDULER_Handle *sched;
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->sched,
305 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
306 GNUNET_CONFIGURATION_destroy (sctx->cfg);
307 GNUNET_SCHEDULER_cancel (sctx->sched,
309 for (i=0;i<sctx->total;i++)
311 sctx->daemons[i]->fs = GNUNET_FS_start (sctx->sched,
312 sctx->daemons[i]->cfg,
316 GNUNET_FS_FLAGS_NONE,
317 GNUNET_FS_OPTIONS_END);
325 start_timeout (void *cls,
326 const struct GNUNET_SCHEDULER_TaskContext *tc)
328 struct StartContext *sctx = cls;
331 GNUNET_TESTING_daemons_stop (sctx->group,
332 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30),
335 for (i=0;i<sctx->total;i++)
338 GNUNET_CONFIGURATION_destroy (sctx->daemons[i]->cfg);
339 GNUNET_free (sctx->daemons[i]);
340 sctx->daemons[i] = NULL;
342 GNUNET_CONFIGURATION_destroy (sctx->cfg);
343 GNUNET_SCHEDULER_add_continuation (sctx->sched,
346 GNUNET_SCHEDULER_REASON_TIMEOUT);
352 * Start daemons for testing.
354 * @param sched scheduler to use
355 * @param template_cfg_file configuration template to use
356 * @param timeout if this operation cannot be completed within the
357 * given period, call the continuation with an error code
358 * @param total number of daemons to start
359 * @param daemons array of 'total' entries to be initialized
360 * (array must already be allocated, will be filled)
361 * @param cont function to call when done
362 * @param cont_cls closure for cont
365 GNUNET_FS_TEST_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
366 const char *template_cfg_file,
367 struct GNUNET_TIME_Relative timeout,
369 struct GNUNET_FS_TestDaemon **daemons,
370 GNUNET_SCHEDULER_Task cont,
373 struct StartContext *sctx;
376 GNUNET_assert (total > 0);
377 sctx = GNUNET_malloc (sizeof (struct StartContext));
379 sctx->daemons = daemons;
382 sctx->cont_cls = cont_cls;
383 sctx->cfg = GNUNET_CONFIGURATION_create ();
385 GNUNET_CONFIGURATION_load (sctx->cfg,
389 GNUNET_CONFIGURATION_destroy (sctx->cfg);
391 GNUNET_SCHEDULER_add_continuation (sched,
394 GNUNET_SCHEDULER_REASON_TIMEOUT);
397 for (i=0;i<total;i++)
398 daemons[i] = GNUNET_malloc (sizeof (struct GNUNET_FS_TestDaemon));
399 sctx->group = GNUNET_TESTING_daemons_start (sched,
409 sctx->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
416 struct ConnectContext
418 struct GNUNET_SCHEDULER_Handle *sched;
419 GNUNET_SCHEDULER_Task cont;
424 * Prototype of a function that will be called whenever
425 * two daemons are connected by the testing library.
428 * @param first peer id for first daemon
429 * @param second peer id for the second daemon
430 * @param distance distance between the connected peers
431 * @param first_cfg config for the first daemon
432 * @param second_cfg config for the second daemon
433 * @param first_daemon handle for the first daemon
434 * @param second_daemon handle for the second daemon
435 * @param emsg error message (NULL on success)
438 notify_connection (void *cls,
439 const struct GNUNET_PeerIdentity *first,
440 const struct GNUNET_PeerIdentity *second,
442 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
443 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
444 struct GNUNET_TESTING_Daemon *first_daemon,
445 struct GNUNET_TESTING_Daemon *second_daemon,
448 struct ConnectContext *cc = cls;
451 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
452 _("Failed to connect peers: %s\n"),
454 GNUNET_SCHEDULER_add_continuation (cc->sched,
458 ? GNUNET_SCHEDULER_REASON_TIMEOUT
459 : GNUNET_SCHEDULER_REASON_PREREQ_DONE);
465 * Connect two daemons for testing.
467 * @param sched scheduler to use
468 * @param daemon1 first daemon to connect
469 * @param daemon2 second first daemon to connect
470 * @param timeout if this operation cannot be completed within the
471 * given period, call the continuation with an error code
472 * @param cont function to call when done
473 * @param cont_cls closure for cont
476 GNUNET_FS_TEST_daemons_connect (struct GNUNET_SCHEDULER_Handle *sched,
477 struct GNUNET_FS_TestDaemon *daemon1,
478 struct GNUNET_FS_TestDaemon *daemon2,
479 struct GNUNET_TIME_Relative timeout,
480 GNUNET_SCHEDULER_Task cont,
483 struct ConnectContext *ncc;
485 ncc = GNUNET_malloc (sizeof (struct ConnectContext));
488 ncc->cont_cls = cont_cls;
489 GNUNET_TESTING_daemons_connect (daemon1->daemon,
499 * Obtain peer group used for testing.
501 * @param daemons array with the daemons (must contain at least one)
504 struct GNUNET_TESTING_PeerGroup *
505 GNUNET_FS_TEST_get_group (struct GNUNET_FS_TestDaemon **daemons)
507 return daemons[0]->group;
512 * Stop daemons used for testing.
514 * @param sched scheduler to use
515 * @param total number of daemons to stop
516 * @param daemons array with the daemons (values will be clobbered)
519 GNUNET_FS_TEST_daemons_stop (struct GNUNET_SCHEDULER_Handle *sched,
521 struct GNUNET_FS_TestDaemon **daemons)
524 struct GNUNET_TESTING_PeerGroup *pg;
526 GNUNET_assert (total > 0);
527 pg = daemons[0]->group;
528 for (i=0;i<total;i++)
530 if (daemons[i]->fs != NULL)
531 GNUNET_FS_stop (daemons[i]->fs);
532 if (daemons[i]->cfg != NULL)
533 GNUNET_CONFIGURATION_destroy (daemons[i]->cfg);
534 GNUNET_free (daemons[i]);
537 GNUNET_TESTING_daemons_stop (pg, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30), &shutdown_callback, NULL);
542 publish_timeout (void *cls,
543 const struct GNUNET_SCHEDULER_TaskContext *tc)
545 struct GNUNET_FS_TestDaemon *daemon = cls;
546 GNUNET_FS_TEST_UriContinuation cont;
548 cont = daemon->publish_cont;
549 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
550 daemon->publish_cont = NULL;
551 GNUNET_FS_publish_stop (daemon->publish_context);
552 daemon->publish_context = NULL;
553 cont (daemon->publish_cont_cls,
559 file_generator (void *cls,
565 struct GNUNET_FS_TestDaemon *daemon = cls;
570 for (pos=0;pos<max;pos++)
572 mod = (255 - (offset / 1024 / 32));
575 cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);
583 * Publish a file at the given daemon.
585 * @param sched scheduler to use
586 * @param daemon where to publish
587 * @param timeout if this operation cannot be completed within the
588 * given period, call the continuation with an error code
589 * @param anonymity option for publication
590 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
591 * GNUNET_SYSERR for simulation
592 * @param size size of the file to publish
593 * @param seed seed to use for file generation
594 * @param verbose how verbose to be in reporting
595 * @param cont function to call when done
596 * @param cont_cls closure for cont
599 GNUNET_FS_TEST_publish (struct GNUNET_SCHEDULER_Handle *sched,
600 struct GNUNET_FS_TestDaemon *daemon,
601 struct GNUNET_TIME_Relative timeout,
606 unsigned int verbose,
607 GNUNET_FS_TEST_UriContinuation cont,
610 GNUNET_assert (daemon->publish_cont == NULL);
611 struct GNUNET_FS_FileInformation *fi;
613 daemon->publish_cont = cont;
614 daemon->publish_cont_cls = cont_cls;
615 daemon->publish_seed = seed;
616 daemon->verbose = verbose;
617 daemon->publish_sched = sched;
618 fi = GNUNET_FS_file_information_create_from_reader (daemon->fs,
628 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
629 daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
632 GNUNET_FS_PUBLISH_OPTION_NONE);
633 daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
641 download_timeout (void *cls,
642 const struct GNUNET_SCHEDULER_TaskContext *tc)
644 struct GNUNET_FS_TestDaemon *daemon = cls;
646 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
647 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
648 daemon->download_context = NULL;
649 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
650 daemon->download_cont,
651 daemon->download_cont_cls,
652 GNUNET_SCHEDULER_REASON_TIMEOUT);
653 daemon->download_cont = NULL;
654 daemon->download_sched = NULL;
659 * Perform test download.
661 * @param sched scheduler to use
662 * @param daemon which peer to download from
663 * @param timeout if this operation cannot be completed within the
664 * given period, call the continuation with an error code
665 * @param anonymity option for download
666 * @param seed used for file validation
667 * @param uri URI of file to download (CHK/LOC only)
668 * @param verbose how verbose to be in reporting
669 * @param cont function to call when done
670 * @param cont_cls closure for cont
673 GNUNET_FS_TEST_download (struct GNUNET_SCHEDULER_Handle *sched,
674 struct GNUNET_FS_TestDaemon *daemon,
675 struct GNUNET_TIME_Relative timeout,
678 const struct GNUNET_FS_Uri *uri,
679 unsigned int verbose,
680 GNUNET_SCHEDULER_Task cont,
685 GNUNET_assert (daemon->download_cont == NULL);
686 size = GNUNET_FS_uri_chk_get_file_size (uri);
687 daemon->verbose = verbose;
688 daemon->download_sched = sched;
689 daemon->download_cont = cont;
690 daemon->download_cont_cls = cont_cls;
691 daemon->download_seed = seed;
692 daemon->download_context = GNUNET_FS_download_start (daemon->fs,
699 GNUNET_FS_DOWNLOAD_OPTION_NONE,
702 daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
708 /* end of test_fs_lib.c */