plugin datastore mysql
[oweals/gnunet.git] / src / hostlist / test_gnunet_daemon_hostlist_learning.c
index 9ef8812f6a2fb1a8aa09cdb57910129627f17af3..717311be74dbb03c1d7bfdac50638997429ee49d 100644 (file)
@@ -1,6 +1,6 @@
 /*
      This file is part of GNUnet
-     (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
+     Copyright (C) 2009, 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
 
      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.
 */
 /**
- * @file hostlist/test_gnunet_daemon_hostlist.c
+ * @file hostlist/test_gnunet_daemon_hostlist_learning.c
  * @brief test for gnunet_daemon_hostslist.c
  * @author Christian Grothoff
  */
 #include "gnunet_resolver_service.h"
 #include "gnunet_statistics_service.h"
 
-#define VERBOSE GNUNET_NO
-
-#define START_ARM GNUNET_YES
-
 #define MAX_URL_LEN 1000
 
 /**
@@ -41,7 +37,7 @@
  */
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 180)
 
-#define CHECK_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
+#define CHECK_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
 
 
 struct PeerContext
@@ -51,9 +47,7 @@ struct PeerContext
   struct GNUNET_MessageHeader *hello;
   struct GNUNET_CORE_Handle *core;
   struct GNUNET_STATISTICS_Handle *stats;
-#if START_ARM
   struct GNUNET_OS_Process *arm_proc;
-#endif
 };
 
 static int timeout;
@@ -70,9 +64,9 @@ static char *current_adv_uri;
 
 static const struct GNUNET_CONFIGURATION_Handle *cfg;
 
-static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
+static struct GNUNET_SCHEDULER_Task * timeout_task;
 
-static GNUNET_SCHEDULER_TaskIdentifier check_task;
+static struct GNUNET_SCHEDULER_Task * check_task;
 
 static struct PeerContext adv_peer;
 
@@ -89,10 +83,10 @@ static void
 shutdown_testcase ()
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown testcase....\n");
-  if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
+  if (timeout_task != NULL)
   {
     GNUNET_SCHEDULER_cancel (timeout_task);
-    timeout_task = GNUNET_SCHEDULER_NO_TASK;
+    timeout_task = NULL;
   }
   if (NULL != download_stats)
   {
@@ -109,10 +103,20 @@ shutdown_testcase ()
     GNUNET_STATISTICS_get_cancel (advsent_stat);
     advsent_stat = NULL;
   }
-  if (check_task != GNUNET_SCHEDULER_NO_TASK)
+  if (NULL != adv_peer.stats)
+  {
+    GNUNET_STATISTICS_destroy (adv_peer.stats, GNUNET_NO);
+    adv_peer.stats = NULL;
+  }
+  if (NULL != learn_peer.stats)
+  {
+    GNUNET_STATISTICS_destroy (learn_peer.stats, GNUNET_NO);
+    learn_peer.stats = NULL;
+  }
+  if (check_task != NULL)
   {
     GNUNET_SCHEDULER_cancel (check_task);
-    check_task = GNUNET_SCHEDULER_NO_TASK;
+    check_task = NULL;
   }
   if (NULL != current_adv_uri)
   {
@@ -139,24 +143,22 @@ shutdown_testcase ()
     GNUNET_CORE_disconnect (learn_peer.core);
     learn_peer.core = NULL;
   }
-#if START_ARM
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Killing hostlist server ARM process.\n");
-  if (0 != GNUNET_OS_process_kill (adv_peer.arm_proc, SIGTERM))
+  if (0 != GNUNET_OS_process_kill (adv_peer.arm_proc, GNUNET_TERM_SIG))
     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
   if (GNUNET_OS_process_wait (adv_peer.arm_proc) != GNUNET_OK)
     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
-  GNUNET_OS_process_close (adv_peer.arm_proc);
+  GNUNET_OS_process_destroy (adv_peer.arm_proc);
   adv_peer.arm_proc = NULL;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Killing hostlist client ARM process.\n");
