test_transport_api2.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.c
  18. * @brief base test case for transport implementations
  19. * @author Christian Grothoff
  20. *
  21. * This test case serves as a base for tcp, udp, and udp-nat
  22. * transport test cases. Based on the executable being run
  23. * the correct test case will be performed. Conservation of
  24. * C code apparently.
  25. */
  26. #include "platform.h"
  27. //#include "gnunet_transport_service.h"
  28. #include "transport-testing2.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 void
  35. notify_receive (void *cls,
  36. struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
  37. const struct GNUNET_PeerIdentity *sender,
  38. const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
  39. {
  40. {
  41. char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
  42. GNUNET_log (GNUNET_ERROR_TYPE_INFO,
  43. "Peer %u (`%s') received message of type %d and size %u size from peer %s!\n",
  44. receiver->no,
  45. ps,
  46. ntohs (message->header.type),
  47. ntohs (message->header.size),
  48. GNUNET_i2s (sender));
  49. GNUNET_free (ps);
  50. }
  51. if ((GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE == ntohs (message->header.type)) &&
  52. (GNUNET_TRANSPORT_TESTING_LARGE_MESSAGE_SIZE == ntohs (
  53. message->header.size)))
  54. {
  55. ccc->global_ret = GNUNET_OK;
  56. GNUNET_SCHEDULER_shutdown ();
  57. }
  58. else
  59. {
  60. GNUNET_break (0);
  61. ccc->global_ret = GNUNET_SYSERR;
  62. GNUNET_SCHEDULER_shutdown ();
  63. }
  64. }
  65. /**
  66. * Runs the test.
  67. *
  68. * @param argv the argv argument from main()
  69. * @param bi_directional should we try to establish connections
  70. * in both directions simultaneously?
  71. */
  72. static int
  73. test (char *argv[],
  74. int bi_directional)
  75. {
  76. struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
  77. .num_messages = 1
  78. };
  79. struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
  80. .connect_continuation = &GNUNET_TRANSPORT_TESTING_large_send,
  81. .connect_continuation_cls = &sc,
  82. .config_file = "test_transport_api_data.conf",
  83. .rec = &notify_receive,
  84. .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
  85. .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
  86. .timeout = TIMEOUT,
  87. .bi_directional = bi_directional
  88. };
  89. ccc = &my_ccc;
  90. sc.ccc = ccc;
  91. if (GNUNET_OK !=
  92. GNUNET_TRANSPORT_TESTING_main (2,
  93. &GNUNET_TRANSPORT_TESTING_connect_check,
  94. ccc))
  95. return 1;
  96. return 0;
  97. }
  98. int
  99. main (int argc,
  100. char *argv[])
  101. {
  102. if ((0 != test (argv,
  103. GNUNET_NO)) ||
  104. (0 != test (argv,
  105. GNUNET_YES)))
  106. return 1;
  107. return 0;
  108. }
  109. /* end of test_transport_api.c */