Debian Codegraph @

modules/multiplayer/tests/test_scene_multiplayer.h

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