print warning messages about hostkey file
[oweals/gnunet.git] / src / testing / testing.c
index c5ff0eb7bd0fe705231fba10666df7aee81dcfa1..c97b3bda372962f845ddfd930279e1fc674a4a22 100644 (file)
@@ -37,8 +37,8 @@
 #include "gnunet_transport_service.h"
 #include "gnunet_hello_lib.h"
 
-#define DEBUG_TESTING GNUNET_YES
-#define DEBUG_TESTING_RECONNECT GNUNET_YES
+#define DEBUG_TESTING GNUNET_NO
+#define DEBUG_TESTING_RECONNECT GNUNET_NO
 
 /**
  * How long do we wait after starting gnunet-service-arm
@@ -385,6 +385,7 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
           return;
         }
       d->hostkeybuf[103] = '\0';
+
       if ((bytes_read < 0) ||
           (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
                                                         &d->id.hashPubKey)))
@@ -452,7 +453,7 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       if (NULL == d->hostname)
         {
 #if DEBUG_TESTING
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                       "Starting `%s', with command `%s %s %s %s %s %s'.\n",
                       "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
                       "-L", "DEBUG", "-s");
@@ -472,7 +473,7 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
             dst = GNUNET_strdup (d->hostname);
 
 #if DEBUG_TESTING
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                       "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
                       "gnunet-arm", "ssh", dst, "gnunet-arm", "-c",
                       d->cfgfile, "-L", "DEBUG", "-s", "-q");
@@ -525,7 +526,7 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
           return;
         }
 #if DEBUG_TESTING
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                   "Started `%s', waiting for `%s' to be up.\n",
                   "gnunet-arm", "gnunet-service-core");
 #endif
@@ -550,6 +551,7 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                     (NULL == d->hostname)
                     ? _("`gnunet-arm' does not seem to terminate.\n")
                     : _("`ssh' does not seem to terminate.\n"));
+              GNUNET_free(d->proc);
               return;
             }
           /* wait some more */
@@ -559,10 +561,15 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
           return;
         }
 #if DEBUG_TESTING
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                   "Successfully started `%s'.\n", "gnunet-arm");
 #endif
+      GNUNET_free(d->proc);
       d->phase = SP_START_CORE;
+#if DEBUG_TESTING
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "Calling CORE_connect\n");
+#endif
       d->server = GNUNET_CORE_connect (d->cfg, 1,
 #if NO_MORE_TIMEOUT_FIXME
                                        ARM_START_WAIT,
@@ -603,6 +610,8 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
               GNUNET_free_non_null (d->hostname);
               GNUNET_free_non_null (d->username);
               GNUNET_free_non_null (d->shortname);
+              GNUNET_free_non_null (d->proc);
+              d->proc = NULL;
               GNUNET_free (d);
               return;
             }
@@ -635,6 +644,8 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
           GNUNET_free_non_null (d->hostname);
           GNUNET_free_non_null (d->username);
           GNUNET_free_non_null (d->shortname);
+          GNUNET_free_non_null (d->proc);
+          d->proc = NULL;
           GNUNET_free (d);
           return;
         }
@@ -665,6 +676,8 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       GNUNET_free_non_null (d->hello);
       d->hello = NULL;
       GNUNET_free_non_null (d->shortname);
+      GNUNET_free_non_null (d->proc);
+      d->proc = NULL;
       d->shortname = NULL;
       if (NULL != d->dead_cb)
         d->dead_cb (d->dead_cb_cls, NULL);
@@ -789,6 +802,7 @@ GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
  *        (use NULL for localhost).
  * @param ssh_username ssh username to use when connecting to hostname
  * @param sshport port to pass to ssh process when connecting to hostname
+ * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
  * @param hostkey_callback function to call once the hostkey has been
  *        generated for this peer, but it hasn't yet been started
  *        (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
@@ -803,6 +817,7 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
                              const char *hostname,
                              const char *ssh_username,
                              uint16_t sshport,
+                             const char *hostkey,
                              GNUNET_TESTING_NotifyHostkeyCreated
                              hostkey_callback, void *hostkey_cls,
                              GNUNET_TESTING_NotifyDaemonRunning cb,
@@ -811,6 +826,12 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
   struct GNUNET_TESTING_Daemon *ret;
   char *arg;
   char *username;
+  char *servicehome;
+  char *baseservicehome;
+  char *slash;
+  char *hostkeyfile;
+  char *temp_file_name;
+  struct GNUNET_DISK_FileHandle *fn;
 
   ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
   ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
@@ -820,7 +841,18 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
     }
   else
     ret->ssh_port_str = NULL;
