tests for mantis #1028
authorMatthias Wachs <wachs@net.in.tum.de>
Tue, 22 Nov 2011 14:22:28 +0000 (14:22 +0000)
committerMatthias Wachs <wachs@net.in.tum.de>
Tue, 22 Nov 2011 14:22:28 +0000 (14:22 +0000)
src/transport/test_transport_api_restart_1peer.c
src/transport/test_transport_api_restart_2peers.c
src/transport/transport-testing.c
src/transport/transport-testing.h

index fb06c26ab685be785d9c909ede003a1f6feede8f..67530f42136faa7a92b84f93079362c1ea3a5693 100644 (file)
@@ -44,7 +44,7 @@
 /**
  * How long until we give up on transmitting the message?
  */
-#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
 
 /**
  * How long until we give up on transmitting the message?
 
 #define MTYPE 12345
 
-static char *test_source;
-
-static char *test_plugin;
-
 static char *test_name;
 
 static int ok;
@@ -65,6 +61,8 @@ static GNUNET_SCHEDULER_TaskIdentifier die_task;
 
 static GNUNET_SCHEDULER_TaskIdentifier send_task;
 
+static GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
+
 struct PeerContext *p1;
 
 struct PeerContext *p2;
@@ -95,9 +93,15 @@ end ()
 
   if (send_task != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_SCHEDULER_cancel (send_task);
+  send_task = GNUNET_SCHEDULER_NO_TASK;
+
+  if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
+    GNUNET_SCHEDULER_cancel (reconnect_task);
+  reconnect_task = GNUNET_SCHEDULER_NO_TASK;
 
   if (die_task != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_SCHEDULER_cancel (die_task);
+  die_task = GNUNET_SCHEDULER_NO_TASK;
 
   if (th != NULL)
     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
@@ -118,10 +122,15 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted\n");
 
   if (restarted == GNUNET_NO)
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NO restarted\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NOT restarted\n");
+
+  if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
+    GNUNET_SCHEDULER_cancel (reconnect_task);
+  reconnect_task = GNUNET_SCHEDULER_NO_TASK;
 
   if (send_task != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_SCHEDULER_cancel (send_task);
+  send_task = GNUNET_SCHEDULER_NO_TASK;
 
   if (cc != NULL)
   {
@@ -142,6 +151,28 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   ok = GNUNET_SYSERR;
 }
 
+static void
+reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct PeerContext *p = cls;
+  reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+
+  GNUNET_TRANSPORT_try_connect (p->th, &p2->id);
+  reconnect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, &reconnect, p);
+}
+
+static void
+restart_cb (struct PeerContext *p, void *cls)
+{
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Restarted peer %u (`%4s'), issuing reconnect\n",
+              p->no,
+              GNUNET_i2s (&p->id));
+
+  reconnect_task = GNUNET_SCHEDULER_add_now(&reconnect, p);
+}
+
 static
 void restart (struct PeerContext *p , char * cfg_file)
 {
@@ -150,7 +181,7 @@ void restart (struct PeerContext *p , char * cfg_file)
               "Restarting peer %u (`%4s')\n",
               p->no,
               GNUNET_i2s (&p->id));
-  GNUNET_TRANSPORT_TESTING_restart_peer (tth, p1, cfg_file);
+  GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, cfg_file, &restart_cb, p);
   return;
 }
 
@@ -169,17 +200,28 @@ notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
   GNUNET_assert (t != NULL);
 
   char *ps = strdup (GNUNET_i2s (&p->id));
-
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
               GNUNET_i2s (&t->id));
+  GNUNET_free (ps);
 
   if ((MTYPE == ntohs (message->type)) &&
       (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
   {
-    restarted = GNUNET_YES;
-    restart (p1, cfg_file_p1);
+    if (restarted == GNUNET_NO)
+    {
+      restarted = GNUNET_YES;
+      restart (p1, cfg_file_p1);
+      return;
+    }
+    else
+    {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Restarted peers connected, stopping test...\n");
+      ok = 0;
+      end ();
+    }
   }
   else
   {
@@ -268,13 +310,15 @@ notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
 
   char *ps = strdup (GNUNET_i2s (&p->id));
 
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
               t->no, GNUNET_i2s (peer));
   GNUNET_free (ps);
 
   if ((restarted == GNUNET_YES) && (c == 4))
-    end ();
+  {
+    send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
+  }
 }
 
 
@@ -282,11 +326,13 @@ static void
 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
 {
   struct PeerContext *p = cls;
-  char *ps = strdup (GNUNET_i2s (&p->id));
 
+
+  char *ps = strdup (GNUNET_i2s (&p->id));
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
               GNUNET_i2s (peer));
+  GNUNET_free (ps);
 
   if (th != NULL)
     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
@@ -322,10 +368,10 @@ start_cb (struct PeerContext *p, void *cls)
     return;
 
   char *sender_c = strdup (GNUNET_i2s (&p1->id));
-
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
+  GNUNET_free (sender_c);
 
   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
                                                NULL);
@@ -390,12 +436,9 @@ int
 main (int argc, char *argv[])
 {
   int ret;
-  int nat_res;
 
   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
-  GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
-  GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
-                                                 &test_plugin);
+
 
   GNUNET_log_setup (test_name,
 #if VERBOSE
@@ -407,23 +450,6 @@ main (int argc, char *argv[])
 
   tth = GNUNET_TRANSPORT_TESTING_init ();
 
-  if ((strcmp (test_plugin, "tcp_nat") == 0) ||
-      (strcmp (test_plugin, "udp_nat") == 0))
-  {
-    nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
-    if (GNUNET_NO == nat_res)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
-                  "gnunet-nat-server", "SUID not set");
-      return 0;
-    }
-    if (GNUNET_SYSERR == nat_res)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
-                  "gnunet-nat-server", "file not found");
-      return 0;
-    }
-  }
 
   GNUNET_asprintf(&cfg_file_p1,"test_transport_api_tcp_peer1.conf");
   GNUNET_asprintf(&cfg_file_p2,"test_transport_api_tcp_peer2.conf");
@@ -434,8 +460,6 @@ main (int argc, char *argv[])
   GNUNET_free (cfg_file_p1);
   GNUNET_free (cfg_file_p2);
 
-  GNUNET_free (test_source);
-  GNUNET_free (test_plugin);
   GNUNET_free (test_name);
 
   GNUNET_TRANSPORT_TESTING_done (tth);
index af9b4638572a02760cb23dcd29088bc8ebb1c1bb..dab8f39a6f7f3b3bd9b9f1e4838868f985d3b6ae 100644 (file)
@@ -44,7 +44,7 @@
 /**
  * How long until we give up on transmitting the message?
  */
