test_transport_api_manipulation_cfg.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_cfg.c
  18. * @brief base test case for transport traffic manipulation implementation
  19. * based on cfg
  20. *
  21. * Peer 1 has inbound and outbound delay of 100ms
  22. * Peer 2 has no inbound and outbound delay
  23. *
  24. * We send a request from P1 to P2 and expect delay of >= TEST_DELAY us
  25. * Then we send response from P2 to P1 and expect delay of >= TEST_DELAY us
  26. */
  27. #include "platform.h"
  28. #include "gnunet_transport_service.h"
  29. #include "transport-testing.h"
  30. /**
  31. * How long until we give up on transmitting the message?
  32. */
  33. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
  34. #define TEST_MESSAGE_SIZE 2600
  35. #define TEST_RESPONSE_MESSAGE_TYPE
  36. /**
  37. * Test delay, in microseconds.
  38. */
  39. #define TEST_DELAY 100 * 1000LL
  40. static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
  41. static struct GNUNET_TIME_Absolute start_request;
  42. static struct GNUNET_TIME_Absolute start_response;
  43. static void
  44. sendtask_response_task (void *cls)
  45. {
  46. int ret;
  47. start_response = GNUNET_TIME_absolute_get ();
  48. ret = GNUNET_TRANSPORT_TESTING_send (ccc->p[1],
  49. ccc->p[0],
  50. GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2,
  51. TEST_MESSAGE_SIZE,
  52. 1,
  53. NULL,
  54. NULL);
  55. if (GNUNET_NO == ret)
  56. {
  57. GNUNET_break (0);
  58. GNUNET_SCHEDULER_shutdown ();
  59. return;
  60. }
  61. GNUNET_assert (GNUNET_SYSERR != ret);
  62. }
  63. static void
  64. notify_receive (void *cls,
  65. struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
  66. const struct GNUNET_PeerIdentity *sender,
  67. const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
  68. {
  69. struct GNUNET_TIME_Relative duration;
  70. {
  71. char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
  72. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  73. "Peer %u (`%s') received message of type %d and size %u size from peer %s)!\n",
  74. receiver->no,
  75. ps,
  76. ntohs (message->header.type),
  77. ntohs (message->header.size),
  78. GNUNET_i2s (sender));
  79. GNUNET_free (ps);
  80. }
  81. switch (ntohs (message->header.type))
  82. {
  83. case GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE:
  84. duration = GNUNET_TIME_absolute_get_difference (start_request,
  85. GNUNET_TIME_absolute_get ());
  86. if (duration.rel_value_us >= TEST_DELAY)
  87. {
  88. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  89. "Request message was delayed for %s\n",
  90. GNUNET_STRINGS_relative_time_to_string (duration,
  91. GNUNET_YES));
  92. }
  93. else
  94. {
  95. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  96. "Request message was delayed for unexpected duration %s\n",
  97. GNUNET_STRINGS_relative_time_to_string (duration,
  98. GNUNET_YES));
  99. ccc->global_ret = GNUNET_SYSERR;
  100. GNUNET_SCHEDULER_shutdown ();
  101. }
  102. /* Send response */
  103. GNUNET_SCHEDULER_add_now (&sendtask_response_task,
  104. NULL);
  105. return;
  106. case GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE2:
  107. duration = GNUNET_TIME_absolute_get_difference (start_response,
  108. GNUNET_TIME_absolute_get ());
  109. if (duration.rel_value_us >= TEST_DELAY)
  110. {
  111. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  112. "Response message was delayed for %s\n",
  113. GNUNET_STRINGS_relative_time_to_string (duration,
  114. GNUNET_YES));
  115. ccc->global_ret = GNUNET_OK;
  116. }
  117. else
  118. {
  119. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  120. "Response message was delayed for unexpected duration %s\n",
  121. GNUNET_STRINGS_relative_time_to_string (duration,
  122. GNUNET_YES));
  123. ccc->global_ret = GNUNET_SYSERR;
  124. }
  125. GNUNET_SCHEDULER_shutdown ();
  126. break;
  127. default:
  128. GNUNET_break (0);
  129. break;
  130. }
  131. }
  132. int
  133. main (int argc,
  134. char *argv[])
  135. {
  136. struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
  137. .num_messages = 1
  138. };
  139. struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
  140. .connect_continuation = &GNUNET_TRANSPORT_TESTING_large_send,
  141. .connect_continuation_cls = &sc,
  142. .config_file = "test_transport_api_data.conf",
  143. .rec = &notify_receive,
  144. .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
  145. .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
  146. .timeout = TIMEOUT
  147. };
  148. ccc = &my_ccc;
  149. sc.ccc = ccc;
  150. start_request = GNUNET_TIME_absolute_get ();
  151. if (GNUNET_OK !=
  152. GNUNET_TRANSPORT_TESTING_main (2,
  153. &GNUNET_TRANSPORT_TESTING_connect_check,
  154. ccc))
  155. return 1;
  156. return 0;
  157. }
  158. /* end of test_transport_api_manipulation_cfg.c */