Debian Codegraph @

tests/core/io/test_tcp_server.h

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