-#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
 
 /**
  * How long until we give up on transmitting the message?
 
 #define MTYPE 12345
 
-static char *test_source;
-
-static char *test_plugin;
-
 static char *test_name;
 
 static int ok;
@@ -65,6 +61,8 @@ static GNUNET_SCHEDULER_TaskIdentifier die_task;
 
 static GNUNET_SCHEDULER_TaskIdentifier send_task;
 
+static GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
+
 struct PeerContext *p1;
 
 struct PeerContext *p2;
@@ -95,9 +93,15 @@ end ()
 
   if (send_task != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_SCHEDULER_cancel (send_task);
+  send_task = GNUNET_SCHEDULER_NO_TASK;
+
+  if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
+    GNUNET_SCHEDULER_cancel (reconnect_task);
+  reconnect_task = GNUNET_SCHEDULER_NO_TASK;
 
   if (die_task != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_SCHEDULER_cancel (die_task);
+  die_task = GNUNET_SCHEDULER_NO_TASK;
 
   if (th != NULL)
     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
@@ -115,13 +119,18 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
 
   if (restarted == GNUNET_YES)
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers were restarted\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was restarted\n");
 
   if (restarted == GNUNET_NO)
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers were NOT restarted\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peer was NOT restarted\n");
+
+  if (reconnect_task != GNUNET_SCHEDULER_NO_TASK)
+    GNUNET_SCHEDULER_cancel (reconnect_task);
+  reconnect_task = GNUNET_SCHEDULER_NO_TASK;
 
   if (send_task != GNUNET_SCHEDULER_NO_TASK)
     GNUNET_SCHEDULER_cancel (send_task);
+  send_task = GNUNET_SCHEDULER_NO_TASK;
 
   if (cc != NULL)
   {
@@ -142,6 +151,33 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   ok = GNUNET_SYSERR;
 }
 
+static void
+reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct PeerContext *p = cls;
+  reconnect_task = GNUNET_SCHEDULER_NO_TASK;
+
+  GNUNET_TRANSPORT_try_connect (p1->th, &p2->id);
+  reconnect_task = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, &reconnect, p);
+}
+
+static void
+restart_cb (struct PeerContext *p, void *cls)
+{
+  static int c;
+  c++;
+
+  if (c != 2)
+    return;
+
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Restarted peer %u (`%4s'), issuing reconnect\n",
+              p->no,
+              GNUNET_i2s (&p->id));
+
+  reconnect_task = GNUNET_SCHEDULER_add_now(&reconnect, p);
+}
+
 static
 void restart (struct PeerContext *p , char * cfg_file)
 {
@@ -150,7 +186,7 @@ void restart (struct PeerContext *p , char * cfg_file)
               "Restarting peer %u (`%4s')\n",
               p->no,
               GNUNET_i2s (&p->id));
-  GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, cfg_file);
+  GNUNET_TRANSPORT_TESTING_restart_peer (tth, p, cfg_file, &restart_cb, p);
   return;
 }
 
@@ -169,18 +205,29 @@ notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
   GNUNET_assert (t != NULL);
 
   char *ps = strdup (GNUNET_i2s (&p->id));
-
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
               p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
               GNUNET_i2s (&t->id));
+  GNUNET_free (ps);
 
   if ((MTYPE == ntohs (message->type)) &&
       (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
   {
-    restarted = GNUNET_YES;
-    restart (p1, cfg_file_p1);
-    restart (p2, cfg_file_p2);
+    if (restarted == GNUNET_NO)
+    {
+      restarted = GNUNET_YES;
+      restart (p1, cfg_file_p1);
+      restart (p2, cfg_file_p2);
+      return;
+    }
+    else
+    {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Restarted peers connected, stopping test...\n");
+      ok = 0;
+      end ();
+    }
   }
   else
   {
@@ -269,13 +316,15 @@ notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
 
   char *ps = strdup (GNUNET_i2s (&p->id));
 
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
               t->no, GNUNET_i2s (peer));
   GNUNET_free (ps);
 
   if ((restarted == GNUNET_YES) && (c == 4))
-    end ();
+  {
+    send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
+  }
 }
 
 
@@ -283,11 +332,13 @@ static void
 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
 {
   struct PeerContext *p = cls;
-  char *ps = strdup (GNUNET_i2s (&p->id));
 
+
+  char *ps = strdup (GNUNET_i2s (&p->id));
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
               GNUNET_i2s (peer));
+  GNUNET_free (ps);
 
   if (th != NULL)
     GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
@@ -323,10 +374,10 @@ start_cb (struct PeerContext *p, void *cls)
     return;
 
   char *sender_c = strdup (GNUNET_i2s (&p1->id));
-
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
               p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
+  GNUNET_free (sender_c);
 
   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
                                                NULL);
@@ -391,12 +442,9 @@ int
 main (int argc, char *argv[])
 {
   int ret;
-  int nat_res;
 
   GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
-  GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
-  GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
-                                                 &test_plugin);
+
 
   GNUNET_log_setup (test_name,
 #if VERBOSE
@@ -408,23 +456,6 @@ main (int argc, char *argv[])
 
   tth = GNUNET_TRANSPORT_TESTING_init ();
 
-  if ((strcmp (test_plugin, "tcp_nat") == 0) ||
-      (strcmp (test_plugin, "udp_nat") == 0))
-  {
-    nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
-    if (GNUNET_NO == nat_res)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
-                  "gnunet-nat-server", "SUID not set");
-      return 0;
-    }
-    if (GNUNET_SYSERR == nat_res)
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
-                  "gnunet-nat-server", "file not found");
-      return 0;
-    }
-  }
 
   GNUNET_asprintf(&cfg_file_p1,"test_transport_api_tcp_peer1.conf");
   GNUNET_asprintf(&cfg_file_p2,"test_transport_api_tcp_peer2.conf");
@@ -435,13 +466,10 @@ main (int argc, char *argv[])
   GNUNET_free (cfg_file_p1);
   GNUNET_free (cfg_file_p2);
 
-  GNUNET_free (test_source);
-  GNUNET_free (test_plugin);
   GNUNET_free (test_name);
 
   GNUNET_TRANSPORT_TESTING_done (tth);
 
   return ret;
 }
-
 /* end of test_transport_api_restart_2peers.c */
