| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 true, // allow_download | 146 true, // allow_download |
| 147 false, // is_async | 147 false, // is_async |
| 148 false); // is_using_lofi | 148 false); // is_using_lofi |
| 149 request->Start(); | 149 request->Start(); |
| 150 base::RunLoop().Run(); | 150 base::RunLoop().Run(); |
| 151 } | 151 } |
| 152 | 152 |
| 153 // Helper method to create a URLRequest, call StartRequest on it, and return | 153 // Helper method to create a URLRequest, call StartRequest on it, and return |
| 154 // the result. If |extension| hasn't already been added to | 154 // the result. If |extension| hasn't already been added to |
| 155 // |extension_info_map_|, this will add it. | 155 // |extension_info_map_|, this will add it. |
| 156 net::URLRequestStatus::Status DoRequest(const Extension& extension, | 156 int DoRequest(const Extension& extension, const std::string& relative_path) { |
| 157 const std::string& relative_path) { | |
| 158 if (!extension_info_map_->extensions().Contains(extension.id())) { | 157 if (!extension_info_map_->extensions().Contains(extension.id())) { |
| 159 extension_info_map_->AddExtension(&extension, | 158 extension_info_map_->AddExtension(&extension, |
| 160 base::Time::Now(), | 159 base::Time::Now(), |
| 161 false, // incognito_enabled | 160 false, // incognito_enabled |
| 162 false); // notifications_disabled | 161 false); // notifications_disabled |
| 163 } | 162 } |
| 164 std::unique_ptr<net::URLRequest> request( | 163 std::unique_ptr<net::URLRequest> request( |
| 165 resource_context_.GetRequestContext()->CreateRequest( | 164 resource_context_.GetRequestContext()->CreateRequest( |
| 166 extension.GetResourceURL(relative_path), net::DEFAULT_PRIORITY, | 165 extension.GetResourceURL(relative_path), net::DEFAULT_PRIORITY, |
| 167 &test_delegate_)); | 166 &test_delegate_)); |
| 168 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); | 167 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); |
| 169 return request->status().status(); | 168 return test_delegate_.request_status(); |
| 170 } | 169 } |
| 171 | 170 |
| 172 protected: | 171 protected: |
| 173 content::TestBrowserThreadBundle thread_bundle_; | 172 content::TestBrowserThreadBundle thread_bundle_; |
| 174 scoped_refptr<InfoMap> extension_info_map_; | 173 scoped_refptr<InfoMap> extension_info_map_; |
| 175 net::URLRequestJobFactoryImpl job_factory_; | 174 net::URLRequestJobFactoryImpl job_factory_; |
| 176 const net::URLRequestJobFactory* old_factory_; | 175 const net::URLRequestJobFactory* old_factory_; |
| 177 net::TestDelegate test_delegate_; | 176 net::TestDelegate test_delegate_; |
| 178 net::TestURLRequestContext test_url_request_context_; | 177 net::TestURLRequestContext test_url_request_context_; |
| 179 content::MockResourceContext resource_context_; | 178 content::MockResourceContext resource_context_; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 // First test a main frame request. | 211 // First test a main frame request. |
| 213 { | 212 { |
| 214 // It doesn't matter that the resource doesn't exist. If the resource | 213 // It doesn't matter that the resource doesn't exist. If the resource |
| 215 // is blocked, we should see BLOCKED_BY_CLIENT. Otherwise, the request | 214 // is blocked, we should see BLOCKED_BY_CLIENT. Otherwise, the request |
| 216 // should just fail because the file doesn't exist. | 215 // should just fail because the file doesn't exist. |
| 217 std::unique_ptr<net::URLRequest> request( | 216 std::unique_ptr<net::URLRequest> request( |
| 218 resource_context_.GetRequestContext()->CreateRequest( | 217 resource_context_.GetRequestContext()->CreateRequest( |
| 219 extension->GetResourceURL("404.html"), net::DEFAULT_PRIORITY, | 218 extension->GetResourceURL("404.html"), net::DEFAULT_PRIORITY, |
| 220 &test_delegate_)); | 219 &test_delegate_)); |
| 221 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); | 220 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); |
| 222 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | |
| 223 | 221 |
| 224 if (cases[i].should_allow_main_frame_load) { | 222 if (cases[i].should_allow_main_frame_load) { |
| 225 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()) << | 223 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, test_delegate_.request_status()) |
| 226 cases[i].name; | 224 << cases[i].name; |
| 227 } else { | 225 } else { |
| 228 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, request->status().error()) | 226 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status()) |
| 229 << cases[i].name; | 227 << cases[i].name; |
| 230 } | 228 } |
| 231 } | 229 } |
| 232 | 230 |
| 233 // Now do a subframe request. | 231 // Now do a subframe request. |
| 234 { | 232 { |
| 235 std::unique_ptr<net::URLRequest> request( | 233 std::unique_ptr<net::URLRequest> request( |
| 236 resource_context_.GetRequestContext()->CreateRequest( | 234 resource_context_.GetRequestContext()->CreateRequest( |
| 237 extension->GetResourceURL("404.html"), net::DEFAULT_PRIORITY, | 235 extension->GetResourceURL("404.html"), net::DEFAULT_PRIORITY, |
| 238 &test_delegate_)); | 236 &test_delegate_)); |
| 239 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME); | 237 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME); |
| 240 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | |
| 241 | 238 |
| 242 if (cases[i].should_allow_sub_frame_load) { | 239 if (cases[i].should_allow_sub_frame_load) { |
| 243 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request->status().error()) << | 240 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, test_delegate_.request_status()) |
| 244 cases[i].name; | 241 << cases[i].name; |
| 245 } else { | 242 } else { |
| 246 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, request->status().error()) | 243 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status()) |
| 247 << cases[i].name; | 244 << cases[i].name; |
| 248 } | 245 } |
| 249 } | 246 } |
| 250 } | 247 } |
| 251 } | 248 } |
| 252 | 249 |
| 253 void CheckForContentLengthHeader(net::URLRequest* request) { | 250 void CheckForContentLengthHeader(net::URLRequest* request) { |
| 254 std::string content_length; | 251 std::string content_length; |
| 255 request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength, | 252 request->GetResponseHeaderByName(net::HttpRequestHeaders::kContentLength, |
| 256 &content_length); | 253 &content_length); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 272 false, | 269 false, |
| 273 false); | 270 false); |
| 274 | 271 |
| 275 // First test it with the extension enabled. | 272 // First test it with the extension enabled. |
| 276 { | 273 { |
| 277 std::unique_ptr<net::URLRequest> request( | 274 std::unique_ptr<net::URLRequest> request( |
| 278 resource_context_.GetRequestContext()->CreateRequest( | 275 resource_context_.GetRequestContext()->CreateRequest( |
| 279 extension->GetResourceURL("webstore_icon_16.png"), | 276 extension->GetResourceURL("webstore_icon_16.png"), |
| 280 net::DEFAULT_PRIORITY, &test_delegate_)); | 277 net::DEFAULT_PRIORITY, &test_delegate_)); |
| 281 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); | 278 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
| 282 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 279 EXPECT_EQ(net::OK, test_delegate_.request_status()); |
| 283 CheckForContentLengthHeader(request.get()); | 280 CheckForContentLengthHeader(request.get()); |
| 284 } | 281 } |
| 285 | 282 |
| 286 // And then test it with the extension disabled. | 283 // And then test it with the extension disabled. |
| 287 extension_info_map_->RemoveExtension(extension->id(), | 284 extension_info_map_->RemoveExtension(extension->id(), |
| 288 UnloadedExtensionInfo::REASON_DISABLE); | 285 UnloadedExtensionInfo::REASON_DISABLE); |
| 289 { | 286 { |
| 290 std::unique_ptr<net::URLRequest> request( | 287 std::unique_ptr<net::URLRequest> request( |
| 291 resource_context_.GetRequestContext()->CreateRequest( | 288 resource_context_.GetRequestContext()->CreateRequest( |
| 292 extension->GetResourceURL("webstore_icon_16.png"), | 289 extension->GetResourceURL("webstore_icon_16.png"), |
| 293 net::DEFAULT_PRIORITY, &test_delegate_)); | 290 net::DEFAULT_PRIORITY, &test_delegate_)); |
| 294 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); | 291 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
| 295 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 292 EXPECT_EQ(net::OK, test_delegate_.request_status()); |
| 296 CheckForContentLengthHeader(request.get()); | 293 CheckForContentLengthHeader(request.get()); |
| 297 } | 294 } |
| 298 } | 295 } |
| 299 | 296 |
| 300 // Tests that a URL request for resource from an extension returns a few | 297 // Tests that a URL request for resource from an extension returns a few |
| 301 // expected response headers. | 298 // expected response headers. |
| 302 TEST_F(ExtensionProtocolTest, ResourceRequestResponseHeaders) { | 299 TEST_F(ExtensionProtocolTest, ResourceRequestResponseHeaders) { |
| 303 // Register a non-incognito extension protocol handler. | 300 // Register a non-incognito extension protocol handler. |
| 304 SetProtocolHandler(false); | 301 SetProtocolHandler(false); |
| 305 | 302 |
| 306 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension(); | 303 scoped_refptr<Extension> extension = CreateTestResponseHeaderExtension(); |
| 307 extension_info_map_->AddExtension(extension.get(), | 304 extension_info_map_->AddExtension(extension.get(), |
| 308 base::Time::Now(), | 305 base::Time::Now(), |
| 309 false, | 306 false, |
| 310 false); | 307 false); |
| 311 | 308 |
| 312 { | 309 { |
| 313 std::unique_ptr<net::URLRequest> request( | 310 std::unique_ptr<net::URLRequest> request( |
| 314 resource_context_.GetRequestContext()->CreateRequest( | 311 resource_context_.GetRequestContext()->CreateRequest( |
| 315 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, | 312 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, |
| 316 &test_delegate_)); | 313 &test_delegate_)); |
| 317 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); | 314 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
| 318 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 315 EXPECT_EQ(net::OK, test_delegate_.request_status()); |
| 319 | 316 |
| 320 // Check that cache-related headers are set. | 317 // Check that cache-related headers are set. |
| 321 std::string etag; | 318 std::string etag; |
| 322 request->GetResponseHeaderByName("ETag", &etag); | 319 request->GetResponseHeaderByName("ETag", &etag); |
| 323 EXPECT_TRUE(base::StartsWith(etag, "\"", base::CompareCase::SENSITIVE)); | 320 EXPECT_TRUE(base::StartsWith(etag, "\"", base::CompareCase::SENSITIVE)); |
| 324 EXPECT_TRUE(base::EndsWith(etag, "\"", base::CompareCase::SENSITIVE)); | 321 EXPECT_TRUE(base::EndsWith(etag, "\"", base::CompareCase::SENSITIVE)); |
| 325 | 322 |
| 326 std::string revalidation_header; | 323 std::string revalidation_header; |
| 327 request->GetResponseHeaderByName("cache-control", &revalidation_header); | 324 request->GetResponseHeaderByName("cache-control", &revalidation_header); |
| 328 EXPECT_EQ("no-cache", revalidation_header); | 325 EXPECT_EQ("no-cache", revalidation_header); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 349 | 346 |
| 350 // All MAIN_FRAME requests should succeed. SUB_FRAME requests that are not | 347 // All MAIN_FRAME requests should succeed. SUB_FRAME requests that are not |
| 351 // explicitly listed in web_accesible_resources or same-origin to the parent | 348 // explicitly listed in web_accesible_resources or same-origin to the parent |
| 352 // should not succeed. | 349 // should not succeed. |
| 353 { | 350 { |
| 354 std::unique_ptr<net::URLRequest> request( | 351 std::unique_ptr<net::URLRequest> request( |
| 355 resource_context_.GetRequestContext()->CreateRequest( | 352 resource_context_.GetRequestContext()->CreateRequest( |
| 356 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, | 353 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, |
| 357 &test_delegate_)); | 354 &test_delegate_)); |
| 358 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); | 355 StartRequest(request.get(), content::RESOURCE_TYPE_MAIN_FRAME); |
| 359 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | 356 EXPECT_EQ(net::OK, test_delegate_.request_status()); |
| 360 } | 357 } |
| 361 { | 358 { |
| 362 std::unique_ptr<net::URLRequest> request( | 359 std::unique_ptr<net::URLRequest> request( |
| 363 resource_context_.GetRequestContext()->CreateRequest( | 360 resource_context_.GetRequestContext()->CreateRequest( |
| 364 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, | 361 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, |
| 365 &test_delegate_)); | 362 &test_delegate_)); |
| 366 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME); | 363 StartRequest(request.get(), content::RESOURCE_TYPE_SUB_FRAME); |
| 367 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | 364 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status()); |
| 368 } | 365 } |
| 369 | 366 |
| 370 // And subresource types, such as media, should fail. | 367 // And subresource types, such as media, should fail. |
| 371 { | 368 { |
| 372 std::unique_ptr<net::URLRequest> request( | 369 std::unique_ptr<net::URLRequest> request( |
| 373 resource_context_.GetRequestContext()->CreateRequest( | 370 resource_context_.GetRequestContext()->CreateRequest( |
| 374 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, | 371 extension->GetResourceURL("test.dat"), net::DEFAULT_PRIORITY, |
| 375 &test_delegate_)); | 372 &test_delegate_)); |
| 376 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); | 373 StartRequest(request.get(), content::RESOURCE_TYPE_MEDIA); |
| 377 EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); | 374 EXPECT_EQ(net::ERR_BLOCKED_BY_CLIENT, test_delegate_.request_status()); |
| 378 } | 375 } |
| 379 } | 376 } |
| 380 | 377 |
| 381 | 378 |
| 382 TEST_F(ExtensionProtocolTest, MetadataFolder) { | 379 TEST_F(ExtensionProtocolTest, MetadataFolder) { |
| 383 SetProtocolHandler(false); | 380 SetProtocolHandler(false); |
| 384 | 381 |
| 385 base::FilePath extension_dir = GetTestPath("metadata_folder"); | 382 base::FilePath extension_dir = GetTestPath("metadata_folder"); |
| 386 std::string error; | 383 std::string error; |
| 387 scoped_refptr<Extension> extension = | 384 scoped_refptr<Extension> extension = |
| 388 file_util::LoadExtension(extension_dir, Manifest::INTERNAL, | 385 file_util::LoadExtension(extension_dir, Manifest::INTERNAL, |
| 389 Extension::NO_FLAGS, &error); | 386 Extension::NO_FLAGS, &error); |
| 390 ASSERT_NE(extension.get(), nullptr) << "error: " << error; | 387 ASSERT_NE(extension.get(), nullptr) << "error: " << error; |
| 391 | 388 |
| 392 // Loading "/test.html" should succeed. | 389 // Loading "/test.html" should succeed. |
| 393 EXPECT_EQ(net::URLRequestStatus::SUCCESS, DoRequest(*extension, "test.html")); | 390 EXPECT_EQ(net::OK, DoRequest(*extension, "test.html")); |
| 394 | 391 |
| 395 // Loading "/_metadata/verified_contents.json" should fail. | 392 // Loading "/_metadata/verified_contents.json" should fail. |
| 396 base::FilePath relative_path = | 393 base::FilePath relative_path = |
| 397 base::FilePath(kMetadataFolder).Append(kVerifiedContentsFilename); | 394 base::FilePath(kMetadataFolder).Append(kVerifiedContentsFilename); |
| 398 EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); | 395 EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); |
| 399 EXPECT_EQ(net::URLRequestStatus::FAILED, | 396 EXPECT_EQ(net::ERR_FAILED, |
| 400 DoRequest(*extension, relative_path.AsUTF8Unsafe())); | 397 DoRequest(*extension, relative_path.AsUTF8Unsafe())); |
| 401 | 398 |
| 402 // Loading "/_metadata/a.txt" should also fail. | 399 // Loading "/_metadata/a.txt" should also fail. |
| 403 relative_path = base::FilePath(kMetadataFolder).AppendASCII("a.txt"); | 400 relative_path = base::FilePath(kMetadataFolder).AppendASCII("a.txt"); |
| 404 EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); | 401 EXPECT_TRUE(base::PathExists(extension_dir.Append(relative_path))); |
| 405 EXPECT_EQ(net::URLRequestStatus::FAILED, | 402 EXPECT_EQ(net::ERR_FAILED, |
| 406 DoRequest(*extension, relative_path.AsUTF8Unsafe())); | 403 DoRequest(*extension, relative_path.AsUTF8Unsafe())); |
| 407 } | 404 } |
| 408 | 405 |
| 409 } // namespace extensions | 406 } // namespace extensions |
| OLD | NEW |