doc
[oweals/gnunet.git] / src / stream / test_stream_big.c
index 58ad4427c6d9d021f598be0d80d394915154ce26..05da826913eb8ca55798c9bf7c51de6f94def5de 100644 (file)
@@ -19,8 +19,8 @@
 */
 
 /**
- * @file stream/test_stream_local.c
- * @brief Stream API testing between local peers
+ * @file stream/test_stream_big.c
+ * @brief large data transfer using stream API between local peers
  * @author Sree Harsha Totakura
  */
 
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_stream_lib.h"
-#include "gnunet_testing_lib.h"
+#include "gnunet_testing_lib-new.h"
 
 #define LOG(kind, ...)                         \
   GNUNET_log (kind, __VA_ARGS__);
 
+#define TIME_REL_SECS(sec) \
+  GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, sec)
+
+
 /**
  * Structure for holding peer's sockets and IO Handles
  */
@@ -44,6 +48,8 @@ struct PeerData
    */
   struct GNUNET_STREAM_Socket *socket;
 
+  struct GNUNET_PeerIdentity self;
+
   /**
    * Peer's io write handle
    */
@@ -65,14 +71,12 @@ struct PeerData
   unsigned int bytes_read;
 };
 
-static struct GNUNET_OS_Process *arm_pid;
 static struct PeerData peer1;
 static struct PeerData peer2;
 static struct GNUNET_STREAM_ListenSocket *peer2_listen_socket;
-static struct GNUNET_CONFIGURATION_Handle *config;
+static const struct GNUNET_CONFIGURATION_Handle *config;
 
 static GNUNET_SCHEDULER_TaskIdentifier abort_task;
-static GNUNET_SCHEDULER_TaskIdentifier test_task;
 static GNUNET_SCHEDULER_TaskIdentifier read_task;
 static GNUNET_SCHEDULER_TaskIdentifier write_task;
 
@@ -86,26 +90,17 @@ static int result;
 static void
 do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-  GNUNET_STREAM_close (peer1.socket);
+  if (NULL != peer1.socket)
+    GNUNET_STREAM_close (peer1.socket);
   if (NULL != peer2.socket)
     GNUNET_STREAM_close (peer2.socket);
   if (NULL != peer2_listen_socket)
     GNUNET_STREAM_listen_close (peer2_listen_socket); /* Close listen socket */
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: shutdown\n");
-  if (0 != abort_task)
-  {
+  if (GNUNET_SCHEDULER_NO_TASK != abort_task)
     GNUNET_SCHEDULER_cancel (abort_task);
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: arm\n");
-  if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM))
-  {
-    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
-  }
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: Wait\n");
-  /* Free the duplicated configuration */
-  GNUNET_CONFIGURATION_destroy (config);
-  GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid));
-  GNUNET_OS_process_destroy (arm_pid);
+  GNUNET_SCHEDULER_shutdown ();
 }
 
 
@@ -116,16 +111,10 @@ static void
 do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test: ABORT\n");
-  if (0 != test_task)
-  {
-    GNUNET_SCHEDULER_cancel (test_task);
-  }
-  if (0 != read_task)
-    {
+  if (GNUNET_SCHEDULER_NO_TASK != read_task)
       GNUNET_SCHEDULER_cancel (read_task);
-    }
   result = GNUNET_SYSERR;
-  abort_task = 0;
+  abort_task = GNUNET_SCHEDULER_NO_TASK;
   do_shutdown (cls, tc);
 }
 
