Merge branch 'credentials' of git+ssh://gnunet.org/gnunet into credentials
[oweals/gnunet.git] / src / testbed / testbed_api_test.c
index ed5e2e8e326f71065a54844666874734fe6dd268..e64e9fe2d84cc8ac0586dbd247c7579a128dd819 100644 (file)
@@ -1,6 +1,6 @@
 /*
       This file is part of GNUnet
-      (C) 2008--2012 Christian Grothoff (and other contributing authors)
+      Copyright (C) 2008--2013 GNUnet e.V.
 
       GNUnet is free software; you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published
 
       You should have received a copy of the GNU General Public License
       along with GNUnet; see the file COPYING.  If not, write to the
-      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-      Boston, MA 02111-1307, USA.
+      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+      Boston, MA 02110-1301, USA.
  */
 
 /**
- * @file testing/new_testing_api_test.c
+ * @file testbed/testbed_api_test.c
  * @brief high-level test function
  * @author Christian Grothoff
+ * @author Sree Harsha Totakura
  */
 #include "platform.h"
-#include "gnunet_testing_service.h"
+#include "gnunet_testbed_service.h"
 
 
+/**
+ * Context information for test run
+ */
+struct TestRunContext
+{
+  /**
+   * Test master callback
+   */
+  GNUNET_TESTBED_TestMaster test_master;
+
+  /**
+   * Closure for test master
+   */
+  void *test_master_cls;
+
+  /**
+   * The controller event callback
+   */
+  GNUNET_TESTBED_ControllerCallback cc;
+
+  /**
+   * Closure for the above callback
+   */
+  void *cc_cls;
+
+  /**
+   * event mask for the controller callback
+   */
+  uint64_t event_mask;
+
+  /**
+   * Number of peers to start
+   */
+  unsigned int num_peers;
+};
+
+
+/**
+ * Main run function.
+ *
+ * @param cls NULL
+ * @param args arguments passed to GNUNET_PROGRAM_run
+ * @param cfgfile the path to configuration file
+ * @param config the configuration file handle
+ */
+static void
+run (void *cls, char *const *args, const char *cfgfile,
+     const struct GNUNET_CONFIGURATION_Handle *config)
+{
+  struct TestRunContext *rc = cls;
+
+  GNUNET_TESTBED_run (NULL, config, rc->num_peers, rc->event_mask, rc->cc,
+                      rc->cc_cls, rc->test_master, rc->test_master_cls);
+}
 
 
 /**
  * @param cfg_filename configuration filename to use
  *              (for testbed, controller and peers)
  * @param num_peers number of peers to start
+ * @param event_mask bit mask with set of events to call 'cc' for;
+ *                   or-ed values of "1LL" shifted by the
+ *                   respective 'enum GNUNET_TESTBED_EventType'
+ *                   (i.e.  "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
+ * @param cc controller callback to invoke on events; This callback is called
+ *        for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
+ *        set in the event_mask as this is the only way get access to the
+ *        handle of each peer
+ * @param cc_cls closure for cc
  * @param test_master task to run once the test is ready
  * @param test_master_cls closure for 'task'.
+ * @return GNUNET_SYSERR on error, GNUNET_OK on success
  */
-void
-GNUNET_TESTBED_test_run (const char *testname,
-                        const char *cfg_filename,
-                        unsigned int num_peers,
-                        GNUNET_TESTBED_TestMaster test_master,
-                        void *test_master_cls)
+int
+GNUNET_TESTBED_test_run (const char *testname, const char *cfg_filename,
+                         unsigned int num_peers, uint64_t event_mask,
+                         GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
+                         GNUNET_TESTBED_TestMaster test_master,
+                         void *test_master_cls)
 {
-  GNUNET_break (0);
-}
-
+  char *argv2[] = {
+    NULL,
+    "-c",
+    NULL,
+    NULL
+  };
+  struct GNUNET_GETOPT_CommandLineOption options[] = {
+    GNUNET_GETOPT_OPTION_END
+  };
+  struct TestRunContext *rc;
+  int ret;
 
+  argv2[0] = GNUNET_strdup (testname);
+  argv2[2] = GNUNET_strdup (cfg_filename);
+  GNUNET_assert (NULL != test_master);
+  GNUNET_assert (num_peers > 0);
+  rc = GNUNET_malloc (sizeof (struct TestRunContext) +
+                      (num_peers * sizeof (struct GNUNET_TESTBED_Peer *)));
+  rc->test_master = test_master;
+  rc->test_master_cls = test_master_cls;
+  rc->num_peers = num_peers;
+  rc->event_mask = event_mask;
+  rc->cc = cc;
+  rc->cc_cls = cc_cls;
+  ret =
+      GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
+                          testname, "nohelp", options, &run, rc);
+  GNUNET_free (rc);
+  GNUNET_free (argv2[0]);
+  GNUNET_free (argv2[2]);
+  return ret;
+}
 
-/* end of new_testing_api_test.c */
+/* end of testbed_api_test.c */