adding single location for no_forcestart configuration list
[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,
194                     po->publish_uri,
195                     (GNUNET_YES == po->do_index)
196                     ? po->publish_tmp_file
197                     : NULL);
198   GNUNET_FS_uri_destroy (po->publish_uri);
199   if ( (GNUNET_YES != po->do_index) &&
200        (NULL != po->publish_tmp_file) )
201     (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
202   GNUNET_free_non_null (po->publish_tmp_file);
203   GNUNET_free (po);
204 }
205
206
207 /**
208  * Task scheduled to run when publish operation times out.
209  *
210  * @param cls the publish operation context
211  * @param tc scheduler context (unused)
212  */
213 static void
214 publish_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
215 {
216   struct TestPublishOperation *po = cls;
217
218   po->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
219   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
220               "Timeout while trying to publish data\n");
221   GNUNET_TESTBED_operation_done (po->fs_op);
222   GNUNET_FS_publish_stop (po->publish_context);
223   po->publish_cont (po->publish_cont_cls, NULL, NULL);
224   (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
225   GNUNET_free_non_null (po->publish_tmp_file);
226   GNUNET_free (po);
227 }
228
229
230 /**
231  * Progress callback for file-sharing events while publishing.
232  *
233  * @param cls the publish operation context
234  * @param info information about the event
235  */
236 static void *
237 publish_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
238 {
239   struct TestPublishOperation *po = cls;
240
241   switch (info->status)
242   {
243   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
244     GNUNET_SCHEDULER_cancel (po->publish_timeout_task);
245     po->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
246     po->publish_uri =
247         GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
248     GNUNET_SCHEDULER_add_continuation (&report_uri, po,
249                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
250     break;
251   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
252     if (po->verbose)
253       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Publishing at %llu/%llu bytes\n",
254                   (unsigned long long) info->value.publish.completed,
255                   (unsigned long long) info->value.publish.size);
256     break;
257   case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
258     break;
259   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
260     if (po->verbose)
261       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
262                   (unsigned long long) info->value.download.completed,
263                   (unsigned long long) info->value.download.size);
264     break;
265   default:
266     break;
267   }
268   return NULL;
269 }
270
271
272 /**
273  * Generate test data for publishing test.
274  *
275  * @param cls pointer to uint32_t with publishing seed
276  * @param offset offset to generate data for
277  * @param max maximum number of bytes to generate
278  * @param buf where to write generated data
279  * @param emsg where to store error message (unused)
280  * @return number of bytes written to buf
281  */
282 static size_t
283 file_generator (void *cls,
284                 uint64_t offset,
285                 size_t max,
286                 void *buf,
287                 char **emsg)
288 {
289   uint32_t *publish_seed = cls;
290   uint64_t pos;
291   uint8_t *cbuf = buf;
292   int mod;
293
294   if (emsg != NULL)
295     *emsg = NULL;
296   if (buf == NULL)
297     return 0;
298   for (pos = 0; pos < 8; pos++)
299     cbuf[pos] = (uint8_t) (offset >> pos * 8);
300   for (pos = 8; pos < max; pos++)
301   {
302     mod = (255 - (offset / 1024 / 32));
303     if (mod == 0)
304       mod = 1;
305     cbuf[pos] = (uint8_t) ((offset * (*publish_seed)) % mod);
306   }
307   return max;
308 }
309
310
311 /**
312  * Connect adapter for publishing operation.
313  *
314  * @param cls the 'struct TestPublishOperation'
315  * @param cfg configuration of the peer to connect to; will be available until
316  *          GNUNET_TESTBED_operation_done() is called on the operation returned
317  *          from GNUNET_TESTBED_service_connect()
318  * @return service handle to return in 'op_result', NULL on error
319  */
320 static void *
321 publish_connect_adapter (void *cls,
322                          const struct GNUNET_CONFIGURATION_Handle *cfg)
323 {
324   struct TestPublishOperation *po = cls;
325
326   return GNUNET_FS_start (cfg,
327                           "fs-test-publish",
328                           &publish_progress_cb, po,
329                           GNUNET_FS_FLAGS_NONE,
330                           GNUNET_FS_OPTIONS_END);
331 }
332
333
334 /**
335  * Adapter function called to destroy connection to file-sharing service.
336  *
337  * @param cls the 'struct GNUNET_FS_Handle'
338  * @param op_result unused (different for publish/download!)
339  */
340 static void
341 fs_disconnect_adapter (void *cls,
342                        void *op_result)
343 {
344   struct GNUNET_FS_Handle *fs = op_result;
345
346   GNUNET_FS_stop (fs);
347 }
348
349
350 /**
351  * Callback to be called when testbed has connected to the fs service
352  *
353  * @param cls the 'struct TestPublishOperation'
354  * @param op the operation that has been finished
355  * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
356  * @param emsg error message in case the operation has failed; will be NULL if
357  *          operation has executed successfully.
358  */
359 static void
360 publish_fs_connect_complete_cb (void *cls,
361                                 struct GNUNET_TESTBED_Operation *op,
362                                 void *ca_result,
363                                 const char *emsg)
364 {
365   struct TestPublishOperation *po = cls;
366   struct GNUNET_FS_FileInformation *fi;
367   struct GNUNET_DISK_FileHandle *fh;
368   char *em;
369   uint64_t off;
370   char buf[DBLOCK_SIZE];
371   size_t bsize;
372   struct GNUNET_FS_BlockOptions bo;
373
374   if (NULL == ca_result)
375     {
376       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect to FS for publishing: %s\n", emsg);
377       po->publish_cont (po->publish_cont_cls,
378                         NULL, NULL);
379       GNUNET_TESTBED_operation_done (po->fs_op);
380       GNUNET_free (po);
381       return;
382     }
383   po->fs = ca_result;
384
385   bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
386   bo.anonymity_level = po->anonymity;
387   bo.content_priority = 42;
388   bo.replication_level = 1;
389   if (GNUNET_YES == po->do_index)
390   {
391     po->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
392     GNUNET_assert (po->publish_tmp_file != NULL);
393     fh = GNUNET_DISK_file_open (po->publish_tmp_file,
394                                 GNUNET_DISK_OPEN_WRITE |
395                                 GNUNET_DISK_OPEN_CREATE,
396                                 GNUNET_DISK_PERM_USER_READ |
397                                 GNUNET_DISK_PERM_USER_WRITE);
398     GNUNET_assert (NULL != fh);
399     off = 0;
400     while (off < po->size)
401     {
402       bsize = GNUNET_MIN (sizeof (buf), po->size - off);
403       emsg = NULL;
404       GNUNET_assert (bsize == file_generator (&po->publish_seed, off, bsize, buf, &em));
405       GNUNET_assert (em == NULL);
406       GNUNET_assert (bsize == GNUNET_DISK_file_write (fh, buf, bsize));
407       off += bsize;
408     }
409     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
410     fi = GNUNET_FS_file_information_create_from_file (po->fs, po,
411                                                       po->publish_tmp_file,
412                                                       NULL, NULL, po->do_index,
413                                                       &bo);
414     GNUNET_assert (NULL != fi);
415   }
416   else
417   {
418     fi = GNUNET_FS_file_information_create_from_reader (po->fs, po,
419                                                         po->size,
420                                                         &file_generator, &po->publish_seed,
421                                                         NULL, NULL,
422                                                         po->do_index, &bo);
423     GNUNET_assert (NULL != fi);
424   }
425   po->publish_context =
426     GNUNET_FS_publish_start (po->fs, fi, NULL, NULL, NULL,
427                              GNUNET_FS_PUBLISH_OPTION_NONE);
428 }
429
430
431 /**
432  * Publish a file at the given peer.
433  *
434  * @param peer where to publish
435  * @param timeout if this operation cannot be completed within the
436  *                given period, call the continuation with an error code
437  * @param anonymity option for publication
438  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
439  *                GNUNET_SYSERR for simulation
440  * @param size size of the file to publish
441  * @param seed seed to use for file generation
442  * @param verbose how verbose to be in reporting
443  * @param cont function to call when done
444  * @param cont_cls closure for cont
445  */
446 void
447 GNUNET_FS_TEST_publish (struct GNUNET_TESTBED_Peer *peer,
448                         struct GNUNET_TIME_Relative timeout, uint32_t anonymity,
449                         int do_index, uint64_t size, uint32_t seed,
450                         unsigned int verbose,
451                         GNUNET_FS_TEST_UriContinuation cont, void *cont_cls)
452 {
453   struct TestPublishOperation *po;
454
455   po = GNUNET_new (struct TestPublishOperation);
456   po->publish_cont = cont;
457   po->publish_cont_cls = cont_cls;
458   po->publish_seed = seed;
459   po->anonymity = anonymity;
460   po->size = size;
461   po->verbose = verbose;
462   po->do_index = do_index;
463   po->fs_op = GNUNET_TESTBED_service_connect (po,
464                                               peer,
465                                               "fs",
466                                               &publish_fs_connect_complete_cb,
467                                               po,
468                                               &publish_connect_adapter,
469                                               &fs_disconnect_adapter,
470                                               po);
471   po->publish_timeout_task =
472       GNUNET_SCHEDULER_add_delayed (timeout, &publish_timeout, po);
473 }
474
475
476 /* ************************** download ************************ */
477
478
479 /**
480  * Task scheduled to run when download operation times out.
481  *
482  * @param cls the download operation context
483  * @param tc scheduler context (unused)
484  */
485 static void
486 download_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
487 {
488   struct TestDownloadOperation *dop = cls;
489
490   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
491               "Timeout while trying to download file\n");
492   dop->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
493   GNUNET_FS_download_stop (dop->download_context, GNUNET_YES);
494   GNUNET_SCHEDULER_add_continuation (dop->download_cont,
495                                      dop->download_cont_cls,
496                                      GNUNET_SCHEDULER_REASON_TIMEOUT);
497   GNUNET_TESTBED_operation_done (dop->fs_op);
498   GNUNET_FS_uri_destroy (dop->uri);
499   GNUNET_free (dop);
500 }
501
502
503 /**
504  * Task scheduled to report on the completion of our download operation.
505  *
506  * @param cls the download operation context
507  * @param tc scheduler context (unused)
508  */
509 static void
510 report_success (void *cls,
511                 const struct GNUNET_SCHEDULER_TaskContext *tc)
512 {
513   struct TestDownloadOperation *dop = cls;
514
515   GNUNET_FS_download_stop (dop->download_context, GNUNET_YES);
516   GNUNET_SCHEDULER_add_continuation (dop->download_cont,
517                                      dop->download_cont_cls,
518                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
519   GNUNET_TESTBED_operation_done (dop->fs_op);
520   GNUNET_FS_uri_destroy (dop->uri);
521   GNUNET_free (dop);
522 }
523
524
525 /**
526  * Progress callback for file-sharing events while downloading.
527  *
528  * @param cls the download operation context
529  * @param info information about the event
530  */
531 static void *
532 download_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
533 {
534   struct TestDownloadOperation *dop = cls;
535
536   switch (info->status)
537   {
538   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
539     if (dop->verbose)
540       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
541                   (unsigned long long) info->value.download.completed,
542                   (unsigned long long) info->value.download.size);
543     break;
544   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
545     GNUNET_SCHEDULER_cancel (dop->download_timeout_task);
546     dop->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
547     GNUNET_SCHEDULER_add_continuation (&report_success, dop,
548                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
549     break;
550   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
551   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
552     break;
553     /* FIXME: monitor data correctness during download progress */
554     /* FIXME: do performance reports given sufficient verbosity */
555     /* FIXME: advance timeout task to "immediate" on error */
556   default:
557     break;
558   }
559   return NULL;
560 }
561
562
563 /**
564  * Connect adapter for download operation.
565  *
566  * @param cls the 'struct TestDownloadOperation'
567  * @param cfg configuration of the peer to connect to; will be available until
568  *          GNUNET_TESTBED_operation_done() is called on the operation returned
569  *          from GNUNET_TESTBED_service_connect()
570  * @return service handle to return in 'op_result', NULL on error
571  */
572 static void *
573 download_connect_adapter (void *cls,
574                          const struct GNUNET_CONFIGURATION_Handle *cfg)
575 {
576   struct TestPublishOperation *po = cls;
577
578   return GNUNET_FS_start (cfg,
579                           "fs-test-download",
580                           &download_progress_cb, po,
581                           GNUNET_FS_FLAGS_NONE,
582                           GNUNET_FS_OPTIONS_END);
583 }
584
585
586 /**
587  * Callback to be called when testbed has connected to the fs service
588  *
589  * @param cls the 'struct TestPublishOperation'
590  * @param op the operation that has been finished
591  * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
592  * @param emsg error message in case the operation has failed; will be NULL if
593  *          operation has executed successfully.
594  */
595 static void
596 download_fs_connect_complete_cb (void *cls,
597                                  struct GNUNET_TESTBED_Operation *op,
598                                  void *ca_result,
599                                  const char *emsg)
600 {
601   struct TestDownloadOperation *dop = cls;
602
603   dop->fs = ca_result;
604   GNUNET_assert (NULL != dop->fs);
605   dop->download_context =
606     GNUNET_FS_download_start (dop->fs, dop->uri, NULL, NULL, NULL, 0, dop->size,
607                               dop->anonymity, GNUNET_FS_DOWNLOAD_OPTION_NONE,
608                               NULL, NULL);
609 }
610
611
612 /**
613  * Perform test download.
614  *
615  * @param peer which peer to download from
616  * @param timeout if this operation cannot be completed within the
617  *                given period, call the continuation with an error code
618  * @param anonymity option for download
619  * @param seed used for file validation
620  * @param uri URI of file to download (CHK/LOC only)
621  * @param verbose how verbose to be in reporting
622  * @param cont function to call when done
623  * @param cont_cls closure for cont
624  */
625 void
626 GNUNET_FS_TEST_download (struct GNUNET_TESTBED_Peer *peer,
627                          struct GNUNET_TIME_Relative timeout,
628                          uint32_t anonymity, uint32_t seed,
629                          const struct GNUNET_FS_Uri *uri, unsigned int verbose,
630                          GNUNET_SCHEDULER_Task cont, void *cont_cls)
631 {
632   struct TestDownloadOperation *dop;
633
634   dop = GNUNET_new (struct TestDownloadOperation);
635   dop->uri = GNUNET_FS_uri_dup (uri);
636   dop->size = GNUNET_FS_uri_chk_get_file_size (uri);
637   dop->verbose = verbose;
638   dop->anonymity = anonymity;
639   dop->download_cont = cont;
640   dop->download_cont_cls = cont_cls;
641   dop->download_seed = seed;
642
643   dop->fs_op = GNUNET_TESTBED_service_connect (dop,
644                                                peer,
645                                                "fs",
646                                                &download_fs_connect_complete_cb,
647                                                dop,
648                                                &download_connect_adapter,
649                                                &fs_disconnect_adapter,
650                                                dop);
651   dop->download_timeout_task =
652       GNUNET_SCHEDULER_add_delayed (timeout, &download_timeout, dop);
653 }
654
655
656 /* end of fs_test_lib.c */