OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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. | |
4 | |
5 #include "base/compiler_specific.h" | |
6 #include "base/file_util.h" | |
7 #include "base/files/file_path.h" | |
8 #include "base/path_service.h" | |
9 #include "base/values.h" | |
10 #include "chrome/browser/component_updater/component_patcher.h" | |
11 #include "chrome/browser/component_updater/component_patcher_internal.h" | |
12 #include "chrome/browser/component_updater/component_updater_service.h" | |
13 #include "chrome/browser/component_updater/test/component_patcher_mock.h" | |
14 #include "chrome/common/chrome_paths.h" | |
15 #include "courgette/courgette.h" | |
16 #include "courgette/third_party/bsdiff.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 const char binary_output_hash[] = | |
20 "599aba6d15a7da390621ef1bacb66601ed6aed04dadc1f9b445dcfe31296142a"; | |
21 | |
22 const int kCourgetteErrorOffset = 300; | |
23 const int kBsdiffErrorOffset = 600; | |
robertshield
2013/06/17 17:47:00
These should be taken from util_constants.h when t
waffles
2013/06/17 23:51:07
As per off-line discussion, we're leaving it and a
| |
24 | |
25 base::FilePath test_file(const char* file); | |
26 | |
27 class DummyInstaller : public ComponentInstaller { | |
erikwright (departed)
2013/06/17 17:24:28
Please consider de-duping this class into a dummy_
waffles
2013/06/17 22:17:49
Done, although it didn't quite have the same behav
| |
28 public: | |
29 explicit DummyInstaller(const base::FilePath& installed_dir); | |
30 | |
31 virtual void OnUpdateError(int error) OVERRIDE; | |
32 | |
33 virtual bool Install(const base::DictionaryValue& manifest, | |
34 const base::FilePath& unpack_path) OVERRIDE; | |
35 | |
36 virtual bool GetInstalledFile(const std::string& file, | |
37 base::FilePath* installed_file) OVERRIDE; | |
38 | |
39 private: | |
40 base::FilePath installed_dir_; | |
41 | |
42 DISALLOW_COPY_AND_ASSIGN(DummyInstaller); | |
43 }; | |
44 | |
45 class ComponentPatcherOperationTest : public testing::Test { | |
46 public: | |
47 ComponentPatcherOperationTest(); | |
48 | |
49 virtual ~ComponentPatcherOperationTest(); | |
50 | |
51 protected: | |
52 base::FilePath input_dir_; | |
53 base::FilePath installed_dir_; | |
54 base::FilePath unpack_dir_; | |
55 scoped_ptr<MockComponentPatcher> patcher_; | |
56 scoped_ptr<DummyInstaller> installer_; | |
57 }; | |
58 | |
59 base::FilePath test_file(const char* file) { | |
60 base::FilePath path; | |
61 PathService::Get(chrome::DIR_TEST_DATA, &path); | |
62 return path.AppendASCII("components").AppendASCII(file); | |
63 } | |
64 | |
65 ComponentPatcherOperationTest::ComponentPatcherOperationTest() { | |
66 file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("TEST_"), &unpack_dir_); | |
67 file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("TEST_"), &input_dir_); | |
68 file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("TEST_"), | |
69 &installed_dir_); | |
70 patcher_.reset(new MockComponentPatcher()); | |
71 installer_.reset(new DummyInstaller(installed_dir_)); | |
72 } | |
73 | |
74 ComponentPatcherOperationTest::~ComponentPatcherOperationTest() { | |
75 file_util::Delete(unpack_dir_, true); | |
76 file_util::Delete(input_dir_, true); | |
77 file_util::Delete(installed_dir_, true); | |
78 } | |
79 | |
80 DummyInstaller::DummyInstaller(const base::FilePath& installed_dir) | |
81 : installed_dir_(installed_dir) { | |
82 } | |
83 | |
84 void DummyInstaller::OnUpdateError(int error) {} | |
85 | |
86 bool DummyInstaller::Install(const base::DictionaryValue& manifest, | |
87 const base::FilePath& unpack_path) { | |
88 return false; | |
89 } | |
90 | |
91 bool DummyInstaller::GetInstalledFile(const std::string& file, | |
92 base::FilePath* installed_file) { | |
93 *installed_file = installed_dir_.AppendASCII(file); | |
94 return true; | |
95 } | |
96 | |
97 ComponentUnpacker::Error MockComponentPatcher::Patch( | |
98 PatchType patch_type, | |
99 const base::FilePath& input_file, | |
100 const base::FilePath& patch_file, | |
101 const base::FilePath& output_file, | |
102 int* error) { | |
103 *error = 0; | |
104 int exit_code; | |
105 if (patch_type == kPatchTypeCourgette) { | |
106 exit_code = courgette::ApplyEnsemblePatch(input_file.value().c_str(), | |
107 patch_file.value().c_str(), | |
108 output_file.value().c_str()); | |
109 if (exit_code == courgette::C_OK) | |
110 return ComponentUnpacker::kNone; | |
111 *error = exit_code + kCourgetteErrorOffset; | |
112 } else if (patch_type == kPatchTypeBsdiff) { | |
113 exit_code = courgette::ApplyBinaryPatch(input_file, | |
114 patch_file, | |
115 output_file); | |
116 if (exit_code == courgette::OK) | |
117 return ComponentUnpacker::kNone; | |
118 *error = exit_code + kBsdiffErrorOffset; | |
119 } | |
120 return ComponentUnpacker::kDeltaOperationFailure; | |
121 } | |
122 | |
123 // Verify that a 'create' delta update operation works correctly. | |
124 TEST_F(ComponentPatcherOperationTest, CheckCreateOperation) { | |
125 EXPECT_TRUE(file_util::CopyFile( | |
126 test_file("binary_output.bin"), | |
127 input_dir_.Append(FILE_PATH_LITERAL("binary_output.bin")))); | |
128 | |
129 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); | |
130 command_args->SetString("output", "output.bin"); | |
131 command_args->SetString("sha256", binary_output_hash); | |
132 command_args->SetString("op", "create"); | |
133 command_args->SetString("patch", "binary_output.bin"); | |
134 | |
135 int error = 0; | |
136 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCreate()); | |
137 ComponentUnpacker::Error result = op->Run(command_args.get(), | |
138 input_dir_, | |
139 unpack_dir_, | |
140 patcher_.get(), | |
141 NULL, | |
142 &error); | |
143 | |
144 EXPECT_EQ(ComponentUnpacker::kNone, result); | |
145 EXPECT_EQ(0, error); | |
146 EXPECT_TRUE(file_util::ContentsEqual( | |
147 unpack_dir_.Append(FILE_PATH_LITERAL("output.bin")), | |
148 test_file("binary_output.bin"))); | |
149 } | |
150 | |
151 // Verify that a 'copy' delta update operation works correctly. | |
152 TEST_F(ComponentPatcherOperationTest, CheckCopyOperation) { | |
153 EXPECT_TRUE(file_util::CopyFile( | |
154 test_file("binary_output.bin"), | |
155 installed_dir_.Append(FILE_PATH_LITERAL("binary_output.bin")))); | |
156 | |
157 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); | |
158 command_args->SetString("output", "output.bin"); | |
159 command_args->SetString("sha256", binary_output_hash); | |
160 command_args->SetString("op", "copy"); | |
161 command_args->SetString("input", "binary_output.bin"); | |
162 | |
163 int error = 0; | |
164 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCopy()); | |
165 ComponentUnpacker::Error result = op->Run(command_args.get(), | |
166 input_dir_, | |
167 unpack_dir_, | |
168 patcher_.get(), | |
169 installer_.get(), | |
170 &error); | |
171 EXPECT_EQ(ComponentUnpacker::kNone, result); | |
172 EXPECT_EQ(0, error); | |
173 EXPECT_TRUE(file_util::ContentsEqual( | |
174 unpack_dir_.Append(FILE_PATH_LITERAL("output.bin")), | |
175 test_file("binary_output.bin"))); | |
176 } | |
177 | |
178 // Verify that a 'courgette' delta update operation works correctly. | |
179 TEST_F(ComponentPatcherOperationTest, CheckCourgetteOperation) { | |
180 EXPECT_TRUE(file_util::CopyFile( | |
181 test_file("binary_input.bin"), | |
182 installed_dir_.Append(FILE_PATH_LITERAL("binary_input.bin")))); | |
183 EXPECT_TRUE(file_util::CopyFile( | |
184 test_file("binary_courgette_patch.bin"), | |
185 input_dir_.Append(FILE_PATH_LITERAL("binary_courgette_patch.bin")))); | |
186 | |
187 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); | |
188 command_args->SetString("output", "output.bin"); | |
189 command_args->SetString("sha256", binary_output_hash); | |
190 command_args->SetString("op", "courgette"); | |
191 command_args->SetString("input", "binary_input.bin"); | |
192 command_args->SetString("patch", "binary_courgette_patch.bin"); | |
193 | |
194 int error = 0; | |
195 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpPatchCourgette()); | |
196 ComponentUnpacker::Error result = op->Run(command_args.get(), | |
197 input_dir_, | |
198 unpack_dir_, | |
199 patcher_.get(), | |
200 installer_.get(), | |
201 &error); | |
202 EXPECT_EQ(ComponentUnpacker::kNone, result); | |
203 EXPECT_EQ(0, error); | |
204 EXPECT_TRUE(file_util::ContentsEqual( | |
205 unpack_dir_.Append(FILE_PATH_LITERAL("output.bin")), | |
206 test_file("binary_output.bin"))); | |
207 } | |
208 | |
209 // Verify that a 'bsdiff' delta update operation works correctly. | |
210 TEST_F(ComponentPatcherOperationTest, CheckBsdiffOperation) { | |
211 EXPECT_TRUE(file_util::CopyFile( | |
212 test_file("binary_input.bin"), | |
213 installed_dir_.Append(FILE_PATH_LITERAL("binary_input.bin")))); | |
214 EXPECT_TRUE(file_util::CopyFile( | |
215 test_file("binary_bsdiff_patch.bin"), | |
216 input_dir_.Append(FILE_PATH_LITERAL("binary_bsdiff_patch.bin")))); | |
217 | |
218 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); | |
219 command_args->SetString("output", "output.bin"); | |
220 command_args->SetString("sha256", binary_output_hash); | |
221 command_args->SetString("op", "courgette"); | |
222 command_args->SetString("input", "binary_input.bin"); | |
223 command_args->SetString("patch", "binary_bsdiff_patch.bin"); | |
224 | |
225 int error = 0; | |
226 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpPatchBsdiff()); | |
227 ComponentUnpacker::Error result = op->Run(command_args.get(), | |
228 input_dir_, | |
229 unpack_dir_, | |
230 patcher_.get(), | |
231 installer_.get(), | |
232 &error); | |
233 EXPECT_EQ(ComponentUnpacker::kNone, result); | |
234 EXPECT_EQ(0, error); | |
235 EXPECT_TRUE(file_util::ContentsEqual( | |
236 unpack_dir_.Append(FILE_PATH_LITERAL("output.bin")), | |
237 test_file("binary_output.bin"))); | |
238 } | |
239 | |
OLD | NEW |