changing datastore API to support replication level --- not fully implemented --...
[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,
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                                   GNUNET_YES,
486                                   &notify_connection,
487                                   ncc);
488 }
489
490
491 /**
492  * Obtain peer configuration used for testing.
493  *
494  * @param daemons array with the daemons
495  * @param off which configuration to get
496  * @return peer configuration
497  */
498 const struct GNUNET_CONFIGURATION_Handle *
499 GNUNET_FS_TEST_get_configuration (struct GNUNET_FS_TestDaemon **daemons,
500                                   unsigned int off)
501 {
502   return daemons[off]->cfg;  
503 }
504
505 /**
506  * Obtain peer group used for testing.
507  *
508  * @param daemons array with the daemons (must contain at least one)
509  * @return peer group
510  */
511 struct GNUNET_TESTING_PeerGroup *
512 GNUNET_FS_TEST_get_group (struct GNUNET_FS_TestDaemon **daemons)
513 {
514   return daemons[0]->group;  
515 }
516
517
518 /**
519  * Stop daemons used for testing.
520  *
521  * @param total number of daemons to stop
522  * @param daemons array with the daemons (values will be clobbered)
523  */
524 void
525 GNUNET_FS_TEST_daemons_stop (unsigned int total,
526                              struct GNUNET_FS_TestDaemon **daemons)
527 {
528   unsigned int i;
529   struct GNUNET_TESTING_PeerGroup *pg;
530   struct GNUNET_CONFIGURATION_Handle *gcfg;
531
532   GNUNET_assert (total > 0);
533   GNUNET_assert (daemons[0] != NULL);
534   pg = daemons[0]->group;
535   gcfg = daemons[0]->gcfg;
536   for (i=0;i<total;i++)
537     {
538       if (daemons[i]->fs != NULL)
539         GNUNET_FS_stop (daemons[i]->fs);
540       if (daemons[i]->cfg != NULL)
541         GNUNET_CONFIGURATION_destroy (daemons[i]->cfg);
542       if (NULL != daemons[i]->publish_tmp_file)
543         {
544           GNUNET_break (GNUNET_OK ==
545                         GNUNET_DISK_directory_remove (daemons[i]->publish_tmp_file));
546           GNUNET_free (daemons[i]->publish_tmp_file);
547           daemons[i]->publish_tmp_file = NULL;
548         }
549       GNUNET_free (daemons[i]);
550       daemons[i] = NULL;
551     }  
552   GNUNET_TESTING_daemons_stop (pg, 
553                                GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30), 
554                                &shutdown_callback, 
555                                gcfg);
556 }
557
558
559 static void
560 publish_timeout (void *cls,
561                  const struct GNUNET_SCHEDULER_TaskContext *tc)
562 {
563   struct GNUNET_FS_TestDaemon *daemon = cls;
564   GNUNET_FS_TEST_UriContinuation cont;
565
566   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
567               "Timeout while trying to publish data\n");
568   cont = daemon->publish_cont;
569   daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
570   daemon->publish_cont = NULL;
571   GNUNET_FS_publish_stop (daemon->publish_context);
572   daemon->publish_context = NULL;
573   cont (daemon->publish_cont_cls,
574         NULL);
575 }
576
577
578 static size_t
579 file_generator (void *cls, 
580                 uint64_t offset,
581                 size_t max, 
582                 void *buf,
583                 char **emsg)
584 {
585   struct GNUNET_FS_TestDaemon *daemon = cls;
586   uint64_t pos;
587   uint8_t *cbuf = buf;
588   int mod;
589
590   if (emsg != NULL)
591     *emsg = NULL;
592   if (buf == NULL)
593     return 0;
594   for (pos=0;pos<8;pos++)
595     cbuf[pos] = (uint8_t) (offset >> pos*8);
596   for (pos=8;pos<max;pos++)
597     {
598       mod = (255 - (offset / 1024 / 32));
599       if (mod == 0)
600         mod = 1;
601       cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);  
602     }
603   return max;
604 }
605
606
607
608 /**
609  * Publish a file at the given daemon.
610  *
611  * @param daemon where to publish
612  * @param timeout if this operation cannot be completed within the
613  *                given period, call the continuation with an error code
614  * @param anonymity option for publication
615  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
616  *                GNUNET_SYSERR for simulation
617  * @param size size of the file to publish
618  * @param seed seed to use for file generation
619  * @param verbose how verbose to be in reporting
620  * @param cont function to call when done
621  * @param cont_cls closure for cont
622  */
623 void
624 GNUNET_FS_TEST_publish (struct GNUNET_FS_TestDaemon *daemon,
625                         struct GNUNET_TIME_Relative timeout,
626                         uint32_t anonymity,
627                         int do_index,
628                         uint64_t size,
629                         uint32_t seed,
630                         unsigned int verbose,
631                         GNUNET_FS_TEST_UriContinuation cont,
632                         void *cont_cls)
633 {
634   struct GNUNET_FS_FileInformation *fi;
635   struct GNUNET_DISK_FileHandle *fh;
636   char *emsg;
637   uint64_t off;
638   char buf[DBLOCK_SIZE];
639   size_t bsize;
640   struct GNUNET_FS_BlockOptions bo;
641  
642   GNUNET_assert (daemon->publish_cont == NULL);
643   daemon->publish_cont = cont;
644   daemon->publish_cont_cls = cont_cls;
645   daemon->publish_seed = seed;
646   daemon->verbose = verbose;
647   bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
648   bo.anonymity_level = anonymity;
649   bo.content_priority = 42;
650   bo.replication_level = 1;
651   if (GNUNET_YES == do_index)
652     {
653       GNUNET_assert (daemon->publish_tmp_file == NULL);
654       daemon->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
655       GNUNET_assert (daemon->publish_tmp_file != NULL);
656       fh = GNUNET_DISK_file_open (daemon->publish_tmp_file,
657                                   GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE,
658                                   GNUNET_DISK_PERM_USER_READ | GNUNET_DISK_PERM_USER_WRITE);
659       GNUNET_assert (NULL != fh);
660       off = 0;      
661       while (off < size)
662         {
663           bsize = GNUNET_MIN (sizeof (buf),
664                               size - off);
665           emsg = NULL;
666           GNUNET_assert (bsize ==
667                          file_generator (daemon,
668                                          off,
669                                          bsize,
670                                          buf,
671                                          &emsg));
672           GNUNET_assert (emsg == NULL);
673           GNUNET_assert (bsize ==
674                          GNUNET_DISK_file_write (fh,
675                                                  buf,
676                                                  bsize));
677           off += bsize;
678         }
679       GNUNET_assert (GNUNET_OK ==
680                      GNUNET_DISK_file_close (fh));
681       fi = GNUNET_FS_file_information_create_from_file (daemon->fs,
682                                                         daemon,                                               
683                                                         daemon->publish_tmp_file,
684                                                         NULL, NULL,
685                                                         do_index,
686                                                         &bo);
687     }
688   else
689     {
690       fi = GNUNET_FS_file_information_create_from_reader (daemon->fs,
691                                                           daemon,                                                     
692                                                           size,
693                                                           &file_generator,
694                                                           daemon,
695                                                           NULL,
696                                                           NULL,
697                                                           do_index,
698                                                           &bo);
699     }
700   daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
701                                                      fi,
702                                                      NULL, NULL, NULL,
703                                                      GNUNET_FS_PUBLISH_OPTION_NONE);
704   daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
705                                                                &publish_timeout,
706                                                                daemon);
707 }
708
709
710 static void
711 download_timeout (void *cls,
712                   const struct GNUNET_SCHEDULER_TaskContext *tc)
713 {
714   struct GNUNET_FS_TestDaemon *daemon = cls;
715
716   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
717               "Timeout while trying to download file\n");
718   daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
719   GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
720   daemon->download_context = NULL;
721   GNUNET_SCHEDULER_add_continuation (daemon->download_cont,
722                                      daemon->download_cont_cls,
723                                      GNUNET_SCHEDULER_REASON_TIMEOUT);
724   daemon->download_cont = NULL;
725 }
726
727
728 /**
729  * Perform test download.
730  *
731  * @param daemon which peer to download from
732  * @param timeout if this operation cannot be completed within the
733  *                given period, call the continuation with an error code
734  * @param anonymity option for download
735  * @param seed used for file validation
736  * @param uri URI of file to download (CHK/LOC only)
737  * @param verbose how verbose to be in reporting
738  * @param cont function to call when done
739  * @param cont_cls closure for cont
740  */
741 void
742 GNUNET_FS_TEST_download (struct GNUNET_FS_TestDaemon *daemon,
743                          struct GNUNET_TIME_Relative timeout,
744                          uint32_t anonymity,
745                          uint32_t seed,
746                          const struct GNUNET_FS_Uri *uri,
747                          unsigned int verbose,
748                          GNUNET_SCHEDULER_Task cont,
749                          void *cont_cls)
750 {
751   uint64_t size;
752  
753   GNUNET_assert (daemon->download_cont == NULL);
754   size = GNUNET_FS_uri_chk_get_file_size (uri);
755   daemon->verbose = verbose;
756   daemon->download_cont = cont;
757   daemon->download_cont_cls = cont_cls;
758   daemon->download_seed = seed;  
759   daemon->download_context = GNUNET_FS_download_start (daemon->fs,
760                                                        uri,
761                                                        NULL, NULL,
762                                                        NULL,
763                                                        0,
764                                                        size,
765                                                        anonymity,
766                                                        GNUNET_FS_DOWNLOAD_OPTION_NONE,
767                                                        NULL,
768                                                        NULL);
769   daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (timeout,
770                                                                 &download_timeout,
771                                                                 daemon);
772 }
773
774 /* end of test_fs_lib.c */