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