test_transport_api_manipulation_recv_tcp.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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_manipulation_recv_tcp.c
  18. * @brief base test case for transport traffic manipulation implementation
  19. *
  20. * This test case will setup 2 peers and connect them, the first message
  21. * will be sent without manipulation, then a receive delay of 1 second will
  22. * be configured and 2 more message will be sent. Time will be measured
  23. *
  24. * In addition the distance on receiver side will be manipulated to be 10
  25. */
  26. #include "platform.h"
  27. #include "gnunet_transport_service.h"
  28. #include "transport-testing.h"
  29. /**
  30. * How long until we give up on transmitting the message?
  31. */
  32. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
  33. static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
  34. static int messages_recv;
  35. static struct GNUNET_TIME_Absolute start_normal;
  36. static struct GNUNET_TIME_Relative dur_normal;
  37. static struct GNUNET_TIME_Absolute start_delayed;
  38. static struct GNUNET_TIME_Relative dur_delayed;
  39. static void
  40. do_free (void *cls)
  41. {
  42. struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
  43. GNUNET_free (sc);
  44. }
  45. static void
  46. delayed_transmit (void *cls)
  47. {
  48. struct GNUNET_TRANSPORT_TESTING_SendClosure *sc = cls;
  49. start_delayed = GNUNET_TIME_absolute_get ();
  50. GNUNET_TRANSPORT_TESTING_large_send (sc);
  51. }
  52. static void
  53. sendtask (void *cls)
  54. {
  55. struct GNUNET_TRANSPORT_TESTING_SendClosure *sc;
  56. struct GNUNET_ATS_Properties prop;
  57. struct GNUNET_TIME_Relative delay;
  58. sc = GNUNET_new (struct GNUNET_TRANSPORT_TESTING_SendClosure);
  59. sc->num_messages = 1;
  60. sc->ccc = ccc;
  61. sc->cont = &do_free;
  62. sc->cont_cls = sc;
  63. if (0 == messages_recv)
  64. {
  65. start_normal = GNUNET_TIME_absolute_get ();
  66. }
  67. if (0 < messages_recv)
  68. {
  69. memset (&prop,
  70. 0,
  71. sizeof(prop));
  72. delay = GNUNET_TIME_UNIT_SECONDS;
  73. GNUNET_TRANSPORT_manipulation_set (ccc->p[1]->tmh,
  74. &ccc->p[0]->id,
  75. &prop,
  76. delay,
  77. GNUNET_TIME_UNIT_ZERO);
  78. /* wait 1s to allow manipulation to go into effect */
  79. if (1 == messages_recv)
  80. {
  81. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  82. &delayed_transmit,
  83. sc);
  84. return;
  85. }
  86. }
  87. GNUNET_TRANSPORT_TESTING_large_send (sc);
  88. }
  89. static void
  90. notify_receive (void *cls,
  91. struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
  92. const struct GNUNET_PeerIdentity *sender,
  93. const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
  94. {
  95. {
  96. char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
  97. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  98. "Peer %u (`%s') received message of type %d and size %u size from peer %s)!\n",
  99. receiver->no,
  100. ps,
  101. ntohs (message->header.type),
  102. ntohs (message->header.size),
  103. GNUNET_i2s (sender));
  104. GNUNET_free (ps);
  105. }
  106. if ((GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (message->header.type)) ||
  107. (GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE != ntohs (
  108. message->header.size)))
  109. {
  110. GNUNET_break (0);
  111. ccc->global_ret = GNUNET_SYSERR;
  112. GNUNET_SCHEDULER_shutdown ();
  113. return;
  114. }
  115. if (messages_recv <= 2)
  116. {
  117. /* Received non-delayed message */
  118. dur_normal = GNUNET_TIME_absolute_get_duration (start_normal);
  119. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  120. "Received non-delayed message %u after %s\n",
  121. messages_recv,
  122. GNUNET_STRINGS_relative_time_to_string (dur_normal,
  123. GNUNET_YES));
  124. GNUNET_SCHEDULER_add_now (&sendtask,
  125. NULL);
  126. messages_recv++;
  127. return;
  128. }
  129. /* Received manipulated message */
  130. dur_delayed = GNUNET_TIME_absolute_get_duration (start_delayed);
  131. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  132. "Received delayed message %u after %s\n",
  133. messages_recv,
  134. GNUNET_STRINGS_relative_time_to_string (dur_delayed,
  135. GNUNET_YES));
  136. if (dur_delayed.rel_value_us < GNUNET_TIME_UNIT_SECONDS.rel_value_us)
  137. {
  138. GNUNET_break (0);
  139. ccc->global_ret = GNUNET_SYSERR;
  140. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  141. "Delayed message was not delayed correctly: took only %s\n",
  142. GNUNET_STRINGS_relative_time_to_string (dur_delayed,
  143. GNUNET_YES));
  144. }
  145. else
  146. {
  147. ccc->global_ret = GNUNET_OK;
  148. }
  149. /* shutdown */
  150. GNUNET_SCHEDULER_shutdown ();
  151. }
  152. int
  153. main (int argc,
  154. char *argv[])
  155. {
  156. struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
  157. .connect_continuation = &sendtask,
  158. .config_file = "test_transport_api_data.conf",
  159. .rec = &notify_receive,
  160. .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
  161. .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
  162. .timeout = TIMEOUT,
  163. .global_ret = GNUNET_NO
  164. };
  165. ccc = &my_ccc;
  166. if (GNUNET_OK !=
  167. GNUNET_TRANSPORT_TESTING_main (2,
  168. &GNUNET_TRANSPORT_TESTING_connect_check,
  169. ccc))
  170. return 1;
  171. return 0;
  172. }
  173. /* end of test_transport_api_manipulation_recv_tcp.c */