-ensure stats queues do not grow too big
[oweals/gnunet.git] / src / multicast / test_multicast.c
index 5280334522236ab5a381058f6f78ad095bfbdce7..db58fe525da0599bfb19477256f9f611c96c3ef9 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * This file is part of GNUnet
- * Copyright (C) 2013 Christian Grothoff (and other contributing authors)
+ * Copyright (C) 2013 GNUnet e.V.
  *
  * GNUnet is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published
@@ -84,17 +84,21 @@ struct GNUNET_MessageHeader *join_req, *join_resp;
 
 enum
 {
-  TEST_NONE               = 0,
-  TEST_ORIGIN_START       = 1,
-  TEST_MEMBER_JOIN_REFUSE = 2,
-  TEST_MEMBER_JOIN_ADMIT  = 3,
-  TEST_ORIGIN_TO_ALL      = 4,
-  TEST_ORIGIN_TO_ALL_RECV = 5,
-  TEST_MEMBER_TO_ORIGIN   = 6,
-  TEST_MEMBER_PART        = 7,
-  TEST_ORIGIN_STOP        = 8,
+  TEST_NONE                = 0,
+  TEST_ORIGIN_START        = 1,
+  TEST_MEMBER_JOIN_REFUSE  = 2,
+  TEST_MEMBER_JOIN_ADMIT   = 3,
+  TEST_ORIGIN_TO_ALL       = 4,
+  TEST_ORIGIN_TO_ALL_RECV  = 5,
+  TEST_MEMBER_TO_ORIGIN    = 6,
+  TEST_MEMBER_REPLAY_ERROR = 7,
+  TEST_MEMBER_REPLAY_OK    = 8,
+  TEST_MEMBER_PART         = 9,
+  TEST_ORIGIN_STOP        = 10,
 } test;
 
+uint64_t replay_fragment_id;
+uint64_t replay_flags;
 
 static void
 member_join (int t);
@@ -128,10 +132,9 @@ cleanup ()
  * Terminate the test case (failure).
  *
  * @param cls NULL
- * @param tc scheduler context
  */
 static void
-end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+end_badly (void *cls)
 {
   res = 1;
   cleanup ();
@@ -143,10 +146,9 @@ end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Terminate the test case (success).
  *
  * @param cls NULL
- * @param tc scheduler context
  */
 static void
-end_normally (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+end_normally (void *cls)
 {
   res = 0;
   cleanup ();
@@ -172,14 +174,14 @@ end ()
 }
 
 
-void
-tmit_resume (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+static void
+tmit_resume (void *cls)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Transmission resumed.\n");
   struct TransmitClosure *tmit = cls;
   if (NULL != tmit->orig_tmit)
     GNUNET_MULTICAST_origin_to_all_resume (tmit->orig_tmit);
-  else
+  else if (NULL != tmit->mem_tmit)
     GNUNET_MULTICAST_member_to_origin_resume (tmit->mem_tmit);
 }
 
@@ -199,9 +201,11 @@ tmit_notify (void *cls, size_t *data_size, void *data)
 
   uint16_t size = strlen (tmit->data[tmit->n]);
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Transmit notify data: %u bytes available, "
-              "processing fragment %u/%u (size %u).\n",
-              *data_size, tmit->n + 1, tmit->data_count, size);
+              "Transmit notify data: %u bytes available, processing fragment %u/%u (size %u).\n",
+              (unsigned int) *data_size,
+              tmit->n + 1,
+              tmit->data_count,
+              size);
   if (*data_size < size)
   {
     *data_size = 0;
@@ -230,149 +234,191 @@ tmit_notify (void *cls, size_t *data_size, void *data)
 
 
 static void
-origin_recv_replay_msg (void *cls,
-                        const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
-                        uint64_t message_id,
-                        uint64_t fragment_offset,
-                        uint64_t flags,
-                        struct GNUNET_MULTICAST_ReplayHandle *rh)
+member_recv_join_request (void *cls,
+                          const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
+                          const struct GNUNET_MessageHeader *join_msg,
+                          struct GNUNET_MULTICAST_JoinHandle *jh)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Test #%u: origin_recv_replay_msg()\n", test);
+              "Test #%u: member_recv_join_request()\n", test);
 }
 
 
 static void
-member_recv_replay_msg (void *cls,
-                        const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
-                        uint64_t message_id,
-                        uint64_t fragment_offset,
-                        uint64_t flags,
-                        struct GNUNET_MULTICAST_ReplayHandle *rh)
+origin_stopped (void *cls)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Test #%u: member_recv_replay_msg()\n", test);
+              "Test #%u: origin_stopped()\n", test);
+  end ();
 }
 
 
 static void