-  ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
+
+  /* Find service home and base service home directories, create it if it doesn't exist */
+  GNUNET_assert(GNUNET_OK ==
+                GNUNET_CONFIGURATION_get_value_string (cfg,
+                                                       "PATHS",
+                                                       "SERVICEHOME",
+                                                       &servicehome));
+
+  GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
+  GNUNET_asprintf(&temp_file_name, "%s/gnunet-testing-config", servicehome);
+  ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
+  GNUNET_free(temp_file_name);
 #if DEBUG_TESTING
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Setting up peer with configuration file `%s'.\n",
@@ -842,7 +874,24 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
   GNUNET_CONFIGURATION_set_value_string (ret->cfg,
                                          "PATHS",
                                          "DEFAULTCONFIG", ret->cfgfile);
-  /* 1) write configuration to temporary file */
+
+  /* Write hostkey to file, if we were given one */
+  hostkeyfile = NULL;
+  if (hostkey != NULL)
+    {
+      GNUNET_asprintf(&hostkeyfile, "%s/.hostkey", servicehome);
+      fn =
+      GNUNET_DISK_file_open (hostkeyfile,
+                             GNUNET_DISK_OPEN_READWRITE
+                             | GNUNET_DISK_OPEN_CREATE,
+                             GNUNET_DISK_PERM_USER_READ |
+                             GNUNET_DISK_PERM_USER_WRITE);
+      GNUNET_assert(fn != NULL);
+      GNUNET_assert(HOSTKEYFILESIZE == GNUNET_DISK_file_write(fn, hostkey, HOSTKEYFILESIZE));
+      GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fn));
+    }
+
+  /* write configuration to temporary file */
   if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
     {
       if (0 != UNLINK (ret->cfgfile))
@@ -869,42 +918,54 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
     }
   ret->username = username;
 
-  /* 2) copy file to remote host */
+  /* copy directory to remote host */
   if (NULL != hostname)
     {
 #if DEBUG_TESTING
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Copying configuration file to host `%s'.\n", hostname);
+                  "Copying configuration directory to host `%s'.\n", hostname);
 #endif
+      baseservicehome = GNUNET_strdup(servicehome);
+      /* Remove trailing /'s */
+      while (baseservicehome[strlen(baseservicehome) - 1] == '/')
+        baseservicehome[strlen(baseservicehome) - 1] = '\0';
+      /* Find next directory /, jump one ahead */
+      slash = strrchr(baseservicehome, '/');
+      if (slash != NULL)
+        *(++slash) = '\0';
+
       ret->phase = SP_COPYING;
       if (NULL != username)
-        GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
+        GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
       else
-        GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
+        GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
 
       if (ret->ssh_port_str == NULL)
         {
-          ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
+          ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
 #if !DEBUG_TESTING
                                                "-q",
 #endif
-                                               ret->cfgfile, arg, NULL);
+                                               servicehome, arg, NULL);
+#if DEBUG_TESTING
+          GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "copying directory with command scp -r %s %s\n", servicehome, arg);
+#endif
         }
       else
         {
           ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
-                                               "scp", "-P", ret->ssh_port_str,
+                                               "scp", "-r", "-P", ret->ssh_port_str,
 #if !DEBUG_TESTING
                                                "-q",
 #endif
-                                               ret->cfgfile, arg, NULL);
+                                               servicehome, arg, NULL);
         }
       GNUNET_free (arg);
       if (NULL == ret->proc)
         {
           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                       _
-                      ("Could not start `%s' process to copy configuration file.\n"),
+                      ("Could not start `%s' process to copy configuration directory.\n"),
                       "scp");
           if (0 != UNLINK (ret->cfgfile))
             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
@@ -914,11 +975,19 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
           GNUNET_free_non_null (ret->username);
           GNUNET_free (ret->cfgfile);
           GNUNET_free (ret);
