Probably broke Win7 Tests (dbg)(6). http://build.chromium.org/p/chromium.win/builders...
[chromium-blink-merge.git] / net / http / http_server_properties_impl_unittest.cc
blobf21c9692f255e4b5228fa474660d3828cfa818a6
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 =
38 HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_google);
40 HostPortPair spdy_server_docs("docs.google.com", 443);
41 std::string spdy_server_d =
42 HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_docs);
44 // Check by initializing NULL spdy servers.
45 impl_.InitializeSpdyServers(NULL, true);
46 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
48 // Check by initializing empty spdy servers.
49 std::vector<std::string> spdy_servers;
50 impl_.InitializeSpdyServers(&spdy_servers, true);
51 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
53 // Check by initializing with www.google.com:443 spdy server.
54 std::vector<std::string> spdy_servers1;
55 spdy_servers1.push_back(spdy_server_g);
56 impl_.InitializeSpdyServers(&spdy_servers1, true);
57 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
59 // Check by initializing with www.google.com:443 and docs.google.com:443 spdy
60 // servers.
61 std::vector<std::string> spdy_servers2;
62 spdy_servers2.push_back(spdy_server_g);
63 spdy_servers2.push_back(spdy_server_d);
64 impl_.InitializeSpdyServers(&spdy_servers2, true);
66 // Verify spdy_server_g and spdy_server_d are in the list in the same order.
67 base::ListValue spdy_server_list;
68 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
69 EXPECT_EQ(2U, spdy_server_list.GetSize());
70 std::string string_value_g;
71 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
72 ASSERT_EQ(spdy_server_g, string_value_g);
73 std::string string_value_d;
74 ASSERT_TRUE(spdy_server_list.GetString(1, &string_value_d));
75 ASSERT_EQ(spdy_server_d, string_value_d);
76 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
77 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_docs));
80 TEST_F(SpdyServerPropertiesTest, SupportsSpdyTest) {
81 HostPortPair spdy_server_empty(std::string(), 443);
82 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_empty));
84 // Add www.google.com:443 as supporting SPDY.
85 HostPortPair spdy_server_google("www.google.com", 443);
86 impl_.SetSupportsSpdy(spdy_server_google, true);
87 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
89 // Add mail.google.com:443 as not supporting SPDY.
90 HostPortPair spdy_server_mail("mail.google.com", 443);
91 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_mail));
93 // Add docs.google.com:443 as supporting SPDY.
94 HostPortPair spdy_server_docs("docs.google.com", 443);
95 impl_.SetSupportsSpdy(spdy_server_docs, true);
96 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_docs));
98 // Verify all the entries are the same after additions.
99 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
100 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_mail));
101 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_docs));
104 TEST_F(SpdyServerPropertiesTest, SetSupportsSpdy) {
105 HostPortPair spdy_server_empty(std::string(), 443);
106 impl_.SetSupportsSpdy(spdy_server_empty, true);
107 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_empty));
109 // Add www.google.com:443 as supporting SPDY.
110 HostPortPair spdy_server_google("www.google.com", 443);
111 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
112 impl_.SetSupportsSpdy(spdy_server_google, true);
113 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
115 // Make www.google.com:443 as not supporting SPDY.
116 impl_.SetSupportsSpdy(spdy_server_google, false);
117 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
119 // Add mail.google.com:443 as supporting SPDY.
120 HostPortPair spdy_server_mail("mail.google.com", 443);
121 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_mail));
122 impl_.SetSupportsSpdy(spdy_server_mail, true);
123 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_mail));
124 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
127 TEST_F(SpdyServerPropertiesTest, Clear) {
128 // Add www.google.com:443 and mail.google.com:443 as supporting SPDY.
129 HostPortPair spdy_server_google("www.google.com", 443);
130 impl_.SetSupportsSpdy(spdy_server_google, true);
131 HostPortPair spdy_server_mail("mail.google.com", 443);
132 impl_.SetSupportsSpdy(spdy_server_mail, true);
134 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
135 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_mail));
137 impl_.Clear();
138 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_google));
139 EXPECT_FALSE(impl_.SupportsSpdy(spdy_server_mail));
142 TEST_F(SpdyServerPropertiesTest, GetSpdyServerList) {
143 base::ListValue spdy_server_list;
145 // Check there are no spdy_servers.
146 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
147 EXPECT_EQ(0U, spdy_server_list.GetSize());
149 // Check empty server is not added.
150 HostPortPair spdy_server_empty(std::string(), 443);
151 impl_.SetSupportsSpdy(spdy_server_empty, true);
152 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
153 EXPECT_EQ(0U, spdy_server_list.GetSize());
155 std::string string_value_g;
156 std::string string_value_m;
157 HostPortPair spdy_server_google("www.google.com", 443);
158 std::string spdy_server_g =
159 HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_google);
160 HostPortPair spdy_server_mail("mail.google.com", 443);
161 std::string spdy_server_m =
162 HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_mail);
164 // Add www.google.com:443 as not supporting SPDY.
165 impl_.SetSupportsSpdy(spdy_server_google, false);
166 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
167 EXPECT_EQ(0U, spdy_server_list.GetSize());
169 // Add www.google.com:443 as supporting SPDY.
170 impl_.SetSupportsSpdy(spdy_server_google, true);
171 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
172 ASSERT_EQ(1U, spdy_server_list.GetSize());
173 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
174 ASSERT_EQ(spdy_server_g, string_value_g);
176 // Add mail.google.com:443 as not supporting SPDY.
177 impl_.SetSupportsSpdy(spdy_server_mail, false);
178 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
179 ASSERT_EQ(1U, spdy_server_list.GetSize());
180 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
181 ASSERT_EQ(spdy_server_g, string_value_g);
183 // Add mail.google.com:443 as supporting SPDY.
184 impl_.SetSupportsSpdy(spdy_server_mail, true);
185 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
186 ASSERT_EQ(2U, spdy_server_list.GetSize());
188 // Verify www.google.com:443 and mail.google.com:443 are in the list.
189 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_m));
190 ASSERT_EQ(spdy_server_m, string_value_m);
191 ASSERT_TRUE(spdy_server_list.GetString(1, &string_value_g));
192 ASSERT_EQ(spdy_server_g, string_value_g);
194 // Request for only one server and verify that we get only one server.
195 impl_.GetSpdyServerList(&spdy_server_list, 1);
196 ASSERT_EQ(1U, spdy_server_list.GetSize());
197 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_m));
198 ASSERT_EQ(spdy_server_m, string_value_m);
201 TEST_F(SpdyServerPropertiesTest, MRUOfGetSpdyServerList) {
202 base::ListValue spdy_server_list;
204 std::string string_value_g;
205 std::string string_value_m;
206 HostPortPair spdy_server_google("www.google.com", 443);
207 std::string spdy_server_g =
208 HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_google);
209 HostPortPair spdy_server_mail("mail.google.com", 443);
210 std::string spdy_server_m =
211 HttpServerPropertiesImpl::GetFlattenedSpdyServer(spdy_server_mail);
213 // Add www.google.com:443 as supporting SPDY.
214 impl_.SetSupportsSpdy(spdy_server_google, true);
215 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
216 ASSERT_EQ(1U, spdy_server_list.GetSize());
217 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
218 ASSERT_EQ(spdy_server_g, string_value_g);
220 // Add mail.google.com:443 as supporting SPDY. Verify mail.google.com:443 and
221 // www.google.com:443 are in the list.
222 impl_.SetSupportsSpdy(spdy_server_mail, true);
223 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
224 ASSERT_EQ(2U, spdy_server_list.GetSize());
225 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_m));
226 ASSERT_EQ(spdy_server_m, string_value_m);
227 ASSERT_TRUE(spdy_server_list.GetString(1, &string_value_g));
228 ASSERT_EQ(spdy_server_g, string_value_g);
230 // Get www.google.com:443 should reorder SpdyServerHostPortMap. Verify that it
231 // is www.google.com:443 is the MRU server.
232 EXPECT_TRUE(impl_.SupportsSpdy(spdy_server_google));
233 impl_.GetSpdyServerList(&spdy_server_list, kMaxSupportsSpdyServerHosts);
234 ASSERT_EQ(2U, spdy_server_list.GetSize());
235 ASSERT_TRUE(spdy_server_list.GetString(0, &string_value_g));
236 ASSERT_EQ(spdy_server_g, string_value_g);
237 ASSERT_TRUE(spdy_server_list.GetString(1, &string_value_m));
238 ASSERT_EQ(spdy_server_m, string_value_m);
241 typedef HttpServerPropertiesImplTest AlternateProtocolServerPropertiesTest;
243 TEST_F(AlternateProtocolServerPropertiesTest, Basic) {
244 HostPortPair test_host_port_pair("foo", 80);
245 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
246 impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, 1);
247 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
248 const AlternateProtocolInfo alternate =
249 impl_.GetAlternateProtocol(test_host_port_pair);
250 EXPECT_EQ(443, alternate.port);
251 EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
253 impl_.Clear();
254 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
257 TEST_F(AlternateProtocolServerPropertiesTest, Probability) {
258 impl_.SetAlternateProtocolProbabilityThreshold(.25);
260 HostPortPair test_host_port_pair("foo", 80);
261 impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, .5);
263 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
264 const AlternateProtocolInfo alternate =
265 impl_.GetAlternateProtocol(test_host_port_pair);
266 EXPECT_EQ(443, alternate.port);
267 EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
268 EXPECT_EQ(.5, alternate.probability);
271 TEST_F(AlternateProtocolServerPropertiesTest, ProbabilityExcluded) {
272 impl_.SetAlternateProtocolProbabilityThreshold(.75);
274 HostPortPair test_host_port_pair("foo", 80);
276 impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, .5);
277 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
280 TEST_F(AlternateProtocolServerPropertiesTest, Initialize) {
281 HostPortPair test_host_port_pair1("foo1", 80);
282 impl_.SetBrokenAlternateProtocol(test_host_port_pair1);
283 HostPortPair test_host_port_pair2("foo2", 80);
284 impl_.SetAlternateProtocol(test_host_port_pair2, 443, NPN_SPDY_3, 1);
286 AlternateProtocolMap alternate_protocol_map(
287 AlternateProtocolMap::NO_AUTO_EVICT);
288 AlternateProtocolInfo port_alternate_protocol_pair(123, NPN_SPDY_3, 1);
289 alternate_protocol_map.Put(test_host_port_pair2,
290 port_alternate_protocol_pair);
291 HostPortPair test_host_port_pair3("foo3", 80);
292 port_alternate_protocol_pair.port = 1234;
293 alternate_protocol_map.Put(test_host_port_pair3,
294 port_alternate_protocol_pair);
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 port_alternate_protocol_pair =
308 impl_.GetAlternateProtocol(test_host_port_pair1);
309 EXPECT_EQ(ALTERNATE_PROTOCOL_BROKEN, port_alternate_protocol_pair.protocol);
310 port_alternate_protocol_pair =
311 impl_.GetAlternateProtocol(test_host_port_pair2);
312 EXPECT_EQ(123, port_alternate_protocol_pair.port);
313 EXPECT_EQ(NPN_SPDY_3, port_alternate_protocol_pair.protocol);
316 TEST_F(AlternateProtocolServerPropertiesTest, MRUOfHasAlternateProtocol) {
317 HostPortPair test_host_port_pair1("foo1", 80);
318 impl_.SetAlternateProtocol(test_host_port_pair1, 443, NPN_SPDY_3, 1);
319 HostPortPair test_host_port_pair2("foo2", 80);
320 impl_.SetAlternateProtocol(test_host_port_pair2, 1234, NPN_SPDY_3, 1);
322 const net::AlternateProtocolMap& map = impl_.alternate_protocol_map();
323 net::AlternateProtocolMap::const_iterator it = map.begin();
324 EXPECT_TRUE(it->first.Equals(test_host_port_pair2));
325 EXPECT_EQ(1234, it->second.port);
326 EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
328 // HasAlternateProtocol should reoder the AlternateProtocol map.
329 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair1));
330 it = map.begin();
331 EXPECT_TRUE(it->first.Equals(test_host_port_pair1));
332 EXPECT_EQ(443, it->second.port);
333 EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
336 TEST_F(AlternateProtocolServerPropertiesTest, MRUOfGetAlternateProtocol) {
337 HostPortPair test_host_port_pair1("foo1", 80);
338 impl_.SetAlternateProtocol(test_host_port_pair1, 443, NPN_SPDY_3, 1);
339 HostPortPair test_host_port_pair2("foo2", 80);
340 impl_.SetAlternateProtocol(test_host_port_pair2, 1234, NPN_SPDY_3, 1);
342 const net::AlternateProtocolMap& map = impl_.alternate_protocol_map();
343 net::AlternateProtocolMap::const_iterator it = map.begin();
344 EXPECT_TRUE(it->first.Equals(test_host_port_pair2));
345 EXPECT_EQ(1234, it->second.port);
346 EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
348 // GetAlternateProtocol should reoder the AlternateProtocol map.
349 AlternateProtocolInfo alternate =
350 impl_.GetAlternateProtocol(test_host_port_pair1);
351 EXPECT_EQ(443, alternate.port);
352 EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
353 it = map.begin();
354 EXPECT_TRUE(it->first.Equals(test_host_port_pair1));
355 EXPECT_EQ(443, it->second.port);
356 EXPECT_EQ(NPN_SPDY_3, it->second.protocol);
359 TEST_F(AlternateProtocolServerPropertiesTest, SetBroken) {
360 HostPortPair test_host_port_pair("foo", 80);
361 impl_.SetBrokenAlternateProtocol(test_host_port_pair);
362 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
363 AlternateProtocolInfo alternate =
364 impl_.GetAlternateProtocol(test_host_port_pair);
365 EXPECT_EQ(ALTERNATE_PROTOCOL_BROKEN, alternate.protocol);
367 impl_.SetAlternateProtocol(
368 test_host_port_pair,
369 1234,
370 NPN_SPDY_3,
372 alternate = impl_.GetAlternateProtocol(test_host_port_pair);
373 EXPECT_EQ(ALTERNATE_PROTOCOL_BROKEN, alternate.protocol)
374 << "Second attempt should be ignored.";
377 TEST_F(AlternateProtocolServerPropertiesTest, ClearBroken) {
378 HostPortPair test_host_port_pair("foo", 80);
379 impl_.SetBrokenAlternateProtocol(test_host_port_pair);
380 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
381 AlternateProtocolInfo alternate =
382 impl_.GetAlternateProtocol(test_host_port_pair);
383 EXPECT_EQ(ALTERNATE_PROTOCOL_BROKEN, alternate.protocol);
384 impl_.ClearAlternateProtocol(test_host_port_pair);
385 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
388 TEST_F(AlternateProtocolServerPropertiesTest, Forced) {
389 // Test forced alternate protocols.
391 AlternateProtocolInfo default_protocol(1234, NPN_SPDY_3, 1);
392 HttpServerPropertiesImpl::ForceAlternateProtocol(default_protocol);
394 // Verify the forced protocol.
395 HostPortPair test_host_port_pair("foo", 80);
396 EXPECT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
397 AlternateProtocolInfo alternate =
398 impl_.GetAlternateProtocol(test_host_port_pair);
399 EXPECT_EQ(default_protocol.port, alternate.port);
400 EXPECT_EQ(default_protocol.protocol, alternate.protocol);
402 // Verify the real protocol overrides the forced protocol.
403 impl_.SetAlternateProtocol(test_host_port_pair, 443, NPN_SPDY_3, 1);
404 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
405 alternate = impl_.GetAlternateProtocol(test_host_port_pair);
406 EXPECT_EQ(443, alternate.port);
407 EXPECT_EQ(NPN_SPDY_3, alternate.protocol);
409 // Turn off the static, forced alternate protocol so that tests don't
410 // have this state.
411 HttpServerPropertiesImpl::DisableForcedAlternateProtocol();
413 // Verify the forced protocol is off.
414 HostPortPair test_host_port_pair2("bar", 80);
415 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair2));
418 TEST_F(AlternateProtocolServerPropertiesTest, Canonical) {
419 HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
420 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
422 HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
423 EXPECT_FALSE(impl_.HasAlternateProtocol(canonical_port_pair));
425 AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
427 impl_.SetAlternateProtocol(canonical_port_pair,
428 canonical_protocol.port,
429 canonical_protocol.protocol,
431 // Verify the forced protocol.
432 ASSERT_TRUE(impl_.HasAlternateProtocol(test_host_port_pair));
433 AlternateProtocolInfo alternate =
434 impl_.GetAlternateProtocol(test_host_port_pair);
435 EXPECT_EQ(canonical_protocol.port, alternate.port);
436 EXPECT_EQ(canonical_protocol.protocol, alternate.protocol);
438 // Verify the canonical suffix.
439 EXPECT_EQ(".c.youtube.com", impl_.GetCanonicalSuffix(test_host_port_pair));
440 EXPECT_EQ(".c.youtube.com", impl_.GetCanonicalSuffix(canonical_port_pair));
443 TEST_F(AlternateProtocolServerPropertiesTest, ClearCanonical) {
444 HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
445 HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
447 AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
449 impl_.SetAlternateProtocol(canonical_port_pair,
450 canonical_protocol.port,
451 canonical_protocol.protocol,
452 canonical_protocol.probability);
454 impl_.ClearAlternateProtocol(canonical_port_pair);
455 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
458 TEST_F(AlternateProtocolServerPropertiesTest, CanonicalBroken) {
459 HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
460 HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
462 AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
464 impl_.SetAlternateProtocol(canonical_port_pair,
465 canonical_protocol.port,
466 canonical_protocol.protocol,
467 canonical_protocol.probability);
469 impl_.SetBrokenAlternateProtocol(canonical_port_pair);
470 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
473 TEST_F(AlternateProtocolServerPropertiesTest, ClearWithCanonical) {
474 HostPortPair test_host_port_pair("foo.c.youtube.com", 80);
475 HostPortPair canonical_port_pair("bar.c.youtube.com", 80);
477 AlternateProtocolInfo canonical_protocol(1234, QUIC, 1);
479 impl_.SetAlternateProtocol(canonical_port_pair,
480 canonical_protocol.port,
481 canonical_protocol.protocol,
482 canonical_protocol.probability);
484 impl_.Clear();
485 EXPECT_FALSE(impl_.HasAlternateProtocol(test_host_port_pair));
488 typedef HttpServerPropertiesImplTest SpdySettingsServerPropertiesTest;
490 TEST_F(SpdySettingsServerPropertiesTest, Initialize) {
491 HostPortPair spdy_server_google("www.google.com", 443);
493 // Check by initializing empty spdy settings.
494 SpdySettingsMap spdy_settings_map(SpdySettingsMap::NO_AUTO_EVICT);
495 impl_.InitializeSpdySettingsServers(&spdy_settings_map);
496 EXPECT_TRUE(impl_.GetSpdySettings(spdy_server_google).empty());
498 // Check by initializing with www.google.com:443 spdy server settings.
499 SettingsMap settings_map;
500 const SpdySettingsIds id = SETTINGS_UPLOAD_BANDWIDTH;
501 const SpdySettingsFlags flags = SETTINGS_FLAG_PERSISTED;
502 const uint32 value = 31337;
503 SettingsFlagsAndValue flags_and_value(flags, value);
504 settings_map[id] = flags_and_value;
505 spdy_settings_map.Put(spdy_server_google, settings_map);
506 impl_.InitializeSpdySettingsServers(&spdy_settings_map);
508 const SettingsMap& settings_map2 = impl_.GetSpdySettings(spdy_server_google);
509 ASSERT_EQ(1U, settings_map2.size());
510 SettingsMap::const_iterator it = settings_map2.find(id);
511 EXPECT_TRUE(it != settings_map2.end());
512 SettingsFlagsAndValue flags_and_value2 = it->second;
513 EXPECT_EQ(flags, flags_and_value2.first);
514 EXPECT_EQ(value, flags_and_value2.second);
517 TEST_F(SpdySettingsServerPropertiesTest, SetSpdySetting) {
518 HostPortPair spdy_server_empty(std::string(), 443);
519 const SettingsMap& settings_map0 = impl_.GetSpdySettings(spdy_server_empty);
520 EXPECT_EQ(0U, settings_map0.size()); // Returns kEmptySettingsMap.
522 // Add www.google.com:443 as persisting.
523 HostPortPair spdy_server_google("www.google.com", 443);
524 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
525 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
526 const uint32 value1 = 31337;
527 EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_google, id1, flags1, value1));
528 // Check the values.
529 const SettingsMap& settings_map1_ret =
530 impl_.GetSpdySettings(spdy_server_google);
531 ASSERT_EQ(1U, settings_map1_ret.size());
532 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
533 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
534 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
535 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
536 EXPECT_EQ(value1, flags_and_value1_ret.second);
538 // Add mail.google.com:443 as not persisting.
539 HostPortPair spdy_server_mail("mail.google.com", 443);
540 const SpdySettingsIds id2 = SETTINGS_DOWNLOAD_BANDWIDTH;
541 const SpdySettingsFlags flags2 = SETTINGS_FLAG_NONE;
542 const uint32 value2 = 62667;
543 EXPECT_FALSE(impl_.SetSpdySetting(spdy_server_mail, id2, flags2, value2));
544 const SettingsMap& settings_map2_ret =
545 impl_.GetSpdySettings(spdy_server_mail);
546 EXPECT_EQ(0U, settings_map2_ret.size()); // Returns kEmptySettingsMap.
548 // Add docs.google.com:443 as persisting
549 HostPortPair spdy_server_docs("docs.google.com", 443);
550 const SpdySettingsIds id3 = SETTINGS_ROUND_TRIP_TIME;
551 const SpdySettingsFlags flags3 = SETTINGS_FLAG_PLEASE_PERSIST;
552 const uint32 value3 = 93997;
553 SettingsFlagsAndValue flags_and_value3(flags3, value3);
554 EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id3, flags3, value3));
555 // Check the values.
556 const SettingsMap& settings_map3_ret =
557 impl_.GetSpdySettings(spdy_server_docs);
558 ASSERT_EQ(1U, settings_map3_ret.size());
559 SettingsMap::const_iterator it3_ret = settings_map3_ret.find(id3);
560 EXPECT_TRUE(it3_ret != settings_map3_ret.end());
561 SettingsFlagsAndValue flags_and_value3_ret = it3_ret->second;
562 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value3_ret.first);
563 EXPECT_EQ(value3, flags_and_value3_ret.second);
565 // Check data for www.google.com:443 (id1).
566 const SettingsMap& settings_map4_ret =
567 impl_.GetSpdySettings(spdy_server_google);
568 ASSERT_EQ(1U, settings_map4_ret.size());
569 SettingsMap::const_iterator it4_ret = settings_map4_ret.find(id1);
570 EXPECT_TRUE(it4_ret != settings_map4_ret.end());
571 SettingsFlagsAndValue flags_and_value4_ret = it4_ret->second;
572 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value4_ret.first);
573 EXPECT_EQ(value1, flags_and_value1_ret.second);
575 // Clear www.google.com:443 as persisting.
576 impl_.ClearSpdySettings(spdy_server_google);
577 // Check the values.
578 const SettingsMap& settings_map5_ret =
579 impl_.GetSpdySettings(spdy_server_google);
580 ASSERT_EQ(0U, settings_map5_ret.size());
582 // Clear all settings.
583 ASSERT_GT(impl_.spdy_settings_map().size(), 0U);
584 impl_.ClearAllSpdySettings();
585 ASSERT_EQ(0U, impl_.spdy_settings_map().size());
588 TEST_F(SpdySettingsServerPropertiesTest, Clear) {
589 // Add www.google.com:443 as persisting.
590 HostPortPair spdy_server_google("www.google.com", 443);
591 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
592 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
593 const uint32 value1 = 31337;
594 EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_google, id1, flags1, value1));
595 // Check the values.
596 const SettingsMap& settings_map1_ret =
597 impl_.GetSpdySettings(spdy_server_google);
598 ASSERT_EQ(1U, settings_map1_ret.size());
599 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
600 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
601 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
602 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
603 EXPECT_EQ(value1, flags_and_value1_ret.second);
605 // Add docs.google.com:443 as persisting
606 HostPortPair spdy_server_docs("docs.google.com", 443);
607 const SpdySettingsIds id3 = SETTINGS_ROUND_TRIP_TIME;
608 const SpdySettingsFlags flags3 = SETTINGS_FLAG_PLEASE_PERSIST;
609 const uint32 value3 = 93997;
610 EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id3, flags3, value3));
611 // Check the values.
612 const SettingsMap& settings_map3_ret =
613 impl_.GetSpdySettings(spdy_server_docs);
614 ASSERT_EQ(1U, settings_map3_ret.size());
615 SettingsMap::const_iterator it3_ret = settings_map3_ret.find(id3);
616 EXPECT_TRUE(it3_ret != settings_map3_ret.end());
617 SettingsFlagsAndValue flags_and_value3_ret = it3_ret->second;
618 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value3_ret.first);
619 EXPECT_EQ(value3, flags_and_value3_ret.second);
621 impl_.Clear();
622 EXPECT_EQ(0U, impl_.GetSpdySettings(spdy_server_google).size());
623 EXPECT_EQ(0U, impl_.GetSpdySettings(spdy_server_docs).size());
626 TEST_F(SpdySettingsServerPropertiesTest, MRUOfGetSpdySettings) {
627 // Add www.google.com:443 as persisting.
628 HostPortPair spdy_server_google("www.google.com", 443);
629 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
630 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
631 const uint32 value1 = 31337;
632 EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_google, id1, flags1, value1));
634 // Add docs.google.com:443 as persisting
635 HostPortPair spdy_server_docs("docs.google.com", 443);
636 const SpdySettingsIds id2 = SETTINGS_ROUND_TRIP_TIME;
637 const SpdySettingsFlags flags2 = SETTINGS_FLAG_PLEASE_PERSIST;
638 const uint32 value2 = 93997;
639 EXPECT_TRUE(impl_.SetSpdySetting(spdy_server_docs, id2, flags2, value2));
641 // Verify the first element is docs.google.com:443.
642 const net::SpdySettingsMap& map = impl_.spdy_settings_map();
643 net::SpdySettingsMap::const_iterator it = map.begin();
644 EXPECT_TRUE(it->first.Equals(spdy_server_docs));
645 const SettingsMap& settings_map2_ret = it->second;
646 ASSERT_EQ(1U, settings_map2_ret.size());
647 SettingsMap::const_iterator it2_ret = settings_map2_ret.find(id2);
648 EXPECT_TRUE(it2_ret != settings_map2_ret.end());
649 SettingsFlagsAndValue flags_and_value2_ret = it2_ret->second;
650 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value2_ret.first);
651 EXPECT_EQ(value2, flags_and_value2_ret.second);
653 // GetSpdySettings should reoder the SpdySettingsMap.
654 const SettingsMap& settings_map1_ret =
655 impl_.GetSpdySettings(spdy_server_google);
656 ASSERT_EQ(1U, settings_map1_ret.size());
657 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
658 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
659 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
660 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
661 EXPECT_EQ(value1, flags_and_value1_ret.second);
663 // Check the first entry is spdy_server_google by accessing it via iterator.
664 it = map.begin();
665 EXPECT_TRUE(it->first.Equals(spdy_server_google));
666 const SettingsMap& settings_map1_it_ret = it->second;
667 ASSERT_EQ(1U, settings_map1_it_ret.size());
668 it1_ret = settings_map1_it_ret.find(id1);
669 EXPECT_TRUE(it1_ret != settings_map1_it_ret.end());
670 flags_and_value1_ret = it1_ret->second;
671 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
672 EXPECT_EQ(value1, flags_and_value1_ret.second);
675 } // namespace
677 } // namespace net