- towards testbed barriers
[oweals/gnunet.git] / src / testbed / test_testbed_api.c
index caa3d09fd0479f06527c0f7b12f6d5af17a603cf..97ca15c5f9ab9a78b6f1c0ee79de3ae399ddf20d 100644 (file)
@@ -1,6 +1,6 @@
 /*
       This file is part of GNUnet
-      (C) 2008--2012 Christian Grothoff (and other contributing authors)
+      (C) 2008--2013 Christian Grothoff (and other contributing authors)
 
       GNUnet is free software; you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published
@@ -26,8 +26,8 @@
 
 #include "platform.h"
 #include "gnunet_util_lib.h"
-#include "gnunet_dht_service.h"
-#include "gnunet_testing_lib-new.h"
+#include "gnunet_arm_service.h"
+#include "gnunet_testing_lib.h"
 #include "gnunet_testbed_service.h"
 
 /**
@@ -83,9 +83,9 @@ static struct GNUNET_CONFIGURATION_Handle *cfg;
 static struct GNUNET_TESTBED_Operation *operation;
 
 /**
- * Handle to peer's DHT service
+ * Handle to peer's ARM service
  */
-static struct GNUNET_DHT_Handle *dht_handle;
+static struct GNUNET_ARM_Handle *arm_handle;
 
 /**
  * Abort task identifier
@@ -136,22 +136,41 @@ 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)
     GNUNET_SCHEDULER_cancel (abort_task);
   if (NULL != reg_handle)
     GNUNET_TESTBED_cancel_registration (reg_handle);
-  GNUNET_TESTBED_controller_disconnect (controller);
-  GNUNET_CONFIGURATION_destroy (cfg);
+  if (NULL != controller)
+    GNUNET_TESTBED_controller_disconnect (controller);
+  if (NULL != cfg)
+    GNUNET_CONFIGURATION_destroy (cfg);
   if (NULL != cp)
     GNUNET_TESTBED_controller_stop (cp);
-  GNUNET_TESTBED_host_destroy (neighbour);
+  if (NULL != neighbour)
+    GNUNET_TESTBED_host_destroy (neighbour);
+  if (NULL != host)
   GNUNET_TESTBED_host_destroy (host);
 }
 
 
+/**
+ * shortcut to exit during failure
+ */
+#define FAIL_TEST(cond, ret) do {                                   \
+    if (!(cond)) {                                              \
+      GNUNET_break(0);                                          \
+      if (GNUNET_SCHEDULER_NO_TASK != abort_task)               \
+        GNUNET_SCHEDULER_cancel (abort_task);                   \
+      abort_task = GNUNET_SCHEDULER_NO_TASK;                    \
+      GNUNET_SCHEDULER_add_now (do_shutdown, NULL);             \
+      ret;                                                   \
+    }                                                          \
+  } while (0)
+
+
 /**
  * abort task to run on test timed out
  *
@@ -159,7 +178,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;
@@ -178,13 +197,13 @@ do_abort (void *cls, const const struct GNUNET_SCHEDULER_TaskContext *tc)
  * @return service handle to return in 'op_result', NULL on error
  */
 static void *
-dht_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
+arm_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-  GNUNET_assert (NULL == cls);
-  GNUNET_assert (OTHER == sub_test);
+  FAIL_TEST (NULL == cls, return NULL);
+  FAIL_TEST (OTHER == sub_test, return NULL);
   sub_test = PEER_SERVICE_CONNECT;
-  dht_handle = GNUNET_DHT_connect (cfg, 10);
-  return dht_handle;
+  arm_handle = GNUNET_ARM_connect (cfg, NULL, NULL);
+  return arm_handle;
 }
 
 
@@ -196,18 +215,82 @@ dht_connect_adapter (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg)
  * @param op_result service handle returned from the connect adapter
  */
 static void
