1 // Copyright 2013 The Chromium 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.
5 #include "base/compiler_specific.h"
6 #include "base/file_util.h"
7 #include "base/files/file_path.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/path_service.h"
10 #include "base/values.h"
11 #include "chrome/browser/component_updater/component_patcher.h"
12 #include "chrome/browser/component_updater/component_patcher_operation.h"
13 #include "chrome/browser/component_updater/component_updater_service.h"
14 #include "chrome/browser/component_updater/test/component_patcher_mock.h"
15 #include "chrome/browser/component_updater/test/component_patcher_unittest.h"
16 #include "chrome/browser/component_updater/test/test_installer.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "courgette/courgette.h"
19 #include "courgette/third_party/bsdiff.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 namespace component_updater {
26 base::FilePath test_file(const char* file) {
28 PathService::Get(chrome::DIR_TEST_DATA, &path);
29 return path.AppendASCII("components").AppendASCII(file);
34 ComponentPatcherOperationTest::ComponentPatcherOperationTest() {
35 EXPECT_TRUE(unpack_dir_.CreateUniqueTempDir());
36 EXPECT_TRUE(input_dir_.CreateUniqueTempDir());
37 EXPECT_TRUE(installed_dir_.CreateUniqueTempDir());
38 patcher_.reset(new MockComponentPatcher());
39 installer_.reset(new ReadOnlyTestInstaller(installed_dir_.path()));
42 ComponentPatcherOperationTest::~ComponentPatcherOperationTest() {
45 ComponentUnpacker::Error MockComponentPatcher::Patch(
47 const base::FilePath& input_file,
48 const base::FilePath& patch_file,
49 const base::FilePath& output_file,
53 if (patch_type == kPatchTypeCourgette) {
54 exit_code = courgette::ApplyEnsemblePatch(input_file.value().c_str(),
55 patch_file.value().c_str(),
56 output_file.value().c_str());
57 if (exit_code == courgette::C_OK)
58 return ComponentUnpacker::kNone;
59 *error = exit_code + kCourgetteErrorOffset;
60 } else if (patch_type == kPatchTypeBsdiff) {
61 exit_code = courgette::ApplyBinaryPatch(input_file,
64 if (exit_code == courgette::OK)
65 return ComponentUnpacker::kNone;
66 *error = exit_code + kBsdiffErrorOffset;
68 return ComponentUnpacker::kDeltaOperationFailure;
71 // Verify that a 'create' delta update operation works correctly.
72 TEST_F(ComponentPatcherOperationTest, CheckCreateOperation) {
73 EXPECT_TRUE(base::CopyFile(
74 test_file("binary_output.bin"),
75 input_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin"))));
77 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue());
78 command_args->SetString("output", "output.bin");
79 command_args->SetString("sha256", binary_output_hash);
80 command_args->SetString("op", "create");
81 command_args->SetString("patch", "binary_output.bin");
84 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCreate());
85 ComponentUnpacker::Error result = op->Run(command_args.get(),
92 EXPECT_EQ(ComponentUnpacker::kNone, result);
94 EXPECT_TRUE(base::ContentsEqual(
95 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")),
96 test_file("binary_output.bin")));
99 // Verify that a 'copy' delta update operation works correctly.
100 TEST_F(ComponentPatcherOperationTest, CheckCopyOperation) {
101 EXPECT_TRUE(base::CopyFile(
102 test_file("binary_output.bin"),
103 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin"))));
105 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue());
106 command_args->SetString("output", "output.bin");
107 command_args->SetString("sha256", binary_output_hash);
108 command_args->SetString("op", "copy");
109 command_args->SetString("input", "binary_output.bin");
112 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCopy());
113 ComponentUnpacker::Error result = op->Run(command_args.get(),
119 EXPECT_EQ(ComponentUnpacker::kNone, result);
121 EXPECT_TRUE(base::ContentsEqual(
122 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")),
123 test_file("binary_output.bin")));
126 // Verify that a 'courgette' delta update operation works correctly.
127 TEST_F(ComponentPatcherOperationTest, CheckCourgetteOperation) {
128 EXPECT_TRUE(base::CopyFile(
129 test_file("binary_input.bin"),
130 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin"))));
131 EXPECT_TRUE(base::CopyFile(
132 test_file("binary_courgette_patch.bin"),
133 input_dir_.path().Append(
134 FILE_PATH_LITERAL("binary_courgette_patch.bin"))));
136 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue());
137 command_args->SetString("output", "output.bin");
138 command_args->SetString("sha256", binary_output_hash);
139 command_args->SetString("op", "courgette");
140 command_args->SetString("input", "binary_input.bin");
141 command_args->SetString("patch", "binary_courgette_patch.bin");
144 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpPatchCourgette());
145 ComponentUnpacker::Error result = op->Run(command_args.get(),
151 EXPECT_EQ(ComponentUnpacker::kNone, result);
153 EXPECT_TRUE(base::ContentsEqual(
154 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")),
155 test_file("binary_output.bin")));
158 // Verify that a 'bsdiff' delta update operation works correctly.
159 TEST_F(ComponentPatcherOperationTest, CheckBsdiffOperation) {
160 EXPECT_TRUE(base::CopyFile(
161 test_file("binary_input.bin"),
162 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin"))));
163 EXPECT_TRUE(base::CopyFile(
164 test_file("binary_bsdiff_patch.bin"),
165 input_dir_.path().Append(FILE_PATH_LITERAL("binary_bsdiff_patch.bin"))));
167 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue());
168 command_args->SetString("output", "output.bin");
169 command_args->SetString("sha256", binary_output_hash);
170 command_args->SetString("op", "courgette");
171 command_args->SetString("input", "binary_input.bin");
172 command_args->SetString("patch", "binary_bsdiff_patch.bin");
175 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpPatchBsdiff());
176 ComponentUnpacker::Error result = op->Run(command_args.get(),
182 EXPECT_EQ(ComponentUnpacker::kNone, result);
184 EXPECT_TRUE(base::ContentsEqual(
185 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")),
186 test_file("binary_output.bin")));
189 } // namespace component_updater