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/bind.h" 6 #include "base/compiler_specific.h" 7 #include "base/file_util.h" 8 #include "base/files/file_path.h" 9 #include "base/files/scoped_temp_dir.h" 10 #include "base/message_loop/message_loop.h" 11 #include "base/path_service.h" 12 #include "base/run_loop.h" 13 #include "base/values.h" 14 #include "chrome/browser/component_updater/component_patcher.h" 15 #include "chrome/browser/component_updater/component_patcher_operation.h" 16 #include "chrome/browser/component_updater/component_updater_service.h" 17 #include "chrome/browser/component_updater/test/component_patcher_unittest.h" 18 #include "chrome/browser/component_updater/test/test_installer.h" 19 #include "chrome/common/chrome_paths.h" 20 #include "content/public/browser/browser_thread.h" 21 #include "courgette/courgette.h" 22 #include "courgette/third_party/bsdiff.h" 23 #include "testing/gtest/include/gtest/gtest.h" 24 25 namespace { 26 27 class TestCallback { 28 public: 29 TestCallback(); 30 virtual ~TestCallback() {} 31 void Set(component_updater::ComponentUnpacker::Error error, int extra_code); 32 33 int error_; 34 int extra_code_; 35 bool called_; 36 37 private: 38 DISALLOW_COPY_AND_ASSIGN(TestCallback); 39 }; 40 41 TestCallback::TestCallback() : error_(-1), extra_code_(-1), called_(false) { 42 } 43 44 void TestCallback::Set(component_updater::ComponentUnpacker::Error error, 45 int extra_code) { 46 error_ = error; 47 extra_code_ = extra_code; 48 called_ = true; 49 } 50 51 } // namespace 52 53 namespace component_updater { 54 55 namespace { 56 57 base::FilePath test_file(const char* file) { 58 base::FilePath path; 59 PathService::Get(chrome::DIR_TEST_DATA, &path); 60 return path.AppendASCII("components").AppendASCII(file); 61 } 62 63 } // namespace 64 65 ComponentPatcherOperationTest::ComponentPatcherOperationTest() { 66 EXPECT_TRUE(unpack_dir_.CreateUniqueTempDir()); 67 EXPECT_TRUE(input_dir_.CreateUniqueTempDir()); 68 EXPECT_TRUE(installed_dir_.CreateUniqueTempDir()); 69 installer_.reset(new ReadOnlyTestInstaller(installed_dir_.path())); 70 task_runner_ = content::BrowserThread::GetMessageLoopProxyForThread( 71 content::BrowserThread::FILE); 72 } 73 74 ComponentPatcherOperationTest::~ComponentPatcherOperationTest() { 75 } 76 77 // Verify that a 'create' delta update operation works correctly. 78 TEST_F(ComponentPatcherOperationTest, CheckCreateOperation) { 79 EXPECT_TRUE(base::CopyFile( 80 test_file("binary_output.bin"), 81 input_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin")))); 82 83 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); 84 command_args->SetString("output", "output.bin"); 85 command_args->SetString("sha256", binary_output_hash); 86 command_args->SetString("op", "create"); 87 command_args->SetString("patch", "binary_output.bin"); 88 89 TestCallback callback; 90 scoped_refptr<DeltaUpdateOp> op = new DeltaUpdateOpCreate(); 91 op->Run(command_args.get(), 92 input_dir_.path(), 93 unpack_dir_.path(), 94 NULL, 95 true, 96 base::Bind(&TestCallback::Set, base::Unretained(&callback)), 97 task_runner_); 98 base::RunLoop().RunUntilIdle(); 99 100 EXPECT_EQ(true, callback.called_); 101 EXPECT_EQ(ComponentUnpacker::kNone, callback.error_); 102 EXPECT_EQ(0, callback.extra_code_); 103 EXPECT_TRUE(base::ContentsEqual( 104 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), 105 test_file("binary_output.bin"))); 106 } 107 108 // Verify that a 'copy' delta update operation works correctly. 109 TEST_F(ComponentPatcherOperationTest, CheckCopyOperation) { 110 EXPECT_TRUE(base::CopyFile( 111 test_file("binary_output.bin"), 112 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin")))); 113 114 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); 115 command_args->SetString("output", "output.bin"); 116 command_args->SetString("sha256", binary_output_hash); 117 command_args->SetString("op", "copy"); 118 command_args->SetString("input", "binary_output.bin"); 119 120 TestCallback callback; 121 scoped_refptr<DeltaUpdateOp> op = new DeltaUpdateOpCopy(); 122 op->Run(command_args.get(), 123 input_dir_.path(), 124 unpack_dir_.path(), 125 installer_.get(), 126 true, 127 base::Bind(&TestCallback::Set, base::Unretained(&callback)), 128 task_runner_); 129 base::RunLoop().RunUntilIdle(); 130 131 EXPECT_EQ(true, callback.called_); 132 EXPECT_EQ(ComponentUnpacker::kNone, callback.error_); 133 EXPECT_EQ(0, callback.extra_code_); 134 EXPECT_TRUE(base::ContentsEqual( 135 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), 136 test_file("binary_output.bin"))); 137 } 138 139 // Verify that a 'courgette' delta update operation works correctly. 140 TEST_F(ComponentPatcherOperationTest, CheckCourgetteOperation) { 141 EXPECT_TRUE(base::CopyFile( 142 test_file("binary_input.bin"), 143 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin")))); 144 EXPECT_TRUE(base::CopyFile(test_file("binary_courgette_patch.bin"), 145 input_dir_.path().Append(FILE_PATH_LITERAL( 146 "binary_courgette_patch.bin")))); 147 148 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); 149 command_args->SetString("output", "output.bin"); 150 command_args->SetString("sha256", binary_output_hash); 151 command_args->SetString("op", "courgette"); 152 command_args->SetString("input", "binary_input.bin"); 153 command_args->SetString("patch", "binary_courgette_patch.bin"); 154 155 TestCallback callback; 156 scoped_refptr<DeltaUpdateOp> op = CreateDeltaUpdateOp("courgette"); 157 op->Run(command_args.get(), 158 input_dir_.path(), 159 unpack_dir_.path(), 160 installer_.get(), 161 true, 162 base::Bind(&TestCallback::Set, base::Unretained(&callback)), 163 task_runner_); 164 base::RunLoop().RunUntilIdle(); 165 166 EXPECT_EQ(true, callback.called_); 167 EXPECT_EQ(ComponentUnpacker::kNone, callback.error_); 168 EXPECT_EQ(0, callback.extra_code_); 169 EXPECT_TRUE(base::ContentsEqual( 170 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), 171 test_file("binary_output.bin"))); 172 } 173 174 // Verify that a 'bsdiff' delta update operation works correctly. 175 TEST_F(ComponentPatcherOperationTest, CheckBsdiffOperation) { 176 EXPECT_TRUE(base::CopyFile( 177 test_file("binary_input.bin"), 178 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin")))); 179 EXPECT_TRUE(base::CopyFile( 180 test_file("binary_bsdiff_patch.bin"), 181 input_dir_.path().Append(FILE_PATH_LITERAL("binary_bsdiff_patch.bin")))); 182 183 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); 184 command_args->SetString("output", "output.bin"); 185 command_args->SetString("sha256", binary_output_hash); 186 command_args->SetString("op", "courgette"); 187 command_args->SetString("input", "binary_input.bin"); 188 command_args->SetString("patch", "binary_bsdiff_patch.bin"); 189 190 TestCallback callback; 191 scoped_refptr<DeltaUpdateOp> op = CreateDeltaUpdateOp("bsdiff"); 192 op->Run(command_args.get(), 193 input_dir_.path(), 194 unpack_dir_.path(), 195 installer_.get(), 196 true, 197 base::Bind(&TestCallback::Set, base::Unretained(&callback)), 198 task_runner_); 199 base::RunLoop().RunUntilIdle(); 200 201 EXPECT_EQ(true, callback.called_); 202 EXPECT_EQ(ComponentUnpacker::kNone, callback.error_); 203 EXPECT_EQ(0, callback.extra_code_); 204 EXPECT_TRUE(base::ContentsEqual( 205 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), 206 test_file("binary_output.bin"))); 207 } 208 209 } // namespace component_updater 210