summaryrefslogtreecommitdiff
path: root/libunwindstack/tests/MemoryXzTest.cpp
blob: 6597240621f384541005a242803807911337ff90 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android-base/file.h>

#include <gtest/gtest.h>

#include <MemoryBuffer.h>
#include <MemoryXz.h>

namespace unwindstack {

class MemoryXzTest : public ::testing::Test {
 protected:
  void SetUp() override { expected_content_ = ReadFile("boot_arm.oat.gnu_debugdata"); }

  static std::unique_ptr<MemoryBuffer> ReadFile(const char* filename) {
    std::string dir = android::base::GetExecutableDirectory() + "/tests/files/";
    std::string data;  // NB: It is actually binary data.
    EXPECT_TRUE(android::base::ReadFileToString(dir + filename, &data)) << filename;
    EXPECT_GT(data.size(), 0u);
    auto memory = std::make_unique<MemoryBuffer>(data.size());
    EXPECT_EQ(data.size(), memory->Size());
    memcpy(memory->GetPtr(0), data.data(), data.size());
    return memory;
  }

  void VerifyContent(MemoryXz& xz, uint64_t offset, uint64_t size) {
    EXPECT_EQ(xz.Size(), expected_content_->Size());
    EXPECT_LE(offset + size, expected_content_->Size());
    std::vector<uint8_t> seen_content(size);
    xz.ReadFully(offset, seen_content.data(), size);
    EXPECT_EQ(memcmp(seen_content.data(), expected_content_->GetPtr(offset), size), 0);
  }

  std::unique_ptr<MemoryBuffer> expected_content_;
};

// Test the expected random-accessible format.
TEST_F(MemoryXzTest, Decompress) {
  auto compressed = ReadFile("boot_arm.oat.gnu_debugdata.xz");
  MemoryXz xz(compressed.get(), 0, compressed->Size(), "boot_arm.oat");
  EXPECT_TRUE(xz.Init());
  EXPECT_GT(xz.BlockCount(), 1u);
  EXPECT_EQ(xz.BlockSize(), 16 * 1024u);
  EXPECT_EQ(xz.MemoryUsage(), 0u);
  VerifyContent(xz, 0, expected_content_->Size());
  EXPECT_EQ(xz.MemoryUsage(), xz.Size());
}

// Test one big monolithic compressed block.
TEST_F(MemoryXzTest, DecompressOneBlock) {
  auto compressed = ReadFile("boot_arm.oat.gnu_debugdata.xz.one-block");
  MemoryXz xz(compressed.get(), 0, compressed->Size(), "boot_arm.oat");
  EXPECT_TRUE(xz.Init());
  EXPECT_EQ(xz.BlockCount(), 1u);
  EXPECT_GT(xz.BlockSize(), xz.Size());
  EXPECT_EQ(xz.MemoryUsage(), 0u);
  VerifyContent(xz, 0, expected_content_->Size());
  EXPECT_EQ(xz.MemoryUsage(), xz.Size());
}

// Test fallback (non-consistent block sizes).
TEST_F(MemoryXzTest, DecompressOddSizes) {
  auto compressed = ReadFile("boot_arm.oat.gnu_debugdata.xz.odd-sizes");
  MemoryXz xz(compressed.get(), 0, compressed->Size(), "boot_arm.oat");
  EXPECT_TRUE(xz.Init());
  EXPECT_EQ(xz.BlockCount(), 1u);
  EXPECT_GT(xz.BlockSize(), xz.Size());
  EXPECT_EQ(xz.MemoryUsage(), xz.Size());
  VerifyContent(xz, 0, expected_content_->Size());
}

// Test fallback (non-power-of-2 block size).
TEST_F(MemoryXzTest, DecompressNonPower) {
  auto compressed = ReadFile("boot_arm.oat.gnu_debugdata.xz.non-power");
  MemoryXz xz(compressed.get(), 0, compressed->Size(), "boot_arm.oat");
  EXPECT_TRUE(xz.Init());
  EXPECT_EQ(xz.BlockCount(), 1u);
  EXPECT_GT(xz.BlockSize(), xz.Size());
  EXPECT_EQ(xz.MemoryUsage(), xz.Size());
  VerifyContent(xz, 0, expected_content_->Size());
}

// Read first byte of some blocks.
TEST_F(MemoryXzTest, ReadFirstByte) {
  auto compressed = ReadFile("boot_arm.oat.gnu_debugdata.xz");
  MemoryXz xz(compressed.get(), 0, compressed->Size(), "boot_arm.oat");
  EXPECT_TRUE(xz.Init());
  EXPECT_GT(xz.BlockCount(), 1u);
  EXPECT_EQ(xz.BlockSize(), 16 * 1024u);
  for (size_t i = 0; i < xz.BlockCount(); i += 3) {
    VerifyContent(xz, i * xz.BlockSize(), 1);
  }
  EXPECT_LT(xz.MemoryUsage(), xz.Size());  // We didn't decompress all blocks.
}

// Read last byte of some blocks.
TEST_F(MemoryXzTest, ReadLastByte) {
  auto compressed = ReadFile("boot_arm.oat.gnu_debugdata.xz");
  MemoryXz xz(compressed.get(), 0, compressed->Size(), "boot_arm.oat");
  EXPECT_TRUE(xz.Init());
  EXPECT_GT(xz.BlockCount(), 1u);
  EXPECT_EQ(xz.BlockSize(), 16 * 1024u);
  for (size_t i = 1; i < xz.BlockCount(); i += 3) {
    VerifyContent(xz, i * xz.BlockSize() - 1, 1);
  }
  EXPECT_LT(xz.MemoryUsage(), xz.Size());  // We didn't decompress all blocks.
}

// Read across boundary of blocks.
TEST_F(MemoryXzTest, ReadBoundary) {
  auto compressed = ReadFile("boot_arm.oat.gnu_debugdata.xz");
  MemoryXz xz(compressed.get(), 0, compressed->Size(), "boot_arm.oat");
  EXPECT_TRUE(xz.Init());
  EXPECT_GT(xz.BlockCount(), 1u);
  EXPECT_EQ(xz.BlockSize(), 16 * 1024u);
  for (size_t i = 1; i < xz.BlockCount(); i += 3) {
    VerifyContent(xz, i * xz.BlockSize() - 1, 2);
  }
  EXPECT_LT(xz.MemoryUsage(), xz.Size());  // We didn't decompress all blocks.
}

}  // namespace unwindstack