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_PUBLISH_PROGRESS:
197 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
198 "Publishing at %llu/%llu bytes\n",
199 (unsigned long long) info->value.publish.completed,
200 (unsigned long long) info->value.publish.size);
202 case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
204 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
205 "Download at %llu/%llu bytes\n",
206 (unsigned long long) info->value.download.completed,
207 (unsigned long long) info->value.download.size);
209 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
210 GNUNET_SCHEDULER_cancel (daemon->download_sched,
211 daemon->download_timeout_task);
212 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
213 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
216 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
218 case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
219 case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
221 /* FIXME: monitor data correctness during download progress */
222 /* FIXME: do performance reports given sufficient verbosity */
223 /* FIXME: advance timeout task to "immediate" on error */
234 struct GNUNET_SCHEDULER_Handle *sched;
235 struct GNUNET_TIME_Relative timeout;
238 struct GNUNET_FS_TestDaemon **daemons;
239 GNUNET_SCHEDULER_Task cont;
241 struct GNUNET_TESTING_PeerGroup *group;
242 struct GNUNET_CONFIGURATION_Handle *cfg;
243 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
248 notify_running (void *cls,
249 const struct GNUNET_PeerIdentity *id,
250 const struct GNUNET_CONFIGURATION_Handle *cfg,
251 struct GNUNET_TESTING_Daemon *d,
254 struct StartContext *sctx = cls;
259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260 _("Failed to start daemon: %s\n"),
264 GNUNET_assert (sctx->have < sctx->total);
265 sctx->daemons[sctx->have]->cfg = GNUNET_CONFIGURATION_dup (cfg);
266 sctx->daemons[sctx->have]->group = sctx->group;
267 sctx->daemons[sctx->have]->daemon = d;
268 sctx->daemons[sctx->have]->id = *id;
270 if (sctx->have == sctx->total)
272 GNUNET_SCHEDULER_add_continuation (sctx->sched,
275 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
276 GNUNET_CONFIGURATION_destroy (sctx->cfg);
277 GNUNET_SCHEDULER_cancel (sctx->sched,
279 for (i=0;i<sctx->total;i++)
280 sctx->daemons[i]->fs = GNUNET_FS_start (sctx->sched,
281 sctx->daemons[i]->cfg,
285 GNUNET_FS_FLAGS_NONE,
286 GNUNET_FS_OPTIONS_END);
293 start_timeout (void *cls,
294 const struct GNUNET_SCHEDULER_TaskContext *tc)
296 struct StartContext *sctx = cls;
299 GNUNET_TESTING_daemons_stop (sctx->group, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30));
300 for (i=0;i<sctx->total;i++)
303 GNUNET_CONFIGURATION_destroy (sctx->daemons[i]->cfg);
304 GNUNET_free (sctx->daemons[i]);
306 GNUNET_CONFIGURATION_destroy (sctx->cfg);
307 GNUNET_SCHEDULER_add_continuation (sctx->sched,
310 GNUNET_SCHEDULER_REASON_TIMEOUT);
316 * Start daemons for testing.
318 * @param sched scheduler to use
319 * @param template_cfg_file configuration template to use
320 * @param timeout if this operation cannot be completed within the
321 * given period, call the continuation with an error code
322 * @param total number of daemons to start
323 * @param daemons array of 'total' entries to be initialized
324 * (array must already be allocated, will be filled)
325 * @param cont function to call when done
326 * @param cont_cls closure for cont
329 GNUNET_FS_TEST_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
330 const char *template_cfg_file,
331 struct GNUNET_TIME_Relative timeout,
333 struct GNUNET_FS_TestDaemon **daemons,
334 GNUNET_SCHEDULER_Task cont,
337 struct StartContext *sctx;
340 GNUNET_assert (total > 0);
341 sctx = GNUNET_malloc (sizeof (struct StartContext));
343 sctx->daemons = daemons;
346 sctx->cont_cls = cont_cls;
347 sctx->cfg = GNUNET_CONFIGURATION_create ();
349 GNUNET_CONFIGURATION_load (sctx->cfg,
353 GNUNET_CONFIGURATION_destroy (sctx->cfg);
355 GNUNET_SCHEDULER_add_continuation (sched,
358 GNUNET_SCHEDULER_REASON_TIMEOUT);
361 for (i=0;i<total;i++)
362 daemons[i] = GNUNET_malloc (sizeof (struct GNUNET_FS_TestDaemon));
363 sctx->group = GNUNET_TESTING_daemons_start (sched,
373 sctx->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
380 struct ConnectContext
382 struct GNUNET_SCHEDULER_Handle *sched;
383 GNUNET_SCHEDULER_Task cont;
389 notify_connection (void *cls,
390 const struct GNUNET_PeerIdentity *first,
391 const struct GNUNET_PeerIdentity *second,
392 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
393 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
394 struct GNUNET_TESTING_Daemon *first_daemon,
395 struct GNUNET_TESTING_Daemon *second_daemon,
398 struct ConnectContext *cc = cls;
401 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
402 _("Failed to connect peers: %s\n"),
404 GNUNET_SCHEDULER_add_continuation (cc->sched,
408 ? GNUNET_SCHEDULER_REASON_TIMEOUT
409 : GNUNET_SCHEDULER_REASON_PREREQ_DONE);
415 * Connect two daemons for testing.
417 * @param sched scheduler to use
418 * @param daemon1 first daemon to connect
419 * @param daemon2 second first daemon to connect
420 * @param timeout if this operation cannot be completed within the
421 * given period, call the continuation with an error code
422 * @param cont function to call when done
423 * @param cont_cls closure for cont
426 GNUNET_FS_TEST_daemons_connect (struct GNUNET_SCHEDULER_Handle *sched,
427 struct GNUNET_FS_TestDaemon *daemon1,
428 struct GNUNET_FS_TestDaemon *daemon2,
429 struct GNUNET_TIME_Relative timeout,
430 GNUNET_SCHEDULER_Task cont,
433 struct ConnectContext *ncc;
435 ncc = GNUNET_malloc (sizeof (struct ConnectContext));
438 ncc->cont_cls = cont_cls;
439 GNUNET_TESTING_daemons_connect (daemon1->daemon,
449 * Stop daemons used for testing.
451 * @param sched scheduler to use
452 * @param total number of daemons to stop
453 * @param daemons array with the daemons (values will be clobbered)
456 GNUNET_FS_TEST_daemons_stop (struct GNUNET_SCHEDULER_Handle *sched,
458 struct GNUNET_FS_TestDaemon **daemons)
462 GNUNET_assert (total > 0);
463 GNUNET_TESTING_daemons_stop (daemons[0]->group, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30));
464 for (i=0;i<total;i++)
466 GNUNET_FS_stop (daemons[i]->fs);
467 GNUNET_CONFIGURATION_destroy (daemons[i]->cfg);
468 GNUNET_free (daemons[i]);
475 publish_timeout (void *cls,
476 const struct GNUNET_SCHEDULER_TaskContext *tc)
478 struct GNUNET_FS_TestDaemon *daemon = cls;
479 GNUNET_FS_TEST_UriContinuation cont;
481 cont = daemon->publish_cont;
482 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
483 daemon->publish_cont = NULL;
484 GNUNET_FS_publish_stop (daemon->publish_context);
485 daemon->publish_context = NULL;
486 cont (daemon->publish_cont_cls,
492 file_generator (void *cls,
498 struct GNUNET_FS_TestDaemon *daemon = cls;
503 for (pos=0;pos<max;pos++)
505 mod = (255 - (offset / 1024 / 32));
508 cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);
516 * Publish a file at the given daemon.
518 * @param sched scheduler to use
519 * @param daemon where to publish
520 * @param timeout if this operation cannot be completed within the
521 * given period, call the continuation with an error code
522 * @param anonymity option for publication
523 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
524 * GNUNET_SYSERR for simulation
525 * @param size size of the file to publish
526 * @param seed seed to use for file generation
527 * @param verbose how verbose to be in reporting
528 * @param cont function to call when done
529 * @param cont_cls closure for cont
532 GNUNET_FS_TEST_publish (struct GNUNET_SCHEDULER_Handle *sched,
533 struct GNUNET_FS_TestDaemon *daemon,
534 struct GNUNET_TIME_Relative timeout,
539 unsigned int verbose,
540 GNUNET_FS_TEST_UriContinuation cont,
543 GNUNET_assert (daemon->publish_cont == NULL);
544 struct GNUNET_FS_FileInformation *fi;
546 daemon->publish_cont = cont;
547 daemon->publish_cont_cls = cont_cls;
548 daemon->publish_seed = seed;
549 daemon->verbose = verbose;
550 daemon->publish_sched = sched;
551 fi = GNUNET_FS_file_information_create_from_reader (daemon->fs,
561 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
562 daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
565 GNUNET_FS_PUBLISH_OPTION_NONE);
566 daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
574 download_timeout (void *cls,
575 const struct GNUNET_SCHEDULER_TaskContext *tc)
577 struct GNUNET_FS_TestDaemon *daemon = cls;
579 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
580 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
581 daemon->download_context = NULL;
582 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
583 daemon->download_cont,
584 daemon->download_cont_cls,
585 GNUNET_SCHEDULER_REASON_TIMEOUT);
586 daemon->download_cont = NULL;
587 daemon->download_sched = NULL;
592 * Perform test download.
594 * @param sched scheduler to use
595 * @param daemon which peer to download from
596 * @param timeout if this operation cannot be completed within the
597 * given period, call the continuation with an error code
598 * @param anonymity option for download
599 * @param seed used for file validation
600 * @param uri URI of file to download (CHK/LOC only)
601 * @param verbose how verbose to be in reporting
602 * @param cont function to call when done
603 * @param cont_cls closure for cont
606 GNUNET_FS_TEST_download (struct GNUNET_SCHEDULER_Handle *sched,
607 struct GNUNET_FS_TestDaemon *daemon,
608 struct GNUNET_TIME_Relative timeout,
611 const struct GNUNET_FS_Uri *uri,
612 unsigned int verbose,
613 GNUNET_SCHEDULER_Task cont,
618 GNUNET_assert (daemon->download_cont == NULL);
619 size = GNUNET_FS_uri_chk_get_file_size (uri);
620 daemon->verbose = verbose;
621 daemon->download_sched = sched;
622 daemon->download_cont = cont;
623 daemon->download_cont_cls = cont_cls;
624 daemon->download_seed = seed;
625 daemon->download_context = GNUNET_FS_download_start (daemon->fs,
632 GNUNET_FS_DOWNLOAD_OPTION_NONE,
635 daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
641 /* end of test_fs_lib.c */