test_scene_multiplayer.h 12 KB

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