Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / http / http_server_properties_impl_unittest.cc
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_.SetAlternateProtocol(test_host_port_pair1, 443, NPN_SPDY_3, 1);
290   impl_.SetBrokenAlternateProtocol(test_host_port_pair1);
291   HostPortPair test_host_port_pair2("foo2", 80);
292   impl_.SetAlternateProtocol(test_host_port_pair2, 443, NPN_SPDY_3, 1);
293
294   AlternateProtocolMap alternate_protocol_map(
295       AlternateProtocolMap::NO_AUTO_EVICT);
296   AlternateProtocolInfo alternate(123, NPN_SPDY_3, 1);
297   alternate_protocol_map.Put(test_host_port_pair2, alternate);
298   HostPortPair test_host_port_pair3("foo3", 80);
299   alternate.port = 1234;
300   alternate_protocol_map.Put(test_host_port_pair3, alternate);
301   impl_.InitializeAlternateProtocolServers(&alternate_protocol_map);
302
303   // Verify test_host_port_pair3 is the MRU server.
304   const net::AlternateProtocolMap& map = impl_.alternate_protocol_map();
305   net::AlternateProtocolMap::const_iterator it = map.begin();
306   it = map.begin();
307   EXPECT_TRUE(it->first.Equals(test_host_port_pair3));
308   EXPECT_EQ(1234, it->second.port);
309   EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
310
311   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair1));
312   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair2));
313   alternate = impl_.GetAlternateProtocol(test_host_port_pair1);
314   EXPECT_TRUE(alternate.is_broken);
315   alternate = impl_.GetAlternateProtocol(test_host_port_pair2);
316   EXPECT_EQ(123, alternate.port);
317   EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
318 }
319
320 TEST_F(AlternateProtocolServerPropertiesTest, MRUOfHasAlternateProtocol) {
321   HostPortPair test_host_port_pair1("foo1", 80);
322   impl_.SetAlternateProtocol(test_host_port_pair1, 443, NPN_SPDY_3, 1);
323   HostPortPair test_host_port_pair2("foo2", 80);
324   impl_.SetAlternateProtocol(test_host_port_pair2, 1234, NPN_SPDY_3, 1);
325
326   const net::AlternateProtocolMap& map = impl_.alternate_protocol_map();
327   net::AlternateProtocolMap::const_iterator it = map.begin();
328   EXPECT_TRUE(it->first.Equals(test_host_port_pair2));
329   EXPECT_EQ(1234, it->second.port);
330   EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
331
332   // HasAlternateProtocol should reoder the AlternateProtocol map.
333   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair1));
334   it = map.begin();
335   EXPECT_TRUE(it->first.Equals(test_host_port_pair1));
336   EXPECT_EQ(443, it->second.port);
337   EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
338 }
339
340 TEST_F(AlternateProtocolServerPropertiesTest, MRUOfGetAlternateProtocol) {
341   HostPortPair test_host_port_pair1("foo1", 80);
342   impl_.SetAlternateProtocol(test_host_port_pair1, 443, NPN_SPDY_3, 1);
343   HostPortPair test_host_port_pair2("foo2", 80);
344   impl_.SetAlternateProtocol(test_host_port_pair2, 1234, NPN_SPDY_3, 1);
345
346   const net::AlternateProtocolMap& map = impl_.alternate_protocol_map();
347   net::AlternateProtocolMap::const_iterator it = map.begin();
348   EXPECT_TRUE(it->first.Equals(test_host_port_pair2));
349   EXPECT_EQ(1234, it->second.port);
350   EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
351
352   // GetAlternateProtocol should reoder the AlternateProtocol map.
353   AlternateProtocolInfo alternate =
354       impl_.GetAlternateProtocol(test_host_port_pair1);
355   EXPECT_EQ(443, alternate.port);
356   EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
357   it = map.begin();
358   EXPECT_TRUE(it->first.Equals(test_host_port_pair1));
359   EXPECT_EQ(443, it->second.port);
360   EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
361 }
362
363 TEST_F(AlternateProtocolServerPropertiesTest, SetBroken) {
364   HostPortPair test_host_port_pair("foo", 80);
365   impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, 1);
366   impl_.SetBrokenAlternateProtocol(test_host_port_pair);
367   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
368   AlternateProtocolInfo alternate =
369       impl_.GetAlternateProtocol(test_host_port_pair);
370   EXPECT_TRUE(alternate.is_broken);
371
372   impl_.SetAlternateProtocol(
373       test_host_port_pair,
374       1234,
375       NPN_SPDY_3,
376       1);
377   alternate = impl_.GetAlternateProtocol(test_host_port_pair);
378   EXPECT_TRUE(alternate.is_broken) << "Second attempt should be ignored.";
379 }
380
381 TEST_F(AlternateProtocolServerPropertiesTest, ClearBroken) {
382   HostPortPair test_host_port_pair("foo", 80);
383   impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, 1);
384   impl_.SetBrokenAlternateProtocol(test_host_port_pair);
385   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
386   AlternateProtocolInfo alternate =
387       impl_.GetAlternateProtocol(test_host_port_pair);
388   EXPECT_TRUE(alternate.is_broken);
389   impl_.ClearAlternateProtocol(test_host_port_pair);
390   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
391 }
392
393 TEST_F(AlternateProtocolServerPropertiesTest, Forced) {
394   // Test forced alternate protocols.
395
396   AlternateProtocolInfo default_protocol(1234, NPN_SPDY_3, 1);
397   HttpServerPropertiesImpl::ForceAlternateProtocol(default_protocol);
398
399   // Verify the forced protocol.
400   HostPortPair test_host_port_pair("foo", 80);
401   EXPECT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
402   AlternateProtocolInfo alternate =
403       impl_.GetAlternateProtocol(test_host_port_pair);
404   EXPECT_EQ(default_protocol.port, alternate.port);
405   EXPECT_EQ(default_protocol.protocol, alternate.protocol);
406
407   // Verify the real protocol overrides the forced protocol.
408   impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, 1);
409   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
410   alternate = impl_.GetAlternateProtocol(test_host_port_pair);
411   EXPECT_EQ(443, alternate.port);
412   EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
413
414   // Turn off the static, forced alternate protocol so that tests don't
415   // have this state.
416   HttpServerPropertiesImpl::DisableForcedAlternateProtocol();
417
418   // Verify the forced protocol is off.
419   HostPortPair test_host_port_pair2("bar", 80);
420   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair2));
421 }
422
423 TEST_F(AlternateProtocolServerPropertiesTest, Canonical) {
424   HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
425   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
426
427   HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
428   EXPECT_FALSE(impl_.HasAlternateProtocol(canonical_port_pair));
429
430   AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
431
432   impl_.SetAlternateProtocol(canonical_port_pair,
433                              canonical_protocol.port,
434                              canonical_protocol.protocol,
435                              1);
436   // Verify the forced protocol.
437   ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
438   AlternateProtocolInfo alternate =
439       impl_.GetAlternateProtocol(test_host_port_pair);
440   EXPECT_EQ(canonical_protocol.port, alternate.port);
441   EXPECT_EQ(canonical_protocol.protocol, alternate.protocol);
442
443   // Verify the canonical suffix.
444   EXPECT_EQ(".c.youtube.com", impl_.GetCanonicalSuffix(test_host_port_pair));
445   EXPECT_EQ(".c.youtube.com", impl_.GetCanonicalSuffix(canonical_port_pair));
446 }
447
448 TEST_F(AlternateProtocolServerPropertiesTest, ClearCanonical) {
449   HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
450   HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
451
452   AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
453
454   impl_.SetAlternateProtocol(canonical_port_pair,
455                              canonical_protocol.port,
456                              canonical_protocol.protocol,
457                              canonical_protocol.probability);
458
459   impl_.ClearAlternateProtocol(canonical_port_pair);
460   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
461 }
462
463 TEST_F(AlternateProtocolServerPropertiesTest, CanonicalBroken) {
464   HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
465   HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
466
467   AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
468
469   impl_.SetAlternateProtocol(canonical_port_pair,
470                              canonical_protocol.port,
471                              canonical_protocol.protocol,
472                              canonical_protocol.probability);
473
474   impl_.SetBrokenAlternateProtocol(canonical_port_pair);
475   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
476 }
477
478 TEST_F(AlternateProtocolServerPropertiesTest, ClearWithCanonical) {
479   HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
480   HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
481
482   AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
483
484   impl_.SetAlternateProtocol(canonical_port_pair,
485                              canonical_protocol.port,
486                              canonical_protocol.protocol,
487                              canonical_protocol.probability);
488
489   impl_.Clear();
490   EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
491 }
492
493 typedef HttpServerPropertiesImplTest SpdySettingsServerPropertiesTest;
494
495 TEST_F(SpdySettingsServerPropertiesTest, Initialize) {
496   HostPortPair spdy_server_google("www.google.com", 443);
497
498   // Check by initializing empty spdy settings.
499   SpdySettingsMap spdy_settings_map(SpdySettingsMap::NO_AUTO_EVICT);
500   impl_.InitializeSpdySettingsServers(&spdy_settings_map);
501   EXPECT_TRUE(impl_.GetSpdySettings(spdy_server_google).empty());
502
503   // Check by initializing with www.google.com:443 spdy server settings.
504   SettingsMap settings_map;
505   const SpdySettingsIds id = SETTINGS_UPLOAD_BANDWIDTH;
506   const SpdySettingsFlags flags = SETTINGS_FLAG_PERSISTED;
507   const uint32 value = 31337;
508   SettingsFlagsAndValue flags_and_value(flags, value);
509   settings_map[id] = flags_and_value;
510   spdy_settings_map.Put(spdy_server_google, settings_map);
511   impl_.InitializeSpdySettingsServers(&spdy_settings_map);
512
513   const SettingsMap& settings_map2 = impl_.GetSpdySettings(spdy_server_google);
514   ASSERT_EQ(1U, settings_map2.size());
515   SettingsMap::const_iterator it = settings_map2.find(id);
516   EXPECT_TRUE(it != settings_map2.end());
517   SettingsFlagsAndValue flags_and_value2 = it->second;
518   EXPECT_EQ(flags, flags_and_value2.first);
519   EXPECT_EQ(value, flags_and_value2.second);
520 }
521
522 TEST_F(SpdySettingsServerPropertiesTest, SetSpdySetting) {
523   HostPortPair spdy_server_empty(std::string(), 443);
524   const SettingsMap& settings_map0 = impl_.GetSpdySettings(spdy_server_empty);
525   EXPECT_EQ(0U, settings_map0.size());  // Returns kEmptySettingsMap.
526
527   // Add www.google.com:443 as persisting.
528   HostPortPair spdy_server_google("www.google.com", 443);
529   const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
530   const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
531   const uint32 value1 = 31337;
532   EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_google, id1, flags1, value1));
533   // Check the values.
534   const SettingsMap& settings_map1_ret =
535       impl_.GetSpdySettings(spdy_server_google);
536   ASSERT_EQ(1U, settings_map1_ret.size());
537   SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
538   EXPECT_TRUE(it1_ret != settings_map1_ret.end());
539   SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
540   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
541   EXPECT_EQ(value1, flags_and_value1_ret.second);
542
543   // Add mail.google.com:443 as not persisting.
544   HostPortPair spdy_server_mail("mail.google.com", 443);
545   const SpdySettingsIds id2 = SETTINGS_DOWNLOAD_BANDWIDTH;
546   const SpdySettingsFlags flags2 = SETTINGS_FLAG_NONE;
547   const uint32 value2 = 62667;
548   EXPECT_FALSE(impl_.SetSpdySetting(spdy_server_mail, id2, flags2, value2));
549   const SettingsMap& settings_map2_ret =
550       impl_.GetSpdySettings(spdy_server_mail);
551   EXPECT_EQ(0U, settings_map2_ret.size());  // Returns kEmptySettingsMap.
552
553   // Add docs.google.com:443 as persisting
554   HostPortPair spdy_server_docs("docs.google.com", 443);
555   const SpdySettingsIds id3 = SETTINGS_ROUND_TRIP_TIME;
556   const SpdySettingsFlags flags3 = SETTINGS_FLAG_PLEASE_PERSIST;
557   const uint32 value3 = 93997;
558   SettingsFlagsAndValue flags_and_value3(flags3, value3);
559   EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id3, flags3, value3));
560   // Check the values.
561   const SettingsMap& settings_map3_ret =
562       impl_.GetSpdySettings(spdy_server_docs);
563   ASSERT_EQ(1U, settings_map3_ret.size());
564   SettingsMap::const_iterator it3_ret = settings_map3_ret.find(id3);
565   EXPECT_TRUE(it3_ret != settings_map3_ret.end());
566   SettingsFlagsAndValue flags_and_value3_ret = it3_ret->second;
567   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value3_ret.first);
568   EXPECT_EQ(value3, flags_and_value3_ret.second);
569
570   // Check data for www.google.com:443 (id1).
571   const SettingsMap& settings_map4_ret =
572       impl_.GetSpdySettings(spdy_server_google);
573   ASSERT_EQ(1U, settings_map4_ret.size());
574   SettingsMap::const_iterator it4_ret = settings_map4_ret.find(id1);
575   EXPECT_TRUE(it4_ret != settings_map4_ret.end());
576   SettingsFlagsAndValue flags_and_value4_ret = it4_ret->second;
577   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value4_ret.first);
578   EXPECT_EQ(value1, flags_and_value1_ret.second);
579
580   // Clear www.google.com:443 as persisting.
581   impl_.ClearSpdySettings(spdy_server_google);
582   // Check the values.
583   const SettingsMap& settings_map5_ret =
584       impl_.GetSpdySettings(spdy_server_google);
585   ASSERT_EQ(0U, settings_map5_ret.size());
586
587   // Clear all settings.
588   ASSERT_GT(impl_.spdy_settings_map().size(), 0U);
589   impl_.ClearAllSpdySettings();
590   ASSERT_EQ(0U, impl_.spdy_settings_map().size());
591 }
592
593 TEST_F(SpdySettingsServerPropertiesTest, Clear) {
594   // Add www.google.com:443 as persisting.
595   HostPortPair spdy_server_google("www.google.com", 443);
596   const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
597   const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
598   const uint32 value1 = 31337;
599   EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_google, id1, flags1, value1));
600   // Check the values.
601   const SettingsMap& settings_map1_ret =
602       impl_.GetSpdySettings(spdy_server_google);
603   ASSERT_EQ(1U, settings_map1_ret.size());
604   SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
605   EXPECT_TRUE(it1_ret != settings_map1_ret.end());
606   SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
607   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
608   EXPECT_EQ(value1, flags_and_value1_ret.second);
609
610   // Add docs.google.com:443 as persisting
611   HostPortPair spdy_server_docs("docs.google.com", 443);
612   const SpdySettingsIds id3 = SETTINGS_ROUND_TRIP_TIME;
613   const SpdySettingsFlags flags3 = SETTINGS_FLAG_PLEASE_PERSIST;
614   const uint32 value3 = 93997;
615   EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id3, flags3, value3));
616   // Check the values.
617   const SettingsMap& settings_map3_ret =
618       impl_.GetSpdySettings(spdy_server_docs);
619   ASSERT_EQ(1U, settings_map3_ret.size());
620   SettingsMap::const_iterator it3_ret = settings_map3_ret.find(id3);
621   EXPECT_TRUE(it3_ret != settings_map3_ret.end());
622   SettingsFlagsAndValue flags_and_value3_ret = it3_ret->second;
623   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value3_ret.first);
624   EXPECT_EQ(value3, flags_and_value3_ret.second);
625
626   impl_.Clear();
627   EXPECT_EQ(0U, impl_.GetSpdySettings(spdy_server_google).size());
628   EXPECT_EQ(0U, impl_.GetSpdySettings(spdy_server_docs).size());
629 }
630
631 TEST_F(SpdySettingsServerPropertiesTest, MRUOfGetSpdySettings) {
632   // Add www.google.com:443 as persisting.
633   HostPortPair spdy_server_google("www.google.com", 443);
634   const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
635   const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
636   const uint32 value1 = 31337;
637   EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_google, id1, flags1, value1));
638
639   // Add docs.google.com:443 as persisting
640   HostPortPair spdy_server_docs("docs.google.com", 443);
641   const SpdySettingsIds id2 = SETTINGS_ROUND_TRIP_TIME;
642   const SpdySettingsFlags flags2 = SETTINGS_FLAG_PLEASE_PERSIST;
643   const uint32 value2 = 93997;
644   EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id2, flags2, value2));
645
646   // Verify the first element is docs.google.com:443.
647   const net::SpdySettingsMap& map = impl_.spdy_settings_map();
648   net::SpdySettingsMap::const_iterator it = map.begin();
649   EXPECT_TRUE(it->first.Equals(spdy_server_docs));
650   const SettingsMap& settings_map2_ret = it->second;
651   ASSERT_EQ(1U, settings_map2_ret.size());
652   SettingsMap::const_iterator it2_ret = settings_map2_ret.find(id2);
653   EXPECT_TRUE(it2_ret != settings_map2_ret.end());
654   SettingsFlagsAndValue flags_and_value2_ret = it2_ret->second;
655   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value2_ret.first);
656   EXPECT_EQ(value2, flags_and_value2_ret.second);
657
658   // GetSpdySettings should reoder the SpdySettingsMap.
659   const SettingsMap& settings_map1_ret =
660       impl_.GetSpdySettings(spdy_server_google);
661   ASSERT_EQ(1U, settings_map1_ret.size());
662   SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
663   EXPECT_TRUE(it1_ret != settings_map1_ret.end());
664   SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
665   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
666   EXPECT_EQ(value1, flags_and_value1_ret.second);
667
668   // Check the first entry is spdy_server_google by accessing it via iterator.
669   it = map.begin();
670   EXPECT_TRUE(it->first.Equals(spdy_server_google));
671   const SettingsMap& settings_map1_it_ret = it->second;
672   ASSERT_EQ(1U, settings_map1_it_ret.size());
673   it1_ret = settings_map1_it_ret.find(id1);
674   EXPECT_TRUE(it1_ret != settings_map1_it_ret.end());
675   flags_and_value1_ret = it1_ret->second;
676   EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
677   EXPECT_EQ(value1, flags_and_value1_ret.second);
678 }
679
680 typedef HttpServerPropertiesImplTest SupportsQuicServerPropertiesTest;
681
682 TEST_F(SupportsQuicServerPropertiesTest, Initialize) {
683   HostPortPair quic_server_google("www.google.com", 443);
684
685   // Check by initializing empty SupportsQuic.
686   SupportsQuicMap supports_quic_map;
687   impl_.InitializeSupportsQuic(&supports_quic_map);
688   SupportsQuic supports_quic = impl_.GetSupportsQuic(quic_server_google);
689   EXPECT_FALSE(supports_quic.used_quic);
690   EXPECT_EQ("", supports_quic.address);
691
692   // Check by initializing with www.google.com:443.
693   SupportsQuic supports_quic1(true, "foo");
694   supports_quic_map.insert(std::make_pair(quic_server_google, supports_quic1));
695   impl_.InitializeSupportsQuic(&supports_quic_map);
696
697   SupportsQuic supports_quic2 = impl_.GetSupportsQuic(quic_server_google);
698   EXPECT_TRUE(supports_quic2.used_quic);
699   EXPECT_EQ("foo", supports_quic2.address);
700 }
701
702 TEST_F(SupportsQuicServerPropertiesTest, SetSupportsQuic) {
703   HostPortPair test_host_port_pair("foo", 80);
704   SupportsQuic supports_quic = impl_.GetSupportsQuic(test_host_port_pair);
705   EXPECT_FALSE(supports_quic.used_quic);
706   EXPECT_EQ("", supports_quic.address);
707   impl_.SetSupportsQuic(test_host_port_pair, true, "foo");
708   SupportsQuic supports_quic1 = impl_.GetSupportsQuic(test_host_port_pair);
709   EXPECT_TRUE(supports_quic1.used_quic);
710   EXPECT_EQ("foo", supports_quic1.address);
711
712   impl_.Clear();
713   SupportsQuic supports_quic2 = impl_.GetSupportsQuic(test_host_port_pair);
714   EXPECT_FALSE(supports_quic2.used_quic);
715   EXPECT_EQ("", supports_quic2.address);
716 }
717 }  // namespace
718
719 }  // namespace net