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