testbed_api_test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. This file is part of GNUnet
  3. (C) 2008--2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file testbed/testbed_api_test.c
  19. * @brief high-level test function
  20. * @author Christian Grothoff
  21. * @author Sree Harsha Totakura
  22. */
  23. #include "platform.h"
  24. #include "gnunet_testbed_service.h"
  25. /**
  26. * Context information for test run
  27. */
  28. struct TestRunContext
  29. {
  30. /**
  31. * Test master callback
  32. */
  33. GNUNET_TESTBED_TestMaster test_master;
  34. /**
  35. * Closure for test master
  36. */
  37. void *test_master_cls;
  38. /**
  39. * The controller event callback
  40. */
  41. GNUNET_TESTBED_ControllerCallback cc;
  42. /**
  43. * Closure for the above callback
  44. */
  45. void *cc_cls;
  46. /**
  47. * event mask for the controller callback
  48. */
  49. uint64_t event_mask;
  50. /**
  51. * Number of peers to start
  52. */
  53. unsigned int num_peers;
  54. };
  55. /**
  56. * Main run function.
  57. *
  58. * @param cls NULL
  59. * @param args arguments passed to GNUNET_PROGRAM_run
  60. * @param cfgfile the path to configuration file
  61. * @param config the configuration file handle
  62. */
  63. static void
  64. run (void *cls, char *const *args, const char *cfgfile,
  65. const struct GNUNET_CONFIGURATION_Handle *config)
  66. {
  67. struct TestRunContext *rc = cls;
  68. GNUNET_TESTBED_run (NULL, config, rc->num_peers, rc->event_mask, rc->cc,
  69. rc->cc_cls, rc->test_master, rc->test_master_cls);
  70. }
  71. /**
  72. * Convenience method for running a "simple" test on the local system
  73. * with a single call from 'main'. Underlay and overlay topology are
  74. * configured using the "UNDERLAY" and "OVERLAY" options in the
  75. * "[testbed]" section of the configuration (with possible options
  76. * given in "UNDERLAY_XXX" and/or "OVERLAY_XXX").
  77. *
  78. * The test is to be terminated using a call to
  79. * "GNUNET_SCHEDULER_shutdown". If starting the test fails,
  80. * the program is stopped without 'master' ever being run.
  81. *
  82. * NOTE: this function should be called from 'main', NOT from
  83. * within a GNUNET_SCHEDULER-loop. This function will initialze
  84. * the scheduler loop, the testbed and then pass control to
  85. * 'master'.
  86. *
  87. * @param testname name of the testcase (to configure logging, etc.)
  88. * @param cfg_filename configuration filename to use
  89. * (for testbed, controller and peers)
  90. * @param num_peers number of peers to start
  91. * @param event_mask bit mask with set of events to call 'cc' for;
  92. * or-ed values of "1LL" shifted by the
  93. * respective 'enum GNUNET_TESTBED_EventType'
  94. * (i.e. "(1LL << GNUNET_TESTBED_ET_CONNECT) || ...")
  95. * @param cc controller callback to invoke on events; This callback is called
  96. * for all peer start events even if GNUNET_TESTBED_ET_PEER_START isn't
  97. * set in the event_mask as this is the only way get access to the
  98. * handle of each peer
  99. * @param cc_cls closure for cc
  100. * @param test_master task to run once the test is ready
  101. * @param test_master_cls closure for 'task'.
  102. * @return GNUNET_SYSERR on error, GNUNET_OK on success
  103. */
  104. int
  105. GNUNET_TESTBED_test_run (const char *testname, const char *cfg_filename,
  106. unsigned int num_peers, uint64_t event_mask,
  107. GNUNET_TESTBED_ControllerCallback cc, void *cc_cls,
  108. GNUNET_TESTBED_TestMaster test_master,
  109. void *test_master_cls)
  110. {
  111. char *argv2[] = {
  112. NULL,
  113. "-c",
  114. NULL,
  115. NULL
  116. };
  117. struct GNUNET_GETOPT_CommandLineOption options[] = {
  118. GNUNET_GETOPT_OPTION_END
  119. };
  120. struct TestRunContext *rc;
  121. int ret;
  122. argv2[0] = GNUNET_strdup (testname);
  123. argv2[2] = GNUNET_strdup (cfg_filename);
  124. GNUNET_assert (NULL != test_master);
  125. GNUNET_assert (num_peers > 0);
  126. rc = GNUNET_malloc (sizeof (struct TestRunContext) +
  127. (num_peers * sizeof (struct GNUNET_TESTBED_Peer *)));
  128. rc->test_master = test_master;
  129. rc->test_master_cls = test_master_cls;
  130. rc->num_peers = num_peers;
  131. rc->event_mask = event_mask;
  132. rc->cc = cc;
  133. rc->cc_cls = cc_cls;
  134. ret =
  135. GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
  136. testname, "nohelp", options, &run, rc);
  137. GNUNET_free (rc);
  138. GNUNET_free (argv2[0]);
  139. GNUNET_free (argv2[2]);
  140. return ret;
  141. }
  142. /* end of testbed_api_test.c */