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