proxygen
ZlibTests.cpp File Reference

Go to the source code of this file.

Classes

class  ZlibTests
 

Functions

std::unique_ptr< folly::IOBufmakeBuf (uint32_t size)
 
void verify (ZlibCompressionType type, std::unique_ptr< IOBuf > original, std::unique_ptr< IOBuf > compressed)
 
void compressThenDecompress (ZlibCompressionType type, int level, unique_ptr< IOBuf > buf)
 
 TEST_F (ZlibTests, CompressDecompressGzip5000)
 
 TEST_F (ZlibTests, CompressDecompressGzip2000)
 
 TEST_F (ZlibTests, CompressDecompressGzip1024)
 
 TEST_F (ZlibTests, CompressDecompressGzip500)
 
 TEST_F (ZlibTests, CompressDecompressGzip50)
 
 TEST_F (ZlibTests, CompressDecompressDeflate)
 
 TEST_F (ZlibTests, CompressDecompressEmpty)
 
 TEST_F (ZlibTests, CompressDecompressChain)
 
 TEST_F (ZlibTests, CompressDecompressStreaming)
 
 TEST_F (ZlibTests, CompressDecompressSmallBuffer)
 

Function Documentation

void compressThenDecompress ( ZlibCompressionType  type,
int  level,
unique_ptr< IOBuf buf 
)

Definition at line 52 of file ZlibTests.cpp.

References ASSERT_FALSE, proxygen::ZlibStreamCompressor::compress(), proxygen::ZlibStreamCompressor::getStatus(), proxygen::ZlibStreamCompressor::hasError(), folly::gen::move, and verify().

Referenced by TEST_F().

54  {
55 
56  unique_ptr<IOBuf> compressed;
57  unique_ptr<IOBuf> decompressed;
58 
59  unique_ptr<ZlibStreamCompressor> zc(new ZlibStreamCompressor(type, level));
60 
61  compressed = zc->compress(buf.get(), true);
62  ASSERT_FALSE(zc->hasError()) << "Compression error. r=" << zc->getStatus();
63 
64  verify(type, std::move(buf), std::move(compressed));
65 }
PskType type
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void verify(ZlibCompressionType type, std::unique_ptr< IOBuf > original, std::unique_ptr< IOBuf > compressed)
Definition: ZlibTests.cpp:40
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
std::unique_ptr<folly::IOBuf> makeBuf ( uint32_t  size)

Definition at line 26 of file ZlibTests.cpp.

References folly::IOBuf::create(), folly::Random::rand32(), folly::Random::rand64(), uint64_t, uint8_t, and folly::io::detail::Writable< Derived >::write().

Referenced by TestPriorityMapBuilder::createVirtualStreams(), HTTP2FramerTest::dataFrameTest(), TestAbortPost< stage >::doAbortTest(), proxygen::fakeMockCodec(), proxygen::makeResponse(), MoveToFbStringTest::SetUp(), TEST_F(), MockCodecDownstreamTest::testConnFlowControlBlocked(), and TYPED_TEST_P().

26  {
27  auto out = folly::IOBuf::create(size);
28  out->append(size);
29  // fill with random junk
30  folly::io::RWPrivateCursor cursor(out.get());
31  while (cursor.length() >= 8) {
33  }
34  while (cursor.length()) {
35  cursor.write<uint8_t>((uint8_t)folly::Random::rand32());
36  }
37  return out;
38 }
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
std::enable_if< std::is_arithmetic< T >::value >::type write(T value)
Definition: Cursor.h:737
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
static uint32_t rand32()
Definition: Random.h:213
static uint64_t rand64()
Definition: Random.h:263
TEST_F ( ZlibTests  ,
CompressDecompressGzip5000   
)

Definition at line 68 of file ZlibTests.cpp.

References ASSERT_NO_FATAL_FAILURE, compressThenDecompress(), and makeBuf().

68  {
70  { compressThenDecompress(ZlibCompressionType::GZIP, 6, makeBuf(5000)); });
71 }
std::unique_ptr< folly::IOBuf > makeBuf(uint32_t size)
Definition: ZlibTests.cpp:26
void compressThenDecompress(ZlibCompressionType type, int level, unique_ptr< IOBuf > buf)
Definition: ZlibTests.cpp:52
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( ZlibTests  ,
CompressDecompressGzip2000   
)

