test_transport_api_timeout.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. This file is part of GNUnet.x
  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_timeout.c
  18. * @brief test case for transport plugin implementations complying timeout
  19. * settings
  20. *
  21. *
  22. * This test case serves ensures that no peer disconnect events occurs
  23. * while plugins are idle
  24. */
  25. #include "platform.h"
  26. #include "gnunet_transport_service.h"
  27. #include "transport-testing.h"
  28. /**
  29. * How long until we give up on transmitting the message?
  30. */
  31. #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
  32. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
  33. #define MTYPE 12345
  34. static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
  35. static struct GNUNET_TIME_Relative time_running;
  36. static struct GNUNET_SCHEDULER_Task *timer_task;
  37. static int shutdown_flag;
  38. static unsigned int disconnects;
  39. static void
  40. custom_shutdown (void *cls)
  41. {
  42. if (NULL != timer_task)
  43. {
  44. GNUNET_SCHEDULER_cancel (timer_task);
  45. timer_task = NULL;
  46. }
  47. if (0 == disconnects)
  48. {
  49. ccc->global_ret = GNUNET_OK;
  50. }
  51. else
  52. {
  53. ccc->global_ret = -GNUNET_SYSERR;
  54. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  55. "Fail! Had %u disconnects while waiting %s\n",
  56. disconnects,
  57. GNUNET_STRINGS_relative_time_to_string (WAIT,
  58. GNUNET_YES));
  59. }
  60. }
  61. static void
  62. notify_receive (void *cls,
  63. struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
  64. const struct GNUNET_PeerIdentity *sender,
  65. const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
  66. {
  67. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  68. "Received message of type %d from peer %s!\n",
  69. ntohs (message->header.type),
  70. GNUNET_i2s (sender));
  71. }
  72. static void
  73. notify_disconnect (void *cls,
  74. struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
  75. const struct GNUNET_PeerIdentity *other)
  76. {
  77. GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
  78. me,
  79. other);
  80. if (shutdown_flag != GNUNET_YES)
  81. {
  82. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  83. "FAIL! Peer `%s' disconnected during waiting period!\n",
  84. GNUNET_i2s (other));
  85. disconnects++;
  86. }
  87. }
  88. static void
  89. timer (void *cls)
  90. {
  91. static unsigned int percentage;
  92. timer_task = NULL;
  93. percentage += 10;
  94. time_running = GNUNET_TIME_relative_add (time_running,
  95. GNUNET_TIME_relative_divide (WAIT,
  96. 10));
  97. if (time_running.rel_value_us ==
  98. GNUNET_TIME_relative_max (time_running, WAIT).rel_value_us)
  99. {
  100. fprintf (stderr, "%s", "100%%\n");
  101. shutdown_flag = GNUNET_YES;
  102. GNUNET_SCHEDULER_shutdown ();
  103. }
  104. else
  105. {
  106. fprintf (stderr,
  107. "%u%%..",
  108. percentage);
  109. timer_task =
  110. GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide (WAIT, 10),
  111. &timer,
  112. NULL);
  113. }
  114. }
  115. int
  116. main (int argc,
  117. char *argv[])
  118. {
  119. struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
  120. .connect_continuation = &timer,
  121. .config_file = "test_transport_api_data.conf",
  122. .rec = &notify_receive,
  123. .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
  124. .nd = &notify_disconnect,
  125. .shutdown_task = &custom_shutdown,
  126. .timeout = TIMEOUT
  127. };
  128. ccc = &my_ccc;
  129. if (GNUNET_OK !=
  130. GNUNET_TRANSPORT_TESTING_main (2,
  131. &GNUNET_TRANSPORT_TESTING_connect_check,
  132. ccc))
  133. return 1;
  134. return 0;
  135. }
  136. /* end of test_transport_api_timeout.c*/