- fix 2699
[oweals/gnunet.git] / src / testing / testing.c
index bba38ead3a57b1a3b8c71847731d42c26fd9faab..16f90289096c01d4c83f78a7fcb0e71b184aa264 100644 (file)
  */
 #include "platform.h"
 #include "gnunet_util_lib.h"
-#include "gnunet_testing_lib-new.h"
+#include "gnunet_testing_lib.h"
 
 #define LOG(kind,...)                                           \
-  GNUNET_log_from (kind, "gnunettestingnew", __VA_ARGS__)
+  GNUNET_log_from (kind, "testing-api", __VA_ARGS__)
+
 
 /**
- * Size of a hostkey when written to a file
+ * We need pipe control only on WINDOWS
  */
-#define HOSTKEYFILESIZE 914
+#if WINDOWS
+#define PIPE_CONTROL GNUNET_YES
+#else
+#define PIPE_CONTROL GNUNET_NO
+#endif
+
 
 /**
  * Lowest port used for GNUnet testing.  Should be high enough to not
@@ -69,12 +75,18 @@ struct GNUNET_TESTING_System
   char *tmppath;
 
   /**
-   * The hostname of the controller
+   * The trusted ip. Can either be a single ip address or a network address in
+   * CIDR notation.
+   */
+  char *trusted_ip;
+
+  /**
+   * our hostname
    */
-  char *controller;
+  char *hostname;
 
   /**
-   * Hostkeys data, contains "HOSTKEYFILESIZE * total_hostkeys" bytes.
+   * Hostkeys data, contains "GNUNET_TESTING_HOSTKEYFILESIZE * total_hostkeys" bytes.
    */
   char *hostkeys_data;
 
@@ -124,6 +136,16 @@ struct GNUNET_TESTING_System
    * The number of hostkeys
    */
   uint32_t total_hostkeys;
+
+  /**
+   * Lowest port we are allowed to use.
+   */
+  uint16_t lowport;
+
+  /**
+   * Highest port we are allowed to use.
+   */
+  uint16_t highport;
 };
 
 
@@ -149,6 +171,7 @@ struct GNUNET_TESTING_Peer
    * necessary).
    */ 
   char *main_binary;
+  char *args;
   
   /**
    * Handle to the running binary of the service, NULL if the
@@ -179,7 +202,7 @@ hostkeys_load (struct GNUNET_TESTING_System *system)
   
   GNUNET_assert (NULL == system->hostkeys_data);
   data_dir = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_DATADIR);
-  GNUNET_asprintf (&filename, "%s/testing_hostkeys.dat", data_dir);
+  GNUNET_asprintf (&filename, "%s/testing_hostkeys.ecc", data_dir);
   GNUNET_free (data_dir);  
 
   if (GNUNET_YES != GNUNET_DISK_file_test (filename))
@@ -198,7 +221,7 @@ hostkeys_load (struct GNUNET_TESTING_System *system)
     GNUNET_free (filename);
     return GNUNET_SYSERR;       /* File is empty */
   }
-  if (0 != (fs % HOSTKEYFILESIZE))
+  if (0 != (fs % GNUNET_TESTING_HOSTKEYFILESIZE))
   {
     LOG (GNUNET_ERROR_TYPE_ERROR,
          _("Incorrect hostkey file format: %s\n"), filename);
@@ -213,7 +236,7 @@ hostkeys_load (struct GNUNET_TESTING_System *system)
     GNUNET_free (filename);
     return GNUNET_SYSERR;
   }
