stuff
[oweals/gnunet.git] / src / transport / test_transport_api_reliability.c
index 50a9cc80e39398cc5f4b099a95d55acd86b142ee..32697e52c4537f9c48197ffdf6c199887b27162b 100644 (file)
@@ -49,7 +49,7 @@
  * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
  * messages may be dropped even for a reliable transport.
  */
-#define TOTAL_MSGS (10000 * 2)
+#define TOTAL_MSGS (1024 * 2)
 
 /**
  * How long until we give up on transmitting the message?
@@ -80,6 +80,8 @@ static int is_wlan;
 
 static int connected;
 
+static int test_failed;
+
 static unsigned long long total_bytes;
 
 static struct GNUNET_TIME_Absolute start_time;
@@ -101,6 +103,9 @@ static int msg_sent;
 static int msg_recv_expected;
 static int msg_recv;
 
+static int p1_hello_canceled;
+static int p2_hello_canceled;
+
 #if VERBOSE
 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
 #else
@@ -160,14 +165,18 @@ stop_arm (struct PeerContext *p)
 
 static void
 end_badly (void *cls,
-          const struct GNUNET_SCHEDULER_TaskContext *tc)
+           const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-             "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
-             msg_sent, 
-             msg_scheduled, 
-             msg_recv, 
-             msg_recv_expected);
+  if (test_failed == GNUNET_NO)
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Testcase timeout\n");
+    else
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+              "Reliability failed: Last message sent %u, Next message scheduled %u, Last message received %u, Message expected %u\n",
+              msg_sent,
+              msg_scheduled,
+              msg_recv,
+              msg_recv_expected);
   if (th_p2 != NULL)
     GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
   th_p2 = NULL;
@@ -184,6 +193,7 @@ end_badly (void *cls,
 }
 
 
+
 struct TestMessage
 {
   struct GNUNET_MessageHeader header;
@@ -196,8 +206,6 @@ get_size (unsigned int iter)
 {
   unsigned int ret;
 
-  if (iter < 60000)
-    return iter + sizeof (struct TestMessage);
   ret = (iter * iter * iter);
   return sizeof (struct TestMessage) + (ret % 60000);
 }
@@ -230,6 +238,7 @@ notify_receive (void *cls,
                  ntohl (hdr->num));
       if (die_task != GNUNET_SCHEDULER_NO_TASK)
         GNUNET_SCHEDULER_cancel (die_task);
+      test_failed = GNUNET_YES;
       die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
       return;
     }
@@ -242,6 +251,7 @@ notify_receive (void *cls,
                  ntohl (hdr->num));
       if (die_task != GNUNET_SCHEDULER_NO_TASK)
         GNUNET_SCHEDULER_cancel (die_task);
+      test_failed = GNUNET_YES;
       die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
       return;
     }
@@ -255,6 +265,7 @@ notify_receive (void *cls,
                  n, (unsigned char) n);
       if (die_task != GNUNET_SCHEDULER_NO_TASK)
         GNUNET_SCHEDULER_cancel (die_task);
+      test_failed = GNUNET_YES;
       die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
       return;
     }
@@ -330,7 +341,8 @@ notify_ready (void *cls, size_t size, void *buf)
   while (size - ret >= s);
   if (n < TOTAL_MSGS)
   {
-    th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
+    if (th_p2 == NULL)
+      th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
                                            &p1.id,
                                            s, 0, TIMEOUT,
                                            &notify_ready,
@@ -357,6 +369,11 @@ notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
               "Peer `%4s' disconnected (%p)!\n",
              GNUNET_i2s (peer), cls);
 #endif
+  if (th_p2 != NULL)
+    {
+      GNUNET_TRANSPORT_notify_transmit_ready_cancel(th_p2);
+      th_p2 = NULL;
+    }
 }
 
 
@@ -366,11 +383,11 @@ exchange_hello_last (void *cls,
 {
   struct PeerContext *me = cls;
 
+  GNUNET_assert (message != NULL);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Exchanging HELLO of size %d with peer (%s)!\n", 
              (int) GNUNET_HELLO_size((const struct GNUNET_HELLO_Message *)message),
              GNUNET_i2s (&me->id));
-  GNUNET_assert (message != NULL);
   GNUNET_assert (GNUNET_OK ==
                  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *)
                                       message, &me->id));
@@ -428,10 +445,18 @@ notify_connect (void *cls,
       if (die_task != GNUNET_SCHEDULER_NO_TASK)
         GNUNET_SCHEDULER_cancel (die_task);
       if (tct != GNUNET_SCHEDULER_NO_TASK)
-      GNUNET_SCHEDULER_cancel (tct);
+        GNUNET_SCHEDULER_cancel (tct);
       tct = GNUNET_SCHEDULER_NO_TASK;
-      GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
-      GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
+      if (p2_hello_canceled == GNUNET_NO)
+      {
+        GNUNET_TRANSPORT_get_hello_cancel (p2.th, &exchange_hello_last, &p2);
+        p2_hello_canceled = GNUNET_YES;
+      }
+      if (p1_hello_canceled == GNUNET_NO)
+      {
+        GNUNET_TRANSPORT_get_hello_cancel (p1.th, &exchange_hello, &p1);
+        p1_hello_canceled = GNUNET_YES;
+      }
       die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
                                               &end_badly, NULL);
       th_p2 = GNUNET_TRANSPORT_notify_transmit_ready (p2.th,
@@ -450,8 +475,13 @@ setup_peer (struct PeerContext *p, const char *cfgname)
   p->cfg = GNUNET_CONFIGURATION_create ();
   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
   if (GNUNET_CONFIGURATION_have_value (p->cfg,"PATHS", "SERVICEHOME"))
-      GNUNET_CONFIGURATION_get_value_string (p->cfg, "PATHS", "SERVICEHOME", &p->servicehome);
-  GNUNET_DISK_directory_remove (p->servicehome);
+    {
+      GNUNET_assert (GNUNET_OK ==
+                    GNUNET_CONFIGURATION_get_value_string (p->cfg, 
+                                                           "PATHS", "SERVICEHOME",
+                                                           &p->servicehome));
+      GNUNET_DISK_directory_remove (p->servicehome);
+    }
 
 #if START_ARM
   p->arm_proc = GNUNET_OS_start_process (NULL, NULL,
@@ -722,7 +752,9 @@ run (void *cls,
   GNUNET_assert(p1.th != NULL);
   GNUNET_assert(p2.th != NULL);
   GNUNET_TRANSPORT_get_hello (p1.th, &exchange_hello, &p1);
+  p1_hello_canceled = GNUNET_NO;
   GNUNET_TRANSPORT_get_hello (p2.th, &exchange_hello_last, &p2);
+  p2_hello_canceled = GNUNET_NO;
   tct = GNUNET_SCHEDULER_add_now (&try_connect, NULL);
 }
 
@@ -812,8 +844,6 @@ check ()
     GNUNET_free(key_file_p2);
     GNUNET_free(cert_file_p1);
     GNUNET_free(cert_file_p2);
-    GNUNET_free(p1.servicehome);
-    GNUNET_free(p2.servicehome);
   }
 
   return ok;
@@ -824,9 +854,8 @@ int
 main (int argc, char *argv[])
 {
   int ret;
-#ifdef MINGW
-  return GNUNET_SYSERR;
-#endif
+
+  test_failed = GNUNET_NO;
 
   if (strstr(argv[0], "tcp_nat") != NULL)
     {
@@ -870,8 +899,16 @@ main (int argc, char *argv[])
 #endif
                     NULL);
   ret = check ();
-  GNUNET_DISK_directory_remove (p1.servicehome);
-  GNUNET_DISK_directory_remove (p2.servicehome);
+  if (p1.servicehome != NULL)
+    {
+      GNUNET_DISK_directory_remove (p1.servicehome);
+      GNUNET_free (p1.servicehome);
+    }
+  if (p2.servicehome != NULL)
+    {   
+      GNUNET_DISK_directory_remove (p2.servicehome);
+      GNUNET_free (p2.servicehome);
+    }
   return ret;
 }