fixing common off-by-one error with respect to maximum message size
[oweals/gnunet.git] / src / fs / test_fs_search_persistence.c
index ad0e12b4bc8a5ed6d5bf272b3ec0162217d0ef74..7ea0130ae260d9ce38735e407c1283b96a080760 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet.
-     (C) 2004, 2005, 2006 Christian Grothoff (and other contributing authors)
+     (C) 2004, 2005, 2006, 2008, 2009, 2010 Christian Grothoff (and other contributing authors)
 
      GNUnet is free software; you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published
 */
 
 /**
- * @file applications/fs/fsui/search_persistence_test.c
- * @brief testcase for fsui download persistence for search
+ * @file fs/test_fs_search_persistence.c
+ * @brief simple testcase for persistence of search operation
  * @author Christian Grothoff
  */
 
 #include "platform.h"
-#include "gnunet_util.h"
-#include "gnunet_fsui_lib.h"
+#include "gnunet_util_lib.h"
+#include "gnunet_arm_service.h"
+#include "gnunet_fs_service.h"
 
-#define DEBUG_VERBOSE GNUNET_NO
+#define VERBOSE GNUNET_NO
 
-#define UPLOAD_PREFIX "/tmp/gnunet-fsui-search_persistence_test"
+#define START_ARM GNUNET_YES
 
-#define CHECK(a) if (!(a)) { ok = GNUNET_NO; GNUNET_GE_BREAK(ectx, 0); goto FAILURE; }
+/**
+ * File-size we use for testing.
+ */
+#define FILESIZE 1024
+
+/**
+ * How long until we give up on transmitting the message?
+ */
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
+
+/**
+ * How long should our test-content live?
+ */ 
+#define LIFETIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 15)
+
+struct PeerContext
+{
+  struct GNUNET_CONFIGURATION_Handle *cfg;
+  struct GNUNET_PeerIdentity id;   
+#if START_ARM
+  pid_t arm_pid;
+#endif
+};
+
+static struct PeerContext p1;
+
+static struct GNUNET_TIME_Absolute start;
+
+static struct GNUNET_SCHEDULER_Handle *sched;
+
+static struct GNUNET_FS_Handle *fs;
+
+static struct GNUNET_FS_SearchContext *search;
+
+static struct GNUNET_FS_PublishContext *publish;
+
+static const struct GNUNET_CONFIGURATION_Handle *cfg;
+
+static void
+abort_publish_task (void *cls,
+                    const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_FS_publish_stop (publish);
+  publish = NULL;
+}
 
-static struct GNUNET_GE_Context *ectx;
 
-static struct GNUNET_FSUI_Context *ctx;
-static struct GNUNET_FSUI_SearchList *search;
-static int have_error;
+static void
+abort_search_task (void *cls,
+                    const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  if (search != NULL)
+    GNUNET_FS_search_stop (search);
+  search = NULL;
+}
+
 
 static void *