-  system->total_hostkeys = fs / HOSTKEYFILESIZE;
+  system->total_hostkeys = fs / GNUNET_TESTING_HOSTKEYFILESIZE;
   system->hostkeys_data = GNUNET_DISK_file_map (system->map_fd,
                                                &system->map,
                                                GNUNET_DISK_MAP_TYPE_READ,
@@ -249,28 +272,39 @@ hostkeys_unload (struct GNUNET_TESTING_System *system)
  * @param testdir only the directory name without any path. This is used for
  *          all service homes; the directory will be created in a temporary
  *          location depending on the underlying OS
- *
- * @param controller hostname of the controlling host, 
- *        service configurations are modified to allow 
- *        control connections from this host; can be NULL
+ * @param trusted_ip the ip address which will be set as TRUSTED HOST in all
+ *          service configurations generated to allow control connections from
+ *          this ip. This can either be a single ip address or a network address
+ *          in CIDR notation.
+ * @param hostname the hostname of the system we are using for testing; NULL for
+ *          localhost
+ * @param lowport lowest port number this system is allowed to allocate (inclusive)
+ * @param highport highest port number this system is allowed to allocate (exclusive)
  * @return handle to this system, NULL on error
  */
 struct GNUNET_TESTING_System *
-GNUNET_TESTING_system_create (const char *testdir,
-                             const char *controller)
+GNUNET_TESTING_system_create_with_portrange (const char *testdir,
+                                            const char *trusted_ip,
+                                            const char *hostname,
+                                            uint16_t lowport,
+                                            uint16_t highport)
 {
   struct GNUNET_TESTING_System *system;
 
   GNUNET_assert (NULL != testdir);
   system = GNUNET_malloc (sizeof (struct GNUNET_TESTING_System));
   system->tmppath = GNUNET_DISK_mkdtemp (testdir);
+  system->lowport = lowport;
+  system->highport = highport;
   if (NULL == system->tmppath)
   {
     GNUNET_free (system);
     return NULL;
   }
-  if (NULL != controller)
-    system->controller = GNUNET_strdup (controller);
+  if (NULL != trusted_ip)
+    system->trusted_ip = GNUNET_strdup (trusted_ip);
+  if (NULL != hostname)
+    system->hostname = GNUNET_strdup (hostname);
   if (GNUNET_OK != hostkeys_load (system))
   {
     GNUNET_TESTING_system_destroy (system, GNUNET_YES);
@@ -280,6 +314,35 @@ GNUNET_TESTING_system_create (const char *testdir,
 }
 
 
+/**
+ * Create a system handle.  There must only be one system handle per operating
+ * system.  Uses a default range for allowed ports.  Ports are still tested for
+ * availability.
+ *
+ * @param testdir only the directory name without any path. This is used for all
+ *          service homes; the directory will be created in a temporary location
+ *          depending on the underlying OS
+ * @param trusted_ip the ip address which will be set as TRUSTED HOST in all
+ *          service configurations generated to allow control connections from
+ *          this ip. This can either be a single ip address or a network address
+ *          in CIDR notation.
+ * @param hostname the hostname of the system we are using for testing; NULL for
+ *          localhost
+ * @return handle to this system, NULL on error
+ */
+struct GNUNET_TESTING_System *
+GNUNET_TESTING_system_create (const char *testdir,
+                             const char *trusted_ip,
+                             const char *hostname)
+{
+  return GNUNET_TESTING_system_create_with_portrange (testdir,
+                                                     trusted_ip,
+                                                     hostname,
+                                                     LOW_PORT,
+                                                     HIGH_PORT);
+}
+
+
 /**
  * Free system resources.
  *
@@ -296,7 +359,8 @@ GNUNET_TESTING_system_destroy (struct GNUNET_TESTING_System *system,
   if (GNUNET_YES == remove_paths)
     GNUNET_DISK_directory_remove (system->tmppath);
   GNUNET_free (system->tmppath);
-  GNUNET_free_non_null (system->controller);
+  GNUNET_free_non_null (system->trusted_ip);
+  GNUNET_free_non_null (system->hostname);
   GNUNET_free (system);
 }
 
@@ -315,6 +379,7 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
   struct GNUNET_NETWORK_Handle *socket;
   struct addrinfo hint;
   struct addrinfo *ret;
+  struct addrinfo *ai;
   uint32_t *port_buckets;
   char *open_port_str;
   int bind_status;
@@ -342,12 +407,12 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
   hint.ai_flags = AI_PASSIVE | AI_NUMERICSERV; /* Wild card address */
   port_buckets = (GNUNET_YES == is_tcp) ?
     system->reserved_tcp_ports : system->reserved_udp_ports;
-  for (index = (LOW_PORT / 32) + 1; index < (HIGH_PORT / 32); index++)
+  for (index = (system->lowport / 32) + 1; index < (system->highport / 32); index++)
   {
     xor_image = (UINT32_MAX ^ port_buckets[index]);
     if (0 == xor_image)        /* Ports in the bucket are full */
       continue;
-    pos = 0;
+    pos = system->lowport % 32;
     while (pos < 32)
     {
       if (0 == ((xor_image >> pos) & 1U))
@@ -356,22 +421,30 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
         continue;
       }
       open_port = (index * 32) + pos;
+      if (open_port >= system->highport)
+       return 0;
       GNUNET_asprintf (&open_port_str, "%u", (unsigned int) open_port);
       ret = NULL;
       GNUNET_assert (0 == getaddrinfo (NULL, open_port_str, &hint, &ret));
-      GNUNET_free (open_port_str);  
-      socket = GNUNET_NETWORK_socket_create (ret->ai_family,
-                                             (GNUNET_YES == is_tcp) ?
-                                             SOCK_STREAM : SOCK_DGRAM,
-                                             0);
-      GNUNET_assert (NULL != socket);
-      bind_status = GNUNET_NETWORK_socket_bind (socket,
-                                                ret->ai_addr,
-                                                ret->ai_addrlen);
-      freeaddrinfo (ret);
-      GNUNET_NETWORK_socket_close (socket);
-      socket = NULL;
+      GNUNET_free (open_port_str);
+      bind_status = GNUNET_NO;
+      for (ai = ret; NULL != ai; ai = ai->ai_next)
+      {
+        socket = GNUNET_NETWORK_socket_create (ai->ai_family,
+                                               (GNUNET_YES == is_tcp) ?
+                                               SOCK_STREAM : SOCK_DGRAM,
+                                               0);
+        if (NULL == socket)
+          continue;
+        bind_status = GNUNET_NETWORK_socket_bind (socket,
+                                                  ai->ai_addr,
+                                                  ai->ai_addrlen);
+        GNUNET_NETWORK_socket_close (socket);
+        if (GNUNET_OK != bind_status)
+          break;
+      }
       port_buckets[index] |= (1U << pos); /* Set the port bit */
+      freeaddrinfo (ret);
       if (GNUNET_OK == bind_status)
       {
         LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -452,13 +525,13 @@ reserve_path (struct GNUNET_TESTING_System *system)
  *        key; if NULL, GNUNET_SYSERR is returned immediately
  * @return NULL on error (not enough keys)
  */
-struct GNUNET_CRYPTO_RsaPrivateKey *
+struct GNUNET_CRYPTO_EccPrivateKey *
 GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
                            uint32_t key_number,
                            struct GNUNET_PeerIdentity *id)
 {  
-  struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
-  struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
+  struct GNUNET_CRYPTO_EccPrivateKey *private_key;
+  struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded public_key;
   
   if ((NULL == id) || (NULL == system->hostkeys_data))
     return NULL;
@@ -468,18 +541,19 @@ GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
          _("Key number %u does not exist\n"), key_number);
     return NULL;
   }   
-  private_key = GNUNET_CRYPTO_rsa_decode_key (system->hostkeys_data +
-                                              (key_number * HOSTKEYFILESIZE),
-                                              HOSTKEYFILESIZE);
+  private_key = GNUNET_CRYPTO_ecc_decode_key (system->hostkeys_data +
+                                              (key_number *
+                                               GNUNET_TESTING_HOSTKEYFILESIZE),
+                                              GNUNET_TESTING_HOSTKEYFILESIZE);
   if (NULL == private_key)
   {
     LOG (GNUNET_ERROR_TYPE_ERROR,
          _("Error while decoding key %u\n"), key_number);
     return NULL;
   }
-  GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
+  GNUNET_CRYPTO_ecc_key_get_public (private_key, &public_key);
   GNUNET_CRYPTO_hash (&public_key,
-                      sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
+                      sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
                       &(id->hashPubKey));
   return private_key;
 }
@@ -556,6 +630,8 @@ update_config (void *cls, const char *section, const char *option,
       if (0 == new_port)
       {
         uc->status = GNUNET_SYSERR;
+        GNUNET_free (single_variable);
+        GNUNET_free (per_host_variable);
         return;
       }
       GNUNET_snprintf (cval, sizeof (cval), "%u", new_port);
@@ -594,9 +670,9 @@ update_config (void *cls, const char *section, const char *option,
       GNUNET_break(0);          /* FIXME */
     }
   }
-  if ((0 == strcmp (option, "HOSTNAME")) && (NULL != uc->system->controller))
+  if (0 == strcmp (option, "HOSTNAME"))
   {
-    value = uc->system->controller;
+    value = (NULL == uc->system->hostname) ? "localhost" : uc->system->hostname;
   }
   GNUNET_free (single_variable);
   GNUNET_free (per_host_variable);
@@ -605,7 +681,8 @@ update_config (void *cls, const char *section, const char *option,
 
 
 /**
- * Section iterator to set ACCEPT_FROM in all sections
+ * Section iterator to set ACCEPT_FROM/ACCEPT_FROM6 to include the address of
+ * 'trusted_hosts' in all sections
  *
  * @param cls the UpdateContext
  * @param section name of the section
@@ -614,35 +691,102 @@ static void
 update_config_sections (void *cls,
                         const char *section)
 {
-  struct UpdateContext *uc = cls;
+  struct UpdateContext *uc = cls;  
+  char **ikeys;
+  char *val;
+  char *ptr;
   char *orig_allowed_hosts;
   char *allowed_hosts;
-
-  if (NULL != strstr (section, "transport-"))
-    return;
-
+  char *ACCEPT_FROM_key;
+  uint16_t ikeys_cnt;
+  uint16_t key;
+  
+  ikeys_cnt = 0;
+  val = NULL;
+  if (GNUNET_YES == GNUNET_CONFIGURATION_have_value (uc->cfg, section,
+                                                     "TESTING_IGNORE_KEYS"))
+  {
+    GNUNET_assert 
+      (GNUNET_YES == 
+       GNUNET_CONFIGURATION_get_value_string (uc->cfg, section,
+                                              "TESTING_IGNORE_KEYS", &val));
+    ptr = val;
+    for (ikeys_cnt = 0; NULL != (ptr = strstr (ptr, ";")); ikeys_cnt++)
+      ptr++;
+    if (0 == ikeys_cnt)
+      GNUNET_break (0);
+    else
+    {
+      ikeys = GNUNET_malloc ((sizeof (char *)) * ikeys_cnt);
+      ptr = val;
+      for (key = 0; key < ikeys_cnt; key++)
+      {
+        ikeys[key] = ptr;
+        ptr = strstr (ptr, ";");
+        *ptr = '\0';
+        ptr++;
+      }
+    }
+  }
+  if (0 != ikeys_cnt)
+  {
+    for (key = 0; key < ikeys_cnt; key++)
+    {
+      if (NULL != strstr (ikeys[key], "ADVERTISED_PORT"))
+       break;
+    }
+    if ((key == ikeys_cnt) &&
+       (GNUNET_YES == GNUNET_CONFIGURATION_have_value (uc->cfg, section,
+                                                       "ADVERTISED_PORT")))
+    {
+      if (GNUNET_OK == 
+         GNUNET_CONFIGURATION_get_value_string (uc->cfg, section, "PORT", &ptr))
+      {
+       GNUNET_CONFIGURATION_set_value_string (uc->cfg, section, 
+                                              "ADVERTISED_PORT", ptr);
+       GNUNET_free (ptr);
+      }
+    }
+    for (key = 0; key < ikeys_cnt; key++)
+    {
+      if (NULL != strstr (ikeys[key], "ACCEPT_FROM"))
+      {
+        GNUNET_free (ikeys);
+        GNUNET_free (val);
+        return;
+      }
+    }
+    GNUNET_free (ikeys);
+  }
+  GNUNET_free_non_null (val);
+  ACCEPT_FROM_key = "ACCEPT_FROM";  
+  if ((NULL != uc->system->trusted_ip) && 
+      (NULL != strstr (uc->system->trusted_ip, ":"))) /* IPv6 in use */
+    ACCEPT_FROM_key = "ACCEPT_FROM6";
   if (GNUNET_OK != 
-      GNUNET_CONFIGURATION_get_value_string (uc->cfg, section, "ACCEPT_FROM",
+      GNUNET_CONFIGURATION_get_value_string (uc->cfg, section, ACCEPT_FROM_key,
                                              &orig_allowed_hosts))
   {
     orig_allowed_hosts = GNUNET_strdup ("127.0.0.1;");
   }
-  if (NULL == uc->system->controller)
+  if (NULL == uc->system->trusted_ip)
     allowed_hosts = GNUNET_strdup (orig_allowed_hosts);
   else
     GNUNET_asprintf (&allowed_hosts, "%s%s;", orig_allowed_hosts,
-                     uc->system->controller);
+                     uc->system->trusted_ip);
   GNUNET_free (orig_allowed_hosts);
-  GNUNET_CONFIGURATION_set_value_string (uc->cfg, section, "ACCEPT_FROM",
+  GNUNET_CONFIGURATION_set_value_string (uc->cfg, section, ACCEPT_FROM_key,
                                          allowed_hosts);
   GNUNET_free (allowed_hosts);  
 }
 
 
 /**
- * Create a new configuration using the given configuration
- * as a template; ports and paths will be modified to select
- * available ports on the local system.  If we run
+ * Create a new configuration using the given configuration as a template;
+ * ports and paths will be modified to select available ports on the local
+ * system. The default configuration will be available in PATHS section under
+ * the option DEFAULTCONFIG after the call. SERVICE_HOME is also set in PATHS
+ * section to the temporary directory specific to this configuration. If we run
  * out of "*port" numbers, return SYSERR.
  *
  * This is primarily a helper function used internally
@@ -673,7 +817,7 @@ GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
                                          uc.service_home);
   /* make PORTs and UNIXPATHs unique */
   GNUNET_CONFIGURATION_iterate (cfg, &update_config, &uc);
-  /* allow connections to services from system controller host */
+  /* allow connections to services from system trusted_ip host */
   GNUNET_CONFIGURATION_iterate_sections (cfg, &update_config_sections, &uc);
   /* enable loopback-based connections between peers */
   GNUNET_CONFIGURATION_set_value_string (cfg, 
@@ -693,7 +837,8 @@ GNUNET_TESTING_configuration_create (struct GNUNET_TESTING_System *system,
  *            changes in port numbers and paths)
  * @param key_number number of the hostkey to use for the peer
  * @param id identifier for the daemon, will be set, can be NULL
- * @param emsg set to error message (set to NULL on success), can be NULL
+ * @param emsg set to freshly allocated error message (set to NULL on success), 
+ *          can be NULL
  * @return handle to the peer, NULL on error
  */
 struct GNUNET_TESTING_Peer *
@@ -708,8 +853,9 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
   char *service_home;  
   char hostkey_filename[128];
   char *config_filename;
+  char *libexec_binary;
   char *emsg_;
-  struct GNUNET_CRYPTO_RsaPrivateKey *pk;
+  struct GNUNET_CRYPTO_EccPrivateKey *pk;
 
   if (NULL != emsg)
     *emsg = NULL;
@@ -717,7 +863,7 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
   {
     GNUNET_asprintf (&emsg_,
                       _("Failed to create configuration for peer (not enough free ports?)\n"));
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", *emsg_);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
     if (NULL != emsg)
       *emsg = emsg_;
     else
@@ -743,7 +889,7 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
     GNUNET_asprintf (&emsg_,
                     _("Failed to initialize hostkey for peer %u\n"),
                     (unsigned int) key_number);
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", *emsg_);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
     if (NULL != emsg)
       *emsg = emsg_;
     else
@@ -751,12 +897,13 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
     return NULL;
   }
   if (NULL != pk)
-    GNUNET_CRYPTO_rsa_key_free (pk);
+    GNUNET_CRYPTO_ecc_key_free (pk);
   GNUNET_assert (GNUNET_OK == 
                  GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
                                                         "SERVICEHOME",
                                                         &service_home));
-  GNUNET_snprintf (hostkey_filename, sizeof (hostkey_filename), "%s/.hostkey",
+  /* FIXME: might be better to evaluate actual configuration option here... */
+  GNUNET_snprintf (hostkey_filename, sizeof (hostkey_filename), "%s/private.ecc",
                    service_home);
   GNUNET_free (service_home);
   fd = GNUNET_DISK_file_open (hostkey_filename,
@@ -768,16 +915,16 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
     GNUNET_break (0); 
     return NULL;
   }
-  if (HOSTKEYFILESIZE !=
+  if (GNUNET_TESTING_HOSTKEYFILESIZE !=
       GNUNET_DISK_file_write (fd, system->hostkeys_data 
-                             + (key_number * HOSTKEYFILESIZE),
-                             HOSTKEYFILESIZE))
+                             + (key_number * GNUNET_TESTING_HOSTKEYFILESIZE),
+                             GNUNET_TESTING_HOSTKEYFILESIZE))
   {
     GNUNET_asprintf (&emsg_,
                     _("Failed to write hostkey file for peer %u: %s\n"),
                     (unsigned int) key_number,
                     STRERROR (errno));
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", *emsg_);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
     if (NULL != emsg)
       *emsg = emsg_;
     else
@@ -796,7 +943,7 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
                     config_filename,
                     (unsigned int) key_number,
                     STRERROR (errno));
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", *emsg_);
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%s", emsg_);
     if (NULL != emsg)
       *emsg = emsg_;
     else
@@ -806,9 +953,18 @@ GNUNET_TESTING_peer_configure (struct GNUNET_TESTING_System *system,
   }
   peer = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Peer));
   peer->cfgfile = config_filename; /* Free in peer_destroy */
-  peer->main_binary = GNUNET_strdup ("gnunet-service-arm");
+  libexec_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
+  if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, "arm", "PREFIX", &peer->main_binary))
+  {
+    /* No prefix */
+    GNUNET_asprintf(&peer->main_binary, "%s", libexec_binary);
+    peer->args = strdup ("");
+  }
+  else
+    peer->args = strdup (libexec_binary);
   peer->system = system;
   peer->key_number = key_number;
