test_testbed_api_peers_manage_services.c 5.6 KB

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