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/base_paths.h"
7 #include "base/bind_helpers.h"
8 #include "base/compiler_specific.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/macros.h"
13 #include "base/path_service.h"
14 #include "base/run_loop.h"
15 #include "base/values.h"
16 #include "components/component_updater/component_patcher.h"
17 #include "components/component_updater/component_patcher_operation.h"
18 #include "components/component_updater/component_updater_service.h"
19 #include "components/component_updater/test/component_patcher_unittest.h"
20 #include "components/component_updater/test/test_installer.h"
21 #include "courgette/courgette.h"
22 #include "courgette/third_party/bsdiff.h"
23 #include "testing/gtest/include/gtest/gtest.h"
30 virtual ~TestCallback() {}
31 void Set(component_updater::ComponentUnpacker::Error error
, int extra_code
);
38 DISALLOW_COPY_AND_ASSIGN(TestCallback
);
41 TestCallback::TestCallback() : error_(-1), extra_code_(-1), called_(false) {
44 void TestCallback::Set(component_updater::ComponentUnpacker::Error error
,
47 extra_code_
= extra_code
;
53 namespace component_updater
{
57 base::FilePath
test_file(const char* file
) {
59 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
60 return path
.AppendASCII("components").AppendASCII("test").AppendASCII("data")
61 .AppendASCII("component_updater").AppendASCII(file
);
66 ComponentPatcherOperationTest::ComponentPatcherOperationTest() {
67 EXPECT_TRUE(unpack_dir_
.CreateUniqueTempDir());
68 EXPECT_TRUE(input_dir_
.CreateUniqueTempDir());
69 EXPECT_TRUE(installed_dir_
.CreateUniqueTempDir());
70 installer_
.reset(new ReadOnlyTestInstaller(installed_dir_
.path()));
71 task_runner_
= base::MessageLoop::current()->task_runner();
74 ComponentPatcherOperationTest::~ComponentPatcherOperationTest() {
77 // Verify that a 'create' delta update operation works correctly.
78 TEST_F(ComponentPatcherOperationTest
, CheckCreateOperation
) {
79 EXPECT_TRUE(base::CopyFile(
80 test_file("binary_output.bin"),
81 input_dir_
.path().Append(FILE_PATH_LITERAL("binary_output.bin"))));
83 scoped_ptr
<base::DictionaryValue
> command_args(new base::DictionaryValue());
84 command_args
->SetString("output", "output.bin");
85 command_args
->SetString("sha256", binary_output_hash
);
86 command_args
->SetString("op", "create");
87 command_args
->SetString("patch", "binary_output.bin");
89 TestCallback callback
;
90 scoped_refptr
<DeltaUpdateOp
> op
= new DeltaUpdateOpCreate();
91 op
->Run(command_args
.get(),
95 base::Bind(&TestCallback::Set
, base::Unretained(&callback
)),
97 base::RunLoop().RunUntilIdle();
99 EXPECT_EQ(true, callback
.called_
);
100 EXPECT_EQ(ComponentUnpacker::kNone
, callback
.error_
);
101 EXPECT_EQ(0, callback
.extra_code_
);
102 EXPECT_TRUE(base::ContentsEqual(
103 unpack_dir_
.path().Append(FILE_PATH_LITERAL("output.bin")),
104 test_file("binary_output.bin")));
107 // Verify that a 'copy' delta update operation works correctly.
108 TEST_F(ComponentPatcherOperationTest
, CheckCopyOperation
) {
109 EXPECT_TRUE(base::CopyFile(
110 test_file("binary_output.bin"),
111 installed_dir_
.path().Append(FILE_PATH_LITERAL("binary_output.bin"))));
113 scoped_ptr
<base::DictionaryValue
> command_args(new base::DictionaryValue());
114 command_args
->SetString("output", "output.bin");
115 command_args
->SetString("sha256", binary_output_hash
);
116 command_args
->SetString("op", "copy");
117 command_args
->SetString("input", "binary_output.bin");
119 TestCallback callback
;
120 scoped_refptr
<DeltaUpdateOp
> op
= new DeltaUpdateOpCopy();
121 op
->Run(command_args
.get(),
125 base::Bind(&TestCallback::Set
, base::Unretained(&callback
)),
127 base::RunLoop().RunUntilIdle();
129 EXPECT_EQ(true, callback
.called_
);
130 EXPECT_EQ(ComponentUnpacker::kNone
, callback
.error_
);
131 EXPECT_EQ(0, callback
.extra_code_
);
132 EXPECT_TRUE(base::ContentsEqual(
133 unpack_dir_
.path().Append(FILE_PATH_LITERAL("output.bin")),
134 test_file("binary_output.bin")));
137 // Verify that a 'courgette' delta update operation works correctly.
138 TEST_F(ComponentPatcherOperationTest
, CheckCourgetteOperation
) {
139 EXPECT_TRUE(base::CopyFile(
140 test_file("binary_input.bin"),
141 installed_dir_
.path().Append(FILE_PATH_LITERAL("binary_input.bin"))));
142 EXPECT_TRUE(base::CopyFile(test_file("binary_courgette_patch.bin"),
143 input_dir_
.path().Append(FILE_PATH_LITERAL(
144 "binary_courgette_patch.bin"))));
146 scoped_ptr
<base::DictionaryValue
> command_args(new base::DictionaryValue());
147 command_args
->SetString("output", "output.bin");
148 command_args
->SetString("sha256", binary_output_hash
);
149 command_args
->SetString("op", "courgette");
150 command_args
->SetString("input", "binary_input.bin");
151 command_args
->SetString("patch", "binary_courgette_patch.bin");
153 TestCallback callback
;
154 scoped_refptr
<DeltaUpdateOp
> op
=
155 CreateDeltaUpdateOp("courgette", NULL
/* out_of_process_patcher */);
156 op
->Run(command_args
.get(),
160 base::Bind(&TestCallback::Set
, base::Unretained(&callback
)),
162 base::RunLoop().RunUntilIdle();
164 EXPECT_EQ(true, callback
.called_
);
165 EXPECT_EQ(ComponentUnpacker::kNone
, callback
.error_
);
166 EXPECT_EQ(0, callback
.extra_code_
);
167 EXPECT_TRUE(base::ContentsEqual(
168 unpack_dir_
.path().Append(FILE_PATH_LITERAL("output.bin")),
169 test_file("binary_output.bin")));
172 // Verify that a 'bsdiff' delta update operation works correctly.
173 TEST_F(ComponentPatcherOperationTest
, CheckBsdiffOperation
) {
174 EXPECT_TRUE(base::CopyFile(
175 test_file("binary_input.bin"),
176 installed_dir_
.path().Append(FILE_PATH_LITERAL("binary_input.bin"))));
177 EXPECT_TRUE(base::CopyFile(
178 test_file("binary_bsdiff_patch.bin"),
179 input_dir_
.path().Append(FILE_PATH_LITERAL("binary_bsdiff_patch.bin"))));
181 scoped_ptr
<base::DictionaryValue
> command_args(new base::DictionaryValue());
182 command_args
->SetString("output", "output.bin");
183 command_args
->SetString("sha256", binary_output_hash
);
184 command_args
->SetString("op", "courgette");
185 command_args
->SetString("input", "binary_input.bin");
186 command_args
->SetString("patch", "binary_bsdiff_patch.bin");
188 TestCallback callback
;
189 scoped_refptr
<DeltaUpdateOp
> op
=
190 CreateDeltaUpdateOp("bsdiff", NULL
/* out_of_process_patcher */);
191 op
->Run(command_args
.get(),
195 base::Bind(&TestCallback::Set
, base::Unretained(&callback
)),
197 base::RunLoop().RunUntilIdle();
199 EXPECT_EQ(true, callback
.called_
);
200 EXPECT_EQ(ComponentUnpacker::kNone
, callback
.error_
);
201 EXPECT_EQ(0, callback
.extra_code_
);
202 EXPECT_TRUE(base::ContentsEqual(
203 unpack_dir_
.path().Append(FILE_PATH_LITERAL("output.bin")),
204 test_file("binary_output.bin")));
207 } // namespace component_updater