ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / extensions / extension_protocols_unittest.cc
blobd641e717ba1f58bc1b05df8f03b4bf76d07eaa80
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/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 {
31 namespace {
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");
41 base::FilePath path;
42 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
43 path = path.AppendASCII("extensions").AppendASCII("response_headers");
45 std::string error;
46 scoped_refptr<Extension> extension(
47 Extension::Create(path, Manifest::INTERNAL, manifest,
48 Extension::NO_FLAGS, &error));
49 EXPECT_TRUE(extension.get()) << error;
50 return extension;
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");
59 base::FilePath path;
60 EXPECT_TRUE(PathService::Get(chrome::DIR_RESOURCES, &path));
61 path = path.AppendASCII("web_store");
63 std::string error;
64 scoped_refptr<Extension> extension(
65 Extension::Create(path, Manifest::COMPONENT, manifest,
66 Extension::NO_FLAGS, &error));
67 EXPECT_TRUE(extension.get()) << error;
68 return extension;
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);
80 base::FilePath path;
81 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path));
82 path = path.AppendASCII("extensions").AppendASCII("response_headers");
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;
92 } // namespace
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 {
98 public:
99 ExtensionProtocolTest()
100 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP),
101 old_factory_(NULL),
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(
122 kExtensionScheme,
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(
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 true, // allow_download
140 false); // is_async
141 request->Start();
142 base::MessageLoop::current()->Run();
145 protected:
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
158 // extension).
159 TEST_F(ExtensionProtocolTest, IncognitoRequest) {
160 // Register an incognito extension protocol handler.
161 SetProtocolHandler(true);
163 struct TestCase {
164 // Inputs.
165 std::string name;
166 bool incognito_split_mode;
167 bool incognito_enabled;
169 // Expected results.
170 bool should_allow_main_frame_load;
171 bool should_allow_sub_frame_load;
172 } cases[] = {
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,
194 &test_delegate_,
195 NULL));
196 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME);
197 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
199 if (cases[i].should_allow_main_frame_load) {
200 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()) <<
201 cases[i].name;
202 } else {
203 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request->status().error()) <<
204 cases[i].name;
208 // Now do a subframe request.
210 scoped_ptr<net::URLRequest> request(
211 resource_context_.GetRequestContext()->CreateRequest(
212 extension->GetResourceURL("404.html"),
213 net::DEFAULT_PRIORITY,
214 &test_delegate_,
215 NULL));
216 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME);
217 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
219 if (cases[i].should_allow_sub_frame_load) {
220 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()) <<
221 cases[i].name;
222 } else {
223 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request->status().error()) <<
224 cases[i].name;
230 void CheckForContentLengthHeader(net::URLRequest* request) {
231 std::string content_length;
232 request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength,
233 &content_length);
234 EXPECT_FALSE(content_length.empty());
235 int length_value = 0;
236 EXPECT_TRUE(base::StringToInt(content_length, &length_value));
237 EXPECT_GT(length_value, 0);
240 // Tests getting a resource for a component extension works correctly, both when
241 // the extension is enabled and when it is disabled.
242 TEST_F(ExtensionProtocolTest, ComponentResourceRequest) {
243 // Register a non-incognito extension protocol handler.
244 SetProtocolHandler(false);
246 scoped_refptr<Extension> extension = CreateWebStoreExtension();
247 extension_info_map_->AddExtension(extension.get(),
248 base::Time::Now(),
249 false,
250 false);
252 // First test it with the extension enabled.
254 scoped_ptr<net::URLRequest> request(
255 resource_context_.GetRequestContext()->CreateRequest(
256 extension->GetResourceURL("webstore_icon_16.png"),
257 net::DEFAULT_PRIORITY,
258 &test_delegate_,
259 NULL));
260 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
261 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
262 CheckForContentLengthHeader(request.get());
265 // And then test it with the extension disabled.
266 extension_info_map_->RemoveExtension(extension->id(),
267 UnloadedExtensionInfo::REASON_DISABLE);
269 scoped_ptr<net::URLRequest> request(
270 resource_context_.GetRequestContext()->CreateRequest(
271 extension->GetResourceURL("webstore_icon_16.png"),
272 net::DEFAULT_PRIORITY,
273 &test_delegate_,
274 NULL));
275 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
276 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
277 CheckForContentLengthHeader(request.get());
281 // Tests that a URL request for resource from an extension returns a few
282 // expected response headers.
283 TEST_F(ExtensionProtocolTest, ResourceRequestResponseHeaders) {
284 // Register a non-incognito extension protocol handler.
285 SetProtocolHandler(false);
287 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension();
288 extension_info_map_->AddExtension(extension.get(),
289 base::Time::Now(),
290 false,
291 false);
294 scoped_ptr<net::URLRequest> request(
295 resource_context_.GetRequestContext()->CreateRequest(
296 extension->GetResourceURL("test.dat"),
297 net::DEFAULT_PRIORITY,
298 &test_delegate_,
299 NULL));
300 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
301 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
303 // Check that cache-related headers are set.
304 std::string etag;
305 request->GetResponseHeaderByName("ETag", &etag);
306 EXPECT_TRUE(StartsWithASCII(etag, "\"", false));
307 EXPECT_TRUE(EndsWith(etag, "\"", false));
309 std::string revalidation_header;
310 request->GetResponseHeaderByName("cache-control", &revalidation_header);
311 EXPECT_EQ("no-cache", revalidation_header);
313 // We set test.dat as web-accessible, so it should have a CORS header.
314 std::string access_control;
315 request->GetResponseHeaderByName("Access-Control-Allow-Origin",
316 &access_control);
317 EXPECT_EQ("*", access_control);
321 // Tests that a URL request for main frame or subframe from an extension
322 // succeeds, but subresources fail. See http://crbug.com/312269.
323 TEST_F(ExtensionProtocolTest, AllowFrameRequests) {
324 // Register a non-incognito extension protocol handler.
325 SetProtocolHandler(false);
327 scoped_refptr<Extension> extension = CreateTestExtension("foo", false);
328 extension_info_map_->AddExtension(extension.get(),
329 base::Time::Now(),
330 false,
331 false);
333 // All MAIN_FRAME and SUB_FRAME requests should succeed.
335 scoped_ptr<net::URLRequest> request(
336 resource_context_.GetRequestContext()->CreateRequest(
337 extension->GetResourceURL("test.dat"),
338 net::DEFAULT_PRIORITY,
339 &test_delegate_,
340 NULL));
341 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME);
342 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
345 scoped_ptr<net::URLRequest> request(
346 resource_context_.GetRequestContext()->CreateRequest(
347 extension->GetResourceURL("test.dat"),
348 net::DEFAULT_PRIORITY,
349 &test_delegate_,
350 NULL));
351 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME);
352 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status());
355 // And subresource types, such as media, should fail.
357 scoped_ptr<net::URLRequest> request(
358 resource_context_.GetRequestContext()->CreateRequest(
359 extension->GetResourceURL("test.dat"),
360 net::DEFAULT_PRIORITY,
361 &test_delegate_,
362 NULL));
363 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA);
364 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status());
368 } // namespace extensions