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/update_client/component_patcher.h"
17 #include "components/update_client/component_patcher_operation.h"
18 #include "components/update_client/component_patcher_unittest.h"
19 #include "components/update_client/test_installer.h"
20 #include "courgette/courgette.h"
21 #include "courgette/third_party/bsdiff.h"
22 #include "testing/gtest/include/gtest/gtest.h"
29 virtual ~TestCallback() {}
30 void Set(update_client::ComponentUnpacker::Error error
, int extra_code
);
37 DISALLOW_COPY_AND_ASSIGN(TestCallback
);
40 TestCallback::TestCallback() : error_(-1), extra_code_(-1), called_(false) {
43 void TestCallback::Set(update_client::ComponentUnpacker::Error error
,
46 extra_code_
= extra_code
;
52 namespace update_client
{
56 base::FilePath
test_file(const char* file
) {
58 PathService::Get(base::DIR_SOURCE_ROOT
, &path
);
59 return path
.AppendASCII("components")
62 .AppendASCII("update_client")
68 ComponentPatcherOperationTest::ComponentPatcherOperationTest() {
69 EXPECT_TRUE(unpack_dir_
.CreateUniqueTempDir());
70 EXPECT_TRUE(input_dir_
.CreateUniqueTempDir());
71 EXPECT_TRUE(installed_dir_
.CreateUniqueTempDir());
72 installer_
= new ReadOnlyTestInstaller(installed_dir_
.path());
73 task_runner_
= base::MessageLoop::current()->task_runner();
76 ComponentPatcherOperationTest::~ComponentPatcherOperationTest() {
79 // Verify that a 'create' delta update operation works correctly.
80 TEST_F(ComponentPatcherOperationTest
, CheckCreateOperation
) {
81 EXPECT_TRUE(base::CopyFile(
82 test_file("binary_output.bin"),
83 input_dir_
.path().Append(FILE_PATH_LITERAL("binary_output.bin"))));
85 scoped_ptr
<base::DictionaryValue
> command_args(new base::DictionaryValue());
86 command_args
->SetString("output", "output.bin");
87 command_args
->SetString("sha256", binary_output_hash
);
88 command_args
->SetString("op", "create");
89 command_args
->SetString("patch", "binary_output.bin");
91 TestCallback callback
;
92 scoped_refptr
<DeltaUpdateOp
> op
= new DeltaUpdateOpCreate();
93 op
->Run(command_args
.get(), input_dir_
.path(), unpack_dir_
.path(), NULL
,
94 base::Bind(&TestCallback::Set
, base::Unretained(&callback
)),
96 base::RunLoop().RunUntilIdle();
98 EXPECT_EQ(true, callback
.called_
);
99 EXPECT_EQ(ComponentUnpacker::kNone
, callback
.error_
);
100 EXPECT_EQ(0, callback
.extra_code_
);
101 EXPECT_TRUE(base::ContentsEqual(
102 unpack_dir_
.path().Append(FILE_PATH_LITERAL("output.bin")),
103 test_file("binary_output.bin")));
106 // Verify that a 'copy' delta update operation works correctly.
107 TEST_F(ComponentPatcherOperationTest
, CheckCopyOperation
) {
108 EXPECT_TRUE(base::CopyFile(
109 test_file("binary_output.bin"),
110 installed_dir_
.path().Append(FILE_PATH_LITERAL("binary_output.bin"))));
112 scoped_ptr
<base::DictionaryValue
> command_args(new base::DictionaryValue());
113 command_args
->SetString("output", "output.bin");
114 command_args
->SetString("sha256", binary_output_hash
);
115 command_args
->SetString("op", "copy");
116 command_args
->SetString("input", "binary_output.bin");
118 TestCallback callback
;
119 scoped_refptr
<DeltaUpdateOp
> op
= new DeltaUpdateOpCopy();
120 op
->Run(command_args
.get(), input_dir_
.path(), unpack_dir_
.path(),
122 base::Bind(&TestCallback::Set
, base::Unretained(&callback
)),
124 base::RunLoop().RunUntilIdle();
126 EXPECT_EQ(true, callback
.called_
);
127 EXPECT_EQ(ComponentUnpacker::kNone
, callback
.error_
);
128 EXPECT_EQ(0, callback
.extra_code_
);
129 EXPECT_TRUE(base::ContentsEqual(
130 unpack_dir_
.path().Append(FILE_PATH_LITERAL("output.bin")),
131 test_file("binary_output.bin")));
134 // Verify that a 'courgette' delta update operation works correctly.
135 TEST_F(ComponentPatcherOperationTest
, CheckCourgetteOperation
) {
136 EXPECT_TRUE(base::CopyFile(
137 test_file("binary_input.bin"),
138 installed_dir_
.path().Append(FILE_PATH_LITERAL("binary_input.bin"))));
139 EXPECT_TRUE(base::CopyFile(test_file("binary_courgette_patch.bin"),
140 input_dir_
.path().Append(FILE_PATH_LITERAL(
141 "binary_courgette_patch.bin"))));
143 scoped_ptr
<base::DictionaryValue
> command_args(new base::DictionaryValue());
144 command_args
->SetString("output", "output.bin");
145 command_args
->SetString("sha256", binary_output_hash
);
146 command_args
->SetString("op", "courgette");
147 command_args
->SetString("input", "binary_input.bin");
148 command_args
->SetString("patch", "binary_courgette_patch.bin");
150 TestCallback callback
;
151 scoped_refptr
<DeltaUpdateOp
> op
=
152 CreateDeltaUpdateOp("courgette", NULL
/* out_of_process_patcher */);
153 op
->Run(command_args
.get(), input_dir_
.path(), unpack_dir_
.path(),
155 base::Bind(&TestCallback::Set
, base::Unretained(&callback
)),
157 base::RunLoop().RunUntilIdle();
159 EXPECT_EQ(true, callback
.called_
);
160 EXPECT_EQ(ComponentUnpacker::kNone
, callback
.error_
);
161 EXPECT_EQ(0, callback
.extra_code_
);
162 EXPECT_TRUE(base::ContentsEqual(
163 unpack_dir_
.path().Append(FILE_PATH_LITERAL("output.bin")),
164 test_file("binary_output.bin")));
167 // Verify that a 'bsdiff' delta update operation works correctly.
168 TEST_F(ComponentPatcherOperationTest
, CheckBsdiffOperation
) {
169 EXPECT_TRUE(base::CopyFile(
170 test_file("binary_input.bin"),
171 installed_dir_
.path().Append(FILE_PATH_LITERAL("binary_input.bin"))));
172 EXPECT_TRUE(base::CopyFile(
173 test_file("binary_bsdiff_patch.bin"),
174 input_dir_
.path().Append(FILE_PATH_LITERAL("binary_bsdiff_patch.bin"))));
176 scoped_ptr
<base::DictionaryValue
> command_args(new base::DictionaryValue());
177 command_args
->SetString("output", "output.bin");
178 command_args
->SetString("sha256", binary_output_hash
);
179 command_args
->SetString("op", "courgette");
180 command_args
->SetString("input", "binary_input.bin");
181 command_args
->SetString("patch", "binary_bsdiff_patch.bin");
183 TestCallback callback
;
184 scoped_refptr
<DeltaUpdateOp
> op
=
185 CreateDeltaUpdateOp("bsdiff", NULL
/* out_of_process_patcher */);
186 op
->Run(command_args
.get(), input_dir_
.path(), unpack_dir_
.path(),
188 base::Bind(&TestCallback::Set
, base::Unretained(&callback
)),
190 base::RunLoop().RunUntilIdle();
192 EXPECT_EQ(true, callback
.called_
);
193 EXPECT_EQ(ComponentUnpacker::kNone
, callback
.error_
);
194 EXPECT_EQ(0, callback
.extra_code_
);
195 EXPECT_TRUE(base::ContentsEqual(
196 unpack_dir_
.path().Append(FILE_PATH_LITERAL("output.bin")),
197 test_file("binary_output.bin")));
200 } // namespace update_client