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/files/scoped_temp_dir.h" |
| 9 #include "base/path_service.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/component_updater/component_patcher.h" |
| 12 #include "chrome/browser/component_updater/component_patcher_internal.h" |
| 13 #include "chrome/browser/component_updater/component_updater_service.h" |
| 14 #include "chrome/browser/component_updater/test/component_patcher_mock.h" |
| 15 #include "chrome/browser/component_updater/test/test_installer.h" |
| 16 #include "chrome/common/chrome_paths.h" |
| 17 #include "courgette/courgette.h" |
| 18 #include "courgette/third_party/bsdiff.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 const char binary_output_hash[] = |
| 22 "599aba6d15a7da390621ef1bacb66601ed6aed04dadc1f9b445dcfe31296142a"; |
| 23 |
| 24 const int kCourgetteErrorOffset = 300; |
| 25 const int kBsdiffErrorOffset = 600; |
| 26 |
| 27 base::FilePath test_file(const char* file); |
| 28 |
| 29 class ComponentPatcherOperationTest : public testing::Test { |
| 30 public: |
| 31 ComponentPatcherOperationTest(); |
| 32 |
| 33 protected: |
| 34 base::ScopedTempDir input_dir_; |
| 35 base::ScopedTempDir installed_dir_; |
| 36 base::ScopedTempDir unpack_dir_; |
| 37 scoped_ptr<MockComponentPatcher> patcher_; |
| 38 scoped_ptr<ReadOnlyTestInstaller> installer_; |
| 39 }; |
| 40 |
| 41 base::FilePath test_file(const char* file) { |
| 42 base::FilePath path; |
| 43 PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 44 return path.AppendASCII("components").AppendASCII(file); |
| 45 } |
| 46 |
| 47 ComponentPatcherOperationTest::ComponentPatcherOperationTest() { |
| 48 EXPECT_TRUE(unpack_dir_.CreateUniqueTempDir()); |
| 49 EXPECT_TRUE(input_dir_.CreateUniqueTempDir()); |
| 50 EXPECT_TRUE(installed_dir_.CreateUniqueTempDir()); |
| 51 patcher_.reset(new MockComponentPatcher()); |
| 52 installer_.reset(new ReadOnlyTestInstaller(installed_dir_.path())); |
| 53 } |
| 54 |
| 55 ComponentUnpacker::Error MockComponentPatcher::Patch( |
| 56 PatchType patch_type, |
| 57 const base::FilePath& input_file, |
| 58 const base::FilePath& patch_file, |
| 59 const base::FilePath& output_file, |
| 60 int* error) { |
| 61 *error = 0; |
| 62 int exit_code; |
| 63 if (patch_type == kPatchTypeCourgette) { |
| 64 exit_code = courgette::ApplyEnsemblePatch(input_file.value().c_str(), |
| 65 patch_file.value().c_str(), |
| 66 output_file.value().c_str()); |
| 67 if (exit_code == courgette::C_OK) |
| 68 return ComponentUnpacker::kNone; |
| 69 *error = exit_code + kCourgetteErrorOffset; |
| 70 } else if (patch_type == kPatchTypeBsdiff) { |
| 71 exit_code = courgette::ApplyBinaryPatch(input_file, |
| 72 patch_file, |
| 73 output_file); |
| 74 if (exit_code == courgette::OK) |
| 75 return ComponentUnpacker::kNone; |
| 76 *error = exit_code + kBsdiffErrorOffset; |
| 77 } |
| 78 return ComponentUnpacker::kDeltaOperationFailure; |
| 79 } |
| 80 |
| 81 // Verify that a 'create' delta update operation works correctly. |
| 82 TEST_F(ComponentPatcherOperationTest, CheckCreateOperation) { |
| 83 EXPECT_TRUE(file_util::CopyFile( |
| 84 test_file("binary_output.bin"), |
| 85 input_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin")))); |
| 86 |
| 87 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); |
| 88 command_args->SetString("output", "output.bin"); |
| 89 command_args->SetString("sha256", binary_output_hash); |
| 90 command_args->SetString("op", "create"); |
| 91 command_args->SetString("patch", "binary_output.bin"); |
| 92 |
| 93 int error = 0; |
| 94 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCreate()); |
| 95 ComponentUnpacker::Error result = op->Run(command_args.get(), |
| 96 input_dir_.path(), |
| 97 unpack_dir_.path(), |
| 98 patcher_.get(), |
| 99 NULL, |
| 100 &error); |
| 101 |
| 102 EXPECT_EQ(ComponentUnpacker::kNone, result); |
| 103 EXPECT_EQ(0, error); |
| 104 EXPECT_TRUE(file_util::ContentsEqual( |
| 105 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), |
| 106 test_file("binary_output.bin"))); |
| 107 } |
| 108 |
| 109 // Verify that a 'copy' delta update operation works correctly. |
| 110 TEST_F(ComponentPatcherOperationTest, CheckCopyOperation) { |
| 111 EXPECT_TRUE(file_util::CopyFile( |
| 112 test_file("binary_output.bin"), |
| 113 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin")))); |
| 114 |
| 115 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); |
| 116 command_args->SetString("output", "output.bin"); |
| 117 command_args->SetString("sha256", binary_output_hash); |
| 118 command_args->SetString("op", "copy"); |
| 119 command_args->SetString("input", "binary_output.bin"); |
| 120 |
| 121 int error = 0; |
| 122 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpCopy()); |
| 123 ComponentUnpacker::Error result = op->Run(command_args.get(), |
| 124 input_dir_.path(), |
| 125 unpack_dir_.path(), |
| 126 patcher_.get(), |
| 127 installer_.get(), |
| 128 &error); |
| 129 EXPECT_EQ(ComponentUnpacker::kNone, result); |
| 130 EXPECT_EQ(0, error); |
| 131 EXPECT_TRUE(file_util::ContentsEqual( |
| 132 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), |
| 133 test_file("binary_output.bin"))); |
| 134 } |
| 135 |
| 136 // Verify that a 'courgette' delta update operation works correctly. |
| 137 TEST_F(ComponentPatcherOperationTest, CheckCourgetteOperation) { |
| 138 EXPECT_TRUE(file_util::CopyFile( |
| 139 test_file("binary_input.bin"), |
| 140 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin")))); |
| 141 EXPECT_TRUE(file_util::CopyFile( |
| 142 test_file("binary_courgette_patch.bin"), |
| 143 input_dir_.path().Append( |
| 144 FILE_PATH_LITERAL("binary_courgette_patch.bin")))); |
| 145 |
| 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"); |
| 152 |
| 153 int error = 0; |
| 154 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpPatchCourgette()); |
| 155 ComponentUnpacker::Error result = op->Run(command_args.get(), |
| 156 input_dir_.path(), |
| 157 unpack_dir_.path(), |
| 158 patcher_.get(), |
| 159 installer_.get(), |
| 160 &error); |
| 161 EXPECT_EQ(ComponentUnpacker::kNone, result); |
| 162 EXPECT_EQ(0, error); |
| 163 EXPECT_TRUE(file_util::ContentsEqual( |
| 164 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), |
| 165 test_file("binary_output.bin"))); |
| 166 } |
| 167 |
| 168 // Verify that a 'bsdiff' delta update operation works correctly. |
| 169 TEST_F(ComponentPatcherOperationTest, CheckBsdiffOperation) { |
| 170 EXPECT_TRUE(file_util::CopyFile( |
| 171 test_file("binary_input.bin"), |
| 172 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin")))); |
| 173 EXPECT_TRUE(file_util::CopyFile( |
| 174 test_file("binary_bsdiff_patch.bin"), |
| 175 input_dir_.path().Append(FILE_PATH_LITERAL("binary_bsdiff_patch.bin")))); |
| 176 |
| 177 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); |
| 178 command_args->SetString("output", "output.bin"); |
| 179 command_args->SetString("sha256", binary_output_hash); |
| 180 command_args->SetString("op", "courgette"); |
| 181 command_args->SetString("input", "binary_input.bin"); |
| 182 command_args->SetString("patch", "binary_bsdiff_patch.bin"); |
| 183 |
| 184 int error = 0; |
| 185 scoped_ptr<DeltaUpdateOp> op(new DeltaUpdateOpPatchBsdiff()); |
| 186 ComponentUnpacker::Error result = op->Run(command_args.get(), |
| 187 input_dir_.path(), |
| 188 unpack_dir_.path(), |
| 189 patcher_.get(), |
| 190 installer_.get(), |
| 191 &error); |
| 192 EXPECT_EQ(ComponentUnpacker::kNone, result); |
| 193 EXPECT_EQ(0, error); |
| 194 EXPECT_TRUE(file_util::ContentsEqual( |
| 195 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), |
| 196 test_file("binary_output.bin"))); |
| 197 } |
| 198 |
OLD | NEW |