doxygen
[oweals/gnunet.git] / src / testbed / test_testbed_api_controllerlink.c
index a82c782481d06a1c227c9cb14b42006f86614a68..703815a2d8a25696d54030565e931b6fc8eda404 100644 (file)
  * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  */
 
+
+/**
+ * The controller architecture we try to achieve in this test case:
+ *
+ *                    Master Controller 
+ *                    //             \\
+ *                   //               \\
+ *         Slave Controller 1---------Slave Controller 3
+ *                  ||                  
+ *                  ||                 
+ *         Slave Controller 2
+ */
+
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_testing_lib-new.h"
 enum Stage
 {
 
-    /**
-     * Initial stage
-     */
+  /**
+   * Initial stage
+   */
   INIT,
-
-    /**
-     * Master controller has started
-     */
+  
+  /**
+   * Master controller has started
+   */
   MASTER_STARTED,
 
-    /**
-     * The first slave has been registered at master controller
-     */
+  /**
+   * A peer has been created on master
+   */
+  MASTER_PEER_CREATE_SUCCESS,
+
+  /**
+   * Peer on master controller has been started successfully.
+   */
+  MASTER_PEER_START_SUCCESS,
+
+  /**
+   * The first slave has been registered at master controller
+   */
   SLAVE1_REGISTERED,
 
-    /**
-     * The second slave has been registered at the master controller
-     */
+  /**
+   * The second slave has been registered at the master controller
+   */
   SLAVE2_REGISTERED,
 
-    /**
-     * Link from master to slave 1 has been successfully created
-     */
+  /**
+   * Link from master to slave 1 has been successfully created
+   */
   SLAVE1_LINK_SUCCESS,
 
-    /**
-     * Link from slave 1 to slave 2 has been successfully created.
-     */
+  /**
+   * Link from slave 1 to slave 2 has been successfully created.
+   */
   SLAVE2_LINK_SUCCESS,
 
-    /**
-     * Peer create on slave 1 successful
-     */
+  /**
+   * Peer create on slave 1 successful
+   */
   SLAVE1_PEER_CREATE_SUCCESS,
 
-    /**
-     * Peer create on slave 2 successful
-     */
+  /**
+   * Peer create on slave 2 successful
+   */
   SLAVE2_PEER_CREATE_SUCCESS,
 
-    /**
-     * Peer startup on slave 1 successful
-     */
+  /**
+   * Peer startup on slave 1 successful
+   */
   SLAVE1_PEER_START_SUCCESS,
 
-    /**
-     * Peer on slave 1 successfully stopped
-     */
+  /**
+   * Peer on slave 1 successfully stopped
+   */
   SLAVE1_PEER_STOP_SUCCESS,
 
-    /**
-     * Peer startup on slave 2 successful
-     */
+  /**
+   * Peer startup on slave 2 successful
+   */
   SLAVE2_PEER_START_SUCCESS,
 
-    /**
-     * Peer on slave 2 successfully stopped
-     */
+  /**
+   * Try to connect peers on master and slave 2.
+   */
+  MASTER_SLAVE2_PEERS_CONNECTED,
+
+  /**
+   * Peer on slave 2 successfully stopped
+   */
   SLAVE2_PEER_STOP_SUCCESS,
 
-    /**
-     * Peer destroy on slave 1 successful
-     */
+  /**
+   * Peer destroy on slave 1 successful
+   */
   SLAVE1_PEER_DESTROY_SUCCESS,
 
-    /**
-     * Peer destory on slave 2 successful; Marks test as successful
-     */
-  SUCCESS
+  /**
+   * Peer destory on slave 2 successful
+   */
+  SLAVE2_PEER_DESTROY_SUCCESS,
+
+  /**
+   * Slave 3 has successfully registered
+   */
+  SLAVE3_REGISTERED,
+
+  /**
+   * Slave 3 has successfully started
+   */
+  SLAVE3_STARTED,
+
+  /**
+   * The configuration of slave 3 is acquired
+   */
+  SLAVE3_GET_CONFIG_SUCCESS,
+
+  /**
+   * Slave 1 has linked to slave 3;
+   */
+  SLAVE3_LINK_SUCCESS
+
 };
 
 /**
@@ -143,6 +192,11 @@ static struct GNUNET_TESTBED_Host *slave;
  */
 static struct GNUNET_TESTBED_Host *slave2;
 
