• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "bsdiff/brotli_decompressor.h"
6 
7 #include <memory>
8 #include <string>
9 #include <vector>
10 
11 #include <gtest/gtest.h>
12 
13 namespace {
14 
15 // echo -n "Hello!" | brotli -9 | hexdump -v -e '"    " 11/1 "0x%02x, " "\n"'
16 constexpr uint8_t kBrotliHello[] = {
17     0x8b, 0x02, 0x80, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x03,
18 };
19 
20 }  // namespace
21 
22 namespace bsdiff {
23 
24 class BrotliDecompressorTest : public testing::Test {
25  protected:
SetUp()26   void SetUp() {
27     decompressor_.reset(new BrotliDecompressor());
28     EXPECT_NE(nullptr, decompressor_.get());
29   }
30 
31   std::unique_ptr<BrotliDecompressor> decompressor_;
32 };
33 
TEST_F(BrotliDecompressorTest,SmokeTest)34 TEST_F(BrotliDecompressorTest, SmokeTest) {
35   EXPECT_TRUE(decompressor_->SetInputData(kBrotliHello, sizeof(kBrotliHello)));
36   std::vector<uint8_t> output_data(6);
37   EXPECT_TRUE(decompressor_->Read(output_data.data(), output_data.size()));
38   std::string hello = "Hello!";
39   EXPECT_EQ(std::vector<uint8_t>(hello.begin(), hello.end()), output_data);
40 }
41 
TEST_F(BrotliDecompressorTest,ReadingFromEmptyFileTest)42 TEST_F(BrotliDecompressorTest, ReadingFromEmptyFileTest) {
43   uint8_t data = 0;
44   EXPECT_TRUE(decompressor_->SetInputData(&data, 0));
45 
46   uint8_t output_data[10];
47   EXPECT_FALSE(decompressor_->Read(output_data, sizeof(output_data)));
48 }
49 
50 // Check that we fail to read from a truncated file.
TEST_F(BrotliDecompressorTest,ReadingFromTruncatedFileTest)51 TEST_F(BrotliDecompressorTest, ReadingFromTruncatedFileTest) {
52   // We feed only half of the compressed file.
53   EXPECT_TRUE(
54       decompressor_->SetInputData(kBrotliHello, sizeof(kBrotliHello) / 2));
55   uint8_t output_data[6];
56   EXPECT_FALSE(decompressor_->Read(output_data, sizeof(output_data)));
57 }
58 
59 // Check that we fail to read more than it is available in the file.
TEST_F(BrotliDecompressorTest,ReadingMoreThanAvailableTest)60 TEST_F(BrotliDecompressorTest, ReadingMoreThanAvailableTest) {
61   EXPECT_TRUE(decompressor_->SetInputData(kBrotliHello, sizeof(kBrotliHello)));
62   uint8_t output_data[1000];
63   EXPECT_FALSE(decompressor_->Read(output_data, sizeof(output_data)));
64 }
65 
66 }  // namespace bsdiff
67