fix
[oweals/gnunet.git] / src / transport / test_plugin_transport.c
index dbd6c6c6300a66bfc6220e52edfb9418b43e5487..db988fbe9dac31f3473d1e5466a04e95328aa5a2 100644 (file)
@@ -40,7 +40,8 @@
 /**
  * How long until we give up on transmitting the message?
  */
-#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
+#define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
+#define TIMEOUT GNUNET_TIME_relative_multiply (WAIT, 3)
 
 /**
  * Our public key.
@@ -67,6 +68,11 @@ const struct GNUNET_CONFIGURATION_Handle *cfg;
  */
 struct GNUNET_STATISTICS_Handle *stats;
 
+/**
+ * Our HELLO
+ */
+struct GNUNET_HELLO_Message *hello;
+
 /**
  * Number of neighbours we'd like to have.
  */
@@ -82,19 +88,38 @@ struct GNUNET_TRANSPORT_PluginEnvironment env;
  */
 struct GNUNET_TRANSPORT_PluginFunctions *api;
 
+/**
+ * Helper handler
+ */
+struct GNUNET_HELPER_Handle *suid_helper;
+
 /**
  * Timeout task
  */
 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
 
+/**
+ * Timeout task
+ */
+static GNUNET_SCHEDULER_TaskIdentifier timeout_wait;
+
 /**
  * Library name
  */
 static char *libname;
 
+/**
+ * Plugin addresses head
+ */
 struct AddressWrapper *head;
+
+/**
+ * Plugin addresses tail
+ */
 struct AddressWrapper *tail;
 
+unsigned int addresses_reported;
+
 /**
  * Did the test pass or fail?
  */
@@ -115,37 +140,38 @@ struct AddressWrapper
 static void
 end ()
 {
-  if (NULL != head)
-  {
-
-  }
-
   if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
   {
       GNUNET_SCHEDULER_cancel (timeout_task);
       timeout_task = GNUNET_SCHEDULER_NO_TASK;
   }
-
   if (NULL != api)
-  {
       GNUNET_PLUGIN_unload (libname, api);
-  }
   GNUNET_free (libname);
   libname = NULL;
   GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
   stats = NULL;
 
+  if (NULL != suid_helper)
+  {
+    GNUNET_HELPER_stop (suid_helper);
+    suid_helper = NULL;
+  }
+
   ok = 0;
 }
 
 static void
-
 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct AddressWrapper *w;
   int c = 0;
-
   timeout_task = GNUNET_SCHEDULER_NO_TASK;
+  if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
+  {
+      GNUNET_SCHEDULER_cancel (timeout_wait);
+      timeout_wait = GNUNET_SCHEDULER_NO_TASK;
+  }
   if (NULL != libname)
   {
     if (NULL != api)
@@ -175,13 +201,34 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     stats = NULL;
   }
 
+  if (NULL != suid_helper)
+  {
+    GNUNET_HELPER_stop (suid_helper);
+    suid_helper = NULL;
+  }
+
   ok = 1;
 }
 
