1 //
2 // Copyright (C) 2015 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/payload_consumer/xz_extent_writer.h"
18
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 #include <algorithm>
25
26 #include <base/memory/ptr_util.h>
27 #include <gtest/gtest.h>
28
29 #include "update_engine/common/test_utils.h"
30 #include "update_engine/common/utils.h"
31 #include "update_engine/payload_consumer/fake_extent_writer.h"
32
33 namespace chromeos_update_engine {
34
35 namespace {
36
37 const char kSampleData[] = "Redundaaaaaaaaaaaaaant\n";
38
39 // Compressed data with CRC-32 check, generated with:
40 // echo "Redundaaaaaaaaaaaaaant" | xz -9 --check=crc32 |
41 // hexdump -v -e '" " 12/1 "0x%02x, " "\n"'
42 const uint8_t kCompressedDataCRC32[] = {
43 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x01, 0x69, 0x22, 0xde, 0x36,
44 0x02, 0x00, 0x21, 0x01, 0x1c, 0x00, 0x00, 0x00, 0x10, 0xcf, 0x58, 0xcc,
45 0xe0, 0x00, 0x16, 0x00, 0x10, 0x5d, 0x00, 0x29, 0x19, 0x48, 0x87, 0x88,
46 0xec, 0x49, 0x88, 0x73, 0x8b, 0x5d, 0xa6, 0x46, 0xb4, 0x00, 0x00, 0x00,
47 0x68, 0xfc, 0x7b, 0x25, 0x00, 0x01, 0x28, 0x17, 0x46, 0x9e, 0x08, 0xfe,
48 0x90, 0x42, 0x99, 0x0d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x59, 0x5a,
49 };
50
51 // Compressed data without checksum, generated with:
52 // echo "Redundaaaaaaaaaaaaaant" | xz -9 --check=none |
53 // hexdump -v -e '" " 12/1 "0x%02x, " "\n"'
54 const uint8_t kCompressedDataNoCheck[] = {
55 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x00, 0xff, 0x12, 0xd9, 0x41,
56 0x02, 0x00, 0x21, 0x01, 0x1c, 0x00, 0x00, 0x00, 0x10, 0xcf, 0x58, 0xcc,
57 0xe0, 0x00, 0x16, 0x00, 0x10, 0x5d, 0x00, 0x29, 0x19, 0x48, 0x87, 0x88,
58 0xec, 0x49, 0x88, 0x73, 0x8b, 0x5d, 0xa6, 0x46, 0xb4, 0x00, 0x00, 0x00,
59 0x00, 0x01, 0x24, 0x17, 0x4a, 0xd1, 0xbd, 0x52, 0x06, 0x72, 0x9e, 0x7a,
60 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x59, 0x5a,
61 };
62
63 // Highly redundant data bigger than the internal buffer, generated with:
64 // dd if=/dev/zero bs=30K count=1 | tr '\0' 'a' | xz -9 --check=crc32 |
65 // hexdump -v -e '" " 12/1 "0x%02x, " "\n"'
66 const uint8_t kCompressed30KiBofA[] = {
67 0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x01, 0x69, 0x22, 0xde, 0x36,
68 0x02, 0x00, 0x21, 0x01, 0x1c, 0x00, 0x00, 0x00, 0x10, 0xcf, 0x58, 0xcc,
69 0xe0, 0x77, 0xff, 0x00, 0x41, 0x5d, 0x00, 0x30, 0xef, 0xfb, 0xbf, 0xfe,
70 0xa3, 0xb1, 0x5e, 0xe5, 0xf8, 0x3f, 0xb2, 0xaa, 0x26, 0x55, 0xf8, 0x68,
71 0x70, 0x41, 0x70, 0x15, 0x0f, 0x8d, 0xfd, 0x1e, 0x4c, 0x1b, 0x8a, 0x42,
72 0xb7, 0x19, 0xf4, 0x69, 0x18, 0x71, 0xae, 0x66, 0x23, 0x8a, 0x8a, 0x4d,
73 0x2f, 0xa3, 0x0d, 0xd9, 0x7f, 0xa6, 0xe3, 0x8c, 0x23, 0x11, 0x53, 0xe0,
74 0x59, 0x18, 0xc5, 0x75, 0x8a, 0xe2, 0x76, 0x4c, 0xee, 0x30, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0xf9, 0x47, 0xb5, 0xee, 0x00, 0x01, 0x59, 0x80,
76 0xf0, 0x01, 0x00, 0x00, 0xe0, 0x41, 0x96, 0xde, 0x3e, 0x30, 0x0d, 0x8b,
77 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x59, 0x5a,
78 };
79
80 } // namespace
81
82 class XzExtentWriterTest : public ::testing::Test {
83 protected:
SetUp()84 void SetUp() override {
85 fake_extent_writer_ = new FakeExtentWriter();
86 xz_writer_.reset(new XzExtentWriter(base::WrapUnique(fake_extent_writer_)));
87 }
88
WriteAll(const brillo::Blob & compressed)89 void WriteAll(const brillo::Blob& compressed) {
90 EXPECT_TRUE(xz_writer_->Init(fd_, {}, 1024));
91 EXPECT_TRUE(xz_writer_->Write(compressed.data(), compressed.size()));
92 EXPECT_TRUE(xz_writer_->End());
93
94 EXPECT_TRUE(fake_extent_writer_->InitCalled());
95 EXPECT_TRUE(fake_extent_writer_->EndCalled());
96 }
97
98 // Owned by |xz_writer_|. This object is invalidated after |xz_writer_| is
99 // deleted.
100 FakeExtentWriter* fake_extent_writer_{nullptr};
101 std::unique_ptr<XzExtentWriter> xz_writer_;
102
103 const brillo::Blob sample_data_{
104 std::begin(kSampleData),
105 std::begin(kSampleData) + strlen(kSampleData)};
106 FileDescriptorPtr fd_;
107 };
108
TEST_F(XzExtentWriterTest,CreateAndDestroy)109 TEST_F(XzExtentWriterTest, CreateAndDestroy) {
110 // Test that no Init() or End() called doesn't crash the program.
111 EXPECT_FALSE(fake_extent_writer_->InitCalled());
112 EXPECT_FALSE(fake_extent_writer_->EndCalled());
113 }
114
TEST_F(XzExtentWriterTest,CompressedSampleData)115 TEST_F(XzExtentWriterTest, CompressedSampleData) {
116 WriteAll(brillo::Blob(std::begin(kCompressedDataNoCheck),
117 std::end(kCompressedDataNoCheck)));
118 EXPECT_EQ(sample_data_, fake_extent_writer_->WrittenData());
119 }
120
TEST_F(XzExtentWriterTest,CompressedSampleDataWithCrc)121 TEST_F(XzExtentWriterTest, CompressedSampleDataWithCrc) {
122 WriteAll(brillo::Blob(std::begin(kCompressedDataCRC32),
123 std::end(kCompressedDataCRC32)));
124 EXPECT_EQ(sample_data_, fake_extent_writer_->WrittenData());
125 }
126
TEST_F(XzExtentWriterTest,CompressedDataBiggerThanTheBuffer)127 TEST_F(XzExtentWriterTest, CompressedDataBiggerThanTheBuffer) {
128 // Test that even if the output data is bigger than the internal buffer, all
129 // the data is written.
130 WriteAll(brillo::Blob(std::begin(kCompressed30KiBofA),
131 std::end(kCompressed30KiBofA)));
132 brillo::Blob expected_data(30 * 1024, 'a');
133 EXPECT_EQ(expected_data, fake_extent_writer_->WrittenData());
134 }
135
TEST_F(XzExtentWriterTest,GarbageDataRejected)136 TEST_F(XzExtentWriterTest, GarbageDataRejected) {
137 EXPECT_TRUE(xz_writer_->Init(fd_, {}, 1024));
138 // The sample_data_ is an uncompressed string.
139 EXPECT_FALSE(xz_writer_->Write(sample_data_.data(), sample_data_.size()));
140 EXPECT_TRUE(xz_writer_->End());
141
142 EXPECT_TRUE(fake_extent_writer_->EndCalled());
143 }
144
TEST_F(XzExtentWriterTest,PartialDataIsKept)145 TEST_F(XzExtentWriterTest, PartialDataIsKept) {
146 brillo::Blob compressed(std::begin(kCompressed30KiBofA),
147 std::end(kCompressed30KiBofA));
148 EXPECT_TRUE(xz_writer_->Init(fd_, {}, 1024));
149 for (uint8_t byte : compressed) {
150 EXPECT_TRUE(xz_writer_->Write(&byte, 1));
151 }
152 EXPECT_TRUE(xz_writer_->End());
153
154 // The sample_data_ is an uncompressed string.
155 brillo::Blob expected_data(30 * 1024, 'a');
156 EXPECT_EQ(expected_data, fake_extent_writer_->WrittenData());
157 }
158
159 } // namespace chromeos_update_engine
160