fixing 1584
[oweals/gnunet.git] / src / fs / test_fs_download.c
index 04cd3e5d8e3f9655a9af40c69cecfafbc004cf7b..010b3336dd0b56c0701ed428e812d522883fc29f 100644 (file)
@@ -4,7 +4,7 @@
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
-     by the Free Software Foundation; either version 2, or (at your
+     by the Free Software Foundation; either version 3, or (at your
      option) any later version.
 
      GNUnet is distributed in the hope that it will be useful, but
@@ -41,7 +41,7 @@
 /**
  * How long until we give up on transmitting the message?
  */
-#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
 
 /**
  * How long should our test-content live?
@@ -68,17 +68,48 @@ static struct GNUNET_FS_DownloadContext *download;
 
 static struct GNUNET_FS_PublishContext *publish;
 
+static GNUNET_SCHEDULER_TaskIdentifier timeout_kill;
+
 static char *fn;
 
+static int err;
+
+static void
+timeout_kill_task (void *cls,
+                  const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  if (download != NULL)
+    {
+      GNUNET_FS_download_stop (download, GNUNET_YES);
+      download = NULL;
+    }
+  else if (publish != NULL)
+    {
+      GNUNET_FS_publish_stop (publish);
+      publish = NULL;
+    }
+  timeout_kill = GNUNET_SCHEDULER_NO_TASK;
+  err = 1;
+}
 
 static void
 abort_publish_task (void *cls,
                     const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  GNUNET_FS_publish_stop (publish);
-  publish = NULL;
+  if (publish != NULL)
+    {
+      GNUNET_FS_publish_stop (publish);
+      publish = NULL;
+    }
 }
 
+static void
+stop_fs_task (void *cls,
+             const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_FS_stop (fs);
+  fs = NULL;
+}
 
 static void
 abort_download_task (void *cls,
@@ -86,13 +117,18 @@ abort_download_task (void *cls,
 {
   uint64_t size;
   
-  GNUNET_FS_download_stop (download, GNUNET_YES);
-  download = NULL;
+  if (download != NULL)
+    {
+      GNUNET_FS_download_stop (download, GNUNET_YES);
+      download = NULL;
+    }
   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_size (fn, &size, GNUNET_YES));
   GNUNET_assert (size == FILESIZE); 
   GNUNET_DISK_directory_remove (fn);
   GNUNET_free (fn);
   fn = NULL;
+  GNUNET_SCHEDULER_cancel (sched, timeout_kill);
+  timeout_kill = GNUNET_SCHEDULER_NO_TASK;
 }
 
 
@@ -114,13 +150,13 @@ progress_cb (void *cls,
       break;
     case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
       printf ("Publishing complete, %llu kbps.\n",
-             (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
+             (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024LL));
       fn = GNUNET_DISK_mktemp ("gnunet-download-test-dst");
       start = GNUNET_TIME_absolute_get ();
       download = GNUNET_FS_download_start (fs,
                                           event->value.publish.specifics.completed.chk_uri,
                                           NULL,
-                                          fn,
+                                          fn, NULL,
                                           0,
                                           FILESIZE,
                                           1,
@@ -131,12 +167,10 @@ progress_cb (void *cls,
       break;
     case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
       printf ("Download complete,  %llu kbps.\n",
-             (unsigned long long) (FILESIZE * 1000 / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024));
-      GNUNET_SCHEDULER_add_continuation (sched,
-                                        GNUNET_NO,
-                                        &abort_download_task,
-                                        NULL,
-                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+             (unsigned long long) (FILESIZE * 1000LL / (1+GNUNET_TIME_absolute_get_duration (start).value) / 1024LL));
+      GNUNET_SCHEDULER_add_now (sched,
+                               &abort_download_task,
+                               NULL);
       break;
     case GNUNET_FS_STATUS_DOWNLOAD_PROGRESS:
       GNUNET_assert (download == event->value.download.dc);
@@ -154,7 +188,6 @@ progress_cb (void *cls,
               event->value.publish.specifics.error.message);
       GNUNET_break (0);
       GNUNET_SCHEDULER_add_continuation (sched,
-                                        GNUNET_NO,
                                         &abort_publish_task,
                                         NULL,
                                         GNUNET_SCHEDULER_REASON_PREREQ_DONE);
@@ -163,11 +196,12 @@ progress_cb (void *cls,
       fprintf (stderr,
               "Error downloading file: %s\n",
               event->value.download.specifics.error.message);
-      GNUNET_SCHEDULER_add_continuation (sched,
-                                        GNUNET_NO,
-                                        &abort_download_task,
-                                        NULL,
-                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+      GNUNET_SCHEDULER_add_now (sched,
+                               &abort_download_task,
+                               NULL);
+      break;
+    case GNUNET_FS_STATUS_DOWNLOAD_ACTIVE:
+    case GNUNET_FS_STATUS_DOWNLOAD_INACTIVE:
       break;
     case GNUNET_FS_STATUS_PUBLISH_START:
       GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
@@ -177,11 +211,12 @@ progress_cb (void *cls,
       GNUNET_assert (1 == event->value.publish.anonymity);
       break;
     case GNUNET_FS_STATUS_PUBLISH_STOPPED:
-      GNUNET_assert (publish == event->value.publish.sc);
+      GNUNET_assert (publish == event->value.publish.pc);
       GNUNET_assert (FILESIZE == event->value.publish.size);
       GNUNET_assert (1 == event->value.publish.anonymity);
-      GNUNET_FS_stop (fs);
-      fs = NULL;
+      GNUNET_SCHEDULER_add_now (sched,
+                               &stop_fs_task,
+                               NULL);
       break;
     case GNUNET_FS_STATUS_DOWNLOAD_START:
       GNUNET_assert (download == NULL);
@@ -196,7 +231,6 @@ progress_cb (void *cls,
     case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
       GNUNET_assert (download == event->value.download.dc);
       GNUNET_SCHEDULER_add_continuation (sched,
-                                        GNUNET_NO,
                                         &abort_publish_task,
                                         NULL,
                                         GNUNET_SCHEDULER_REASON_PREREQ_DONE);
@@ -215,16 +249,14 @@ setup_peer (struct PeerContext *p, const char *cfgname)
 {
   p->cfg = GNUNET_CONFIGURATION_create ();
 #if START_ARM
-  p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
+  p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
                                         "gnunet-service-arm",
 #if VERBOSE
                                         "-L", "DEBUG",
 #endif
                                         "-c", cfgname, NULL);
-  sleep (1);                    /* allow ARM to start */
 #endif
   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
-  GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
 }
 
 
@@ -275,7 +307,8 @@ run (void *cls,
     buf[i] = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 256);
   meta = GNUNET_CONTAINER_meta_data_create ();
   kuri = GNUNET_FS_uri_ksk_create_from_args (2, keywords);
-  fi = GNUNET_FS_file_information_create_from_data ("publish-context",
+  fi = GNUNET_FS_file_information_create_from_data (fs,
+                                                   "publish-context",
                                                    FILESIZE,
                                                    buf,
                                                    kuri,
@@ -287,6 +320,10 @@ run (void *cls,
   GNUNET_FS_uri_destroy (kuri);
   GNUNET_CONTAINER_meta_data_destroy (meta);
   GNUNET_assert (NULL != fi);
+  timeout_kill = GNUNET_SCHEDULER_add_delayed (sched,
+                                              TIMEOUT,
+                                              &timeout_kill_task,
+                                              NULL);
   start = GNUNET_TIME_absolute_get ();
   publish = GNUNET_FS_publish_start (fs,
                                    fi,
@@ -324,7 +361,7 @@ main (int argc, char *argv[])
                      "nohelp", options, &run, NULL);
   stop_arm (&p1);
   GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-download/");
-  return 0;
+  return err;
 }
 
 /* end of test_fs_download.c */