test_testbed_api_peers_manage_services.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. This file is part of GNUnet
  3. (C) 2008--2013 Christian Grothoff (and other contributing authors)
  4. GNUnet is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published
  6. by the Free Software Foundation; either version 3, or (at your
  7. 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. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNUnet; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. */
  17. /**
  18. * @file testbed/test_testbed_api_peers_manage_services.c
  19. * @brief testcase for testing GNUNET_TESTBED_peer_manage_service()
  20. * implementation
  21. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  22. */
  23. #include "platform.h"
  24. #include "gnunet_util_lib.h"
  25. #include "gnunet_testbed_service.h"
  26. /**
  27. * Number of peers we want to start
  28. */
  29. #define NUM_PEERS 2
  30. /**
  31. * The array of peers; we get them from the testbed
  32. */
  33. static struct GNUNET_TESTBED_Peer **peers;
  34. /**
  35. * Operation handle
  36. */
  37. static struct GNUNET_TESTBED_Operation *op;
  38. /**
  39. * dummy pointer
  40. */
  41. static void *dummy_cls = (void *) 0xDEAD0001;
  42. /**
  43. * Abort task identifier
  44. */
  45. static GNUNET_SCHEDULER_TaskIdentifier abort_task;
  46. /**
  47. * States in this test
  48. */
  49. enum {
  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 (GNUNET_SCHEDULER_NO_TASK != 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. * @param tc scheduler task context
  88. */
  89. static void
  90. do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  91. {
  92. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Aborting\n");
  93. abort_task = GNUNET_SCHEDULER_NO_TASK;
  94. if (NULL != op)
  95. {
  96. GNUNET_TESTBED_operation_done (op);
  97. op = NULL;
  98. }
  99. GNUNET_SCHEDULER_shutdown();
  100. }
  101. /**
  102. * Callback to be called when an operation is completed
  103. *
  104. * @param cls the callback closure from functions generating an operation
  105. * @param op the operation that has been finished
  106. * @param emsg error message in case the operation has failed; will be NULL if
  107. * operation has executed successfully.
  108. */
  109. static void
  110. op_comp_cb (void *cls,
  111. struct GNUNET_TESTBED_Operation *op,
  112. const char *emsg)
  113. {
  114. FAIL_TEST (cls == dummy_cls, return);
  115. FAIL_TEST (NULL == emsg, return);
  116. GNUNET_TESTBED_operation_done (op);
  117. op = NULL;
  118. switch (state)
  119. {
  120. case STATE_PEERS_STARTED:
  121. state = STATE_SERVICE_DOWN;
  122. op = GNUNET_TESTBED_peer_manage_service (dummy_cls,
  123. peers[1],
  124. "topology",
  125. op_comp_cb,
  126. dummy_cls,
  127. 0);
  128. GNUNET_assert (NULL != op);
  129. break;
  130. case STATE_SERVICE_DOWN:
  131. state = STATE_SERVICE_UP;
  132. GNUNET_SCHEDULER_cancel (abort_task);
  133. abort_task = GNUNET_SCHEDULER_NO_TASK;
  134. state = STATE_OK;
  135. GNUNET_SCHEDULER_shutdown ();
  136. break;
  137. default:
  138. FAIL_TEST (0, return);
  139. }
  140. }
  141. /**
  142. * Signature of a main function for a testcase.
  143. *
  144. * @param cls closure
  145. * @param h the run handle
  146. * @param num_peers number of peers in 'peers'
  147. * @param peers_ handle to peers run in the testbed
  148. * @param links_succeeded the number of overlay link connection attempts that
  149. * succeeded
  150. * @param links_failed the number of overlay link connection attempts that
  151. * failed
  152. */
  153. static void
  154. test_master (void *cls,
  155. struct GNUNET_TESTBED_RunHandle *h,
  156. unsigned int num_peers,
  157. struct GNUNET_TESTBED_Peer **peers_,
  158. unsigned int links_succeeded,
  159. unsigned int links_failed)
  160. {
  161. FAIL_TEST (NUM_PEERS == num_peers, return);
  162. state = STATE_PEERS_STARTED;
  163. peers = peers_;
  164. op = GNUNET_TESTBED_peer_manage_service (dummy_cls,
  165. peers[1],
  166. "topology",
  167. op_comp_cb,
  168. dummy_cls,
  169. 1);
  170. FAIL_TEST (NULL != op, return);
  171. abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  172. (GNUNET_TIME_UNIT_MINUTES, 1),
  173. &do_abort, NULL);
  174. }
  175. /**
  176. * Main function
  177. */
  178. int
  179. main (int argc, char **argv)
  180. {
  181. state = STATE_INIT;
  182. (void) GNUNET_TESTBED_test_run ("test_testbed_api_peers_manage_services",
  183. "test_testbed_api.conf",
  184. NUM_PEERS,
  185. 1LL, NULL, NULL,
  186. &test_master, NULL);
  187. if (STATE_OK != state)
  188. return 1;
  189. return 0;
  190. }