+/**
+ * Host for slave 3
+ */
+static struct GNUNET_TESTBED_Host *slave3;
+
 /**
  * Slave host registration handle
  */
@@ -153,6 +207,11 @@ static struct GNUNET_TESTBED_HostRegistrationHandle *rh;
  */
 static struct GNUNET_CONFIGURATION_Handle *cfg;
 
+/**
+ * Configuration of slave 3 controller
+ */
+static struct GNUNET_CONFIGURATION_Handle *cfg3;
+
 /**
  * Abort task
  */
@@ -173,6 +232,11 @@ static struct GNUNET_TESTBED_Peer *slave1_peer;
  */
 static struct GNUNET_TESTBED_Peer *slave2_peer;
 
+/**
+ * Handle to a peer started at master controller
+ */
+static struct GNUNET_TESTBED_Peer *master_peer;
+
 /**
  * Event mask
  */
@@ -191,10 +255,12 @@ static enum Stage result;
  * @param tc the task context
  */
 static void
-do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
     GNUNET_SCHEDULER_cancel (abort_task);
+  if (NULL != slave3)
+    GNUNET_TESTBED_host_destroy (slave3);
   if (NULL != slave2)
     GNUNET_TESTBED_host_destroy (slave2);
   if (NULL != slave)
@@ -205,11 +271,12 @@ do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
     GNUNET_TESTBED_controller_disconnect (mc);
   if (NULL != cfg)
     GNUNET_CONFIGURATION_destroy (cfg);
+  if (NULL != cfg3)
+    GNUNET_CONFIGURATION_destroy (cfg3);
   if (NULL != cp)
     GNUNET_TESTBED_controller_stop (cp);
   if (NULL != rh)
     GNUNET_TESTBED_cancel_registration (rh);
-
 }
 
 