-dht_disconnect_adapter (void *cls, void *op_result)
+arm_disconnect_adapter (void *cls, void *op_result)
 {
-  if (NULL != op_result)
-    GNUNET_DHT_disconnect (op_result);
-  GNUNET_assert (PEER_SERVICE_CONNECT == sub_test);
-  GNUNET_assert (NULL != operation);
-  operation = GNUNET_TESTBED_peer_stop (peer);
-  GNUNET_assert (NULL != operation);
+  FAIL_TEST (NULL != op_result, return);
+  FAIL_TEST (op_result == arm_handle, return);
+  GNUNET_ARM_disconnect_and_free (arm_handle);
+  arm_handle = NULL;
+  FAIL_TEST (PEER_SERVICE_CONNECT == sub_test, return);
+  FAIL_TEST (NULL != operation, return);
+  operation = GNUNET_TESTBED_peer_stop (NULL, peer, NULL, NULL);
+  FAIL_TEST (NULL != operation, return);
+}
+
+
+/**
+ * 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:
+    FAIL_TEST (operation == op, return);
+    FAIL_TEST (NULL == emsg, return);
+    FAIL_TEST (NULL == cls, return);
+    FAIL_TEST (ca_result == arm_handle, return);
+    GNUNET_TESTBED_operation_done (operation);  /* This results in call to
+                                                 * disconnect adapter */
+    break;
+  default:
+    FAIL_TEST (0, return);
+  }
 }
 
 
 
+/**
+ * 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:
+    FAIL_TEST (NULL != pinfo, return);
+    FAIL_TEST (NULL == emsg, return);
+    FAIL_TEST (NULL == cb_cls, return);
+    FAIL_TEST (operation == op, return);
+    FAIL_TEST (GNUNET_TESTBED_PIT_CONFIGURATION == pinfo->pit, return);
+    FAIL_TEST (NULL != pinfo->result.cfg, return);
+    sub_test = PEER_DESTROY;
+    GNUNET_TESTBED_operation_done (operation);
+    operation = GNUNET_TESTBED_peer_destroy (peer);
+    break;
+  default:
+    FAIL_TEST (0, return);
+  }
+}
+
+
 /**
  * Signature of the event handler function called by the
  * respective event controller.
@@ -223,67 +306,51 @@ controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
   case GNUNET_TESTBED_ET_OPERATION_FINISHED:
     switch (sub_test)
     {
-    case PEER_GETCONFIG:
-      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_TESTBED_operation_done (operation);
-      operation = GNUNET_TESTBED_peer_destroy (peer);
-      break;
     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_GENERIC ==
-                     event->details.operation_finished.pit);
-      GNUNET_assert (NULL ==
-                     event->details.operation_finished.op_result.generic);
+      FAIL_TEST (event->op == operation, return);
+      FAIL_TEST (NULL == event->op_cls, return);
+      FAIL_TEST (NULL == event->details.operation_finished.emsg, return);
+      FAIL_TEST (NULL == event->details.operation_finished.generic, return);
       GNUNET_TESTBED_operation_done (operation);
       GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
       break;
     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 != dht_handle);
-      GNUNET_assert (event->details.operation_finished.op_result.generic ==
-                     dht_handle);
-      GNUNET_TESTBED_operation_done (operation);        /* This results in call to
-                                                         * disconnect adapter */
+      FAIL_TEST (event->op == operation, return);
+      FAIL_TEST (NULL == event->op_cls, return);
+      FAIL_TEST (NULL == event->details.operation_finished.emsg, return);
+      FAIL_TEST (NULL != arm_handle, return);
+      FAIL_TEST (event->details.operation_finished.generic == arm_handle, return);
       break;
-    case OTHER:
-      GNUNET_assert (0);
+    default:
+      FAIL_TEST (0, return);
       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);
+    FAIL_TEST (event->details.peer_start.host == host, return);
+    FAIL_TEST (event->details.peer_start.peer == peer, return);
+    FAIL_TEST (OTHER == sub_test, return);
     GNUNET_TESTBED_operation_done (operation);
     operation =