index d11c885f811ba00ded265a6a3a8ca4b9f466afe3..a8f034c9c781ccbc594cd8b8cb1f1a051e28b1c1 100644 (file)
@@ -27,7 +27,6 @@
 
 #include "transport-testing.h"
 
-#define VERBOSE GNUNET_YES
 #define HOSTKEYFILESIZE 914
 
 static const char *
@@ -358,11 +357,14 @@ GNUNET_TRANSPORT_TESTING_start_peer (struct GNUNET_TRANSPORT_TESTING_handle
 int
 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
                                        struct PeerContext *p,
-                                       const char *cfgname)
+                                       const char *cfgname,
+                                       GNUNET_TRANSPORT_TESTING_start_cb restart_cb,
+                                       void *cb_cls)
 {
   struct GNUNET_DISK_FileHandle *fn;
   int success = GNUNET_OK;
 
+  GNUNET_assert (tth != NULL);
   GNUNET_assert (p != NULL);
   GNUNET_assert (p->hostkeyfile != NULL);
   GNUNET_assert (p->servicehome != NULL);
@@ -396,6 +398,7 @@ GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle *t
     GNUNET_CONFIGURATION_destroy (p->cfg);
   p->cfg = NULL;
 
+
   /* start */
 #if VERBOSE
     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "transport-testing",
@@ -403,8 +406,8 @@ GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle *t
                      GNUNET_i2s (&p->id));
 #endif
 
