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