[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 | |
| 5 | #include <string> |
| 6 | |
thestig | c9e38a2 | 2014-09-13 01:02:11 | [diff] [blame^] | 7 | #include "base/files/file_util.h" |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 8 | #include "base/memory/scoped_ptr.h" |
[email protected] | b19fe57 | 2013-07-18 04:54:26 | [diff] [blame] | 9 | #include "base/message_loop/message_loop.h" |
[email protected] | 774cebd | 2013-09-26 04:55:01 | [diff] [blame] | 10 | #include "base/strings/string_number_conversions.h" |
[email protected] | 00e7bef | 2013-06-10 20:35:17 | [diff] [blame] | 11 | #include "base/strings/string_util.h" |
[email protected] | 06492ed | 2013-03-24 22:13:14 | [diff] [blame] | 12 | #include "base/values.h" |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 13 | #include "chrome/common/chrome_paths.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 14 | #include "content/public/browser/resource_request_info.h" |
[email protected] | 08a932d5 | 2012-06-03 21:42:12 | [diff] [blame] | 15 | #include "content/public/test/mock_resource_context.h" |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 16 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 17 | #include "extensions/browser/extension_protocols.h" |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 18 | #include "extensions/browser/info_map.h" |
[email protected] | 885c0e9 | 2012-11-13 20:27:42 | [diff] [blame] | 19 | #include "extensions/common/constants.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 20 | #include "extensions/common/extension.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 21 | #include "net/base/request_priority.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 22 | #include "net/url_request/url_request.h" |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 23 | #include "net/url_request/url_request_job_factory_impl.h" |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 24 | #include "net/url_request/url_request_status.h" |
| 25 | #include "net/url_request/url_request_test_util.h" |
| 26 | #include "testing/gtest/include/gtest/gtest.h" |
| 27 | |
[email protected] | 7491ad0 | 2014-07-05 19:10:07 | [diff] [blame] | 28 | using content::ResourceType; |
| 29 | |
[email protected] | 702d8b4 | 2013-02-27 20:55:50 | [diff] [blame] | 30 | namespace extensions { |
jamescook | 8816ae5 | 2014-09-05 17:02:37 | [diff] [blame] | 31 | namespace { |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 32 | |
| 33 | scoped_refptr<Extension> CreateTestExtension(const std::string& name, |
| 34 | bool incognito_split_mode) { |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 35 | base::DictionaryValue manifest; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 36 | manifest.SetString("name", name); |
| 37 | manifest.SetString("version", "1"); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 38 | manifest.SetInteger("manifest_version", 2); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 39 | manifest.SetString("incognito", incognito_split_mode ? "split" : "spanning"); |
| 40 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 41 | base::FilePath path; |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 42 | EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); |
| 43 | path = path.AppendASCII("extensions").AppendASCII("response_headers"); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 44 | |
| 45 | std::string error; |
| 46 | scoped_refptr<Extension> extension( |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 47 | Extension::Create(path, Manifest::INTERNAL, manifest, |
[email protected] | ed3b9b1 | 2012-05-31 18:37:51 | [diff] [blame] | 48 | Extension::NO_FLAGS, &error)); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 49 | EXPECT_TRUE(extension.get()) << error; |
| 50 | return extension; |
| 51 | } |
| 52 | |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 53 | scoped_refptr<Extension> CreateWebStoreExtension() { |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 54 | base::DictionaryValue manifest; |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 55 | manifest.SetString("name", "WebStore"); |
| 56 | manifest.SetString("version", "1"); |
| 57 | manifest.SetString("icons.16", "webstore_icon_16.png"); |
| 58 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 59 | base::FilePath path; |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 60 | EXPECT_TRUE(PathService::Get(chrome::DIR_RESOURCES, &path)); |
| 61 | path = path.AppendASCII("web_store"); |
| 62 | |
| 63 | std::string error; |
| 64 | scoped_refptr<Extension> extension( |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 65 | Extension::Create(path, Manifest::COMPONENT, manifest, |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 66 | Extension::NO_FLAGS, &error)); |
| 67 | EXPECT_TRUE(extension.get()) << error; |
| 68 | return extension; |
| 69 | } |
| 70 | |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 71 | scoped_refptr<Extension> CreateTestResponseHeaderExtension() { |
[email protected] | 023b3d1 | 2013-12-23 18:46:49 | [diff] [blame] | 72 | base::DictionaryValue manifest; |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 73 | manifest.SetString("name", "An extension with web-accessible resources"); |
| 74 | manifest.SetString("version", "2"); |
| 75 | |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 76 | base::ListValue* web_accessible_list = new base::ListValue(); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 77 | web_accessible_list->AppendString("test.dat"); |
| 78 | manifest.Set("web_accessible_resources", web_accessible_list); |
| 79 | |
| 80 | base::FilePath path; |
| 81 | EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); |
| 82 | path = path.AppendASCII("extensions").AppendASCII("response_headers"); |
| 83 | |
| 84 | std::string error; |
| 85 | scoped_refptr<Extension> extension( |
| 86 | Extension::Create(path, Manifest::UNPACKED, manifest, |
| 87 | Extension::NO_FLAGS, &error)); |
| 88 | EXPECT_TRUE(extension.get()) << error; |
| 89 | return extension; |
| 90 | } |
| 91 | |
jamescook | 8816ae5 | 2014-09-05 17:02:37 | [diff] [blame] | 92 | } // namespace |
| 93 | |
| 94 | // This test lives in src/chrome instead of src/extensions because it tests |
| 95 | // functionality delegated back to Chrome via ChromeExtensionsBrowserClient. |
| 96 | // See chrome/browser/extensions/url_request_util.cc. |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 97 | class ExtensionProtocolTest : public testing::Test { |
| 98 | public: |
| 99 | ExtensionProtocolTest() |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 100 | : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 101 | old_factory_(NULL), |
| 102 | resource_context_(&test_url_request_context_) {} |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 103 | |
[email protected] | 06492ed | 2013-03-24 22:13:14 | [diff] [blame] | 104 | virtual void SetUp() OVERRIDE { |
| 105 | testing::Test::SetUp(); |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 106 | extension_info_map_ = new InfoMap(); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 107 | net::URLRequestContext* request_context = |
| 108 | resource_context_.GetRequestContext(); |
| 109 | old_factory_ = request_context->job_factory(); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | virtual void TearDown() { |
| 113 | net::URLRequestContext* request_context = |
| 114 | resource_context_.GetRequestContext(); |
| 115 | request_context->set_job_factory(old_factory_); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 118 | void SetProtocolHandler(bool is_incognito) { |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 119 | net::URLRequestContext* request_context = |
| 120 | resource_context_.GetRequestContext(); |
| 121 | job_factory_.SetProtocolHandler( |
[email protected] | 702d8b4 | 2013-02-27 20:55:50 | [diff] [blame] | 122 | kExtensionScheme, |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 123 | CreateExtensionProtocolHandler(is_incognito, |
[email protected] | 7b7e0b3 | 2014-03-17 16:09:23 | [diff] [blame] | 124 | extension_info_map_.get())); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 125 | request_context->set_job_factory(&job_factory_); |
| 126 | } |
| 127 | |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 128 | void StartRequest(net::URLRequest* request, |
[email protected] | 6c1e0521 | 2014-07-31 00:59:40 | [diff] [blame] | 129 | ResourceType resource_type) { |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 130 | content::ResourceRequestInfo::AllocateForTesting(request, |
| 131 | resource_type, |
[email protected] | ef108e7 | 2012-06-20 14:03:54 | [diff] [blame] | 132 | &resource_context_, |
| 133 | -1, |
[email protected] | 716db0f | 2013-10-01 20:28:30 | [diff] [blame] | 134 | -1, |
[email protected] | 5dcaf8e | 2013-12-28 01:31:42 | [diff] [blame] | 135 | -1, |
[email protected] | 716db0f | 2013-10-01 20:28:30 | [diff] [blame] | 136 | false); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 137 | request->Start(); |
[email protected] | b3a2509 | 2013-05-28 22:08:16 | [diff] [blame] | 138 | base::MessageLoop::current()->Run(); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | protected: |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 142 | content::TestBrowserThreadBundle thread_bundle_; |
[email protected] | 38427a1 | 2013-11-09 17:34:20 | [diff] [blame] | 143 | scoped_refptr<InfoMap> extension_info_map_; |
[email protected] | 9d5730b | 2012-08-24 17:42:49 | [diff] [blame] | 144 | net::URLRequestJobFactoryImpl job_factory_; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 145 | const net::URLRequestJobFactory* old_factory_; |
[email protected] | 2086a3d | 2012-11-13 17:49:20 | [diff] [blame] | 146 | net::TestDelegate test_delegate_; |
[email protected] | 37ac95b | 2013-07-23 23:39:35 | [diff] [blame] | 147 | net::TestURLRequestContext test_url_request_context_; |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 148 | content::MockResourceContext resource_context_; |
| 149 | }; |
| 150 | |
| 151 | // Tests that making a chrome-extension request in an incognito context is |
| 152 | // only allowed under the right circumstances (if the extension is allowed |
| 153 | // in incognito, and it's either a non-main-frame request or a split-mode |
| 154 | // extension). |
| 155 | TEST_F(ExtensionProtocolTest, IncognitoRequest) { |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 156 | // Register an incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 157 | SetProtocolHandler(true); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 158 | |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 159 | struct TestCase { |
| 160 | // Inputs. |
| 161 | std::string name; |
| 162 | bool incognito_split_mode; |
| 163 | bool incognito_enabled; |
| 164 | |
| 165 | // Expected results. |
| 166 | bool should_allow_main_frame_load; |
| 167 | bool should_allow_sub_frame_load; |
| 168 | } cases[] = { |
| 169 | {"spanning disabled", false, false, false, false}, |
| 170 | {"split disabled", true, false, false, false}, |
| 171 | {"spanning enabled", false, true, false, true}, |
| 172 | {"split enabled", true, true, true, true}, |
| 173 | }; |
| 174 | |
| 175 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) { |
| 176 | scoped_refptr<Extension> extension = |
| 177 | CreateTestExtension(cases[i].name, cases[i].incognito_split_mode); |
| 178 | extension_info_map_->AddExtension( |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 179 | extension.get(), base::Time::Now(), cases[i].incognito_enabled, false); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 180 | |
| 181 | // First test a main frame request. |
| 182 | { |
| 183 | // It doesn't matter that the resource doesn't exist. If the resource |
| 184 | // is blocked, we should see ADDRESS_UNREACHABLE. Otherwise, the request |
| 185 | // should just fail because the file doesn't exist. |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 186 | scoped_ptr<net::URLRequest> request( |
| 187 | resource_context_.GetRequestContext()->CreateRequest( |
| 188 | extension->GetResourceURL("404.html"), |
| 189 | net::DEFAULT_PRIORITY, |
| 190 | &test_delegate_, |
| 191 | NULL)); |
| 192 | StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); |
| 193 | EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 194 | |
| 195 | if (cases[i].should_allow_main_frame_load) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 196 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()) << |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 197 | cases[i].name; |
| 198 | } else { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 199 | EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request->status().error()) << |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 200 | cases[i].name; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Now do a subframe request. |
| 205 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 206 | scoped_ptr<net::URLRequest> request( |
| 207 | resource_context_.GetRequestContext()->CreateRequest( |
| 208 | extension->GetResourceURL("404.html"), |
| 209 | net::DEFAULT_PRIORITY, |
| 210 | &test_delegate_, |
| 211 | NULL)); |
| 212 | StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME); |
| 213 | EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 214 | |
| 215 | if (cases[i].should_allow_sub_frame_load) { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 216 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()) << |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 217 | cases[i].name; |
| 218 | } else { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 219 | EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request->status().error()) << |
[email protected] | 5e212ed | 2012-03-21 23:29:15 | [diff] [blame] | 220 | cases[i].name; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
[email protected] | 774cebd | 2013-09-26 04:55:01 | [diff] [blame] | 226 | void CheckForContentLengthHeader(net::URLRequest* request) { |
| 227 | std::string content_length; |
| 228 | request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength, |
| 229 | &content_length); |
| 230 | EXPECT_FALSE(content_length.empty()); |
| 231 | int length_value = 0; |
| 232 | EXPECT_TRUE(base::StringToInt(content_length, &length_value)); |
| 233 | EXPECT_GT(length_value, 0); |
| 234 | } |
| 235 | |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 236 | // Tests getting a resource for a component extension works correctly, both when |
| 237 | // the extension is enabled and when it is disabled. |
| 238 | TEST_F(ExtensionProtocolTest, ComponentResourceRequest) { |
| 239 | // Register a non-incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 240 | SetProtocolHandler(false); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 241 | |
| 242 | scoped_refptr<Extension> extension = CreateWebStoreExtension(); |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 243 | extension_info_map_->AddExtension(extension.get(), |
| 244 | base::Time::Now(), |
| 245 | false, |
| 246 | false); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 247 | |
| 248 | // First test it with the extension enabled. |
| 249 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 250 | scoped_ptr<net::URLRequest> request( |
| 251 | resource_context_.GetRequestContext()->CreateRequest( |
| 252 | extension->GetResourceURL("webstore_icon_16.png"), |
| 253 | net::DEFAULT_PRIORITY, |
| 254 | &test_delegate_, |
| 255 | NULL)); |
| 256 | StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
| 257 | EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
| 258 | CheckForContentLengthHeader(request.get()); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | // And then test it with the extension disabled. |
| 262 | extension_info_map_->RemoveExtension(extension->id(), |
[email protected] | b0af479 | 2013-10-23 09:12:13 | [diff] [blame] | 263 | UnloadedExtensionInfo::REASON_DISABLE); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 264 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 265 | scoped_ptr<net::URLRequest> request( |
| 266 | resource_context_.GetRequestContext()->CreateRequest( |
| 267 | extension->GetResourceURL("webstore_icon_16.png"), |
| 268 | net::DEFAULT_PRIORITY, |
| 269 | &test_delegate_, |
| 270 | NULL)); |
| 271 | StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
| 272 | EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
| 273 | CheckForContentLengthHeader(request.get()); |
[email protected] | 93ac047a | 2012-12-13 02:53:49 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 277 | // Tests that a URL request for resource from an extension returns a few |
| 278 | // expected response headers. |
| 279 | TEST_F(ExtensionProtocolTest, ResourceRequestResponseHeaders) { |
| 280 | // Register a non-incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 281 | SetProtocolHandler(false); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 282 | |
| 283 | scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension(); |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 284 | extension_info_map_->AddExtension(extension.get(), |
| 285 | base::Time::Now(), |
| 286 | false, |
| 287 | false); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 288 | |
| 289 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 290 | scoped_ptr<net::URLRequest> request( |
| 291 | resource_context_.GetRequestContext()->CreateRequest( |
| 292 | extension->GetResourceURL("test.dat"), |
| 293 | net::DEFAULT_PRIORITY, |
| 294 | &test_delegate_, |
| 295 | NULL)); |
| 296 | StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
| 297 | EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 298 | |
| 299 | // Check that cache-related headers are set. |
| 300 | std::string etag; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 301 | request->GetResponseHeaderByName("ETag", &etag); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 302 | EXPECT_TRUE(StartsWithASCII(etag, "\"", false)); |
| 303 | EXPECT_TRUE(EndsWith(etag, "\"", false)); |
| 304 | |
| 305 | std::string revalidation_header; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 306 | request->GetResponseHeaderByName("cache-control", &revalidation_header); |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 307 | EXPECT_EQ("no-cache", revalidation_header); |
| 308 | |
| 309 | // We set test.dat as web-accessible, so it should have a CORS header. |
| 310 | std::string access_control; |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 311 | request->GetResponseHeaderByName("Access-Control-Allow-Origin", |
[email protected] | 6f7d706 | 2013-06-04 03:49:33 | [diff] [blame] | 312 | &access_control); |
| 313 | EXPECT_EQ("*", access_control); |
| 314 | } |
| 315 | } |
| 316 | |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 317 | // Tests that a URL request for main frame or subframe from an extension |
| 318 | // succeeds, but subresources fail. See http://crbug.com/312269. |
| 319 | TEST_F(ExtensionProtocolTest, AllowFrameRequests) { |
| 320 | // Register a non-incognito extension protocol handler. |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 321 | SetProtocolHandler(false); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 322 | |
| 323 | scoped_refptr<Extension> extension = CreateTestExtension("foo", false); |
[email protected] | 9afacd2 | 2013-11-13 20:23:31 | [diff] [blame] | 324 | extension_info_map_->AddExtension(extension.get(), |
| 325 | base::Time::Now(), |
| 326 | false, |
| 327 | false); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 328 | |
| 329 | // All MAIN_FRAME and SUB_FRAME requests should succeed. |
| 330 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 331 | scoped_ptr<net::URLRequest> request( |
| 332 | resource_context_.GetRequestContext()->CreateRequest( |
| 333 | extension->GetResourceURL("test.dat"), |
| 334 | net::DEFAULT_PRIORITY, |
| 335 | &test_delegate_, |
| 336 | NULL)); |
| 337 | StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); |
| 338 | EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 339 | } |
| 340 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 341 | scoped_ptr<net::URLRequest> request( |
| 342 | resource_context_.GetRequestContext()->CreateRequest( |
| 343 | extension->GetResourceURL("test.dat"), |
| 344 | net::DEFAULT_PRIORITY, |
| 345 | &test_delegate_, |
| 346 | NULL)); |
| 347 | StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME); |
| 348 | EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | // And subresource types, such as media, should fail. |
| 352 | { |
[email protected] | f7022f3 | 2014-08-21 16:32:19 | [diff] [blame] | 353 | scoped_ptr<net::URLRequest> request( |
| 354 | resource_context_.GetRequestContext()->CreateRequest( |
| 355 | extension->GetResourceURL("test.dat"), |
| 356 | net::DEFAULT_PRIORITY, |
| 357 | &test_delegate_, |
| 358 | NULL)); |
| 359 | StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
| 360 | EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
[email protected] | b109bdd | 2013-11-04 18:08:43 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
[email protected] | 702d8b4 | 2013-02-27 20:55:50 | [diff] [blame] | 364 | } // namespace extensions |