Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2009 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 Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 16 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 17 | #include "update_engine/payload_consumer/bzip_extent_writer.h" |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 18 | |
Xiyuan Xia | 4eccae2 | 2016-02-26 14:30:02 -0800 | [diff] [blame] | 19 | #include <fcntl.h> |
| 20 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 21 | #include <algorithm> |
Ben Chan | ab5a0af | 2017-10-12 14:57:50 -0700 | [diff] [blame] | 22 | #include <memory> |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 23 | #include <string> |
| 24 | #include <vector> |
Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 25 | |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
Alex Deymo | 0532287 | 2015-09-30 09:50:24 -0700 | [diff] [blame] | 27 | |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 28 | #include "update_engine/common/test_utils.h" |
| 29 | #include "update_engine/common/utils.h" |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 30 | #include "update_engine/payload_generator/extent_ranges.h" |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 31 | |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 32 | using google::protobuf::RepeatedPtrField; |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 33 | using std::min; |
| 34 | using std::string; |
| 35 | using std::vector; |
| 36 | |
| 37 | namespace chromeos_update_engine { |
| 38 | |
| 39 | namespace { |
Andrew de los Reyes | 09e56d6 | 2010-04-23 13:45:53 -0700 | [diff] [blame] | 40 | const uint32_t kBlockSize = 4096; |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | class BzipExtentWriterTest : public ::testing::Test { |
| 44 | protected: |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 45 | void SetUp() override { |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 46 | fd_.reset(new EintrSafeFileDescriptor); |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 47 | ASSERT_TRUE(fd_->Open(temp_file_.path().c_str(), O_RDWR, 0600)); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 48 | } |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 49 | void TearDown() override { fd_->Close(); } |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 50 | |
| 51 | FileDescriptorPtr fd_; |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 52 | test_utils::ScopedTempFile temp_file_{"BzipExtentWriterTest-file.XXXXXX"}; |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | TEST_F(BzipExtentWriterTest, SimpleTest) { |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 56 | vector<Extent> extents = {ExtentForRange(0, 1)}; |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 57 | |
| 58 | // 'echo test | bzip2 | hexdump' yields: |
Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 59 | static const char test_uncompressed[] = "test\n"; |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 60 | static const uint8_t test[] = { |
Amin Hassani | 008c458 | 2019-01-13 16:22:47 -0800 | [diff] [blame^] | 61 | 0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xcc, 0xc3, |
| 62 | 0x71, 0xd4, 0x00, 0x00, 0x02, 0x41, 0x80, 0x00, 0x10, 0x02, 0x00, 0x0c, |
| 63 | 0x00, 0x20, 0x00, 0x21, 0x9a, 0x68, 0x33, 0x4d, 0x19, 0x97, 0x8b, 0xb9, |
| 64 | 0x22, 0x9c, 0x28, 0x48, 0x66, 0x61, 0xb8, 0xea, 0x00, |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 65 | }; |
Gilad Arnold | e1d1e98 | 2013-07-01 04:25:55 -0700 | [diff] [blame] | 66 | |
Ben Chan | ab5a0af | 2017-10-12 14:57:50 -0700 | [diff] [blame] | 67 | BzipExtentWriter bzip_writer(std::make_unique<DirectExtentWriter>()); |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 68 | EXPECT_TRUE( |
| 69 | bzip_writer.Init(fd_, {extents.begin(), extents.end()}, kBlockSize)); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 70 | EXPECT_TRUE(bzip_writer.Write(test, sizeof(test))); |
Gilad Arnold | e1d1e98 | 2013-07-01 04:25:55 -0700 | [diff] [blame] | 71 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 72 | brillo::Blob buf; |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 73 | EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &buf)); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 74 | EXPECT_EQ(strlen(test_uncompressed), buf.size()); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 75 | EXPECT_EQ(string(buf.begin(), buf.end()), string(test_uncompressed)); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | TEST_F(BzipExtentWriterTest, ChunkedTest) { |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 79 | // Generated with: |
Xiyuan Xia | 4eccae2 | 2016-02-26 14:30:02 -0800 | [diff] [blame] | 80 | // yes "ABC" | head -c 819200 | bzip2 -9 | |
| 81 | // hexdump -v -e '" " 11/1 "0x%02x, " "\n"' |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 82 | static const uint8_t kCompressedData[] = { |
| 83 | 0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xbe, |
| 84 | 0x1c, 0xda, 0xee, 0x03, 0x1f, 0xff, 0xc4, 0x00, 0x00, 0x10, 0x38, |
| 85 | 0x00, 0x20, 0x00, 0x50, 0x66, 0x9a, 0x05, 0x28, 0x38, 0x00, 0x11, |
| 86 | 0x60, 0x00, 0x22, 0xd0, 0x00, 0x45, 0xc0, 0x00, 0x8b, 0xc5, 0xdc, |
| 87 | 0x91, 0x4e, 0x14, 0x24, 0x2f, 0x87, 0x36, 0xbb, 0x80}; |
| 88 | brillo::Blob compressed_data(std::begin(kCompressedData), |
| 89 | std::end(kCompressedData)); |
| 90 | |
| 91 | const brillo::Blob::size_type kDecompressedLength = 800 * 1024; // 800 KiB |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 92 | const size_t kChunkSize = 3; |
Gilad Arnold | e1d1e98 | 2013-07-01 04:25:55 -0700 | [diff] [blame] | 93 | |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 94 | brillo::Blob decompressed_data(kDecompressedLength); |
| 95 | for (size_t i = 0; i < decompressed_data.size(); ++i) |
| 96 | decompressed_data[i] = static_cast<uint8_t>("ABC\n"[i % 4]); |
| 97 | |
Sen Jiang | 0a582fb | 2018-06-26 19:27:21 -0700 | [diff] [blame] | 98 | vector<Extent> extents = {ExtentForBytes(kBlockSize, 0, kDecompressedLength)}; |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 99 | |
Ben Chan | ab5a0af | 2017-10-12 14:57:50 -0700 | [diff] [blame] | 100 | BzipExtentWriter bzip_writer(std::make_unique<DirectExtentWriter>()); |
Amin Hassani | cd7edbe | 2017-09-18 17:05:02 -0700 | [diff] [blame] | 101 | EXPECT_TRUE( |
| 102 | bzip_writer.Init(fd_, {extents.begin(), extents.end()}, kBlockSize)); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 103 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 104 | brillo::Blob original_compressed_data = compressed_data; |
| 105 | for (brillo::Blob::size_type i = 0; i < compressed_data.size(); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 106 | i += kChunkSize) { |
| 107 | size_t this_chunk_size = min(kChunkSize, compressed_data.size() - i); |
| 108 | EXPECT_TRUE(bzip_writer.Write(&compressed_data[i], this_chunk_size)); |
| 109 | } |
Darin Petkov | e062239 | 2013-04-24 12:56:19 +0200 | [diff] [blame] | 110 | |
| 111 | // Check that the const input has not been clobbered. |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 112 | test_utils::ExpectVectorsEq(original_compressed_data, compressed_data); |
Gilad Arnold | e1d1e98 | 2013-07-01 04:25:55 -0700 | [diff] [blame] | 113 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 114 | brillo::Blob output; |
Alex Deymo | bffa060 | 2016-02-12 17:16:29 -0800 | [diff] [blame] | 115 | EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &output)); |
Nam T. Nguyen | f1d582e | 2014-12-08 15:07:17 -0800 | [diff] [blame] | 116 | EXPECT_EQ(kDecompressedLength, output.size()); |
Alex Deymo | 10875d9 | 2014-11-10 21:52:57 -0800 | [diff] [blame] | 117 | test_utils::ExpectVectorsEq(decompressed_data, output); |
Andrew de los Reyes | 8006106 | 2010-02-04 14:25:00 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | } // namespace chromeos_update_engine |