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   ScopedTempFile orig_blobs("ReorderBlobsTest.orig.XXXXXX");
40 
41   // The operations have three blob and one gap (the whitespace):
42   // Rootfs operation 1: [8, 3] bcd
43   // Rootfs operation 2: [7, 1] a
44   // Kernel operation 1: [0, 6] kernel
45   string orig_data = "kernel abcd";
46   EXPECT_TRUE(test_utils::WriteFileString(orig_blobs.path(), orig_data));
47 
48   ScopedTempFile new_blobs("ReorderBlobsTest.new.XXXXXX");
49 
50   payload_.part_vec_.resize(2);
51 
52   vector<AnnotatedOperation> aops;
53   AnnotatedOperation aop;
54   aop.op.set_data_offset(8);
55   aop.op.set_data_length(3);
56   aops.push_back(aop);
57 
58   aop.op.set_data_offset(7);
59   aop.op.set_data_length(1);
60   aops.push_back(aop);
61   payload_.part_vec_[0].aops = aops;
62 
63   aop.op.set_data_offset(0);
64   aop.op.set_data_length(6);
65   payload_.part_vec_[1].aops = {aop};
66 
67   EXPECT_TRUE(payload_.ReorderDataBlobs(orig_blobs.path(), new_blobs.path()));
68 
69   const vector<AnnotatedOperation>& part0_aops = payload_.part_vec_[0].aops;
70   const vector<AnnotatedOperation>& part1_aops = payload_.part_vec_[1].aops;
71   string new_data;
72   EXPECT_TRUE(utils::ReadFile(new_blobs.path(), &new_data));
73   // Kernel blobs should appear at the end.
74   EXPECT_EQ("bcdakernel", new_data);
75 
76   EXPECT_EQ(2U, part0_aops.size());
77   EXPECT_EQ(0U, part0_aops[0].op.data_offset());
78   EXPECT_EQ(3U, part0_aops[0].op.data_length());
79   EXPECT_EQ(3U, part0_aops[1].op.data_offset());
80   EXPECT_EQ(1U, part0_aops[1].op.data_length());
81 
82   EXPECT_EQ(1U, part1_aops.size());
83   EXPECT_EQ(4U, part1_aops[0].op.data_offset());
84   EXPECT_EQ(6U, part1_aops[0].op.data_length());
85 }
86 
87 }  // namespace chromeos_update_engine
88