| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "base/base_paths.h" | |
| 6 #include "base/bind.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/test/component_patcher_unittest.h" | |
| 19 #include "components/update_client/test/test_installer.h" | |
| 20 #include "courgette/courgette.h" | |
| 21 #include "courgette/third_party/bsdiff.h" | |
| 22 #include "testing/gtest/include/gtest/gtest.h" | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 class TestCallback { | |
| 27 public: | |
| 28 TestCallback(); | |
| 29 virtual ~TestCallback() {} | |
| 30 void Set(update_client::ComponentUnpacker::Error error, int extra_code); | |
| 31 | |
| 32 int error_; | |
| 33 int extra_code_; | |
| 34 bool called_; | |
| 35 | |
| 36 private: | |
| 37 DISALLOW_COPY_AND_ASSIGN(TestCallback); | |
| 38 }; | |
| 39 | |
| 40 TestCallback::TestCallback() : error_(-1), extra_code_(-1), called_(false) { | |
| 41 } | |
| 42 | |
| 43 void TestCallback::Set(update_client::ComponentUnpacker::Error error, | |
| 44 int extra_code) { | |
| 45 error_ = error; | |
| 46 extra_code_ = extra_code; | |
| 47 called_ = true; | |
| 48 } | |
| 49 | |
| 50 } // namespace | |
| 51 | |
| 52 namespace update_client { | |
| 53 | |
| 54 namespace { | |
| 55 | |
| 56 base::FilePath test_file(const char* file) { | |
| 57 base::FilePath path; | |
| 58 PathService::Get(base::DIR_SOURCE_ROOT, &path); | |
| 59 return path.AppendASCII("components") | |
| 60 .AppendASCII("test") | |
| 61 .AppendASCII("data") | |
| 62 .AppendASCII("update_client") | |
| 63 .AppendASCII(file); | |
| 64 } | |
| 65 | |
| 66 } // namespace | |
| 67 | |
| 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(); | |
| 74 } | |
| 75 | |
| 76 ComponentPatcherOperationTest::~ComponentPatcherOperationTest() { | |
| 77 } | |
| 78 | |
| 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")))); | |
| 84 | |
| 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"); | |
| 90 | |
| 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)), | |
| 95 task_runner_); | |
| 96 base::RunLoop().RunUntilIdle(); | |
| 97 | |
| 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"))); | |
| 104 } | |
| 105 | |
| 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")))); | |
| 111 | |
| 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"); | |
| 117 | |
| 118 TestCallback callback; | |
| 119 scoped_refptr<DeltaUpdateOp> op = new DeltaUpdateOpCopy(); | |
| 120 op->Run(command_args.get(), input_dir_.path(), unpack_dir_.path(), | |
| 121 installer_.get(), | |
| 122 base::Bind(&TestCallback::Set, base::Unretained(&callback)), | |
| 123 task_runner_); | |
| 124 base::RunLoop().RunUntilIdle(); | |
| 125 | |
| 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"))); | |
| 132 } | |
| 133 | |
| 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")))); | |
| 142 | |
| 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"); | |
| 149 | |
| 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(), | |
| 154 installer_.get(), | |
| 155 base::Bind(&TestCallback::Set, base::Unretained(&callback)), | |
| 156 task_runner_); | |
| 157 base::RunLoop().RunUntilIdle(); | |
| 158 | |
| 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"))); | |
| 165 } | |
| 166 | |
| 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")))); | |
| 175 | |
| 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"); | |
| 182 | |
| 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(), | |
| 187 installer_.get(), | |
| 188 base::Bind(&TestCallback::Set, base::Unretained(&callback)), | |
| 189 task_runner_); | |
| 190 base::RunLoop().RunUntilIdle(); | |
| 191 | |
| 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"))); | |
| 198 } | |
| 199 | |
| 200 } // namespace update_client | |
| OLD | NEW |