Home | History | Annotate | Download | only in payload_generator
      1 //
      2 // Copyright (C) 2010 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_generator/full_update_generator.h"
     18 
     19 #include <memory>
     20 #include <string>
     21 #include <vector>
     22 
     23 #include <gtest/gtest.h>
     24 
     25 #include "update_engine/common/test_utils.h"
     26 #include "update_engine/payload_consumer/payload_constants.h"
     27 #include "update_engine/payload_generator/extent_utils.h"
     28 
     29 using chromeos_update_engine::test_utils::FillWithData;
     30 using std::string;
     31 using std::vector;
     32 
     33 namespace chromeos_update_engine {
     34 
     35 class FullUpdateGeneratorTest : public ::testing::Test {
     36  protected:
     37   void SetUp() override {
     38     config_.is_delta = false;
     39     config_.version.minor = kFullPayloadMinorVersion;
     40     config_.hard_chunk_size = 128 * 1024;
     41     config_.block_size = 4096;
     42 
     43     EXPECT_TRUE(utils::MakeTempFile("FullUpdateTest_partition.XXXXXX",
     44                                     &new_part_conf.path,
     45                                     nullptr));
     46     EXPECT_TRUE(utils::MakeTempFile("FullUpdateTest_blobs.XXXXXX",
     47                                     &out_blobs_path_,
     48                                     &out_blobs_fd_));
     49 
     50     blob_file_.reset(new BlobFileWriter(out_blobs_fd_, &out_blobs_length_));
     51     part_path_unlinker_.reset(new ScopedPathUnlinker(new_part_conf.path));
     52     out_blobs_unlinker_.reset(new ScopedPathUnlinker(out_blobs_path_));
     53   }
     54 
     55   PayloadGenerationConfig config_;
     56   PartitionConfig new_part_conf{"part"};
     57 
     58   vector<AnnotatedOperation> aops;
     59 
     60   // Output file holding the payload blobs.
     61   string out_blobs_path_;
     62   int out_blobs_fd_{-1};
     63   off_t out_blobs_length_{0};
     64   ScopedFdCloser out_blobs_fd_closer_{&out_blobs_fd_};
     65 
     66   std::unique_ptr<BlobFileWriter> blob_file_;
     67   std::unique_ptr<ScopedPathUnlinker> part_path_unlinker_;
     68   std::unique_ptr<ScopedPathUnlinker> out_blobs_unlinker_;
     69 
     70   // FullUpdateGenerator under test.
     71   FullUpdateGenerator generator_;
     72 };
     73 
     74 TEST_F(FullUpdateGeneratorTest, RunTest) {
     75   brillo::Blob new_part(9 * 1024 * 1024);
     76   FillWithData(&new_part);
     77   new_part_conf.size = new_part.size();
     78 
     79   EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
     80 
     81   EXPECT_TRUE(generator_.GenerateOperations(config_,
     82                                             new_part_conf,  // this is ignored
     83                                             new_part_conf,
     84                                             blob_file_.get(),
     85                                             &aops));
     86   int64_t new_part_chunks = new_part_conf.size / config_.hard_chunk_size;
     87   EXPECT_EQ(new_part_chunks, static_cast<int64_t>(aops.size()));
     88   for (off_t i = 0; i < new_part_chunks; ++i) {
     89     EXPECT_EQ(1, aops[i].op.dst_extents_size());
     90     EXPECT_EQ(
     91         static_cast<uint64_t>(i * config_.hard_chunk_size / config_.block_size),
     92         aops[i].op.dst_extents(0).start_block())
     93         << "i = " << i;
     94     EXPECT_EQ(config_.hard_chunk_size / config_.block_size,
     95               aops[i].op.dst_extents(0).num_blocks());
     96     if (aops[i].op.type() != InstallOperation::REPLACE) {
     97       EXPECT_EQ(InstallOperation::REPLACE_BZ, aops[i].op.type());
     98     }
     99   }
    100 }
    101 
    102 // Test that if the chunk size is not a divisor of the image size, it handles
    103 // correctly the last chunk of the partition.
    104 TEST_F(FullUpdateGeneratorTest, ChunkSizeTooBig) {
    105   config_.hard_chunk_size = 1024 * 1024;
    106   config_.soft_chunk_size = config_.hard_chunk_size;
    107   brillo::Blob new_part(1536 * 1024);  // 1.5 MiB
    108   new_part_conf.size = new_part.size();
    109 
    110   EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
    111 
    112   EXPECT_TRUE(generator_.GenerateOperations(config_,
    113                                             new_part_conf,  // this is ignored
    114                                             new_part_conf,
    115                                             blob_file_.get(),
    116                                             &aops));
    117   // new_part has one chunk and a half.
    118   EXPECT_EQ(2U, aops.size());
    119   EXPECT_EQ(config_.hard_chunk_size / config_.block_size,
    120             utils::BlocksInExtents(aops[0].op.dst_extents()));
    121   EXPECT_EQ((new_part.size() - config_.hard_chunk_size) / config_.block_size,
    122             utils::BlocksInExtents(aops[1].op.dst_extents()));
    123 }
    124 
    125 // Test that if the image size is much smaller than the chunk size, it handles
    126 // correctly the only chunk of the partition.
    127 TEST_F(FullUpdateGeneratorTest, ImageSizeTooSmall) {
    128   brillo::Blob new_part(16 * 1024);
    129   new_part_conf.size = new_part.size();
    130 
    131   EXPECT_TRUE(test_utils::WriteFileVector(new_part_conf.path, new_part));
    132 
    133   EXPECT_TRUE(generator_.GenerateOperations(config_,
    134                                             new_part_conf,  // this is ignored
    135                                             new_part_conf,
    136                                             blob_file_.get(),
    137                                             &aops));
    138 
    139   // new_part has less than one chunk.
    140   EXPECT_EQ(1U, aops.size());
    141   EXPECT_EQ(new_part.size() / config_.block_size,
    142             utils::BlocksInExtents(aops[0].op.dst_extents()));
    143 }
    144 
    145 }  // namespace chromeos_update_engine
    146