test_scene_multiplayer.h 11 KB

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