print warning messages about hostkey file
[oweals/gnunet.git] / src / testing / testing.c
index 75eab1a06f6ace449256696ba552bac23ad363f5..c97b3bda372962f845ddfd930279e1fc674a4a22 100644 (file)
@@ -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;
 }