-        GNUNET_TESTBED_service_connect (NULL, peer, "dht", &dht_connect_adapter,
-                                        &dht_disconnect_adapter, NULL);
-    GNUNET_assert (NULL != operation);
+        GNUNET_TESTBED_service_connect (NULL, peer, "dht",
+                                        &service_connect_comp_cb, NULL,
+                                        &arm_connect_adapter,
+                                        &arm_disconnect_adapter, NULL);
+    FAIL_TEST (NULL != operation, return);
     break;
   case GNUNET_TESTBED_ET_PEER_STOP:
-    GNUNET_assert (event->details.peer_stop.peer == peer);
-    GNUNET_assert (PEER_SERVICE_CONNECT == sub_test);
+    FAIL_TEST (event->details.peer_stop.peer == peer, return);
+    FAIL_TEST (PEER_SERVICE_CONNECT == sub_test, return);
     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_PIT_CONFIGURATION,
+                                             &peerinfo_cb, NULL);
     break;
   default:
-    GNUNET_assert (0);          /* We should never reach this state */
+    FAIL_TEST (0, return);          /* We should never reach this state */
   }
 }
 
@@ -303,12 +370,12 @@ 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);
+  FAIL_TEST (NULL != peer, return);
+  FAIL_TEST (NULL != peer_ptr, return);
   *peer_ptr = peer;
   GNUNET_TESTBED_operation_done (operation);
-  operation = GNUNET_TESTBED_peer_start (peer);
-  GNUNET_assert (NULL != operation);
+  operation = GNUNET_TESTBED_peer_start (NULL, peer, NULL, NULL);
+  FAIL_TEST (NULL != operation, return);
 }
 
 
@@ -321,12 +388,12 @@ peer_create_cb (void *cls, struct GNUNET_TESTBED_Peer *peer, const char *emsg)
 static void
 registration_comp (void *cls, const char *emsg)
 {
-  GNUNET_assert (cls == neighbour);
+  FAIL_TEST (cls == neighbour, return);
   reg_handle = NULL;
   operation =
       GNUNET_TESTBED_peer_create (controller, host, cfg, &peer_create_cb,
                                   &peer);
-  GNUNET_assert (NULL != operation);
+  FAIL_TEST (NULL != operation, return);
 }
 
 
@@ -340,26 +407,31 @@ registration_comp (void *cls, const char *emsg)
  *          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)
+status_cb (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg_, int status)
 {
   uint64_t event_mask;
 
-  GNUNET_assert (GNUNET_OK == status);
+  if (GNUNET_OK != status)
+  {
+    cp = NULL;
+    FAIL_TEST (0, return);
+    return;
+  }  
   event_mask = 0;
   event_mask |= (1L << GNUNET_TESTBED_ET_PEER_START);
   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,
+      GNUNET_TESTBED_controller_connect (host, event_mask, &controller_cb,
                                          NULL);
-  GNUNET_assert (NULL != controller);
-  neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, 0);
-  GNUNET_assert (NULL != neighbour);
+  FAIL_TEST (NULL != controller, return);
+  neighbour = GNUNET_TESTBED_host_create ("localhost", NULL, cfg, 0);
+  FAIL_TEST (NULL != neighbour, return);
   reg_handle =
       GNUNET_TESTBED_register_host (controller, neighbour, &registration_comp,
                                     neighbour);
-  GNUNET_assert (NULL != reg_handle);
+  FAIL_TEST (NULL != reg_handle, return);
 }
 
 
@@ -376,10 +448,10 @@ static void
 run (void *cls, char *const *args, const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *config)
 {
-  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,
+  host = GNUNET_TESTBED_host_create (NULL, NULL, cfg, 0);
+  FAIL_TEST (NULL != host, return);
+  cp = GNUNET_TESTBED_controller_start ("127.0.0.1", host, status_cb,
                                         NULL);
   abort_task =
       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
@@ -412,3 +484,5 @@ main (int argc, char **argv)
     return 1;
   return 0;
 }
+
+/* end of test_testbed_api.c */