-origin_recv_replay_frag (void *cls,
-                         const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
-                         uint64_t fragment_id,
-                         uint64_t flags,
-                         struct GNUNET_MULTICAST_ReplayHandle *rh)
+schedule_origin_stop (void *cls)
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Test #%u: origin_recv_replay_frag()\n", test);
+  test = TEST_ORIGIN_STOP;
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Test #%u: origin_stop()\n", test);
+  GNUNET_MULTICAST_origin_stop (origin, origin_stopped, NULL);
+  origin = NULL;
 }
 
 
 static void
-member_recv_replay_frag (void *cls,
-                         const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
-                         uint64_t fragment_id,
-                         uint64_t flags,
-                         struct GNUNET_MULTICAST_ReplayHandle *rh)
+member_parted (void *cls)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Test #%u: member_recv_replay_frag()\n", test);
+              "Test #%u: member_parted()\n", test);
+  member = NULL;
+
+  switch (test)
+  {
+  case TEST_MEMBER_JOIN_REFUSE:
+    member_join (TEST_MEMBER_JOIN_ADMIT);
+    break;
+
+  case TEST_MEMBER_PART:
+    GNUNET_SCHEDULER_add_now (&schedule_origin_stop, NULL);
+    break;
+
+  default:
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Invalid test #%d in member_recv_join_decision()\n", test);
+    GNUNET_assert (0);
+  }
 }
 
 
 static void
-origin_recv_membership_test (void *cls,
-                             const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
-                             uint64_t message_id,
-                             uint64_t group_generation,
-                             struct GNUNET_MULTICAST_MembershipTestHandle *mth)
+schedule_member_part (void *cls)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Test #%u: origin_recv_membership_test()\n", test);
+              "Test #%u: schedule_member_part()\n", test);
+  GNUNET_MULTICAST_member_part (member, member_parted, NULL);
 }
 
 
 static void
-member_recv_membership_test (void *cls,
-                             const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
-                             uint64_t message_id,
-                             uint64_t group_generation,
-                             struct GNUNET_MULTICAST_MembershipTestHandle *mth)
+member_part ()
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Test #%u: member_recv_membership_test()\n", test);
+  test = TEST_MEMBER_PART;
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "Test #%u: member_part()\n", test);
+  GNUNET_SCHEDULER_add_now (&schedule_member_part, NULL);
 }
 
 
 static void
-member_recv_join_request (void *cls,
-                          const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
-                          const struct GNUNET_MessageHeader *join_msg,
-                          struct GNUNET_MULTICAST_JoinHandle *jh)
+member_replay_ok ()
 {
+  test = TEST_MEMBER_REPLAY_OK;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Test #%u: member_recv_join_request()\n", test);
+              "Test #%u: member_replay_ok()\n", test);
+  replay_fragment_id = 1;
+  replay_flags = 1 | 1<<11;
+  GNUNET_MULTICAST_member_replay_fragment (member, replay_fragment_id,
+                                           replay_flags);
 }
 
 
 static void
-origin_stopped (void *cls)
+member_replay_error ()
 {
+  test = TEST_MEMBER_REPLAY_ERROR;
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Test #%u: origin_stopped()\n", test);
-  end ();
+              "Test #%u: member_replay_error()\n", test);
+  replay_fragment_id = 1234;
+  replay_flags = 11 | 1<<11;
+  GNUNET_MULTICAST_member_replay_fragment (member, replay_fragment_id,
+                                           replay_flags);
 }
 
 
 static void
-schedule_origin_stop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+origin_recv_replay_msg (void *cls,
+                        const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
+                        uint64_t message_id,
+                        uint64_t fragment_offset,
+                        uint64_t flags,
+                        struct GNUNET_MULTICAST_ReplayHandle *rh)
 {
-  test = TEST_ORIGIN_STOP;
-  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-              "Test #%u: origin_stop()\n", test);
-  GNUNET_MULTICAST_origin_stop (origin, origin_stopped, NULL);
-  origin = NULL;
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Test #%u: origin_recv_replay_msg()\n", test);
+  GNUNET_assert (0);
 }
 
 
 static void
-member_parted (void *cls)
+member_recv_replay_msg (void *cls,
+                        const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
+                        uint64_t message_id,
+                        uint64_t fragment_offset,
+                        uint64_t flags,
+                        struct GNUNET_MULTICAST_ReplayHandle *rh)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Test #%u: member_parted()\n", test);
+              "Test #%u: member_recv_replay_msg()\n", test);
+  GNUNET_assert (0);
+}
 
