- moved timeout handling responsibility from for nat tests from caller to the library
[oweals/gnunet.git] / src / cadet / test_cadet.c
index ac2661a86e2019e3b824fe31c8b5caa6a10991cb..fb2a99f5bac899225241099150acdd7c90764cd1 100644 (file)
@@ -122,7 +122,12 @@ static int data_received;
 static int data_ack;
 
 /**
- * Total number of currently running peers.
+ * Total number of peers asked to run.
+ */
+static unsigned long long peers_requested;
+
+/**
+ * Number of currently running peers (should be same as @c peers_requested).
  */
 static unsigned long long peers_running;
 
@@ -325,8 +330,9 @@ data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     channel = ch;
   }
   th = GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
-                                          GNUNET_TIME_UNIT_FOREVER_REL,
-                                          size_payload, &tmt_rdy, (void *) 1L);
+                                           GNUNET_TIME_UNIT_FOREVER_REL,
+                                           size_payload + data_sent,
+                                           &tmt_rdy, (void *) 1L);
   if (NULL == th)
   {
     unsigned long i = (unsigned long) cls;
@@ -354,7 +360,7 @@ data_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 /**
  * Transmit ready callback
  *
- * @param cls Closure (message type).
+ * @param cls Closure (unused).
  * @param size Size of the buffer we have.
  * @param buf Buffer to copy data to.
  */
@@ -362,11 +368,13 @@ size_t
 tmt_rdy (void *cls, size_t size, void *buf)
 {
   struct GNUNET_MessageHeader *msg = buf;
+  size_t msg_size;
   uint32_t *data;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "tmt_rdy called, filling buffer\n");
-  if (size < size_payload || NULL == buf)
+  msg_size = size_payload + data_sent;
+  if (size < msg_size || NULL == buf)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "size %u, buf %p, data_sent %u, data_received %u\n",
@@ -382,23 +390,22 @@ tmt_rdy (void *cls, size_t size, void *buf)
   *data = htonl (data_sent);
   if (GNUNET_NO == initialized)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                "sending initializer\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "sending initializer\n");
   }
   else if (SPEED == test)
   {
     data_sent++;
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              " Sent packet %d\n", data_sent);
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sent message %d size %u\n",
+                data_sent, msg_size);
     if (data_sent < TOTAL_PACKETS)
     {
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              " Scheduling packet %d\n", data_sent + 1);
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Scheduling message %d\n",
+                  data_sent + 1);
       GNUNET_SCHEDULER_add_now (&data_task, NULL);
     }
   }
 
-  return size_payload;
+  return msg_size;
 }
 
 
@@ -441,14 +448,16 @@ data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
   case 0L:
     GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Root client got a message!\n");
     break;
+  case 1L:
   case 4L:
+    GNUNET_assert (client == peers_requested - 1);
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                 "Leaf client %li got a message.\n",
                 client);
     break;
   default:
-    GNUNET_assert (0);
-    break;
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Client %li not valid.\n", client);
+    GNUNET_abort ();
   }
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: (%d/%d)\n", ok, ok_goal);
   data = (uint32_t *) &message[1];
@@ -459,7 +468,7 @@ data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
   }
   else
   {
-    expected_target_client = 4L;
+    expected_target_client = peers_requested - 1;
   }
 
   if (GNUNET_NO == initialized)
@@ -468,7 +477,7 @@ data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
     start_time = GNUNET_TIME_absolute_get ();
     if (SPEED == test)
     {
-      GNUNET_assert (4L == client);
+      GNUNET_assert (peers_requested - 1 == client);
       GNUNET_SCHEDULER_add_now (&data_task, NULL);
       return GNUNET_OK;
     }
@@ -481,8 +490,9 @@ data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
     if (SPEED != test || (ok_goal - 2) == ok)
     {
       GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
-                                         GNUNET_TIME_UNIT_FOREVER_REL,
-                                         size_payload, &tmt_rdy, (void *) 1L);
+                                          GNUNET_TIME_UNIT_FOREVER_REL,
+                                          size_payload + data_sent,
+                                          &tmt_rdy, (void *) 1L);
       return GNUNET_OK;
     }
     else
@@ -498,8 +508,9 @@ data_callback (void *cls, struct GNUNET_CADET_Channel *channel,
       data_ack++;
       GNUNET_log (GNUNET_ERROR_TYPE_INFO, " received ack %u\n", data_ack);
       GNUNET_CADET_notify_transmit_ready (channel, GNUNET_NO,
-                                         GNUNET_TIME_UNIT_FOREVER_REL,
-                                         size_payload, &tmt_rdy, (void *) 1L);
+                                          GNUNET_TIME_UNIT_FOREVER_REL,
+                                          size_payload + data_sent,
+                                          &tmt_rdy, (void *) 1L);
       if (data_ack < TOTAL_PACKETS && SPEED != test)
         return GNUNET_OK;
       if (ok == 2 && SPEED == test)
