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/component_updater/component_patcher.h" 17#include "components/component_updater/component_patcher_operation.h" 18#include "components/component_updater/component_updater_service.h" 19#include "components/component_updater/test/component_patcher_unittest.h" 20#include "components/component_updater/test/test_installer.h" 21#include "courgette/courgette.h" 22#include "courgette/third_party/bsdiff.h" 23#include "testing/gtest/include/gtest/gtest.h" 24 25namespace { 26 27class 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 41TestCallback::TestCallback() : error_(-1), extra_code_(-1), called_(false) { 42} 43 44void 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 53namespace component_updater { 54 55namespace { 56 57base::FilePath test_file(const char* file) { 58 base::FilePath path; 59 PathService::Get(base::DIR_SOURCE_ROOT, &path); 60 return path.AppendASCII("components").AppendASCII("test").AppendASCII("data") 61 .AppendASCII("component_updater").AppendASCII(file); 62} 63 64} // namespace 65 66ComponentPatcherOperationTest::ComponentPatcherOperationTest() { 67 EXPECT_TRUE(unpack_dir_.CreateUniqueTempDir()); 68 EXPECT_TRUE(input_dir_.CreateUniqueTempDir()); 69 EXPECT_TRUE(installed_dir_.CreateUniqueTempDir()); 70 installer_.reset(new ReadOnlyTestInstaller(installed_dir_.path())); 71 task_runner_ = base::MessageLoop::current()->task_runner(); 72} 73 74ComponentPatcherOperationTest::~ComponentPatcherOperationTest() { 75} 76 77// Verify that a 'create' delta update operation works correctly. 78TEST_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 base::Bind(&TestCallback::Set, base::Unretained(&callback)), 96 task_runner_); 97 base::RunLoop().RunUntilIdle(); 98 99 EXPECT_EQ(true, callback.called_); 100 EXPECT_EQ(ComponentUnpacker::kNone, callback.error_); 101 EXPECT_EQ(0, callback.extra_code_); 102 EXPECT_TRUE(base::ContentsEqual( 103 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), 104 test_file("binary_output.bin"))); 105} 106 107// Verify that a 'copy' delta update operation works correctly. 108TEST_F(ComponentPatcherOperationTest, CheckCopyOperation) { 109 EXPECT_TRUE(base::CopyFile( 110 test_file("binary_output.bin"), 111 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_output.bin")))); 112 113 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); 114 command_args->SetString("output", "output.bin"); 115 command_args->SetString("sha256", binary_output_hash); 116 command_args->SetString("op", "copy"); 117 command_args->SetString("input", "binary_output.bin"); 118 119 TestCallback callback; 120 scoped_refptr<DeltaUpdateOp> op = new DeltaUpdateOpCopy(); 121 op->Run(command_args.get(), 122 input_dir_.path(), 123 unpack_dir_.path(), 124 installer_.get(), 125 base::Bind(&TestCallback::Set, base::Unretained(&callback)), 126 task_runner_); 127 base::RunLoop().RunUntilIdle(); 128 129 EXPECT_EQ(true, callback.called_); 130 EXPECT_EQ(ComponentUnpacker::kNone, callback.error_); 131 EXPECT_EQ(0, callback.extra_code_); 132 EXPECT_TRUE(base::ContentsEqual( 133 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), 134 test_file("binary_output.bin"))); 135} 136 137// Verify that a 'courgette' delta update operation works correctly. 138TEST_F(ComponentPatcherOperationTest, CheckCourgetteOperation) { 139 EXPECT_TRUE(base::CopyFile( 140 test_file("binary_input.bin"), 141 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin")))); 142 EXPECT_TRUE(base::CopyFile(test_file("binary_courgette_patch.bin"), 143 input_dir_.path().Append(FILE_PATH_LITERAL( 144 "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 TestCallback callback; 154 scoped_refptr<DeltaUpdateOp> op = 155 CreateDeltaUpdateOp("courgette", NULL /* out_of_process_patcher */); 156 op->Run(command_args.get(), 157 input_dir_.path(), 158 unpack_dir_.path(), 159 installer_.get(), 160 base::Bind(&TestCallback::Set, base::Unretained(&callback)), 161 task_runner_); 162 base::RunLoop().RunUntilIdle(); 163 164 EXPECT_EQ(true, callback.called_); 165 EXPECT_EQ(ComponentUnpacker::kNone, callback.error_); 166 EXPECT_EQ(0, callback.extra_code_); 167 EXPECT_TRUE(base::ContentsEqual( 168 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), 169 test_file("binary_output.bin"))); 170} 171 172// Verify that a 'bsdiff' delta update operation works correctly. 173TEST_F(ComponentPatcherOperationTest, CheckBsdiffOperation) { 174 EXPECT_TRUE(base::CopyFile( 175 test_file("binary_input.bin"), 176 installed_dir_.path().Append(FILE_PATH_LITERAL("binary_input.bin")))); 177 EXPECT_TRUE(base::CopyFile( 178 test_file("binary_bsdiff_patch.bin"), 179 input_dir_.path().Append(FILE_PATH_LITERAL("binary_bsdiff_patch.bin")))); 180 181 scoped_ptr<base::DictionaryValue> command_args(new base::DictionaryValue()); 182 command_args->SetString("output", "output.bin"); 183 command_args->SetString("sha256", binary_output_hash); 184 command_args->SetString("op", "courgette"); 185 command_args->SetString("input", "binary_input.bin"); 186 command_args->SetString("patch", "binary_bsdiff_patch.bin"); 187 188 TestCallback callback; 189 scoped_refptr<DeltaUpdateOp> op = 190 CreateDeltaUpdateOp("bsdiff", NULL /* out_of_process_patcher */); 191 op->Run(command_args.get(), 192 input_dir_.path(), 193 unpack_dir_.path(), 194 installer_.get(), 195 base::Bind(&TestCallback::Set, base::Unretained(&callback)), 196 task_runner_); 197 base::RunLoop().RunUntilIdle(); 198 199 EXPECT_EQ(true, callback.called_); 200 EXPECT_EQ(ComponentUnpacker::kNone, callback.error_); 201 EXPECT_EQ(0, callback.extra_code_); 202 EXPECT_TRUE(base::ContentsEqual( 203 unpack_dir_.path().Append(FILE_PATH_LITERAL("output.bin")), 204 test_file("binary_output.bin"))); 205} 206 207} // namespace component_updater 208