diff --git a/tests/core/io/test_udp_server.h b/tests/core/io/test_udp_server.h new file mode 100644 index 00000000000..ce02dd45e22 --- /dev/null +++ b/tests/core/io/test_udp_server.h @@ -0,0 +1,288 @@ +/**************************************************************************/ +/* test_udp_server.h */ +/**************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/**************************************************************************/ +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/**************************************************************************/ + +#ifndef TEST_UDP_SERVER_H +#define TEST_UDP_SERVER_H + +#include "core/io/packet_peer_udp.h" +#include "core/io/udp_server.h" +#include "tests/test_macros.h" + +namespace TestUDPServer { + +const int port = 12345; +const IPAddress localhost("127.0.0.1"); + +Ref create_server(const IPAddress &p_address, int p_port) { + Ref server; + server.instantiate(); + + Error err = server->listen(port, localhost); + REQUIRE_EQ(Error::OK, err); + REQUIRE(server->is_listening()); + CHECK_FALSE(server->is_connection_available()); + CHECK_EQ(server->get_max_pending_connections(), 16); + + return server; +} + +Ref create_client(const IPAddress &p_address, int p_port) { + Ref client; + client.instantiate(); + + Error err = client->connect_to_host(localhost, port); + REQUIRE_EQ(Error::OK, err); + CHECK(client->is_bound()); + CHECK(client->is_socket_connected()); + + return client; +} + +Ref accept_connection(Ref &server) { + Error err = server->poll(); + CHECK_EQ(err, Error::OK); + + REQUIRE(server->is_connection_available()); + Ref client_from_server = server->take_connection(); + REQUIRE_MESSAGE(client_from_server.is_valid(), "A Packet Peer UDP from the UDP Server should not be a null pointer."); + CHECK(client_from_server->is_bound()); + CHECK(client_from_server->is_socket_connected()); + + return client_from_server; +} + +TEST_CASE("[UDPServer] Instantiation") { + Ref server; + server.instantiate(); + + REQUIRE_MESSAGE(server.is_valid(), "A UDP Server created should not be a null pointer."); + CHECK_EQ(false, server->is_listening()); +} + +TEST_CASE("[UDPServer] Accept a connection and receive/send data") { + Ref server = create_server(localhost, port); + Ref client = create_client(localhost, port); + + // Sending data from client to server. + String hello_world = "Hello World!"; + CHECK_EQ(client->put_var(hello_world), Error::OK); + + // Required to get the connection properly established. + OS::get_singleton()->delay_usec(500000); + CHECK_EQ(server->poll(), Error::OK); + + Variant hello_world_received; + Ref client_from_server = accept_connection(server); + CHECK_EQ(client_from_server->get_var(hello_world_received), Error::OK); + CHECK_EQ(String(hello_world_received), hello_world); + + // Sending data from server to client. + Variant pi = 3.1415; + CHECK_EQ(client_from_server->put_var(pi), Error::OK); + + // Required to get the connection properly established. + OS::get_singleton()->delay_usec(500000); + CHECK_EQ(server->poll(), Error::OK); + + // The recommended way to call _poll(), because there is no public poll(). + CHECK_GT(client->get_available_packet_count(), 0); + + Variant pi_received; + CHECK_EQ(client->get_var(pi_received), Error::OK); + CHECK_EQ(pi_received, pi); + + client->close(); + server->stop(); + CHECK_FALSE(server->is_listening()); +} + +TEST_CASE("[UDPServer] Handle multiple clients at the same time") { + Ref server = create_server(localhost, port); + + Vector> clients; + for (int i = 0; i < 5; i++) { + Ref c = create_client(localhost, port); + + // Sending data from client to server. + String hello_client = "Hello " + itos(i); + CHECK_EQ(c->put_var(hello_client), Error::OK); + + clients.push_back(c); + } + + // Required to get the connection properly established. + OS::get_singleton()->delay_usec(500000); + CHECK_EQ(server->poll(), Error::OK); + + for (int i = 0; i < clients.size(); i++) { + Ref cfs = accept_connection(server); + + Variant hello_world_received; + CHECK_EQ(cfs->get_var(hello_world_received), Error::OK); + CHECK_EQ(String(hello_world_received), "Hello " + itos(i)); + + // Sending data from server to client. + Variant pi = 3.1415 + i; + CHECK_EQ(cfs->put_var(pi), Error::OK); + } + + // Required to get the connection properly established. + OS::get_singleton()->delay_usec(500000); + CHECK_EQ(server->poll(), Error::OK); + + for (int i = 0; i < clients.size(); i++) { + Ref c = clients[i]; + // The recommended way to call _poll(), because there is no public poll(). + CHECK_GT(c->get_available_packet_count(), 0); + + Variant pi_received; + Variant pi = 3.1415 + i; + CHECK_EQ(c->get_var(pi_received), Error::OK); + CHECK_EQ(pi_received, pi); + } + + for (Ref &c : clients) { + c->close(); + } + server->stop(); +} + +TEST_CASE("[UDPServer] When stopped shouldn't accept new connections") { + Ref server = create_server(localhost, port); + Ref client = create_client(localhost, port); + + // Sending data from client to server. + String hello_world = "Hello World!"; + CHECK_EQ(client->put_var(hello_world), Error::OK); + + // Required to get the connection properly established. + OS::get_singleton()->delay_usec(500000); + CHECK_EQ(server->poll(), Error::OK); + + Variant hello_world_received; + Ref client_from_server = accept_connection(server); + CHECK_EQ(client_from_server->get_var(hello_world_received), Error::OK); + CHECK_EQ(String(hello_world_received), hello_world); + + client->close(); + server->stop(); + CHECK_FALSE(server->is_listening()); + + Ref new_client = create_client(localhost, port); + CHECK_EQ(new_client->put_var(hello_world), Error::OK); + + // Required to get the connection properly established. + OS::get_singleton()->delay_usec(500000); + + Error err = server->poll(); + REQUIRE_EQ(Error::ERR_UNCONFIGURED, err); + CHECK_FALSE(server->is_connection_available()); + + // The recommended way to call _poll(), because there is no public poll(). + int packet_count = new_client->get_available_packet_count(); + CHECK_MESSAGE((packet_count == 0 || packet_count == -1), "Packet count should be 0 or -1."); +} + +TEST_CASE("[UDPServer] Should disconnect client") { + Ref server = create_server(localhost, port); + Ref client = create_client(localhost, port); + + // Sending data from client to server. + String hello_world = "Hello World!"; + CHECK_EQ(client->put_var(hello_world), Error::OK); + + // Required to get the connection properly established. + OS::get_singleton()->delay_usec(500000); + CHECK_EQ(server->poll(), Error::OK); + + Variant hello_world_received; + Ref client_from_server = accept_connection(server); + CHECK_EQ(client_from_server->get_var(hello_world_received), Error::OK); + CHECK_EQ(String(hello_world_received), hello_world); + + server->stop(); + CHECK_FALSE(server->is_listening()); + CHECK_FALSE(client_from_server->is_bound()); + CHECK_FALSE(client_from_server->is_socket_connected()); + + // Required to get the connection properly established. + OS::get_singleton()->delay_usec(500000); + + // Sending data from client to server. + CHECK_EQ(client->put_var(hello_world), Error::OK); + + // Required to get the connection properly established. + OS::get_singleton()->delay_usec(500000); + + // The recommended way to call _poll(), because there is no public poll(). + int packet_count = client->get_available_packet_count(); + CHECK_MESSAGE((packet_count == 0 || packet_count == -1), "Packet count should be 0 or -1."); + + client->close(); +} + +TEST_CASE("[UDPServer] Should drop new connections when pending max connection is reached") { + Ref server = create_server(localhost, port); + server->set_max_pending_connections(3); + + Vector> clients; + for (int i = 0; i < 5; i++) { + Ref c = create_client(localhost, port); + + // Sending data from client to server. + String hello_client = "Hello " + itos(i); + CHECK_EQ(c->put_var(hello_client), Error::OK); + + clients.push_back(c); + } + + // Required to get the connection properly established. + OS::get_singleton()->delay_usec(500000); + + for (int i = 0; i < server->get_max_pending_connections(); i++) { + Ref cfs = accept_connection(server); + + Variant hello_world_received; + CHECK_EQ(cfs->get_var(hello_world_received), Error::OK); + CHECK_EQ(String(hello_world_received), "Hello " + itos(i)); + } + + CHECK_EQ(server->poll(), Error::OK); + + REQUIRE_FALSE(server->is_connection_available()); + Ref client_from_server = server->take_connection(); + REQUIRE_FALSE_MESSAGE(client_from_server.is_valid(), "A Packet Peer UDP from the UDP Server should be a null pointer because the pending connection was drop."); + + server->stop(); +} + +} // namespace TestUDPServer + +#endif // TEST_UDP_SERVER_H diff --git a/tests/test_main.cpp b/tests/test_main.cpp index 12ff3ad4bc9..912dc0b6c6f 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -52,6 +52,7 @@ #include "tests/core/io/test_marshalls.h" #include "tests/core/io/test_pck_packer.h" #include "tests/core/io/test_resource.h" +#include "tests/core/io/test_udp_server.h" #include "tests/core/io/test_xml_parser.h" #include "tests/core/math/test_aabb.h" #include "tests/core/math/test_astar.h"