mirror of
https://github.com/expo/bsdiff.git
synced 2026-11-14 17:49:03 -10:00
Merge "Fix brotli decompressor interface usage when reading past the end."
This commit is contained in:
commit
a65e5485c9
5 files changed
+81
No files matched your search
@@ -113,6 +113,7 @@ cc_test {
|
||||
test_suites: ["device-tests"],
|
||||
srcs: [
|
||||
"brotli_compressor_unittest.cc",
|
||||
"brotli_decompressor_unittest.cc",
|
||||
"bsdiff_arguments.cc",
|
||||
"bsdiff_arguments_unittest.cc",
|
||||
"bsdiff_unittest.cc",
|
||||
|
||||
@@ -64,6 +64,7 @@ bspatch_src_files := \
|
||||
# Unit test files.
|
||||
bsdiff_common_unittests := \
|
||||
brotli_compressor_unittest.cc \
|
||||
brotli_decompressor_unittest.cc \
|
||||
bsdiff_arguments.cc \
|
||||
bsdiff_arguments_unittest.cc \
|
||||
bsdiff_unittest.cc \
|
||||
|
||||
@@ -48,6 +48,18 @@ bool BrotliDecompressor::Read(uint8_t* output_data, size_t bytes_to_output) {
|
||||
} else if (result == BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT) {
|
||||
LOG(ERROR) << "Decompressor reached EOF while reading from input stream.";
|
||||
return false;
|
||||
} else if (result == BROTLI_DECODER_RESULT_SUCCESS) {
|
||||
// This means that decoding is finished, no more input might be consumed
|
||||
// and no more output will be produced. In the normal case, when there is
|
||||
// more data available than what was requested in this Read() call it
|
||||
// returns BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT.
|
||||
if (available_out > 0) {
|
||||
LOG(ERROR) << "Expected to read " << available_out
|
||||
<< " more bytes but reached the end of compressed brotli "
|
||||
"stream";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
// Copyright 2018 The Chromium OS Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "bsdiff/brotli_decompressor.h"
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace {
|
||||
|
||||
// echo -n "Hello!" | brotli -9 | hexdump -v -e '" " 11/1 "0x%02x, " "\n"'
|
||||
constexpr uint8_t kBrotliHello[] = {
|
||||
0x8b, 0x02, 0x80, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x03,
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace bsdiff {
|
||||
|
||||
class BrotliDecompressorTest : public testing::Test {
|
||||
protected:
|
||||
void SetUp() {
|
||||
decompressor_.reset(new BrotliDecompressor());
|
||||
EXPECT_NE(nullptr, decompressor_.get());
|
||||
}
|
||||
|
||||
std::unique_ptr<BrotliDecompressor> decompressor_;
|
||||
};
|
||||
|
||||
TEST_F(BrotliDecompressorTest, SmokeTest) {
|
||||
EXPECT_TRUE(decompressor_->SetInputData(kBrotliHello, sizeof(kBrotliHello)));
|
||||
std::vector<uint8_t> output_data(6);
|
||||
EXPECT_TRUE(decompressor_->Read(output_data.data(), output_data.size()));
|
||||
std::string hello = "Hello!";
|
||||
EXPECT_EQ(std::vector<uint8_t>(hello.begin(), hello.end()), output_data);
|
||||
}
|
||||
|
||||
TEST_F(BrotliDecompressorTest, ReadingFromEmptyFileTest) {
|
||||
uint8_t data = 0;
|
||||
EXPECT_TRUE(decompressor_->SetInputData(&data, 0));
|
||||
|
||||
uint8_t output_data[10];
|
||||
EXPECT_FALSE(decompressor_->Read(output_data, sizeof(output_data)));
|
||||
}
|
||||
|
||||
// Check that we fail to read from a truncated file.
|
||||
TEST_F(BrotliDecompressorTest, ReadingFromTruncatedFileTest) {
|
||||
// We feed only half of the compressed file.
|
||||
EXPECT_TRUE(
|
||||
decompressor_->SetInputData(kBrotliHello, sizeof(kBrotliHello) / 2));
|
||||
uint8_t output_data[6];
|
||||
EXPECT_FALSE(decompressor_->Read(output_data, sizeof(output_data)));
|
||||
}
|
||||
|
||||
// Check that we fail to read more than it is available in the file.
|
||||
TEST_F(BrotliDecompressorTest, ReadingMoreThanAvailableTest) {
|
||||
EXPECT_TRUE(decompressor_->SetInputData(kBrotliHello, sizeof(kBrotliHello)));
|
||||
uint8_t output_data[1000];
|
||||
EXPECT_FALSE(decompressor_->Read(output_data, sizeof(output_data)));
|
||||
}
|
||||
|
||||
} // namespace bsdiff
|
||||
@@ -151,6 +151,7 @@
|
||||
'includes': ['../common-mk/common_test.gypi'],
|
||||
'sources': [
|
||||
'brotli_compressor_unittest.cc',
|
||||
'brotli_decompressor_unittest.cc',
|
||||
'bsdiff_arguments.cc',
|
||||
'bsdiff_arguments_unittest.cc',
|
||||
'bsdiff_unittest.cc',
|
||||
|
||||
Reference in new issue
Block a user