Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for UDPServer #97311

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
289 changes: 289 additions & 0 deletions tests/core/io/test_udp_server.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,289 @@
/**************************************************************************/
/* 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");
const int sleep_duration = 100000;

Ref<UDPServer> create_server(const IPAddress &p_address, int p_port) {
Ref<UDPServer> 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<PacketPeerUDP> create_client(const IPAddress &p_address, int p_port) {
Ref<PacketPeerUDP> 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<PacketPeerUDP> accept_connection(Ref<UDPServer> &server) {
Error err = server->poll();
CHECK_EQ(err, Error::OK);

REQUIRE(server->is_connection_available());
Ref<PacketPeerUDP> 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<UDPServer> 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<UDPServer> server = create_server(localhost, port);
Ref<PacketPeerUDP> 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(sleep_duration);
CHECK_EQ(server->poll(), Error::OK);

Variant hello_world_received;
Ref<PacketPeerUDP> 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(sleep_duration);
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<UDPServer> server = create_server(localhost, port);

Vector<Ref<PacketPeerUDP>> clients;
for (int i = 0; i < 5; i++) {
Ref<PacketPeerUDP> 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(sleep_duration);
CHECK_EQ(server->poll(), Error::OK);

for (int i = 0; i < clients.size(); i++) {
Ref<PacketPeerUDP> 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(sleep_duration);
CHECK_EQ(server->poll(), Error::OK);

for (int i = 0; i < clients.size(); i++) {
Ref<PacketPeerUDP> 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<PacketPeerUDP> &c : clients) {
c->close();
}
server->stop();
}

TEST_CASE("[UDPServer] When stopped shouldn't accept new connections") {
Ref<UDPServer> server = create_server(localhost, port);
Ref<PacketPeerUDP> 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(sleep_duration);
CHECK_EQ(server->poll(), Error::OK);

Variant hello_world_received;
Ref<PacketPeerUDP> 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<PacketPeerUDP> 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(sleep_duration);

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<UDPServer> server = create_server(localhost, port);
Ref<PacketPeerUDP> 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(sleep_duration);
CHECK_EQ(server->poll(), Error::OK);

Variant hello_world_received;
Ref<PacketPeerUDP> 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(sleep_duration);

// 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(sleep_duration);

// 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<UDPServer> server = create_server(localhost, port);
server->set_max_pending_connections(3);

Vector<Ref<PacketPeerUDP>> clients;
for (int i = 0; i < 5; i++) {
Ref<PacketPeerUDP> 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(sleep_duration);

for (int i = 0; i < server->get_max_pending_connections(); i++) {
Ref<PacketPeerUDP> 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<PacketPeerUDP> 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 dropped.");

server->stop();
}

} // namespace TestUDPServer

#endif // TEST_UDP_SERVER_H
1 change: 1 addition & 0 deletions tests/test_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Loading