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.
5 #include "net/http/http_server_properties_impl.h"
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 "net/base/ip_address_number.h"
17 #include "testing/gtest/include/gtest/gtest.h"
25 const int kMaxSupportsSpdyServerHosts
= 500;
27 class HttpServerPropertiesImplPeer
{
29 static void AddBrokenAlternativeServiceWithExpirationTime(
30 HttpServerPropertiesImpl
& impl
,
31 AlternativeService alternative_service
,
32 base::TimeTicks when
) {
33 impl
.broken_alternative_services_
.insert(
34 std::make_pair(alternative_service
, when
));
35 ++impl
.recently_broken_alternative_services_
[alternative_service
];
38 static void ExpireBrokenAlternateProtocolMappings(
39 HttpServerPropertiesImpl
& impl
) {
40 impl
.ExpireBrokenAlternateProtocolMappings();
44 void PrintTo(const AlternativeService
& alternative_service
, std::ostream
* os
) {
45 *os
<< alternative_service
.ToString();
50 class HttpServerPropertiesImplTest
: public testing::Test
{
52 bool HasAlternativeService(const HostPortPair
& origin
) {
53 const AlternativeServiceVector alternative_service_vector
=
54 impl_
.GetAlternativeServices(origin
);
55 return !alternative_service_vector
.empty();
58 bool SetAlternativeService(const HostPortPair
& origin
,
59 const AlternativeService
& alternative_service
,
60 double alternative_probability
) {
61 const base::Time expiration
=
62 base::Time::Now() + base::TimeDelta::FromDays(1);
63 return impl_
.SetAlternativeService(origin
, alternative_service
,
64 alternative_probability
, expiration
);
67 HttpServerPropertiesImpl impl_
;
70 typedef HttpServerPropertiesImplTest SpdyServerPropertiesTest
;
72 TEST_F(SpdyServerPropertiesTest
, Initialize
) {
73 HostPortPair
spdy_server_google("www.google.com", 443);
74 std::string spdy_server_g
= spdy_server_google
.ToString();
76 HostPortPair
spdy_server_docs("docs.google.com", 443);
77 std::string spdy_server_d
= spdy_server_docs
.ToString();
79 // Check by initializing NULL spdy servers.
80 impl_
.InitializeSpdyServers(NULL
, true);
81 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_google
));
83 // Check by initializing empty spdy servers.
84 std::vector
<std::string
> spdy_servers
;
85 impl_
.InitializeSpdyServers(&spdy_servers
, true);
86 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_google
));
88 // Check by initializing with www.google.com:443 spdy server.
89 std::vector
<std::string
> spdy_servers1
;
90 spdy_servers1
.push_back(spdy_server_g
);
91 impl_
.InitializeSpdyServers(&spdy_servers1
, true);
92 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
94 // Check by initializing with www.google.com:443 and docs.google.com:443 spdy
96 std::vector
<std::string
> spdy_servers2
;
97 spdy_servers2
.push_back(spdy_server_g
);
98 spdy_servers2
.push_back(spdy_server_d
);
99 impl_
.InitializeSpdyServers(&spdy_servers2
, true);
101 // Verify spdy_server_g and spdy_server_d are in the list in the same order.
102 base::ListValue spdy_server_list
;
103 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
104 EXPECT_EQ(2U, spdy_server_list
.GetSize());
105 std::string string_value_g
;
106 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
107 ASSERT_EQ(spdy_server_g
, string_value_g
);
108 std::string string_value_d
;
109 ASSERT_TRUE(spdy_server_list
.GetString(1, &string_value_d
));
110 ASSERT_EQ(spdy_server_d
, string_value_d
);
111 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
112 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_docs
));
115 TEST_F(SpdyServerPropertiesTest
, SupportsRequestPriorityTest
) {
116 HostPortPair
spdy_server_empty(std::string(), 443);
117 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_empty
));
119 // Add www.google.com:443 as supporting SPDY.
120 HostPortPair
spdy_server_google("www.google.com", 443);
121 impl_
.SetSupportsSpdy(spdy_server_google
, true);
122 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
124 // Add mail.google.com:443 as not supporting SPDY.
125 HostPortPair
spdy_server_mail("mail.google.com", 443);
126 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_mail
));
128 // Add docs.google.com:443 as supporting SPDY.
129 HostPortPair
spdy_server_docs("docs.google.com", 443);
130 impl_
.SetSupportsSpdy(spdy_server_docs
, true);
131 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_docs
));
133 // Add www.youtube.com:443 as supporting QUIC.
134 HostPortPair
quic_server_youtube("www.youtube.com", 443);
135 const AlternativeService
alternative_service1(QUIC
, "www.youtube.com", 443);
136 SetAlternativeService(quic_server_youtube
, alternative_service1
, 1.0);
137 EXPECT_TRUE(impl_
.SupportsRequestPriority(quic_server_youtube
));
139 // Add www.example.com:443 with two alternative services, one supporting QUIC.
140 HostPortPair
quic_server_example("www.example.com", 443);
141 const AlternativeService
alternative_service2(NPN_HTTP_2
, "", 443);
142 SetAlternativeService(quic_server_example
, alternative_service2
, 1.0);
143 SetAlternativeService(quic_server_example
, alternative_service1
, 1.0);
144 EXPECT_TRUE(impl_
.SupportsRequestPriority(quic_server_example
));
146 // Verify all the entries are the same after additions.
147 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
148 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_mail
));
149 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_docs
));
150 EXPECT_TRUE(impl_
.SupportsRequestPriority(quic_server_youtube
));
151 EXPECT_TRUE(impl_
.SupportsRequestPriority(quic_server_example
));
154 TEST_F(SpdyServerPropertiesTest
, Clear
) {
155 // Add www.google.com:443 and mail.google.com:443 as supporting SPDY.
156 HostPortPair
spdy_server_google("www.google.com", 443);
157 impl_
.SetSupportsSpdy(spdy_server_google
, true);
158 HostPortPair
spdy_server_mail("mail.google.com", 443);
159 impl_
.SetSupportsSpdy(spdy_server_mail
, true);
161 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
162 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_mail
));
165 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_google
));
166 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_mail
));
169 TEST_F(SpdyServerPropertiesTest
, GetSpdyServerList
) {
170 base::ListValue spdy_server_list
;
172 // Check there are no spdy_servers.
173 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
174 EXPECT_EQ(0U, spdy_server_list
.GetSize());
176 // Check empty server is not added.
177 HostPortPair
spdy_server_empty(std::string(), 443);
178 impl_
.SetSupportsSpdy(spdy_server_empty
, true);
179 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
180 EXPECT_EQ(0U, spdy_server_list
.GetSize());
182 std::string string_value_g
;
183 std::string string_value_m
;
184 HostPortPair
spdy_server_google("www.google.com", 443);
185 std::string spdy_server_g
= spdy_server_google
.ToString();
186 HostPortPair
spdy_server_mail("mail.google.com", 443);
187 std::string spdy_server_m
= spdy_server_mail
.ToString();
189 // Add www.google.com:443 as not supporting SPDY.
190 impl_
.SetSupportsSpdy(spdy_server_google
, false);
191 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
192 EXPECT_EQ(0U, spdy_server_list
.GetSize());
194 // Add www.google.com:443 as supporting SPDY.
195 impl_
.SetSupportsSpdy(spdy_server_google
, true);
196 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
197 ASSERT_EQ(1U, spdy_server_list
.GetSize());
198 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
199 ASSERT_EQ(spdy_server_g
, string_value_g
);
201 // Add mail.google.com:443 as not supporting SPDY.
202 impl_
.SetSupportsSpdy(spdy_server_mail
, false);
203 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
204 ASSERT_EQ(1U, spdy_server_list
.GetSize());
205 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
206 ASSERT_EQ(spdy_server_g
, string_value_g
);
208 // Add mail.google.com:443 as supporting SPDY.
209 impl_
.SetSupportsSpdy(spdy_server_mail
, true);
210 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
211 ASSERT_EQ(2U, spdy_server_list
.GetSize());
213 // Verify www.google.com:443 and mail.google.com:443 are in the list.
214 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_m
));
215 ASSERT_EQ(spdy_server_m
, string_value_m
);
216 ASSERT_TRUE(spdy_server_list
.GetString(1, &string_value_g
));
217 ASSERT_EQ(spdy_server_g
, string_value_g
);
219 // Request for only one server and verify that we get only one server.
220 impl_
.GetSpdyServerList(&spdy_server_list
, 1);
221 ASSERT_EQ(1U, spdy_server_list
.GetSize());
222 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_m
));
223 ASSERT_EQ(spdy_server_m
, string_value_m
);
226 TEST_F(SpdyServerPropertiesTest
, MRUOfGetSpdyServerList
) {
227 base::ListValue spdy_server_list
;
229 std::string string_value_g
;
230 std::string string_value_m
;
231 HostPortPair
spdy_server_google("www.google.com", 443);
232 std::string spdy_server_g
= spdy_server_google
.ToString();
233 HostPortPair
spdy_server_mail("mail.google.com", 443);
234 std::string spdy_server_m
= spdy_server_mail
.ToString();
236 // Add www.google.com:443 as supporting SPDY.
237 impl_
.SetSupportsSpdy(spdy_server_google
, true);
238 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
239 ASSERT_EQ(1U, spdy_server_list
.GetSize());
240 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
241 ASSERT_EQ(spdy_server_g
, string_value_g
);
243 // Add mail.google.com:443 as supporting SPDY. Verify mail.google.com:443 and
244 // www.google.com:443 are in the list.
245 impl_
.SetSupportsSpdy(spdy_server_mail
, true);
246 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
247 ASSERT_EQ(2U, spdy_server_list
.GetSize());
248 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_m
));
249 ASSERT_EQ(spdy_server_m
, string_value_m
);
250 ASSERT_TRUE(spdy_server_list
.GetString(1, &string_value_g
));
251 ASSERT_EQ(spdy_server_g
, string_value_g
);
253 // Get www.google.com:443 should reorder SpdyServerHostPortMap. Verify that it
254 // is www.google.com:443 is the MRU server.
255 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
256 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
257 ASSERT_EQ(2U, spdy_server_list
.GetSize());
258 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
259 ASSERT_EQ(spdy_server_g
, string_value_g
);
260 ASSERT_TRUE(spdy_server_list
.GetString(1, &string_value_m
));
261 ASSERT_EQ(spdy_server_m
, string_value_m
);
264 typedef HttpServerPropertiesImplTest AlternateProtocolServerPropertiesTest
;
266 TEST_F(AlternateProtocolServerPropertiesTest
, Basic
) {
267 HostPortPair
test_host_port_pair("foo", 80);
268 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
270 AlternativeService
alternative_service(NPN_HTTP_2
, "foo", 443);
271 SetAlternativeService(test_host_port_pair
, alternative_service
, 1.0);
272 const AlternativeServiceVector alternative_service_vector
=
273 impl_
.GetAlternativeServices(test_host_port_pair
);
274 ASSERT_EQ(1u, alternative_service_vector
.size());
275 EXPECT_EQ(alternative_service
, alternative_service_vector
[0]);
278 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
281 TEST_F(AlternateProtocolServerPropertiesTest
, ExcludeOrigin
) {
282 AlternativeServiceInfoVector alternative_service_info_vector
;
283 base::Time expiration
= base::Time::Now() + base::TimeDelta::FromDays(1);
284 // Same hostname, same port, TCP: should be ignored.
285 AlternativeService
alternative_service1(NPN_HTTP_2
, "foo", 443);
286 alternative_service_info_vector
.push_back(
287 AlternativeServiceInfo(alternative_service1
, 1.0, expiration
));
288 // Different hostname: GetAlternativeServices should return this one.
289 AlternativeService
alternative_service2(NPN_HTTP_2
, "bar", 443);
290 alternative_service_info_vector
.push_back(
291 AlternativeServiceInfo(alternative_service2
, 1.0, expiration
));
292 // Different port: GetAlternativeServices should return this one too.
293 AlternativeService
alternative_service3(NPN_HTTP_2
, "foo", 80);
294 alternative_service_info_vector
.push_back(
295 AlternativeServiceInfo(alternative_service3
, 1.0, expiration
));
296 // QUIC: GetAlternativeServices should return this one too.
297 AlternativeService
alternative_service4(QUIC
, "foo", 443);
298 alternative_service_info_vector
.push_back(
299 AlternativeServiceInfo(alternative_service4
, 1.0, expiration
));
301 HostPortPair
test_host_port_pair("foo", 443);
302 impl_
.SetAlternativeServices(test_host_port_pair
,
303 alternative_service_info_vector
);
305 const AlternativeServiceVector alternative_service_vector
=
306 impl_
.GetAlternativeServices(test_host_port_pair
);
307 ASSERT_EQ(3u, alternative_service_vector
.size());
308 EXPECT_EQ(alternative_service2
, alternative_service_vector
[0]);
309 EXPECT_EQ(alternative_service3
, alternative_service_vector
[1]);
310 EXPECT_EQ(alternative_service4
, alternative_service_vector
[2]);
313 TEST_F(AlternateProtocolServerPropertiesTest
, DefaultProbabilityExcluded
) {
314 HostPortPair
test_host_port_pair("foo", 80);
315 const AlternativeService
alternative_service(NPN_HTTP_2
, "foo", 443);
316 SetAlternativeService(test_host_port_pair
, alternative_service
, 0.99);
318 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
321 // GetAlternativeServices and HasAlternativeServices should only return the ones
322 // with probability greater than or equal to the threshold.
323 TEST_F(AlternateProtocolServerPropertiesTest
, Probability
) {
324 impl_
.SetAlternativeServiceProbabilityThreshold(0.5);
326 AlternativeServiceInfoVector alternative_service_info_vector
;
327 base::Time expiration
= base::Time::Now() + base::TimeDelta::FromDays(1);
328 const AlternativeService
alternative_service1(NPN_HTTP_2
, "foo", 443);
329 alternative_service_info_vector
.push_back(
330 AlternativeServiceInfo(alternative_service1
, 0.3, expiration
));
331 const AlternativeService
alternative_service2(QUIC
, "bar", 123);
332 alternative_service_info_vector
.push_back(
333 AlternativeServiceInfo(alternative_service2
, 0.7, expiration
));
334 const AlternativeService
alternative_service3(NPN_SPDY_3_1
, "baz", 443);
335 alternative_service_info_vector
.push_back(
336 AlternativeServiceInfo(alternative_service3
, 0.4, expiration
));
337 const AlternativeService
alternative_service4(NPN_HTTP_2
, "qux", 1234);
338 alternative_service_info_vector
.push_back(
339 AlternativeServiceInfo(alternative_service4
, 0.6, expiration
));
341 HostPortPair
test_host_port_pair("foo", 80);
342 impl_
.SetAlternativeServices(test_host_port_pair
,
343 alternative_service_info_vector
);
345 const AlternativeServiceVector alternative_service_vector
=
346 impl_
.GetAlternativeServices(test_host_port_pair
);
347 ASSERT_EQ(2u, alternative_service_vector
.size());
348 EXPECT_EQ(alternative_service2
, alternative_service_vector
[0]);
349 EXPECT_EQ(alternative_service4
, alternative_service_vector
[1]);
352 TEST_F(AlternateProtocolServerPropertiesTest
, ProbabilityExcluded
) {
353 impl_
.SetAlternativeServiceProbabilityThreshold(0.75);
355 HostPortPair
test_host_port_pair("foo", 80);
356 const AlternativeService
alternative_service(NPN_HTTP_2
, "foo", 443);
357 SetAlternativeService(test_host_port_pair
, alternative_service
, 0.5);
358 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
361 TEST_F(AlternateProtocolServerPropertiesTest
, Initialize
) {
362 // |test_host_port_pair1| has one alternative service, which is non-broken,
363 // and thus will be removed by InitializeAlternativeServiceServers().
364 HostPortPair
test_host_port_pair1("foo1", 80);
365 const AlternativeService
alternative_service1(NPN_HTTP_2
, "bar1", 443);
366 const base::Time now
= base::Time::Now();
367 base::Time expiration1
= now
+ base::TimeDelta::FromDays(1);
368 impl_
.SetAlternativeService(test_host_port_pair1
, alternative_service1
, 1.0,
371 // |test_host_port_pair2| has two alternative services. The broken one will
372 // remain, the non-broken one will be removed by
373 // InitializeAlternativeServiceServers().
374 AlternativeServiceInfoVector alternative_service_info_vector
;
375 const AlternativeService
alternative_service2(NPN_SPDY_3_1
, "bar2", 443);
376 base::Time expiration2
= now
+ base::TimeDelta::FromDays(2);
377 alternative_service_info_vector
.push_back(
378 AlternativeServiceInfo(alternative_service2
, 1.0, expiration2
));
379 const AlternativeService
alternative_service3(NPN_SPDY_3_1
, "bar3", 1234);
380 base::Time expiration3
= now
+ base::TimeDelta::FromDays(3);
381 alternative_service_info_vector
.push_back(
382 AlternativeServiceInfo(alternative_service3
, 0.8, expiration3
));
383 HostPortPair
test_host_port_pair2("foo2", 80);
384 impl_
.SetAlternativeServices(test_host_port_pair2
,
385 alternative_service_info_vector
);
386 impl_
.MarkAlternativeServiceBroken(alternative_service2
);
388 // Prepare |alternative_service_map| to be loaded by
389 // InitializeAlternativeServiceServers().
390 AlternativeServiceMap
alternative_service_map(
391 AlternativeServiceMap::NO_AUTO_EVICT
);
392 const AlternativeService
alternative_service4(NPN_HTTP_2
, "bar4", 123);
393 base::Time expiration4
= now
+ base::TimeDelta::FromDays(4);
394 const AlternativeServiceInfo
alternative_service_info1(alternative_service4
,
396 alternative_service_map
.Put(
397 test_host_port_pair2
,
398 AlternativeServiceInfoVector(/*size=*/1, alternative_service_info1
));
400 HostPortPair
test_host_port_pair3("foo3", 80);
401 const AlternativeService
alternative_service5(NPN_HTTP_2
, "bar5", 1234);
402 base::Time expiration5
= now
+ base::TimeDelta::FromDays(5);
403 const AlternativeServiceInfo
alternative_service_info2(alternative_service5
,
405 alternative_service_map
.Put(
406 test_host_port_pair3
,
407 AlternativeServiceInfoVector(/*size=*/1, alternative_service_info2
));
409 impl_
.InitializeAlternativeServiceServers(&alternative_service_map
);
411 // Verify alternative_service_map.
412 const AlternativeServiceMap
& map
= impl_
.alternative_service_map();
413 ASSERT_EQ(2u, map
.size());
414 AlternativeServiceMap::const_iterator map_it
= map
.begin();
415 EXPECT_TRUE(map_it
->first
.Equals(test_host_port_pair3
));
416 ASSERT_EQ(1u, map_it
->second
.size());
417 EXPECT_EQ(alternative_service5
, map_it
->second
[0].alternative_service
);
418 EXPECT_EQ(0.2, map_it
->second
[0].probability
);
419 EXPECT_EQ(expiration5
, map_it
->second
[0].expiration
);
421 EXPECT_TRUE(map_it
->first
.Equals(test_host_port_pair2
));
422 ASSERT_EQ(2u, map_it
->second
.size());
423 EXPECT_EQ(alternative_service2
, map_it
->second
[0].alternative_service
);
424 EXPECT_EQ(1.0, map_it
->second
[0].probability
);
425 EXPECT_EQ(expiration2
, map_it
->second
[0].expiration
);
426 EXPECT_EQ(alternative_service4
, map_it
->second
[1].alternative_service
);
427 EXPECT_EQ(0.7, map_it
->second
[1].probability
);
428 EXPECT_EQ(expiration4
, map_it
->second
[1].expiration
);
431 // Regression test for https://crbug.com/504032:
432 // InitializeAlternativeServiceServers() should not crash if there is an empty
433 // hostname is the mapping.
434 TEST_F(AlternateProtocolServerPropertiesTest
, InitializeWithEmptyHostname
) {
435 const HostPortPair
host_port_pair("foo", 443);
436 const AlternativeService
alternative_service_with_empty_hostname(NPN_HTTP_2
,
438 const AlternativeService
alternative_service_with_foo_hostname(NPN_HTTP_2
,
440 SetAlternativeService(host_port_pair
, alternative_service_with_empty_hostname
,
442 impl_
.MarkAlternativeServiceBroken(alternative_service_with_foo_hostname
);
444 AlternativeServiceMap
alternative_service_map(
445 AlternativeServiceMap::NO_AUTO_EVICT
);
446 impl_
.InitializeAlternativeServiceServers(&alternative_service_map
);
449 impl_
.IsAlternativeServiceBroken(alternative_service_with_foo_hostname
));
450 const AlternativeServiceVector alternative_service_vector
=
451 impl_
.GetAlternativeServices(host_port_pair
);
452 ASSERT_EQ(1u, alternative_service_vector
.size());
453 EXPECT_EQ(alternative_service_with_foo_hostname
,
454 alternative_service_vector
[0]);
457 TEST_F(AlternateProtocolServerPropertiesTest
, MRUOfGetAlternativeServices
) {
458 HostPortPair
test_host_port_pair1("foo1", 80);
459 const AlternativeService
alternative_service1(NPN_SPDY_3_1
, "foo1", 443);
460 SetAlternativeService(test_host_port_pair1
, alternative_service1
, 1.0);
461 HostPortPair
test_host_port_pair2("foo2", 80);
462 const AlternativeService
alternative_service2(NPN_HTTP_2
, "foo2", 1234);
463 SetAlternativeService(test_host_port_pair2
, alternative_service2
, 1.0);
465 const AlternativeServiceMap
& map
= impl_
.alternative_service_map();
466 AlternativeServiceMap::const_iterator it
= map
.begin();
467 EXPECT_TRUE(it
->first
.Equals(test_host_port_pair2
));
468 ASSERT_EQ(1u, it
->second
.size());
469 EXPECT_EQ(alternative_service2
, it
->second
[0].alternative_service
);
471 const AlternativeServiceVector alternative_service_vector
=
472 impl_
.GetAlternativeServices(test_host_port_pair1
);
473 ASSERT_EQ(1u, alternative_service_vector
.size());
474 EXPECT_EQ(alternative_service1
, alternative_service_vector
[0]);
476 // GetAlternativeServices should reorder the AlternateProtocol map.
478 EXPECT_TRUE(it
->first
.Equals(test_host_port_pair1
));
479 ASSERT_EQ(1u, it
->second
.size());
480 EXPECT_EQ(alternative_service1
, it
->second
[0].alternative_service
);
483 TEST_F(AlternateProtocolServerPropertiesTest
, SetBroken
) {
484 HostPortPair
test_host_port_pair("foo", 80);
485 const AlternativeService
alternative_service1(NPN_HTTP_2
, "foo", 443);
486 SetAlternativeService(test_host_port_pair
, alternative_service1
, 1.0);
487 AlternativeServiceVector alternative_service_vector
=
488 impl_
.GetAlternativeServices(test_host_port_pair
);
489 ASSERT_EQ(1u, alternative_service_vector
.size());
490 EXPECT_EQ(alternative_service1
, alternative_service_vector
[0]);
491 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service1
));
493 // GetAlternativeServices should return the broken alternative service.
494 impl_
.MarkAlternativeServiceBroken(alternative_service1
);
495 alternative_service_vector
=
496 impl_
.GetAlternativeServices(test_host_port_pair
);
497 ASSERT_EQ(1u, alternative_service_vector
.size());
498 EXPECT_EQ(alternative_service1
, alternative_service_vector
[0]);
499 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service1
));
501 // SetAlternativeServices should add a broken alternative service to the map.
502 AlternativeServiceInfoVector alternative_service_info_vector
;
503 base::Time expiration
= base::Time::Now() + base::TimeDelta::FromDays(1);
504 alternative_service_info_vector
.push_back(
505 AlternativeServiceInfo(alternative_service1
, 1.0, expiration
));
506 const AlternativeService
alternative_service2(NPN_HTTP_2
, "foo", 1234);
507 alternative_service_info_vector
.push_back(
508 AlternativeServiceInfo(alternative_service2
, 1.0, expiration
));
509 impl_
.SetAlternativeServices(test_host_port_pair
,
510 alternative_service_info_vector
);
511 alternative_service_vector
=
512 impl_
.GetAlternativeServices(test_host_port_pair
);
513 ASSERT_EQ(2u, alternative_service_vector
.size());
514 EXPECT_EQ(alternative_service1
, alternative_service_vector
[0]);
515 EXPECT_EQ(alternative_service2
, alternative_service_vector
[1]);
516 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service_vector
[0]));
517 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service_vector
[1]));
519 // SetAlternativeService should add a broken alternative service to the map.
520 SetAlternativeService(test_host_port_pair
, alternative_service1
, 1.0);
521 alternative_service_vector
=
522 impl_
.GetAlternativeServices(test_host_port_pair
);
523 ASSERT_EQ(1u, alternative_service_vector
.size());
524 EXPECT_EQ(alternative_service1
, alternative_service_vector
[0]);
525 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service_vector
[0]));
528 TEST_F(AlternateProtocolServerPropertiesTest
, MaxAge
) {
529 AlternativeServiceInfoVector alternative_service_info_vector
;
530 base::Time now
= base::Time::Now();
531 base::TimeDelta one_day
= base::TimeDelta::FromDays(1);
533 // First alternative service expired one day ago, should not be returned by
534 // GetAlternativeServices().
535 const AlternativeService
alternative_service1(NPN_SPDY_3_1
, "foo", 443);
536 alternative_service_info_vector
.push_back(
537 AlternativeServiceInfo(alternative_service1
, 1.0, now
- one_day
));
539 // Second alterrnative service will expire one day from now, should be
540 // returned by GetAlternativeSerices().
541 const AlternativeService
alternative_service2(NPN_HTTP_2
, "bar", 1234);
542 alternative_service_info_vector
.push_back(
543 AlternativeServiceInfo(alternative_service2
, 1.0, now
+ one_day
));
545 HostPortPair
test_host_port_pair("foo", 80);
546 impl_
.SetAlternativeServices(test_host_port_pair
,
547 alternative_service_info_vector
);
549 AlternativeServiceVector alternative_service_vector
=
550 impl_
.GetAlternativeServices(test_host_port_pair
);
551 ASSERT_EQ(1u, alternative_service_vector
.size());
552 EXPECT_EQ(alternative_service2
, alternative_service_vector
[0]);
555 TEST_F(AlternateProtocolServerPropertiesTest
, MaxAgeCanonical
) {
556 AlternativeServiceInfoVector alternative_service_info_vector
;
557 base::Time now
= base::Time::Now();
558 base::TimeDelta one_day
= base::TimeDelta::FromDays(1);
560 // First alternative service expired one day ago, should not be returned by
561 // GetAlternativeServices().
562 const AlternativeService
alternative_service1(NPN_SPDY_3_1
, "foo", 443);
563 alternative_service_info_vector
.push_back(
564 AlternativeServiceInfo(alternative_service1
, 1.0, now
- one_day
));
566 // Second alterrnative service will expire one day from now, should be
567 // returned by GetAlternativeSerices().
568 const AlternativeService
alternative_service2(NPN_HTTP_2
, "bar", 1234);
569 alternative_service_info_vector
.push_back(
570 AlternativeServiceInfo(alternative_service2
, 1.0, now
+ one_day
));
572 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
573 impl_
.SetAlternativeServices(canonical_host_port_pair
,
574 alternative_service_info_vector
);
576 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
577 AlternativeServiceVector alternative_service_vector
=
578 impl_
.GetAlternativeServices(test_host_port_pair
);
579 ASSERT_EQ(1u, alternative_service_vector
.size());
580 EXPECT_EQ(alternative_service2
, alternative_service_vector
[0]);
583 TEST_F(AlternateProtocolServerPropertiesTest
, ClearAlternativeServices
) {
584 AlternativeServiceInfoVector alternative_service_info_vector
;
585 const AlternativeService
alternative_service1(NPN_SPDY_3_1
, "foo", 443);
586 base::Time expiration
= base::Time::Now() + base::TimeDelta::FromDays(1);
587 alternative_service_info_vector
.push_back(
588 AlternativeServiceInfo(alternative_service1
, 1.0, expiration
));
589 const AlternativeService
alternative_service2(NPN_HTTP_2
, "bar", 1234);
590 alternative_service_info_vector
.push_back(
591 AlternativeServiceInfo(alternative_service2
, 1.0, expiration
));
592 HostPortPair
test_host_port_pair("foo", 80);
593 impl_
.SetAlternativeServices(test_host_port_pair
,
594 alternative_service_info_vector
);
596 const net::AlternativeServiceMap
& map
= impl_
.alternative_service_map();
597 net::AlternativeServiceMap::const_iterator it
= map
.begin();
598 EXPECT_TRUE(it
->first
.Equals(test_host_port_pair
));
599 ASSERT_EQ(2u, it
->second
.size());
600 EXPECT_EQ(alternative_service1
, it
->second
[0].alternative_service
);
601 EXPECT_EQ(alternative_service2
, it
->second
[1].alternative_service
);
603 impl_
.ClearAlternativeServices(test_host_port_pair
);
604 EXPECT_TRUE(map
.empty());
607 // A broken alternative service in the mapping carries meaningful information,
608 // therefore it should not be ignored by SetAlternativeService(). In
609 // particular, an alternative service mapped to an origin shadows alternative
610 // services of canonical hosts.
611 TEST_F(AlternateProtocolServerPropertiesTest
, BrokenShadowsCanonical
) {
612 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
613 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
614 AlternativeService
canonical_alternative_service(QUIC
, "bar.c.youtube.com",
616 SetAlternativeService(canonical_host_port_pair
, canonical_alternative_service
,
618 AlternativeServiceVector alternative_service_vector
=
619 impl_
.GetAlternativeServices(test_host_port_pair
);
620 ASSERT_EQ(1u, alternative_service_vector
.size());
621 EXPECT_EQ(canonical_alternative_service
, alternative_service_vector
[0]);
623 const AlternativeService
broken_alternative_service(NPN_HTTP_2
, "foo", 443);
624 impl_
.MarkAlternativeServiceBroken(broken_alternative_service
);
625 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(broken_alternative_service
));
627 SetAlternativeService(test_host_port_pair
, broken_alternative_service
, 1.0);
628 alternative_service_vector
=
629 impl_
.GetAlternativeServices(test_host_port_pair
);
630 ASSERT_EQ(1u, alternative_service_vector
.size());
631 EXPECT_EQ(broken_alternative_service
, alternative_service_vector
[0]);
632 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(broken_alternative_service
));
635 TEST_F(AlternateProtocolServerPropertiesTest
, ClearBroken
) {
636 HostPortPair
test_host_port_pair("foo", 80);
637 const AlternativeService
alternative_service(NPN_HTTP_2
, "foo", 443);
638 SetAlternativeService(test_host_port_pair
, alternative_service
, 1.0);
639 impl_
.MarkAlternativeServiceBroken(alternative_service
);
640 ASSERT_TRUE(HasAlternativeService(test_host_port_pair
));
641 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service
));
642 // ClearAlternativeServices should leave a broken alternative service marked
644 impl_
.ClearAlternativeServices(test_host_port_pair
);
645 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service
));
648 TEST_F(AlternateProtocolServerPropertiesTest
, MarkRecentlyBroken
) {
649 HostPortPair
host_port_pair("foo", 80);
650 const AlternativeService
alternative_service(NPN_HTTP_2
, "foo", 443);
651 SetAlternativeService(host_port_pair
, alternative_service
, 1.0);
653 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
654 EXPECT_FALSE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
656 impl_
.MarkAlternativeServiceRecentlyBroken(alternative_service
);
657 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
658 EXPECT_TRUE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
660 impl_
.ConfirmAlternativeService(alternative_service
);
661 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
662 EXPECT_FALSE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
665 TEST_F(AlternateProtocolServerPropertiesTest
, Canonical
) {
666 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
667 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
669 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
670 EXPECT_FALSE(HasAlternativeService(canonical_host_port_pair
));
672 AlternativeServiceInfoVector alternative_service_info_vector
;
673 const AlternativeService
canonical_alternative_service1(
674 QUIC
, "bar.c.youtube.com", 1234);
675 base::Time expiration
= base::Time::Now() + base::TimeDelta::FromDays(1);
676 alternative_service_info_vector
.push_back(
677 AlternativeServiceInfo(canonical_alternative_service1
, 1.0, expiration
));
678 const AlternativeService
canonical_alternative_service2(NPN_HTTP_2
, "", 443);
679 alternative_service_info_vector
.push_back(
680 AlternativeServiceInfo(canonical_alternative_service2
, 1.0, expiration
));
681 impl_
.SetAlternativeServices(canonical_host_port_pair
,
682 alternative_service_info_vector
);
684 // Since |test_host_port_pair| does not have an alternative service itself,
685 // GetAlternativeServices should return those of |canonical_host_port_pair|.
686 AlternativeServiceVector alternative_service_vector
=
687 impl_
.GetAlternativeServices(test_host_port_pair
);
688 ASSERT_EQ(2u, alternative_service_vector
.size());
689 EXPECT_EQ(canonical_alternative_service1
, alternative_service_vector
[0]);
691 // Since |canonical_alternative_service2| has an empty host,
692 // GetAlternativeServices should substitute the hostname of its |origin|
694 EXPECT_EQ(test_host_port_pair
.host(), alternative_service_vector
[1].host
);
695 EXPECT_EQ(canonical_alternative_service2
.protocol
,
696 alternative_service_vector
[1].protocol
);
697 EXPECT_EQ(canonical_alternative_service2
.port
,
698 alternative_service_vector
[1].port
);
700 // Verify the canonical suffix.
701 EXPECT_EQ(".c.youtube.com",
702 impl_
.GetCanonicalSuffix(test_host_port_pair
.host()));
703 EXPECT_EQ(".c.youtube.com",
704 impl_
.GetCanonicalSuffix(canonical_host_port_pair
.host()));
707 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalBelowThreshold
) {
708 impl_
.SetAlternativeServiceProbabilityThreshold(0.02);
710 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
711 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
712 AlternativeService
canonical_alternative_service(QUIC
, "bar.c.youtube.com",
715 SetAlternativeService(canonical_host_port_pair
, canonical_alternative_service
,
717 EXPECT_FALSE(HasAlternativeService(canonical_host_port_pair
));
718 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
721 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalAboveThreshold
) {
722 impl_
.SetAlternativeServiceProbabilityThreshold(0.02);
724 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
725 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
726 AlternativeService
canonical_alternative_service(QUIC
, "bar.c.youtube.com",
729 SetAlternativeService(canonical_host_port_pair
, canonical_alternative_service
,
731 EXPECT_TRUE(HasAlternativeService(canonical_host_port_pair
));
732 EXPECT_TRUE(HasAlternativeService(test_host_port_pair
));
735 TEST_F(AlternateProtocolServerPropertiesTest
, ClearCanonical
) {
736 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
737 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
738 AlternativeService
canonical_alternative_service(QUIC
, "bar.c.youtube.com",
741 SetAlternativeService(canonical_host_port_pair
, canonical_alternative_service
,
743 impl_
.ClearAlternativeServices(canonical_host_port_pair
);
744 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
747 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalBroken
) {
748 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
749 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
750 AlternativeService
canonical_alternative_service(QUIC
, "bar.c.youtube.com",
753 SetAlternativeService(canonical_host_port_pair
, canonical_alternative_service
,
755 impl_
.MarkAlternativeServiceBroken(canonical_alternative_service
);
756 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
759 // Adding an alternative service for a new host overrides canonical host.
760 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalOverride
) {
761 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
762 HostPortPair
bar_host_port_pair("bar.c.youtube.com", 80);
763 AlternativeService
bar_alternative_service(QUIC
, "bar.c.youtube.com", 1234);
764 SetAlternativeService(bar_host_port_pair
, bar_alternative_service
, 1.0);
765 AlternativeServiceVector alternative_service_vector
=
766 impl_
.GetAlternativeServices(test_host_port_pair
);
767 ASSERT_EQ(1u, alternative_service_vector
.size());
768 EXPECT_EQ(bar_alternative_service
, alternative_service_vector
[0]);
770 HostPortPair
qux_host_port_pair("qux.c.youtube.com", 80);
771 AlternativeService
qux_alternative_service(QUIC
, "qux.c.youtube.com", 443);
772 SetAlternativeService(qux_host_port_pair
, qux_alternative_service
, 1.0);
773 alternative_service_vector
=
774 impl_
.GetAlternativeServices(test_host_port_pair
);
775 ASSERT_EQ(1u, alternative_service_vector
.size());
776 EXPECT_EQ(qux_alternative_service
, alternative_service_vector
[0]);
779 TEST_F(AlternateProtocolServerPropertiesTest
, ClearWithCanonical
) {
780 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
781 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
782 AlternativeService
canonical_alternative_service(QUIC
, "bar.c.youtube.com",
785 SetAlternativeService(canonical_host_port_pair
, canonical_alternative_service
,
788 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
791 TEST_F(AlternateProtocolServerPropertiesTest
,
792 ExpireBrokenAlternateProtocolMappings
) {
793 HostPortPair
host_port_pair("foo", 443);
794 AlternativeService
alternative_service(QUIC
, "foo", 443);
795 SetAlternativeService(host_port_pair
, alternative_service
, 1.0);
796 EXPECT_TRUE(HasAlternativeService(host_port_pair
));
797 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
798 EXPECT_FALSE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
800 base::TimeTicks past
=
801 base::TimeTicks::Now() - base::TimeDelta::FromSeconds(42);
802 HttpServerPropertiesImplPeer::AddBrokenAlternativeServiceWithExpirationTime(
803 impl_
, alternative_service
, past
);
804 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service
));
805 EXPECT_TRUE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
807 HttpServerPropertiesImplPeer::ExpireBrokenAlternateProtocolMappings(impl_
);
808 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
809 EXPECT_TRUE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
812 // Regression test for https://crbug.com/505413.
813 TEST_F(AlternateProtocolServerPropertiesTest
, RemoveExpiredBrokenAltSvc
) {
814 HostPortPair
foo_host_port_pair("foo", 443);
815 AlternativeService
bar_alternative_service(QUIC
, "bar", 443);
816 SetAlternativeService(foo_host_port_pair
, bar_alternative_service
, 1.0);
817 EXPECT_TRUE(HasAlternativeService(foo_host_port_pair
));
819 HostPortPair
bar_host_port_pair1("bar", 80);
820 AlternativeService
nohost_alternative_service(QUIC
, "", 443);
821 SetAlternativeService(bar_host_port_pair1
, nohost_alternative_service
, 1.0);
822 EXPECT_TRUE(HasAlternativeService(bar_host_port_pair1
));
824 HostPortPair
bar_host_port_pair2("bar", 443);
825 AlternativeService
baz_alternative_service(QUIC
, "baz", 1234);
826 SetAlternativeService(bar_host_port_pair2
, baz_alternative_service
, 1.0);
827 EXPECT_TRUE(HasAlternativeService(bar_host_port_pair2
));
829 // Mark "bar:443" as broken.
830 base::TimeTicks past
=
831 base::TimeTicks::Now() - base::TimeDelta::FromSeconds(42);
832 HttpServerPropertiesImplPeer::AddBrokenAlternativeServiceWithExpirationTime(
833 impl_
, bar_alternative_service
, past
);
835 // Expire brokenness of "bar:443".
836 HttpServerPropertiesImplPeer::ExpireBrokenAlternateProtocolMappings(impl_
);
838 // "foo:443" should have no alternative service now.
839 EXPECT_FALSE(HasAlternativeService(foo_host_port_pair
));
840 // "bar:80" should have no alternative service now.
841 EXPECT_FALSE(HasAlternativeService(bar_host_port_pair1
));
842 // The alternative service of "bar:443" should be unaffected.
843 EXPECT_TRUE(HasAlternativeService(bar_host_port_pair2
));
846 impl_
.WasAlternativeServiceRecentlyBroken(bar_alternative_service
));
848 impl_
.WasAlternativeServiceRecentlyBroken(baz_alternative_service
));
851 typedef HttpServerPropertiesImplTest SpdySettingsServerPropertiesTest
;
853 TEST_F(SpdySettingsServerPropertiesTest
, Initialize
) {
854 HostPortPair
spdy_server_google("www.google.com", 443);
856 // Check by initializing empty spdy settings.
857 SpdySettingsMap
spdy_settings_map(SpdySettingsMap::NO_AUTO_EVICT
);
858 impl_
.InitializeSpdySettingsServers(&spdy_settings_map
);
859 EXPECT_TRUE(impl_
.GetSpdySettings(spdy_server_google
).empty());
861 // Check by initializing with www.google.com:443 spdy server settings.
862 SettingsMap settings_map
;
863 const SpdySettingsIds id
= SETTINGS_UPLOAD_BANDWIDTH
;
864 const SpdySettingsFlags flags
= SETTINGS_FLAG_PERSISTED
;
865 const uint32 value
= 31337;
866 SettingsFlagsAndValue
flags_and_value(flags
, value
);
867 settings_map
[id
] = flags_and_value
;
868 spdy_settings_map
.Put(spdy_server_google
, settings_map
);
869 impl_
.InitializeSpdySettingsServers(&spdy_settings_map
);
871 const SettingsMap
& settings_map2
= impl_
.GetSpdySettings(spdy_server_google
);
872 ASSERT_EQ(1U, settings_map2
.size());
873 SettingsMap::const_iterator it
= settings_map2
.find(id
);
874 EXPECT_TRUE(it
!= settings_map2
.end());
875 SettingsFlagsAndValue flags_and_value2
= it
->second
;
876 EXPECT_EQ(flags
, flags_and_value2
.first
);
877 EXPECT_EQ(value
, flags_and_value2
.second
);
880 TEST_F(SpdySettingsServerPropertiesTest
, SetSpdySetting
) {
881 HostPortPair
spdy_server_empty(std::string(), 443);
882 const SettingsMap
& settings_map0
= impl_
.GetSpdySettings(spdy_server_empty
);
883 EXPECT_EQ(0U, settings_map0
.size()); // Returns kEmptySettingsMap.
885 // Add www.google.com:443 as persisting.
886 HostPortPair
spdy_server_google("www.google.com", 443);
887 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
888 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
889 const uint32 value1
= 31337;
890 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_google
, id1
, flags1
, value1
));
892 const SettingsMap
& settings_map1_ret
=
893 impl_
.GetSpdySettings(spdy_server_google
);
894 ASSERT_EQ(1U, settings_map1_ret
.size());
895 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
896 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
897 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
898 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
899 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
901 // Add mail.google.com:443 as not persisting.
902 HostPortPair
spdy_server_mail("mail.google.com", 443);
903 const SpdySettingsIds id2
= SETTINGS_DOWNLOAD_BANDWIDTH
;
904 const SpdySettingsFlags flags2
= SETTINGS_FLAG_NONE
;
905 const uint32 value2
= 62667;
906 EXPECT_FALSE(impl_
.SetSpdySetting(spdy_server_mail
, id2
, flags2
, value2
));
907 const SettingsMap
& settings_map2_ret
=
908 impl_
.GetSpdySettings(spdy_server_mail
);
909 EXPECT_EQ(0U, settings_map2_ret
.size()); // Returns kEmptySettingsMap.
911 // Add docs.google.com:443 as persisting
912 HostPortPair
spdy_server_docs("docs.google.com", 443);
913 const SpdySettingsIds id3
= SETTINGS_ROUND_TRIP_TIME
;
914 const SpdySettingsFlags flags3
= SETTINGS_FLAG_PLEASE_PERSIST
;
915 const uint32 value3
= 93997;
916 SettingsFlagsAndValue
flags_and_value3(flags3
, value3
);
917 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_docs
, id3
, flags3
, value3
));
919 const SettingsMap
& settings_map3_ret
=
920 impl_
.GetSpdySettings(spdy_server_docs
);
921 ASSERT_EQ(1U, settings_map3_ret
.size());
922 SettingsMap::const_iterator it3_ret
= settings_map3_ret
.find(id3
);
923 EXPECT_TRUE(it3_ret
!= settings_map3_ret
.end());
924 SettingsFlagsAndValue flags_and_value3_ret
= it3_ret
->second
;
925 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value3_ret
.first
);
926 EXPECT_EQ(value3
, flags_and_value3_ret
.second
);
928 // Check data for www.google.com:443 (id1).
929 const SettingsMap
& settings_map4_ret
=
930 impl_
.GetSpdySettings(spdy_server_google
);
931 ASSERT_EQ(1U, settings_map4_ret
.size());
932 SettingsMap::const_iterator it4_ret
= settings_map4_ret
.find(id1
);
933 EXPECT_TRUE(it4_ret
!= settings_map4_ret
.end());
934 SettingsFlagsAndValue flags_and_value4_ret
= it4_ret
->second
;
935 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value4_ret
.first
);
936 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
938 // Clear www.google.com:443 as persisting.
939 impl_
.ClearSpdySettings(spdy_server_google
);
941 const SettingsMap
& settings_map5_ret
=
942 impl_
.GetSpdySettings(spdy_server_google
);
943 ASSERT_EQ(0U, settings_map5_ret
.size());
945 // Clear all settings.
946 ASSERT_GT(impl_
.spdy_settings_map().size(), 0U);
947 impl_
.ClearAllSpdySettings();
948 ASSERT_EQ(0U, impl_
.spdy_settings_map().size());
951 TEST_F(SpdySettingsServerPropertiesTest
, Clear
) {
952 // Add www.google.com:443 as persisting.
953 HostPortPair
spdy_server_google("www.google.com", 443);
954 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
955 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
956 const uint32 value1
= 31337;
957 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_google
, id1
, flags1
, value1
));
959 const SettingsMap
& settings_map1_ret
=
960 impl_
.GetSpdySettings(spdy_server_google
);
961 ASSERT_EQ(1U, settings_map1_ret
.size());
962 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
963 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
964 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
965 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
966 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
968 // Add docs.google.com:443 as persisting
969 HostPortPair
spdy_server_docs("docs.google.com", 443);
970 const SpdySettingsIds id3
= SETTINGS_ROUND_TRIP_TIME
;
971 const SpdySettingsFlags flags3
= SETTINGS_FLAG_PLEASE_PERSIST
;
972 const uint32 value3
= 93997;
973 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_docs
, id3
, flags3
, value3
));
975 const SettingsMap
& settings_map3_ret
=
976 impl_
.GetSpdySettings(spdy_server_docs
);
977 ASSERT_EQ(1U, settings_map3_ret
.size());
978 SettingsMap::const_iterator it3_ret
= settings_map3_ret
.find(id3
);
979 EXPECT_TRUE(it3_ret
!= settings_map3_ret
.end());
980 SettingsFlagsAndValue flags_and_value3_ret
= it3_ret
->second
;
981 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value3_ret
.first
);
982 EXPECT_EQ(value3
, flags_and_value3_ret
.second
);
985 EXPECT_EQ(0U, impl_
.GetSpdySettings(spdy_server_google
).size());
986 EXPECT_EQ(0U, impl_
.GetSpdySettings(spdy_server_docs
).size());
989 TEST_F(SpdySettingsServerPropertiesTest
, MRUOfGetSpdySettings
) {
990 // Add www.google.com:443 as persisting.
991 HostPortPair
spdy_server_google("www.google.com", 443);
992 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
993 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
994 const uint32 value1
= 31337;
995 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_google
, id1
, flags1
, value1
));
997 // Add docs.google.com:443 as persisting
998 HostPortPair
spdy_server_docs("docs.google.com", 443);
999 const SpdySettingsIds id2
= SETTINGS_ROUND_TRIP_TIME
;
1000 const SpdySettingsFlags flags2
= SETTINGS_FLAG_PLEASE_PERSIST
;
1001 const uint32 value2
= 93997;
1002 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_docs
, id2
, flags2
, value2
));
1004 // Verify the first element is docs.google.com:443.
1005 const SpdySettingsMap
& map
= impl_
.spdy_settings_map();
1006 SpdySettingsMap::const_iterator it
= map
.begin();
1007 EXPECT_TRUE(it
->first
.Equals(spdy_server_docs
));
1008 const SettingsMap
& settings_map2_ret
= it
->second
;
1009 ASSERT_EQ(1U, settings_map2_ret
.size());
1010 SettingsMap::const_iterator it2_ret
= settings_map2_ret
.find(id2
);
1011 EXPECT_TRUE(it2_ret
!= settings_map2_ret
.end());
1012 SettingsFlagsAndValue flags_and_value2_ret
= it2_ret
->second
;
1013 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value2_ret
.first
);
1014 EXPECT_EQ(value2
, flags_and_value2_ret
.second
);
1016 // GetSpdySettings should reorder the SpdySettingsMap.
1017 const SettingsMap
& settings_map1_ret
=
1018 impl_
.GetSpdySettings(spdy_server_google
);
1019 ASSERT_EQ(1U, settings_map1_ret
.size());
1020 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
1021 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
1022 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
1023 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
1024 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
1026 // Check the first entry is spdy_server_google by accessing it via iterator.
1028 EXPECT_TRUE(it
->first
.Equals(spdy_server_google
));
1029 const SettingsMap
& settings_map1_it_ret
= it
->second
;
1030 ASSERT_EQ(1U, settings_map1_it_ret
.size());
1031 it1_ret
= settings_map1_it_ret
.find(id1
);
1032 EXPECT_TRUE(it1_ret
!= settings_map1_it_ret
.end());
1033 flags_and_value1_ret
= it1_ret
->second
;
1034 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
1035 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
1038 typedef HttpServerPropertiesImplTest SupportsQuicServerPropertiesTest
;
1040 TEST_F(SupportsQuicServerPropertiesTest
, Initialize
) {
1041 HostPortPair
quic_server_google("www.google.com", 443);
1043 // Check by initializing empty address.
1044 IPAddressNumber initial_address
;
1045 impl_
.InitializeSupportsQuic(&initial_address
);
1047 IPAddressNumber address
;
1048 EXPECT_FALSE(impl_
.GetSupportsQuic(&address
));
1049 EXPECT_TRUE(address
.empty());
1051 // Check by initializing with a valid address.
1052 CHECK(ParseIPLiteralToNumber("127.0.0.1", &initial_address
));
1053 impl_
.InitializeSupportsQuic(&initial_address
);
1055 EXPECT_TRUE(impl_
.GetSupportsQuic(&address
));
1056 EXPECT_EQ(initial_address
, address
);
1059 TEST_F(SupportsQuicServerPropertiesTest
, SetSupportsQuic
) {
1060 IPAddressNumber address
;
1061 EXPECT_FALSE(impl_
.GetSupportsQuic(&address
));
1062 EXPECT_TRUE(address
.empty());
1064 IPAddressNumber actual_address
;
1065 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address
));
1066 impl_
.SetSupportsQuic(true, actual_address
);
1068 EXPECT_TRUE(impl_
.GetSupportsQuic(&address
));
1069 EXPECT_EQ(actual_address
, address
);
1073 EXPECT_FALSE(impl_
.GetSupportsQuic(&address
));
1076 typedef HttpServerPropertiesImplTest ServerNetworkStatsServerPropertiesTest
;
1078 TEST_F(ServerNetworkStatsServerPropertiesTest
, Initialize
) {
1079 HostPortPair
google_server("www.google.com", 443);
1081 // Check by initializing empty ServerNetworkStats.
1082 ServerNetworkStatsMap
server_network_stats_map(
1083 ServerNetworkStatsMap::NO_AUTO_EVICT
);
1084 impl_
.InitializeServerNetworkStats(&server_network_stats_map
);
1085 const ServerNetworkStats
* stats
= impl_
.GetServerNetworkStats(google_server
);
1086 EXPECT_EQ(NULL
, stats
);
1088 // Check by initializing with www.google.com:443.
1089 ServerNetworkStats stats1
;
1090 stats1
.srtt
= base::TimeDelta::FromMicroseconds(10);
1091 stats1
.bandwidth_estimate
= QuicBandwidth::FromBitsPerSecond(100);
1092 server_network_stats_map
.Put(google_server
, stats1
);
1093 impl_
.InitializeServerNetworkStats(&server_network_stats_map
);
1095 const ServerNetworkStats
* stats2
= impl_
.GetServerNetworkStats(google_server
);
1096 EXPECT_EQ(10, stats2
->srtt
.ToInternalValue());
1097 EXPECT_EQ(100, stats2
->bandwidth_estimate
.ToBitsPerSecond());
1100 TEST_F(ServerNetworkStatsServerPropertiesTest
, SetServerNetworkStats
) {
1101 HostPortPair
foo_server("foo", 80);
1102 const ServerNetworkStats
* stats
= impl_
.GetServerNetworkStats(foo_server
);
1103 EXPECT_EQ(NULL
, stats
);
1105 ServerNetworkStats stats1
;
1106 stats1
.srtt
= base::TimeDelta::FromMicroseconds(10);
1107 stats1
.bandwidth_estimate
= QuicBandwidth::FromBitsPerSecond(100);
1108 impl_
.SetServerNetworkStats(foo_server
, stats1
);
1110 const ServerNetworkStats
* stats2
= impl_
.GetServerNetworkStats(foo_server
);
1111 EXPECT_EQ(10, stats2
->srtt
.ToInternalValue());
1112 EXPECT_EQ(100, stats2
->bandwidth_estimate
.ToBitsPerSecond());
1115 const ServerNetworkStats
* stats3
= impl_
.GetServerNetworkStats(foo_server
);
1116 EXPECT_EQ(NULL
, stats3
);