+          if ((hostkey != NULL) && (0 != UNLINK(hostkeyfile)))
+            GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
+                                      "unlink", hostkeyfile);
+          GNUNET_free_non_null(hostkeyfile);
+          GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
+          GNUNET_free(servicehome);
           return NULL;
         }
       ret->task
         = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
                                         &start_fsm, ret);
+      GNUNET_free_non_null(hostkeyfile);
+      GNUNET_free(servicehome);
       return ret;
     }
 #if DEBUG_TESTING
@@ -929,6 +998,8 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
   GNUNET_SCHEDULER_add_continuation (&start_fsm,
                                      ret,
                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+  GNUNET_free_non_null(hostkeyfile);
+  GNUNET_free(servicehome);
   return ret;
 }
 
@@ -1266,6 +1337,11 @@ struct ConnectContext
    */
   struct GNUNET_CORE_Handle *d1core;
 
+  /**
+   * Have we actually connected to the core of the first daemon yet?
+   */
+  int d1core_ready;
+
   /**
    * Testing handle to the second daemon.
    */
@@ -1407,6 +1483,7 @@ notify_connect_result (void *cls,
           GNUNET_CORE_disconnect (ctx->d1core);
           ctx->d1core = NULL;
         }
+      ctx->d1core_ready = GNUNET_NO;
 #if CONNECT_CORE2
       if (ctx->d2core != NULL)
         {
@@ -1456,7 +1533,7 @@ connect_notify (void *cls,
 {
   struct ConnectContext *ctx = cls;
 
-  if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
+  if (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
     {
       ctx->connected = GNUNET_YES;
       ctx->distance = 0;        /* FIXME: distance */
@@ -1498,17 +1575,15 @@ connect_notify_core2 (void *cls,
  * Task called once a core connect request has been transmitted.
  *
  * @param cls struct ConnectContext
- * @param tc context information (why was this task triggered now)
+ * @param success was the request successful?
  */
 void
 core_connect_request_cont (void *cls,
-                           const struct GNUNET_SCHEDULER_TaskContext *tc)
+                           int success)
 {
   struct ConnectContext *ctx = cls;
 
-  if (tc->reason == GNUNET_SCHEDULER_REASON_PREREQ_DONE)
-    ctx->connect_request_handle = NULL;
-  return;
+  ctx->connect_request_handle = NULL;
 }
 
 static void
@@ -1519,7 +1594,7 @@ send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     return;
-  if ((ctx->d2->hello != NULL)
+  if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL)
       && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)))
     {
       hello = GNUNET_HELLO_get_header (ctx->d2->hello);
@@ -1548,6 +1623,20 @@ send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                                                        &send_hello, ctx);
 }
 
+void
+core_init_notify (void *cls,
+                  struct GNUNET_CORE_Handle * server,
+                  const struct GNUNET_PeerIdentity *
+                  my_identity,
+                  const struct
+                  GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
+                  publicKey)
+{
+  struct ConnectContext *connect_ctx = cls;
+
+  connect_ctx->d1core_ready = GNUNET_YES;
+}
+
 /**
  * Establish a connection between two GNUnet daemons.
  *
@@ -1594,9 +1683,10 @@ GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
               d1->shortname, d2->shortname);
 #endif
 
+  /* FIXME: possible bug, core gets connected after peers are connected, thus the connect_notify function is never called (?) */
   ctx->d1core = GNUNET_CORE_connect (d1->cfg, 1,
                                      ctx,
-                                     NULL,
+                                     &core_init_notify,
                                      &connect_notify, NULL, NULL,
                                      NULL, GNUNET_NO,
                                      NULL, GNUNET_NO, no_handlers);
@@ -1676,14 +1766,10 @@ reattempt_daemons_connect (void *cls,
 #endif
 
   GNUNET_assert (ctx->d1core == NULL);
-
+  ctx->d1core_ready = GNUNET_NO;
   ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
-#if NO_MORE_TIMEOUT_FIXME
-                                     GNUNET_TIME_absolute_get_remaining
-                                     (ctx->timeout),
-#endif
                                      ctx,
-                                     NULL,
+                                     &core_init_notify,
                                      &connect_notify, NULL, NULL,
                                      NULL, GNUNET_NO,
                                      NULL, GNUNET_NO, no_handlers);