- debug
[oweals/gnunet.git] / src / pt / test_gns_vpn.c
index 0c6e42500fc51c8d08cb476f6557a71c3757e5e2..6fe1e63a225c8a755b2ab007f2d4e9368db18218 100644 (file)
 #include <microhttpd.h>
 #include "gnunet_namestore_service.h"
 #include "gnunet_gns_service.h"
-#include "gnunet_testing_lib-new.h"
+#include "gnunet_testing_lib.h"
 
 #define PORT 8080
-#define TEST_DOMAIN "www.gnunet"
+#define TEST_DOMAIN "www.gads"
 
 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
 
@@ -239,6 +239,7 @@ curl_main ()
                                              NULL);  
 }
 
+
 static void
 start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
@@ -261,6 +262,15 @@ start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   curl_main ();
 }
 
+
+static void
+disco_ns (void* cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  GNUNET_NAMESTORE_disconnect (namestore);
+  namestore = NULL;
+}
+
+
 /**
  * Callback invoked from the namestore service once record is
  * created.
@@ -276,23 +286,19 @@ start_curl (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void
 commence_testing (void *cls, int32_t success, const char *emsg)
 {
-  GNUNET_NAMESTORE_disconnect (namestore);
-  
+  GNUNET_SCHEDULER_add_now (&disco_ns, NULL);
+
   if ((emsg != NULL) && (GNUNET_YES != success))
   {
     fprintf (stderr, 
             "NS failed to create record %s\n", emsg);
     GNUNET_SCHEDULER_shutdown ();
     return;
-  }
-  
-  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10), start_curl, NULL);
-
+  }  
+  GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10), &start_curl, NULL);
 }
 
 
-
-
 /**
  * Function to keep the HTTP server running.
  */
@@ -348,6 +354,7 @@ mhd_main ()
                                             NULL);  
 }
 
