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