test_transport_api_monitor_peers.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2016 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 transport/test_transport_api_monitor_peers.c
  18. * @brief base test case for transport peer monitor API
  19. */
  20. #include "platform.h"
  21. #include "gnunet_transport_service.h"
  22. #include "transport-testing.h"
  23. /**
  24. * How long until we give up on transmitting the message?
  25. */
  26. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
  27. /**
  28. * How long until we give up on transmitting the message?
  29. */
  30. #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply ( \
  31. GNUNET_TIME_UNIT_SECONDS, 30)
  32. #define TEST_MESSAGE_SIZE 2600
  33. #define TEST_MESSAGE_TYPE 12345
  34. static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
  35. static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p1;
  36. static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p2;
  37. static int p1_c;
  38. static int p2_c;
  39. static int p1_c_notify;
  40. static int p2_c_notify;
  41. static void
  42. custom_shutdown (void *cls)
  43. {
  44. if (NULL != pmc_p1)
  45. {
  46. GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
  47. pmc_p1 = NULL;
  48. }
  49. if (NULL != pmc_p2)
  50. {
  51. GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
  52. pmc_p2 = NULL;
  53. }
  54. }
  55. static void
  56. notify_receive (void *cls,
  57. struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
  58. const struct GNUNET_PeerIdentity *sender,
  59. const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
  60. {
  61. char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
  62. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  63. "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
  64. receiver->no,
  65. ps,
  66. ntohs (message->header.type),
  67. ntohs (message->header.size),
  68. GNUNET_i2s (sender));
  69. GNUNET_free (ps);
  70. }
  71. static void
  72. sendtask (void *cls)
  73. {
  74. /* intentionally empty */
  75. }
  76. static void
  77. check_done ()
  78. {
  79. if ((GNUNET_YES == p1_c) &&
  80. (GNUNET_YES == p2_c) &&
  81. p1_c_notify &&
  82. p2_c_notify)
  83. {
  84. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  85. "Both peers state to be connected\n");
  86. ccc->global_ret = GNUNET_OK;
  87. GNUNET_SCHEDULER_shutdown ();
  88. }
  89. }
  90. static void
  91. notify_connect (void *cls,
  92. struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
  93. const struct GNUNET_PeerIdentity *other)
  94. {
  95. GNUNET_TRANSPORT_TESTING_log_connect (cls,
  96. me,
  97. other);
  98. if (0 == memcmp (other, &ccc->p[0]->id, sizeof(struct GNUNET_PeerIdentity)))
  99. {
  100. p1_c_notify = GNUNET_YES;
  101. }
  102. if (0 == memcmp (other, &ccc->p[1]->id, sizeof(struct GNUNET_PeerIdentity)))
  103. {
  104. p2_c_notify = GNUNET_YES;
  105. }
  106. check_done ();
  107. }
  108. static void
  109. monitor1_cb (void *cls,
  110. const struct GNUNET_PeerIdentity *peer,
  111. const struct GNUNET_HELLO_Address *address,
  112. enum GNUNET_TRANSPORT_PeerState state,
  113. struct GNUNET_TIME_Absolute state_timeout)
  114. {
  115. if ((NULL == address) || (NULL == ccc->p[0]))
  116. return;
  117. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  118. "Monitor 1: %s %s %s\n",
  119. GNUNET_i2s (&address->peer),
  120. GNUNET_TRANSPORT_ps2s (state),
  121. GNUNET_STRINGS_absolute_time_to_string (state_timeout));
  122. if ((0 == memcmp (&address->peer, &ccc->p[1]->id, sizeof(ccc->p[1]->id))) &&
  123. (GNUNET_YES == GNUNET_TRANSPORT_is_connected (state)) &&
  124. (GNUNET_NO == p1_c))
  125. {
  126. p1_c = GNUNET_YES;
  127. check_done ();
  128. }
  129. }
  130. static void
  131. monitor2_cb (void *cls,
  132. const struct GNUNET_PeerIdentity *peer,
  133. const struct GNUNET_HELLO_Address *address,
  134. enum GNUNET_TRANSPORT_PeerState state,
  135. struct GNUNET_TIME_Absolute state_timeout)
  136. {
  137. if ((NULL == address) || (NULL == ccc->p[1]))
  138. return;
  139. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  140. "Monitor 2: %s %s %s\n",
  141. GNUNET_i2s (&address->peer),
  142. GNUNET_TRANSPORT_ps2s (state),
  143. GNUNET_STRINGS_absolute_time_to_string (state_timeout));
  144. if ((0 == memcmp (&address->peer, &ccc->p[0]->id, sizeof(ccc->p[0]->id))) &&
  145. (GNUNET_YES == GNUNET_TRANSPORT_is_connected (state)) &&
  146. (GNUNET_NO == p2_c))
  147. {
  148. p2_c = GNUNET_YES;
  149. check_done ();
  150. }
  151. }
  152. static void
  153. start_monitors (void *cls)
  154. {
  155. pmc_p1 = GNUNET_TRANSPORT_monitor_peers (ccc->p[0]->cfg,
  156. NULL,
  157. GNUNET_NO,
  158. &monitor1_cb,
  159. NULL);
  160. pmc_p2 = GNUNET_TRANSPORT_monitor_peers (ccc->p[1]->cfg,
  161. NULL,
  162. GNUNET_NO,
  163. &monitor2_cb,
  164. NULL);
  165. }
  166. int
  167. main (int argc, char *argv[])
  168. {
  169. struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
  170. .pre_connect_task = &start_monitors,
  171. .connect_continuation = &sendtask,
  172. .config_file = "test_transport_api_data.conf",
  173. .rec = &notify_receive,
  174. .nc = &notify_connect,
  175. .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
  176. .shutdown_task = &custom_shutdown,
  177. .timeout = TIMEOUT
  178. };
  179. ccc = &my_ccc;
  180. if (GNUNET_OK !=
  181. GNUNET_TRANSPORT_TESTING_main (2,
  182. &GNUNET_TRANSPORT_TESTING_connect_check,
  183. ccc))
  184. return 1;
  185. return 0;
  186. }
  187. /* end of test_transport_api_monitor_peers.c */