lin
2025-07-30 fcd736bf35fd93b563e9bbf594f2aa7b62028cc9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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