-  if (0 != GNUNET_OS_process_kill (learn_peer.arm_proc, SIGTERM))
+  if (0 != GNUNET_OS_process_kill (learn_peer.arm_proc, GNUNET_TERM_SIG))
     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
   if (GNUNET_OS_process_wait (learn_peer.arm_proc) != GNUNET_OK)
     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
-  GNUNET_OS_process_close (learn_peer.arm_proc);
+  GNUNET_OS_process_destroy (learn_peer.arm_proc);
   learn_peer.arm_proc = NULL;
-#endif
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown complete....\n");
 }
 
@@ -164,9 +166,9 @@ shutdown_testcase ()
  * Timeout, give up.
  */
 static void
-timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+timeout_error (void *cls)
 {
-  timeout_task = GNUNET_SCHEDULER_NO_TASK;
+  timeout_task = NULL;
   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
               "Timeout while executing testcase, test failed.\n");
   timeout = GNUNET_YES;
@@ -181,6 +183,13 @@ process_downloads_done (void *cls, int success)
 }
 
 
+static void
+do_shutdown (void *cls)
+{
+  shutdown_testcase ();
+}
+
+
 static int
 process_downloads (void *cls, const char *subsystem, const char *name,
                    uint64_t value, int is_persistent)
@@ -191,7 +200,9 @@ process_downloads (void *cls, const char *subsystem, const char *name,
                 "Peer has successfully downloaded advertised URI\n");
     learned_hostlist_downloaded = GNUNET_YES;
     if ((learned_hostlist_saved == GNUNET_YES) && (adv_sent == GNUNET_YES))
-      shutdown_testcase ();
+    {
+      GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
+    }
   }
   return GNUNET_OK;
 }
@@ -215,7 +226,9 @@ process_uris_recv (void *cls, const char *subsystem, const char *name,
                 "Peer has successfully saved advertised URI\n");
     learned_hostlist_saved = GNUNET_YES;
     if ((learned_hostlist_downloaded == GNUNET_YES) && (adv_sent == GNUNET_YES))
-      shutdown_testcase ();
+    {
+      GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
+    }
   }
   return GNUNET_OK;
 }
@@ -229,7 +242,9 @@ process_adv_sent_done (void *cls, int success)
 
 
 static int
