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"
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 "fs_test_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 total number of daemons to stop
429 * @param daemons array with the daemons (values will be clobbered)
432 GNUNET_FS_TEST_daemons_stop (struct GNUNET_SCHEDULER_Handle *sched,
434 struct GNUNET_FS_TestDaemon **daemons)
438 GNUNET_assert (total > 0);
439 GNUNET_TESTING_daemons_stop (daemons[0]->group);
440 for (i=0;i<total;i++)
442 GNUNET_FS_stop (daemons[i]->fs);
443 GNUNET_CONFIGURATION_destroy (daemons[i]->cfg);
444 GNUNET_free (daemons[i]);
451 publish_timeout (void *cls,
452 const struct GNUNET_SCHEDULER_TaskContext *tc)
454 struct GNUNET_FS_TestDaemon *daemon = cls;
455 GNUNET_FS_TEST_UriContinuation cont;
457 cont = daemon->publish_cont;
458 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
459 daemon->publish_cont = NULL;
460 GNUNET_FS_publish_stop (daemon->publish_context);
461 daemon->publish_context = NULL;
462 cont (daemon->publish_cont_cls,
468 file_generator (void *cls,
474 struct GNUNET_FS_TestDaemon *daemon = cls;
478 for (pos=0;pos<max;pos++)
479 cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % (255 - (offset / 1024 / 32)));
486 * Publish a file at the given daemon.
488 * @param sched scheduler to use
489 * @param daemon where to publish
490 * @param timeout if this operation cannot be completed within the
491 * given period, call the continuation with an error code
492 * @param anonymity option for publication
493 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
494 * GNUNET_SYSERR for simulation
495 * @param size size of the file to publish
496 * @param seed seed to use for file generation
497 * @param verbose how verbose to be in reporting
498 * @param cont function to call when done
499 * @param cont_cls closure for cont
502 GNUNET_FS_TEST_publish (struct GNUNET_SCHEDULER_Handle *sched,
503 struct GNUNET_FS_TestDaemon *daemon,
504 struct GNUNET_TIME_Relative timeout,
509 unsigned int verbose,
510 GNUNET_FS_TEST_UriContinuation cont,
513 GNUNET_assert (daemon->publish_cont == NULL);
514 struct GNUNET_FS_FileInformation *fi;
516 daemon->publish_cont = cont;
517 daemon->publish_cont_cls = cont_cls;
518 daemon->publish_seed = seed;
519 daemon->verbose = verbose;
520 daemon->publish_sched = sched;
521 fi = GNUNET_FS_file_information_create_from_reader (daemon,
530 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
531 daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
534 GNUNET_FS_PUBLISH_OPTION_NONE);
535 daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
543 download_timeout (void *cls,
544 const struct GNUNET_SCHEDULER_TaskContext *tc)
546 struct GNUNET_FS_TestDaemon *daemon = cls;
548 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
549 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
550 daemon->download_cont,
551 daemon->download_cont_cls,
552 GNUNET_SCHEDULER_REASON_TIMEOUT);
553 daemon->download_cont = NULL;
554 daemon->download_sched = NULL;
559 * Perform test download.
561 * @param sched scheduler to use
562 * @param daemon which peer to download from
563 * @param timeout if this operation cannot be completed within the
564 * given period, call the continuation with an error code
565 * @param anonymity option for download
566 * @param seed used for file validation
567 * @param uri URI of file to download (CHK/LOC only)
568 * @param verbose how verbose to be in reporting
569 * @param cont function to call when done
570 * @param cont_cls closure for cont
573 GNUNET_FS_TEST_download (struct GNUNET_SCHEDULER_Handle *sched,
574 struct GNUNET_FS_TestDaemon *daemon,
575 struct GNUNET_TIME_Relative timeout,
578 const struct GNUNET_FS_Uri *uri,
579 unsigned int verbose,
580 GNUNET_SCHEDULER_Task cont,
585 GNUNET_assert (daemon->download_cont == NULL);
586 size = GNUNET_FS_uri_chk_get_file_size (uri);
587 daemon->verbose = verbose;
588 daemon->download_sched = sched;
589 daemon->download_cont = cont;
590 daemon->download_cont_cls = cont_cls;
591 daemon->download_seed = seed;
592 daemon->download_context = GNUNET_FS_download_start (daemon->fs,
599 GNUNET_FS_DOWNLOAD_OPTION_NONE,
602 daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
608 /* end of test_fs_lib.c */