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