OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/strings/string16.h" | 6 #include "base/strings/string16.h" |
7 #include "base/strings/string_split.h" | 7 #include "base/strings/string_split.h" |
8 #include "base/task_scheduler/post_task.h" | 8 #include "base/task_scheduler/post_task.h" |
9 #include "base/threading/platform_thread.h" // For |Sleep()|. | 9 #include "base/threading/platform_thread.h" // For |Sleep()|. |
10 #include "chrome/browser/prerender/prerender_manager.h" | 10 #include "chrome/browser/prerender/prerender_manager.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 } // namespace | 70 } // namespace |
71 | 71 |
72 namespace prerender { | 72 namespace prerender { |
73 | 73 |
74 // These URLs used for test resources must be relative with the exception of | 74 // These URLs used for test resources must be relative with the exception of |
75 // |PrefetchLoaderPath|, which is only used in |PrerenderTestURLImpl()|. | 75 // |PrefetchLoaderPath|, which is only used in |PrerenderTestURLImpl()|. |
76 const char kPrefetchImagePage[] = "prerender/prefetch_image.html"; | 76 const char kPrefetchImagePage[] = "prerender/prefetch_image.html"; |
77 const char kPrefetchJpeg[] = "prerender/image.jpeg"; | 77 const char kPrefetchJpeg[] = "prerender/image.jpeg"; |
78 const char kPrefetchLoaderPath[] = "/prerender/prefetch_loader.html"; | 78 const char kPrefetchLoaderPath[] = "/prerender/prefetch_loader.html"; |
79 const char kPrefetchLoopPage[] = "prerender/prefetch_loop.html"; | 79 const char kPrefetchLoopPage[] = "prerender/prefetch_loop.html"; |
80 const char kPrefetchMetaCSP[] = "prerender/prefetch_meta_csp.html"; | |
80 const char kPrefetchPage[] = "prerender/prefetch_page.html"; | 81 const char kPrefetchPage[] = "prerender/prefetch_page.html"; |
81 const char kPrefetchPage2[] = "prerender/prefetch_page2.html"; | 82 const char kPrefetchPage2[] = "prerender/prefetch_page2.html"; |
82 const char kPrefetchPng[] = "prerender/image.png"; | 83 const char kPrefetchPng[] = "prerender/image.png"; |
84 const char kPrefetchResponseHeaderCSP[] = | |
85 "prerender/prefetch_response_csp.html"; | |
83 const char kPrefetchScript[] = "prerender/prefetch.js"; | 86 const char kPrefetchScript[] = "prerender/prefetch.js"; |
84 const char kPrefetchScript2[] = "prerender/prefetch2.js"; | 87 const char kPrefetchScript2[] = "prerender/prefetch2.js"; |
85 const char kPrefetchSubresourceRedirectPage[] = | 88 const char kPrefetchSubresourceRedirectPage[] = |
86 "prerender/prefetch_subresource_redirect.html"; | 89 "prerender/prefetch_subresource_redirect.html"; |
87 | 90 |
88 const char kPageBool[] = "pageBool"; | 91 const char kPageBool[] = "pageBool"; |
89 const char kScriptBool[] = "scriptBool"; | 92 const char kScriptBool[] = "scriptBool"; |
90 | 93 |
91 class NoStatePrefetchBrowserTest | 94 class NoStatePrefetchBrowserTest |
92 : public test_utils::PrerenderInProcessBrowserTest { | 95 : public test_utils::PrerenderInProcessBrowserTest { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 GetPrerenderManager()->SetTimeOverride(std::move(test_time)); | 144 GetPrerenderManager()->SetTimeOverride(std::move(test_time)); |
142 } | 145 } |
143 | 146 |
144 // Set up a request counter for |path|, which is also the location of the data | 147 // Set up a request counter for |path|, which is also the location of the data |
145 // served by the request. | 148 // served by the request. |
146 void CountRequestFor(const std::string& path_str, RequestCounter* counter) { | 149 void CountRequestFor(const std::string& path_str, RequestCounter* counter) { |
147 url::StringPieceReplacements<base::FilePath::StringType> replacement; | 150 url::StringPieceReplacements<base::FilePath::StringType> replacement; |
148 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(path_str); | 151 base::FilePath file_path = base::FilePath::FromUTF8Unsafe(path_str); |
149 replacement.SetPathStr(file_path.value()); | 152 replacement.SetPathStr(file_path.value()); |
150 const GURL url = src_server()->base_url().ReplaceComponents(replacement); | 153 const GURL url = src_server()->base_url().ReplaceComponents(replacement); |
151 CountRequestFor(url, path_str, counter); | 154 CountRequestForUrl(url, path_str, counter); |
152 } | 155 } |
153 | 156 |
154 // As above, but specify the data path and URL separately. | 157 // As above, but specify the data path and URL separately. |
155 void CountRequestFor(const GURL& url, | 158 void CountRequestForUrl(const GURL& url, |
156 const std::string& path_str, | 159 const std::string& path_str, |
157 RequestCounter* counter) { | 160 RequestCounter* counter) { |
158 base::FilePath url_file = ui_test_utils::GetTestFilePath( | 161 base::FilePath url_file = ui_test_utils::GetTestFilePath( |
159 base::FilePath(), base::FilePath::FromUTF8Unsafe(path_str)); | 162 base::FilePath(), base::FilePath::FromUTF8Unsafe(path_str)); |
160 content::BrowserThread::PostTask( | 163 content::BrowserThread::PostTask( |
161 content::BrowserThread::IO, FROM_HERE, | 164 content::BrowserThread::IO, FROM_HERE, |
162 base::Bind(&CreateCountingInterceptorOnIO, url, url_file, | 165 base::Bind(&CreateCountingInterceptorOnIO, url, url_file, |
163 counter->AsWeakPtr())); | 166 counter->AsWeakPtr())); |
164 } | 167 } |
165 | 168 |
166 BrowserTestTime* GetTimeOverride() const { return browser_test_time_; } | 169 BrowserTestTime* GetTimeOverride() const { return browser_test_time_; } |
167 | 170 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
265 } | 268 } |
266 | 269 |
267 // Checks that a cross-domain prefetching works correctly. | 270 // Checks that a cross-domain prefetching works correctly. |
268 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchCrossDomain) { | 271 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchCrossDomain) { |
269 static const std::string secondary_domain = "www.foo.com"; | 272 static const std::string secondary_domain = "www.foo.com"; |
270 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); | 273 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); |
271 GURL cross_domain_url(base::StringPrintf( | 274 GURL cross_domain_url(base::StringPrintf( |
272 "http://%s:%d/%s", secondary_domain.c_str(), | 275 "http://%s:%d/%s", secondary_domain.c_str(), |
273 embedded_test_server()->host_port_pair().port(), kPrefetchPage)); | 276 embedded_test_server()->host_port_pair().port(), kPrefetchPage)); |
274 RequestCounter cross_domain_counter; | 277 RequestCounter cross_domain_counter; |
275 CountRequestFor(cross_domain_url, kPrefetchPage, &cross_domain_counter); | 278 CountRequestForUrl(cross_domain_url, kPrefetchPage, &cross_domain_counter); |
276 PrerenderTestURL(cross_domain_url, FINAL_STATUS_APP_TERMINATING, 1); | 279 PrerenderTestURL(cross_domain_url, FINAL_STATUS_APP_TERMINATING, 1); |
277 cross_domain_counter.WaitForCount(1); | 280 cross_domain_counter.WaitForCount(1); |
278 } | 281 } |
279 | 282 |
283 // Checks that response header CSP is respected. | |
284 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, ResponseHeaderCSP) { | |
285 static const std::string secondary_domain = "foo.bar"; | |
286 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); | |
287 RequestCounter main_page; | |
288 CountRequestFor(kPrefetchResponseHeaderCSP, &main_page); | |
289 RequestCounter first_script; | |
290 CountRequestFor(kPrefetchScript, &first_script); | |
291 RequestCounter second_script; | |
292 GURL second_script_url(std::string("http://foo.bar/") + kPrefetchScript2); | |
293 CountRequestForUrl(second_script_url, kPrefetchScript2, &second_script); | |
294 PrerenderTestURL(kPrefetchResponseHeaderCSP, FINAL_STATUS_APP_TERMINATING, 1); | |
295 // The second script is in the correct domain for CSP, but the first script is | |
296 // not. | |
297 main_page.WaitForCount(1); | |
298 second_script.WaitForCount(1); | |
299 // TODO(pasko): wait for prefetch to be finished before checking the counts. | |
300 first_script.WaitForCount(0); | |
301 } | |
302 | |
303 // Checks that CSP in the meta tag cancels the prefetch. | |
304 // TODO(mattcary): probably this behavior should be consistent with | |
305 // response-header CSP. See crbug/XXX. | |
pasko
2016/10/17 11:23:18
nit: please substitute the XXX appropriately
mattcary
2016/10/17 11:32:08
Oops, bug created and this comment updated, thanks
pasko
2016/10/17 11:39:26
.. and uploaded?
| |
306 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, MetaTagCSP) { | |
307 static const std::string secondary_domain = "foo.bar"; | |
308 host_resolver()->AddRule(secondary_domain, "127.0.0.1"); | |
309 RequestCounter main_page; | |
310 CountRequestFor(kPrefetchMetaCSP, &main_page); | |
311 RequestCounter first_script; | |
312 CountRequestFor(kPrefetchScript, &first_script); | |
313 RequestCounter second_script; | |
314 GURL second_script_url(std::string("http://foo.bar/") + kPrefetchScript2); | |
315 CountRequestForUrl(second_script_url, kPrefetchScript2, &second_script); | |
316 PrerenderTestURL(kPrefetchMetaCSP, FINAL_STATUS_APP_TERMINATING, 1); | |
317 // TODO(mattcary): See test comment above. If the meta CSP tag were parsed, | |
318 // |second_script| would be loaded. Instead as the background scanner bails as | |
319 // soon as the meta CSP tag is seen, only |main_page| is fetched. | |
320 main_page.WaitForCount(1); | |
321 // TODO(pasko): wait for prefetch to be finished before checking the counts. | |
322 second_script.WaitForCount(0); | |
323 first_script.WaitForCount(0); | |
324 } | |
325 | |
280 // Checks simultaneous prefetch. | 326 // Checks simultaneous prefetch. |
281 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchSimultaneous) { | 327 IN_PROC_BROWSER_TEST_F(NoStatePrefetchBrowserTest, PrefetchSimultaneous) { |
282 RequestCounter first_main_counter; | 328 RequestCounter first_main_counter; |
283 CountRequestFor(kPrefetchPage, &first_main_counter); | 329 CountRequestFor(kPrefetchPage, &first_main_counter); |
284 RequestCounter second_main_counter; | 330 RequestCounter second_main_counter; |
285 CountRequestFor(kPrefetchPage2, &second_main_counter); | 331 CountRequestFor(kPrefetchPage2, &second_main_counter); |
286 RequestCounter first_script_counter; | 332 RequestCounter first_script_counter; |
287 CountRequestFor(kPrefetchScript, &first_script_counter); | 333 CountRequestFor(kPrefetchScript, &first_script_counter); |
288 RequestCounter second_script_counter; | 334 RequestCounter second_script_counter; |
289 CountRequestFor(kPrefetchScript2, &second_script_counter); | 335 CountRequestFor(kPrefetchScript2, &second_script_counter); |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
487 // special way. | 533 // special way. |
488 // TODO(mattcary): since the prerender will count itself as loaded even if the | 534 // TODO(mattcary): since the prerender will count itself as loaded even if the |
489 // fetch of the main resource fails, the test doesn't actually confirm what we | 535 // fetch of the main resource fails, the test doesn't actually confirm what we |
490 // want it to confirm. This may be fixed by planned changes to the prerender | 536 // want it to confirm. This may be fixed by planned changes to the prerender |
491 // lifecycle. | 537 // lifecycle. |
492 std::unique_ptr<TestPrerender> prerender = | 538 std::unique_ptr<TestPrerender> prerender = |
493 PrerenderTestURL(kPrefetchPage, FINAL_STATUS_SAFE_BROWSING, 1); | 539 PrerenderTestURL(kPrefetchPage, FINAL_STATUS_SAFE_BROWSING, 1); |
494 } | 540 } |
495 | 541 |
496 } // namespace prerender | 542 } // namespace prerender |
OLD | NEW |