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