1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/files/file_util.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/string_util.h"
12 #include "base/values.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "content/public/browser/resource_request_info.h"
15 #include "content/public/test/mock_resource_context.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "extensions/browser/extension_protocols.h"
18 #include "extensions/browser/info_map.h"
19 #include "extensions/common/constants.h"
20 #include "extensions/common/extension.h"
21 #include "net/base/request_priority.h"
22 #include "net/url_request/url_request.h"
23 #include "net/url_request/url_request_job_factory_impl.h"
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"
28 using content::ResourceType
;
30 namespace extensions
{
33 scoped_refptr
<Extension
> CreateTestExtension(const std::string
& name
,
34 bool incognito_split_mode
) {
35 base::DictionaryValue manifest
;
36 manifest
.SetString("name", name
);
37 manifest
.SetString("version", "1");
38 manifest
.SetInteger("manifest_version", 2);
39 manifest
.SetString("incognito", incognito_split_mode
? "split" : "spanning");
42 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &path
));
43 path
= path
.AppendASCII("extensions").AppendASCII("response_headers");
46 scoped_refptr
<Extension
> extension(
47 Extension::Create(path
, Manifest::INTERNAL
, manifest
,
48 Extension::NO_FLAGS
, &error
));
49 EXPECT_TRUE(extension
.get()) << error
;
53 scoped_refptr
<Extension
> CreateWebStoreExtension() {
54 base::DictionaryValue manifest
;
55 manifest
.SetString("name", "WebStore");
56 manifest
.SetString("version", "1");
57 manifest
.SetString("icons.16", "webstore_icon_16.png");
60 EXPECT_TRUE(PathService::Get(chrome::DIR_RESOURCES
, &path
));
61 path
= path
.AppendASCII("web_store");
64 scoped_refptr
<Extension
> extension(
65 Extension::Create(path
, Manifest::COMPONENT
, manifest
,
66 Extension::NO_FLAGS
, &error
));
67 EXPECT_TRUE(extension
.get()) << error
;
71 scoped_refptr
<Extension
> CreateTestResponseHeaderExtension() {
72 base::DictionaryValue manifest
;
73 manifest
.SetString("name", "An extension with web-accessible resources");
74 manifest
.SetString("version", "2");
76 base::ListValue
* web_accessible_list
= new base::ListValue();
77 web_accessible_list
->AppendString("test.dat");
78 manifest
.Set("web_accessible_resources", web_accessible_list
);
81 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &path
));
82 path
= path
.AppendASCII("extensions").AppendASCII("response_headers");
85 scoped_refptr
<Extension
> extension(
86 Extension::Create(path
, Manifest::UNPACKED
, manifest
,
87 Extension::NO_FLAGS
, &error
));
88 EXPECT_TRUE(extension
.get()) << error
;
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/chrome_url_request_util.cc.
97 class ExtensionProtocolTest
: public testing::Test
{
99 ExtensionProtocolTest()
100 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
102 resource_context_(&test_url_request_context_
) {}
104 void SetUp() override
{
105 testing::Test::SetUp();
106 extension_info_map_
= new InfoMap();
107 net::URLRequestContext
* request_context
=
108 resource_context_
.GetRequestContext();
109 old_factory_
= request_context
->job_factory();
112 void TearDown() override
{
113 net::URLRequestContext
* request_context
=
114 resource_context_
.GetRequestContext();
115 request_context
->set_job_factory(old_factory_
);
118 void SetProtocolHandler(bool is_incognito
) {
119 net::URLRequestContext
* request_context
=
120 resource_context_
.GetRequestContext();
121 job_factory_
.SetProtocolHandler(
123 CreateExtensionProtocolHandler(is_incognito
,
124 extension_info_map_
.get()));
125 request_context
->set_job_factory(&job_factory_
);
128 void StartRequest(net::URLRequest
* request
,
129 ResourceType resource_type
) {
130 content::ResourceRequestInfo::AllocateForTesting(
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 true, // allow_download
142 base::MessageLoop::current()->Run();
146 content::TestBrowserThreadBundle thread_bundle_
;
147 scoped_refptr
<InfoMap
> extension_info_map_
;
148 net::URLRequestJobFactoryImpl job_factory_
;
149 const net::URLRequestJobFactory
* old_factory_
;
150 net::TestDelegate test_delegate_
;
151 net::TestURLRequestContext test_url_request_context_
;
152 content::MockResourceContext resource_context_
;
155 // Tests that making a chrome-extension request in an incognito context is
156 // only allowed under the right circumstances (if the extension is allowed
157 // in incognito, and it's either a non-main-frame request or a split-mode
159 TEST_F(ExtensionProtocolTest
, IncognitoRequest
) {
160 // Register an incognito extension protocol handler.
161 SetProtocolHandler(true);
166 bool incognito_split_mode
;
167 bool incognito_enabled
;
170 bool should_allow_main_frame_load
;
171 bool should_allow_sub_frame_load
;
173 {"spanning disabled", false, false, false, false},
174 {"split disabled", true, false, false, false},
175 {"spanning enabled", false, true, false, true},
176 {"split enabled", true, true, true, true},
179 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
180 scoped_refptr
<Extension
> extension
=
181 CreateTestExtension(cases
[i
].name
, cases
[i
].incognito_split_mode
);
182 extension_info_map_
->AddExtension(
183 extension
.get(), base::Time::Now(), cases
[i
].incognito_enabled
, false);
185 // First test a main frame request.
187 // It doesn't matter that the resource doesn't exist. If the resource
188 // is blocked, we should see ADDRESS_UNREACHABLE. Otherwise, the request
189 // should just fail because the file doesn't exist.
190 scoped_ptr
<net::URLRequest
> request(
191 resource_context_
.GetRequestContext()->CreateRequest(
192 extension
->GetResourceURL("404.html"),
193 net::DEFAULT_PRIORITY
,
195 StartRequest(request
.get(), content::RESOURCE_TYPE_MAIN_FRAME
);
196 EXPECT_EQ(net::URLRequestStatus::FAILED
, request
->status().status());
198 if (cases
[i
].should_allow_main_frame_load
) {
199 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request
->status().error()) <<
202 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE
, request
->status().error()) <<
207 // Now do a subframe request.
209 scoped_ptr
<net::URLRequest
> request(
210 resource_context_
.GetRequestContext()->CreateRequest(
211 extension
->GetResourceURL("404.html"),
212 net::DEFAULT_PRIORITY
,
214 StartRequest(request
.get(), content::RESOURCE_TYPE_SUB_FRAME
);
215 EXPECT_EQ(net::URLRequestStatus::FAILED
, request
->status().status());
217 if (cases
[i
].should_allow_sub_frame_load
) {
218 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request
->status().error()) <<
221 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE
, request
->status().error()) <<
228 void CheckForContentLengthHeader(net::URLRequest
* request
) {
229 std::string content_length
;
230 request
->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength
,
232 EXPECT_FALSE(content_length
.empty());
233 int length_value
= 0;
234 EXPECT_TRUE(base::StringToInt(content_length
, &length_value
));
235 EXPECT_GT(length_value
, 0);
238 // Tests getting a resource for a component extension works correctly, both when
239 // the extension is enabled and when it is disabled.
240 TEST_F(ExtensionProtocolTest
, ComponentResourceRequest
) {
241 // Register a non-incognito extension protocol handler.
242 SetProtocolHandler(false);
244 scoped_refptr
<Extension
> extension
= CreateWebStoreExtension();
245 extension_info_map_
->AddExtension(extension
.get(),
250 // First test it with the extension enabled.
252 scoped_ptr
<net::URLRequest
> request(
253 resource_context_
.GetRequestContext()->CreateRequest(
254 extension
->GetResourceURL("webstore_icon_16.png"),
255 net::DEFAULT_PRIORITY
,
257 StartRequest(request
.get(), content::RESOURCE_TYPE_MEDIA
);
258 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
->status().status());
259 CheckForContentLengthHeader(request
.get());
262 // And then test it with the extension disabled.
263 extension_info_map_
->RemoveExtension(extension
->id(),
264 UnloadedExtensionInfo::REASON_DISABLE
);
266 scoped_ptr
<net::URLRequest
> request(
267 resource_context_
.GetRequestContext()->CreateRequest(
268 extension
->GetResourceURL("webstore_icon_16.png"),
269 net::DEFAULT_PRIORITY
,
271 StartRequest(request
.get(), content::RESOURCE_TYPE_MEDIA
);
272 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
->status().status());
273 CheckForContentLengthHeader(request
.get());
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.
281 SetProtocolHandler(false);
283 scoped_refptr
<Extension
> extension
= CreateTestResponseHeaderExtension();
284 extension_info_map_
->AddExtension(extension
.get(),
290 scoped_ptr
<net::URLRequest
> request(
291 resource_context_
.GetRequestContext()->CreateRequest(
292 extension
->GetResourceURL("test.dat"),
293 net::DEFAULT_PRIORITY
,
295 StartRequest(request
.get(), content::RESOURCE_TYPE_MEDIA
);
296 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
->status().status());
298 // Check that cache-related headers are set.
300 request
->GetResponseHeaderByName("ETag", &etag
);
301 EXPECT_TRUE(base::StartsWith(etag
, "\"", base::CompareCase::SENSITIVE
));
302 EXPECT_TRUE(base::EndsWith(etag
, "\"", base::CompareCase::SENSITIVE
));
304 std::string revalidation_header
;
305 request
->GetResponseHeaderByName("cache-control", &revalidation_header
);
306 EXPECT_EQ("no-cache", revalidation_header
);
308 // We set test.dat as web-accessible, so it should have a CORS header.
309 std::string access_control
;
310 request
->GetResponseHeaderByName("Access-Control-Allow-Origin",
312 EXPECT_EQ("*", access_control
);
316 // Tests that a URL request for main frame or subframe from an extension
317 // succeeds, but subresources fail. See http://crbug.com/312269.
318 TEST_F(ExtensionProtocolTest
, AllowFrameRequests
) {
319 // Register a non-incognito extension protocol handler.
320 SetProtocolHandler(false);
322 scoped_refptr
<Extension
> extension
= CreateTestExtension("foo", false);
323 extension_info_map_
->AddExtension(extension
.get(),
328 // All MAIN_FRAME and SUB_FRAME requests should succeed.
330 scoped_ptr
<net::URLRequest
> request(
331 resource_context_
.GetRequestContext()->CreateRequest(
332 extension
->GetResourceURL("test.dat"),
333 net::DEFAULT_PRIORITY
,
335 StartRequest(request
.get(), content::RESOURCE_TYPE_MAIN_FRAME
);
336 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
->status().status());
339 scoped_ptr
<net::URLRequest
> request(
340 resource_context_
.GetRequestContext()->CreateRequest(
341 extension
->GetResourceURL("test.dat"),
342 net::DEFAULT_PRIORITY
,
344 StartRequest(request
.get(), content::RESOURCE_TYPE_SUB_FRAME
);
345 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
->status().status());
348 // And subresource types, such as media, should fail.
350 scoped_ptr
<net::URLRequest
> request(
351 resource_context_
.GetRequestContext()->CreateRequest(
352 extension
->GetResourceURL("test.dat"),
353 net::DEFAULT_PRIORITY
,
355 StartRequest(request
.get(), content::RESOURCE_TYPE_MEDIA
);
356 EXPECT_EQ(net::URLRequestStatus::FAILED
, request
->status().status());
360 } // namespace extensions