@@ -228,6 +295,21 @@ do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+/**
+ * Calls abort now
+ *
+ * @param 
+ * @return 
+ */
+static void
+do_abort_now (void *cls)
+{
+  if (GNUNET_SCHEDULER_NO_TASK != abort_task)
+    GNUNET_SCHEDULER_cancel (abort_task);
+  abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL);
+}
+
+
 /**
  * Task for inserting delay between tests
  *
@@ -243,7 +325,7 @@ delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     op = GNUNET_TESTBED_peer_stop (slave1_peer, NULL, NULL);
     GNUNET_assert (NULL != op);
     break;
-  case SLAVE2_PEER_START_SUCCESS:
+  case MASTER_SLAVE2_PEERS_CONNECTED:
     op = GNUNET_TESTBED_peer_stop (slave2_peer, NULL, NULL);
     GNUNET_assert (NULL != op);
     break;
@@ -265,29 +347,32 @@ delay_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 static void
 peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
 {
+  GNUNET_assert (NULL != peer);
+  GNUNET_assert (NULL == emsg);
   switch (result)
   {
+  case MASTER_STARTED:
+    result = MASTER_PEER_CREATE_SUCCESS;
+    master_peer = peer;
+    GNUNET_TESTBED_operation_done (op);
+    op = GNUNET_TESTBED_peer_start (NULL, master_peer, NULL, NULL);
+    break;
   case SLAVE2_LINK_SUCCESS:
-    GNUNET_assert (NULL != peer);
-    GNUNET_assert (NULL == emsg);
     result = SLAVE1_PEER_CREATE_SUCCESS;
     slave1_peer = peer;
     GNUNET_TESTBED_operation_done (op);
     op = GNUNET_TESTBED_peer_create (mc, slave2, cfg, peer_create_cb, NULL);
-    GNUNET_assert (NULL != op);
     break;
   case SLAVE1_PEER_CREATE_SUCCESS:
-    GNUNET_assert (NULL != peer);
-    GNUNET_assert (NULL == emsg);
     result = SLAVE2_PEER_CREATE_SUCCESS;
     slave2_peer = peer;
     GNUNET_TESTBED_operation_done (op);
-    op = GNUNET_TESTBED_peer_start (slave1_peer, NULL, NULL);
-    GNUNET_assert (NULL != op);
+    op = GNUNET_TESTBED_peer_start (NULL, slave1_peer, NULL, NULL);
     break;
   default:
     GNUNET_assert (0);
   }
+  GNUNET_assert (NULL != op);
 }
 
 
@@ -309,6 +394,16 @@ check_operation_success (const struct GNUNET_TESTBED_EventInformation *event)
 }
 
 
+/**
+ * Callback which will be called to after a host registration succeeded or failed
+ *
+ * @param cls the host which has been registered
+ * @param emsg the error message; NULL if host registration is successful
+ */
+static void
+registration_cont (void *cls, const char *emsg);
+
+
 /**
  * Signature of the event handler function called by the
  * respective event controller.
@@ -328,7 +423,7 @@ controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
     result = SLAVE1_LINK_SUCCESS;
     GNUNET_assert (NULL != slave2);
     GNUNET_assert (NULL != slave);
-    op = GNUNET_TESTBED_controller_link (mc, slave2, slave, cfg, GNUNET_YES);
+    op = GNUNET_TESTBED_controller_link (NULL, mc, slave2, slave, cfg, GNUNET_YES);
     GNUNET_assert (NULL != op);
     break;
   case SLAVE1_LINK_SUCCESS:
@@ -339,6 +434,17 @@ controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
     op = GNUNET_TESTBED_peer_create (mc, slave, cfg, peer_create_cb, NULL);
     GNUNET_assert (NULL != op);
     break;
+  case MASTER_PEER_CREATE_SUCCESS:
+    GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type);
+    GNUNET_assert (event->details.peer_start.host == host);
+    GNUNET_assert (event->details.peer_start.peer == master_peer);
+    GNUNET_TESTBED_operation_done (op);
+    result = MASTER_PEER_START_SUCCESS;
+    slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, 0);
+    GNUNET_assert (NULL != slave);
+    rh = GNUNET_TESTBED_register_host (mc, slave, &registration_cont, NULL);
+    GNUNET_assert (NULL != rh);
+    break;
   case SLAVE2_PEER_CREATE_SUCCESS:
     GNUNET_assert (GNUNET_TESTBED_ET_PEER_START == event->type);
     GNUNET_assert (event->details.peer_start.host == slave);
@@ -354,7 +460,7 @@ controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
     GNUNET_assert (event->details.peer_stop.peer == slave1_peer);
     GNUNET_TESTBED_operation_done (op);
     result = SLAVE1_PEER_STOP_SUCCESS;
-    op = GNUNET_TESTBED_peer_start (slave2_peer, NULL, NULL);
+    op = GNUNET_TESTBED_peer_start (NULL, slave2_peer, NULL, NULL);
     GNUNET_assert (NULL != op);
     break;
   case SLAVE1_PEER_STOP_SUCCESS:
@@ -363,11 +469,22 @@ controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
     GNUNET_assert (event->details.peer_start.peer == slave2_peer);
     GNUNET_TESTBED_operation_done (op);
     result = SLAVE2_PEER_START_SUCCESS;
+    op = GNUNET_TESTBED_overlay_connect (mc, NULL, NULL, master_peer,
+                                         slave2_peer);
+    break;
+  case SLAVE2_PEER_START_SUCCESS:
+    GNUNET_assert (NULL != event);
+    GNUNET_assert (GNUNET_TESTBED_ET_CONNECT == event->type);
+    GNUNET_assert (event->details.peer_connect.peer1 == master_peer);
+    GNUNET_assert (event->details.peer_connect.peer2 == slave2_peer);
+    result = MASTER_SLAVE2_PEERS_CONNECTED;
+    GNUNET_TESTBED_operation_done (op);
+    op = NULL;
     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
                                   (GNUNET_TIME_UNIT_SECONDS, 1), &delay_task,
                                   NULL);
     break;
-  case SLAVE2_PEER_START_SUCCESS:
+  case MASTER_SLAVE2_PEERS_CONNECTED:
     GNUNET_assert (GNUNET_TESTBED_ET_PEER_STOP == event->type);
     GNUNET_assert (event->details.peer_stop.peer == slave2_peer);
     GNUNET_TESTBED_operation_done (op);
@@ -385,8 +502,36 @@ controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
   case SLAVE1_PEER_DESTROY_SUCCESS:
     check_operation_success (event);
     GNUNET_TESTBED_operation_done (op);
-    result = SUCCESS;
-    GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
+    op = NULL;
+    result = SLAVE2_PEER_DESTROY_SUCCESS;
+    slave3 = GNUNET_TESTBED_host_create_with_id (3, "127.0.0.1", NULL, 0);
+    rh = GNUNET_TESTBED_register_host (mc, slave3, &registration_cont, NULL);
+    break;
+  case SLAVE3_REGISTERED:
+    check_operation_success (event);
+    GNUNET_TESTBED_operation_done (op);
+    op = NULL;
+    result = SLAVE3_STARTED;
+    op = GNUNET_TESTBED_get_slave_config (NULL, mc, slave3);
+    GNUNET_assert (NULL != op);    
+    break;
+  case SLAVE3_STARTED:
+    GNUNET_assert (NULL != event);
+    GNUNET_assert (GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type);
+    GNUNET_assert (event->details.operation_finished.operation == op);
+    GNUNET_assert (NULL == event->details.operation_finished.op_cls);
+    GNUNET_assert (NULL == event->details.operation_finished.emsg);
+    cfg3 = GNUNET_CONFIGURATION_dup (event->details.operation_finished.generic);
+    GNUNET_TESTBED_operation_done (op);
+    result = SLAVE3_GET_CONFIG_SUCCESS;
+    op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, slave, cfg3, GNUNET_NO);
+    break;
+  case SLAVE3_GET_CONFIG_SUCCESS:
+    result = SLAVE3_LINK_SUCCESS;
+    GNUNET_TESTBED_operation_done (op);
+    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+                                 (GNUNET_TIME_UNIT_SECONDS, 3),
+                                 &do_shutdown, NULL);
     break;
   default:
     GNUNET_assert (0);
@@ -406,7 +551,7 @@ registration_cont (void *cls, const char *emsg)
   rh = NULL;
   switch (result)
   {
-  case MASTER_STARTED:
+  case MASTER_PEER_START_SUCCESS:
     GNUNET_assert (NULL == emsg);
     GNUNET_assert (NULL != mc);
     result = SLAVE1_REGISTERED;
@@ -420,11 +565,20 @@ registration_cont (void *cls, const char *emsg)
     GNUNET_assert (NULL != mc);
     result = SLAVE2_REGISTERED;
     GNUNET_assert (NULL != cfg);
-    op = GNUNET_TESTBED_controller_link (mc, slave, NULL, cfg, GNUNET_YES);
+    op = GNUNET_TESTBED_controller_link (NULL, mc, slave, NULL, cfg, GNUNET_YES);
+    GNUNET_assert (NULL != op);
+    break;
+  case SLAVE2_PEER_DESTROY_SUCCESS:
+    GNUNET_assert (NULL == emsg);
+    GNUNET_assert (NULL != mc);
+    GNUNET_assert (NULL == op);
+    result = SLAVE3_REGISTERED;
+    op = GNUNET_TESTBED_controller_link (NULL, mc, slave3, NULL, cfg, GNUNET_YES);
     GNUNET_assert (NULL != op);
     break;
   default:
-    GNUNET_assert (0);
+    GNUNET_break (0);
+    do_abort_now (NULL);
   }
 }
 
@@ -454,13 +608,13 @@ status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *config,
                                             &controller_cb, NULL);
     GNUNET_assert (NULL != mc);
     result = MASTER_STARTED;
-    slave = GNUNET_TESTBED_host_create_with_id (1, "127.0.0.1", NULL, 0);
-    GNUNET_assert (NULL != slave);
-    rh = GNUNET_TESTBED_register_host (mc, slave, &registration_cont, NULL);
-    GNUNET_assert (NULL != rh);
+    op = GNUNET_TESTBED_peer_create (mc, host, cfg, peer_create_cb, NULL);
+    GNUNET_assert (NULL != op);
     break;
-  default:
-    GNUNET_assert (0);
+  default:    
+    GNUNET_break (0);
+    cp = NULL;
+    do_abort_now (NULL);
   }
 }
 
@@ -490,26 +644,26 @@ run (void *cls, char *const *args, const char *cfgfile,
 
 
 /**
- * Main function
+ * Function to check if 
+ * 1. Password-less SSH logins to given ip work
+ * 2. gnunet-helper-testbed is found on the PATH on the remote side
+ *
+ * @param host_str numeric representation of the host's ip
+ * @return GNUNET_YES if password-less SSH login to the given host works;
+ *           GNUNET_NO if not
  */
