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