+
+static void
+origin_recv_replay_frag (void *cls,
+                         const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
+                         uint64_t fragment_id,
+                         uint64_t flags,
+                         struct GNUNET_MULTICAST_ReplayHandle *rh)
+{
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Test #%u: origin_recv_replay_frag()"
+              " - fragment_id=%" PRIu64 " flags=%" PRIu64 "\n",
+              test, fragment_id, flags);
+  GNUNET_assert (replay_fragment_id == fragment_id && replay_flags == flags);
   switch (test)
   {
-  case TEST_MEMBER_JOIN_REFUSE:
-    member_join (TEST_MEMBER_JOIN_ADMIT);
+  case TEST_MEMBER_REPLAY_ERROR:
+    GNUNET_MULTICAST_replay_response (rh, NULL, GNUNET_SYSERR);
+    member_replay_ok ();
     break;
 
-  case TEST_MEMBER_PART:
-    GNUNET_SCHEDULER_add_now (schedule_origin_stop, NULL);
+  case TEST_MEMBER_REPLAY_OK:
+  {
+    struct GNUNET_MULTICAST_MessageHeader mmsg = {
+      .header = {
+        .type = htons (GNUNET_MESSAGE_TYPE_MULTICAST_MESSAGE),
+        .size = htons (sizeof (mmsg)),
+      },
+      .fragment_id = GNUNET_htonll (1),
+      .message_id = GNUNET_htonll (1),
+      .fragment_offset = 0,
+      .group_generation = GNUNET_htonll (1),
+      .flags = 0,
+    };
+    member_cls.n = 0;
+    member_cls.msgs_expected = 1;
+    GNUNET_MULTICAST_replay_response (rh, &mmsg.header, GNUNET_MULTICAST_REC_OK);
+    GNUNET_MULTICAST_replay_response_end (rh);
     break;
+  }
 
   default:
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Invalid test #%d in member_recv_join_decision()\n", test);
+                "Invalid test #%d in origin_recv_replay_frag()\n", test);
     GNUNET_assert (0);
   }
 }
 
 
 static void
-member_part ()
-{
-  test = TEST_MEMBER_PART;
-  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-              "Test #%u: member_part()\n", test);
-  GNUNET_MULTICAST_member_part (member, member_parted, NULL);
-  member = NULL;
-}
-
-
-static void
-schedule_member_part (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+member_recv_replay_frag (void *cls,
+                         const struct GNUNET_CRYPTO_EcdsaPublicKey *member_key,
+                         uint64_t fragment_id,
+                         uint64_t flags,
+                         struct GNUNET_MULTICAST_ReplayHandle *rh)
 {
-  GNUNET_MULTICAST_member_part (member, member_parted, NULL);
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Test #%u: member_recv_replay_frag()\n", test);
+  GNUNET_assert (0);
 }
 
 
@@ -385,14 +431,14 @@ origin_recv_request (void *cls,
               "Test #%u: origin_recv_request()\n", test);
   if (++ocls->n != ocls->msgs_expected)
     return;
-  
-  GNUNET_assert (0 == memcmp (&req->member_key,
+
+  GNUNET_assert (0 == memcmp (&req->member_pub_key,
                               &member_pub_key, sizeof (member_pub_key)));
 
 
   // FIXME: check message content
 
-  member_part ();
+  member_replay_error ();
 }
 
 
@@ -407,14 +453,15 @@ member_to_origin ()
   *tmit = (struct TransmitClosure) {};
   tmit->data[0] = "abc def";
   tmit->data[1] = "ghi jkl mno";
-  tmit->data_delay[1] = 1;
+  tmit->data_delay[1] = 2;
   tmit->data[2] = "pqr stuw xyz";
   tmit->data_count = 3;
 
   origin_cls.n = 0;
   origin_cls.msgs_expected = 1;
 
-  GNUNET_MULTICAST_member_to_origin (member, 1, tmit_notify, tmit);
+  tmit->mem_tmit = GNUNET_MULTICAST_member_to_origin (member, 1,
+                                                      tmit_notify, tmit);
 }
 
 
@@ -423,9 +470,12 @@ member_recv_message (void *cls,
                      const struct GNUNET_MULTICAST_MessageHeader *msg)
 {
   struct MemberClosure *mcls = cls;
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Test #%u: member_recv_message() %u/%u\n",
-              test, mcls->n + 1, mcls->msgs_expected);
+              test,
+              (unsigned int) (mcls->n + 1),
+              mcls->msgs_expected);
   if (++mcls->n != mcls->msgs_expected)
     return;
 
