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 "testing/gtest/include/gtest/gtest.h"
24 const int kMaxSupportsSpdyServerHosts
= 500;
26 class HttpServerPropertiesImplPeer
{
28 static void AddBrokenAlternativeServiceWithExpirationTime(
29 HttpServerPropertiesImpl
& impl
,
30 AlternativeService alternative_service
,
31 base::TimeTicks when
) {
32 impl
.broken_alternative_services_
.insert(
33 std::make_pair(alternative_service
, when
));
34 ++impl
.recently_broken_alternative_services_
[alternative_service
];
37 static void ExpireBrokenAlternateProtocolMappings(
38 HttpServerPropertiesImpl
& impl
) {
39 impl
.ExpireBrokenAlternateProtocolMappings();
45 class HttpServerPropertiesImplTest
: public testing::Test
{
47 bool HasAlternativeService(const HostPortPair
& origin
) {
48 const AlternativeService alternative_service
=
49 impl_
.GetAlternativeService(origin
);
50 return alternative_service
.protocol
!= UNINITIALIZED_ALTERNATE_PROTOCOL
;
53 HttpServerPropertiesImpl impl_
;
56 typedef HttpServerPropertiesImplTest SpdyServerPropertiesTest
;
58 TEST_F(SpdyServerPropertiesTest
, Initialize
) {
59 HostPortPair
spdy_server_google("www.google.com", 443);
60 std::string spdy_server_g
= spdy_server_google
.ToString();
62 HostPortPair
spdy_server_docs("docs.google.com", 443);
63 std::string spdy_server_d
= spdy_server_docs
.ToString();
65 // Check by initializing NULL spdy servers.
66 impl_
.InitializeSpdyServers(NULL
, true);
67 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_google
));
69 // Check by initializing empty spdy servers.
70 std::vector
<std::string
> spdy_servers
;
71 impl_
.InitializeSpdyServers(&spdy_servers
, true);
72 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_google
));
74 // Check by initializing with www.google.com:443 spdy server.
75 std::vector
<std::string
> spdy_servers1
;
76 spdy_servers1
.push_back(spdy_server_g
);
77 impl_
.InitializeSpdyServers(&spdy_servers1
, true);
78 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
80 // Check by initializing with www.google.com:443 and docs.google.com:443 spdy
82 std::vector
<std::string
> spdy_servers2
;
83 spdy_servers2
.push_back(spdy_server_g
);
84 spdy_servers2
.push_back(spdy_server_d
);
85 impl_
.InitializeSpdyServers(&spdy_servers2
, true);
87 // Verify spdy_server_g and spdy_server_d are in the list in the same order.
88 base::ListValue spdy_server_list
;
89 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
90 EXPECT_EQ(2U, spdy_server_list
.GetSize());
91 std::string string_value_g
;
92 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
93 ASSERT_EQ(spdy_server_g
, string_value_g
);
94 std::string string_value_d
;
95 ASSERT_TRUE(spdy_server_list
.GetString(1, &string_value_d
));
96 ASSERT_EQ(spdy_server_d
, string_value_d
);
97 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
98 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_docs
));
101 TEST_F(SpdyServerPropertiesTest
, SupportsRequestPriorityTest
) {
102 HostPortPair
spdy_server_empty(std::string(), 443);
103 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_empty
));
105 // Add www.google.com:443 as supporting SPDY.
106 HostPortPair
spdy_server_google("www.google.com", 443);
107 impl_
.SetSupportsSpdy(spdy_server_google
, true);
108 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
110 // Add mail.google.com:443 as not supporting SPDY.
111 HostPortPair
spdy_server_mail("mail.google.com", 443);
112 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_mail
));
114 // Add docs.google.com:443 as supporting SPDY.
115 HostPortPair
spdy_server_docs("docs.google.com", 443);
116 impl_
.SetSupportsSpdy(spdy_server_docs
, true);
117 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_docs
));
119 // Add www.youtube.com:443 as supporting QUIC.
120 HostPortPair
quic_server_youtube("www.youtube.com", 443);
121 const AlternativeService
alternative_service(QUIC
, "www.youtube.com", 443);
122 impl_
.SetAlternativeService(quic_server_youtube
, alternative_service
, 1.0);
123 EXPECT_TRUE(impl_
.SupportsRequestPriority(quic_server_youtube
));
125 // Verify all the entries are the same after additions.
126 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
127 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_mail
));
128 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_docs
));
129 EXPECT_TRUE(impl_
.SupportsRequestPriority(quic_server_youtube
));
132 TEST_F(SpdyServerPropertiesTest
, Clear
) {
133 // Add www.google.com:443 and mail.google.com:443 as supporting SPDY.
134 HostPortPair
spdy_server_google("www.google.com", 443);
135 impl_
.SetSupportsSpdy(spdy_server_google
, true);
136 HostPortPair
spdy_server_mail("mail.google.com", 443);
137 impl_
.SetSupportsSpdy(spdy_server_mail
, true);
139 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
140 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_mail
));
143 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_google
));
144 EXPECT_FALSE(impl_
.SupportsRequestPriority(spdy_server_mail
));
147 TEST_F(SpdyServerPropertiesTest
, GetSpdyServerList
) {
148 base::ListValue spdy_server_list
;
150 // Check there are no spdy_servers.
151 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
152 EXPECT_EQ(0U, spdy_server_list
.GetSize());
154 // Check empty server is not added.
155 HostPortPair
spdy_server_empty(std::string(), 443);
156 impl_
.SetSupportsSpdy(spdy_server_empty
, true);
157 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
158 EXPECT_EQ(0U, spdy_server_list
.GetSize());
160 std::string string_value_g
;
161 std::string string_value_m
;
162 HostPortPair
spdy_server_google("www.google.com", 443);
163 std::string spdy_server_g
= spdy_server_google
.ToString();
164 HostPortPair
spdy_server_mail("mail.google.com", 443);
165 std::string spdy_server_m
= spdy_server_mail
.ToString();
167 // Add www.google.com:443 as not supporting SPDY.
168 impl_
.SetSupportsSpdy(spdy_server_google
, false);
169 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
170 EXPECT_EQ(0U, spdy_server_list
.GetSize());
172 // Add www.google.com:443 as supporting SPDY.
173 impl_
.SetSupportsSpdy(spdy_server_google
, true);
174 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
175 ASSERT_EQ(1U, spdy_server_list
.GetSize());
176 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
177 ASSERT_EQ(spdy_server_g
, string_value_g
);
179 // Add mail.google.com:443 as not supporting SPDY.
180 impl_
.SetSupportsSpdy(spdy_server_mail
, false);
181 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
182 ASSERT_EQ(1U, spdy_server_list
.GetSize());
183 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
184 ASSERT_EQ(spdy_server_g
, string_value_g
);
186 // Add mail.google.com:443 as supporting SPDY.
187 impl_
.SetSupportsSpdy(spdy_server_mail
, true);
188 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
189 ASSERT_EQ(2U, spdy_server_list
.GetSize());
191 // Verify www.google.com:443 and mail.google.com:443 are in the list.
192 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_m
));
193 ASSERT_EQ(spdy_server_m
, string_value_m
);
194 ASSERT_TRUE(spdy_server_list
.GetString(1, &string_value_g
));
195 ASSERT_EQ(spdy_server_g
, string_value_g
);
197 // Request for only one server and verify that we get only one server.
198 impl_
.GetSpdyServerList(&spdy_server_list
, 1);
199 ASSERT_EQ(1U, spdy_server_list
.GetSize());
200 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_m
));
201 ASSERT_EQ(spdy_server_m
, string_value_m
);
204 TEST_F(SpdyServerPropertiesTest
, MRUOfGetSpdyServerList
) {
205 base::ListValue spdy_server_list
;
207 std::string string_value_g
;
208 std::string string_value_m
;
209 HostPortPair
spdy_server_google("www.google.com", 443);
210 std::string spdy_server_g
= spdy_server_google
.ToString();
211 HostPortPair
spdy_server_mail("mail.google.com", 443);
212 std::string spdy_server_m
= spdy_server_mail
.ToString();
214 // Add www.google.com:443 as supporting SPDY.
215 impl_
.SetSupportsSpdy(spdy_server_google
, true);
216 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
217 ASSERT_EQ(1U, spdy_server_list
.GetSize());
218 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
219 ASSERT_EQ(spdy_server_g
, string_value_g
);
221 // Add mail.google.com:443 as supporting SPDY. Verify mail.google.com:443 and
222 // www.google.com:443 are in the list.
223 impl_
.SetSupportsSpdy(spdy_server_mail
, true);
224 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
225 ASSERT_EQ(2U, spdy_server_list
.GetSize());
226 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_m
));
227 ASSERT_EQ(spdy_server_m
, string_value_m
);
228 ASSERT_TRUE(spdy_server_list
.GetString(1, &string_value_g
));
229 ASSERT_EQ(spdy_server_g
, string_value_g
);
231 // Get www.google.com:443 should reorder SpdyServerHostPortMap. Verify that it
232 // is www.google.com:443 is the MRU server.
233 EXPECT_TRUE(impl_
.SupportsRequestPriority(spdy_server_google
));
234 impl_
.GetSpdyServerList(&spdy_server_list
, kMaxSupportsSpdyServerHosts
);
235 ASSERT_EQ(2U, spdy_server_list
.GetSize());
236 ASSERT_TRUE(spdy_server_list
.GetString(0, &string_value_g
));
237 ASSERT_EQ(spdy_server_g
, string_value_g
);
238 ASSERT_TRUE(spdy_server_list
.GetString(1, &string_value_m
));
239 ASSERT_EQ(spdy_server_m
, string_value_m
);
242 typedef HttpServerPropertiesImplTest AlternateProtocolServerPropertiesTest
;
244 TEST_F(AlternateProtocolServerPropertiesTest
, Basic
) {
245 HostPortPair
test_host_port_pair("foo", 80);
246 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
248 AlternativeService
alternative_service(NPN_SPDY_4
, "foo", 443);
249 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service
, 1.0);
250 ASSERT_TRUE(HasAlternativeService(test_host_port_pair
));
251 alternative_service
= impl_
.GetAlternativeService(test_host_port_pair
);
252 EXPECT_EQ(443, alternative_service
.port
);
253 EXPECT_EQ(NPN_SPDY_4
, alternative_service
.protocol
);
256 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
259 TEST_F(AlternateProtocolServerPropertiesTest
, DefaultProbabilityExcluded
) {
260 HostPortPair
test_host_port_pair("foo", 80);
261 const AlternativeService
alternative_service(NPN_SPDY_4
, "foo", 443);
262 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service
, 0.99);
264 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
267 TEST_F(AlternateProtocolServerPropertiesTest
, Probability
) {
268 impl_
.SetAlternativeServiceProbabilityThreshold(0.25);
270 HostPortPair
test_host_port_pair("foo", 80);
271 const AlternativeService
alternative_service(NPN_SPDY_4
, "foo", 443);
272 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service
, 0.5);
273 EXPECT_TRUE(HasAlternativeService(test_host_port_pair
));
275 AlternativeServiceMap::const_iterator it
=
276 impl_
.alternative_service_map().Peek(test_host_port_pair
);
277 ASSERT_TRUE(it
!= impl_
.alternative_service_map().end());
278 EXPECT_EQ(443, it
->second
.alternative_service
.port
);
279 EXPECT_EQ(NPN_SPDY_4
, it
->second
.alternative_service
.protocol
);
280 EXPECT_EQ(0.5, it
->second
.probability
);
283 TEST_F(AlternateProtocolServerPropertiesTest
, ProbabilityExcluded
) {
284 impl_
.SetAlternativeServiceProbabilityThreshold(0.75);
286 HostPortPair
test_host_port_pair("foo", 80);
287 const AlternativeService
alternative_service(NPN_SPDY_4
, "foo", 443);
288 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service
, 0.5);
289 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
292 TEST_F(AlternateProtocolServerPropertiesTest
, Initialize
) {
293 HostPortPair
test_host_port_pair1("foo1", 80);
294 const AlternativeService
alternative_service1(NPN_SPDY_4
, "foo1", 443);
295 impl_
.SetAlternativeService(test_host_port_pair1
, alternative_service1
, 1.0);
296 impl_
.MarkAlternativeServiceBroken(alternative_service1
);
298 HostPortPair
test_host_port_pair2("foo2", 80);
299 const AlternativeService
alternative_service2(NPN_SPDY_4
, "foo2", 443);
300 impl_
.SetAlternativeService(test_host_port_pair2
, alternative_service2
, 1.0);
302 AlternativeServiceMap
alternative_service_map(
303 AlternativeServiceMap::NO_AUTO_EVICT
);
304 AlternativeServiceInfo
alternative_service_info(NPN_SPDY_4
, "bar", 123, 1.0);
305 alternative_service_map
.Put(test_host_port_pair2
, alternative_service_info
);
306 HostPortPair
test_host_port_pair3("foo3", 80);
307 alternative_service_info
.alternative_service
.port
= 1234;
308 alternative_service_map
.Put(test_host_port_pair3
, alternative_service_info
);
309 impl_
.InitializeAlternativeServiceServers(&alternative_service_map
);
311 // Verify test_host_port_pair3 is the MRU server.
312 const AlternativeServiceMap
& map
= impl_
.alternative_service_map();
313 AlternativeServiceMap::const_iterator it
= map
.begin();
314 ASSERT_TRUE(it
!= map
.end());
315 EXPECT_TRUE(it
->first
.Equals(test_host_port_pair3
));
316 EXPECT_EQ(NPN_SPDY_4
, it
->second
.alternative_service
.protocol
);
317 EXPECT_EQ(1234, it
->second
.alternative_service
.port
);
319 ASSERT_TRUE(HasAlternativeService(test_host_port_pair1
));
320 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service1
));
321 const AlternativeService alternative_service
=
322 impl_
.GetAlternativeService(test_host_port_pair2
);
323 EXPECT_EQ(NPN_SPDY_4
, alternative_service
.protocol
);
324 EXPECT_EQ(123, alternative_service
.port
);
327 TEST_F(AlternateProtocolServerPropertiesTest
, MRUOfGetAlternateProtocol
) {
328 HostPortPair
test_host_port_pair1("foo1", 80);
329 const AlternativeService
alternative_service1(NPN_SPDY_4
, "foo1", 443);
330 impl_
.SetAlternativeService(test_host_port_pair1
, alternative_service1
, 1.0);
331 HostPortPair
test_host_port_pair2("foo2", 80);
332 const AlternativeService
alternative_service2(NPN_SPDY_4
, "foo2", 1234);
333 impl_
.SetAlternativeService(test_host_port_pair2
, alternative_service2
, 1.0);
335 const AlternativeServiceMap
& map
= impl_
.alternative_service_map();
336 AlternativeServiceMap::const_iterator it
= map
.begin();
337 EXPECT_TRUE(it
->first
.Equals(test_host_port_pair2
));
338 EXPECT_EQ(NPN_SPDY_4
, it
->second
.alternative_service
.protocol
);
339 EXPECT_EQ(1234, it
->second
.alternative_service
.port
);
341 // GetAlternativeService should reorder the AlternateProtocol map.
342 const AlternativeService alternative_service
=
343 impl_
.GetAlternativeService(test_host_port_pair1
);
344 EXPECT_EQ(443, alternative_service
.port
);
345 EXPECT_EQ(NPN_SPDY_4
, alternative_service
.protocol
);
347 EXPECT_TRUE(it
->first
.Equals(test_host_port_pair1
));
348 EXPECT_EQ(NPN_SPDY_4
, it
->second
.alternative_service
.protocol
);
349 EXPECT_EQ(443, it
->second
.alternative_service
.port
);
352 TEST_F(AlternateProtocolServerPropertiesTest
, SetBroken
) {
353 HostPortPair
test_host_port_pair("foo", 80);
354 const AlternativeService
alternative_service1(NPN_SPDY_4
, "foo", 443);
355 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service1
, 1.0);
356 impl_
.MarkAlternativeServiceBroken(alternative_service1
);
357 ASSERT_TRUE(HasAlternativeService(test_host_port_pair
));
358 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service1
));
360 const AlternativeService
alternative_service2(NPN_SPDY_4
, "foo", 1234);
361 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service2
, 1.0);
362 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service1
));
363 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service2
));
364 EXPECT_EQ(1234, impl_
.GetAlternativeService(test_host_port_pair
).port
);
367 TEST_F(AlternateProtocolServerPropertiesTest
, ClearBroken
) {
368 HostPortPair
test_host_port_pair("foo", 80);
369 const AlternativeService
alternative_service(NPN_SPDY_4
, "foo", 443);
370 impl_
.SetAlternativeService(test_host_port_pair
, alternative_service
, 1.0);
371 impl_
.MarkAlternativeServiceBroken(alternative_service
);
372 ASSERT_TRUE(HasAlternativeService(test_host_port_pair
));
373 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service
));
374 impl_
.ClearAlternativeService(test_host_port_pair
);
375 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
378 TEST_F(AlternateProtocolServerPropertiesTest
, MarkRecentlyBroken
) {
379 HostPortPair
host_port_pair("foo", 80);
380 const AlternativeService
alternative_service(NPN_SPDY_4
, "foo", 443);
381 impl_
.SetAlternativeService(host_port_pair
, alternative_service
, 1.0);
383 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
384 EXPECT_FALSE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
386 impl_
.MarkAlternativeServiceRecentlyBroken(alternative_service
);
387 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
388 EXPECT_TRUE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
390 impl_
.ConfirmAlternativeService(alternative_service
);
391 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
392 EXPECT_FALSE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
395 TEST_F(AlternateProtocolServerPropertiesTest
, Canonical
) {
396 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
397 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
399 HostPortPair
canonical_port_pair("bar.c.youtube.com", 80);
400 EXPECT_FALSE(HasAlternativeService(canonical_port_pair
));
402 AlternativeService
canonical_altsvc(QUIC
, "bar.c.youtube.com", 1234);
403 impl_
.SetAlternativeService(canonical_port_pair
, canonical_altsvc
, 1.0);
404 // Verify the forced protocol.
405 ASSERT_TRUE(HasAlternativeService(test_host_port_pair
));
406 const AlternativeService alternative_service
=
407 impl_
.GetAlternativeService(test_host_port_pair
);
408 EXPECT_EQ(canonical_altsvc
.port
, alternative_service
.port
);
409 EXPECT_EQ(canonical_altsvc
.protocol
, alternative_service
.protocol
);
411 // Verify the canonical suffix.
412 EXPECT_EQ(".c.youtube.com",
413 impl_
.GetCanonicalSuffix(test_host_port_pair
.host()));
414 EXPECT_EQ(".c.youtube.com",
415 impl_
.GetCanonicalSuffix(canonical_port_pair
.host()));
418 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalDefaultHost
) {
419 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
420 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
422 HostPortPair
canonical_port_pair("bar.c.youtube.com", 80);
423 EXPECT_FALSE(HasAlternativeService(canonical_port_pair
));
425 AlternativeService
canonical_altsvc(QUIC
, "", 1234);
426 impl_
.SetAlternativeService(canonical_port_pair
, canonical_altsvc
, 1.0);
427 ASSERT_TRUE(HasAlternativeService(test_host_port_pair
));
428 const AlternativeService alternative_service
=
429 impl_
.GetAlternativeService(test_host_port_pair
);
430 EXPECT_EQ(canonical_altsvc
.protocol
, alternative_service
.protocol
);
431 EXPECT_EQ(test_host_port_pair
.host(), alternative_service
.host
);
432 EXPECT_EQ(canonical_altsvc
.port
, alternative_service
.port
);
435 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalBelowThreshold
) {
436 impl_
.SetAlternativeServiceProbabilityThreshold(0.02);
438 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
439 HostPortPair
canonical_port_pair("bar.c.youtube.com", 80);
440 AlternativeService
canonical_altsvc(QUIC
, "bar.c.youtube.com", 1234);
442 impl_
.SetAlternativeService(canonical_port_pair
, canonical_altsvc
, 0.01);
443 EXPECT_FALSE(HasAlternativeService(canonical_port_pair
));
444 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
447 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalAboveThreshold
) {
448 impl_
.SetAlternativeServiceProbabilityThreshold(0.02);
450 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
451 HostPortPair
canonical_port_pair("bar.c.youtube.com", 80);
452 AlternativeService
canonical_altsvc(QUIC
, "bar.c.youtube.com", 1234);
454 impl_
.SetAlternativeService(canonical_port_pair
, canonical_altsvc
, 0.03);
455 EXPECT_TRUE(HasAlternativeService(canonical_port_pair
));
456 EXPECT_TRUE(HasAlternativeService(test_host_port_pair
));
459 TEST_F(AlternateProtocolServerPropertiesTest
, ClearCanonical
) {
460 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
461 HostPortPair
canonical_port_pair("bar.c.youtube.com", 80);
462 AlternativeService
canonical_altsvc(QUIC
, "bar.c.youtube.com", 1234);
464 impl_
.SetAlternativeService(canonical_port_pair
, canonical_altsvc
, 1.0);
465 impl_
.ClearAlternativeService(canonical_port_pair
);
466 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
469 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalBroken
) {
470 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
471 HostPortPair
canonical_port_pair("bar.c.youtube.com", 80);
472 AlternativeService
canonical_altsvc(QUIC
, "bar.c.youtube.com", 1234);
474 impl_
.SetAlternativeService(canonical_port_pair
, canonical_altsvc
, 1.0);
475 impl_
.MarkAlternativeServiceBroken(canonical_altsvc
);
476 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
479 // Adding an alternative service for a new host overrides canonical host.
480 TEST_F(AlternateProtocolServerPropertiesTest
, CanonicalOverride
) {
481 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
482 HostPortPair
bar_host_port_pair("bar.c.youtube.com", 80);
483 AlternativeService
bar_alternative_service(QUIC
, "bar.c.youtube.com", 1234);
484 impl_
.SetAlternativeService(bar_host_port_pair
, bar_alternative_service
, 1.0);
485 AlternativeService altsvc
= impl_
.GetAlternativeService(test_host_port_pair
);
486 EXPECT_EQ(1234, altsvc
.port
);
488 HostPortPair
qux_host_port_pair("qux.c.youtube.com", 80);
489 AlternativeService
qux_alternative_service(QUIC
, "qux.c.youtube.com", 443);
490 impl_
.SetAlternativeService(qux_host_port_pair
, qux_alternative_service
, 1.0);
491 altsvc
= impl_
.GetAlternativeService(test_host_port_pair
);
492 EXPECT_EQ(443, altsvc
.port
);
495 TEST_F(AlternateProtocolServerPropertiesTest
, ClearWithCanonical
) {
496 HostPortPair
test_host_port_pair("foo.c.youtube.com", 80);
497 HostPortPair
canonical_port_pair("bar.c.youtube.com", 80);
498 AlternativeService
canonical_altsvc(QUIC
, "bar.c.youtube.com", 1234);
500 impl_
.SetAlternativeService(canonical_port_pair
, canonical_altsvc
, 1.0);
502 EXPECT_FALSE(HasAlternativeService(test_host_port_pair
));
505 TEST_F(AlternateProtocolServerPropertiesTest
,
506 ExpireBrokenAlternateProtocolMappings
) {
507 HostPortPair
host_port_pair("foo", 443);
508 AlternativeService
alternative_service(QUIC
, "foo", 443);
509 impl_
.SetAlternativeService(host_port_pair
, alternative_service
, 1.0);
510 EXPECT_TRUE(HasAlternativeService(host_port_pair
));
511 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
512 EXPECT_FALSE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
514 base::TimeTicks past
=
515 base::TimeTicks::Now() - base::TimeDelta::FromSeconds(42);
516 HttpServerPropertiesImplPeer::AddBrokenAlternativeServiceWithExpirationTime(
517 impl_
, alternative_service
, past
);
518 EXPECT_TRUE(impl_
.IsAlternativeServiceBroken(alternative_service
));
519 EXPECT_TRUE(impl_
.WasAlternativeServiceRecentlyBroken(alternative_service
));
521 HttpServerPropertiesImplPeer::ExpireBrokenAlternateProtocolMappings(impl_
);
522 EXPECT_FALSE(impl_
.IsAlternativeServiceBroken(alternative_service
));
523 // TODO(bnc): Test WasAlternativeServiceRecentlyBroken once it's changed to
524 // take AlternativeService as argument.
527 typedef HttpServerPropertiesImplTest SpdySettingsServerPropertiesTest
;
529 TEST_F(SpdySettingsServerPropertiesTest
, Initialize
) {
530 HostPortPair
spdy_server_google("www.google.com", 443);
532 // Check by initializing empty spdy settings.
533 SpdySettingsMap
spdy_settings_map(SpdySettingsMap::NO_AUTO_EVICT
);
534 impl_
.InitializeSpdySettingsServers(&spdy_settings_map
);
535 EXPECT_TRUE(impl_
.GetSpdySettings(spdy_server_google
).empty());
537 // Check by initializing with www.google.com:443 spdy server settings.
538 SettingsMap settings_map
;
539 const SpdySettingsIds id
= SETTINGS_UPLOAD_BANDWIDTH
;
540 const SpdySettingsFlags flags
= SETTINGS_FLAG_PERSISTED
;
541 const uint32 value
= 31337;
542 SettingsFlagsAndValue
flags_and_value(flags
, value
);
543 settings_map
[id
] = flags_and_value
;
544 spdy_settings_map
.Put(spdy_server_google
, settings_map
);
545 impl_
.InitializeSpdySettingsServers(&spdy_settings_map
);
547 const SettingsMap
& settings_map2
= impl_
.GetSpdySettings(spdy_server_google
);
548 ASSERT_EQ(1U, settings_map2
.size());
549 SettingsMap::const_iterator it
= settings_map2
.find(id
);
550 EXPECT_TRUE(it
!= settings_map2
.end());
551 SettingsFlagsAndValue flags_and_value2
= it
->second
;
552 EXPECT_EQ(flags
, flags_and_value2
.first
);
553 EXPECT_EQ(value
, flags_and_value2
.second
);
556 TEST_F(SpdySettingsServerPropertiesTest
, SetSpdySetting
) {
557 HostPortPair
spdy_server_empty(std::string(), 443);
558 const SettingsMap
& settings_map0
= impl_
.GetSpdySettings(spdy_server_empty
);
559 EXPECT_EQ(0U, settings_map0
.size()); // Returns kEmptySettingsMap.
561 // Add www.google.com:443 as persisting.
562 HostPortPair
spdy_server_google("www.google.com", 443);
563 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
564 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
565 const uint32 value1
= 31337;
566 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_google
, id1
, flags1
, value1
));
568 const SettingsMap
& settings_map1_ret
=
569 impl_
.GetSpdySettings(spdy_server_google
);
570 ASSERT_EQ(1U, settings_map1_ret
.size());
571 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
572 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
573 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
574 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
575 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
577 // Add mail.google.com:443 as not persisting.
578 HostPortPair
spdy_server_mail("mail.google.com", 443);
579 const SpdySettingsIds id2
= SETTINGS_DOWNLOAD_BANDWIDTH
;
580 const SpdySettingsFlags flags2
= SETTINGS_FLAG_NONE
;
581 const uint32 value2
= 62667;
582 EXPECT_FALSE(impl_
.SetSpdySetting(spdy_server_mail
, id2
, flags2
, value2
));
583 const SettingsMap
& settings_map2_ret
=
584 impl_
.GetSpdySettings(spdy_server_mail
);
585 EXPECT_EQ(0U, settings_map2_ret
.size()); // Returns kEmptySettingsMap.
587 // Add docs.google.com:443 as persisting
588 HostPortPair
spdy_server_docs("docs.google.com", 443);
589 const SpdySettingsIds id3
= SETTINGS_ROUND_TRIP_TIME
;
590 const SpdySettingsFlags flags3
= SETTINGS_FLAG_PLEASE_PERSIST
;
591 const uint32 value3
= 93997;
592 SettingsFlagsAndValue
flags_and_value3(flags3
, value3
);
593 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_docs
, id3
, flags3
, value3
));
595 const SettingsMap
& settings_map3_ret
=
596 impl_
.GetSpdySettings(spdy_server_docs
);
597 ASSERT_EQ(1U, settings_map3_ret
.size());
598 SettingsMap::const_iterator it3_ret
= settings_map3_ret
.find(id3
);
599 EXPECT_TRUE(it3_ret
!= settings_map3_ret
.end());
600 SettingsFlagsAndValue flags_and_value3_ret
= it3_ret
->second
;
601 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value3_ret
.first
);
602 EXPECT_EQ(value3
, flags_and_value3_ret
.second
);
604 // Check data for www.google.com:443 (id1).
605 const SettingsMap
& settings_map4_ret
=
606 impl_
.GetSpdySettings(spdy_server_google
);
607 ASSERT_EQ(1U, settings_map4_ret
.size());
608 SettingsMap::const_iterator it4_ret
= settings_map4_ret
.find(id1
);
609 EXPECT_TRUE(it4_ret
!= settings_map4_ret
.end());
610 SettingsFlagsAndValue flags_and_value4_ret
= it4_ret
->second
;
611 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value4_ret
.first
);
612 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
614 // Clear www.google.com:443 as persisting.
615 impl_
.ClearSpdySettings(spdy_server_google
);
617 const SettingsMap
& settings_map5_ret
=
618 impl_
.GetSpdySettings(spdy_server_google
);
619 ASSERT_EQ(0U, settings_map5_ret
.size());
621 // Clear all settings.
622 ASSERT_GT(impl_
.spdy_settings_map().size(), 0U);
623 impl_
.ClearAllSpdySettings();
624 ASSERT_EQ(0U, impl_
.spdy_settings_map().size());
627 TEST_F(SpdySettingsServerPropertiesTest
, Clear
) {
628 // Add www.google.com:443 as persisting.
629 HostPortPair
spdy_server_google("www.google.com", 443);
630 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
631 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
632 const uint32 value1
= 31337;
633 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_google
, id1
, flags1
, value1
));
635 const SettingsMap
& settings_map1_ret
=
636 impl_
.GetSpdySettings(spdy_server_google
);
637 ASSERT_EQ(1U, settings_map1_ret
.size());
638 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
639 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
640 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
641 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
642 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
644 // Add docs.google.com:443 as persisting
645 HostPortPair
spdy_server_docs("docs.google.com", 443);
646 const SpdySettingsIds id3
= SETTINGS_ROUND_TRIP_TIME
;
647 const SpdySettingsFlags flags3
= SETTINGS_FLAG_PLEASE_PERSIST
;
648 const uint32 value3
= 93997;
649 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_docs
, id3
, flags3
, value3
));
651 const SettingsMap
& settings_map3_ret
=
652 impl_
.GetSpdySettings(spdy_server_docs
);
653 ASSERT_EQ(1U, settings_map3_ret
.size());
654 SettingsMap::const_iterator it3_ret
= settings_map3_ret
.find(id3
);
655 EXPECT_TRUE(it3_ret
!= settings_map3_ret
.end());
656 SettingsFlagsAndValue flags_and_value3_ret
= it3_ret
->second
;
657 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value3_ret
.first
);
658 EXPECT_EQ(value3
, flags_and_value3_ret
.second
);
661 EXPECT_EQ(0U, impl_
.GetSpdySettings(spdy_server_google
).size());
662 EXPECT_EQ(0U, impl_
.GetSpdySettings(spdy_server_docs
).size());
665 TEST_F(SpdySettingsServerPropertiesTest
, MRUOfGetSpdySettings
) {
666 // Add www.google.com:443 as persisting.
667 HostPortPair
spdy_server_google("www.google.com", 443);
668 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
669 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
670 const uint32 value1
= 31337;
671 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_google
, id1
, flags1
, value1
));
673 // Add docs.google.com:443 as persisting
674 HostPortPair
spdy_server_docs("docs.google.com", 443);
675 const SpdySettingsIds id2
= SETTINGS_ROUND_TRIP_TIME
;
676 const SpdySettingsFlags flags2
= SETTINGS_FLAG_PLEASE_PERSIST
;
677 const uint32 value2
= 93997;
678 EXPECT_TRUE(impl_
.SetSpdySetting(spdy_server_docs
, id2
, flags2
, value2
));
680 // Verify the first element is docs.google.com:443.
681 const SpdySettingsMap
& map
= impl_
.spdy_settings_map();
682 SpdySettingsMap::const_iterator it
= map
.begin();
683 EXPECT_TRUE(it
->first
.Equals(spdy_server_docs
));
684 const SettingsMap
& settings_map2_ret
= it
->second
;
685 ASSERT_EQ(1U, settings_map2_ret
.size());
686 SettingsMap::const_iterator it2_ret
= settings_map2_ret
.find(id2
);
687 EXPECT_TRUE(it2_ret
!= settings_map2_ret
.end());
688 SettingsFlagsAndValue flags_and_value2_ret
= it2_ret
->second
;
689 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value2_ret
.first
);
690 EXPECT_EQ(value2
, flags_and_value2_ret
.second
);
692 // GetSpdySettings should reorder the SpdySettingsMap.
693 const SettingsMap
& settings_map1_ret
=
694 impl_
.GetSpdySettings(spdy_server_google
);
695 ASSERT_EQ(1U, settings_map1_ret
.size());
696 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
697 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
698 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
699 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
700 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
702 // Check the first entry is spdy_server_google by accessing it via iterator.
704 EXPECT_TRUE(it
->first
.Equals(spdy_server_google
));
705 const SettingsMap
& settings_map1_it_ret
= it
->second
;
706 ASSERT_EQ(1U, settings_map1_it_ret
.size());
707 it1_ret
= settings_map1_it_ret
.find(id1
);
708 EXPECT_TRUE(it1_ret
!= settings_map1_it_ret
.end());
709 flags_and_value1_ret
= it1_ret
->second
;
710 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
711 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
714 typedef HttpServerPropertiesImplTest SupportsQuicServerPropertiesTest
;
716 TEST_F(SupportsQuicServerPropertiesTest
, Initialize
) {
717 HostPortPair
quic_server_google("www.google.com", 443);
719 // Check by initializing empty address.
720 IPAddressNumber initial_address
;
721 impl_
.InitializeSupportsQuic(&initial_address
);
723 IPAddressNumber address
;
724 EXPECT_FALSE(impl_
.GetSupportsQuic(&address
));
725 EXPECT_TRUE(address
.empty());
727 // Check by initializing with a valid address.
728 CHECK(ParseIPLiteralToNumber("127.0.0.1", &initial_address
));
729 impl_
.InitializeSupportsQuic(&initial_address
);
731 EXPECT_TRUE(impl_
.GetSupportsQuic(&address
));
732 EXPECT_EQ(initial_address
, address
);
735 TEST_F(SupportsQuicServerPropertiesTest
, SetSupportsQuic
) {
736 IPAddressNumber address
;
737 EXPECT_FALSE(impl_
.GetSupportsQuic(&address
));
738 EXPECT_TRUE(address
.empty());
740 IPAddressNumber actual_address
;
741 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address
));
742 impl_
.SetSupportsQuic(true, actual_address
);
744 EXPECT_TRUE(impl_
.GetSupportsQuic(&address
));
745 EXPECT_EQ(actual_address
, address
);
749 EXPECT_FALSE(impl_
.GetSupportsQuic(&address
));
752 typedef HttpServerPropertiesImplTest ServerNetworkStatsServerPropertiesTest
;
754 TEST_F(ServerNetworkStatsServerPropertiesTest
, Initialize
) {
755 HostPortPair
google_server("www.google.com", 443);
757 // Check by initializing empty ServerNetworkStats.
758 ServerNetworkStatsMap
server_network_stats_map(
759 ServerNetworkStatsMap::NO_AUTO_EVICT
);
760 impl_
.InitializeServerNetworkStats(&server_network_stats_map
);
761 const ServerNetworkStats
* stats
= impl_
.GetServerNetworkStats(google_server
);
762 EXPECT_EQ(NULL
, stats
);
764 // Check by initializing with www.google.com:443.
765 ServerNetworkStats stats1
;
766 stats1
.srtt
= base::TimeDelta::FromMicroseconds(10);
767 stats1
.bandwidth_estimate
= QuicBandwidth::FromBitsPerSecond(100);
768 server_network_stats_map
.Put(google_server
, stats1
);
769 impl_
.InitializeServerNetworkStats(&server_network_stats_map
);
771 const ServerNetworkStats
* stats2
= impl_
.GetServerNetworkStats(google_server
);
772 EXPECT_EQ(10, stats2
->srtt
.ToInternalValue());
773 EXPECT_EQ(100, stats2
->bandwidth_estimate
.ToBitsPerSecond());
776 TEST_F(ServerNetworkStatsServerPropertiesTest
, SetServerNetworkStats
) {
777 HostPortPair
foo_server("foo", 80);
778 const ServerNetworkStats
* stats
= impl_
.GetServerNetworkStats(foo_server
);
779 EXPECT_EQ(NULL
, stats
);
781 ServerNetworkStats stats1
;
782 stats1
.srtt
= base::TimeDelta::FromMicroseconds(10);
783 stats1
.bandwidth_estimate
= QuicBandwidth::FromBitsPerSecond(100);
784 impl_
.SetServerNetworkStats(foo_server
, stats1
);
786 const ServerNetworkStats
* stats2
= impl_
.GetServerNetworkStats(foo_server
);
787 EXPECT_EQ(10, stats2
->srtt
.ToInternalValue());
788 EXPECT_EQ(100, stats2
->bandwidth_estimate
.ToBitsPerSecond());
791 const ServerNetworkStats
* stats3
= impl_
.GetServerNetworkStats(foo_server
);
792 EXPECT_EQ(NULL
, stats3
);