fixing tng testcase unix communicator
authorSchanzenbach, Martin <mschanzenbach@posteo.de>
Sun, 22 Dec 2019 03:26:04 +0000 (12:26 +0900)
committerSchanzenbach, Martin <mschanzenbach@posteo.de>
Sun, 22 Dec 2019 03:26:04 +0000 (12:26 +0900)
src/transport/Makefile.am
src/transport/gnunet-communicator-unix.c
src/transport/test_communicator_unix.c
src/transport/transport-testing2.c
src/transport/transport-testing2.h

index ae77899a09eab7f29c1eab80f649649827fc52c5..a5b9aaaa8da727d2e22bf56d967abc9a4d1bd8fe 100644 (file)
@@ -786,6 +786,7 @@ test_communicator_unix_SOURCES = \
  test_communicator_unix.c
 test_communicator_unix_LDADD = \
  libgnunettransporttesting2.la \
+ $(top_builddir)/src/testing/libgnunettesting.la \
  $(top_builddir)/src/util/libgnunetutil.la
 
 test_plugin_unix_SOURCES = \
index 58a144ecbd81f7ab2e10cbf642a21c6771d7fa10..404bcfb7f25a0a349a031acd79cda478977b0c73 100644 (file)
@@ -114,7 +114,7 @@ struct Queue
    * Message currently scheduled for transmission, non-NULL if and only
    * if this queue is in the #queue_head DLL.
    */
-  const struct GNUNET_MessageHeader *msg;
+  struct UNIXMessage *msg;
 
   /**
    * Message queue we are providing for the #ch.
@@ -142,6 +142,10 @@ struct Queue
   struct GNUNET_SCHEDULER_Task *timeout_task;
 };
 
+/**
+ * My Peer Identity
+ */
+static struct GNUNET_PeerIdentity my_identity;
 
 /**
  * ID of read task
@@ -357,7 +361,7 @@ lookup_queue_it (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
   struct LookupCtx *lctx = cls;
   struct Queue *queue = value;
 
-  if ((queue->address_len = lctx->un_len) &&
+  if ((queue->address_len == lctx->un_len) &&
       (0 == memcmp (lctx->un, queue->address, queue->address_len)))
   {
     lctx->res = queue;
@@ -383,6 +387,7 @@ lookup_queue (const struct GNUNET_PeerIdentity *peer,
 
   lctx.un = un;
   lctx.un_len = un_len;
+  lctx.res = NULL;
   GNUNET_CONTAINER_multipeermap_get_multiple (queue_map,
                                               peer,
                                               &lookup_queue_it,
@@ -401,7 +406,7 @@ static void
 select_write_cb (void *cls)
 {
   struct Queue *queue = queue_tail;
-  const struct GNUNET_MessageHeader *msg = queue->msg;
+  const struct GNUNET_MessageHeader *msg = &queue->msg->header;
   size_t msg_size = ntohs (msg->size);
   ssize_t sent;
 
@@ -514,10 +519,15 @@ mq_send (struct GNUNET_MQ_Handle *mq,
          void *impl_state)
 {
   struct Queue *queue = impl_state;
+  size_t msize = ntohs (msg->size);
 
   GNUNET_assert (mq == queue->mq);
   GNUNET_assert (NULL == queue->msg);
-  queue->msg = msg;
+  //Convert to UNIXMessage
+  queue->msg = GNUNET_malloc (msize + sizeof (struct UNIXMessage));
+  queue->msg->header.size = htons(msize + sizeof (struct UNIXMessage));
+  queue->msg->sender = my_identity;
+  memcpy (&queue->msg[1], msg, msize);
   GNUNET_CONTAINER_DLL_insert (queue_head, queue_tail, queue);
   GNUNET_assert (NULL != unix_sock);
   if (NULL == write_task)
@@ -747,6 +757,9 @@ select_read_cb (void *cls)
   msize = ntohs (msg->header.size);
   if ((msize < sizeof(struct UNIXMessage)) || (msize > ret))
   {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Wrong message size: %d bytes\n",
+                msize);
     GNUNET_break_op (0);
     return;
   }
@@ -974,9 +987,22 @@ run (void *cls,
   struct sockaddr_un *un;
   socklen_t un_len;
   char *my_addr;
+  struct GNUNET_CRYPTO_EddsaPrivateKey *my_private_key;
 
   (void) cls;
 
+  my_private_key = GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg);
+  if (NULL == my_private_key)
+  {
+    GNUNET_log (
+      GNUNET_ERROR_TYPE_ERROR,
+      _ (
+        "UNIX communicator is lacking key configuration settings. Exiting.\n"));
+    GNUNET_SCHEDULER_shutdown ();
+    return;
+  }
+  GNUNET_CRYPTO_eddsa_key_get_public (my_private_key, &my_identity.public_key);
+
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_filename (cfg,
                                                COMMUNICATOR_CONFIG_SECTION,
index db355c93e2fe7458063f3a6802a582b14977329f..83ab1ea269c792b67a8825616b83f22e7190455b 100644 (file)
@@ -1,33 +1,34 @@
 /*
-     This file is part of GNUnet.
-     Copyright (C) 2019 GNUnet e.V.
+    This file is part of GNUnet.
+    Copyright (C) 2019 GNUnet e.V.
 
-     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 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
-     Affero General Public License for more details.
+    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
+    Affero General Public License for more details.
 
-     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/>.
+    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/>.
 
-     SPDX-License-Identifier: AGPL3.0-or-later
- */
+    SPDX-License-Identifier: AGPL3.0-or-later
+*/
 
 /**
- * @file transport/test_communicator_unix.c
- * @brief test the unix communicator
- * @author Julius Bünger
- */
+* @file transport/test_communicator_unix.c
+* @brief test the unix communicator
+* @author Julius Bünger
+*/
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "transport-testing2.h"
 #include "gnunet_ats_transport_service.h"
 #include "gnunet_signatures.h"
