Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: chrome/browser/net/predictor_browsertest.cc

Issue 1131293004: Add cross origin to Blink-driven preconnect (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added more tests and fixed a test bug Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/json/json_string_value_serializer.h" 9 #include "base/json/json_string_value_serializer.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 ConnectionListener() : task_runner_(base::ThreadTaskRunnerHandle::Get()) {} 54 ConnectionListener() : task_runner_(base::ThreadTaskRunnerHandle::Get()) {}
55 55
56 ~ConnectionListener() override {} 56 ~ConnectionListener() override {}
57 57
58 // Get called from the EmbeddedTestServer thread to be notified that 58 // Get called from the EmbeddedTestServer thread to be notified that
59 // a connection was accepted. 59 // a connection was accepted.
60 void AcceptedSocket( 60 void AcceptedSocket(
61 const net::test_server::StreamListenSocket& connection) override { 61 const net::test_server::StreamListenSocket& connection) override {
62 base::AutoLock lock(lock_); 62 base::AutoLock lock(lock_);
63 uint16_t socket = GetPort(connection); 63 uint16_t socket = GetPort(connection);
64 printf("Socket %d\n", socket);
Ryan Sleevi 2015/07/10 09:46:45 *cough* :P
Yoav Weiss 2015/07/10 10:03:40 Doh! I got accustomed to Blink's cl upload hooks y
64 EXPECT_TRUE(sockets_.find(socket) == sockets_.end()); 65 EXPECT_TRUE(sockets_.find(socket) == sockets_.end());
65 66
66 sockets_[socket] = SOCKET_ACCEPTED; 67 sockets_[socket] = SOCKET_ACCEPTED;
67 task_runner_->PostTask(FROM_HERE, accept_loop_.QuitClosure()); 68 task_runner_->PostTask(FROM_HERE, accept_loop_.QuitClosure());
68 } 69 }
69 70
70 // Get called from the EmbeddedTestServer thread to be notified that 71 // Get called from the EmbeddedTestServer thread to be notified that
71 // a connection was read from. 72 // a connection was read from.
72 void ReadFromSocket( 73 void ReadFromSocket(
73 const net::test_server::StreamListenSocket& connection) override { 74 const net::test_server::StreamListenSocket& connection) override {
(...skipping 23 matching lines...) Expand all
97 } 98 }
98 99
99 void WaitUntilFirstConnectionAccepted() { accept_loop_.Run(); } 100 void WaitUntilFirstConnectionAccepted() { accept_loop_.Run(); }
100 101
101 void WaitUntilFirstConnectionRead() { read_loop_.Run(); } 102 void WaitUntilFirstConnectionRead() { read_loop_.Run(); }
102 103
103 private: 104 private:
104 static uint16_t GetPort( 105 static uint16_t GetPort(
105 const net::test_server::StreamListenSocket& connection) { 106 const net::test_server::StreamListenSocket& connection) {
106 net::IPEndPoint address; 107 net::IPEndPoint address;
107 EXPECT_EQ(net::OK, connection.GetLocalAddress(&address)); 108 EXPECT_EQ(net::OK, connection.GetPeerAddress(&address));
Ryan Sleevi 2015/07/10 09:46:45 Wait, what? Why this change? Connections are a tu
Yoav Weiss 2015/07/10 10:03:40 GetLocal gives me the port number of the local lis
Ryan Sleevi 2015/07/10 10:09:35 Right, that's basically what I was assuming, but w
108 return address.port(); 109 return address.port();
109 } 110 }
110 111
111 enum SocketStatus { SOCKET_ACCEPTED, SOCKET_READ_FROM }; 112 enum SocketStatus { SOCKET_ACCEPTED, SOCKET_READ_FROM };
112 113
113 typedef base::hash_map<uint16_t, SocketStatus> SocketContainer; 114 typedef base::hash_map<uint16_t, SocketStatus> SocketContainer;
114 SocketContainer sockets_; 115 SocketContainer sockets_;
115 116
116 base::RunLoop accept_loop_; 117 base::RunLoop accept_loop_;
117 base::RunLoop read_loop_; 118 base::RunLoop read_loop_;
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 browser(), 344 browser(),
344 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); 345 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html")));
345 WaitUntilHostHasBeenRequested(kChromiumHostname); 346 WaitUntilHostHasBeenRequested(kChromiumHostname);
346 ASSERT_FALSE(HasHostBeenRequested(kInvalidLongHostname)); 347 ASSERT_FALSE(HasHostBeenRequested(kInvalidLongHostname));
347 ASSERT_EQ(hostnames_requested_before_load + 1, RequestedHostnameCount()); 348 ASSERT_EQ(hostnames_requested_before_load + 1, RequestedHostnameCount());
348 } 349 }
349 350
350 // Tests that preconnect warms up a socket connection to a test server. 351 // Tests that preconnect warms up a socket connection to a test server.
351 // Note: This test uses a data URI to serve the preconnect hint, to make sure 352 // Note: This test uses a data URI to serve the preconnect hint, to make sure
352 // that the network stack doesn't just re-use its connection to the test server. 353 // that the network stack doesn't just re-use its connection to the test server.
353 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, Preconnect) { 354 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectNonCORS) {
354 GURL preconnect_url = embedded_test_server()->base_url(); 355 GURL preconnect_url = embedded_test_server()->base_url();
355 std::string preconnect_content = 356 std::string preconnect_content =
356 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">"; 357 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">";
357 NavigateToDataURLWithContent(preconnect_content); 358 NavigateToDataURLWithContent(preconnect_content);
358 connection_listener_->WaitUntilFirstConnectionAccepted(); 359 connection_listener_->WaitUntilFirstConnectionAccepted();
359 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); 360 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount());
360 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount()); 361 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount());
361 } 362 }
362 363
363 // Tests that preconnect warms up a socket connection to a test server, 364 // Tests that preconnect warms up a socket connection to a test server,
364 // and that that socket is later used when fetching a resource. 365 // and that that socket is later used when fetching a resource.
365 // Note: This test uses a data URI to serve the preconnect hint, to make sure 366 // Note: This test uses a data URI to serve the preconnect hint, to make sure
366 // that the network stack doesn't just re-use its connection to the test server. 367 // that the network stack doesn't just re-use its connection to the test server.
367 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectAndUse) { 368 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectAndFetchNonCORS) {
368 GURL preconnect_url = embedded_test_server()->base_url(); 369 GURL preconnect_url = embedded_test_server()->base_url();
369 // First navigation to content with a preconnect hint. 370 // First navigation to content with a preconnect hint.
370 std::string preconnect_content = 371 std::string preconnect_content =
371 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">"; 372 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">";
372 NavigateToDataURLWithContent(preconnect_content); 373 NavigateToDataURLWithContent(preconnect_content);
373 connection_listener_->WaitUntilFirstConnectionAccepted(); 374 connection_listener_->WaitUntilFirstConnectionAccepted();
374 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); 375 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount());
375 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount()); 376 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount());
376 377
377 // Second navigation to content with an img. 378 // Second navigation to content with an img.
378 std::string img_content = 379 std::string img_content =
379 "<img src=\"" + preconnect_url.spec() + "test.gif\">"; 380 "<img src=\"" + preconnect_url.spec() + "test.gif\">";
380 NavigateToDataURLWithContent(img_content); 381 NavigateToDataURLWithContent(img_content);
381 connection_listener_->WaitUntilFirstConnectionRead(); 382 connection_listener_->WaitUntilFirstConnectionRead();
382 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); 383 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount());
383 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount()); 384 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount());
384 } 385 }
385 386
387 // Tests that preconnect warms up a CORS connection to a test
388 // server, and that socket is later used when fetching a CORS resource.
389 // Note: This test uses a data URI to serve the preconnect hint, to make sure
390 // that the network stack doesn't just re-use its connection to the test server.
391 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectAndFetchCORS) {
392 GURL preconnect_url = embedded_test_server()->base_url();
393 // First navigation to content with a preconnect hint.
394 std::string preconnect_content = "<link rel=\"preconnect\" href=\"" +
395 preconnect_url.spec() + "\" crossorigin>";
396 NavigateToDataURLWithContent(preconnect_content);
397 connection_listener_->WaitUntilFirstConnectionAccepted();
398 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount());
399 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount());
400
401 // Second navigation to content with a font.
402 std::string font_content = "<script>var font = new FontFace('FontA', 'url(" +
403 preconnect_url.spec() +
404 "test.woff2)');font.load();</script>";
405 NavigateToDataURLWithContent(font_content);
406 connection_listener_->WaitUntilFirstConnectionRead();
407 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount());
408 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount());
409 }
410
411 // Tests that preconnect warms up a non-CORS connection to a test
412 // server, but that socket is not used when fetching a CORS resource.
413 // Note: This test uses a data URI to serve the preconnect hint, to make sure
414 // that the network stack doesn't just re-use its connection to the test server.
415 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectNonCORSAndFetchCORS) {
416 GURL preconnect_url = embedded_test_server()->base_url();
417 // First navigation to content with a preconnect hint.
418 std::string preconnect_content =
419 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">";
420 NavigateToDataURLWithContent(preconnect_content);
421 connection_listener_->WaitUntilFirstConnectionAccepted();
422 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount());
423 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount());
424
425 // Second navigation to content with a font.
426 std::string font_content = "<script>var font = new FontFace('FontA', 'url(" +
427 preconnect_url.spec() +
428 "test.woff2)');font.load();</script>";
429 NavigateToDataURLWithContent(font_content);
430 connection_listener_->WaitUntilFirstConnectionRead();
431 EXPECT_EQ(2u, connection_listener_->GetAcceptedSocketCount());
432 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount());
433 }
434
435 // Tests that preconnect warms up a CORS connection to a test server,
436 // but that socket is not used when fetching a non-CORS resource.
437 // Note: This test uses a data URI to serve the preconnect hint, to make sure
438 // that the network stack doesn't just re-use its connection to the test server.
439 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectCORSAndFetchNonCORS) {
440 GURL preconnect_url = embedded_test_server()->base_url();
441 // First navigation to content with a preconnect hint.
442 std::string preconnect_content = "<link rel=\"preconnect\" href=\"" +
443 preconnect_url.spec() + "\" crossorigin>";
444 NavigateToDataURLWithContent(preconnect_content);
445 connection_listener_->WaitUntilFirstConnectionAccepted();
446 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount());
447 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount());
448
449 // Second navigation to content with an img.
450 std::string img_content =
451 "<img src=\"" + preconnect_url.spec() + "test.gif\">";
452 NavigateToDataURLWithContent(img_content);
453 connection_listener_->WaitUntilFirstConnectionRead();
454 EXPECT_EQ(2u, connection_listener_->GetAcceptedSocketCount());
455 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount());
456 }
457
386 } // namespace chrome_browser_net 458 } // namespace chrome_browser_net
387 459
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698