blob: 61bfba09b987622dfbb0e5f8a3d4f70bb5fc664a [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.
lfg048201a2014-09-16 19:09:3696// See chrome/browser/extensions/chrome_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
dcheng72191812014-10-28 20:49:56104 void SetUp() override {
[email protected]06492ed2013-03-24 22:13:14105 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
dcheng72191812014-10-28 20:49:56112 void TearDown() override {
[email protected]5e212ed2012-03-21 23:29:15113 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) {
gabf9d15582014-11-13 16:40:15130 content::ResourceRequestInfo::AllocateForTesting(
131 request,
132 resource_type,
133 &resource_context_,
134 -1, // render_process_id
135 -1, // render_view_id
136 -1, // render_frame_id
137 resource_type == content::RESOURCE_TYPE_MAIN_FRAME, // is_main_frame
138 false, // parent_is_main_frame
139 false); // is_async
[email protected]5e212ed2012-03-21 23:29:15140 request->Start();
[email protected]b3a25092013-05-28 22:08:16141 base::MessageLoop::current()->Run();
[email protected]5e212ed2012-03-21 23:29:15142 }
143
144 protected:
[email protected]ec04d3f2013-06-06 21:31:39145 content::TestBrowserThreadBundle thread_bundle_;
[email protected]38427a12013-11-09 17:34:20146 scoped_refptr<InfoMap> extension_info_map_;
[email protected]9d5730b2012-08-24 17:42:49147 net::URLRequestJobFactoryImpl job_factory_;
[email protected]5e212ed2012-03-21 23:29:15148 const net::URLRequestJobFactory* old_factory_;
[email protected]2086a3d2012-11-13 17:49:20149 net::TestDelegate test_delegate_;
[email protected]37ac95b2013-07-23 23:39:35150 net::TestURLRequestContext test_url_request_context_;
[email protected]5e212ed2012-03-21 23:29:15151 content::MockResourceContext resource_context_;
152};
153
154// Tests that making a chrome-extension request in an incognito context is
155// only allowed under the right circumstances (if the extension is allowed
156// in incognito, and it's either a non-main-frame request or a split-mode
157// extension).
158TEST_F(ExtensionProtocolTest, IncognitoRequest) {
[email protected]93ac047a2012-12-13 02:53:49159 // Register an incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01160 SetProtocolHandler(true);
[email protected]93ac047a2012-12-13 02:53:49161
[email protected]5e212ed2012-03-21 23:29:15162 struct TestCase {
163 // Inputs.
164 std::string name;
165 bool incognito_split_mode;
166 bool incognito_enabled;
167
168 // Expected results.
169 bool should_allow_main_frame_load;
170 bool should_allow_sub_frame_load;
171 } cases[] = {
172 {"spanning disabled", false, false, false, false},
173 {"split disabled", true, false, false, false},
174 {"spanning enabled", false, true, false, true},
175 {"split enabled", true, true, true, true},
176 };
177
viettrungluu9e65ad12014-10-16 04:22:26178 for (size_t i = 0; i < arraysize(cases); ++i) {
[email protected]5e212ed2012-03-21 23:29:15179 scoped_refptr<Extension> extension =
180 CreateTestExtension(cases[i].name, cases[i].incognito_split_mode);
181 extension_info_map_->AddExtension(
[email protected]9afacd22013-11-13 20:23:31182 extension.get(), base::Time::Now(), cases[i].incognito_enabled, false);
[email protected]5e212ed2012-03-21 23:29:15183
184 // First test a main frame request.
185 {
186 // It doesn't matter that the resource doesn't exist. If the resource
187 // is blocked, we should see ADDRESS_UNREACHABLE. Otherwise, the request
188 // should just fail because the file doesn't exist.
[email protected]f7022f32014-08-21 16:32:19189 scoped_ptr<net::URLRequest> request(
190 resource_context_.GetRequestContext()->CreateRequest(
191 extension->GetResourceURL("404.html"),
192 net::DEFAULT_PRIORITY,
193 &test_delegate_,
194 NULL));
195 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME);
196 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
[email protected]5e212ed2012-03-21 23:29:15197
198 if (cases[i].should_allow_main_frame_load) {
[email protected]f7022f32014-08-21 16:32:19199 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()) <<
[email protected]5e212ed2012-03-21 23:29:15200 cases[i].name;
201 } else {
[email protected]f7022f32014-08-21 16:32:19202 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request->status().error()) <<
[email protected]5e212ed2012-03-21 23:29:15203 cases[i].name;
204 }
205 }
206
207 // Now do a subframe request.
208 {
[email protected]f7022f32014-08-21 16:32:19209 scoped_ptr<net::URLRequest> request(
210 resource_context_.GetRequestContext()->CreateRequest(
211 extension->GetResourceURL("404.html"),
212 net::DEFAULT_PRIORITY,
213 &test_delegate_,
214 NULL));
215 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME);
216 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
[email protected]5e212ed2012-03-21 23:29:15217
218 if (cases[i].should_allow_sub_frame_load) {
[email protected]f7022f32014-08-21 16:32:19219 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()) <<
[email protected]5e212ed2012-03-21 23:29:15220 cases[i].name;
221 } else {
[email protected]f7022f32014-08-21 16:32:19222 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request->status().error()) <<
[email protected]5e212ed2012-03-21 23:29:15223 cases[i].name;
224 }
225 }
226 }
227}
228
[email protected]774cebd2013-09-26 04:55:01229void CheckForContentLengthHeader(net::URLRequest* request) {
230 std::string content_length;
231 request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength,
232 &content_length);
233 EXPECT_FALSE(content_length.empty());
234 int length_value = 0;
235 EXPECT_TRUE(base::StringToInt(content_length, &length_value));
236 EXPECT_GT(length_value, 0);
237}
238
[email protected]93ac047a2012-12-13 02:53:49239// Tests getting a resource for a component extension works correctly, both when
240// the extension is enabled and when it is disabled.
241TEST_F(ExtensionProtocolTest, ComponentResourceRequest) {
242 // Register a non-incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01243 SetProtocolHandler(false);
[email protected]93ac047a2012-12-13 02:53:49244
245 scoped_refptr<Extension> extension = CreateWebStoreExtension();
[email protected]9afacd22013-11-13 20:23:31246 extension_info_map_->AddExtension(extension.get(),
247 base::Time::Now(),
248 false,
249 false);
[email protected]93ac047a2012-12-13 02:53:49250
251 // First test it with the extension enabled.
252 {
[email protected]f7022f32014-08-21 16:32:19253 scoped_ptr<net::URLRequest> request(
254 resource_context_.GetRequestContext()->CreateRequest(
255 extension->GetResourceURL("webstore_icon_16.png"),
256 net::DEFAULT_PRIORITY,
257 &test_delegate_,
258 NULL));
259 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
260 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
261 CheckForContentLengthHeader(request.get());
[email protected]93ac047a2012-12-13 02:53:49262 }
263
264 // And then test it with the extension disabled.
265 extension_info_map_->RemoveExtension(extension->id(),
[email protected]b0af4792013-10-23 09:12:13266 UnloadedExtensionInfo::REASON_DISABLE);
[email protected]93ac047a2012-12-13 02:53:49267 {
[email protected]f7022f32014-08-21 16:32:19268 scoped_ptr<net::URLRequest> request(
269 resource_context_.GetRequestContext()->CreateRequest(
270 extension->GetResourceURL("webstore_icon_16.png"),
271 net::DEFAULT_PRIORITY,
272 &test_delegate_,
273 NULL));
274 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
275 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
276 CheckForContentLengthHeader(request.get());
[email protected]93ac047a2012-12-13 02:53:49277 }
278}
279
[email protected]6f7d7062013-06-04 03:49:33280// Tests that a URL request for resource from an extension returns a few
281// expected response headers.
282TEST_F(ExtensionProtocolTest, ResourceRequestResponseHeaders) {
283 // Register a non-incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01284 SetProtocolHandler(false);
[email protected]6f7d7062013-06-04 03:49:33285
286 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension();
[email protected]9afacd22013-11-13 20:23:31287 extension_info_map_->AddExtension(extension.get(),
288 base::Time::Now(),
289 false,
290 false);
[email protected]6f7d7062013-06-04 03:49:33291
292 {
[email protected]f7022f32014-08-21 16:32:19293 scoped_ptr<net::URLRequest> request(
294 resource_context_.GetRequestContext()->CreateRequest(
295 extension->GetResourceURL("test.dat"),
296 net::DEFAULT_PRIORITY,
297 &test_delegate_,
298 NULL));
299 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
300 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
[email protected]6f7d7062013-06-04 03:49:33301
302 // Check that cache-related headers are set.
303 std::string etag;
[email protected]f7022f32014-08-21 16:32:19304 request->GetResponseHeaderByName("ETag", &etag);
[email protected]6f7d7062013-06-04 03:49:33305 EXPECT_TRUE(StartsWithASCII(etag, "\"", false));
306 EXPECT_TRUE(EndsWith(etag, "\"", false));
307
308 std::string revalidation_header;
[email protected]f7022f32014-08-21 16:32:19309 request->GetResponseHeaderByName("cache-control", &revalidation_header);
[email protected]6f7d7062013-06-04 03:49:33310 EXPECT_EQ("no-cache", revalidation_header);
311
312 // We set test.dat as web-accessible, so it should have a CORS header.
313 std::string access_control;
[email protected]f7022f32014-08-21 16:32:19314 request->GetResponseHeaderByName("Access-Control-Allow-Origin",
[email protected]6f7d7062013-06-04 03:49:33315 &access_control);
316 EXPECT_EQ("*", access_control);
317 }
318}
319
[email protected]b109bdd2013-11-04 18:08:43320// Tests that a URL request for main frame or subframe from an extension
321// succeeds, but subresources fail. See http://crbug.com/312269.
322TEST_F(ExtensionProtocolTest, AllowFrameRequests) {
323 // Register a non-incognito extension protocol handler.
[email protected]1791e6c92014-04-11 08:29:01324 SetProtocolHandler(false);
[email protected]b109bdd2013-11-04 18:08:43325
326 scoped_refptr<Extension> extension = CreateTestExtension("foo", false);
[email protected]9afacd22013-11-13 20:23:31327 extension_info_map_->AddExtension(extension.get(),
328 base::Time::Now(),
329 false,
330 false);
[email protected]b109bdd2013-11-04 18:08:43331
332 // All MAIN_FRAME and SUB_FRAME requests should succeed.
333 {
[email protected]f7022f32014-08-21 16:32:19334 scoped_ptr<net::URLRequest> request(
335 resource_context_.GetRequestContext()->CreateRequest(
336 extension->GetResourceURL("test.dat"),
337 net::DEFAULT_PRIORITY,
338 &test_delegate_,
339 NULL));
340 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME);
341 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
[email protected]b109bdd2013-11-04 18:08:43342 }
343 {
[email protected]f7022f32014-08-21 16:32:19344 scoped_ptr<net::URLRequest> request(
345 resource_context_.GetRequestContext()->CreateRequest(
346 extension->GetResourceURL("test.dat"),
347 net::DEFAULT_PRIORITY,
348 &test_delegate_,
349 NULL));
350 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME);
351 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
[email protected]b109bdd2013-11-04 18:08:43352 }
353
354 // And subresource types, such as media, should fail.
355 {
[email protected]f7022f32014-08-21 16:32:19356 scoped_ptr<net::URLRequest> request(
357 resource_context_.GetRequestContext()->CreateRequest(
358 extension->GetResourceURL("test.dat"),
359 net::DEFAULT_PRIORITY,
360 &test_delegate_,
361 NULL));
362 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
363 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
[email protected]b109bdd2013-11-04 18:08:43364 }
365}
366
[email protected]702d8b42013-02-27 20:55:50367} // namespace extensions