Index: net/http/http_server_properties_manager_unittest.cc |
diff --git a/net/http/http_server_properties_manager_unittest.cc b/net/http/http_server_properties_manager_unittest.cc |
index b10b3379edbd85b948d92e042e13282fb16e4a70..a5a0a923a451106aa5efe1986c09e748399ad841 100644 |
--- a/net/http/http_server_properties_manager_unittest.cc |
+++ b/net/http/http_server_properties_manager_unittest.cc |
@@ -22,6 +22,7 @@ |
#include "base/threading/thread_task_runner_handle.h" |
#include "base/values.h" |
#include "net/base/ip_address.h" |
+#include "net/http/http_network_session.h" |
#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "url/gurl.h" |
@@ -163,6 +164,7 @@ class HttpServerPropertiesManagerTest : public testing::TestWithParam<int> { |
void SetUp() override { |
one_day_from_now_ = base::Time::Now() + base::TimeDelta::FromDays(1); |
+ advertised_versions_ = HttpNetworkSession::Params().quic_supported_versions; |
pref_delegate_ = new MockPrefDelegate; |
http_server_props_manager_.reset( |
new StrictMock<TestingHttpServerPropertiesManager>( |
@@ -238,6 +240,7 @@ class HttpServerPropertiesManagerTest : public testing::TestWithParam<int> { |
std::unique_ptr<TestingHttpServerPropertiesManager> |
http_server_props_manager_; |
base::Time one_day_from_now_; |
+ QuicVersionVector advertised_versions_; |
// Overrides the main thread's message loop with a mock tick clock. Making the |
// main thread the |pref_test_task_runner_| matches expectations better than |
@@ -729,10 +732,12 @@ TEST_P(HttpServerPropertiesManagerTest, GetAlternativeServiceInfos) { |
const AlternativeService alternative_service(kProtoHTTP2, "mail.google.com", |
443); |
http_server_props_manager_->SetAlternativeService( |
- spdy_server_mail, alternative_service, one_day_from_now_); |
+ spdy_server_mail, alternative_service, one_day_from_now_, |
+ QuicVersionVector()); |
// ExpectScheduleUpdatePrefsOnNetworkSequence() should be called only once. |
http_server_props_manager_->SetAlternativeService( |
- spdy_server_mail, alternative_service, one_day_from_now_); |
+ spdy_server_mail, alternative_service, one_day_from_now_, |
+ QuicVersionVector()); |
// Run the task. |
EXPECT_FALSE(pref_test_task_runner_->HasPendingTask()); |
@@ -825,7 +830,8 @@ TEST_P(HttpServerPropertiesManagerTest, ConfirmAlternativeService) { |
ExpectScheduleUpdatePrefsOnNetworkSequence(); |
http_server_props_manager_->SetAlternativeService( |
- spdy_server_mail, alternative_service, one_day_from_now_); |
+ spdy_server_mail, alternative_service, one_day_from_now_, |
+ QuicVersionVector()); |
EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken( |
alternative_service)); |
@@ -998,7 +1004,8 @@ TEST_P(HttpServerPropertiesManagerTest, Clear) { |
AlternativeService alternative_service(kProtoHTTP2, "mail.google.com", |
1234); |
http_server_props_manager_->SetAlternativeService( |
- spdy_server, alternative_service, one_day_from_now_); |
+ spdy_server, alternative_service, one_day_from_now_, |
+ QuicVersionVector()); |
http_server_props_manager_->SetSupportsQuic(true, actual_address); |
ServerNetworkStats stats; |
stats.srtt = base::TimeDelta::FromMicroseconds(10); |
@@ -1169,6 +1176,7 @@ TEST_P(HttpServerPropertiesManagerTest, UpdatePrefsWithCache) { |
ASSERT_TRUE(base::Time::FromUTCString("2036-12-01 10:00:00", &expiration1)); |
alternative_service_info_vector.push_back( |
AlternativeServiceInfo(www_alternative_service1, expiration1)); |
+ |
AlternativeService www_alternative_service2(kProtoHTTP2, "www.google.com", |
1234); |
base::Time expiration2; |
@@ -1182,7 +1190,7 @@ TEST_P(HttpServerPropertiesManagerTest, UpdatePrefsWithCache) { |
444); |
base::Time expiration3 = base::Time::Max(); |
ASSERT_TRUE(http_server_props_manager_->SetAlternativeService( |
- server_mail, mail_alternative_service, expiration3)); |
+ server_mail, mail_alternative_service, expiration3, QuicVersionVector())); |
// #3: Set ServerNetworkStats. |
ServerNetworkStats stats; |
@@ -1213,17 +1221,15 @@ TEST_P(HttpServerPropertiesManagerTest, UpdatePrefsWithCache) { |
const char expected_json[] = |
"{\"quic_servers\":{\"https://" |
"mail.google.com:80\":{\"server_info\":\"quic_server_info1\"}}," |
- "\"servers\":[" |
- "{\"https://www.google.com:80\":{" |
- "\"alternative_service\":[{\"expiration\":\"13756212000000000\"," |
- "\"port\":443,\"protocol_str\":\"h2\"}," |
- "{\"expiration\":\"13758804000000000\",\"host\":\"www.google.com\"," |
- "\"port\":1234,\"protocol_str\":\"h2\"}]}}," |
+ "\"servers\":[{\"https://www.google.com:80\":{" |
+ "\"alternative_service\":[{\"advertised_versions\":[],\"expiration\":" |
+ "\"13756212000000000\",\"port\":443,\"protocol_str\":\"h2\"}," |
+ "{\"advertised_versions\":[],\"expiration\":\"13758804000000000\"," |
+ "\"host\":\"www.google.com\",\"port\":1234,\"protocol_str\":\"h2\"}]}}," |
"{\"https://mail.google.com:80\":{\"alternative_service\":[{" |
- "\"expiration\":\"9223372036854775807\",\"host\":\"foo.google.com\"," |
- "\"port\":444,\"protocol_str\":\"h2\"}]," |
- "\"network_stats\":{\"srtt\":42}}}" |
- "]," |
+ "\"advertised_versions\":[],\"expiration\":\"9223372036854775807\"," |
+ "\"host\":\"foo.google.com\",\"port\":444,\"protocol_str\":\"h2\"}]," |
+ "\"network_stats\":{\"srtt\":42}}}]," |
"\"supports_quic\":{\"address\":\"127.0.0.1\",\"used_quic\":true}," |
"\"version\":5}"; |
@@ -1567,4 +1573,232 @@ TEST_P(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs2) { |
EXPECT_FALSE(pref_test_task_runner_->HasPendingTask()); |
} |
+TEST_P(HttpServerPropertiesManagerTest, PersistAdvertisedVersionsToPref) { |
+ ExpectScheduleUpdatePrefsOnNetworkSequenceRepeatedly(5); |
+ |
+ const url::SchemeHostPort server_www("https", "www.google.com", 80); |
+ const url::SchemeHostPort server_mail("https", "mail.google.com", 80); |
+ |
+ // #1 & #2: Set alternate protocol. |
+ AlternativeServiceInfoVector alternative_service_info_vector; |
+ // Quic alternative service set with two advertised QUIC versions. |
+ AlternativeService quic_alternative_service1(kProtoQUIC, "", 443); |
+ base::Time expiration1; |
+ ASSERT_TRUE(base::Time::FromUTCString("2036-12-01 10:00:00", &expiration1)); |
+ QuicVersionVector advertised_versions = {QUIC_VERSION_37, QUIC_VERSION_35}; |
+ alternative_service_info_vector.push_back(AlternativeServiceInfo( |
+ quic_alternative_service1, expiration1, advertised_versions)); |
+ // HTTP/2 alternative service should not set any advertised version. |
+ AlternativeService h2_alternative_service(kProtoHTTP2, "www.google.com", |
+ 1234); |
+ base::Time expiration2; |
+ ASSERT_TRUE(base::Time::FromUTCString("2036-12-31 10:00:00", &expiration2)); |
+ alternative_service_info_vector.push_back( |
+ AlternativeServiceInfo(h2_alternative_service, expiration2)); |
+ ASSERT_TRUE(http_server_props_manager_->SetAlternativeServices( |
+ server_www, alternative_service_info_vector)); |
+ |
+ // Set another QUIC alternative service with a single advertised QUIC version. |
+ AlternativeService mail_alternative_service(kProtoQUIC, "foo.google.com", |
+ 444); |
+ base::Time expiration3 = base::Time::Max(); |
+ ASSERT_TRUE(http_server_props_manager_->SetAlternativeService( |
+ server_mail, mail_alternative_service, expiration3, |
+ advertised_versions_)); |
+ // #3: Set ServerNetworkStats. |
+ ServerNetworkStats stats; |
+ stats.srtt = base::TimeDelta::FromInternalValue(42); |
+ http_server_props_manager_->SetServerNetworkStats(server_mail, stats); |
+ |
+ // #4: Set quic_server_info string. |
+ QuicServerId mail_quic_server_id("mail.google.com", 80); |
+ std::string quic_server_info1("quic_server_info1"); |
+ http_server_props_manager_->SetQuicServerInfo(mail_quic_server_id, |
+ quic_server_info1); |
+ |
+ // #5: Set SupportsQuic. |
+ IPAddress actual_address(127, 0, 0, 1); |
+ http_server_props_manager_->SetSupportsQuic(true, actual_address); |
+ |
+ // Update Prefs. |
+ ExpectPrefsUpdate(1); |
+ EXPECT_TRUE(net_test_task_runner_->HasPendingTask()); |
+ EXPECT_FALSE(pref_test_task_runner_->HasPendingTask()); |
+ net_test_task_runner_->FastForwardUntilNoTasksRemain(); |
+ EXPECT_TRUE(pref_test_task_runner_->HasPendingTask()); |
+ pref_test_task_runner_->FastForwardUntilNoTasksRemain(); |
+ EXPECT_FALSE(net_test_task_runner_->HasPendingTask()); |
+ EXPECT_FALSE(pref_test_task_runner_->HasPendingTask()); |
+ |
+ // Verify preferences with correct advertised version field. |
+ const char expected_json[] = |
+ "{\"quic_servers\":{\"https://mail.google.com:80\":{" |
+ "\"server_info\":\"quic_server_info1\"}},\"servers\":[" |
+ "{\"https://www.google.com:80\":{\"alternative_service\":[{" |
+ "\"advertised_versions\":[35,37],\"expiration\":\"13756212000000000\"," |
+ "\"port\":443,\"protocol_str\":\"quic\"},{\"advertised_versions\":[]," |
+ "\"expiration\":\"13758804000000000\",\"host\":\"www.google.com\"," |
+ "\"port\":1234,\"protocol_str\":\"h2\"}]}}," |
+ "{\"https://mail.google.com:80\":{\"alternative_service\":[{" |
+ "\"advertised_versions\":[37],\"expiration\":\"9223372036854775807\"," |
+ "\"host\":\"foo.google.com\",\"port\":444,\"protocol_str\":\"quic\"}]," |
+ "\"network_stats\":{\"srtt\":42}}}],\"supports_quic\":{" |
+ "\"address\":\"127.0.0.1\",\"used_quic\":true},\"version\":5}"; |
+ |
+ const base::Value* http_server_properties = |
+ &pref_delegate_->GetServerProperties(); |
+ std::string preferences_json; |
+ EXPECT_TRUE( |
+ base::JSONWriter::Write(*http_server_properties, &preferences_json)); |
+ EXPECT_EQ(expected_json, preferences_json); |
+} |
+ |
+TEST_P(HttpServerPropertiesManagerTest, ReadAdvertisedVersionsFromPref) { |
+ std::unique_ptr<base::Value> server_value = base::JSONReader::Read( |
+ "{\"alternative_service\":[" |
+ "{\"port\":443,\"protocol_str\":\"quic\"}," |
+ "{\"port\":123,\"protocol_str\":\"quic\"," |
+ "\"expiration\":\"9223372036854775807\"," |
+ "\"advertised_versions\":[37,35]}]}"); |
+ ASSERT_TRUE(server_value); |
+ base::DictionaryValue* server_dict; |
+ ASSERT_TRUE(server_value->GetAsDictionary(&server_dict)); |
+ |
+ const url::SchemeHostPort server("https", "example.com", 443); |
+ AlternativeServiceMap alternative_service_map(/*max_size=*/5); |
+ EXPECT_TRUE(http_server_props_manager_->AddToAlternativeServiceMap( |
+ server, *server_dict, &alternative_service_map)); |
+ |
+ AlternativeServiceMap::iterator it = alternative_service_map.Get(server); |
+ ASSERT_NE(alternative_service_map.end(), it); |
+ AlternativeServiceInfoVector alternative_service_info_vector = it->second; |
+ ASSERT_EQ(2u, alternative_service_info_vector.size()); |
+ |
+ // Verify the first alternative service with no advertised version listed. |
+ EXPECT_EQ(kProtoQUIC, |
+ alternative_service_info_vector[0].alternative_service.protocol); |
+ EXPECT_EQ("", alternative_service_info_vector[0].alternative_service.host); |
+ EXPECT_EQ(443, alternative_service_info_vector[0].alternative_service.port); |
+ // Expiration defaults to one day from now, testing with tolerance. |
+ const base::Time now = base::Time::Now(); |
+ const base::Time expiration = alternative_service_info_vector[0].expiration; |
+ EXPECT_LE(now + base::TimeDelta::FromHours(23), expiration); |
+ EXPECT_GE(now + base::TimeDelta::FromDays(1), expiration); |
+ EXPECT_TRUE(alternative_service_info_vector[0].advertised_versions().empty()); |
+ |
+ // Verify the second alterntaive service with two advertised versions. |
+ EXPECT_EQ(kProtoQUIC, |
+ alternative_service_info_vector[1].alternative_service.protocol); |
+ EXPECT_EQ("", alternative_service_info_vector[1].alternative_service.host); |
+ EXPECT_EQ(123, alternative_service_info_vector[1].alternative_service.port); |
+ EXPECT_EQ(base::Time::Max(), alternative_service_info_vector[1].expiration); |
+ // Verify advertised versions. |
+ const QuicVersionVector loaded_advertised_versions = |
+ alternative_service_info_vector[1].advertised_versions(); |
+ EXPECT_EQ(2u, loaded_advertised_versions.size()); |
+ EXPECT_EQ(QUIC_VERSION_35, loaded_advertised_versions[0]); |
+ EXPECT_EQ(QUIC_VERSION_37, loaded_advertised_versions[1]); |
+} |
+ |
+TEST_P(HttpServerPropertiesManagerTest, |
+ UpdatePrefWhenAdvertisedVersionsChange) { |
+ ExpectScheduleUpdatePrefsOnNetworkSequenceRepeatedly(4); |
+ |
+ const url::SchemeHostPort server_www("https", "www.google.com", 80); |
+ |
+ // #1: Set alternate protocol. |
+ AlternativeServiceInfoVector alternative_service_info_vector; |
+ // Quic alternative service set with a single QUIC version: QUIC_VERSION_37. |
+ AlternativeService quic_alternative_service1(kProtoQUIC, "", 443); |
+ base::Time expiration1; |
+ ASSERT_TRUE(base::Time::FromUTCString("2036-12-01 10:00:00", &expiration1)); |
+ alternative_service_info_vector.push_back(AlternativeServiceInfo( |
+ quic_alternative_service1, expiration1, advertised_versions_)); |
+ ASSERT_TRUE(http_server_props_manager_->SetAlternativeServices( |
+ server_www, alternative_service_info_vector)); |
+ |
+ // Set quic_server_info string. |
+ QuicServerId mail_quic_server_id("mail.google.com", 80); |
+ std::string quic_server_info1("quic_server_info1"); |
+ http_server_props_manager_->SetQuicServerInfo(mail_quic_server_id, |
+ quic_server_info1); |
+ |
+ // Set SupportsQuic. |
+ IPAddress actual_address(127, 0, 0, 1); |
+ http_server_props_manager_->SetSupportsQuic(true, actual_address); |
+ |
+ // Update Prefs. |
+ ExpectPrefsUpdate(1); |
+ EXPECT_TRUE(net_test_task_runner_->HasPendingTask()); |
+ EXPECT_FALSE(pref_test_task_runner_->HasPendingTask()); |
+ net_test_task_runner_->FastForwardUntilNoTasksRemain(); |
+ EXPECT_TRUE(pref_test_task_runner_->HasPendingTask()); |
+ pref_test_task_runner_->FastForwardUntilNoTasksRemain(); |
+ EXPECT_FALSE(net_test_task_runner_->HasPendingTask()); |
+ EXPECT_FALSE(pref_test_task_runner_->HasPendingTask()); |
+ |
+ // Verify preferences with correct advertised version field. |
+ const char expected_json[] = |
+ "{\"quic_servers\":{\"https://mail.google.com:80\":" |
+ "{\"server_info\":\"quic_server_info1\"}},\"servers\":[" |
+ "{\"https://www.google.com:80\":" |
+ "{\"alternative_service\":[{\"advertised_versions\":[37]," |
+ "\"expiration\":\"13756212000000000\",\"port\":443," |
+ "\"protocol_str\":\"quic\"}]}}],\"supports_quic\":" |
+ "{\"address\":\"127.0.0.1\",\"used_quic\":true},\"version\":5}"; |
+ |
+ const base::Value* http_server_properties = |
+ &pref_delegate_->GetServerProperties(); |
+ std::string preferences_json; |
+ EXPECT_TRUE( |
+ base::JSONWriter::Write(*http_server_properties, &preferences_json)); |
+ EXPECT_EQ(expected_json, preferences_json); |
+ |
+ // #2: Set AlternativeService with different advertised_versions for the same |
+ // AlternativeService. |
+ AlternativeServiceInfoVector alternative_service_info_vector_2; |
+ // Quic alternative service set with two advertised QUIC versions. |
+ QuicVersionVector advertised_versions = {QUIC_VERSION_37, QUIC_VERSION_35}; |
+ alternative_service_info_vector_2.push_back(AlternativeServiceInfo( |
+ quic_alternative_service1, expiration1, advertised_versions)); |
+ ASSERT_TRUE(http_server_props_manager_->SetAlternativeServices( |
+ server_www, alternative_service_info_vector_2)); |
+ |
+ // Update Prefs. |
+ ExpectPrefsUpdate(1); |
+ EXPECT_TRUE(net_test_task_runner_->HasPendingTask()); |
+ EXPECT_FALSE(pref_test_task_runner_->HasPendingTask()); |
+ net_test_task_runner_->FastForwardUntilNoTasksRemain(); |
+ EXPECT_TRUE(pref_test_task_runner_->HasPendingTask()); |
+ pref_test_task_runner_->FastForwardUntilNoTasksRemain(); |
+ EXPECT_FALSE(net_test_task_runner_->HasPendingTask()); |
+ EXPECT_FALSE(pref_test_task_runner_->HasPendingTask()); |
+ |
+ // Verify preferences updated with new advertised versions. |
+ const char expected_json_updated[] = |
+ "{\"quic_servers\":{\"https://mail.google.com:80\":" |
+ "{\"server_info\":\"quic_server_info1\"}},\"servers\":[" |
+ "{\"https://www.google.com:80\":" |
+ "{\"alternative_service\":[{\"advertised_versions\":[35,37]," |
+ "\"expiration\":\"13756212000000000\",\"port\":443," |
+ "\"protocol_str\":\"quic\"}]}}],\"supports_quic\":" |
+ "{\"address\":\"127.0.0.1\",\"used_quic\":true},\"version\":5}"; |
+ EXPECT_TRUE( |
+ base::JSONWriter::Write(*http_server_properties, &preferences_json)); |
+ EXPECT_EQ(expected_json_updated, preferences_json); |
+ |
+ // #3: Set AlternativeService with same advertised_versions. |
+ AlternativeServiceInfoVector alternative_service_info_vector_3; |
+ // A same set of QUIC versions but listed in a different order. |
+ QuicVersionVector advertised_versions_2 = {QUIC_VERSION_35, QUIC_VERSION_37}; |
+ alternative_service_info_vector_3.push_back(AlternativeServiceInfo( |
+ quic_alternative_service1, expiration1, advertised_versions_2)); |
+ ASSERT_FALSE(http_server_props_manager_->SetAlternativeServices( |
+ server_www, alternative_service_info_vector_3)); |
+ |
+ // No Prefs update. |
+ EXPECT_FALSE(net_test_task_runner_->HasPendingTask()); |
+ EXPECT_FALSE(pref_test_task_runner_->HasPendingTask()); |
+} |
+ |
} // namespace net |