Update path of checkdeps to buildtools checkout
[chromium-blink-merge.git] / extensions / browser / extension_protocols_unittest.cc
blob0aa097bfebb11a409dd6f4328798bd57c6ae60fb
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.
5 #include <string>
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/common/chrome_paths.h"
13 //#include "chrome/common/url_constants.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 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");
38 base::FilePath path;
39 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
40 path = path.AppendASCII("extensions").AppendASCII("response_headers");
42 std::string error;
43 scoped_refptr<Extension> extension(
44 Extension::Create(path, Manifest::INTERNAL, manifest,
45 Extension::NO_FLAGS, &error));
46 EXPECT_TRUE(extension.get()) << error;
47 return extension;
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");
56 base::FilePath path;
57 EXPECT_TRUE(PathService::Get(chrome::DIR_RESOURCES, &path));
58 path = path.AppendASCII("web_store");
60 std::string error;
61 scoped_refptr<Extension> extension(
62 Extension::Create(path, Manifest::COMPONENT, manifest,
63 Extension::NO_FLAGS, &error));
64 EXPECT_TRUE(extension.get()) << error;
65 return extension;
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);
77 base::FilePath path;
78 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
79 path = path.AppendASCII("extensions").AppendASCII("response_headers");
81 std::string error;
82 scoped_refptr<Extension> extension(
83 Extension::Create(path, Manifest::UNPACKED, manifest,
84 Extension::NO_FLAGS, &error));
85 EXPECT_TRUE(extension.get()) << error;
86 return extension;
89 class ExtensionProtocolTest : public testing::Test {
90 public:
91 ExtensionProtocolTest()
92 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
93 old_factory_(NULL),
94 resource_context_(&test_url_request_context_) {}
96 virtual void SetUp() OVERRIDE {
97 testing::Test::SetUp();
98 extension_info_map_ = new InfoMap();
99 net::URLRequestContext* request_context =
100 resource_context_.GetRequestContext();
101 old_factory_ = request_context->job_factory();
104 virtual void TearDown() {
105 net::URLRequestContext* request_context =
106 resource_context_.GetRequestContext();
107 request_context->set_job_factory(old_factory_);
110 void SetProtocolHandler(bool is_incognito) {
111 net::URLRequestContext* request_context =
112 resource_context_.GetRequestContext();
113 job_factory_.SetProtocolHandler(
114 kExtensionScheme,
115 CreateExtensionProtocolHandler(is_incognito,
116 extension_info_map_.get()));
117 request_context->set_job_factory(&job_factory_);
120 void StartRequest(net::URLRequest* request,
121 ResourceType::Type resource_type) {
122 content::ResourceRequestInfo::AllocateForTesting(request,
123 resource_type,
124 &resource_context_,
128 false);
129 request->Start();
130 base::MessageLoop::current()->Run();
133 protected:
134 content::TestBrowserThreadBundle thread_bundle_;
135 scoped_refptr<InfoMap> extension_info_map_;
136 net::URLRequestJobFactoryImpl job_factory_;
137 const net::URLRequestJobFactory* old_factory_;
138 net::TestDelegate test_delegate_;
139 net::TestURLRequestContext test_url_request_context_;
140 content::MockResourceContext resource_context_;
143 // Tests that making a chrome-extension request in an incognito context is
144 // only allowed under the right circumstances (if the extension is allowed
145 // in incognito, and it's either a non-main-frame request or a split-mode
146 // extension).
147 TEST_F(ExtensionProtocolTest, IncognitoRequest) {
148 // Register an incognito extension protocol handler.
149 SetProtocolHandler(true);
151 struct TestCase {
152 // Inputs.
153 std::string name;
154 bool incognito_split_mode;
155 bool incognito_enabled;
157 // Expected results.
158 bool should_allow_main_frame_load;
159 bool should_allow_sub_frame_load;
160 } cases[] = {
161 {"spanning disabled", false, false, false, false},
162 {"split disabled", true, false, false, false},
163 {"spanning enabled", false, true, false, true},
164 {"split enabled", true, true, true, true},
167 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
168 scoped_refptr<Extension> extension =
169 CreateTestExtension(cases[i].name, cases[i].incognito_split_mode);
170 extension_info_map_->AddExtension(
171 extension.get(), base::Time::Now(), cases[i].incognito_enabled, false);
173 // First test a main frame request.
175 // It doesn't matter that the resource doesn't exist. If the resource
176 // is blocked, we should see ADDRESS_UNREACHABLE. Otherwise, the request
177 // should just fail because the file doesn't exist.
178 net::URLRequest request(extension->GetResourceURL("404.html"),
179 net::DEFAULT_PRIORITY,
180 &test_delegate_,
181 resource_context_.GetRequestContext());
182 StartRequest(&request, ResourceType::MAIN_FRAME);
183 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status());
185 if (cases[i].should_allow_main_frame_load) {
186 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error()) <<
187 cases[i].name;
188 } else {
189 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request.status().error()) <<
190 cases[i].name;
194 // Now do a subframe request.
196 net::URLRequest request(extension->GetResourceURL("404.html"),
197 net::DEFAULT_PRIORITY,
198 &test_delegate_,
199 resource_context_.GetRequestContext());
200 StartRequest(&request, ResourceType::SUB_FRAME);
201 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status());
203 if (cases[i].should_allow_sub_frame_load) {
204 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error()) <<
205 cases[i].name;
206 } else {
207 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request.status().error()) <<
208 cases[i].name;
214 void CheckForContentLengthHeader(net::URLRequest* request) {
215 std::string content_length;
216 request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength,
217 &content_length);
218 EXPECT_FALSE(content_length.empty());
219 int length_value = 0;
220 EXPECT_TRUE(base::StringToInt(content_length, &length_value));
221 EXPECT_GT(length_value, 0);
224 // Tests getting a resource for a component extension works correctly, both when
225 // the extension is enabled and when it is disabled.
226 TEST_F(ExtensionProtocolTest, ComponentResourceRequest) {
227 // Register a non-incognito extension protocol handler.
228 SetProtocolHandler(false);
230 scoped_refptr<Extension> extension = CreateWebStoreExtension();
231 extension_info_map_->AddExtension(extension.get(),
232 base::Time::Now(),
233 false,
234 false);
236 // First test it with the extension enabled.
238 net::URLRequest request(extension->GetResourceURL("webstore_icon_16.png"),
239 net::DEFAULT_PRIORITY,
240 &test_delegate_,
241 resource_context_.GetRequestContext());
242 StartRequest(&request, ResourceType::MEDIA);
243 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status());
244 CheckForContentLengthHeader(&request);
247 // And then test it with the extension disabled.
248 extension_info_map_->RemoveExtension(extension->id(),
249 UnloadedExtensionInfo::REASON_DISABLE);
251 net::URLRequest request(extension->GetResourceURL("webstore_icon_16.png"),
252 net::DEFAULT_PRIORITY,
253 &test_delegate_,
254 resource_context_.GetRequestContext());
255 StartRequest(&request, ResourceType::MEDIA);
256 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status());
257 CheckForContentLengthHeader(&request);
261 // Tests that a URL request for resource from an extension returns a few
262 // expected response headers.
263 TEST_F(ExtensionProtocolTest, ResourceRequestResponseHeaders) {
264 // Register a non-incognito extension protocol handler.
265 SetProtocolHandler(false);
267 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension();
268 extension_info_map_->AddExtension(extension.get(),
269 base::Time::Now(),
270 false,
271 false);
274 net::URLRequest request(extension->GetResourceURL("test.dat"),
275 net::DEFAULT_PRIORITY,
276 &test_delegate_,
277 resource_context_.GetRequestContext());
278 StartRequest(&request, ResourceType::MEDIA);
279 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status());
281 // Check that cache-related headers are set.
282 std::string etag;
283 request.GetResponseHeaderByName("ETag", &etag);
284 EXPECT_TRUE(StartsWithASCII(etag, "\"", false));
285 EXPECT_TRUE(EndsWith(etag, "\"", false));
287 std::string revalidation_header;
288 request.GetResponseHeaderByName("cache-control", &revalidation_header);
289 EXPECT_EQ("no-cache", revalidation_header);
291 // We set test.dat as web-accessible, so it should have a CORS header.
292 std::string access_control;
293 request.GetResponseHeaderByName("Access-Control-Allow-Origin",
294 &access_control);
295 EXPECT_EQ("*", access_control);
299 // Tests that a URL request for main frame or subframe from an extension
300 // succeeds, but subresources fail. See http://crbug.com/312269.
301 TEST_F(ExtensionProtocolTest, AllowFrameRequests) {
302 // Register a non-incognito extension protocol handler.
303 SetProtocolHandler(false);
305 scoped_refptr<Extension> extension = CreateTestExtension("foo", false);
306 extension_info_map_->AddExtension(extension.get(),
307 base::Time::Now(),
308 false,
309 false);
311 // All MAIN_FRAME and SUB_FRAME requests should succeed.
313 net::URLRequest request(extension->GetResourceURL("test.dat"),
314 net::DEFAULT_PRIORITY,
315 &test_delegate_,
316 resource_context_.GetRequestContext());
317 StartRequest(&request, ResourceType::MAIN_FRAME);
318 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status());
321 net::URLRequest request(extension->GetResourceURL("test.dat"),
322 net::DEFAULT_PRIORITY,
323 &test_delegate_,
324 resource_context_.GetRequestContext());
325 StartRequest(&request, ResourceType::SUB_FRAME);
326 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status());
329 // And subresource types, such as media, should fail.
331 net::URLRequest request(extension->GetResourceURL("test.dat"),
332 net::DEFAULT_PRIORITY,
333 &test_delegate_,
334 resource_context_.GetRequestContext());
335 StartRequest(&request, ResourceType::MEDIA);
336 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status());
340 } // namespace extensions