- todo
[oweals/gnunet.git] / src / mesh / test_mesh_regex.c
index 499eac5bf961c537ede44880f02c60507f719cf7..4bb0c8ddf40cc35700b339d0644e961f5319e00d 100644 (file)
 #include "gnunet_testing_lib.h"
 #include "gnunet_mesh_service.h"
 
-#define VERBOSE GNUNET_YES
 #define REMOVE_DIR GNUNET_YES
-#define MESH_REGEX_PEERS 2
+#define MESH_REGEX_PEERS 4
 
 /**
  * How long until we give up on connecting the peers?
  */
-#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
+#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
 
 /**
  * Time to wait for stuff that should be rather fast
  */
 #define SHORT_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
 
-
 /**
  * Which strings have been found & connected.
  */
@@ -50,7 +48,7 @@ static int ok[MESH_REGEX_PEERS];
 /**
  * How many connects have happened.
  */
-static int connected_peers;
+static int regex_peers;
 
 /**
  * Be verbose
@@ -122,22 +120,25 @@ static struct GNUNET_MESH_Tunnel *incoming_t[MESH_REGEX_PEERS];
  */
 static char *regexes[MESH_REGEX_PEERS] = {"(0|1)"
                                           "(0|1)"
-                                          "23456789A*BC",
+                                          "23456789ABC",
+
+                                          "0123456789A*BC",
 
-                                          "0123456789ABC"/*,
+                                          "0*123456789ABC*",
 
-                                          "0*123456789ABC*"*/};
-//                                          "(0|1|2|3|4|5|6|7|8|9)"
+                                          "GNUNETVPN0001000IPEX401110011101100100000111(0|1)*"};
 
 
 /**
  * Service strings to look for.
  */
-static char *strings[MESH_REGEX_PEERS] = {"0123456789ABC",
+static char *strings[MESH_REGEX_PEERS] = {"1123456789ABC",
+
+                                          "0123456789AABC",
 
-                                          "0123456789ABC"/*,
+                                          "00123456789ABCCCC",
 
-                                          "000123456789ABCCCC"*/};
+                                          "GNUNETVPN0001000IPEX401110011101100100000111"};
 
 /**
  * Check whether peers successfully shut down.
@@ -157,13 +158,11 @@ shutdown_callback (void *cls, const char *emsg)
     for (i = 0; i < MESH_REGEX_PEERS; i++)
       ok[i] = GNUNET_NO;
   }
-#if VERBOSE
   else
   {
     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "test: All peers successfully shut down!\n");
   }
-#endif
   GNUNET_CONFIGURATION_destroy (testing_cfg);
 }
 
@@ -178,9 +177,7 @@ shutdown_callback (void *cls, const char *emsg)
 static void
 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-#if VERBOSE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Ending test.\n");
-#endif
   shutdown_handle = GNUNET_SCHEDULER_NO_TASK;
   GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
 }
@@ -189,7 +186,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 /**
  * Ends test: Disconnects peers and calls shutdown.
  * @param cls Closure (not used).
- * @param tc TaskContext. 
+ * @param tc TaskContext.
  */
 static void
 disconnect_peers (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
@@ -226,7 +223,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;
@@ -248,6 +245,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.
@@ -265,15 +284,13 @@ ch (void *cls, const struct GNUNET_PeerIdentity *peer,
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Peer connected: %s\n",
               GNUNET_i2s (peer));
-  connected_peers++;
-  GNUNET_assert (i = 1L);
+  regex_peers++;
 
-  if (GNUNET_SCHEDULER_NO_TASK != disconnect_task)
-  {
-    GNUNET_SCHEDULER_cancel (disconnect_task);
-    disconnect_task =
-        GNUNET_SCHEDULER_add_now (&disconnect_peers, NULL);
-  } 
+  GNUNET_MESH_notify_transmit_ready(t[i], GNUNET_NO,
+                                    GNUNET_TIME_UNIT_FOREVER_REL,
+                                    peer,
+                                    sizeof(struct GNUNET_MessageHeader),
+                                    &data_ready, NULL);
 }
 
 /**
@@ -296,11 +313,10 @@ incoming_tunnel (void *cls, struct GNUNET_MESH_Tunnel *tunnel,
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Incoming tunnel from %s to peer %d\n",
               GNUNET_i2s (initiator), (long) cls);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, " ok: %d\n", ok);
   if (i > 1L && i <= 1L + MESH_REGEX_PEERS)
   {
-    incoming_t[i - 1] = tunnel;
-    ok[i - 1] = GNUNET_OK;
+    incoming_t[i - 2] = tunnel;
+    ok[i - 2] = GNUNET_OK;
   }
   else
   {
@@ -334,7 +350,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;
 }
 
 /**
@@ -367,13 +402,9 @@ peergroup_ready (void *cls, const char *emsg)
     GNUNET_TESTING_daemons_stop (pg, TIMEOUT, &shutdown_callback, NULL);
     return;
   }
-#if VERBOSE
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-              "test: Peer Group started successfully!\n");
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: Have %u connections\n",
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Peer Group started successfully with %u connections\n",
               total_connections);
-#endif
-
   peers_running = GNUNET_TESTING_daemons_running (pg);
   if (0 < failed_connections)
   {
@@ -390,36 +421,37 @@ peergroup_ready (void *cls, const char *emsg)
 
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Connect to mesh\n");
-  h1 = GNUNET_MESH_connect (d->cfg, 5, (void *) 1L,
+  h1 = GNUNET_MESH_connect (d->cfg, (void *) 1L,
                             NULL,
                             NULL,
                             handlers,
                             &app);
+  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, 5, (void *) (long) (i + 1),
+    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_MESH_announce_regex (h2[i], regexes[i], 1);
   }
 
   for (i = 0; i < MESH_REGEX_PEERS; i++)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                 "Create tunnel\n");
-    t[i] = GNUNET_MESH_tunnel_create (h1, NULL, &ch, &dh, (void *) 1L);
+    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 */
-  
+
 }
 
 
@@ -478,19 +510,9 @@ run (void *cls, char *const *args, const char *cfgfile,
   testing_cfg = GNUNET_CONFIGURATION_dup (cfg);
 
   GNUNET_log_setup ("test_mesh_regex",
-#if VERBOSE
-                    "DEBUG",
-#else
                     "WARNING",
-#endif
                     NULL);
-
-#if VERBOSE
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Starting daemons.\n");
-  GNUNET_CONFIGURATION_set_value_string (testing_cfg, "testing_old",
-                                         "use_progressbars", "YES");
-#endif
-
   if (GNUNET_OK !=
       GNUNET_CONFIGURATION_get_value_number (testing_cfg, "testing_old",
                                              "num_peers", &num_peers))
@@ -533,10 +555,6 @@ main (int argc, char *argv[])
     argv[0],
     "-c",
     "test_mesh_2dtorus.conf",
-#if VERBOSE
-    "-L",
-    "DEBUG",
-#endif
     NULL
   };
   int result;
@@ -563,14 +581,14 @@ main (int argc, char *argv[])
                   i, strings[i]);
     }
   }
-  if (GNUNET_OK != result || connected_peers != MESH_REGEX_PEERS)
+  if (GNUNET_OK != result || regex_peers != MESH_REGEX_PEERS)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "test: FAILED! %u connected peers\n",
-                connected_peers);
+                regex_peers);
     return 1;
   }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: success\n");
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test: success\n");
   return 0;
 }