| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "core/io/file_access.h" |
| 34 |
#include "core/io/stream_peer_uds.h" |
| 35 |
#include "core/io/uds_server.h" |
| 36 |
#include "tests/test_macros.h" |
| 37 |
|
| 38 |
#include <functional> |
| 39 |
|
| 40 |
namespace TestUDSServer { |
| 41 |
|
| 42 |
#ifdef UNIX_ENABLED |
| 43 |
|
| 44 |
const String SOCKET_PATH = "/tmp/godot_test_uds_socket"; |
| 45 |
const uint32_t SLEEP_DURATION = 1000; |
| 46 |
const uint64_t MAX_WAIT_USEC = 2000000; |
| 47 |
|
| 48 |
void wait_for_condition(std::function<bool()> f_test) { |
| 49 |
const uint64_t time = OS::get_singleton()->get_ticks_usec(); |
| 50 |
while (!f_test() && (OS::get_singleton()->get_ticks_usec() - time) < MAX_WAIT_USEC) { |
| 51 |
OS::get_singleton()->delay_usec(SLEEP_DURATION); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
void cleanup_socket_file() { |
| 56 |
|
| 57 |
if (FileAccess::exists(SOCKET_PATH)) { |
| 58 |
DirAccess::remove_absolute(SOCKET_PATH); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
Ref<UDSServer> create_server(const String &p_path) { |
| 63 |
cleanup_socket_file(); |
| 64 |
|
| 65 |
Ref<UDSServer> server; |
| 66 |
server.instantiate(); |
| 67 |
|
| 68 |
REQUIRE_EQ(server->listen(p_path), Error::OK); |
| 69 |
REQUIRE(server->is_listening()); |
| 70 |
CHECK_FALSE(server->is_connection_available()); |
| 71 |
|
| 72 |
return server; |
| 73 |
} |
| 74 |
|
| 75 |
Ref<StreamPeerUDS> create_client(const String &p_path) { |
| 76 |
Ref<StreamPeerUDS> client; |
| 77 |
client.instantiate(); |
| 78 |
|
| 79 |
Error err = client->connect_to_host(p_path); |
| 80 |
REQUIRE_EQ(err, Error::OK); |
| 81 |
|
| 82 |
|
| 83 |
StreamPeerUDS::Status status = client->get_status(); |
| 84 |
REQUIRE((status == StreamPeerUDS::STATUS_CONNECTED || status == StreamPeerUDS::STATUS_CONNECTING)); |
| 85 |
|
| 86 |
if (status == StreamPeerUDS::STATUS_CONNECTED) { |
| 87 |
CHECK_EQ(client->get_connected_path(), p_path); |
| 88 |
} |
| 89 |
|
| 90 |
return client; |
| 91 |
} |
| 92 |
|
| 93 |
Ref<StreamPeerUDS> accept_connection(Ref<UDSServer> &p_server) { |
| 94 |
wait_for_condition([&]() { |
| 95 |
return p_server->is_connection_available(); |
| 96 |
}); |
| 97 |
|
| 98 |
REQUIRE(p_server->is_connection_available()); |
| 99 |
Ref<StreamPeerUDS> client_from_server = p_server->take_connection(); |
| 100 |
REQUIRE(client_from_server.is_valid()); |
| 101 |
CHECK_EQ(client_from_server->get_status(), StreamPeerUDS::STATUS_CONNECTED); |
| 102 |
|
| 103 |
return client_from_server; |
| 104 |
} |
| 105 |
|
| 106 |
TEST_CASE("[UDSServer] Instantiation") { |
| 107 |
Ref<UDSServer> server; |
| 108 |
server.instantiate(); |
| 109 |
|
| 110 |
REQUIRE(server.is_valid()); |
| 111 |
CHECK_FALSE(server->is_listening()); |
| 112 |
} |
| 113 |
|
| 114 |
TEST_CASE("[UDSServer] Accept a connection and receive/send data") { |
| 115 |
Ref<UDSServer> server = create_server(SOCKET_PATH); |
| 116 |
Ref<StreamPeerUDS> client = create_client(SOCKET_PATH); |
| 117 |
Ref<StreamPeerUDS> client_from_server = accept_connection(server); |
| 118 |
|
| 119 |
wait_for_condition([&]() { |
| 120 |
return client->poll() != Error::OK || client->get_status() == StreamPeerUDS::STATUS_CONNECTED; |
| 121 |
}); |
| 122 |
|
| 123 |
CHECK_EQ(client->get_status(), StreamPeerUDS::STATUS_CONNECTED); |
| 124 |
|
| 125 |
|
| 126 |
const String hello_world = "Hello World!"; |
| 127 |
client->put_string(hello_world); |
| 128 |
CHECK_EQ(client_from_server->get_string(), hello_world); |
| 129 |
|
| 130 |
|
| 131 |
const float pi = 3.1415; |
| 132 |
client_from_server->put_float(pi); |
| 133 |
CHECK_EQ(client->get_float(), pi); |
| 134 |
|
| 135 |
client->disconnect_from_host(); |
| 136 |
server->stop(); |
| 137 |
CHECK_FALSE(server->is_listening()); |
| 138 |
|
| 139 |
cleanup_socket_file(); |
| 140 |
} |
| 141 |
|
| 142 |
TEST_CASE("[UDSServer] Handle multiple clients at the same time") { |
| 143 |
Ref<UDSServer> server = create_server(SOCKET_PATH); |
| 144 |
|
| 145 |
Vector<Ref<StreamPeerUDS>> clients; |
| 146 |
for (int i = 0; i < 5; i++) { |
| 147 |
clients.push_back(create_client(SOCKET_PATH)); |
| 148 |
} |
| 149 |
|
| 150 |
Vector<Ref<StreamPeerUDS>> clients_from_server; |
| 151 |
for (int i = 0; i < clients.size(); i++) { |
| 152 |
clients_from_server.push_back(accept_connection(server)); |
| 153 |
} |
| 154 |
|
| 155 |
wait_for_condition([&]() { |
| 156 |
bool should_exit = true; |
| 157 |
for (Ref<StreamPeerUDS> &c : clients) { |
| 158 |
if (c->poll() != Error::OK) { |
| 159 |
return true; |
| 160 |
} |
| 161 |
StreamPeerUDS::Status status = c->get_status(); |
| 162 |
if (status != StreamPeerUDS::STATUS_CONNECTED && status != StreamPeerUDS::STATUS_CONNECTING) { |
| 163 |
return true; |
| 164 |
} |
| 165 |
if (status != StreamPeerUDS::STATUS_CONNECTED) { |
| 166 |
should_exit = false; |
| 167 |
} |
| 168 |
} |
| 169 |
return should_exit; |
| 170 |
}); |
| 171 |
|
| 172 |
for (Ref<StreamPeerUDS> &c : clients) { |
| 173 |
REQUIRE_EQ(c->get_status(), StreamPeerUDS::STATUS_CONNECTED); |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
for (int i = 0; i < clients.size(); i++) { |
| 178 |
String hello_client = "Hello " + itos(i); |
| 179 |
clients[i]->put_string(hello_client); |
| 180 |
CHECK_EQ(clients_from_server[i]->get_string(), hello_client); |
| 181 |
} |
| 182 |
|
| 183 |
for (Ref<StreamPeerUDS> &c : clients) { |
| 184 |
c->disconnect_from_host(); |
| 185 |
} |
| 186 |
server->stop(); |
| 187 |
|
| 188 |
cleanup_socket_file(); |
| 189 |
} |
| 190 |
|
| 191 |
TEST_CASE("[UDSServer] When stopped shouldn't accept new connections") { |
| 192 |
Ref<UDSServer> server = create_server(SOCKET_PATH); |
| 193 |
Ref<StreamPeerUDS> client = create_client(SOCKET_PATH); |
| 194 |
Ref<StreamPeerUDS> client_from_server = accept_connection(server); |
| 195 |
|
| 196 |
wait_for_condition([&]() { |
| 197 |
return client->poll() != Error::OK || client->get_status() == StreamPeerUDS::STATUS_CONNECTED; |
| 198 |
}); |
| 199 |
|
| 200 |
CHECK_EQ(client->get_status(), StreamPeerUDS::STATUS_CONNECTED); |
| 201 |
|
| 202 |
|
| 203 |
const String hello_world = "Hello World!"; |
| 204 |
client->put_string(hello_world); |
| 205 |
CHECK_EQ(client_from_server->get_string(), hello_world); |
| 206 |
|
| 207 |
client->disconnect_from_host(); |
| 208 |
server->stop(); |
| 209 |
CHECK_FALSE(server->is_listening()); |
| 210 |
|
| 211 |
|
| 212 |
cleanup_socket_file(); |
| 213 |
|
| 214 |
|
| 215 |
Ref<StreamPeerUDS> new_client; |
| 216 |
new_client.instantiate(); |
| 217 |
Error err = new_client->connect_to_host(SOCKET_PATH); |
| 218 |
|
| 219 |
|
| 220 |
CHECK_NE(err, Error::OK); |
| 221 |
CHECK_FALSE(server->is_connection_available()); |
| 222 |
|
| 223 |
cleanup_socket_file(); |
| 224 |
} |
| 225 |
|
| 226 |
TEST_CASE("[UDSServer] Should disconnect client") { |
| 227 |
Ref<UDSServer> server = create_server(SOCKET_PATH); |
| 228 |
Ref<StreamPeerUDS> client = create_client(SOCKET_PATH); |
| 229 |
Ref<StreamPeerUDS> client_from_server = accept_connection(server); |
| 230 |
|
| 231 |
wait_for_condition([&]() { |
| 232 |
return client->poll() != Error::OK || client->get_status() == StreamPeerUDS::STATUS_CONNECTED; |
| 233 |
}); |
| 234 |
|
| 235 |
CHECK_EQ(client->get_status(), StreamPeerUDS::STATUS_CONNECTED); |
| 236 |
|
| 237 |
|
| 238 |
const String hello_world = "Hello World!"; |
| 239 |
client->put_string(hello_world); |
| 240 |
CHECK_EQ(client_from_server->get_string(), hello_world); |
| 241 |
|
| 242 |
client_from_server->disconnect_from_host(); |
| 243 |
server->stop(); |
| 244 |
CHECK_FALSE(server->is_listening()); |
| 245 |
|
| 246 |
|
| 247 |
wait_for_condition([&]() { |
| 248 |
return client->poll() != Error::OK || client->get_status() == StreamPeerUDS::STATUS_NONE; |
| 249 |
}); |
| 250 |
|
| 251 |
|
| 252 |
wait_for_condition([&]() { |
| 253 |
return client_from_server->poll() != Error::OK || client_from_server->get_status() == StreamPeerUDS::STATUS_NONE; |
| 254 |
}); |
| 255 |
|
| 256 |
CHECK_EQ(client->get_status(), StreamPeerUDS::STATUS_NONE); |
| 257 |
CHECK_EQ(client_from_server->get_status(), StreamPeerUDS::STATUS_NONE); |
| 258 |
|
| 259 |
ERR_PRINT_OFF; |
| 260 |
CHECK_EQ(client->get_string(), String()); |
| 261 |
CHECK_EQ(client_from_server->get_string(), String()); |
| 262 |
ERR_PRINT_ON; |
| 263 |
|
| 264 |
cleanup_socket_file(); |
| 265 |
} |
| 266 |
|
| 267 |
TEST_CASE("[UDSServer] Test with different socket paths") { |
| 268 |
|
| 269 |
const String alt_socket_path = "/tmp/godot_test_uds_socket_alt"; |
| 270 |
|
| 271 |
|
| 272 |
if (FileAccess::exists(alt_socket_path)) { |
| 273 |
DirAccess::remove_absolute(alt_socket_path); |
| 274 |
} |
| 275 |
|
| 276 |
Ref<UDSServer> server = create_server(alt_socket_path); |
| 277 |
Ref<StreamPeerUDS> client = create_client(alt_socket_path); |
| 278 |
Ref<StreamPeerUDS> client_from_server = accept_connection(server); |
| 279 |
|
| 280 |
wait_for_condition([&]() { |
| 281 |
return client->poll() != Error::OK || client->get_status() == StreamPeerUDS::STATUS_CONNECTED; |
| 282 |
}); |
| 283 |
|
| 284 |
CHECK_EQ(client->get_status(), StreamPeerUDS::STATUS_CONNECTED); |
| 285 |
|
| 286 |
|
| 287 |
const int test_number = 42; |
| 288 |
client->put_32(test_number); |
| 289 |
CHECK_EQ(client_from_server->get_32(), test_number); |
| 290 |
|
| 291 |
client->disconnect_from_host(); |
| 292 |
server->stop(); |
| 293 |
|
| 294 |
|
| 295 |
if (FileAccess::exists(alt_socket_path)) { |
| 296 |
DirAccess::remove_absolute(alt_socket_path); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
#endif |
| 301 |
|
| 302 |
} |