1 // Copyright 2014 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_manager.h"
7 #include "base/basictypes.h"
8 #include "base/json/json_writer.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/testing_pref_service.h"
12 #include "base/run_loop.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/test/test_simple_task_runner.h"
16 #include "base/values.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
25 using base::StringPrintf
;
27 using ::testing::Invoke
;
28 using ::testing::Mock
;
29 using ::testing::StrictMock
;
31 const char kTestHttpServerProperties
[] = "TestHttpServerProperties";
33 class TestingHttpServerPropertiesManager
: public HttpServerPropertiesManager
{
35 TestingHttpServerPropertiesManager(
36 PrefService
* pref_service
,
37 const char* pref_path
,
38 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
)
39 : HttpServerPropertiesManager(pref_service
, pref_path
, io_task_runner
) {
40 InitializeOnNetworkThread();
43 virtual ~TestingHttpServerPropertiesManager() {}
45 // Make these methods public for testing.
46 using HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread
;
47 using HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread
;
49 // Post tasks without a delay during tests.
50 virtual void StartPrefsUpdateTimerOnNetworkThread(
51 base::TimeDelta delay
) override
{
52 HttpServerPropertiesManager::StartPrefsUpdateTimerOnNetworkThread(
56 void UpdateCacheFromPrefsOnUIConcrete() {
57 HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread();
60 // Post tasks without a delay during tests.
61 virtual void StartCacheUpdateTimerOnPrefThread(
62 base::TimeDelta delay
) override
{
63 HttpServerPropertiesManager::StartCacheUpdateTimerOnPrefThread(
67 void UpdatePrefsFromCacheOnNetworkThreadConcrete(
68 const base::Closure
& callback
) {
69 HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread(callback
);
72 MOCK_METHOD0(UpdateCacheFromPrefsOnPrefThread
, void());
73 MOCK_METHOD1(UpdatePrefsFromCacheOnNetworkThread
, void(const base::Closure
&));
74 MOCK_METHOD6(UpdateCacheFromPrefsOnNetworkThread
,
75 void(std::vector
<std::string
>* spdy_servers
,
76 SpdySettingsMap
* spdy_settings_map
,
77 AlternativeServiceMap
* alternative_service_map
,
78 IPAddressNumber
* last_quic_address
,
79 ServerNetworkStatsMap
* server_network_stats_map
,
80 bool detected_corrupted_prefs
));
81 MOCK_METHOD5(UpdatePrefsOnPref
,
82 void(base::ListValue
* spdy_server_list
,
83 SpdySettingsMap
* spdy_settings_map
,
84 AlternativeServiceMap
* alternative_service_map
,
85 IPAddressNumber
* last_quic_address
,
86 ServerNetworkStatsMap
* server_network_stats_map
));
89 DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager
);
92 class HttpServerPropertiesManagerTest
: public testing::Test
{
94 HttpServerPropertiesManagerTest() {}
96 void SetUp() override
{
97 pref_service_
.registry()->RegisterDictionaryPref(kTestHttpServerProperties
);
98 http_server_props_manager_
.reset(
99 new StrictMock
<TestingHttpServerPropertiesManager
>(
101 kTestHttpServerProperties
,
102 base::MessageLoop::current()->message_loop_proxy()));
104 base::RunLoop().RunUntilIdle();
107 void TearDown() override
{
108 if (http_server_props_manager_
.get())
109 http_server_props_manager_
->ShutdownOnPrefThread();
110 base::RunLoop().RunUntilIdle();
111 http_server_props_manager_
.reset();
114 void ExpectCacheUpdate() {
115 EXPECT_CALL(*http_server_props_manager_
, UpdateCacheFromPrefsOnPrefThread())
116 .WillOnce(Invoke(http_server_props_manager_
.get(),
117 &TestingHttpServerPropertiesManager::
118 UpdateCacheFromPrefsOnUIConcrete
));
121 void ExpectPrefsUpdate() {
122 EXPECT_CALL(*http_server_props_manager_
,
123 UpdatePrefsFromCacheOnNetworkThread(_
))
124 .WillOnce(Invoke(http_server_props_manager_
.get(),
125 &TestingHttpServerPropertiesManager::
126 UpdatePrefsFromCacheOnNetworkThreadConcrete
));
129 void ExpectPrefsUpdateRepeatedly() {
130 EXPECT_CALL(*http_server_props_manager_
,
131 UpdatePrefsFromCacheOnNetworkThread(_
))
133 Invoke(http_server_props_manager_
.get(),
134 &TestingHttpServerPropertiesManager::
135 UpdatePrefsFromCacheOnNetworkThreadConcrete
));
138 bool HasAlternativeService(const HostPortPair
& server
) {
139 const AlternativeService alternative_service
=
140 http_server_props_manager_
->GetAlternativeService(server
);
141 return alternative_service
.protocol
!= UNINITIALIZED_ALTERNATE_PROTOCOL
;
144 //base::RunLoop loop_;
145 TestingPrefServiceSimple pref_service_
;
146 scoped_ptr
<TestingHttpServerPropertiesManager
> http_server_props_manager_
;
149 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest
);
152 TEST_F(HttpServerPropertiesManagerTest
,
153 SingleUpdateForTwoSpdyServerPrefChanges
) {
156 // Set up the prefs for www.google.com:80 and mail.google.com:80 and then set
157 // it twice. Only expect a single cache update.
159 base::DictionaryValue
* server_pref_dict
= new base::DictionaryValue
;
160 HostPortPair
google_server("www.google.com", 80);
161 HostPortPair
mail_server("mail.google.com", 80);
163 // Set supports_spdy for www.google.com:80.
164 server_pref_dict
->SetBoolean("supports_spdy", true);
166 // Set up alternative_service for www.google.com:80.
167 base::DictionaryValue
* alternative_service_dict
= new base::DictionaryValue
;
168 alternative_service_dict
->SetString("protocol_str", "npn-spdy/3");
169 alternative_service_dict
->SetString("host", "maps.google.com");
170 alternative_service_dict
->SetInteger("port", 443);
171 base::ListValue
* alternative_service_list
= new base::ListValue
;
172 alternative_service_list
->Append(alternative_service_dict
);
173 server_pref_dict
->SetWithoutPathExpansion("alternative_service",
174 alternative_service_list
);
176 // Set up ServerNetworkStats for www.google.com:80.
177 base::DictionaryValue
* stats
= new base::DictionaryValue
;
178 stats
->SetInteger("srtt", 10);
179 server_pref_dict
->SetWithoutPathExpansion("network_stats", stats
);
181 // Set the server preference for www.google.com:80.
182 base::DictionaryValue
* servers_dict
= new base::DictionaryValue
;
183 servers_dict
->SetWithoutPathExpansion("www.google.com:80", server_pref_dict
);
185 // Set the preference for mail.google.com server.
186 base::DictionaryValue
* server_pref_dict1
= new base::DictionaryValue
;
188 // Set supports_spdy for mail.google.com:80
189 server_pref_dict1
->SetBoolean("supports_spdy", true);
191 // Set up alternate_protocol for mail.google.com:80 to test migration to
192 // alternative-service.
193 base::DictionaryValue
* alternate_protocol_dict
= new base::DictionaryValue
;
194 alternate_protocol_dict
->SetString("protocol_str", "npn-spdy/3.1");
195 alternate_protocol_dict
->SetInteger("port", 444);
196 server_pref_dict1
->SetWithoutPathExpansion("alternate_protocol",
197 alternate_protocol_dict
);
199 // Set up ServerNetworkStats for mail.google.com:80.
200 base::DictionaryValue
* stats1
= new base::DictionaryValue
;
201 stats1
->SetInteger("srtt", 20);
202 server_pref_dict1
->SetWithoutPathExpansion("network_stats", stats1
);
203 // Set the server preference for mail.google.com:80.
204 servers_dict
->SetWithoutPathExpansion("mail.google.com:80",
207 base::DictionaryValue
* http_server_properties_dict
=
208 new base::DictionaryValue
;
209 HttpServerPropertiesManager::SetVersion(http_server_properties_dict
, -1);
210 http_server_properties_dict
->SetWithoutPathExpansion("servers", servers_dict
);
211 base::DictionaryValue
* supports_quic
= new base::DictionaryValue
;
212 supports_quic
->SetBoolean("used_quic", true);
213 supports_quic
->SetString("address", "127.0.0.1");
214 http_server_properties_dict
->SetWithoutPathExpansion("supports_quic",
217 // Set the same value for kHttpServerProperties multiple times.
218 pref_service_
.SetManagedPref(kTestHttpServerProperties
,
219 http_server_properties_dict
);
220 base::DictionaryValue
* http_server_properties_dict2
=
221 http_server_properties_dict
->DeepCopy();
222 pref_service_
.SetManagedPref(kTestHttpServerProperties
,
223 http_server_properties_dict2
);
225 base::RunLoop().RunUntilIdle();
226 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
228 // Verify SupportsSpdy.
230 http_server_props_manager_
->SupportsRequestPriority(google_server
));
231 EXPECT_TRUE(http_server_props_manager_
->SupportsRequestPriority(mail_server
));
232 EXPECT_FALSE(http_server_props_manager_
->SupportsRequestPriority(
233 HostPortPair::FromString("foo.google.com:1337")));
235 // Verify alternative service.
236 AlternativeService alternative_service
=
237 http_server_props_manager_
->GetAlternativeService(google_server
);
238 EXPECT_EQ(NPN_SPDY_3
, alternative_service
.protocol
);
239 EXPECT_EQ("maps.google.com", alternative_service
.host
);
240 EXPECT_EQ(443, alternative_service
.port
);
241 alternative_service
=
242 http_server_props_manager_
->GetAlternativeService(mail_server
);
243 EXPECT_EQ(NPN_SPDY_3_1
, alternative_service
.protocol
);
244 EXPECT_EQ("mail.google.com", alternative_service
.host
);
245 EXPECT_EQ(444, alternative_service
.port
);
247 // Verify SupportsQuic.
248 IPAddressNumber last_address
;
249 EXPECT_TRUE(http_server_props_manager_
->GetSupportsQuic(&last_address
));
250 EXPECT_EQ("127.0.0.1", IPAddressToString(last_address
));
252 // Verify ServerNetworkStats.
253 const ServerNetworkStats
* stats2
=
254 http_server_props_manager_
->GetServerNetworkStats(google_server
);
255 EXPECT_EQ(10, stats2
->srtt
.ToInternalValue());
256 const ServerNetworkStats
* stats3
=
257 http_server_props_manager_
->GetServerNetworkStats(mail_server
);
258 EXPECT_EQ(20, stats3
->srtt
.ToInternalValue());
261 TEST_F(HttpServerPropertiesManagerTest
, BadCachedHostPortPair
) {
263 // The prefs are automaticalls updated in the case corruption is detected.
266 base::DictionaryValue
* server_pref_dict
= new base::DictionaryValue
;
268 // Set supports_spdy for www.google.com:65536.
269 server_pref_dict
->SetBoolean("supports_spdy", true);
271 // Set up alternative_service for www.google.com:65536.
273 base::DictionaryValue
* alternative_service_dict
= new base::DictionaryValue
;
274 alternative_service_dict
->SetString("protocol_str", "npn-spdy/3");
275 alternative_service_dict
->SetInteger("port", 80);
276 base::ListValue
* alternative_service_list
= new base::ListValue
;
277 alternative_service_list
->Append(alternative_service_dict
);
278 server_pref_dict
->SetWithoutPathExpansion("alternative_service",
279 alternative_service_list
);
281 // Set up ServerNetworkStats for www.google.com:65536.
282 base::DictionaryValue
* stats
= new base::DictionaryValue
;
283 stats
->SetInteger("srtt", 10);
284 server_pref_dict
->SetWithoutPathExpansion("network_stats", stats
);
286 // Set the server preference for www.google.com:65536.
287 base::DictionaryValue
* servers_dict
= new base::DictionaryValue
;
288 servers_dict
->SetWithoutPathExpansion("www.google.com:65536",
291 base::DictionaryValue
* http_server_properties_dict
=
292 new base::DictionaryValue
;
293 HttpServerPropertiesManager::SetVersion(http_server_properties_dict
, -1);
294 http_server_properties_dict
->SetWithoutPathExpansion("servers", servers_dict
);
297 pref_service_
.SetManagedPref(kTestHttpServerProperties
,
298 http_server_properties_dict
);
300 base::RunLoop().RunUntilIdle();
301 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
303 // Verify that nothing is set.
304 EXPECT_FALSE(http_server_props_manager_
->SupportsRequestPriority(
305 HostPortPair::FromString("www.google.com:65536")));
307 HasAlternativeService(HostPortPair::FromString("www.google.com:65536")));
308 const ServerNetworkStats
* stats1
=
309 http_server_props_manager_
->GetServerNetworkStats(
310 HostPortPair::FromString("www.google.com:65536"));
311 EXPECT_EQ(NULL
, stats1
);
314 TEST_F(HttpServerPropertiesManagerTest
, BadCachedAltProtocolPort
) {
316 // The prefs are automaticalls updated in the case corruption is detected.
319 base::DictionaryValue
* server_pref_dict
= new base::DictionaryValue
;
321 // Set supports_spdy for www.google.com:80.
322 server_pref_dict
->SetBoolean("supports_spdy", true);
324 // Set up alternative_service for www.google.com:80.
325 base::DictionaryValue
* alternative_service_dict
= new base::DictionaryValue
;
326 alternative_service_dict
->SetString("protocol_str", "npn-spdy/3");
327 alternative_service_dict
->SetInteger("port", 65536);
328 base::ListValue
* alternative_service_list
= new base::ListValue
;
329 alternative_service_list
->Append(alternative_service_dict
);
330 server_pref_dict
->SetWithoutPathExpansion("alternative_service",
331 alternative_service_list
);
333 // Set the server preference for www.google.com:80.
334 base::DictionaryValue
* servers_dict
= new base::DictionaryValue
;
335 servers_dict
->SetWithoutPathExpansion("www.google.com:80", server_pref_dict
);
337 base::DictionaryValue
* http_server_properties_dict
=
338 new base::DictionaryValue
;
339 HttpServerPropertiesManager::SetVersion(http_server_properties_dict
, -1);
340 http_server_properties_dict
->SetWithoutPathExpansion("servers", servers_dict
);
343 pref_service_
.SetManagedPref(kTestHttpServerProperties
,
344 http_server_properties_dict
);
346 base::RunLoop().RunUntilIdle();
347 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
349 // Verify alternative service is not set.
351 HasAlternativeService(HostPortPair::FromString("www.google.com:80")));
354 TEST_F(HttpServerPropertiesManagerTest
, SupportsSpdy
) {
357 // Post an update task to the network thread. SetSupportsSpdy calls
358 // ScheduleUpdatePrefsOnNetworkThread.
360 // Add mail.google.com:443 as a supporting spdy server.
361 HostPortPair
spdy_server_mail("mail.google.com", 443);
363 http_server_props_manager_
->SupportsRequestPriority(spdy_server_mail
));
364 http_server_props_manager_
->SetSupportsSpdy(spdy_server_mail
, true);
367 base::RunLoop().RunUntilIdle();
370 http_server_props_manager_
->SupportsRequestPriority(spdy_server_mail
));
371 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
374 TEST_F(HttpServerPropertiesManagerTest
, SetSpdySetting
) {
377 // Add SpdySetting for mail.google.com:443.
378 HostPortPair
spdy_server_mail("mail.google.com", 443);
379 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
380 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
381 const uint32 value1
= 31337;
382 http_server_props_manager_
->SetSpdySetting(
383 spdy_server_mail
, id1
, flags1
, value1
);
386 base::RunLoop().RunUntilIdle();
388 const SettingsMap
& settings_map1_ret
=
389 http_server_props_manager_
->GetSpdySettings(spdy_server_mail
);
390 ASSERT_EQ(1U, settings_map1_ret
.size());
391 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
392 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
393 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
394 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
395 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
397 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
400 TEST_F(HttpServerPropertiesManagerTest
, ClearSpdySetting
) {
401 ExpectPrefsUpdateRepeatedly();
403 // Add SpdySetting for mail.google.com:443.
404 HostPortPair
spdy_server_mail("mail.google.com", 443);
405 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
406 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
407 const uint32 value1
= 31337;
408 http_server_props_manager_
->SetSpdySetting(
409 spdy_server_mail
, id1
, flags1
, value1
);
412 base::RunLoop().RunUntilIdle();
414 const SettingsMap
& settings_map1_ret
=
415 http_server_props_manager_
->GetSpdySettings(spdy_server_mail
);
416 ASSERT_EQ(1U, settings_map1_ret
.size());
417 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
418 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
419 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
420 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
421 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
423 // Clear SpdySetting for mail.google.com:443.
424 http_server_props_manager_
->ClearSpdySettings(spdy_server_mail
);
427 base::RunLoop().RunUntilIdle();
429 // Verify that there are no entries in the settings map for
430 // mail.google.com:443.
431 const SettingsMap
& settings_map2_ret
=
432 http_server_props_manager_
->GetSpdySettings(spdy_server_mail
);
433 ASSERT_EQ(0U, settings_map2_ret
.size());
435 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
438 TEST_F(HttpServerPropertiesManagerTest
, ClearAllSpdySetting
) {
439 ExpectPrefsUpdateRepeatedly();
441 // Add SpdySetting for mail.google.com:443.
442 HostPortPair
spdy_server_mail("mail.google.com", 443);
443 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
444 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
445 const uint32 value1
= 31337;
446 http_server_props_manager_
->SetSpdySetting(
447 spdy_server_mail
, id1
, flags1
, value1
);
450 base::RunLoop().RunUntilIdle();
452 const SettingsMap
& settings_map1_ret
=
453 http_server_props_manager_
->GetSpdySettings(spdy_server_mail
);
454 ASSERT_EQ(1U, settings_map1_ret
.size());
455 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
456 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
457 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
458 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
459 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
461 // Clear All SpdySettings.
462 http_server_props_manager_
->ClearAllSpdySettings();
465 base::RunLoop().RunUntilIdle();
467 // Verify that there are no entries in the settings map.
468 const SpdySettingsMap
& spdy_settings_map2_ret
=
469 http_server_props_manager_
->spdy_settings_map();
470 ASSERT_EQ(0U, spdy_settings_map2_ret
.size());
472 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
475 TEST_F(HttpServerPropertiesManagerTest
, GetAlternativeService
) {
478 HostPortPair
spdy_server_mail("mail.google.com", 80);
479 EXPECT_FALSE(HasAlternativeService(spdy_server_mail
));
480 AlternativeService
alternative_service(NPN_SPDY_3
, "mail.google.com", 443);
481 http_server_props_manager_
->SetAlternativeService(spdy_server_mail
,
482 alternative_service
, 1.0);
485 base::RunLoop().RunUntilIdle();
486 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
488 alternative_service
=
489 http_server_props_manager_
->GetAlternativeService(spdy_server_mail
);
490 EXPECT_EQ(443, alternative_service
.port
);
491 EXPECT_EQ(NPN_SPDY_3
, alternative_service
.protocol
);
494 TEST_F(HttpServerPropertiesManagerTest
, SupportsQuic
) {
497 IPAddressNumber address
;
498 EXPECT_FALSE(http_server_props_manager_
->GetSupportsQuic(&address
));
500 IPAddressNumber actual_address
;
501 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address
));
502 http_server_props_manager_
->SetSupportsQuic(true, actual_address
);
505 base::RunLoop().RunUntilIdle();
506 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
508 EXPECT_TRUE(http_server_props_manager_
->GetSupportsQuic(&address
));
509 EXPECT_EQ(actual_address
, address
);
512 TEST_F(HttpServerPropertiesManagerTest
, ServerNetworkStats
) {
515 HostPortPair
mail_server("mail.google.com", 80);
516 const ServerNetworkStats
* stats
=
517 http_server_props_manager_
->GetServerNetworkStats(mail_server
);
518 EXPECT_EQ(NULL
, stats
);
519 ServerNetworkStats stats1
;
520 stats1
.srtt
= base::TimeDelta::FromMicroseconds(10);
521 http_server_props_manager_
->SetServerNetworkStats(mail_server
, stats1
);
524 base::RunLoop().RunUntilIdle();
525 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
527 const ServerNetworkStats
* stats2
=
528 http_server_props_manager_
->GetServerNetworkStats(mail_server
);
529 EXPECT_EQ(10, stats2
->srtt
.ToInternalValue());
532 TEST_F(HttpServerPropertiesManagerTest
, Clear
) {
535 HostPortPair
spdy_server_mail("mail.google.com", 443);
536 http_server_props_manager_
->SetSupportsSpdy(spdy_server_mail
, true);
537 AlternativeService
alternative_service(NPN_SPDY_3
, "mail.google.com", 443);
538 http_server_props_manager_
->SetAlternativeService(spdy_server_mail
,
539 alternative_service
, 1.0);
540 IPAddressNumber actual_address
;
541 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address
));
542 http_server_props_manager_
->SetSupportsQuic(true, actual_address
);
543 ServerNetworkStats stats
;
544 stats
.srtt
= base::TimeDelta::FromMicroseconds(10);
545 http_server_props_manager_
->SetServerNetworkStats(spdy_server_mail
, stats
);
547 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
548 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
549 const uint32 value1
= 31337;
550 http_server_props_manager_
->SetSpdySetting(
551 spdy_server_mail
, id1
, flags1
, value1
);
554 base::RunLoop().RunUntilIdle();
557 http_server_props_manager_
->SupportsRequestPriority(spdy_server_mail
));
558 EXPECT_TRUE(HasAlternativeService(spdy_server_mail
));
559 IPAddressNumber address
;
560 EXPECT_TRUE(http_server_props_manager_
->GetSupportsQuic(&address
));
561 EXPECT_EQ(actual_address
, address
);
562 const ServerNetworkStats
* stats1
=
563 http_server_props_manager_
->GetServerNetworkStats(spdy_server_mail
);
564 EXPECT_EQ(10, stats1
->srtt
.ToInternalValue());
566 // Check SPDY settings values.
567 const SettingsMap
& settings_map1_ret
=
568 http_server_props_manager_
->GetSpdySettings(spdy_server_mail
);
569 ASSERT_EQ(1U, settings_map1_ret
.size());
570 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
571 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
572 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
573 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
574 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
576 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
580 // Clear http server data, time out if we do not get a completion callback.
581 http_server_props_manager_
->Clear(base::MessageLoop::QuitClosure());
582 base::RunLoop().Run();
585 http_server_props_manager_
->SupportsRequestPriority(spdy_server_mail
));
586 EXPECT_FALSE(HasAlternativeService(spdy_server_mail
));
587 EXPECT_FALSE(http_server_props_manager_
->GetSupportsQuic(&address
));
588 const ServerNetworkStats
* stats2
=
589 http_server_props_manager_
->GetServerNetworkStats(spdy_server_mail
);
590 EXPECT_EQ(NULL
, stats2
);
592 const SettingsMap
& settings_map2_ret
=
593 http_server_props_manager_
->GetSpdySettings(spdy_server_mail
);
594 EXPECT_EQ(0U, settings_map2_ret
.size());
596 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
599 // https://crbug.com/444956: Add 200 alternative_service servers followed by
600 // supports_quic and verify we have read supports_quic from prefs.
601 TEST_F(HttpServerPropertiesManagerTest
, BadSupportsQuic
) {
604 base::DictionaryValue
* servers_dict
= new base::DictionaryValue
;
606 for (int i
= 0; i
< 200; ++i
) {
607 // Set up alternative_service for www.google.com:i.
608 base::DictionaryValue
* alternative_service_dict
= new base::DictionaryValue
;
609 alternative_service_dict
->SetString("protocol_str", "npn-spdy/3");
610 alternative_service_dict
->SetString("host", "");
611 alternative_service_dict
->SetInteger("port", i
);
612 base::ListValue
* alternative_service_list
= new base::ListValue
;
613 alternative_service_list
->Append(alternative_service_dict
);
614 base::DictionaryValue
* server_pref_dict
= new base::DictionaryValue
;
615 server_pref_dict
->SetWithoutPathExpansion("alternative_service",
616 alternative_service_list
);
617 servers_dict
->SetWithoutPathExpansion(StringPrintf("www.google.com:%d", i
),
621 // Set the preference for mail.google.com server.
622 base::DictionaryValue
* server_pref_dict1
= new base::DictionaryValue
;
624 // Set the server preference for mail.google.com:80.
625 servers_dict
->SetWithoutPathExpansion("mail.google.com:80",
628 base::DictionaryValue
* http_server_properties_dict
=
629 new base::DictionaryValue
;
630 HttpServerPropertiesManager::SetVersion(http_server_properties_dict
, -1);
631 http_server_properties_dict
->SetWithoutPathExpansion("servers", servers_dict
);
633 // Set up SupportsQuic for 127.0.0.1
634 base::DictionaryValue
* supports_quic
= new base::DictionaryValue
;
635 supports_quic
->SetBoolean("used_quic", true);
636 supports_quic
->SetString("address", "127.0.0.1");
637 http_server_properties_dict
->SetWithoutPathExpansion("supports_quic",
641 pref_service_
.SetManagedPref(kTestHttpServerProperties
,
642 http_server_properties_dict
);
644 base::RunLoop().RunUntilIdle();
645 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
647 // Verify alternative service.
648 for (int i
= 0; i
< 200; ++i
) {
649 std::string server
= StringPrintf("www.google.com:%d", i
);
650 AlternativeService alternative_service
=
651 http_server_props_manager_
->GetAlternativeService(
652 HostPortPair::FromString(server
));
653 EXPECT_EQ(i
, alternative_service
.port
);
654 EXPECT_EQ(NPN_SPDY_3
, alternative_service
.protocol
);
657 // Verify SupportsQuic.
658 IPAddressNumber address
;
659 ASSERT_TRUE(http_server_props_manager_
->GetSupportsQuic(&address
));
660 EXPECT_EQ("127.0.0.1", IPAddressToString(address
));
663 TEST_F(HttpServerPropertiesManagerTest
, UpdateCacheWithPrefs
) {
664 const HostPortPair
server_www("www.google.com", 80);
665 const HostPortPair
server_mail("mail.google.com", 80);
667 // Set alternate protocol.
668 AlternativeService
www_altsvc(NPN_SPDY_3
, "", 443);
669 AlternativeService
mail_altsvc(NPN_SPDY_3_1
, "mail.google.com", 444);
670 http_server_props_manager_
->SetAlternativeService(server_www
, www_altsvc
,
672 http_server_props_manager_
->SetAlternativeService(server_mail
, mail_altsvc
,
675 // Set ServerNetworkStats.
676 ServerNetworkStats stats
;
677 stats
.srtt
= base::TimeDelta::FromInternalValue(42);
678 http_server_props_manager_
->SetServerNetworkStats(server_mail
, stats
);
681 IPAddressNumber actual_address
;
682 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address
));
683 http_server_props_manager_
->SetSupportsQuic(true, actual_address
);
688 http_server_props_manager_
->ScheduleUpdateCacheOnPrefThread();
689 base::RunLoop().RunUntilIdle();
691 // Verify preferences.
692 const char expected_json
[] =
693 "{\"servers\":{\"mail.google.com:80\":{\"alternative_service\":[{"
695 "\"mail.google.com\",\"port\":444,\"probability\":0.2,\"protocol_str\":"
697 "3.1\"}],\"network_stats\":{\"srtt\":42}},\"www.google.com:80\":"
698 "{\"alternative_service\":[{\"port\":443,\"probability\":1.0,"
699 "\"protocol_str\":\"npn-spdy/3\"}]}},\"supports_quic\":"
700 "{\"address\":\"127.0.0.1\",\"used_quic\":true},\"version\":3}";
702 const base::Value
* http_server_properties
=
703 pref_service_
.GetUserPref(kTestHttpServerProperties
);
704 ASSERT_NE(nullptr, http_server_properties
);
705 std::string preferences_json
;
707 base::JSONWriter::Write(http_server_properties
, &preferences_json
));
708 EXPECT_EQ(expected_json
, preferences_json
);
711 TEST_F(HttpServerPropertiesManagerTest
, ShutdownWithPendingUpdateCache0
) {
712 // Post an update task to the UI thread.
713 http_server_props_manager_
->ScheduleUpdateCacheOnPrefThread();
714 // Shutdown comes before the task is executed.
715 http_server_props_manager_
->ShutdownOnPrefThread();
716 http_server_props_manager_
.reset();
717 // Run the task after shutdown and deletion.
718 base::RunLoop().RunUntilIdle();
721 TEST_F(HttpServerPropertiesManagerTest
, ShutdownWithPendingUpdateCache1
) {
722 // Post an update task.
723 http_server_props_manager_
->ScheduleUpdateCacheOnPrefThread();
724 // Shutdown comes before the task is executed.
725 http_server_props_manager_
->ShutdownOnPrefThread();
726 // Run the task after shutdown, but before deletion.
727 base::RunLoop().RunUntilIdle();
728 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
729 http_server_props_manager_
.reset();
730 base::RunLoop().RunUntilIdle();
733 TEST_F(HttpServerPropertiesManagerTest
, ShutdownWithPendingUpdateCache2
) {
734 http_server_props_manager_
->UpdateCacheFromPrefsOnUIConcrete();
735 // Shutdown comes before the task is executed.
736 http_server_props_manager_
->ShutdownOnPrefThread();
737 // Run the task after shutdown, but before deletion.
738 base::RunLoop().RunUntilIdle();
739 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
740 http_server_props_manager_
.reset();
741 base::RunLoop().RunUntilIdle();
745 // Tests for shutdown when updating prefs.
747 TEST_F(HttpServerPropertiesManagerTest
, ShutdownWithPendingUpdatePrefs0
) {
748 // Post an update task to the IO thread.
749 http_server_props_manager_
->ScheduleUpdatePrefsOnNetworkThread();
750 // Shutdown comes before the task is executed.
751 http_server_props_manager_
->ShutdownOnPrefThread();
752 http_server_props_manager_
.reset();
753 // Run the task after shutdown and deletion.
754 base::RunLoop().RunUntilIdle();
757 TEST_F(HttpServerPropertiesManagerTest
, ShutdownWithPendingUpdatePrefs1
) {
759 // Post an update task.
760 http_server_props_manager_
->ScheduleUpdatePrefsOnNetworkThread();
761 // Shutdown comes before the task is executed.
762 http_server_props_manager_
->ShutdownOnPrefThread();
763 // Run the task after shutdown, but before deletion.
764 base::RunLoop().RunUntilIdle();
765 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
766 http_server_props_manager_
.reset();
767 base::RunLoop().RunUntilIdle();
770 TEST_F(HttpServerPropertiesManagerTest
, ShutdownWithPendingUpdatePrefs2
) {
771 // This posts a task to the UI thread.
772 http_server_props_manager_
->UpdatePrefsFromCacheOnNetworkThreadConcrete(
774 // Shutdown comes before the task is executed.
775 http_server_props_manager_
->ShutdownOnPrefThread();
776 // Run the task after shutdown, but before deletion.
777 base::RunLoop().RunUntilIdle();
778 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
779 http_server_props_manager_
.reset();
780 base::RunLoop().RunUntilIdle();