zlib_ut.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "zlib.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include "file.h"
  4. #include <util/system/tempfile.h>
  5. #include <util/random/entropy.h>
  6. #define ZDATA "./zdata"
  7. class TThrowingStream: public IOutputStream {
  8. public:
  9. TThrowingStream(int limit)
  10. : Limit_(limit)
  11. {
  12. }
  13. void DoWrite(const void*, size_t size) override {
  14. if (Ignore) {
  15. return;
  16. }
  17. Limit_ -= size;
  18. if (Limit_ < 0) {
  19. throw yexception() << "catch this";
  20. }
  21. }
  22. void DoFinish() override {
  23. if (Ignore) {
  24. return;
  25. }
  26. if (Limit_ < 0) {
  27. throw yexception() << "catch this";
  28. }
  29. }
  30. void DoFlush() override {
  31. if (Ignore) {
  32. return;
  33. }
  34. if (Limit_ < 0) {
  35. throw yexception() << "catch this";
  36. }
  37. }
  38. bool Ignore = false;
  39. private:
  40. int Limit_;
  41. };
  42. Y_UNIT_TEST_SUITE(TZLibTest) {
  43. static const TString DATA = "8s7d5vc6s5vc67sa4c65ascx6asd4xcv76adsfxv76s";
  44. static const TString DATA2 = "cn8wk2bd9vb3vdfif83g1ks94bfiovtwv";
  45. Y_UNIT_TEST(Compress) {
  46. TUnbufferedFileOutput o(ZDATA);
  47. TZLibCompress c(&o, ZLib::ZLib);
  48. c.Write(DATA.data(), DATA.size());
  49. c.Finish();
  50. o.Finish();
  51. }
  52. Y_UNIT_TEST(Decompress) {
  53. TTempFile tmpFile(ZDATA);
  54. {
  55. TUnbufferedFileInput i(ZDATA);
  56. TZLibDecompress d(&i);
  57. UNIT_ASSERT_EQUAL(d.ReadAll(), DATA);
  58. }
  59. }
  60. Y_UNIT_TEST(Dictionary) {
  61. static constexpr TStringBuf data = "<html><body></body></html>";
  62. static constexpr TStringBuf dict = "</<html><body>";
  63. for (auto type : {ZLib::Raw, ZLib::ZLib}) {
  64. TStringStream compressed;
  65. {
  66. TZLibCompress compressor(TZLibCompress::TParams(&compressed).SetDict(dict).SetType(type));
  67. compressor.Write(data);
  68. }
  69. TZLibDecompress decompressor(&compressed, type, ZLib::ZLIB_BUF_LEN, dict);
  70. UNIT_ASSERT_STRINGS_EQUAL(decompressor.ReadAll(), data);
  71. }
  72. }
  73. Y_UNIT_TEST(DecompressTwoStreams) {
  74. // Check that Decompress(Compress(X) + Compress(Y)) == X + Y
  75. TTempFile tmpFile(ZDATA);
  76. {
  77. TUnbufferedFileOutput o(ZDATA);
  78. TZLibCompress c1(&o, ZLib::ZLib);
  79. c1.Write(DATA.data(), DATA.size());
  80. c1.Finish();
  81. TZLibCompress c2(&o, ZLib::ZLib);
  82. c2.Write(DATA2.data(), DATA2.size());
  83. c2.Finish();
  84. o.Finish();
  85. }
  86. {
  87. TUnbufferedFileInput i(ZDATA);
  88. TZLibDecompress d(&i);
  89. UNIT_ASSERT_EQUAL(d.ReadAll(), DATA + DATA2);
  90. }
  91. }
  92. Y_UNIT_TEST(CompressionExceptionSegfault) {
  93. TVector<char> buf(512 * 1024);
  94. EntropyPool().Load(buf.data(), buf.size());
  95. TThrowingStream o(128 * 1024);
  96. TZLibCompress c(&o, ZLib::GZip, 4, 1 << 15);
  97. try {
  98. c.Write(buf.data(), buf.size());
  99. } catch (...) {
  100. }
  101. o.Ignore = true;
  102. TVector<char>().swap(buf);
  103. }
  104. Y_UNIT_TEST(DecompressFirstOfTwoStreams) {
  105. // Check that Decompress(Compress(X) + Compress(Y)) == X when single stream is allowed
  106. TTempFile tmpFile(ZDATA);
  107. {
  108. TUnbufferedFileOutput o(ZDATA);
  109. TZLibCompress c1(&o, ZLib::ZLib);
  110. c1.Write(DATA.data(), DATA.size());
  111. c1.Finish();
  112. TZLibCompress c2(&o, ZLib::ZLib);
  113. c2.Write(DATA2.data(), DATA2.size());
  114. c2.Finish();
  115. o.Finish();
  116. }
  117. {
  118. TUnbufferedFileInput i(ZDATA);
  119. TZLibDecompress d(&i);
  120. d.SetAllowMultipleStreams(false);
  121. UNIT_ASSERT_EQUAL(d.ReadAll(), DATA);
  122. }
  123. }
  124. Y_UNIT_TEST(CompressFlush) {
  125. TString data = "";
  126. for (size_t i = 0; i < 32; ++i) {
  127. TTempFile tmpFile(ZDATA);
  128. TUnbufferedFileOutput output(ZDATA);
  129. TZLibCompress compressor(&output, ZLib::ZLib);
  130. compressor.Write(data.data(), data.size());
  131. compressor.Flush();
  132. {
  133. TUnbufferedFileInput input(ZDATA);
  134. TZLibDecompress decompressor(&input);
  135. UNIT_ASSERT_EQUAL(decompressor.ReadAll(), data);
  136. }
  137. data += 'A' + i;
  138. }
  139. }
  140. Y_UNIT_TEST(CompressEmptyFlush) {
  141. TTempFile tmpFile(ZDATA);
  142. TUnbufferedFileOutput output(ZDATA);
  143. TZLibCompress compressor(&output, ZLib::ZLib);
  144. TUnbufferedFileInput input(ZDATA);
  145. compressor.Write(DATA.data(), DATA.size());
  146. compressor.Flush();
  147. {
  148. TZLibDecompress decompressor(&input);
  149. UNIT_ASSERT_EQUAL(decompressor.ReadAll(), DATA);
  150. }
  151. for (size_t i = 0; i < 10; ++i) {
  152. compressor.Flush();
  153. }
  154. UNIT_ASSERT_EQUAL(input.ReadAll(), "");
  155. }
  156. Y_UNIT_TEST(CompressFlushSmallBuffer) {
  157. for (size_t bufferSize = 16; bufferSize < 32; ++bufferSize) {
  158. TString firstData = "";
  159. for (size_t firstDataSize = 0; firstDataSize < 16; ++firstDataSize) {
  160. TString secondData = "";
  161. for (size_t secondDataSize = 0; secondDataSize < 16; ++secondDataSize) {
  162. TTempFile tmpFile(ZDATA);
  163. TUnbufferedFileOutput output(ZDATA);
  164. TZLibCompress compressor(TZLibCompress::TParams(&output).SetType(ZLib::ZLib).SetBufLen(bufferSize));
  165. TUnbufferedFileInput input(ZDATA);
  166. TZLibDecompress decompressor(&input);
  167. compressor.Write(firstData.data(), firstData.size());
  168. compressor.Flush();
  169. UNIT_ASSERT_EQUAL(decompressor.ReadAll(), firstData);
  170. compressor.Write(secondData.data(), secondData.size());
  171. compressor.Flush();
  172. UNIT_ASSERT_EQUAL(decompressor.ReadAll(), secondData);
  173. secondData += 'A' + secondDataSize;
  174. }
  175. firstData += 'A' + firstDataSize;
  176. }
  177. }
  178. }
  179. }