test_socket.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "test.h"
  17. #include "log.h"
  18. #include "settings.h"
  19. #include "network/socket.h"
  20. class TestSocket : public TestBase {
  21. public:
  22. TestSocket()
  23. {
  24. if (INTERNET_SIMULATOR == false)
  25. TestManager::registerTestModule(this);
  26. }
  27. const char *getName() { return "TestSocket"; }
  28. void runTests(IGameDef *gamedef);
  29. void testIPv4Socket();
  30. void testIPv6Socket();
  31. static const int port = 30003;
  32. };
  33. static TestSocket g_test_instance;
  34. void TestSocket::runTests(IGameDef *gamedef)
  35. {
  36. TEST(testIPv4Socket);
  37. if (g_settings->getBool("enable_ipv6"))
  38. TEST(testIPv6Socket);
  39. }
  40. ////////////////////////////////////////////////////////////////////////////////
  41. void TestSocket::testIPv4Socket()
  42. {
  43. Address address(0, 0, 0, 0, port);
  44. Address bind_addr(0, 0, 0, 0, port);
  45. /*
  46. * Try to use the bind_address for servers with no localhost address
  47. * For example: FreeBSD jails
  48. */
  49. std::string bind_str = g_settings->get("bind_address");
  50. try {
  51. bind_addr.Resolve(bind_str.c_str());
  52. if (!bind_addr.isIPv6()) {
  53. address = bind_addr;
  54. }
  55. } catch (ResolveError &e) {
  56. }
  57. UDPSocket socket(false);
  58. socket.Bind(address);
  59. const char sendbuffer[] = "hello world!";
  60. /*
  61. * If there is a bind address, use it.
  62. * It's useful in container environments
  63. */
  64. if (address != Address(0, 0, 0, 0, port))
  65. socket.Send(address, sendbuffer, sizeof(sendbuffer));
  66. else
  67. socket.Send(Address(127, 0, 0, 1, port), sendbuffer, sizeof(sendbuffer));
  68. sleep_ms(50);
  69. char rcvbuffer[256] = { 0 };
  70. Address sender;
  71. for (;;) {
  72. if (socket.Receive(sender, rcvbuffer, sizeof(rcvbuffer)) < 0)
  73. break;
  74. }
  75. //FIXME: This fails on some systems
  76. UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);
  77. if (address != Address(0, 0, 0, 0, port)) {
  78. UASSERT(sender.getAddress().sin_addr.s_addr ==
  79. address.getAddress().sin_addr.s_addr);
  80. } else {
  81. UASSERT(sender.getAddress().sin_addr.s_addr ==
  82. Address(127, 0, 0, 1, 0).getAddress().sin_addr.s_addr);
  83. }
  84. }
  85. void TestSocket::testIPv6Socket()
  86. {
  87. Address address6((IPv6AddressBytes *)NULL, port);
  88. UDPSocket socket6;
  89. if (!socket6.init(true, true)) {
  90. /* Note: Failing to create an IPv6 socket is not technically an
  91. error because the OS may not support IPv6 or it may
  92. have been disabled. IPv6 is not /required/ by
  93. minetest and therefore this should not cause the unit
  94. test to fail
  95. */
  96. dstream << "WARNING: IPv6 socket creation failed (unit test)"
  97. << std::endl;
  98. return;
  99. }
  100. const char sendbuffer[] = "hello world!";
  101. IPv6AddressBytes bytes;
  102. bytes.bytes[15] = 1;
  103. socket6.Bind(address6);
  104. try {
  105. socket6.Send(Address(&bytes, port), sendbuffer, sizeof(sendbuffer));
  106. sleep_ms(50);
  107. char rcvbuffer[256] = { 0 };
  108. Address sender;
  109. for(;;) {
  110. if (socket6.Receive(sender, rcvbuffer, sizeof(rcvbuffer)) < 0)
  111. break;
  112. }
  113. //FIXME: This fails on some systems
  114. UASSERT(strncmp(sendbuffer, rcvbuffer, sizeof(sendbuffer)) == 0);
  115. UASSERT(memcmp(sender.getAddress6().sin6_addr.s6_addr,
  116. Address(&bytes, 0).getAddress6().sin6_addr.s6_addr, 16) == 0);
  117. } catch (SendFailedException &e) {
  118. errorstream << "IPv6 support enabled but not available!"
  119. << std::endl;
  120. }
  121. }