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