test_transport_api_manipulation_send_tcp.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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_send_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 send delay of 1 second will
  22. * be configured and 1 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, 30)
  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_TIME_Relative delay;
  57. struct GNUNET_ATS_Properties prop;
  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 (1 == messages_recv)
  68. {
  69. memset (&prop,
  70. 0,
  71. sizeof (prop));
  72. delay = GNUNET_TIME_UNIT_SECONDS;
  73. GNUNET_TRANSPORT_manipulation_set (ccc->p[0]->tmh,
  74. &ccc->p[1]->id,
  75. &prop,
  76. GNUNET_TIME_UNIT_ZERO,
  77. delay);
  78. /* wait 1s to allow manipulation to go into effect */
  79. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
  80. &delayed_transmit,
  81. sc);
  82. return;
  83. }
  84. GNUNET_TRANSPORT_TESTING_large_send (sc);
  85. }
  86. static void
  87. notify_receive (void *cls,
  88. struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
  89. const struct GNUNET_PeerIdentity *sender,
  90. const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
  91. {
  92. {
  93. char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
  94. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  95. "Peer %u (`%s') received message of type %d and size %u size from peer %s)!\n",
  96. receiver->no,
  97. ps,
  98. ntohs (message->header.type),
  99. ntohs (message->header.size),
  100. GNUNET_i2s (sender));
  101. GNUNET_free (ps);
  102. }
  103. if ( (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (message->header.type)) ||
  104. (GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE != ntohs (message->header.size)) )
  105. {
  106. GNUNET_break (0);
  107. ccc->global_ret = GNUNET_SYSERR;
  108. GNUNET_SCHEDULER_shutdown ();
  109. return;
  110. }
  111. if (0 == messages_recv)
  112. {
  113. /* Received non-delayed message */
  114. dur_normal = GNUNET_TIME_absolute_get_duration (start_normal);
  115. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  116. "Received non-delayed message %u after %s\n",
  117. messages_recv,
  118. GNUNET_STRINGS_relative_time_to_string (dur_normal,
  119. GNUNET_YES));
  120. GNUNET_SCHEDULER_add_now (&sendtask,
  121. NULL);
  122. messages_recv++;
  123. return;
  124. }
  125. /* Received manipulated message */
  126. dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
  127. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  128. "Received delayed message %u after %s\n",
  129. messages_recv,
  130. GNUNET_STRINGS_relative_time_to_string (dur_delayed,
  131. GNUNET_YES));
  132. if (dur_delayed.rel_value_us < GNUNET_TIME_UNIT_SECONDS.rel_value_us)
  133. {
  134. GNUNET_break (0);
  135. ccc->global_ret = GNUNET_SYSERR;
  136. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  137. "Delayed message was not delayed correctly: took only %s\n",
  138. GNUNET_STRINGS_relative_time_to_string (dur_delayed,
  139. GNUNET_YES));
  140. }
  141. else
  142. {
  143. ccc->global_ret = GNUNET_OK;
  144. }
  145. GNUNET_SCHEDULER_shutdown ();
  146. }
  147. int
  148. main (int argc,
  149. char *argv[])
  150. {
  151. struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
  152. .connect_continuation = &sendtask,
  153. .config_file = "test_transport_api_data.conf",
  154. .rec = &notify_receive,
  155. .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
  156. .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
  157. .timeout = TIMEOUT,
  158. .global_ret = GNUNET_NO
  159. };
  160. ccc = &my_ccc;
  161. if (GNUNET_OK !=
  162. GNUNET_TRANSPORT_TESTING_main (2,
  163. &GNUNET_TRANSPORT_TESTING_connect_check,
  164. ccc))
  165. return 1;
  166. return 0;
  167. }
  168. /* end of test_transport_api_manipulation_send_tcp.c */