-add adv port
[oweals/gnunet.git] / src / testing / testing.c
index 8a5ff66797561b33b5e6830920497cbb5cb366aa..f4df6d48c45376ee82691f38db44c150502bf63a 100644 (file)
 #define LOG(kind,...)                                           \
   GNUNET_log_from (kind, "testing-api", __VA_ARGS__)
 
-/**
- * Size of a hostkey when written to a file
- */
-#define HOSTKEYFILESIZE 914
-
 /**
  * Lowest port used for GNUnet testing.  Should be high enough to not
  * conflict with other applications running on the hosts but be low
@@ -79,7 +74,7 @@ struct GNUNET_TESTING_System
   char *hostname;
 
   /**
-   * Hostkeys data, contains "HOSTKEYFILESIZE * total_hostkeys" bytes.
+   * Hostkeys data, contains "GNUNET_TESTING_HOSTKEYFILESIZE * total_hostkeys" bytes.
    */
   char *hostkeys_data;
 
@@ -213,7 +208,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);
@@ -228,7 +223,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,
@@ -368,6 +363,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;
@@ -414,19 +410,25 @@ GNUNET_TESTING_reserve_port (struct GNUNET_TESTING_System *system,
       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,
@@ -524,8 +526,9 @@ GNUNET_TESTING_hostkey_get (const struct GNUNET_TESTING_System *system,
     return NULL;
   }   
   private_key = GNUNET_CRYPTO_rsa_decode_key (system->hostkeys_data +
-                                              (key_number * HOSTKEYFILESIZE),
-                                              HOSTKEYFILESIZE);
+                                              (key_number *
+                                               GNUNET_TESTING_HOSTKEYFILESIZE),
+                                              GNUNET_TESTING_HOSTKEYFILESIZE);
   if (NULL == private_key)
   {
     LOG (GNUNET_ERROR_TYPE_ERROR,
@@ -841,7 +844,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
@@ -867,7 +870,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
@@ -892,16 +895,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
@@ -920,7 +923,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
@@ -930,7 +933,7 @@ 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");
+  peer->main_binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
   peer->system = system;
   peer->key_number = key_number;
   return peer;
@@ -1131,6 +1134,7 @@ GNUNET_TESTING_service_run (const char *testdir,
   struct GNUNET_TESTING_System *system;
   struct GNUNET_TESTING_Peer *peer;
   struct GNUNET_CONFIGURATION_Handle *cfg;
+  char *binary;
 
   GNUNET_log_setup (testdir, "WARNING", NULL);
   system = GNUNET_TESTING_system_create (testdir, "127.0.0.1", NULL);
@@ -1154,7 +1158,9 @@ 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_asprintf (&binary, "gnunet-service-%s", service_name);
+  peer->main_binary = GNUNET_OS_get_libexec_binary_path (binary);
+  GNUNET_free (binary);
   if (GNUNET_OK != GNUNET_TESTING_peer_start (peer))
   {    
     GNUNET_TESTING_peer_destroy (peer);