+  GNUNET_free (libexec_binary);
   return peer;
 }
 
@@ -823,7 +979,7 @@ void
 GNUNET_TESTING_peer_get_identity (const struct GNUNET_TESTING_Peer *peer,
                                  struct GNUNET_PeerIdentity *id)
 {
-  GNUNET_CRYPTO_rsa_key_free (GNUNET_TESTING_hostkey_get (peer->system,
+  GNUNET_CRYPTO_ecc_key_free (GNUNET_TESTING_hostkey_get (peer->system,
                                                          peer->key_number,
                                                          id));
 }
@@ -844,9 +1000,12 @@ GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer)
     return GNUNET_SYSERR;
   }
   GNUNET_assert (NULL != peer->cfgfile);
-  peer->main_process = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL,
+  peer->main_process = GNUNET_OS_start_process (PIPE_CONTROL, 
+                                                GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
+                                                NULL, NULL,
                                                 peer->main_binary,
                                                 peer->main_binary,
+                                                peer->args,
                                                 "-c",
                                                 peer->cfgfile,
                                                 NULL);
@@ -863,23 +1022,62 @@ GNUNET_TESTING_peer_start (struct GNUNET_TESTING_Peer *peer)
 
 
 /**
- * Stop the peer. 
+ * Sends SIGTERM to the peer's main process
  *
- * @param peer peer to stop
- * @return GNUNET_OK on success, GNUNET_SYSERR on error (i.e. peer not running)
+ * @param peer the handle to the peer
+ * @return GNUNET_OK if successful; GNUNET_SYSERR if the main process is NULL
+ *           or upon any error while sending SIGTERM
  */
 int
-GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer)
+GNUNET_TESTING_peer_kill (struct GNUNET_TESTING_Peer *peer)
+{
+  if (NULL == peer->main_process)
+  {
+    GNUNET_break (0);
+    return GNUNET_SYSERR;
+  }
+  return (0 == GNUNET_OS_process_kill (peer->main_process, SIGTERM)) ?
+      GNUNET_OK : GNUNET_SYSERR;
+}
+
+
+/**
+ * Waits for a peer to terminate. The peer's main process will also be destroyed.
+ *
+ * @param peer the handle to the peer
+ * @return GNUNET_OK if successful; GNUNET_SYSERR if the main process is NULL
+ *           or upon any error while waiting
+ */
+int
+GNUNET_TESTING_peer_wait (struct GNUNET_TESTING_Peer *peer)
 {
+  int ret;
+
   if (NULL == peer->main_process)
   {
     GNUNET_break (0);
     return GNUNET_SYSERR;
   }
-  (void) GNUNET_OS_process_kill (peer->main_process, SIGTERM);
-  GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (peer->main_process));
+  ret = GNUNET_OS_process_wait (peer->main_process);
   GNUNET_OS_process_destroy (peer->main_process);
   peer->main_process = NULL;
+  return ret;
+}
+
+
+/**
+ * Stop the peer. 
+ *
+ * @param peer peer to stop
+ * @return GNUNET_OK on success, GNUNET_SYSERR on error
+ */
+int
+GNUNET_TESTING_peer_stop (struct GNUNET_TESTING_Peer *peer)
+{
+  if (GNUNET_SYSERR == GNUNET_TESTING_peer_kill (peer))
+    return GNUNET_SYSERR;
+  if (GNUNET_SYSERR == GNUNET_TESTING_peer_wait (peer))
+    return GNUNET_SYSERR;
   return GNUNET_OK;
 }
 
