glitch in the license text detected by hyazinthe, thank you!
[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
16 /**
17  * @file fs/fs_test_lib.c
18  * @brief library routines for testing FS publishing and downloading;
19  *        this code is limited to flat files
20  *        and no keywords (those functions can be tested with
21  *        single-peer setups; this is for testing routing).
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "fs_api.h"
26 #include "fs_test_lib.h"
27
28
29 #define CONTENT_LIFETIME GNUNET_TIME_UNIT_HOURS
30
31
32 /**
33  * Handle for a publishing operation started for testing FS.
34  */
35 struct TestPublishOperation
36 {
37
38   /**
39    * Handle for the operation to connect to the peer's 'fs' service.
40    */
41   struct GNUNET_TESTBED_Operation *fs_op;
42
43   /**
44    * Handle to the file sharing context using this daemon.
45    */
46   struct GNUNET_FS_Handle *fs;
47
48   /**
49    * Function to call when upload is done.
50    */
51   GNUNET_FS_TEST_UriContinuation publish_cont;
52
53   /**
54    * Closure for publish_cont.
55    */
56   void *publish_cont_cls;
57
58   /**
59    * Task to abort publishing (timeout).
60    */
61   struct GNUNET_SCHEDULER_Task * publish_timeout_task;
62
63   /**
64    * Seed for file generation.
65    */
66   uint32_t publish_seed;
67
68   /**
69    * Context for current publishing operation.
70    */
71   struct GNUNET_FS_PublishContext *publish_context;
72
73   /**
74    * Result URI.
75    */
76   struct GNUNET_FS_Uri *publish_uri;
77
78   /**
79    * Name of the temporary file used, or NULL for none.
80    */
81   char *publish_tmp_file;
82
83   /**
84    * Size of the file.
85    */
86   uint64_t size;
87
88   /**
89    * Anonymity level used.
90    */
91   uint32_t anonymity;
92
93   /**
94    * Verbosity level of the current operation.
95    */
96   unsigned int verbose;
97
98   /**
99    * Are we testing indexing? (YES: index, NO: insert, SYSERR: simulate)
100    */
101   int do_index;
102 };
103
104
105 /**
106  * Handle for a download operation started for testing FS.
107  */
108 struct TestDownloadOperation
109 {
110
111   /**
112    * Handle for the operation to connect to the peer's 'fs' service.
113    */
114   struct GNUNET_TESTBED_Operation *fs_op;
115
116   /**
117    * Handle to the file sharing context using this daemon.
118    */
119   struct GNUNET_FS_Handle *fs;
120
121   /**
122    * Handle to the daemon via testing.
123    */
124   struct GNUNET_TESTING_Daemon *daemon;
125
126   /**
127    * Function to call when download is done.
128    */
129   GNUNET_SCHEDULER_TaskCallback download_cont;
130
131   /**
132    * Closure for download_cont.
133    */
134   void *download_cont_cls;
135
136   /**
137    * URI to download.
138    */
139   struct GNUNET_FS_Uri *uri;
140
141   /**
142    * Task to abort downloading (timeout).
143    */
144   struct GNUNET_SCHEDULER_Task * download_timeout_task;
145
146   /**
147    * Context for current download operation.
148    */
149   struct GNUNET_FS_DownloadContext *download_context;
150
151   /**
152    * Size of the file.
153    */
154   uint64_t size;
155
156   /**
157    * Anonymity level used.
158    */
159   uint32_t anonymity;
160
161   /**
162    * Seed for download verification.
163    */
164   uint32_t download_seed;
165
166   /**
167    * Verbosity level of the current operation.
168    */
169   unsigned int verbose;
170
171 };
172
173
174 /**
175  * Task scheduled to report on the completion of our publish operation.
176  *
177  * @param cls the publish operation context
178  * @param tc scheduler context (unused)
179  */
180 static void
181 report_uri (void *cls)
182 {
183   struct TestPublishOperation *po = cls;
184
185   GNUNET_FS_publish_stop (po->publish_context);
186   GNUNET_TESTBED_operation_done (po->fs_op);
187   po->publish_cont (po->publish_cont_cls,
188                     po->publish_uri,
189                     (GNUNET_YES == po->do_index)
190                     ? po->publish_tmp_file
191                     : NULL);
192   GNUNET_FS_uri_destroy (po->publish_uri);
193   if ( (GNUNET_YES != po->do_index) &&
194        (NULL != po->publish_tmp_file) )
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  */
206 static void
207 publish_timeout (void *cls)
208 {
209   struct TestPublishOperation *po = cls;
210
211   po->publish_timeout_task = NULL;
212   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
213               "Timeout while trying to publish data\n");
214   GNUNET_TESTBED_operation_done (po->fs_op);
215   GNUNET_FS_publish_stop (po->publish_context);
216   po->publish_cont (po->publish_cont_cls, NULL, NULL);
217   (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
218   GNUNET_free_non_null (po->publish_tmp_file);
219   GNUNET_free (po);
220 }
221
222
223 /**
224  * Progress callback for file-sharing events while publishing.
225  *
226  * @param cls the publish operation context
227  * @param info information about the event
228  */
229 static void *
230 publish_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
231 {
232   struct TestPublishOperation *po = cls;
233
234   switch (info->status)
235   {
236   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
237     GNUNET_SCHEDULER_cancel (po->publish_timeout_task);
238     po->publish_timeout_task = NULL;
239     po->publish_uri =
240         GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
241     GNUNET_SCHEDULER_add_now (&report_uri,
242                               po);
243     break;
244   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
245     if (po->verbose)
246       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Publishing at %llu/%llu bytes\n",
247                   (unsigned long long) info->value.publish.completed,
248                   (unsigned long long) info->value.publish.size);
249     break;
250   case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
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, 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     GNUNET_assert (NULL != fi);
408   }
409   else
410   {
411     fi = GNUNET_FS_file_information_create_from_reader (po->fs, po,
412                                                         po->size,
413                                                         &file_generator, &po->publish_seed,
414                                                         NULL, NULL,
415                                                         po->do_index, &bo);
416     GNUNET_assert (NULL != fi);
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_new (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  */
477 static void
478 download_timeout (void *cls)
479 {
480   struct TestDownloadOperation *dop = cls;
481
482   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
483               "Timeout while trying to download file\n");
484   dop->download_timeout_task = NULL;
485   GNUNET_FS_download_stop (dop->download_context,
486                            GNUNET_YES);
487   GNUNET_SCHEDULER_add_now (dop->download_cont,
488                             dop->download_cont_cls);
489   GNUNET_TESTBED_operation_done (dop->fs_op);
490   GNUNET_FS_uri_destroy (dop->uri);
491   GNUNET_free (dop);
492 }
493
494
495 /**
496  * Task scheduled to report on the completion of our download operation.
497  *
498  * @param cls the download operation context
499  */
500 static void
501 report_success (void *cls)
502 {
503   struct TestDownloadOperation *dop = cls;
504
505   GNUNET_FS_download_stop (dop->download_context,
506                            GNUNET_YES);
507   GNUNET_SCHEDULER_add_now (dop->download_cont,
508                             dop->download_cont_cls);
509   GNUNET_TESTBED_operation_done (dop->fs_op);
510   GNUNET_FS_uri_destroy (dop->uri);
511   GNUNET_free (dop);
512 }
513
514
515 /**
516  * Progress callback for file-sharing events while downloading.
517  *
518  * @param cls the download operation context
519  * @param info information about the event
520  */
521 static void *
522 download_progress_cb (void *cls,
523                       const struct GNUNET_FS_ProgressInfo *info)
524 {
525   struct TestDownloadOperation *dop = cls;
526
527   switch (info->status)
528   {
529   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
530     if (dop->verbose)
531       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
532                   "Download at %llu/%llu bytes\n",
533                   (unsigned long long) info->value.download.completed,
534                   (unsigned long long) info->value.download.size);
535     break;
536   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
537     GNUNET_SCHEDULER_cancel (dop->download_timeout_task);
538     dop->download_timeout_task = NULL;
539     GNUNET_SCHEDULER_add_now (&report_success, dop);
540     break;
541   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
542   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
543     break;
544     /* FIXME: monitor data correctness during download progress */
545     /* FIXME: do performance reports given sufficient verbosity */
546     /* FIXME: advance timeout task to "immediate" on error */
547   default:
548     break;
549   }
550   return NULL;
551 }
552
553
554 /**
555  * Connect adapter for download operation.
556  *
557  * @param cls the 'struct TestDownloadOperation'
558  * @param cfg configuration of the peer to connect to; will be available until
559  *          GNUNET_TESTBED_operation_done() is called on the operation returned
560  *          from GNUNET_TESTBED_service_connect()
561  * @return service handle to return in 'op_result', NULL on error
562  */
563 static void *
564 download_connect_adapter (void *cls,
565                          const struct GNUNET_CONFIGURATION_Handle *cfg)
566 {
567   struct TestPublishOperation *po = cls;
568
569   return GNUNET_FS_start (cfg,
570                           "fs-test-download",
571                           &download_progress_cb, po,
572                           GNUNET_FS_FLAGS_NONE,
573                           GNUNET_FS_OPTIONS_END);
574 }
575
576
577 /**
578  * Callback to be called when testbed has connected to the fs service
579  *
580  * @param cls the 'struct TestPublishOperation'
581  * @param op the operation that has been finished
582  * @param ca_result the 'struct GNUNET_FS_Handle ' (NULL on error)
583  * @param emsg error message in case the operation has failed; will be NULL if
584  *          operation has executed successfully.
585  */
586 static void
587 download_fs_connect_complete_cb (void *cls,
588                                  struct GNUNET_TESTBED_Operation *op,
589                                  void *ca_result,
590                                  const char *emsg)
591 {
592   struct TestDownloadOperation *dop = cls;
593
594   dop->fs = ca_result;
595   GNUNET_assert (NULL != dop->fs);
596   dop->download_context =
597     GNUNET_FS_download_start (dop->fs, dop->uri, NULL, NULL, NULL, 0, dop->size,
598                               dop->anonymity, GNUNET_FS_DOWNLOAD_OPTION_NONE,
599                               NULL, NULL);
600 }
601
602
603 /**
604  * Perform test download.
605  *
606  * @param peer which peer to download from
607  * @param timeout if this operation cannot be completed within the
608  *                given period, call the continuation with an error code
609  * @param anonymity option for download
610  * @param seed used for file validation
611  * @param uri URI of file to download (CHK/LOC only)
612  * @param verbose how verbose to be in reporting
613  * @param cont function to call when done
614  * @param cont_cls closure for cont
615  */
616 void
617 GNUNET_FS_TEST_download (struct GNUNET_TESTBED_Peer *peer,
618                          struct GNUNET_TIME_Relative timeout,
619                          uint32_t anonymity, uint32_t seed,
620                          const struct GNUNET_FS_Uri *uri, unsigned int verbose,
621                          GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls)
622 {
623   struct TestDownloadOperation *dop;
624
625   dop = GNUNET_new (struct TestDownloadOperation);
626   dop->uri = GNUNET_FS_uri_dup (uri);
627   dop->size = GNUNET_FS_uri_chk_get_file_size (uri);
628   dop->verbose = verbose;
629   dop->anonymity = anonymity;
630   dop->download_cont = cont;
631   dop->download_cont_cls = cont_cls;
632   dop->download_seed = seed;
633
634   dop->fs_op = GNUNET_TESTBED_service_connect (dop,
635                                                peer,
636                                                "fs",
637                                                &download_fs_connect_complete_cb,
638                                                dop,
639                                                &download_connect_adapter,
640                                                &fs_disconnect_adapter,
641                                                dop);
642   dop->download_timeout_task =
643       GNUNET_SCHEDULER_add_delayed (timeout, &download_timeout, dop);
644 }
645
646
647 /* end of fs_test_lib.c */