@@ -437,6 +487,11 @@ member_recv_message (void *cls,
     member_to_origin ();
     break;
 
+  case TEST_MEMBER_REPLAY_OK:
+    GNUNET_assert (replay_fragment_id == GNUNET_ntohll (msg->fragment_id));
+    member_part ();
+    break;
+
   default:
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Invalid test #%d in origin_recv_message()\n", test);
@@ -482,15 +537,20 @@ origin_to_all ()
   struct TransmitClosure *tmit = &tmit_cls;
   *tmit = (struct TransmitClosure) {};
   tmit->data[0] = "ABC DEF";
-  tmit->data[1] = "GHI JKL MNO";
-  tmit->data_delay[1] = 1;
-  tmit->data[2] = "PQR STUW XYZ";
-  tmit->data_count = 3;
+  tmit->data[1] =  GNUNET_malloc (GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD + 1);
+  uint16_t i;
+  for (i = 0; i < GNUNET_MULTICAST_FRAGMENT_MAX_PAYLOAD; i++)
+    tmit->data[1][i] = (0 == i % 10000) ? '0' + i / 10000 : '_';
+  tmit->data[2] = "GHI JKL MNO";
+  tmit->data_delay[2] = 2;
+  tmit->data[3] = "PQR STUW XYZ";
+  tmit->data_count = 4;
 
   origin_cls.n = member_cls.n = 0;
-  origin_cls.msgs_expected = member_cls.msgs_expected = 1;
+  origin_cls.msgs_expected = member_cls.msgs_expected = tmit->data_count;
 
-  GNUNET_MULTICAST_origin_to_all (origin, 1, 1, tmit_notify, tmit);
+  tmit->orig_tmit = GNUNET_MULTICAST_origin_to_all (origin, 1, 1,
+                                                    tmit_notify, tmit);
 }
 
 
@@ -514,9 +574,9 @@ member_recv_join_decision (void *cls,
   {
   case TEST_MEMBER_JOIN_REFUSE:
     GNUNET_assert (0 == relay_count);
-    GNUNET_SCHEDULER_add_now (schedule_member_part, NULL);
+    GNUNET_SCHEDULER_add_now (&schedule_member_part, NULL);
     break;
-    
+
   case TEST_MEMBER_JOIN_ADMIT:
     GNUNET_assert (1 == relay_count);
     GNUNET_assert (0 == memcmp (relays, &this_peer, sizeof (this_peer)));
@@ -551,13 +611,13 @@ origin_recv_join_request (void *cls,
   join_resp->size = htons (sizeof (join_resp) + data_size);
   join_resp->type = htons (456);
   memcpy (&join_resp[1], data, data_size);
-  
+
   switch (test)
   {
   case TEST_MEMBER_JOIN_REFUSE:
     GNUNET_MULTICAST_join_decision (jh, GNUNET_NO, 0, NULL, join_resp);
     break;
-    
+
   case TEST_MEMBER_JOIN_ADMIT:
     GNUNET_MULTICAST_join_decision (jh, GNUNET_YES, 1, &this_peer, join_resp);
     break;
@@ -595,7 +655,6 @@ member_join (int t)
                                          &this_peer, 1, &this_peer, join_req,
                                          member_recv_join_request,
                                          member_recv_join_decision,
-                                         member_recv_membership_test,
                                          member_recv_replay_frag,
                                          member_recv_replay_msg,
                                          member_recv_message,
@@ -615,7 +674,6 @@ origin_start ()
 
   origin = GNUNET_MULTICAST_origin_start (cfg, group_key, 0,
                                           origin_recv_join_request,
-                                          origin_recv_membership_test,
                                           origin_recv_replay_frag,
                                           origin_recv_replay_msg,
                                           origin_recv_request,
@@ -642,7 +700,9 @@ core_connected (void *cls, const struct GNUNET_PeerIdentity *my_identity)
  */
 static void
 #if DEBUG_TEST_MULTICAST
-run (void *cls, char *const *args, const char *cfgfile,
+run (void *cls,
+     char *const *args,
+     const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *c)
 #else
 run (void *cls,
@@ -651,10 +711,13 @@ run (void *cls,
 #endif
 {
   cfg = c;
-  end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
-
-  core = GNUNET_CORE_connect (cfg, NULL, &core_connected, NULL, NULL,
-                              NULL, GNUNET_NO, NULL, GNUNET_NO, NULL);
+  end_badly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
+                                                &end_badly, NULL);
+  core = GNUNET_CORE_connect (cfg, NULL,
+                             &core_connected, NULL, NULL,
+                              NULL, GNUNET_NO,
+                             NULL, GNUNET_NO,
+                             NULL);
 }