blob: e8e7e143d593cd97512e74baa28c8539caec006c [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
Alex Deymo14158572015-06-13 03:37:08 -070016
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
Alex Deymo39910dc2015-11-09 17:04:30 -080025#include "update_engine/common/test_utils.h"
Alex Deymo14158572015-06-13 03:37:08 -070026#include "update_engine/payload_generator/extent_ranges.h"
Alex Deymo14158572015-06-13 03:37:08 -070027
28using std::string;
29using std::vector;
30
31namespace chromeos_update_engine {
32
33class PayloadFileTest : public ::testing::Test {
34 protected:
35 PayloadFile payload_;
36};
37
38TEST_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
Sen Jiangb9ef4912015-09-21 15:06:13 -070057 payload_.part_vec_.resize(2);
58
Alex Deymo14158572015-06-13 03:37:08 -070059 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);
Sen Jiangb9ef4912015-09-21 15:06:13 -070068 payload_.part_vec_[0].aops = aops;
Alex Deymo14158572015-06-13 03:37:08 -070069
70 aop.op.set_data_offset(0);
71 aop.op.set_data_length(6);
Sen Jiangb9ef4912015-09-21 15:06:13 -070072 payload_.part_vec_[1].aops = {aop};
Alex Deymo14158572015-06-13 03:37:08 -070073
74 EXPECT_TRUE(payload_.ReorderDataBlobs(orig_blobs, new_blobs));
75
Sen Jiangb9ef4912015-09-21 15:06:13 -070076 const vector<AnnotatedOperation>& part0_aops = payload_.part_vec_[0].aops;
77 const vector<AnnotatedOperation>& part1_aops = payload_.part_vec_[1].aops;
Alex Deymo14158572015-06-13 03:37:08 -070078 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
Alex Deymo80f70ff2016-02-10 16:08:11 -080083 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());
Alex Deymo14158572015-06-13 03:37:08 -070088
Alex Deymo80f70ff2016-02-10 16:08:11 -080089 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());
Alex Deymo14158572015-06-13 03:37:08 -070092}
93
94} // namespace chromeos_update_engine