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