- Remove deprecated code, coverity 10236
[oweals/gnunet.git] / src / mesh / test_mesh_regex.c
index 9c545b4992f7f73109dc71af69f01662a8dd2e31..f88822bc8d541a9d777005d8e63424b6f7c9e1a4 100644 (file)
 
 #define VERBOSE GNUNET_YES
 #define REMOVE_DIR GNUNET_YES
+#define MESH_REGEX_PEERS 3
 
 /**
  * How long until we give up on connecting the peers?
  */
-#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
 
 /**
  * Time to wait for stuff that should be rather fast
 
 
 /**
- * How many events have happened
+ * Which strings have been found & connected.
  */
-static int ok;
+static int ok[MESH_REGEX_PEERS];
+
+/**
+ * How many connects have happened.
+ */
+static int regex_peers;
 
 /**
  * Be verbose
@@ -91,18 +97,46 @@ static GNUNET_SCHEDULER_TaskIdentifier disconnect_task;
  */
 static GNUNET_SCHEDULER_TaskIdentifier shutdown_handle;
 
+/**
+ * Mesh handle for connecting peer.
+ */
+static struct GNUNET_MESH_Handle *h1;
+
+/**
+ * Mesh handle for announcing peers.
+ */
+static struct GNUNET_MESH_Handle *h2[MESH_REGEX_PEERS];
 
-static struct GNUNET_TESTING_Daemon *d1;
+/**
+ * Tunnel handles for announcing peer.
+ */
+static struct GNUNET_MESH_Tunnel *t[MESH_REGEX_PEERS];
 
-static struct GNUNET_TESTING_Daemon *d2;
+/**
+ * Incoming tunnels for announcing peers.
+ */
+static struct GNUNET_MESH_Tunnel *incoming_t[MESH_REGEX_PEERS];
 
-static struct GNUNET_MESH_Handle *h1;
+/**
+ * Regular expressions for the announces.
+ */
+static char *regexes[MESH_REGEX_PEERS] = {"(0|1)"
+                                          "(0|1)"
+                                          "23456789ABC",
 
-static struct GNUNET_MESH_Handle *h2;
+                                          "0123456789A*BC",
 
-static struct GNUNET_MESH_Tunnel *t;
+                                          "0*123456789ABC*"};
 
