test_packet_peer.h 6.4 KB

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