blob: d3622b164bc040edbc643615c30c496d71f00613 [file] [log] [blame]
[email protected]1791e6c92014-04-11 08:29:011// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]5e212ed2012-03-21 23:29:152// 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
thestigc9e38a22014-09-13 01:02:117#include "base/files/file_util.h"
[email protected]f7022f32014-08-21 16:32:198#include "base/memory/scoped_ptr.h"
[email protected]b19fe572013-07-18 04:54:269#include "base/message_loop/message_loop.h"
[email protected]774cebd2013-09-26 04:55:0110#include "base/strings/string_number_conversions.h"
[email protected]00e7bef2013-06-10 20:35:1711#include "base/strings/string_util.h"
[email protected]06492ed2013-03-24 22:13:1412#include "base/values.h"
[email protected]93ac047a2012-12-13 02:53:4913#include "chrome/common/chrome_paths.h"
[email protected]5e212ed2012-03-21 23:29:1514#include "content/public/browser/resource_request_info.h"
[email protected]08a932d52012-06-03 21:42:1215#include "content/public/test/mock_resource_context.h"
[email protected]ec04d3f2013-06-06 21:31:3916#include "content/public/test/test_browser_thread_bundle.h"
[email protected]1791e6c92014-04-11 08:29:0117#include "extensions/browser/extension_protocols.h"
[email protected]38427a12013-11-09 17:34:2018#include "extensions/browser/info_map.h"
[email protected]885c0e92012-11-13 20:27:4219#include "extensions/common/constants.h"
[email protected]e4452d32013-11-15 23:07:4120#include "extensions/common/extension.h"
[email protected]2ca01e52013-10-31 22:05:1921#include "net/base/request_priority.h"
[email protected]5e212ed2012-03-21 23:29:1522#include "net/url_request/url_request.h"
[email protected]9d5730b2012-08-24 17:42:4923#include "net/url_request/url_request_job_factory_impl.h"
[email protected]5e212ed2012-03-21 23:29:1524#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]7491ad02014-07-05 19:10:0728using content::ResourceType;
29
[email protected]702d8b42013-02-27 20:55:5030namespace extensions {
jamescook8816ae52014-09-05 17:02:3731namespace {
[email protected]5e212ed2012-03-21 23:29:1532
33scoped_refptr<Extension> CreateTestExtension(const std::string& name,
34 bool incognito_split_mode) {
[email protected]023b3d12013-12-23 18:46:4935 base::DictionaryValue manifest;
[email protected]5e212ed2012-03-21 23:29:1536 manifest.SetString("name", name);
37 manifest.SetString("version", "1");
[email protected]b109bdd2013-11-04 18:08:4338 manifest.SetInteger("manifest_version", 2);
[email protected]5e212ed2012-03-21 23:29:1539 manifest.SetString("incognito", incognito_split_mode ? "split" : "spanning");
40
[email protected]650b2d52013-02-10 03:41:4541 base::FilePath path;
[email protected]b109bdd2013-11-04 18:08:4342 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
43 path = path.AppendASCII("extensions").AppendASCII("response_headers");
[email protected]5e212ed2012-03-21 23:29:1544
45 std::string error;
46 scoped_refptr<Extension> extension(
[email protected]1d5e58b2013-01-31 08:41:4047 Extension::Create(path, Manifest::INTERNAL, manifest,
[email protected]ed3b9b12012-05-31 18:37:5148 Extension::NO_FLAGS, &error));
[email protected]5e212ed2012-03-21 23:29:1549 EXPECT_TRUE(extension.get()) << error;
50 return extension;
51}
52
[email protected]93ac047a2012-12-13 02:53:4953scoped_refptr<Extension> CreateWebStoreExtension() {
[email protected]023b3d12013-12-23 18:46:4954 base::DictionaryValue manifest;
[email protected]93ac047a2012-12-13 02:53:4955 manifest.SetString("name", "WebStore");
56 manifest.SetString("version", "1");
57 manifest.SetString("icons.16", "webstore_icon_16.png");
58
[email protected]650b2d52013-02-10 03:41:4559 base::FilePath path;
[email protected]93ac047a2012-12-13 02:53:4960 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]1d5e58b2013-01-31 08:41:4065 Extension::Create(path, Manifest::COMPONENT, manifest,
[email protected]93ac047a2012-12-13 02:53:4966 Extension::NO_FLAGS, &error));
67 EXPECT_TRUE(extension.get()) << error;
68 return extension;
69}
70
[email protected]6f7d7062013-06-04 03:49:3371scoped_refptr<Extension> CreateTestResponseHeaderExtension() {
[email protected]023b3d12013-12-23 18:46:4972 base::DictionaryValue manifest;
[email protected]6f7d7062013-06-04 03:49:3373 manifest.SetString("name", "An extension with web-accessible resources");
74 manifest.SetString("version", "2");
75
[email protected]aeca23f2013-06-21 22:34:4176 base::ListValue* web_accessible_list = new base::ListValue();
[email protected]6f7d7062013-06-04 03:49:3377 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
jamescook8816ae52014-09-05 17:02:3792} // 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]5e212ed2012-03-21 23:29:1597class ExtensionProtocolTest : public testing::Test {
98 public:
99 ExtensionProtocolTest()
[email protected]1791e6c92014-04-11 08:29:01100 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
101 old_factory_(NULL),
102 resource_context_(&test_url_request_context_) {}
[email protected]5e212ed2012-03-21 23:29:15103
[email protected]06492ed2013-03-24 22:13:14104 virtual void SetUp() OVERRIDE {
105 testing::Test::SetUp();
[email protected]38427a12013-11-09 17:34:20106 extension_info_map_ = new InfoMap();
[email protected]5e212ed2012-03-21 23:29:15107 net::URLRequestContext* request_context =
108 resource_context_.GetRequestContext();
109 old_factory_ = request_context->job_factory();
[email protected]5e212ed2012-03-21 23:29:15110 }
111
112 virtual void TearDown() {
113 net::URLRequestContext* request_context =
114 resource_context_.GetRequestContext();
115 request_context->set_job_factory(old_factory_);
[email protected]5e212ed2012-03-21 23:29:15116 }
117
[email protected]1791e6c92014-04-11 08:29:01118 void SetProtocolHandler(bool is_incognito) {
[email protected]93ac047a2012-12-13 02:53:49119 net::URLRequestContext* request_context =
120 resource_context_.GetRequestContext();
121 job_factory_.SetProtocolHandler(
[email protected]702d8b42013-02-27 20:55:50122 kExtensionScheme,
[email protected]1791e6c92014-04-11 08:29:01123 CreateExtensionProtocolHandler(is_incognito,
[email protected]7b7e0b32014-03-17 16:09:23124 extension_info_map_.get()));
[email protected]93ac047a2012-12-13 02:53:49125 request_context->set_job_factory(&job_factory_);
126 }
127
[email protected]5e212ed2012-03-21 23:29:15128 void StartRequest(net::URLRequest* request,
[email protected]6c1e05212014-07-31 00:59:40129 ResourceType resource_type) {
[email protected]5e212ed2012-03-21 23:29:15130 content::ResourceRequestInfo::AllocateForTesting(request,
131 resource_type,
[email protected]ef108e72012-06-20 14:03:54132 &resource_context_,
133 -1,
[email protected]716db0f2013-10-01 20:28:30134 -1,
[email protected]5dcaf8e2013-12-28 01:31:42135 -1,
[email protected]716db0f2013-10-01 20:28:30136 false);
[email protected]5e212ed2012-03-21 23:29:15137 request->Start();
[email protected]b3a25092013-05-28 22:08:16138 base::MessageLoop::current()->Run();
[email protected]5e212ed2012-03-21 23:29:15139 }
140
141 protected:
[email protected]ec04d3f2013-06-06 21:31:39142 content::TestBrowserThreadBundle thread_bundle_;
[email protected]38427a12013-11-09 17:34:20143 scoped_refptr<InfoMap> extension_info_map_;
[email protected]9d5730b2012-08-24 17:42:49144 net::URLRequestJobFactoryImpl job_factory_;
[email protected]5e212ed2012-03-21 23:29:15145 const net::URLRequestJobFactory* old_factory_;
[email protected]2086a3d2012-11-13 17:49:20146 net::TestDelegate test_delegate_;
[email protected]37ac95b2013-07-23 23:39:35147 net::TestURLRequestContext test_url_request_context_;
[email protected]5e212ed2012-03-21 23:29:15148 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).
155TEST_F(ExtensionProtocolTest, IncognitoRequest) {
[email protected]93ac047a2012-12-13 02:53:49156 // Register an incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01157 SetProtocolHandler(true);
[email protected]93ac047a2012-12-13 02:53:49158
[email protected]5e212ed2012-03-21 23:29:15159 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]9afacd22013-11-13 20:23:31179 extension.get(), base::Time::Now(), cases[i].incognito_enabled, false);
[email protected]5e212ed2012-03-21 23:29:15180
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]f7022f32014-08-21 16:32:19186 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]5e212ed2012-03-21 23:29:15194
195 if (cases[i].should_allow_main_frame_load) {
[email protected]f7022f32014-08-21 16:32:19196 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()) <<
[email protected]5e212ed2012-03-21 23:29:15197 cases[i].name;
198 } else {
[email protected]f7022f32014-08-21 16:32:19199 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request->status().error()) <<
[email protected]5e212ed2012-03-21 23:29:15200 cases[i].name;
201 }
202 }
203
204 // Now do a subframe request.
205 {
[email protected]f7022f32014-08-21 16:32:19206 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]5e212ed2012-03-21 23:29:15214
215 if (cases[i].should_allow_sub_frame_load) {
[email protected]f7022f32014-08-21 16:32:19216 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()) <<
[email protected]5e212ed2012-03-21 23:29:15217 cases[i].name;
218 } else {
[email protected]f7022f32014-08-21 16:32:19219 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request->status().error()) <<
[email protected]5e212ed2012-03-21 23:29:15220 cases[i].name;
221 }
222 }
223 }
224}
225
[email protected]774cebd2013-09-26 04:55:01226void 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]93ac047a2012-12-13 02:53:49236// Tests getting a resource for a component extension works correctly, both when
237// the extension is enabled and when it is disabled.
238TEST_F(ExtensionProtocolTest, ComponentResourceRequest) {
239 // Register a non-incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01240 SetProtocolHandler(false);
[email protected]93ac047a2012-12-13 02:53:49241
242 scoped_refptr<Extension> extension = CreateWebStoreExtension();
[email protected]9afacd22013-11-13 20:23:31243 extension_info_map_->AddExtension(extension.get(),
244 base::Time::Now(),
245 false,
246 false);
[email protected]93ac047a2012-12-13 02:53:49247
248 // First test it with the extension enabled.
249 {
[email protected]f7022f32014-08-21 16:32:19250 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]93ac047a2012-12-13 02:53:49259 }
260
261 // And then test it with the extension disabled.
262 extension_info_map_->RemoveExtension(extension->id(),
[email protected]b0af4792013-10-23 09:12:13263 UnloadedExtensionInfo::REASON_DISABLE);
[email protected]93ac047a2012-12-13 02:53:49264 {
[email protected]f7022f32014-08-21 16:32:19265 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]93ac047a2012-12-13 02:53:49274 }
275}
276
[email protected]6f7d7062013-06-04 03:49:33277// Tests that a URL request for resource from an extension returns a few
278// expected response headers.
279TEST_F(ExtensionProtocolTest, ResourceRequestResponseHeaders) {
280 // Register a non-incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01281 SetProtocolHandler(false);
[email protected]6f7d7062013-06-04 03:49:33282
283 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension();
[email protected]9afacd22013-11-13 20:23:31284 extension_info_map_->AddExtension(extension.get(),
285 base::Time::Now(),
286 false,
287 false);
[email protected]6f7d7062013-06-04 03:49:33288
289 {
[email protected]f7022f32014-08-21 16:32:19290 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]6f7d7062013-06-04 03:49:33298
299 // Check that cache-related headers are set.
300 std::string etag;
[email protected]f7022f32014-08-21 16:32:19301 request->GetResponseHeaderByName("ETag", &etag);
[email protected]6f7d7062013-06-04 03:49:33302 EXPECT_TRUE(StartsWithASCII(etag, "\"", false));
303 EXPECT_TRUE(EndsWith(etag, "\"", false));
304
305 std::string revalidation_header;
[email protected]f7022f32014-08-21 16:32:19306 request->GetResponseHeaderByName("cache-control", &revalidation_header);
[email protected]6f7d7062013-06-04 03:49:33307 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]f7022f32014-08-21 16:32:19311 request->GetResponseHeaderByName("Access-Control-Allow-Origin",
[email protected]6f7d7062013-06-04 03:49:33312 &access_control);
313 EXPECT_EQ("*", access_control);
314 }
315}
316
[email protected]b109bdd2013-11-04 18:08:43317// Tests that a URL request for main frame or subframe from an extension
318// succeeds, but subresources fail. See http://crbug.com/312269.
319TEST_F(ExtensionProtocolTest, AllowFrameRequests) {
320 // Register a non-incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01321 SetProtocolHandler(false);
[email protected]b109bdd2013-11-04 18:08:43322
323 scoped_refptr<Extension> extension = CreateTestExtension("foo", false);
[email protected]9afacd22013-11-13 20:23:31324 extension_info_map_->AddExtension(extension.get(),
325 base::Time::Now(),
326 false,
327 false);
[email protected]b109bdd2013-11-04 18:08:43328
329 // All MAIN_FRAME and SUB_FRAME requests should succeed.
330 {
[email protected]f7022f32014-08-21 16:32:19331 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]b109bdd2013-11-04 18:08:43339 }
340 {
[email protected]f7022f32014-08-21 16:32:19341 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]b109bdd2013-11-04 18:08:43349 }
350
351 // And subresource types, such as media, should fail.
352 {
[email protected]f7022f32014-08-21 16:32:19353 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]b109bdd2013-11-04 18:08:43361 }
362}
363
[email protected]702d8b42013-02-27 20:55:50364} // namespace extensions