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/test_fs_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"
35 * Handle for a daemon started for testing FS.
37 struct GNUNET_FS_TestDaemon
41 * Handle to the file sharing context using this daemon.
43 struct GNUNET_FS_Handle *fs;
46 * Handle to the daemon via testing.
48 struct GNUNET_TESTING_Daemon *daemon;
51 * Note that 'group' will be the same value for all of the
52 * daemons started jointly.
54 struct GNUNET_TESTING_PeerGroup *group;
57 * Configuration for accessing this peer.
59 struct GNUNET_CONFIGURATION_Handle *cfg;
64 struct GNUNET_PeerIdentity id;
67 * Scheduler to use (for publish_cont).
69 struct GNUNET_SCHEDULER_Handle *publish_sched;
72 * Function to call when upload is done.
74 GNUNET_FS_TEST_UriContinuation publish_cont;
77 * Closure for publish_cont.
79 void *publish_cont_cls;
82 * Task to abort publishing (timeout).
84 GNUNET_SCHEDULER_TaskIdentifier publish_timeout_task;
87 * Seed for file generation.
89 uint32_t publish_seed;
92 * Context for current publishing operation.
94 struct GNUNET_FS_PublishContext *publish_context;
99 struct GNUNET_FS_Uri *publish_uri;
102 * Scheduler to use (for download_cont).
104 struct GNUNET_SCHEDULER_Handle *download_sched;
107 * Function to call when download is done.
109 GNUNET_SCHEDULER_Task download_cont;
112 * Closure for download_cont.
114 void *download_cont_cls;
117 * Seed for download verification.
119 uint32_t download_seed;
122 * Task to abort downloading (timeout).
124 GNUNET_SCHEDULER_TaskIdentifier download_timeout_task;
127 * Context for current download operation.
129 struct GNUNET_FS_DownloadContext *download_context;
132 * Verbosity level of the current operation.
141 report_uri (void *cls,
142 const struct GNUNET_SCHEDULER_TaskContext *tc)
144 struct GNUNET_FS_TestDaemon *daemon = cls;
145 GNUNET_FS_TEST_UriContinuation cont;
146 struct GNUNET_FS_Uri *uri;
148 GNUNET_FS_publish_stop (daemon->publish_context);
149 daemon->publish_context = NULL;
150 daemon->publish_sched = NULL;
151 cont = daemon->publish_cont;
152 daemon->publish_cont = NULL;
153 uri = daemon->publish_uri;
154 cont (daemon->publish_cont_cls,
156 GNUNET_FS_uri_destroy (uri);
161 report_success (void *cls,
162 const struct GNUNET_SCHEDULER_TaskContext *tc)
164 struct GNUNET_FS_TestDaemon *daemon = cls;
166 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
167 daemon->download_context = NULL;
168 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
169 daemon->download_cont,
170 daemon->download_cont_cls,
171 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
172 daemon->download_cont = NULL;
173 daemon->download_sched = NULL;
177 progress_cb (void *cls,
178 const struct GNUNET_FS_ProgressInfo *info)
180 struct GNUNET_FS_TestDaemon *daemon = cls;
182 switch (info->status)
184 case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
185 GNUNET_SCHEDULER_cancel (daemon->publish_sched,
186 daemon->publish_timeout_task);
187 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
188 daemon->publish_uri = GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
189 GNUNET_SCHEDULER_add_continuation (daemon->publish_sched,
192 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
194 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
195 GNUNET_SCHEDULER_cancel (daemon->download_sched,
196 daemon->download_timeout_task);
197 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
198 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
201 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
203 /* FIXME: monitor data correctness during download progress */
204 /* FIXME: do performance reports given sufficient verbosity */
205 /* FIXME: advance timeout task to "immediate" on error */
216 struct GNUNET_SCHEDULER_Handle *sched;
217 struct GNUNET_TIME_Relative timeout;
220 struct GNUNET_FS_TestDaemon **daemons;
221 GNUNET_SCHEDULER_Task cont;
223 struct GNUNET_TESTING_PeerGroup *group;
224 struct GNUNET_CONFIGURATION_Handle *cfg;
225 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
230 notify_running (void *cls,
231 const struct GNUNET_PeerIdentity *id,
232 const struct GNUNET_CONFIGURATION_Handle *cfg,
233 struct GNUNET_TESTING_Daemon *d,
236 struct StartContext *sctx = cls;
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242 _("Failed to start daemon: %s\n"),
246 GNUNET_assert (sctx->have < sctx->total);
247 sctx->daemons[sctx->have]->cfg = GNUNET_CONFIGURATION_dup (cfg);
248 sctx->daemons[sctx->have]->group = sctx->group;
249 sctx->daemons[sctx->have]->daemon = d;
250 sctx->daemons[sctx->have]->id = *id;
252 if (sctx->have == sctx->total)
254 GNUNET_SCHEDULER_add_continuation (sctx->sched,
257 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
258 GNUNET_CONFIGURATION_destroy (sctx->cfg);
259 GNUNET_SCHEDULER_cancel (sctx->sched,
261 for (i=0;i<sctx->total;i++)
262 sctx->daemons[i]->fs = GNUNET_FS_start (sctx->sched,
263 sctx->daemons[i]->cfg,
267 GNUNET_FS_FLAGS_NONE,
268 GNUNET_FS_OPTIONS_END);
275 start_timeout (void *cls,
276 const struct GNUNET_SCHEDULER_TaskContext *tc)
278 struct StartContext *sctx = cls;
281 GNUNET_TESTING_daemons_stop (sctx->group);
282 for (i=0;i<sctx->total;i++)
285 GNUNET_CONFIGURATION_destroy (sctx->daemons[i]->cfg);
286 GNUNET_free (sctx->daemons[i]);
288 GNUNET_CONFIGURATION_destroy (sctx->cfg);
289 GNUNET_SCHEDULER_add_continuation (sctx->sched,
292 GNUNET_SCHEDULER_REASON_TIMEOUT);
298 * Start daemons for testing.
300 * @param sched scheduler to use
301 * @param timeout if this operation cannot be completed within the
302 * given period, call the continuation with an error code
303 * @param total number of daemons to start
304 * @param daemons array of 'total' entries to be initialized
305 * (array must already be allocated, will be filled)
306 * @param cont function to call when done
307 * @param cont_cls closure for cont
310 GNUNET_FS_TEST_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
311 struct GNUNET_TIME_Relative timeout,
313 struct GNUNET_FS_TestDaemon **daemons,
314 GNUNET_SCHEDULER_Task cont,
317 struct StartContext *sctx;
320 GNUNET_assert (total > 0);
321 sctx = GNUNET_malloc (sizeof (struct StartContext));
323 sctx->daemons = daemons;
326 sctx->cont_cls = cont_cls;
327 sctx->cfg = GNUNET_CONFIGURATION_create ();
329 GNUNET_CONFIGURATION_load (sctx->cfg,
330 "test_fs_lib_data.conf"))
333 GNUNET_CONFIGURATION_destroy (sctx->cfg);
335 GNUNET_SCHEDULER_add_continuation (sched,
338 GNUNET_SCHEDULER_REASON_TIMEOUT);
341 for (i=0;i<total;i++)
342 daemons[i] = GNUNET_malloc (sizeof (struct GNUNET_FS_TestDaemon));
343 sctx->group = GNUNET_TESTING_daemons_start (sched,
350 sctx->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
357 struct ConnectContext
359 struct GNUNET_SCHEDULER_Handle *sched;
360 GNUNET_SCHEDULER_Task cont;
366 notify_connection (void *cls,
367 const struct GNUNET_PeerIdentity *first,
368 const struct GNUNET_PeerIdentity *second,
369 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
370 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
371 struct GNUNET_TESTING_Daemon *first_daemon,
372 struct GNUNET_TESTING_Daemon *second_daemon,
375 struct ConnectContext *cc = cls;
378 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
379 _("Failed to connect peers: %s\n"),
381 GNUNET_SCHEDULER_add_continuation (cc->sched,
385 ? GNUNET_SCHEDULER_REASON_TIMEOUT
386 : GNUNET_SCHEDULER_REASON_PREREQ_DONE);
392 * Connect two daemons for testing.
394 * @param sched scheduler to use
395 * @param daemon1 first daemon to connect
396 * @param daemon2 second first daemon to connect
397 * @param timeout if this operation cannot be completed within the
398 * given period, call the continuation with an error code
399 * @param cont function to call when done
400 * @param cont_cls closure for cont
403 GNUNET_FS_TEST_daemons_connect (struct GNUNET_SCHEDULER_Handle *sched,
404 struct GNUNET_FS_TestDaemon *daemon1,
405 struct GNUNET_FS_TestDaemon *daemon2,
406 struct GNUNET_TIME_Relative timeout,
407 GNUNET_SCHEDULER_Task cont,
410 struct ConnectContext *ncc;
412 ncc = GNUNET_malloc (sizeof (struct ConnectContext));
415 ncc->cont_cls = cont_cls;
416 GNUNET_TESTING_daemons_connect (daemon1->daemon,
425 * Stop daemons used for testing.
427 * @param sched scheduler to use
428 * @param timeout if this operation cannot be completed within the
429 * given period, call the continuation with an error code
430 * @param total number of daemons to stop
431 * @param daemons array with the daemons (values will be clobbered)
432 * @param cont function to call when done
433 * @param cont_cls closure for cont
436 GNUNET_FS_TEST_daemons_stop (struct GNUNET_SCHEDULER_Handle *sched,
438 struct GNUNET_FS_TestDaemon **daemons)
442 GNUNET_assert (total > 0);
443 GNUNET_TESTING_daemons_stop (daemons[0]->group);
444 for (i=0;i<total;i++)
446 GNUNET_FS_stop (daemons[i]->fs);
447 GNUNET_CONFIGURATION_destroy (daemons[i]->cfg);
448 GNUNET_free (daemons[i]);
455 publish_timeout (void *cls,
456 const struct GNUNET_SCHEDULER_TaskContext *tc)
458 struct GNUNET_FS_TestDaemon *daemon = cls;
459 GNUNET_FS_TEST_UriContinuation cont;
461 cont = daemon->publish_cont;
462 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
463 daemon->publish_cont = NULL;
464 GNUNET_FS_publish_stop (daemon->publish_context);
465 daemon->publish_context = NULL;
466 cont (daemon->publish_cont_cls,
472 file_generator (void *cls,
478 struct GNUNET_FS_TestDaemon *daemon = cls;
482 for (pos=0;pos<max;pos++)
483 cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % (255 - (offset / 1024 / 32)));
490 * Publish a file at the given daemon.
492 * @param sched scheduler to use
493 * @param daemon where to publish
494 * @param timeout if this operation cannot be completed within the
495 * given period, call the continuation with an error code
496 * @param anonymity option for publication
497 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
498 * GNUNET_SYSERR for simulation
499 * @param size size of the file to publish
500 * @param seed seed to use for file generation
501 * @param verbose how verbose to be in reporting
502 * @param cont function to call when done
503 * @param cont_cls closure for cont
506 GNUNET_FS_TEST_publish (struct GNUNET_SCHEDULER_Handle *sched,
507 struct GNUNET_FS_TestDaemon *daemon,
508 struct GNUNET_TIME_Relative timeout,
513 unsigned int verbose,
514 GNUNET_FS_TEST_UriContinuation cont,
517 GNUNET_assert (daemon->publish_cont == NULL);
518 struct GNUNET_FS_FileInformation *fi;
520 daemon->publish_cont = cont;
521 daemon->publish_cont_cls = cont_cls;
522 daemon->publish_seed = seed;
523 daemon->verbose = verbose;
524 daemon->publish_sched = sched;
525 fi = GNUNET_FS_file_information_create_from_reader (daemon,
534 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
535 daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
538 GNUNET_FS_PUBLISH_OPTION_NONE);
539 daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
547 download_timeout (void *cls,
548 const struct GNUNET_SCHEDULER_TaskContext *tc)
550 struct GNUNET_FS_TestDaemon *daemon = cls;
552 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
553 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
554 daemon->download_cont,
555 daemon->download_cont_cls,
556 GNUNET_SCHEDULER_REASON_TIMEOUT);
557 daemon->download_cont = NULL;
558 daemon->download_sched = NULL;
563 * Perform test download.
565 * @param sched scheduler to use
566 * @param daemon which peer to download from
567 * @param timeout if this operation cannot be completed within the
568 * given period, call the continuation with an error code
569 * @param anonymity option for download
570 * @param seed used for file validation
571 * @param verbose how verbose to be in reporting
572 * @param cont function to call when done
573 * @param cont_cls closure for cont
576 GNUNET_FS_TEST_download (struct GNUNET_SCHEDULER_Handle *sched,
577 struct GNUNET_FS_TestDaemon *daemon,
578 struct GNUNET_TIME_Relative timeout,
581 const struct GNUNET_FS_Uri *uri,
582 unsigned int verbose,
583 GNUNET_SCHEDULER_Task cont,
588 GNUNET_assert (daemon->download_cont == NULL);
589 size = GNUNET_FS_uri_chk_get_file_size (uri);
590 daemon->verbose = verbose;
591 daemon->download_sched = sched;
592 daemon->download_cont = cont;
593 daemon->download_cont_cls = cont_cls;
594 daemon->download_seed = seed;
595 daemon->download_context = GNUNET_FS_download_start (daemon->fs,
602 GNUNET_FS_DOWNLOAD_OPTION_NONE,
605 daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
611 /* end of test_fs_lib.c */