towards fi synchronization
[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);
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                                               &notify_running,
358                                               sctx,
359                                               NULL, NULL,
360                                               NULL);
361   sctx->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
362                                                      timeout,
363                                                      &start_timeout,
364                                                      sctx);
365 }
366
367
368 struct ConnectContext
369 {
370   struct GNUNET_SCHEDULER_Handle *sched;
371   GNUNET_SCHEDULER_Task cont;
372   void *cont_cls;
373 };
374
375
376 static void
377 notify_connection (void *cls,
378                    const struct GNUNET_PeerIdentity *first,
379                    const struct GNUNET_PeerIdentity *second,
380                    const struct GNUNET_CONFIGURATION_Handle *first_cfg,
381                    const struct GNUNET_CONFIGURATION_Handle *second_cfg,
382                    struct GNUNET_TESTING_Daemon *first_daemon,
383                    struct GNUNET_TESTING_Daemon *second_daemon,
384                    const char *emsg)
385 {
386   struct ConnectContext *cc = cls;
387   
388   if (emsg != NULL)
389     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
390                 _("Failed to connect peers: %s\n"),
391                 emsg);
392   GNUNET_SCHEDULER_add_continuation (cc->sched,
393                                      cc->cont,
394                                      cc->cont_cls,
395                                      (emsg != NULL) 
396                                      ? GNUNET_SCHEDULER_REASON_TIMEOUT 
397                                      : GNUNET_SCHEDULER_REASON_PREREQ_DONE);
398   GNUNET_free (cc);
399 }
400
401
402 /**
403  * Connect two daemons for testing.
404  *
405  * @param sched scheduler to use
406  * @param daemon1 first daemon to connect
407  * @param daemon2 second first daemon to connect
408  * @param timeout if this operation cannot be completed within the
409  *                given period, call the continuation with an error code
410  * @param cont function to call when done
411  * @param cont_cls closure for cont
412  */
413 void
414 GNUNET_FS_TEST_daemons_connect (struct GNUNET_SCHEDULER_Handle *sched,
415                                 struct GNUNET_FS_TestDaemon *daemon1,
416                                 struct GNUNET_FS_TestDaemon *daemon2,
417                                 struct GNUNET_TIME_Relative timeout,
418                                 GNUNET_SCHEDULER_Task cont,
419                                 void *cont_cls)
420 {
421   struct ConnectContext *ncc;
422
423   ncc = GNUNET_malloc (sizeof (struct ConnectContext));
424   ncc->sched = sched;
425   ncc->cont = cont;
426   ncc->cont_cls = cont_cls;
427   GNUNET_TESTING_daemons_connect (daemon1->daemon,
428                                   daemon2->daemon,
429                                   timeout,
430                                   CONNECT_ATTEMPTS,
431                                   &notify_connection,
432                                   ncc);
433 }
434
435
436 /**
437  * Stop daemons used for testing.
438  *
439  * @param sched scheduler to use
440  * @param total number of daemons to stop
441  * @param daemons array with the daemons (values will be clobbered)
442  */
443 void
444 GNUNET_FS_TEST_daemons_stop (struct GNUNET_SCHEDULER_Handle *sched,
445                              unsigned int total,
446                              struct GNUNET_FS_TestDaemon **daemons)
447 {
448   unsigned int i;
449
450   GNUNET_assert (total > 0);
451   GNUNET_TESTING_daemons_stop (daemons[0]->group);
452   for (i=0;i<total;i++)
453     {
454       GNUNET_FS_stop (daemons[i]->fs);
455       GNUNET_CONFIGURATION_destroy (daemons[i]->cfg);
456       GNUNET_free (daemons[i]);
457       daemons[i] = NULL;
458     }  
459 }
460
461
462 static void
463 publish_timeout (void *cls,
464                  const struct GNUNET_SCHEDULER_TaskContext *tc)
465 {
466   struct GNUNET_FS_TestDaemon *daemon = cls;
467   GNUNET_FS_TEST_UriContinuation cont;
468   
469   cont = daemon->publish_cont;
470   daemon->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
471   daemon->publish_cont = NULL;
472   GNUNET_FS_publish_stop (daemon->publish_context);
473   daemon->publish_context = NULL;
474   cont (daemon->publish_cont_cls,
475         NULL);
476 }
477
478
479 static size_t
480 file_generator (void *cls, 
481                 uint64_t offset,
482                 size_t max, 
483                 void *buf,
484                 char **emsg)
485 {
486   struct GNUNET_FS_TestDaemon *daemon = cls;
487   uint64_t pos;
488   uint8_t *cbuf = buf;
489   int mod;
490
491   for (pos=0;pos<max;pos++)
492     {
493       mod = (255 - (offset / 1024 / 32));
494       if (mod == 0)
495         mod = 1;
496       cbuf[pos] = (uint8_t) ((offset * daemon->publish_seed) % mod);  
497     }
498   return max;
499 }
500
501
502
503 /**
504  * Publish a file at the given daemon.
505  *
506  * @param sched scheduler to use
507  * @param daemon where to publish
508  * @param timeout if this operation cannot be completed within the
509  *                given period, call the continuation with an error code
510  * @param anonymity option for publication
511  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
512  *                GNUNET_SYSERR for simulation
513  * @param size size of the file to publish
514  * @param seed seed to use for file generation
515  * @param verbose how verbose to be in reporting
516  * @param cont function to call when done
517  * @param cont_cls closure for cont
518  */
519 void
520 GNUNET_FS_TEST_publish (struct GNUNET_SCHEDULER_Handle *sched,
521                         struct GNUNET_FS_TestDaemon *daemon,
522                         struct GNUNET_TIME_Relative timeout,
523                         uint32_t anonymity,
524                         int do_index,
525                         uint64_t size,
526                         uint32_t seed,
527                         unsigned int verbose,
528                         GNUNET_FS_TEST_UriContinuation cont,
529                         void *cont_cls)
530 {
531   GNUNET_assert (daemon->publish_cont == NULL);
532   struct GNUNET_FS_FileInformation *fi;
533
534   daemon->publish_cont = cont;
535   daemon->publish_cont_cls = cont_cls;
536   daemon->publish_seed = seed;
537   daemon->verbose = verbose;
538   daemon->publish_sched = sched;
539   fi = GNUNET_FS_file_information_create_from_reader (daemon->fs,
540                                                       daemon,                                                 
541                                                       size,
542                                                       &file_generator,
543                                                       daemon,
544                                                       NULL,
545                                                       NULL,
546                                                       do_index,
547                                                       anonymity,
548                                                       42 /* priority */,
549                                                       GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS));
550   daemon->publish_context = GNUNET_FS_publish_start (daemon->fs,
551                                                      fi,
552                                                      NULL, NULL, NULL,
553                                                      GNUNET_FS_PUBLISH_OPTION_NONE);
554   daemon->publish_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
555                                                                timeout,
556                                                                &publish_timeout,
557                                                                daemon);
558 }
559
560
561 static void
562 download_timeout (void *cls,
563                   const struct GNUNET_SCHEDULER_TaskContext *tc)
564 {
565   struct GNUNET_FS_TestDaemon *daemon = cls;
566
567   daemon->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
568   GNUNET_FS_download_stop (daemon->download_context, GNUNET_YES);
569   daemon->download_context = NULL;
570   GNUNET_SCHEDULER_add_continuation (daemon->download_sched,
571                                      daemon->download_cont,
572                                      daemon->download_cont_cls,
573                                      GNUNET_SCHEDULER_REASON_TIMEOUT);
574   daemon->download_cont = NULL;
575   daemon->download_sched = NULL;
576 }
577
578
579 /**
580  * Perform test download.
581  *
582  * @param sched scheduler to use
583  * @param daemon which peer to download from
584  * @param timeout if this operation cannot be completed within the
585  *                given period, call the continuation with an error code
586  * @param anonymity option for download
587  * @param seed used for file validation
588  * @param uri URI of file to download (CHK/LOC only)
589  * @param verbose how verbose to be in reporting
590  * @param cont function to call when done
591  * @param cont_cls closure for cont
592  */
593 void
594 GNUNET_FS_TEST_download (struct GNUNET_SCHEDULER_Handle *sched,
595                          struct GNUNET_FS_TestDaemon *daemon,
596                          struct GNUNET_TIME_Relative timeout,
597                          uint32_t anonymity,
598                          uint32_t seed,
599                          const struct GNUNET_FS_Uri *uri,
600                          unsigned int verbose,
601                          GNUNET_SCHEDULER_Task cont,
602                          void *cont_cls)
603 {
604   uint64_t size;
605  
606   GNUNET_assert (daemon->download_cont == NULL);
607   size = GNUNET_FS_uri_chk_get_file_size (uri);
608   daemon->verbose = verbose;
609   daemon->download_sched = sched;
610   daemon->download_cont = cont;
611   daemon->download_cont_cls = cont_cls;
612   daemon->download_seed = seed;  
613   daemon->download_context = GNUNET_FS_download_start (daemon->fs,
614                                                        uri,
615                                                        NULL, NULL,
616                                                        NULL,
617                                                        0,
618                                                        size,
619                                                        anonymity,
620                                                        GNUNET_FS_DOWNLOAD_OPTION_NONE,
621                                                        NULL,
622                                                        NULL);
623   daemon->download_timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
624                                                                 timeout,
625                                                                 &download_timeout,
626                                                                 daemon);
627 }
628
629 /* end of test_fs_lib.c */