- fix error messages
[oweals/gnunet.git] / src / transport / test_transport_api_manipulation_cfg.c
index b9063ef5ebd94d682fefecaa11668b98a21e66b6..c9bb5d08a4988f74eee85c2c481b49022d0b9258 100644 (file)
 /**
  * @file transport/test_transport_api_manipulation_send_tcp.c
  * @brief base test case for transport traffic manipulation implementation
+ * based onf cfg
  *
- * This test case will setup 2 peers and connect them, the first message
- * will be sent without manipulation, then a send delay of 1 second will
- * be configured and 1 more message will be sent. Time will be measured.
+ * Peer 1 has inbound and outbound delay of 100ms
+ * Peer 2 has no inbound and outbound delay
  *
- * In addition the distance on receiver side will be manipulated to be 10
+ * We send a request from P1 to P2 and expect delay of >= TEST_DELAY us
+ * Then we send response from P2 to P1 and expect delay of >= TEST_DELAY us
  */
 #include "platform.h"
 #include "gnunet_transport_service.h"
 
 #define TEST_MESSAGE_SIZE 2600
 
-#define TEST_MESSAGE_TYPE 12345
+#define TEST_REQUEST_MESSAGE_TYPE 12345
+
+#define TEST_RESPONSE_MESSAGE_TYPE 12346
+
+/**
+ * Test delay, in microseconds.
+ */
+#define TEST_DELAY 100 * 1000LL
 
 static char *test_source;
 
@@ -77,13 +85,8 @@ static char *cfg_file_p1;
 
 static char *cfg_file_p2;
 
-static int messages_recv;
-
-static struct GNUNET_TIME_Absolute start_normal;
-static struct GNUNET_TIME_Relative dur_normal;
-
-static struct GNUNET_TIME_Absolute start_delayed;
-static struct GNUNET_TIME_Relative dur_delayed;
+static struct GNUNET_TIME_Absolute start_request;
+static struct GNUNET_TIME_Absolute start_response;
 
 static void
 end ()
@@ -157,80 +160,68 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
-static void
-sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
-
-static void
-notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
-                const struct GNUNET_MessageHeader *message)
+static size_t
+notify_request_ready (void *cls, size_t size, void *buf)
 {
   struct PeerContext *p = cls;
-  struct PeerContext *t = NULL;
+  struct GNUNET_MessageHeader *hdr;
 
-  if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
-    t = p1;
-  if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
-    t = p2;
-  GNUNET_assert (t != NULL);
+  th = NULL;
 
-  char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
+  if (buf == NULL)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Timeout occurred while waiting for transmit_ready\n");
+    if (GNUNET_SCHEDULER_NO_TASK != die_task)
+      GNUNET_SCHEDULER_cancel (die_task);
+    die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
+    ok = 42;
+    return 0;
+  }
+
+  GNUNET_assert (size >= TEST_MESSAGE_SIZE);
+  if (buf != NULL)
+  {
+    memset (buf, '\0', TEST_MESSAGE_SIZE);
+    hdr = buf;
+    hdr->size = htons (TEST_MESSAGE_SIZE);
+    hdr->type = htons (TEST_REQUEST_MESSAGE_TYPE);
+  }
 
+  char *ps = GNUNET_strdup (GNUNET_i2s (&p1->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));
+              "Sending request message from peer %u (`%4s') with type %u and size %u bytes to peer %u (`%4s')\n",
+              p1->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
+              GNUNET_i2s (&p->id));
   GNUNET_free (ps);
+  return TEST_MESSAGE_SIZE;
+}
 
-  if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
-      (TEST_MESSAGE_SIZE == ntohs (message->size)))
-  {
-    ok = 0;
 
-  }
-  else
-  {
-    GNUNET_break (0);
-    ok = 1;
-    end ();
+static void
+sendtask_request_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  send_task = GNUNET_SCHEDULER_NO_TASK;
+
+  if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
     return;
-  }
+  char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p2->id));
 
-  if (0 == messages_recv)
-  {
-       /* Received outbound only delayed message message */
-       dur_normal = GNUNET_TIME_absolute_get_duration(start_normal);
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "Received outbound message %u after %llu\n",
-                messages_recv,
-                (long long unsigned int) dur_normal.rel_value);
-    send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
-  }
-  if (1 == messages_recv)
-  {
-       /* Received outbound and inbound delayed message  */
-       dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Received delayed message %u after %llu\n",
-                  messages_recv,
-                  (long long unsigned int) dur_delayed.rel_value);
-      if (dur_delayed.rel_value < (dur_normal.rel_value * 1.80))
-      {
-                               GNUNET_break (0);
-                               ok += 1;
-        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                    "Outbound and inbound delayed message was not delayed correctly: took only %llu\n",
-                    (long long unsigned int) dur_delayed.rel_value);
-      }
-
-      /* shutdown */
-      end ();
-  }
-  messages_recv ++;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
+              p1->no, GNUNET_i2s (&p1->id), p2->no, receiver_s);
+  GNUNET_free (receiver_s);
+
+  s_sending = GNUNET_YES;
+       start_request = GNUNET_TIME_absolute_get();
+  th = GNUNET_TRANSPORT_notify_transmit_ready (p1->th, &p2->id, TEST_MESSAGE_SIZE, 0,
+                                               TIMEOUT_TRANSMIT, &notify_request_ready,
+                                               p2);
 }
 
 
 static size_t
