changes
[oweals/gnunet.git] / src / transport / test_plugin_transport.c
index dbd6c6c6300a66bfc6220e52edfb9418b43e5487..e3ab5fd561792e64fcb4874b08d17acaea78ff48 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.
  */
@@ -87,14 +93,28 @@ struct GNUNET_TRANSPORT_PluginFunctions *api;
  */
 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,21 +135,13 @@ 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);
@@ -139,13 +151,16 @@ end ()
 }
 
 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)
@@ -178,10 +193,25 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   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_ERROR,
+              _("Plugin did not report any addresses, could not check plugin \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 +232,6 @@ env_receive (void *cls,
             uint16_t sender_address_len)
 {
   /* do nothing */
-  GNUNET_break (0);
   return GNUNET_TIME_relative_get_zero_();
 }
 
@@ -220,7 +249,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 +272,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 +292,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 +302,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 +341,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 +353,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,
@@ -409,10 +445,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,7 +460,7 @@ run (void *cls, char *const *args, const char *cfgfile,
       end_badly_now ();
       return;
   }
-  plugin++;
+  plugin += strlen ("test_plugin_");
   if (NULL != sep)
       sep[0] = '\0';
 
@@ -431,11 +471,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)
   {