Definition at line 73 of file ZlibTests.cpp.

References ASSERT_NO_FATAL_FAILURE, compressThenDecompress(), and makeBuf().

73  {
75  { compressThenDecompress(ZlibCompressionType::GZIP, 6, makeBuf(2000)); });
76 }
std::unique_ptr< folly::IOBuf > makeBuf(uint32_t size)
Definition: ZlibTests.cpp:26
void compressThenDecompress(ZlibCompressionType type, int level, unique_ptr< IOBuf > buf)
Definition: ZlibTests.cpp:52
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( ZlibTests  ,
CompressDecompressGzip1024   
)

Definition at line 78 of file ZlibTests.cpp.

References ASSERT_NO_FATAL_FAILURE, compressThenDecompress(), and makeBuf().

78  {
80  { compressThenDecompress(ZlibCompressionType::GZIP, 6, makeBuf(1024)); });
81 }
std::unique_ptr< folly::IOBuf > makeBuf(uint32_t size)
Definition: ZlibTests.cpp:26
void compressThenDecompress(ZlibCompressionType type, int level, unique_ptr< IOBuf > buf)
Definition: ZlibTests.cpp:52
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( ZlibTests  ,
CompressDecompressGzip500   
)

Definition at line 83 of file ZlibTests.cpp.

References ASSERT_NO_FATAL_FAILURE, compressThenDecompress(), and makeBuf().

83  {
85  { compressThenDecompress(ZlibCompressionType::GZIP, 6, makeBuf(500)); });
86 }
std::unique_ptr< folly::IOBuf > makeBuf(uint32_t size)
Definition: ZlibTests.cpp:26
void compressThenDecompress(ZlibCompressionType type, int level, unique_ptr< IOBuf > buf)
Definition: ZlibTests.cpp:52
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( ZlibTests  ,
CompressDecompressGzip50   
)

Definition at line 88 of file ZlibTests.cpp.

References ASSERT_NO_FATAL_FAILURE, compressThenDecompress(), and makeBuf().

88  {
90  { compressThenDecompress(ZlibCompressionType::GZIP, 6, makeBuf(50)); });
91 }
std::unique_ptr< folly::IOBuf > makeBuf(uint32_t size)
Definition: ZlibTests.cpp:26
void compressThenDecompress(ZlibCompressionType type, int level, unique_ptr< IOBuf > buf)
Definition: ZlibTests.cpp:52
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( ZlibTests  ,
CompressDecompressDeflate   
)

Definition at line 93 of file ZlibTests.cpp.

References ASSERT_NO_FATAL_FAILURE, compressThenDecompress(), and makeBuf().

93  {
95  compressThenDecompress(ZlibCompressionType::DEFLATE, 6, makeBuf(500));
96  });
97 }
std::unique_ptr< folly::IOBuf > makeBuf(uint32_t size)
Definition: ZlibTests.cpp:26
void compressThenDecompress(ZlibCompressionType type, int level, unique_ptr< IOBuf > buf)
Definition: ZlibTests.cpp:52
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( ZlibTests  ,
CompressDecompressEmpty   
)

Definition at line 99 of file ZlibTests.cpp.

References ASSERT_NO_FATAL_FAILURE, compressThenDecompress(), and makeBuf().

99  {
101  { compressThenDecompress(ZlibCompressionType::GZIP, 4, makeBuf(0)); });
102 }
std::unique_ptr< folly::IOBuf > makeBuf(uint32_t size)
Definition: ZlibTests.cpp:26
void compressThenDecompress(ZlibCompressionType type, int level, unique_ptr< IOBuf > buf)
Definition: ZlibTests.cpp:52
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( ZlibTests  ,
CompressDecompressChain   
)

Definition at line 104 of file ZlibTests.cpp.

References ASSERT_NO_FATAL_FAILURE, compressThenDecompress(), makeBuf(), and folly::gen::move.