-int
-main (int argc, char **argv)
+static int
+check_ssh (char *host_str)
 {
-  int ret;
-
-  char *const argv2[] = { "test_testbed_api_controllerlink",
-    "-c", "test_testbed_api.conf",
-    NULL
-  };
-  struct GNUNET_GETOPT_CommandLineOption options[] = {
-    GNUNET_GETOPT_OPTION_END
-  };
   char *const remote_args[] = {
-    "ssh", "-o", "BatchMode=yes", "127.0.0.1", "echo", "Hello", "World", NULL
+    "ssh", "-o", "BatchMode=yes", "-o", "CheckHostIP=no", 
+    "-o", "NoHostAuthenticationForLocalhost=yes", "-q",
+    host_str, "which", "gnunet-helper-testbed", NULL
   };
   struct GNUNET_OS_Process *auxp;
   enum GNUNET_OS_ProcessStatusType type;
   unsigned long code;
+  int ret;
 
   auxp =
       GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ALL, NULL,
@@ -524,21 +678,42 @@ main (int argc, char **argv)
   while (GNUNET_NO == ret);
   (void) GNUNET_OS_process_wait (auxp);
   GNUNET_OS_process_destroy (auxp);
-  if (0 != code)
-  {
-    (void) printf ("Unable to run the test as this system is not configured "
-                   "to use password less SSH logins to localhost.\n"
-                   "Marking test as successful\n");
-    return 0;
-  }
+  return (0 != code) ? GNUNET_NO : GNUNET_YES;
+}
+
+
+/**
+ * Main function
+ */
+int
+main (int argc, char **argv)
+{
+  char *const argv2[] = { "test_testbed_api_controllerlink",
+    "-c", "test_testbed_api.conf",
+    NULL
+  };
+  struct GNUNET_GETOPT_CommandLineOption options[] = {
+    GNUNET_GETOPT_OPTION_END
+  };
+  int ret;
+
+  if (GNUNET_YES != check_ssh ("127.0.0.1"))
+    goto error_exit;  
   result = INIT;
   ret =
       GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
                           "test_testbed_api_controllerlink", "nohelp", options,
                           &run, NULL);
-  if ((GNUNET_OK != ret) || (SUCCESS != result))
+  if ((GNUNET_OK != ret) || (SLAVE3_LINK_SUCCESS != result))
     return 1;
   return 0;
+
+ error_exit:
+  (void) PRINTF ("%s",
+                 "Unable to run the test as this system is not configured "
+                 "to use password less SSH logins to localhost.\n"
+                 "Marking test as successful\n");
+  return 0;
 }
 
 /* end of test_testbed_api_controllerlink.c */