+#include "gnunet_testing_lib.h"
 #include "transport.h"
 
 #include <inttypes.h>
 
 static struct GNUNET_PeerIdentity peer_id[NUM_PEERS];
 
+static char *communicator_binary;
+
 static struct
 GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS];
 
+static struct GNUNET_CONFIGURATION_Handle *cfg_peers[NUM_PEERS];
+
+static char **cfg_peers_name;
+
+static int ret;
+
 // static char *addresses[NUM_PEERS];
 
 
@@ -53,7 +62,6 @@ GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_hs[NUM_PEERS];
 // static char payload[] = "TEST PAYLOAD";
 static uint32_t payload = 42;
 
-
 static void
 communicator_available_cb (void *cls,
                            struct
@@ -62,7 +70,7 @@ communicator_available_cb (void *cls,
                            enum GNUNET_TRANSPORT_CommunicatorCharacteristics cc,
                            char *address_prefix)
 {
-  LOG (GNUNET_ERROR_TYPE_DEBUG,
+  LOG (GNUNET_ERROR_TYPE_INFO,
        "Communicator available. (cc: %u, prefix: %s)\n",
        cc,
        address_prefix);
@@ -85,9 +93,12 @@ add_address_cb (void *cls,
        aid,
        nt);
   // addresses[1] = GNUNET_strdup (address);
-  GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0],
-                                                              &peer_id[1],
-                                                              address);
+  if (0 == strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1]))
+    GNUNET_TRANSPORT_TESTING_transport_communicator_open_queue (tc_hs[0],
+                                                                &peer_id[
+                                                                  NUM_PEERS
+                                                                  - 1],
+                                                                address);
 }
 
 
@@ -156,11 +167,20 @@ void
 incoming_message_cb (void *cls,
                      struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
                      *tc_h,
-                     const struct GNUNET_MessageHeader *msg)
+                     const struct GNUNET_TRANSPORT_IncomingMessage *msg)
 {
+  char *payload_ptr;
+  if (0 != strcmp ((char*) cls, cfg_peers_name[NUM_PEERS - 1]))
+    return; // TODO?
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "%s received data (%lu bytes payload)\n",
+              (char*) cls,
+              ntohs (msg->header.size) - sizeof (struct GNUNET_TRANSPORT_IncomingMessage));
+  payload_ptr = (char*)&msg[1] + sizeof (struct GNUNET_MessageHeader);
+  ret = memcmp (payload_ptr, &payload, sizeof (payload));
+  GNUNET_SCHEDULER_shutdown ();
 }
 
