test_transport_api_blacklisting.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2011, 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_blacklisting.c
  18. * @brief test for the blacklisting API
  19. * @author Matthias Wachs
  20. * @author Christian Grothoff
  21. */
  22. #include "platform.h"
  23. #include "gnunet_transport_service.h"
  24. #include "transport-testing.h"
  25. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
  26. static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
  27. static int connected;
  28. static int blacklist_request_p1;
  29. static int blacklist_request_p2;
  30. static struct GNUNET_TRANSPORT_Blacklist *blacklist_p1;
  31. static struct GNUNET_TRANSPORT_Blacklist *blacklist_p2;
  32. static struct GNUNET_SCHEDULER_Task *shutdown_task;
  33. static void
  34. end (void *cls)
  35. {
  36. shutdown_task = NULL;
  37. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  38. "Stopping\n");
  39. if ((GNUNET_YES == blacklist_request_p1) &&
  40. (GNUNET_YES == blacklist_request_p2) &&
  41. (GNUNET_NO == connected))
  42. {
  43. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  44. "Peers were never connected, success\n");
  45. ccc->global_ret = GNUNET_OK;
  46. }
  47. else
  48. {
  49. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  50. "Peers were not connected, fail\n");
  51. ccc->global_ret = GNUNET_SYSERR;
  52. }
  53. GNUNET_SCHEDULER_shutdown ();
  54. }
  55. static void
  56. custom_shutdown (void *cls)
  57. {
  58. if (NULL != shutdown_task)
  59. {
  60. GNUNET_SCHEDULER_cancel (shutdown_task);
  61. shutdown_task = NULL;
  62. }
  63. if (NULL != blacklist_p1)
  64. {
  65. GNUNET_TRANSPORT_blacklist_cancel (blacklist_p1);
  66. blacklist_p1 = NULL;
  67. }
  68. if (NULL != blacklist_p2)
  69. {
  70. GNUNET_TRANSPORT_blacklist_cancel (blacklist_p2);
  71. blacklist_p2 = NULL;
  72. }
  73. }
  74. static void
  75. notify_receive (void *cls,
  76. struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
  77. const struct GNUNET_PeerIdentity *sender,
  78. const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
  79. {
  80. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  81. "Unexpectedly even received the message despite blacklist\n");
  82. connected = GNUNET_YES;
  83. GNUNET_SCHEDULER_cancel (shutdown_task);
  84. end (NULL);
  85. }
  86. static void
  87. notify_connect (void *cls,
  88. struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
  89. const struct GNUNET_PeerIdentity *other)
  90. {
  91. GNUNET_TRANSPORT_TESTING_log_connect (cls,
  92. me,
  93. other);
  94. GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
  95. "Peers connected despite blacklist!\n");
  96. connected = GNUNET_YES; /* this test now failed */
  97. GNUNET_SCHEDULER_cancel (shutdown_task);
  98. end (NULL);
  99. }
  100. static int
  101. blacklist_cb (void *cls,
  102. const struct GNUNET_PeerIdentity *pid)
  103. {
  104. struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
  105. int res = GNUNET_SYSERR;
  106. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  107. "Peer %u: Blacklist request for peer `%s'\n",
  108. p->no,
  109. GNUNET_i2s (pid));
  110. if (p == ccc->p[0])
  111. {
  112. blacklist_request_p1 = GNUNET_YES;
  113. res = GNUNET_OK;
  114. }
  115. if (p == ccc->p[1])
  116. {
  117. blacklist_request_p2 = GNUNET_YES;
  118. res = GNUNET_SYSERR;
  119. }
  120. if ((GNUNET_YES == blacklist_request_p2) &&
  121. (GNUNET_YES == blacklist_request_p1) &&
  122. (NULL == shutdown_task))
  123. {
  124. shutdown_task
  125. = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply (
  126. GNUNET_TIME_UNIT_SECONDS, 3),
  127. &end,
  128. NULL);
  129. }
  130. return res;
  131. }
  132. static void
  133. start_blacklist (void *cls)
  134. {
  135. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  136. "Starting blacklists\n");
  137. blacklist_p1 = GNUNET_TRANSPORT_blacklist (ccc->p[0]->cfg,
  138. &blacklist_cb,
  139. ccc->p[0]);
  140. GNUNET_assert (NULL != blacklist_p1);
  141. blacklist_p2 = GNUNET_TRANSPORT_blacklist (ccc->p[1]->cfg,
  142. &blacklist_cb,
  143. ccc->p[1]);
  144. GNUNET_assert (NULL != blacklist_p2);
  145. }
  146. int
  147. main (int argc,
  148. char *argv[])
  149. {
  150. struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
  151. .num_messages = 1
  152. };
  153. struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
  154. .pre_connect_task = &start_blacklist,
  155. .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
  156. .connect_continuation_cls = &sc,
  157. .config_file = "test_transport_api_data.conf",
  158. .rec = &notify_receive,
  159. .nc = &notify_connect,
  160. .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
  161. .shutdown_task = &custom_shutdown,
  162. .timeout = TIMEOUT,
  163. .bi_directional = GNUNET_YES
  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 transport_api_blacklisting.c */