1 //
2 // Copyright (C) 2015 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/payload_file.h"
18
19 #include <string>
20 #include <utility>
21 #include <vector>
22
23 #include <gtest/gtest.h>
24
25 #include "update_engine/common/test_utils.h"
26 #include "update_engine/payload_generator/extent_ranges.h"
27
28 using std::string;
29 using std::vector;
30
31 namespace chromeos_update_engine {
32
33 class PayloadFileTest : public ::testing::Test {
34 protected:
35 PayloadFile payload_;
36 };
37
TEST_F(PayloadFileTest,ReorderBlobsTest)38 TEST_F(PayloadFileTest, ReorderBlobsTest) {
39 string orig_blobs;
40 EXPECT_TRUE(utils::MakeTempFile("ReorderBlobsTest.orig.XXXXXX", &orig_blobs,
41 nullptr));
42 ScopedPathUnlinker orig_blobs_unlinker(orig_blobs);
43
44 // The operations have three blob and one gap (the whitespace):
45 // Rootfs operation 1: [8, 3] bcd
46 // Rootfs operation 2: [7, 1] a
47 // Kernel operation 1: [0, 6] kernel
48 string orig_data = "kernel abcd";
49 EXPECT_TRUE(
50 utils::WriteFile(orig_blobs.c_str(), orig_data.data(), orig_data.size()));
51
52 string new_blobs;
53 EXPECT_TRUE(
54 utils::MakeTempFile("ReorderBlobsTest.new.XXXXXX", &new_blobs, nullptr));
55 ScopedPathUnlinker new_blobs_unlinker(new_blobs);
56
57 payload_.part_vec_.resize(2);
58
59 vector<AnnotatedOperation> aops;
60 AnnotatedOperation aop;
61 aop.op.set_data_offset(8);
62 aop.op.set_data_length(3);
63 aops.push_back(aop);
64
65 aop.op.set_data_offset(7);
66 aop.op.set_data_length(1);
67 aops.push_back(aop);
68 payload_.part_vec_[0].aops = aops;
69
70 aop.op.set_data_offset(0);
71 aop.op.set_data_length(6);
72 payload_.part_vec_[1].aops = {aop};
73
74 EXPECT_TRUE(payload_.ReorderDataBlobs(orig_blobs, new_blobs));
75
76 const vector<AnnotatedOperation>& part0_aops = payload_.part_vec_[0].aops;
77 const vector<AnnotatedOperation>& part1_aops = payload_.part_vec_[1].aops;
78 string new_data;
79 EXPECT_TRUE(utils::ReadFile(new_blobs, &new_data));
80 // Kernel blobs should appear at the end.
81 EXPECT_EQ("bcdakernel", new_data);
82
83 EXPECT_EQ(2U, part0_aops.size());
84 EXPECT_EQ(0U, part0_aops[0].op.data_offset());
85 EXPECT_EQ(3U, part0_aops[0].op.data_length());
86 EXPECT_EQ(3U, part0_aops[1].op.data_offset());
87 EXPECT_EQ(1U, part0_aops[1].op.data_length());
88
89 EXPECT_EQ(1U, part1_aops.size());
90 EXPECT_EQ(4U, part1_aops[0].op.data_offset());
91 EXPECT_EQ(6U, part1_aops[0].op.data_length());
92 }
93
94 } // namespace chromeos_update_engine
95