Avoid dvision by zero
[oweals/gnunet.git] / src / multicast / test_multicast_2peers.c
index 929637e8eaceaa23e516019ce6e51007a6ca2d55..325d814988c496ab6ee10e031bcf11f9f2640171 100644 (file)
@@ -2,25 +2,24 @@
  * This file is part of GNUnet
  * 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
- * by the Free Software Foundation; either version 3, or (at your
- * option) any later version.
+ * GNUnet is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU Affero General Public License as published
+ * by the Free Software Foundation, either version 3 of the License,
+ * or (at your option) any later version.
  *
  * GNUnet is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * Affero General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with GNUnet; see the file COPYING.  If not, write to the
- * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 /**
- * @file multicast/test_multicast_multipeers.c
- * @brief Tests for the Multicast API with multiple peers.
+ * @file multicast/test_multicast_2peers.c
+ * @brief Tests for the Multicast API with two peers doing the ping
+ *        pong test.
  * @author xrs
  */
 
@@ -116,16 +115,18 @@ member_join_request (void *cls,
 
 }
 
-int notify (void *cls,
-            size_t *data_size,
-            void *data)
+
+static int
+notify (void *cls,
+        size_t *data_size,
+        void *data)
 {
 
   char text[] = "ping";
   *data_size = strlen(text)+1;
   GNUNET_memcpy(data, text, *data_size);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Member sents message to origin: %s\n", text);
 
   return GNUNET_YES;
@@ -140,26 +141,31 @@ member_join_decision (void *cls,
                       const struct GNUNET_PeerIdentity *relays,
                       const struct GNUNET_MessageHeader *join_msg)
 {
-  struct GNUNET_MULTICAST_MemberTransmitHandle *req;
-  
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
-              "Member received a decision from origin: %s\n", (GNUNET_YES == is_admitted)?"accepted":"rejected");
-  
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Member received a decision from origin: %s\n",
+              (GNUNET_YES == is_admitted)
+              ? "accepted"
+              : "rejected");
+
   if (GNUNET_YES == is_admitted)
   {
+    struct GNUNET_MULTICAST_MemberTransmitHandle *req;
+
+    // FIXME: move to MQ-style API!
     req = GNUNET_MULTICAST_member_to_origin (member,
                                              0,
-                                             notify,
+                                             &notify,
                                              NULL);
-    
   }
 }
 
+
 static void
-member_message (void *cls, 
+member_message (void *cls,
                 const struct GNUNET_MULTICAST_MessageHeader *msg)
 {
-  if (0 != strncmp ("pong", (char *)&msg[1], 4)) {
+  if (0 != strncmp ("pong", (char *)&msg[1], 4))
+  {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "member did not receive pong\n");
     result = GNUNET_SYSERR;
     GNUNET_SCHEDULER_shutdown ();
@@ -173,6 +179,7 @@ member_message (void *cls,
   GNUNET_SCHEDULER_shutdown ();
 }
 
+
 static void
 origin_join_request (void *cls,
                  const struct GNUNET_CRYPTO_EcdsaPublicKey *member_pub_key,
@@ -183,19 +190,19 @@ origin_join_request (void *cls,
 
   uint8_t data_size = ntohs (join_msg->size);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "origin got a join request...\n");
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "origin receives: '%s'\n", (char *)&join_msg[1]);
 
-  char data[] = "Come in!";
+  const char data[] = "Come in!";
   data_size = strlen (data) + 1;
   join_resp = GNUNET_malloc (sizeof (join_resp) + data_size);
   join_resp->size = htons (sizeof (join_resp) + data_size);
   join_resp->type = htons (123);
   GNUNET_memcpy (&join_resp[1], data, data_size);
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, 
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "origin sends: '%s'\n", data);
 
   GNUNET_MULTICAST_join_decision (jh,
@@ -203,22 +210,26 @@ origin_join_request (void *cls,
                                   0,
                                   NULL,
                                   join_resp);
-
+  GNUNET_free (join_resp);
   result = GNUNET_OK;
 }
 
+
 int
-origin_notify (void *cls, 
-               size_t *data_size, 
+origin_notify (void *cls,
+               size_t *data_size,
                void *data)
 {
   char text[] = "pong";
+
   *data_size = strlen(text)+1;
-  memcpy(data, text, *data_size); 
+  GNUNET_memcpy (data,
+                 text,
+                 *data_size);
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin sends (to all): %s\n", text);
 
-  return GNUNET_YES; 
+  return GNUNET_YES;
 }
 
 
@@ -227,10 +238,9 @@ origin_request (void *cls,
                 const struct GNUNET_MULTICAST_RequestHeader *req)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin receives: %s\n", (char *)&req[1]);
-  
-  if (0 != strncmp ("ping", (char *)&req[1], 4)) {
+
+  if (0 != strncmp ("ping", (char *)&req[1], 4))
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "origin didn't reveice a correct request");
-  }
 
   GNUNET_MULTICAST_origin_to_all (origin,
                                   0,
@@ -239,9 +249,10 @@ origin_request (void *cls,
                                   NULL);
 }
 
+
 static void
 origin_message (void *cls,
-                const struct GNUNET_MULTICAST_MessageHeader *msg) 
+                const struct GNUNET_MULTICAST_MessageHeader *msg)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "origin message msg\n");
 }