-static struct GNUNET_MESH_Tunnel *incoming_t;
+
+/**
+ * Service strings to look for.
+ */
+static char *strings[MESH_REGEX_PEERS] = {"1123456789ABC",
+
+                                          "0123456789AABC",
+
+                                          "00123456789ABCCCC"};
 
 /**
  * Check whether peers successfully shut down.
@@ -113,11 +147,14 @@ static struct GNUNET_MESH_Tunnel *incoming_t;
 static void
 shutdown_callback (void *cls, const char *emsg)
 {
+  unsigned int i;
+
   if (emsg != NULL)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 "test: Shutdown of peers failed! (%s)\n", emsg);
-    ok--;
+    for (i = 0; i < MESH_REGEX_PEERS; i++)
+      ok[i] = GNUNET_NO;
   }
 #if VERBOSE
   else
@@ -156,11 +193,16 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void
 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
+  unsigned int i;
+
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: disconnecting peers\n");
 
-  GNUNET_MESH_tunnel_destroy (t);
+  for (i = 0; i < MESH_REGEX_PEERS; i++)
+  {
+    GNUNET_MESH_tunnel_destroy (t[i]);
+    GNUNET_MESH_disconnect (h2[i]);
+  }
   GNUNET_MESH_disconnect (h1);
-  GNUNET_MESH_disconnect (h2);
   if (GNUNET_SCHEDULER_NO_TASK != shutdown_handle)
   {
     GNUNET_SCHEDULER_cancel (shutdown_handle);
@@ -183,7 +225,7 @@ tunnel_cleaner (void *cls, const struct GNUNET_MESH_Tunnel *tunnel,
 {
   long i = (long) cls;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Incoming tunnel disconnected at peer %d\n",
               i);
   return;
@@ -205,6 +247,28 @@ dh (void *cls, const struct GNUNET_PeerIdentity *peer)
   return;
 }
 
+/**
+ * Function called to notify a client about the connection
+ * begin ready to queue more data.  "buf" will be
+ * NULL and "size" zero if the connection was closed for
+ * writing in the meantime.
+ *
+ * @param cls closure
+ * @param size number of bytes available in buf
+ * @param buf where the callee should write the message
+ * @return number of bytes written to buf
+ */
+static size_t
+data_ready (void *cls, size_t size, void *buf)
+{
+  struct GNUNET_MessageHeader *m = buf;
+
+  if (NULL == buf || size < sizeof(struct GNUNET_MessageHeader))
+    return 0;
+  m->type = htons (1);
+  m->size = htons (sizeof(struct GNUNET_MessageHeader));
+  return sizeof(struct GNUNET_MessageHeader);
+}
 
 /**
  * Method called whenever a peer connects to a tunnel.
@@ -219,22 +283,16 @@ ch (void *cls, const struct GNUNET_PeerIdentity *peer,
 {
   long i = (long) cls;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "************************************************************\n");
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Peer connected: %s\n",
               GNUNET_i2s (peer));
-
-  if (i != 1L || peer == NULL)
-    ok = 0;
-  else
-    ok = 1;
-  if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
-  {
-    GNUNET_SCHEDULER_cancel (disconnect_task);
-    disconnect_task =
-        GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
-  } 
+  regex_peers++;
+  GNUNET_MESH_notify_transmit_ready(t[i], GNUNET_NO,
+                                    GNUNET_TIME_UNIT_FOREVER_REL,
+                                    peer,
+                                    sizeof(struct GNUNET_MessageHeader),
+                                    &data_ready, NULL);
 }
 
 /**
@@ -253,13 +311,15 @@ incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
                  const struct GNUNET_PeerIdentity *initiator,
                  const struct GNUNET_ATS_Information *atsi)
 {
+  long i = (long) cls;
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Incoming tunnel from %s to peer %d\n",
               GNUNET_i2s (initiator), (long) cls);
-  ok++;
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
-  if ((long) cls == 2L)
-    incoming_t = tunnel;
+  if (i > 1L && i <= 1L + MESH_REGEX_PEERS)
+  {
+    incoming_t[i - 2] = tunnel;
+    ok[i - 2] = GNUNET_OK;
+  }
   else
   {
     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
@@ -292,8 +352,26 @@ data_callback (void *cls, struct GNUNET_MESH_Tunnel *tunnel, void **tunnel_ctx,
                const struct GNUNET_MessageHeader *message,
                const struct GNUNET_ATS_Information *atsi)
 {
-
-    return GNUNET_OK;
+  int i;
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "test: GOT DATA!\n");
+  for (i = 0; i < MESH_REGEX_PEERS; i++)
+  {
+    if (GNUNET_OK != ok[i]) {
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "test: %u DATA MISSING!\n", i);
+      return GNUNET_OK;
+    }
+  }
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "test: EVERYONE GOT DATA, FINISHING!\n");
+  if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
+  {
+    GNUNET_SCHEDULER_cancel (disconnect_task);
+    disconnect_task =
+        GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
+  } 
+  return GNUNET_OK;
 }
 
 /**
@@ -314,20 +392,19 @@ static void
 peergroup_ready (void *cls, const char *emsg)
 {
   GNUNET_MESH_ApplicationType app;
+  struct GNUNET_TESTING_Daemon *d;
+  unsigned int i;
 
   if (emsg != NULL)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                 "test: Peergroup callback called with error, aborting test!\n");
-    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Error from testing: `%s'\n",
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: Error from testing: `%s'\n",
                 emsg);
-    ok--;
     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
     return;
   }
 #if VERBOSE
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "************************************************************\n");
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "test: Peer Group started successfully!\n");
   GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: Have %u connections\n",
@@ -337,49 +414,48 @@ peergroup_ready (void *cls, const char *emsg)
   peers_running = GNUNET_TESTING_daemons_running (pg);
   if (0 < failed_connections)
   {
-    ok = GNUNET_SYSERR;
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "test: %u connections have FAILED!\n",
                 failed_connections);
     disconnect_task = GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
     return;
   }
-  ok = 0;
   disconnect_task =
     GNUNET_SCHEDULER_add_delayed (TIMEOUT, &disconnect_peers, NULL);
-  d1 = GNUNET_TESTING_daemon_get (pg, 1);
-  d2 = GNUNET_TESTING_daemon_get (pg, 10);
+  d = GNUNET_TESTING_daemon_get (pg, 1);
+
   app = (GNUNET_MESH_ApplicationType) 0;
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "************************************************************\n");
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Connect to mesh\n");
-  h1 = GNUNET_MESH_connect (d1->cfg, 5, (void *) 1L,
+  h1 = GNUNET_MESH_connect (d->cfg, (void *) 1L,
                             NULL,
                             NULL,
                             handlers,
                             &app);
-  h2 = GNUNET_MESH_connect (d2->cfg, 5, (void *) 2L,
-                            &incoming_tunnel,
-                            &tunnel_cleaner,
-                            handlers,
-                            &app);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "************************************************************\n");
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Announce REGEX\n");
-  GNUNET_MESH_announce_regex (h2, "abc");
+  regex_peers = 0;
+  for (i = 0; i < MESH_REGEX_PEERS; i++)
+  {
+    ok[i] = GNUNET_NO;
+    d = GNUNET_TESTING_daemon_get (pg, 10 + i);
+    h2[i] = GNUNET_MESH_connect (d->cfg, (void *) (long) (i + 2),
+                                 &incoming_tunnel,
+                                 &tunnel_cleaner,
+                                 handlers,
+                                 &app);
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "Announce REGEX %u: %s\n", i, regexes[i]);
+    GNUNET_MESH_announce_regex (h2[i], regexes[i]);
+  }
 
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "************************************************************\n");
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Create tunnel\n");
-  t = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, NULL);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "************************************************************\n");
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "Connect by string\n");
-  GNUNET_MESH_peer_request_connect_by_string (t, "abc");
+  for (i = 0; i < MESH_REGEX_PEERS; i++)
+  {
+GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                "Create tunnel\n");
+    t[i] = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) (long) i);
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "Connect by string %s\n", strings[i]);
+    GNUNET_MESH_peer_request_connect_by_string (t[i], strings[i]);
+  }
   /* connect handler = success, timeout = error */
   
 }
@@ -435,7 +511,6 @@ run (void *cls, char *const *args, const char *cfgfile,
 {
   struct GNUNET_TESTING_Host *hosts;
 
-  ok = GNUNET_NO;
   total_connections = 0;
   failed_connections = 0;
   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
@@ -502,6 +577,8 @@ main (int argc, char *argv[])
 #endif
     NULL
   };
+  int result;
+  unsigned int i;
 
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Start\n");
 
@@ -513,12 +590,25 @@ main (int argc, char *argv[])
 #if REMOVE_DIR
   GNUNET_DISK_directory_remove ("/tmp/test_mesh_2dtorus");
 #endif
-  if (GNUNET_OK != ok)
+  result = GNUNET_OK;
+  for (i = 0; i < MESH_REGEX_PEERS; i++)
+  {
+    if (GNUNET_OK != ok[i])
+    {
+      result = GNUNET_SYSERR;
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "COULD NOT CONNECT TO %u: %s!\n",
+                  i, strings[i]);
+    }
+  }
+  if (GNUNET_OK != result || regex_peers != MESH_REGEX_PEERS)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test: FAILED!\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "test: FAILED! %u connected peers\n",
+                regex_peers);
     return 1;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: success\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: success\n");
   return 0;
 }