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 2, 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.
142 report_uri (void *cls,
143 const struct GNUNET_SCHEDULER_TaskContext *tc)
145 struct GNUNET_FS_TestDaemon *daemon = cls;
146 GNUNET_FS_TEST_UriContinuation cont;
147 struct GNUNET_FS_Uri *uri;
149 GNUNET_FS_publish_stop (daemon->publish_context);
150 daemon->publish_context = NULL;
151 daemon->publish_sched = NULL;
152 cont = daemon->publish_cont;
153 daemon->publish_cont = NULL;
154 uri = daemon->publish_uri;
155 cont (daemon->publish_cont_cls,
157 GNUNET_FS_uri_destroy (uri);
162 report_success (void *cls,
163 const struct GNUNET_SCHEDULER_TaskContext *tc)
165 struct GNUNET_FS_TestDaemon *daemon = cls;
167 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
168 daemon->download_context = NULL;
169 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
170 daemon->download_cont,
171 daemon->download_cont_cls,
172 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
173 daemon->download_cont = NULL;
174 daemon->download_sched = NULL;
178 progress_cb (void *cls,
179 const struct GNUNET_FS_ProgressInfo *info)
181 struct GNUNET_FS_TestDaemon *daemon = cls;
183 switch (info->status)
185 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
186 GNUNET_SCHEDULER_cancel (daemon->publish_sched,
187 daemon->publish_timeout_task);
188 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
189 daemon->publish_uri = GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
190 GNUNET_SCHEDULER_add_continuation (daemon->publish_sched,
193 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
195 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
197 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
198 "Download at %llu/%llu bytes\n",
199 (unsigned long long) info->value.download.completed,
200 (unsigned long long) info->value.download.size);
202 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
203 GNUNET_SCHEDULER_cancel (daemon->download_sched,
204 daemon->download_timeout_task);
205 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
206 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
209 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
211 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
212 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
214 /* FIXME: monitor data correctness during download progress */
215 /* FIXME: do performance reports given sufficient verbosity */
216 /* FIXME: advance timeout task to "immediate" on error */
227 struct GNUNET_SCHEDULER_Handle *sched;
228 struct GNUNET_TIME_Relative timeout;
231 struct GNUNET_FS_TestDaemon **daemons;
232 GNUNET_SCHEDULER_Task cont;
234 struct GNUNET_TESTING_PeerGroup *group;
235 struct GNUNET_CONFIGURATION_Handle *cfg;
236 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
241 notify_running (void *cls,
242 const struct GNUNET_PeerIdentity *id,
243 const struct GNUNET_CONFIGURATION_Handle *cfg,
244 struct GNUNET_TESTING_Daemon *d,
247 struct StartContext *sctx = cls;
252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
253 _("Failed to start daemon: %s\n"),
257 GNUNET_assert (sctx->have < sctx->total);
258 sctx->daemons[sctx->have]->cfg = GNUNET_CONFIGURATION_dup (cfg);
259 sctx->daemons[sctx->have]->group = sctx->group;
260 sctx->daemons[sctx->have]->daemon = d;
261 sctx->daemons[sctx->have]->id = *id;
263 if (sctx->have == sctx->total)
265 GNUNET_SCHEDULER_add_continuation (sctx->sched,
268 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
269 GNUNET_CONFIGURATION_destroy (sctx->cfg);
270 GNUNET_SCHEDULER_cancel (sctx->sched,
272 for (i=0;i<sctx->total;i++)
273 sctx->daemons[i]->fs = GNUNET_FS_start (sctx->sched,
274 sctx->daemons[i]->cfg,
278 GNUNET_FS_FLAGS_NONE,
279 GNUNET_FS_OPTIONS_END);
286 start_timeout (void *cls,
287 const struct GNUNET_SCHEDULER_TaskContext *tc)
289 struct StartContext *sctx = cls;
292 GNUNET_TESTING_daemons_stop (sctx->group);
293 for (i=0;i<sctx->total;i++)
296 GNUNET_CONFIGURATION_destroy (sctx->daemons[i]->cfg);
297 GNUNET_free (sctx->daemons[i]);
299 GNUNET_CONFIGURATION_destroy (sctx->cfg);
300 GNUNET_SCHEDULER_add_continuation (sctx->sched,
303 GNUNET_SCHEDULER_REASON_TIMEOUT);
309 * Start daemons for testing.
311 * @param sched scheduler to use
312 * @param timeout if this operation cannot be completed within the
313 * given period, call the continuation with an error code
314 * @param total number of daemons to start
315 * @param daemons array of 'total' entries to be initialized
316 * (array must already be allocated, will be filled)
317 * @param cont function to call when done
318 * @param cont_cls closure for cont
321 GNUNET_FS_TEST_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
322 struct GNUNET_TIME_Relative timeout,
324 struct GNUNET_FS_TestDaemon **daemons,
325 GNUNET_SCHEDULER_Task cont,
328 struct StartContext *sctx;
331 GNUNET_assert (total > 0);
332 sctx = GNUNET_malloc (sizeof (struct StartContext));
334 sctx->daemons = daemons;
337 sctx->cont_cls = cont_cls;
338 sctx->cfg = GNUNET_CONFIGURATION_create ();
340 GNUNET_CONFIGURATION_load (sctx->cfg,
341 "fs_test_lib_data.conf"))
344 GNUNET_CONFIGURATION_destroy (sctx->cfg);
346 GNUNET_SCHEDULER_add_continuation (sched,
349 GNUNET_SCHEDULER_REASON_TIMEOUT);
352 for (i=0;i<total;i++)
353 daemons[i] = GNUNET_malloc (sizeof (struct GNUNET_FS_TestDaemon));
354 sctx->group = GNUNET_TESTING_daemons_start (sched,
361 sctx->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
368 struct ConnectContext
370 struct GNUNET_SCHEDULER_Handle *sched;
371 GNUNET_SCHEDULER_Task cont;
377 notify_connection (void *cls,
378 const struct GNUNET_PeerIdentity *first,
379 const struct GNUNET_PeerIdentity *second,
380 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
381 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
382 struct GNUNET_TESTING_Daemon *first_daemon,
383 struct GNUNET_TESTING_Daemon *second_daemon,
386 struct ConnectContext *cc = cls;
389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
390 _("Failed to connect peers: %s\n"),
392 GNUNET_SCHEDULER_add_continuation (cc->sched,
396 ? GNUNET_SCHEDULER_REASON_TIMEOUT
397 : GNUNET_SCHEDULER_REASON_PREREQ_DONE);
403 * Connect two daemons for testing.
405 * @param sched scheduler to use
406 * @param daemon1 first daemon to connect
407 * @param daemon2 second first daemon to connect
408 * @param timeout if this operation cannot be completed within the
409 * given period, call the continuation with an error code
410 * @param cont function to call when done
411 * @param cont_cls closure for cont
414 GNUNET_FS_TEST_daemons_connect (struct GNUNET_SCHEDULER_Handle *sched,
415 struct GNUNET_FS_TestDaemon *daemon1,
416 struct GNUNET_FS_TestDaemon *daemon2,
417 struct GNUNET_TIME_Relative timeout,
418 GNUNET_SCHEDULER_Task cont,
421 struct ConnectContext *ncc;
423 ncc = GNUNET_malloc (sizeof (struct ConnectContext));
426 ncc->cont_cls = cont_cls;
427 GNUNET_TESTING_daemons_connect (daemon1->daemon,
437 * Stop daemons used for testing.
439 * @param sched scheduler to use
440 * @param total number of daemons to stop
441 * @param daemons array with the daemons (values will be clobbered)
444 GNUNET_FS_TEST_daemons_stop (struct GNUNET_SCHEDULER_Handle *sched,
446 struct GNUNET_FS_TestDaemon **daemons)
450 GNUNET_assert (total > 0);
451 GNUNET_TESTING_daemons_stop (daemons[0]->group);
452 for (i=0;i<total;i++)
454 GNUNET_FS_stop (daemons[i]->fs);
455 GNUNET_CONFIGURATION_destroy (daemons[i]->cfg);
456 GNUNET_free (daemons[i]);
463 publish_timeout (void *cls,
464 const struct GNUNET_SCHEDULER_TaskContext *tc)
466 struct GNUNET_FS_TestDaemon *daemon = cls;
467 GNUNET_FS_TEST_UriContinuation cont;
469 cont = daemon->publish_cont;
470 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
471 daemon->publish_cont = NULL;
472 GNUNET_FS_publish_stop (daemon->publish_context);
473 daemon->publish_context = NULL;
474 cont (daemon->publish_cont_cls,
480 file_generator (void *cls,
486 struct GNUNET_FS_TestDaemon *daemon = cls;
491 for (pos=0;pos<max;pos++)
493 mod = (255 - (offset / 1024 / 32));
496 cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);
504 * Publish a file at the given daemon.
506 * @param sched scheduler to use
507 * @param daemon where to publish
508 * @param timeout if this operation cannot be completed within the
509 * given period, call the continuation with an error code
510 * @param anonymity option for publication
511 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
512 * GNUNET_SYSERR for simulation
513 * @param size size of the file to publish
514 * @param seed seed to use for file generation
515 * @param verbose how verbose to be in reporting
516 * @param cont function to call when done
517 * @param cont_cls closure for cont
520 GNUNET_FS_TEST_publish (struct GNUNET_SCHEDULER_Handle *sched,
521 struct GNUNET_FS_TestDaemon *daemon,
522 struct GNUNET_TIME_Relative timeout,
527 unsigned int verbose,
528 GNUNET_FS_TEST_UriContinuation cont,
531 GNUNET_assert (daemon->publish_cont == NULL);
532 struct GNUNET_FS_FileInformation *fi;
534 daemon->publish_cont = cont;
535 daemon->publish_cont_cls = cont_cls;
536 daemon->publish_seed = seed;
537 daemon->verbose = verbose;
538 daemon->publish_sched = sched;
539 fi = GNUNET_FS_file_information_create_from_reader (daemon,
548 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
549 daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
552 GNUNET_FS_PUBLISH_OPTION_NONE);
553 daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
561 download_timeout (void *cls,
562 const struct GNUNET_SCHEDULER_TaskContext *tc)
564 struct GNUNET_FS_TestDaemon *daemon = cls;
566 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
567 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
568 daemon->download_context = NULL;
569 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
570 daemon->download_cont,
571 daemon->download_cont_cls,
572 GNUNET_SCHEDULER_REASON_TIMEOUT);
573 daemon->download_cont = NULL;
574 daemon->download_sched = NULL;
579 * Perform test download.
581 * @param sched scheduler to use
582 * @param daemon which peer to download from
583 * @param timeout if this operation cannot be completed within the
584 * given period, call the continuation with an error code
585 * @param anonymity option for download
586 * @param seed used for file validation
587 * @param uri URI of file to download (CHK/LOC only)
588 * @param verbose how verbose to be in reporting
589 * @param cont function to call when done
590 * @param cont_cls closure for cont
593 GNUNET_FS_TEST_download (struct GNUNET_SCHEDULER_Handle *sched,
594 struct GNUNET_FS_TestDaemon *daemon,
595 struct GNUNET_TIME_Relative timeout,
598 const struct GNUNET_FS_Uri *uri,
599 unsigned int verbose,
600 GNUNET_SCHEDULER_Task cont,
605 GNUNET_assert (daemon->download_cont == NULL);
606 size = GNUNET_FS_uri_chk_get_file_size (uri);
607 daemon->verbose = verbose;
608 daemon->download_sched = sched;
609 daemon->download_cont = cont;
610 daemon->download_cont_cls = cont_cls;
611 daemon->download_seed = seed;
612 daemon->download_context = GNUNET_FS_download_start (daemon->fs,
619 GNUNET_FS_DOWNLOAD_OPTION_NONE,
622 daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
628 /* end of test_fs_lib.c */