test_tcp_server.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**************************************************************************/
  2. /* test_tcp_server.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #pragma once
  31. #include "core/io/stream_peer_tcp.h"
  32. #include "core/io/tcp_server.h"
  33. #include "tests/test_macros.h"
  34. #include <functional>
  35. namespace TestTCPServer {
  36. const int PORT = 12345;
  37. const IPAddress LOCALHOST("127.0.0.1");
  38. const uint32_t SLEEP_DURATION = 1000;
  39. const uint64_t MAX_WAIT_USEC = 2000000;
  40. void wait_for_condition(std::function<bool()> f_test) {
  41. const uint64_t time = OS::get_singleton()->get_ticks_usec();
  42. while (!f_test() && (OS::get_singleton()->get_ticks_usec() - time) < MAX_WAIT_USEC) {
  43. OS::get_singleton()->delay_usec(SLEEP_DURATION);
  44. }
  45. }
  46. Ref<TCPServer> create_server(const IPAddress &p_address, int p_port) {
  47. Ref<TCPServer> server;
  48. server.instantiate();
  49. REQUIRE_EQ(server->listen(PORT, LOCALHOST), Error::OK);
  50. REQUIRE(server->is_listening());
  51. CHECK_FALSE(server->is_connection_available());
  52. return server;
  53. }
  54. Ref<StreamPeerTCP> create_client(const IPAddress &p_address, int p_port) {
  55. Ref<StreamPeerTCP> client;
  56. client.instantiate();
  57. REQUIRE_EQ(client->connect_to_host(LOCALHOST, PORT), Error::OK);
  58. CHECK_EQ(client->get_connected_host(), LOCALHOST);
  59. CHECK_EQ(client->get_connected_port(), PORT);
  60. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTING);
  61. return client;
  62. }
  63. Ref<StreamPeerTCP> accept_connection(Ref<TCPServer> &p_server) {
  64. wait_for_condition([&]() {
  65. return p_server->is_connection_available();
  66. });
  67. REQUIRE(p_server->is_connection_available());
  68. Ref<StreamPeerTCP> client_from_server = p_server->take_connection();
  69. REQUIRE(client_from_server.is_valid());
  70. CHECK_EQ(client_from_server->get_connected_host(), LOCALHOST);
  71. CHECK_EQ(client_from_server->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  72. return client_from_server;
  73. }
  74. TEST_CASE("[TCPServer] Instantiation") {
  75. Ref<TCPServer> server;
  76. server.instantiate();
  77. REQUIRE(server.is_valid());
  78. CHECK_EQ(false, server->is_listening());
  79. }
  80. TEST_CASE("[TCPServer] Accept a connection and receive/send data") {
  81. Ref<TCPServer> server = create_server(LOCALHOST, PORT);
  82. Ref<StreamPeerTCP> client = create_client(LOCALHOST, PORT);
  83. Ref<StreamPeerTCP> client_from_server = accept_connection(server);
  84. wait_for_condition([&]() {
  85. return client->poll() != Error::OK || client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
  86. });
  87. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  88. // Sending data from client to server.
  89. const String hello_world = "Hello World!";
  90. client->put_string(hello_world);
  91. CHECK_EQ(client_from_server->get_string(), hello_world);
  92. // Sending data from server to client.
  93. const float pi = 3.1415;
  94. client_from_server->put_float(pi);
  95. CHECK_EQ(client->get_float(), pi);
  96. client->disconnect_from_host();
  97. server->stop();
  98. CHECK_FALSE(server->is_listening());
  99. }
  100. TEST_CASE("[TCPServer] Handle multiple clients at the same time") {
  101. Ref<TCPServer> server = create_server(LOCALHOST, PORT);
  102. Vector<Ref<StreamPeerTCP>> clients;
  103. for (int i = 0; i < 5; i++) {
  104. clients.push_back(create_client(LOCALHOST, PORT));
  105. }
  106. Vector<Ref<StreamPeerTCP>> clients_from_server;
  107. for (int i = 0; i < clients.size(); i++) {
  108. clients_from_server.push_back(accept_connection(server));
  109. }
  110. wait_for_condition([&]() {
  111. bool should_exit = true;
  112. for (Ref<StreamPeerTCP> &c : clients) {
  113. if (c->poll() != Error::OK) {
  114. return true;
  115. }
  116. StreamPeerTCP::Status status = c->get_status();
  117. if (status != StreamPeerTCP::STATUS_CONNECTED && status != StreamPeerTCP::STATUS_CONNECTING) {
  118. return true;
  119. }
  120. if (status != StreamPeerTCP::STATUS_CONNECTED) {
  121. should_exit = false;
  122. }
  123. }
  124. return should_exit;
  125. });
  126. for (Ref<StreamPeerTCP> &c : clients) {
  127. REQUIRE_EQ(c->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  128. }
  129. // Sending data from each client to server.
  130. for (int i = 0; i < clients.size(); i++) {
  131. String hello_client = "Hello " + itos(i);
  132. clients[i]->put_string(hello_client);
  133. CHECK_EQ(clients_from_server[i]->get_string(), hello_client);
  134. }
  135. for (Ref<StreamPeerTCP> &c : clients) {
  136. c->disconnect_from_host();
  137. }
  138. server->stop();
  139. }
  140. TEST_CASE("[TCPServer] When stopped shouldn't accept new connections") {
  141. Ref<TCPServer> server = create_server(LOCALHOST, PORT);
  142. Ref<StreamPeerTCP> client = create_client(LOCALHOST, PORT);
  143. Ref<StreamPeerTCP> client_from_server = accept_connection(server);
  144. wait_for_condition([&]() {
  145. return client->poll() != Error::OK || client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
  146. });
  147. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  148. // Sending data from client to server.
  149. const String hello_world = "Hello World!";
  150. client->put_string(hello_world);
  151. CHECK_EQ(client_from_server->get_string(), hello_world);
  152. client->disconnect_from_host();
  153. server->stop();
  154. CHECK_FALSE(server->is_listening());
  155. // Make sure the client times out in less than the wait time.
  156. int timeout = ProjectSettings::get_singleton()->get_setting("network/limits/tcp/connect_timeout_seconds");
  157. ProjectSettings::get_singleton()->set_setting("network/limits/tcp/connect_timeout_seconds", 1);
  158. Ref<StreamPeerTCP> new_client = create_client(LOCALHOST, PORT);
  159. // Reset the timeout setting.
  160. ProjectSettings::get_singleton()->set_setting("network/limits/tcp/connect_timeout_seconds", timeout);
  161. CHECK_FALSE(server->is_connection_available());
  162. wait_for_condition([&]() {
  163. return new_client->poll() != Error::OK || new_client->get_status() == StreamPeerTCP::STATUS_ERROR;
  164. });
  165. CHECK_FALSE(server->is_connection_available());
  166. CHECK_EQ(new_client->get_status(), StreamPeerTCP::STATUS_ERROR);
  167. new_client->disconnect_from_host();
  168. CHECK_EQ(new_client->get_status(), StreamPeerTCP::STATUS_NONE);
  169. }
  170. TEST_CASE("[TCPServer] Should disconnect client") {
  171. Ref<TCPServer> server = create_server(LOCALHOST, PORT);
  172. Ref<StreamPeerTCP> client = create_client(LOCALHOST, PORT);
  173. Ref<StreamPeerTCP> client_from_server = accept_connection(server);
  174. wait_for_condition([&]() {
  175. return client->poll() != Error::OK || client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
  176. });
  177. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  178. // Sending data from client to server.
  179. const String hello_world = "Hello World!";
  180. client->put_string(hello_world);
  181. CHECK_EQ(client_from_server->get_string(), hello_world);
  182. client_from_server->disconnect_from_host();
  183. server->stop();
  184. CHECK_FALSE(server->is_listening());
  185. // Wait for disconnection
  186. wait_for_condition([&]() {
  187. return client->poll() != Error::OK || client->get_status() == StreamPeerTCP::STATUS_NONE;
  188. });
  189. // Wait for disconnection
  190. wait_for_condition([&]() {
  191. return client_from_server->poll() != Error::OK || client_from_server->get_status() == StreamPeerTCP::STATUS_NONE;
  192. });
  193. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_NONE);
  194. CHECK_EQ(client_from_server->get_status(), StreamPeerTCP::STATUS_NONE);
  195. ERR_PRINT_OFF;
  196. CHECK_EQ(client->get_string(), String());
  197. CHECK_EQ(client_from_server->get_string(), String());
  198. ERR_PRINT_ON;
  199. }
  200. } // namespace TestTCPServer