@@ -255,12 +266,18 @@ service_connect1 (void *cls,
 {
   member = ca_result;
 
-  if (NULL != member)
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to multicast service of member\n");
-  else
+  if (NULL == member)
+  {
     result = GNUNET_SYSERR;
+    GNUNET_SCHEDULER_shutdown ();
+  }
+  else
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Connected to multicast service of member\n");
+  }
 }
 
+
 static void
 multicast_da1 (void *cls,
                void * op_result)
@@ -277,11 +294,12 @@ multicast_ca1 (void *cls,
                const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
   struct GNUNET_MessageHeader *join_msg;
+  void *ret;
 
   // Get members keys
   member_key = GNUNET_CRYPTO_ecdsa_key_create ();
   GNUNET_CRYPTO_ecdsa_key_get_public (member_key, &member_pub_key);
-  
+
   char data[] = "Hi, can I enter?";
   uint8_t data_size = strlen (data) + 1;
   join_msg = GNUNET_malloc (sizeof (join_msg) + data_size);
@@ -292,7 +310,7 @@ multicast_ca1 (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Members tries to join multicast group\n");
 
-  return GNUNET_MULTICAST_member_join (cfg,
+  ret = GNUNET_MULTICAST_member_join (cfg,
                                        &group_pub_key,
                                        member_key,
                                        peer_id[0],
@@ -305,6 +323,8 @@ multicast_ca1 (void *cls,
                                        NULL, /* no test for member_replay_msg */
                                        member_message,
                                        NULL);
+  GNUNET_free (join_msg);
+  return ret;
 }
 
 
@@ -316,7 +336,8 @@ peer_information_cb (void *cls,
 {
   int i = (int) (long) cls;
 
-  if (NULL == pinfo) {
+  if (NULL == pinfo)
+  {
     result = GNUNET_SYSERR;
     GNUNET_SCHEDULER_shutdown ();
   }
@@ -329,22 +350,24 @@ peer_information_cb (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Create member peer\n");
 
-  if (0 == i) {
-  /* connect to multicast service of member */
-  op1 = GNUNET_TESTBED_service_connect (NULL,                    /* Closure for operation */
-                                        peers[1],                /* The peer whose service to connect to */
-                                        "multicast",             /* The name of the service */
-                                        service_connect1,   /* callback to call after a handle to service
-                                                               is opened */
-                                        NULL,                    /* closure for the above callback */
-                                        multicast_ca1,      /* callback to call with peer's configuration;
-                                                               this should open the needed service connection */
-                                        multicast_da1,     /* callback to be called when closing the
-                                                              opened service connection */
-                                        NULL);                   /* closure for the above two callbacks */
+  if (0 == i)
+  {
+    /* connect to multicast service of member */
+    op1 = GNUNET_TESTBED_service_connect (NULL,                    /* Closure for operation */
+                                          peers[1],                /* The peer whose service to connect to */
+                                          "multicast",             /* The name of the service */
+                                          service_connect1,   /* callback to call after a handle to service
+                                                                 is opened */
+                                          NULL,                    /* closure for the above callback */
+                                          multicast_ca1,      /* callback to call with peer's configuration;
+                                                                 this should open the needed service connection */
+                                          multicast_da1,     /* callback to be called when closing the
+                                                                opened service connection */
+                                          NULL);                   /* closure for the above two callbacks */
   }
 }
 
+
 /**
  * Test logic of peer "0" being origin starts here.
  *
@@ -478,7 +501,7 @@ main (int argc, char *argv[])
 
   result = GNUNET_SYSERR;
   ret = GNUNET_TESTBED_test_run
-      ("test-multicast-multipeer",  /* test case name */
+      ("test-multicast-2peers",  /* test case name */
        "test_multicast.conf", /* template configuration */
        NUM_PEERS,       /* number of peers to start */
        0LL, /* Event mask - set to 0 for no event notifications */
@@ -491,4 +514,5 @@ main (int argc, char *argv[])
   return 0;
 }
 
-/* end of test_multicast_multipeer.c */
+
+/* end of test_multicast_2peers.c */