Commit 9dc004f8 authored by Stella Lau's avatar Stella Lau Committed by Facebook Github Bot

Add LZMA streaming interface

Summary:
- Replace LZMA2Codec with LZMA2StreamCodec
- Update tests to reflect LZMA2_VARINT_SIZE requiring data length

Reviewed By: terrelln

Differential Revision: D5625388

fbshipit-source-id: 3303c6dda5d41f40615c87504a46923815b0b716
parent 4d4a549d
This diff is collapsed.
...@@ -175,20 +175,22 @@ static std::vector<CodecType> availableStreamCodecs() { ...@@ -175,20 +175,22 @@ static std::vector<CodecType> availableStreamCodecs() {
} }
TEST(CompressionTestNeedsUncompressedLength, Simple) { TEST(CompressionTestNeedsUncompressedLength, Simple) {
static const struct { CodecType type; bool needsUncompressedLength; } static const struct {
expectations[] = { CodecType type;
{ CodecType::NO_COMPRESSION, false }, bool needsUncompressedLength;
{ CodecType::LZ4, true }, } expectations[] = {
{ CodecType::SNAPPY, false }, {CodecType::NO_COMPRESSION, false},
{ CodecType::ZLIB, false }, {CodecType::LZ4, true},
{ CodecType::LZ4_VARINT_SIZE, false }, {CodecType::SNAPPY, false},
{ CodecType::LZMA2, false }, {CodecType::ZLIB, false},
{ CodecType::LZMA2_VARINT_SIZE, false }, {CodecType::LZ4_VARINT_SIZE, false},
{ CodecType::ZSTD, false }, {CodecType::LZMA2, false},
{ CodecType::GZIP, false }, {CodecType::LZMA2_VARINT_SIZE, false},
{ CodecType::LZ4_FRAME, false }, {CodecType::ZSTD, false},
{ CodecType::BZIP2, false }, {CodecType::GZIP, false},
}; {CodecType::LZ4_FRAME, false},
{CodecType::BZIP2, false},
};
for (auto const& test : expectations) { for (auto const& test : expectations) {
if (hasCodec(test.type)) { if (hasCodec(test.type)) {
...@@ -360,7 +362,19 @@ INSTANTIATE_TEST_CASE_P( ...@@ -360,7 +362,19 @@ INSTANTIATE_TEST_CASE_P(
testing::ValuesIn(supportedCodecs({ testing::ValuesIn(supportedCodecs({
CodecType::LZ4_VARINT_SIZE, CodecType::LZ4_VARINT_SIZE,
CodecType::LZMA2_VARINT_SIZE, CodecType::LZMA2_VARINT_SIZE,
})))); }))));
TEST(LZMATest, UncompressBadVarint) {
if (hasStreamCodec(CodecType::LZMA2_VARINT_SIZE)) {
std::string const str(kMaxVarintLength64 * 2, '\xff');
ByteRange input((folly::StringPiece(str)));
auto codec = getStreamCodec(CodecType::LZMA2_VARINT_SIZE);
auto buffer = IOBuf::create(16);
buffer->append(buffer->capacity());
MutableByteRange output{buffer->writableData(), buffer->length()};
EXPECT_THROW(codec->uncompressStream(input, output), std::runtime_error);
}
}
class CompressionCorruptionTest : public testing::TestWithParam<CodecType> { class CompressionCorruptionTest : public testing::TestWithParam<CodecType> {
protected: protected:
...@@ -449,6 +463,26 @@ class StreamingUnitTest : public testing::TestWithParam<CodecType> { ...@@ -449,6 +463,26 @@ class StreamingUnitTest : public testing::TestWithParam<CodecType> {
std::unique_ptr<StreamCodec> codec_; std::unique_ptr<StreamCodec> codec_;
}; };
TEST(StreamingUnitTest, needsDataLength) {
static const struct {
CodecType type;
bool needsDataLength;
} expectations[] = {
{CodecType::ZLIB, false},
{CodecType::GZIP, false},
{CodecType::LZMA2, false},
{CodecType::LZMA2_VARINT_SIZE, true},
{CodecType::ZSTD, false},
};
for (auto const& test : expectations) {
if (hasStreamCodec(test.type)) {
EXPECT_EQ(
getStreamCodec(test.type)->needsDataLength(), test.needsDataLength);
}
}
}
TEST_P(StreamingUnitTest, maxCompressedLength) { TEST_P(StreamingUnitTest, maxCompressedLength) {
EXPECT_EQ(0, codec_->maxCompressedLength(0)); EXPECT_EQ(0, codec_->maxCompressedLength(0));
for (uint64_t const length : {1, 10, 100, 1000, 10000, 100000, 1000000}) { for (uint64_t const length : {1, 10, 100, 1000, 10000, 100000, 1000000}) {
...@@ -483,22 +517,24 @@ TEST_P(StreamingUnitTest, emptyData) { ...@@ -483,22 +517,24 @@ TEST_P(StreamingUnitTest, emptyData) {
MutableByteRange output{}; MutableByteRange output{};
// Test compressing empty data in one pass // Test compressing empty data in one pass
EXPECT_TRUE(codec_->compressStream(input, output, StreamCodec::FlushOp::END)); if (!codec_->needsDataLength()) {
EXPECT_TRUE(
codec_->compressStream(input, output, StreamCodec::FlushOp::END));
}
codec_->resetStream(0); codec_->resetStream(0);
EXPECT_TRUE(codec_->compressStream(input, output, StreamCodec::FlushOp::END)); EXPECT_TRUE(codec_->compressStream(input, output, StreamCodec::FlushOp::END));
codec_->resetStream();
output = {buffer->writableData(), buffer->length()}; output = {buffer->writableData(), buffer->length()};
EXPECT_TRUE(codec_->compressStream(input, output, StreamCodec::FlushOp::END)); EXPECT_TRUE(codec_->compressStream(input, output, StreamCodec::FlushOp::END));
EXPECT_EQ(buffer->length(), output.size()); EXPECT_EQ(buffer->length(), output.size());
// Test compressing empty data with multiple calls to compressStream() // Test compressing empty data with multiple calls to compressStream()
codec_->resetStream(); codec_->resetStream(0);
output = {}; output = {};
EXPECT_FALSE(codec_->compressStream(input, output)); EXPECT_FALSE(codec_->compressStream(input, output));
EXPECT_TRUE( EXPECT_TRUE(
codec_->compressStream(input, output, StreamCodec::FlushOp::FLUSH)); codec_->compressStream(input, output, StreamCodec::FlushOp::FLUSH));
EXPECT_TRUE(codec_->compressStream(input, output, StreamCodec::FlushOp::END)); EXPECT_TRUE(codec_->compressStream(input, output, StreamCodec::FlushOp::END));
codec_->resetStream(); codec_->resetStream(0);
output = {buffer->writableData(), buffer->length()}; output = {buffer->writableData(), buffer->length()};
EXPECT_FALSE(codec_->compressStream(input, output)); EXPECT_FALSE(codec_->compressStream(input, output));
EXPECT_TRUE( EXPECT_TRUE(
...@@ -541,7 +577,11 @@ TEST_P(StreamingUnitTest, noForwardProgressOkay) { ...@@ -541,7 +577,11 @@ TEST_P(StreamingUnitTest, noForwardProgressOkay) {
MutableByteRange emptyOutput; MutableByteRange emptyOutput;
// Compress some data to avoid empty data special casing // Compress some data to avoid empty data special casing
codec_->resetStream(); if (codec_->needsDataLength()) {
codec_->resetStream(inBuffer->computeChainDataLength());
} else {
codec_->resetStream();
}
while (!input.empty()) { while (!input.empty()) {
codec_->compressStream(input, output); codec_->compressStream(input, output);
} }
...@@ -549,7 +589,11 @@ TEST_P(StreamingUnitTest, noForwardProgressOkay) { ...@@ -549,7 +589,11 @@ TEST_P(StreamingUnitTest, noForwardProgressOkay) {
codec_->compressStream(emptyInput, emptyOutput); codec_->compressStream(emptyInput, emptyOutput);
codec_->compressStream(emptyInput, emptyOutput, StreamCodec::FlushOp::FLUSH); codec_->compressStream(emptyInput, emptyOutput, StreamCodec::FlushOp::FLUSH);
codec_->resetStream(); if (codec_->needsDataLength()) {
codec_->resetStream(inBuffer->computeChainDataLength());
} else {
codec_->resetStream();
}
input = inBuffer->coalesce(); input = inBuffer->coalesce();
output = {outBuffer->writableTail(), outBuffer->tailroom()}; output = {outBuffer->writableTail(), outBuffer->tailroom()};
while (!input.empty()) { while (!input.empty()) {
...@@ -590,6 +634,20 @@ TEST_P(StreamingUnitTest, stateTransitions) { ...@@ -590,6 +634,20 @@ TEST_P(StreamingUnitTest, stateTransitions) {
auto output = empty ? MutableByteRange{} : out; auto output = empty ? MutableByteRange{} : out;
return codec_->compressStream(input, output, flushOp); return codec_->compressStream(input, output, flushOp);
}; };
auto compress_all = [&](bool expect,
StreamCodec::FlushOp flushOp =
StreamCodec::FlushOp::NONE,
bool empty = false) {
auto input = in;
auto output = empty ? MutableByteRange{} : out;
while (!input.empty()) {
if (expect) {
EXPECT_TRUE(codec_->compressStream(input, output, flushOp));
} else {
EXPECT_FALSE(codec_->compressStream(input, output, flushOp));
}
}
};
auto uncompress = [&]( auto uncompress = [&](
StreamCodec::FlushOp flushOp = StreamCodec::FlushOp::NONE, StreamCodec::FlushOp flushOp = StreamCodec::FlushOp::NONE,
bool empty = false) { bool empty = false) {
...@@ -599,12 +657,21 @@ TEST_P(StreamingUnitTest, stateTransitions) { ...@@ -599,12 +657,21 @@ TEST_P(StreamingUnitTest, stateTransitions) {
}; };
// compression flow // compression flow
codec_->resetStream(); if (!codec_->needsDataLength()) {
EXPECT_FALSE(compress()); codec_->resetStream();
EXPECT_FALSE(compress()); EXPECT_FALSE(compress());
EXPECT_TRUE(compress(StreamCodec::FlushOp::FLUSH)); EXPECT_FALSE(compress());
EXPECT_FALSE(compress()); EXPECT_TRUE(compress(StreamCodec::FlushOp::FLUSH));
EXPECT_TRUE(compress(StreamCodec::FlushOp::END)); EXPECT_FALSE(compress());
EXPECT_TRUE(compress(StreamCodec::FlushOp::END));
}
codec_->resetStream(in.size() * 5);
compress_all(false);
compress_all(false);
compress_all(true, StreamCodec::FlushOp::FLUSH);
compress_all(false);
compress_all(true, StreamCodec::FlushOp::END);
// uncompression flow // uncompression flow
codec_->resetStream(); codec_->resetStream();
EXPECT_FALSE(uncompress(StreamCodec::FlushOp::NONE, true)); EXPECT_FALSE(uncompress(StreamCodec::FlushOp::NONE, true));
...@@ -617,34 +684,40 @@ TEST_P(StreamingUnitTest, stateTransitions) { ...@@ -617,34 +684,40 @@ TEST_P(StreamingUnitTest, stateTransitions) {
codec_->resetStream(); codec_->resetStream();
EXPECT_TRUE(uncompress(StreamCodec::FlushOp::FLUSH)); EXPECT_TRUE(uncompress(StreamCodec::FlushOp::FLUSH));
// compress -> uncompress // compress -> uncompress
codec_->resetStream(); codec_->resetStream(in.size());
EXPECT_FALSE(compress()); EXPECT_FALSE(compress());
EXPECT_THROW(uncompress(), std::logic_error); EXPECT_THROW(uncompress(), std::logic_error);
// uncompress -> compress // uncompress -> compress
codec_->resetStream(); codec_->resetStream(inBuffer->computeChainDataLength());
EXPECT_TRUE(uncompress(StreamCodec::FlushOp::FLUSH)); EXPECT_TRUE(uncompress(StreamCodec::FlushOp::FLUSH));
EXPECT_THROW(compress(), std::logic_error); EXPECT_THROW(compress(), std::logic_error);
// end -> compress // end -> compress
codec_->resetStream(); if (!codec_->needsDataLength()) {
EXPECT_FALSE(compress()); codec_->resetStream();
EXPECT_TRUE(compress(StreamCodec::FlushOp::END)); EXPECT_FALSE(compress());
EXPECT_TRUE(compress(StreamCodec::FlushOp::END));
EXPECT_THROW(compress(), std::logic_error);
}
codec_->resetStream(in.size() * 2);
compress_all(false);
compress_all(true, StreamCodec::FlushOp::END);
EXPECT_THROW(compress(), std::logic_error); EXPECT_THROW(compress(), std::logic_error);
// end -> uncompress // end -> uncompress
codec_->resetStream(); codec_->resetStream();
EXPECT_TRUE(uncompress(StreamCodec::FlushOp::FLUSH)); EXPECT_TRUE(uncompress(StreamCodec::FlushOp::FLUSH));
EXPECT_THROW(uncompress(), std::logic_error); EXPECT_THROW(uncompress(), std::logic_error);
// flush -> compress // flush -> compress
codec_->resetStream(); codec_->resetStream(in.size());
EXPECT_FALSE(compress(StreamCodec::FlushOp::FLUSH, true)); EXPECT_FALSE(compress(StreamCodec::FlushOp::FLUSH, true));
EXPECT_THROW(compress(), std::logic_error); EXPECT_THROW(compress(), std::logic_error);
// flush -> end // flush -> end
codec_->resetStream(); codec_->resetStream(in.size());
EXPECT_FALSE(compress(StreamCodec::FlushOp::FLUSH, true)); EXPECT_FALSE(compress(StreamCodec::FlushOp::FLUSH, true));
EXPECT_THROW(compress(StreamCodec::FlushOp::END), std::logic_error); EXPECT_THROW(compress(StreamCodec::FlushOp::END), std::logic_error);
// undefined -> compress // undefined -> compress
codec_->compress(inBuffer.get()); codec_->compress(inBuffer.get());
EXPECT_THROW(compress(), std::logic_error); EXPECT_THROW(compress(), std::logic_error);
codec_->uncompress(compressed.get()); codec_->uncompress(compressed.get(), inBuffer->computeChainDataLength());
EXPECT_THROW(compress(), std::logic_error); EXPECT_THROW(compress(), std::logic_error);
// undefined -> undefined // undefined -> undefined
codec_->uncompress(compressed.get()); codec_->uncompress(compressed.get());
...@@ -738,7 +811,11 @@ void StreamingCompressionTest::runResetStreamTest(DataHolder const& dh) { ...@@ -738,7 +811,11 @@ void StreamingCompressionTest::runResetStreamTest(DataHolder const& dh) {
codec_->resetStream(uncompressedLength_); codec_->resetStream(uncompressedLength_);
compressSome(codec_.get(), input, chunkSize_, StreamCodec::FlushOp::NONE); compressSome(codec_.get(), input, chunkSize_, StreamCodec::FlushOp::NONE);
// Reset stream and compress all // Reset stream and compress all
codec_->resetStream(); if (codec_->needsDataLength()) {
codec_->resetStream(uncompressedLength_);
} else {
codec_->resetStream();
}
auto compressed = auto compressed =
compressSome(codec_.get(), input, chunkSize_, StreamCodec::FlushOp::END); compressSome(codec_.get(), input, chunkSize_, StreamCodec::FlushOp::END);
auto const uncompressed = codec_->uncompress(compressed.get(), input.size()); auto const uncompressed = codec_->uncompress(compressed.get(), input.size());
...@@ -820,7 +897,11 @@ void StreamingCompressionTest::runFlushTest(DataHolder const& dh) { ...@@ -820,7 +897,11 @@ void StreamingCompressionTest::runFlushTest(DataHolder const& dh) {
auto const inputs = split(dh.data(uncompressedLength_)); auto const inputs = split(dh.data(uncompressedLength_));
auto uncodec = getStreamCodec(codec_->type()); auto uncodec = getStreamCodec(codec_->type());
codec_->resetStream(); if (codec_->needsDataLength()) {
codec_->resetStream(uncompressedLength_);
} else {
codec_->resetStream();
}
for (auto input : inputs) { for (auto input : inputs) {
// Compress some data and flush the stream // Compress some data and flush the stream
auto compressed = compressSome( auto compressed = compressSome(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment