1 // Copyright (c) 2012 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/message_loop/message_loop.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/values.h"
12 #include "chrome/browser/extensions/extension_protocols.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/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 namespace extensions
{
30 scoped_refptr
<Extension
> CreateTestExtension(const std::string
& name
,
31 bool incognito_split_mode
) {
32 base::DictionaryValue manifest
;
33 manifest
.SetString("name", name
);
34 manifest
.SetString("version", "1");
35 manifest
.SetInteger("manifest_version", 2);
36 manifest
.SetString("incognito", incognito_split_mode
? "split" : "spanning");
39 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &path
));
40 path
= path
.AppendASCII("extensions").AppendASCII("response_headers");
43 scoped_refptr
<Extension
> extension(
44 Extension::Create(path
, Manifest::INTERNAL
, manifest
,
45 Extension::NO_FLAGS
, &error
));
46 EXPECT_TRUE(extension
.get()) << error
;
50 scoped_refptr
<Extension
> CreateWebStoreExtension() {
51 base::DictionaryValue manifest
;
52 manifest
.SetString("name", "WebStore");
53 manifest
.SetString("version", "1");
54 manifest
.SetString("icons.16", "webstore_icon_16.png");
57 EXPECT_TRUE(PathService::Get(chrome::DIR_RESOURCES
, &path
));
58 path
= path
.AppendASCII("web_store");
61 scoped_refptr
<Extension
> extension(
62 Extension::Create(path
, Manifest::COMPONENT
, manifest
,
63 Extension::NO_FLAGS
, &error
));
64 EXPECT_TRUE(extension
.get()) << error
;
68 scoped_refptr
<Extension
> CreateTestResponseHeaderExtension() {
69 base::DictionaryValue manifest
;
70 manifest
.SetString("name", "An extension with web-accessible resources");
71 manifest
.SetString("version", "2");
73 base::ListValue
* web_accessible_list
= new base::ListValue();
74 web_accessible_list
->AppendString("test.dat");
75 manifest
.Set("web_accessible_resources", web_accessible_list
);
78 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &path
));
79 path
= path
.AppendASCII("extensions").AppendASCII("response_headers");
82 scoped_refptr
<Extension
> extension(
83 Extension::Create(path
, Manifest::UNPACKED
, manifest
,
84 Extension::NO_FLAGS
, &error
));
85 EXPECT_TRUE(extension
.get()) << error
;
89 class ExtensionProtocolTest
: public testing::Test
{
91 ExtensionProtocolTest()
92 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
93 resource_context_(&test_url_request_context_
) {}
95 virtual void SetUp() OVERRIDE
{
96 testing::Test::SetUp();
97 extension_info_map_
= new InfoMap();
98 net::URLRequestContext
* request_context
=
99 resource_context_
.GetRequestContext();
100 old_factory_
= request_context
->job_factory();
103 virtual void TearDown() {
104 net::URLRequestContext
* request_context
=
105 resource_context_
.GetRequestContext();
106 request_context
->set_job_factory(old_factory_
);
109 void SetProtocolHandler(bool incognito
) {
110 net::URLRequestContext
* request_context
=
111 resource_context_
.GetRequestContext();
112 job_factory_
.SetProtocolHandler(
114 CreateExtensionProtocolHandler(incognito
, extension_info_map_
.get()));
115 request_context
->set_job_factory(&job_factory_
);
118 void StartRequest(net::URLRequest
* request
,
119 ResourceType::Type resource_type
) {
120 content::ResourceRequestInfo::AllocateForTesting(request
,
128 base::MessageLoop::current()->Run();
132 content::TestBrowserThreadBundle thread_bundle_
;
133 scoped_refptr
<InfoMap
> extension_info_map_
;
134 net::URLRequestJobFactoryImpl job_factory_
;
135 const net::URLRequestJobFactory
* old_factory_
;
136 net::TestDelegate test_delegate_
;
137 net::TestURLRequestContext test_url_request_context_
;
138 content::MockResourceContext resource_context_
;
141 // Tests that making a chrome-extension request in an incognito context is
142 // only allowed under the right circumstances (if the extension is allowed
143 // in incognito, and it's either a non-main-frame request or a split-mode
145 TEST_F(ExtensionProtocolTest
, IncognitoRequest
) {
146 // Register an incognito extension protocol handler.
147 SetProtocolHandler(true);
152 bool incognito_split_mode
;
153 bool incognito_enabled
;
156 bool should_allow_main_frame_load
;
157 bool should_allow_sub_frame_load
;
159 {"spanning disabled", false, false, false, false},
160 {"split disabled", true, false, false, false},
161 {"spanning enabled", false, true, false, true},
162 {"split enabled", true, true, true, true},
165 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(cases
); ++i
) {
166 scoped_refptr
<Extension
> extension
=
167 CreateTestExtension(cases
[i
].name
, cases
[i
].incognito_split_mode
);
168 extension_info_map_
->AddExtension(
169 extension
.get(), base::Time::Now(), cases
[i
].incognito_enabled
, false);
171 // First test a main frame request.
173 // It doesn't matter that the resource doesn't exist. If the resource
174 // is blocked, we should see ADDRESS_UNREACHABLE. Otherwise, the request
175 // should just fail because the file doesn't exist.
176 net::URLRequest
request(extension
->GetResourceURL("404.html"),
177 net::DEFAULT_PRIORITY
,
179 resource_context_
.GetRequestContext());
180 StartRequest(&request
, ResourceType::MAIN_FRAME
);
181 EXPECT_EQ(net::URLRequestStatus::FAILED
, request
.status().status());
183 if (cases
[i
].should_allow_main_frame_load
) {
184 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request
.status().error()) <<
187 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE
, request
.status().error()) <<
192 // Now do a subframe request.
194 net::URLRequest
request(extension
->GetResourceURL("404.html"),
195 net::DEFAULT_PRIORITY
,
197 resource_context_
.GetRequestContext());
198 StartRequest(&request
, ResourceType::SUB_FRAME
);
199 EXPECT_EQ(net::URLRequestStatus::FAILED
, request
.status().status());
201 if (cases
[i
].should_allow_sub_frame_load
) {
202 EXPECT_EQ(net::ERR_FILE_NOT_FOUND
, request
.status().error()) <<
205 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE
, request
.status().error()) <<
212 void CheckForContentLengthHeader(net::URLRequest
* request
) {
213 std::string content_length
;
214 request
->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength
,
216 EXPECT_FALSE(content_length
.empty());
217 int length_value
= 0;
218 EXPECT_TRUE(base::StringToInt(content_length
, &length_value
));
219 EXPECT_GT(length_value
, 0);
222 // Tests getting a resource for a component extension works correctly, both when
223 // the extension is enabled and when it is disabled.
224 TEST_F(ExtensionProtocolTest
, ComponentResourceRequest
) {
225 // Register a non-incognito extension protocol handler.
226 SetProtocolHandler(false);
228 scoped_refptr
<Extension
> extension
= CreateWebStoreExtension();
229 extension_info_map_
->AddExtension(extension
.get(),
234 // First test it with the extension enabled.
236 net::URLRequest
request(extension
->GetResourceURL("webstore_icon_16.png"),
237 net::DEFAULT_PRIORITY
,
239 resource_context_
.GetRequestContext());
240 StartRequest(&request
, ResourceType::MEDIA
);
241 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
.status().status());
242 CheckForContentLengthHeader(&request
);
245 // And then test it with the extension disabled.
246 extension_info_map_
->RemoveExtension(extension
->id(),
247 UnloadedExtensionInfo::REASON_DISABLE
);
249 net::URLRequest
request(extension
->GetResourceURL("webstore_icon_16.png"),
250 net::DEFAULT_PRIORITY
,
252 resource_context_
.GetRequestContext());
253 StartRequest(&request
, ResourceType::MEDIA
);
254 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
.status().status());
255 CheckForContentLengthHeader(&request
);
259 // Tests that a URL request for resource from an extension returns a few
260 // expected response headers.
261 TEST_F(ExtensionProtocolTest
, ResourceRequestResponseHeaders
) {
262 // Register a non-incognito extension protocol handler.
263 SetProtocolHandler(false);
265 scoped_refptr
<Extension
> extension
= CreateTestResponseHeaderExtension();
266 extension_info_map_
->AddExtension(extension
.get(),
272 net::URLRequest
request(extension
->GetResourceURL("test.dat"),
273 net::DEFAULT_PRIORITY
,
275 resource_context_
.GetRequestContext());
276 StartRequest(&request
, ResourceType::MEDIA
);
277 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
.status().status());
279 // Check that cache-related headers are set.
281 request
.GetResponseHeaderByName("ETag", &etag
);
282 EXPECT_TRUE(StartsWithASCII(etag
, "\"", false));
283 EXPECT_TRUE(EndsWith(etag
, "\"", false));
285 std::string revalidation_header
;
286 request
.GetResponseHeaderByName("cache-control", &revalidation_header
);
287 EXPECT_EQ("no-cache", revalidation_header
);
289 // We set test.dat as web-accessible, so it should have a CORS header.
290 std::string access_control
;
291 request
.GetResponseHeaderByName("Access-Control-Allow-Origin",
293 EXPECT_EQ("*", access_control
);
297 // Tests that a URL request for main frame or subframe from an extension
298 // succeeds, but subresources fail. See http://crbug.com/312269.
299 TEST_F(ExtensionProtocolTest
, AllowFrameRequests
) {
300 // Register a non-incognito extension protocol handler.
301 SetProtocolHandler(false);
303 scoped_refptr
<Extension
> extension
= CreateTestExtension("foo", false);
304 extension_info_map_
->AddExtension(extension
.get(),
309 // All MAIN_FRAME and SUB_FRAME requests should succeed.
311 net::URLRequest
request(extension
->GetResourceURL("test.dat"),
312 net::DEFAULT_PRIORITY
,
314 resource_context_
.GetRequestContext());
315 StartRequest(&request
, ResourceType::MAIN_FRAME
);
316 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
.status().status());
319 net::URLRequest
request(extension
->GetResourceURL("test.dat"),
320 net::DEFAULT_PRIORITY
,
322 resource_context_
.GetRequestContext());
323 StartRequest(&request
, ResourceType::SUB_FRAME
);
324 EXPECT_EQ(net::URLRequestStatus::SUCCESS
, request
.status().status());
327 // And subresource types, such as media, should fail.
329 net::URLRequest
request(extension
->GetResourceURL("test.dat"),
330 net::DEFAULT_PRIORITY
,
332 resource_context_
.GetRequestContext());
333 StartRequest(&request
, ResourceType::MEDIA
);
334 EXPECT_EQ(net::URLRequestStatus::FAILED
, request
.status().status());
338 } // namespace extensions