[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [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 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 7 | #include <memory> |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 8 | #include <string> |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 9 | #include <utility> |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 10 | |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 11 | #include "base/command_line.h" |
thestig | c9e38a2 | 2014-09-13 01:02:11 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
fdoray | cb32419d | 2016-06-23 15:52:55 | [diff] [blame] | 13 | #include "base/run_loop.h" |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame^] | 14 | #include "base/stl_util.h" |
[email protected] | 774cebd | 2013-09-26 04:55:01 | [diff] [blame] | 15 | #include "base/strings/string_number_conversions.h" |
[email protected] | 00e7bef | 2013-06-10 20:35:17 | [diff] [blame] | 16 | #include "base/strings/string_util.h" |
Karandeep Bhatia | ab489a4d | 2018-11-28 21:56:06 | [diff] [blame] | 17 | #include "base/test/power_monitor_test_base.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 18 | #include "base/test/test_file_util.h" |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 19 | #include "base/test/values_test_util.h" |
[email protected] | 06492ed | 2013-03-24 22:13:14 | [diff] [blame] | 20 | #include "base/values.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 21 | #include "chrome/browser/extensions/chrome_content_verifier_delegate.h" |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 22 | #include "chrome/common/chrome_paths.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 23 | #include "chrome/common/chrome_switches.h" |
| 24 | #include "chrome/test/base/testing_profile.h" |
| 25 | #include "components/crx_file/id_util.h" |
Matt Falkenhagen | ae65b29 | 2018-04-25 16:03:27 | [diff] [blame] | 26 | #include "content/public/browser/render_process_host.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 27 | #include "content/public/browser/resource_request_info.h" |
megjablon | caf312f | 2017-01-12 18:47:49 | [diff] [blame] | 28 | #include "content/public/common/previews_state.h" |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 29 | #include "content/public/test/test_browser_thread_bundle.h" |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 30 | #include "content/public/test/test_renderer_host.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 31 | #include "content/public/test/test_utils.h" |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 32 | #include "content/public/test/web_contents_tester.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 33 | #include "extensions/browser/content_verifier.h" |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 34 | #include "extensions/browser/content_verifier/test_utils.h" |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 35 | #include "extensions/browser/extension_prefs.h" |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 36 | #include "extensions/browser/extension_protocols.h" |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 37 | #include "extensions/browser/extension_registry.h" |
John Abd-El-Malek | ea00630 | 2018-05-10 05:50:46 | [diff] [blame] | 38 | #include "extensions/browser/extension_system.h" |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 39 | #include "extensions/browser/info_map.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 40 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 41 | #include "extensions/common/extension.h" |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 42 | #include "extensions/common/extension_builder.h" |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 43 | #include "extensions/common/extension_paths.h" |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 44 | #include "extensions/common/file_util.h" |
Devlin Cronin | 1fa235b6 | 2018-04-12 14:04:07 | [diff] [blame] | 45 | #include "extensions/common/value_builder.h" |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 46 | #include "extensions/test/test_extension_dir.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 47 | #include "net/base/request_priority.h" |
rhalavati | 04b9338 | 2017-04-07 19:00:54 | [diff] [blame] | 48 | #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 49 | #include "net/url_request/url_request.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 50 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 51 | #include "net/url_request/url_request_status.h" |
| 52 | #include "net/url_request/url_request_test_util.h" |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 53 | #include "services/network/test/test_url_loader_client.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 54 | #include "testing/gtest/include/gtest/gtest.h" |
| 55 | |
[email protected] | 7491ad0 | 2014-07-05 19:10:07 | [diff] [blame] | 56 | using content::ResourceType; |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 57 | using extensions::ExtensionRegistry; |
| 58 | using network::mojom::URLLoader; |
[email protected] | 7491ad0 | 2014-07-05 19:10:07 | [diff] [blame] | 59 | |
[email protected] | 702d8b4 | 2013-02-27 20:55:50 | [diff] [blame] | 60 | namespace extensions { |
jamescook | 8816ae5 | 2014-09-05 17:02:37 | [diff] [blame] | 61 | namespace { |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 62 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 63 | enum class RequestHandlerType { |
| 64 | kURLLoader, |
| 65 | kURLRequest, |
| 66 | }; |
| 67 | |
| 68 | const RequestHandlerType kTestModes[] = {RequestHandlerType::kURLLoader, |
| 69 | RequestHandlerType::kURLRequest}; |
| 70 | |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 71 | base::FilePath GetTestPath(const std::string& name) { |
| 72 | base::FilePath path; |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 73 | EXPECT_TRUE(base::PathService::Get(chrome::DIR_TEST_DATA, &path)); |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 74 | return path.AppendASCII("extensions").AppendASCII(name); |
| 75 | } |
| 76 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 77 | base::FilePath GetContentVerifierTestPath() { |
| 78 | base::FilePath path; |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 79 | EXPECT_TRUE(base::PathService::Get(extensions::DIR_TEST_DATA, &path)); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 80 | return path.AppendASCII("content_hash_fetcher") |
| 81 | .AppendASCII("different_sized_files"); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 84 | scoped_refptr<Extension> CreateTestExtension(const std::string& name, |
| 85 | bool incognito_split_mode) { |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 86 | base::DictionaryValue manifest; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 87 | manifest.SetString("name", name); |
| 88 | manifest.SetString("version", "1"); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 89 | manifest.SetInteger("manifest_version", 2); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 90 | manifest.SetString("incognito", incognito_split_mode ? "split" : "spanning"); |
| 91 | |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 92 | base::FilePath path = GetTestPath("response_headers"); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 93 | |
| 94 | std::string error; |
| 95 | scoped_refptr<Extension> extension( |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 96 | Extension::Create(path, Manifest::INTERNAL, manifest, |
[email protected] | ed3b9b1 | 2012-05-31 18:37:51 | [diff] [blame] | 97 | Extension::NO_FLAGS, &error)); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 98 | EXPECT_TRUE(extension.get()) << error; |
| 99 | return extension; |
| 100 | } |
| 101 | |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 102 | scoped_refptr<Extension> CreateWebStoreExtension() { |
Devlin Cronin | f43c331 | 2018-03-27 19:54:17 | [diff] [blame] | 103 | std::unique_ptr<base::DictionaryValue> manifest = |
| 104 | DictionaryBuilder() |
| 105 | .Set("name", "WebStore") |
| 106 | .Set("version", "1") |
| 107 | .Set("manifest_version", 2) |
| 108 | .Set("icons", |
| 109 | DictionaryBuilder().Set("16", "webstore_icon_16.png").Build()) |
| 110 | .Set("web_accessible_resources", |
| 111 | ListBuilder().Append("webstore_icon_16.png").Build()) |
| 112 | .Build(); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 113 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 114 | base::FilePath path; |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 115 | EXPECT_TRUE(base::PathService::Get(chrome::DIR_RESOURCES, &path)); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 116 | path = path.AppendASCII("web_store"); |
| 117 | |
| 118 | std::string error; |
Devlin Cronin | f43c331 | 2018-03-27 19:54:17 | [diff] [blame] | 119 | scoped_refptr<Extension> extension(Extension::Create( |
| 120 | path, Manifest::COMPONENT, *manifest, Extension::NO_FLAGS, &error)); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 121 | EXPECT_TRUE(extension.get()) << error; |
| 122 | return extension; |
| 123 | } |
| 124 | |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 125 | scoped_refptr<const Extension> CreateTestResponseHeaderExtension() { |
Devlin Cronin | 1fa235b6 | 2018-04-12 14:04:07 | [diff] [blame] | 126 | return ExtensionBuilder("An extension with web-accessible resources") |
Devlin Cronin | 98cd658 | 2018-05-08 19:18:12 | [diff] [blame] | 127 | .SetManifestKey("web_accessible_resources", |
| 128 | ListBuilder().Append("test.dat").Build()) |
Devlin Cronin | 1fa235b6 | 2018-04-12 14:04:07 | [diff] [blame] | 129 | .SetPath(GetTestPath("response_headers")) |
| 130 | .Build(); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 131 | } |
| 132 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 133 | // Helper function to create a |ResourceRequest| for testing purposes. |
| 134 | network::ResourceRequest CreateResourceRequest(const std::string& method, |
| 135 | ResourceType resource_type, |
| 136 | const GURL& url) { |
| 137 | network::ResourceRequest request; |
| 138 | request.method = method; |
| 139 | request.url = url; |
| 140 | request.site_for_cookies = url; // bypass third-party cookie blocking. |
| 141 | request.request_initiator = |
| 142 | url::Origin::Create(url); // ensure initiator set. |
| 143 | request.referrer_policy = content::Referrer::GetDefaultReferrerPolicy(); |
| 144 | request.resource_type = resource_type; |
| 145 | request.is_main_frame = resource_type == content::RESOURCE_TYPE_MAIN_FRAME; |
| 146 | request.allow_download = true; |
| 147 | return request; |
| 148 | } |
| 149 | |
| 150 | // The result of either a URLRequest of a URLLoader response (but not both) |
| 151 | // depending on the on test type. |
| 152 | class GetResult { |
| 153 | public: |
| 154 | GetResult(std::unique_ptr<net::URLRequest> request, int result) |
| 155 | : request_(std::move(request)), result_(result) {} |
| 156 | GetResult(const network::ResourceResponseHead& response, int result) |
| 157 | : resource_response_(response), result_(result) {} |
| 158 | GetResult(GetResult&& other) |
| 159 | : request_(std::move(other.request_)), result_(other.result_) {} |
| 160 | ~GetResult() = default; |
| 161 | |
| 162 | std::string GetResponseHeaderByName(const std::string& name) const { |
| 163 | std::string value; |
| 164 | if (request_) |
| 165 | request_->GetResponseHeaderByName(name, &value); |
| 166 | else if (resource_response_.headers) |
| 167 | resource_response_.headers->GetNormalizedHeader(name, &value); |
| 168 | return value; |
| 169 | } |
| 170 | |
| 171 | int result() const { return result_; } |
| 172 | |
| 173 | private: |
| 174 | std::unique_ptr<net::URLRequest> request_; |
| 175 | const network::ResourceResponseHead resource_response_; |
| 176 | int result_; |
| 177 | |
| 178 | DISALLOW_COPY_AND_ASSIGN(GetResult); |
| 179 | }; |
| 180 | |
jamescook | 8816ae5 | 2014-09-05 17:02:37 | [diff] [blame] | 181 | } // namespace |
| 182 | |
| 183 | // This test lives in src/chrome instead of src/extensions because it tests |
| 184 | // functionality delegated back to Chrome via ChromeExtensionsBrowserClient. |
lfg | 048201a | 2014-09-16 19:09:36 | [diff] [blame] | 185 | // See chrome/browser/extensions/chrome_url_request_util.cc. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 186 | class ExtensionProtocolsTest |
| 187 | : public testing::Test, |
| 188 | public testing::WithParamInterface<RequestHandlerType> { |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 189 | public: |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 190 | ExtensionProtocolsTest() |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 191 | : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 192 | rvh_test_enabler_(new content::RenderViewHostTestEnabler()), |
John Abd-El-Malek | 6fee690 | 2018-11-28 18:30:57 | [diff] [blame] | 193 | old_factory_(NULL) {} |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 194 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 195 | void SetUp() override { |
[email protected] | 06492ed | 2013-03-24 22:13:14 | [diff] [blame] | 196 | testing::Test::SetUp(); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 197 | testing_profile_ = TestingProfile::Builder().Build(); |
Erik Chen | 5bab499 | 2018-05-05 15:19:53 | [diff] [blame] | 198 | contents_ = CreateTestWebContents(); |
John Abd-El-Malek | 6fee690 | 2018-11-28 18:30:57 | [diff] [blame] | 199 | old_factory_ = test_url_request_context_.job_factory(); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 200 | |
| 201 | // Set up content verification. |
| 202 | base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 203 | command_line->AppendSwitchASCII( |
| 204 | switches::kExtensionContentVerification, |
| 205 | switches::kExtensionContentVerificationEnforce); |
| 206 | content_verifier_ = new ContentVerifier( |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 207 | browser_context(), |
| 208 | std::make_unique<ChromeContentVerifierDelegate>(browser_context())); |
John Abd-El-Malek | ea00630 | 2018-05-10 05:50:46 | [diff] [blame] | 209 | info_map()->SetContentVerifier(content_verifier_.get()); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 210 | } |
| 211 | |
dcheng | 7219181 | 2014-10-28 20:49:56 | [diff] [blame] | 212 | void TearDown() override { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 213 | loader_factory_.reset(); |
John Abd-El-Malek | 6fee690 | 2018-11-28 18:30:57 | [diff] [blame] | 214 | test_url_request_context_.set_job_factory(old_factory_); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 215 | content_verifier_->Shutdown(); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 216 | } |
| 217 | |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 218 | void SetProtocolHandler(bool is_incognito) { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 219 | switch (request_handler()) { |
| 220 | case RequestHandlerType::kURLLoader: |
| 221 | loader_factory_ = extensions::CreateExtensionNavigationURLLoaderFactory( |
John Abd-El-Malek | ea00630 | 2018-05-10 05:50:46 | [diff] [blame] | 222 | browser_context(), false); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 223 | break; |
| 224 | case RequestHandlerType::kURLRequest: |
| 225 | job_factory_.SetProtocolHandler( |
John Abd-El-Malek | ea00630 | 2018-05-10 05:50:46 | [diff] [blame] | 226 | kExtensionScheme, |
| 227 | CreateExtensionProtocolHandler(is_incognito, info_map())); |
John Abd-El-Malek | 6fee690 | 2018-11-28 18:30:57 | [diff] [blame] | 228 | test_url_request_context_.set_job_factory(&job_factory_); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 229 | break; |
| 230 | } |
| 231 | testing_profile_->ForceIncognito(is_incognito); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 232 | } |
| 233 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 234 | GetResult RequestOrLoad(const GURL& url, ResourceType resource_type) { |
| 235 | switch (request_handler()) { |
| 236 | case RequestHandlerType::kURLLoader: |
| 237 | return LoadURL(url, resource_type); |
| 238 | case RequestHandlerType::kURLRequest: |
| 239 | return RequestURL(url, resource_type); |
| 240 | } |
| 241 | NOTREACHED(); |
| 242 | return GetResult(nullptr, net::ERR_FAILED); |
| 243 | } |
| 244 | |
| 245 | void AddExtension(const scoped_refptr<const Extension>& extension, |
| 246 | bool incognito_enabled, |
| 247 | bool notifications_disabled) { |
John Abd-El-Malek | ea00630 | 2018-05-10 05:50:46 | [diff] [blame] | 248 | info_map()->AddExtension(extension.get(), base::Time::Now(), |
| 249 | incognito_enabled, notifications_disabled); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 250 | if (request_handler() == RequestHandlerType::kURLLoader) { |
| 251 | EXPECT_TRUE(extension_registry()->AddEnabled(extension)); |
| 252 | ExtensionPrefs::Get(browser_context()) |
| 253 | ->SetIsIncognitoEnabled(extension->id(), incognito_enabled); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | void RemoveExtension(const scoped_refptr<const Extension>& extension, |
| 258 | const UnloadedExtensionReason reason) { |
John Abd-El-Malek | ea00630 | 2018-05-10 05:50:46 | [diff] [blame] | 259 | info_map()->RemoveExtension(extension->id(), reason); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 260 | if (request_handler() == RequestHandlerType::kURLLoader) { |
| 261 | EXPECT_TRUE(extension_registry()->RemoveEnabled(extension->id())); |
| 262 | if (reason == UnloadedExtensionReason::DISABLE) |
| 263 | EXPECT_TRUE(extension_registry()->AddDisabled(extension)); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | // Helper method to create a URL request/loader, call RequestOrLoad on it, and |
| 268 | // return the result. If |extension| hasn't already been added to |
John Abd-El-Malek | ea00630 | 2018-05-10 05:50:46 | [diff] [blame] | 269 | // info_map(), this will add it. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 270 | GetResult DoRequestOrLoad(const scoped_refptr<Extension> extension, |
| 271 | const std::string& relative_path) { |
John Abd-El-Malek | ea00630 | 2018-05-10 05:50:46 | [diff] [blame] | 272 | if (!info_map()->extensions().Contains(extension->id())) { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 273 | AddExtension(extension.get(), |
| 274 | /*incognito_enabled=*/false, |
| 275 | /*notifications_disabled=*/false); |
| 276 | } |
| 277 | return RequestOrLoad(extension->GetResourceURL(relative_path), |
| 278 | content::RESOURCE_TYPE_MAIN_FRAME); |
| 279 | } |
| 280 | |
| 281 | ExtensionRegistry* extension_registry() { |
| 282 | return ExtensionRegistry::Get(browser_context()); |
| 283 | } |
| 284 | |
John Abd-El-Malek | ea00630 | 2018-05-10 05:50:46 | [diff] [blame] | 285 | InfoMap* info_map() { |
| 286 | return ExtensionSystem::Get(browser_context())->info_map(); |
| 287 | } |
| 288 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 289 | content::BrowserContext* browser_context() { return testing_profile_.get(); } |
| 290 | |
Karandeep Bhatia | ab489a4d | 2018-11-28 21:56:06 | [diff] [blame] | 291 | void SimulateSystemSuspendForRequests() { |
| 292 | power_monitor_source_ = new base::PowerMonitorTestSource(); |
| 293 | power_monitor_ = std::make_unique<base::PowerMonitor>( |
| 294 | std::unique_ptr<base::PowerMonitorSource>(power_monitor_source_)); |
| 295 | } |
| 296 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 297 | protected: |
| 298 | scoped_refptr<ContentVerifier> content_verifier_; |
| 299 | |
| 300 | private: |
| 301 | GetResult LoadURL(const GURL& url, ResourceType resource_type) { |
| 302 | constexpr int32_t kRoutingId = 81; |
| 303 | constexpr int32_t kRequestId = 28; |
| 304 | |
| 305 | network::mojom::URLLoaderPtr loader; |
| 306 | network::TestURLLoaderClient client; |
| 307 | loader_factory_->CreateLoaderAndStart( |
| 308 | mojo::MakeRequest(&loader), kRoutingId, kRequestId, |
| 309 | network::mojom::kURLLoadOptionNone, |
| 310 | CreateResourceRequest("GET", resource_type, url), |
| 311 | client.CreateInterfacePtr(), |
| 312 | net::MutableNetworkTrafficAnnotationTag(TRAFFIC_ANNOTATION_FOR_TESTS)); |
| 313 | |
Karandeep Bhatia | ab489a4d | 2018-11-28 21:56:06 | [diff] [blame] | 314 | if (power_monitor_source_) { |
| 315 | power_monitor_source_->GenerateSuspendEvent(); |
| 316 | power_monitor_source_->GenerateResumeEvent(); |
| 317 | } |
| 318 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 319 | client.RunUntilComplete(); |
| 320 | return GetResult(client.response_head(), |
| 321 | client.completion_status().error_code); |
| 322 | } |
| 323 | |
| 324 | GetResult RequestURL(const GURL& url, ResourceType resource_type) { |
John Abd-El-Malek | 6fee690 | 2018-11-28 18:30:57 | [diff] [blame] | 325 | auto request = test_url_request_context_.CreateRequest( |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 326 | url, net::DEFAULT_PRIORITY, &test_delegate_, |
| 327 | TRAFFIC_ANNOTATION_FOR_TESTS); |
| 328 | |
gab | f9d1558 | 2014-11-13 16:40:15 | [diff] [blame] | 329 | content::ResourceRequestInfo::AllocateForTesting( |
John Abd-El-Malek | 6fee690 | 2018-11-28 18:30:57 | [diff] [blame] | 330 | request.get(), resource_type, |
| 331 | /* resource_context */nullptr, |
megjablon | caf312f | 2017-01-12 18:47:49 | [diff] [blame] | 332 | /*render_process_id=*/-1, |
| 333 | /*render_view_id=*/-1, |
| 334 | /*render_frame_id=*/-1, |
| 335 | /*is_main_frame=*/resource_type == content::RESOURCE_TYPE_MAIN_FRAME, |
megjablon | caf312f | 2017-01-12 18:47:49 | [diff] [blame] | 336 | /*allow_download=*/true, |
Jian Li | 1817342 | 2017-11-08 03:00:02 | [diff] [blame] | 337 | /*is_async=*/false, content::PREVIEWS_OFF, |
| 338 | /*navigation_ui_data*/ nullptr); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 339 | request->Start(); |
Karandeep Bhatia | ab489a4d | 2018-11-28 21:56:06 | [diff] [blame] | 340 | |
| 341 | if (power_monitor_source_) { |
| 342 | power_monitor_source_->GenerateSuspendEvent(); |
| 343 | power_monitor_source_->GenerateResumeEvent(); |
| 344 | |
| 345 | // PowerMonitorTestSource calls RunLoop().RunUntilIdle() which causes the |
| 346 | // request to be completed. |
| 347 | EXPECT_TRUE(test_delegate_.response_completed()); |
| 348 | } else { |
| 349 | base::RunLoop().Run(); |
| 350 | } |
| 351 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 352 | return GetResult(std::move(request), test_delegate_.request_status()); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 353 | } |
| 354 | |
Erik Chen | 5bab499 | 2018-05-05 15:19:53 | [diff] [blame] | 355 | std::unique_ptr<content::WebContents> CreateTestWebContents() { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 356 | auto site_instance = content::SiteInstance::Create(browser_context()); |
| 357 | return content::WebContentsTester::CreateTestWebContents( |
| 358 | browser_context(), std::move(site_instance)); |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 359 | } |
| 360 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 361 | content::WebContents* web_contents() { return contents_.get(); } |
| 362 | |
| 363 | content::RenderFrameHost* main_rfh() { |
| 364 | return web_contents()->GetMainFrame(); |
| 365 | } |
| 366 | |
| 367 | RequestHandlerType request_handler() const { return GetParam(); } |
| 368 | |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 369 | content::TestBrowserThreadBundle thread_bundle_; |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 370 | std::unique_ptr<content::RenderViewHostTestEnabler> rvh_test_enabler_; |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 371 | net::URLRequestJobFactoryImpl job_factory_; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 372 | const net::URLRequestJobFactory* old_factory_; |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 373 | std::unique_ptr<network::mojom::URLLoaderFactory> loader_factory_; |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 374 | net::TestURLRequestContext test_url_request_context_; |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 375 | std::unique_ptr<TestingProfile> testing_profile_; |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 376 | net::TestDelegate test_delegate_; |
| 377 | std::unique_ptr<content::WebContents> contents_; |
Karandeep Bhatia | ab489a4d | 2018-11-28 21:56:06 | [diff] [blame] | 378 | |
| 379 | std::unique_ptr<base::PowerMonitor> power_monitor_; |
| 380 | |
| 381 | // |power_monitor_source_| is owned by |power_monitor_| |
| 382 | base::PowerMonitorTestSource* power_monitor_source_ = nullptr; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | // Tests that making a chrome-extension request in an incognito context is |
| 386 | // only allowed under the right circumstances (if the extension is allowed |
| 387 | // in incognito, and it's either a non-main-frame request or a split-mode |
| 388 | // extension). |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 389 | TEST_P(ExtensionProtocolsTest, IncognitoRequest) { |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 390 | // Register an incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 391 | SetProtocolHandler(true); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 392 | |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 393 | struct TestCase { |
| 394 | // Inputs. |
| 395 | std::string name; |
| 396 | bool incognito_split_mode; |
| 397 | bool incognito_enabled; |
| 398 | |
| 399 | // Expected results. |
| 400 | bool should_allow_main_frame_load; |
| 401 | bool should_allow_sub_frame_load; |
| 402 | } cases[] = { |
| 403 | {"spanning disabled", false, false, false, false}, |
| 404 | {"split disabled", true, false, false, false}, |
nasko | 5cf9d45 | 2016-06-01 05:34:56 | [diff] [blame] | 405 | {"spanning enabled", false, true, false, false}, |
| 406 | {"split enabled", true, true, true, false}, |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 407 | }; |
| 408 | |
Avi Drissman | 5f0fb8c | 2018-12-25 23:20:49 | [diff] [blame^] | 409 | for (size_t i = 0; i < base::size(cases); ++i) { |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 410 | scoped_refptr<Extension> extension = |
| 411 | CreateTestExtension(cases[i].name, cases[i].incognito_split_mode); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 412 | AddExtension(extension, cases[i].incognito_enabled, false); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 413 | |
| 414 | // First test a main frame request. |
| 415 | { |
| 416 | // It doesn't matter that the resource doesn't exist. If the resource |
nasko | b9164c4 | 2016-06-07 01:21:35 | [diff] [blame] | 417 | // is blocked, we should see BLOCKED_BY_CLIENT. Otherwise, the request |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 418 | // should just fail because the file doesn't exist. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 419 | auto get_result = RequestOrLoad(extension->GetResourceURL("404.html"), |
| 420 | content::RESOURCE_TYPE_MAIN_FRAME); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 421 | |
| 422 | if (cases[i].should_allow_main_frame_load) { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 423 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, get_result.result()) |
maksim.sisov | 1b83bb7 | 2016-10-07 06:07:23 | [diff] [blame] | 424 | << cases[i].name; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 425 | } else { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 426 | EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, get_result.result()) |
nasko | b9164c4 | 2016-06-07 01:21:35 | [diff] [blame] | 427 | << cases[i].name; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
John Abd-El-Malek | 2305cdc | 2018-02-14 20:26:28 | [diff] [blame] | 431 | // Subframe navigation requests are blocked in ExtensionNavigationThrottle |
| 432 | // which isn't added in this unit test. This is tested in an integration |
| 433 | // test in ExtensionResourceRequestPolicyTest.IframeNavigateToInaccessible. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 434 | RemoveExtension(extension, UnloadedExtensionReason::UNINSTALL); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 438 | void CheckForContentLengthHeader(const GetResult& get_result) { |
| 439 | std::string content_length = get_result.GetResponseHeaderByName( |
| 440 | net::HttpRequestHeaders::kContentLength); |
| 441 | |
[email protected] | 774cebd | 2013-09-26 04:55:01 | [diff] [blame] | 442 | EXPECT_FALSE(content_length.empty()); |
| 443 | int length_value = 0; |
| 444 | EXPECT_TRUE(base::StringToInt(content_length, &length_value)); |
| 445 | EXPECT_GT(length_value, 0); |
| 446 | } |
| 447 | |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 448 | // Tests getting a resource for a component extension works correctly, both when |
| 449 | // the extension is enabled and when it is disabled. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 450 | TEST_P(ExtensionProtocolsTest, ComponentResourceRequest) { |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 451 | // Register a non-incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 452 | SetProtocolHandler(false); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 453 | |
| 454 | scoped_refptr<Extension> extension = CreateWebStoreExtension(); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 455 | AddExtension(extension, false, false); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 456 | |
| 457 | // First test it with the extension enabled. |
| 458 | { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 459 | auto get_result = |
| 460 | RequestOrLoad(extension->GetResourceURL("webstore_icon_16.png"), |
| 461 | content::RESOURCE_TYPE_MEDIA); |
| 462 | EXPECT_EQ(net::OK, get_result.result()); |
| 463 | CheckForContentLengthHeader(get_result); |
| 464 | EXPECT_EQ("image/png", get_result.GetResponseHeaderByName( |
| 465 | net::HttpRequestHeaders::kContentType)); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | // And then test it with the extension disabled. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 469 | RemoveExtension(extension, UnloadedExtensionReason::DISABLE); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 470 | { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 471 | auto get_result = |
| 472 | RequestOrLoad(extension->GetResourceURL("webstore_icon_16.png"), |
| 473 | content::RESOURCE_TYPE_MEDIA); |
| 474 | EXPECT_EQ(net::OK, get_result.result()); |
| 475 | CheckForContentLengthHeader(get_result); |
| 476 | EXPECT_EQ("image/png", get_result.GetResponseHeaderByName( |
| 477 | net::HttpRequestHeaders::kContentType)); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 481 | // Tests that a URL request for resource from an extension returns a few |
| 482 | // expected response headers. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 483 | TEST_P(ExtensionProtocolsTest, ResourceRequestResponseHeaders) { |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 484 | // Register a non-incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 485 | SetProtocolHandler(false); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 486 | |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 487 | scoped_refptr<const Extension> extension = |
| 488 | CreateTestResponseHeaderExtension(); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 489 | AddExtension(extension, false, false); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 490 | |
| 491 | { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 492 | auto get_result = RequestOrLoad(extension->GetResourceURL("test.dat"), |
| 493 | content::RESOURCE_TYPE_MEDIA); |
| 494 | EXPECT_EQ(net::OK, get_result.result()); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 495 | |
| 496 | // Check that cache-related headers are set. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 497 | std::string etag = get_result.GetResponseHeaderByName("ETag"); |
brettw | 66d1b81b | 2015-07-06 19:29:40 | [diff] [blame] | 498 | EXPECT_TRUE(base::StartsWith(etag, "\"", base::CompareCase::SENSITIVE)); |
| 499 | EXPECT_TRUE(base::EndsWith(etag, "\"", base::CompareCase::SENSITIVE)); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 500 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 501 | std::string revalidation_header = |
| 502 | get_result.GetResponseHeaderByName("cache-control"); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 503 | EXPECT_EQ("no-cache", revalidation_header); |
| 504 | |
| 505 | // We set test.dat as web-accessible, so it should have a CORS header. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 506 | std::string access_control = |
| 507 | get_result.GetResponseHeaderByName("Access-Control-Allow-Origin"); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 508 | EXPECT_EQ("*", access_control); |
| 509 | } |
| 510 | } |
| 511 | |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 512 | // Tests that a URL request for main frame or subframe from an extension |
| 513 | // succeeds, but subresources fail. See http://crbug.com/312269. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 514 | TEST_P(ExtensionProtocolsTest, AllowFrameRequests) { |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 515 | // Register a non-incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 516 | SetProtocolHandler(false); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 517 | |
| 518 | scoped_refptr<Extension> extension = CreateTestExtension("foo", false); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 519 | AddExtension(extension, false, false); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 520 | |
nasko | 5cf9d45 | 2016-06-01 05:34:56 | [diff] [blame] | 521 | // All MAIN_FRAME requests should succeed. SUB_FRAME requests that are not |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 522 | // explicitly listed in web_accessible_resources or same-origin to the parent |
nasko | 5cf9d45 | 2016-06-01 05:34:56 | [diff] [blame] | 523 | // should not succeed. |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 524 | { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 525 | auto get_result = RequestOrLoad(extension->GetResourceURL("test.dat"), |
| 526 | content::RESOURCE_TYPE_MAIN_FRAME); |
| 527 | EXPECT_EQ(net::OK, get_result.result()); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 528 | } |
John Abd-El-Malek | 2305cdc | 2018-02-14 20:26:28 | [diff] [blame] | 529 | |
| 530 | // Subframe navigation requests are blocked in ExtensionNavigationThrottle |
| 531 | // which isn't added in this unit test. This is tested in an integration test |
| 532 | // in ExtensionResourceRequestPolicyTest.IframeNavigateToInaccessible. |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 533 | |
| 534 | // And subresource types, such as media, should fail. |
| 535 | { |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 536 | auto get_result = RequestOrLoad(extension->GetResourceURL("test.dat"), |
| 537 | content::RESOURCE_TYPE_MEDIA); |
| 538 | EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, get_result.result()); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 542 | TEST_P(ExtensionProtocolsTest, MetadataFolder) { |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 543 | SetProtocolHandler(false); |
| 544 | |
| 545 | base::FilePath extension_dir = GetTestPath("metadata_folder"); |
| 546 | std::string error; |
| 547 | scoped_refptr<Extension> extension = |
| 548 | file_util::LoadExtension(extension_dir, Manifest::INTERNAL, |
| 549 | Extension::NO_FLAGS, &error); |
| 550 | ASSERT_NE(extension.get(), nullptr) << "error: " << error; |
| 551 | |
| 552 | // Loading "/test.html" should succeed. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 553 | EXPECT_EQ(net::OK, DoRequestOrLoad(extension, "test.html").result()); |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 554 | |
| 555 | // Loading "/_metadata/verified_contents.json" should fail. |
| 556 | base::FilePath relative_path = |
| 557 | base::FilePath(kMetadataFolder).Append(kVerifiedContentsFilename); |
| 558 | EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 559 | EXPECT_NE(net::OK, |
| 560 | DoRequestOrLoad(extension, relative_path.AsUTF8Unsafe()).result()); |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 561 | |
| 562 | // Loading "/_metadata/a.txt" should also fail. |
| 563 | relative_path = base::FilePath(kMetadataFolder).AppendASCII("a.txt"); |
| 564 | EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 565 | EXPECT_NE(net::OK, |
| 566 | DoRequestOrLoad(extension, relative_path.AsUTF8Unsafe()).result()); |
asargent | a093ec3 | 2016-02-13 01:36:43 | [diff] [blame] | 567 | } |
| 568 | |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 569 | // Tests that unreadable files and deleted files correctly go through |
| 570 | // ContentVerifyJob. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 571 | TEST_P(ExtensionProtocolsTest, VerificationSeenForFileAccessErrors) { |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 572 | SetProtocolHandler(false); |
| 573 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 574 | // Unzip extension containing verification hashes to a temporary directory. |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 575 | base::ScopedTempDir temp_dir; |
| 576 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 577 | base::FilePath unzipped_path = temp_dir.GetPath(); |
| 578 | scoped_refptr<Extension> extension = |
| 579 | content_verifier_test_utils::UnzipToDirAndLoadExtension( |
| 580 | GetContentVerifierTestPath().AppendASCII("source.zip"), |
| 581 | unzipped_path); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 582 | ASSERT_TRUE(extension.get()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 583 | ExtensionId extension_id = extension->id(); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 584 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 585 | const std::string kJs("1024.js"); |
| 586 | base::FilePath kRelativePath(FILE_PATH_LITERAL("1024.js")); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 587 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 588 | // Valid and readable 1024.js. |
| 589 | { |
Istiaque Ahmed | 584fe14 | 2018-03-13 09:19:04 | [diff] [blame] | 590 | TestContentVerifySingleJobObserver observer(extension_id, kRelativePath); |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 591 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 592 | content_verifier_->OnExtensionLoaded(browser_context(), extension.get()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 593 | // Wait for PostTask to ContentVerifierIOData::AddData() to finish. |
| 594 | content::RunAllPendingInMessageLoop(); |
| 595 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 596 | EXPECT_EQ(net::OK, DoRequestOrLoad(extension, kJs).result()); |
Istiaque Ahmed | 1dec1d4 | 2018-02-02 00:01:36 | [diff] [blame] | 597 | EXPECT_EQ(ContentVerifyJob::NONE, observer.WaitForJobFinished()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | // chmod -r 1024.js. |
| 601 | { |
Istiaque Ahmed | 584fe14 | 2018-03-13 09:19:04 | [diff] [blame] | 602 | TestContentVerifySingleJobObserver observer(extension->id(), kRelativePath); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 603 | base::FilePath file_path = unzipped_path.AppendASCII(kJs); |
| 604 | ASSERT_TRUE(base::MakeFileUnreadable(file_path)); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 605 | EXPECT_EQ(net::ERR_ACCESS_DENIED, DoRequestOrLoad(extension, kJs).result()); |
Istiaque Ahmed | 1dec1d4 | 2018-02-02 00:01:36 | [diff] [blame] | 606 | EXPECT_EQ(ContentVerifyJob::HASH_MISMATCH, observer.WaitForJobFinished()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 607 | // NOTE: In production, hash mismatch would have disabled |extension|, but |
| 608 | // since UnzipToDirAndLoadExtension() doesn't add the extension to |
| 609 | // ExtensionRegistry, ChromeContentVerifierDelegate won't disable it. |
| 610 | // TODO(lazyboy): We may want to update this to more closely reflect the |
| 611 | // real flow. |
| 612 | } |
| 613 | |
| 614 | // Delete 1024.js. |
| 615 | { |
Istiaque Ahmed | 584fe14 | 2018-03-13 09:19:04 | [diff] [blame] | 616 | TestContentVerifySingleJobObserver observer(extension_id, kRelativePath); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 617 | base::FilePath file_path = unzipped_path.AppendASCII(kJs); |
| 618 | ASSERT_TRUE(base::DieFileDie(file_path, false)); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 619 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, |
| 620 | DoRequestOrLoad(extension, kJs).result()); |
Istiaque Ahmed | 1dec1d4 | 2018-02-02 00:01:36 | [diff] [blame] | 621 | EXPECT_EQ(ContentVerifyJob::HASH_MISMATCH, observer.WaitForJobFinished()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 622 | } |
lazyboy | d6dbb26 | 2017-03-30 00:57:30 | [diff] [blame] | 623 | } |
| 624 | |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 625 | // Tests that zero byte files correctly go through ContentVerifyJob. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 626 | TEST_P(ExtensionProtocolsTest, VerificationSeenForZeroByteFile) { |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 627 | SetProtocolHandler(false); |
| 628 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 629 | const std::string kEmptyJs("empty.js"); |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 630 | base::ScopedTempDir temp_dir; |
| 631 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 632 | base::FilePath unzipped_path = temp_dir.GetPath(); |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 633 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 634 | scoped_refptr<Extension> extension = |
| 635 | content_verifier_test_utils::UnzipToDirAndLoadExtension( |
| 636 | GetContentVerifierTestPath().AppendASCII("source.zip"), |
| 637 | unzipped_path); |
| 638 | ASSERT_TRUE(extension.get()); |
| 639 | |
| 640 | base::FilePath kRelativePath(FILE_PATH_LITERAL("empty.js")); |
| 641 | ExtensionId extension_id = extension->id(); |
| 642 | |
| 643 | // Sanity check empty.js. |
| 644 | base::FilePath file_path = unzipped_path.AppendASCII(kEmptyJs); |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 645 | int64_t foo_file_size = -1; |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 646 | ASSERT_TRUE(base::GetFileSize(file_path, &foo_file_size)); |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 647 | ASSERT_EQ(0, foo_file_size); |
| 648 | |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 649 | // Request empty.js. |
| 650 | { |
Istiaque Ahmed | 584fe14 | 2018-03-13 09:19:04 | [diff] [blame] | 651 | TestContentVerifySingleJobObserver observer(extension_id, kRelativePath); |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 652 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 653 | content_verifier_->OnExtensionLoaded(browser_context(), extension.get()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 654 | // Wait for PostTask to ContentVerifierIOData::AddData() to finish. |
| 655 | content::RunAllPendingInMessageLoop(); |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 656 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 657 | EXPECT_EQ(net::OK, DoRequestOrLoad(extension, kEmptyJs).result()); |
Istiaque Ahmed | 1dec1d4 | 2018-02-02 00:01:36 | [diff] [blame] | 658 | EXPECT_EQ(ContentVerifyJob::NONE, observer.WaitForJobFinished()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | // chmod -r empty.js. |
| 662 | // Unreadable empty file doesn't generate hash mismatch. Note that this is the |
| 663 | // current behavior of ContentVerifyJob. |
| 664 | // TODO(lazyboy): The behavior is probably incorrect. |
| 665 | { |
Istiaque Ahmed | 584fe14 | 2018-03-13 09:19:04 | [diff] [blame] | 666 | TestContentVerifySingleJobObserver observer(extension->id(), kRelativePath); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 667 | base::FilePath file_path = unzipped_path.AppendASCII(kEmptyJs); |
| 668 | ASSERT_TRUE(base::MakeFileUnreadable(file_path)); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 669 | EXPECT_EQ(net::ERR_ACCESS_DENIED, |
| 670 | DoRequestOrLoad(extension, kEmptyJs).result()); |
Istiaque Ahmed | 1dec1d4 | 2018-02-02 00:01:36 | [diff] [blame] | 671 | EXPECT_EQ(ContentVerifyJob::NONE, observer.WaitForJobFinished()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | // rm empty.js. |
| 675 | // Deleted empty file doesn't generate hash mismatch. Note that this is the |
| 676 | // current behavior of ContentVerifyJob. |
| 677 | // TODO(lazyboy): The behavior is probably incorrect. |
| 678 | { |
Istiaque Ahmed | 584fe14 | 2018-03-13 09:19:04 | [diff] [blame] | 679 | TestContentVerifySingleJobObserver observer(extension_id, kRelativePath); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 680 | base::FilePath file_path = unzipped_path.AppendASCII(kEmptyJs); |
| 681 | ASSERT_TRUE(base::DieFileDie(file_path, false)); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 682 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, |
| 683 | DoRequestOrLoad(extension, kEmptyJs).result()); |
Istiaque Ahmed | 1dec1d4 | 2018-02-02 00:01:36 | [diff] [blame] | 684 | EXPECT_EQ(ContentVerifyJob::NONE, observer.WaitForJobFinished()); |
Istiaque Ahmed | 72816eaa | 2018-01-30 22:37:06 | [diff] [blame] | 685 | } |
lazyboy | e83ab9c6 | 2017-03-30 03:18:26 | [diff] [blame] | 686 | } |
| 687 | |
proberge | 4f9644c | 2018-03-27 23:01:54 | [diff] [blame] | 688 | TEST_P(ExtensionProtocolsTest, VerifyScriptListedAsIcon) { |
| 689 | SetProtocolHandler(false); |
| 690 | |
| 691 | const std::string kBackgroundJs("background.js"); |
| 692 | base::ScopedTempDir temp_dir; |
| 693 | ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 694 | base::FilePath unzipped_path = temp_dir.GetPath(); |
| 695 | |
| 696 | base::FilePath path; |
Avi Drissman | 9098f900 | 2018-05-04 00:11:52 | [diff] [blame] | 697 | EXPECT_TRUE(base::PathService::Get(extensions::DIR_TEST_DATA, &path)); |
proberge | 4f9644c | 2018-03-27 23:01:54 | [diff] [blame] | 698 | |
| 699 | scoped_refptr<Extension> extension = |
| 700 | content_verifier_test_utils::UnzipToDirAndLoadExtension( |
| 701 | path.AppendASCII("content_hash_fetcher") |
| 702 | .AppendASCII("manifest_mislabeled_script") |
| 703 | .AppendASCII("source.zip"), |
| 704 | unzipped_path); |
| 705 | ASSERT_TRUE(extension.get()); |
| 706 | |
| 707 | base::FilePath kRelativePath(FILE_PATH_LITERAL("background.js")); |
| 708 | ExtensionId extension_id = extension->id(); |
| 709 | |
| 710 | // Request background.js. |
| 711 | { |
| 712 | TestContentVerifySingleJobObserver observer(extension_id, kRelativePath); |
| 713 | |
| 714 | content_verifier_->OnExtensionLoaded(browser_context(), extension.get()); |
| 715 | // Wait for PostTask to ContentVerifierIOData::AddData() to finish. |
| 716 | base::RunLoop().RunUntilIdle(); |
| 717 | |
| 718 | EXPECT_EQ(net::OK, DoRequestOrLoad(extension, kBackgroundJs).result()); |
| 719 | EXPECT_EQ(ContentVerifyJob::NONE, observer.WaitForJobFinished()); |
| 720 | } |
| 721 | |
| 722 | // Modify background.js and request it. |
| 723 | { |
| 724 | base::FilePath file_path = unzipped_path.AppendASCII("background.js"); |
| 725 | const std::string content = "new content"; |
| 726 | EXPECT_NE(base::WriteFile(file_path, content.c_str(), content.size()), -1); |
| 727 | TestContentVerifySingleJobObserver observer(extension_id, kRelativePath); |
| 728 | |
| 729 | content_verifier_->OnExtensionLoaded(browser_context(), extension.get()); |
| 730 | // Wait for PostTask to ContentVerifierIOData::AddData() to finish. |
| 731 | base::RunLoop().RunUntilIdle(); |
| 732 | |
| 733 | EXPECT_EQ(net::OK, DoRequestOrLoad(extension, kBackgroundJs).result()); |
| 734 | EXPECT_EQ(ContentVerifyJob::HASH_MISMATCH, observer.WaitForJobFinished()); |
| 735 | } |
| 736 | } |
| 737 | |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 738 | // Tests that mime types are properly set for returned extension resources. |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 739 | TEST_P(ExtensionProtocolsTest, MimeTypesForKnownFiles) { |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 740 | // Register a non-incognito extension protocol handler. |
| 741 | SetProtocolHandler(false); |
| 742 | |
| 743 | TestExtensionDir test_dir; |
| 744 | constexpr char kManifest[] = R"( |
| 745 | { |
| 746 | "name": "Test Ext", |
| 747 | "description": "A test extension", |
| 748 | "manifest_version": 2, |
| 749 | "version": "0.1", |
| 750 | "web_accessible_resources": ["*"] |
| 751 | })"; |
| 752 | test_dir.WriteManifest(kManifest); |
| 753 | std::unique_ptr<base::DictionaryValue> manifest = |
| 754 | base::DictionaryValue::From(base::test::ParseJson(kManifest)); |
| 755 | ASSERT_TRUE(manifest); |
| 756 | |
| 757 | test_dir.WriteFile(FILE_PATH_LITERAL("json_file.json"), "{}"); |
| 758 | test_dir.WriteFile(FILE_PATH_LITERAL("js_file.js"), "function() {}"); |
| 759 | |
| 760 | base::FilePath unpacked_path = test_dir.UnpackedPath(); |
| 761 | ASSERT_TRUE(base::PathExists(unpacked_path.AppendASCII("json_file.json"))); |
| 762 | std::string error; |
| 763 | scoped_refptr<const Extension> extension = |
| 764 | ExtensionBuilder() |
| 765 | .SetManifest(std::move(manifest)) |
| 766 | .SetPath(unpacked_path) |
| 767 | .SetLocation(Manifest::INTERNAL) |
| 768 | .Build(); |
| 769 | ASSERT_TRUE(extension); |
| 770 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 771 | AddExtension(extension.get(), false, false); |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 772 | |
| 773 | struct { |
| 774 | const char* file_name; |
| 775 | const char* expected_mime_type; |
| 776 | } test_cases[] = { |
Devlin Cronin | f8d8988 | 2018-06-29 14:52:43 | [diff] [blame] | 777 | {"json_file.json", "application/json"}, {"js_file.js", "text/javascript"}, |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 778 | }; |
| 779 | |
| 780 | for (const auto& test_case : test_cases) { |
| 781 | SCOPED_TRACE(test_case.file_name); |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 782 | auto result = RequestOrLoad(extension->GetResourceURL(test_case.file_name), |
| 783 | content::RESOURCE_TYPE_SUB_RESOURCE); |
| 784 | EXPECT_EQ( |
| 785 | test_case.expected_mime_type, |
| 786 | result.GetResponseHeaderByName(net::HttpRequestHeaders::kContentType)); |
Devlin Cronin | c3f8807 | 2018-01-30 02:10:11 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
Karandeep Bhatia | ab489a4d | 2018-11-28 21:56:06 | [diff] [blame] | 790 | // Tests that requests for extension resources (including the generated |
| 791 | // background page) are not aborted on system suspend. |
| 792 | TEST_P(ExtensionProtocolsTest, ExtensionRequestsNotAborted) { |
| 793 | // Register a non-incognito extension protocol handler. |
| 794 | SetProtocolHandler(false); |
| 795 | |
| 796 | base::FilePath extension_dir = |
| 797 | GetTestPath("common").AppendASCII("background_script"); |
| 798 | std::string error; |
| 799 | scoped_refptr<Extension> extension = file_util::LoadExtension( |
| 800 | extension_dir, Manifest::INTERNAL, Extension::NO_FLAGS, &error); |
| 801 | ASSERT_TRUE(extension.get()) << error; |
| 802 | |
| 803 | SimulateSystemSuspendForRequests(); |
| 804 | |
| 805 | // Request the generated background page. Ensure the request completes |
| 806 | // successfully. |
| 807 | EXPECT_EQ(net::OK, |
| 808 | DoRequestOrLoad(extension.get(), kGeneratedBackgroundPageFilename) |
| 809 | .result()); |
| 810 | |
| 811 | // Request the background.js file. Ensure the request completes successfully. |
| 812 | EXPECT_EQ(net::OK, DoRequestOrLoad(extension.get(), "background.js").result()); |
| 813 | } |
| 814 | |
Chris Mumford | 8f81266 | 2018-02-22 00:27:57 | [diff] [blame] | 815 | INSTANTIATE_TEST_CASE_P(Extensions, |
| 816 | ExtensionProtocolsTest, |
| 817 | ::testing::ValuesIn(kTestModes)); |
| 818 | |
[email protected] | 702d8b4 | 2013-02-27 20:55:50 | [diff] [blame] | 819 | } // namespace extensions |