• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //
16 
17 #include "update_engine/payload_consumer/bzip_extent_writer.h"
18 
19 #include <fcntl.h>
20 
21 #include <algorithm>
22 #include <memory>
23 #include <string>
24 #include <vector>
25 
26 #include <gtest/gtest.h>
27 
28 #include "update_engine/common/test_utils.h"
29 #include "update_engine/common/utils.h"
30 #include "update_engine/payload_generator/extent_ranges.h"
31 
32 using std::min;
33 using std::string;
34 using std::vector;
35 
36 namespace chromeos_update_engine {
37 
38 namespace {
39 const uint32_t kBlockSize = 4096;
40 }
41 
42 class BzipExtentWriterTest : public ::testing::Test {
43  protected:
SetUp()44   void SetUp() override {
45     fd_.reset(new EintrSafeFileDescriptor);
46     ASSERT_TRUE(fd_->Open(temp_file_.path().c_str(), O_RDWR, 0600));
47   }
TearDown()48   void TearDown() override { fd_->Close(); }
49 
50   FileDescriptorPtr fd_;
51   ScopedTempFile temp_file_{"BzipExtentWriterTest-file.XXXXXX"};
52 };
53 
TEST_F(BzipExtentWriterTest,SimpleTest)54 TEST_F(BzipExtentWriterTest, SimpleTest) {
55   vector<Extent> extents = {ExtentForRange(0, 1)};
56 
57   // 'echo test | bzip2 | hexdump' yields:
58   static const char test_uncompressed[] = "test\n";
59   static const uint8_t test[] = {
60       0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xcc, 0xc3,
61       0x71, 0xd4, 0x00, 0x00, 0x02, 0x41, 0x80, 0x00, 0x10, 0x02, 0x00, 0x0c,
62       0x00, 0x20, 0x00, 0x21, 0x9a, 0x68, 0x33, 0x4d, 0x19, 0x97, 0x8b, 0xb9,
63       0x22, 0x9c, 0x28, 0x48, 0x66, 0x61, 0xb8, 0xea, 0x00,
64   };
65 
66   BzipExtentWriter bzip_writer(std::make_unique<DirectExtentWriter>(fd_));
67   EXPECT_TRUE(bzip_writer.Init({extents.begin(), extents.end()}, kBlockSize));
68   EXPECT_TRUE(bzip_writer.Write(test, sizeof(test)));
69 
70   brillo::Blob buf;
71   EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &buf));
72   EXPECT_EQ(strlen(test_uncompressed), buf.size());
73   EXPECT_EQ(string(buf.begin(), buf.end()), string(test_uncompressed));
74 }
75 
TEST_F(BzipExtentWriterTest,ChunkedTest)76 TEST_F(BzipExtentWriterTest, ChunkedTest) {
77   // Generated with:
78   //   yes "ABC" | head -c 819200 | bzip2 -9 |
79   //     hexdump -v -e '"      " 11/1 "0x%02x, " "\n"'
80   static const uint8_t kCompressedData[] = {
81       0x42, 0x5a, 0x68, 0x39, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0xbe,
82       0x1c, 0xda, 0xee, 0x03, 0x1f, 0xff, 0xc4, 0x00, 0x00, 0x10, 0x38,
83       0x00, 0x20, 0x00, 0x50, 0x66, 0x9a, 0x05, 0x28, 0x38, 0x00, 0x11,
84       0x60, 0x00, 0x22, 0xd0, 0x00, 0x45, 0xc0, 0x00, 0x8b, 0xc5, 0xdc,
85       0x91, 0x4e, 0x14, 0x24, 0x2f, 0x87, 0x36, 0xbb, 0x80};
86   brillo::Blob compressed_data(std::begin(kCompressedData),
87                                std::end(kCompressedData));
88 
89   const brillo::Blob::size_type kDecompressedLength = 800 * 1024;  // 800 KiB
90   const size_t kChunkSize = 3;
91 
92   brillo::Blob decompressed_data(kDecompressedLength);
93   for (size_t i = 0; i < decompressed_data.size(); ++i)
94     decompressed_data[i] = static_cast<uint8_t>("ABC\n"[i % 4]);
95 
96   vector<Extent> extents = {ExtentForBytes(kBlockSize, 0, kDecompressedLength)};
97 
98   BzipExtentWriter bzip_writer(std::make_unique<DirectExtentWriter>(fd_));
99   EXPECT_TRUE(bzip_writer.Init({extents.begin(), extents.end()}, kBlockSize));
100 
101   brillo::Blob original_compressed_data = compressed_data;
102   for (brillo::Blob::size_type i = 0; i < compressed_data.size();
103        i += kChunkSize) {
104     size_t this_chunk_size = min(kChunkSize, compressed_data.size() - i);
105     EXPECT_TRUE(bzip_writer.Write(&compressed_data[i], this_chunk_size));
106   }
107 
108   // Check that the const input has not been clobbered.
109   test_utils::ExpectVectorsEq(original_compressed_data, compressed_data);
110 
111   brillo::Blob output;
112   EXPECT_TRUE(utils::ReadFile(temp_file_.path(), &output));
113   EXPECT_EQ(kDecompressedLength, output.size());
114   test_utils::ExpectVectorsEq(decompressed_data, output);
115 }
116 
117 }  // namespace chromeos_update_engine
118