test_testbed_api_statistics.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_statistics.c
  18. * @brief testcase for testing GNUNET_TESTBED_get_statistics() implementation
  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. * Number of peers we want to start
  26. */
  27. #define NUM_PEERS 5
  28. /**
  29. * The array of peers; we get them from the testbed
  30. */
  31. static struct GNUNET_TESTBED_Peer **peers;
  32. /**
  33. * Operation handle
  34. */
  35. static struct GNUNET_TESTBED_Operation *op;
  36. /**
  37. * dummy pointer
  38. */
  39. static void *dummy_cls = (void *) 0xDEAD0001;
  40. /**
  41. * Abort task identifier
  42. */
  43. static struct GNUNET_SCHEDULER_Task * abort_task;
  44. /**
  45. * Global testing result
  46. */
  47. static int result;
  48. /**
  49. * The peers we have seen in the statistics iterator
  50. */
  51. static struct GNUNET_TESTBED_Peer **seen_peers;
  52. /**
  53. * Number of peers in the above array
  54. */
  55. static unsigned int num_seen_peers;
  56. /**
  57. * Fail testcase
  58. */
  59. #define FAIL_TEST(cond, ret) do { \
  60. if (!(cond)) { \
  61. GNUNET_break(0); \
  62. if (NULL != abort_task) \
  63. GNUNET_SCHEDULER_cancel (abort_task); \
  64. abort_task = GNUNET_SCHEDULER_add_now (&do_abort, NULL); \
  65. ret; \
  66. } \
  67. } while (0)
  68. /**
  69. * Abort task
  70. *
  71. * @param cls NULL
  72. */
  73. static void
  74. do_abort (void *cls)
  75. {
  76. GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Test timed out -- Aborting\n");
  77. abort_task = NULL;
  78. if (NULL != op)
  79. {
  80. GNUNET_TESTBED_operation_done (op);
  81. op = NULL;
  82. }
  83. result = GNUNET_SYSERR;
  84. }
  85. /**
  86. * Callback function to process statistic values from all peers.
  87. *
  88. * @param cls closure
  89. * @param peer the peer the statistic belong to
  90. * @param subsystem name of subsystem that created the statistic
  91. * @param name the name of the datum
  92. * @param value the current value
  93. * @param is_persistent GNUNET_YES if the value is persistent, GNUNET_NO if not
  94. * @return GNUNET_OK to continue, GNUNET_SYSERR to abort iteration
  95. */
  96. static int
  97. stats_iterator (void *cls,
  98. const struct GNUNET_TESTBED_Peer *peer,
  99. const char *subsystem, const char *name, uint64_t value,
  100. int is_persistent)
  101. {
  102. unsigned int cnt;
  103. FAIL_TEST (cls == dummy_cls, return GNUNET_SYSERR);
  104. for (cnt = 0; cnt < num_seen_peers; cnt++)
  105. FAIL_TEST (peer != seen_peers[cnt], return GNUNET_SYSERR);
  106. FAIL_TEST (NULL != subsystem, return GNUNET_SYSERR);
  107. FAIL_TEST (NULL != name, return GNUNET_SYSERR);
  108. GNUNET_array_append (seen_peers, num_seen_peers,
  109. (struct GNUNET_TESTBED_Peer *) peer);
  110. return GNUNET_SYSERR;
  111. }
  112. /**
  113. * Callback to be called when an operation is completed
  114. *
  115. * @param cls the callback closure from functions generating an operation
  116. * @param op the operation that has been finished
  117. * @param emsg error message in case the operation has failed; will be NULL if
  118. * operation has executed successfully.
  119. */
  120. static void
  121. op_comp_cb (void *cls,
  122. struct GNUNET_TESTBED_Operation *op,
  123. const char *emsg)
  124. {
  125. FAIL_TEST (cls == dummy_cls, return);
  126. result = GNUNET_OK;
  127. GNUNET_TESTBED_operation_done (op);
  128. op = NULL;
  129. GNUNET_SCHEDULER_cancel (abort_task);
  130. GNUNET_SCHEDULER_shutdown ();
  131. }
  132. /**
  133. * Signature of a main function for a testcase.
  134. *
  135. * @param cls closure
  136. * @param h the run handle
  137. * @param num_peers number of peers in 'peers'
  138. * @param peers_ handle to peers run in the testbed
  139. * @param links_succeeded the number of overlay link connection attempts that
  140. * succeeded
  141. * @param links_failed the number of overlay link connection attempts that
  142. * failed
  143. */
  144. static void
  145. test_master (void *cls,
  146. struct GNUNET_TESTBED_RunHandle *h,
  147. unsigned int num_peers,
  148. struct GNUNET_TESTBED_Peer **peers_,
  149. unsigned int links_succeeded,
  150. unsigned int links_failed)
  151. {
  152. FAIL_TEST (NUM_PEERS == num_peers, return);
  153. peers = peers_;
  154. op = GNUNET_TESTBED_get_statistics (num_peers, peers,
  155. NULL, NULL,
  156. &stats_iterator,
  157. &op_comp_cb,
  158. dummy_cls);
  159. abort_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
  160. (GNUNET_TIME_UNIT_MINUTES, 1),
  161. &do_abort, NULL);
  162. }
  163. /**
  164. * Main function
  165. */
  166. int
  167. main (int argc, char **argv)
  168. {
  169. (void) GNUNET_TESTBED_test_run ("test_testbed_api_statistics",
  170. "test_testbed_api_statistics.conf",
  171. NUM_PEERS,
  172. 1LL, NULL, NULL,
  173. &test_master, NULL);
  174. GNUNET_free_non_null (seen_peers);
  175. if (GNUNET_OK != result)
  176. return 1;
  177. return 0;
  178. }