test_tcp_server.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #ifndef TEST_TCP_SERVER_H
  31. #define TEST_TCP_SERVER_H
  32. #include "core/io/stream_peer_tcp.h"
  33. #include "core/io/tcp_server.h"
  34. #include "tests/test_macros.h"
  35. #include <functional>
  36. namespace TestTCPServer {
  37. const int PORT = 12345;
  38. const IPAddress LOCALHOST("127.0.0.1");
  39. const uint32_t SLEEP_DURATION = 1000;
  40. const uint64_t MAX_WAIT_USEC = 2000000;
  41. void wait_for_condition(std::function<bool()> f_test) {
  42. const uint64_t time = OS::get_singleton()->get_ticks_usec();
  43. while (!f_test() && (OS::get_singleton()->get_ticks_usec() - time) < MAX_WAIT_USEC) {
  44. OS::get_singleton()->delay_usec(SLEEP_DURATION);
  45. }
  46. }
  47. Ref<TCPServer> create_server(const IPAddress &p_address, int p_port) {
  48. Ref<TCPServer> server;
  49. server.instantiate();
  50. REQUIRE_EQ(server->listen(PORT, LOCALHOST), Error::OK);
  51. REQUIRE(server->is_listening());
  52. CHECK_FALSE(server->is_connection_available());
  53. return server;
  54. }
  55. Ref<StreamPeerTCP> create_client(const IPAddress &p_address, int p_port) {
  56. Ref<StreamPeerTCP> client;
  57. client.instantiate();
  58. REQUIRE_EQ(client->connect_to_host(LOCALHOST, PORT), Error::OK);
  59. CHECK_EQ(client->get_connected_host(), LOCALHOST);
  60. CHECK_EQ(client->get_connected_port(), PORT);
  61. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTING);
  62. return client;
  63. }
  64. Ref<StreamPeerTCP> accept_connection(Ref<TCPServer> &p_server) {
  65. wait_for_condition([&]() {
  66. return p_server->is_connection_available();
  67. });
  68. REQUIRE(p_server->is_connection_available());
  69. Ref<StreamPeerTCP> client_from_server = p_server->take_connection();
  70. REQUIRE(client_from_server.is_valid());
  71. CHECK_EQ(client_from_server->get_connected_host(), LOCALHOST);
  72. CHECK_EQ(client_from_server->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  73. return client_from_server;
  74. }
  75. TEST_CASE("[TCPServer] Instantiation") {
  76. Ref<TCPServer> server;
  77. server.instantiate();
  78. REQUIRE(server.is_valid());
  79. CHECK_EQ(false, server->is_listening());
  80. }
  81. TEST_CASE("[TCPServer] Accept a connection and receive/send data") {
  82. Ref<TCPServer> server = create_server(LOCALHOST, PORT);
  83. Ref<StreamPeerTCP> client = create_client(LOCALHOST, PORT);
  84. Ref<StreamPeerTCP> client_from_server = accept_connection(server);
  85. wait_for_condition([&]() {
  86. return client->poll() != Error::OK || client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
  87. });
  88. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  89. // Sending data from client to server.
  90. const String hello_world = "Hello World!";
  91. client->put_string(hello_world);
  92. CHECK_EQ(client_from_server->get_string(), hello_world);
  93. // Sending data from server to client.
  94. const float pi = 3.1415;
  95. client_from_server->put_float(pi);
  96. CHECK_EQ(client->get_float(), pi);
  97. client->disconnect_from_host();
  98. server->stop();
  99. CHECK_FALSE(server->is_listening());
  100. }
  101. TEST_CASE("[TCPServer] Handle multiple clients at the same time") {
  102. Ref<TCPServer> server = create_server(LOCALHOST, PORT);
  103. Vector<Ref<StreamPeerTCP>> clients;
  104. for (int i = 0; i < 5; i++) {
  105. clients.push_back(create_client(LOCALHOST, PORT));
  106. }
  107. Vector<Ref<StreamPeerTCP>> clients_from_server;
  108. for (int i = 0; i < clients.size(); i++) {
  109. clients_from_server.push_back(accept_connection(server));
  110. }
  111. wait_for_condition([&]() {
  112. bool should_exit = true;
  113. for (Ref<StreamPeerTCP> &c : clients) {
  114. if (c->poll() != Error::OK) {
  115. return true;
  116. }
  117. StreamPeerTCP::Status status = c->get_status();
  118. if (status != StreamPeerTCP::STATUS_CONNECTED && status != StreamPeerTCP::STATUS_CONNECTING) {
  119. return true;
  120. }
  121. if (status != StreamPeerTCP::STATUS_CONNECTED) {
  122. should_exit = false;
  123. }
  124. }
  125. return should_exit;
  126. });
  127. for (Ref<StreamPeerTCP> &c : clients) {
  128. REQUIRE_EQ(c->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  129. }
  130. // Sending data from each client to server.
  131. for (int i = 0; i < clients.size(); i++) {
  132. String hello_client = "Hello " + itos(i);
  133. clients[i]->put_string(hello_client);
  134. CHECK_EQ(clients_from_server[i]->get_string(), hello_client);
  135. }
  136. for (Ref<StreamPeerTCP> &c : clients) {
  137. c->disconnect_from_host();
  138. }
  139. server->stop();
  140. }
  141. TEST_CASE("[TCPServer] When stopped shouldn't accept new connections") {
  142. Ref<TCPServer> server = create_server(LOCALHOST, PORT);
  143. Ref<StreamPeerTCP> client = create_client(LOCALHOST, PORT);
  144. Ref<StreamPeerTCP> client_from_server = accept_connection(server);
  145. wait_for_condition([&]() {
  146. return client->poll() != Error::OK || client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
  147. });
  148. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  149. // Sending data from client to server.
  150. const String hello_world = "Hello World!";
  151. client->put_string(hello_world);
  152. CHECK_EQ(client_from_server->get_string(), hello_world);
  153. client->disconnect_from_host();
  154. server->stop();
  155. CHECK_FALSE(server->is_listening());
  156. // Make sure the client times out in less than the wait time.
  157. int timeout = ProjectSettings::get_singleton()->get_setting("network/limits/tcp/connect_timeout_seconds");
  158. ProjectSettings::get_singleton()->set_setting("network/limits/tcp/connect_timeout_seconds", 1);
  159. Ref<StreamPeerTCP> new_client = create_client(LOCALHOST, PORT);
  160. // Reset the timeout setting.
  161. ProjectSettings::get_singleton()->set_setting("network/limits/tcp/connect_timeout_seconds", timeout);
  162. CHECK_FALSE(server->is_connection_available());
  163. wait_for_condition([&]() {
  164. return new_client->poll() != Error::OK || new_client->get_status() == StreamPeerTCP::STATUS_ERROR;
  165. });
  166. CHECK_FALSE(server->is_connection_available());
  167. CHECK_EQ(new_client->get_status(), StreamPeerTCP::STATUS_ERROR);
  168. new_client->disconnect_from_host();
  169. CHECK_EQ(new_client->get_status(), StreamPeerTCP::STATUS_NONE);
  170. }
  171. TEST_CASE("[TCPServer] Should disconnect client") {
  172. Ref<TCPServer> server = create_server(LOCALHOST, PORT);
  173. Ref<StreamPeerTCP> client = create_client(LOCALHOST, PORT);
  174. Ref<StreamPeerTCP> client_from_server = accept_connection(server);
  175. wait_for_condition([&]() {
  176. return client->poll() != Error::OK || client->get_status() == StreamPeerTCP::STATUS_CONNECTED;
  177. });
  178. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  179. // Sending data from client to server.
  180. const String hello_world = "Hello World!";
  181. client->put_string(hello_world);
  182. CHECK_EQ(client_from_server->get_string(), hello_world);
  183. client_from_server->disconnect_from_host();
  184. server->stop();
  185. CHECK_FALSE(server->is_listening());
  186. // Wait for disconnection
  187. wait_for_condition([&]() {
  188. return client->poll() != Error::OK || client->get_status() == StreamPeerTCP::STATUS_NONE;
  189. });
  190. // Wait for disconnection
  191. wait_for_condition([&]() {
  192. return client_from_server->poll() != Error::OK || client_from_server->get_status() == StreamPeerTCP::STATUS_NONE;
  193. });
  194. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_NONE);
  195. CHECK_EQ(client_from_server->get_status(), StreamPeerTCP::STATUS_NONE);
  196. ERR_PRINT_OFF;
  197. CHECK_EQ(client->get_string(), String());
  198. CHECK_EQ(client_from_server->get_string(), String());
  199. ERR_PRINT_ON;
  200. }
  201. } // namespace TestTCPServer
  202. #endif // TEST_TCP_SERVER_H