test_packet_peer.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /**************************************************************************/
  2. /* test_packet_peer.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_PACKET_PEER_H
  31. #define TEST_PACKET_PEER_H
  32. #include "core/io/packet_peer.h"
  33. #include "tests/test_macros.h"
  34. namespace TestPacketPeer {
  35. TEST_CASE("[PacketPeer][PacketPeerStream] Encode buffer max size") {
  36. Ref<PacketPeerStream> pps;
  37. pps.instantiate();
  38. SUBCASE("Default value") {
  39. CHECK_EQ(pps->get_encode_buffer_max_size(), 8 * 1024 * 1024);
  40. }
  41. SUBCASE("Max encode buffer must be at least 1024 bytes") {
  42. ERR_PRINT_OFF;
  43. pps->set_encode_buffer_max_size(42);
  44. ERR_PRINT_ON;
  45. CHECK_EQ(pps->get_encode_buffer_max_size(), 8 * 1024 * 1024);
  46. }
  47. SUBCASE("Max encode buffer cannot exceed 256 MiB") {
  48. ERR_PRINT_OFF;
  49. pps->set_encode_buffer_max_size((256 * 1024 * 1024) + 42);
  50. ERR_PRINT_ON;
  51. CHECK_EQ(pps->get_encode_buffer_max_size(), 8 * 1024 * 1024);
  52. }
  53. SUBCASE("Should be next power of two") {
  54. pps->set_encode_buffer_max_size(2000);
  55. CHECK_EQ(pps->get_encode_buffer_max_size(), 2048);
  56. }
  57. }
  58. TEST_CASE("[PacketPeer][PacketPeerStream] Read a variant from peer") {
  59. String godot_rules = "Godot Rules!!!";
  60. Ref<StreamPeerBuffer> spb;
  61. spb.instantiate();
  62. spb->put_var(godot_rules);
  63. spb->seek(0);
  64. Ref<PacketPeerStream> pps;
  65. pps.instantiate();
  66. pps->set_stream_peer(spb);
  67. Variant value;
  68. CHECK_EQ(pps->get_var(value), Error::OK);
  69. CHECK_EQ(String(value), godot_rules);
  70. }
  71. TEST_CASE("[PacketPeer][PacketPeerStream] Read a variant from peer fails") {
  72. Ref<PacketPeerStream> pps;
  73. pps.instantiate();
  74. Variant value;
  75. ERR_PRINT_OFF;
  76. CHECK_EQ(pps->get_var(value), Error::ERR_UNCONFIGURED);
  77. ERR_PRINT_ON;
  78. }
  79. TEST_CASE("[PacketPeer][PacketPeerStream] Put a variant to peer") {
  80. String godot_rules = "Godot Rules!!!";
  81. Ref<StreamPeerBuffer> spb;
  82. spb.instantiate();
  83. Ref<PacketPeerStream> pps;
  84. pps.instantiate();
  85. pps->set_stream_peer(spb);
  86. CHECK_EQ(pps->put_var(godot_rules), Error::OK);
  87. spb->seek(0);
  88. CHECK_EQ(String(spb->get_var()), godot_rules);
  89. }
  90. TEST_CASE("[PacketPeer][PacketPeerStream] Put a variant to peer out of memory failure") {
  91. String more_than_1mb = String("*").repeat(1024 + 1);
  92. Ref<StreamPeerBuffer> spb;
  93. spb.instantiate();
  94. Ref<PacketPeerStream> pps;
  95. pps.instantiate();
  96. pps->set_stream_peer(spb);
  97. pps->set_encode_buffer_max_size(1024);
  98. ERR_PRINT_OFF;
  99. CHECK_EQ(pps->put_var(more_than_1mb), Error::ERR_OUT_OF_MEMORY);
  100. ERR_PRINT_ON;
  101. }
  102. TEST_CASE("[PacketPeer][PacketPeerStream] Get packet buffer") {
  103. String godot_rules = "Godot Rules!!!";
  104. Ref<StreamPeerBuffer> spb;
  105. spb.instantiate();
  106. // First 4 bytes are the length of the string.
  107. CharString cs = godot_rules.ascii();
  108. Vector<uint8_t> buffer = { (uint8_t)(cs.length() + 1), 0, 0, 0 };
  109. buffer.resize_zeroed(4 + cs.length() + 1);
  110. memcpy(buffer.ptrw() + 4, cs.get_data(), cs.length());
  111. spb->set_data_array(buffer);
  112. Ref<PacketPeerStream> pps;
  113. pps.instantiate();
  114. pps->set_stream_peer(spb);
  115. buffer.clear();
  116. CHECK_EQ(pps->get_packet_buffer(buffer), Error::OK);
  117. CHECK_EQ(String(reinterpret_cast<const char *>(buffer.ptr())), godot_rules);
  118. }
  119. TEST_CASE("[PacketPeer][PacketPeerStream] Get packet buffer from an empty peer") {
  120. Ref<StreamPeerBuffer> spb;
  121. spb.instantiate();
  122. Ref<PacketPeerStream> pps;
  123. pps.instantiate();
  124. pps->set_stream_peer(spb);
  125. Vector<uint8_t> buffer;
  126. ERR_PRINT_OFF;
  127. CHECK_EQ(pps->get_packet_buffer(buffer), Error::ERR_UNAVAILABLE);
  128. ERR_PRINT_ON;
  129. CHECK_EQ(buffer.size(), 0);
  130. }
  131. TEST_CASE("[PacketPeer][PacketPeerStream] Put packet buffer") {
  132. String godot_rules = "Godot Rules!!!";
  133. Ref<StreamPeerBuffer> spb;
  134. spb.instantiate();
  135. Ref<PacketPeerStream> pps;
  136. pps.instantiate();
  137. pps->set_stream_peer(spb);
  138. CHECK_EQ(pps->put_packet_buffer(godot_rules.to_ascii_buffer()), Error::OK);
  139. spb->seek(0);
  140. CHECK_EQ(spb->get_string(), godot_rules);
  141. // First 4 bytes are the length of the string.
  142. CharString cs = godot_rules.ascii();
  143. Vector<uint8_t> buffer = { (uint8_t)cs.length(), 0, 0, 0 };
  144. buffer.resize(4 + cs.length());
  145. memcpy(buffer.ptrw() + 4, cs.get_data(), cs.length());
  146. CHECK_EQ(spb->get_data_array(), buffer);
  147. }
  148. TEST_CASE("[PacketPeer][PacketPeerStream] Put packet buffer when is empty") {
  149. Ref<StreamPeerBuffer> spb;
  150. spb.instantiate();
  151. Ref<PacketPeerStream> pps;
  152. pps.instantiate();
  153. pps->set_stream_peer(spb);
  154. Vector<uint8_t> buffer;
  155. CHECK_EQ(pps->put_packet_buffer(buffer), Error::OK);
  156. CHECK_EQ(spb->get_size(), 0);
  157. }
  158. } // namespace TestPacketPeer
  159. #endif // TEST_PACKET_PEER_H