[email protected] | f5bf184 | 2012-02-15 02:52:26 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
noel | c8702c4 | 2017-03-16 08:51:19 | [diff] [blame] | 5 | #include "extensions/browser/sandboxed_unpacker.h" |
| 6 | |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 7 | #include "base/base64.h" |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 8 | #include "base/bind.h" |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 9 | #include "base/command_line.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 10 | #include "base/files/file_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 12 | #include "base/path_service.h" |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 13 | #include "base/run_loop.h" |
Jay Civelli | 6d0e68e | 2018-01-24 16:42:53 | [diff] [blame] | 14 | #include "base/strings/pattern.h" |
[email protected] | 46acbf1 | 2013-06-10 18:43:42 | [diff] [blame] | 15 | #include "base/strings/string_util.h" |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
gab | 273138e | 2016-05-11 18:09:39 | [diff] [blame] | 17 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 18 | #include "base/values.h" |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 19 | #include "components/crx_file/id_util.h" |
Ken Rockot | 61df041 | 2019-07-26 17:12:19 | [diff] [blame^] | 20 | #include "components/services/unzip/content/unzip_service.h" |
| 21 | #include "components/services/unzip/in_process_unzipper.h" |
Alex Kalugin | a34f8c0 | 2017-07-27 11:33:03 | [diff] [blame] | 22 | #include "content/public/browser/browser_thread.h" |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 23 | #include "content/public/test/test_browser_thread_bundle.h" |
| 24 | #include "content/public/test/test_utils.h" |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 25 | #include "extensions/browser/extensions_test.h" |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 26 | #include "extensions/browser/install/crx_install_error.h" |
| 27 | #include "extensions/browser/install/sandboxed_unpacker_failure_reason.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 28 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 29 | #include "extensions/common/extension.h" |
asargent | 12a9cab7 | 2015-01-16 21:34:16 | [diff] [blame] | 30 | #include "extensions/common/extension_paths.h" |
Jay Civelli | 6d0e68e | 2018-01-24 16:42:53 | [diff] [blame] | 31 | #include "extensions/common/manifest_constants.h" |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 32 | #include "extensions/common/switches.h" |
Joshua Pawlicki | fd01b7c | 2019-01-17 16:18:34 | [diff] [blame] | 33 | #include "extensions/common/verifier_formats.h" |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 34 | #include "extensions/strings/grit/extensions_strings.h" |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 35 | #include "extensions/test/test_extensions_client.h" |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 36 | #include "services/data_decoder/data_decoder_service.h" |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 37 | #include "services/data_decoder/public/cpp/test_data_decoder_service.h" |
Ken Rockot | a373add | 2018-10-30 23:22:42 | [diff] [blame] | 38 | #include "services/data_decoder/public/mojom/constants.mojom.h" |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 39 | #include "services/service_manager/public/cpp/connector.h" |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 40 | #include "services/service_manager/public/cpp/test/test_connector_factory.h" |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 41 | #include "testing/gmock/include/gmock/gmock.h" |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 42 | #include "testing/gtest/include/gtest/gtest.h" |
| 43 | #include "third_party/skia/include/core/SkBitmap.h" |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 44 | #include "third_party/zlib/google/zip.h" |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 45 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 46 | |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 47 | namespace extensions { |
| 48 | |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | // Inserts an illegal path into the browser images returned by |
| 52 | // TestExtensionsClient for any extension. |
| 53 | class IllegalImagePathInserter |
| 54 | : public TestExtensionsClient::BrowserImagePathsFilter { |
| 55 | public: |
| 56 | IllegalImagePathInserter(TestExtensionsClient* client) : client_(client) { |
| 57 | client_->AddBrowserImagePathsFilter(this); |
| 58 | } |
| 59 | |
| 60 | virtual ~IllegalImagePathInserter() { |
| 61 | client_->RemoveBrowserImagePathsFilter(this); |
| 62 | } |
| 63 | |
| 64 | void Filter(const Extension* extension, |
| 65 | std::set<base::FilePath>* paths) override { |
| 66 | base::FilePath illegal_path = |
| 67 | base::FilePath(base::FilePath::kParentDirectory) |
| 68 | .AppendASCII(kTempExtensionName) |
| 69 | .AppendASCII("product_logo_128.png"); |
| 70 | paths->insert(illegal_path); |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | TestExtensionsClient* client_; |
| 75 | }; |
| 76 | |
| 77 | } // namespace |
| 78 | |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 79 | class MockSandboxedUnpackerClient : public SandboxedUnpackerClient { |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 80 | public: |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 81 | void WaitForUnpack() { |
| 82 | scoped_refptr<content::MessageLoopRunner> runner = |
| 83 | new content::MessageLoopRunner; |
| 84 | quit_closure_ = runner->QuitClosure(); |
| 85 | runner->Run(); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 86 | } |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 87 | |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 88 | base::FilePath temp_dir() const { return temp_dir_; } |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 89 | base::string16 unpack_error_message() const { |
| 90 | if (error_) |
| 91 | return error_->message(); |
| 92 | return base::string16(); |
| 93 | } |
| 94 | CrxInstallErrorType unpack_error_type() const { |
| 95 | if (error_) |
| 96 | return error_->type(); |
| 97 | return CrxInstallErrorType::NONE; |
| 98 | } |
| 99 | int unpack_error_detail() const { |
| 100 | if (error_) { |
| 101 | return error_->type() == CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE |
| 102 | ? static_cast<int>(error_->sandbox_failure_detail()) |
| 103 | : static_cast<int>(error_->detail()); |
| 104 | } |
| 105 | return 0; |
| 106 | } |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 107 | |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 108 | void set_deleted_tracker(bool* deleted_tracker) { |
| 109 | deleted_tracker_ = deleted_tracker; |
| 110 | } |
| 111 | |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 112 | private: |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 113 | ~MockSandboxedUnpackerClient() override { |
| 114 | if (deleted_tracker_) |
| 115 | *deleted_tracker_ = true; |
| 116 | } |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 117 | |
Karandeep Bhatia | a893065 | 2017-10-11 17:41:12 | [diff] [blame] | 118 | void OnUnpackSuccess( |
| 119 | const base::FilePath& temp_dir, |
| 120 | const base::FilePath& extension_root, |
| 121 | std::unique_ptr<base::DictionaryValue> original_manifest, |
| 122 | const Extension* extension, |
| 123 | const SkBitmap& install_icon, |
| 124 | const base::Optional<int>& dnr_ruleset_checksum) override { |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 125 | temp_dir_ = temp_dir; |
| 126 | quit_closure_.Run(); |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 127 | } |
| 128 | |
ginkage | 47e603e | 2015-02-27 08:42:41 | [diff] [blame] | 129 | void OnUnpackFailure(const CrxInstallError& error) override { |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 130 | error_ = error; |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 131 | quit_closure_.Run(); |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 132 | } |
| 133 | |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 134 | base::Optional<CrxInstallError> error_; |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 135 | base::Closure quit_closure_; |
| 136 | base::FilePath temp_dir_; |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 137 | bool* deleted_tracker_ = nullptr; |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 138 | }; |
| 139 | |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 140 | class SandboxedUnpackerTest : public ExtensionsTest { |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 141 | public: |
danakj | aee6717 | 2017-06-13 16:37:02 | [diff] [blame] | 142 | SandboxedUnpackerTest() |
Alex Clarke | f0527a9b | 2019-01-23 10:43:25 | [diff] [blame] | 143 | : ExtensionsTest(content::TestBrowserThreadBundle::IO_MAINLOOP) { |
Ken Rockot | 1030629 | 2018-11-02 15:45:29 | [diff] [blame] | 144 | test_connector_factory_.set_ignore_quit_requests(true); |
| 145 | } |
Alex Kalugin | a34f8c0 | 2017-07-27 11:33:03 | [diff] [blame] | 146 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 147 | void SetUp() override { |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 148 | ExtensionsTest::SetUp(); |
| 149 | ASSERT_TRUE(extensions_dir_.CreateUniqueTempDir()); |
[email protected] | c514acf | 2014-01-09 07:16:52 | [diff] [blame] | 150 | in_process_utility_thread_helper_.reset( |
| 151 | new content::InProcessUtilityThreadHelper); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 152 | // It will delete itself. |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 153 | client_ = new MockSandboxedUnpackerClient; |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 154 | |
Ken Rockot | 61df041 | 2019-07-26 17:12:19 | [diff] [blame^] | 155 | InitSanboxedUnpacker(/*data_decode_service=*/nullptr); |
| 156 | |
| 157 | // By default, we host an in-process UnzipperImpl to support any service |
| 158 | // clients. Tests may explicitly override the launch callback to prevent |
| 159 | // this. |
| 160 | unzip::SetUnzipperLaunchOverrideForTesting( |
| 161 | base::BindRepeating(&unzip::LaunchInProcessUnzipper)); |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void InitSanboxedUnpacker( |
Ken Rockot | 61df041 | 2019-07-26 17:12:19 | [diff] [blame^] | 165 | std::unique_ptr<service_manager::Service> data_decoder_service) { |
Ken Rockot | a373add | 2018-10-30 23:22:42 | [diff] [blame] | 166 | if (data_decoder_service) { |
| 167 | data_decoder_service_ = std::move(data_decoder_service); |
| 168 | } else { |
| 169 | data_decoder_service_ = |
| 170 | std::make_unique<data_decoder::DataDecoderService>( |
| 171 | RegisterDataDecoder()); |
| 172 | } |
| 173 | |
Ken Rockot | a373add | 2018-10-30 23:22:42 | [diff] [blame] | 174 | connector_ = test_connector_factory_.CreateConnector(); |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 175 | |
| 176 | sandboxed_unpacker_ = |
| 177 | new SandboxedUnpacker(connector_->Clone(), Manifest::INTERNAL, |
| 178 | Extension::NO_FLAGS, extensions_dir_.GetPath(), |
| 179 | base::ThreadTaskRunnerHandle::Get(), client_); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 180 | } |
| 181 | |
Ken Rockot | a373add | 2018-10-30 23:22:42 | [diff] [blame] | 182 | service_manager::mojom::ServiceRequest RegisterDataDecoder() { |
| 183 | return test_connector_factory_.RegisterInstance( |
| 184 | data_decoder::mojom::kServiceName); |
| 185 | } |
| 186 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 187 | void TearDown() override { |
Ken Rockot | 61df041 | 2019-07-26 17:12:19 | [diff] [blame^] | 188 | unzip::SetUnzipperLaunchOverrideForTesting(base::NullCallback()); |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 189 | // Need to destruct SandboxedUnpacker before the message loop since |
[email protected] | 38f285a5 | 2010-11-05 21:02:28 | [diff] [blame] | 190 | // it posts a task to it. |
danakj | aee6717 | 2017-06-13 16:37:02 | [diff] [blame] | 191 | sandboxed_unpacker_ = nullptr; |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 192 | base::RunLoop().RunUntilIdle(); |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 193 | ExtensionsTest::TearDown(); |
danakj | aee6717 | 2017-06-13 16:37:02 | [diff] [blame] | 194 | in_process_utility_thread_helper_.reset(); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 195 | } |
| 196 | |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 197 | base::FilePath GetCrxFullPath(const std::string& crx_name) { |
| 198 | base::FilePath full_path; |
Avi Drissman | 210441b7 | 2018-05-01 15:51:00 | [diff] [blame] | 199 | EXPECT_TRUE(base::PathService::Get(extensions::DIR_TEST_DATA, &full_path)); |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 200 | full_path = full_path.AppendASCII("unpacker").AppendASCII(crx_name); |
| 201 | EXPECT_TRUE(base::PathExists(full_path)) << full_path.value(); |
| 202 | return full_path; |
| 203 | } |
| 204 | |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 205 | void SetupUnpacker(const std::string& crx_name, |
| 206 | const std::string& package_hash) { |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 207 | base::FilePath crx_path = GetCrxFullPath(crx_name); |
pranay.kumar | 7b7ceb18 | 2015-05-05 12:12:46 | [diff] [blame] | 208 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 209 | FROM_HERE, |
kylechar | 0686a523 | 2019-02-19 14:10:29 | [diff] [blame] | 210 | base::BindOnce( |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 211 | &SandboxedUnpacker::StartWithCrx, sandboxed_unpacker_, |
Joshua Pawlicki | fd01b7c | 2019-01-17 16:18:34 | [diff] [blame] | 212 | extensions::CRXFileInfo(std::string(), crx_path, package_hash, |
| 213 | GetTestVerifierFormat()))); |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 214 | client_->WaitForUnpack(); |
| 215 | } |
| 216 | |
| 217 | void SetupUnpackerWithDirectory(const std::string& crx_name) { |
| 218 | base::ScopedTempDir temp_dir; |
| 219 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 220 | base::FilePath crx_path = GetCrxFullPath(crx_name); |
vabr | 9142fe2 | 2016-09-08 13:19:22 | [diff] [blame] | 221 | ASSERT_TRUE(zip::Unzip(crx_path, temp_dir.GetPath())); |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 222 | |
| 223 | std::string fake_id = crx_file::id_util::GenerateId(crx_name); |
| 224 | std::string fake_public_key; |
| 225 | base::Base64Encode(std::string(2048, 'k'), &fake_public_key); |
| 226 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
kylechar | 0686a523 | 2019-02-19 14:10:29 | [diff] [blame] | 227 | FROM_HERE, base::BindOnce(&SandboxedUnpacker::StartWithDirectory, |
| 228 | sandboxed_unpacker_, fake_id, fake_public_key, |
| 229 | temp_dir.Take())); |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 230 | client_->WaitForUnpack(); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 231 | } |
| 232 | |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 233 | bool InstallSucceeded() const { return !client_->temp_dir().empty(); } |
| 234 | |
| 235 | base::FilePath GetInstallPath() const { |
[email protected] | b22c8af6 | 2013-07-23 23:17:02 | [diff] [blame] | 236 | return client_->temp_dir().AppendASCII(kTempExtensionName); |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 237 | } |
| 238 | |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 239 | base::string16 GetInstallErrorMessage() const { |
| 240 | return client_->unpack_error_message(); |
| 241 | } |
| 242 | |
| 243 | CrxInstallErrorType GetInstallErrorType() const { |
| 244 | return client_->unpack_error_type(); |
| 245 | } |
| 246 | |
| 247 | int GetInstallErrorDetail() const { return client_->unpack_error_detail(); } |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 248 | |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 249 | void ExpectInstallErrorContains(const std::string& error) { |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 250 | std::string full_error = base::UTF16ToUTF8(client_->unpack_error_message()); |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 251 | EXPECT_TRUE(full_error.find(error) != std::string::npos) |
| 252 | << "Error message " << full_error << " does not contain " << error; |
| 253 | } |
| 254 | |
| 255 | // Unpacks the package |package_name| and checks that |sandboxed_unpacker_| |
| 256 | // gets deleted. |
| 257 | void TestSandboxedUnpackerDeleted(const std::string& package_name, |
| 258 | bool expect_success) { |
| 259 | bool client_deleted = false; |
| 260 | client_->set_deleted_tracker(&client_deleted); |
| 261 | SetupUnpacker(package_name, ""); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 262 | EXPECT_EQ(GetInstallErrorMessage().empty(), expect_success); |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 263 | // Remove our reference to |sandboxed_unpacker_|, it should get deleted |
| 264 | // since/ it's the last reference. |
| 265 | sandboxed_unpacker_ = nullptr; |
| 266 | // The SandboxedUnpacker should have been deleted and deleted the client. |
| 267 | EXPECT_TRUE(client_deleted); |
| 268 | } |
| 269 | |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 270 | protected: |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 271 | base::ScopedTempDir extensions_dir_; |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 272 | MockSandboxedUnpackerClient* client_; |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 273 | scoped_refptr<SandboxedUnpacker> sandboxed_unpacker_; |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 274 | std::unique_ptr<content::InProcessUtilityThreadHelper> |
[email protected] | c514acf | 2014-01-09 07:16:52 | [diff] [blame] | 275 | in_process_utility_thread_helper_; |
Ken Rockot | a373add | 2018-10-30 23:22:42 | [diff] [blame] | 276 | |
| 277 | service_manager::TestConnectorFactory test_connector_factory_; |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 278 | std::unique_ptr<service_manager::Connector> connector_; |
Ken Rockot | a373add | 2018-10-30 23:22:42 | [diff] [blame] | 279 | |
| 280 | std::unique_ptr<service_manager::Service> data_decoder_service_; |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 281 | }; |
| 282 | |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 283 | TEST_F(SandboxedUnpackerTest, EmptyDefaultLocale) { |
| 284 | SetupUnpacker("empty_default_locale.crx", ""); |
| 285 | ExpectInstallErrorContains(manifest_errors::kInvalidDefaultLocale); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 286 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 287 | GetInstallErrorType()); |
| 288 | EXPECT_EQ( |
| 289 | static_cast<int>(SandboxedUnpackerFailureReason::UNPACKER_CLIENT_FAILED), |
| 290 | GetInstallErrorDetail()); |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | TEST_F(SandboxedUnpackerTest, HasDefaultLocaleMissingLocalesFolder) { |
| 294 | SetupUnpacker("has_default_missing_locales.crx", ""); |
| 295 | ExpectInstallErrorContains(manifest_errors::kLocalesTreeMissing); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 296 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 297 | GetInstallErrorType()); |
| 298 | EXPECT_EQ( |
| 299 | static_cast<int>(SandboxedUnpackerFailureReason::UNPACKER_CLIENT_FAILED), |
| 300 | GetInstallErrorDetail()); |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | TEST_F(SandboxedUnpackerTest, InvalidDefaultLocale) { |
| 304 | SetupUnpacker("invalid_default_locale.crx", ""); |
| 305 | ExpectInstallErrorContains(manifest_errors::kInvalidDefaultLocale); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 306 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 307 | GetInstallErrorType()); |
| 308 | EXPECT_EQ( |
| 309 | static_cast<int>(SandboxedUnpackerFailureReason::UNPACKER_CLIENT_FAILED), |
| 310 | GetInstallErrorDetail()); |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | TEST_F(SandboxedUnpackerTest, MissingDefaultData) { |
| 314 | SetupUnpacker("missing_default_data.crx", ""); |
| 315 | ExpectInstallErrorContains(manifest_errors::kLocalesNoDefaultMessages); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 316 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 317 | GetInstallErrorType()); |
| 318 | EXPECT_EQ( |
| 319 | static_cast<int>(SandboxedUnpackerFailureReason::UNPACKER_CLIENT_FAILED), |
| 320 | GetInstallErrorDetail()); |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | TEST_F(SandboxedUnpackerTest, MissingDefaultLocaleHasLocalesFolder) { |
| 324 | SetupUnpacker("missing_default_has_locales.crx", ""); |
| 325 | ExpectInstallErrorContains(l10n_util::GetStringUTF8( |
| 326 | IDS_EXTENSION_LOCALES_NO_DEFAULT_LOCALE_SPECIFIED)); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 327 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 328 | GetInstallErrorType()); |
| 329 | EXPECT_EQ( |
| 330 | static_cast<int>(SandboxedUnpackerFailureReason::UNPACKER_CLIENT_FAILED), |
| 331 | GetInstallErrorDetail()); |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | TEST_F(SandboxedUnpackerTest, MissingMessagesFile) { |
| 335 | SetupUnpacker("missing_messages_file.crx", ""); |
| 336 | EXPECT_TRUE(base::MatchPattern( |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 337 | GetInstallErrorMessage(), |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 338 | base::ASCIIToUTF16("*") + |
| 339 | base::ASCIIToUTF16(manifest_errors::kLocalesMessagesFileMissing) + |
| 340 | base::ASCIIToUTF16("*_locales?en_US?messages.json'."))) |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 341 | << GetInstallErrorMessage(); |
| 342 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 343 | GetInstallErrorType()); |
| 344 | EXPECT_EQ( |
| 345 | static_cast<int>(SandboxedUnpackerFailureReason::UNPACKER_CLIENT_FAILED), |
| 346 | GetInstallErrorDetail()); |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | TEST_F(SandboxedUnpackerTest, NoLocaleData) { |
| 350 | SetupUnpacker("no_locale_data.crx", ""); |
| 351 | ExpectInstallErrorContains(manifest_errors::kLocalesNoDefaultMessages); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 352 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 353 | GetInstallErrorType()); |
| 354 | EXPECT_EQ( |
| 355 | static_cast<int>(SandboxedUnpackerFailureReason::UNPACKER_CLIENT_FAILED), |
| 356 | GetInstallErrorDetail()); |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 357 | } |
| 358 | |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 359 | TEST_F(SandboxedUnpackerTest, ImageDecodingError) { |
| 360 | const char kExpected[] = "Could not decode image: "; |
| 361 | SetupUnpacker("bad_image.crx", ""); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 362 | EXPECT_TRUE(base::StartsWith(GetInstallErrorMessage(), |
| 363 | base::ASCIIToUTF16(kExpected), |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 364 | base::CompareCase::INSENSITIVE_ASCII)) |
| 365 | << "Expected prefix: \"" << kExpected << "\", actual error: \"" |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 366 | << GetInstallErrorMessage() << "\""; |
| 367 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 368 | GetInstallErrorType()); |
| 369 | EXPECT_EQ( |
| 370 | static_cast<int>(SandboxedUnpackerFailureReason::UNPACKER_CLIENT_FAILED), |
| 371 | GetInstallErrorDetail()); |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | TEST_F(SandboxedUnpackerTest, BadPathError) { |
| 375 | IllegalImagePathInserter inserter( |
| 376 | static_cast<TestExtensionsClient*>(ExtensionsClient::Get())); |
| 377 | SetupUnpacker("good_package.crx", ""); |
| 378 | // Install should have failed with an error. |
| 379 | EXPECT_FALSE(InstallSucceeded()); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 380 | EXPECT_FALSE(GetInstallErrorMessage().empty()); |
| 381 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 382 | GetInstallErrorType()); |
| 383 | EXPECT_EQ(static_cast<int>( |
| 384 | SandboxedUnpackerFailureReason::INVALID_PATH_FOR_BROWSER_IMAGE), |
| 385 | GetInstallErrorDetail()); |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 386 | } |
| 387 | |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 388 | TEST_F(SandboxedUnpackerTest, NoCatalogsSuccess) { |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 389 | SetupUnpacker("no_l10n.crx", ""); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 390 | // Check that there is no _locales folder. |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 391 | base::FilePath install_path = GetInstallPath().Append(kLocaleFolder); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 392 | EXPECT_FALSE(base::PathExists(install_path)); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 393 | EXPECT_EQ(CrxInstallErrorType::NONE, GetInstallErrorType()); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 394 | } |
| 395 | |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 396 | TEST_F(SandboxedUnpackerTest, FromDirNoCatalogsSuccess) { |
| 397 | SetupUnpackerWithDirectory("no_l10n.crx"); |
| 398 | // Check that there is no _locales folder. |
| 399 | base::FilePath install_path = GetInstallPath().Append(kLocaleFolder); |
| 400 | EXPECT_FALSE(base::PathExists(install_path)); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 401 | EXPECT_EQ(CrxInstallErrorType::NONE, GetInstallErrorType()); |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 402 | } |
| 403 | |
[email protected] | 11170a77 | 2013-12-13 11:38:32 | [diff] [blame] | 404 | TEST_F(SandboxedUnpackerTest, WithCatalogsSuccess) { |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 405 | SetupUnpacker("good_l10n.crx", ""); |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 406 | // Check that there is _locales folder. |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 407 | base::FilePath install_path = GetInstallPath().Append(kLocaleFolder); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 408 | EXPECT_TRUE(base::PathExists(install_path)); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 409 | EXPECT_EQ(CrxInstallErrorType::NONE, GetInstallErrorType()); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 410 | } |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 411 | |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 412 | TEST_F(SandboxedUnpackerTest, FromDirWithCatalogsSuccess) { |
| 413 | SetupUnpackerWithDirectory("good_l10n.crx"); |
| 414 | // Check that there is _locales folder. |
| 415 | base::FilePath install_path = GetInstallPath().Append(kLocaleFolder); |
| 416 | EXPECT_TRUE(base::PathExists(install_path)); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 417 | EXPECT_EQ(CrxInstallErrorType::NONE, GetInstallErrorType()); |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 418 | } |
| 419 | |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 420 | TEST_F(SandboxedUnpackerTest, FailHashCheck) { |
| 421 | base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 422 | extensions::switches::kEnableCrxHashCheck); |
waffles | 5918d5f | 2017-05-23 01:45:28 | [diff] [blame] | 423 | SetupUnpacker("good_l10n.crx", std::string(64, '0')); |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 424 | // Check that there is an error message. |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 425 | EXPECT_FALSE(GetInstallErrorMessage().empty()); |
| 426 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 427 | GetInstallErrorType()); |
| 428 | EXPECT_EQ(static_cast<int>( |
| 429 | SandboxedUnpackerFailureReason::CRX_HASH_VERIFICATION_FAILED), |
| 430 | GetInstallErrorDetail()); |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 431 | } |
| 432 | |
Jay Civelli | 6d0e68e | 2018-01-24 16:42:53 | [diff] [blame] | 433 | TEST_F(SandboxedUnpackerTest, InvalidMessagesFile) { |
| 434 | SetupUnpackerWithDirectory("invalid_messages_file.crx"); |
| 435 | // Check that there is no _locales folder. |
| 436 | base::FilePath install_path = GetInstallPath().Append(kLocaleFolder); |
| 437 | EXPECT_FALSE(base::PathExists(install_path)); |
| 438 | EXPECT_TRUE(base::MatchPattern( |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 439 | GetInstallErrorMessage(), |
Jay Civelli | 6d0e68e | 2018-01-24 16:42:53 | [diff] [blame] | 440 | base::ASCIIToUTF16("*_locales?en_US?messages.json': Line: 2, column: 10," |
| 441 | " Syntax error.'."))) |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 442 | << GetInstallErrorMessage(); |
| 443 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 444 | GetInstallErrorType()); |
| 445 | EXPECT_EQ(static_cast<int>( |
| 446 | SandboxedUnpackerFailureReason::COULD_NOT_LOCALIZE_EXTENSION), |
| 447 | GetInstallErrorDetail()); |
Jay Civelli | 6d0e68e | 2018-01-24 16:42:53 | [diff] [blame] | 448 | } |
| 449 | |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 450 | TEST_F(SandboxedUnpackerTest, PassHashCheck) { |
| 451 | base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 452 | extensions::switches::kEnableCrxHashCheck); |
| 453 | SetupUnpacker( |
| 454 | "good_l10n.crx", |
Devlin Cronin | 1de93897 | 2018-04-24 19:17:15 | [diff] [blame] | 455 | "614AE3D608F4C2185E9173293AB3F93EE7C7C79C9A2C3CF71F633386A3296A6C"); |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 456 | // Check that there is no error message. |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 457 | EXPECT_THAT(GetInstallErrorMessage(), testing::IsEmpty()); |
| 458 | EXPECT_EQ(CrxInstallErrorType::NONE, GetInstallErrorType()); |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | TEST_F(SandboxedUnpackerTest, SkipHashCheck) { |
| 462 | SetupUnpacker("good_l10n.crx", "badhash"); |
| 463 | // Check that there is no error message. |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 464 | EXPECT_THAT(GetInstallErrorMessage(), testing::IsEmpty()); |
| 465 | EXPECT_EQ(CrxInstallErrorType::NONE, GetInstallErrorType()); |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 466 | } |
| 467 | |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 468 | // The following tests simulate the utility services failling. |
| 469 | TEST_F(SandboxedUnpackerTest, UnzipperServiceFails) { |
Ken Rockot | 61df041 | 2019-07-26 17:12:19 | [diff] [blame^] | 470 | // We override the Unzipper's launching behavior to drop the interface |
| 471 | // receiver, effectively simulating a crashy service process. |
| 472 | unzip::SetUnzipperLaunchOverrideForTesting(base::BindRepeating([]() -> auto { |
| 473 | mojo::PendingRemote<unzip::mojom::Unzipper> remote; |
| 474 | ignore_result(remote.InitWithNewPipeAndPassReceiver()); |
| 475 | return remote; |
| 476 | })); |
| 477 | |
| 478 | InitSanboxedUnpacker(/*data_decoder_service=*/nullptr); |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 479 | SetupUnpacker("good_package.crx", ""); |
| 480 | EXPECT_FALSE(InstallSucceeded()); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 481 | EXPECT_FALSE(GetInstallErrorMessage().empty()); |
| 482 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 483 | GetInstallErrorType()); |
| 484 | EXPECT_EQ(static_cast<int>(SandboxedUnpackerFailureReason::UNZIP_FAILED), |
| 485 | GetInstallErrorDetail()); |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | TEST_F(SandboxedUnpackerTest, JsonParserFails) { |
Ken Rockot | 61df041 | 2019-07-26 17:12:19 | [diff] [blame^] | 489 | InitSanboxedUnpacker(std::make_unique<data_decoder::CrashyDataDecoderService>( |
| 490 | RegisterDataDecoder(), /*crash_json=*/true, /*crash_image=*/false)); |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 491 | SetupUnpacker("good_package.crx", ""); |
| 492 | EXPECT_FALSE(InstallSucceeded()); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 493 | EXPECT_FALSE(GetInstallErrorMessage().empty()); |
| 494 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 495 | GetInstallErrorType()); |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | TEST_F(SandboxedUnpackerTest, ImageDecoderFails) { |
Ken Rockot | 61df041 | 2019-07-26 17:12:19 | [diff] [blame^] | 499 | InitSanboxedUnpacker(std::make_unique<data_decoder::CrashyDataDecoderService>( |
| 500 | RegisterDataDecoder(), /*crash_json=*/false, /*crash_image=*/true)); |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 501 | SetupUnpacker("good_package.crx", ""); |
| 502 | EXPECT_FALSE(InstallSucceeded()); |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 503 | EXPECT_FALSE(GetInstallErrorMessage().empty()); |
| 504 | ASSERT_EQ(CrxInstallErrorType::SANDBOXED_UNPACKER_FAILURE, |
| 505 | GetInstallErrorType()); |
| 506 | EXPECT_EQ( |
| 507 | static_cast<int>(SandboxedUnpackerFailureReason:: |
| 508 | UTILITY_PROCESS_CRASHED_WHILE_TRYING_TO_INSTALL), |
| 509 | GetInstallErrorDetail()); |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 510 | } |
| 511 | |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 512 | // SandboxedUnpacker is ref counted and is reference by callbacks and |
| 513 | // InterfacePtrs. This tests that it gets deleted as expected (so that no extra |
| 514 | // refs are left). |
| 515 | TEST_F(SandboxedUnpackerTest, DeletedOnSuccess) { |
| 516 | TestSandboxedUnpackerDeleted("good_l10n.crx", /*expect_success=*/true); |
| 517 | } |
| 518 | |
| 519 | TEST_F(SandboxedUnpackerTest, DeletedOnFailure) { |
| 520 | TestSandboxedUnpackerDeleted("bad_image.crx", /*expect_success=*/false); |
| 521 | } |
| 522 | |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 523 | } // namespace extensions |