| 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 "net/http/http_server_properties_impl.h" | 5 #include "net/http/http_server_properties_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "net/base/host_port_pair.h" | 15 #include "net/base/host_port_pair.h" |
| 16 #include "net/base/ip_address_number.h" | 16 #include "net/base/ip_address_number.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 class ListValue; | 20 class ListValue; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace net { | 23 namespace net { |
| 24 | 24 |
| 25 const int kMaxSupportsSpdyServerHosts = 500; | |
| 26 | |
| 27 class HttpServerPropertiesImplPeer { | 25 class HttpServerPropertiesImplPeer { |
| 28 public: | 26 public: |
| 29 static void AddBrokenAlternativeServiceWithExpirationTime( | 27 static void AddBrokenAlternativeServiceWithExpirationTime( |
| 30 HttpServerPropertiesImpl& impl, | 28 HttpServerPropertiesImpl& impl, |
| 31 AlternativeService alternative_service, | 29 AlternativeService alternative_service, |
| 32 base::TimeTicks when) { | 30 base::TimeTicks when) { |
| 33 impl.broken_alternative_services_.insert( | 31 impl.broken_alternative_services_.insert( |
| 34 std::make_pair(alternative_service, when)); | 32 std::make_pair(alternative_service, when)); |
| 35 ++impl.recently_broken_alternative_services_[alternative_service]; | 33 ++impl.recently_broken_alternative_services_[alternative_service]; |
| 36 } | 34 } |
| 37 | 35 |
| 38 static void ExpireBrokenAlternateProtocolMappings( | 36 static void ExpireBrokenAlternateProtocolMappings( |
| 39 HttpServerPropertiesImpl& impl) { | 37 HttpServerPropertiesImpl& impl) { |
| 40 impl.ExpireBrokenAlternateProtocolMappings(); | 38 impl.ExpireBrokenAlternateProtocolMappings(); |
| 41 } | 39 } |
| 42 }; | 40 }; |
| 43 | 41 |
| 44 void PrintTo(const AlternativeService& alternative_service, std::ostream* os) { | 42 void PrintTo(const AlternativeService& alternative_service, std::ostream* os) { |
| 45 *os << alternative_service.ToString(); | 43 *os << alternative_service.ToString(); |
| 46 } | 44 } |
| 47 | 45 |
| 48 namespace { | 46 namespace { |
| 49 | 47 |
| 48 const int kMaxSupportsSpdyServerHosts = 500; |
| 49 const SpdySettingsIds kSpdySettingsId = SETTINGS_UPLOAD_BANDWIDTH; |
| 50 const SpdySettingsFlags kSpdySettingsFlags = SETTINGS_FLAG_PERSISTED; |
| 51 |
| 52 struct SpdySettingsDataToVerify { |
| 53 HostPortPair spdy_server; |
| 54 uint32 value; |
| 55 }; |
| 56 |
| 50 class HttpServerPropertiesImplTest : public testing::Test { | 57 class HttpServerPropertiesImplTest : public testing::Test { |
| 51 protected: | 58 protected: |
| 52 bool HasAlternativeService(const HostPortPair& origin) { | 59 bool HasAlternativeService(const HostPortPair& origin) { |
| 53 const AlternativeServiceVector alternative_service_vector = | 60 const AlternativeServiceVector alternative_service_vector = |
| 54 impl_.GetAlternativeServices(origin); | 61 impl_.GetAlternativeServices(origin); |
| 55 return !alternative_service_vector.empty(); | 62 return !alternative_service_vector.empty(); |
| 56 } | 63 } |
| 57 | 64 |
| 58 bool SetAlternativeService(const HostPortPair& origin, | 65 bool SetAlternativeService(const HostPortPair& origin, |
| 59 const AlternativeService& alternative_service, | 66 const AlternativeService& alternative_service, |
| 60 double alternative_probability) { | 67 double alternative_probability) { |
| 61 const base::Time expiration = | 68 const base::Time expiration = |
| 62 base::Time::Now() + base::TimeDelta::FromDays(1); | 69 base::Time::Now() + base::TimeDelta::FromDays(1); |
| 63 return impl_.SetAlternativeService(origin, alternative_service, | 70 return impl_.SetAlternativeService(origin, alternative_service, |
| 64 alternative_probability, expiration); | 71 alternative_probability, expiration); |
| 65 } | 72 } |
| 66 | 73 |
| 74 void InitializeSpdySettingsUploadBandwidth(SpdySettingsMap* spdy_settings_map, |
| 75 const HostPortPair& spdy_server, |
| 76 uint32 value) { |
| 77 SettingsMap settings_map; |
| 78 settings_map[kSpdySettingsId] = |
| 79 SettingsFlagsAndValue(kSpdySettingsFlags, value); |
| 80 spdy_settings_map->Put(spdy_server, settings_map); |
| 81 } |
| 82 |
| 83 void VerifySpdySettingsUploadBandwidth( |
| 84 const SpdySettingsDataToVerify* data_to_verify) { |
| 85 const SpdySettingsMap& spdy_settings_map = impl_.spdy_settings_map(); |
| 86 int count = 0; |
| 87 for (SpdySettingsMap::const_iterator map_it = spdy_settings_map.begin(); |
| 88 map_it != spdy_settings_map.end(); ++map_it, ++count) { |
| 89 const SpdySettingsDataToVerify& data = data_to_verify[count]; |
| 90 EXPECT_TRUE(data.spdy_server.Equals(map_it->first)); |
| 91 const SettingsMap& settings_map_memory = map_it->second; |
| 92 |
| 93 EXPECT_EQ(1U, settings_map_memory.size()); |
| 94 SettingsMap::const_iterator it = |
| 95 settings_map_memory.find(kSpdySettingsId); |
| 96 EXPECT_TRUE(it != settings_map_memory.end()); |
| 97 SettingsFlagsAndValue flags_and_value_memory = it->second; |
| 98 EXPECT_EQ(kSpdySettingsFlags, flags_and_value_memory.first); |
| 99 EXPECT_EQ(data.value, flags_and_value_memory.second); |
| 100 } |
| 101 } |
| 102 |
| 67 HttpServerPropertiesImpl impl_; | 103 HttpServerPropertiesImpl impl_; |
| 68 }; | 104 }; |
| 69 | 105 |
| 70 typedef HttpServerPropertiesImplTest SpdyServerPropertiesTest; | 106 typedef HttpServerPropertiesImplTest SpdyServerPropertiesTest; |
| 71 | 107 |
| 72 TEST_F(SpdyServerPropertiesTest, Initialize) { | 108 TEST_F(SpdyServerPropertiesTest, Initialize) { |
| 73 HostPortPair spdy_server_google("www.google.com", 443); | 109 HostPortPair spdy_server_google("www.google.com", 443); |
| 74 std::string spdy_server_g = spdy_server_google.ToString(); | 110 std::string spdy_server_g = spdy_server_google.ToString(); |
| 75 | 111 |
| 76 HostPortPair spdy_server_docs("docs.google.com", 443); | 112 HostPortPair spdy_server_docs("docs.google.com", 443); |
| (...skipping 854 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 931 EXPECT_TRUE( | 967 EXPECT_TRUE( |
| 932 impl_.WasAlternativeServiceRecentlyBroken(bar_alternative_service)); | 968 impl_.WasAlternativeServiceRecentlyBroken(bar_alternative_service)); |
| 933 EXPECT_FALSE( | 969 EXPECT_FALSE( |
| 934 impl_.WasAlternativeServiceRecentlyBroken(baz_alternative_service)); | 970 impl_.WasAlternativeServiceRecentlyBroken(baz_alternative_service)); |
| 935 } | 971 } |
| 936 | 972 |
| 937 typedef HttpServerPropertiesImplTest SpdySettingsServerPropertiesTest; | 973 typedef HttpServerPropertiesImplTest SpdySettingsServerPropertiesTest; |
| 938 | 974 |
| 939 TEST_F(SpdySettingsServerPropertiesTest, Initialize) { | 975 TEST_F(SpdySettingsServerPropertiesTest, Initialize) { |
| 940 HostPortPair spdy_server_google("www.google.com", 443); | 976 HostPortPair spdy_server_google("www.google.com", 443); |
| 977 HostPortPair spdy_server_photos("photos.google.com", 443); |
| 978 HostPortPair spdy_server_docs("docs.google.com", 443); |
| 979 HostPortPair spdy_server_mail("mail.google.com", 443); |
| 941 | 980 |
| 942 // Check by initializing empty spdy settings. | 981 // Check by initializing empty spdy settings. |
| 943 SpdySettingsMap spdy_settings_map(SpdySettingsMap::NO_AUTO_EVICT); | 982 SpdySettingsMap spdy_settings_map(SpdySettingsMap::NO_AUTO_EVICT); |
| 944 impl_.InitializeSpdySettingsServers(&spdy_settings_map); | 983 impl_.InitializeSpdySettingsServers(&spdy_settings_map); |
| 945 EXPECT_TRUE(impl_.GetSpdySettings(spdy_server_google).empty()); | 984 EXPECT_TRUE(impl_.GetSpdySettings(spdy_server_google).empty()); |
| 946 | 985 |
| 947 // Check by initializing with www.google.com:443 spdy server settings. | 986 // Check by initializing spdy server settings for www.google.com:443 and |
| 948 SettingsMap settings_map; | 987 // photos.google.com:443. |
| 949 const SpdySettingsIds id = SETTINGS_UPLOAD_BANDWIDTH; | 988 const SpdySettingsDataToVerify data_to_verify1[] = { |
| 950 const SpdySettingsFlags flags = SETTINGS_FLAG_PERSISTED; | 989 {spdy_server_google, 10000}, {spdy_server_photos, 20000}, |
| 951 const uint32 value = 31337; | 990 }; |
| 952 SettingsFlagsAndValue flags_and_value(flags, value); | 991 // Insert them in reverse order to make spdy_server_google as MRU. |
| 953 settings_map[id] = flags_and_value; | 992 SpdySettingsMap spdy_settings_map1(SpdySettingsMap::NO_AUTO_EVICT); |
| 954 spdy_settings_map.Put(spdy_server_google, settings_map); | 993 InitializeSpdySettingsUploadBandwidth(&spdy_settings_map1, |
| 955 impl_.InitializeSpdySettingsServers(&spdy_settings_map); | 994 data_to_verify1[1].spdy_server, |
| 995 data_to_verify1[1].value); |
| 996 InitializeSpdySettingsUploadBandwidth(&spdy_settings_map1, |
| 997 data_to_verify1[0].spdy_server, |
| 998 data_to_verify1[0].value); |
| 999 impl_.InitializeSpdySettingsServers(&spdy_settings_map1); |
| 1000 VerifySpdySettingsUploadBandwidth(data_to_verify1); |
| 956 | 1001 |
| 957 const SettingsMap& settings_map2 = impl_.GetSpdySettings(spdy_server_google); | 1002 // Check by initializing mail.google.com:443 and docs.google.com:443 as spdy |
| 958 ASSERT_EQ(1U, settings_map2.size()); | 1003 // servers. |
| 959 SettingsMap::const_iterator it = settings_map2.find(id); | 1004 const SpdySettingsDataToVerify data_to_verify2[] = { |
| 960 EXPECT_TRUE(it != settings_map2.end()); | 1005 {spdy_server_google, 10000}, |
| 961 SettingsFlagsAndValue flags_and_value2 = it->second; | 1006 {spdy_server_photos, 20000}, |
| 962 EXPECT_EQ(flags, flags_and_value2.first); | 1007 {spdy_server_mail, 30000}, |
| 963 EXPECT_EQ(value, flags_and_value2.second); | 1008 {spdy_server_docs, 40000}, |
| 1009 }; |
| 1010 SpdySettingsMap spdy_settings_map2(SpdySettingsMap::NO_AUTO_EVICT); |
| 1011 InitializeSpdySettingsUploadBandwidth(&spdy_settings_map2, |
| 1012 data_to_verify2[3].spdy_server, |
| 1013 data_to_verify2[3].value); |
| 1014 InitializeSpdySettingsUploadBandwidth(&spdy_settings_map2, |
| 1015 data_to_verify2[2].spdy_server, |
| 1016 data_to_verify2[2].value); |
| 1017 impl_.InitializeSpdySettingsServers(&spdy_settings_map2); |
| 1018 VerifySpdySettingsUploadBandwidth(data_to_verify2); |
| 1019 |
| 1020 // Verify new data that is being initialized overwrites what is already in the |
| 1021 // memory and also verify the recency list order by updating 3rd and 1st |
| 1022 // element's data. |
| 1023 const SpdySettingsDataToVerify data_to_verify3[] = { |
| 1024 {spdy_server_google, 10000}, |
| 1025 {spdy_server_photos, 60000}, // Change the value of photos. |
| 1026 {spdy_server_mail, 30000}, |
| 1027 {spdy_server_docs, 50000}, // Change the value of docs. |
| 1028 }; |
| 1029 SpdySettingsMap spdy_settings_map3(SpdySettingsMap::NO_AUTO_EVICT); |
| 1030 InitializeSpdySettingsUploadBandwidth(&spdy_settings_map3, |
| 1031 data_to_verify3[3].spdy_server, |
| 1032 data_to_verify3[3].value); |
| 1033 InitializeSpdySettingsUploadBandwidth(&spdy_settings_map3, |
| 1034 data_to_verify3[1].spdy_server, |
| 1035 data_to_verify3[1].value); |
| 1036 impl_.InitializeSpdySettingsServers(&spdy_settings_map3); |
| 1037 VerifySpdySettingsUploadBandwidth(data_to_verify3); |
| 964 } | 1038 } |
| 965 | 1039 |
| 966 TEST_F(SpdySettingsServerPropertiesTest, SetSpdySetting) { | 1040 TEST_F(SpdySettingsServerPropertiesTest, SetSpdySetting) { |
| 967 HostPortPair spdy_server_empty(std::string(), 443); | 1041 HostPortPair spdy_server_empty(std::string(), 443); |
| 968 const SettingsMap& settings_map0 = impl_.GetSpdySettings(spdy_server_empty); | 1042 const SettingsMap& settings_map0 = impl_.GetSpdySettings(spdy_server_empty); |
| 969 EXPECT_EQ(0U, settings_map0.size()); // Returns kEmptySettingsMap. | 1043 EXPECT_EQ(0U, settings_map0.size()); // Returns kEmptySettingsMap. |
| 970 | 1044 |
| 971 // Add www.google.com:443 as persisting. | 1045 // Add www.google.com:443 as persisting. |
| 972 HostPortPair spdy_server_google("www.google.com", 443); | 1046 HostPortPair spdy_server_google("www.google.com", 443); |
| 973 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; | 1047 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; |
| (...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1234 EXPECT_EQ(quic_server_info1, *(impl_.GetQuicServerInfo(quic_server_id))); | 1308 EXPECT_EQ(quic_server_info1, *(impl_.GetQuicServerInfo(quic_server_id))); |
| 1235 | 1309 |
| 1236 impl_.Clear(); | 1310 impl_.Clear(); |
| 1237 EXPECT_EQ(0u, impl_.quic_server_info_map().size()); | 1311 EXPECT_EQ(0u, impl_.quic_server_info_map().size()); |
| 1238 EXPECT_EQ(nullptr, impl_.GetQuicServerInfo(quic_server_id)); | 1312 EXPECT_EQ(nullptr, impl_.GetQuicServerInfo(quic_server_id)); |
| 1239 } | 1313 } |
| 1240 | 1314 |
| 1241 } // namespace | 1315 } // namespace |
| 1242 | 1316 |
| 1243 } // namespace net | 1317 } // namespace net |
| OLD | NEW |