test_tcp_server.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. 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 = 100000;
  40. Ref<TCPServer> create_server(const IPAddress &p_address, int p_port) {
  41. Ref<TCPServer> server;
  42. server.instantiate();
  43. REQUIRE_EQ(server->listen(PORT, LOCALHOST), Error::OK);
  44. REQUIRE(server->is_listening());
  45. CHECK_FALSE(server->is_connection_available());
  46. return server;
  47. }
  48. Ref<StreamPeerTCP> create_client(const IPAddress &p_address, int p_port) {
  49. Ref<StreamPeerTCP> client;
  50. client.instantiate();
  51. REQUIRE_EQ(client->connect_to_host(LOCALHOST, PORT), Error::OK);
  52. CHECK_EQ(client->get_connected_host(), LOCALHOST);
  53. CHECK_EQ(client->get_connected_port(), PORT);
  54. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTING);
  55. return client;
  56. }
  57. Ref<StreamPeerTCP> accept_connection(Ref<TCPServer> &p_server) {
  58. // Required to get the connection properly established.
  59. const uint64_t time = OS::get_singleton()->get_ticks_usec();
  60. while (!p_server->is_connection_available() && (OS::get_singleton()->get_ticks_usec() - time) < MAX_WAIT_USEC) {
  61. OS::get_singleton()->delay_usec(SLEEP_DURATION);
  62. }
  63. REQUIRE(p_server->is_connection_available());
  64. Ref<StreamPeerTCP> client_from_server = p_server->take_connection();
  65. REQUIRE(client_from_server.is_valid());
  66. CHECK_EQ(client_from_server->get_connected_host(), LOCALHOST);
  67. CHECK_EQ(client_from_server->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  68. return client_from_server;
  69. }
  70. Error poll(Ref<StreamPeerTCP> p_client) {
  71. const uint64_t time = OS::get_singleton()->get_ticks_usec();
  72. Error err = p_client->poll();
  73. while (err != Error::OK && (OS::get_singleton()->get_ticks_usec() - time) < MAX_WAIT_USEC) {
  74. OS::get_singleton()->delay_usec(SLEEP_DURATION);
  75. err = p_client->poll();
  76. }
  77. return err;
  78. }
  79. TEST_CASE("[TCPServer] Instantiation") {
  80. Ref<TCPServer> server;
  81. server.instantiate();
  82. REQUIRE(server.is_valid());
  83. CHECK_EQ(false, server->is_listening());
  84. }
  85. TEST_CASE("[TCPServer] Accept a connection and receive/send data") {
  86. Ref<TCPServer> server = create_server(LOCALHOST, PORT);
  87. Ref<StreamPeerTCP> client = create_client(LOCALHOST, PORT);
  88. Ref<StreamPeerTCP> client_from_server = accept_connection(server);
  89. REQUIRE_EQ(poll(client), Error::OK);
  90. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  91. // Sending data from client to server.
  92. const String hello_world = "Hello World!";
  93. client->put_string(hello_world);
  94. CHECK_EQ(client_from_server->get_string(), hello_world);
  95. // Sending data from server to client.
  96. const float pi = 3.1415;
  97. client_from_server->put_float(pi);
  98. CHECK_EQ(client->get_float(), pi);
  99. client->disconnect_from_host();
  100. server->stop();
  101. CHECK_FALSE(server->is_listening());
  102. }
  103. TEST_CASE("[TCPServer] Handle multiple clients at the same time") {
  104. Ref<TCPServer> server = create_server(LOCALHOST, PORT);
  105. Vector<Ref<StreamPeerTCP>> clients;
  106. for (int i = 0; i < 5; i++) {
  107. clients.push_back(create_client(LOCALHOST, PORT));
  108. }
  109. Vector<Ref<StreamPeerTCP>> clients_from_server;
  110. for (int i = 0; i < clients.size(); i++) {
  111. clients_from_server.push_back(accept_connection(server));
  112. }
  113. // Calling poll() to update client status.
  114. for (Ref<StreamPeerTCP> &c : clients) {
  115. REQUIRE_EQ(poll(c), Error::OK);
  116. }
  117. // Sending data from each client to server.
  118. for (int i = 0; i < clients.size(); i++) {
  119. String hello_client = "Hello " + itos(i);
  120. clients[i]->put_string(hello_client);
  121. CHECK_EQ(clients_from_server[i]->get_string(), hello_client);
  122. }
  123. for (Ref<StreamPeerTCP> &c : clients) {
  124. c->disconnect_from_host();
  125. }
  126. server->stop();
  127. }
  128. TEST_CASE("[TCPServer] When stopped shouldn't accept new connections") {
  129. Ref<TCPServer> server = create_server(LOCALHOST, PORT);
  130. Ref<StreamPeerTCP> client = create_client(LOCALHOST, PORT);
  131. Ref<StreamPeerTCP> client_from_server = accept_connection(server);
  132. REQUIRE_EQ(poll(client), Error::OK);
  133. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  134. // Sending data from client to server.
  135. const String hello_world = "Hello World!";
  136. client->put_string(hello_world);
  137. CHECK_EQ(client_from_server->get_string(), hello_world);
  138. client->disconnect_from_host();
  139. server->stop();
  140. CHECK_FALSE(server->is_listening());
  141. Ref<StreamPeerTCP> new_client = create_client(LOCALHOST, PORT);
  142. // Required to get the connection properly established.
  143. uint64_t time = OS::get_singleton()->get_ticks_usec();
  144. while (!server->is_connection_available() && (OS::get_singleton()->get_ticks_usec() - time) < MAX_WAIT_USEC) {
  145. OS::get_singleton()->delay_usec(SLEEP_DURATION);
  146. }
  147. CHECK_FALSE(server->is_connection_available());
  148. time = OS::get_singleton()->get_ticks_usec();
  149. Error err = new_client->poll();
  150. while (err != Error::OK && err != Error::ERR_CONNECTION_ERROR && (OS::get_singleton()->get_ticks_usec() - time) < MAX_WAIT_USEC) {
  151. OS::get_singleton()->delay_usec(SLEEP_DURATION);
  152. err = new_client->poll();
  153. }
  154. REQUIRE((err == Error::OK || err == Error::ERR_CONNECTION_ERROR));
  155. StreamPeerTCP::Status status = new_client->get_status();
  156. CHECK((status == StreamPeerTCP::STATUS_CONNECTING || status == StreamPeerTCP::STATUS_ERROR));
  157. new_client->disconnect_from_host();
  158. }
  159. TEST_CASE("[TCPServer] Should disconnect client") {
  160. Ref<TCPServer> server = create_server(LOCALHOST, PORT);
  161. Ref<StreamPeerTCP> client = create_client(LOCALHOST, PORT);
  162. Ref<StreamPeerTCP> client_from_server = accept_connection(server);
  163. REQUIRE_EQ(poll(client), Error::OK);
  164. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_CONNECTED);
  165. // Sending data from client to server.
  166. const String hello_world = "Hello World!";
  167. client->put_string(hello_world);
  168. CHECK_EQ(client_from_server->get_string(), hello_world);
  169. client_from_server->disconnect_from_host();
  170. server->stop();
  171. CHECK_FALSE(server->is_listening());
  172. // Reading for a closed connection will print an error.
  173. ERR_PRINT_OFF;
  174. CHECK_EQ(client->get_string(), String());
  175. ERR_PRINT_ON;
  176. REQUIRE_EQ(poll(client), Error::OK);
  177. CHECK_EQ(client->get_status(), StreamPeerTCP::STATUS_NONE);
  178. }
  179. } // namespace TestTCPServer
  180. #endif // TEST_TCP_SERVER_H