1 // Copyright (C) 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <libsnapshot/snapshot.h>
16
17 #include <unordered_set>
18
19 #include <android-base/file.h>
20 #include <gtest/gtest.h>
21 #include <libsnapshot/cow_writer.h>
22 #include <payload_consumer/file_descriptor.h>
23
24 namespace android {
25 namespace snapshot {
26
27 using android::base::unique_fd;
28 using chromeos_update_engine::FileDescriptor;
29
30 static constexpr uint32_t kBlockSize = 4096;
31 static constexpr size_t kBlockCount = 10;
32
33 class OfflineSnapshotTest : public ::testing::Test {
34 protected:
SetUp()35 virtual void SetUp() override {
36 base_ = std::make_unique<TemporaryFile>();
37 ASSERT_GE(base_->fd, 0) << strerror(errno);
38
39 cow_ = std::make_unique<TemporaryFile>();
40 ASSERT_GE(cow_->fd, 0) << strerror(errno);
41
42 WriteBaseDevice();
43 }
44
TearDown()45 virtual void TearDown() override {
46 base_ = nullptr;
47 cow_ = nullptr;
48 base_blocks_ = {};
49 }
50
WriteBaseDevice()51 void WriteBaseDevice() {
52 unique_fd random(open("/dev/urandom", O_RDONLY));
53 ASSERT_GE(random, 0);
54
55 for (size_t i = 0; i < kBlockCount; i++) {
56 std::string block(kBlockSize, 0);
57 ASSERT_TRUE(android::base::ReadFully(random, block.data(), block.size()));
58 ASSERT_TRUE(android::base::WriteFully(base_->fd, block.data(), block.size()));
59 base_blocks_.emplace_back(std::move(block));
60 }
61 ASSERT_EQ(fsync(base_->fd), 0);
62 }
63
WriteCow(ISnapshotWriter * writer)64 void WriteCow(ISnapshotWriter* writer) {
65 std::string new_block = MakeNewBlockString();
66
67 ASSERT_TRUE(writer->AddCopy(3, 0));
68 ASSERT_TRUE(writer->AddRawBlocks(5, new_block.data(), new_block.size()));
69 ASSERT_TRUE(writer->AddZeroBlocks(7, 2));
70 ASSERT_TRUE(writer->Finalize());
71 }
72
TestBlockReads(ISnapshotWriter * writer)73 void TestBlockReads(ISnapshotWriter* writer) {
74 auto reader = writer->OpenReader();
75 ASSERT_NE(reader, nullptr);
76
77 // Test that unchanged blocks are not modified.
78 std::unordered_set<size_t> changed_blocks = {3, 5, 7, 8};
79 for (size_t i = 0; i < kBlockCount; i++) {
80 if (changed_blocks.count(i)) {
81 continue;
82 }
83
84 std::string block(kBlockSize, 0);
85 ASSERT_EQ(reader->Seek(i * kBlockSize, SEEK_SET), i * kBlockSize);
86 ASSERT_EQ(reader->Read(block.data(), block.size()), kBlockSize);
87 ASSERT_EQ(block, base_blocks_[i]);
88 }
89
90 // Test that we can read back our modified blocks.
91 std::string block(kBlockSize, 0);
92 ASSERT_EQ(reader->Seek(3 * kBlockSize, SEEK_SET), 3 * kBlockSize);
93 ASSERT_EQ(reader->Read(block.data(), block.size()), kBlockSize);
94 ASSERT_EQ(block, base_blocks_[0]);
95
96 ASSERT_EQ(reader->Seek(5 * kBlockSize, SEEK_SET), 5 * kBlockSize);
97 ASSERT_EQ(reader->Read(block.data(), block.size()), kBlockSize);
98 ASSERT_EQ(block, MakeNewBlockString());
99
100 std::string two_blocks(kBlockSize * 2, 0x7f);
101 std::string zeroes(kBlockSize * 2, 0);
102 ASSERT_EQ(reader->Seek(7 * kBlockSize, SEEK_SET), 7 * kBlockSize);
103 ASSERT_EQ(reader->Read(two_blocks.data(), two_blocks.size()), two_blocks.size());
104 ASSERT_EQ(two_blocks, zeroes);
105 }
106
TestByteReads(ISnapshotWriter * writer)107 void TestByteReads(ISnapshotWriter* writer) {
108 auto reader = writer->OpenReader();
109 ASSERT_NE(reader, nullptr);
110
111 std::string blob(kBlockSize * 3, 'x');
112
113 // Test that we can read in the middle of a block.
114 static constexpr size_t kOffset = 970;
115 off64_t offset = 3 * kBlockSize + kOffset;
116 ASSERT_EQ(reader->Seek(0, SEEK_SET), 0);
117 ASSERT_EQ(reader->Seek(offset, SEEK_CUR), offset);
118 ASSERT_EQ(reader->Read(blob.data(), blob.size()), blob.size());
119 ASSERT_EQ(blob.substr(0, 100), base_blocks_[0].substr(kOffset, 100));
120 ASSERT_EQ(blob.substr(kBlockSize - kOffset, kBlockSize), base_blocks_[4]);
121 ASSERT_EQ(blob.substr(kBlockSize * 2 - kOffset, 100), MakeNewBlockString().substr(0, 100));
122 ASSERT_EQ(blob.substr(blob.size() - kOffset), base_blocks_[6].substr(0, kOffset));
123
124 // Pull a random byte from the compressed block.
125 char value;
126 offset = 5 * kBlockSize + 1000;
127 ASSERT_EQ(reader->Seek(offset, SEEK_SET), offset);
128 ASSERT_EQ(reader->Read(&value, sizeof(value)), sizeof(value));
129 ASSERT_EQ(value, MakeNewBlockString()[1000]);
130 }
131
TestReads(ISnapshotWriter * writer)132 void TestReads(ISnapshotWriter* writer) {
133 ASSERT_NO_FATAL_FAILURE(TestBlockReads(writer));
134 ASSERT_NO_FATAL_FAILURE(TestByteReads(writer));
135 }
136
MakeNewBlockString()137 std::string MakeNewBlockString() {
138 std::string new_block = "This is a new block";
139 new_block.resize(kBlockSize / 2, '*');
140 new_block.resize(kBlockSize, '!');
141 return new_block;
142 }
143
144 std::unique_ptr<TemporaryFile> base_;
145 std::unique_ptr<TemporaryFile> cow_;
146 std::vector<std::string> base_blocks_;
147 };
148
TEST_F(OfflineSnapshotTest,CompressedSnapshot)149 TEST_F(OfflineSnapshotTest, CompressedSnapshot) {
150 CowOptions options;
151 options.compression = "gz";
152 options.max_blocks = {kBlockCount};
153 options.scratch_space = false;
154
155 unique_fd cow_fd(dup(cow_->fd));
156 ASSERT_GE(cow_fd, 0);
157
158 auto writer = std::make_unique<CompressedSnapshotWriter>(options);
159 writer->SetSourceDevice(base_->path);
160 ASSERT_TRUE(writer->SetCowDevice(std::move(cow_fd)));
161 ASSERT_TRUE(writer->Initialize());
162 ASSERT_NO_FATAL_FAILURE(WriteCow(writer.get()));
163 ASSERT_NO_FATAL_FAILURE(TestReads(writer.get()));
164 }
165
166 } // namespace snapshot
167 } // namespace android
168