test_testbed_api_topology.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_topology.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. * Number of peers we want to start
  27. */
  28. #define NUM_PEERS 10
  29. /**
  30. * Array of peers
  31. */
  32. static struct GNUNET_TESTBED_Peer **peers;
  33. /**
  34. * Operation handle
  35. */
  36. static struct GNUNET_TESTBED_Operation *op;
  37. /**
  38. * Shutdown task
  39. */
  40. static GNUNET_SCHEDULER_TaskIdentifier shutdown_task;
  41. /**
  42. * Testing result
  43. */
  44. static int result;
  45. /**
  46. * Counter for counting overlay connections
  47. */
  48. static unsigned int overlay_connects;
  49. /**
  50. * Shutdown nicely
  51. *
  52. * @param cls NULL
  53. * @param tc the task context
  54. */
  55. static void
  56. do_shutdown (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  57. {
  58. shutdown_task = GNUNET_SCHEDULER_NO_TASK;
  59. if (NULL != op)
  60. {
  61. GNUNET_TESTBED_operation_done (op);
  62. op = NULL;
  63. }
  64. GNUNET_SCHEDULER_shutdown ();
  65. }
  66. /**
  67. * Controller event callback
  68. *
  69. * @param cls NULL
  70. * @param event the controller event
  71. */
  72. static void
  73. controller_event_cb (void *cls,
  74. const struct GNUNET_TESTBED_EventInformation *event)
  75. {
  76. switch (event->type)
  77. {
  78. case GNUNET_TESTBED_ET_CONNECT:
  79. overlay_connects++;
  80. if ((NUM_PEERS) == overlay_connects)
  81. {
  82. result = GNUNET_OK;
  83. GNUNET_SCHEDULER_cancel (shutdown_task);
  84. shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  85. }
  86. break;
  87. case GNUNET_TESTBED_ET_OPERATION_FINISHED:
  88. GNUNET_assert (NULL != event->details.operation_finished.emsg);
  89. break;
  90. default:
  91. GNUNET_break (0);
  92. if ((GNUNET_TESTBED_ET_OPERATION_FINISHED == event->type) &&
  93. (NULL != event->details.operation_finished.emsg))
  94. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  95. "An operation failed with error: %s\n",
  96. event->details.operation_finished.emsg);
  97. result = GNUNET_SYSERR;
  98. GNUNET_SCHEDULER_cancel (shutdown_task);
  99. shutdown_task = GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
  100. }
  101. }
  102. /**
  103. * Signature of a main function for a testcase.
  104. *
  105. * @param cls closure
  106. * @param h the run handle
  107. * @param num_peers number of peers in 'peers'
  108. * @param peers_ handle to peers run in the testbed
  109. * @param links_succeeded the number of overlay link connection attempts that
  110. * succeeded
  111. * @param links_failed the number of overlay link connection attempts that
  112. * failed
  113. */
  114. static void
  115. test_master (void *cls,
  116. struct GNUNET_TESTBED_RunHandle *h,
  117. unsigned int num_peers,
  118. struct GNUNET_TESTBED_Peer **peers_,
  119. unsigned int links_succeeded,
  120. unsigned int links_failed)
  121. {
  122. unsigned int peer;
  123. GNUNET_assert (NULL == cls);
  124. if (NULL == peers_)
  125. {
  126. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failing test due to timeout\n");
  127. return;
  128. }
  129. GNUNET_assert (NUM_PEERS == num_peers);
  130. for (peer = 0; peer < num_peers; peer++)
  131. GNUNET_assert (NULL != peers_[peer]);
  132. peers = peers_;
  133. overlay_connects = 0;
  134. op = GNUNET_TESTBED_overlay_configure_topology (NULL, NUM_PEERS, peers, NULL,
  135. NULL,
  136. NULL,
  137. GNUNET_TESTBED_TOPOLOGY_ERDOS_RENYI,
  138. NUM_PEERS,
  139. GNUNET_TESTBED_TOPOLOGY_OPTION_END);
  140. GNUNET_assert (NULL != op);
  141. shutdown_task =
  142. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  143. (GNUNET_TIME_UNIT_SECONDS, 300),
  144. do_shutdown, NULL);
  145. }
  146. /**
  147. * Main function
  148. */
  149. int
  150. main (int argc, char **argv)
  151. {
  152. uint64_t event_mask;
  153. result = GNUNET_SYSERR;
  154. event_mask = 0;
  155. event_mask |= (1LL << GNUNET_TESTBED_ET_CONNECT);
  156. event_mask |= (1LL << GNUNET_TESTBED_ET_OPERATION_FINISHED);
  157. (void) GNUNET_TESTBED_test_run ("test_testbed_api_test",
  158. "test_testbed_api.conf", NUM_PEERS,
  159. event_mask, &controller_event_cb, NULL,
  160. &test_master, NULL);
  161. if (GNUNET_OK != result)
  162. return 1;
  163. return 0;
  164. }
  165. /* end of test_testbed_api_topology.c */