|
| 1 | +/**************************************************************************/ |
| 2 | +/* test_scene_multiplayer.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 | +#ifndef TEST_SCENE_MULTIPLAYER_H |
| 32 | +#define TEST_SCENE_MULTIPLAYER_H |
| 33 | + |
| 34 | +#include "tests/test_macros.h" |
| 35 | +#include "tests/test_utils.h" |
| 36 | + |
| 37 | +#include "../scene_multiplayer.h" |
| 38 | + |
| 39 | +namespace TestSceneMultiplayer { |
| 40 | + |
| 41 | +static inline Array build_array() { |
| 42 | + return Array(); |
| 43 | +} |
| 44 | +template <typename... Targs> |
| 45 | +static inline Array build_array(Variant item, Targs... Fargs) { |
| 46 | + Array a = build_array(Fargs...); |
| 47 | + a.push_front(item); |
| 48 | + return a; |
| 49 | +} |
| 50 | + |
| 51 | +TEST_CASE("[Multiplayer][SceneMultiplayer] Defaults") { |
| 52 | + Ref<SceneMultiplayer> scene_multiplayer; |
| 53 | + scene_multiplayer.instantiate(); |
| 54 | + |
| 55 | + REQUIRE(scene_multiplayer->has_multiplayer_peer()); |
| 56 | + Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer(); |
| 57 | + REQUIRE_MESSAGE(Object::cast_to<OfflineMultiplayerPeer>(multiplayer_peer.ptr()) != nullptr, "By default it must be an OfflineMultiplayerPeer instance."); |
| 58 | + CHECK_EQ(scene_multiplayer->poll(), Error::OK); |
| 59 | + CHECK_EQ(scene_multiplayer->get_unique_id(), MultiplayerPeer::TARGET_PEER_SERVER); |
| 60 | + CHECK_EQ(scene_multiplayer->get_peer_ids(), Vector<int>()); |
| 61 | + CHECK_EQ(scene_multiplayer->get_remote_sender_id(), 0); |
| 62 | + CHECK_EQ(scene_multiplayer->get_root_path(), NodePath()); |
| 63 | + CHECK(scene_multiplayer->get_connected_peers().is_empty()); |
| 64 | + CHECK_FALSE(scene_multiplayer->is_refusing_new_connections()); |
| 65 | + CHECK_FALSE(scene_multiplayer->is_object_decoding_allowed()); |
| 66 | + CHECK(scene_multiplayer->is_server_relay_enabled()); |
| 67 | + CHECK_EQ(scene_multiplayer->get_max_sync_packet_size(), 1350); |
| 68 | + CHECK_EQ(scene_multiplayer->get_max_delta_packet_size(), 65535); |
| 69 | + CHECK(scene_multiplayer->is_server()); |
| 70 | +} |
| 71 | + |
| 72 | +TEST_CASE("[Multiplayer][SceneMultiplayer][SceneTree] SceneTree has a OfflineMultiplayerPeer by default") { |
| 73 | + Ref<SceneMultiplayer> scene_multiplayer = SceneTree::get_singleton()->get_multiplayer(); |
| 74 | + REQUIRE(scene_multiplayer->has_multiplayer_peer()); |
| 75 | + |
| 76 | + Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer(); |
| 77 | + REQUIRE_MESSAGE(Object::cast_to<OfflineMultiplayerPeer>(multiplayer_peer.ptr()) != nullptr, "By default it must be an OfflineMultiplayerPeer instance."); |
| 78 | +} |
| 79 | + |
| 80 | +TEST_CASE("[Multiplayer][SceneMultiplayer][SceneTree] Object configuration add/remove") { |
| 81 | + Ref<SceneMultiplayer> scene_multiplayer; |
| 82 | + scene_multiplayer.instantiate(); |
| 83 | + |
| 84 | + SUBCASE("Returns invalid parameter") { |
| 85 | + CHECK_EQ(scene_multiplayer->object_configuration_add(nullptr, "ImInvalid"), Error::ERR_INVALID_PARAMETER); |
| 86 | + CHECK_EQ(scene_multiplayer->object_configuration_remove(nullptr, "ImInvalid"), Error::ERR_INVALID_PARAMETER); |
| 87 | + |
| 88 | + NodePath foo_path("/Foo"); |
| 89 | + NodePath bar_path("/Bar"); |
| 90 | + CHECK_EQ(scene_multiplayer->object_configuration_add(nullptr, foo_path), Error::OK); |
| 91 | + ERR_PRINT_OFF; |
| 92 | + CHECK_EQ(scene_multiplayer->object_configuration_remove(nullptr, bar_path), Error::ERR_INVALID_PARAMETER); |
| 93 | + ERR_PRINT_ON; |
| 94 | + } |
| 95 | + |
| 96 | + SUBCASE("Sets root path") { |
| 97 | + NodePath foo_path("/Foo"); |
| 98 | + CHECK_EQ(scene_multiplayer->object_configuration_add(nullptr, foo_path), Error::OK); |
| 99 | + |
| 100 | + CHECK_EQ(scene_multiplayer->get_root_path(), foo_path); |
| 101 | + } |
| 102 | + |
| 103 | + SUBCASE("Unsets root path") { |
| 104 | + NodePath foo_path("/Foo"); |
| 105 | + CHECK_EQ(scene_multiplayer->object_configuration_add(nullptr, foo_path), Error::OK); |
| 106 | + |
| 107 | + CHECK_EQ(scene_multiplayer->object_configuration_remove(nullptr, foo_path), Error::OK); |
| 108 | + CHECK_EQ(scene_multiplayer->get_root_path(), NodePath()); |
| 109 | + } |
| 110 | + |
| 111 | + SUBCASE("Add/Remove a MultiplayerSpawner") { |
| 112 | + Node2D *node = memnew(Node2D); |
| 113 | + MultiplayerSpawner *spawner = memnew(MultiplayerSpawner); |
| 114 | + |
| 115 | + CHECK_EQ(scene_multiplayer->object_configuration_add(node, spawner), Error::OK); |
| 116 | + CHECK_EQ(scene_multiplayer->object_configuration_remove(node, spawner), Error::OK); |
| 117 | + |
| 118 | + memdelete(spawner); |
| 119 | + memdelete(node); |
| 120 | + } |
| 121 | + |
| 122 | + SUBCASE("Add/Remove a MultiplayerSynchronizer") { |
| 123 | + Node2D *node = memnew(Node2D); |
| 124 | + MultiplayerSynchronizer *synchronizer = memnew(MultiplayerSynchronizer); |
| 125 | + |
| 126 | + CHECK_EQ(scene_multiplayer->object_configuration_add(node, synchronizer), Error::OK); |
| 127 | + CHECK_EQ(scene_multiplayer->object_configuration_remove(node, synchronizer), Error::OK); |
| 128 | + |
| 129 | + memdelete(synchronizer); |
| 130 | + memdelete(node); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +TEST_CASE("[Multiplayer][SceneMultiplayer] Root Path") { |
| 135 | + Ref<SceneMultiplayer> scene_multiplayer; |
| 136 | + scene_multiplayer.instantiate(); |
| 137 | + |
| 138 | + SUBCASE("Is set") { |
| 139 | + NodePath foo_path("/Foo"); |
| 140 | + scene_multiplayer->set_root_path(foo_path); |
| 141 | + |
| 142 | + CHECK_EQ(scene_multiplayer->get_root_path(), foo_path); |
| 143 | + } |
| 144 | + |
| 145 | + SUBCASE("Fails when path is empty") { |
| 146 | + ERR_PRINT_OFF; |
| 147 | + scene_multiplayer->set_root_path(NodePath()); |
| 148 | + ERR_PRINT_ON; |
| 149 | + } |
| 150 | + |
| 151 | + SUBCASE("Fails when path is relative") { |
| 152 | + NodePath foo_path("Foo"); |
| 153 | + ERR_PRINT_OFF; |
| 154 | + scene_multiplayer->set_root_path(foo_path); |
| 155 | + ERR_PRINT_ON; |
| 156 | + |
| 157 | + CHECK_EQ(scene_multiplayer->get_root_path(), NodePath()); |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +// This one could be a dummy callback because the current set of test is not actually testing the full auth flow. |
| 162 | +static Variant auth_callback(Variant sv, Variant pvav) { |
| 163 | + return Variant(); |
| 164 | +} |
| 165 | + |
| 166 | +TEST_CASE("[Multiplayer][SceneMultiplayer][SceneTree] Send Authentication") { |
| 167 | + Ref<SceneMultiplayer> scene_multiplayer; |
| 168 | + scene_multiplayer.instantiate(); |
| 169 | + SceneTree::get_singleton()->set_multiplayer(scene_multiplayer); |
| 170 | + scene_multiplayer->set_auth_callback(callable_mp_static(auth_callback)); |
| 171 | + |
| 172 | + SUBCASE("Is properly sent") { |
| 173 | + SIGNAL_WATCH(scene_multiplayer.ptr(), "peer_authenticating"); |
| 174 | + |
| 175 | + // Adding a peer to MultiplayerPeer. |
| 176 | + Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer(); |
| 177 | + int peer_id = 42; |
| 178 | + multiplayer_peer->emit_signal(SNAME("peer_connected"), peer_id); |
| 179 | + SIGNAL_CHECK("peer_authenticating", build_array(build_array(peer_id))); |
| 180 | + |
| 181 | + CHECK_EQ(scene_multiplayer->send_auth(peer_id, String("It's me").to_ascii_buffer()), Error::OK); |
| 182 | + |
| 183 | + Vector<int> expected_peer_ids = { peer_id }; |
| 184 | + CHECK_EQ(scene_multiplayer->get_authenticating_peer_ids(), expected_peer_ids); |
| 185 | + |
| 186 | + SIGNAL_UNWATCH(scene_multiplayer.ptr(), "peer_authenticating"); |
| 187 | + } |
| 188 | + |
| 189 | + SUBCASE("peer_authentication_failed is emitted when a peer is deleted before authentication is completed") { |
| 190 | + SIGNAL_WATCH(scene_multiplayer.ptr(), "peer_authentication_failed"); |
| 191 | + |
| 192 | + // Adding a peer to MultiplayerPeer. |
| 193 | + Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer(); |
| 194 | + int peer_id = 42; |
| 195 | + multiplayer_peer->emit_signal(SNAME("peer_connected"), peer_id); |
| 196 | + multiplayer_peer->emit_signal(SNAME("peer_disconnected"), peer_id); |
| 197 | + SIGNAL_CHECK("peer_authentication_failed", build_array(build_array(peer_id))); |
| 198 | + |
| 199 | + SIGNAL_UNWATCH(scene_multiplayer.ptr(), "peer_authentication_failed"); |
| 200 | + } |
| 201 | + |
| 202 | + SUBCASE("peer_authentication_failed is emitted when authentication timeout") { |
| 203 | + SIGNAL_WATCH(scene_multiplayer.ptr(), "peer_authentication_failed"); |
| 204 | + scene_multiplayer->set_auth_timeout(0.01); |
| 205 | + CHECK_EQ(scene_multiplayer->get_auth_timeout(), 0.01); |
| 206 | + |
| 207 | + // Adding two peesr to MultiplayerPeer. |
| 208 | + Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer(); |
| 209 | + int first_peer_id = 42; |
| 210 | + int second_peer_id = 84; |
| 211 | + multiplayer_peer->emit_signal(SNAME("peer_connected"), first_peer_id); |
| 212 | + multiplayer_peer->emit_signal(SNAME("peer_connected"), second_peer_id); |
| 213 | + |
| 214 | + // Let timeout happens. |
| 215 | + OS::get_singleton()->delay_usec(500000); |
| 216 | + |
| 217 | + CHECK_EQ(scene_multiplayer->poll(), Error::OK); |
| 218 | + |
| 219 | + SIGNAL_CHECK("peer_authentication_failed", build_array(build_array(first_peer_id), build_array(second_peer_id))); |
| 220 | + |
| 221 | + SIGNAL_UNWATCH(scene_multiplayer.ptr(), "peer_authentication_failed"); |
| 222 | + } |
| 223 | + |
| 224 | + SUBCASE("Fails when there is no MultiplayerPeer configured") { |
| 225 | + scene_multiplayer->set_multiplayer_peer(nullptr); |
| 226 | + |
| 227 | + ERR_PRINT_OFF; |
| 228 | + CHECK_EQ(scene_multiplayer->send_auth(42, Vector<uint8_t>()), Error::ERR_UNCONFIGURED); |
| 229 | + ERR_PRINT_ON; |
| 230 | + } |
| 231 | + |
| 232 | + SUBCASE("Fails when the peer to send the auth is not pending") { |
| 233 | + ERR_PRINT_OFF; |
| 234 | + CHECK_EQ(scene_multiplayer->send_auth(42, String("It's me").to_ascii_buffer()), Error::ERR_INVALID_PARAMETER); |
| 235 | + ERR_PRINT_ON; |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +TEST_CASE("[Multiplayer][SceneMultiplayer][SceneTree] Complete Authentication") { |
| 240 | + Ref<SceneMultiplayer> scene_multiplayer; |
| 241 | + scene_multiplayer.instantiate(); |
| 242 | + SceneTree::get_singleton()->set_multiplayer(scene_multiplayer); |
| 243 | + scene_multiplayer->set_auth_callback(callable_mp_static(auth_callback)); |
| 244 | + |
| 245 | + SUBCASE("Is properly completed") { |
| 246 | + Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer(); |
| 247 | + int peer_id = 42; |
| 248 | + multiplayer_peer->emit_signal(SNAME("peer_connected"), peer_id); |
| 249 | + CHECK_EQ(scene_multiplayer->send_auth(peer_id, String("It's me").to_ascii_buffer()), Error::OK); |
| 250 | + |
| 251 | + CHECK_EQ(scene_multiplayer->complete_auth(peer_id), Error::OK); |
| 252 | + } |
| 253 | + |
| 254 | + SUBCASE("Fails when there is no MultiplayerPeer configured") { |
| 255 | + scene_multiplayer->set_multiplayer_peer(nullptr); |
| 256 | + |
| 257 | + ERR_PRINT_OFF; |
| 258 | + CHECK_EQ(scene_multiplayer->complete_auth(42), Error::ERR_UNCONFIGURED); |
| 259 | + ERR_PRINT_ON; |
| 260 | + } |
| 261 | + |
| 262 | + SUBCASE("Fails when the peer to complete the auth is not pending") { |
| 263 | + ERR_PRINT_OFF; |
| 264 | + CHECK_EQ(scene_multiplayer->complete_auth(42), Error::ERR_INVALID_PARAMETER); |
| 265 | + ERR_PRINT_ON; |
| 266 | + } |
| 267 | + |
| 268 | + SUBCASE("Fails to send auth or completed for a second time") { |
| 269 | + Ref<MultiplayerPeer> multiplayer_peer = scene_multiplayer->get_multiplayer_peer(); |
| 270 | + int peer_id = 42; |
| 271 | + multiplayer_peer->emit_signal(SNAME("peer_connected"), peer_id); |
| 272 | + CHECK_EQ(scene_multiplayer->send_auth(peer_id, String("It's me").to_ascii_buffer()), Error::OK); |
| 273 | + CHECK_EQ(scene_multiplayer->complete_auth(peer_id), Error::OK); |
| 274 | + |
| 275 | + ERR_PRINT_OFF; |
| 276 | + CHECK_EQ(scene_multiplayer->send_auth(peer_id, String("It's me").to_ascii_buffer()), Error::ERR_FILE_CANT_WRITE); |
| 277 | + CHECK_EQ(scene_multiplayer->complete_auth(peer_id), Error::ERR_FILE_CANT_WRITE); |
| 278 | + ERR_PRINT_ON; |
| 279 | + } |
| 280 | +} |
| 281 | + |
| 282 | +} // namespace TestSceneMultiplayer |
| 283 | + |
| 284 | +#endif // TEST_SCENE_MULTIPLAYER_H |
0 commit comments