test_testbed_api_peers_manage_services.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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_peers_manage_services.c
  18. * @brief testcase for testing GNUNET_TESTBED_peer_manage_service()
  19. * implementation
  20. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  21. */
  22. #include "platform.h"
  23. #include "gnunet_util_lib.h"
  24. #include "gnunet_testbed_service.h"
  25. /**
  26. * Number of peers we want to start
  27. */
  28. #define NUM_PEERS 2
  29. /**
  30. * The array of peers; we get them from the testbed
  31. */
  32. static struct GNUNET_TESTBED_Peer **peers;
  33. /**
  34. * Operation handle
  35. */
  36. static struct GNUNET_TESTBED_Operation *op;
  37. /**
  38. * dummy pointer
  39. */
  40. static void *dummy_cls = (void *) 0xDEAD0001;
  41. /**
  42. * Abort task identifier
  43. */
  44. static struct GNUNET_SCHEDULER_Task *abort_task;
  45. /**
  46. * States in this test
  47. */
  48. enum
  49. {
  50. /**
  51. * Test has just been initialized
  52. */
  53. STATE_INIT,
  54. /**
  55. * Peers have been started
  56. */
  57. STATE_PEERS_STARTED,
  58. /**
  59. * statistics service went down
  60. */
  61. STATE_SERVICE_DOWN,
  62. /**
  63. * statistics service went up
  64. */
  65. STATE_SERVICE_UP,
  66. /**
  67. * Testing completed successfully
  68. */
  69. STATE_OK
  70. } state;
  71. /**
  72. * Fail testcase
  73. */
  74. #define FAIL_TEST(cond, ret) do { \
  75. if (! (cond)) { \
  76. GNUNET_break (0); \
  77. if (NULL != abort_task) \
  78. GNUNET_SCHEDULER_cancel (abort_task); \
  79. abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL); \
  80. ret; \
  81. } \
  82. } while (0)
  83. /**
  84. * Abort task
  85. *
  86. * @param cls NULL
  87. */
  88. static void
  89. do_abort (void *cls)
  90. {
  91. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Aborting\n");
  92. abort_task = NULL;
  93. if (NULL != op)
  94. {
  95. GNUNET_TESTBED_operation_done (op);
  96. op = NULL;
  97. }
  98. GNUNET_SCHEDULER_shutdown ();
  99. }
  100. /**
  101. * Callback to be called when an operation is completed
  102. *
  103. * @param cls the callback closure from functions generating an operation
  104. * @param op the operation that has been finished
  105. * @param emsg error message in case the operation has failed; will be NULL if
  106. * operation has executed successfully.
  107. */
  108. static void
  109. op_comp_cb (void *cls,
  110. struct GNUNET_TESTBED_Operation *op,
  111. const char *emsg)
  112. {
  113. FAIL_TEST (cls == dummy_cls, return );
  114. FAIL_TEST (NULL == emsg, return );
  115. GNUNET_TESTBED_operation_done (op);
  116. op = NULL;
  117. switch (state)
  118. {
  119. case STATE_PEERS_STARTED:
  120. state = STATE_SERVICE_DOWN;
  121. op = GNUNET_TESTBED_peer_manage_service (dummy_cls,
  122. peers[1],
  123. "topology",
  124. op_comp_cb,
  125. dummy_cls,
  126. 0);
  127. GNUNET_assert (NULL != op);
  128. break;
  129. case STATE_SERVICE_DOWN:
  130. state = STATE_SERVICE_UP;
  131. GNUNET_SCHEDULER_cancel (abort_task);
  132. abort_task = NULL;
  133. state = STATE_OK;
  134. GNUNET_SCHEDULER_shutdown ();
  135. break;
  136. default:
  137. FAIL_TEST (0, return );
  138. }
  139. }
  140. /**
  141. * Signature of a main function for a testcase.
  142. *
  143. * @param cls closure
  144. * @param h the run handle
  145. * @param num_peers number of peers in 'peers'
  146. * @param peers_ handle to peers run in the testbed
  147. * @param links_succeeded the number of overlay link connection attempts that
  148. * succeeded
  149. * @param links_failed the number of overlay link connection attempts that
  150. * failed
  151. */
  152. static void
  153. test_master (void *cls,
  154. struct GNUNET_TESTBED_RunHandle *h,
  155. unsigned int num_peers,
  156. struct GNUNET_TESTBED_Peer **peers_,
  157. unsigned int links_succeeded,
  158. unsigned int links_failed)
  159. {
  160. FAIL_TEST (NUM_PEERS == num_peers, return );
  161. state = STATE_PEERS_STARTED;
  162. peers = peers_;
  163. op = GNUNET_TESTBED_peer_manage_service (dummy_cls,
  164. peers[1],
  165. "topology",
  166. op_comp_cb,
  167. dummy_cls,
  168. 1);
  169. FAIL_TEST (NULL != op, return );
  170. abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  171. (GNUNET_TIME_UNIT_MINUTES, 1),
  172. &do_abort, NULL);
  173. }
  174. /**
  175. * Main function
  176. */
  177. int
  178. main (int argc, char **argv)
  179. {
  180. state = STATE_INIT;
  181. (void) GNUNET_TESTBED_test_run ("test_testbed_api_peers_manage_services",
  182. "test_testbed_api.conf",
  183. NUM_PEERS,
  184. 1LL, NULL, NULL,
  185. &test_master, NULL);
  186. if (STATE_OK != state)
  187. return 1;
  188. return 0;
  189. }