+  sleep (5);
 
-  GNUNET_assert (tth != NULL);
   if (GNUNET_DISK_file_test (cfgname) == GNUNET_NO)
   {
   GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "transport-testing",
@@ -447,6 +450,9 @@ GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle *t
                                &notify_connect, &notify_disconnect);
   GNUNET_assert (p->th != NULL);
 
+  p->start_cb = restart_cb;
+  p->cb_cls = cb_cls;
+
   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &get_hello, p);
   GNUNET_assert (p->ghh != NULL);
 
index 6af2d68f529a9b11774c7bebc02e20aa85701de1..883c688379b83c72546c2857572469e38d8bee65 100644 (file)
@@ -34,6 +34,7 @@
 #include "gnunet_container_lib.h"
 #include "gnunet_transport_service.h"
 
+
 #define GNUNET_TRANSPORT_TESTING_ConnectRequest void *
 
 
@@ -170,7 +171,9 @@ GNUNET_TRANSPORT_TESTING_stop_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
 int
 GNUNET_TRANSPORT_TESTING_restart_peer (struct GNUNET_TRANSPORT_TESTING_handle *tth,
                                        struct PeerContext *p,
-                                       const char *cfgname);
+                                       const char *cfgname,
+                                       GNUNET_TRANSPORT_TESTING_start_cb start_cb,
+                                       void *cb_cls);
 
 /**
  * Connect the given peers and call the callback when both peers report the