Chromium Code Reviews| Index: Source/core/fetch/ResourceFetcherTest.cpp |
| diff --git a/Source/core/fetch/ResourceFetcherTest.cpp b/Source/core/fetch/ResourceFetcherTest.cpp |
| index b01276766c8a9cac6005ae74a03415e75628dbae..9e4dd3749f2985f6310ddf2d03837c34270a279c 100644 |
| --- a/Source/core/fetch/ResourceFetcherTest.cpp |
| +++ b/Source/core/fetch/ResourceFetcherTest.cpp |
| @@ -40,30 +40,122 @@ |
| #include "core/html/HTMLDocument.h" |
| #include "core/loader/DocumentLoader.h" |
| #include "platform/network/ResourceRequest.h" |
| +#include "platform/weborigin/KURL.h" |
| +#include "platform/weborigin/SecurityOrigin.h" |
| -using namespace blink; |
| +namespace blink { |
| -namespace { |
| +class ResourceFetcherTest : public ::testing::Test { |
| +public: |
| + ResourceFetcherTest() |
| + : secureURL(ParsedURLString, "https://example.test/image.png") |
| + , insecureURL(ParsedURLString, "http://example.test/image.png") |
| + , secureOrigin(SecurityOrigin::create(secureURL)) |
| + , insecureOrigin(SecurityOrigin::create(insecureURL)) |
| + { |
| + } |
| -TEST(ResourceFetcherTest, StartLoadAfterFrameDetach) |
| +protected: |
| + virtual void SetUp() |
| + { |
| + // Create a ResourceFetcher that has a real DocumentLoader and Document, but is not attached to a LocalFrame. |
| + // Technically, we're concerned about what happens after a LocalFrame is detached (rather than before |
| + // any attach occurs), but ResourceFetcher can't tell the difference. |
| + documentLoader = DocumentLoader::create(0, ResourceRequest(secureURL), SubstituteData()); |
| + document = Document::create(); |
| + fetcher = documentLoader->fetcher(); |
| + fetcher->setDocument(document.get()); |
| + } |
| + |
| + void expectUpgrade(const char* input, const char* expected) |
| + { |
| + KURL inputURL(ParsedURLString, input); |
| + KURL expectedURL(ParsedURLString, expected); |
| + |
| + FetchRequest fetchRequest = FetchRequest(ResourceRequest(inputURL), FetchInitiatorInfo()); |
| + fetcher->maybeUpgradeInsecureRequestURL(fetchRequest); |
| + EXPECT_STREQ(expectedURL.string().utf8().data(), fetchRequest.resourceRequest().url().string().utf8().data()); |
| + EXPECT_EQ(expectedURL.protocol(), fetchRequest.resourceRequest().url().protocol()); |
| + EXPECT_EQ(expectedURL.host(), fetchRequest.resourceRequest().url().host()); |
| + EXPECT_EQ(expectedURL.port(), fetchRequest.resourceRequest().url().port()); |
| + EXPECT_EQ(expectedURL.hasPort(), fetchRequest.resourceRequest().url().hasPort()); |
| + EXPECT_EQ(expectedURL.path(), fetchRequest.resourceRequest().url().path()); |
| + } |
| + |
| + KURL secureURL; |
| + KURL insecureURL; |
| + RefPtr<SecurityOrigin> secureOrigin; |
| + RefPtr<SecurityOrigin> insecureOrigin; |
|
Yoav Weiss
2015/02/06 09:58:36
Is insecureOrigin used anywhere?
|
| + RefPtr<DocumentLoader> documentLoader; |
|
Yoav Weiss
2015/02/06 09:58:36
documentLoader is a member only because we want to
|
| + RefPtrWillBeRawPtr<ResourceFetcher> fetcher; |
| + |
| + RefPtrWillBePersistent<Document> document; |
|
Yoav Weiss
2015/02/06 09:58:36
Not sure if it's a requirement or just commonly us
|
| +}; |
| + |
| +TEST_F(ResourceFetcherTest, StartLoadAfterFrameDetach) |
| { |
| - KURL testURL(ParsedURLString, "http://www.test.com/cancelTest.jpg"); |
| - |
| - // Create a ResourceFetcher that has a real DocumentLoader and Document, but is not attached to a LocalFrame. |
| - // Technically, we're concerned about what happens after a LocalFrame is detached (rather than before |
| - // any attach occurs), but ResourceFetcher can't tell the difference. |
| - RefPtr<DocumentLoader> documentLoader = DocumentLoader::create(0, ResourceRequest(testURL), SubstituteData()); |
| - RefPtrWillBeRawPtr<HTMLDocument> document = HTMLDocument::create(); |
| - RefPtrWillBeRawPtr<ResourceFetcher> fetcher(documentLoader->fetcher()); |
| - fetcher->setDocument(document.get()); |
| EXPECT_EQ(fetcher->frame(), static_cast<LocalFrame*>(0)); |
| // Try to request a url. The request should fail, no resource should be returned, |
| // and no resource should be present in the cache. |
| - FetchRequest fetchRequest = FetchRequest(ResourceRequest(testURL), FetchInitiatorInfo()); |
| + FetchRequest fetchRequest = FetchRequest(ResourceRequest(insecureURL), FetchInitiatorInfo()); |
| ResourcePtr<ImageResource> image = fetcher->fetchImage(fetchRequest); |
| EXPECT_EQ(image.get(), static_cast<ImageResource*>(0)); |
| - EXPECT_EQ(memoryCache()->resourceForURL(testURL), static_cast<Resource*>(0)); |
| + EXPECT_EQ(memoryCache()->resourceForURL(insecureURL), static_cast<Resource*>(0)); |
| +} |
| + |
| +TEST_F(ResourceFetcherTest, UpgradeInsecureResourceRequests) |
| +{ |
| + document->setSecurityOrigin(secureOrigin); |
| + document->setInsecureContentPolicy(SecurityContext::InsecureContentUpgrade); |
| + |
| + expectUpgrade("http://example.test/image.png", "https://example.test/image.png"); |
| + expectUpgrade("http://example.test:80/image.png", "https://example.test:443/image.png"); |
| + expectUpgrade("http://example.test:1212/image.png", "https://example.test:1212/image.png"); |
| + |
| + expectUpgrade("https://example.test/image.png", "https://example.test/image.png"); |
| + expectUpgrade("https://example.test:80/image.png", "https://example.test:80/image.png"); |
| + expectUpgrade("https://example.test:1212/image.png", "https://example.test:1212/image.png"); |
| + |
| + expectUpgrade("ftp://example.test/image.png", "ftp://example.test/image.png"); |
| + expectUpgrade("ftp://example.test:21/image.png", "ftp://example.test:21/image.png"); |
| + expectUpgrade("ftp://example.test:1212/image.png", "ftp://example.test:1212/image.png"); |
| +} |
| + |
| +TEST_F(ResourceFetcherTest, DoNotUpgradeInsecureResourceRequests) |
| +{ |
| + document->setSecurityOrigin(secureOrigin); |
| + document->setInsecureContentPolicy(SecurityContext::InsecureContentDoNotUpgrade); |
| + |
| + expectUpgrade("http://example.test/image.png", "http://example.test/image.png"); |
| + expectUpgrade("http://example.test:80/image.png", "http://example.test:80/image.png"); |
| + expectUpgrade("http://example.test:1212/image.png", "http://example.test:1212/image.png"); |
| + |
| + expectUpgrade("https://example.test/image.png", "https://example.test/image.png"); |
| + expectUpgrade("https://example.test:80/image.png", "https://example.test:80/image.png"); |
| + expectUpgrade("https://example.test:1212/image.png", "https://example.test:1212/image.png"); |
| + |
| + expectUpgrade("ftp://example.test/image.png", "ftp://example.test/image.png"); |
| + expectUpgrade("ftp://example.test:21/image.png", "ftp://example.test:21/image.png"); |
| + expectUpgrade("ftp://example.test:1212/image.png", "ftp://example.test:1212/image.png"); |
| +} |
| + |
| +TEST_F(ResourceFetcherTest, MonitorInsecureResourceRequests) |
| +{ |
| + document->setSecurityOrigin(secureOrigin); |
| + document->setInsecureContentPolicy(SecurityContext::InsecureContentMonitor); |
| + |
| + expectUpgrade("http://example.test/image.png", "http://example.test/image.png"); |
| + expectUpgrade("http://example.test:80/image.png", "http://example.test:80/image.png"); |
| + expectUpgrade("http://example.test:1212/image.png", "http://example.test:1212/image.png"); |
| + |
| + expectUpgrade("https://example.test/image.png", "https://example.test/image.png"); |
| + expectUpgrade("https://example.test:80/image.png", "https://example.test:80/image.png"); |
| + expectUpgrade("https://example.test:1212/image.png", "https://example.test:1212/image.png"); |
| + |
| + expectUpgrade("ftp://example.test/image.png", "ftp://example.test/image.png"); |
| + expectUpgrade("ftp://example.test:21/image.png", "ftp://example.test:21/image.png"); |
| + expectUpgrade("ftp://example.test:1212/image.png", "ftp://example.test:1212/image.png"); |
| } |
| } // namespace |