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 <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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 return read_sockets; | 96 return read_sockets; |
97 } | 97 } |
98 | 98 |
99 void WaitUntilFirstConnectionAccepted() { accept_loop_.Run(); } | 99 void WaitUntilFirstConnectionAccepted() { accept_loop_.Run(); } |
100 | 100 |
101 void WaitUntilFirstConnectionRead() { read_loop_.Run(); } | 101 void WaitUntilFirstConnectionRead() { read_loop_.Run(); } |
102 | 102 |
103 private: | 103 private: |
104 static uint16_t GetPort( | 104 static uint16_t GetPort( |
105 const net::test_server::StreamListenSocket& connection) { | 105 const net::test_server::StreamListenSocket& connection) { |
106 // Get the remote port of the peer, since the local port will always be the | |
107 // port the test server is listening on. This isn't strictly correct - it's | |
108 // possible for multiple peers to connect with the same remote port but | |
109 // different remote IPs - but the tests here assume that connections to the | |
110 // test server (running on localhost) will always come from localhost, and | |
111 // thus the peer port is all thats needed to distinguish two connections. | |
112 // This also would be problematic if the OS reused ports, but that's not | |
113 // something to worry about for these tests. | |
mmenke
2015/07/13 21:02:01
optional: Could just switch to a map of IPEndpoin
Yoav Weiss
2015/07/13 22:11:37
Maybe in a followup CL?
mmenke
2015/07/13 22:30:16
Works for me (And, as I said, it's optional, so no
| |
106 net::IPEndPoint address; | 114 net::IPEndPoint address; |
107 EXPECT_EQ(net::OK, connection.GetLocalAddress(&address)); | 115 EXPECT_EQ(net::OK, connection.GetPeerAddress(&address)); |
108 return address.port(); | 116 return address.port(); |
109 } | 117 } |
110 | 118 |
111 enum SocketStatus { SOCKET_ACCEPTED, SOCKET_READ_FROM }; | 119 enum SocketStatus { SOCKET_ACCEPTED, SOCKET_READ_FROM }; |
112 | 120 |
113 typedef base::hash_map<uint16_t, SocketStatus> SocketContainer; | 121 typedef base::hash_map<uint16_t, SocketStatus> SocketContainer; |
114 SocketContainer sockets_; | 122 SocketContainer sockets_; |
115 | 123 |
116 base::RunLoop accept_loop_; | 124 base::RunLoop accept_loop_; |
117 base::RunLoop read_loop_; | 125 base::RunLoop read_loop_; |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
343 browser(), | 351 browser(), |
344 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); | 352 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); |
345 WaitUntilHostHasBeenRequested(kChromiumHostname); | 353 WaitUntilHostHasBeenRequested(kChromiumHostname); |
346 ASSERT_FALSE(HasHostBeenRequested(kInvalidLongHostname)); | 354 ASSERT_FALSE(HasHostBeenRequested(kInvalidLongHostname)); |
347 ASSERT_EQ(hostnames_requested_before_load + 1, RequestedHostnameCount()); | 355 ASSERT_EQ(hostnames_requested_before_load + 1, RequestedHostnameCount()); |
348 } | 356 } |
349 | 357 |
350 // Tests that preconnect warms up a socket connection to a test server. | 358 // 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 | 359 // 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. | 360 // that the network stack doesn't just re-use its connection to the test server. |
353 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, Preconnect) { | 361 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectNonCORS) { |
354 GURL preconnect_url = embedded_test_server()->base_url(); | 362 GURL preconnect_url = embedded_test_server()->base_url(); |
355 std::string preconnect_content = | 363 std::string preconnect_content = |
356 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">"; | 364 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">"; |
357 NavigateToDataURLWithContent(preconnect_content); | 365 NavigateToDataURLWithContent(preconnect_content); |
358 connection_listener_->WaitUntilFirstConnectionAccepted(); | 366 connection_listener_->WaitUntilFirstConnectionAccepted(); |
359 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); | 367 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); |
360 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount()); | 368 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount()); |
361 } | 369 } |
362 | 370 |
363 // Tests that preconnect warms up a socket connection to a test server, | 371 // Tests that preconnect warms up a socket connection to a test server, |
364 // and that that socket is later used when fetching a resource. | 372 // 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 | 373 // 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. | 374 // that the network stack doesn't just re-use its connection to the test server. |
367 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectAndUse) { | 375 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectAndFetchNonCORS) { |
368 GURL preconnect_url = embedded_test_server()->base_url(); | 376 GURL preconnect_url = embedded_test_server()->base_url(); |
369 // First navigation to content with a preconnect hint. | 377 // First navigation to content with a preconnect hint. |
370 std::string preconnect_content = | 378 std::string preconnect_content = |
371 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">"; | 379 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">"; |
372 NavigateToDataURLWithContent(preconnect_content); | 380 NavigateToDataURLWithContent(preconnect_content); |
373 connection_listener_->WaitUntilFirstConnectionAccepted(); | 381 connection_listener_->WaitUntilFirstConnectionAccepted(); |
374 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); | 382 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); |
375 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount()); | 383 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount()); |
376 | 384 |
377 // Second navigation to content with an img. | 385 // Second navigation to content with an img. |
378 std::string img_content = | 386 std::string img_content = |
379 "<img src=\"" + preconnect_url.spec() + "test.gif\">"; | 387 "<img src=\"" + preconnect_url.spec() + "test.gif\">"; |
380 NavigateToDataURLWithContent(img_content); | 388 NavigateToDataURLWithContent(img_content); |
381 connection_listener_->WaitUntilFirstConnectionRead(); | 389 connection_listener_->WaitUntilFirstConnectionRead(); |
382 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); | 390 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); |
383 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount()); | 391 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount()); |
384 } | 392 } |
385 | 393 |
394 // Tests that preconnect warms up a CORS connection to a test | |
395 // server, and that socket is later used when fetching a CORS resource. | |
396 // Note: This test uses a data URI to serve the preconnect hint, to make sure | |
397 // that the network stack doesn't just re-use its connection to the test server. | |
398 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectAndFetchCORS) { | |
399 GURL preconnect_url = embedded_test_server()->base_url(); | |
400 // First navigation to content with a preconnect hint. | |
401 std::string preconnect_content = "<link rel=\"preconnect\" href=\"" + | |
402 preconnect_url.spec() + "\" crossorigin>"; | |
403 NavigateToDataURLWithContent(preconnect_content); | |
404 connection_listener_->WaitUntilFirstConnectionAccepted(); | |
405 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); | |
406 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount()); | |
407 | |
408 // Second navigation to content with a font. | |
409 std::string font_content = "<script>var font = new FontFace('FontA', 'url(" + | |
410 preconnect_url.spec() + | |
411 "test.woff2)');font.load();</script>"; | |
412 NavigateToDataURLWithContent(font_content); | |
413 connection_listener_->WaitUntilFirstConnectionRead(); | |
414 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); | |
415 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount()); | |
416 } | |
417 | |
418 // Tests that preconnect warms up a non-CORS connection to a test | |
419 // server, but that socket is not used when fetching a CORS resource. | |
420 // Note: This test uses a data URI to serve the preconnect hint, to make sure | |
421 // that the network stack doesn't just re-use its connection to the test server. | |
422 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectNonCORSAndFetchCORS) { | |
423 GURL preconnect_url = embedded_test_server()->base_url(); | |
424 // First navigation to content with a preconnect hint. | |
425 std::string preconnect_content = | |
426 "<link rel=\"preconnect\" href=\"" + preconnect_url.spec() + "\">"; | |
427 NavigateToDataURLWithContent(preconnect_content); | |
428 connection_listener_->WaitUntilFirstConnectionAccepted(); | |
429 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); | |
430 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount()); | |
431 | |
432 // Second navigation to content with a font. | |
433 std::string font_content = "<script>var font = new FontFace('FontA', 'url(" + | |
434 preconnect_url.spec() + | |
435 "test.woff2)');font.load();</script>"; | |
436 NavigateToDataURLWithContent(font_content); | |
437 connection_listener_->WaitUntilFirstConnectionRead(); | |
438 EXPECT_EQ(2u, connection_listener_->GetAcceptedSocketCount()); | |
439 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount()); | |
440 } | |
441 | |
442 // Tests that preconnect warms up a CORS connection to a test server, | |
443 // but that socket is not used when fetching a non-CORS resource. | |
444 // Note: This test uses a data URI to serve the preconnect hint, to make sure | |
445 // that the network stack doesn't just re-use its connection to the test server. | |
446 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, PreconnectCORSAndFetchNonCORS) { | |
447 GURL preconnect_url = embedded_test_server()->base_url(); | |
448 // First navigation to content with a preconnect hint. | |
449 std::string preconnect_content = "<link rel=\"preconnect\" href=\"" + | |
450 preconnect_url.spec() + "\" crossorigin>"; | |
451 NavigateToDataURLWithContent(preconnect_content); | |
452 connection_listener_->WaitUntilFirstConnectionAccepted(); | |
453 EXPECT_EQ(1u, connection_listener_->GetAcceptedSocketCount()); | |
454 EXPECT_EQ(0u, connection_listener_->GetReadSocketCount()); | |
455 | |
456 // Second navigation to content with an img. | |
457 std::string img_content = | |
458 "<img src=\"" + preconnect_url.spec() + "test.gif\">"; | |
459 NavigateToDataURLWithContent(img_content); | |
460 connection_listener_->WaitUntilFirstConnectionRead(); | |
461 EXPECT_EQ(2u, connection_listener_->GetAcceptedSocketCount()); | |
462 EXPECT_EQ(1u, connection_listener_->GetReadSocketCount()); | |
463 } | |
464 | |
386 } // namespace chrome_browser_net | 465 } // namespace chrome_browser_net |
387 | 466 |
OLD | NEW |