tracking forwarded operations
[oweals/gnunet.git] / src / testbed / test_testbed_api.c
index 0d2a9d6770b2088d00f0ccd5657e69f4607e38a9..694d06274c5b0b464b60ad3cd79f11417053ef26 100644 (file)
 
 #include "platform.h"
 #include "gnunet_util_lib.h"
+#include "gnunet_dht_service.h"
 #include "gnunet_testing_lib-new.h"
 #include "gnunet_testbed_service.h"
 
-
 /**
  * Generic logging shortcut
  */
@@ -82,6 +82,11 @@ static struct GNUNET_CONFIGURATION_Handle *cfg;
  */
 static struct GNUNET_TESTBED_Operation *operation;
 
+/**
+ * Handle to peer's DHT service
+ */
+static struct GNUNET_DHT_Handle *dht_handle;
+
 /**
  * Abort task identifier
  */
@@ -97,22 +102,27 @@ static int result;
  * Enumeration of sub testcases
  */
 enum Test
-  {
+{
     /**
      * Test cases which are not covered by the below ones
      */
-    OTHER,
+  OTHER,
 
     /**
      * Test where we get a peer config from controller
      */
-    PEER_GETCONFIG,
+  PEER_GETCONFIG,
+
+    /**
+     * Test where we connect to a service running on the peer
+     */
+  PEER_SERVICE_CONNECT,
 
     /**
      * Test where we get a peer's identity from controller
      */
-    PEER_DESTROY,
-  };
+  PEER_DESTROY,
+};
 
 /**
  * Testing status
@@ -126,7 +136,7 @@ static enum Test sub_test;
  * @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)
 {
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Shutting down...\n");
   if (GNUNET_SCHEDULER_NO_TASK != abort_task)
@@ -149,7 +159,7 @@ do_shutdown (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param tc the task context
  */
 static void
-do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
   abort_task = GNUNET_SCHEDULER_NO_TASK;
@@ -157,6 +167,111 @@ do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 
+/**
+ * Adapter function called to establish a connection to
+ * a service.
+ *
+ * @param cls closure
+ * @param cfg configuration of the peer to connect to; will be available until
+ *          GNUNET_TESTBED_operation_done() is called on the operation returned
+ *          from GNUNET_TESTBED_service_connect()
+ * @return service handle to return in 'op_result', NULL on error
+ */
+static void *
+dht_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+  GNUNET_assert (NULL == cls);
+  GNUNET_assert (OTHER == sub_test);
+  sub_test = PEER_SERVICE_CONNECT;
+  dht_handle = GNUNET_DHT_connect (cfg, 10);
+  return dht_handle;
+}
+
+
+/**
+ * Adapter function called to destroy a connection to
+ * a service.
+ *
+ * @param cls closure
+ * @param op_result service handle returned from the connect adapter
+ */
+static void
+dht_disconnect_adapter (void *cls, void *op_result)
+{
+  GNUNET_assert (NULL != op_result);
+  GNUNET_assert (op_result == dht_handle);
+  GNUNET_DHT_disconnect (dht_handle);
+  dht_handle = NULL;
+  GNUNET_assert (PEER_SERVICE_CONNECT == sub_test);
+  GNUNET_assert (NULL != operation);
+  operation = GNUNET_TESTBED_peer_stop (peer, NULL, NULL);
+  GNUNET_assert (NULL != operation);
+}
+
+
+/**
+ * Callback to be called when a service connect operation is completed
+ *
+ * @param cls the callback closure from functions generating an operation
+ * @param op the operation that has been finished
+ * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
+ * @param emsg error message in case the operation has failed; will be NULL if
+ *          operation has executed successfully.
+ */
+static void 
+service_connect_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op,
+                        void *ca_result, const char *emsg)
+{
+  switch (sub_test)
+  {
+  case PEER_SERVICE_CONNECT:
+    GNUNET_assert (operation == op);
+    GNUNET_assert (NULL == emsg);
+    GNUNET_assert (NULL == cls);
+    GNUNET_assert (ca_result == dht_handle);
+    GNUNET_TESTBED_operation_done (operation);        /* This results in call to
+                                                      * disconnect adapter */
+    break;
+  default:
+    GNUNET_assert (0);
+  }
+}
+
+
+
+/**
+ * Callback to be called when the requested peer information is available
+ *
+ * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
+ * @param op the operation this callback corresponds to
+ * @param pinfo the result; will be NULL if the operation has failed
+ * @param emsg error message if the operation has failed; will be NULL if the
+ *          operation is successfull
+ */
+static void 
+peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op,
+            const struct GNUNET_TESTBED_PeerInformation *pinfo,
+            const char *emsg)
+{
+  switch (sub_test)
+  {
+  case PEER_GETCONFIG:
+    GNUNET_assert (NULL != pinfo);
+    GNUNET_assert (NULL == emsg);
+    GNUNET_assert (NULL == cb_cls);
+    GNUNET_assert (operation == op);
+    GNUNET_assert (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit);
+    GNUNET_assert (NULL != pinfo->result.cfg);
+    sub_test = PEER_DESTROY;
+    GNUNET_TESTBED_operation_done (operation);
+    operation = GNUNET_TESTBED_peer_destroy (peer);
+    break;
+  default:
+    GNUNET_assert (0);
+  }
+}
+
+
 /**
  * Signature of the event handler function called by the
  * respective event controller.
@@ -164,58 +279,59 @@ do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @param cls closure
  * @param event information about the event
  */
