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