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 HttpServerPropertiesImpl impl_
;
61 typedef HttpServerPropertiesImplTest SpdyServerPropertiesTest
;
63 TEST_F(SpdyServerPropertiesTest
, Initialize
) {
64 HostPortPair
spdy_server_google("www.google.com", 443);
65 std::string spdy_server_g
= spdy_server_google
.ToString();
67 HostPortPair
spdy_server_docs("docs.google.com", 443);
68 std::string spdy_server_d
= spdy_server_docs
.ToString();
70 // Check by initializing NULL spdy servers.
71 impl_
.InitializeSpdyServers(NULL
, true);
72 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_google
));
74 // Check by initializing empty spdy servers.
75 std::vector
<std::string
> spdy_servers
;
76 impl_
.InitializeSpdyServers(&spdy_servers
, true);
77 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_google
));
79 // Check by initializing with www.google.com:443 spdy server.
80 std::vector
<std::string
> spdy_servers1
;
81 spdy_servers1
.push_back(spdy_server_g
);
82 impl_
.InitializeSpdyServers(&spdy_servers1
, true);
83 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
85 // Check by initializing with www.google.com:443 and docs.google.com:443 spdy
87 std::vector
<std::string
> spdy_servers2
;
88 spdy_servers2
.push_back(spdy_server_g
);
89 spdy_servers2
.push_back(spdy_server_d
);
90 impl_
.InitializeSpdyServers(&spdy_servers2
, true);
92 // Verify spdy_server_g and spdy_server_d are in the list in the same order.
93 base::ListValue spdy_server_list
;
94 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
95 EXPECT_EQ(2U, spdy_server_list
.GetSize());
96 std::string string_value_g
;
97 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
98 ASSERT_EQ(spdy_server_g
, string_value_g
);
99 std::string string_value_d
;
100 ASSERT_TRUE(spdy_server_list
.GetString(1, &string_value_d
));
101 ASSERT_EQ(spdy_server_d
, string_value_d
);
102 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
103 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_docs
));
106 TEST_F(SpdyServerPropertiesTest
, SupportsRequestPriorityTest
) {
107 HostPortPair
spdy_server_empty(std::string(), 443);
108 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_empty
));
110 // Add www.google.com:443 as supporting SPDY.
111 HostPortPair
spdy_server_google("www.google.com", 443);
112 impl_
.SetSupportsSpdy(spdy_server_google
, true);
113 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
115 // Add mail.google.com:443 as not supporting SPDY.
116 HostPortPair
spdy_server_mail("mail.google.com", 443);
117 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_mail
));
119 // Add docs.google.com:443 as supporting SPDY.
120 HostPortPair
spdy_server_docs("docs.google.com", 443);
121 impl_
.SetSupportsSpdy(spdy_server_docs
, true);
122 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_docs
));
124 // Add www.youtube.com:443 as supporting QUIC.
125 HostPortPair
quic_server_youtube("www.youtube.com", 443);
126 const AlternativeService
alternative_service1(QUIC
, "www.youtube.com", 443);
127 impl_
.SetAlternativeService(quic_server_youtube
, alternative_service1
, 1.0);
128 EXPECT_TRUE(impl_
.SupportsRequestPriority(quic_server_youtube
));
130 // Add www.example.com:443 with two alternative services, one supporting QUIC.
131 HostPortPair
quic_server_example("www.example.com", 443);
132 const AlternativeService
alternative_service2(NPN_HTTP_2
, "", 443);
133 impl_
.SetAlternativeService(quic_server_example
, alternative_service2
, 1.0);
134 impl_
.SetAlternativeService(quic_server_example
, alternative_service1
, 1.0);
135 EXPECT_TRUE(impl_
.SupportsRequestPriority(quic_server_example
));
137 // Verify all the entries are the same after additions.
138 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
139 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_mail
));
140 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_docs
));
141 EXPECT_TRUE(impl_
.SupportsRequestPriority(quic_server_youtube
));
142 EXPECT_TRUE(impl_
.SupportsRequestPriority(quic_server_example
));
145 TEST_F(SpdyServerPropertiesTest
, Clear
) {
146 // Add www.google.com:443 and mail.google.com:443 as supporting SPDY.
147 HostPortPair
spdy_server_google("www.google.com", 443);
148 impl_
.SetSupportsSpdy(spdy_server_google
, true);
149 HostPortPair
spdy_server_mail("mail.google.com", 443);
150 impl_
.SetSupportsSpdy(spdy_server_mail
, true);
152 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
153 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_mail
));
156 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_google
));
157 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_mail
));
160 TEST_F(SpdyServerPropertiesTest
, GetSpdyServerList
) {
161 base::ListValue spdy_server_list
;
163 // Check there are no spdy_servers.
164 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
165 EXPECT_EQ(0U, spdy_server_list
.GetSize());
167 // Check empty server is not added.
168 HostPortPair
spdy_server_empty(std::string(), 443);
169 impl_
.SetSupportsSpdy(spdy_server_empty
, true);
170 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
171 EXPECT_EQ(0U, spdy_server_list
.GetSize());
173 std::string string_value_g
;
174 std::string string_value_m
;
175 HostPortPair
spdy_server_google("www.google.com", 443);
176 std::string spdy_server_g
= spdy_server_google
.ToString();
177 HostPortPair
spdy_server_mail("mail.google.com", 443);
178 std::string spdy_server_m
= spdy_server_mail
.ToString();
180 // Add www.google.com:443 as not supporting SPDY.
181 impl_
.SetSupportsSpdy(spdy_server_google
, false);
182 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
183 EXPECT_EQ(0U, spdy_server_list
.GetSize());
185 // Add www.google.com:443 as supporting SPDY.
186 impl_
.SetSupportsSpdy(spdy_server_google
, true);
187 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
188 ASSERT_EQ(1U, spdy_server_list
.GetSize());
189 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
190 ASSERT_EQ(spdy_server_g
, string_value_g
);
192 // Add mail.google.com:443 as not supporting SPDY.
193 impl_
.SetSupportsSpdy(spdy_server_mail
, false);
194 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
195 ASSERT_EQ(1U, spdy_server_list
.GetSize());
196 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
197 ASSERT_EQ(spdy_server_g
, string_value_g
);
199 // Add mail.google.com:443 as supporting SPDY.
200 impl_
.SetSupportsSpdy(spdy_server_mail
, true);
201 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
202 ASSERT_EQ(2U, spdy_server_list
.GetSize());
204 // Verify www.google.com:443 and mail.google.com:443 are in the list.
205 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_m
));
206 ASSERT_EQ(spdy_server_m
, string_value_m
);
207 ASSERT_TRUE(spdy_server_list
.GetString(1, &string_value_g
));
208 ASSERT_EQ(spdy_server_g
, string_value_g
);
210 // Request for only one server and verify that we get only one server.
211 impl_
.GetSpdyServerList(&spdy_server_list
, 1);
212 ASSERT_EQ(1U, spdy_server_list
.GetSize());
213 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_m
));
214 ASSERT_EQ(spdy_server_m
, string_value_m
);
217 TEST_F(SpdyServerPropertiesTest
, MRUOfGetSpdyServerList
) {
218 base::ListValue spdy_server_list
;
220 std::string string_value_g
;
221 std::string string_value_m
;
222 HostPortPair
spdy_server_google("www.google.com", 443);
223 std::string spdy_server_g
= spdy_server_google
.ToString();
224 HostPortPair
spdy_server_mail("mail.google.com", 443);
225 std::string spdy_server_m
= spdy_server_mail
.ToString();
227 // Add www.google.com:443 as supporting SPDY.
228 impl_
.SetSupportsSpdy(spdy_server_google
, true);
229 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
230 ASSERT_EQ(1U, spdy_server_list
.GetSize());
231 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
232 ASSERT_EQ(spdy_server_g
, string_value_g
);
234 // Add mail.google.com:443 as supporting SPDY. Verify mail.google.com:443 and
235 // www.google.com:443 are in the list.
236 impl_
.SetSupportsSpdy(spdy_server_mail
, true);
237 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
238 ASSERT_EQ(2U, spdy_server_list
.GetSize());
239 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_m
));
240 ASSERT_EQ(spdy_server_m
, string_value_m
);
241 ASSERT_TRUE(spdy_server_list
.GetString(1, &string_value_g
));
242 ASSERT_EQ(spdy_server_g
, string_value_g
);
244 // Get www.google.com:443 should reorder SpdyServerHostPortMap. Verify that it
245 // is www.google.com:443 is the MRU server.
246 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
247 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
248 ASSERT_EQ(2U, spdy_server_list
.GetSize());
249 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
250 ASSERT_EQ(spdy_server_g
, string_value_g
);
251 ASSERT_TRUE(spdy_server_list
.GetString(1, &string_value_m
));
252 ASSERT_EQ(spdy_server_m
, string_value_m
);
255 typedef HttpServerPropertiesImplTest AlternateProtocolServerPropertiesTest
;
257 TEST_F(AlternateProtocolServerPropertiesTest
, Basic
) {
258 HostPortPair
test_host_port_pair("foo", 80);
259 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
261 AlternativeService
alternative_service(NPN_HTTP_2
, "foo", 443);
262 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service
, 1.0);
263 const AlternativeServiceVector alternative_service_vector
=
264 impl_
.GetAlternativeServices(test_host_port_pair
);
265 ASSERT_EQ(1u, alternative_service_vector
.size());
266 EXPECT_EQ(alternative_service
, alternative_service_vector
[0]);
269 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
272 TEST_F(AlternateProtocolServerPropertiesTest
, ExcludeOrigin
) {
273 AlternativeServiceInfoVector alternative_service_info_vector
;
274 // Same hostname, same port, TCP: should be ignored.
275 AlternativeService
alternative_service1(NPN_HTTP_2
, "foo", 443);
276 alternative_service_info_vector
.push_back(
277 AlternativeServiceInfo(alternative_service1
, 1.0));
278 // Different hostname: GetAlternativeServices should return this one.
279 AlternativeService
alternative_service2(NPN_HTTP_2
, "bar", 443);
280 alternative_service_info_vector
.push_back(
281 AlternativeServiceInfo(alternative_service2
, 1.0));
282 // Different port: GetAlternativeServices should return this one too.
283 AlternativeService
alternative_service3(NPN_HTTP_2
, "foo", 80);
284 alternative_service_info_vector
.push_back(
285 AlternativeServiceInfo(alternative_service3
, 1.0));
286 // QUIC: GetAlternativeServices should return this one too.
287 AlternativeService
alternative_service4(QUIC
, "foo", 443);
288 alternative_service_info_vector
.push_back(
289 AlternativeServiceInfo(alternative_service4
, 1.0));
291 HostPortPair
test_host_port_pair("foo", 443);
292 impl_
.SetAlternativeServices(test_host_port_pair
,
293 alternative_service_info_vector
);
295 const AlternativeServiceVector alternative_service_vector
=
296 impl_
.GetAlternativeServices(test_host_port_pair
);
297 ASSERT_EQ(3u, alternative_service_vector
.size());
298 EXPECT_EQ(alternative_service2
, alternative_service_vector
[0]);
299 EXPECT_EQ(alternative_service3
, alternative_service_vector
[1]);
300 EXPECT_EQ(alternative_service4
, alternative_service_vector
[2]);
303 TEST_F(AlternateProtocolServerPropertiesTest
, DefaultProbabilityExcluded
) {
304 HostPortPair
test_host_port_pair("foo", 80);
305 const AlternativeService
alternative_service(NPN_HTTP_2
, "foo", 443);
306 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service
, 0.99);
308 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
311 // GetAlternativeServices and HasAlternativeServices should only return the ones
312 // with probability greater than or equal to the threshold.
313 TEST_F(AlternateProtocolServerPropertiesTest
, Probability
) {
314 impl_
.SetAlternativeServiceProbabilityThreshold(0.5);
316 AlternativeServiceInfoVector alternative_service_info_vector
;
317 const AlternativeService
alternative_service1(NPN_HTTP_2
, "foo", 443);
318 alternative_service_info_vector
.push_back(
319 AlternativeServiceInfo(alternative_service1
, 0.3));
320 const AlternativeService
alternative_service2(QUIC
, "bar", 123);
321 alternative_service_info_vector
.push_back(
322 AlternativeServiceInfo(alternative_service2
, 0.7));
323 const AlternativeService
alternative_service3(NPN_SPDY_3_1
, "baz", 443);
324 alternative_service_info_vector
.push_back(
325 AlternativeServiceInfo(alternative_service3
, 0.4));
326 const AlternativeService
alternative_service4(NPN_HTTP_2
, "qux", 1234);
327 alternative_service_info_vector
.push_back(
328 AlternativeServiceInfo(alternative_service4
, 0.6));
330 HostPortPair
test_host_port_pair("foo", 80);
331 impl_
.SetAlternativeServices(test_host_port_pair
,
332 alternative_service_info_vector
);
334 const AlternativeServiceVector alternative_service_vector
=
335 impl_
.GetAlternativeServices(test_host_port_pair
);
336 ASSERT_EQ(2u, alternative_service_vector
.size());
337 EXPECT_EQ(alternative_service2
, alternative_service_vector
[0]);
338 EXPECT_EQ(alternative_service4
, alternative_service_vector
[1]);
341 TEST_F(AlternateProtocolServerPropertiesTest
, ProbabilityExcluded
) {
342 impl_
.SetAlternativeServiceProbabilityThreshold(0.75);
344 HostPortPair
test_host_port_pair("foo", 80);
345 const AlternativeService
alternative_service(NPN_HTTP_2
, "foo", 443);
346 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service
, 0.5);
347 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
350 TEST_F(AlternateProtocolServerPropertiesTest
, Initialize
) {
351 // |test_host_port_pair1| has one alternative service, which is non-broken,
352 // and thus will be removed by InitializeAlternativeServiceServers().
353 HostPortPair
test_host_port_pair1("foo1", 80);
354 const AlternativeService
alternative_service1(NPN_HTTP_2
, "bar1", 443);
355 impl_
.SetAlternativeService(test_host_port_pair1
, alternative_service1
, 1.0);
357 // |test_host_port_pair2| has two alternative services. The broken one will
358 // remain, the non-broken one will be removed by
359 // InitializeAlternativeServiceServers().
360 AlternativeServiceInfoVector alternative_service_info_vector
;
361 const AlternativeService
alternative_service2(NPN_SPDY_3_1
, "bar2", 443);
362 alternative_service_info_vector
.push_back(
363 AlternativeServiceInfo(alternative_service2
, 1.0));
364 const AlternativeService
alternative_service3(NPN_SPDY_3_1
, "bar3", 1234);
365 alternative_service_info_vector
.push_back(
366 AlternativeServiceInfo(alternative_service3
, 0.8));
367 HostPortPair
test_host_port_pair2("foo2", 80);
368 impl_
.SetAlternativeServices(test_host_port_pair2
,
369 alternative_service_info_vector
);
370 impl_
.MarkAlternativeServiceBroken(alternative_service2
);
372 // Prepare |alternative_service_map| to be loaded by
373 // InitializeAlternativeServiceServers().
374 AlternativeServiceMap
alternative_service_map(
375 AlternativeServiceMap::NO_AUTO_EVICT
);
376 const AlternativeService
alternative_service4(NPN_HTTP_2
, "bar4", 123);
377 const AlternativeServiceInfo
alternative_service_info1(alternative_service4
,
379 alternative_service_map
.Put(
380 test_host_port_pair2
,
381 AlternativeServiceInfoVector(/*size=*/1, alternative_service_info1
));
383 HostPortPair
test_host_port_pair3("foo3", 80);
384 const AlternativeService
alternative_service5(NPN_HTTP_2
, "bar5", 1234);
385 const AlternativeServiceInfo
alternative_service_info2(alternative_service5
,
387 alternative_service_map
.Put(
388 test_host_port_pair3
,
389 AlternativeServiceInfoVector(/*size=*/1, alternative_service_info2
));
391 impl_
.InitializeAlternativeServiceServers(&alternative_service_map
);
393 // Verify alternative_service_map.
394 const AlternativeServiceMap
& map
= impl_
.alternative_service_map();
395 ASSERT_EQ(2u, map
.size());
396 AlternativeServiceMap::const_iterator map_it
= map
.begin();
397 EXPECT_TRUE(map_it
->first
.Equals(test_host_port_pair3
));
398 ASSERT_EQ(1u, map_it
->second
.size());
399 EXPECT_EQ(alternative_service5
, map_it
->second
[0].alternative_service
);
400 EXPECT_EQ(0.2, map_it
->second
[0].probability
);
402 EXPECT_TRUE(map_it
->first
.Equals(test_host_port_pair2
));
403 ASSERT_EQ(2u, map_it
->second
.size());
404 EXPECT_EQ(alternative_service2
, map_it
->second
[0].alternative_service
);
405 EXPECT_EQ(1.0, map_it
->second
[0].probability
);
406 EXPECT_EQ(alternative_service4
, map_it
->second
[1].alternative_service
);
407 EXPECT_EQ(0.7, map_it
->second
[1].probability
);
410 // Regression test for https://crbug.com/504032:
411 // InitializeAlternativeServiceServers() should not crash if there is an empty
412 // hostname is the mapping.
413 TEST_F(AlternateProtocolServerPropertiesTest
, InitializeWithEmptyHostname
) {
414 const HostPortPair
host_port_pair("foo", 443);
415 const AlternativeService
alternative_service_with_empty_hostname(NPN_HTTP_2
,
417 const AlternativeService
alternative_service_with_foo_hostname(NPN_HTTP_2
,
419 impl_
.SetAlternativeService(host_port_pair
,
420 alternative_service_with_empty_hostname
, 1.0);
421 impl_
.MarkAlternativeServiceBroken(alternative_service_with_foo_hostname
);
423 AlternativeServiceMap
alternative_service_map(
424 AlternativeServiceMap::NO_AUTO_EVICT
);
425 impl_
.InitializeAlternativeServiceServers(&alternative_service_map
);
428 impl_
.IsAlternativeServiceBroken(alternative_service_with_foo_hostname
));
429 const AlternativeServiceVector alternative_service_vector
=
430 impl_
.GetAlternativeServices(host_port_pair
);
431 ASSERT_EQ(1u, alternative_service_vector
.size());
432 EXPECT_EQ(alternative_service_with_foo_hostname
,
433 alternative_service_vector
[0]);
436 TEST_F(AlternateProtocolServerPropertiesTest
, MRUOfGetAlternativeServices
) {
437 HostPortPair
test_host_port_pair1("foo1", 80);
438 const AlternativeService
alternative_service1(NPN_SPDY_3_1
, "foo1", 443);
439 impl_
.SetAlternativeService(test_host_port_pair1
, alternative_service1
, 1.0);
440 HostPortPair
test_host_port_pair2("foo2", 80);
441 const AlternativeService
alternative_service2(NPN_HTTP_2
, "foo2", 1234);
442 impl_
.SetAlternativeService(test_host_port_pair2
, alternative_service2
, 1.0);
444 const AlternativeServiceMap
& map
= impl_
.alternative_service_map();
445 AlternativeServiceMap::const_iterator it
= map
.begin();
446 EXPECT_TRUE(it
->first
.Equals(test_host_port_pair2
));
447 ASSERT_EQ(1u, it
->second
.size());
448 EXPECT_EQ(alternative_service2
, it
->second
[0].alternative_service
);
450 const AlternativeServiceVector alternative_service_vector
=
451 impl_
.GetAlternativeServices(test_host_port_pair1
);
452 ASSERT_EQ(1u, alternative_service_vector
.size());
453 EXPECT_EQ(alternative_service1
, alternative_service_vector
[0]);
455 // GetAlternativeServices should reorder the AlternateProtocol map.
457 EXPECT_TRUE(it
->first
.Equals(test_host_port_pair1
));
458 ASSERT_EQ(1u, it
->second
.size());
459 EXPECT_EQ(alternative_service1
, it
->second
[0].alternative_service
);
462 TEST_F(AlternateProtocolServerPropertiesTest
, SetBroken
) {
463 HostPortPair
test_host_port_pair("foo", 80);
464 const AlternativeService
alternative_service1(NPN_HTTP_2
, "foo", 443);
465 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service1
, 1.0);
466 AlternativeServiceVector alternative_service_vector
=
467 impl_
.GetAlternativeServices(test_host_port_pair
);
468 ASSERT_EQ(1u, alternative_service_vector
.size());
469 EXPECT_EQ(alternative_service1
, alternative_service_vector
[0]);
470 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service1
));
472 // GetAlternativeServices should return the broken alternative service.
473 impl_
.MarkAlternativeServiceBroken(alternative_service1
);
474 alternative_service_vector
=
475 impl_
.GetAlternativeServices(test_host_port_pair
);
476 ASSERT_EQ(1u, alternative_service_vector
.size());
477 EXPECT_EQ(alternative_service1
, alternative_service_vector
[0]);
478 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service1
));
480 // SetAlternativeServices should add a broken alternative service to the map.
481 AlternativeServiceInfoVector alternative_service_info_vector
;
482 alternative_service_info_vector
.push_back(
483 AlternativeServiceInfo(alternative_service1
, 1.0));
484 const AlternativeService
alternative_service2(NPN_HTTP_2
, "foo", 1234);
485 alternative_service_info_vector
.push_back(
486 AlternativeServiceInfo(alternative_service2
, 1.0));
487 impl_
.SetAlternativeServices(test_host_port_pair
,
488 alternative_service_info_vector
);
489 alternative_service_vector
=
490 impl_
.GetAlternativeServices(test_host_port_pair
);
491 ASSERT_EQ(2u, alternative_service_vector
.size());
492 EXPECT_EQ(alternative_service1
, alternative_service_vector
[0]);
493 EXPECT_EQ(alternative_service2
, alternative_service_vector
[1]);
494 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service_vector
[0]));
495 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service_vector
[1]));
497 // SetAlternativeService should add a broken alternative service to the map.
498 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service1
, 1.0);
499 alternative_service_vector
=
500 impl_
.GetAlternativeServices(test_host_port_pair
);
501 ASSERT_EQ(1u, alternative_service_vector
.size());
502 EXPECT_EQ(alternative_service1
, alternative_service_vector
[0]);
503 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service_vector
[0]));
506 TEST_F(AlternateProtocolServerPropertiesTest
, ClearAlternativeServices
) {
507 AlternativeServiceInfoVector alternative_service_info_vector
;
508 const AlternativeService
alternative_service1(NPN_SPDY_3_1
, "foo", 443);
509 alternative_service_info_vector
.push_back(
510 AlternativeServiceInfo(alternative_service1
, 1.0));
511 const AlternativeService
alternative_service2(NPN_HTTP_2
, "bar", 1234);
512 alternative_service_info_vector
.push_back(
513 AlternativeServiceInfo(alternative_service2
, 1.0));
514 HostPortPair
test_host_port_pair("foo", 80);
515 impl_
.SetAlternativeServices(test_host_port_pair
,
516 alternative_service_info_vector
);
518 const net::AlternativeServiceMap
& map
= impl_
.alternative_service_map();
519 net::AlternativeServiceMap::const_iterator it
= map
.begin();
520 EXPECT_TRUE(it
->first
.Equals(test_host_port_pair
));
521 ASSERT_EQ(2u, it
->second
.size());
522 EXPECT_EQ(alternative_service1
, it
->second
[0].alternative_service
);
523 EXPECT_EQ(alternative_service2
, it
->second
[1].alternative_service
);
525 impl_
.ClearAlternativeServices(test_host_port_pair
);
526 EXPECT_TRUE(map
.empty());
529 // A broken alternative service in the mapping carries meaningful information,
530 // therefore it should not be ignored by SetAlternativeService(). In
531 // particular, an alternative service mapped to an origin shadows alternative
532 // services of canonical hosts.
533 TEST_F(AlternateProtocolServerPropertiesTest
, BrokenShadowsCanonical
) {
534 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
535 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
536 AlternativeService
canonical_alternative_service(QUIC
, "bar.c.youtube.com",
538 impl_
.SetAlternativeService(canonical_host_port_pair
,
539 canonical_alternative_service
, 1.0);
540 AlternativeServiceVector alternative_service_vector
=
541 impl_
.GetAlternativeServices(test_host_port_pair
);
542 ASSERT_EQ(1u, alternative_service_vector
.size());
543 EXPECT_EQ(canonical_alternative_service
, alternative_service_vector
[0]);
545 const AlternativeService
broken_alternative_service(NPN_HTTP_2
, "foo", 443);
546 impl_
.MarkAlternativeServiceBroken(broken_alternative_service
);
547 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(broken_alternative_service
));
549 impl_
.SetAlternativeService(test_host_port_pair
, broken_alternative_service
,
551 alternative_service_vector
=
552 impl_
.GetAlternativeServices(test_host_port_pair
);
553 ASSERT_EQ(1u, alternative_service_vector
.size());
554 EXPECT_EQ(broken_alternative_service
, alternative_service_vector
[0]);
555 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(broken_alternative_service
));
558 TEST_F(AlternateProtocolServerPropertiesTest
, ClearBroken
) {
559 HostPortPair
test_host_port_pair("foo", 80);
560 const AlternativeService
alternative_service(NPN_HTTP_2
, "foo", 443);
561 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service
, 1.0);
562 impl_
.MarkAlternativeServiceBroken(alternative_service
);
563 ASSERT_TRUE(HasAlternativeService(test_host_port_pair
));
564 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service
));
565 // ClearAlternativeServices should leave a broken alternative service marked
567 impl_
.ClearAlternativeServices(test_host_port_pair
);
568 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service
));
571 TEST_F(AlternateProtocolServerPropertiesTest
, MarkRecentlyBroken
) {
572 HostPortPair
host_port_pair("foo", 80);
573 const AlternativeService
alternative_service(NPN_HTTP_2
, "foo", 443);
574 impl_
.SetAlternativeService(host_port_pair
, alternative_service
, 1.0);
576 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
577 EXPECT_FALSE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
579 impl_
.MarkAlternativeServiceRecentlyBroken(alternative_service
);
580 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
581 EXPECT_TRUE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
583 impl_
.ConfirmAlternativeService(alternative_service
);
584 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
585 EXPECT_FALSE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
588 TEST_F(AlternateProtocolServerPropertiesTest
, Canonical
) {
589 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
590 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
592 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
593 EXPECT_FALSE(HasAlternativeService(canonical_host_port_pair
));
595 AlternativeServiceInfoVector alternative_service_info_vector
;
596 const AlternativeService
canonical_alternative_service1(
597 QUIC
, "bar.c.youtube.com", 1234);
598 alternative_service_info_vector
.push_back(
599 AlternativeServiceInfo(canonical_alternative_service1
, 1.0));
600 const AlternativeService
canonical_alternative_service2(NPN_HTTP_2
, "", 443);
601 alternative_service_info_vector
.push_back(
602 AlternativeServiceInfo(canonical_alternative_service2
, 1.0));
603 impl_
.SetAlternativeServices(canonical_host_port_pair
,
604 alternative_service_info_vector
);
606 // Since |test_host_port_pair| does not have an alternative service itself,
607 // GetAlternativeServices should return those of |canonical_host_port_pair|.
608 AlternativeServiceVector alternative_service_vector
=
609 impl_
.GetAlternativeServices(test_host_port_pair
);
610 ASSERT_EQ(2u, alternative_service_vector
.size());
611 EXPECT_EQ(canonical_alternative_service1
, alternative_service_vector
[0]);
613 // Since |canonical_alternative_service2| has an empty host,
614 // GetAlternativeServices should substitute the hostname of its |origin|
616 EXPECT_EQ(test_host_port_pair
.host(), alternative_service_vector
[1].host
);
617 EXPECT_EQ(canonical_alternative_service2
.protocol
,
618 alternative_service_vector
[1].protocol
);
619 EXPECT_EQ(canonical_alternative_service2
.port
,
620 alternative_service_vector
[1].port
);
622 // Verify the canonical suffix.
623 EXPECT_EQ(".c.youtube.com",
624 impl_
.GetCanonicalSuffix(test_host_port_pair
.host()));
625 EXPECT_EQ(".c.youtube.com",
626 impl_
.GetCanonicalSuffix(canonical_host_port_pair
.host()));
629 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalBelowThreshold
) {
630 impl_
.SetAlternativeServiceProbabilityThreshold(0.02);
632 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
633 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
634 AlternativeService
canonical_alternative_service(QUIC
, "bar.c.youtube.com",
637 impl_
.SetAlternativeService(canonical_host_port_pair
,
638 canonical_alternative_service
, 0.01);
639 EXPECT_FALSE(HasAlternativeService(canonical_host_port_pair
));
640 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
643 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalAboveThreshold
) {
644 impl_
.SetAlternativeServiceProbabilityThreshold(0.02);
646 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
647 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
648 AlternativeService
canonical_alternative_service(QUIC
, "bar.c.youtube.com",
651 impl_
.SetAlternativeService(canonical_host_port_pair
,
652 canonical_alternative_service
, 0.03);
653 EXPECT_TRUE(HasAlternativeService(canonical_host_port_pair
));
654 EXPECT_TRUE(HasAlternativeService(test_host_port_pair
));
657 TEST_F(AlternateProtocolServerPropertiesTest
, ClearCanonical
) {
658 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
659 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
660 AlternativeService
canonical_alternative_service(QUIC
, "bar.c.youtube.com",
663 impl_
.SetAlternativeService(canonical_host_port_pair
,
664 canonical_alternative_service
, 1.0);
665 impl_
.ClearAlternativeServices(canonical_host_port_pair
);
666 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
669 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalBroken
) {
670 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
671 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
672 AlternativeService
canonical_alternative_service(QUIC
, "bar.c.youtube.com",
675 impl_
.SetAlternativeService(canonical_host_port_pair
,
676 canonical_alternative_service
, 1.0);
677 impl_
.MarkAlternativeServiceBroken(canonical_alternative_service
);
678 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
681 // Adding an alternative service for a new host overrides canonical host.
682 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalOverride
) {
683 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
684 HostPortPair
bar_host_port_pair("bar.c.youtube.com", 80);
685 AlternativeService
bar_alternative_service(QUIC
, "bar.c.youtube.com", 1234);
686 impl_
.SetAlternativeService(bar_host_port_pair
, bar_alternative_service
, 1.0);
687 AlternativeServiceVector alternative_service_vector
=
688 impl_
.GetAlternativeServices(test_host_port_pair
);
689 ASSERT_EQ(1u, alternative_service_vector
.size());
690 EXPECT_EQ(bar_alternative_service
, alternative_service_vector
[0]);
692 HostPortPair
qux_host_port_pair("qux.c.youtube.com", 80);
693 AlternativeService
qux_alternative_service(QUIC
, "qux.c.youtube.com", 443);
694 impl_
.SetAlternativeService(qux_host_port_pair
, qux_alternative_service
, 1.0);
695 alternative_service_vector
=
696 impl_
.GetAlternativeServices(test_host_port_pair
);
697 ASSERT_EQ(1u, alternative_service_vector
.size());
698 EXPECT_EQ(qux_alternative_service
, alternative_service_vector
[0]);
701 TEST_F(AlternateProtocolServerPropertiesTest
, ClearWithCanonical
) {
702 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
703 HostPortPair
canonical_host_port_pair("bar.c.youtube.com", 80);
704 AlternativeService
canonical_alternative_service(QUIC
, "bar.c.youtube.com",
707 impl_
.SetAlternativeService(canonical_host_port_pair
,
708 canonical_alternative_service
, 1.0);
710 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
713 TEST_F(AlternateProtocolServerPropertiesTest
,
714 ExpireBrokenAlternateProtocolMappings
) {
715 HostPortPair
host_port_pair("foo", 443);
716 AlternativeService
alternative_service(QUIC
, "foo", 443);
717 impl_
.SetAlternativeService(host_port_pair
, alternative_service
, 1.0);
718 EXPECT_TRUE(HasAlternativeService(host_port_pair
));
719 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
720 EXPECT_FALSE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
722 base::TimeTicks past
=
723 base::TimeTicks::Now() - base::TimeDelta::FromSeconds(42);
724 HttpServerPropertiesImplPeer::AddBrokenAlternativeServiceWithExpirationTime(
725 impl_
, alternative_service
, past
);
726 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service
));
727 EXPECT_TRUE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
729 HttpServerPropertiesImplPeer::ExpireBrokenAlternateProtocolMappings(impl_
);
730 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
731 EXPECT_TRUE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
734 // Regression test for https://crbug.com/505413.
735 TEST_F(AlternateProtocolServerPropertiesTest
, RemoveExpiredBrokenAltSvc
) {
736 HostPortPair
foo_host_port_pair("foo", 443);
737 AlternativeService
bar_alternative_service(QUIC
, "bar", 443);
738 impl_
.SetAlternativeService(foo_host_port_pair
, bar_alternative_service
, 1.0);
739 EXPECT_TRUE(HasAlternativeService(foo_host_port_pair
));
741 HostPortPair
bar_host_port_pair1("bar", 80);
742 AlternativeService
nohost_alternative_service(QUIC
, "", 443);
743 impl_
.SetAlternativeService(bar_host_port_pair1
, nohost_alternative_service
,
745 EXPECT_TRUE(HasAlternativeService(bar_host_port_pair1
));
747 HostPortPair
bar_host_port_pair2("bar", 443);
748 AlternativeService
baz_alternative_service(QUIC
, "baz", 1234);
749 impl_
.SetAlternativeService(bar_host_port_pair2
, baz_alternative_service
,
751 EXPECT_TRUE(HasAlternativeService(bar_host_port_pair2
));
753 // Mark "bar:443" as broken.
754 base::TimeTicks past
=
755 base::TimeTicks::Now() - base::TimeDelta::FromSeconds(42);
756 HttpServerPropertiesImplPeer::AddBrokenAlternativeServiceWithExpirationTime(
757 impl_
, bar_alternative_service
, past
);
759 // Expire brokenness of "bar:443".
760 HttpServerPropertiesImplPeer::ExpireBrokenAlternateProtocolMappings(impl_
);
762 // "foo:443" should have no alternative service now.
763 EXPECT_FALSE(HasAlternativeService(foo_host_port_pair
));
764 // "bar:80" should have no alternative service now.
765 EXPECT_FALSE(HasAlternativeService(bar_host_port_pair1
));
766 // The alternative service of "bar:443" should be unaffected.
767 EXPECT_TRUE(HasAlternativeService(bar_host_port_pair2
));
770 impl_
.WasAlternativeServiceRecentlyBroken(bar_alternative_service
));
772 impl_
.WasAlternativeServiceRecentlyBroken(baz_alternative_service
));
775 typedef HttpServerPropertiesImplTest SpdySettingsServerPropertiesTest
;
777 TEST_F(SpdySettingsServerPropertiesTest
, Initialize
) {
778 HostPortPair
spdy_server_google("www.google.com", 443);
780 // Check by initializing empty spdy settings.
781 SpdySettingsMap
spdy_settings_map(SpdySettingsMap::NO_AUTO_EVICT
);
782 impl_
.InitializeSpdySettingsServers(&spdy_settings_map
);
783 EXPECT_TRUE(impl_
.GetSpdySettings(spdy_server_google
).empty());
785 // Check by initializing with www.google.com:443 spdy server settings.
786 SettingsMap settings_map
;
787 const SpdySettingsIds id
= SETTINGS_UPLOAD_BANDWIDTH
;
788 const SpdySettingsFlags flags
= SETTINGS_FLAG_PERSISTED
;
789 const uint32 value
= 31337;
790 SettingsFlagsAndValue
flags_and_value(flags
, value
);
791 settings_map
[id
] = flags_and_value
;
792 spdy_settings_map
.Put(spdy_server_google
, settings_map
);
793 impl_
.InitializeSpdySettingsServers(&spdy_settings_map
);
795 const SettingsMap
& settings_map2
= impl_
.GetSpdySettings(spdy_server_google
);
796 ASSERT_EQ(1U, settings_map2
.size());
797 SettingsMap::const_iterator it
= settings_map2
.find(id
);
798 EXPECT_TRUE(it
!= settings_map2
.end());
799 SettingsFlagsAndValue flags_and_value2
= it
->second
;
800 EXPECT_EQ(flags
, flags_and_value2
.first
);
801 EXPECT_EQ(value
, flags_and_value2
.second
);
804 TEST_F(SpdySettingsServerPropertiesTest
, SetSpdySetting
) {
805 HostPortPair
spdy_server_empty(std::string(), 443);
806 const SettingsMap
& settings_map0
= impl_
.GetSpdySettings(spdy_server_empty
);
807 EXPECT_EQ(0U, settings_map0
.size()); // Returns kEmptySettingsMap.
809 // Add www.google.com:443 as persisting.
810 HostPortPair
spdy_server_google("www.google.com", 443);
811 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
812 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
813 const uint32 value1
= 31337;
814 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_google
, id1
, flags1
, value1
));
816 const SettingsMap
& settings_map1_ret
=
817 impl_
.GetSpdySettings(spdy_server_google
);
818 ASSERT_EQ(1U, settings_map1_ret
.size());
819 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
820 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
821 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
822 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
823 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
825 // Add mail.google.com:443 as not persisting.
826 HostPortPair
spdy_server_mail("mail.google.com", 443);
827 const SpdySettingsIds id2
= SETTINGS_DOWNLOAD_BANDWIDTH
;
828 const SpdySettingsFlags flags2
= SETTINGS_FLAG_NONE
;
829 const uint32 value2
= 62667;
830 EXPECT_FALSE(impl_
.SetSpdySetting(spdy_server_mail
, id2
, flags2
, value2
));
831 const SettingsMap
& settings_map2_ret
=
832 impl_
.GetSpdySettings(spdy_server_mail
);
833 EXPECT_EQ(0U, settings_map2_ret
.size()); // Returns kEmptySettingsMap.
835 // Add docs.google.com:443 as persisting
836 HostPortPair
spdy_server_docs("docs.google.com", 443);
837 const SpdySettingsIds id3
= SETTINGS_ROUND_TRIP_TIME
;
838 const SpdySettingsFlags flags3
= SETTINGS_FLAG_PLEASE_PERSIST
;
839 const uint32 value3
= 93997;
840 SettingsFlagsAndValue
flags_and_value3(flags3
, value3
);
841 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_docs
, id3
, flags3
, value3
));
843 const SettingsMap
& settings_map3_ret
=
844 impl_
.GetSpdySettings(spdy_server_docs
);
845 ASSERT_EQ(1U, settings_map3_ret
.size());
846 SettingsMap::const_iterator it3_ret
= settings_map3_ret
.find(id3
);
847 EXPECT_TRUE(it3_ret
!= settings_map3_ret
.end());
848 SettingsFlagsAndValue flags_and_value3_ret
= it3_ret
->second
;
849 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value3_ret
.first
);
850 EXPECT_EQ(value3
, flags_and_value3_ret
.second
);
852 // Check data for www.google.com:443 (id1).
853 const SettingsMap
& settings_map4_ret
=
854 impl_
.GetSpdySettings(spdy_server_google
);
855 ASSERT_EQ(1U, settings_map4_ret
.size());
856 SettingsMap::const_iterator it4_ret
= settings_map4_ret
.find(id1
);
857 EXPECT_TRUE(it4_ret
!= settings_map4_ret
.end());
858 SettingsFlagsAndValue flags_and_value4_ret
= it4_ret
->second
;
859 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value4_ret
.first
);
860 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
862 // Clear www.google.com:443 as persisting.
863 impl_
.ClearSpdySettings(spdy_server_google
);
865 const SettingsMap
& settings_map5_ret
=
866 impl_
.GetSpdySettings(spdy_server_google
);
867 ASSERT_EQ(0U, settings_map5_ret
.size());
869 // Clear all settings.
870 ASSERT_GT(impl_
.spdy_settings_map().size(), 0U);
871 impl_
.ClearAllSpdySettings();
872 ASSERT_EQ(0U, impl_
.spdy_settings_map().size());
875 TEST_F(SpdySettingsServerPropertiesTest
, Clear
) {
876 // Add www.google.com:443 as persisting.
877 HostPortPair
spdy_server_google("www.google.com", 443);
878 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
879 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
880 const uint32 value1
= 31337;
881 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_google
, id1
, flags1
, value1
));
883 const SettingsMap
& settings_map1_ret
=
884 impl_
.GetSpdySettings(spdy_server_google
);
885 ASSERT_EQ(1U, settings_map1_ret
.size());
886 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
887 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
888 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
889 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
890 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
892 // Add docs.google.com:443 as persisting
893 HostPortPair
spdy_server_docs("docs.google.com", 443);
894 const SpdySettingsIds id3
= SETTINGS_ROUND_TRIP_TIME
;
895 const SpdySettingsFlags flags3
= SETTINGS_FLAG_PLEASE_PERSIST
;
896 const uint32 value3
= 93997;
897 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_docs
, id3
, flags3
, value3
));
899 const SettingsMap
& settings_map3_ret
=
900 impl_
.GetSpdySettings(spdy_server_docs
);
901 ASSERT_EQ(1U, settings_map3_ret
.size());
902 SettingsMap::const_iterator it3_ret
= settings_map3_ret
.find(id3
);
903 EXPECT_TRUE(it3_ret
!= settings_map3_ret
.end());
904 SettingsFlagsAndValue flags_and_value3_ret
= it3_ret
->second
;
905 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value3_ret
.first
);
906 EXPECT_EQ(value3
, flags_and_value3_ret
.second
);
909 EXPECT_EQ(0U, impl_
.GetSpdySettings(spdy_server_google
).size());
910 EXPECT_EQ(0U, impl_
.GetSpdySettings(spdy_server_docs
).size());
913 TEST_F(SpdySettingsServerPropertiesTest
, MRUOfGetSpdySettings
) {
914 // Add www.google.com:443 as persisting.
915 HostPortPair
spdy_server_google("www.google.com", 443);
916 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
917 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
918 const uint32 value1
= 31337;
919 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_google
, id1
, flags1
, value1
));
921 // Add docs.google.com:443 as persisting
922 HostPortPair
spdy_server_docs("docs.google.com", 443);
923 const SpdySettingsIds id2
= SETTINGS_ROUND_TRIP_TIME
;
924 const SpdySettingsFlags flags2
= SETTINGS_FLAG_PLEASE_PERSIST
;
925 const uint32 value2
= 93997;
926 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_docs
, id2
, flags2
, value2
));
928 // Verify the first element is docs.google.com:443.
929 const SpdySettingsMap
& map
= impl_
.spdy_settings_map();
930 SpdySettingsMap::const_iterator it
= map
.begin();
931 EXPECT_TRUE(it
->first
.Equals(spdy_server_docs
));
932 const SettingsMap
& settings_map2_ret
= it
->second
;
933 ASSERT_EQ(1U, settings_map2_ret
.size());
934 SettingsMap::const_iterator it2_ret
= settings_map2_ret
.find(id2
);
935 EXPECT_TRUE(it2_ret
!= settings_map2_ret
.end());
936 SettingsFlagsAndValue flags_and_value2_ret
= it2_ret
->second
;
937 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value2_ret
.first
);
938 EXPECT_EQ(value2
, flags_and_value2_ret
.second
);
940 // GetSpdySettings should reorder the SpdySettingsMap.
941 const SettingsMap
& settings_map1_ret
=
942 impl_
.GetSpdySettings(spdy_server_google
);
943 ASSERT_EQ(1U, settings_map1_ret
.size());
944 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
945 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
946 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
947 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
948 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
950 // Check the first entry is spdy_server_google by accessing it via iterator.
952 EXPECT_TRUE(it
->first
.Equals(spdy_server_google
));
953 const SettingsMap
& settings_map1_it_ret
= it
->second
;
954 ASSERT_EQ(1U, settings_map1_it_ret
.size());
955 it1_ret
= settings_map1_it_ret
.find(id1
);
956 EXPECT_TRUE(it1_ret
!= settings_map1_it_ret
.end());
957 flags_and_value1_ret
= it1_ret
->second
;
958 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
959 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
962 typedef HttpServerPropertiesImplTest SupportsQuicServerPropertiesTest
;
964 TEST_F(SupportsQuicServerPropertiesTest
, Initialize
) {
965 HostPortPair
quic_server_google("www.google.com", 443);
967 // Check by initializing empty address.
968 IPAddressNumber initial_address
;
969 impl_
.InitializeSupportsQuic(&initial_address
);
971 IPAddressNumber address
;
972 EXPECT_FALSE(impl_
.GetSupportsQuic(&address
));
973 EXPECT_TRUE(address
.empty());
975 // Check by initializing with a valid address.
976 CHECK(ParseIPLiteralToNumber("127.0.0.1", &initial_address
));
977 impl_
.InitializeSupportsQuic(&initial_address
);
979 EXPECT_TRUE(impl_
.GetSupportsQuic(&address
));
980 EXPECT_EQ(initial_address
, address
);
983 TEST_F(SupportsQuicServerPropertiesTest
, SetSupportsQuic
) {
984 IPAddressNumber address
;
985 EXPECT_FALSE(impl_
.GetSupportsQuic(&address
));
986 EXPECT_TRUE(address
.empty());
988 IPAddressNumber actual_address
;
989 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address
));
990 impl_
.SetSupportsQuic(true, actual_address
);
992 EXPECT_TRUE(impl_
.GetSupportsQuic(&address
));
993 EXPECT_EQ(actual_address
, address
);
997 EXPECT_FALSE(impl_
.GetSupportsQuic(&address
));
1000 typedef HttpServerPropertiesImplTest ServerNetworkStatsServerPropertiesTest
;
1002 TEST_F(ServerNetworkStatsServerPropertiesTest
, Initialize
) {
1003 HostPortPair
google_server("www.google.com", 443);
1005 // Check by initializing empty ServerNetworkStats.
1006 ServerNetworkStatsMap
server_network_stats_map(
1007 ServerNetworkStatsMap::NO_AUTO_EVICT
);
1008 impl_
.InitializeServerNetworkStats(&server_network_stats_map
);
1009 const ServerNetworkStats
* stats
= impl_
.GetServerNetworkStats(google_server
);
1010 EXPECT_EQ(NULL
, stats
);
1012 // Check by initializing with www.google.com:443.
1013 ServerNetworkStats stats1
;
1014 stats1
.srtt
= base::TimeDelta::FromMicroseconds(10);
1015 stats1
.bandwidth_estimate
= QuicBandwidth::FromBitsPerSecond(100);
1016 server_network_stats_map
.Put(google_server
, stats1
);
1017 impl_
.InitializeServerNetworkStats(&server_network_stats_map
);
1019 const ServerNetworkStats
* stats2
= impl_
.GetServerNetworkStats(google_server
);
1020 EXPECT_EQ(10, stats2
->srtt
.ToInternalValue());
1021 EXPECT_EQ(100, stats2
->bandwidth_estimate
.ToBitsPerSecond());
1024 TEST_F(ServerNetworkStatsServerPropertiesTest
, SetServerNetworkStats
) {
1025 HostPortPair
foo_server("foo", 80);
1026 const ServerNetworkStats
* stats
= impl_
.GetServerNetworkStats(foo_server
);
1027 EXPECT_EQ(NULL
, stats
);
1029 ServerNetworkStats stats1
;
1030 stats1
.srtt
= base::TimeDelta::FromMicroseconds(10);
1031 stats1
.bandwidth_estimate
= QuicBandwidth::FromBitsPerSecond(100);
1032 impl_
.SetServerNetworkStats(foo_server
, stats1
);
1034 const ServerNetworkStats
* stats2
= impl_
.GetServerNetworkStats(foo_server
);
1035 EXPECT_EQ(10, stats2
->srtt
.ToInternalValue());
1036 EXPECT_EQ(100, stats2
->bandwidth_estimate
.ToBitsPerSecond());
1039 const ServerNetworkStats
* stats3
= impl_
.GetServerNetworkStats(foo_server
);
1040 EXPECT_EQ(NULL
, stats3
);