removed GNUNET_TESTBED_operation_cancel
[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_done (po->fs_op);
216   else
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);
220   GNUNET_free_non_null (po->publish_tmp_file);
221   GNUNET_free (po);
222 }
223
224
225 /**
226  * Progress callback for file-sharing events while publishing.
227  *
228  * @param cls the publish operation context
229  * @param info information about the event
230  */
231 static void *
232 publish_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
233 {
234   struct TestPublishOperation *po = cls;
235
236   switch (info->status)
237   {
238   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
239     GNUNET_SCHEDULER_cancel (po->publish_timeout_task);
240     po->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
241     po->publish_uri =
242         GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
243     GNUNET_SCHEDULER_add_continuation (&report_uri, po,
244                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
245     break;
246   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
247     if (po->verbose)
248       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Publishing at %llu/%llu bytes\n",
249                   (unsigned long long) info->value.publish.completed,
250                   (unsigned long long) info->value.publish.size);
251     break;
252   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
253     if (po->verbose)
254       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
255                   (unsigned long long) info->value.download.completed,
256                   (unsigned long long) info->value.download.size);
257     break;
258   default:
259     break;
260   }
261   return NULL;
262 }
263
264
265 /**
266  * Generate test data for publishing test.
267  *
268  * @param cls pointer to uint32_t with publishing seed
269  * @param offset offset to generate data for
270  * @param max maximum number of bytes to generate
271  * @param buf where to write generated data 
272  * @param emsg where to store error message (unused)
273  * @return number of bytes written to buf
274  */
275 static size_t
276 file_generator (void *cls, 
277                 uint64_t offset,
278                 size_t max, 
279                 void *buf, 
280                 char **emsg)
281 {
282   uint32_t *publish_seed = cls;
283   uint64_t pos;
284   uint8_t *cbuf = buf;
285   int mod;
286
287   if (emsg != NULL)
288     *emsg = NULL;
289   if (buf == NULL)
290     return 0;
291   for (pos = 0; pos < 8; pos++)
292     cbuf[pos] = (uint8_t) (offset >> pos * 8);
293   for (pos = 8; pos < max; pos++)
294   {
295     mod = (255 - (offset / 1024 / 32));
296     if (mod == 0)
297       mod = 1;
298     cbuf[pos] = (uint8_t) ((offset * (*publish_seed)) % mod);
299   }
300   return max;
301 }
302
303
304 /**
305  * Connect adapter for publishing operation.
306  * 
307  * @param cls the 'struct TestPublishOperation'
308  * @param cfg configuration of the peer to connect to; will be available until
309  *          GNUNET_TESTBED_operation_done() is called on the operation returned
310  *          from GNUNET_TESTBED_service_connect()
311  * @return service handle to return in 'op_result', NULL on error
312  */
313 static void *
314 publish_connect_adapter (void *cls,
315                          const struct GNUNET_CONFIGURATION_Handle *cfg)
316 {
317   struct TestPublishOperation *po = cls;
318  
319   return GNUNET_FS_start (cfg,
320                           "fs-test-publish",
321                           &publish_progress_cb, po,
322                           GNUNET_FS_FLAGS_NONE,
323                           GNUNET_FS_OPTIONS_END);
324 }
325
326
327 /**
328  * Adapter function called to destroy connection to file-sharing service.
329  * 
330  * @param cls the 'struct GNUNET_FS_Handle'
331  * @param op_result unused (different for publish/download!)
332  */
333 static void 
334 fs_disconnect_adapter (void *cls,
335                        void *op_result)
336 {
337   struct GNUNET_FS_Handle *fs = op_result;
338
339   GNUNET_FS_stop (fs);
340 }
341
342
343 /**
344  * Callback to be called when testbed has connected to the fs service
345  *
346  * @param cls the 'struct TestPublishOperation'
347  * @param op the operation that has been finished
348  * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
349  * @param emsg error message in case the operation has failed; will be NULL if
350  *          operation has executed successfully.
351  */
352 static void
353 publish_fs_connect_complete_cb (void *cls,
354                                 struct GNUNET_TESTBED_Operation *op,
355                                 void *ca_result,
356                                 const char *emsg)
357 {
358   struct TestPublishOperation *po = cls;
359   struct GNUNET_FS_FileInformation *fi;
360   struct GNUNET_DISK_FileHandle *fh;
361   char *em;
362   uint64_t off;
363   char buf[DBLOCK_SIZE];
364   size_t bsize;
365   struct GNUNET_FS_BlockOptions bo;
366
367   if (NULL == ca_result)
368     {
369       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect to FS for publishing: %s\n", emsg);
370       po->publish_cont (po->publish_cont_cls,
371                         NULL);
372       GNUNET_TESTBED_operation_done (po->fs_op);
373       GNUNET_free (po);
374       return;
375     }
376   po->fs = ca_result;
377
378   bo.expiration_time = GNUNET_TIME_relative_to_absolute (CONTENT_LIFETIME);
379   bo.anonymity_level = po->anonymity;
380   bo.content_priority = 42;
381   bo.replication_level = 1;
382   if (GNUNET_YES == po->do_index)
383   {
384     po->publish_tmp_file = GNUNET_DISK_mktemp ("fs-test-publish-index");
385     GNUNET_assert (po->publish_tmp_file != NULL);
386     fh = GNUNET_DISK_file_open (po->publish_tmp_file,
387                                 GNUNET_DISK_OPEN_WRITE |
388                                 GNUNET_DISK_OPEN_CREATE,
389                                 GNUNET_DISK_PERM_USER_READ |
390                                 GNUNET_DISK_PERM_USER_WRITE);
391     GNUNET_assert (NULL != fh);
392     off = 0;
393     while (off < po->size)
394     {
395       bsize = GNUNET_MIN (sizeof (buf), po->size - off);
396       emsg = NULL;
397       GNUNET_assert (bsize == file_generator (&po->publish_seed, off, bsize, buf, &em));
398       GNUNET_assert (em == NULL);
399       GNUNET_assert (bsize == GNUNET_DISK_file_write (fh, buf, bsize));
400       off += bsize;
401     }
402     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
403     fi = GNUNET_FS_file_information_create_from_file (po->fs, po,
404                                                       po->publish_tmp_file,
405                                                       NULL, NULL, po->do_index,
406                                                       &bo);
407   }
408   else
409   {
410     fi = GNUNET_FS_file_information_create_from_reader (po->fs, po,
411                                                         po->size, 
412                                                         &file_generator, &po->publish_seed, 
413                                                         NULL, NULL,
414                                                         po->do_index, &bo);
415   }
416   po->publish_context =
417     GNUNET_FS_publish_start (po->fs, fi, NULL, NULL, NULL,
418                              GNUNET_FS_PUBLISH_OPTION_NONE);
419 }
420
421
422 /**
423  * Publish a file at the given peer.
424  *
425  * @param peer where to publish
426  * @param timeout if this operation cannot be completed within the
427  *                given period, call the continuation with an error code
428  * @param anonymity option for publication
429  * @param do_index GNUNET_YES for index, GNUNET_NO for insertion,
430  *                GNUNET_SYSERR for simulation
431  * @param size size of the file to publish
432  * @param seed seed to use for file generation
433  * @param verbose how verbose to be in reporting
434  * @param cont function to call when done
435  * @param cont_cls closure for cont
436  */
437 void
438 GNUNET_FS_TEST_publish (struct GNUNET_TESTBED_Peer *peer,
439                         struct GNUNET_TIME_Relative timeout, uint32_t anonymity,
440                         int do_index, uint64_t size, uint32_t seed,
441                         unsigned int verbose,
442                         GNUNET_FS_TEST_UriContinuation cont, void *cont_cls)
443 {
444   struct TestPublishOperation *po;
445
446   po = GNUNET_malloc (sizeof (struct TestPublishOperation));
447   po->publish_cont = cont;
448   po->publish_cont_cls = cont_cls;
449   po->publish_seed = seed;
450   po->anonymity = anonymity;
451   po->size = size;
452   po->verbose = verbose;
453   po->do_index = do_index;
454   po->fs_op = GNUNET_TESTBED_service_connect (po,
455                                               peer,
456                                               "fs",
457                                               &publish_fs_connect_complete_cb,
458                                               po,
459                                               &publish_connect_adapter,
460                                               &fs_disconnect_adapter,
461                                               po);
462   po->publish_timeout_task =
463       GNUNET_SCHEDULER_add_delayed (timeout, &publish_timeout, po);
464 }
465
466
467 /* ************************** download ************************ */
468
469
470 /**
471  * Task scheduled to run when download operation times out.
472  *
473  * @param cls the download operation context
474  * @param tc scheduler context (unused)
475  */
476 static void
477 download_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
478 {
479   struct TestDownloadOperation *dop = cls;
480
481   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
482               "Timeout while trying to download file\n");
483   dop->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
484   GNUNET_FS_download_stop (dop->download_context, GNUNET_YES);
485   GNUNET_SCHEDULER_add_continuation (dop->download_cont,
486                                      dop->download_cont_cls,
487                                      GNUNET_SCHEDULER_REASON_TIMEOUT);
488   if (NULL == dop->fs)
489     GNUNET_TESTBED_operation_done (dop->fs_op);
490   else
491     GNUNET_TESTBED_operation_done (dop->fs_op);
492   GNUNET_FS_uri_destroy (dop->uri);
493   GNUNET_free (dop);
494 }
495
496
497 /**
498  * Task scheduled to report on the completion of our download operation.
499  *
500  * @param cls the download operation context
501  * @param tc scheduler context (unused)
502  */
503 static void
504 report_success (void *cls,
505                 const struct GNUNET_SCHEDULER_TaskContext *tc)
506 {
507   struct TestDownloadOperation *dop = cls;
508
509   GNUNET_FS_download_stop (dop->download_context, GNUNET_YES);
510   GNUNET_SCHEDULER_add_continuation (dop->download_cont,
511                                      dop->download_cont_cls,
512                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
513   GNUNET_TESTBED_operation_done (dop->fs_op);
514   GNUNET_FS_uri_destroy (dop->uri);
515   GNUNET_free (dop);
516 }
517
518
519 /**
520  * Progress callback for file-sharing events while downloading.
521  *
522  * @param cls the download operation context
523  * @param info information about the event
524  */
525 static void *
526 download_progress_cb (void *cls, 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, "Download at %llu/%llu bytes\n",
535                   (unsigned long long) info->value.download.completed,
536                   (unsigned long long) info->value.download.size);
537     break;
538   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
539     GNUNET_SCHEDULER_cancel (dop->download_timeout_task);
540     dop->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
541     GNUNET_SCHEDULER_add_continuation (&report_success, dop,
542                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
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_Task cont, void *cont_cls)
625 {
626   struct TestDownloadOperation *dop;
627
628   dop = GNUNET_malloc (sizeof (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 */