@@ -901,6 +1099,7 @@ GNUNET_TESTING_peer_destroy (struct GNUNET_TESTING_Peer *peer)
   }
   GNUNET_free (peer->cfgfile);
   GNUNET_free (peer->main_binary);
+  GNUNET_free (peer->args);
   GNUNET_free (peer);
 }
 
@@ -1007,9 +1206,11 @@ GNUNET_TESTING_service_run (const char *testdir,
   struct GNUNET_TESTING_System *system;
   struct GNUNET_TESTING_Peer *peer;
   struct GNUNET_CONFIGURATION_Handle *cfg;
+  char *binary;
+  char *libexec_binary;
 
   GNUNET_log_setup (testdir, "WARNING", NULL);
-  system = GNUNET_TESTING_system_create (testdir, "127.0.0.1");
+  system = GNUNET_TESTING_system_create (testdir, "127.0.0.1", NULL);
   if (NULL == system)
     return 1;
   cfg = GNUNET_CONFIGURATION_create ();
@@ -1030,7 +1231,20 @@ GNUNET_TESTING_service_run (const char *testdir,
     return 1;
   }
   GNUNET_free (peer->main_binary);
-  GNUNET_asprintf (&peer->main_binary, "gnunet-service-%s", service_name);
+  GNUNET_free (peer->args);
+  GNUNET_asprintf (&binary, "gnunet-service-%s", service_name);
+  libexec_binary = GNUNET_OS_get_libexec_binary_path (binary);
+  if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string(cfg, service_name, "PREFIX", &peer->main_binary))
+  {
+    /* No prefix */
+    GNUNET_asprintf(&peer->main_binary, "%s", libexec_binary);
+    peer->args = strdup ("");
+  }
+  else
+    peer->args = strdup (libexec_binary);
+
+  GNUNET_free (libexec_binary);
+  GNUNET_free (binary);
   if (GNUNET_OK != GNUNET_TESTING_peer_start (peer))
   {    
     GNUNET_TESTING_peer_destroy (peer);