-process_adv_sent (void *cls, const char *subsystem, const char *name,
+process_adv_sent (void *cls,
+                 const char *subsystem,
+                 const char *name,
                   uint64_t value, int is_persistent)
 {
   if ((value >= 1) && (adv_sent == GNUNET_NO))
@@ -239,7 +254,9 @@ process_adv_sent (void *cls, const char *subsystem, const char *name,
     adv_sent = GNUNET_YES;
     if ((learned_hostlist_downloaded == GNUNET_YES) &&
         (learned_hostlist_saved == GNUNET_YES))
-      shutdown_testcase ();
+    {
+      GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
+    }
   }
   return GNUNET_OK;
 }
@@ -249,14 +266,13 @@ process_adv_sent (void *cls, const char *subsystem, const char *name,
  * Check the server statistics regularly
  */
 static void
-check_statistics (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+check_statistics (void *cls)
 {
   char *stat;
 
-  check_task = GNUNET_SCHEDULER_NO_TASK;
-  if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
-    return;
-  GNUNET_asprintf (&stat, gettext_noop ("# advertised URI `%s' downloaded"),
+  check_task = NULL;
+  GNUNET_asprintf (&stat,
+                  gettext_noop ("# advertised URI `%s' downloaded"),
                    current_adv_uri);
   if (NULL != learn_peer.stats)
   {
@@ -288,7 +304,7 @@ check_statistics (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                                &process_adv_sent, NULL);
   }
   check_task =
-      GNUNET_SCHEDULER_add_delayed (CHECK_INTERVALL, &check_statistics, NULL);
+      GNUNET_SCHEDULER_add_delayed (CHECK_INTERVAL, &check_statistics, NULL);
 }
 
 
@@ -296,10 +312,9 @@ check_statistics (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Core handler for p2p hostlist advertisements
  */
 static int
-ad_arrive_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
-                   const struct GNUNET_MessageHeader *message,
-                   const struct GNUNET_ATS_Information *atsi,
-                   unsigned int atsi_count)
+ad_arrive_handler (void *cls,
+                  const struct GNUNET_PeerIdentity *peer,
+                   const struct GNUNET_MessageHeader *message)
 {
   char *hostname;
   char *expected_uri;
@@ -344,7 +359,7 @@ ad_arrive_handler (void *cls, const struct GNUNET_PeerIdentity *peer,
   }
   else
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Expected URI `%s' and recieved URI `%s' differ\n",
+                "Expected URI `%s' and received URI `%s' differ\n",
                 expected_uri, current_adv_uri);
   GNUNET_free (expected_uri);
   GNUNET_free_non_null (hostname);
@@ -366,17 +381,16 @@ setup_learn_peer (struct PeerContext *p, const char *cfgname)
 {
   char *filename;
   unsigned int result;
+  char *binary;
 
+  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
   p->cfg = GNUNET_CONFIGURATION_create ();
-#if START_ARM
   p->arm_proc =
-    GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
-                               "gnunet-service-arm",
-#if VERBOSE
-                               "-L", "DEBUG",
-#endif
-                               "-c", cfgname, NULL);
-#endif
+    GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
+                             NULL, NULL, NULL,
+                             binary,
+                             "gnunet-service-arm",
+                             "-c", cfgname, NULL);
   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
   if (GNUNET_OK ==
       GNUNET_CONFIGURATION_get_value_string (p->cfg, "HOSTLIST", "HOSTLISTFILE",
@@ -392,30 +406,32 @@ setup_learn_peer (struct PeerContext *p, const char *cfgname)
     GNUNET_free (filename);
   }
   p->core =
-      GNUNET_CORE_connect (p->cfg, 1, NULL, NULL, NULL, NULL, NULL, GNUNET_NO,
+      GNUNET_CORE_connect (p->cfg, NULL, NULL, NULL, NULL, NULL, GNUNET_NO,
                            NULL, GNUNET_NO, learn_handlers);
   GNUNET_assert (NULL != p->core);
   p->stats = GNUNET_STATISTICS_create ("hostlist", p->cfg);
   GNUNET_assert (NULL != p->stats);
+  GNUNET_free (binary);
 }
 
 
 static void
 setup_adv_peer (struct PeerContext *p, const char *cfgname)
 {
+  char *binary;
+
+  binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
   p->cfg = GNUNET_CONFIGURATION_create ();
-#if START_ARM
   p->arm_proc =
-    GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
-                               "gnunet-service-arm",
-#if VERBOSE
-                               "-L", "DEBUG",
-#endif
-                               "-c", cfgname, NULL);
-#endif
+    GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
+                             NULL, NULL, NULL,
+                             binary,
+                            "gnunet-service-arm",
+                            "-c", cfgname, NULL);
   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
   p->stats = GNUNET_STATISTICS_create ("hostlist", p->cfg);
   GNUNET_assert (NULL != p->stats);
+  GNUNET_free (binary);
 }
 
 
@@ -437,7 +453,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_error, NULL);
 
   check_task =
-      GNUNET_SCHEDULER_add_delayed (CHECK_INTERVALL, &check_statistics, NULL);
+      GNUNET_SCHEDULER_add_delayed (CHECK_INTERVAL, &check_statistics, NULL);
 }
 
 
@@ -449,9 +465,6 @@ check ()
   char *const argv[] = {
     "test-gnunet-daemon-hostlist-learning",
     "-c", "learning_data.conf",
-#if VERBOSE
-    "-L", "DEBUG",
-#endif
     NULL
   };
   struct GNUNET_GETOPT_CommandLineOption options[] = {
@@ -505,11 +518,7 @@ main (int argc, char *argv[])
   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
   GNUNET_log_setup ("test-gnunet-daemon-hostlist",
-#if VERBOSE
-                    "DEBUG",
-#else
                     "WARNING",
-#endif
                     NULL);
 #if !WINDOWS
   system ("gnunet-peerinfo -s -c test_learning_adv_peer.conf > /dev/null");
@@ -530,4 +539,4 @@ main (int argc, char *argv[])
   return ret;
 }
 
-/* end of test_gnunet_daemon_hostlist.c */
+/* end of test_gnunet_daemon_hostlist_learning.c */