summaryrefslogtreecommitdiff
path: root/bz2_decompressor_unittest.cc
blob: 6fa819c9fe619bdaaf27328cdad921ce6112d1d7 (plain)
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
// 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/bz2_decompressor.h"

#include <memory>

#include <gtest/gtest.h>

namespace {

// echo -n "Hello!" | bzip2 -9 | hexdump -v -e '"    " 11/1 "0x%02x, " "\n"'
constexpr uint8_t kBZ2Hello[] = {
    0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0x1a,
    0xea, 0x74, 0xba, 0x00, 0x00, 0x00, 0x95, 0x00, 0x20, 0x00, 0x00,
    0x40, 0x02, 0x04, 0xa0, 0x00, 0x21, 0x83, 0x41, 0x9a, 0x02, 0x5c,
    0x2e, 0x2e, 0xe4, 0x8a, 0x70, 0xa1, 0x20, 0x35, 0xd4, 0xe9, 0x74,
};

}  // namespace

namespace bsdiff {

class BZ2DecompressorTest : public testing::Test {
 protected:
  void SetUp() {
    decompressor_.reset(new BZ2Decompressor());
    EXPECT_NE(nullptr, decompressor_.get());
  }

  std::unique_ptr<BZ2Decompressor> decompressor_;
};

TEST_F(BZ2DecompressorTest, 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(BZ2DecompressorTest, ReadingFromTruncatedFileTest) {
  // We feed only half of the compressed file.
  EXPECT_TRUE(decompressor_->SetInputData(kBZ2Hello, sizeof(kBZ2Hello) / 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(BZ2DecompressorTest, ReadingMoreThanAvailableTest) {
  EXPECT_TRUE(decompressor_->SetInputData(kBZ2Hello, sizeof(kBZ2Hello)));
  uint8_t output_data[1000];
  EXPECT_FALSE(decompressor_->Read(output_data, sizeof(output_data)));
}

}  // namespace bsdiff