test_stream_peer_gzip.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**************************************************************************/
  2. /* test_stream_peer_gzip.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/stream_peer_gzip.h"
  32. #include "tests/test_macros.h"
  33. namespace TestStreamPeerGZIP {
  34. const String hello = "Hello World!!!";
  35. TEST_CASE("[StreamPeerGZIP] Initialization") {
  36. Ref<StreamPeerGZIP> spgz;
  37. spgz.instantiate();
  38. CHECK_EQ(spgz->get_available_bytes(), 0);
  39. }
  40. TEST_CASE("[StreamPeerGZIP] Compress/Decompress") {
  41. Ref<StreamPeerGZIP> spgz;
  42. spgz.instantiate();
  43. bool is_deflate = false;
  44. SUBCASE("GZIP") {
  45. is_deflate = false;
  46. }
  47. SUBCASE("DEFLATE") {
  48. is_deflate = true;
  49. }
  50. CHECK_EQ(spgz->start_compression(is_deflate), Error::OK);
  51. CHECK_EQ(spgz->put_data(hello.to_ascii_buffer().ptr(), hello.to_ascii_buffer().size()), Error::OK);
  52. CHECK_EQ(spgz->finish(), Error::OK);
  53. Vector<uint8_t> hello_compressed;
  54. int hello_compressed_size = spgz->get_available_bytes();
  55. hello_compressed.resize(hello_compressed_size);
  56. CHECK_EQ(spgz->get_data(hello_compressed.ptrw(), hello_compressed_size), Error::OK);
  57. spgz->clear();
  58. CHECK_EQ(spgz->start_decompression(is_deflate), Error::OK);
  59. CHECK_EQ(spgz->put_data(hello_compressed.ptr(), hello_compressed.size()), Error::OK);
  60. Vector<uint8_t> hello_decompressed;
  61. int hello_decompressed_size = spgz->get_available_bytes();
  62. hello_decompressed.resize(hello_decompressed_size);
  63. CHECK_EQ(spgz->get_data(hello_decompressed.ptrw(), hello_decompressed_size), Error::OK);
  64. CHECK_EQ(hello_decompressed, hello.to_ascii_buffer());
  65. }
  66. TEST_CASE("[StreamPeerGZIP] Compress/Decompress big chunks of data") { // GH-97201
  67. Ref<StreamPeerGZIP> spgz;
  68. spgz.instantiate();
  69. CHECK_EQ(spgz->start_compression(false), Error::OK);
  70. Vector<uint8_t> big_data;
  71. big_data.resize(2500);
  72. // Filling it with random data because the issue is related to the size of the data when it's compress.
  73. // Random data results in bigger compressed data size.
  74. for (int i = 0; i < big_data.size(); i++) {
  75. big_data.write[i] = Math::random(48, 122);
  76. }
  77. CHECK_EQ(spgz->put_data(big_data.ptr(), big_data.size()), Error::OK);
  78. CHECK_EQ(spgz->finish(), Error::OK);
  79. Vector<uint8_t> big_data_compressed;
  80. int big_data_compressed_size = spgz->get_available_bytes();
  81. big_data_compressed.resize(big_data_compressed_size);
  82. CHECK_EQ(spgz->get_data(big_data_compressed.ptrw(), big_data_compressed_size), Error::OK);
  83. spgz->clear();
  84. CHECK_EQ(spgz->start_decompression(false), Error::OK);
  85. CHECK_EQ(spgz->put_data(big_data_compressed.ptr(), big_data_compressed.size()), Error::OK);
  86. Vector<uint8_t> big_data_decompressed;
  87. int big_data_decompressed_size = spgz->get_available_bytes();
  88. big_data_decompressed.resize(big_data_decompressed_size);
  89. CHECK_EQ(spgz->get_data(big_data_decompressed.ptrw(), big_data_decompressed_size), Error::OK);
  90. CHECK_EQ(big_data_decompressed, big_data);
  91. }
  92. TEST_CASE("[StreamPeerGZIP] Can't start twice") {
  93. Ref<StreamPeerGZIP> spgz;
  94. spgz.instantiate();
  95. CHECK_EQ(spgz->start_compression(false), Error::OK);
  96. ERR_PRINT_OFF;
  97. CHECK_EQ(spgz->start_compression(false), Error::ERR_ALREADY_IN_USE);
  98. CHECK_EQ(spgz->start_decompression(false), Error::ERR_ALREADY_IN_USE);
  99. ERR_PRINT_ON;
  100. }
  101. TEST_CASE("[StreamPeerGZIP] Can't start with a buffer size equal or less than zero") {
  102. Ref<StreamPeerGZIP> spgz;
  103. spgz.instantiate();
  104. ERR_PRINT_OFF;
  105. CHECK_EQ(spgz->start_compression(false, 0), Error::ERR_INVALID_PARAMETER);
  106. CHECK_EQ(spgz->start_compression(false, -1), Error::ERR_INVALID_PARAMETER);
  107. CHECK_EQ(spgz->start_decompression(false, 0), Error::ERR_INVALID_PARAMETER);
  108. CHECK_EQ(spgz->start_decompression(false, -1), Error::ERR_INVALID_PARAMETER);
  109. ERR_PRINT_ON;
  110. }
  111. TEST_CASE("[StreamPeerGZIP] Can't put/get data with a buffer size less than zero") {
  112. Ref<StreamPeerGZIP> spgz;
  113. spgz.instantiate();
  114. CHECK_EQ(spgz->start_compression(false), Error::OK);
  115. ERR_PRINT_OFF;
  116. CHECK_EQ(spgz->put_data(hello.to_ascii_buffer().ptr(), -1), Error::ERR_INVALID_PARAMETER);
  117. Vector<uint8_t> hello_compressed;
  118. hello_compressed.resize(5);
  119. CHECK_EQ(spgz->get_data(hello_compressed.ptrw(), -1), Error::ERR_INVALID_PARAMETER);
  120. ERR_PRINT_ON;
  121. }
  122. TEST_CASE("[StreamPeerGZIP] Needs to be started before use") {
  123. Ref<StreamPeerGZIP> spgz;
  124. spgz.instantiate();
  125. ERR_PRINT_OFF;
  126. CHECK_EQ(spgz->put_data(hello.to_ascii_buffer().ptr(), hello.to_ascii_buffer().size()), Error::ERR_UNCONFIGURED);
  127. ERR_PRINT_ON;
  128. }
  129. TEST_CASE("[StreamPeerGZIP] Can't be finished after clear or if it's decompressing") {
  130. Ref<StreamPeerGZIP> spgz;
  131. spgz.instantiate();
  132. CHECK_EQ(spgz->start_compression(false), Error::OK);
  133. spgz->clear();
  134. ERR_PRINT_OFF;
  135. CHECK_EQ(spgz->finish(), Error::ERR_UNAVAILABLE);
  136. ERR_PRINT_ON;
  137. spgz->clear();
  138. CHECK_EQ(spgz->start_decompression(false), Error::OK);
  139. ERR_PRINT_OFF;
  140. CHECK_EQ(spgz->finish(), Error::ERR_UNAVAILABLE);
  141. ERR_PRINT_ON;
  142. }
  143. TEST_CASE("[StreamPeerGZIP] Fails to get if nothing was compress/decompress") {
  144. Ref<StreamPeerGZIP> spgz;
  145. spgz.instantiate();
  146. SUBCASE("Compression") {
  147. CHECK_EQ(spgz->start_compression(false), Error::OK);
  148. }
  149. SUBCASE("Decompression") {
  150. CHECK_EQ(spgz->start_decompression(false), Error::OK);
  151. }
  152. ERR_PRINT_OFF;
  153. Vector<uint8_t> hello_compressed;
  154. hello_compressed.resize(5);
  155. CHECK_EQ(spgz->get_data(hello_compressed.ptrw(), hello_compressed.size()), Error::ERR_UNAVAILABLE);
  156. ERR_PRINT_ON;
  157. }
  158. } // namespace TestStreamPeerGZIP