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/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 "chrome/common/url_constants.h"
15 #include "content/public/browser/resource_request_info.h"
16 #include "content/public/test/mock_resource_context.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "extensions/browser/extension_protocols.h"
19 #include "extensions/browser/info_map.h"
20 #include "extensions/common/constants.h"
21 #include "extensions/common/extension.h"
22 #include "net/base/request_priority.h"
23 #include "net/url_request/url_request.h"
24 #include "net/url_request/url_request_job_factory_impl.h"
25 #include "net/url_request/url_request_status.h"
26 #include "net/url_request/url_request_test_util.h"
27 #include "testing/gtest/include/gtest/gtest.h"
29 using content::ResourceType
;
31 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
;
92 class ExtensionProtocolTest
: public testing::Test
{
94 ExtensionProtocolTest()
95 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
97 resource_context_(&test_url_request_context_
) {}
99 virtual void SetUp() OVERRIDE
{
100 testing::Test::SetUp();
101 extension_info_map_
= new InfoMap();
102 net::URLRequestContext
* request_context
=
103 resource_context_
.GetRequestContext();
104 old_factory_
= request_context
->job_factory();
107 virtual void TearDown() {
108 net::URLRequestContext
* request_context
=
109 resource_context_
.GetRequestContext();
110 request_context
->set_job_factory(old_factory_
);
113 void SetProtocolHandler(bool is_incognito
) {
114 net::URLRequestContext
* request_context
=
115 resource_context_
.GetRequestContext();
116 job_factory_
.SetProtocolHandler(
118 CreateExtensionProtocolHandler(is_incognito
,
119 extension_info_map_
.get()));
120 request_context
->set_job_factory(&job_factory_
);
123 void StartRequest(net::URLRequest
* request
,
124 ResourceType resource_type
) {
125 content::ResourceRequestInfo::AllocateForTesting(request
,
133 base::MessageLoop::current()->Run();
137 content::TestBrowserThreadBundle thread_bundle_
;
138 scoped_refptr
<InfoMap
> extension_info_map_
;
139 net::URLRequestJobFactoryImpl job_factory_
;
140 const net::URLRequestJobFactory
* old_factory_
;
141 net::TestDelegate test_delegate_
;
142 net::TestURLRequestContext test_url_request_context_
;
143 content::MockResourceContext resource_context_
;
146 // Tests that making a chrome-extension request in an incognito context is
147 // only allowed under the right circumstances (if the extension is allowed
148 // in incognito, and it's either a non-main-frame request or a split-mode
150 TEST_F(ExtensionProtocolTest
, IncognitoRequest
) {
151 // Register an incognito extension protocol handler.
152 SetProtocolHandler(true);
157 bool incognito_split_mode
;
158 bool incognito_enabled
;
161 bool should_allow_main_frame_load
;
162 bool should_allow_sub_frame_load
;
164 {"spanning disabled", false, false, false, false},
165 {"split disabled", true, false, false, false},
166 {"spanning enabled", false, true, false, true},
167 {"split enabled", true, true, true, true},
170 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
171 scoped_refptr
<Extension
> extension
=
172 CreateTestExtension(cases
[i
].name
, cases
[i
].incognito_split_mode
);
173 extension_info_map_
->AddExtension(
174 extension
.get(), base::Time::Now(), cases
[i
].incognito_enabled
, false);
176 // First test a main frame request.
178 // It doesn't matter that the resource doesn't exist. If the resource
179 // is blocked, we should see ADDRESS_UNREACHABLE. Otherwise, the request
180 // should just fail because the file doesn't exist.
181 scoped_ptr
<net::URLRequest
> request(
182 resource_context_
.GetRequestContext()->CreateRequest(
183 extension
->GetResourceURL("404.html"),
184 net::DEFAULT_PRIORITY
,
187 StartRequest(request
.get(), content::RESOURCE_TYPE_MAIN_FRAME
);
188 EXPECT_EQ(net::URLRequestStatus::FAILED
, request
->status().status());
190 if (cases
[i
].should_allow_main_frame_load
) {
191 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request
->status().error()) <<
194 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE
, request
->status().error()) <<
199 // Now do a subframe request.
201 scoped_ptr
<net::URLRequest
> request(
202 resource_context_
.GetRequestContext()->CreateRequest(
203 extension
->GetResourceURL("404.html"),
204 net::DEFAULT_PRIORITY
,
207 StartRequest(request
.get(), content::RESOURCE_TYPE_SUB_FRAME
);
208 EXPECT_EQ(net::URLRequestStatus::FAILED
, request
->status().status());
210 if (cases
[i
].should_allow_sub_frame_load
) {
211 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request
->status().error()) <<
214 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE
, request
->status().error()) <<
221 void CheckForContentLengthHeader(net::URLRequest
* request
) {
222 std::string content_length
;
223 request
->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength
,
225 EXPECT_FALSE(content_length
.empty());
226 int length_value
= 0;
227 EXPECT_TRUE(base::StringToInt(content_length
, &length_value
));
228 EXPECT_GT(length_value
, 0);
231 // Tests getting a resource for a component extension works correctly, both when
232 // the extension is enabled and when it is disabled.
233 TEST_F(ExtensionProtocolTest
, ComponentResourceRequest
) {
234 // Register a non-incognito extension protocol handler.
235 SetProtocolHandler(false);
237 scoped_refptr
<Extension
> extension
= CreateWebStoreExtension();
238 extension_info_map_
->AddExtension(extension
.get(),
243 // First test it with the extension enabled.
245 scoped_ptr
<net::URLRequest
> request(
246 resource_context_
.GetRequestContext()->CreateRequest(
247 extension
->GetResourceURL("webstore_icon_16.png"),
248 net::DEFAULT_PRIORITY
,
251 StartRequest(request
.get(), content::RESOURCE_TYPE_MEDIA
);
252 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
->status().status());
253 CheckForContentLengthHeader(request
.get());
256 // And then test it with the extension disabled.
257 extension_info_map_
->RemoveExtension(extension
->id(),
258 UnloadedExtensionInfo::REASON_DISABLE
);
260 scoped_ptr
<net::URLRequest
> request(
261 resource_context_
.GetRequestContext()->CreateRequest(
262 extension
->GetResourceURL("webstore_icon_16.png"),
263 net::DEFAULT_PRIORITY
,
266 StartRequest(request
.get(), content::RESOURCE_TYPE_MEDIA
);
267 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
->status().status());
268 CheckForContentLengthHeader(request
.get());
272 // Tests that a URL request for resource from an extension returns a few
273 // expected response headers.
274 TEST_F(ExtensionProtocolTest
, ResourceRequestResponseHeaders
) {
275 // Register a non-incognito extension protocol handler.
276 SetProtocolHandler(false);
278 scoped_refptr
<Extension
> extension
= CreateTestResponseHeaderExtension();
279 extension_info_map_
->AddExtension(extension
.get(),
285 scoped_ptr
<net::URLRequest
> request(
286 resource_context_
.GetRequestContext()->CreateRequest(
287 extension
->GetResourceURL("test.dat"),
288 net::DEFAULT_PRIORITY
,
291 StartRequest(request
.get(), content::RESOURCE_TYPE_MEDIA
);
292 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
->status().status());
294 // Check that cache-related headers are set.
296 request
->GetResponseHeaderByName("ETag", &etag
);
297 EXPECT_TRUE(StartsWithASCII(etag
, "\"", false));
298 EXPECT_TRUE(EndsWith(etag
, "\"", false));
300 std::string revalidation_header
;
301 request
->GetResponseHeaderByName("cache-control", &revalidation_header
);
302 EXPECT_EQ("no-cache", revalidation_header
);
304 // We set test.dat as web-accessible, so it should have a CORS header.
305 std::string access_control
;
306 request
->GetResponseHeaderByName("Access-Control-Allow-Origin",
308 EXPECT_EQ("*", access_control
);
312 // Tests that a URL request for main frame or subframe from an extension
313 // succeeds, but subresources fail. See http://crbug.com/312269.
314 TEST_F(ExtensionProtocolTest
, AllowFrameRequests
) {
315 // Register a non-incognito extension protocol handler.
316 SetProtocolHandler(false);
318 scoped_refptr
<Extension
> extension
= CreateTestExtension("foo", false);
319 extension_info_map_
->AddExtension(extension
.get(),
324 // All MAIN_FRAME and SUB_FRAME requests should succeed.
326 scoped_ptr
<net::URLRequest
> request(
327 resource_context_
.GetRequestContext()->CreateRequest(
328 extension
->GetResourceURL("test.dat"),
329 net::DEFAULT_PRIORITY
,
332 StartRequest(request
.get(), content::RESOURCE_TYPE_MAIN_FRAME
);
333 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
->status().status());
336 scoped_ptr
<net::URLRequest
> request(
337 resource_context_
.GetRequestContext()->CreateRequest(
338 extension
->GetResourceURL("test.dat"),
339 net::DEFAULT_PRIORITY
,
342 StartRequest(request
.get(), content::RESOURCE_TYPE_SUB_FRAME
);
343 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
->status().status());
346 // And subresource types, such as media, should fail.
348 scoped_ptr
<net::URLRequest
> request(
349 resource_context_
.GetRequestContext()->CreateRequest(
350 extension
->GetResourceURL("test.dat"),
351 net::DEFAULT_PRIORITY
,
354 StartRequest(request
.get(), content::RESOURCE_TYPE_MEDIA
);
355 EXPECT_EQ(net::URLRequestStatus::FAILED
, request
->status().status());
359 } // namespace extensions