Path: blob/master/modules/multiplayer/tests/test_scene_multiplayer.h
10278 views
/**************************************************************************/1/* test_scene_multiplayer.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "tests/test_macros.h"33#include "tests/test_utils.h"3435#include "../scene_multiplayer.h"3637namespace TestSceneMultiplayer {38TEST_CASE("[Multiplayer][SceneMultiplayer] Defaults") {39Ref<SceneMultiplayer> scene_multiplayer;40scene_multiplayer.instantiate();4142REQUIRE(scene_multiplayer->has_multiplayer_peer());43Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer();44REQUIRE_MESSAGE(Object::cast_to<OfflineMultiplayerPeer>(multiplayer_peer.ptr()) != nullptr, "By default it must be an OfflineMultiplayerPeer instance.");45CHECK_EQ(scene_multiplayer->poll(), Error::OK);46CHECK_EQ(scene_multiplayer->get_unique_id(), MultiplayerPeer::TARGET_PEER_SERVER);47CHECK_EQ(scene_multiplayer->get_peer_ids(), Vector<int>());48CHECK_EQ(scene_multiplayer->get_remote_sender_id(), 0);49CHECK_EQ(scene_multiplayer->get_root_path(), NodePath());50CHECK(scene_multiplayer->get_connected_peers().is_empty());51CHECK_FALSE(scene_multiplayer->is_refusing_new_connections());52CHECK_FALSE(scene_multiplayer->is_object_decoding_allowed());53CHECK(scene_multiplayer->is_server_relay_enabled());54CHECK_EQ(scene_multiplayer->get_max_sync_packet_size(), 1350);55CHECK_EQ(scene_multiplayer->get_max_delta_packet_size(), 65535);56CHECK(scene_multiplayer->is_server());57}5859TEST_CASE("[Multiplayer][SceneMultiplayer][SceneTree] SceneTree has a OfflineMultiplayerPeer by default") {60Ref<SceneMultiplayer> scene_multiplayer = SceneTree::get_singleton()->get_multiplayer();61REQUIRE(scene_multiplayer->has_multiplayer_peer());6263Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer();64REQUIRE_MESSAGE(Object::cast_to<OfflineMultiplayerPeer>(multiplayer_peer.ptr()) != nullptr, "By default it must be an OfflineMultiplayerPeer instance.");65}6667TEST_CASE("[Multiplayer][SceneMultiplayer][SceneTree] Object configuration add/remove") {68Ref<SceneMultiplayer> scene_multiplayer;69scene_multiplayer.instantiate();7071SUBCASE("Returns invalid parameter") {72CHECK_EQ(scene_multiplayer->object_configuration_add(nullptr, "ImInvalid"), Error::ERR_INVALID_PARAMETER);73CHECK_EQ(scene_multiplayer->object_configuration_remove(nullptr, "ImInvalid"), Error::ERR_INVALID_PARAMETER);7475NodePath foo_path("/Foo");76NodePath bar_path("/Bar");77CHECK_EQ(scene_multiplayer->object_configuration_add(nullptr, foo_path), Error::OK);78ERR_PRINT_OFF;79CHECK_EQ(scene_multiplayer->object_configuration_remove(nullptr, bar_path), Error::ERR_INVALID_PARAMETER);80ERR_PRINT_ON;81}8283SUBCASE("Sets root path") {84NodePath foo_path("/Foo");85CHECK_EQ(scene_multiplayer->object_configuration_add(nullptr, foo_path), Error::OK);8687CHECK_EQ(scene_multiplayer->get_root_path(), foo_path);88}8990SUBCASE("Unsets root path") {91NodePath foo_path("/Foo");92CHECK_EQ(scene_multiplayer->object_configuration_add(nullptr, foo_path), Error::OK);9394CHECK_EQ(scene_multiplayer->object_configuration_remove(nullptr, foo_path), Error::OK);95CHECK_EQ(scene_multiplayer->get_root_path(), NodePath());96}9798SUBCASE("Add/Remove a MultiplayerSpawner") {99Node2D *node = memnew(Node2D);100MultiplayerSpawner *spawner = memnew(MultiplayerSpawner);101102CHECK_EQ(scene_multiplayer->object_configuration_add(node, spawner), Error::OK);103CHECK_EQ(scene_multiplayer->object_configuration_remove(node, spawner), Error::OK);104105memdelete(spawner);106memdelete(node);107}108109SUBCASE("Add/Remove a MultiplayerSynchronizer") {110Node2D *node = memnew(Node2D);111MultiplayerSynchronizer *synchronizer = memnew(MultiplayerSynchronizer);112113CHECK_EQ(scene_multiplayer->object_configuration_add(node, synchronizer), Error::OK);114CHECK_EQ(scene_multiplayer->object_configuration_remove(node, synchronizer), Error::OK);115116memdelete(synchronizer);117memdelete(node);118}119}120121TEST_CASE("[Multiplayer][SceneMultiplayer] Root Path") {122Ref<SceneMultiplayer> scene_multiplayer;123scene_multiplayer.instantiate();124125SUBCASE("Is set") {126NodePath foo_path("/Foo");127scene_multiplayer->set_root_path(foo_path);128129CHECK_EQ(scene_multiplayer->get_root_path(), foo_path);130}131132SUBCASE("Fails when path is empty") {133ERR_PRINT_OFF;134scene_multiplayer->set_root_path(NodePath());135ERR_PRINT_ON;136}137138SUBCASE("Fails when path is relative") {139NodePath foo_path("Foo");140ERR_PRINT_OFF;141scene_multiplayer->set_root_path(foo_path);142ERR_PRINT_ON;143144CHECK_EQ(scene_multiplayer->get_root_path(), NodePath());145}146}147148// This one could be a dummy callback because the current set of test is not actually testing the full auth flow.149static Variant auth_callback(Variant sv, Variant pvav) {150return Variant();151}152153TEST_CASE("[Multiplayer][SceneMultiplayer][SceneTree] Send Authentication") {154Ref<SceneMultiplayer> scene_multiplayer;155scene_multiplayer.instantiate();156SceneTree::get_singleton()->set_multiplayer(scene_multiplayer);157scene_multiplayer->set_auth_callback(callable_mp_static(auth_callback));158159SUBCASE("Is properly sent") {160SIGNAL_WATCH(scene_multiplayer.ptr(), "peer_authenticating");161162// Adding a peer to MultiplayerPeer.163Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer();164int peer_id = 42;165multiplayer_peer->emit_signal(SNAME("peer_connected"), peer_id);166SIGNAL_CHECK("peer_authenticating", { { peer_id } });167168CHECK_EQ(scene_multiplayer->send_auth(peer_id, String("It's me").to_ascii_buffer()), Error::OK);169170Vector<int> expected_peer_ids = { peer_id };171CHECK_EQ(scene_multiplayer->get_authenticating_peer_ids(), expected_peer_ids);172173SIGNAL_UNWATCH(scene_multiplayer.ptr(), "peer_authenticating");174}175176SUBCASE("peer_authentication_failed is emitted when a peer is deleted before authentication is completed") {177SIGNAL_WATCH(scene_multiplayer.ptr(), "peer_authentication_failed");178179// Adding a peer to MultiplayerPeer.180Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer();181int peer_id = 42;182multiplayer_peer->emit_signal(SNAME("peer_connected"), peer_id);183multiplayer_peer->emit_signal(SNAME("peer_disconnected"), peer_id);184SIGNAL_CHECK("peer_authentication_failed", { { peer_id } });185186SIGNAL_UNWATCH(scene_multiplayer.ptr(), "peer_authentication_failed");187}188189SUBCASE("peer_authentication_failed is emitted when authentication timeout") {190SIGNAL_WATCH(scene_multiplayer.ptr(), "peer_authentication_failed");191scene_multiplayer->set_auth_timeout(0.01);192CHECK_EQ(scene_multiplayer->get_auth_timeout(), 0.01);193194// Adding two peesr to MultiplayerPeer.195Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer();196int first_peer_id = 42;197int second_peer_id = 84;198multiplayer_peer->emit_signal(SNAME("peer_connected"), first_peer_id);199multiplayer_peer->emit_signal(SNAME("peer_connected"), second_peer_id);200201// Let timeout happens.202OS::get_singleton()->delay_usec(500000);203204CHECK_EQ(scene_multiplayer->poll(), Error::OK);205206SIGNAL_CHECK("peer_authentication_failed", Array({ { first_peer_id }, { second_peer_id } }));207208SIGNAL_UNWATCH(scene_multiplayer.ptr(), "peer_authentication_failed");209}210211SUBCASE("Fails when there is no MultiplayerPeer configured") {212scene_multiplayer->set_multiplayer_peer(nullptr);213214ERR_PRINT_OFF;215CHECK_EQ(scene_multiplayer->send_auth(42, Vector<uint8_t>()), Error::ERR_UNCONFIGURED);216ERR_PRINT_ON;217}218219SUBCASE("Fails when the peer to send the auth is not pending") {220ERR_PRINT_OFF;221CHECK_EQ(scene_multiplayer->send_auth(42, String("It's me").to_ascii_buffer()), Error::ERR_INVALID_PARAMETER);222ERR_PRINT_ON;223}224}225226TEST_CASE("[Multiplayer][SceneMultiplayer][SceneTree] Complete Authentication") {227Ref<SceneMultiplayer> scene_multiplayer;228scene_multiplayer.instantiate();229SceneTree::get_singleton()->set_multiplayer(scene_multiplayer);230scene_multiplayer->set_auth_callback(callable_mp_static(auth_callback));231232SUBCASE("Is properly completed") {233Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer();234int peer_id = 42;235multiplayer_peer->emit_signal(SNAME("peer_connected"), peer_id);236CHECK_EQ(scene_multiplayer->send_auth(peer_id, String("It's me").to_ascii_buffer()), Error::OK);237238CHECK_EQ(scene_multiplayer->complete_auth(peer_id), Error::OK);239}240241SUBCASE("Fails when there is no MultiplayerPeer configured") {242scene_multiplayer->set_multiplayer_peer(nullptr);243244ERR_PRINT_OFF;245CHECK_EQ(scene_multiplayer->complete_auth(42), Error::ERR_UNCONFIGURED);246ERR_PRINT_ON;247}248249SUBCASE("Fails when the peer to complete the auth is not pending") {250ERR_PRINT_OFF;251CHECK_EQ(scene_multiplayer->complete_auth(42), Error::ERR_INVALID_PARAMETER);252ERR_PRINT_ON;253}254255SUBCASE("Fails to send auth or completed for a second time") {256Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer();257int peer_id = 42;258multiplayer_peer->emit_signal(SNAME("peer_connected"), peer_id);259CHECK_EQ(scene_multiplayer->send_auth(peer_id, String("It's me").to_ascii_buffer()), Error::OK);260CHECK_EQ(scene_multiplayer->complete_auth(peer_id), Error::OK);261262ERR_PRINT_OFF;263CHECK_EQ(scene_multiplayer->send_auth(peer_id, String("It's me").to_ascii_buffer()), Error::ERR_FILE_CANT_WRITE);264CHECK_EQ(scene_multiplayer->complete_auth(peer_id), Error::ERR_FILE_CANT_WRITE);265ERR_PRINT_ON;266}267}268269} // namespace TestSceneMultiplayer270271272