test_testbed_api_peer_reconfiguration.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_peer_reconfiguration.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 1
  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. * Abort task identifier
  40. */
  41. static GNUNET_SCHEDULER_TaskIdentifier abort_task;
  42. /**
  43. * States in this test
  44. */
  45. enum {
  46. /**
  47. * Test has just been initialized
  48. */
  49. STATE_INIT,
  50. /**
  51. * Peers have been started
  52. */
  53. STATE_PEER_STARTED,
  54. /**
  55. * Peer has been reconfigured. Test completed successfully
  56. */
  57. STATE_PEER_RECONFIGURED
  58. } state;
  59. /**
  60. * Fail testcase
  61. */
  62. #define FAIL_TEST(cond, ret) do { \
  63. if (!(cond)) { \
  64. GNUNET_break(0); \
  65. if (GNUNET_SCHEDULER_NO_TASK != abort_task) \
  66. GNUNET_SCHEDULER_cancel (abort_task); \
  67. abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL); \
  68. ret; \
  69. } \
  70. } while (0)
  71. /**
  72. * Abort task
  73. *
  74. * @param cls NULL
  75. * @param tc scheduler task context
  76. */
  77. static void
  78. do_abort (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  79. {
  80. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Aborting\n");
  81. abort_task = GNUNET_SCHEDULER_NO_TASK;
  82. if (NULL != op)
  83. {
  84. GNUNET_TESTBED_operation_done (op);
  85. op = NULL;
  86. }
  87. GNUNET_SCHEDULER_shutdown();
  88. }
  89. /**
  90. * Signature of the event handler function called by the
  91. * respective event controller.
  92. *
  93. * @param cls closure
  94. * @param event information about the event
  95. */
  96. static void
  97. controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
  98. {
  99. if (STATE_PEER_STARTED != state)
  100. return;
  101. if (GNUNET_TESTBED_ET_OPERATION_FINISHED != event->type)
  102. {
  103. GNUNET_TESTBED_operation_done (op);
  104. op = NULL;
  105. FAIL_TEST (0, return);
  106. }
  107. if (NULL != event->details.operation_finished.emsg)
  108. {
  109. fprintf (stderr, "Operation failed: %s\n",
  110. event->details.operation_finished.emsg);
  111. GNUNET_TESTBED_operation_done (op);
  112. op = NULL;
  113. FAIL_TEST (0, return);
  114. }
  115. GNUNET_TESTBED_operation_done (op);
  116. state = STATE_PEER_RECONFIGURED;
  117. GNUNET_SCHEDULER_cancel (abort_task);
  118. abort_task = GNUNET_SCHEDULER_NO_TASK;
  119. GNUNET_SCHEDULER_shutdown ();
  120. }
  121. /**
  122. * Signature of a main function for a testcase.
  123. *
  124. * @param cls closure
  125. * @param h the run handle
  126. * @param num_peers number of peers in 'peers'
  127. * @param peers_ handle to peers run in the testbed
  128. * @param links_succeeded the number of overlay link connection attempts that
  129. * succeeded
  130. * @param links_failed the number of overlay link connection attempts that
  131. * failed
  132. */
  133. static void
  134. test_master (void *cls,
  135. struct GNUNET_TESTBED_RunHandle *h,
  136. unsigned int num_peers,
  137. struct GNUNET_TESTBED_Peer **peers_,
  138. unsigned int links_succeeded,
  139. unsigned int links_failed)
  140. {
  141. struct GNUNET_CONFIGURATION_Handle *cfg;
  142. FAIL_TEST (NUM_PEERS == num_peers, return);
  143. state = STATE_PEER_STARTED;
  144. peers = peers_;
  145. cfg = GNUNET_CONFIGURATION_create ();
  146. FAIL_TEST (GNUNET_OK == GNUNET_CONFIGURATION_load
  147. (cfg, "test_testbed_api_testbed_run_topologyrandom.conf"), return);
  148. op = GNUNET_TESTBED_peer_update_configuration (peers[0], cfg);
  149. GNUNET_CONFIGURATION_destroy (cfg);
  150. FAIL_TEST (NULL != op, return);
  151. abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  152. (GNUNET_TIME_UNIT_SECONDS, 30),
  153. &do_abort, NULL);
  154. }
  155. /**
  156. * Main function
  157. */
  158. int
  159. main (int argc, char **argv)
  160. {
  161. state = STATE_INIT;
  162. (void) GNUNET_TESTBED_test_run ("test_testbed_api_peer_reconfiguration",
  163. "test_testbed_api.conf",
  164. NUM_PEERS,
  165. 1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED,
  166. &controller_cb, NULL,
  167. &test_master, NULL);
  168. if (STATE_PEER_RECONFIGURED != state)
  169. return 1;
  170. return 0;
  171. }