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