| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 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" | |
| 28 | |
| 29 using content::ResourceType; | |
| 30 | |
| 31 namespace extensions { | |
| 32 | |
| 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"); | |
| 40 | |
| 41 base::FilePath path; | |
| 42 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | |
| 43 path = path.AppendASCII("extensions").AppendASCII("response_headers"); | |
| 44 | |
| 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; | |
| 51 } | |
| 52 | |
| 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"); | |
| 58 | |
| 59 base::FilePath path; | |
| 60 EXPECT_TRUE(PathService::Get(chrome::DIR_RESOURCES, &path)); | |
| 61 path = path.AppendASCII("web_store"); | |
| 62 | |
| 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; | |
| 69 } | |
| 70 | |
| 71 scoped_refptr<Extension> CreateTestResponseHeaderExtension() { | |
| 72 base::DictionaryValue manifest; | |
| 73 manifest.SetString("name", "An extension with web-accessible resources"); | |
| 74 manifest.SetString("version", "2"); | |
| 75 | |
| 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); | |
| 79 | |
| 80 base::FilePath path; | |
| 81 EXPECT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &path)); | |
| 82 path = path.AppendASCII("extensions").AppendASCII("response_headers"); | |
| 83 | |
| 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; | |
| 90 } | |
| 91 | |
| 92 class ExtensionProtocolTest : public testing::Test { | |
| 93 public: | |
| 94 ExtensionProtocolTest() | |
| 95 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | |
| 96 old_factory_(NULL), | |
| 97 resource_context_(&test_url_request_context_) {} | |
| 98 | |
| 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(); | |
| 105 } | |
| 106 | |
| 107 virtual void TearDown() { | |
| 108 net::URLRequestContext* request_context = | |
| 109 resource_context_.GetRequestContext(); | |
| 110 request_context->set_job_factory(old_factory_); | |
| 111 } | |
| 112 | |
| 113 void SetProtocolHandler(bool is_incognito) { | |
| 114 net::URLRequestContext* request_context = | |
| 115 resource_context_.GetRequestContext(); | |
| 116 job_factory_.SetProtocolHandler( | |
| 117 kExtensionScheme, | |
| 118 CreateExtensionProtocolHandler(is_incognito, | |
| 119 extension_info_map_.get())); | |
| 120 request_context->set_job_factory(&job_factory_); | |
| 121 } | |
| 122 | |
| 123 void StartRequest(net::URLRequest* request, | |
| 124 ResourceType resource_type) { | |
| 125 content::ResourceRequestInfo::AllocateForTesting(request, | |
| 126 resource_type, | |
| 127 &resource_context_, | |
| 128 -1, | |
| 129 -1, | |
| 130 -1, | |
| 131 false); | |
| 132 request->Start(); | |
| 133 base::MessageLoop::current()->Run(); | |
| 134 } | |
| 135 | |
| 136 protected: | |
| 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_; | |
| 144 }; | |
| 145 | |
| 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 | |
| 149 // extension). | |
| 150 TEST_F(ExtensionProtocolTest, IncognitoRequest) { | |
| 151 // Register an incognito extension protocol handler. | |
| 152 SetProtocolHandler(true); | |
| 153 | |
| 154 struct TestCase { | |
| 155 // Inputs. | |
| 156 std::string name; | |
| 157 bool incognito_split_mode; | |
| 158 bool incognito_enabled; | |
| 159 | |
| 160 // Expected results. | |
| 161 bool should_allow_main_frame_load; | |
| 162 bool should_allow_sub_frame_load; | |
| 163 } cases[] = { | |
| 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}, | |
| 168 }; | |
| 169 | |
| 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); | |
| 175 | |
| 176 // First test a main frame request. | |
| 177 { | |
| 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, | |
| 185 &test_delegate_, | |
| 186 NULL)); | |
| 187 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); | |
| 188 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | |
| 189 | |
| 190 if (cases[i].should_allow_main_frame_load) { | |
| 191 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()) << | |
| 192 cases[i].name; | |
| 193 } else { | |
| 194 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request->status().error()) << | |
| 195 cases[i].name; | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 // Now do a subframe request. | |
| 200 { | |
| 201 scoped_ptr<net::URLRequest> request( | |
| 202 resource_context_.GetRequestContext()->CreateRequest( | |
| 203 extension->GetResourceURL("404.html"), | |
| 204 net::DEFAULT_PRIORITY, | |
| 205 &test_delegate_, | |
| 206 NULL)); | |
| 207 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME); | |
| 208 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | |
| 209 | |
| 210 if (cases[i].should_allow_sub_frame_load) { | |
| 211 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()) << | |
| 212 cases[i].name; | |
| 213 } else { | |
| 214 EXPECT_EQ(net::ERR_ADDRESS_UNREACHABLE, request->status().error()) << | |
| 215 cases[i].name; | |
| 216 } | |
| 217 } | |
| 218 } | |
| 219 } | |
| 220 | |
| 221 void CheckForContentLengthHeader(net::URLRequest* request) { | |
| 222 std::string content_length; | |
| 223 request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength, | |
| 224 &content_length); | |
| 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); | |
| 229 } | |
| 230 | |
| 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); | |
| 236 | |
| 237 scoped_refptr<Extension> extension = CreateWebStoreExtension(); | |
| 238 extension_info_map_->AddExtension(extension.get(), | |
| 239 base::Time::Now(), | |
| 240 false, | |
| 241 false); | |
| 242 | |
| 243 // First test it with the extension enabled. | |
| 244 { | |
| 245 scoped_ptr<net::URLRequest> request( | |
| 246 resource_context_.GetRequestContext()->CreateRequest( | |
| 247 extension->GetResourceURL("webstore_icon_16.png"), | |
| 248 net::DEFAULT_PRIORITY, | |
| 249 &test_delegate_, | |
| 250 NULL)); | |
| 251 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); | |
| 252 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | |
| 253 CheckForContentLengthHeader(request.get()); | |
| 254 } | |
| 255 | |
| 256 // And then test it with the extension disabled. | |
| 257 extension_info_map_->RemoveExtension(extension->id(), | |
| 258 UnloadedExtensionInfo::REASON_DISABLE); | |
| 259 { | |
| 260 scoped_ptr<net::URLRequest> request( | |
| 261 resource_context_.GetRequestContext()->CreateRequest( | |
| 262 extension->GetResourceURL("webstore_icon_16.png"), | |
| 263 net::DEFAULT_PRIORITY, | |
| 264 &test_delegate_, | |
| 265 NULL)); | |
| 266 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); | |
| 267 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | |
| 268 CheckForContentLengthHeader(request.get()); | |
| 269 } | |
| 270 } | |
| 271 | |
| 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); | |
| 277 | |
| 278 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension(); | |
| 279 extension_info_map_->AddExtension(extension.get(), | |
| 280 base::Time::Now(), | |
| 281 false, | |
| 282 false); | |
| 283 | |
| 284 { | |
| 285 scoped_ptr<net::URLRequest> request( | |
| 286 resource_context_.GetRequestContext()->CreateRequest( | |
| 287 extension->GetResourceURL("test.dat"), | |
| 288 net::DEFAULT_PRIORITY, | |
| 289 &test_delegate_, | |
| 290 NULL)); | |
| 291 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); | |
| 292 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | |
| 293 | |
| 294 // Check that cache-related headers are set. | |
| 295 std::string etag; | |
| 296 request->GetResponseHeaderByName("ETag", &etag); | |
| 297 EXPECT_TRUE(StartsWithASCII(etag, "\"", false)); | |
| 298 EXPECT_TRUE(EndsWith(etag, "\"", false)); | |
| 299 | |
| 300 std::string revalidation_header; | |
| 301 request->GetResponseHeaderByName("cache-control", &revalidation_header); | |
| 302 EXPECT_EQ("no-cache", revalidation_header); | |
| 303 | |
| 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", | |
| 307 &access_control); | |
| 308 EXPECT_EQ("*", access_control); | |
| 309 } | |
| 310 } | |
| 311 | |
| 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); | |
| 317 | |
| 318 scoped_refptr<Extension> extension = CreateTestExtension("foo", false); | |
| 319 extension_info_map_->AddExtension(extension.get(), | |
| 320 base::Time::Now(), | |
| 321 false, | |
| 322 false); | |
| 323 | |
| 324 // All MAIN_FRAME and SUB_FRAME requests should succeed. | |
| 325 { | |
| 326 scoped_ptr<net::URLRequest> request( | |
| 327 resource_context_.GetRequestContext()->CreateRequest( | |
| 328 extension->GetResourceURL("test.dat"), | |
| 329 net::DEFAULT_PRIORITY, | |
| 330 &test_delegate_, | |
| 331 NULL)); | |
| 332 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); | |
| 333 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | |
| 334 } | |
| 335 { | |
| 336 scoped_ptr<net::URLRequest> request( | |
| 337 resource_context_.GetRequestContext()->CreateRequest( | |
| 338 extension->GetResourceURL("test.dat"), | |
| 339 net::DEFAULT_PRIORITY, | |
| 340 &test_delegate_, | |
| 341 NULL)); | |
| 342 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME); | |
| 343 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | |
| 344 } | |
| 345 | |
| 346 // And subresource types, such as media, should fail. | |
| 347 { | |
| 348 scoped_ptr<net::URLRequest> request( | |
| 349 resource_context_.GetRequestContext()->CreateRequest( | |
| 350 extension->GetResourceURL("test.dat"), | |
| 351 net::DEFAULT_PRIORITY, | |
| 352 &test_delegate_, | |
| 353 NULL)); | |
| 354 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); | |
| 355 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | |
| 356 } | |
| 357 } | |
| 358 | |
| 359 } // namespace extensions | |
| OLD | NEW |