summaryrefslogtreecommitdiff
path: root/payload_generator/payload_file_unittest.cc
blob: 1d9661f04289ac73d18962294ad4ae17c5bb20cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "update_engine/payload_generator/payload_file.h"

#include <string>
#include <utility>
#include <vector>

#include <gtest/gtest.h>

#include "update_engine/payload_constants.h"
#include "update_engine/payload_generator/extent_ranges.h"
#include "update_engine/test_utils.h"

using std::string;
using std::vector;

namespace chromeos_update_engine {

class PayloadFileTest : public ::testing::Test {
 protected:
  PayloadFile payload_;
};

TEST_F(PayloadFileTest, ReorderBlobsTest) {
  string orig_blobs;
  EXPECT_TRUE(utils::MakeTempFile("ReorderBlobsTest.orig.XXXXXX", &orig_blobs,
                                  nullptr));
  ScopedPathUnlinker orig_blobs_unlinker(orig_blobs);

  // The operations have three blob and one gap (the whitespace):
  // Rootfs operation 1: [8, 3] bcd
  // Rootfs operation 2: [7, 1] a
  // Kernel operation 1: [0, 6] kernel
  string orig_data = "kernel abcd";
  EXPECT_TRUE(
      utils::WriteFile(orig_blobs.c_str(), orig_data.data(), orig_data.size()));

  string new_blobs;
  EXPECT_TRUE(
      utils::MakeTempFile("ReorderBlobsTest.new.XXXXXX", &new_blobs, nullptr));
  ScopedPathUnlinker new_blobs_unlinker(new_blobs);

  vector<AnnotatedOperation> aops;
  AnnotatedOperation aop;
  aop.op.set_data_offset(8);
  aop.op.set_data_length(3);
  aops.push_back(aop);

  aop.op.set_data_offset(7);
  aop.op.set_data_length(1);
  aops.push_back(aop);
  payload_.AddPartitionOperations(PartitionName::kRootfs, aops);

  aop.op.set_data_offset(0);
  aop.op.set_data_length(6);
  aops = {aop};
  payload_.AddPartitionOperations(PartitionName::kKernel, aops);

  EXPECT_TRUE(payload_.ReorderDataBlobs(orig_blobs, new_blobs));

  const vector<AnnotatedOperation>& rootfs_aops =
      payload_.aops_map_[PartitionName::kRootfs];
  const vector<AnnotatedOperation>& kernel_aops =
      payload_.aops_map_[PartitionName::kKernel];
  string new_data;
  EXPECT_TRUE(utils::ReadFile(new_blobs, &new_data));
  // Kernel blobs should appear at the end.
  EXPECT_EQ("bcdakernel", new_data);

  EXPECT_EQ(2, rootfs_aops.size());
  EXPECT_EQ(0, rootfs_aops[0].op.data_offset());
  EXPECT_EQ(3, rootfs_aops[0].op.data_length());
  EXPECT_EQ(3, rootfs_aops[1].op.data_offset());
  EXPECT_EQ(1, rootfs_aops[1].op.data_length());

  EXPECT_EQ(1, kernel_aops.size());
  EXPECT_EQ(4, kernel_aops[0].op.data_offset());
  EXPECT_EQ(6, kernel_aops[0].op.data_length());
}

}  // namespace chromeos_update_engine