test_transport_api_limited_sockets.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010 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_limited_sockets.c
  18. * @brief base test case for transport implementations
  19. *
  20. * This test case serves as a base for tcp, udp, and udp-nat
  21. * transport test cases. Based on the executable being run
  22. * the correct test case will be performed. Conservation of
  23. * C code apparently.
  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 TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 300)
  32. #define MAX_FILES 50
  33. #if HAVE_SETRLIMIT
  34. static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
  35. static void
  36. notify_receive (void *cls,
  37. struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
  38. const struct GNUNET_PeerIdentity *sender,
  39. const struct GNUNET_TRANSPORT_TESTING_TestMessage *message)
  40. {
  41. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  42. "Received message of type %d from peer %s!\n",
  43. ntohs (message->header.type),
  44. GNUNET_i2s (sender));
  45. if ((GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE ==
  46. ntohs (message->header.type)) &&
  47. (sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage) ==
  48. ntohs (message->header.size)))
  49. {
  50. ccc->global_ret = GNUNET_OK;
  51. }
  52. else
  53. {
  54. GNUNET_break (0);
  55. }
  56. GNUNET_SCHEDULER_shutdown ();
  57. }
  58. int
  59. main (int argc, char *argv[])
  60. {
  61. struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
  62. .num_messages = 1
  63. };
  64. struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
  65. .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
  66. .connect_continuation_cls = &sc,
  67. .config_file = "test_transport_api_data.conf",
  68. .rec = &notify_receive,
  69. .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
  70. .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
  71. .timeout = TIMEOUT,
  72. .global_ret = GNUNET_SYSERR
  73. };
  74. struct rlimit r_file_old;
  75. struct rlimit r_file_new;
  76. int res;
  77. sc.ccc = &my_ccc;
  78. res = getrlimit (RLIMIT_NOFILE,
  79. &r_file_old);
  80. r_file_new.rlim_cur = MAX_FILES;
  81. r_file_new.rlim_max = r_file_old.rlim_max;
  82. res = setrlimit (RLIMIT_NOFILE,
  83. &r_file_new);
  84. if (0 != res)
  85. {
  86. fprintf (stderr,
  87. "Setting limit failed: %s\n",
  88. strerror (errno));
  89. return 77;
  90. }
  91. ccc = &my_ccc;
  92. ccc->global_ret = GNUNET_SYSERR;
  93. if (GNUNET_OK !=
  94. GNUNET_TRANSPORT_TESTING_main (2,
  95. &GNUNET_TRANSPORT_TESTING_connect_check,
  96. ccc))
  97. return 1;
  98. return 0;
  99. }
  100. #else
  101. /* cannot setrlimit */
  102. int
  103. main (int argc, char *argv[])
  104. {
  105. fprintf (stderr,
  106. "Cannot run test on this system\n");
  107. return 77;
  108. }
  109. #endif
  110. /* end of test_transport_api_limited_sockets.c */