report port numbers
[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
232 struct StartContext
233 {
234   struct GNUNET_SCHEDULER_Handle *sched;
235   struct GNUNET_TIME_Relative timeout;
236   unsigned int total;
237   unsigned int have;
238   struct GNUNET_FS_TestDaemon **daemons;
239   GNUNET_SCHEDULER_Task cont;
240   void *cont_cls;
241   struct GNUNET_TESTING_PeerGroup *group;
242   struct GNUNET_CONFIGURATION_Handle *cfg;
243   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
244 };
245
246
247 static void 
248 notify_running (void *cls,
249                 const struct GNUNET_PeerIdentity *id,
250                 const struct GNUNET_CONFIGURATION_Handle *cfg,
251                 struct GNUNET_TESTING_Daemon *d,
252                 const char *emsg)
253 {
254   struct StartContext *sctx = cls;
255   unsigned int i;
256   unsigned long long fsport;
257
258   if (emsg != NULL)
259     {
260       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
261                   _("Failed to start daemon: %s\n"),
262                   emsg);
263       return;
264     }
265   GNUNET_assert (sctx->have < sctx->total);
266   sctx->daemons[sctx->have]->cfg = GNUNET_CONFIGURATION_dup (cfg);
267   sctx->daemons[sctx->have]->group = sctx->group;
268   sctx->daemons[sctx->have]->daemon = d;
269   sctx->daemons[sctx->have]->id = *id;
270   sctx->have++;
271   if (sctx->have == sctx->total)
272     {
273       GNUNET_SCHEDULER_add_continuation (sctx->sched,
274                                          sctx->cont,
275                                          sctx->cont_cls,
276                                          GNUNET_SCHEDULER_REASON_PREREQ_DONE);
277       GNUNET_CONFIGURATION_destroy (sctx->cfg);
278       GNUNET_SCHEDULER_cancel (sctx->sched,
279                                sctx->timeout_task);
280       for (i=0;i<sctx->total;i++)
281         {
282           fsport = 0;
283           GNUNET_break (GNUNET_OK ==
284                         GNUNET_CONFIGURATION_get_value_number (sctx->daemons[i]->cfg,
285                                                                "fs",
286                                                                "PORT",
287                                                                &fsport));
288           GNUNET_log (GNUNET_ERROR_TYPE_INFO,
289                       _("Testing connects to port %llu for peer %u\n"),
290                       fsport,
291                       i);                       
292           sctx->daemons[i]->fs = GNUNET_FS_start (sctx->sched,
293                                                   sctx->daemons[i]->cfg,
294                                                   "<tester>",
295                                                   &progress_cb,
296                                                   sctx->daemons[i],
297                                                   GNUNET_FS_FLAGS_NONE,
298                                                   GNUNET_FS_OPTIONS_END);
299         }
300       GNUNET_free (sctx);
301     }
302 }
303
304
305 static void
306 start_timeout (void *cls,
307                const struct GNUNET_SCHEDULER_TaskContext *tc)
308 {
309   struct StartContext *sctx = cls;
310   unsigned int i;
311
312   GNUNET_TESTING_daemons_stop (sctx->group, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30));
313   for (i=0;i<sctx->total;i++)
314     {
315       if (i < sctx->have)
316         GNUNET_CONFIGURATION_destroy (sctx->daemons[i]->cfg);
317       GNUNET_free (sctx->daemons[i]);
318     }
319   GNUNET_CONFIGURATION_destroy (sctx->cfg);
320   GNUNET_SCHEDULER_add_continuation (sctx->sched,
321                                      sctx->cont,
322                                      sctx->cont_cls,
323                                      GNUNET_SCHEDULER_REASON_TIMEOUT);
324   GNUNET_free (sctx);
325 }
326
327
328 /**
329  * Start daemons for testing.
330  *
331  * @param sched scheduler to use
332  * @param template_cfg_file configuration template to use
333  * @param timeout if this operation cannot be completed within the
334  *                given period, call the continuation with an error code
335  * @param total number of daemons to start
336  * @param daemons array of 'total' entries to be initialized
337  *                (array must already be allocated, will be filled)
338  * @param cont function to call when done
339  * @param cont_cls closure for cont
340  */
341 void
342 GNUNET_FS_TEST_daemons_start (struct GNUNET_SCHEDULER_Handle *sched,
343                               const char *template_cfg_file,
344                               struct GNUNET_TIME_Relative timeout,
345                               unsigned int total,
346                               struct GNUNET_FS_TestDaemon **daemons,
347                               GNUNET_SCHEDULER_Task cont,
348                               void *cont_cls)
349 {
350   struct StartContext *sctx;
351   unsigned int i;
352
353   GNUNET_assert (total > 0);
354   sctx = GNUNET_malloc (sizeof (struct StartContext));
355   sctx->sched = sched;
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 !=
362       GNUNET_CONFIGURATION_load (sctx->cfg,
363                                  template_cfg_file))
364     {
365       GNUNET_break (0);
366       GNUNET_CONFIGURATION_destroy (sctx->cfg);
367       GNUNET_free (sctx);
368       GNUNET_SCHEDULER_add_continuation (sched,
369                                          cont,
370                                          cont_cls,
371                                          GNUNET_SCHEDULER_REASON_TIMEOUT);
372       return;
373     }
374   for (i=0;i<total;i++)
375     daemons[i] = GNUNET_malloc (sizeof (struct GNUNET_FS_TestDaemon));
376   sctx->group = GNUNET_TESTING_daemons_start (sched,
377                                               sctx->cfg,
378                                               total,
379                                               timeout,
380                                               NULL,
381                                               NULL,
382                                               &notify_running,
383                                               sctx,
384                                               NULL, NULL,
385                                               NULL);
386   sctx->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
387                                                      timeout,
388                                                      &start_timeout,
389                                                      sctx);
390 }
391
392
393 struct ConnectContext
394 {
395   struct GNUNET_SCHEDULER_Handle *sched;
396   GNUNET_SCHEDULER_Task cont;
397   void *cont_cls;
398 };
399
400
401 static void
402 notify_connection (void *cls,
403                    const struct GNUNET_PeerIdentity *first,
404                    const struct GNUNET_PeerIdentity *second,
405                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
406                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
407                    struct GNUNET_TESTING_Daemon *first_daemon,
408                    struct GNUNET_TESTING_Daemon *second_daemon,
409                    const char *emsg)
410 {
411   struct ConnectContext *cc = cls;
412   
413   if (emsg != NULL)
414     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
415                 _("Failed to connect peers: %s\n"),
416                 emsg);
417   GNUNET_SCHEDULER_add_continuation (cc->sched,
418                                      cc->cont,
419                                      cc->cont_cls,
420                                      (emsg != NULL) 
421                                      ? GNUNET_SCHEDULER_REASON_TIMEOUT 
422                                      : GNUNET_SCHEDULER_REASON_PREREQ_DONE);
423   GNUNET_free (cc);
424 }
425
426
427 /**
428  * Connect two daemons for testing.
429  *
430  * @param sched scheduler to use
431  * @param daemon1 first daemon to connect
432  * @param daemon2 second first daemon to connect
433  * @param timeout if this operation cannot be completed within the
434  *                given period, call the continuation with an error code
435  * @param cont function to call when done
436  * @param cont_cls closure for cont
437  */
438 void
439 GNUNET_FS_TEST_daemons_connect (struct GNUNET_SCHEDULER_Handle *sched,
440                                 struct GNUNET_FS_TestDaemon *daemon1,
441                                 struct GNUNET_FS_TestDaemon *daemon2,
442                                 struct GNUNET_TIME_Relative timeout,
443                                 GNUNET_SCHEDULER_Task cont,
444                                 void *cont_cls)
445 {
446   struct ConnectContext *ncc;
447
448   ncc = GNUNET_malloc (sizeof (struct ConnectContext));
449   ncc->sched = sched;
450   ncc->cont = cont;
451   ncc->cont_cls = cont_cls;
452   GNUNET_TESTING_daemons_connect (daemon1->daemon,
453                                   daemon2->daemon,
454                                   timeout,
455                                   CONNECT_ATTEMPTS,
456                                   &notify_connection,
457                                   ncc);
458 }
459
460
461 /**
462  * Obtain peer group used for testing.
463  *
464  * @param daemons array with the daemons (must contain at least one)
465  * @return peer group
466  */
467 struct GNUNET_TESTING_PeerGroup *
468 GNUNET_FS_TEST_get_group (struct GNUNET_FS_TestDaemon **daemons)
469 {
470   return daemons[0]->group;  
471 }
472
473
474 /**
475  * Stop daemons used for testing.
476  *
477  * @param sched scheduler to use
478  * @param total number of daemons to stop
479  * @param daemons array with the daemons (values will be clobbered)
480  */
481 void
482 GNUNET_FS_TEST_daemons_stop (struct GNUNET_SCHEDULER_Handle *sched,
483                              unsigned int total,
484                              struct GNUNET_FS_TestDaemon **daemons)
485 {
486   unsigned int i;
487   struct GNUNET_TESTING_PeerGroup *pg;
488
489   GNUNET_assert (total > 0);
490   pg = daemons[0]->group;
491   for (i=0;i<total;i++)
492     {
493       GNUNET_FS_stop (daemons[i]->fs);
494       GNUNET_CONFIGURATION_destroy (daemons[i]->cfg);
495       GNUNET_free (daemons[i]);
496       daemons[i] = NULL;
497     }  
498   GNUNET_TESTING_daemons_stop (pg, GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 30));
499 }
500
501
502 static void
503 publish_timeout (void *cls,
504                  const struct GNUNET_SCHEDULER_TaskContext *tc)
505 {
506   struct GNUNET_FS_TestDaemon *daemon = cls;
507   GNUNET_FS_TEST_UriContinuation cont;
508   
509   cont = daemon->publish_cont;
510   daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
511   daemon->publish_cont = NULL;
512   GNUNET_FS_publish_stop (daemon->publish_context);
513   daemon->publish_context = NULL;
514   cont (daemon->publish_cont_cls,
515         NULL);
516 }
517
518
519 static size_t
520 file_generator (void *cls, 
521                 uint64_t offset,
522                 size_t max, 
523                 void *buf,
524                 char **emsg)
525 {
526   struct GNUNET_FS_TestDaemon *daemon = cls;
527   uint64_t pos;
528   uint8_t *cbuf = buf;
529   int mod;
530
531   for (pos=0;pos<max;pos++)
532     {
533       mod = (255 - (offset / 1024 / 32));
534       if (mod == 0)
535         mod = 1;
536       cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);  
537     }
538   return max;
539 }
540
541
542
543 /**
544  * Publish a file at the given daemon.
545  *
546  * @param sched scheduler to use
547  * @param daemon where to publish
548  * @param timeout if this operation cannot be completed within the
549  *                given period, call the continuation with an error code
550  * @param anonymity option for publication
551  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
552  *                GNUNET_SYSERR for simulation
553  * @param size size of the file to publish
554  * @param seed seed to use for file generation
555  * @param verbose how verbose to be in reporting
556  * @param cont function to call when done
557  * @param cont_cls closure for cont
558  */
559 void
560 GNUNET_FS_TEST_publish (struct GNUNET_SCHEDULER_Handle *sched,
561                         struct GNUNET_FS_TestDaemon *daemon,
562                         struct GNUNET_TIME_Relative timeout,
563                         uint32_t anonymity,
564                         int do_index,
565                         uint64_t size,
566                         uint32_t seed,
567                         unsigned int verbose,
568                         GNUNET_FS_TEST_UriContinuation cont,
569                         void *cont_cls)
570 {
571   GNUNET_assert (daemon->publish_cont == NULL);
572   struct GNUNET_FS_FileInformation *fi;
573
574   daemon->publish_cont = cont;
575   daemon->publish_cont_cls = cont_cls;
576   daemon->publish_seed = seed;
577   daemon->verbose = verbose;
578   daemon->publish_sched = sched;
579   fi = GNUNET_FS_file_information_create_from_reader (daemon->fs,
580                                                       daemon,                                                 
581                                                       size,
582                                                       &file_generator,
583                                                       daemon,
584                                                       NULL,
585                                                       NULL,
586                                                       do_index,
587                                                       anonymity,
588                                                       42 /* priority */,
589                                                       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
590   daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
591                                                      fi,
592                                                      NULL, NULL, NULL,
593                                                      GNUNET_FS_PUBLISH_OPTION_NONE);
594   daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
595                                                                timeout,
596                                                                &publish_timeout,
597                                                                daemon);
598 }
599
600
601 static void
602 download_timeout (void *cls,
603                   const struct GNUNET_SCHEDULER_TaskContext *tc)
604 {
605   struct GNUNET_FS_TestDaemon *daemon = cls;
606
607   daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
608   GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
609   daemon->download_context = NULL;
610   GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
611                                      daemon->download_cont,
612                                      daemon->download_cont_cls,
613                                      GNUNET_SCHEDULER_REASON_TIMEOUT);
614   daemon->download_cont = NULL;
615   daemon->download_sched = NULL;
616 }
617
618
619 /**
620  * Perform test download.
621  *
622  * @param sched scheduler to use
623  * @param daemon which peer to download from
624  * @param timeout if this operation cannot be completed within the
625  *                given period, call the continuation with an error code
626  * @param anonymity option for download
627  * @param seed used for file validation
628  * @param uri URI of file to download (CHK/LOC only)
629  * @param verbose how verbose to be in reporting
630  * @param cont function to call when done
631  * @param cont_cls closure for cont
632  */
633 void
634 GNUNET_FS_TEST_download (struct GNUNET_SCHEDULER_Handle *sched,
635                          struct GNUNET_FS_TestDaemon *daemon,
636                          struct GNUNET_TIME_Relative timeout,
637                          uint32_t anonymity,
638                          uint32_t seed,
639                          const struct GNUNET_FS_Uri *uri,
640                          unsigned int verbose,
641                          GNUNET_SCHEDULER_Task cont,
642                          void *cont_cls)
643 {
644   uint64_t size;
645  
646   GNUNET_assert (daemon->download_cont == NULL);
647   size = GNUNET_FS_uri_chk_get_file_size (uri);
648   daemon->verbose = verbose;
649   daemon->download_sched = sched;
650   daemon->download_cont = cont;
651   daemon->download_cont_cls = cont_cls;
652   daemon->download_seed = seed;  
653   daemon->download_context = GNUNET_FS_download_start (daemon->fs,
654                                                        uri,
655                                                        NULL, NULL,
656                                                        NULL,
657                                                        0,
658                                                        size,
659                                                        anonymity,
660                                                        GNUNET_FS_DOWNLOAD_OPTION_NONE,
661                                                        NULL,
662                                                        NULL);
663   daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
664                                                                 timeout,
665                                                                 &download_timeout,
666                                                                 daemon);
667 }
668
669 /* end of test_fs_lib.c */