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/bz2_decompressor.h"
6
7 #include <memory>
8
9 #include <gtest/gtest.h>
10
11 namespace {
12
13 // echo -n "Hello!" | bzip2 -9 | hexdump -v -e '" " 11/1 "0x%02x, " "\n"'
14 constexpr uint8_t kBZ2Hello[] = {
15 0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0x1a,
16 0xea, 0x74, 0xba, 0x00, 0x00, 0x00, 0x95, 0x00, 0x20, 0x00, 0x00,
17 0x40, 0x02, 0x04, 0xa0, 0x00, 0x21, 0x83, 0x41, 0x9a, 0x02, 0x5c,
18 0x2e, 0x2e, 0xe4, 0x8a, 0x70, 0xa1, 0x20, 0x35, 0xd4, 0xe9, 0x74,
19 };
20
21 } // namespace
22
23 namespace bsdiff {
24
25 class BZ2DecompressorTest : public testing::Test {
26 protected:
SetUp()27 void SetUp() {
28 decompressor_.reset(new BZ2Decompressor());
29 EXPECT_NE(nullptr, decompressor_.get());
30 }
31
32 std::unique_ptr<BZ2Decompressor> decompressor_;
33 };
34
TEST_F(BZ2DecompressorTest,ReadingFromEmptyFileTest)35 TEST_F(BZ2DecompressorTest, ReadingFromEmptyFileTest) {
36 uint8_t data = 0;
37 EXPECT_TRUE(decompressor_->SetInputData(&data, 0));
38
39 uint8_t output_data[10];
40 EXPECT_FALSE(decompressor_->Read(output_data, sizeof(output_data)));
41 }
42
43 // Check that we fail to read from a truncated file.
TEST_F(BZ2DecompressorTest,ReadingFromTruncatedFileTest)44 TEST_F(BZ2DecompressorTest, ReadingFromTruncatedFileTest) {
45 // We feed only half of the compressed file.
46 EXPECT_TRUE(decompressor_->SetInputData(kBZ2Hello, sizeof(kBZ2Hello) / 2));
47 uint8_t output_data[6];
48 EXPECT_FALSE(decompressor_->Read(output_data, sizeof(output_data)));
49 }
50
51 // Check that we fail to read more than it is available in the file.
TEST_F(BZ2DecompressorTest,ReadingMoreThanAvailableTest)52 TEST_F(BZ2DecompressorTest, ReadingMoreThanAvailableTest) {
53 EXPECT_TRUE(decompressor_->SetInputData(kBZ2Hello, sizeof(kBZ2Hello)));
54 uint8_t output_data[1000];
55 EXPECT_FALSE(decompressor_->Read(output_data, sizeof(output_data)));
56 }
57
58 } // namespace bsdiff
59