test_testbed_api_test.c 7.0 KB

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