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