| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/http/http_server_properties_manager.h" | 5 #include "net/http/http_server_properties_manager.h" |
| 6 | 6 |
| 7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/test/test_simple_task_runner.h" | 15 #include "base/test/test_simple_task_runner.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "net/base/ip_address.h" | 18 #include "net/base/ip_address.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "url/gurl.h" | |
| 22 | 21 |
| 23 namespace net { | 22 namespace net { |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 using base::StringPrintf; | 26 using base::StringPrintf; |
| 28 using ::testing::_; | 27 using ::testing::_; |
| 29 using ::testing::Invoke; | 28 using ::testing::Invoke; |
| 30 using ::testing::Mock; | 29 using ::testing::Mock; |
| 31 using ::testing::StrictMock; | 30 using ::testing::StrictMock; |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 | 225 |
| 227 TEST_P(HttpServerPropertiesManagerTest, | 226 TEST_P(HttpServerPropertiesManagerTest, |
| 228 SingleUpdateForTwoSpdyServerPrefChanges) { | 227 SingleUpdateForTwoSpdyServerPrefChanges) { |
| 229 ExpectCacheUpdate(); | 228 ExpectCacheUpdate(); |
| 230 | 229 |
| 231 // Set up the prefs for www.google.com:80 and mail.google.com:80 and then set | 230 // Set up the prefs for www.google.com:80 and mail.google.com:80 and then set |
| 232 // it twice. Only expect a single cache update. | 231 // it twice. Only expect a single cache update. |
| 233 | 232 |
| 234 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; | 233 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; |
| 235 HostPortPair google_server("www.google.com", 80); | 234 HostPortPair google_server("www.google.com", 80); |
| 235 url::SchemeHostPort google_scheme_host_port("http", "www.google.com", 80); |
| 236 HostPortPair mail_server("mail.google.com", 80); | 236 HostPortPair mail_server("mail.google.com", 80); |
| 237 url::SchemeHostPort mail_scheme_host_port("http", "mail.google.com", 80); |
| 237 | 238 |
| 238 // Set supports_spdy for www.google.com:80. | 239 // Set supports_spdy for www.google.com:80. |
| 239 server_pref_dict->SetBoolean("supports_spdy", true); | 240 server_pref_dict->SetBoolean("supports_spdy", true); |
| 240 | 241 |
| 241 // Set up alternative_services for www.google.com:80. | 242 // Set up alternative_services for www.google.com:80. |
| 242 base::DictionaryValue* alternative_service_dict0 = new base::DictionaryValue; | 243 base::DictionaryValue* alternative_service_dict0 = new base::DictionaryValue; |
| 243 alternative_service_dict0->SetInteger("port", 443); | 244 alternative_service_dict0->SetInteger("port", 443); |
| 244 alternative_service_dict0->SetString("protocol_str", "npn-h2"); | 245 alternative_service_dict0->SetString("protocol_str", "npn-h2"); |
| 245 base::DictionaryValue* alternative_service_dict1 = new base::DictionaryValue; | 246 base::DictionaryValue* alternative_service_dict1 = new base::DictionaryValue; |
| 246 alternative_service_dict1->SetInteger("port", 1234); | 247 alternative_service_dict1->SetInteger("port", 1234); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 quic_servers_dict); | 341 quic_servers_dict); |
| 341 | 342 |
| 342 // Set the same value for kHttpServerProperties multiple times. | 343 // Set the same value for kHttpServerProperties multiple times. |
| 343 pref_delegate_->SetPrefs(http_server_properties_dict); | 344 pref_delegate_->SetPrefs(http_server_properties_dict); |
| 344 pref_delegate_->SetPrefs(http_server_properties_dict); | 345 pref_delegate_->SetPrefs(http_server_properties_dict); |
| 345 | 346 |
| 346 base::RunLoop().RunUntilIdle(); | 347 base::RunLoop().RunUntilIdle(); |
| 347 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | 348 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 348 | 349 |
| 349 // Verify SupportsSpdy. | 350 // Verify SupportsSpdy. |
| 350 EXPECT_TRUE( | 351 EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority( |
| 351 http_server_props_manager_->SupportsRequestPriority(google_server)); | 352 google_scheme_host_port)); |
| 352 EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(mail_server)); | 353 EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority( |
| 354 mail_scheme_host_port)); |
| 355 HostPortPair foo_server = HostPortPair::FromString("foo.google.com:1337"); |
| 356 url::SchemeHostPort foo_scheme_host_port("http", foo_server.host(), |
| 357 foo_server.port()); |
| 358 |
| 353 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority( | 359 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority( |
| 354 HostPortPair::FromString("foo.google.com:1337"))); | 360 foo_scheme_host_port)); |
| 355 | 361 |
| 356 // Verify alternative service. | 362 // Verify alternative service. |
| 357 if (GetParam() == 4) { | 363 if (GetParam() == 4) { |
| 358 const AlternativeServiceMap& map = | 364 const AlternativeServiceMap& map = |
| 359 http_server_props_manager_->alternative_service_map(); | 365 http_server_props_manager_->alternative_service_map(); |
| 360 ASSERT_EQ(2u, map.size()); | 366 ASSERT_EQ(2u, map.size()); |
| 361 | 367 |
| 362 AlternativeServiceMap::const_iterator map_it = map.begin(); | 368 AlternativeServiceMap::const_iterator map_it = map.begin(); |
| 363 EXPECT_EQ("mail.google.com", map_it->first.host()); | 369 EXPECT_EQ("mail.google.com", map_it->first.host()); |
| 364 ASSERT_EQ(1u, map_it->second.size()); | 370 ASSERT_EQ(1u, map_it->second.size()); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 http_server_properties_dict.SetWithoutPathExpansion("quic_servers", | 487 http_server_properties_dict.SetWithoutPathExpansion("quic_servers", |
| 482 quic_servers_dict); | 488 quic_servers_dict); |
| 483 | 489 |
| 484 // Set up the pref. | 490 // Set up the pref. |
| 485 pref_delegate_->SetPrefs(http_server_properties_dict); | 491 pref_delegate_->SetPrefs(http_server_properties_dict); |
| 486 | 492 |
| 487 base::RunLoop().RunUntilIdle(); | 493 base::RunLoop().RunUntilIdle(); |
| 488 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | 494 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 489 | 495 |
| 490 // Verify that nothing is set. | 496 // Verify that nothing is set. |
| 497 HostPortPair google_server = HostPortPair::FromString("www.google.com:65536"); |
| 498 url::SchemeHostPort google_scheme_host_port("http", google_server.host(), |
| 499 google_server.port()); |
| 500 |
| 491 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority( | 501 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority( |
| 492 HostPortPair::FromString("www.google.com:65536"))); | 502 google_scheme_host_port)); |
| 493 EXPECT_FALSE( | 503 EXPECT_FALSE( |
| 494 HasAlternativeService(HostPortPair::FromString("www.google.com:65536"))); | 504 HasAlternativeService(HostPortPair::FromString("www.google.com:65536"))); |
| 495 const ServerNetworkStats* stats1 = | 505 const ServerNetworkStats* stats1 = |
| 496 http_server_props_manager_->GetServerNetworkStats( | 506 http_server_props_manager_->GetServerNetworkStats( |
| 497 HostPortPair::FromString("www.google.com:65536")); | 507 HostPortPair::FromString("www.google.com:65536")); |
| 498 EXPECT_EQ(nullptr, stats1); | 508 EXPECT_EQ(nullptr, stats1); |
| 499 EXPECT_EQ(0u, http_server_props_manager_->quic_server_info_map().size()); | 509 EXPECT_EQ(0u, http_server_props_manager_->quic_server_info_map().size()); |
| 500 } | 510 } |
| 501 | 511 |
| 502 TEST_P(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) { | 512 TEST_P(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 | 560 |
| 551 TEST_P(HttpServerPropertiesManagerTest, SupportsSpdy) { | 561 TEST_P(HttpServerPropertiesManagerTest, SupportsSpdy) { |
| 552 ExpectPrefsUpdate(); | 562 ExpectPrefsUpdate(); |
| 553 ExpectScheduleUpdatePrefsOnNetworkThread(); | 563 ExpectScheduleUpdatePrefsOnNetworkThread(); |
| 554 | 564 |
| 555 // Post an update task to the network thread. SetSupportsSpdy calls | 565 // Post an update task to the network thread. SetSupportsSpdy calls |
| 556 // ScheduleUpdatePrefsOnNetworkThread. | 566 // ScheduleUpdatePrefsOnNetworkThread. |
| 557 | 567 |
| 558 // Add mail.google.com:443 as a supporting spdy server. | 568 // Add mail.google.com:443 as a supporting spdy server. |
| 559 HostPortPair spdy_server_mail("mail.google.com", 443); | 569 HostPortPair spdy_server_mail("mail.google.com", 443); |
| 570 url::SchemeHostPort spdy_server("https", "mail.google.com", 443); |
| 560 EXPECT_FALSE( | 571 EXPECT_FALSE( |
| 561 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail)); | 572 http_server_props_manager_->SupportsRequestPriority(spdy_server)); |
| 562 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true); | 573 http_server_props_manager_->SetSupportsSpdy(spdy_server, true); |
| 563 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once. | 574 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once. |
| 564 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true); | 575 http_server_props_manager_->SetSupportsSpdy(spdy_server, true); |
| 565 | 576 |
| 566 // Run the task. | 577 // Run the task. |
| 567 base::RunLoop().RunUntilIdle(); | 578 base::RunLoop().RunUntilIdle(); |
| 568 | 579 |
| 569 EXPECT_TRUE( | 580 EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(spdy_server)); |
| 570 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail)); | |
| 571 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | 581 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 572 } | 582 } |
| 573 | 583 |
| 574 TEST_P(HttpServerPropertiesManagerTest, SetSpdySetting) { | 584 TEST_P(HttpServerPropertiesManagerTest, SetSpdySetting) { |
| 575 ExpectPrefsUpdate(); | 585 ExpectPrefsUpdate(); |
| 576 ExpectScheduleUpdatePrefsOnNetworkThread(); | 586 ExpectScheduleUpdatePrefsOnNetworkThread(); |
| 577 | 587 |
| 578 // Add SpdySetting for mail.google.com:443. | 588 // Add SpdySetting for mail.google.com:443. |
| 579 HostPortPair spdy_server_mail("mail.google.com", 443); | 589 HostPortPair spdy_server_mail("mail.google.com", 443); |
| 580 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; | 590 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 876 | 886 |
| 877 EXPECT_EQ(quic_server_info1, *http_server_props_manager_->GetQuicServerInfo( | 887 EXPECT_EQ(quic_server_info1, *http_server_props_manager_->GetQuicServerInfo( |
| 878 mail_quic_server_id)); | 888 mail_quic_server_id)); |
| 879 } | 889 } |
| 880 | 890 |
| 881 TEST_P(HttpServerPropertiesManagerTest, Clear) { | 891 TEST_P(HttpServerPropertiesManagerTest, Clear) { |
| 882 ExpectPrefsUpdate(); | 892 ExpectPrefsUpdate(); |
| 883 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly(); | 893 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly(); |
| 884 | 894 |
| 885 HostPortPair spdy_server_mail("mail.google.com", 443); | 895 HostPortPair spdy_server_mail("mail.google.com", 443); |
| 886 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true); | 896 url::SchemeHostPort spdy_server("https", "mail.google.com", 443); |
| 897 http_server_props_manager_->SetSupportsSpdy(spdy_server, true); |
| 887 AlternativeService alternative_service(NPN_HTTP_2, "mail.google.com", 1234); | 898 AlternativeService alternative_service(NPN_HTTP_2, "mail.google.com", 1234); |
| 888 http_server_props_manager_->SetAlternativeService( | 899 http_server_props_manager_->SetAlternativeService( |
| 889 spdy_server_mail, alternative_service, one_day_from_now_); | 900 spdy_server_mail, alternative_service, one_day_from_now_); |
| 890 IPAddress actual_address(127, 0, 0, 1); | 901 IPAddress actual_address(127, 0, 0, 1); |
| 891 http_server_props_manager_->SetSupportsQuic(true, actual_address); | 902 http_server_props_manager_->SetSupportsQuic(true, actual_address); |
| 892 ServerNetworkStats stats; | 903 ServerNetworkStats stats; |
| 893 stats.srtt = base::TimeDelta::FromMicroseconds(10); | 904 stats.srtt = base::TimeDelta::FromMicroseconds(10); |
| 894 http_server_props_manager_->SetServerNetworkStats(spdy_server_mail, stats); | 905 http_server_props_manager_->SetServerNetworkStats(spdy_server_mail, stats); |
| 895 | 906 |
| 896 QuicServerId mail_quic_server_id("mail.google.com", 80); | 907 QuicServerId mail_quic_server_id("mail.google.com", 80); |
| 897 std::string quic_server_info1("quic_server_info1"); | 908 std::string quic_server_info1("quic_server_info1"); |
| 898 http_server_props_manager_->SetQuicServerInfo(mail_quic_server_id, | 909 http_server_props_manager_->SetQuicServerInfo(mail_quic_server_id, |
| 899 quic_server_info1); | 910 quic_server_info1); |
| 900 | 911 |
| 901 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; | 912 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; |
| 902 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST; | 913 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST; |
| 903 const uint32_t value1 = 31337; | 914 const uint32_t value1 = 31337; |
| 904 http_server_props_manager_->SetSpdySetting(spdy_server_mail, id1, flags1, | 915 http_server_props_manager_->SetSpdySetting(spdy_server_mail, id1, flags1, |
| 905 value1); | 916 value1); |
| 906 | 917 |
| 907 // Run the task. | 918 // Run the task. |
| 908 base::RunLoop().RunUntilIdle(); | 919 base::RunLoop().RunUntilIdle(); |
| 909 | 920 |
| 910 EXPECT_TRUE( | 921 EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(spdy_server)); |
| 911 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail)); | |
| 912 EXPECT_TRUE(HasAlternativeService(spdy_server_mail)); | 922 EXPECT_TRUE(HasAlternativeService(spdy_server_mail)); |
| 913 IPAddress address; | 923 IPAddress address; |
| 914 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address)); | 924 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address)); |
| 915 EXPECT_EQ(actual_address, address); | 925 EXPECT_EQ(actual_address, address); |
| 916 const ServerNetworkStats* stats1 = | 926 const ServerNetworkStats* stats1 = |
| 917 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail); | 927 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail); |
| 918 EXPECT_EQ(10, stats1->srtt.ToInternalValue()); | 928 EXPECT_EQ(10, stats1->srtt.ToInternalValue()); |
| 919 EXPECT_EQ(quic_server_info1, *http_server_props_manager_->GetQuicServerInfo( | 929 EXPECT_EQ(quic_server_info1, *http_server_props_manager_->GetQuicServerInfo( |
| 920 mail_quic_server_id)); | 930 mail_quic_server_id)); |
| 921 | 931 |
| 922 // Check SPDY settings values. | 932 // Check SPDY settings values. |
| 923 const SettingsMap& settings_map1_ret = | 933 const SettingsMap& settings_map1_ret = |
| 924 http_server_props_manager_->GetSpdySettings(spdy_server_mail); | 934 http_server_props_manager_->GetSpdySettings(spdy_server_mail); |
| 925 ASSERT_EQ(1U, settings_map1_ret.size()); | 935 ASSERT_EQ(1U, settings_map1_ret.size()); |
| 926 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); | 936 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); |
| 927 EXPECT_TRUE(it1_ret != settings_map1_ret.end()); | 937 EXPECT_TRUE(it1_ret != settings_map1_ret.end()); |
| 928 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; | 938 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; |
| 929 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); | 939 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); |
| 930 EXPECT_EQ(value1, flags_and_value1_ret.second); | 940 EXPECT_EQ(value1, flags_and_value1_ret.second); |
| 931 | 941 |
| 932 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | 942 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 933 | 943 |
| 934 ExpectPrefsUpdate(); | 944 ExpectPrefsUpdate(); |
| 935 | 945 |
| 936 // Clear http server data, time out if we do not get a completion callback. | 946 // Clear http server data, time out if we do not get a completion callback. |
| 937 http_server_props_manager_->Clear(base::MessageLoop::QuitWhenIdleClosure()); | 947 http_server_props_manager_->Clear(base::MessageLoop::QuitWhenIdleClosure()); |
| 938 base::RunLoop().Run(); | 948 base::RunLoop().Run(); |
| 939 | 949 |
| 940 EXPECT_FALSE( | 950 EXPECT_FALSE( |
| 941 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail)); | 951 http_server_props_manager_->SupportsRequestPriority(spdy_server)); |
| 942 EXPECT_FALSE(HasAlternativeService(spdy_server_mail)); | 952 EXPECT_FALSE(HasAlternativeService(spdy_server_mail)); |
| 943 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address)); | 953 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address)); |
| 944 const ServerNetworkStats* stats2 = | 954 const ServerNetworkStats* stats2 = |
| 945 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail); | 955 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail); |
| 946 EXPECT_EQ(nullptr, stats2); | 956 EXPECT_EQ(nullptr, stats2); |
| 947 EXPECT_EQ(nullptr, | 957 EXPECT_EQ(nullptr, |
| 948 http_server_props_manager_->GetQuicServerInfo(mail_quic_server_id)); | 958 http_server_props_manager_->GetQuicServerInfo(mail_quic_server_id)); |
| 949 | 959 |
| 950 const SettingsMap& settings_map2_ret = | 960 const SettingsMap& settings_map2_ret = |
| 951 http_server_props_manager_->GetSpdySettings(spdy_server_mail); | 961 http_server_props_manager_->GetSpdySettings(spdy_server_mail); |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1335 // Shutdown comes before the task is executed. | 1345 // Shutdown comes before the task is executed. |
| 1336 http_server_props_manager_->ShutdownOnPrefThread(); | 1346 http_server_props_manager_->ShutdownOnPrefThread(); |
| 1337 // Run the task after shutdown, but before deletion. | 1347 // Run the task after shutdown, but before deletion. |
| 1338 base::RunLoop().RunUntilIdle(); | 1348 base::RunLoop().RunUntilIdle(); |
| 1339 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | 1349 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); |
| 1340 http_server_props_manager_.reset(); | 1350 http_server_props_manager_.reset(); |
| 1341 base::RunLoop().RunUntilIdle(); | 1351 base::RunLoop().RunUntilIdle(); |
| 1342 } | 1352 } |
| 1343 | 1353 |
| 1344 } // namespace net | 1354 } // namespace net |
| OLD | NEW |