full_update_generator_unittest.cc revision f68bbbc952aa9a71898e4939b5f36187fa564a50
1// Copyright (c) 2010 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/full_update_generator.h"
6
7#include <string>
8#include <vector>
9
10#include <gtest/gtest.h>
11
12#include "update_engine/test_utils.h"
13
14using chromeos_update_engine::test_utils::FillWithData;
15using std::string;
16using std::vector;
17
18namespace chromeos_update_engine {
19
20namespace {
21  const size_t kBlockSize = 4096;
22}  // namespace
23
24
25class FullUpdateGeneratorTest : public ::testing::Test { };
26
27
28TEST(FullUpdateGeneratorTest, RunTest) {
29  chromeos::Blob new_root(20 * 1024 * 1024);
30  chromeos::Blob new_kern(16 * 1024 * 1024);
31  const off_t kChunkSize = 128 * 1024;
32  FillWithData(&new_root);
33  FillWithData(&new_kern);
34  // Assume hashes take 2 MiB beyond the rootfs.
35  off_t new_rootfs_size = new_root.size() - 2 * 1024 * 1024;
36
37  string new_root_path;
38  EXPECT_TRUE(utils::MakeTempFile("NewFullUpdateTest_R.XXXXXX",
39                                  &new_root_path,
40                                  nullptr));
41  ScopedPathUnlinker new_root_path_unlinker(new_root_path);
42  EXPECT_TRUE(test_utils::WriteFileVector(new_root_path, new_root));
43
44  string new_kern_path;
45  EXPECT_TRUE(utils::MakeTempFile("NewFullUpdateTest_K.XXXXXX",
46                                  &new_kern_path,
47                                  nullptr));
48  ScopedPathUnlinker new_kern_path_unlinker(new_kern_path);
49  EXPECT_TRUE(test_utils::WriteFileVector(new_kern_path, new_kern));
50
51  string out_blobs_path;
52  int out_blobs_fd;
53  EXPECT_TRUE(utils::MakeTempFile("NewFullUpdateTest_D.XXXXXX",
54                                  &out_blobs_path,
55                                  &out_blobs_fd));
56  ScopedPathUnlinker out_blobs_path_unlinker(out_blobs_path);
57  ScopedFdCloser out_blobs_fd_closer(&out_blobs_fd);
58
59  off_t out_blobs_length = 0;
60
61  Graph graph;
62  vector<DeltaArchiveManifest_InstallOperation> kernel_ops;
63  vector<Vertex::Index> final_order;
64
65  EXPECT_TRUE(FullUpdateGenerator::Run(&graph,
66                                       new_kern_path,
67                                       new_root_path,
68                                       new_rootfs_size,
69                                       out_blobs_fd,
70                                       &out_blobs_length,
71                                       kChunkSize,
72                                       kBlockSize,
73                                       &kernel_ops,
74                                       &final_order));
75  EXPECT_EQ(new_rootfs_size / kChunkSize, graph.size());
76  EXPECT_EQ(new_rootfs_size / kChunkSize, final_order.size());
77  EXPECT_EQ(new_kern.size() / kChunkSize, kernel_ops.size());
78  for (off_t i = 0; i < (new_rootfs_size / kChunkSize); ++i) {
79    EXPECT_EQ(i, final_order[i]);
80    EXPECT_EQ(1, graph[i].op.dst_extents_size());
81    EXPECT_EQ(i * kChunkSize / kBlockSize,
82              graph[i].op.dst_extents(0).start_block()) << "i = " << i;
83    EXPECT_EQ(kChunkSize / kBlockSize,
84              graph[i].op.dst_extents(0).num_blocks());
85    if (graph[i].op.type() !=
86        DeltaArchiveManifest_InstallOperation_Type_REPLACE) {
87      EXPECT_EQ(DeltaArchiveManifest_InstallOperation_Type_REPLACE_BZ,
88                graph[i].op.type());
89    }
90  }
91}
92
93}  // namespace chromeos_update_engine
94