Alex Deymo | 1415857 | 2015-06-13 03:37:08 -0700 | [diff] [blame^] | 1 | // Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/payload_generator/payload_file.h" |
| 6 | |
| 7 | #include <string> |
| 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <gtest/gtest.h> |
| 12 | |
| 13 | #include "update_engine/payload_constants.h" |
| 14 | #include "update_engine/payload_generator/extent_ranges.h" |
| 15 | #include "update_engine/test_utils.h" |
| 16 | |
| 17 | using std::string; |
| 18 | using std::vector; |
| 19 | |
| 20 | namespace chromeos_update_engine { |
| 21 | |
| 22 | class PayloadFileTest : public ::testing::Test { |
| 23 | protected: |
| 24 | PayloadFile payload_; |
| 25 | }; |
| 26 | |
| 27 | TEST_F(PayloadFileTest, ReorderBlobsTest) { |
| 28 | string orig_blobs; |
| 29 | EXPECT_TRUE(utils::MakeTempFile("ReorderBlobsTest.orig.XXXXXX", &orig_blobs, |
| 30 | nullptr)); |
| 31 | ScopedPathUnlinker orig_blobs_unlinker(orig_blobs); |
| 32 | |
| 33 | // The operations have three blob and one gap (the whitespace): |
| 34 | // Rootfs operation 1: [8, 3] bcd |
| 35 | // Rootfs operation 2: [7, 1] a |
| 36 | // Kernel operation 1: [0, 6] kernel |
| 37 | string orig_data = "kernel abcd"; |
| 38 | EXPECT_TRUE( |
| 39 | utils::WriteFile(orig_blobs.c_str(), orig_data.data(), orig_data.size())); |
| 40 | |
| 41 | string new_blobs; |
| 42 | EXPECT_TRUE( |
| 43 | utils::MakeTempFile("ReorderBlobsTest.new.XXXXXX", &new_blobs, nullptr)); |
| 44 | ScopedPathUnlinker new_blobs_unlinker(new_blobs); |
| 45 | |
| 46 | vector<AnnotatedOperation> aops; |
| 47 | AnnotatedOperation aop; |
| 48 | aop.op.set_data_offset(8); |
| 49 | aop.op.set_data_length(3); |
| 50 | aops.push_back(aop); |
| 51 | |
| 52 | aop.op.set_data_offset(7); |
| 53 | aop.op.set_data_length(1); |
| 54 | aops.push_back(aop); |
| 55 | payload_.AddPartitionOperations(PartitionName::kRootfs, aops); |
| 56 | |
| 57 | aop.op.set_data_offset(0); |
| 58 | aop.op.set_data_length(6); |
| 59 | aops = {aop}; |
| 60 | payload_.AddPartitionOperations(PartitionName::kKernel, aops); |
| 61 | |
| 62 | EXPECT_TRUE(payload_.ReorderDataBlobs(orig_blobs, new_blobs)); |
| 63 | |
| 64 | const vector<AnnotatedOperation>& rootfs_aops = |
| 65 | payload_.aops_map_[PartitionName::kRootfs]; |
| 66 | const vector<AnnotatedOperation>& kernel_aops = |
| 67 | payload_.aops_map_[PartitionName::kKernel]; |
| 68 | string new_data; |
| 69 | EXPECT_TRUE(utils::ReadFile(new_blobs, &new_data)); |
| 70 | // Kernel blobs should appear at the end. |
| 71 | EXPECT_EQ("bcdakernel", new_data); |
| 72 | |
| 73 | EXPECT_EQ(2, rootfs_aops.size()); |
| 74 | EXPECT_EQ(0, rootfs_aops[0].op.data_offset()); |
| 75 | EXPECT_EQ(3, rootfs_aops[0].op.data_length()); |
| 76 | EXPECT_EQ(3, rootfs_aops[1].op.data_offset()); |
| 77 | EXPECT_EQ(1, rootfs_aops[1].op.data_length()); |
| 78 | |
| 79 | EXPECT_EQ(1, kernel_aops.size()); |
| 80 | EXPECT_EQ(4, kernel_aops[0].op.data_offset()); |
| 81 | EXPECT_EQ(6, kernel_aops[0].op.data_length()); |
| 82 | } |
| 83 | |
| 84 | } // namespace chromeos_update_engine |