Testing api change and related test case fixes, remember retry task and clean up...
[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                                               total, /* Outstanding connections */
398                                               total, /* Outstanding ssh connections */
399                                               timeout,
400                                               NULL,
401                                               NULL,
402                                               &notify_running,
403                                               sctx,
404                                               NULL, NULL,
405                                               NULL);
406   sctx->timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
407                                                      &start_timeout,
408                                                      sctx);
409 }
410
411
412 struct ConnectContext
413 {
414   GNUNET_SCHEDULER_Task cont;
415   void *cont_cls;
416 };
417
418
419 /**
420  * Prototype of a function that will be called whenever
421  * two daemons are connected by the testing library.
422  *
423  * @param cls closure
424  * @param first peer id for first daemon
425  * @param second peer id for the second daemon
426  * @param distance distance between the connected peers
427  * @param first_cfg config for the first daemon
428  * @param second_cfg config for the second daemon
429  * @param first_daemon handle for the first daemon
430  * @param second_daemon handle for the second daemon
431  * @param emsg error message (NULL on success)
432  */
433 static void
434 notify_connection (void *cls,
435                    const struct GNUNET_PeerIdentity *first,
436                    const struct GNUNET_PeerIdentity *second,
437                    uint32_t distance,
438                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
439                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
440                    struct GNUNET_TESTING_Daemon *first_daemon,
441                    struct GNUNET_TESTING_Daemon *second_daemon,
442                    const char *emsg)
443 {
444   struct ConnectContext *cc = cls;
445   
446   if (emsg != NULL)
447     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
448                 "Failed to connect peers: %s\n",
449                 emsg);
450   GNUNET_SCHEDULER_add_continuation (cc->cont,
451                                      cc->cont_cls,
452                                      (emsg != NULL) 
453                                      ? GNUNET_SCHEDULER_REASON_TIMEOUT 
454                                      : GNUNET_SCHEDULER_REASON_PREREQ_DONE);
455   GNUNET_free (cc);
456 }
457
458
459 /**
460  * Connect two daemons for testing.
461  *
462  * @param daemon1 first daemon to connect
463  * @param daemon2 second first daemon to connect
464  * @param timeout if this operation cannot be completed within the
465  *                given period, call the continuation with an error code
466  * @param cont function to call when done
467  * @param cont_cls closure for cont
468  */
469 void
470 GNUNET_FS_TEST_daemons_connect (struct GNUNET_FS_TestDaemon *daemon1,
471                                 struct GNUNET_FS_TestDaemon *daemon2,
472                                 struct GNUNET_TIME_Relative timeout,
473                                 GNUNET_SCHEDULER_Task cont,
474                                 void *cont_cls)
475 {
476   struct ConnectContext *ncc;
477
478   ncc = GNUNET_malloc (sizeof (struct ConnectContext));
479   ncc->cont = cont;
480   ncc->cont_cls = cont_cls;
481   GNUNET_TESTING_daemons_connect (daemon1->daemon,
482                                   daemon2->daemon,
483                                   timeout,
484                                   CONNECT_ATTEMPTS,
485                                   &notify_connection,
486                                   ncc);
487 }
488
489
490 /**
491  * Obtain peer configuration used for testing.
492  *
493  * @param daemons array with the daemons
494  * @param off which configuration to get
495  * @return peer configuration
496  */
497 const struct GNUNET_CONFIGURATION_Handle *
498 GNUNET_FS_TEST_get_configuration (struct GNUNET_FS_TestDaemon **daemons,
499                                   unsigned int off)
500 {
501   return daemons[off]->cfg;  
502 }
503
504 /**
505  * Obtain peer group used for testing.
506  *
507  * @param daemons array with the daemons (must contain at least one)
508  * @return peer group
509  */
510 struct GNUNET_TESTING_PeerGroup *
511 GNUNET_FS_TEST_get_group (struct GNUNET_FS_TestDaemon **daemons)
512 {
513   return daemons[0]->group;  
514 }
515
516
517 /**
518  * Stop daemons used for testing.
519  *
520  * @param total number of daemons to stop
521  * @param daemons array with the daemons (values will be clobbered)
522  */
523 void
524 GNUNET_FS_TEST_daemons_stop (unsigned int total,
525                              struct GNUNET_FS_TestDaemon **daemons)
526 {
527   unsigned int i;
528   struct GNUNET_TESTING_PeerGroup *pg;
529   struct GNUNET_CONFIGURATION_Handle *gcfg;
530
531   GNUNET_assert (total > 0);
532   GNUNET_assert (daemons[0] != NULL);
533   pg = daemons[0]->group;
534   gcfg = daemons[0]->gcfg;
535   for (i=0;i<total;i++)
536     {
537       if (daemons[i]->fs != NULL)
538         GNUNET_FS_stop (daemons[i]->fs);
539       if (daemons[i]->cfg != NULL)
540         GNUNET_CONFIGURATION_destroy (daemons[i]->cfg);
541       if (NULL != daemons[i]->publish_tmp_file)
542         {
543           GNUNET_break (GNUNET_OK ==
544                         GNUNET_DISK_directory_remove (daemons[i]->publish_tmp_file));
545           GNUNET_free (daemons[i]->publish_tmp_file);
546           daemons[i]->publish_tmp_file = NULL;
547         }
548       GNUNET_free (daemons[i]);
549       daemons[i] = NULL;
550     }  
551   GNUNET_TESTING_daemons_stop (pg, 
552                                GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30), 
553                                &shutdown_callback, 
554                                gcfg);
555 }
556
557
558 static void
559 publish_timeout (void *cls,
560                  const struct GNUNET_SCHEDULER_TaskContext *tc)
561 {
562   struct GNUNET_FS_TestDaemon *daemon = cls;
563   GNUNET_FS_TEST_UriContinuation cont;
564
565   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
566               "Timeout while trying to publish data\n");
567   cont = daemon->publish_cont;
568   daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
569   daemon->publish_cont = NULL;
570   GNUNET_FS_publish_stop (daemon->publish_context);
571   daemon->publish_context = NULL;
572   cont (daemon->publish_cont_cls,
573         NULL);
574 }
575
576
577 static size_t
578 file_generator (void *cls, 
579                 uint64_t offset,
580                 size_t max, 
581                 void *buf,
582                 char **emsg)
583 {
584   struct GNUNET_FS_TestDaemon *daemon = cls;
585   uint64_t pos;
586   uint8_t *cbuf = buf;
587   int mod;
588
589   if (emsg != NULL)
590     *emsg = NULL;
591   if (buf == NULL)
592     return 0;
593   for (pos=0;pos<8;pos++)
594     cbuf[pos] = (uint8_t) (offset >> pos*8);
595   for (pos=8;pos<max;pos++)
596     {
597       mod = (255 - (offset / 1024 / 32));
598       if (mod == 0)
599         mod = 1;
600       cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);  
601     }
602   return max;
603 }
604
605
606
607 /**
608  * Publish a file at the given daemon.
609  *
610  * @param daemon where to publish
611  * @param timeout if this operation cannot be completed within the
612  *                given period, call the continuation with an error code
613  * @param anonymity option for publication
614  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
615  *                GNUNET_SYSERR for simulation
616  * @param size size of the file to publish
617  * @param seed seed to use for file generation
618  * @param verbose how verbose to be in reporting
619  * @param cont function to call when done
620  * @param cont_cls closure for cont
621  */
622 void
623 GNUNET_FS_TEST_publish (struct GNUNET_FS_TestDaemon *daemon,
624                         struct GNUNET_TIME_Relative timeout,
625                         uint32_t anonymity,
626                         int do_index,
627                         uint64_t size,
628                         uint32_t seed,
629                         unsigned int verbose,
630                         GNUNET_FS_TEST_UriContinuation cont,
631                         void *cont_cls)
632 {
633   struct GNUNET_FS_FileInformation *fi;
634   struct GNUNET_DISK_FileHandle *fh;
635   char *emsg;
636   uint64_t off;
637   char buf[DBLOCK_SIZE];
638   size_t bsize;
639
640   GNUNET_assert (daemon->publish_cont == NULL);
641   daemon->publish_cont = cont;
642   daemon->publish_cont_cls = cont_cls;
643   daemon->publish_seed = seed;
644   daemon->verbose = verbose;
645   if (GNUNET_YES == do_index)
646     {
647       GNUNET_assert (daemon->publish_tmp_file == NULL);
648       daemon->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
649       GNUNET_assert (daemon->publish_tmp_file != NULL);
650       fh = GNUNET_DISK_file_open (daemon->publish_tmp_file,
651                                   GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
652                                   GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
653       GNUNET_assert (NULL != fh);
654       off = 0;      
655       while (off < size)
656         {
657           bsize = GNUNET_MIN (sizeof (buf),
658                               size - off);
659           emsg = NULL;
660           GNUNET_assert (bsize ==
661                          file_generator (daemon,
662                                          off,
663                                          bsize,
664                                          buf,
665                                          &emsg));
666           GNUNET_assert (emsg == NULL);
667           GNUNET_assert (bsize ==
668                          GNUNET_DISK_file_write (fh,
669                                                  buf,
670                                                  bsize));
671           off += bsize;
672         }
673       GNUNET_assert (GNUNET_OK ==
674                      GNUNET_DISK_file_close (fh));
675       fi = GNUNET_FS_file_information_create_from_file (daemon->fs,
676                                                         daemon,                                               
677                                                         daemon->publish_tmp_file,
678                                                         NULL, NULL,
679                                                         do_index,
680                                                         anonymity,
681                                                         42 /* priority */,
682                                                         GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME));
683     }
684   else
685     {
686       fi = GNUNET_FS_file_information_create_from_reader (daemon->fs,
687                                                           daemon,                                                     
688                                                           size,
689                                                           &file_generator,
690                                                           daemon,
691                                                           NULL,
692                                                           NULL,
693                                                           do_index,
694                                                           anonymity,
695                                                           42 /* priority */,
696                                                           GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME));
697     }
698   daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
699                                                      fi,
700                                                      NULL, NULL, NULL,
701                                                      GNUNET_FS_PUBLISH_OPTION_NONE);
702   daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
703                                                                &publish_timeout,
704                                                                daemon);
705 }
706
707
708 static void
709 download_timeout (void *cls,
710                   const struct GNUNET_SCHEDULER_TaskContext *tc)
711 {
712   struct GNUNET_FS_TestDaemon *daemon = cls;
713
714   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
715               "Timeout while trying to download file\n");
716   daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
717   GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
718   daemon->download_context = NULL;
719   GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
720                                      daemon->download_cont_cls,
721                                      GNUNET_SCHEDULER_REASON_TIMEOUT);
722   daemon->download_cont = NULL;
723 }
724
725
726 /**
727  * Perform test download.
728  *
729  * @param daemon which peer to download from
730  * @param timeout if this operation cannot be completed within the
731  *                given period, call the continuation with an error code
732  * @param anonymity option for download
733  * @param seed used for file validation
734  * @param uri URI of file to download (CHK/LOC only)
735  * @param verbose how verbose to be in reporting
736  * @param cont function to call when done
737  * @param cont_cls closure for cont
738  */
739 void
740 GNUNET_FS_TEST_download (struct GNUNET_FS_TestDaemon *daemon,
741                          struct GNUNET_TIME_Relative timeout,
742                          uint32_t anonymity,
743                          uint32_t seed,
744                          const struct GNUNET_FS_Uri *uri,
745                          unsigned int verbose,
746                          GNUNET_SCHEDULER_Task cont,
747                          void *cont_cls)
748 {
749   uint64_t size;
750  
751   GNUNET_assert (daemon->download_cont == NULL);
752   size = GNUNET_FS_uri_chk_get_file_size (uri);
753   daemon->verbose = verbose;
754   daemon->download_cont = cont;
755   daemon->download_cont_cls = cont_cls;
756   daemon->download_seed = seed;  
757   daemon->download_context = GNUNET_FS_download_start (daemon->fs,
758                                                        uri,
759                                                        NULL, NULL,
760                                                        NULL,
761                                                        0,
762                                                        size,
763                                                        anonymity,
764                                                        GNUNET_FS_DOWNLOAD_OPTION_NONE,
765                                                        NULL,
766                                                        NULL);
767   daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
768                                                                 &download_timeout,
769                                                                 daemon);
770 }
771
772 /* end of test_fs_lib.c */