Merge branch 'master' of git+ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / fs / fs_test_lib.c
index bbed98249d80bef64bb9a0c6c8e3591de8bec43f..d03888021f70395aedb01db389822dd8a230367f 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2010, 2011, 2012 GNUnet e.V.
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
@@ -14,8 +14,8 @@
 
      You should have received a copy of the GNU General Public License
      along with GNUnet; see the file COPYING.  If not, write to the
-     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-     Boston, MA 02111-1307, USA.
+     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+     Boston, MA 02110-1301, USA.
 */
 
 /**
@@ -63,7 +63,7 @@ struct TestPublishOperation
   /**
    * Task to abort publishing (timeout).
    */
-  GNUNET_SCHEDULER_TaskIdentifier publish_timeout_task;
+  struct GNUNET_SCHEDULER_Task * publish_timeout_task;
 
   /**
    * Seed for file generation.
@@ -131,7 +131,7 @@ struct TestDownloadOperation
   /**
    * Function to call when download is done.
    */
-  GNUNET_SCHEDULER_Task download_cont;
+  GNUNET_SCHEDULER_TaskCallback download_cont;
 
   /**
    * Closure for download_cont.
@@ -146,7 +146,7 @@ struct TestDownloadOperation
   /**
    * Task to abort downloading (timeout).
    */
-  GNUNET_SCHEDULER_TaskIdentifier download_timeout_task;
+  struct GNUNET_SCHEDULER_Task * download_timeout_task;
 
   /**
    * Context for current download operation.
@@ -183,15 +183,21 @@ struct TestDownloadOperation
  * @param tc scheduler context (unused)
  */
 static void