-static void 
-controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
+static void
+controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
 {
   switch (event->type)
   {
   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
-    switch(sub_test)
+    switch (sub_test)
     {
-    case PEER_GETCONFIG:
+    case PEER_DESTROY:
       GNUNET_assert (event->details.operation_finished.operation == operation);
       GNUNET_assert (NULL == event->details.operation_finished.op_cls);
       GNUNET_assert (NULL == event->details.operation_finished.emsg);
-      GNUNET_assert (GNUNET_TESTBED_PIT_CONFIGURATION ==
-                    event->details.operation_finished.pit);
-      GNUNET_assert (NULL != event->details.operation_finished.op_result.cfg);
-      sub_test = PEER_DESTROY;
+      GNUNET_assert (NULL == event->details.operation_finished.generic);
       GNUNET_TESTBED_operation_done (operation);
-      operation = GNUNET_TESTBED_peer_destroy (peer);
+      GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
       break;
-    case PEER_DESTROY:
+    case PEER_SERVICE_CONNECT:
       GNUNET_assert (event->details.operation_finished.operation == operation);
       GNUNET_assert (NULL == event->details.operation_finished.op_cls);
       GNUNET_assert (NULL == event->details.operation_finished.emsg);
-      GNUNET_assert (GNUNET_TESTBED_PIT_GENERIC ==
-                    event->details.operation_finished.pit);
-      GNUNET_assert (NULL ==
-                    event->details.operation_finished.op_result.generic); 
-      GNUNET_TESTBED_operation_done (operation);
-      GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
+      GNUNET_assert (NULL != dht_handle);
+      GNUNET_assert (event->details.operation_finished.generic == dht_handle);     
       break;
-    case OTHER:
+    default:
       GNUNET_assert (0);
       break;
-    }    
+    }
     break;
   case GNUNET_TESTBED_ET_PEER_START:
     GNUNET_assert (event->details.peer_start.host == host);
     GNUNET_assert (event->details.peer_start.peer == peer);
+    GNUNET_assert (OTHER == sub_test);
     GNUNET_TESTBED_operation_done (operation);
-    operation = GNUNET_TESTBED_peer_stop (peer);
+    operation =
+        GNUNET_TESTBED_service_connect (NULL, peer, "dht", 
+                                       &service_connect_comp_cb, NULL,
+                                       &dht_connect_adapter,
+                                       &dht_disconnect_adapter, NULL);
+    GNUNET_assert (NULL != operation);
     break;
   case GNUNET_TESTBED_ET_PEER_STOP:
-    GNUNET_assert (event->details.peer_stop.peer == peer);    
+    GNUNET_assert (event->details.peer_stop.peer == peer);
+    GNUNET_assert (PEER_SERVICE_CONNECT == sub_test);
     result = GNUNET_YES;
     sub_test = PEER_GETCONFIG;
     GNUNET_TESTBED_operation_done (operation);
     operation = 
-      GNUNET_TESTBED_peer_get_information (peer,
-                                          GNUNET_TESTBED_PIT_CONFIGURATION);
+       GNUNET_TESTBED_peer_get_information (peer,
+                                            GNUNET_TESTBED_PIT_CONFIGURATION,
+                                            &peerinfo_cb, NULL);
     break;
   default:
-    GNUNET_assert (0);         /* We should never reach this state */
+    GNUNET_assert (0);          /* We should never reach this state */
   }
 }
 
