Merge "Fix brotli decompressor interface usage when reading past the end." am: a65e5485c9

am: 48d4e28696

Change-Id: Ied60f019836327b3c1fe349434420cbfb760820c
This commit is contained in:
Alex Deymo authored and android-build-merger committed 2018-03-27 18:50:16 +00:00
commit 85fd25412a
5 files changed
+81

No files matched your search

+1
View File
@@ -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",
+1
View File
@@ -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 \
+12
View File
@@ -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;
+66
View File
@@ -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
+1
View File
@@ -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',