test_testbed_api_barriers.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_barriers.c
  18. * @brief testcase binary for testing testbed barriers API
  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. #include "test_testbed_api_barriers.h"
  25. /**
  26. * logging short hand
  27. */
  28. #define LOG(type,...) \
  29. GNUNET_log (type, __VA_ARGS__);
  30. /**
  31. * Number of peers we start in this test case
  32. */
  33. #define NUM_PEERS 3
  34. /**
  35. * Our barrier
  36. */
  37. struct GNUNET_TESTBED_Barrier *barrier;
  38. /**
  39. * Identifier for the shutdown task
  40. */
  41. static struct GNUNET_SCHEDULER_Task *timeout_task;
  42. /**
  43. * Result of this test case
  44. */
  45. static int result;
  46. /**
  47. * Handle SIGINT and SIGTERM
  48. */
  49. static void
  50. shutdown_handler(void *cls)
  51. {
  52. if (NULL != timeout_task)
  53. {
  54. GNUNET_SCHEDULER_cancel(timeout_task);
  55. timeout_task = NULL;
  56. }
  57. }
  58. /**
  59. * Shutdown this test case when it takes too long
  60. *
  61. * @param cls NULL
  62. */
  63. static void
  64. do_timeout (void *cls)
  65. {
  66. timeout_task = NULL;
  67. if (barrier != NULL)
  68. GNUNET_TESTBED_barrier_cancel (barrier);
  69. GNUNET_SCHEDULER_shutdown ();
  70. }
  71. /**
  72. * Functions of this type are to be given as callback argument to
  73. * GNUNET_TESTBED_barrier_init(). The callback will be called when status
  74. * information is available for the barrier.
  75. *
  76. * @param cls the closure given to GNUNET_TESTBED_barrier_init()
  77. * @param name the name of the barrier
  78. * @param barrier the barrier handle
  79. * @param status status of the barrier; #GNUNET_OK if the barrier is crossed;
  80. * #GNUNET_SYSERR upon error
  81. * @param emsg if the status were to be #GNUNET_SYSERR, this parameter has the
  82. * error messsage
  83. */
  84. static void
  85. barrier_cb (void *cls,
  86. const char *name,
  87. struct GNUNET_TESTBED_Barrier *_barrier,
  88. enum GNUNET_TESTBED_BarrierStatus status,
  89. const char *emsg)
  90. {
  91. static enum GNUNET_TESTBED_BarrierStatus old_status;
  92. GNUNET_assert (NULL == cls);
  93. GNUNET_assert (_barrier == barrier);
  94. switch (status)
  95. {
  96. case GNUNET_TESTBED_BARRIERSTATUS_INITIALISED:
  97. LOG (GNUNET_ERROR_TYPE_INFO,
  98. "Barrier initialised\n");
  99. old_status = status;
  100. return;
  101. case GNUNET_TESTBED_BARRIERSTATUS_ERROR:
  102. LOG (GNUNET_ERROR_TYPE_ERROR,
  103. "Barrier initialisation failed: %s",
  104. (NULL == emsg) ? "unknown reason" : emsg);
  105. break;
  106. case GNUNET_TESTBED_BARRIERSTATUS_CROSSED:
  107. LOG (GNUNET_ERROR_TYPE_INFO,
  108. "Barrier crossed\n");
  109. if (old_status == GNUNET_TESTBED_BARRIERSTATUS_INITIALISED)
  110. result = GNUNET_OK;
  111. break;
  112. default:
  113. GNUNET_assert (0);
  114. return;
  115. }
  116. barrier = NULL;
  117. GNUNET_SCHEDULER_shutdown ();
  118. }
  119. /**
  120. * Signature of a main function for a testcase.
  121. *
  122. * @param cls closure
  123. * @param h the run handle
  124. * @param num_peers number of peers in 'peers'
  125. * @param peers_ handle to peers run in the testbed
  126. * @param links_succeeded the number of overlay link connection attempts that
  127. * succeeded
  128. * @param links_failed the number of overlay link connection attempts that
  129. * failed
  130. */
  131. static void
  132. test_master (void *cls,
  133. struct GNUNET_TESTBED_RunHandle *h,
  134. unsigned int num_peers,
  135. struct GNUNET_TESTBED_Peer **peers_,
  136. unsigned int links_succeeded,
  137. unsigned int links_failed)
  138. {
  139. struct GNUNET_TESTBED_Controller *c;
  140. GNUNET_assert (NULL == cls);
  141. if (NULL == peers_)
  142. {
  143. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  144. "Failing test due to timeout\n");
  145. return;
  146. }
  147. GNUNET_assert (NUM_PEERS == num_peers);
  148. c = GNUNET_TESTBED_run_get_controller_handle (h);
  149. barrier = GNUNET_TESTBED_barrier_init (c,
  150. TEST_BARRIER_NAME,
  151. 100,
  152. &barrier_cb,
  153. NULL);
  154. timeout_task =
  155. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  156. (GNUNET_TIME_UNIT_SECONDS,
  157. 10 * (NUM_PEERS + 1)),
  158. &do_timeout, NULL);
  159. GNUNET_SCHEDULER_add_shutdown(&shutdown_handler, NULL);
  160. }
  161. #ifndef PATH_MAX
  162. /**
  163. * Assumed maximum path length (for the log file name).
  164. */
  165. #define PATH_MAX 4096
  166. #endif
  167. /**
  168. * Main function
  169. */
  170. int
  171. main (int argc, char **argv)
  172. {
  173. struct GNUNET_CONFIGURATION_Handle *cfg;
  174. char pwd[PATH_MAX];
  175. char *binary;
  176. uint64_t event_mask;
  177. result = GNUNET_SYSERR;
  178. event_mask = 0;
  179. cfg = GNUNET_CONFIGURATION_create ();
  180. GNUNET_assert (GNUNET_YES ==
  181. GNUNET_CONFIGURATION_parse (cfg,
  182. "test_testbed_api_barriers.conf.in"));
  183. if (NULL == getcwd (pwd, PATH_MAX))
  184. return 1;
  185. GNUNET_assert (0 < GNUNET_asprintf (&binary, "%s/%s", pwd,
  186. "gnunet-service-test-barriers"));
  187. GNUNET_CONFIGURATION_set_value_string (cfg, "test-barriers","BINARY", binary);
  188. GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write
  189. (cfg, "test_testbed_api_barriers.conf"));
  190. GNUNET_CONFIGURATION_destroy (cfg);
  191. cfg = NULL;
  192. GNUNET_free (binary);
  193. binary = NULL;
  194. (void) GNUNET_TESTBED_test_run ("test_testbed_api_barriers",
  195. "test_testbed_api_barriers.conf", NUM_PEERS,
  196. event_mask, NULL, NULL,
  197. &test_master, NULL);
  198. (void) unlink ("test_testbed_api_barriers.conf");
  199. if (GNUNET_OK != result)
  200. return 1;
  201. return 0;
  202. }