+
 static void
 run (void *cls,
      const struct GNUNET_CONFIGURATION_Handle *cfg,
@@ -387,7 +394,7 @@ run (void *cls,
 
   host_key = GNUNET_CRYPTO_rsa_key_create_from_file (zone_keyfile);
   rd.expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value;
-  GNUNET_asprintf (&rd_string, "6 %s %s", (char*)&peername, "www.gnunet.");
+  GNUNET_asprintf (&rd_string, "6 %s %s", (char*)&peername, "www.gads.");
   GNUNET_assert (GNUNET_OK == GNUNET_NAMESTORE_string_to_value (GNUNET_GNS_RECORD_VPN,
                                                                rd_string,
                                                                (void**)&rd.data,
@@ -408,32 +415,119 @@ run (void *cls,
 
 
 /**
- * Test if the given AF is supported by this system.
+ * Open '/dev/null' and make the result the given
+ * file descriptor.
+ *
+ * @param target_fd desired FD to point to /dev/null
+ * @param flags open flags (O_RDONLY, O_WRONLY)
+ */
+static void
+open_dev_null (int target_fd,
+              int flags)
+{
+  int fd;
+
+  fd = open ("/dev/null", flags);
+  if (-1 == fd)
+    abort ();
+  if (fd == target_fd)
+    return;
+  if (-1 == dup2 (fd, target_fd))
+  {    
+    (void) close (fd);
+    abort ();
+  }
+  (void) close (fd);
+}
+
+
+/**
+ * Run the given command and wait for it to complete.
  * 
- * @param af to test
- * @return GNUNET_OK if the AF is supported
+ * @param file name of the binary to run
+ * @param cmd command line arguments (as given to 'execv')
+ * @return 0 on success, 1 on any error
  */
 static int
-test_af (int af)
+fork_and_exec (const char *file, 
+              char *const cmd[])
 {
-  int s;
+  int status;
+  pid_t pid;
+  pid_t ret;
 
-  s = socket (af, SOCK_STREAM, 0);
-  if (-1 == s)
+  pid = fork ();
+  if (-1 == pid)
+  {
+    fprintf (stderr, 
+            "fork failed: %s\n", 
+            strerror (errno));
+    return 1;
+  }
+  if (0 == pid)
+  {
+    /* we are the child process */
+    /* close stdin/stdout to not cause interference
+       with the helper's main protocol! */
+    (void) close (0); 
+    open_dev_null (0, O_RDONLY);
+    (void) close (1); 
+    open_dev_null (1, O_WRONLY);
+    (void) execv (file, cmd);
+    /* can only get here on error */
+    fprintf (stderr, 
+            "exec `%s' failed: %s\n", 
+            file,
+            strerror (errno));
+    _exit (1);
+  }
+  /* keep running waitpid as long as the only error we get is 'EINTR' */
+  while ( (-1 == (ret = waitpid (pid, &status, 0))) &&
+         (errno == EINTR) ); 
+  if (-1 == ret)
   {
-    if (EAFNOSUPPORT == errno)
-      return GNUNET_NO;
-    fprintf (stderr, "Failed to create test socket: %s\n", STRERROR (errno));
-    return GNUNET_SYSERR;
+    fprintf (stderr, 
+            "waitpid failed: %s\n", 
+            strerror (errno));
+    return 1;
   }
-  close (s);
-  return GNUNET_OK;
+  if (! (WIFEXITED (status) && (0 == WEXITSTATUS (status))))
+    return 1;
+  /* child process completed and returned success, we're happy */
+  return 0;
 }
 
-
 int
 main (int argc, char *const *argv)
 {
+  char *sbin_iptables;
+  char *bin_vpn;
+  char *bin_exit;
+  char *bin_dns;
+  char *const iptables_args[] =
+  {
+    "iptables", "-t", "mangle", "-L", "-v", NULL
+  };
+  
+  if (0 == access ("/sbin/iptables", X_OK))
+    sbin_iptables = "/sbin/iptables";
+  else if (0 == access ("/usr/sbin/iptables", X_OK))
+    sbin_iptables = "/usr/sbin/iptables";
+  else
+  {
+    fprintf (stderr, 
+            "Executable iptables not found in approved directories: %s, skipping\n",
+            strerror (errno));
+    return 0;
+  }
+  
+  if (0 != fork_and_exec (sbin_iptables, iptables_args))
+  {
+    fprintf (stderr,
+             "Failed to run `iptables -t mangle -L -v'. Skipping test.\n");
+    return 0;
+  }
+
   if (0 != ACCESS ("/dev/net/tun", R_OK))
   {
     GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
@@ -443,32 +537,43 @@ main (int argc, char *const *argv)
             "WARNING: System unable to run test, skipping.\n");
     return 0;
   }
-  if ( (GNUNET_YES !=
-       GNUNET_OS_check_helper_binary ("gnunet-helper-vpn")) ||
-       (GNUNET_YES !=
-       GNUNET_OS_check_helper_binary ("gnunet-helper-exit")) ||
-       (GNUNET_YES !=
-  GNUNET_OS_check_helper_binary ("gnunet-helper-dns")))
-  {
+
+  bin_vpn = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-vpn");
+  bin_exit = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-exit");
+  bin_dns = GNUNET_OS_get_libexec_binary_path ("gnunet-helper-dns");
+  if ( (0 != geteuid ()) &&
+       ( (GNUNET_YES !=
+         GNUNET_OS_check_helper_binary (bin_vpn)) ||
+        (GNUNET_YES !=
+         GNUNET_OS_check_helper_binary (bin_exit)) ||
+        (GNUNET_YES !=
+         GNUNET_OS_check_helper_binary (bin_dns))) )
+  {    
     fprintf (stderr,
             "WARNING: gnunet-helper-{exit,vpn,dns} binaries in $PATH are not SUID, refusing to run test (as it would have to fail).\n");
     fprintf (stderr,
             "Change $PATH ('.' in $PATH before $GNUNET_PREFIX/bin is problematic) or permissions (run 'make install' as root) to fix this!\n");
+    GNUNET_free (bin_vpn);    
+    GNUNET_free (bin_exit);
+    GNUNET_free (bin_dns);
     return 0;
   }
-  GNUNET_CRYPTO_setup_hostkey ("test_gns_vpn.conf");
+  GNUNET_free (bin_vpn);    
+  GNUNET_free (bin_exit);
+  GNUNET_free (bin_dns);
+  GNUNET_CRYPTO_rsa_setup_hostkey ("test_gns_vpn.conf");
   
   dest_ip = "169.254.86.1";
   dest_af = AF_INET;
   src_af = AF_INET;
 
-  if (GNUNET_OK == test_af (AF_INET6))
+  if (GNUNET_OK == GNUNET_NETWORK_test_pf (PF_INET6))
     use_v6 = GNUNET_YES;
   else
     use_v6 = GNUNET_NO;
   
-  if ( (GNUNET_OK != test_af (src_af)) ||
-       (GNUNET_OK != test_af (dest_af)) )
+  if ( (GNUNET_OK != GNUNET_NETWORK_test_pf (src_af)) ||
+       (GNUNET_OK != GNUNET_NETWORK_test_pf (dest_af)) )
   {
     fprintf (stderr, 
             "Required address families not supported by this system, skipping test.\n");