+static void
+wait_end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  timeout_wait = GNUNET_SCHEDULER_NO_TASK;
+  if (0 == addresses_reported)
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              _("Plugin did not report any addresses, could not check address conversion functions\n"));
+  end ();
+}
+
 
 static void
 end_badly_now ()
 {
+  if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
+  {
+      GNUNET_SCHEDULER_cancel (timeout_wait);
+      timeout_wait = GNUNET_SCHEDULER_NO_TASK;
+  }
   if (GNUNET_SCHEDULER_NO_TASK != timeout_task)
   {
       GNUNET_SCHEDULER_cancel (timeout_task);
@@ -202,7 +249,6 @@ env_receive (void *cls,
             uint16_t sender_address_len)
 {
   /* do nothing */
-  GNUNET_break (0);
   return GNUNET_TIME_relative_get_zero_();
 }
 
@@ -211,7 +257,8 @@ static void
 env_notify_address (void *cls,
                     int add_remove,
                     const void *addr,
-                    size_t addrlen)
+                    size_t addrlen,
+                    const char *plugin)
 {
   struct AddressWrapper *w;
   char *a2s;
@@ -220,7 +267,8 @@ env_notify_address (void *cls,
 
   if (GNUNET_YES == add_remove)
   {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+      addresses_reported ++;
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   _("Adding address of length %u\n"), addrlen);
 
       w = GNUNET_malloc (sizeof (struct AddressWrapper));
@@ -242,7 +290,7 @@ env_notify_address (void *cls,
       GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                   _("Plugin added address `%s'\n"), a2s);
 
-      if (GNUNET_OK != api->string_to_address (api, a2s, strlen (a2s)+1, &s2a, &s2a_len))
+      if ((GNUNET_OK != api->string_to_address (api, a2s, strlen (a2s)+1, &s2a, &s2a_len)) || (NULL == s2a))
       {
           GNUNET_break (0);
           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -262,7 +310,8 @@ env_notify_address (void *cls,
             GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                         _("Plugin creates different address length when connecting back and forth!\n"));
       }
-
+      GNUNET_free (s2a);
+      GNUNET_free (a2s);
       if (GNUNET_OK != api->check_address (api->cls, w->addr, w->addrlen))
       {
           GNUNET_break (0);
@@ -271,10 +320,16 @@ env_notify_address (void *cls,
           end_badly_now();
           return;
       }
+      if (GNUNET_SCHEDULER_NO_TASK != timeout_wait)
+      {
+          GNUNET_SCHEDULER_cancel (timeout_wait);
+          timeout_wait = GNUNET_SCHEDULER_NO_TASK;
+      }
+      timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
   }
   else if (GNUNET_NO == add_remove)
   {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   _("Removing address of length %u\n"), addrlen);
 
       w = head;
@@ -304,8 +359,9 @@ env_notify_address (void *cls,
   {
       GNUNET_break (0);
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  _("Invalid operation\n"));
+                  _("Invalid operation: %u \n"), add_remove);
       end_badly_now ();
+      return;
   }
 }
 
@@ -315,17 +371,15 @@ env_get_address_type (void *cls,
                      size_t addrlen)
 {
   struct GNUNET_ATS_Information ats;
-  ats.type = htonl (0);
-  ats.value = htonl (0);
+  ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
+  ats.value = htonl (GNUNET_ATS_NET_LOOPBACK);
   return ats;
 }
 
-
 const struct GNUNET_MessageHeader *
 env_get_our_hello (void)
 {
-  GNUNET_break (0);
-  return NULL;
+  return (const struct GNUNET_MessageHeader *) hello;
 }
 
 void env_session_end (void *cls,
@@ -351,6 +405,12 @@ setup_plugin_environment ()
   env.session_end = &env_session_end;
 }
 
+static int
+handle_helper_message (void *cls, void *client,
+                       const struct GNUNET_MessageHeader *hdr)
+{
+  return GNUNET_OK;
+}
 
 /**
  * Runs the test.
@@ -409,10 +469,14 @@ run (void *cls, char *const *args, const char *cfgfile,
   GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
                       &my_identity.hashPubKey);
 
+
+  hello = GNUNET_HELLO_create(&my_public_key, NULL, NULL);
+
   /* load plugins... */
   setup_plugin_environment ();
 
-  plugin = strrchr(argv[0],'_');
+  GNUNET_assert (strlen (argv[0]) > strlen ("test_plugin_"));
+  plugin = strstr(argv[0],"test_plugin_");
   sep = strrchr(argv[0],'.');
   if (NULL == plugin)
   {
@@ -420,10 +484,25 @@ run (void *cls, char *const *args, const char *cfgfile,
       end_badly_now ();
       return;
   }
-  plugin++;
+  plugin += strlen ("test_plugin_");
   if (NULL != sep)
       sep[0] = '\0';
 
+  /* Hack for WLAN: start a second helper */
+  if (0 == strcmp (plugin, "wlan"))
+  {
+    char * helper_argv[3];
+    helper_argv[0] = (char *) "gnunet-helper-transport-wlan-dummy";
+    helper_argv[1] = (char *) "2";
+    helper_argv[2] = NULL;
+    suid_helper = GNUNET_HELPER_start ("gnunet-helper-transport-wlan-dummy",
+                                       helper_argv,
+                                       &handle_helper_message,
+                                       NULL,
+                                       NULL);
+  }
+
+
   /* Loading plugin */
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading transport plugin %s\n"), plugin);
   GNUNET_asprintf (&libname, "libgnunet_plugin_transport_%s", plugin);
@@ -431,11 +510,13 @@ run (void *cls, char *const *args, const char *cfgfile,
   if (api == NULL)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Failed to load transport plugin for tcp\n"));
+                _("Failed to load transport plugin for %s\n"), plugin);
     end_badly_now ();
     return;
   }
 
+  timeout_wait = GNUNET_SCHEDULER_add_delayed (WAIT, &wait_end, NULL);
+
   /* Check if all functions are implemented */
   if (NULL == api->address_pretty_printer)
   {