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