-report_uri (void *cls, 
-           const struct GNUNET_SCHEDULER_TaskContext *tc)
+report_uri (void *cls)
 {
   struct TestPublishOperation *po = cls;
 
   GNUNET_FS_publish_stop (po->publish_context);
   GNUNET_TESTBED_operation_done (po->fs_op);
-  po->publish_cont (po->publish_cont_cls, po->publish_uri);
+  po->publish_cont (po->publish_cont_cls,
+                   po->publish_uri,
+                   (GNUNET_YES == po->do_index)
+                   ? po->publish_tmp_file
+                   : NULL);
   GNUNET_FS_uri_destroy (po->publish_uri);
+  if ( (GNUNET_YES != po->do_index) &&
+       (NULL != po->publish_tmp_file) )
+    (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
   GNUNET_free_non_null (po->publish_tmp_file);
   GNUNET_free (po);
 }
@@ -201,23 +207,19 @@ report_uri (void *cls,
  * Task scheduled to run when publish operation times out.
  *
  * @param cls the publish operation context
- * @param tc scheduler context (unused)
  */
 static void
-publish_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+publish_timeout (void *cls)
 {
   struct TestPublishOperation *po = cls;
 
-  po->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
+  po->publish_timeout_task = NULL;
   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
               "Timeout while trying to publish data\n");
-  if (NULL == po->fs)
-    GNUNET_TESTBED_operation_cancel (po->fs_op);
-  else
-    GNUNET_TESTBED_operation_done (po->fs_op);
-  GNUNET_FS_publish_stop (po->publish_context);
   GNUNET_TESTBED_operation_done (po->fs_op);
-  po->publish_cont (po->publish_cont_cls, NULL);
+  GNUNET_FS_publish_stop (po->publish_context);
+  po->publish_cont (po->publish_cont_cls, NULL, NULL);
+  (void) GNUNET_DISK_directory_remove (po->publish_tmp_file);
   GNUNET_free_non_null (po->publish_tmp_file);
   GNUNET_free (po);
 }
@@ -238,11 +240,11 @@ publish_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
   {
   case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
     GNUNET_SCHEDULER_cancel (po->publish_timeout_task);
-    po->publish_timeout_task = GNUNET_SCHEDULER_NO_TASK;
+    po->publish_timeout_task = NULL;
     po->publish_uri =
         GNUNET_FS_uri_dup (info->value.publish.specifics.completed.chk_uri);
-    GNUNET_SCHEDULER_add_continuation (&report_uri, po,
-                                       GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+    GNUNET_SCHEDULER_add_now (&report_uri,
+                              po);
     break;
   case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
     if (po->verbose)
@@ -250,6 +252,8 @@ publish_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
                   (unsigned long long) info->value.publish.completed,
                   (unsigned long long) info->value.publish.size);
     break;
+  case GNUNET_FS_STATUS_PUBLISH_PROGRESS_DIRECTORY:
+    break;
   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
     if (po->verbose)
       GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
@@ -269,15 +273,15 @@ publish_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
  * @param cls pointer to uint32_t with publishing seed
  * @param offset offset to generate data for
  * @param max maximum number of bytes to generate
- * @param buf where to write generated data 
+ * @param buf where to write generated data
  * @param emsg where to store error message (unused)
  * @return number of bytes written to buf
  */
 static size_t
-file_generator (void *cls, 
+file_generator (void *cls,
                uint64_t offset,
-               size_t max, 
-               void *buf, 
+               size_t max,
+               void *buf,
                char **emsg)
 {
   uint32_t *publish_seed = cls;
@@ -304,7 +308,7 @@ file_generator (void *cls,
 
 /**
  * Connect adapter for publishing operation.
- * 
+ *
  * @param cls the 'struct TestPublishOperation'
  * @param cfg configuration of the peer to connect to; will be available until
  *          GNUNET_TESTBED_operation_done() is called on the operation returned
@@ -316,21 +320,22 @@ publish_connect_adapter (void *cls,
                         const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct TestPublishOperation *po = cls;
+
   return GNUNET_FS_start (cfg,
                          "fs-test-publish",
                          &publish_progress_cb, po,
+                         GNUNET_FS_FLAGS_NONE,
                          GNUNET_FS_OPTIONS_END);
 }
 
 
 /**
  * Adapter function called to destroy connection to file-sharing service.
- * 
+ *
  * @param cls the 'struct GNUNET_FS_Handle'
  * @param op_result unused (different for publish/download!)
  */
-static void 
+static void
 fs_disconnect_adapter (void *cls,
                       void *op_result)
 {
@@ -368,7 +373,7 @@ publish_fs_connect_complete_cb (void *cls,
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to connect to FS for publishing: %s\n", emsg);
       po->publish_cont (po->publish_cont_cls,
-                       NULL);
+                       NULL, NULL);
       GNUNET_TESTBED_operation_done (po->fs_op);
       GNUNET_free (po);
       return;
@@ -404,14 +409,16 @@ publish_fs_connect_complete_cb (void *cls,
                                                       po->publish_tmp_file,
                                                       NULL, NULL, po->do_index,
                                                       &bo);
+    GNUNET_assert (NULL != fi);
   }
   else
   {
     fi = GNUNET_FS_file_information_create_from_reader (po->fs, po,
-                                                        po->size, 
-                                                       &file_generator, &po->publish_seed, 
+                                                        po->size,
+                                                       &file_generator, &po->publish_seed,
                                                        NULL, NULL,
                                                         po->do_index, &bo);
+    GNUNET_assert (NULL != fi);
   }
   po->publish_context =
     GNUNET_FS_publish_start (po->fs, fi, NULL, NULL, NULL,
@@ -443,7 +450,7 @@ GNUNET_FS_TEST_publish (struct GNUNET_TESTBED_Peer *peer,
 {
   struct TestPublishOperation *po;
 
-  po = GNUNET_malloc (sizeof (struct TestPublishOperation));
+  po = GNUNET_new (struct TestPublishOperation);
   po->publish_cont = cont;
   po->publish_cont_cls = cont_cls;
   po->publish_seed = seed;
@@ -471,24 +478,20 @@ GNUNET_FS_TEST_publish (struct GNUNET_TESTBED_Peer *peer,
  * Task scheduled to run when download operation times out.
  *
  * @param cls the download operation context
- * @param tc scheduler context (unused)
  */
 static void
-download_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+download_timeout (void *cls)
 {
   struct TestDownloadOperation *dop = cls;
 
   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
               "Timeout while trying to download file\n");
-  dop->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
-  GNUNET_FS_download_stop (dop->download_context, GNUNET_YES);
-  GNUNET_SCHEDULER_add_continuation (dop->download_cont,
-                                     dop->download_cont_cls,
-                                     GNUNET_SCHEDULER_REASON_TIMEOUT);
-  if (NULL == dop->fs)
-    GNUNET_TESTBED_operation_cancel (dop->fs_op);
-  else
-    GNUNET_TESTBED_operation_done (dop->fs_op);
+  dop->download_timeout_task = NULL;
+  GNUNET_FS_download_stop (dop->download_context,
+                           GNUNET_YES);
+  GNUNET_SCHEDULER_add_now (dop->download_cont,
+                            dop->download_cont_cls);
+  GNUNET_TESTBED_operation_done (dop->fs_op);
   GNUNET_FS_uri_destroy (dop->uri);
   GNUNET_free (dop);
 }
@@ -498,18 +501,16 @@ download_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Task scheduled to report on the completion of our download operation.
  *
  * @param cls the download operation context
- * @param tc scheduler context (unused)
  */
 static void
-report_success (void *cls,
-               const struct GNUNET_SCHEDULER_TaskContext *tc)
+report_success (void *cls)
 {
   struct TestDownloadOperation *dop = cls;
 
-  GNUNET_FS_download_stop (dop->download_context, GNUNET_YES);
-  GNUNET_SCHEDULER_add_continuation (dop->download_cont,
-                                     dop->download_cont_cls,
-                                     GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+  GNUNET_FS_download_stop (dop->download_context,
+                           GNUNET_YES);
+  GNUNET_SCHEDULER_add_now (dop->download_cont,
+                            dop->download_cont_cls);
   GNUNET_TESTBED_operation_done (dop->fs_op);
   GNUNET_FS_uri_destroy (dop->uri);
   GNUNET_free (dop);
@@ -523,7 +524,8 @@ report_success (void *cls,
  * @param info information about the event
  */
 static void *
-download_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
+download_progress_cb (void *cls,
+                      const struct GNUNET_FS_ProgressInfo *info)
 {
   struct TestDownloadOperation *dop = cls;
 
@@ -531,15 +533,15 @@ download_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
   {
   case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
     if (dop->verbose)
-      GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Download at %llu/%llu bytes\n",
+      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                  "Download at %llu/%llu bytes\n",
                   (unsigned long long) info->value.download.completed,
                   (unsigned long long) info->value.download.size);
     break;
   case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
     GNUNET_SCHEDULER_cancel (dop->download_timeout_task);
-    dop->download_timeout_task = GNUNET_SCHEDULER_NO_TASK;
-    GNUNET_SCHEDULER_add_continuation (&report_success, dop,
-                                       GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+    dop->download_timeout_task = NULL;
+    GNUNET_SCHEDULER_add_now (&report_success, dop);
     break;
   case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
   case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
@@ -556,7 +558,7 @@ download_progress_cb (void *cls, const struct GNUNET_FS_ProgressInfo *info)
 
 /**
  * Connect adapter for download operation.
- * 
+ *
  * @param cls the 'struct TestDownloadOperation'
  * @param cfg configuration of the peer to connect to; will be available until
  *          GNUNET_TESTBED_operation_done() is called on the operation returned
@@ -568,10 +570,11 @@ download_connect_adapter (void *cls,
                         const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct TestPublishOperation *po = cls;
+
   return GNUNET_FS_start (cfg,
                          "fs-test-download",
                          &download_progress_cb, po,
+                         GNUNET_FS_FLAGS_NONE,
                          GNUNET_FS_OPTIONS_END);
 }
 
@@ -593,6 +596,8 @@ download_fs_connect_complete_cb (void *cls,
 {
   struct TestDownloadOperation *dop = cls;
 
+  dop->fs = ca_result;
+  GNUNET_assert (NULL != dop->fs);
   dop->download_context =
     GNUNET_FS_download_start (dop->fs, dop->uri, NULL, NULL, NULL, 0, dop->size,
                              dop->anonymity, GNUNET_FS_DOWNLOAD_OPTION_NONE,
@@ -618,11 +623,11 @@ GNUNET_FS_TEST_download (struct GNUNET_TESTBED_Peer *peer,
                          struct GNUNET_TIME_Relative timeout,
                          uint32_t anonymity, uint32_t seed,
                          const struct GNUNET_FS_Uri *uri, unsigned int verbose,
-                         GNUNET_SCHEDULER_Task cont, void *cont_cls)
+                         GNUNET_SCHEDULER_TaskCallback cont, void *cont_cls)
 {
   struct TestDownloadOperation *dop;
 
-  dop = GNUNET_malloc (sizeof (struct TestDownloadOperation));
+  dop = GNUNET_new (struct TestDownloadOperation);
   dop->uri = GNUNET_FS_uri_dup (uri);
   dop->size = GNUNET_FS_uri_chk_get_file_size (uri);
   dop->verbose = verbose;