testbed_api_test.c 4.9 KB

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