[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" |
Colin Blundell | 6a897c7 | 2018-03-16 11:14:14 | [diff] [blame] | 20 | #include "components/services/unzip/public/cpp/test_unzip_service.h" |
| 21 | #include "components/services/unzip/unzip_service.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" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 26 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 27 | #include "extensions/common/extension.h" |
asargent | 12a9cab7 | 2015-01-16 21:34:16 | [diff] [blame] | 28 | #include "extensions/common/extension_paths.h" |
Jay Civelli | 6d0e68e | 2018-01-24 16:42:53 | [diff] [blame] | 29 | #include "extensions/common/manifest_constants.h" |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 30 | #include "extensions/common/switches.h" |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 31 | #include "extensions/strings/grit/extensions_strings.h" |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 32 | #include "extensions/test/test_extensions_client.h" |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 33 | #include "services/data_decoder/data_decoder_service.h" |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 34 | #include "services/data_decoder/public/cpp/test_data_decoder_service.h" |
| 35 | #include "services/service_manager/public/cpp/connector.h" |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 36 | #include "services/service_manager/public/cpp/test/test_connector_factory.h" |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 37 | #include "testing/gtest/include/gtest/gtest.h" |
| 38 | #include "third_party/skia/include/core/SkBitmap.h" |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 39 | #include "third_party/zlib/google/zip.h" |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 40 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 41 | |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 42 | namespace extensions { |
| 43 | |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 44 | namespace { |
| 45 | |
| 46 | // Inserts an illegal path into the browser images returned by |
| 47 | // TestExtensionsClient for any extension. |
| 48 | class IllegalImagePathInserter |
| 49 | : public TestExtensionsClient::BrowserImagePathsFilter { |
| 50 | public: |
| 51 | IllegalImagePathInserter(TestExtensionsClient* client) : client_(client) { |
| 52 | client_->AddBrowserImagePathsFilter(this); |
| 53 | } |
| 54 | |
| 55 | virtual ~IllegalImagePathInserter() { |
| 56 | client_->RemoveBrowserImagePathsFilter(this); |
| 57 | } |
| 58 | |
| 59 | void Filter(const Extension* extension, |
| 60 | std::set<base::FilePath>* paths) override { |
| 61 | base::FilePath illegal_path = |
| 62 | base::FilePath(base::FilePath::kParentDirectory) |
| 63 | .AppendASCII(kTempExtensionName) |
| 64 | .AppendASCII("product_logo_128.png"); |
| 65 | paths->insert(illegal_path); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | TestExtensionsClient* client_; |
| 70 | }; |
| 71 | |
| 72 | } // namespace |
| 73 | |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 74 | class MockSandboxedUnpackerClient : public SandboxedUnpackerClient { |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 75 | public: |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 76 | void WaitForUnpack() { |
| 77 | scoped_refptr<content::MessageLoopRunner> runner = |
| 78 | new content::MessageLoopRunner; |
| 79 | quit_closure_ = runner->QuitClosure(); |
| 80 | runner->Run(); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 81 | } |
[email protected] | 5f2a475 | 2012-04-27 22:18:58 | [diff] [blame] | 82 | |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 83 | base::FilePath temp_dir() const { return temp_dir_; } |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 84 | base::string16 unpack_err() const { return error_; } |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 85 | |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 86 | void set_deleted_tracker(bool* deleted_tracker) { |
| 87 | deleted_tracker_ = deleted_tracker; |
| 88 | } |
| 89 | |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 90 | private: |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 91 | ~MockSandboxedUnpackerClient() override { |
| 92 | if (deleted_tracker_) |
| 93 | *deleted_tracker_ = true; |
| 94 | } |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 95 | |
Karandeep Bhatia | a893065 | 2017-10-11 17:41:12 | [diff] [blame] | 96 | void OnUnpackSuccess( |
| 97 | const base::FilePath& temp_dir, |
| 98 | const base::FilePath& extension_root, |
| 99 | std::unique_ptr<base::DictionaryValue> original_manifest, |
| 100 | const Extension* extension, |
| 101 | const SkBitmap& install_icon, |
| 102 | const base::Optional<int>& dnr_ruleset_checksum) override { |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 103 | temp_dir_ = temp_dir; |
| 104 | quit_closure_.Run(); |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 105 | } |
| 106 | |
ginkage | 47e603e | 2015-02-27 08:42:41 | [diff] [blame] | 107 | void OnUnpackFailure(const CrxInstallError& error) override { |
| 108 | error_ = error.message(); |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 109 | quit_closure_.Run(); |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 110 | } |
| 111 | |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 112 | base::string16 error_; |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 113 | base::Closure quit_closure_; |
| 114 | base::FilePath temp_dir_; |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 115 | bool* deleted_tracker_ = nullptr; |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 116 | }; |
| 117 | |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 118 | class SandboxedUnpackerTest : public ExtensionsTest { |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 119 | public: |
danakj | aee6717 | 2017-06-13 16:37:02 | [diff] [blame] | 120 | SandboxedUnpackerTest() |
Alex Kalugin | a34f8c0 | 2017-07-27 11:33:03 | [diff] [blame] | 121 | : SandboxedUnpackerTest(content::TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 122 | |
| 123 | SandboxedUnpackerTest(content::TestBrowserThreadBundle::Options options) |
Lukasz Anforowicz | 58d0dac | 2018-03-23 15:48:10 | [diff] [blame] | 124 | : ExtensionsTest(options) {} |
danakj | aee6717 | 2017-06-13 16:37:02 | [diff] [blame] | 125 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 126 | void SetUp() override { |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 127 | ExtensionsTest::SetUp(); |
| 128 | ASSERT_TRUE(extensions_dir_.CreateUniqueTempDir()); |
[email protected] | c514acf | 2014-01-09 07:16:52 | [diff] [blame] | 129 | in_process_utility_thread_helper_.reset( |
| 130 | new content::InProcessUtilityThreadHelper); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 131 | // It will delete itself. |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 132 | client_ = new MockSandboxedUnpackerClient; |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 133 | |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 134 | InitSanboxedUnpacker(/*data_decode_service=*/nullptr, |
| 135 | /*unzip_service=*/nullptr); |
| 136 | } |
| 137 | |
| 138 | void InitSanboxedUnpacker( |
| 139 | std::unique_ptr<service_manager::Service> data_decode_service, |
| 140 | std::unique_ptr<service_manager::Service> unzip_service) { |
| 141 | service_manager::TestConnectorFactory::NameToServiceMap services; |
| 142 | if (!data_decode_service) |
| 143 | data_decode_service = data_decoder::DataDecoderService::Create(); |
| 144 | if (!unzip_service) |
| 145 | unzip_service = unzip::UnzipService::CreateService(); |
| 146 | services.insert( |
| 147 | std::make_pair("data_decoder", std::move(data_decode_service))); |
| 148 | services.insert(std::make_pair("unzip_service", std::move(unzip_service))); |
| 149 | test_connector_factory_ = |
| 150 | service_manager::TestConnectorFactory::CreateForServices( |
| 151 | std::move(services)); |
| 152 | connector_ = test_connector_factory_->CreateConnector(); |
| 153 | |
| 154 | sandboxed_unpacker_ = |
| 155 | new SandboxedUnpacker(connector_->Clone(), Manifest::INTERNAL, |
| 156 | Extension::NO_FLAGS, extensions_dir_.GetPath(), |
| 157 | base::ThreadTaskRunnerHandle::Get(), client_); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 158 | } |
| 159 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 160 | void TearDown() override { |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 161 | // Need to destruct SandboxedUnpacker before the message loop since |
[email protected] | 38f285a5 | 2010-11-05 21:02:28 | [diff] [blame] | 162 | // it posts a task to it. |
danakj | aee6717 | 2017-06-13 16:37:02 | [diff] [blame] | 163 | sandboxed_unpacker_ = nullptr; |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 164 | base::RunLoop().RunUntilIdle(); |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 165 | ExtensionsTest::TearDown(); |
danakj | aee6717 | 2017-06-13 16:37:02 | [diff] [blame] | 166 | in_process_utility_thread_helper_.reset(); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 167 | } |
| 168 | |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 169 | base::FilePath GetCrxFullPath(const std::string& crx_name) { |
| 170 | base::FilePath full_path; |
| 171 | EXPECT_TRUE(PathService::Get(extensions::DIR_TEST_DATA, &full_path)); |
| 172 | full_path = full_path.AppendASCII("unpacker").AppendASCII(crx_name); |
| 173 | EXPECT_TRUE(base::PathExists(full_path)) << full_path.value(); |
| 174 | return full_path; |
| 175 | } |
| 176 | |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 177 | void SetupUnpacker(const std::string& crx_name, |
| 178 | const std::string& package_hash) { |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 179 | base::FilePath crx_path = GetCrxFullPath(crx_name); |
pranay.kumar | 7b7ceb18 | 2015-05-05 12:12:46 | [diff] [blame] | 180 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 181 | FROM_HERE, |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 182 | base::Bind( |
| 183 | &SandboxedUnpacker::StartWithCrx, sandboxed_unpacker_, |
| 184 | extensions::CRXFileInfo(std::string(), crx_path, package_hash))); |
| 185 | client_->WaitForUnpack(); |
| 186 | } |
| 187 | |
| 188 | void SetupUnpackerWithDirectory(const std::string& crx_name) { |
| 189 | base::ScopedTempDir temp_dir; |
| 190 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 191 | base::FilePath crx_path = GetCrxFullPath(crx_name); |
vabr | 9142fe2 | 2016-09-08 13:19:22 | [diff] [blame] | 192 | ASSERT_TRUE(zip::Unzip(crx_path, temp_dir.GetPath())); |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 193 | |
| 194 | std::string fake_id = crx_file::id_util::GenerateId(crx_name); |
| 195 | std::string fake_public_key; |
| 196 | base::Base64Encode(std::string(2048, 'k'), &fake_public_key); |
| 197 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
tzik | 07cace4 | 2016-09-01 04:21:25 | [diff] [blame] | 198 | FROM_HERE, |
| 199 | base::Bind(&SandboxedUnpacker::StartWithDirectory, sandboxed_unpacker_, |
| 200 | fake_id, fake_public_key, temp_dir.Take())); |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 201 | client_->WaitForUnpack(); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 202 | } |
| 203 | |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 204 | bool InstallSucceeded() const { return !client_->temp_dir().empty(); } |
| 205 | |
| 206 | base::FilePath GetInstallPath() const { |
[email protected] | b22c8af6 | 2013-07-23 23:17:02 | [diff] [blame] | 207 | return client_->temp_dir().AppendASCII(kTempExtensionName); |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 208 | } |
| 209 | |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 210 | base::string16 GetInstallError() const { return client_->unpack_err(); } |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 211 | |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 212 | void ExpectInstallErrorContains(const std::string& error) { |
| 213 | std::string full_error = base::UTF16ToUTF8(client_->unpack_err()); |
| 214 | EXPECT_TRUE(full_error.find(error) != std::string::npos) |
| 215 | << "Error message " << full_error << " does not contain " << error; |
| 216 | } |
| 217 | |
| 218 | // Unpacks the package |package_name| and checks that |sandboxed_unpacker_| |
| 219 | // gets deleted. |
| 220 | void TestSandboxedUnpackerDeleted(const std::string& package_name, |
| 221 | bool expect_success) { |
| 222 | bool client_deleted = false; |
| 223 | client_->set_deleted_tracker(&client_deleted); |
| 224 | SetupUnpacker(package_name, ""); |
| 225 | EXPECT_EQ(GetInstallError().empty(), expect_success); |
| 226 | // Remove our reference to |sandboxed_unpacker_|, it should get deleted |
| 227 | // since/ it's the last reference. |
| 228 | sandboxed_unpacker_ = nullptr; |
| 229 | // The SandboxedUnpacker should have been deleted and deleted the client. |
| 230 | EXPECT_TRUE(client_deleted); |
| 231 | } |
| 232 | |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 233 | protected: |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 234 | base::ScopedTempDir extensions_dir_; |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 235 | MockSandboxedUnpackerClient* client_; |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 236 | scoped_refptr<SandboxedUnpacker> sandboxed_unpacker_; |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 237 | std::unique_ptr<content::InProcessUtilityThreadHelper> |
[email protected] | c514acf | 2014-01-09 07:16:52 | [diff] [blame] | 238 | in_process_utility_thread_helper_; |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 239 | std::unique_ptr<service_manager::TestConnectorFactory> |
| 240 | test_connector_factory_; |
| 241 | std::unique_ptr<service_manager::Connector> connector_; |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 242 | }; |
| 243 | |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 244 | TEST_F(SandboxedUnpackerTest, EmptyDefaultLocale) { |
| 245 | SetupUnpacker("empty_default_locale.crx", ""); |
| 246 | ExpectInstallErrorContains(manifest_errors::kInvalidDefaultLocale); |
| 247 | } |
| 248 | |
| 249 | TEST_F(SandboxedUnpackerTest, HasDefaultLocaleMissingLocalesFolder) { |
| 250 | SetupUnpacker("has_default_missing_locales.crx", ""); |
| 251 | ExpectInstallErrorContains(manifest_errors::kLocalesTreeMissing); |
| 252 | } |
| 253 | |
| 254 | TEST_F(SandboxedUnpackerTest, InvalidDefaultLocale) { |
| 255 | SetupUnpacker("invalid_default_locale.crx", ""); |
| 256 | ExpectInstallErrorContains(manifest_errors::kInvalidDefaultLocale); |
| 257 | } |
| 258 | |
| 259 | TEST_F(SandboxedUnpackerTest, MissingDefaultData) { |
| 260 | SetupUnpacker("missing_default_data.crx", ""); |
| 261 | ExpectInstallErrorContains(manifest_errors::kLocalesNoDefaultMessages); |
| 262 | } |
| 263 | |
| 264 | TEST_F(SandboxedUnpackerTest, MissingDefaultLocaleHasLocalesFolder) { |
| 265 | SetupUnpacker("missing_default_has_locales.crx", ""); |
| 266 | ExpectInstallErrorContains(l10n_util::GetStringUTF8( |
| 267 | IDS_EXTENSION_LOCALES_NO_DEFAULT_LOCALE_SPECIFIED)); |
| 268 | } |
| 269 | |
| 270 | TEST_F(SandboxedUnpackerTest, MissingMessagesFile) { |
| 271 | SetupUnpacker("missing_messages_file.crx", ""); |
| 272 | EXPECT_TRUE(base::MatchPattern( |
| 273 | GetInstallError(), |
| 274 | base::ASCIIToUTF16("*") + |
| 275 | base::ASCIIToUTF16(manifest_errors::kLocalesMessagesFileMissing) + |
| 276 | base::ASCIIToUTF16("*_locales?en_US?messages.json'."))) |
| 277 | << GetInstallError(); |
| 278 | } |
| 279 | |
| 280 | TEST_F(SandboxedUnpackerTest, NoLocaleData) { |
| 281 | SetupUnpacker("no_locale_data.crx", ""); |
| 282 | ExpectInstallErrorContains(manifest_errors::kLocalesNoDefaultMessages); |
| 283 | } |
| 284 | |
Jay Civelli | ea8f3df | 2018-01-24 05:17:32 | [diff] [blame] | 285 | TEST_F(SandboxedUnpackerTest, ImageDecodingError) { |
| 286 | const char kExpected[] = "Could not decode image: "; |
| 287 | SetupUnpacker("bad_image.crx", ""); |
| 288 | EXPECT_TRUE(base::StartsWith(GetInstallError(), base::ASCIIToUTF16(kExpected), |
| 289 | base::CompareCase::INSENSITIVE_ASCII)) |
| 290 | << "Expected prefix: \"" << kExpected << "\", actual error: \"" |
| 291 | << GetInstallError() << "\""; |
| 292 | } |
| 293 | |
| 294 | TEST_F(SandboxedUnpackerTest, BadPathError) { |
| 295 | IllegalImagePathInserter inserter( |
| 296 | static_cast<TestExtensionsClient*>(ExtensionsClient::Get())); |
| 297 | SetupUnpacker("good_package.crx", ""); |
| 298 | // Install should have failed with an error. |
| 299 | EXPECT_FALSE(InstallSucceeded()); |
| 300 | EXPECT_FALSE(GetInstallError().empty()); |
| 301 | } |
| 302 | |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 303 | TEST_F(SandboxedUnpackerTest, NoCatalogsSuccess) { |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 304 | SetupUnpacker("no_l10n.crx", ""); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 305 | // Check that there is no _locales folder. |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 306 | base::FilePath install_path = GetInstallPath().Append(kLocaleFolder); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 307 | EXPECT_FALSE(base::PathExists(install_path)); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 308 | } |
| 309 | |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 310 | TEST_F(SandboxedUnpackerTest, FromDirNoCatalogsSuccess) { |
| 311 | SetupUnpackerWithDirectory("no_l10n.crx"); |
| 312 | // Check that there is no _locales folder. |
| 313 | base::FilePath install_path = GetInstallPath().Append(kLocaleFolder); |
| 314 | EXPECT_FALSE(base::PathExists(install_path)); |
| 315 | } |
| 316 | |
[email protected] | 11170a77 | 2013-12-13 11:38:32 | [diff] [blame] | 317 | TEST_F(SandboxedUnpackerTest, WithCatalogsSuccess) { |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 318 | SetupUnpacker("good_l10n.crx", ""); |
[email protected] | 43c05d90 | 2013-07-10 21:27:00 | [diff] [blame] | 319 | // Check that there is _locales folder. |
asargent | 275faaa | 2015-01-27 23:43:29 | [diff] [blame] | 320 | base::FilePath install_path = GetInstallPath().Append(kLocaleFolder); |
[email protected] | 756748414 | 2013-07-11 17:36:07 | [diff] [blame] | 321 | EXPECT_TRUE(base::PathExists(install_path)); |
[email protected] | 9428edc | 2009-11-18 18:02:47 | [diff] [blame] | 322 | } |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 323 | |
asargent | c4fdad2 | 2015-08-28 22:44:39 | [diff] [blame] | 324 | TEST_F(SandboxedUnpackerTest, FromDirWithCatalogsSuccess) { |
| 325 | SetupUnpackerWithDirectory("good_l10n.crx"); |
| 326 | // Check that there is _locales folder. |
| 327 | base::FilePath install_path = GetInstallPath().Append(kLocaleFolder); |
| 328 | EXPECT_TRUE(base::PathExists(install_path)); |
| 329 | } |
| 330 | |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 331 | TEST_F(SandboxedUnpackerTest, FailHashCheck) { |
| 332 | base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 333 | extensions::switches::kEnableCrxHashCheck); |
waffles | 5918d5f | 2017-05-23 01:45:28 | [diff] [blame] | 334 | SetupUnpacker("good_l10n.crx", std::string(64, '0')); |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 335 | // Check that there is an error message. |
| 336 | EXPECT_NE(base::string16(), GetInstallError()); |
| 337 | } |
| 338 | |
Jay Civelli | 6d0e68e | 2018-01-24 16:42:53 | [diff] [blame] | 339 | TEST_F(SandboxedUnpackerTest, InvalidMessagesFile) { |
| 340 | SetupUnpackerWithDirectory("invalid_messages_file.crx"); |
| 341 | // Check that there is no _locales folder. |
| 342 | base::FilePath install_path = GetInstallPath().Append(kLocaleFolder); |
| 343 | EXPECT_FALSE(base::PathExists(install_path)); |
| 344 | EXPECT_TRUE(base::MatchPattern( |
| 345 | GetInstallError(), |
| 346 | base::ASCIIToUTF16("*_locales?en_US?messages.json': Line: 2, column: 10," |
| 347 | " Syntax error.'."))) |
| 348 | << GetInstallError(); |
| 349 | } |
| 350 | |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 351 | TEST_F(SandboxedUnpackerTest, PassHashCheck) { |
| 352 | base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 353 | extensions::switches::kEnableCrxHashCheck); |
| 354 | SetupUnpacker( |
| 355 | "good_l10n.crx", |
Devlin Cronin | 1de93897 | 2018-04-24 19:17:15 | [diff] [blame^] | 356 | "614AE3D608F4C2185E9173293AB3F93EE7C7C79C9A2C3CF71F633386A3296A6C"); |
ginkage | 553af320 | 2015-02-04 12:39:09 | [diff] [blame] | 357 | // Check that there is no error message. |
| 358 | EXPECT_EQ(base::string16(), GetInstallError()); |
| 359 | } |
| 360 | |
| 361 | TEST_F(SandboxedUnpackerTest, SkipHashCheck) { |
| 362 | SetupUnpacker("good_l10n.crx", "badhash"); |
| 363 | // Check that there is no error message. |
| 364 | EXPECT_EQ(base::string16(), GetInstallError()); |
| 365 | } |
| 366 | |
Jay Civelli | b6f2cc9c | 2018-03-10 01:13:57 | [diff] [blame] | 367 | // The following tests simulate the utility services failling. |
| 368 | TEST_F(SandboxedUnpackerTest, UnzipperServiceFails) { |
| 369 | InitSanboxedUnpacker( |
| 370 | /*data_decoder_service=*/nullptr, |
| 371 | std::make_unique<unzip::CrashyUnzipService>()); |
| 372 | SetupUnpacker("good_package.crx", ""); |
| 373 | EXPECT_FALSE(InstallSucceeded()); |
| 374 | EXPECT_FALSE(GetInstallError().empty()); |
| 375 | } |
| 376 | |
| 377 | TEST_F(SandboxedUnpackerTest, JsonParserFails) { |
| 378 | InitSanboxedUnpacker(std::make_unique<data_decoder::CrashyDataDecoderService>( |
| 379 | /*crash_json=*/true, /*crash_image=*/false), |
| 380 | /*unzip_service=*/nullptr); |
| 381 | SetupUnpacker("good_package.crx", ""); |
| 382 | EXPECT_FALSE(InstallSucceeded()); |
| 383 | EXPECT_FALSE(GetInstallError().empty()); |
| 384 | } |
| 385 | |
| 386 | TEST_F(SandboxedUnpackerTest, ImageDecoderFails) { |
| 387 | InitSanboxedUnpacker(std::make_unique<data_decoder::CrashyDataDecoderService>( |
| 388 | /*crash_json=*/false, /*crash_image=*/true), |
| 389 | /*unzip_service=*/nullptr); |
| 390 | SetupUnpacker("good_package.crx", ""); |
| 391 | EXPECT_FALSE(InstallSucceeded()); |
| 392 | EXPECT_FALSE(GetInstallError().empty()); |
| 393 | } |
| 394 | |
Jay Civelli | 26a8564 | 2018-01-26 21:29:39 | [diff] [blame] | 395 | // SandboxedUnpacker is ref counted and is reference by callbacks and |
| 396 | // InterfacePtrs. This tests that it gets deleted as expected (so that no extra |
| 397 | // refs are left). |
| 398 | TEST_F(SandboxedUnpackerTest, DeletedOnSuccess) { |
| 399 | TestSandboxedUnpackerDeleted("good_l10n.crx", /*expect_success=*/true); |
| 400 | } |
| 401 | |
| 402 | TEST_F(SandboxedUnpackerTest, DeletedOnFailure) { |
| 403 | TestSandboxedUnpackerDeleted("bad_image.crx", /*expect_success=*/false); |
| 404 | } |
| 405 | |
[email protected] | f5ac274 | 2012-07-02 17:50:58 | [diff] [blame] | 406 | } // namespace extensions |