-
 /**
  * @brief Main function called by the scheduler
  *
@@ -169,28 +189,19 @@ incoming_message_cb (void *cls,
 static void
 run (void *cls)
 {
-  struct GNUNET_CONFIGURATION_Handle *cfg = cls;
-
-  tc_hs[0] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
-    "transport",
-    "gnunet-communicator-unix",
-    "test_communicator_1.conf",
-    &communicator_available_cb,
-    NULL,
-    &queue_create_reply_cb,
-    &add_queue_cb,
-    NULL,
-    NULL);   /* cls */
-  tc_hs[1] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
-    "transport",
-    "gnunet-communicator-unix",
-    "test_communicator_2.conf",
-    &communicator_available_cb,
-    &add_address_cb,
-    NULL,
-    &add_queue_cb,
-    NULL,
-    NULL);   /* cls */
+  for (int i = 0; i < NUM_PEERS; i++)
+  {
+    tc_hs[i] = GNUNET_TRANSPORT_TESTING_transport_communicator_service_start (
+      "transport",
+      communicator_binary,
+      cfg_peers_name[i],
+      &communicator_available_cb,
+      &add_address_cb,
+      &queue_create_reply_cb,
+      &add_queue_cb,
+      &incoming_message_cb,
+      cfg_peers_name[i]); /* cls */
+  }
 }
 
 
@@ -198,81 +209,72 @@ int
 main (int argc,
       char *const *argv)
 {
-  char *cfg_filename;
-  char *opt_cfg_filename;
-  const char *xdg;
-  char *loglev;
-  char *logfile;
-  struct GNUNET_CONFIGURATION_Handle *cfg;
-
-  struct GNUNET_GETOPT_CommandLineOption service_options[] = {
-    GNUNET_GETOPT_option_cfgfile (&opt_cfg_filename),
-    GNUNET_GETOPT_option_help (NULL),
-    GNUNET_GETOPT_option_loglevel (&loglev),
-    GNUNET_GETOPT_option_logfile (&logfile),
-    GNUNET_GETOPT_OPTION_END
-  };
-
-  if (GNUNET_OK != GNUNET_log_setup ("test_communicator_unix",
-                                     loglev,
-                                     logfile))
+  struct GNUNET_CRYPTO_EddsaPrivateKey *private_key;
+  char *communicator_name;
+  char *cfg_peer;
+  ret = 1;
+
+  communicator_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
+  GNUNET_asprintf (&communicator_binary, "gnunet-communicator-%s",
+                   communicator_name);
+  cfg_peers_name = GNUNET_malloc (sizeof(char*) * NUM_PEERS);
+  if (GNUNET_OK != GNUNET_log_setup ("test_communicator",
+                                     "DEBUG",
+                                     "test_communicator.log"))
   {
+    fprintf (stderr, "Unable to setup log\n");
     GNUNET_break (0);
-    return GNUNET_SYSERR;
-  }
-
-  xdg = getenv ("XDG_CONFIG_HOME");
-  if (NULL != xdg)
-    GNUNET_asprintf (&cfg_filename,
-                     "%s%s%s",
-                     xdg,
-                     DIR_SEPARATOR_STR,
-                     GNUNET_OS_project_data_get ()->config_file);
-  else
-    cfg_filename = GNUNET_strdup (
-      GNUNET_OS_project_data_get ()->user_config_file);
-  cfg = GNUNET_CONFIGURATION_create ();
-  if (NULL != opt_cfg_filename)
-  {
-    if ((GNUNET_YES !=
-         GNUNET_DISK_file_test (opt_cfg_filename)) ||
-        (GNUNET_SYSERR ==
-         GNUNET_CONFIGURATION_load (cfg,
-                                    opt_cfg_filename)))
-    {
-      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  _ ("Malformed configuration file `%s', exit ...\n"),
-                  opt_cfg_filename);
-      return GNUNET_SYSERR;
-    }
+    return 2;
   }
-  else
+  for (int i = 0; i < NUM_PEERS; i++)
   {
+    GNUNET_asprintf ((&cfg_peer),
+                     "test_communicator_%s_peer%u.conf",
+                     communicator_name, i + 1);
+    cfg_peers_name[i] = cfg_peer;
+    cfg_peers[i] = GNUNET_CONFIGURATION_create ();
     if (GNUNET_YES ==
-        GNUNET_DISK_file_test (cfg_filename))
+        GNUNET_DISK_file_test (cfg_peers_name[i]))
     {
       if (GNUNET_SYSERR ==
-          GNUNET_CONFIGURATION_load (cfg,
-                                     cfg_filename))
+          GNUNET_CONFIGURATION_load (cfg_peers[i],
+                                     cfg_peers_name[i]))
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                    _ ("Malformed configuration file `%s', exit ...\n"),
-                    cfg_filename);
-        return GNUNET_SYSERR;
+        fprintf (stderr,
+                 "Malformed configuration file `%s', exiting ...\n",
+                 cfg_peers_name[i]);
+        return 1;
       }
     }
     else
     {
       if (GNUNET_SYSERR ==
-          GNUNET_CONFIGURATION_load (cfg,
+          GNUNET_CONFIGURATION_load (cfg_peers[i],
                                      NULL))
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                    _ ("Malformed configuration, exit ...\n"));
-        return GNUNET_SYSERR;
+        fprintf (stderr,
+                 "Configuration file %s does not exist, exiting ...\n",
+                 cfg_peers_name[i]);
+        return 1;
       }
     }