-notify_ready (void *cls, size_t size, void *buf)
+notify_response_ready (void *cls, size_t size, void *buf)
 {
   struct PeerContext *p = cls;
   struct GNUNET_MessageHeader *hdr;
@@ -239,7 +230,7 @@ notify_ready (void *cls, size_t size, void *buf)
 
   if (buf == NULL)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Timeout occurred while waiting for transmit_ready\n");
     if (GNUNET_SCHEDULER_NO_TASK != die_task)
       GNUNET_SCHEDULER_cancel (die_task);
@@ -254,31 +245,21 @@ notify_ready (void *cls, size_t size, void *buf)
     memset (buf, '\0', TEST_MESSAGE_SIZE);
     hdr = buf;
     hdr->size = htons (TEST_MESSAGE_SIZE);
-    hdr->type = htons (TEST_MESSAGE_TYPE);
-  }
-
-  if (0 == messages_recv)
-  {
-       start_normal = GNUNET_TIME_absolute_get();
-  }
-  if (1 == messages_recv)
-  {
-               start_delayed = GNUNET_TIME_absolute_get();
+    hdr->type = htons (TEST_RESPONSE_MESSAGE_TYPE);
   }
 
-  char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
+  char *ps = GNUNET_strdup (GNUNET_i2s (&p1->id));
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
-              p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
+              "Sending response message from peer %u (`%4s') with type %u and size %u bytes to peer %u (`%4s')\n",
+              p1->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
               GNUNET_i2s (&p->id));
   GNUNET_free (ps);
 
   return TEST_MESSAGE_SIZE;
 }
 
-
 static void
-sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+sendtask_response_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   send_task = GNUNET_SCHEDULER_NO_TASK;
 
@@ -291,15 +272,86 @@ sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
               p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
   GNUNET_free (receiver_s);
 
-
-
   s_sending = GNUNET_YES;
+       start_response = GNUNET_TIME_absolute_get();
   th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
-                                               TIMEOUT_TRANSMIT, &notify_ready,
+                                               TIMEOUT_TRANSMIT, &notify_response_ready,
                                                p1);
 }
 
 
+
+static void
+notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
+                const struct GNUNET_MessageHeader *message)
+{
+  struct PeerContext *p = cls;
+  struct PeerContext *t = NULL;
+  struct GNUNET_TIME_Relative duration;
+
+  if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
+    t = p1;
+  if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
+    t = p2;
+  GNUNET_assert (t != NULL);
+
+  char *ps = GNUNET_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);
+
+  switch (ntohs (message->type)) {
+               case TEST_REQUEST_MESSAGE_TYPE:
+                       duration = GNUNET_TIME_absolute_get_difference(start_request,
+                                       GNUNET_TIME_absolute_get());
+                       if (duration.rel_value_us >= TEST_DELAY)
+                               GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                                           "Request message was delayed for %s\n",
+                                           GNUNET_STRINGS_relative_time_to_string (duration,
+                                                                                   GNUNET_YES));
+                       else
+                         {
+                           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                                       "Request message was delayed for unexpected duration %s\n",
+                                       GNUNET_STRINGS_relative_time_to_string (duration,
+                                                                               GNUNET_YES));
+                           ok = 1;
+                       }
+
+                 /* Send response */
+                 send_task = GNUNET_SCHEDULER_add_now (&sendtask_response_task, NULL);
+                 return;
+                       break;
+               case TEST_RESPONSE_MESSAGE_TYPE:
+                       duration = GNUNET_TIME_absolute_get_difference(start_response,
+                                       GNUNET_TIME_absolute_get());
+                       if (duration.rel_value_us >= TEST_DELAY)
+                         GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                                     "Response message was delayed for %s\n",
+                                     GNUNET_STRINGS_relative_time_to_string (duration,
+                                                                             GNUNET_YES));
+                       else
+                         {
+                           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                                       "Response message was delayed for unexpected duration %s\n",
+                                       GNUNET_STRINGS_relative_time_to_string (duration,
+                                                                               GNUNET_YES));
+                           ok = 1;
+                       }
+                 /* Done */
+                       ok = 0;
+                 end();
+                       break;
+               default:
+                       break;
+       }
+}
+
+
+
 static void
 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
 {
@@ -353,7 +405,7 @@ testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
   GNUNET_free (p1_c);
 
   s_connected = GNUNET_YES;
-  send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
+  send_task = GNUNET_SCHEDULER_add_now (&sendtask_request_task, NULL);
 }
 
 
@@ -376,15 +428,7 @@ start_cb (struct PeerContext *p, void *cls)
               "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);
-  /*
-  struct GNUNET_ATS_Information ats[2];
-       ats[0].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
-       ats[0].value = htonl (1000);
-       ats[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
-       ats[1].value = htonl (10);
-
-  GNUNET_TRANSPORT_set_traffic_metric (p1->th, &p2->id, TM_RECEIVE, ats, 2);
-*/
+
   cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
                                                NULL);