test_transport_api_restart_reconnect.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2015, 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_restart_reconnect.c
  18. * @brief base test case for transport implementations
  19. *
  20. * This test case starts 2 peers, connects and exchanges a message.
  21. * Then, 1 or 2 peers are restarted and it is tested if peers reconnect.
  22. * How many peers are restarted is determined by the name of the binary.
  23. */
  24. #include "platform.h"
  25. #include "gnunet_transport_service.h"
  26. #include "transport-testing.h"
  27. /**
  28. * How long until we give up on transmitting the message?
  29. */
  30. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
  31. static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
  32. static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
  33. static int p1_connected;
  34. static int p2_connected;
  35. static int restarted;
  36. static void
  37. custom_shutdown (void *cls)
  38. {
  39. if (NULL != ats_sh)
  40. {
  41. GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
  42. ats_sh = NULL;
  43. }
  44. }
  45. static void
  46. restart_cb (void *cls)
  47. {
  48. static unsigned int c;
  49. struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
  50. c++;
  51. if ((2 != c) &&
  52. (NULL != strstr (ccc->test_name,
  53. "2peers")))
  54. return;
  55. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  56. "Restarted peer %u (`%s'), issuing reconnect\n",
  57. p->no,
  58. GNUNET_i2s (&p->id));
  59. ats_sh = GNUNET_ATS_connectivity_suggest (p->ats,
  60. &ccc->p[1]->id,
  61. 1);
  62. }
  63. static void
  64. restart (struct GNUNET_TRANSPORT_TESTING_PeerContext *p)
  65. {
  66. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  67. "Restarting peer %u (`%s')\n",
  68. p->no,
  69. GNUNET_i2s (&p->id));
  70. GNUNET_assert (GNUNET_OK ==
  71. GNUNET_TRANSPORT_TESTING_restart_peer (p,
  72. &restart_cb,
  73. p));
  74. }
  75. static void
  76. notify_receive (void *cls,
  77. struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
  78. const struct GNUNET_PeerIdentity *sender,
  79. const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
  80. {
  81. {
  82. char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
  83. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  84. "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
  85. receiver->no,
  86. ps,
  87. ntohs (message->header.type),
  88. ntohs (message->header.size),
  89. GNUNET_i2s (sender));
  90. GNUNET_free (ps);
  91. }
  92. if ((GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE == ntohs (message->header.type)) &&
  93. (sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage) == ntohs (
  94. message->header.size)))
  95. {
  96. if (GNUNET_NO == restarted)
  97. {
  98. restarted = GNUNET_YES;
  99. fprintf (stderr, "TN: %s\n", ccc->test_name);
  100. restart (ccc->p[0]);
  101. if (NULL != strstr (ccc->test_name,
  102. "2peers"))
  103. restart (ccc->p[1]);
  104. return;
  105. }
  106. else
  107. {
  108. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  109. "Restarted peers connected and message was sent, stopping test...\n");
  110. ccc->global_ret = GNUNET_OK;
  111. GNUNET_SCHEDULER_shutdown ();
  112. }
  113. }
  114. else
  115. {
  116. GNUNET_break (0);
  117. ccc->global_ret = GNUNET_SYSERR;
  118. GNUNET_SCHEDULER_shutdown ();
  119. }
  120. }
  121. static void
  122. notify_connect (void *cls,
  123. struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
  124. const struct GNUNET_PeerIdentity *other)
  125. {
  126. static struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
  127. .num_messages = 1
  128. };
  129. sc.ccc = ccc;
  130. GNUNET_TRANSPORT_TESTING_log_connect (cls,
  131. me,
  132. other);
  133. if (me == ccc->p[0])
  134. p1_connected = GNUNET_YES;
  135. if (me == ccc->p[1])
  136. p2_connected = GNUNET_YES;
  137. if ((GNUNET_YES == restarted) &&
  138. (GNUNET_YES == p1_connected) &&
  139. (GNUNET_YES == p2_connected))
  140. {
  141. /* Peer was restarted and we received 3 connect messages (2 from first connect, 1 from reconnect) */
  142. GNUNET_SCHEDULER_add_now (&GNUNET_TRANSPORT_TESTING_simple_send,
  143. &sc);
  144. }
  145. }
  146. static void
  147. notify_disconnect (void *cls,
  148. struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
  149. const struct GNUNET_PeerIdentity *other)
  150. {
  151. GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
  152. me,
  153. other);
  154. if (me == ccc->p[0])
  155. p1_connected = GNUNET_NO;
  156. if (me == ccc->p[1])
  157. p2_connected = GNUNET_NO;
  158. }
  159. int
  160. main (int argc,
  161. char *argv[])
  162. {
  163. struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
  164. .num_messages = 1
  165. };
  166. struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
  167. .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
  168. .connect_continuation_cls = &sc,
  169. .config_file = "test_transport_api_data.conf",
  170. .rec = &notify_receive,
  171. .nc = &notify_connect,
  172. .nd = &notify_disconnect,
  173. .shutdown_task = &custom_shutdown,
  174. .timeout = TIMEOUT
  175. };
  176. ccc = &my_ccc;
  177. sc.ccc = ccc;
  178. if (GNUNET_OK !=
  179. GNUNET_TRANSPORT_TESTING_main (2,
  180. &GNUNET_TRANSPORT_TESTING_connect_check,
  181. ccc))
  182. return 1;
  183. return 0;
  184. }
  185. /* end of test_transport_api_restart_1peer.c */