Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / net / http / http_server_properties_impl_unittest.cc
blob4dda0db83fee69ceb5086b962fd2b28efb6133d5
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"
7 #include <string>
8 #include <vector>
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"
18 namespace base {
19 class ListValue;
22 namespace net {
24 const int kMaxSupportsSpdyServerHosts = 500;
26 namespace {
28 class HttpServerPropertiesImplTest : public testing::Test {
29 protected:
30 HttpServerPropertiesImpl impl_;
33 typedef HttpServerPropertiesImplTest SpdyServerPropertiesTest;
35 TEST_F(SpdyServerPropertiesTest, Initialize) {
36 HostPortPair spdy_server_google("www.google.com", 443);
37 std::string spdy_server_g = spdy_server_google.ToString();
39 HostPortPair spdy_server_docs("docs.google.com", 443);
40 std::string spdy_server_d = spdy_server_docs.ToString();
42 // Check by initializing NULL spdy servers.
43 impl_.InitializeSpdyServers(NULL, true);
44 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
46 // Check by initializing empty spdy servers.
47 std::vector<std::string> spdy_servers;
48 impl_.InitializeSpdyServers(&spdy_servers, true);
49 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
51 // Check by initializing with www.google.com:443 spdy server.
52 std::vector<std::string> spdy_servers1;
53 spdy_servers1.push_back(spdy_server_g);
54 impl_.InitializeSpdyServers(&spdy_servers1, true);
55 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
57 // Check by initializing with www.google.com:443 and docs.google.com:443 spdy
58 // servers.
59 std::vector<std::string> spdy_servers2;
60 spdy_servers2.push_back(spdy_server_g);
61 spdy_servers2.push_back(spdy_server_d);
62 impl_.InitializeSpdyServers(&spdy_servers2, true);
64 // Verify spdy_server_g and spdy_server_d are in the list in the same order.
65 base::ListValue spdy_server_list;
66 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
67 EXPECT_EQ(2U, spdy_server_list.GetSize());
68 std::string string_value_g;
69 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
70 ASSERT_EQ(spdy_server_g, string_value_g);
71 std::string string_value_d;
72 ASSERT_TRUE(spdy_server_list.GetString(1, &string_value_d));
73 ASSERT_EQ(spdy_server_d, string_value_d);
74 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
75 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_docs));
78 TEST_F(SpdyServerPropertiesTest, SupportsSpdyTest) {
79 HostPortPair spdy_server_empty(std::string(), 443);
80 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_empty));
82 // Add www.google.com:443 as supporting SPDY.
83 HostPortPair spdy_server_google("www.google.com", 443);
84 impl_.SetSupportsSpdy(spdy_server_google, true);
85 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
87 // Add mail.google.com:443 as not supporting SPDY.
88 HostPortPair spdy_server_mail("mail.google.com", 443);
89 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_mail));
91 // Add docs.google.com:443 as supporting SPDY.
92 HostPortPair spdy_server_docs("docs.google.com", 443);
93 impl_.SetSupportsSpdy(spdy_server_docs, true);
94 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_docs));
96 // Verify all the entries are the same after additions.
97 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
98 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_mail));
99 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_docs));
102 TEST_F(SpdyServerPropertiesTest, SetSupportsSpdy) {
103 HostPortPair spdy_server_empty(std::string(), 443);
104 impl_.SetSupportsSpdy(spdy_server_empty, true);
105 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_empty));
107 // Add www.google.com:443 as supporting SPDY.
108 HostPortPair spdy_server_google("www.google.com", 443);
109 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
110 impl_.SetSupportsSpdy(spdy_server_google, true);
111 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
113 // Make www.google.com:443 as not supporting SPDY.
114 impl_.SetSupportsSpdy(spdy_server_google, false);
115 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
117 // Add mail.google.com:443 as supporting SPDY.
118 HostPortPair spdy_server_mail("mail.google.com", 443);
119 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_mail));
120 impl_.SetSupportsSpdy(spdy_server_mail, true);
121 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_mail));
122 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
125 TEST_F(SpdyServerPropertiesTest, Clear) {
126 // Add www.google.com:443 and mail.google.com:443 as supporting SPDY.
127 HostPortPair spdy_server_google("www.google.com", 443);
128 impl_.SetSupportsSpdy(spdy_server_google, true);
129 HostPortPair spdy_server_mail("mail.google.com", 443);
130 impl_.SetSupportsSpdy(spdy_server_mail, true);
132 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
133 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_mail));
135 impl_.Clear();
136 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
137 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_mail));
140 TEST_F(SpdyServerPropertiesTest, GetSpdyServerList) {
141 base::ListValue spdy_server_list;
143 // Check there are no spdy_servers.
144 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
145 EXPECT_EQ(0U, spdy_server_list.GetSize());
147 // Check empty server is not added.
148 HostPortPair spdy_server_empty(std::string(), 443);
149 impl_.SetSupportsSpdy(spdy_server_empty, true);
150 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
151 EXPECT_EQ(0U, spdy_server_list.GetSize());
153 std::string string_value_g;
154 std::string string_value_m;
155 HostPortPair spdy_server_google("www.google.com", 443);
156 std::string spdy_server_g = spdy_server_google.ToString();
157 HostPortPair spdy_server_mail("mail.google.com", 443);
158 std::string spdy_server_m = spdy_server_mail.ToString();
160 // Add www.google.com:443 as not supporting SPDY.
161 impl_.SetSupportsSpdy(spdy_server_google, false);
162 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
163 EXPECT_EQ(0U, spdy_server_list.GetSize());
165 // Add www.google.com:443 as supporting SPDY.
166 impl_.SetSupportsSpdy(spdy_server_google, true);
167 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
168 ASSERT_EQ(1U, spdy_server_list.GetSize());
169 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
170 ASSERT_EQ(spdy_server_g, string_value_g);
172 // Add mail.google.com:443 as not supporting SPDY.
173 impl_.SetSupportsSpdy(spdy_server_mail, false);
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 supporting SPDY.
180 impl_.SetSupportsSpdy(spdy_server_mail, true);
181 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
182 ASSERT_EQ(2U, spdy_server_list.GetSize());
184 // Verify www.google.com:443 and mail.google.com:443 are in the list.
185 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_m));
186 ASSERT_EQ(spdy_server_m, string_value_m);
187 ASSERT_TRUE(spdy_server_list.GetString(1, &string_value_g));
188 ASSERT_EQ(spdy_server_g, string_value_g);
190 // Request for only one server and verify that we get only one server.
191 impl_.GetSpdyServerList(&spdy_server_list, 1);
192 ASSERT_EQ(1U, spdy_server_list.GetSize());
193 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_m));
194 ASSERT_EQ(spdy_server_m, string_value_m);
197 TEST_F(SpdyServerPropertiesTest, MRUOfGetSpdyServerList) {
198 base::ListValue spdy_server_list;
200 std::string string_value_g;
201 std::string string_value_m;
202 HostPortPair spdy_server_google("www.google.com", 443);
203 std::string spdy_server_g = spdy_server_google.ToString();
204 HostPortPair spdy_server_mail("mail.google.com", 443);
205 std::string spdy_server_m = spdy_server_mail.ToString();
207 // Add www.google.com:443 as supporting SPDY.
208 impl_.SetSupportsSpdy(spdy_server_google, true);
209 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
210 ASSERT_EQ(1U, spdy_server_list.GetSize());
211 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
212 ASSERT_EQ(spdy_server_g, string_value_g);
214 // Add mail.google.com:443 as supporting SPDY. Verify mail.google.com:443 and
215 // www.google.com:443 are in the list.
216 impl_.SetSupportsSpdy(spdy_server_mail, true);
217 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
218 ASSERT_EQ(2U, spdy_server_list.GetSize());
219 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_m));
220 ASSERT_EQ(spdy_server_m, string_value_m);
221 ASSERT_TRUE(spdy_server_list.GetString(1, &string_value_g));
222 ASSERT_EQ(spdy_server_g, string_value_g);
224 // Get www.google.com:443 should reorder SpdyServerHostPortMap. Verify that it
225 // is www.google.com:443 is the MRU server.
226 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
227 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
228 ASSERT_EQ(2U, spdy_server_list.GetSize());
229 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
230 ASSERT_EQ(spdy_server_g, string_value_g);
231 ASSERT_TRUE(spdy_server_list.GetString(1, &string_value_m));
232 ASSERT_EQ(spdy_server_m, string_value_m);
235 typedef HttpServerPropertiesImplTest AlternateProtocolServerPropertiesTest;
237 TEST_F(AlternateProtocolServerPropertiesTest, Basic) {
238 HostPortPair test_host_port_pair("foo", 80);
239 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
240 impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, 1);
241 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
242 const AlternateProtocolInfo alternate =
243 impl_.GetAlternateProtocol(test_host_port_pair);
244 EXPECT_EQ(443, alternate.port);
245 EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
247 impl_.Clear();
248 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
251 TEST_F(AlternateProtocolServerPropertiesTest, DefaultProbabilityExcluded) {
252 HostPortPair test_host_port_pair("foo", 80);
253 impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, .99);
255 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
258 TEST_F(AlternateProtocolServerPropertiesTest, Probability) {
259 impl_.SetAlternateProtocolProbabilityThreshold(.25);
261 HostPortPair test_host_port_pair("foo", 80);
262 impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, .5);
264 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
265 const AlternateProtocolInfo alternate =
266 impl_.GetAlternateProtocol(test_host_port_pair);
267 EXPECT_EQ(443, alternate.port);
268 EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
269 EXPECT_EQ(.5, alternate.probability);
272 TEST_F(AlternateProtocolServerPropertiesTest, ProbabilityExcluded) {
273 impl_.SetAlternateProtocolProbabilityThreshold(.75);
275 HostPortPair test_host_port_pair("foo", 80);
277 impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, .5);
278 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
281 TEST_F(AlternateProtocolServerPropertiesTest, Initialize) {
282 HostPortPair test_host_port_pair1("foo1", 80);
283 impl_.SetAlternateProtocol(test_host_port_pair1, 443, NPN_SPDY_3, 1);
284 impl_.SetBrokenAlternateProtocol(test_host_port_pair1);
285 HostPortPair test_host_port_pair2("foo2", 80);
286 impl_.SetAlternateProtocol(test_host_port_pair2, 443, NPN_SPDY_3, 1);
288 AlternateProtocolMap alternate_protocol_map(
289 AlternateProtocolMap::NO_AUTO_EVICT);
290 AlternateProtocolInfo alternate(123, NPN_SPDY_3, 1);
291 alternate_protocol_map.Put(test_host_port_pair2, alternate);
292 HostPortPair test_host_port_pair3("foo3", 80);
293 alternate.port = 1234;
294 alternate_protocol_map.Put(test_host_port_pair3, alternate);
295 impl_.InitializeAlternateProtocolServers(&alternate_protocol_map);
297 // Verify test_host_port_pair3 is the MRU server.
298 const net::AlternateProtocolMap& map = impl_.alternate_protocol_map();
299 net::AlternateProtocolMap::const_iterator it = map.begin();
300 it = map.begin();
301 EXPECT_TRUE(it->first.Equals(test_host_port_pair3));
302 EXPECT_EQ(1234, it->second.port);
303 EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
305 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair1));
306 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair2));
307 alternate = impl_.GetAlternateProtocol(test_host_port_pair1);
308 EXPECT_TRUE(alternate.is_broken);
309 alternate = impl_.GetAlternateProtocol(test_host_port_pair2);
310 EXPECT_EQ(123, alternate.port);
311 EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
314 TEST_F(AlternateProtocolServerPropertiesTest, MRUOfHasAlternateProtocol) {
315 HostPortPair test_host_port_pair1("foo1", 80);
316 impl_.SetAlternateProtocol(test_host_port_pair1, 443, NPN_SPDY_3, 1);
317 HostPortPair test_host_port_pair2("foo2", 80);
318 impl_.SetAlternateProtocol(test_host_port_pair2, 1234, NPN_SPDY_3, 1);
320 const net::AlternateProtocolMap& map = impl_.alternate_protocol_map();
321 net::AlternateProtocolMap::const_iterator it = map.begin();
322 EXPECT_TRUE(it->first.Equals(test_host_port_pair2));
323 EXPECT_EQ(1234, it->second.port);
324 EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
326 // HasAlternateProtocol should reoder the AlternateProtocol map.
327 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair1));
328 it = map.begin();
329 EXPECT_TRUE(it->first.Equals(test_host_port_pair1));
330 EXPECT_EQ(443, it->second.port);
331 EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
334 TEST_F(AlternateProtocolServerPropertiesTest, MRUOfGetAlternateProtocol) {
335 HostPortPair test_host_port_pair1("foo1", 80);
336 impl_.SetAlternateProtocol(test_host_port_pair1, 443, NPN_SPDY_3, 1);
337 HostPortPair test_host_port_pair2("foo2", 80);
338 impl_.SetAlternateProtocol(test_host_port_pair2, 1234, NPN_SPDY_3, 1);
340 const net::AlternateProtocolMap& map = impl_.alternate_protocol_map();
341 net::AlternateProtocolMap::const_iterator it = map.begin();
342 EXPECT_TRUE(it->first.Equals(test_host_port_pair2));
343 EXPECT_EQ(1234, it->second.port);
344 EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
346 // GetAlternateProtocol should reoder the AlternateProtocol map.
347 AlternateProtocolInfo alternate =
348 impl_.GetAlternateProtocol(test_host_port_pair1);
349 EXPECT_EQ(443, alternate.port);
350 EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
351 it = map.begin();
352 EXPECT_TRUE(it->first.Equals(test_host_port_pair1));
353 EXPECT_EQ(443, it->second.port);
354 EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
357 TEST_F(AlternateProtocolServerPropertiesTest, SetBroken) {
358 HostPortPair test_host_port_pair("foo", 80);
359 impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, 1);
360 impl_.SetBrokenAlternateProtocol(test_host_port_pair);
361 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
362 AlternateProtocolInfo alternate =
363 impl_.GetAlternateProtocol(test_host_port_pair);
364 EXPECT_TRUE(alternate.is_broken);
366 impl_.SetAlternateProtocol(
367 test_host_port_pair,
368 1234,
369 NPN_SPDY_3,
371 alternate = impl_.GetAlternateProtocol(test_host_port_pair);
372 EXPECT_TRUE(alternate.is_broken) << "Second attempt should be ignored.";
375 TEST_F(AlternateProtocolServerPropertiesTest, ClearBroken) {
376 HostPortPair test_host_port_pair("foo", 80);
377 impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, 1);
378 impl_.SetBrokenAlternateProtocol(test_host_port_pair);
379 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
380 AlternateProtocolInfo alternate =
381 impl_.GetAlternateProtocol(test_host_port_pair);
382 EXPECT_TRUE(alternate.is_broken);
383 impl_.ClearAlternateProtocol(test_host_port_pair);
384 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
387 TEST_F(AlternateProtocolServerPropertiesTest, Forced) {
388 // Test forced alternate protocols.
390 AlternateProtocolInfo default_protocol(1234, NPN_SPDY_3, 1);
391 HttpServerPropertiesImpl::ForceAlternateProtocol(default_protocol);
393 // Verify the forced protocol.
394 HostPortPair test_host_port_pair("foo", 80);
395 EXPECT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
396 AlternateProtocolInfo alternate =
397 impl_.GetAlternateProtocol(test_host_port_pair);
398 EXPECT_EQ(default_protocol.port, alternate.port);
399 EXPECT_EQ(default_protocol.protocol, alternate.protocol);
401 // Verify the real protocol overrides the forced protocol.
402 impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, 1);
403 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
404 alternate = impl_.GetAlternateProtocol(test_host_port_pair);
405 EXPECT_EQ(443, alternate.port);
406 EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
408 // Turn off the static, forced alternate protocol so that tests don't
409 // have this state.
410 HttpServerPropertiesImpl::DisableForcedAlternateProtocol();
412 // Verify the forced protocol is off.
413 HostPortPair test_host_port_pair2("bar", 80);
414 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair2));
417 TEST_F(AlternateProtocolServerPropertiesTest, Canonical) {
418 HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
419 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
421 HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
422 EXPECT_FALSE(impl_.HasAlternateProtocol(canonical_port_pair));
424 AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
426 impl_.SetAlternateProtocol(canonical_port_pair,
427 canonical_protocol.port,
428 canonical_protocol.protocol,
430 // Verify the forced protocol.
431 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
432 AlternateProtocolInfo alternate =
433 impl_.GetAlternateProtocol(test_host_port_pair);
434 EXPECT_EQ(canonical_protocol.port, alternate.port);
435 EXPECT_EQ(canonical_protocol.protocol, alternate.protocol);
437 // Verify the canonical suffix.
438 EXPECT_EQ(".c.youtube.com", impl_.GetCanonicalSuffix(test_host_port_pair));
439 EXPECT_EQ(".c.youtube.com", impl_.GetCanonicalSuffix(canonical_port_pair));
442 TEST_F(AlternateProtocolServerPropertiesTest, CanonicalBelowThreshold) {
443 impl_.SetAlternateProtocolProbabilityThreshold(0.02);
445 HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
446 HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
447 AlternateProtocolInfo canonical_protocol(1234, QUIC, 0.01);
449 impl_.SetAlternateProtocol(canonical_port_pair,
450 canonical_protocol.port,
451 canonical_protocol.protocol,
452 canonical_protocol.probability);
453 EXPECT_FALSE(impl_.HasAlternateProtocol(canonical_port_pair));
454 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
457 TEST_F(AlternateProtocolServerPropertiesTest, CanonicalAboveThreshold) {
458 impl_.SetAlternateProtocolProbabilityThreshold(0.02);
460 HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
461 HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
462 AlternateProtocolInfo canonical_protocol(1234, QUIC, 0.03);
464 impl_.SetAlternateProtocol(canonical_port_pair,
465 canonical_protocol.port,
466 canonical_protocol.protocol,
467 canonical_protocol.probability);
468 EXPECT_TRUE(impl_.HasAlternateProtocol(canonical_port_pair));
469 EXPECT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
472 TEST_F(AlternateProtocolServerPropertiesTest, ClearCanonical) {
473 HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
474 HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
476 AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
478 impl_.SetAlternateProtocol(canonical_port_pair,
479 canonical_protocol.port,
480 canonical_protocol.protocol,
481 canonical_protocol.probability);
483 impl_.ClearAlternateProtocol(canonical_port_pair);
484 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
487 TEST_F(AlternateProtocolServerPropertiesTest, CanonicalBroken) {
488 HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
489 HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
491 AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
493 impl_.SetAlternateProtocol(canonical_port_pair,
494 canonical_protocol.port,
495 canonical_protocol.protocol,
496 canonical_protocol.probability);
498 impl_.SetBrokenAlternateProtocol(canonical_port_pair);
499 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
502 TEST_F(AlternateProtocolServerPropertiesTest, CanonicalBroken2) {
503 HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
504 HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
506 AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
508 impl_.SetAlternateProtocol(canonical_port_pair,
509 canonical_protocol.port,
510 canonical_protocol.protocol,
511 canonical_protocol.probability);
513 impl_.SetBrokenAlternateProtocol(test_host_port_pair);
514 AlternateProtocolInfo alternate =
515 impl_.GetAlternateProtocol(test_host_port_pair);
516 EXPECT_TRUE(alternate.is_broken);
519 TEST_F(AlternateProtocolServerPropertiesTest, ClearWithCanonical) {
520 HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
521 HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
523 AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
525 impl_.SetAlternateProtocol(canonical_port_pair,
526 canonical_protocol.port,
527 canonical_protocol.protocol,
528 canonical_protocol.probability);
530 impl_.Clear();
531 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
534 typedef HttpServerPropertiesImplTest SpdySettingsServerPropertiesTest;
536 TEST_F(SpdySettingsServerPropertiesTest, Initialize) {
537 HostPortPair spdy_server_google("www.google.com", 443);
539 // Check by initializing empty spdy settings.
540 SpdySettingsMap spdy_settings_map(SpdySettingsMap::NO_AUTO_EVICT);
541 impl_.InitializeSpdySettingsServers(&spdy_settings_map);
542 EXPECT_TRUE(impl_.GetSpdySettings(spdy_server_google).empty());
544 // Check by initializing with www.google.com:443 spdy server settings.
545 SettingsMap settings_map;
546 const SpdySettingsIds id = SETTINGS_UPLOAD_BANDWIDTH;
547 const SpdySettingsFlags flags = SETTINGS_FLAG_PERSISTED;
548 const uint32 value = 31337;
549 SettingsFlagsAndValue flags_and_value(flags, value);
550 settings_map[id] = flags_and_value;
551 spdy_settings_map.Put(spdy_server_google, settings_map);
552 impl_.InitializeSpdySettingsServers(&spdy_settings_map);
554 const SettingsMap& settings_map2 = impl_.GetSpdySettings(spdy_server_google);
555 ASSERT_EQ(1U, settings_map2.size());
556 SettingsMap::const_iterator it = settings_map2.find(id);
557 EXPECT_TRUE(it != settings_map2.end());
558 SettingsFlagsAndValue flags_and_value2 = it->second;
559 EXPECT_EQ(flags, flags_and_value2.first);
560 EXPECT_EQ(value, flags_and_value2.second);
563 TEST_F(SpdySettingsServerPropertiesTest, SetSpdySetting) {
564 HostPortPair spdy_server_empty(std::string(), 443);
565 const SettingsMap& settings_map0 = impl_.GetSpdySettings(spdy_server_empty);
566 EXPECT_EQ(0U, settings_map0.size()); // Returns kEmptySettingsMap.
568 // Add www.google.com:443 as persisting.
569 HostPortPair spdy_server_google("www.google.com", 443);
570 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
571 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
572 const uint32 value1 = 31337;
573 EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_google, id1, flags1, value1));
574 // Check the values.
575 const SettingsMap& settings_map1_ret =
576 impl_.GetSpdySettings(spdy_server_google);
577 ASSERT_EQ(1U, settings_map1_ret.size());
578 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
579 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
580 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
581 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
582 EXPECT_EQ(value1, flags_and_value1_ret.second);
584 // Add mail.google.com:443 as not persisting.
585 HostPortPair spdy_server_mail("mail.google.com", 443);
586 const SpdySettingsIds id2 = SETTINGS_DOWNLOAD_BANDWIDTH;
587 const SpdySettingsFlags flags2 = SETTINGS_FLAG_NONE;
588 const uint32 value2 = 62667;
589 EXPECT_FALSE(impl_.SetSpdySetting(spdy_server_mail, id2, flags2, value2));
590 const SettingsMap& settings_map2_ret =
591 impl_.GetSpdySettings(spdy_server_mail);
592 EXPECT_EQ(0U, settings_map2_ret.size()); // Returns kEmptySettingsMap.
594 // Add docs.google.com:443 as persisting
595 HostPortPair spdy_server_docs("docs.google.com", 443);
596 const SpdySettingsIds id3 = SETTINGS_ROUND_TRIP_TIME;
597 const SpdySettingsFlags flags3 = SETTINGS_FLAG_PLEASE_PERSIST;
598 const uint32 value3 = 93997;
599 SettingsFlagsAndValue flags_and_value3(flags3, value3);
600 EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id3, flags3, value3));
601 // Check the values.
602 const SettingsMap& settings_map3_ret =
603 impl_.GetSpdySettings(spdy_server_docs);
604 ASSERT_EQ(1U, settings_map3_ret.size());
605 SettingsMap::const_iterator it3_ret = settings_map3_ret.find(id3);
606 EXPECT_TRUE(it3_ret != settings_map3_ret.end());
607 SettingsFlagsAndValue flags_and_value3_ret = it3_ret->second;
608 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value3_ret.first);
609 EXPECT_EQ(value3, flags_and_value3_ret.second);
611 // Check data for www.google.com:443 (id1).
612 const SettingsMap& settings_map4_ret =
613 impl_.GetSpdySettings(spdy_server_google);
614 ASSERT_EQ(1U, settings_map4_ret.size());
615 SettingsMap::const_iterator it4_ret = settings_map4_ret.find(id1);
616 EXPECT_TRUE(it4_ret != settings_map4_ret.end());
617 SettingsFlagsAndValue flags_and_value4_ret = it4_ret->second;
618 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value4_ret.first);
619 EXPECT_EQ(value1, flags_and_value1_ret.second);
621 // Clear www.google.com:443 as persisting.
622 impl_.ClearSpdySettings(spdy_server_google);
623 // Check the values.
624 const SettingsMap& settings_map5_ret =
625 impl_.GetSpdySettings(spdy_server_google);
626 ASSERT_EQ(0U, settings_map5_ret.size());
628 // Clear all settings.
629 ASSERT_GT(impl_.spdy_settings_map().size(), 0U);
630 impl_.ClearAllSpdySettings();
631 ASSERT_EQ(0U, impl_.spdy_settings_map().size());
634 TEST_F(SpdySettingsServerPropertiesTest, Clear) {
635 // Add www.google.com:443 as persisting.
636 HostPortPair spdy_server_google("www.google.com", 443);
637 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
638 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
639 const uint32 value1 = 31337;
640 EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_google, id1, flags1, value1));
641 // Check the values.
642 const SettingsMap& settings_map1_ret =
643 impl_.GetSpdySettings(spdy_server_google);
644 ASSERT_EQ(1U, settings_map1_ret.size());
645 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
646 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
647 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
648 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
649 EXPECT_EQ(value1, flags_and_value1_ret.second);
651 // Add docs.google.com:443 as persisting
652 HostPortPair spdy_server_docs("docs.google.com", 443);
653 const SpdySettingsIds id3 = SETTINGS_ROUND_TRIP_TIME;
654 const SpdySettingsFlags flags3 = SETTINGS_FLAG_PLEASE_PERSIST;
655 const uint32 value3 = 93997;
656 EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id3, flags3, value3));
657 // Check the values.
658 const SettingsMap& settings_map3_ret =
659 impl_.GetSpdySettings(spdy_server_docs);
660 ASSERT_EQ(1U, settings_map3_ret.size());
661 SettingsMap::const_iterator it3_ret = settings_map3_ret.find(id3);
662 EXPECT_TRUE(it3_ret != settings_map3_ret.end());
663 SettingsFlagsAndValue flags_and_value3_ret = it3_ret->second;
664 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value3_ret.first);
665 EXPECT_EQ(value3, flags_and_value3_ret.second);
667 impl_.Clear();
668 EXPECT_EQ(0U, impl_.GetSpdySettings(spdy_server_google).size());
669 EXPECT_EQ(0U, impl_.GetSpdySettings(spdy_server_docs).size());
672 TEST_F(SpdySettingsServerPropertiesTest, MRUOfGetSpdySettings) {
673 // Add www.google.com:443 as persisting.
674 HostPortPair spdy_server_google("www.google.com", 443);
675 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
676 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
677 const uint32 value1 = 31337;
678 EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_google, id1, flags1, value1));
680 // Add docs.google.com:443 as persisting
681 HostPortPair spdy_server_docs("docs.google.com", 443);
682 const SpdySettingsIds id2 = SETTINGS_ROUND_TRIP_TIME;
683 const SpdySettingsFlags flags2 = SETTINGS_FLAG_PLEASE_PERSIST;
684 const uint32 value2 = 93997;
685 EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id2, flags2, value2));
687 // Verify the first element is docs.google.com:443.
688 const net::SpdySettingsMap& map = impl_.spdy_settings_map();
689 net::SpdySettingsMap::const_iterator it = map.begin();
690 EXPECT_TRUE(it->first.Equals(spdy_server_docs));
691 const SettingsMap& settings_map2_ret = it->second;
692 ASSERT_EQ(1U, settings_map2_ret.size());
693 SettingsMap::const_iterator it2_ret = settings_map2_ret.find(id2);
694 EXPECT_TRUE(it2_ret != settings_map2_ret.end());
695 SettingsFlagsAndValue flags_and_value2_ret = it2_ret->second;
696 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value2_ret.first);
697 EXPECT_EQ(value2, flags_and_value2_ret.second);
699 // GetSpdySettings should reoder the SpdySettingsMap.
700 const SettingsMap& settings_map1_ret =
701 impl_.GetSpdySettings(spdy_server_google);
702 ASSERT_EQ(1U, settings_map1_ret.size());
703 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
704 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
705 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
706 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
707 EXPECT_EQ(value1, flags_and_value1_ret.second);
709 // Check the first entry is spdy_server_google by accessing it via iterator.
710 it = map.begin();
711 EXPECT_TRUE(it->first.Equals(spdy_server_google));
712 const SettingsMap& settings_map1_it_ret = it->second;
713 ASSERT_EQ(1U, settings_map1_it_ret.size());
714 it1_ret = settings_map1_it_ret.find(id1);
715 EXPECT_TRUE(it1_ret != settings_map1_it_ret.end());
716 flags_and_value1_ret = it1_ret->second;
717 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
718 EXPECT_EQ(value1, flags_and_value1_ret.second);
721 typedef HttpServerPropertiesImplTest SupportsQuicServerPropertiesTest;
723 TEST_F(SupportsQuicServerPropertiesTest, Initialize) {
724 HostPortPair quic_server_google("www.google.com", 443);
726 // Check by initializing empty SupportsQuic.
727 SupportsQuicMap supports_quic_map;
728 impl_.InitializeSupportsQuic(&supports_quic_map);
729 SupportsQuic supports_quic = impl_.GetSupportsQuic(quic_server_google);
730 EXPECT_FALSE(supports_quic.used_quic);
731 EXPECT_EQ("", supports_quic.address);
733 // Check by initializing with www.google.com:443.
734 SupportsQuic supports_quic1(true, "foo");
735 supports_quic_map.insert(std::make_pair(quic_server_google, supports_quic1));
736 impl_.InitializeSupportsQuic(&supports_quic_map);
738 SupportsQuic supports_quic2 = impl_.GetSupportsQuic(quic_server_google);
739 EXPECT_TRUE(supports_quic2.used_quic);
740 EXPECT_EQ("foo", supports_quic2.address);
743 TEST_F(SupportsQuicServerPropertiesTest, SetSupportsQuic) {
744 HostPortPair test_host_port_pair("foo", 80);
745 SupportsQuic supports_quic = impl_.GetSupportsQuic(test_host_port_pair);
746 EXPECT_FALSE(supports_quic.used_quic);
747 EXPECT_EQ("", supports_quic.address);
748 impl_.SetSupportsQuic(test_host_port_pair, true, "foo");
749 SupportsQuic supports_quic1 = impl_.GetSupportsQuic(test_host_port_pair);
750 EXPECT_TRUE(supports_quic1.used_quic);
751 EXPECT_EQ("foo", supports_quic1.address);
753 impl_.Clear();
754 SupportsQuic supports_quic2 = impl_.GetSupportsQuic(test_host_port_pair);
755 EXPECT_FALSE(supports_quic2.used_quic);
756 EXPECT_EQ("", supports_quic2.address);
758 } // namespace
760 } // namespace net