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