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.
6 #include "base/compiler_specific.h"
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/path_service.h"
12 #include "base/run_loop.h"
13 #include "base/values.h"
14 #include "chrome/browser/component_updater/component_patcher.h"
15 #include "chrome/browser/component_updater/component_patcher_operation.h"
16 #include "chrome/browser/component_updater/component_updater_service.h"
17 #include "chrome/browser/component_updater/test/component_patcher_unittest.h"
18 #include "chrome/browser/component_updater/test/test_installer.h"
19 #include "chrome/common/chrome_paths.h"
20 #include "content/public/browser/browser_thread.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(chrome::DIR_TEST_DATA
, &path
);
60 return path
.AppendASCII("components").AppendASCII(file
);
65 ComponentPatcherOperationTest::ComponentPatcherOperationTest() {
66 EXPECT_TRUE(unpack_dir_
.CreateUniqueTempDir());
67 EXPECT_TRUE(input_dir_
.CreateUniqueTempDir());
68 EXPECT_TRUE(installed_dir_
.CreateUniqueTempDir());
69 installer_
.reset(new ReadOnlyTestInstaller(installed_dir_
.path()));
70 task_runner_
= content::BrowserThread::GetMessageLoopProxyForThread(
71 content::BrowserThread::FILE);
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(),
96 base::Bind(&TestCallback::Set
, base::Unretained(&callback
)),
98 base::RunLoop().RunUntilIdle();
100 EXPECT_EQ(true, callback
.called_
);
101 EXPECT_EQ(ComponentUnpacker::kNone
, callback
.error_
);
102 EXPECT_EQ(0, callback
.extra_code_
);
103 EXPECT_TRUE(base::ContentsEqual(
104 unpack_dir_
.path().Append(FILE_PATH_LITERAL("output.bin")),
105 test_file("binary_output.bin")));
108 // Verify that a 'copy' delta update operation works correctly.
109 TEST_F(ComponentPatcherOperationTest
, CheckCopyOperation
) {
110 EXPECT_TRUE(base::CopyFile(
111 test_file("binary_output.bin"),
112 installed_dir_
.path().Append(FILE_PATH_LITERAL("binary_output.bin"))));
114 scoped_ptr
<base::DictionaryValue
> command_args(new base::DictionaryValue());
115 command_args
->SetString("output", "output.bin");
116 command_args
->SetString("sha256", binary_output_hash
);
117 command_args
->SetString("op", "copy");
118 command_args
->SetString("input", "binary_output.bin");
120 TestCallback callback
;
121 scoped_refptr
<DeltaUpdateOp
> op
= new DeltaUpdateOpCopy();
122 op
->Run(command_args
.get(),
127 base::Bind(&TestCallback::Set
, base::Unretained(&callback
)),
129 base::RunLoop().RunUntilIdle();
131 EXPECT_EQ(true, callback
.called_
);
132 EXPECT_EQ(ComponentUnpacker::kNone
, callback
.error_
);
133 EXPECT_EQ(0, callback
.extra_code_
);
134 EXPECT_TRUE(base::ContentsEqual(
135 unpack_dir_
.path().Append(FILE_PATH_LITERAL("output.bin")),
136 test_file("binary_output.bin")));
139 // Verify that a 'courgette' delta update operation works correctly.
140 TEST_F(ComponentPatcherOperationTest
, CheckCourgetteOperation
) {
141 EXPECT_TRUE(base::CopyFile(
142 test_file("binary_input.bin"),
143 installed_dir_
.path().Append(FILE_PATH_LITERAL("binary_input.bin"))));
144 EXPECT_TRUE(base::CopyFile(test_file("binary_courgette_patch.bin"),
145 input_dir_
.path().Append(FILE_PATH_LITERAL(
146 "binary_courgette_patch.bin"))));
148 scoped_ptr
<base::DictionaryValue
> command_args(new base::DictionaryValue());
149 command_args
->SetString("output", "output.bin");
150 command_args
->SetString("sha256", binary_output_hash
);
151 command_args
->SetString("op", "courgette");
152 command_args
->SetString("input", "binary_input.bin");
153 command_args
->SetString("patch", "binary_courgette_patch.bin");
155 TestCallback callback
;
156 scoped_refptr
<DeltaUpdateOp
> op
= CreateDeltaUpdateOp("courgette");
157 op
->Run(command_args
.get(),
162 base::Bind(&TestCallback::Set
, base::Unretained(&callback
)),
164 base::RunLoop().RunUntilIdle();
166 EXPECT_EQ(true, callback
.called_
);
167 EXPECT_EQ(ComponentUnpacker::kNone
, callback
.error_
);
168 EXPECT_EQ(0, callback
.extra_code_
);
169 EXPECT_TRUE(base::ContentsEqual(
170 unpack_dir_
.path().Append(FILE_PATH_LITERAL("output.bin")),
171 test_file("binary_output.bin")));
174 // Verify that a 'bsdiff' delta update operation works correctly.
175 TEST_F(ComponentPatcherOperationTest
, CheckBsdiffOperation
) {
176 EXPECT_TRUE(base::CopyFile(
177 test_file("binary_input.bin"),
178 installed_dir_
.path().Append(FILE_PATH_LITERAL("binary_input.bin"))));
179 EXPECT_TRUE(base::CopyFile(
180 test_file("binary_bsdiff_patch.bin"),
181 input_dir_
.path().Append(FILE_PATH_LITERAL("binary_bsdiff_patch.bin"))));
183 scoped_ptr
<base::DictionaryValue
> command_args(new base::DictionaryValue());
184 command_args
->SetString("output", "output.bin");
185 command_args
->SetString("sha256", binary_output_hash
);
186 command_args
->SetString("op", "courgette");
187 command_args
->SetString("input", "binary_input.bin");
188 command_args
->SetString("patch", "binary_bsdiff_patch.bin");
190 TestCallback callback
;
191 scoped_refptr
<DeltaUpdateOp
> op
= CreateDeltaUpdateOp("bsdiff");
192 op
->Run(command_args
.get(),
197 base::Bind(&TestCallback::Set
, base::Unretained(&callback
)),
199 base::RunLoop().RunUntilIdle();
201 EXPECT_EQ(true, callback
.called_
);
202 EXPECT_EQ(ComponentUnpacker::kNone
, callback
.error_
);
203 EXPECT_EQ(0, callback
.extra_code_
);
204 EXPECT_TRUE(base::ContentsEqual(
205 unpack_dir_
.path().Append(FILE_PATH_LITERAL("output.bin")),
206 test_file("binary_output.bin")));
209 } // namespace component_updater