@@ -147,7 +136,7 @@ write_completion (void *cls,
 
   peer = (struct PeerData *) cls;
   GNUNET_assert (GNUNET_STREAM_OK == status);
-  GNUNET_assert (size < DATA_SIZE);
+  GNUNET_assert (size <= DATA_SIZE);
   peer->bytes_wrote += size;
 
   if (peer->bytes_wrote < DATA_SIZE) /* Have more data to send */
@@ -155,7 +144,7 @@ write_completion (void *cls,
       peer->io_write_handle =
         GNUNET_STREAM_write (peer->socket,
                              ((void *) data) + peer->bytes_wrote,
-                             DATA_SIZE - peer->bytes_wrote,
+                            sizeof (data) - peer->bytes_wrote,
                              GNUNET_TIME_relative_multiply
                              (GNUNET_TIME_UNIT_SECONDS, 5),
                              &write_completion,
@@ -193,7 +182,7 @@ stream_write_task (void *cls,
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Generation of random data complete\n");
   peer->io_write_handle = GNUNET_STREAM_write (peer->socket,
                                                data,
-                                               DATA_SIZE,
+                                               sizeof (data),
                                                GNUNET_TIME_relative_multiply
                                                (GNUNET_TIME_UNIT_SECONDS, 10),
                                                &write_completion,
@@ -310,9 +299,17 @@ stream_read_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  */
 static int
 stream_listen_cb (void *cls,
-           struct GNUNET_STREAM_Socket *socket,
-           const struct GNUNET_PeerIdentity *initiator)
+                 struct GNUNET_STREAM_Socket *socket,
+                 const struct GNUNET_PeerIdentity *initiator)
 {
+  if ((NULL == socket) || (NULL == initiator))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Binding error\n");
+    if (GNUNET_SCHEDULER_NO_TASK != abort_task)
+      GNUNET_SCHEDULER_cancel (abort_task);
+    abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
+    return GNUNET_OK;
+  }
   GNUNET_assert (NULL != socket);
   GNUNET_assert (socket != peer1.socket);
 
@@ -327,62 +324,55 @@ stream_listen_cb (void *cls,
 
 
 /**
- * Testing function
- *
- * @param cls NULL
- * @param tc the task context
+ * Listen success callback; connects a peer to stream as client
  */
 static void
-test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+stream_connect (void)
 {
-  struct GNUNET_PeerIdentity self;
+  struct PeerData *peer = &peer1;
 
-  test_task = GNUNET_SCHEDULER_NO_TASK;
-  /* Get our identity */
-  GNUNET_assert (GNUNET_OK == GNUNET_TESTING_get_peer_identity (config,
-                                                                &self));
-
-  peer2_listen_socket = GNUNET_STREAM_listen (config,
-                                              10, /* App port */
-                                              &stream_listen_cb,
-                                              NULL,
-                                              GNUNET_STREAM_OPTION_END);
-  GNUNET_assert (NULL != peer2_listen_socket);
-
-  /* Connect to stream library */
-  peer1.socket = GNUNET_STREAM_open (config,
-                                     &self,         /* Null for local peer? */
+  /* Connect to stream */
+  peer->socket = GNUNET_STREAM_open (config,
+                                     &peer2.self,         /* Null for local peer? */
                                      10,           /* App port */
-                                     &stream_open_cb,
-                                     (void *) &peer1,
-                                     GNUNET_STREAM_OPTION_END);
-  GNUNET_assert (NULL != peer1.socket);                  
+                                     &stream_open_cb, &peer1,
+                                    GNUNET_STREAM_OPTION_MAX_PAYLOAD_SIZE, 500,
+                                    GNUNET_STREAM_OPTION_END);
+  GNUNET_assert (NULL != peer->socket);
 }
 
 
 /**
  * Initialize framework and start test
+ *
+ * @param cls closure
+ * @param cfg configuration of the peer that was started
+ * @param peer identity of the peer that was created
  */
 static void
-run (void *cls, char *const *args, const char *cfgfile,
-     const struct GNUNET_CONFIGURATION_Handle *cfg)
+run (void *cls, 
+     const struct GNUNET_CONFIGURATION_Handle *cfg,
+     struct GNUNET_TESTING_Peer *peer)
 {
-  /* Duplicate the configuration */
-  config = GNUNET_CONFIGURATION_dup (cfg);
-  arm_pid =
-    GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-arm",
-                             "gnunet-service-arm",
-#if VERBOSE_ARM
-                             "-L", "DEBUG",
-#endif
-                             "-c", "test_stream_local.conf", NULL);
+  struct GNUNET_PeerIdentity self;
   
+  GNUNET_TESTING_peer_get_identity (peer, &self);
+  config = cfg;  
+  peer2_listen_socket = 
+    GNUNET_STREAM_listen (config,
+                          10, /* App port */
+                          &stream_listen_cb,
+                          NULL,
+                          GNUNET_STREAM_OPTION_SIGNAL_LISTEN_SUCCESS,
+                          &stream_connect,
+                          GNUNET_STREAM_OPTION_END);
+  GNUNET_assert (NULL != peer2_listen_socket);
+  peer1.self = self;
+  peer2.self = self;
   abort_task =
     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
                                   (GNUNET_TIME_UNIT_SECONDS, 60), &do_abort,
                                   NULL);
-  
-  test_task = GNUNET_SCHEDULER_add_now (&test, NULL);  
 }
 
 /**
@@ -390,33 +380,11 @@ run (void *cls, char *const *args, const char *cfgfile,
  */
 int main (int argc, char **argv)
 {
-  int ret;
-  
-  char *const argv2[] = { "test-stream-big",
-                          "-c", "test_stream_local.conf",
-                          "-L", "DEBUG",
-                          NULL
-  };
-  
-  struct GNUNET_GETOPT_CommandLineOption options[] = {
-    GNUNET_GETOPT_OPTION_END
-  };
-  
-  ret =
-    GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
-                        "test-stream-big", "nohelp", options, &run, NULL);
-  
-  if (GNUNET_OK != ret)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "run failed with error code %d\n",
-                ret);
-    return 1;
-  }
-  if (GNUNET_SYSERR == result)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "test failed\n");
+  if (0 != GNUNET_TESTING_peer_run ("test_stream_big",
+                                   "test_stream_local.conf",
+                                   &run, NULL))
     return 1;
-  }
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "test ok\n");
-  return 0;
+  return (GNUNET_SYSERR == result) ? 1 : 0;
 }
+
+/* end of test_stream_big.c */