test_testbed_api_peer_reconfiguration.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_peer_reconfiguration.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 1
  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. * Abort task identifier
  39. */
  40. static struct GNUNET_SCHEDULER_Task *abort_task;
  41. /**
  42. * States in this test
  43. */
  44. enum
  45. {
  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 (NULL != 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. */
  76. static void
  77. do_abort (void *cls)
  78. {
  79. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Aborting\n");
  80. abort_task = NULL;
  81. if (NULL != op)
  82. {
  83. GNUNET_TESTBED_operation_done (op);
  84. op = NULL;
  85. }
  86. GNUNET_SCHEDULER_shutdown ();
  87. }
  88. /**
  89. * Signature of the event handler function called by the
  90. * respective event controller.
  91. *
  92. * @param cls closure
  93. * @param event information about the event
  94. */
  95. static void
  96. controller_cb (void *cls, const struct GNUNET_TESTBED_EventInformation *event)
  97. {
  98. if (STATE_PEER_STARTED != state)
  99. return;
  100. if (GNUNET_TESTBED_ET_OPERATION_FINISHED != event->type)
  101. {
  102. GNUNET_TESTBED_operation_done (op);
  103. op = NULL;
  104. FAIL_TEST (0, return );
  105. }
  106. if (NULL != event->details.operation_finished.emsg)
  107. {
  108. fprintf (stderr, "Operation failed: %s\n",
  109. event->details.operation_finished.emsg);
  110. GNUNET_TESTBED_operation_done (op);
  111. op = NULL;
  112. FAIL_TEST (0, return );
  113. }
  114. GNUNET_TESTBED_operation_done (op);
  115. state = STATE_PEER_RECONFIGURED;
  116. GNUNET_SCHEDULER_cancel (abort_task);
  117. abort_task = NULL;
  118. GNUNET_SCHEDULER_shutdown ();
  119. }
  120. /**
  121. * Signature of a main function for a testcase.
  122. *
  123. * @param cls closure
  124. * @param h the run handle
  125. * @param num_peers number of peers in 'peers'
  126. * @param peers_ handle to peers run in the testbed
  127. * @param links_succeeded the number of overlay link connection attempts that
  128. * succeeded
  129. * @param links_failed the number of overlay link connection attempts that
  130. * failed
  131. */
  132. static void
  133. test_master (void *cls,
  134. struct GNUNET_TESTBED_RunHandle *h,
  135. unsigned int num_peers,
  136. struct GNUNET_TESTBED_Peer **peers_,
  137. unsigned int links_succeeded,
  138. unsigned int links_failed)
  139. {
  140. struct GNUNET_CONFIGURATION_Handle *cfg;
  141. FAIL_TEST (NUM_PEERS == num_peers, return );
  142. state = STATE_PEER_STARTED;
  143. peers = peers_;
  144. cfg = GNUNET_CONFIGURATION_create ();
  145. FAIL_TEST (GNUNET_OK == GNUNET_CONFIGURATION_load
  146. (cfg, "test_testbed_api_testbed_run_topologyrandom.conf"),
  147. 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. }