test_testbed_api_test.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 src/testbed/test_testbed_api_test.c
  18. * @brief testing cases for testing high level testbed api helper functions
  19. * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  20. */
  21. #include "platform.h"
  22. #include "gnunet_util_lib.h"
  23. #include "gnunet_testbed_service.h"
  24. /**
  25. * Generic logging shortcut
  26. */
  27. #define LOG(kind, ...) \
  28. GNUNET_log (kind, __VA_ARGS__)
  29. /**
  30. * Number of peers we want to start
  31. */
  32. #define NUM_PEERS 2
  33. /**
  34. * Array of peers
  35. */
  36. static struct GNUNET_TESTBED_Peer **peers;
  37. /**
  38. * Operation handle
  39. */
  40. static struct GNUNET_TESTBED_Operation *op;
  41. /**
  42. * Abort task identifier
  43. */
  44. static struct GNUNET_SCHEDULER_Task *abort_task;
  45. /**
  46. * shutdown task identifier
  47. */
  48. static struct GNUNET_SCHEDULER_Task *shutdown_task;
  49. /**
  50. * Testing result
  51. */
  52. static int result;
  53. /**
  54. * Shutdown nicely
  55. *
  56. * @param cls NULL
  57. */
  58. static void
  59. do_shutdown (void *cls)
  60. {
  61. shutdown_task = NULL;
  62. if (NULL != abort_task)
  63. GNUNET_SCHEDULER_cancel (abort_task);
  64. if (NULL != op)
  65. GNUNET_TESTBED_operation_done (op);
  66. GNUNET_SCHEDULER_shutdown ();
  67. }
  68. /**
  69. * shortcut to exit during failure
  70. */
  71. #define FAIL_TEST(cond) do { \
  72. if (! (cond)) { \
  73. GNUNET_break (0); \
  74. if (NULL != abort_task) \
  75. GNUNET_SCHEDULER_cancel (abort_task); \
  76. abort_task = NULL; \
  77. if (NULL == shutdown_task) \
  78. shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL); \
  79. return; \
  80. } \
  81. } while (0)
  82. /**
  83. * abort task to run on test timed out
  84. *
  85. * @param cls NULL
  86. */
  87. static void
  88. do_abort (void *cls)
  89. {
  90. LOG (GNUNET_ERROR_TYPE_WARNING, "Test timedout -- Aborting\n");
  91. abort_task = NULL;
  92. if (NULL != shutdown_task)
  93. GNUNET_SCHEDULER_cancel (shutdown_task);
  94. do_shutdown (cls);
  95. }
  96. /**
  97. * Callback to be called when the requested peer information is available
  98. *
  99. * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
  100. * @param op the operation this callback corresponds to
  101. * @param pinfo the result; will be NULL if the operation has failed
  102. * @param emsg error message if the operation has failed; will be NULL if the
  103. * operation is successfull
  104. */
  105. static void
  106. peerinfo_cb (void *cb_cls, struct GNUNET_TESTBED_Operation *op_,
  107. const struct GNUNET_TESTBED_PeerInformation *pinfo,
  108. const char *emsg)
  109. {
  110. FAIL_TEST (op == op_);
  111. FAIL_TEST (NULL == cb_cls);
  112. FAIL_TEST (NULL == emsg);
  113. FAIL_TEST (GNUNET_TESTBED_PIT_IDENTITY == pinfo->pit);
  114. FAIL_TEST (NULL != pinfo->result.id);
  115. GNUNET_TESTBED_operation_done (op);
  116. op = NULL;
  117. result = GNUNET_OK;
  118. shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  119. }
  120. /**
  121. * Callback to be called when an operation is completed
  122. *
  123. * @param cls the callback closure from functions generating an operation
  124. * @param op the operation that has been finished
  125. * @param emsg error message in case the operation has failed; will be NULL if
  126. * operation has executed successfully.
  127. */
  128. static void
  129. op_comp_cb (void *cls, struct GNUNET_TESTBED_Operation *op_, const char *emsg)
  130. {
  131. FAIL_TEST (NULL == cls);
  132. FAIL_TEST (op == op_);
  133. if (NULL != emsg)
  134. {
  135. LOG (GNUNET_ERROR_TYPE_WARNING, "%s\n", emsg);
  136. FAIL_TEST (0);
  137. }
  138. GNUNET_TESTBED_operation_done (op);
  139. op = GNUNET_TESTBED_peer_get_information (peers[0],
  140. GNUNET_TESTBED_PIT_IDENTITY,
  141. &peerinfo_cb, NULL);
  142. }
  143. /**
  144. * Controller event callback
  145. *
  146. * @param cls NULL
  147. * @param event the controller event
  148. */
  149. static void
  150. controller_event_cb (void *cls,
  151. const struct GNUNET_TESTBED_EventInformation *event)
  152. {
  153. switch (event->type)
  154. {
  155. case GNUNET_TESTBED_ET_CONNECT:
  156. FAIL_TEST (event->details.peer_connect.peer1 == peers[0]);
  157. FAIL_TEST (event->details.peer_connect.peer2 == peers[1]);
  158. break;
  159. default:
  160. FAIL_TEST (0);
  161. }
  162. }
  163. /**
  164. * Signature of a main function for a testcase.
  165. *
  166. * @param cls closure
  167. * @param h the run handle
  168. * @param num_peers number of peers in 'peers'
  169. * @param peers- handle to peers run in the testbed
  170. * @param links_succeeded the number of overlay link connection attempts that
  171. * succeeded
  172. * @param links_failed the number of overlay link connection attempts that
  173. * failed
  174. */
  175. static void
  176. test_master (void *cls,
  177. struct GNUNET_TESTBED_RunHandle *h,
  178. unsigned int num_peers,
  179. struct GNUNET_TESTBED_Peer **peers_,
  180. unsigned int links_succeeded,
  181. unsigned int links_failed)
  182. {
  183. unsigned int peer;
  184. FAIL_TEST (NULL == cls);
  185. FAIL_TEST (NUM_PEERS == num_peers);
  186. FAIL_TEST (NULL != peers_);
  187. for (peer = 0; peer < num_peers; peer++)
  188. FAIL_TEST (NULL != peers_[peer]);
  189. peers = peers_;
  190. op = GNUNET_TESTBED_overlay_connect (NULL, &op_comp_cb, NULL, peers[0],
  191. peers[1]);
  192. abort_task =
  193. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  194. (GNUNET_TIME_UNIT_MINUTES, 3), &do_abort,
  195. NULL);
  196. }
  197. /**
  198. * Main function
  199. */
  200. int
  201. main (int argc, char **argv)
  202. {
  203. uint64_t event_mask;
  204. result = GNUNET_SYSERR;
  205. event_mask = 0;
  206. event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
  207. event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
  208. (void) GNUNET_TESTBED_test_run ("test_testbed_api_test",
  209. "test_testbed_api.conf", NUM_PEERS,
  210. event_mask, &controller_event_cb, NULL,
  211. &test_master, NULL);
  212. if (GNUNET_OK != result)
  213. return 1;
  214. return 0;
  215. }
  216. /* end of test_testbed_api_test.c */