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_PROGRESS:
196 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
197 "Download at %llu/%llu bytes\n",
198 (unsigned long long) info->value.download.completed,
199 (unsigned long long) info->value.download.size);
201 case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
202 GNUNET_SCHEDULER_cancel (daemon->download_sched,
203 daemon->download_timeout_task);
204 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
205 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
208 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
210 /* FIXME: monitor data correctness during download progress */
211 /* FIXME: do performance reports given sufficient verbosity */
212 /* FIXME: advance timeout task to "immediate" on error */
223 struct GNUNET_SCHEDULER_Handle *sched;
224 struct GNUNET_TIME_Relative timeout;
227 struct GNUNET_FS_TestDaemon **daemons;
228 GNUNET_SCHEDULER_Task cont;
230 struct GNUNET_TESTING_PeerGroup *group;
231 struct GNUNET_CONFIGURATION_Handle *cfg;
232 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
237 notify_running (void *cls,
238 const struct GNUNET_PeerIdentity *id,
239 const struct GNUNET_CONFIGURATION_Handle *cfg,
240 struct GNUNET_TESTING_Daemon *d,
243 struct StartContext *sctx = cls;
248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
249 _("Failed to start daemon: %s\n"),
253 GNUNET_assert (sctx->have < sctx->total);
254 sctx->daemons[sctx->have]->cfg = GNUNET_CONFIGURATION_dup (cfg);
255 sctx->daemons[sctx->have]->group = sctx->group;
256 sctx->daemons[sctx->have]->daemon = d;
257 sctx->daemons[sctx->have]->id = *id;
259 if (sctx->have == sctx->total)
261 GNUNET_SCHEDULER_add_continuation (sctx->sched,
264 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
265 GNUNET_CONFIGURATION_destroy (sctx->cfg);
266 GNUNET_SCHEDULER_cancel (sctx->sched,
268 for (i=0;i<sctx->total;i++)
269 sctx->daemons[i]->fs = GNUNET_FS_start (sctx->sched,
270 sctx->daemons[i]->cfg,
274 GNUNET_FS_FLAGS_NONE,
275 GNUNET_FS_OPTIONS_END);
282 start_timeout (void *cls,
283 const struct GNUNET_SCHEDULER_TaskContext *tc)
285 struct StartContext *sctx = cls;
288 GNUNET_TESTING_daemons_stop (sctx->group);
289 for (i=0;i<sctx->total;i++)
292 GNUNET_CONFIGURATION_destroy (sctx->daemons[i]->cfg);
293 GNUNET_free (sctx->daemons[i]);
295 GNUNET_CONFIGURATION_destroy (sctx->cfg);
296 GNUNET_SCHEDULER_add_continuation (sctx->sched,
299 GNUNET_SCHEDULER_REASON_TIMEOUT);
305 * Start daemons for testing.
307 * @param sched scheduler to use
308 * @param timeout if this operation cannot be completed within the
309 * given period, call the continuation with an error code
310 * @param total number of daemons to start
311 * @param daemons array of 'total' entries to be initialized
312 * (array must already be allocated, will be filled)
313 * @param cont function to call when done
314 * @param cont_cls closure for cont
317 GNUNET_FS_TEST_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
318 struct GNUNET_TIME_Relative timeout,
320 struct GNUNET_FS_TestDaemon **daemons,
321 GNUNET_SCHEDULER_Task cont,
324 struct StartContext *sctx;
327 GNUNET_assert (total > 0);
328 sctx = GNUNET_malloc (sizeof (struct StartContext));
330 sctx->daemons = daemons;
333 sctx->cont_cls = cont_cls;
334 sctx->cfg = GNUNET_CONFIGURATION_create ();
336 GNUNET_CONFIGURATION_load (sctx->cfg,
337 "fs_test_lib_data.conf"))
340 GNUNET_CONFIGURATION_destroy (sctx->cfg);
342 GNUNET_SCHEDULER_add_continuation (sched,
345 GNUNET_SCHEDULER_REASON_TIMEOUT);
348 for (i=0;i<total;i++)
349 daemons[i] = GNUNET_malloc (sizeof (struct GNUNET_FS_TestDaemon));
350 sctx->group = GNUNET_TESTING_daemons_start (sched,
357 sctx->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
364 struct ConnectContext
366 struct GNUNET_SCHEDULER_Handle *sched;
367 GNUNET_SCHEDULER_Task cont;
373 notify_connection (void *cls,
374 const struct GNUNET_PeerIdentity *first,
375 const struct GNUNET_PeerIdentity *second,
376 const struct GNUNET_CONFIGURATION_Handle *first_cfg,
377 const struct GNUNET_CONFIGURATION_Handle *second_cfg,
378 struct GNUNET_TESTING_Daemon *first_daemon,
379 struct GNUNET_TESTING_Daemon *second_daemon,
382 struct ConnectContext *cc = cls;
385 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
386 _("Failed to connect peers: %s\n"),
388 GNUNET_SCHEDULER_add_continuation (cc->sched,
392 ? GNUNET_SCHEDULER_REASON_TIMEOUT
393 : GNUNET_SCHEDULER_REASON_PREREQ_DONE);
399 * Connect two daemons for testing.
401 * @param sched scheduler to use
402 * @param daemon1 first daemon to connect
403 * @param daemon2 second first daemon to connect
404 * @param timeout if this operation cannot be completed within the
405 * given period, call the continuation with an error code
406 * @param cont function to call when done
407 * @param cont_cls closure for cont
410 GNUNET_FS_TEST_daemons_connect (struct GNUNET_SCHEDULER_Handle *sched,
411 struct GNUNET_FS_TestDaemon *daemon1,
412 struct GNUNET_FS_TestDaemon *daemon2,
413 struct GNUNET_TIME_Relative timeout,
414 GNUNET_SCHEDULER_Task cont,
417 struct ConnectContext *ncc;
419 ncc = GNUNET_malloc (sizeof (struct ConnectContext));
422 ncc->cont_cls = cont_cls;
423 GNUNET_TESTING_daemons_connect (daemon1->daemon,
432 * Stop daemons used for testing.
434 * @param sched scheduler to use
435 * @param total number of daemons to stop
436 * @param daemons array with the daemons (values will be clobbered)
439 GNUNET_FS_TEST_daemons_stop (struct GNUNET_SCHEDULER_Handle *sched,
441 struct GNUNET_FS_TestDaemon **daemons)
445 GNUNET_assert (total > 0);
446 GNUNET_TESTING_daemons_stop (daemons[0]->group);
447 for (i=0;i<total;i++)
449 GNUNET_FS_stop (daemons[i]->fs);
450 GNUNET_CONFIGURATION_destroy (daemons[i]->cfg);
451 GNUNET_free (daemons[i]);
458 publish_timeout (void *cls,
459 const struct GNUNET_SCHEDULER_TaskContext *tc)
461 struct GNUNET_FS_TestDaemon *daemon = cls;
462 GNUNET_FS_TEST_UriContinuation cont;
464 cont = daemon->publish_cont;
465 daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
466 daemon->publish_cont = NULL;
467 GNUNET_FS_publish_stop (daemon->publish_context);
468 daemon->publish_context = NULL;
469 cont (daemon->publish_cont_cls,
475 file_generator (void *cls,
481 struct GNUNET_FS_TestDaemon *daemon = cls;
486 for (pos=0;pos<max;pos++)
488 mod = (255 - (offset / 1024 / 32));
491 cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);
499 * Publish a file at the given daemon.
501 * @param sched scheduler to use
502 * @param daemon where to publish
503 * @param timeout if this operation cannot be completed within the
504 * given period, call the continuation with an error code
505 * @param anonymity option for publication
506 * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
507 * GNUNET_SYSERR for simulation
508 * @param size size of the file to publish
509 * @param seed seed to use for file generation
510 * @param verbose how verbose to be in reporting
511 * @param cont function to call when done
512 * @param cont_cls closure for cont
515 GNUNET_FS_TEST_publish (struct GNUNET_SCHEDULER_Handle *sched,
516 struct GNUNET_FS_TestDaemon *daemon,
517 struct GNUNET_TIME_Relative timeout,
522 unsigned int verbose,
523 GNUNET_FS_TEST_UriContinuation cont,
526 GNUNET_assert (daemon->publish_cont == NULL);
527 struct GNUNET_FS_FileInformation *fi;
529 daemon->publish_cont = cont;
530 daemon->publish_cont_cls = cont_cls;
531 daemon->publish_seed = seed;
532 daemon->verbose = verbose;
533 daemon->publish_sched = sched;
534 fi = GNUNET_FS_file_information_create_from_reader (daemon,
543 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
544 daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
547 GNUNET_FS_PUBLISH_OPTION_NONE);
548 daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
556 download_timeout (void *cls,
557 const struct GNUNET_SCHEDULER_TaskContext *tc)
559 struct GNUNET_FS_TestDaemon *daemon = cls;
561 daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
562 GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
563 daemon->download_context = NULL;
564 GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
565 daemon->download_cont,
566 daemon->download_cont_cls,
567 GNUNET_SCHEDULER_REASON_TIMEOUT);
568 daemon->download_cont = NULL;
569 daemon->download_sched = NULL;
574 * Perform test download.
576 * @param sched scheduler to use
577 * @param daemon which peer to download from
578 * @param timeout if this operation cannot be completed within the
579 * given period, call the continuation with an error code
580 * @param anonymity option for download
581 * @param seed used for file validation
582 * @param uri URI of file to download (CHK/LOC only)
583 * @param verbose how verbose to be in reporting
584 * @param cont function to call when done
585 * @param cont_cls closure for cont
588 GNUNET_FS_TEST_download (struct GNUNET_SCHEDULER_Handle *sched,
589 struct GNUNET_FS_TestDaemon *daemon,
590 struct GNUNET_TIME_Relative timeout,
593 const struct GNUNET_FS_Uri *uri,
594 unsigned int verbose,
595 GNUNET_SCHEDULER_Task cont,
600 GNUNET_assert (daemon->download_cont == NULL);
601 size = GNUNET_FS_uri_chk_get_file_size (uri);
602 daemon->verbose = verbose;
603 daemon->download_sched = sched;
604 daemon->download_cont = cont;
605 daemon->download_cont_cls = cont_cls;
606 daemon->download_seed = seed;
607 daemon->download_context = GNUNET_FS_download_start (daemon->fs,
614 GNUNET_FS_DOWNLOAD_OPTION_NONE,
617 daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
623 /* end of test_fs_lib.c */