@@ -230,17 +346,16 @@ controller_cb(void *cls, const struct GNUNET_TESTBED_EventInformation *event)
  * @param emsg NULL if peer is not NULL; else MAY contain the error description
  */
 static void
-peer_create_cb (void *cls,
-               struct GNUNET_TESTBED_Peer *peer, const char *emsg)
+peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
 {
   struct GNUNET_TESTBED_Peer **peer_ptr;
-  
+
   peer_ptr = cls;
   GNUNET_assert (NULL != peer);
   GNUNET_assert (NULL != peer_ptr);
   *peer_ptr = peer;
   GNUNET_TESTBED_operation_done (operation);
-  operation = GNUNET_TESTBED_peer_start (peer);
+  operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
   GNUNET_assert (NULL != operation);
 }
 
@@ -251,12 +366,14 @@ peer_create_cb (void *cls,
  * @param cls the host which has been registered
  * @param emsg the error message; NULL if host registration is successful
  */
-static void 
+static void
 registration_comp (void *cls, const char *emsg)
 {
   GNUNET_assert (cls == neighbour);
-  reg_handle = NULL;  
-  operation = GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb, &peer);
+  reg_handle = NULL;
+  operation =
+      GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
+                                  &peer);
   GNUNET_assert (NULL != operation);
 }
 
@@ -270,9 +387,8 @@ registration_comp (void *cls, const char *emsg)
  * @param status GNUNET_OK if the startup is successfull; GNUNET_SYSERR if not,
  *          GNUNET_TESTBED_controller_stop() shouldn't be called in this case
  */
-static void 
-status_cb (void *cls, 
-          const struct GNUNET_CONFIGURATION_Handle *cfg, int status)
+static void
+status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, int status)
 {
   uint64_t event_mask;
 
@@ -282,21 +398,22 @@ status_cb (void *cls,
   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_STOP);
   event_mask |= (1L << GNUNET_TESTBED_ET_CONNECT);
   event_mask |= (1L << GNUNET_TESTBED_ET_OPERATION_FINISHED);
-  controller = GNUNET_TESTBED_controller_connect (cfg, host, event_mask,
-                                                  &controller_cb, NULL);
+  controller =
+      GNUNET_TESTBED_controller_connect (cfg, host, event_mask, &controller_cb,
+                                         NULL);
   GNUNET_assert (NULL != controller);
   neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
   GNUNET_assert (NULL != neighbour);
-  reg_handle = 
-    GNUNET_TESTBED_register_host (controller, neighbour, &registration_comp,
-                                  neighbour);
+  reg_handle =
+      GNUNET_TESTBED_register_host (controller, neighbour, &registration_comp,
+                                    neighbour);
   GNUNET_assert (NULL != reg_handle);
 }
 
 
 
 /**
- * Main run function. 
+ * Main run function.
  *
  * @param cls NULL
  * @param args arguments passed to GNUNET_PROGRAM_run
@@ -310,31 +427,38 @@ run (void *cls, char *const *args, const char *cfgfile,
   host = GNUNET_TESTBED_host_create (NULL, NULL, 0);
   GNUNET_assert (NULL != host);
   cfg = GNUNET_CONFIGURATION_dup (config);
-  cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb, NULL);
-  abort_task = GNUNET_SCHEDULER_add_delayed 
-    (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort, NULL);
+  cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, cfg, status_cb,
+                                        NULL);
+  abort_task =
+      GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
+                                    (GNUNET_TIME_UNIT_MINUTES, 5), &do_abort,
+                                    NULL);
 }
 
 
 /**
  * Main function
  */
-int main (int argc, char **argv)
+int
+main (int argc, char **argv)
 {
   int ret;
 
   char *const argv2[] = { "test_testbed_api",
-                          "-c", "test_testbed_api.conf",
-                          NULL
+    "-c", "test_testbed_api.conf",
+    NULL
   };
   struct GNUNET_GETOPT_CommandLineOption options[] = {
     GNUNET_GETOPT_OPTION_END
   };
+
   result = GNUNET_SYSERR;
-  ret = GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
-                           "test_testbed_api", "nohelp", options, &run,
-                           NULL);
+  ret =
+      GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
+                          "test_testbed_api", "nohelp", options, &run, NULL);
   if ((GNUNET_OK != ret) || (GNUNET_OK != result))
     return 1;
   return 0;
 }
+
+/* end of test_testbed_api.c */