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