fix
[oweals/gnunet.git] / src / fs / test_fs_download.c
index f6fb2a1f2e64d18c469d4e17b74c9bf961e28749..5cfbc391ba5e1e52fcc5ba5a18a3c57dae873df9 100644 (file)
@@ -20,7 +20,7 @@
 
 /**
  * @file fs/test_fs_download.c
- * @brief simple testcase for download
+ * @brief simple testcase for simple publish + download operation
  * @author Christian Grothoff
  */
 
 #include "gnunet_arm_service.h"
 #include "gnunet_fs_service.h"
 
-#define VERBOSE GNUNET_YES
+#define VERBOSE GNUNET_NO
 
 #define START_ARM GNUNET_YES
 
+/**
+ * File-size we use for testing.
+ */
 #define FILESIZE (1024 * 1024 * 2)
 
 /**
  * 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?
@@ -48,7 +51,6 @@
 struct PeerContext
 {
   struct GNUNET_CONFIGURATION_Handle *cfg;
-  struct GNUNET_PeerIdentity id;   
 #if START_ARM
   pid_t arm_pid;
 #endif
@@ -56,6 +58,7 @@ struct PeerContext
 
 static struct PeerContext p1;
 
+static struct GNUNET_TIME_Absolute start;
 
 static struct GNUNET_SCHEDULER_Handle *sched;
 
@@ -65,24 +68,67 @@ 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
-abort_download_task (void *cls,
+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_download_stop (download, GNUNET_YES);
-  download = 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_publish_task (void *cls,
+abort_download_task (void *cls,
                     const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  GNUNET_FS_publish_stop (publish);
-  publish = NULL;
+  uint64_t size;
+  
+  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;
 }
 
 
@@ -100,40 +146,34 @@ progress_cb (void *cls,
               (unsigned long long) event->value.publish.size,
              event->value.publish.specifics.progress.depth,
              (unsigned long long) event->value.publish.specifics.progress.offset);
-#endif
+#endif      
       break;
     case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
-#if VERBOSE
-      printf ("Publish complete.\n");
-#endif
-      GNUNET_SCHEDULER_add_continuation (sched,
-                                        GNUNET_NO,
-                                        &abort_publish_task,
-                                        NULL,
-                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
-      fn = GNUNET_DISK_mktemp ("gnunet-download-test-dstXXXXXX");
+      printf ("Publishing complete, %llu kbps.\n",
+             (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,
                                           GNUNET_FS_DOWNLOAD_OPTION_NONE,
+                                          "download",
                                           NULL);
       GNUNET_assert (download != NULL);
       break;
     case GNUNET_FS_STATUS_DOWNLOAD_COMPLETED:
-#if VERBOSE
-      printf ("Download complete.\n");
-#endif
-      GNUNET_SCHEDULER_add_continuation (sched,
-                                        GNUNET_NO,
-                                        &abort_download_task,
-                                        NULL,
-                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+      printf ("Download complete,  %llu kbps.\n",
+             (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);
 #if VERBOSE
       printf ("Download is progressing (%llu/%llu at level %u off %llu)...\n",
               (unsigned long long) event->value.download.completed,
@@ -148,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);
@@ -157,25 +196,44 @@ 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:
-      /* FIXME: add checks here... */
+      GNUNET_assert (0 == strcmp ("publish-context", event->value.publish.cctx));
+      GNUNET_assert (NULL == event->value.publish.pctx);
+      GNUNET_assert (FILESIZE == event->value.publish.size);
+      GNUNET_assert (0 == event->value.publish.completed);
+      GNUNET_assert (1 == event->value.publish.anonymity);
       break;
     case GNUNET_FS_STATUS_PUBLISH_STOPPED:
-      /* FIXME: add checks here... */
+      GNUNET_assert (publish == event->value.publish.pc);
+      GNUNET_assert (FILESIZE == event->value.publish.size);
+      GNUNET_assert (1 == event->value.publish.anonymity);
+      GNUNET_SCHEDULER_add_now (sched,
+                               &stop_fs_task,
+                               NULL);
       break;
     case GNUNET_FS_STATUS_DOWNLOAD_START:
-      /* FIXME: add checks here... */
+      GNUNET_assert (download == NULL);
+      GNUNET_assert (0 == strcmp ("download", event->value.download.cctx));
+      GNUNET_assert (NULL == event->value.download.pctx);
+      GNUNET_assert (NULL != event->value.download.uri);
+      GNUNET_assert (0 == strcmp (fn, event->value.download.filename));
+      GNUNET_assert (FILESIZE == event->value.download.size);
+      GNUNET_assert (0 == event->value.download.completed);
+      GNUNET_assert (1 == event->value.download.anonymity);
       break;
     case GNUNET_FS_STATUS_DOWNLOAD_STOPPED:
-      /* FIXME: add checks here... */
-      GNUNET_FS_stop (fs);
-      fs = NULL;
+      GNUNET_assert (download == event->value.download.dc);
+      GNUNET_SCHEDULER_add_continuation (sched,
+                                        &abort_publish_task,
+                                        NULL,
+                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
       break;
     default:
       printf ("Unexpected event: %d\n", 
@@ -191,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);
 }
 
 
@@ -251,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 ("file-to-publish",
+  fi = GNUNET_FS_file_information_create_from_data (fs,
+                                                   "publish-context",
                                                    FILESIZE,
                                                    buf,
                                                    kuri,
@@ -263,8 +320,12 @@ 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,
-                                   "publish-context",
                                    fi,
                                    NULL, NULL, NULL,
                                    GNUNET_FS_PUBLISH_OPTION_NONE);
@@ -299,7 +360,8 @@ main (int argc, char *argv[])
                       argvx, "test-fs-download",
                      "nohelp", options, &run, NULL);
   stop_arm (&p1);
-  return 0;
+  GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-download/");
+  return err;
 }
 
 /* end of test_fs_download.c */