test_testbed_api_testbed_run.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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/test_testbed_api_testbed_run.c
  18. * @brief Test cases for testing high-level testbed management
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testbed_service.h"
  24. /**
  25. * Number of peers we want to start
  26. */
  27. #define NUM_PEERS 5
  28. /**
  29. * The array of peers; we fill this as the peers are given to us by the testbed
  30. */
  31. static struct GNUNET_TESTBED_Peer *peers[NUM_PEERS];
  32. /**
  33. * Operation handle
  34. */
  35. static struct GNUNET_TESTBED_Operation *op;
  36. /**
  37. * Abort task identifier
  38. */
  39. static struct GNUNET_SCHEDULER_Task * abort_task;
  40. /**
  41. * Current peer id
  42. */
  43. static unsigned int peer_id;
  44. /**
  45. * Testing result
  46. */
  47. static int result;
  48. /**
  49. * Should we wait forever after testbed is initialized?
  50. */
  51. static int wait_forever;
  52. /**
  53. * Shutdown nicely
  54. *
  55. * @param cls NULL
  56. */
  57. static void
  58. do_shutdown (void *cls)
  59. {
  60. if (NULL != abort_task)
  61. GNUNET_SCHEDULER_cancel (abort_task);
  62. GNUNET_SCHEDULER_shutdown (); /* Stop scheduler to shutdown testbed run */
  63. }
  64. /**
  65. * abort task to run on test timed out
  66. *
  67. * @param cls NULL
  68. */
  69. static void
  70. do_abort (void *cls)
  71. {
  72. abort_task = NULL;
  73. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  74. "Test timed out -- Aborting\n");
  75. GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  76. }
  77. /**
  78. * Signature of a main function for a testcase.
  79. *
  80. * @param cls closure
  81. * @param h the run handle
  82. * @param num_peers number of peers in 'peers'
  83. * @param peers_ handle to peers run in the testbed
  84. * @param links_succeeded the number of overlay link connection attempts that
  85. * succeeded
  86. * @param links_failed the number of overlay link connection attempts that
  87. * failed
  88. */
  89. static void
  90. test_master (void *cls,
  91. struct GNUNET_TESTBED_RunHandle *h,
  92. unsigned int num_peers,
  93. struct GNUNET_TESTBED_Peer **peers_,
  94. unsigned int links_succeeded,
  95. unsigned int links_failed)
  96. {
  97. result = GNUNET_OK;
  98. if (GNUNET_YES == wait_forever)
  99. {
  100. if (NULL == abort_task)
  101. return; /* abort already scheduled */
  102. GNUNET_SCHEDULER_cancel (abort_task);
  103. abort_task = NULL;
  104. GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
  105. return;
  106. }
  107. GNUNET_assert (NULL != peers[0]);
  108. op = GNUNET_TESTBED_peer_stop (NULL, peers[0], NULL, NULL);
  109. GNUNET_assert (NULL != op);
  110. }
  111. /**
  112. * Controller event callback
  113. *
  114. * @param cls NULL
  115. * @param event the controller event
  116. */
  117. static void
  118. controller_event_cb (void *cls,
  119. const struct GNUNET_TESTBED_EventInformation *event)
  120. {
  121. switch (event->type)
  122. {
  123. case GNUNET_TESTBED_ET_PEER_START:
  124. GNUNET_assert (NULL == peers[peer_id]);
  125. GNUNET_assert (NULL != event->details.peer_start.peer);
  126. peers[peer_id++] = event->details.peer_start.peer;
  127. break;
  128. case GNUNET_TESTBED_ET_PEER_STOP:
  129. GNUNET_assert (NULL != op);
  130. GNUNET_TESTBED_operation_done (op);
  131. GNUNET_assert (peers[0] == event->details.peer_stop.peer);
  132. GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  133. break;
  134. default:
  135. GNUNET_assert (0);
  136. }
  137. }
  138. /**
  139. * Main run function.
  140. *
  141. * @param cls NULL
  142. * @param args arguments passed to GNUNET_PROGRAM_run
  143. * @param cfgfile the path to configuration file
  144. * @param cfg the configuration file handle
  145. */
  146. static void
  147. run (void *cls,
  148. char *const *args,
  149. const char *cfgfile,
  150. const struct GNUNET_CONFIGURATION_Handle *config)
  151. {
  152. uint64_t event_mask;
  153. event_mask = 0;
  154. event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_START);
  155. event_mask |= (1LL << GNUNET_TESTBED_ET_PEER_STOP);
  156. GNUNET_TESTBED_run (NULL, config, NUM_PEERS, event_mask,
  157. &controller_event_cb, NULL,
  158. &test_master, NULL);
  159. abort_task =
  160. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  161. (GNUNET_TIME_UNIT_SECONDS, 300),
  162. &do_abort,
  163. NULL);
  164. }
  165. /**
  166. * Main function
  167. */
  168. int
  169. main (int argc, char **argv)
  170. {
  171. char *argv2[] = {
  172. "test_testbed_api_testbed_run",
  173. "-c", NULL,
  174. NULL
  175. };
  176. struct GNUNET_GETOPT_CommandLineOption options[] = {
  177. GNUNET_GETOPT_OPTION_END
  178. };
  179. char *testname;
  180. char *config_filename;
  181. int ret;
  182. if (NULL == (testname = strrchr (argv[0], (int) '_')))
  183. {
  184. GNUNET_break (0);
  185. return 1;
  186. }
  187. testname++;
  188. testname = GNUNET_strdup (testname);
  189. #ifdef MINGW
  190. {
  191. char *period;
  192. /* check and remove .exe extension */
  193. period = strrchr (testname, (int) '.');
  194. if (NULL != period)
  195. *period = '\0';
  196. else
  197. GNUNET_break (0); /* Windows with no .exe? */
  198. }
  199. #endif
  200. if (0 == strcmp ("waitforever", testname))
  201. wait_forever = GNUNET_YES;
  202. if ( (GNUNET_YES != wait_forever) && (0 != strcmp ("run", testname)) )
  203. {
  204. GNUNET_asprintf (&config_filename, "test_testbed_api_testbed_run_%s.conf",
  205. testname);
  206. }
  207. else
  208. config_filename = GNUNET_strdup ("test_testbed_api.conf");
  209. GNUNET_free (testname);
  210. argv2[2] = config_filename;
  211. result = GNUNET_SYSERR;
  212. ret =
  213. GNUNET_PROGRAM_run ((sizeof (argv2) / sizeof (char *)) - 1, argv2,
  214. "test_testbed_api_testbed_run", "nohelp", options,
  215. &run, NULL);
  216. GNUNET_free (config_filename);
  217. if ((GNUNET_OK != ret) || (GNUNET_OK != result))
  218. return 1;
  219. return 0;
  220. }
  221. /* end of test_testbed_api_testbed_run.c */