@@ -583,7 +594,7 @@ stats_iterator (void *cls, const struct GNUNET_TESTBED_Peer *peer,
   if (0 == strncmp (s_sent, name, strlen (s_sent)) && 0 == i)
     ka_sent = value;
 
-  if (0 == strncmp(s_recv, name, strlen (s_recv)) && 4 == i)
+  if (0 == strncmp(s_recv, name, strlen (s_recv)) && peers_requested - 1 == i)
     ka_received = value;
 
   return GNUNET_OK;
@@ -605,7 +616,7 @@ check_keepalives (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   disconnect_task = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "check keepalives\n");
   GNUNET_CADET_channel_destroy (ch);
-  stats_op = GNUNET_TESTBED_get_statistics (5, testbed_peers,
+  stats_op = GNUNET_TESTBED_get_statistics (peers_running, testbed_peers,
                                             "cadet", NULL,
                                             stats_iterator, stats_cont, NULL);
 }
@@ -642,7 +653,7 @@ incoming_channel (void *cls, struct GNUNET_CADET_Channel *channel,
               GNUNET_i2s (initiator), (long) cls);
   ok++;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
-  if ((long) cls == 4L)
+  if ((long) cls == peers_requested - 1)
     incoming_ch = channel;
   else
   {
@@ -686,7 +697,7 @@ channel_cleaner (void *cls, const struct GNUNET_CADET_Channel *channel,
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Incoming channel disconnected at peer %ld\n", i);
-  if (4L == i)
+  if (peers_running - 1 == i)
   {
     ok++;
     GNUNET_break (channel == incoming_ch);
@@ -814,15 +825,16 @@ tmain (void *cls,
        struct GNUNET_CADET_TEST_Context *ctx,
        unsigned int num_peers,
        struct GNUNET_TESTBED_Peer **peers,
-       struct GNUNET_CADET_Handle **cadetes)
+       struct GNUNET_CADET_Handle **cadets)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test main\n");
   ok = 0;
   test_ctx = ctx;
   peers_running = num_peers;
+  GNUNET_assert (peers_running == peers_requested);
   testbed_peers = peers;
-  h1 = cadetes[0];
-  h2 = cadetes[num_peers - 1];
+  h1 = cadets[0];
+  h2 = cadets[num_peers - 1];
   disconnect_task = GNUNET_SCHEDULER_add_delayed (SHORT_TIME,
                                                   &disconnect_cadet_peers,
                                                   (void *) __LINE__);
@@ -852,21 +864,40 @@ main (int argc, char *argv[])
   config_file = "test_cadet.conf";
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Start\n");
-  if (strstr (argv[0], "_cadet_forward") != NULL)
+
+  /* Find out requested size */
+  if (strstr (argv[0], "_2_") != NULL)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "DIRECT CONNECTIONs\n");
+    peers_requested = 2;
+  }
+  else if (strstr (argv[0], "_5_") != NULL)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "5 PEER LINE\n");
+    peers_requested = 5;
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "SIZE UNKNOWN, USING 2\n");
+    peers_requested = 2;
+  }
+
+  /* Find out requested test */
+  if (strstr (argv[0], "_forward") != NULL)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "FORWARD\n");
     test = FORWARD;
     test_name = "unicast";
     ok_goal = 4;
   }
-  else if (strstr (argv[0], "_cadet_signal") != NULL)
+  else if (strstr (argv[0], "_signal") != NULL)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "SIGNAL\n");
     test = P2P_SIGNAL;
     test_name = "signal";
     ok_goal = 4;
   }
-  else if (strstr (argv[0], "_cadet_speed_ack") != NULL)
+  else if (strstr (argv[0], "_speed_ack") != NULL)
   {
     /* Test is supposed to generate the following callbacks:
      * 1 incoming channel (@dest)
@@ -879,7 +910,7 @@ main (int argc, char *argv[])
     test = SPEED_ACK;
     test_name = "speed ack";
   }
-  else if (strstr (argv[0], "_cadet_speed") != NULL)
+  else if (strstr (argv[0], "_speed") != NULL)
   {
     /* Test is supposed to generate the following callbacks:
      * 1 incoming channel (@dest)
@@ -931,7 +962,7 @@ main (int argc, char *argv[])
   ports[1] = 0;
   GNUNET_CADET_TEST_run ("test_cadet_small",
                         config_file,
-                        5,
+                        peers_requested,
                         &tmain,
                         NULL, /* tmain cls */
                         &incoming_channel,