104  {
106  auto buf = makeBuf(4);
107  buf->appendChain(makeBuf(38));
108  buf->appendChain(makeBuf(12));
109  buf->appendChain(makeBuf(0));
110  compressThenDecompress(ZlibCompressionType::GZIP, 6, std::move(buf));
111  });
112 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< folly::IOBuf > makeBuf(uint32_t size)
Definition: ZlibTests.cpp:26
void compressThenDecompress(ZlibCompressionType type, int level, unique_ptr< IOBuf > buf)
Definition: ZlibTests.cpp:52
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
TEST_F ( ZlibTests  ,
CompressDecompressStreaming   
)

Definition at line 114 of file ZlibTests.cpp.

References ASSERT_NO_FATAL_FAILURE, folly::IOBuf::create(), folly::empty(), folly::gen::first, makeBuf(), folly::gen::move, and verify().

114  {
116  auto compressor =
117  std::make_unique<ZlibStreamCompressor>(ZlibCompressionType::GZIP, 6);
118 
119  auto first = makeBuf(38);
120  auto out = compressor->compress(first.get(), false);
121 
122  auto second = makeBuf(12);
123  out->prev()->appendChain(compressor->compress(second.get(), false));
124  first->prev()->appendChain(std::move(second));
125 
126  auto third = makeBuf(4096); // Larger than buffer size
127  out->prev()->appendChain(compressor->compress(third.get(), false));
128  first->prev()->appendChain(std::move(third));
129 
130  auto empty = IOBuf::create(0);
131  out->prev()->appendChain(compressor->compress(empty.get(), true));
132 
133  verify(ZlibCompressionType::GZIP, std::move(first), std::move(out));
134  });
135 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< folly::IOBuf > makeBuf(uint32_t size)
Definition: ZlibTests.cpp:26
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
void verify(ZlibCompressionType type, std::unique_ptr< IOBuf > original, std::unique_ptr< IOBuf > compressed)
Definition: ZlibTests.cpp:40
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
constexpr detail::First first
Definition: Base-inl.h:2553
TEST_F ( ZlibTests  ,
CompressDecompressSmallBuffer   
)

Definition at line 137 of file ZlibTests.cpp.

References ASSERT_NO_FATAL_FAILURE, compressThenDecompress(), folly::gen::guard(), makeBuf(), and folly::makeGuard().

137  {
139  auto oldFlag = FLAGS_zlib_compressor_buffer_growth;
140  auto guard = folly::makeGuard([&] {
141  FLAGS_zlib_compressor_buffer_growth = oldFlag;
142  });
143  // NB: This is picked intentionally so we don't generate multiple
144  // zlib flush markers as ZlibStreamDecompressor fatals on them.
145  FLAGS_zlib_compressor_buffer_growth = 10;
146  compressThenDecompress(ZlibCompressionType::GZIP, 4, makeBuf(127));
147  });
148 }
std::unique_ptr< folly::IOBuf > makeBuf(uint32_t size)
Definition: ZlibTests.cpp:26
void compressThenDecompress(ZlibCompressionType type, int level, unique_ptr< IOBuf > buf)
Definition: ZlibTests.cpp:52
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
FOLLY_NODISCARD detail::ScopeGuardImplDecay< F, true > makeGuard(F &&f) noexcept(noexcept(detail::ScopeGuardImplDecay< F, true >(static_cast< F && >(f))))
Definition: ScopeGuard.h:184
#define ASSERT_NO_FATAL_FAILURE(statement)
Definition: gtest.h:2099
void verify ( ZlibCompressionType  type,
std::unique_ptr< IOBuf original,
std::unique_ptr< IOBuf compressed 
)

Definition at line 40 of file ZlibTests.cpp.

References ASSERT_FALSE, ASSERT_TRUE, folly::eq, and type.

Referenced by compressThenDecompress(), and TEST_F().

42  {
43  auto zd = std::make_unique<ZlibStreamDecompressor>(type);
44 
45  auto decompressed = zd->decompress(compressed.get());
46  ASSERT_FALSE(zd->hasError()) << "Decompression error. r=" << zd->getStatus();
47 
49  ASSERT_TRUE(eq(original, decompressed));
50 }
PskType type
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865