-fix
[oweals/gnunet.git] / src / fs / fs_test_lib.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010, 2011, 2012 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 3, 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  *        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_api.h"
31 #include "fs_test_lib.h"
32
33
34 #define CONTENT_LIFETIME GNUNET_TIME_UNIT_HOURS
35
36
37 /**
38  * Handle for a publishing operation started for testing FS.
39  */
40 struct TestPublishOperation
41 {
42
43   /**
44    * Handle for the operation to connect to the peer's 'fs' service.
45    */
46   struct GNUNET_TESTBED_Operation *fs_op;
47
48   /**
49    * Handle to the file sharing context using this daemon.
50    */
51   struct GNUNET_FS_Handle *fs;
52
53   /**
54    * Function to call when upload is done.
55    */
56   GNUNET_FS_TEST_UriContinuation publish_cont;
57
58   /**
59    * Closure for publish_cont.
60    */
61   void *publish_cont_cls;
62
63   /**
64    * Task to abort publishing (timeout).
65    */
66   GNUNET_SCHEDULER_TaskIdentifier publish_timeout_task;
67
68   /**
69    * Seed for file generation.
70    */
71   uint32_t publish_seed;
72
73   /**
74    * Context for current publishing operation.
75    */
76   struct GNUNET_FS_PublishContext *publish_context;
77
78   /**
79    * Result URI.
80    */
81   struct GNUNET_FS_Uri *publish_uri;
82
83   /**
84    * Name of the temporary file used, or NULL for none.
85    */
86   char *publish_tmp_file;
87
88   /**
89    * Size of the file.
90    */
91   uint64_t size;
92
93   /**
94    * Anonymity level used.
95    */
96   uint32_t anonymity;
97
98   /**
99    * Verbosity level of the current operation.
100    */
101   unsigned int verbose;
102
103   /**
104    * Are we testing indexing? (YES: index, NO: insert, SYSERR: simulate)
105    */
106   int do_index;
107 };
108
109
110 /**
111  * Handle for a download operation started for testing FS.
112  */
113 struct TestDownloadOperation
114 {
115
116   /**
117    * Handle for the operation to connect to the peer's 'fs' service.
118    */
119   struct GNUNET_TESTBED_Operation *fs_op;
120
121   /**
122    * Handle to the file sharing context using this daemon.
123    */
124   struct GNUNET_FS_Handle *fs;
125
126   /**
127    * Handle to the daemon via testing.
128    */
129   struct GNUNET_TESTING_Daemon *daemon;
130
131   /**
132    * Function to call when download is done.
133    */
134   GNUNET_SCHEDULER_Task download_cont;
135
136   /**
137    * Closure for download_cont.
138    */
139   void *download_cont_cls;
140
141   /**
142    * URI to download.
143    */
144   struct GNUNET_FS_Uri *uri;
145
146   /**
147    * Task to abort downloading (timeout).
148    */
149   GNUNET_SCHEDULER_TaskIdentifier download_timeout_task;
150
151   /**
152    * Context for current download operation.
153    */
154   struct GNUNET_FS_DownloadContext *download_context;
155
156   /**
157    * Size of the file.
158    */
159   uint64_t size;
160
161   /**
162    * Anonymity level used.
163    */
164   uint32_t anonymity;
165
166   /**
167    * Seed for download verification.
168    */
169   uint32_t download_seed;
170
171   /**
172    * Verbosity level of the current operation.
173    */
174   unsigned int verbose;
175
176 };
177
178
179 /**
180  * Task scheduled to report on the completion of our publish operation.
181  *
182  * @param cls the publish operation context
183  * @param tc scheduler context (unused)
184  */
185 static void
186 report_uri (void *cls, 
187             const struct GNUNET_SCHEDULER_TaskContext *tc)
188 {
189   struct TestPublishOperation *po = cls;
190
191   GNUNET_FS_publish_stop (po->publish_context);
192   GNUNET_TESTBED_operation_done (po->fs_op);
193   po->publish_cont (po->publish_cont_cls, po->publish_uri);
194   GNUNET_FS_uri_destroy (po->publish_uri);
195   GNUNET_free_non_null (po->publish_tmp_file);
196   GNUNET_free (po);
197 }
198
199
200 /**
201  * Task scheduled to run when publish operation times out.
202  *
203  * @param cls the publish operation context
204  * @param tc scheduler context (unused)
205  */
206 static void
207 publish_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
208 {
209   struct TestPublishOperation *po = cls;
210
211   po->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
212   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
213               "Timeout while trying to publish data\n");
214   if (NULL == po->fs)
215     GNUNET_TESTBED_operation_cancel (po->fs_op);
216   else
217     GNUNET_TESTBED_operation_done (po->fs_op);
218   GNUNET_FS_publish_stop (po->publish_context);
219   GNUNET_TESTBED_operation_done (po->fs_op);
220   po->publish_cont (po->publish_cont_cls, NULL);
221   GNUNET_free_non_null (po->publish_tmp_file);
222   GNUNET_free (po);
223 }
224
225
226 /**
227  * Progress callback for file-sharing events while publishing.
228  *
229  * @param cls the publish operation context
230  * @param info information about the event
231  */
232 static void *
233 publish_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
234 {
235   struct TestPublishOperation *po = cls;
236
237   switch (info->status)
238   {
239   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
240     GNUNET_SCHEDULER_cancel (po->publish_timeout_task);
241     po->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
242     po->publish_uri =
243         GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
244     GNUNET_SCHEDULER_add_continuation (&report_uri, po,
245                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
246     break;
247   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
248     if (po->verbose)
249       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Publishing at %llu/%llu bytes\n",
250                   (unsigned long long) info->value.publish.completed,
251                   (unsigned long long) info->value.publish.size);
252     break;
253   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
254     if (po->verbose)
255       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
256                   (unsigned long long) info->value.download.completed,
257                   (unsigned long long) info->value.download.size);
258     break;
259   default:
260     break;
261   }
262   return NULL;
263 }
264
265
266 /**
267  * Generate test data for publishing test.
268  *
269  * @param cls pointer to uint32_t with publishing seed
270  * @param offset offset to generate data for
271  * @param max maximum number of bytes to generate
272  * @param buf where to write generated data 
273  * @param emsg where to store error message (unused)
274  * @return number of bytes written to buf
275  */
276 static size_t
277 file_generator (void *cls, 
278                 uint64_t offset,
279                 size_t max, 
280                 void *buf, 
281                 char **emsg)
282 {
283   uint32_t *publish_seed = cls;
284   uint64_t pos;
285   uint8_t *cbuf = buf;
286   int mod;
287
288   if (emsg != NULL)
289     *emsg = NULL;
290   if (buf == NULL)
291     return 0;
292   for (pos = 0; pos < 8; pos++)
293     cbuf[pos] = (uint8_t) (offset >> pos * 8);
294   for (pos = 8; pos < max; pos++)
295   {
296     mod = (255 - (offset / 1024 / 32));
297     if (mod == 0)
298       mod = 1;
299     cbuf[pos] = (uint8_t) ((offset * (*publish_seed)) % mod);
300   }
301   return max;
302 }
303
304
305 /**
306  * Connect adapter for publishing operation.
307  * 
308  * @param cls the 'struct TestPublishOperation'
309  * @param cfg configuration of the peer to connect to; will be available until
310  *          GNUNET_TESTBED_operation_done() is called on the operation returned
311  *          from GNUNET_TESTBED_service_connect()
312  * @return service handle to return in 'op_result', NULL on error
313  */
314 static void *
315 publish_connect_adapter (void *cls,
316                          const struct GNUNET_CONFIGURATION_Handle *cfg)
317 {
318   struct TestPublishOperation *po = cls;
319  
320   return GNUNET_FS_start (cfg,
321                           "fs-test-publish",
322                           &publish_progress_cb, po,
323                           GNUNET_FS_FLAGS_NONE,
324                           GNUNET_FS_OPTIONS_END);
325 }
326
327
328 /**
329  * Adapter function called to destroy connection to file-sharing service.
330  * 
331  * @param cls the 'struct GNUNET_FS_Handle'
332  * @param op_result unused (different for publish/download!)
333  */
334 static void 
335 fs_disconnect_adapter (void *cls,
336                        void *op_result)
337 {
338   struct GNUNET_FS_Handle *fs = op_result;
339
340   GNUNET_FS_stop (fs);
341 }
342
343
344 /**
345  * Callback to be called when testbed has connected to the fs service
346  *
347  * @param cls the 'struct TestPublishOperation'
348  * @param op the operation that has been finished
349  * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
350  * @param emsg error message in case the operation has failed; will be NULL if
351  *          operation has executed successfully.
352  */
353 static void
354 publish_fs_connect_complete_cb (void *cls,
355                                 struct GNUNET_TESTBED_Operation *op,
356                                 void *ca_result,
357                                 const char *emsg)
358 {
359   struct TestPublishOperation *po = cls;
360   struct GNUNET_FS_FileInformation *fi;
361   struct GNUNET_DISK_FileHandle *fh;
362   char *em;
363   uint64_t off;
364   char buf[DBLOCK_SIZE];
365   size_t bsize;
366   struct GNUNET_FS_BlockOptions bo;
367
368   if (NULL == ca_result)
369     {
370       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect to FS for publishing: %s\n", emsg);
371       po->publish_cont (po->publish_cont_cls,
372                         NULL);
373       GNUNET_TESTBED_operation_done (po->fs_op);
374       GNUNET_free (po);
375       return;
376     }
377   po->fs = ca_result;
378
379   bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
380   bo.anonymity_level = po->anonymity;
381   bo.content_priority = 42;
382   bo.replication_level = 1;
383   if (GNUNET_YES == po->do_index)
384   {
385     po->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
386     GNUNET_assert (po->publish_tmp_file != NULL);
387     fh = GNUNET_DISK_file_open (po->publish_tmp_file,
388                                 GNUNET_DISK_OPEN_WRITE |
389                                 GNUNET_DISK_OPEN_CREATE,
390                                 GNUNET_DISK_PERM_USER_READ |
391                                 GNUNET_DISK_PERM_USER_WRITE);
392     GNUNET_assert (NULL != fh);
393     off = 0;
394     while (off < po->size)
395     {
396       bsize = GNUNET_MIN (sizeof (buf), po->size - off);
397       emsg = NULL;
398       GNUNET_assert (bsize == file_generator (&po->publish_seed, off, bsize, buf, &em));
399       GNUNET_assert (em == NULL);
400       GNUNET_assert (bsize == GNUNET_DISK_file_write (fh, buf, bsize));
401       off += bsize;
402     }
403     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
404     fi = GNUNET_FS_file_information_create_from_file (po->fs, po,
405                                                       po->publish_tmp_file,
406                                                       NULL, NULL, po->do_index,
407                                                       &bo);
408   }
409   else
410   {
411     fi = GNUNET_FS_file_information_create_from_reader (po->fs, po,
412                                                         po->size, 
413                                                         &file_generator, &po->publish_seed, 
414                                                         NULL, NULL,
415                                                         po->do_index, &bo);
416   }
417   po->publish_context =
418     GNUNET_FS_publish_start (po->fs, fi, NULL, NULL, NULL,
419                              GNUNET_FS_PUBLISH_OPTION_NONE);
420 }
421
422
423 /**
424  * Publish a file at the given peer.
425  *
426  * @param peer where to publish
427  * @param timeout if this operation cannot be completed within the
428  *                given period, call the continuation with an error code
429  * @param anonymity option for publication
430  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
431  *                GNUNET_SYSERR for simulation
432  * @param size size of the file to publish
433  * @param seed seed to use for file generation
434  * @param verbose how verbose to be in reporting
435  * @param cont function to call when done
436  * @param cont_cls closure for cont
437  */
438 void
439 GNUNET_FS_TEST_publish (struct GNUNET_TESTBED_Peer *peer,
440                         struct GNUNET_TIME_Relative timeout, uint32_t anonymity,
441                         int do_index, uint64_t size, uint32_t seed,
442                         unsigned int verbose,
443                         GNUNET_FS_TEST_UriContinuation cont, void *cont_cls)
444 {
445   struct TestPublishOperation *po;
446
447   po = GNUNET_malloc (sizeof (struct TestPublishOperation));
448   po->publish_cont = cont;
449   po->publish_cont_cls = cont_cls;
450   po->publish_seed = seed;
451   po->anonymity = anonymity;
452   po->size = size;
453   po->verbose = verbose;
454   po->do_index = do_index;
455   po->fs_op = GNUNET_TESTBED_service_connect (po,
456                                               peer,
457                                               "fs",
458                                               &publish_fs_connect_complete_cb,
459                                               po,
460                                               &publish_connect_adapter,
461                                               &fs_disconnect_adapter,
462                                               po);
463   po->publish_timeout_task =
464       GNUNET_SCHEDULER_add_delayed (timeout, &publish_timeout, po);
465 }
466
467
468 /* ************************** download ************************ */
469
470
471 /**
472  * Task scheduled to run when download operation times out.
473  *
474  * @param cls the download operation context
475  * @param tc scheduler context (unused)
476  */
477 static void
478 download_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
479 {
480   struct TestDownloadOperation *dop = cls;
481
482   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
483               "Timeout while trying to download file\n");
484   dop->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
485   GNUNET_FS_download_stop (dop->download_context, GNUNET_YES);
486   GNUNET_SCHEDULER_add_continuation (dop->download_cont,
487                                      dop->download_cont_cls,
488                                      GNUNET_SCHEDULER_REASON_TIMEOUT);
489   if (NULL == dop->fs)
490     GNUNET_TESTBED_operation_cancel (dop->fs_op);
491   else
492     GNUNET_TESTBED_operation_done (dop->fs_op);
493   GNUNET_FS_uri_destroy (dop->uri);
494   GNUNET_free (dop);
495 }
496
497
498 /**
499  * Task scheduled to report on the completion of our download operation.
500  *
501  * @param cls the download operation context
502  * @param tc scheduler context (unused)
503  */
504 static void
505 report_success (void *cls,
506                 const struct GNUNET_SCHEDULER_TaskContext *tc)
507 {
508   struct TestDownloadOperation *dop = cls;
509
510   GNUNET_FS_download_stop (dop->download_context, GNUNET_YES);
511   GNUNET_SCHEDULER_add_continuation (dop->download_cont,
512                                      dop->download_cont_cls,
513                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
514   GNUNET_TESTBED_operation_done (dop->fs_op);
515   GNUNET_FS_uri_destroy (dop->uri);
516   GNUNET_free (dop);
517 }
518
519
520 /**
521  * Progress callback for file-sharing events while downloading.
522  *
523  * @param cls the download operation context
524  * @param info information about the event
525  */
526 static void *
527 download_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
528 {
529   struct TestDownloadOperation *dop = cls;
530
531   switch (info->status)
532   {
533   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
534     if (dop->verbose)
535       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
536                   (unsigned long long) info->value.download.completed,
537                   (unsigned long long) info->value.download.size);
538     break;
539   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
540     GNUNET_SCHEDULER_cancel (dop->download_timeout_task);
541     dop->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
542     GNUNET_SCHEDULER_add_continuation (&report_success, dop,
543                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
544     break;
545   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
546   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
547     break;
548     /* FIXME: monitor data correctness during download progress */
549     /* FIXME: do performance reports given sufficient verbosity */
550     /* FIXME: advance timeout task to "immediate" on error */
551   default:
552     break;
553   }
554   return NULL;
555 }
556
557
558 /**
559  * Connect adapter for download operation.
560  * 
561  * @param cls the 'struct TestDownloadOperation'
562  * @param cfg configuration of the peer to connect to; will be available until
563  *          GNUNET_TESTBED_operation_done() is called on the operation returned
564  *          from GNUNET_TESTBED_service_connect()
565  * @return service handle to return in 'op_result', NULL on error
566  */
567 static void *
568 download_connect_adapter (void *cls,
569                          const struct GNUNET_CONFIGURATION_Handle *cfg)
570 {
571   struct TestPublishOperation *po = cls;
572  
573   return GNUNET_FS_start (cfg,
574                           "fs-test-download",
575                           &download_progress_cb, po,
576                           GNUNET_FS_FLAGS_NONE,
577                           GNUNET_FS_OPTIONS_END);
578 }
579
580
581 /**
582  * Callback to be called when testbed has connected to the fs service
583  *
584  * @param cls the 'struct TestPublishOperation'
585  * @param op the operation that has been finished
586  * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
587  * @param emsg error message in case the operation has failed; will be NULL if
588  *          operation has executed successfully.
589  */
590 static void
591 download_fs_connect_complete_cb (void *cls,
592                                  struct GNUNET_TESTBED_Operation *op,
593                                  void *ca_result,
594                                  const char *emsg)
595 {
596   struct TestDownloadOperation *dop = cls;
597
598   dop->download_context =
599     GNUNET_FS_download_start (dop->fs, dop->uri, NULL, NULL, NULL, 0, dop->size,
600                               dop->anonymity, GNUNET_FS_DOWNLOAD_OPTION_NONE,
601                               NULL, NULL);
602 }
603
604
605 /**
606  * Perform test download.
607  *
608  * @param peer which peer to download from
609  * @param timeout if this operation cannot be completed within the
610  *                given period, call the continuation with an error code
611  * @param anonymity option for download
612  * @param seed used for file validation
613  * @param uri URI of file to download (CHK/LOC only)
614  * @param verbose how verbose to be in reporting
615  * @param cont function to call when done
616  * @param cont_cls closure for cont
617  */
618 void
619 GNUNET_FS_TEST_download (struct GNUNET_TESTBED_Peer *peer,
620                          struct GNUNET_TIME_Relative timeout,
621                          uint32_t anonymity, uint32_t seed,
622                          const struct GNUNET_FS_Uri *uri, unsigned int verbose,
623                          GNUNET_SCHEDULER_Task cont, void *cont_cls)
624 {
625   struct TestDownloadOperation *dop;
626
627   dop = GNUNET_malloc (sizeof (struct TestDownloadOperation));
628   dop->uri = GNUNET_FS_uri_dup (uri);
629   dop->size = GNUNET_FS_uri_chk_get_file_size (uri);
630   dop->verbose = verbose;
631   dop->anonymity = anonymity;
632   dop->download_cont = cont;
633   dop->download_cont_cls = cont_cls;
634   dop->download_seed = seed;
635
636   dop->fs_op = GNUNET_TESTBED_service_connect (dop,
637                                                peer,
638                                                "fs",
639                                                &download_fs_connect_complete_cb,
640                                                dop,
641                                                &download_connect_adapter,
642                                                &fs_disconnect_adapter,
643                                                dop);
644   dop->download_timeout_task =
645       GNUNET_SCHEDULER_add_delayed (timeout, &download_timeout, dop);
646 }
647
648
649 /* end of fs_test_lib.c */