Home | History | Annotate | Download | only in http
      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
      3 // found in the LICENSE file.
      4 
      5 #include "net/http/http_server_properties_impl.h"
      6 
      7 #include <string>
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/containers/hash_tables.h"
     12 #include "base/logging.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/values.h"
     15 #include "net/base/host_port_pair.h"
     16 #include "testing/gtest/include/gtest/gtest.h"
     17 
     18 namespace base {
     19 class ListValue;
     20 }
     21 
     22 namespace net {
     23 
     24 const int kMaxSupportsSpdyServerHosts = 500;
     25 
     26 namespace {
     27 
     28 class HttpServerPropertiesImplTest : public testing::Test {
     29  protected:
     30   HttpServerPropertiesImpl impl_;
     31 };
     32 
     33 typedef HttpServerPropertiesImplTest SpdyServerPropertiesTest;
     34 
     35 TEST_F(SpdyServerPropertiesTest, Initialize) {
     36   HostPortPair spdy_server_google("www.google.com", 443);
     37   std::string spdy_server_g =
     38       HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_google);
     39 
     40   HostPortPair spdy_server_docs("docs.google.com", 443);
     41   std::string spdy_server_d =
     42       HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_docs);
     43 
     44   // Check by initializing NULL spdy servers.
     45   impl_.InitializeSpdyServers(NULL, true);
     46   EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
     47 
     48   // Check by initializing empty spdy servers.
     49   std::vector<std::string> spdy_servers;
     50   impl_.InitializeSpdyServers(&spdy_servers, true);
     51   EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
     52 
     53   // Check by initializing with www.google.com:443 spdy server.
     54   std::vector<std::string> spdy_servers1;
     55   spdy_servers1.push_back(spdy_server_g);
     56   impl_.InitializeSpdyServers(&spdy_servers1, true);
     57   EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
     58 
     59   // Check by initializing with www.google.com:443 and docs.google.com:443 spdy
     60   // servers.
     61   std::vector<std::string> spdy_servers2;
     62   spdy_servers2.push_back(spdy_server_g);
     63   spdy_servers2.push_back(spdy_server_d);
     64   impl_.InitializeSpdyServers(&spdy_servers2, true);
     65 
     66   // Verify spdy_server_g and spdy_server_d are in the list in the same order.
     67   base::ListValue spdy_server_list;
     68   impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
     69   EXPECT_EQ(2U, spdy_server_list.GetSize());
     70   std::string string_value_g;
     71   ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
     72   ASSERT_EQ(spdy_server_g, string_value_g);
     73   std::string string_value_d;
     74   ASSERT_TRUE(spdy_server_list.GetString(1, &string_value_d));
     75   ASSERT_EQ(spdy_server_d, string_value_d);
     76   EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
     77   EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_docs));
     78 }
     79 
     80 TEST_F(SpdyServerPropertiesTest, SupportsSpdyTest) {
     81   HostPortPair spdy_server_empty(std::string(), 443);
     82   EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_empty));
     83 
     84   // Add www.google.com:443 as supporting SPDY.
     85   HostPortPair spdy_server_google("www.google.com", 443);
     86   impl_.SetSupportsSpdy(spdy_server_google, true);
     87   EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
     88 
     89   // Add mail.google.com:443 as not supporting SPDY.
     90   HostPortPair spdy_server_mail("mail.google.com", 443);
     91   EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_mail));
     92 
     93   // Add docs.google.com:443 as supporting SPDY.
     94   HostPortPair spdy_server_docs("docs.google.com", 443);
     95   impl_.SetSupportsSpdy(spdy_server_docs, true);
     96   EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_docs));
     97 
     98   // Verify all the entries are the same after additions.
     99   EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
    100   EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_mail));
    101   EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_docs));
    102 }
    103 
    104 TEST_F(SpdyServerPropertiesTest, SetSupportsSpdy) {
    105   HostPortPair spdy_server_empty(std::string(), 443);
    106   impl_.SetSupportsSpdy(spdy_server_empty, true);
    107   EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_empty));
    108 
    109   // Add www.google.com:443 as supporting SPDY.
    110   HostPortPair spdy_server_google("www.google.com", 443);
    111   EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
    112   impl_.SetSupportsSpdy(spdy_server_google, true);
    113   EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
    114 
    115   // Make www.google.com:443 as not supporting SPDY.
    116   impl_.SetSupportsSpdy(spdy_server_google, false);
    117   EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
    118 
    119   // Add mail.google.com:443 as supporting SPDY.
    120   HostPortPair spdy_server_mail("mail.google.com", 443);
    121   EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_mail));
    122   impl_.SetSupportsSpdy(spdy_server_mail, true);
    123   EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_mail));
    124   EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
    125 }
    126 
    127 TEST_F(SpdyServerPropertiesTest, Clear) {
    128   // Add www.google.com:443 and mail.google.com:443 as supporting SPDY.
    129   HostPortPair spdy_server_google("www.google.com", 443);
    130   impl_.SetSupportsSpdy(spdy_server_google, true);
    131   HostPortPair spdy_server_mail("mail.google.com", 443);
    132   impl_.SetSupportsSpdy(spdy_server_mail, true);
    133 
    134   EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
    135   EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_mail));
    136 
    137   impl_.Clear();
    138   EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
    139   EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_mail));
    140 }
    141 
    142 TEST_F(SpdyServerPropertiesTest, GetSpdyServerList) {
    143   base::ListValue spdy_server_list;
    144 
    145   // Check there are no spdy_servers.
    146   impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
    147   EXPECT_EQ(0U, spdy_server_list.GetSize());
    148 
    149   // Check empty server is not added.
    150   HostPortPair spdy_server_empty(std::string(), 443);
    151   impl_.SetSupportsSpdy(spdy_server_empty, true);
    152   impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
    153   EXPECT_EQ(0U, spdy_server_list.GetSize());
    154 
    155   std::string string_value_g;
    156   std::string string_value_m;
    157   HostPortPair spdy_server_google("www.google.com", 443);
    158   std::string spdy_server_g =
    159       HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_google);
    160   HostPortPair spdy_server_mail("mail.google.com", 443);
    161   std::string spdy_server_m =
    162       HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_mail);
    163 
    164   // Add www.google.com:443 as not supporting SPDY.
    165   impl_.SetSupportsSpdy(spdy_server_google, false);
    166   impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
    167   EXPECT_EQ(0U, spdy_server_list.GetSize());
    168 
    169   // Add www.google.com:443 as supporting SPDY.
    170   impl_.SetSupportsSpdy(spdy_server_google, true);
    171   impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
    172   ASSERT_EQ(1U, spdy_server_list.GetSize());
    173   ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
    174   ASSERT_EQ(spdy_server_g, string_value_g);
    175 
    176   // Add mail.google.com:443 as not supporting SPDY.
    177   impl_.SetSupportsSpdy(spdy_server_mail, false);
    178   impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
    179   ASSERT_EQ(1U, spdy_server_list.GetSize());
    180   ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
    181   ASSERT_EQ(spdy_server_g, string_value_g);
    182 
    183   // Add mail.google.com:443 as supporting SPDY.
    184   impl_.SetSupportsSpdy(spdy_server_mail, true);
    185   impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
    186   ASSERT_EQ(2U, spdy_server_list.GetSize());
    187 
    188   // Verify www.google.com:443 and mail.google.com:443 are in the list.
    189   ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_m));
    190   ASSERT_EQ(spdy_server_m, string_value_m);
    191   ASSERT_TRUE(spdy_server_list.GetString(1, &string_value_g));
    192   ASSERT_EQ(spdy_server_g, string_value_g);
    193 
    194   // Request for only one server and verify that we get only one server.
    195   impl_.GetSpdyServerList(&spdy_server_list, 1);
    196   ASSERT_EQ(1U, spdy_server_list.GetSize());
    197   ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_m));
    198   ASSERT_EQ(spdy_server_m, string_value_m);
    199 }
    200 
    201 TEST_F(SpdyServerPropertiesTest, MRUOfGetSpdyServerList) {
    202   base::ListValue spdy_server_list;
    203 
    204   std::string string_value_g;
    205   std::string string_value_m;
    206   HostPortPair spdy_server_google("www.google.com", 443);
    207   std::string spdy_server_g =
    208       HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_google);
    209   HostPortPair spdy_server_mail("mail.google.com", 443);
    210   std::string spdy_server_m =
    211       HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_mail);
    212 
    213   // Add www.google.com:443 as supporting SPDY.
    214   impl_.SetSupportsSpdy(spdy_server_google, true);
    215   impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
    216   ASSERT_EQ(1U, spdy_server_list.GetSize());
    217   ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
    218   ASSERT_EQ(spdy_server_g, string_value_g);
    219 
    220   // Add mail.google.com:443 as supporting SPDY. Verify mail.google.com:443 and
    221   // www.google.com:443 are in the list.
    222   impl_.SetSupportsSpdy(spdy_server_mail, true);
    223   impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
    224   ASSERT_EQ(2U, spdy_server_list.GetSize());
    225   ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_m));
    226   ASSERT_EQ(spdy_server_m, string_value_m);
    227   ASSERT_TRUE(spdy_server_list.GetString(1, &string_value_g));
    228   ASSERT_EQ(spdy_server_g, string_value_g);
    229 
    230   // Get www.google.com:443 should reorder SpdyServerHostPortMap. Verify that it
    231   // is www.google.com:443 is the MRU server.
    232   EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
    233   impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
    234   ASSERT_EQ(2U, spdy_server_list.GetSize());
    235   ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
    236   ASSERT_EQ(spdy_server_g, string_value_g);
    237   ASSERT_TRUE(spdy_server_list.GetString(1, &string_value_m));
    238   ASSERT_EQ(spdy_server_m, string_value_m);
    239 }
    240 
    241 typedef HttpServerPropertiesImplTest AlternateProtocolServerPropertiesTest;
    242 
    243 TEST_F(AlternateProtocolServerPropertiesTest, Basic) {
    244   HostPortPair test_host_port_pair("foo", 80);
    245   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
    246   impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, 1);
    247   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
    248   const AlternateProtocolInfo alternate =
    249       impl_.GetAlternateProtocol(test_host_port_pair);
    250   EXPECT_EQ(443, alternate.port);
    251   EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
    252 
    253   impl_.Clear();
    254   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
    255 }
    256 
    257 TEST_F(AlternateProtocolServerPropertiesTest, DefaultProbabilityExcluded) {
    258   HostPortPair test_host_port_pair("foo", 80);
    259   impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, .99);
    260 
    261   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
    262 }
    263 
    264 TEST_F(AlternateProtocolServerPropertiesTest, Probability) {
    265   impl_.SetAlternateProtocolProbabilityThreshold(.25);
    266 
    267   HostPortPair test_host_port_pair("foo", 80);
    268   impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, .5);
    269 
    270   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
    271   const AlternateProtocolInfo alternate =
    272       impl_.GetAlternateProtocol(test_host_port_pair);
    273   EXPECT_EQ(443, alternate.port);
    274   EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
    275   EXPECT_EQ(.5, alternate.probability);
    276 }
    277 
    278 TEST_F(AlternateProtocolServerPropertiesTest, ProbabilityExcluded) {
    279   impl_.SetAlternateProtocolProbabilityThreshold(.75);
    280 
    281   HostPortPair test_host_port_pair("foo", 80);
    282 
    283   impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, .5);
    284   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
    285 }
    286 
    287 TEST_F(AlternateProtocolServerPropertiesTest, Initialize) {
    288   HostPortPair test_host_port_pair1("foo1", 80);
    289   impl_.SetBrokenAlternateProtocol(test_host_port_pair1);
    290   HostPortPair test_host_port_pair2("foo2", 80);
    291   impl_.SetAlternateProtocol(test_host_port_pair2, 443, NPN_SPDY_3, 1);
    292 
    293   AlternateProtocolMap alternate_protocol_map(
    294       AlternateProtocolMap::NO_AUTO_EVICT);
    295   AlternateProtocolInfo port_alternate_protocol_pair(123, NPN_SPDY_3, 1);
    296   alternate_protocol_map.Put(test_host_port_pair2,
    297                              port_alternate_protocol_pair);
    298   HostPortPair test_host_port_pair3("foo3", 80);
    299   port_alternate_protocol_pair.port = 1234;
    300   alternate_protocol_map.Put(test_host_port_pair3,
    301                              port_alternate_protocol_pair);
    302   impl_.InitializeAlternateProtocolServers(&alternate_protocol_map);
    303 
    304   // Verify test_host_port_pair3 is the MRU server.
    305   const net::AlternateProtocolMap& map = impl_.alternate_protocol_map();
    306   net::AlternateProtocolMap::const_iterator it = map.begin();
    307   it = map.begin();
    308   EXPECT_TRUE(it->first.Equals(test_host_port_pair3));
    309   EXPECT_EQ(1234, it->second.port);
    310   EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
    311 
    312   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair1));
    313   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair2));
    314   port_alternate_protocol_pair =
    315       impl_.GetAlternateProtocol(test_host_port_pair1);
    316   EXPECT_EQ(ALTERNATE_PROTOCOL_BROKEN, port_alternate_protocol_pair.protocol);
    317   port_alternate_protocol_pair =
    318       impl_.GetAlternateProtocol(test_host_port_pair2);
    319   EXPECT_EQ(123, port_alternate_protocol_pair.port);
    320   EXPECT_EQ(NPN_SPDY_3, port_alternate_protocol_pair.protocol);
    321 }
    322 
    323 TEST_F(AlternateProtocolServerPropertiesTest, MRUOfHasAlternateProtocol) {
    324   HostPortPair test_host_port_pair1("foo1", 80);
    325   impl_.SetAlternateProtocol(test_host_port_pair1, 443, NPN_SPDY_3, 1);
    326   HostPortPair test_host_port_pair2("foo2", 80);
    327   impl_.SetAlternateProtocol(test_host_port_pair2, 1234, NPN_SPDY_3, 1);
    328 
    329   const net::AlternateProtocolMap& map = impl_.alternate_protocol_map();
    330   net::AlternateProtocolMap::const_iterator it = map.begin();
    331   EXPECT_TRUE(it->first.Equals(test_host_port_pair2));
    332   EXPECT_EQ(1234, it->second.port);
    333   EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
    334 
    335   // HasAlternateProtocol should reoder the AlternateProtocol map.
    336   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair1));
    337   it = map.begin();
    338   EXPECT_TRUE(it->first.Equals(test_host_port_pair1));
    339   EXPECT_EQ(443, it->second.port);
    340   EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
    341 }
    342 
    343 TEST_F(AlternateProtocolServerPropertiesTest, MRUOfGetAlternateProtocol) {
    344   HostPortPair test_host_port_pair1("foo1", 80);
    345   impl_.SetAlternateProtocol(test_host_port_pair1, 443, NPN_SPDY_3, 1);
    346   HostPortPair test_host_port_pair2("foo2", 80);
    347   impl_.SetAlternateProtocol(test_host_port_pair2, 1234, NPN_SPDY_3, 1);
    348 
    349   const net::AlternateProtocolMap& map = impl_.alternate_protocol_map();
    350   net::AlternateProtocolMap::const_iterator it = map.begin();
    351   EXPECT_TRUE(it->first.Equals(test_host_port_pair2));
    352   EXPECT_EQ(1234, it->second.port);
    353   EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
    354 
    355   // GetAlternateProtocol should reoder the AlternateProtocol map.
    356   AlternateProtocolInfo alternate =
    357       impl_.GetAlternateProtocol(test_host_port_pair1);
    358   EXPECT_EQ(443, alternate.port);
    359   EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
    360   it = map.begin();
    361   EXPECT_TRUE(it->first.Equals(test_host_port_pair1));
    362   EXPECT_EQ(443, it->second.port);
    363   EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
    364 }
    365 
    366 TEST_F(AlternateProtocolServerPropertiesTest, SetBroken) {
    367   HostPortPair test_host_port_pair("foo", 80);
    368   impl_.SetBrokenAlternateProtocol(test_host_port_pair);
    369   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
    370   AlternateProtocolInfo alternate =
    371       impl_.GetAlternateProtocol(test_host_port_pair);
    372   EXPECT_EQ(ALTERNATE_PROTOCOL_BROKEN, alternate.protocol);
    373 
    374   impl_.SetAlternateProtocol(
    375       test_host_port_pair,
    376       1234,
    377       NPN_SPDY_3,
    378       1);
    379   alternate = impl_.GetAlternateProtocol(test_host_port_pair);
    380   EXPECT_EQ(ALTERNATE_PROTOCOL_BROKEN, alternate.protocol)
    381       << "Second attempt should be ignored.";
    382 }
    383 
    384 TEST_F(AlternateProtocolServerPropertiesTest, ClearBroken) {
    385   HostPortPair test_host_port_pair("foo", 80);
    386   impl_.SetBrokenAlternateProtocol(test_host_port_pair);
    387   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
    388   AlternateProtocolInfo alternate =
    389       impl_.GetAlternateProtocol(test_host_port_pair);
    390   EXPECT_EQ(ALTERNATE_PROTOCOL_BROKEN, alternate.protocol);
    391   impl_.ClearAlternateProtocol(test_host_port_pair);
    392   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
    393 }
    394 
    395 TEST_F(AlternateProtocolServerPropertiesTest, Forced) {
    396   // Test forced alternate protocols.
    397 
    398   AlternateProtocolInfo default_protocol(1234, NPN_SPDY_3, 1);
    399   HttpServerPropertiesImpl::ForceAlternateProtocol(default_protocol);
    400 
    401   // Verify the forced protocol.
    402   HostPortPair test_host_port_pair("foo", 80);
    403   EXPECT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
    404   AlternateProtocolInfo alternate =
    405       impl_.GetAlternateProtocol(test_host_port_pair);
    406   EXPECT_EQ(default_protocol.port, alternate.port);
    407   EXPECT_EQ(default_protocol.protocol, alternate.protocol);
    408 
    409   // Verify the real protocol overrides the forced protocol.
    410   impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, 1);
    411   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
    412   alternate = impl_.GetAlternateProtocol(test_host_port_pair);
    413   EXPECT_EQ(443, alternate.port);
    414   EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
    415 
    416   // Turn off the static, forced alternate protocol so that tests don't
    417   // have this state.
    418   HttpServerPropertiesImpl::DisableForcedAlternateProtocol();
    419 
    420   // Verify the forced protocol is off.
    421   HostPortPair test_host_port_pair2("bar", 80);
    422   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair2));
    423 }
    424 
    425 TEST_F(AlternateProtocolServerPropertiesTest, Canonical) {
    426   HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
    427   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
    428 
    429   HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
    430   EXPECT_FALSE(impl_.HasAlternateProtocol(canonical_port_pair));
    431 
    432   AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
    433 
    434   impl_.SetAlternateProtocol(canonical_port_pair,
    435                              canonical_protocol.port,
    436                              canonical_protocol.protocol,
    437                              1);
    438   // Verify the forced protocol.
    439   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
    440   AlternateProtocolInfo alternate =
    441       impl_.GetAlternateProtocol(test_host_port_pair);
    442   EXPECT_EQ(canonical_protocol.port, alternate.port);
    443   EXPECT_EQ(canonical_protocol.protocol, alternate.protocol);
    444 
    445   // Verify the canonical suffix.
    446   EXPECT_EQ(".c.youtube.com", impl_.GetCanonicalSuffix(test_host_port_pair));
    447   EXPECT_EQ(".c.youtube.com", impl_.GetCanonicalSuffix(canonical_port_pair));
    448 }
    449 
    450 TEST_F(AlternateProtocolServerPropertiesTest, ClearCanonical) {
    451   HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
    452   HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
    453 
    454   AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
    455 
    456   impl_.SetAlternateProtocol(canonical_port_pair,
    457                              canonical_protocol.port,
    458                              canonical_protocol.protocol,
    459                              canonical_protocol.probability);
    460 
    461   impl_.ClearAlternateProtocol(canonical_port_pair);
    462   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
    463 }
    464 
    465 TEST_F(AlternateProtocolServerPropertiesTest, CanonicalBroken) {
    466   HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
    467   HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
    468 
    469   AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
    470 
    471   impl_.SetAlternateProtocol(canonical_port_pair,
    472                              canonical_protocol.port,
    473                              canonical_protocol.protocol,
    474                              canonical_protocol.probability);
    475 
    476   impl_.SetBrokenAlternateProtocol(canonical_port_pair);
    477   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
    478 }
    479 
    480 TEST_F(AlternateProtocolServerPropertiesTest, ClearWithCanonical) {
    481   HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
    482   HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
    483 
    484   AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
    485 
    486   impl_.SetAlternateProtocol(canonical_port_pair,
    487                              canonical_protocol.port,
    488                              canonical_protocol.protocol,
    489                              canonical_protocol.probability);
    490 
    491   impl_.Clear();
    492   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
    493 }
    494 
    495 typedef HttpServerPropertiesImplTest SpdySettingsServerPropertiesTest;
    496 
    497 TEST_F(SpdySettingsServerPropertiesTest, Initialize) {
    498   HostPortPair spdy_server_google("www.google.com", 443);
    499 
    500   // Check by initializing empty spdy settings.
    501   SpdySettingsMap spdy_settings_map(SpdySettingsMap::NO_AUTO_EVICT);
    502   impl_.InitializeSpdySettingsServers(&spdy_settings_map);
    503   EXPECT_TRUE(impl_.GetSpdySettings(spdy_server_google).empty());
    504 
    505   // Check by initializing with www.google.com:443 spdy server settings.
    506   SettingsMap settings_map;
    507   const SpdySettingsIds id = SETTINGS_UPLOAD_BANDWIDTH;
    508   const SpdySettingsFlags flags = SETTINGS_FLAG_PERSISTED;
    509   const uint32 value = 31337;
    510   SettingsFlagsAndValue flags_and_value(flags, value);
    511   settings_map[id] = flags_and_value;
    512   spdy_settings_map.Put(spdy_server_google, settings_map);
    513   impl_.InitializeSpdySettingsServers(&spdy_settings_map);
    514 
    515   const SettingsMap& settings_map2 = impl_.GetSpdySettings(spdy_server_google);
    516   ASSERT_EQ(1U, settings_map2.size());
    517   SettingsMap::const_iterator it = settings_map2.find(id);
    518   EXPECT_TRUE(it != settings_map2.end());
    519   SettingsFlagsAndValue flags_and_value2 = it->second;
    520   EXPECT_EQ(flags, flags_and_value2.first);
    521   EXPECT_EQ(value, flags_and_value2.second);
    522 }
    523 
    524 TEST_F(SpdySettingsServerPropertiesTest, SetSpdySetting) {
    525   HostPortPair spdy_server_empty(std::string(), 443);
    526   const SettingsMap& settings_map0 = impl_.GetSpdySettings(spdy_server_empty);
    527   EXPECT_EQ(0U, settings_map0.size());  // Returns kEmptySettingsMap.
    528 
    529   // Add www.google.com:443 as persisting.
    530   HostPortPair spdy_server_google("www.google.com", 443);
    531   const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
    532   const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
    533   const uint32 value1 = 31337;
    534   EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_google, id1, flags1, value1));
    535   // Check the values.
    536   const SettingsMap& settings_map1_ret =
    537       impl_.GetSpdySettings(spdy_server_google);
    538   ASSERT_EQ(1U, settings_map1_ret.size());
    539   SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
    540   EXPECT_TRUE(it1_ret != settings_map1_ret.end());
    541   SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
    542   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
    543   EXPECT_EQ(value1, flags_and_value1_ret.second);
    544 
    545   // Add mail.google.com:443 as not persisting.
    546   HostPortPair spdy_server_mail("mail.google.com", 443);
    547   const SpdySettingsIds id2 = SETTINGS_DOWNLOAD_BANDWIDTH;
    548   const SpdySettingsFlags flags2 = SETTINGS_FLAG_NONE;
    549   const uint32 value2 = 62667;
    550   EXPECT_FALSE(impl_.SetSpdySetting(spdy_server_mail, id2, flags2, value2));
    551   const SettingsMap& settings_map2_ret =
    552       impl_.GetSpdySettings(spdy_server_mail);
    553   EXPECT_EQ(0U, settings_map2_ret.size());  // Returns kEmptySettingsMap.
    554 
    555   // Add docs.google.com:443 as persisting
    556   HostPortPair spdy_server_docs("docs.google.com", 443);
    557   const SpdySettingsIds id3 = SETTINGS_ROUND_TRIP_TIME;
    558   const SpdySettingsFlags flags3 = SETTINGS_FLAG_PLEASE_PERSIST;
    559   const uint32 value3 = 93997;
    560   SettingsFlagsAndValue flags_and_value3(flags3, value3);
    561   EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id3, flags3, value3));
    562   // Check the values.
    563   const SettingsMap& settings_map3_ret =
    564       impl_.GetSpdySettings(spdy_server_docs);
    565   ASSERT_EQ(1U, settings_map3_ret.size());
    566   SettingsMap::const_iterator it3_ret = settings_map3_ret.find(id3);
    567   EXPECT_TRUE(it3_ret != settings_map3_ret.end());
    568   SettingsFlagsAndValue flags_and_value3_ret = it3_ret->second;
    569   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value3_ret.first);
    570   EXPECT_EQ(value3, flags_and_value3_ret.second);
    571 
    572   // Check data for www.google.com:443 (id1).
    573   const SettingsMap& settings_map4_ret =
    574       impl_.GetSpdySettings(spdy_server_google);
    575   ASSERT_EQ(1U, settings_map4_ret.size());
    576   SettingsMap::const_iterator it4_ret = settings_map4_ret.find(id1);
    577   EXPECT_TRUE(it4_ret != settings_map4_ret.end());
    578   SettingsFlagsAndValue flags_and_value4_ret = it4_ret->second;
    579   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value4_ret.first);
    580   EXPECT_EQ(value1, flags_and_value1_ret.second);
    581 
    582   // Clear www.google.com:443 as persisting.
    583   impl_.ClearSpdySettings(spdy_server_google);
    584   // Check the values.
    585   const SettingsMap& settings_map5_ret =
    586       impl_.GetSpdySettings(spdy_server_google);
    587   ASSERT_EQ(0U, settings_map5_ret.size());
    588 
    589   // Clear all settings.
    590   ASSERT_GT(impl_.spdy_settings_map().size(), 0U);
    591   impl_.ClearAllSpdySettings();
    592   ASSERT_EQ(0U, impl_.spdy_settings_map().size());
    593 }
    594 
    595 TEST_F(SpdySettingsServerPropertiesTest, Clear) {
    596   // Add www.google.com:443 as persisting.
    597   HostPortPair spdy_server_google("www.google.com", 443);
    598   const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
    599   const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
    600   const uint32 value1 = 31337;
    601   EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_google, id1, flags1, value1));
    602   // Check the values.
    603   const SettingsMap& settings_map1_ret =
    604       impl_.GetSpdySettings(spdy_server_google);
    605   ASSERT_EQ(1U, settings_map1_ret.size());
    606   SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
    607   EXPECT_TRUE(it1_ret != settings_map1_ret.end());
    608   SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
    609   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
    610   EXPECT_EQ(value1, flags_and_value1_ret.second);
    611 
    612   // Add docs.google.com:443 as persisting
    613   HostPortPair spdy_server_docs("docs.google.com", 443);
    614   const SpdySettingsIds id3 = SETTINGS_ROUND_TRIP_TIME;
    615   const SpdySettingsFlags flags3 = SETTINGS_FLAG_PLEASE_PERSIST;
    616   const uint32 value3 = 93997;
    617   EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id3, flags3, value3));
    618   // Check the values.
    619   const SettingsMap& settings_map3_ret =
    620       impl_.GetSpdySettings(spdy_server_docs);
    621   ASSERT_EQ(1U, settings_map3_ret.size());
    622   SettingsMap::const_iterator it3_ret = settings_map3_ret.find(id3);
    623   EXPECT_TRUE(it3_ret != settings_map3_ret.end());
    624   SettingsFlagsAndValue flags_and_value3_ret = it3_ret->second;
    625   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value3_ret.first);
    626   EXPECT_EQ(value3, flags_and_value3_ret.second);
    627 
    628   impl_.Clear();
    629   EXPECT_EQ(0U, impl_.GetSpdySettings(spdy_server_google).size());
    630   EXPECT_EQ(0U, impl_.GetSpdySettings(spdy_server_docs).size());
    631 }
    632 
    633 TEST_F(SpdySettingsServerPropertiesTest, MRUOfGetSpdySettings) {
    634   // Add www.google.com:443 as persisting.
    635   HostPortPair spdy_server_google("www.google.com", 443);
    636   const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
    637   const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
    638   const uint32 value1 = 31337;
    639   EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_google, id1, flags1, value1));
    640 
    641   // Add docs.google.com:443 as persisting
    642   HostPortPair spdy_server_docs("docs.google.com", 443);
    643   const SpdySettingsIds id2 = SETTINGS_ROUND_TRIP_TIME;
    644   const SpdySettingsFlags flags2 = SETTINGS_FLAG_PLEASE_PERSIST;
    645   const uint32 value2 = 93997;
    646   EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id2, flags2, value2));
    647 
    648   // Verify the first element is docs.google.com:443.
    649   const net::SpdySettingsMap& map = impl_.spdy_settings_map();
    650   net::SpdySettingsMap::const_iterator it = map.begin();
    651   EXPECT_TRUE(it->first.Equals(spdy_server_docs));
    652   const SettingsMap& settings_map2_ret = it->second;
    653   ASSERT_EQ(1U, settings_map2_ret.size());
    654   SettingsMap::const_iterator it2_ret = settings_map2_ret.find(id2);
    655   EXPECT_TRUE(it2_ret != settings_map2_ret.end());
    656   SettingsFlagsAndValue flags_and_value2_ret = it2_ret->second;
    657   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value2_ret.first);
    658   EXPECT_EQ(value2, flags_and_value2_ret.second);
    659 
    660   // GetSpdySettings should reoder the SpdySettingsMap.
    661   const SettingsMap& settings_map1_ret =
    662       impl_.GetSpdySettings(spdy_server_google);
    663   ASSERT_EQ(1U, settings_map1_ret.size());
    664   SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
    665   EXPECT_TRUE(it1_ret != settings_map1_ret.end());
    666   SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
    667   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
    668   EXPECT_EQ(value1, flags_and_value1_ret.second);
    669 
    670   // Check the first entry is spdy_server_google by accessing it via iterator.
    671   it = map.begin();
    672   EXPECT_TRUE(it->first.Equals(spdy_server_google));
    673   const SettingsMap& settings_map1_it_ret = it->second;
    674   ASSERT_EQ(1U, settings_map1_it_ret.size());
    675   it1_ret = settings_map1_it_ret.find(id1);
    676   EXPECT_TRUE(it1_ret != settings_map1_it_ret.end());
    677   flags_and_value1_ret = it1_ret->second;
    678   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
    679   EXPECT_EQ(value1, flags_and_value1_ret.second);
    680 }
    681 
    682 typedef HttpServerPropertiesImplTest SupportsQuicServerPropertiesTest;
    683 
    684 TEST_F(SupportsQuicServerPropertiesTest, Initialize) {
    685   HostPortPair quic_server_google("www.google.com", 443);
    686 
    687   // Check by initializing empty SupportsQuic.
    688   SupportsQuicMap supports_quic_map;
    689   impl_.InitializeSupportsQuic(&supports_quic_map);
    690   SupportsQuic supports_quic = impl_.GetSupportsQuic(quic_server_google);
    691   EXPECT_FALSE(supports_quic.used_quic);
    692   EXPECT_EQ("", supports_quic.address);
    693 
    694   // Check by initializing with www.google.com:443.
    695   SupportsQuic supports_quic1(true, "foo");
    696   supports_quic_map.insert(std::make_pair(quic_server_google, supports_quic1));
    697   impl_.InitializeSupportsQuic(&supports_quic_map);
    698 
    699   SupportsQuic supports_quic2 = impl_.GetSupportsQuic(quic_server_google);
    700   EXPECT_TRUE(supports_quic2.used_quic);
    701   EXPECT_EQ("foo", supports_quic2.address);
    702 }
    703 
    704 TEST_F(SupportsQuicServerPropertiesTest, SetSupportsQuic) {
    705   HostPortPair test_host_port_pair("foo", 80);
    706   SupportsQuic supports_quic = impl_.GetSupportsQuic(test_host_port_pair);
    707   EXPECT_FALSE(supports_quic.used_quic);
    708   EXPECT_EQ("", supports_quic.address);
    709   impl_.SetSupportsQuic(test_host_port_pair, true, "foo");
    710   SupportsQuic supports_quic1 = impl_.GetSupportsQuic(test_host_port_pair);
    711   EXPECT_TRUE(supports_quic1.used_quic);
    712   EXPECT_EQ("foo", supports_quic1.address);
    713 
    714   impl_.Clear();
    715   SupportsQuic supports_quic2 = impl_.GetSupportsQuic(test_host_port_pair);
    716   EXPECT_FALSE(supports_quic2.used_quic);
    717   EXPECT_EQ("", supports_quic2.address);
    718 }
    719 }  // namespace
    720 
    721 }  // namespace net
    722