blob: 6e1f5a3b308e9b7280f2959a4523b992c16cd947 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 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//
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080016
17#include <string.h>
18#include <unistd.h>
Darin Petkov7ed561b2011-10-04 02:59:03 -070019
Ben Chanab5a0af2017-10-12 14:57:50 -070020#include <memory>
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080021#include <string>
22#include <vector>
Darin Petkov7ed561b2011-10-04 02:59:03 -070023
Kelvin Zhangc5803b72021-09-02 09:06:16 -070024#include <brillo/secure_blob.h>
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080025#include <gtest/gtest.h>
Darin Petkov7ed561b2011-10-04 02:59:03 -070026
Alex Deymo39910dc2015-11-09 17:04:30 -080027#include "update_engine/common/test_utils.h"
Alex Deymo246bf212016-03-22 19:27:33 -070028#include "update_engine/payload_consumer/bzip_extent_writer.h"
29#include "update_engine/payload_consumer/extent_writer.h"
30#include "update_engine/payload_consumer/xz_extent_writer.h"
Alex Deymo0bc26112015-10-19 20:54:57 -070031#include "update_engine/payload_generator/bzip.h"
Alex Deymo246bf212016-03-22 19:27:33 -070032#include "update_engine/payload_generator/xz.h"
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080033
Alex Deymo10875d92014-11-10 21:52:57 -080034using chromeos_update_engine::test_utils::kRandomString;
Amin Hassanicd7edbe2017-09-18 17:05:02 -070035using google::protobuf::RepeatedPtrField;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080036using std::string;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080037
38namespace chromeos_update_engine {
39
Alex Deymo246bf212016-03-22 19:27:33 -070040namespace {
41
42// ExtentWriter class that writes to memory, used to test the decompression
43// step with the corresponding extent writer.
44class MemoryExtentWriter : public ExtentWriter {
45 public:
46 // Creates the ExtentWriter that will write all the bytes to the passed |data|
47 // blob.
48 explicit MemoryExtentWriter(brillo::Blob* data) : data_(data) {
49 data_->clear();
50 }
51 ~MemoryExtentWriter() override = default;
52
Kelvin Zhang4d22ca22021-02-09 14:06:25 -050053 bool Init(const RepeatedPtrField<Extent>& extents,
Alex Deymo246bf212016-03-22 19:27:33 -070054 uint32_t block_size) override {
55 return true;
56 }
57 bool Write(const void* bytes, size_t count) override {
Alex Deymo246bf212016-03-22 19:27:33 -070058 data_->insert(data_->end(),
59 static_cast<const uint8_t*>(bytes),
60 static_cast<const uint8_t*>(bytes) + count);
61 return true;
62 }
Alex Deymo246bf212016-03-22 19:27:33 -070063
64 private:
65 brillo::Blob* data_;
66};
67
68template <typename W>
69bool DecompressWithWriter(const brillo::Blob& in, brillo::Blob* out) {
Kelvin Zhangc5803b72021-09-02 09:06:16 -070070 out->reserve(in.size());
Alex Deymo246bf212016-03-22 19:27:33 -070071 std::unique_ptr<ExtentWriter> writer(
Ben Chanab5a0af2017-10-12 14:57:50 -070072 new W(std::make_unique<MemoryExtentWriter>(out)));
Alex Deymo246bf212016-03-22 19:27:33 -070073 // Init() parameters are ignored by the testing MemoryExtentWriter.
Kelvin Zhang4d22ca22021-02-09 14:06:25 -050074 bool ok = writer->Init({}, 1);
Alex Deymod3251ef2016-03-29 21:01:54 -070075 ok = writer->Write(in.data(), in.size()) && ok;
Alex Deymod3251ef2016-03-29 21:01:54 -070076 return ok;
Alex Deymo246bf212016-03-22 19:27:33 -070077}
78
79} // namespace
80
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080081template <typename T>
82class ZipTest : public ::testing::Test {
83 public:
Alex Deymo0bc26112015-10-19 20:54:57 -070084 bool ZipCompress(const brillo::Blob& in, brillo::Blob* out) const = 0;
Alex Deymo246bf212016-03-22 19:27:33 -070085 bool ZipDecompress(const brillo::Blob& in, brillo::Blob* out) const = 0;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080086};
87
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080088class BzipTest {};
89
90template <>
91class ZipTest<BzipTest> : public ::testing::Test {
92 public:
Alex Deymo0bc26112015-10-19 20:54:57 -070093 bool ZipCompress(const brillo::Blob& in, brillo::Blob* out) const {
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080094 return BzipCompress(in, out);
95 }
Alex Deymo246bf212016-03-22 19:27:33 -070096 bool ZipDecompress(const brillo::Blob& in, brillo::Blob* out) const {
97 return DecompressWithWriter<BzipExtentWriter>(in, out);
Andrew de los Reyesd2135f32010-03-11 16:00:28 -080098 }
99};
100
Alex Deymo246bf212016-03-22 19:27:33 -0700101class XzTest {};
102
103template <>
104class ZipTest<XzTest> : public ::testing::Test {
105 public:
106 bool ZipCompress(const brillo::Blob& in, brillo::Blob* out) const {
107 return XzCompress(in, out);
108 }
109 bool ZipDecompress(const brillo::Blob& in, brillo::Blob* out) const {
110 return DecompressWithWriter<XzExtentWriter>(in, out);
111 }
112};
113
Alex Deymo246bf212016-03-22 19:27:33 -0700114typedef ::testing::Types<BzipTest, XzTest> ZipTestTypes;
Alex Deymo246bf212016-03-22 19:27:33 -0700115
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800116TYPED_TEST_CASE(ZipTest, ZipTestTypes);
117
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800118TYPED_TEST(ZipTest, SimpleTest) {
Alex Deymo246bf212016-03-22 19:27:33 -0700119 string in_str(
120 "this should compress well xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
121 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
122 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
123 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
124 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
125 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
126 brillo::Blob in(in_str.begin(), in_str.end());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700127 brillo::Blob out;
Alex Deymo246bf212016-03-22 19:27:33 -0700128 EXPECT_TRUE(this->ZipCompress(in, &out));
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800129 EXPECT_LT(out.size(), in.size());
Alex Deymo80f70ff2016-02-10 16:08:11 -0800130 EXPECT_GT(out.size(), 0U);
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700131 brillo::Blob decompressed;
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800132 EXPECT_TRUE(this->ZipDecompress(out, &decompressed));
133 EXPECT_EQ(in.size(), decompressed.size());
Sen Jiang5288bd12018-05-03 11:39:04 -0700134 EXPECT_EQ(0, memcmp(in.data(), decompressed.data(), in.size()));
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800135}
136
137TYPED_TEST(ZipTest, PoorCompressionTest) {
Alex Deymo246bf212016-03-22 19:27:33 -0700138 brillo::Blob in(std::begin(kRandomString), std::end(kRandomString));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700139 brillo::Blob out;
Alex Deymo246bf212016-03-22 19:27:33 -0700140 EXPECT_TRUE(this->ZipCompress(in, &out));
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800141 EXPECT_GT(out.size(), in.size());
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700142 brillo::Blob decompressed;
Alex Deymo246bf212016-03-22 19:27:33 -0700143 EXPECT_TRUE(this->ZipDecompress(out, &decompressed));
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800144 EXPECT_EQ(in.size(), decompressed.size());
Alex Deymo246bf212016-03-22 19:27:33 -0700145 EXPECT_EQ(in, decompressed);
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800146}
147
148TYPED_TEST(ZipTest, MalformedZipTest) {
Alex Deymo246bf212016-03-22 19:27:33 -0700149 brillo::Blob in(std::begin(kRandomString), std::end(kRandomString));
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700150 brillo::Blob out;
Alex Deymo246bf212016-03-22 19:27:33 -0700151 EXPECT_FALSE(this->ZipDecompress(in, &out));
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800152}
153
154TYPED_TEST(ZipTest, EmptyInputsTest) {
Alex Deymo246bf212016-03-22 19:27:33 -0700155 brillo::Blob in;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700156 brillo::Blob out;
Alex Deymo246bf212016-03-22 19:27:33 -0700157 EXPECT_TRUE(this->ZipDecompress(in, &out));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800158 EXPECT_EQ(0U, out.size());
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800159
Alex Deymo246bf212016-03-22 19:27:33 -0700160 EXPECT_TRUE(this->ZipCompress(in, &out));
Alex Deymo80f70ff2016-02-10 16:08:11 -0800161 EXPECT_EQ(0U, out.size());
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800162}
163
Sen Jiang5288bd12018-05-03 11:39:04 -0700164TYPED_TEST(ZipTest, CompressELFTest) {
165 string path = test_utils::GetBuildArtifactsPath("delta_generator");
166 brillo::Blob in;
Kelvin Zhangc5803b72021-09-02 09:06:16 -0700167 ASSERT_TRUE(utils::ReadFile(path, &in));
Sen Jiang5288bd12018-05-03 11:39:04 -0700168 brillo::Blob out;
169 EXPECT_TRUE(this->ZipCompress(in, &out));
170 EXPECT_LT(out.size(), in.size());
171 EXPECT_GT(out.size(), 0U);
172 brillo::Blob decompressed;
173 EXPECT_TRUE(this->ZipDecompress(out, &decompressed));
174 EXPECT_EQ(in.size(), decompressed.size());
175 EXPECT_EQ(0, memcmp(in.data(), decompressed.data(), in.size()));
176}
177
Andrew de los Reyesd2135f32010-03-11 16:00:28 -0800178} // namespace chromeos_update_engine