-eventCallback (void *cls, const GNUNET_FSUI_Event * event)
+progress_cb (void *cls, 
+            const struct GNUNET_FS_ProgressInfo *event);
+
+
+static void
+restart_fs_task (void *cls,
+                const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_FS_stop (fs);
+  fs = GNUNET_FS_start (sched,
+                       cfg,
+                       "test-fs-search-persistence",
+                       &progress_cb,
+                       NULL,
+                       GNUNET_FS_FLAGS_PERSISTENCE,
+                       GNUNET_FS_OPTIONS_END);
+}
+
+
+
+
+/**
+ * Consider scheduling the restart-task. 
+ * Only runs the restart task once per event 
+ * category.
+ *
+ * @param ev type of the event to consider
+ */
+static void
+consider_restart (int ev)
 {
-  switch (event->type)
+  static int prev[32];
+  static int off;
+  int i;
+  for (i=0;i<off;i++)
+    if (prev[i] == ev)
+      return;
+  prev[off++] = ev;
+  GNUNET_SCHEDULER_add_with_priority (sched,
+                                     GNUNET_SCHEDULER_PRIORITY_URGENT,
+                                     &restart_fs_task,
+                                     NULL);
+}
+
+
+static void *
+progress_cb (void *cls, 
+            const struct GNUNET_FS_ProgressInfo *event)
+{  
+  const char *keywords[] = {
+    "down_foo"
+  };
+  struct GNUNET_FS_Uri *kuri;
+
+  switch (event->status)
     {
-    case GNUNET_FSUI_search_suspended:
-      search = NULL;
+    case GNUNET_FS_STATUS_PUBLISH_PROGRESS:
+#if VERBOSE
+      printf ("Publish is progressing (%llu/%llu at level %u off %llu)...\n",
+              (unsigned long long) event->value.publish.completed,
+              (unsigned long long) event->value.publish.size,
+             event->value.publish.specifics.progress.depth,
+             (unsigned long long) event->value.publish.specifics.progress.offset);
+#endif      
       break;
-    case GNUNET_FSUI_search_resumed:
-#if DEBUG_VERBOSE
-      printf ("Search resuming\n");
-#endif
-      search = event->data.SearchResumed.sc.pos;
+    case GNUNET_FS_STATUS_PUBLISH_COMPLETED:
+      kuri = GNUNET_FS_uri_ksk_create_from_args (1, keywords);
+      start = GNUNET_TIME_absolute_get ();
+      GNUNET_FS_search_start (fs,
+                             kuri,
+                             1,
+                             GNUNET_FS_SEARCH_OPTION_NONE,
+                             "search");
+      GNUNET_FS_uri_destroy (kuri);
+      GNUNET_assert (search != NULL);
       break;
-    case GNUNET_FSUI_search_result:
-#if DEBUG_VERBOSE
-      printf ("Received search result\n");
-#endif
+    case GNUNET_FS_STATUS_PUBLISH_SUSPEND:
+      if  (event->value.publish.pc == publish)
+       publish = NULL;
       break;
-    case GNUNET_FSUI_upload_progress:
-#if DEBUG_VERBOSE
-      printf ("Upload is progressing (%llu/%llu)...\n",
-              event->data.UploadProgress.completed,
-              event->data.UploadProgress.total);
-#endif
+    case GNUNET_FS_STATUS_PUBLISH_RESUME:
+      if (NULL == publish)
+       publish = event->value.publish.pc;
       break;
-    case GNUNET_FSUI_upload_completed:
-#if DEBUG_VERBOSE
-      printf ("Upload complete.\n");
+    case GNUNET_FS_STATUS_SEARCH_RESULT:
+      /* FIXME: consider_restart (event->status); cannot be tested with
+        search result since we exit here after the first one... */
+#if VERBOSE
+      printf ("Search complete.\n");
 #endif
+      GNUNET_SCHEDULER_add_continuation (sched,
+                                        &abort_search_task,
+                                        NULL,
+                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
       break;
-    case GNUNET_FSUI_unindex_progress:
-#if DEBUG_VERBOSE
-      printf ("Unindex is progressing (%llu/%llu)...\n",
-              event->data.UnindexProgress.completed,
-              event->data.UnindexProgress.total);
-#endif
+    case GNUNET_FS_STATUS_PUBLISH_ERROR:
+      fprintf (stderr,
+              "Error publishing file: %s\n",
+              event->value.publish.specifics.error.message);
+      GNUNET_break (0);
+      GNUNET_SCHEDULER_add_continuation (sched,
+                                        &abort_publish_task,
+                                        NULL,
+                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
       break;
-    case GNUNET_FSUI_unindex_completed:
-#if DEBUG_VERBOSE
-      printf ("Unindex complete.\n");
-#endif
+    case GNUNET_FS_STATUS_SEARCH_ERROR:
+      fprintf (stderr,
+              "Error searching file: %s\n",
+              event->value.search.specifics.error.message);
+      GNUNET_SCHEDULER_add_continuation (sched,
+                                        &abort_search_task,
+                                        NULL,
+                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
       break;
-    case GNUNET_FSUI_unindex_error:
-    case GNUNET_FSUI_upload_error:
-    case GNUNET_FSUI_download_error:
-      fprintf (stderr, "Received ERROR: %d\n", event->type);
-      GNUNET_GE_BREAK (ectx, 0);
+    case GNUNET_FS_STATUS_SEARCH_SUSPEND:
+      if  (event->value.search.sc == search)
+       search = NULL;
       break;
-    case GNUNET_FSUI_download_aborted:
-#if DEBUG_VERBOSE
-      printf ("Received download aborted event.\n");
-#endif
+    case GNUNET_FS_STATUS_SEARCH_RESUME:
+      if (NULL == search)
+       {
+         search = event->value.search.sc;
+         return "search";
+       }
       break;
-    case GNUNET_FSUI_unindex_suspended:
-    case GNUNET_FSUI_upload_suspended:
-#if DEBUG_VERBOSE
-      fprintf (stderr, "Received SUSPENDING: %d\n", event->type);
-#endif
+    case GNUNET_FS_STATUS_PUBLISH_START:
+      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:
+      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;
+      break;
+    case GNUNET_FS_STATUS_SEARCH_START:
+      consider_restart (event->status);
+      GNUNET_assert (search == NULL);
+      search = event->value.search.sc;
+      GNUNET_assert (0 == strcmp ("search", event->value.search.cctx));
+      GNUNET_assert (1 == event->value.search.anonymity);
       break;
-    case GNUNET_FSUI_upload_started:
-    case GNUNET_FSUI_upload_stopped:
-    case GNUNET_FSUI_search_started:
-    case GNUNET_FSUI_search_aborted:
-    case GNUNET_FSUI_search_stopped:
-    case GNUNET_FSUI_search_update:
-    case GNUNET_FSUI_unindex_started:
-    case GNUNET_FSUI_unindex_stopped:
+    case GNUNET_FS_STATUS_SEARCH_RESULT_STOPPED:
+      break;
+    case GNUNET_FS_STATUS_SEARCH_STOPPED:
+      GNUNET_assert (search == event->value.search.sc);
+      GNUNET_SCHEDULER_add_continuation (sched,
+                                        &abort_publish_task,
+                                        NULL,
+                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+      search = NULL;
       break;
     default:
-      printf ("Unexpected event: %d\n", event->type);
+      fprintf (stderr,
+              "Unexpected event: %d\n", 
+              event->status);
       break;
     }
   return NULL;
 }
 
-#define FILESIZE (1024)
 
-#define START_DAEMON 1
+static void
+setup_peer (struct PeerContext *p, const char *cfgname)
+{
+  p->cfg = GNUNET_CONFIGURATION_create ();
+#if START_ARM
+  p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
+                                        "gnunet-service-arm",
+#if VERBOSE
+                                        "-L", "DEBUG",
+#endif
+                                        "-c", cfgname, NULL);
+#endif
+  GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
+}
+
 
-int
-main (int argc, char *argv[])
+static void
+stop_arm (struct PeerContext *p)
 {
-#if START_DAEMON
-  pid_t daemon;
+#if START_ARM
+  if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
+  if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "ARM process %u stopped\n", p->arm_pid);
 #endif
-  int ok;
-  struct GNUNET_ECRS_URI *uri = NULL;
-  char *keywords[] = {
+  GNUNET_CONFIGURATION_destroy (p->cfg);
+}
+
+
+static void
+run (void *cls,
+     struct GNUNET_SCHEDULER_Handle *s,
+     char *const *args,
+     const char *cfgfile,
+     const struct GNUNET_CONFIGURATION_Handle *c)
+{
+  const char *keywords[] = {
     "down_foo",
-    "down_bar",
+    "down_bar"
   };
-  char keyword[40];
-  int prog;
-  struct GNUNET_GC_Configuration *cfg;
-  int suspendRestart = 0;
+  char *buf;
+  struct GNUNET_CONTAINER_MetaData *meta;
+  struct GNUNET_FS_Uri *kuri;
+  struct GNUNET_FS_FileInformation *fi;
+  size_t i;
 
+  sched = s;
+  cfg = c;
+  setup_peer (&p1, "test_fs_search_data.conf");
+  fs = GNUNET_FS_start (sched,
+                       cfg,
+                       "test-fs-search-persistence",
+                       &progress_cb,
+                       NULL,
+                       GNUNET_FS_FLAGS_PERSISTENCE,
+                       GNUNET_FS_OPTIONS_END);
+  GNUNET_assert (NULL != fs); 
+  buf = GNUNET_malloc (FILESIZE);
+  for (i = 0; i < FILESIZE; i++)
+    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 (fs,
+                                                   "publish-context",
+                                                   FILESIZE,
+                                                   buf,
+                                                   kuri,
+                                                   meta,
+                                                   GNUNET_NO,
+                                                   1,
+                                                   42,
+                                                   GNUNET_TIME_relative_to_absolute (LIFETIME)); 
+  GNUNET_FS_uri_destroy (kuri);
+  GNUNET_CONTAINER_meta_data_destroy (meta);
+  GNUNET_assert (NULL != fi);
+  start = GNUNET_TIME_absolute_get ();
+  publish = GNUNET_FS_publish_start (fs,
+                                   fi,
+                                   NULL, NULL, NULL,
+                                   GNUNET_FS_PUBLISH_OPTION_NONE);
+  GNUNET_assert (publish != NULL);
+}
 
-  ok = GNUNET_YES;
-  cfg = GNUNET_GC_create ();
-  if (-1 == GNUNET_GC_parse_configuration (cfg, "check.conf"))
-    {
-      GNUNET_GC_free (cfg);
-      return -1;
-    }
-#if START_DAEMON
-  daemon = GNUNET_daemon_start (NULL, cfg, "peer.conf", GNUNET_NO);
-  GNUNET_GE_ASSERT (NULL, daemon > 0);
-  CHECK (GNUNET_OK ==
-         GNUNET_wait_for_daemon_running (NULL, cfg,
-                                         30 * GNUNET_CRON_SECONDS));
-  GNUNET_thread_sleep (5 * GNUNET_CRON_SECONDS);        /* give apps time to start */
-  /* ACTUAL TEST CODE */
-#endif
-  ctx = GNUNET_FSUI_start (NULL,
-                           cfg, "search_persistence_test", 32, GNUNET_YES,
-                           &eventCallback, NULL);
-  CHECK (ctx != NULL);
-  GNUNET_snprintf (keyword, 40, "+%s +%s", keywords[0], keywords[1]);
-  uri = GNUNET_ECRS_keyword_string_to_uri (ectx, keyword);
-  search = GNUNET_FSUI_search_start (ctx, 0, uri);
-  CHECK (search != NULL);
-  prog = 0;
-  suspendRestart = 10;
-  while (prog < 100)
-    {
-      prog++;
-      GNUNET_thread_sleep (50 * GNUNET_CRON_MILLISECONDS);
-      if ((suspendRestart > 0)
-          && (GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK, 10) == 0))
-        {
-#if 1
-#if DEBUG_VERBOSE
-          printf ("Testing FSUI suspend-resume\n");
-#endif
-          GNUNET_FSUI_stop (ctx);       /* download possibly incomplete
-                                           at this point, thus testing resume */
-          CHECK (search == NULL);
-          ctx = GNUNET_FSUI_start (NULL,
-                                   cfg,
-                                   "search_persistence_test", 32, GNUNET_YES,
-                                   &eventCallback, NULL);
-#if DEBUG_VERBOSE
-          printf ("Resumed...\n");
-#endif
+
+int
+main (int argc, char *argv[])
+{
+  char *const argvx[] = { 
+    "test-fs-search-persistence",
+    "-c",
+    "test_fs_search_data.conf",
+#if VERBOSE
+    "-L", "DEBUG",
 #endif
-          suspendRestart--;
-        }
-      if (GNUNET_shutdown_test () == GNUNET_YES)
-        break;
-    }
-  GNUNET_FSUI_search_abort (search);
-  GNUNET_FSUI_search_stop (search);
-  search = NULL;
-  /* END OF TEST CODE */
-FAILURE:
-  if (ctx != NULL)
-    GNUNET_FSUI_stop (ctx);
-  if (uri != NULL)
-    GNUNET_ECRS_uri_destroy (uri);
-
-#if START_DAEMON
-  GNUNET_GE_BREAK (NULL, GNUNET_OK == GNUNET_daemon_stop (NULL, daemon));
+    NULL
+  };
+  struct GNUNET_GETOPT_CommandLineOption options[] = {
+    GNUNET_GETOPT_OPTION_END
+  };
+
+  GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-search/");
+  GNUNET_log_setup ("test_fs_search_persistence", 
+#if VERBOSE
+                   "DEBUG",
+#else
+                   "WARNING",
 #endif
-  GNUNET_GC_free (cfg);
-  if (have_error)
-    ok = GNUNET_NO;
-  return (ok == GNUNET_YES) ? 0 : 1;
+                   NULL);
+  GNUNET_PROGRAM_run ((sizeof (argvx) / sizeof (char *)) - 1,
+                      argvx, "test-fs-search-persistence",
+                     "nohelp", options, &run, NULL);
+  stop_arm (&p1);
+  GNUNET_DISK_directory_remove ("/tmp/gnunet-test-fs-search/");
+  return 0;
 }
 
-/* end of search_persistence_test.c */
+/* end of test_fs_search_persistence.c */