indentation
[oweals/gnunet.git] / src / fs / fs_test_lib.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010, 2011 Christian Grothoff (and other contributing authors)
4
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 3, or (at your
8      option) any later version.
9
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.
14
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.
19 */
20
21 /**
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
28  */
29 #include "platform.h"
30 #include "fs.h"
31 #include "fs_test_lib.h"
32 #include "gnunet_testing_lib.h"
33
34 #define CONNECT_ATTEMPTS 4
35
36 #define CONTENT_LIFETIME GNUNET_TIME_UNIT_HOURS
37
38 /**
39  * Handle for a daemon started for testing FS.
40  */
41 struct GNUNET_FS_TestDaemon
42 {
43
44   /**
45    * Global configuration, only stored in first test daemon,
46    * otherwise NULL.
47    */
48   struct GNUNET_CONFIGURATION_Handle *gcfg;
49
50   /**
51    * Handle to the file sharing context using this daemon.
52    */
53   struct GNUNET_FS_Handle *fs;
54
55   /**
56    * Handle to the daemon via testing.
57    */
58   struct GNUNET_TESTING_Daemon *daemon;
59
60   /**
61    * Note that 'group' will be the same value for all of the
62    * daemons started jointly.
63    */
64   struct GNUNET_TESTING_PeerGroup *group;
65
66   /**
67    * Configuration for accessing this peer.
68    */
69   struct GNUNET_CONFIGURATION_Handle *cfg;
70
71   /**
72    * ID of this peer.
73    */
74   struct GNUNET_PeerIdentity id;
75
76   /**
77    * Function to call when upload is done.
78    */
79   GNUNET_FS_TEST_UriContinuation publish_cont;
80
81   /**
82    * Closure for publish_cont.
83    */
84   void *publish_cont_cls;
85
86   /**
87    * Task to abort publishing (timeout).
88    */
89   GNUNET_SCHEDULER_TaskIdentifier publish_timeout_task;
90
91   /**
92    * Seed for file generation.
93    */
94   uint32_t publish_seed;
95
96   /**
97    * Context for current publishing operation.
98    */
99   struct GNUNET_FS_PublishContext *publish_context;
100
101   /**
102    * Result URI.
103    */
104   struct GNUNET_FS_Uri *publish_uri;
105
106   /**
107    * Name of the temporary file used, or NULL for none.
108    */
109   char *publish_tmp_file;
110
111   /**
112    * Function to call when download is done.
113    */
114   GNUNET_SCHEDULER_Task download_cont;
115
116   /**
117    * Closure for download_cont.
118    */
119   void *download_cont_cls;
120
121   /**
122    * Seed for download verification.
123    */
124   uint32_t download_seed;
125
126   /**
127    * Task to abort downloading (timeout).
128    */
129   GNUNET_SCHEDULER_TaskIdentifier download_timeout_task;
130
131   /**
132    * Context for current download operation.
133    */
134   struct GNUNET_FS_DownloadContext *download_context;
135
136   /**
137    * Verbosity level of the current operation.
138    */
139   int verbose;
140
141
142 };
143
144 /**
145  * Check whether peers successfully shut down.
146  */
147 static void
148 shutdown_callback (void *cls, const char *emsg)
149 {
150   struct GNUNET_CONFIGURATION_Handle *gcfg = cls;
151
152   if (emsg != NULL)
153   {
154     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
155                 "Shutdown of peers failed: %s\n", emsg);
156   }
157   else
158   {
159 #if VERBOSE
160     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All peers successfully shut down!\n");
161 #endif
162   }
163   if (gcfg != NULL)
164     GNUNET_CONFIGURATION_destroy (gcfg);
165 }
166
167
168 static void
169 report_uri (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
170 {
171   struct GNUNET_FS_TestDaemon *daemon = cls;
172   GNUNET_FS_TEST_UriContinuation cont;
173   struct GNUNET_FS_Uri *uri;
174
175   GNUNET_FS_publish_stop (daemon->publish_context);
176   daemon->publish_context = NULL;
177   cont = daemon->publish_cont;
178   daemon->publish_cont = NULL;
179   uri = daemon->publish_uri;
180   cont (daemon->publish_cont_cls, uri);
181   GNUNET_FS_uri_destroy (uri);
182 }
183
184
185 static void
186 report_success (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
187 {
188   struct GNUNET_FS_TestDaemon *daemon = cls;
189
190   GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
191   daemon->download_context = NULL;
192   GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
193                                      daemon->download_cont_cls,
194                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
195   daemon->download_cont = NULL;
196 }
197
198
199 static void *
200 progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
201 {
202   struct GNUNET_FS_TestDaemon *daemon = cls;
203
204   switch (info->status)
205   {
206   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
207     GNUNET_SCHEDULER_cancel (daemon->publish_timeout_task);
208     daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
209     daemon->publish_uri =
210         GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
211     GNUNET_SCHEDULER_add_continuation (&report_uri, daemon,
212                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
213     break;
214   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
215     if (daemon->verbose)
216       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
217                   "Publishing at %llu/%llu bytes\n",
218                   (unsigned long long) info->value.publish.completed,
219                   (unsigned long long) info->value.publish.size);
220     break;
221   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
222     if (daemon->verbose)
223       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
224                   "Download at %llu/%llu bytes\n",
225                   (unsigned long long) info->value.download.completed,
226                   (unsigned long long) info->value.download.size);
227     break;
228   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
229     GNUNET_SCHEDULER_cancel (daemon->download_timeout_task);
230     daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
231     GNUNET_SCHEDULER_add_continuation (&report_success,
232                                        daemon,
233                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
234     break;
235   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
236   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
237     break;
238     /* FIXME: monitor data correctness during download progress */
239     /* FIXME: do performance reports given sufficient verbosity */
240     /* FIXME: advance timeout task to "immediate" on error */
241   default:
242     break;
243   }
244   return NULL;
245 }
246
247
248 struct StartContext
249 {
250   struct GNUNET_TIME_Relative timeout;
251   unsigned int total;
252   unsigned int have;
253   struct GNUNET_FS_TestDaemon **daemons;
254   GNUNET_SCHEDULER_Task cont;
255   void *cont_cls;
256   struct GNUNET_TESTING_PeerGroup *group;
257   struct GNUNET_CONFIGURATION_Handle *cfg;
258   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
259 };
260
261
262 static void
263 notify_running (void *cls,
264                 const struct GNUNET_PeerIdentity *id,
265                 const struct GNUNET_CONFIGURATION_Handle *cfg,
266                 struct GNUNET_TESTING_Daemon *d, const char *emsg)
267 {
268   struct StartContext *sctx = cls;
269   unsigned int i;
270
271   if (emsg != NULL)
272   {
273     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
274                 _("Failed to start daemon: %s\n"), emsg);
275     return;
276   }
277   i = 0;
278   while (i < sctx->total)
279   {
280     if (GNUNET_TESTING_daemon_get (sctx->group, i) == d)
281       break;
282     i++;
283   }
284   GNUNET_assert (i < sctx->total);
285   GNUNET_assert (sctx->have < sctx->total);
286   GNUNET_assert (sctx->daemons[i]->cfg == NULL);
287   sctx->daemons[i]->cfg = GNUNET_CONFIGURATION_dup (cfg);
288   sctx->daemons[i]->group = sctx->group;
289   sctx->daemons[i]->daemon = d;
290   sctx->daemons[i]->id = *id;
291   sctx->have++;
292   if (sctx->have == sctx->total)
293   {
294     GNUNET_SCHEDULER_add_continuation (sctx->cont,
295                                        sctx->cont_cls,
296                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
297     sctx->daemons[0]->gcfg = sctx->cfg;
298     GNUNET_SCHEDULER_cancel (sctx->timeout_task);
299     for (i = 0; i < sctx->total; i++)
300     {
301       sctx->daemons[i]->fs = GNUNET_FS_start (sctx->daemons[i]->cfg,
302                                               "<tester>",
303                                               &progress_cb,
304                                               sctx->daemons[i],
305                                               GNUNET_FS_FLAGS_NONE,
306                                               GNUNET_FS_OPTIONS_END);
307     }
308     GNUNET_free (sctx);
309   }
310 }
311
312
313 static void
314 start_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
315 {
316   struct StartContext *sctx = cls;
317   unsigned int i;
318
319   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
320               "Timeout while trying to start daemons\n");
321   GNUNET_TESTING_daemons_stop (sctx->group,
322                                GNUNET_TIME_relative_multiply
323                                (GNUNET_TIME_UNIT_SECONDS, 30),
324                                &shutdown_callback, NULL);
325   for (i = 0; i < sctx->total; i++)
326   {
327     if (i < sctx->have)
328       GNUNET_CONFIGURATION_destroy (sctx->daemons[i]->cfg);
329     GNUNET_free (sctx->daemons[i]);
330     sctx->daemons[i] = NULL;
331   }
332   GNUNET_CONFIGURATION_destroy (sctx->cfg);
333   GNUNET_SCHEDULER_add_continuation (sctx->cont,
334                                      sctx->cont_cls,
335                                      GNUNET_SCHEDULER_REASON_TIMEOUT);
336   GNUNET_free (sctx);
337 }
338
339
340 /**
341  * Start daemons for testing.
342  *
343  * @param template_cfg_file configuration template to use
344  * @param timeout if this operation cannot be completed within the
345  *                given period, call the continuation with an error code
346  * @param total number of daemons to start
347  * @param daemons array of 'total' entries to be initialized
348  *                (array must already be allocated, will be filled)
349  * @param cont function to call when done
350  * @param cont_cls closure for cont
351  */
352 void
353 GNUNET_FS_TEST_daemons_start (const char *template_cfg_file,
354                               struct GNUNET_TIME_Relative timeout,
355                               unsigned int total,
356                               struct GNUNET_FS_TestDaemon **daemons,
357                               GNUNET_SCHEDULER_Task cont, void *cont_cls)
358 {
359   struct StartContext *sctx;
360   unsigned int i;
361
362   GNUNET_assert (total > 0);
363   sctx = GNUNET_malloc (sizeof (struct StartContext));
364   sctx->daemons = daemons;
365   sctx->total = total;
366   sctx->cont = cont;
367   sctx->cont_cls = cont_cls;
368   sctx->cfg = GNUNET_CONFIGURATION_create ();
369   if (GNUNET_OK != GNUNET_CONFIGURATION_load (sctx->cfg, template_cfg_file))
370   {
371     GNUNET_break (0);
372     GNUNET_CONFIGURATION_destroy (sctx->cfg);
373     GNUNET_free (sctx);
374     GNUNET_SCHEDULER_add_continuation (cont,
375                                        cont_cls,
376                                        GNUNET_SCHEDULER_REASON_TIMEOUT);
377     return;
378   }
379   for (i = 0; i < total; i++)
380     daemons[i] = GNUNET_malloc (sizeof (struct GNUNET_FS_TestDaemon));
381   sctx->group = GNUNET_TESTING_daemons_start (sctx->cfg, total, total,  /* Outstanding connections */
382                                               total,    /* Outstanding ssh connections */
383                                               timeout,
384                                               NULL,
385                                               NULL,
386                                               &notify_running,
387                                               sctx, NULL, NULL, NULL);
388   sctx->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
389                                                      &start_timeout, sctx);
390 }
391
392
393 struct ConnectContext
394 {
395   GNUNET_SCHEDULER_Task cont;
396   void *cont_cls;
397 };
398
399
400 /**
401  * Prototype of a function that will be called whenever
402  * two daemons are connected by the testing library.
403  *
404  * @param cls closure
405  * @param first peer id for first daemon
406  * @param second peer id for the second daemon
407  * @param distance distance between the connected peers
408  * @param first_cfg config for the first daemon
409  * @param second_cfg config for the second daemon
410  * @param first_daemon handle for the first daemon
411  * @param second_daemon handle for the second daemon
412  * @param emsg error message (NULL on success)
413  */
414 static void
415 notify_connection (void *cls,
416                    const struct GNUNET_PeerIdentity *first,
417                    const struct GNUNET_PeerIdentity *second,
418                    uint32_t distance,
419                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
420                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
421                    struct GNUNET_TESTING_Daemon *first_daemon,
422                    struct GNUNET_TESTING_Daemon *second_daemon,
423                    const char *emsg)
424 {
425   struct ConnectContext *cc = cls;
426
427   if (emsg != NULL)
428     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
429                 "Failed to connect peers: %s\n", emsg);
430   GNUNET_SCHEDULER_add_continuation (cc->cont,
431                                      cc->cont_cls,
432                                      (emsg != NULL)
433                                      ? GNUNET_SCHEDULER_REASON_TIMEOUT
434                                      : GNUNET_SCHEDULER_REASON_PREREQ_DONE);
435   GNUNET_free (cc);
436 }
437
438
439 /**
440  * Connect two daemons for testing.
441  *
442  * @param daemon1 first daemon to connect
443  * @param daemon2 second first daemon to connect
444  * @param timeout if this operation cannot be completed within the
445  *                given period, call the continuation with an error code
446  * @param cont function to call when done
447  * @param cont_cls closure for cont
448  */
449 void
450 GNUNET_FS_TEST_daemons_connect (struct GNUNET_FS_TestDaemon *daemon1,
451                                 struct GNUNET_FS_TestDaemon *daemon2,
452                                 struct GNUNET_TIME_Relative timeout,
453                                 GNUNET_SCHEDULER_Task cont, void *cont_cls)
454 {
455   struct ConnectContext *ncc;
456
457   ncc = GNUNET_malloc (sizeof (struct ConnectContext));
458   ncc->cont = cont;
459   ncc->cont_cls = cont_cls;
460   GNUNET_TESTING_daemons_connect (daemon1->daemon,
461                                   daemon2->daemon,
462                                   timeout,
463                                   CONNECT_ATTEMPTS,
464                                   GNUNET_YES, &notify_connection, ncc);
465 }
466
467
468 /**
469  * Obtain peer configuration used for testing.
470  *
471  * @param daemons array with the daemons
472  * @param off which configuration to get
473  * @return peer configuration
474  */
475 const struct GNUNET_CONFIGURATION_Handle *
476 GNUNET_FS_TEST_get_configuration (struct GNUNET_FS_TestDaemon **daemons,
477                                   unsigned int off)
478 {
479   return daemons[off]->cfg;
480 }
481
482 /**
483  * Obtain peer group used for testing.
484  *
485  * @param daemons array with the daemons (must contain at least one)
486  * @return peer group
487  */
488 struct GNUNET_TESTING_PeerGroup *
489 GNUNET_FS_TEST_get_group (struct GNUNET_FS_TestDaemon **daemons)
490 {
491   return daemons[0]->group;
492 }
493
494
495 /**
496  * Stop daemons used for testing.
497  *
498  * @param total number of daemons to stop
499  * @param daemons array with the daemons (values will be clobbered)
500  */
501 void
502 GNUNET_FS_TEST_daemons_stop (unsigned int total,
503                              struct GNUNET_FS_TestDaemon **daemons)
504 {
505   unsigned int i;
506   struct GNUNET_TESTING_PeerGroup *pg;
507   struct GNUNET_CONFIGURATION_Handle *gcfg;
508   struct GNUNET_FS_TestDaemon *daemon;
509
510   GNUNET_assert (total > 0);
511   GNUNET_assert (daemons[0] != NULL);
512   pg = daemons[0]->group;
513   gcfg = daemons[0]->gcfg;
514   for (i = 0; i < total; i++)
515   {
516     daemon = daemons[i];
517     if (daemon->download_timeout_task != GNUNET_SCHEDULER_NO_TASK)
518     {
519       GNUNET_SCHEDULER_cancel (daemon->download_timeout_task);
520       daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
521     }
522     if (daemon->publish_timeout_task != GNUNET_SCHEDULER_NO_TASK)
523     {
524       GNUNET_SCHEDULER_cancel (daemon->publish_timeout_task);
525       daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
526     }
527     if (NULL != daemon->download_context)
528     {
529       GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
530       daemon->download_context = NULL;
531     }
532     if (daemon->fs != NULL)
533       GNUNET_FS_stop (daemon->fs);
534     if (daemon->cfg != NULL)
535       GNUNET_CONFIGURATION_destroy (daemon->cfg);
536     if (NULL != daemon->publish_tmp_file)
537     {
538       GNUNET_break (GNUNET_OK ==
539                     GNUNET_DISK_directory_remove (daemon->publish_tmp_file));
540       GNUNET_free (daemon->publish_tmp_file);
541       daemon->publish_tmp_file = NULL;
542     }
543     GNUNET_free (daemon);
544     daemons[i] = NULL;
545   }
546   GNUNET_TESTING_daemons_stop (pg,
547                                GNUNET_TIME_relative_multiply
548                                (GNUNET_TIME_UNIT_SECONDS, 30),
549                                &shutdown_callback, gcfg);
550 }
551
552
553 static void
554 publish_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
555 {
556   struct GNUNET_FS_TestDaemon *daemon = cls;
557   GNUNET_FS_TEST_UriContinuation cont;
558
559   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
560               "Timeout while trying to publish data\n");
561   cont = daemon->publish_cont;
562   daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
563   daemon->publish_cont = NULL;
564   GNUNET_FS_publish_stop (daemon->publish_context);
565   daemon->publish_context = NULL;
566   cont (daemon->publish_cont_cls, NULL);
567 }
568
569
570 static size_t
571 file_generator (void *cls, uint64_t offset, size_t max, void *buf, char **emsg)
572 {
573   struct GNUNET_FS_TestDaemon *daemon = cls;
574   uint64_t pos;
575   uint8_t *cbuf = buf;
576   int mod;
577
578   if (emsg != NULL)
579     *emsg = NULL;
580   if (buf == NULL)
581     return 0;
582   for (pos = 0; pos < 8; pos++)
583     cbuf[pos] = (uint8_t) (offset >> pos * 8);
584   for (pos = 8; pos < max; pos++)
585   {
586     mod = (255 - (offset / 1024 / 32));
587     if (mod == 0)
588       mod = 1;
589     cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);
590   }
591   return max;
592 }
593
594
595
596 /**
597  * Publish a file at the given daemon.
598  *
599  * @param daemon where to publish
600  * @param timeout if this operation cannot be completed within the
601  *                given period, call the continuation with an error code
602  * @param anonymity option for publication
603  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
604  *                GNUNET_SYSERR for simulation
605  * @param size size of the file to publish
606  * @param seed seed to use for file generation
607  * @param verbose how verbose to be in reporting
608  * @param cont function to call when done
609  * @param cont_cls closure for cont
610  */
611 void
612 GNUNET_FS_TEST_publish (struct GNUNET_FS_TestDaemon *daemon,
613                         struct GNUNET_TIME_Relative timeout,
614                         uint32_t anonymity,
615                         int do_index,
616                         uint64_t size,
617                         uint32_t seed,
618                         unsigned int verbose,
619                         GNUNET_FS_TEST_UriContinuation cont, void *cont_cls)
620 {
621   struct GNUNET_FS_FileInformation *fi;
622   struct GNUNET_DISK_FileHandle *fh;
623   char *emsg;
624   uint64_t off;
625   char buf[DBLOCK_SIZE];
626   size_t bsize;
627   struct GNUNET_FS_BlockOptions bo;
628
629   GNUNET_assert (daemon->publish_cont == NULL);
630   daemon->publish_cont = cont;
631   daemon->publish_cont_cls = cont_cls;
632   daemon->publish_seed = seed;
633   daemon->verbose = verbose;
634   bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
635   bo.anonymity_level = anonymity;
636   bo.content_priority = 42;
637   bo.replication_level = 1;
638   if (GNUNET_YES == do_index)
639   {
640     GNUNET_assert (daemon->publish_tmp_file == NULL);
641     daemon->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
642     GNUNET_assert (daemon->publish_tmp_file != NULL);
643     fh = GNUNET_DISK_file_open (daemon->publish_tmp_file,
644                                 GNUNET_DISK_OPEN_WRITE |
645                                 GNUNET_DISK_OPEN_CREATE,
646                                 GNUNET_DISK_PERM_USER_READ |
647                                 GNUNET_DISK_PERM_USER_WRITE);
648     GNUNET_assert (NULL != fh);
649     off = 0;
650     while (off < size)
651     {
652       bsize = GNUNET_MIN (sizeof (buf), size - off);
653       emsg = NULL;
654       GNUNET_assert (bsize == file_generator (daemon, off, bsize, buf, &emsg));
655       GNUNET_assert (emsg == NULL);
656       GNUNET_assert (bsize == GNUNET_DISK_file_write (fh, buf, bsize));
657       off += bsize;
658     }
659     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
660     fi = GNUNET_FS_file_information_create_from_file (daemon->fs,
661                                                       daemon,
662                                                       daemon->publish_tmp_file,
663                                                       NULL, NULL,
664                                                       do_index, &bo);
665   }
666   else
667   {
668     fi = GNUNET_FS_file_information_create_from_reader (daemon->fs,
669                                                         daemon,
670                                                         size,
671                                                         &file_generator,
672                                                         daemon,
673                                                         NULL,
674                                                         NULL, do_index, &bo);
675   }
676   daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
677                                                      fi,
678                                                      NULL, NULL, NULL,
679                                                      GNUNET_FS_PUBLISH_OPTION_NONE);
680   daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
681                                                                &publish_timeout,
682                                                                daemon);
683 }
684
685
686 static void
687 download_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
688 {
689   struct GNUNET_FS_TestDaemon *daemon = cls;
690
691   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
692               "Timeout while trying to download file\n");
693   daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
694   GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
695   daemon->download_context = NULL;
696   GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
697                                      daemon->download_cont_cls,
698                                      GNUNET_SCHEDULER_REASON_TIMEOUT);
699   daemon->download_cont = NULL;
700 }
701
702
703 /**
704  * Perform test download.
705  *
706  * @param daemon which peer to download from
707  * @param timeout if this operation cannot be completed within the
708  *                given period, call the continuation with an error code
709  * @param anonymity option for download
710  * @param seed used for file validation
711  * @param uri URI of file to download (CHK/LOC only)
712  * @param verbose how verbose to be in reporting
713  * @param cont function to call when done
714  * @param cont_cls closure for cont
715  */
716 void
717 GNUNET_FS_TEST_download (struct GNUNET_FS_TestDaemon *daemon,
718                          struct GNUNET_TIME_Relative timeout,
719                          uint32_t anonymity,
720                          uint32_t seed,
721                          const struct GNUNET_FS_Uri *uri,
722                          unsigned int verbose,
723                          GNUNET_SCHEDULER_Task cont, void *cont_cls)
724 {
725   uint64_t size;
726
727   GNUNET_assert (daemon->download_cont == NULL);
728   size = GNUNET_FS_uri_chk_get_file_size (uri);
729   daemon->verbose = verbose;
730   daemon->download_cont = cont;
731   daemon->download_cont_cls = cont_cls;
732   daemon->download_seed = seed;
733   daemon->download_context = GNUNET_FS_download_start (daemon->fs,
734                                                        uri,
735                                                        NULL, NULL,
736                                                        NULL,
737                                                        0,
738                                                        size,
739                                                        anonymity,
740                                                        GNUNET_FS_DOWNLOAD_OPTION_NONE,
741                                                        NULL, NULL);
742   daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
743                                                                 &download_timeout,
744                                                                 daemon);
745 }
746
747 /* end of test_fs_lib.c */