+    private_key =
+      GNUNET_CRYPTO_eddsa_key_create_from_configuration (cfg_peers[i]);
+    if (NULL == private_key)
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR,
+           "Unable to get peer ID\n");
+      return 1;
+    }
+    GNUNET_CRYPTO_eddsa_key_get_public (private_key,
+                                        &peer_id[i].public_key);
+    GNUNET_free (private_key);
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "Identity of peer %u is %s\n",
+                i, GNUNET_i2s_full (&peer_id[i]));
   }
+  fprintf (stderr, "Starting test...\n");
   GNUNET_SCHEDULER_run (&run,
-                        cfg);
+                        NULL);
+  return ret;
 }
index 7204d26dadfa7eea5c118b99aff8f3c487476ead..b354f7c2ac8269c0498c7d05853f4966a2d8f0a4 100644 (file)
@@ -306,14 +306,14 @@ handle_add_address (void *cls,
 {
   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle *tc_h = cls;
   uint16_t size;
-
   size = ntohs (msg->header.size) - sizeof(*msg);
   if (0 == size)
     return; /* receive-only communicator */
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "received add address cb %u\n", size);
   tc_h->c_address = GNUNET_strdup ((const char *) &msg[1]);
   if (NULL != tc_h->add_address_cb)
   {
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "calling communicator_available()\n");
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "calling add_address_cb()\n");
     tc_h->add_address_cb (tc_h->cb_cls,
                           tc_h,
                           tc_h->c_address,
@@ -366,7 +366,7 @@ handle_incoming_msg (void *cls,
   {
     tc_h->incoming_msg_cb (tc_h->cb_cls,
                            tc_h,
-                           (const struct GNUNET_MessageHeader *) msg);
+                           msg);
   }
   else
   {
@@ -452,9 +452,19 @@ handle_add_queue_message (void *cls,
   struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue *tc_queue;
 
   tc_queue = tc_h->queue_head;
-  while (tc_queue->qid != msg->qid)
+  if (NULL != tc_queue)
   {
-    tc_queue = tc_queue->next;
+    while (tc_queue->qid != msg->qid)
+    {
+      tc_queue = tc_queue->next;
+    }
+  } else {
+    tc_queue =
+      GNUNET_new (struct GNUNET_TRANSPORT_TESTING_TransportCommunicatorQueue);
+    tc_queue->tc_h = tc_h;
+    tc_queue->qid = msg->qid;
+    tc_queue->peer_id = msg->receiver;
+    GNUNET_CONTAINER_DLL_insert (tc_h->queue_head, tc_h->queue_tail, tc_queue);
   }
   GNUNET_assert (tc_queue->qid == msg->qid);
   GNUNET_assert (0 == GNUNET_memcmp (&tc_queue->peer_id, &msg->receiver));
@@ -663,7 +673,7 @@ communicator_start (
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to start communicator!");
     return;
   }
-  LOG (GNUNET_ERROR_TYPE_DEBUG, "started communicator\n");
+  LOG (GNUNET_ERROR_TYPE_INFO, "started communicator\n");
   GNUNET_free (binary);
   /* TODO */ GNUNET_SCHEDULER_add_shutdown (&shutdown_communicator,
                                             tc_h->c_proc);
index b136be1251daf4ea718e201063e89a7afb8d0517..49e4791aad6c3a897e1757f9b73063e131ec55eb 100644 (file)
@@ -135,7 +135,7 @@ typedef void
                                                     GNUNET_TRANSPORT_TESTING_TransportCommunicatorHandle
                                                     *tc_h,
                                                     const struct
-                                                    GNUNET_MessageHeader *msg);
+                                                    GNUNET_TRANSPORT_IncomingMessage *msg);
 
 
 /**