| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013, Google Inc. All rights reserved. | 2 * Copyright (c) 2013, Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "core/fetch/ResourceFetcher.h" | 32 #include "core/fetch/ResourceFetcher.h" |
| 33 | 33 |
| 34 #include <gtest/gtest.h> | 34 #include <gtest/gtest.h> |
| 35 #include "core/fetch/FetchInitiatorInfo.h" | 35 #include "core/fetch/FetchInitiatorInfo.h" |
| 36 #include "core/fetch/FetchRequest.h" | 36 #include "core/fetch/FetchRequest.h" |
| 37 #include "core/fetch/ImageResource.h" | 37 #include "core/fetch/ImageResource.h" |
| 38 #include "core/fetch/MemoryCache.h" | 38 #include "core/fetch/MemoryCache.h" |
| 39 #include "core/fetch/ResourcePtr.h" | 39 #include "core/fetch/ResourcePtr.h" |
| 40 #include "core/frame/FrameView.h" |
| 40 #include "core/html/HTMLDocument.h" | 41 #include "core/html/HTMLDocument.h" |
| 41 #include "core/loader/DocumentLoader.h" | 42 #include "core/loader/DocumentLoader.h" |
| 43 #include "core/page/Page.h" |
| 44 #include "core/testing/DummyPageHolder.h" |
| 42 #include "platform/network/ResourceRequest.h" | 45 #include "platform/network/ResourceRequest.h" |
| 43 #include "platform/weborigin/KURL.h" | 46 #include "platform/weborigin/KURL.h" |
| 44 #include "platform/weborigin/SecurityOrigin.h" | 47 #include "platform/weborigin/SecurityOrigin.h" |
| 45 #include "public/platform/WebURLRequest.h" | 48 #include "public/platform/WebURLRequest.h" |
| 46 | 49 |
| 47 namespace blink { | 50 namespace blink { |
| 48 | 51 |
| 49 class ResourceFetcherTest : public ::testing::Test { | 52 class ResourceFetcherUpgradeTest : public ::testing::Test { |
| 50 public: | 53 public: |
| 51 ResourceFetcherTest() | 54 ResourceFetcherUpgradeTest() |
| 52 : secureURL(ParsedURLString, "https://secureorigin.test/image.png") | 55 : secureURL(ParsedURLString, "https://secureorigin.test/image.png") |
| 53 , exampleOrigin(SecurityOrigin::create(KURL(ParsedURLString, "https://ex
ample.test/"))) | 56 , exampleOrigin(SecurityOrigin::create(KURL(ParsedURLString, "https://ex
ample.test/"))) |
| 54 , secureOrigin(SecurityOrigin::create(secureURL)) | 57 , secureOrigin(SecurityOrigin::create(secureURL)) |
| 55 { | 58 { |
| 56 } | 59 } |
| 57 | 60 |
| 58 protected: | 61 protected: |
| 59 virtual void SetUp() | 62 virtual void SetUp() |
| 60 { | 63 { |
| 61 // Create a ResourceFetcher that has a real DocumentLoader and Document,
but is not attached to a LocalFrame. | 64 // Create a ResourceFetcher that has a real DocumentLoader and Document,
but is not attached to a LocalFrame. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 RefPtr<SecurityOrigin> exampleOrigin; | 112 RefPtr<SecurityOrigin> exampleOrigin; |
| 110 RefPtr<SecurityOrigin> secureOrigin; | 113 RefPtr<SecurityOrigin> secureOrigin; |
| 111 | 114 |
| 112 // We don't use the DocumentLoader directly in any tests, but need to keep i
t around as long | 115 // We don't use the DocumentLoader directly in any tests, but need to keep i
t around as long |
| 113 // as the ResourceFetcher and Document live due to indirect usage. | 116 // as the ResourceFetcher and Document live due to indirect usage. |
| 114 RefPtr<DocumentLoader> documentLoader; | 117 RefPtr<DocumentLoader> documentLoader; |
| 115 RefPtrWillBePersistent<ResourceFetcher> fetcher; | 118 RefPtrWillBePersistent<ResourceFetcher> fetcher; |
| 116 RefPtrWillBePersistent<Document> document; | 119 RefPtrWillBePersistent<Document> document; |
| 117 }; | 120 }; |
| 118 | 121 |
| 119 TEST_F(ResourceFetcherTest, StartLoadAfterFrameDetach) | 122 TEST_F(ResourceFetcherUpgradeTest, StartLoadAfterFrameDetach) |
| 120 { | 123 { |
| 121 EXPECT_EQ(fetcher->frame(), static_cast<LocalFrame*>(0)); | 124 EXPECT_EQ(fetcher->frame(), static_cast<LocalFrame*>(0)); |
| 122 | 125 |
| 123 // Try to request a url. The request should fail, no resource should be retu
rned, | 126 // Try to request a url. The request should fail, no resource should be retu
rned, |
| 124 // and no resource should be present in the cache. | 127 // and no resource should be present in the cache. |
| 125 FetchRequest fetchRequest = FetchRequest(ResourceRequest(secureURL), FetchIn
itiatorInfo()); | 128 FetchRequest fetchRequest = FetchRequest(ResourceRequest(secureURL), FetchIn
itiatorInfo()); |
| 126 ResourcePtr<ImageResource> image = fetcher->fetchImage(fetchRequest); | 129 ResourcePtr<ImageResource> image = fetcher->fetchImage(fetchRequest); |
| 127 EXPECT_EQ(image.get(), static_cast<ImageResource*>(0)); | 130 EXPECT_EQ(image.get(), static_cast<ImageResource*>(0)); |
| 128 EXPECT_EQ(memoryCache()->resourceForURL(secureURL), static_cast<Resource*>(0
)); | 131 EXPECT_EQ(memoryCache()->resourceForURL(secureURL), static_cast<Resource*>(0
)); |
| 129 } | 132 } |
| 130 | 133 |
| 131 TEST_F(ResourceFetcherTest, UpgradeInsecureResourceRequests) | 134 TEST_F(ResourceFetcherUpgradeTest, UpgradeInsecureResourceRequests) |
| 132 { | 135 { |
| 133 struct TestCase { | 136 struct TestCase { |
| 134 const char* original; | 137 const char* original; |
| 135 const char* upgraded; | 138 const char* upgraded; |
| 136 } tests[] = { | 139 } tests[] = { |
| 137 { "http://example.test/image.png", "https://example.test/image.png" }, | 140 { "http://example.test/image.png", "https://example.test/image.png" }, |
| 138 { "http://example.test:80/image.png", "https://example.test:443/image.pn
g" }, | 141 { "http://example.test:80/image.png", "https://example.test:443/image.pn
g" }, |
| 139 { "http://example.test:1212/image.png", "https://example.test:1212/image
.png" }, | 142 { "http://example.test:1212/image.png", "https://example.test:1212/image
.png" }, |
| 140 | 143 |
| 141 { "https://example.test/image.png", "https://example.test/image.png" }, | 144 { "https://example.test/image.png", "https://example.test/image.png" }, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 165 expectUpgrade(test.original, WebURLRequest::RequestContextForm, WebURLRe
quest::FrameTypeTopLevel, test.upgraded); | 168 expectUpgrade(test.original, WebURLRequest::RequestContextForm, WebURLRe
quest::FrameTypeTopLevel, test.upgraded); |
| 166 expectUpgrade(test.original, WebURLRequest::RequestContextForm, WebURLRe
quest::FrameTypeAuxiliary, test.upgraded); | 169 expectUpgrade(test.original, WebURLRequest::RequestContextForm, WebURLRe
quest::FrameTypeAuxiliary, test.upgraded); |
| 167 | 170 |
| 168 // Or unless the host of the document matches the host of the resource: | 171 // Or unless the host of the document matches the host of the resource: |
| 169 document->setSecurityOrigin(exampleOrigin); | 172 document->setSecurityOrigin(exampleOrigin); |
| 170 expectUpgrade(test.original, WebURLRequest::RequestContextScript, WebURL
Request::FrameTypeTopLevel, test.upgraded); | 173 expectUpgrade(test.original, WebURLRequest::RequestContextScript, WebURL
Request::FrameTypeTopLevel, test.upgraded); |
| 171 expectUpgrade(test.original, WebURLRequest::RequestContextScript, WebURL
Request::FrameTypeAuxiliary, test.upgraded); | 174 expectUpgrade(test.original, WebURLRequest::RequestContextScript, WebURL
Request::FrameTypeAuxiliary, test.upgraded); |
| 172 } | 175 } |
| 173 } | 176 } |
| 174 | 177 |
| 175 TEST_F(ResourceFetcherTest, DoNotUpgradeInsecureResourceRequests) | 178 TEST_F(ResourceFetcherUpgradeTest, DoNotUpgradeInsecureResourceRequests) |
| 176 { | 179 { |
| 177 document->setSecurityOrigin(secureOrigin); | 180 document->setSecurityOrigin(secureOrigin); |
| 178 document->setInsecureContentPolicy(SecurityContext::InsecureContentDoNotUpgr
ade); | 181 document->setInsecureContentPolicy(SecurityContext::InsecureContentDoNotUpgr
ade); |
| 179 | 182 |
| 180 expectUpgrade("http://example.test/image.png", "http://example.test/image.pn
g"); | 183 expectUpgrade("http://example.test/image.png", "http://example.test/image.pn
g"); |
| 181 expectUpgrade("http://example.test:80/image.png", "http://example.test:80/im
age.png"); | 184 expectUpgrade("http://example.test:80/image.png", "http://example.test:80/im
age.png"); |
| 182 expectUpgrade("http://example.test:1212/image.png", "http://example.test:121
2/image.png"); | 185 expectUpgrade("http://example.test:1212/image.png", "http://example.test:121
2/image.png"); |
| 183 | 186 |
| 184 expectUpgrade("https://example.test/image.png", "https://example.test/image.
png"); | 187 expectUpgrade("https://example.test/image.png", "https://example.test/image.
png"); |
| 185 expectUpgrade("https://example.test:80/image.png", "https://example.test:80/
image.png"); | 188 expectUpgrade("https://example.test:80/image.png", "https://example.test:80/
image.png"); |
| 186 expectUpgrade("https://example.test:1212/image.png", "https://example.test:1
212/image.png"); | 189 expectUpgrade("https://example.test:1212/image.png", "https://example.test:1
212/image.png"); |
| 187 | 190 |
| 188 expectUpgrade("ftp://example.test/image.png", "ftp://example.test/image.png"
); | 191 expectUpgrade("ftp://example.test/image.png", "ftp://example.test/image.png"
); |
| 189 expectUpgrade("ftp://example.test:21/image.png", "ftp://example.test:21/imag
e.png"); | 192 expectUpgrade("ftp://example.test:21/image.png", "ftp://example.test:21/imag
e.png"); |
| 190 expectUpgrade("ftp://example.test:1212/image.png", "ftp://example.test:1212/
image.png"); | 193 expectUpgrade("ftp://example.test:1212/image.png", "ftp://example.test:1212/
image.png"); |
| 191 } | 194 } |
| 192 | 195 |
| 193 TEST_F(ResourceFetcherTest, MonitorInsecureResourceRequests) | 196 TEST_F(ResourceFetcherUpgradeTest, MonitorInsecureResourceRequests) |
| 194 { | 197 { |
| 195 document->setSecurityOrigin(secureOrigin); | 198 document->setSecurityOrigin(secureOrigin); |
| 196 document->setInsecureContentPolicy(SecurityContext::InsecureContentMonitor); | 199 document->setInsecureContentPolicy(SecurityContext::InsecureContentMonitor); |
| 197 | 200 |
| 198 expectUpgrade("http://example.test/image.png", "http://example.test/image.pn
g"); | 201 expectUpgrade("http://example.test/image.png", "http://example.test/image.pn
g"); |
| 199 expectUpgrade("http://example.test:80/image.png", "http://example.test:80/im
age.png"); | 202 expectUpgrade("http://example.test:80/image.png", "http://example.test:80/im
age.png"); |
| 200 expectUpgrade("http://example.test:1212/image.png", "http://example.test:121
2/image.png"); | 203 expectUpgrade("http://example.test:1212/image.png", "http://example.test:121
2/image.png"); |
| 201 | 204 |
| 202 expectUpgrade("https://example.test/image.png", "https://example.test/image.
png"); | 205 expectUpgrade("https://example.test/image.png", "https://example.test/image.
png"); |
| 203 expectUpgrade("https://example.test:80/image.png", "https://example.test:80/
image.png"); | 206 expectUpgrade("https://example.test:80/image.png", "https://example.test:80/
image.png"); |
| 204 expectUpgrade("https://example.test:1212/image.png", "https://example.test:1
212/image.png"); | 207 expectUpgrade("https://example.test:1212/image.png", "https://example.test:1
212/image.png"); |
| 205 | 208 |
| 206 expectUpgrade("ftp://example.test/image.png", "ftp://example.test/image.png"
); | 209 expectUpgrade("ftp://example.test/image.png", "ftp://example.test/image.png"
); |
| 207 expectUpgrade("ftp://example.test:21/image.png", "ftp://example.test:21/imag
e.png"); | 210 expectUpgrade("ftp://example.test:21/image.png", "ftp://example.test:21/imag
e.png"); |
| 208 expectUpgrade("ftp://example.test:1212/image.png", "ftp://example.test:1212/
image.png"); | 211 expectUpgrade("ftp://example.test:1212/image.png", "ftp://example.test:1212/
image.png"); |
| 209 } | 212 } |
| 210 | 213 |
| 211 TEST_F(ResourceFetcherTest, SendPreferHeader) | 214 TEST_F(ResourceFetcherUpgradeTest, SendPreferHeader) |
| 212 { | 215 { |
| 213 struct TestCase { | 216 struct TestCase { |
| 214 const char* toRequest; | 217 const char* toRequest; |
| 215 WebURLRequest::FrameType frameType; | 218 WebURLRequest::FrameType frameType; |
| 216 bool shouldPrefer; | 219 bool shouldPrefer; |
| 217 } tests[] = { | 220 } tests[] = { |
| 218 { "http://example.test/page.html", WebURLRequest::FrameTypeAuxiliary, tr
ue }, | 221 { "http://example.test/page.html", WebURLRequest::FrameTypeAuxiliary, tr
ue }, |
| 219 { "http://example.test/page.html", WebURLRequest::FrameTypeNested, true
}, | 222 { "http://example.test/page.html", WebURLRequest::FrameTypeNested, true
}, |
| 220 { "http://example.test/page.html", WebURLRequest::FrameTypeNone, false }
, | 223 { "http://example.test/page.html", WebURLRequest::FrameTypeNone, false }
, |
| 221 { "http://example.test/page.html", WebURLRequest::FrameTypeTopLevel, tru
e }, | 224 { "http://example.test/page.html", WebURLRequest::FrameTypeTopLevel, tru
e }, |
| 222 { "https://example.test/page.html", WebURLRequest::FrameTypeAuxiliary, f
alse }, | 225 { "https://example.test/page.html", WebURLRequest::FrameTypeAuxiliary, f
alse }, |
| 223 { "https://example.test/page.html", WebURLRequest::FrameTypeNested, fals
e }, | 226 { "https://example.test/page.html", WebURLRequest::FrameTypeNested, fals
e }, |
| 224 { "https://example.test/page.html", WebURLRequest::FrameTypeNone, false
}, | 227 { "https://example.test/page.html", WebURLRequest::FrameTypeNone, false
}, |
| 225 { "https://example.test/page.html", WebURLRequest::FrameTypeTopLevel, fa
lse } | 228 { "https://example.test/page.html", WebURLRequest::FrameTypeTopLevel, fa
lse } |
| 226 }; | 229 }; |
| 227 | 230 |
| 228 for (auto test : tests) { | 231 for (auto test : tests) { |
| 229 document->setInsecureContentPolicy(SecurityContext::InsecureContentDoNot
Upgrade); | 232 document->setInsecureContentPolicy(SecurityContext::InsecureContentDoNot
Upgrade); |
| 230 expectPreferHeader(test.toRequest, test.frameType, test.shouldPrefer); | 233 expectPreferHeader(test.toRequest, test.frameType, test.shouldPrefer); |
| 231 | 234 |
| 232 document->setInsecureContentPolicy(SecurityContext::InsecureContentUpgra
de); | 235 document->setInsecureContentPolicy(SecurityContext::InsecureContentUpgra
de); |
| 233 expectPreferHeader(test.toRequest, test.frameType, test.shouldPrefer); | 236 expectPreferHeader(test.toRequest, test.frameType, test.shouldPrefer); |
| 234 | 237 |
| 235 document->setInsecureContentPolicy(SecurityContext::InsecureContentMonit
or); | 238 document->setInsecureContentPolicy(SecurityContext::InsecureContentMonit
or); |
| 236 expectPreferHeader(test.toRequest, test.frameType, test.shouldPrefer); | 239 expectPreferHeader(test.toRequest, test.frameType, test.shouldPrefer); |
| 237 } | 240 } |
| 238 } | 241 } |
| 239 | 242 |
| 243 class ResourceFetcherHintsTest : public ::testing::Test { |
| 244 public: |
| 245 ResourceFetcherHintsTest() { } |
| 246 |
| 247 protected: |
| 248 virtual void SetUp() |
| 249 { |
| 250 dummyPageHolder = DummyPageHolder::create(IntSize(500, 500)); |
| 251 dummyPageHolder->page().setDeviceScaleFactor(1.0); |
| 252 documentLoader = DocumentLoader::create(&dummyPageHolder->frame(), Resou
rceRequest("http://www.example.com"), SubstituteData()); |
| 253 document = toHTMLDocument(&dummyPageHolder->document()); |
| 254 fetcher = documentLoader->fetcher(); |
| 255 fetcher->setDocument(document.get()); |
| 256 } |
| 257 |
| 258 void expectHeader(const char* input, const char* headerName, bool isPresent,
const char* headerValue) |
| 259 { |
| 260 KURL inputURL(ParsedURLString, input); |
| 261 FetchRequest fetchRequest = FetchRequest(ResourceRequest(inputURL), Fetc
hInitiatorInfo()); |
| 262 fetcher->addClientHintsIfNeccessary(fetchRequest); |
| 263 |
| 264 EXPECT_STREQ(isPresent ? headerValue : "", |
| 265 fetchRequest.resourceRequest().httpHeaderField(headerName).utf8().da
ta()); |
| 266 } |
| 267 |
| 268 OwnPtr<DummyPageHolder> dummyPageHolder; |
| 269 // We don't use the DocumentLoader directly in any tests, but need to keep i
t around as long |
| 270 // as the ResourceFetcher and Document live due to indirect usage. |
| 271 RefPtr<DocumentLoader> documentLoader; |
| 272 RefPtrWillBePersistent<Document> document; |
| 273 RefPtrWillBePersistent<ResourceFetcher> fetcher; |
| 274 }; |
| 275 |
| 276 TEST_F(ResourceFetcherHintsTest, MonitorDPRHints) |
| 277 { |
| 278 expectHeader("http://www.example.com/1.gif", "DPR", false, ""); |
| 279 dummyPageHolder->frame().setShouldSendDPRHint(); |
| 280 expectHeader("http://www.example.com/1.gif", "DPR", true, "1"); |
| 281 dummyPageHolder->page().setDeviceScaleFactor(2.5); |
| 282 expectHeader("http://www.example.com/1.gif", "DPR", true, "2.5"); |
| 283 expectHeader("http://www.example.com/1.gif", "RW", false, ""); |
| 284 } |
| 285 |
| 286 TEST_F(ResourceFetcherHintsTest, MonitorRWHints) |
| 287 { |
| 288 expectHeader("http://www.example.com/1.gif", "RW", false, ""); |
| 289 dummyPageHolder->frame().setShouldSendRWHint(); |
| 290 expectHeader("http://www.example.com/1.gif", "RW", true, "500"); |
| 291 dummyPageHolder->frameView().setLayoutSizeFixedToFrameSize(false); |
| 292 dummyPageHolder->frameView().setLayoutSize(IntSize(800, 800)); |
| 293 expectHeader("http://www.example.com/1.gif", "RW", true, "800"); |
| 294 expectHeader("http://www.example.com/1.gif", "DPR", false, ""); |
| 295 } |
| 296 |
| 297 TEST_F(ResourceFetcherHintsTest, MonitorBothHints) |
| 298 { |
| 299 expectHeader("http://www.example.com/1.gif", "DPR", false, ""); |
| 300 expectHeader("http://www.example.com/1.gif", "RW", false, ""); |
| 301 |
| 302 dummyPageHolder->frame().setShouldSendDPRHint(); |
| 303 dummyPageHolder->frame().setShouldSendRWHint(); |
| 304 expectHeader("http://www.example.com/1.gif", "DPR", true, "1"); |
| 305 expectHeader("http://www.example.com/1.gif", "RW", true, "500"); |
| 306 } |
| 307 |
| 240 } // namespace | 308 } // namespace |
| OLD | NEW |