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 ~TestingHttpServerPropertiesManager() override
{}
45 // Make these methods public for testing.
46 using HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread
;
47 using HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread
;
49 // Post tasks without a delay during tests.
50 void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay
) override
{
51 HttpServerPropertiesManager::StartPrefsUpdateTimerOnNetworkThread(
55 void UpdateCacheFromPrefsOnUIConcrete() {
56 HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread();
59 // Post tasks without a delay during tests.
60 void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay
) override
{
61 HttpServerPropertiesManager::StartCacheUpdateTimerOnPrefThread(
65 void UpdatePrefsFromCacheOnNetworkThreadConcrete(
66 const base::Closure
& callback
) {
67 HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread(callback
);
70 void ScheduleUpdatePrefsOnNetworkThread() {
71 // Picked a random Location as caller.
72 HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread(
73 DETECTED_CORRUPTED_PREFS
);
76 MOCK_METHOD0(UpdateCacheFromPrefsOnPrefThread
, void());
77 MOCK_METHOD1(UpdatePrefsFromCacheOnNetworkThread
, void(const base::Closure
&));
78 MOCK_METHOD6(UpdateCacheFromPrefsOnNetworkThread
,
79 void(std::vector
<std::string
>* spdy_servers
,
80 SpdySettingsMap
* spdy_settings_map
,
81 AlternativeServiceMap
* alternative_service_map
,
82 IPAddressNumber
* last_quic_address
,
83 ServerNetworkStatsMap
* server_network_stats_map
,
84 bool detected_corrupted_prefs
));
85 MOCK_METHOD5(UpdatePrefsOnPref
,
86 void(base::ListValue
* spdy_server_list
,
87 SpdySettingsMap
* spdy_settings_map
,
88 AlternativeServiceMap
* alternative_service_map
,
89 IPAddressNumber
* last_quic_address
,
90 ServerNetworkStatsMap
* server_network_stats_map
));
93 DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager
);
96 class HttpServerPropertiesManagerTest
: public testing::Test
{
98 HttpServerPropertiesManagerTest() {}
100 void SetUp() override
{
101 pref_service_
.registry()->RegisterDictionaryPref(kTestHttpServerProperties
);
102 http_server_props_manager_
.reset(
103 new StrictMock
<TestingHttpServerPropertiesManager
>(
105 kTestHttpServerProperties
,
106 base::MessageLoop::current()->message_loop_proxy()));
108 base::RunLoop().RunUntilIdle();
111 void TearDown() override
{
112 if (http_server_props_manager_
.get())
113 http_server_props_manager_
->ShutdownOnPrefThread();
114 base::RunLoop().RunUntilIdle();
115 http_server_props_manager_
.reset();
118 void ExpectCacheUpdate() {
119 EXPECT_CALL(*http_server_props_manager_
, UpdateCacheFromPrefsOnPrefThread())
120 .WillOnce(Invoke(http_server_props_manager_
.get(),
121 &TestingHttpServerPropertiesManager::
122 UpdateCacheFromPrefsOnUIConcrete
));
125 void ExpectPrefsUpdate() {
126 EXPECT_CALL(*http_server_props_manager_
,
127 UpdatePrefsFromCacheOnNetworkThread(_
))
128 .WillOnce(Invoke(http_server_props_manager_
.get(),
129 &TestingHttpServerPropertiesManager::
130 UpdatePrefsFromCacheOnNetworkThreadConcrete
));
133 void ExpectPrefsUpdateRepeatedly() {
134 EXPECT_CALL(*http_server_props_manager_
,
135 UpdatePrefsFromCacheOnNetworkThread(_
))
137 Invoke(http_server_props_manager_
.get(),
138 &TestingHttpServerPropertiesManager::
139 UpdatePrefsFromCacheOnNetworkThreadConcrete
));
142 bool HasAlternativeService(const HostPortPair
& server
) {
143 const AlternativeService alternative_service
=
144 http_server_props_manager_
->GetAlternativeService(server
);
145 return alternative_service
.protocol
!= UNINITIALIZED_ALTERNATE_PROTOCOL
;
148 //base::RunLoop loop_;
149 TestingPrefServiceSimple pref_service_
;
150 scoped_ptr
<TestingHttpServerPropertiesManager
> http_server_props_manager_
;
153 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest
);
156 TEST_F(HttpServerPropertiesManagerTest
,
157 SingleUpdateForTwoSpdyServerPrefChanges
) {
160 // Set up the prefs for www.google.com:80 and mail.google.com:80 and then set
161 // it twice. Only expect a single cache update.
163 base::DictionaryValue
* server_pref_dict
= new base::DictionaryValue
;
164 HostPortPair
google_server("www.google.com", 80);
165 HostPortPair
mail_server("mail.google.com", 80);
167 // Set supports_spdy for www.google.com:80.
168 server_pref_dict
->SetBoolean("supports_spdy", true);
170 // Set up alternative_service for www.google.com:80.
171 base::DictionaryValue
* alternative_service_dict
= new base::DictionaryValue
;
172 alternative_service_dict
->SetString("protocol_str", "npn-h2");
173 alternative_service_dict
->SetString("host", "maps.google.com");
174 alternative_service_dict
->SetInteger("port", 443);
175 base::ListValue
* alternative_service_list
= new base::ListValue
;
176 alternative_service_list
->Append(alternative_service_dict
);
177 server_pref_dict
->SetWithoutPathExpansion("alternative_service",
178 alternative_service_list
);
180 // Set up ServerNetworkStats for www.google.com:80.
181 base::DictionaryValue
* stats
= new base::DictionaryValue
;
182 stats
->SetInteger("srtt", 10);
183 server_pref_dict
->SetWithoutPathExpansion("network_stats", stats
);
185 // Set the server preference for www.google.com:80.
186 base::DictionaryValue
* servers_dict
= new base::DictionaryValue
;
187 servers_dict
->SetWithoutPathExpansion("www.google.com:80", server_pref_dict
);
189 // Set the preference for mail.google.com server.
190 base::DictionaryValue
* server_pref_dict1
= new base::DictionaryValue
;
192 // Set supports_spdy for mail.google.com:80
193 server_pref_dict1
->SetBoolean("supports_spdy", true);
195 // Set up alternate_protocol for mail.google.com:80 to test migration to
196 // alternative-service.
197 base::DictionaryValue
* alternate_protocol_dict
= new base::DictionaryValue
;
198 alternate_protocol_dict
->SetString("protocol_str", "npn-spdy/3.1");
199 alternate_protocol_dict
->SetInteger("port", 444);
200 server_pref_dict1
->SetWithoutPathExpansion("alternate_protocol",
201 alternate_protocol_dict
);
203 // Set up ServerNetworkStats for mail.google.com:80.
204 base::DictionaryValue
* stats1
= new base::DictionaryValue
;
205 stats1
->SetInteger("srtt", 20);
206 server_pref_dict1
->SetWithoutPathExpansion("network_stats", stats1
);
207 // Set the server preference for mail.google.com:80.
208 servers_dict
->SetWithoutPathExpansion("mail.google.com:80",
211 base::DictionaryValue
* http_server_properties_dict
=
212 new base::DictionaryValue
;
213 HttpServerPropertiesManager::SetVersion(http_server_properties_dict
, -1);
214 http_server_properties_dict
->SetWithoutPathExpansion("servers", servers_dict
);
215 base::DictionaryValue
* supports_quic
= new base::DictionaryValue
;
216 supports_quic
->SetBoolean("used_quic", true);
217 supports_quic
->SetString("address", "127.0.0.1");
218 http_server_properties_dict
->SetWithoutPathExpansion("supports_quic",
221 // Set the same value for kHttpServerProperties multiple times.
222 pref_service_
.SetManagedPref(kTestHttpServerProperties
,
223 http_server_properties_dict
);
224 base::DictionaryValue
* http_server_properties_dict2
=
225 http_server_properties_dict
->DeepCopy();
226 pref_service_
.SetManagedPref(kTestHttpServerProperties
,
227 http_server_properties_dict2
);
229 base::RunLoop().RunUntilIdle();
230 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
232 // Verify SupportsSpdy.
234 http_server_props_manager_
->SupportsRequestPriority(google_server
));
235 EXPECT_TRUE(http_server_props_manager_
->SupportsRequestPriority(mail_server
));
236 EXPECT_FALSE(http_server_props_manager_
->SupportsRequestPriority(
237 HostPortPair::FromString("foo.google.com:1337")));
239 // Verify alternative service.
240 AlternativeService alternative_service
=
241 http_server_props_manager_
->GetAlternativeService(google_server
);
242 EXPECT_EQ(NPN_SPDY_4
, alternative_service
.protocol
);
243 EXPECT_EQ("maps.google.com", alternative_service
.host
);
244 EXPECT_EQ(443, alternative_service
.port
);
245 alternative_service
=
246 http_server_props_manager_
->GetAlternativeService(mail_server
);
247 EXPECT_EQ(NPN_SPDY_3_1
, alternative_service
.protocol
);
248 EXPECT_EQ("mail.google.com", alternative_service
.host
);
249 EXPECT_EQ(444, alternative_service
.port
);
251 // Verify SupportsQuic.
252 IPAddressNumber last_address
;
253 EXPECT_TRUE(http_server_props_manager_
->GetSupportsQuic(&last_address
));
254 EXPECT_EQ("127.0.0.1", IPAddressToString(last_address
));
256 // Verify ServerNetworkStats.
257 const ServerNetworkStats
* stats2
=
258 http_server_props_manager_
->GetServerNetworkStats(google_server
);
259 EXPECT_EQ(10, stats2
->srtt
.ToInternalValue());
260 const ServerNetworkStats
* stats3
=
261 http_server_props_manager_
->GetServerNetworkStats(mail_server
);
262 EXPECT_EQ(20, stats3
->srtt
.ToInternalValue());
265 TEST_F(HttpServerPropertiesManagerTest
, BadCachedHostPortPair
) {
267 // The prefs are automaticalls updated in the case corruption is detected.
270 base::DictionaryValue
* server_pref_dict
= new base::DictionaryValue
;
272 // Set supports_spdy for www.google.com:65536.
273 server_pref_dict
->SetBoolean("supports_spdy", true);
275 // Set up alternative_service for www.google.com:65536.
277 base::DictionaryValue
* alternative_service_dict
= new base::DictionaryValue
;
278 alternative_service_dict
->SetString("protocol_str", "npn-spdy/3");
279 alternative_service_dict
->SetInteger("port", 80);
280 base::ListValue
* alternative_service_list
= new base::ListValue
;
281 alternative_service_list
->Append(alternative_service_dict
);
282 server_pref_dict
->SetWithoutPathExpansion("alternative_service",
283 alternative_service_list
);
285 // Set up ServerNetworkStats for www.google.com:65536.
286 base::DictionaryValue
* stats
= new base::DictionaryValue
;
287 stats
->SetInteger("srtt", 10);
288 server_pref_dict
->SetWithoutPathExpansion("network_stats", stats
);
290 // Set the server preference for www.google.com:65536.
291 base::DictionaryValue
* servers_dict
= new base::DictionaryValue
;
292 servers_dict
->SetWithoutPathExpansion("www.google.com:65536",
295 base::DictionaryValue
* http_server_properties_dict
=
296 new base::DictionaryValue
;
297 HttpServerPropertiesManager::SetVersion(http_server_properties_dict
, -1);
298 http_server_properties_dict
->SetWithoutPathExpansion("servers", servers_dict
);
301 pref_service_
.SetManagedPref(kTestHttpServerProperties
,
302 http_server_properties_dict
);
304 base::RunLoop().RunUntilIdle();
305 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
307 // Verify that nothing is set.
308 EXPECT_FALSE(http_server_props_manager_
->SupportsRequestPriority(
309 HostPortPair::FromString("www.google.com:65536")));
311 HasAlternativeService(HostPortPair::FromString("www.google.com:65536")));
312 const ServerNetworkStats
* stats1
=
313 http_server_props_manager_
->GetServerNetworkStats(
314 HostPortPair::FromString("www.google.com:65536"));
315 EXPECT_EQ(NULL
, stats1
);
318 TEST_F(HttpServerPropertiesManagerTest
, BadCachedAltProtocolPort
) {
320 // The prefs are automaticalls updated in the case corruption is detected.
323 base::DictionaryValue
* server_pref_dict
= new base::DictionaryValue
;
325 // Set supports_spdy for www.google.com:80.
326 server_pref_dict
->SetBoolean("supports_spdy", true);
328 // Set up alternative_service for www.google.com:80.
329 base::DictionaryValue
* alternative_service_dict
= new base::DictionaryValue
;
330 alternative_service_dict
->SetString("protocol_str", "npn-spdy/3");
331 alternative_service_dict
->SetInteger("port", 65536);
332 base::ListValue
* alternative_service_list
= new base::ListValue
;
333 alternative_service_list
->Append(alternative_service_dict
);
334 server_pref_dict
->SetWithoutPathExpansion("alternative_service",
335 alternative_service_list
);
337 // Set the server preference for www.google.com:80.
338 base::DictionaryValue
* servers_dict
= new base::DictionaryValue
;
339 servers_dict
->SetWithoutPathExpansion("www.google.com:80", server_pref_dict
);
341 base::DictionaryValue
* http_server_properties_dict
=
342 new base::DictionaryValue
;
343 HttpServerPropertiesManager::SetVersion(http_server_properties_dict
, -1);
344 http_server_properties_dict
->SetWithoutPathExpansion("servers", servers_dict
);
347 pref_service_
.SetManagedPref(kTestHttpServerProperties
,
348 http_server_properties_dict
);
350 base::RunLoop().RunUntilIdle();
351 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
353 // Verify alternative service is not set.
355 HasAlternativeService(HostPortPair::FromString("www.google.com:80")));
358 TEST_F(HttpServerPropertiesManagerTest
, SupportsSpdy
) {
361 // Post an update task to the network thread. SetSupportsSpdy calls
362 // ScheduleUpdatePrefsOnNetworkThread.
364 // Add mail.google.com:443 as a supporting spdy server.
365 HostPortPair
spdy_server_mail("mail.google.com", 443);
367 http_server_props_manager_
->SupportsRequestPriority(spdy_server_mail
));
368 http_server_props_manager_
->SetSupportsSpdy(spdy_server_mail
, true);
371 base::RunLoop().RunUntilIdle();
374 http_server_props_manager_
->SupportsRequestPriority(spdy_server_mail
));
375 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
378 TEST_F(HttpServerPropertiesManagerTest
, SetSpdySetting
) {
381 // Add SpdySetting for mail.google.com:443.
382 HostPortPair
spdy_server_mail("mail.google.com", 443);
383 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
384 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
385 const uint32 value1
= 31337;
386 http_server_props_manager_
->SetSpdySetting(
387 spdy_server_mail
, id1
, flags1
, value1
);
390 base::RunLoop().RunUntilIdle();
392 const SettingsMap
& settings_map1_ret
=
393 http_server_props_manager_
->GetSpdySettings(spdy_server_mail
);
394 ASSERT_EQ(1U, settings_map1_ret
.size());
395 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
396 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
397 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
398 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
399 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
401 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
404 TEST_F(HttpServerPropertiesManagerTest
, ClearSpdySetting
) {
405 ExpectPrefsUpdateRepeatedly();
407 // Add SpdySetting for mail.google.com:443.
408 HostPortPair
spdy_server_mail("mail.google.com", 443);
409 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
410 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
411 const uint32 value1
= 31337;
412 http_server_props_manager_
->SetSpdySetting(
413 spdy_server_mail
, id1
, flags1
, value1
);
416 base::RunLoop().RunUntilIdle();
418 const SettingsMap
& settings_map1_ret
=
419 http_server_props_manager_
->GetSpdySettings(spdy_server_mail
);
420 ASSERT_EQ(1U, settings_map1_ret
.size());
421 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
422 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
423 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
424 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
425 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
427 // Clear SpdySetting for mail.google.com:443.
428 http_server_props_manager_
->ClearSpdySettings(spdy_server_mail
);
431 base::RunLoop().RunUntilIdle();
433 // Verify that there are no entries in the settings map for
434 // mail.google.com:443.
435 const SettingsMap
& settings_map2_ret
=
436 http_server_props_manager_
->GetSpdySettings(spdy_server_mail
);
437 ASSERT_EQ(0U, settings_map2_ret
.size());
439 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
442 TEST_F(HttpServerPropertiesManagerTest
, ClearAllSpdySetting
) {
443 ExpectPrefsUpdateRepeatedly();
445 // Add SpdySetting for mail.google.com:443.
446 HostPortPair
spdy_server_mail("mail.google.com", 443);
447 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
448 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
449 const uint32 value1
= 31337;
450 http_server_props_manager_
->SetSpdySetting(
451 spdy_server_mail
, id1
, flags1
, value1
);
454 base::RunLoop().RunUntilIdle();
456 const SettingsMap
& settings_map1_ret
=
457 http_server_props_manager_
->GetSpdySettings(spdy_server_mail
);
458 ASSERT_EQ(1U, settings_map1_ret
.size());
459 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
460 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
461 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
462 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
463 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
465 // Clear All SpdySettings.
466 http_server_props_manager_
->ClearAllSpdySettings();
469 base::RunLoop().RunUntilIdle();
471 // Verify that there are no entries in the settings map.
472 const SpdySettingsMap
& spdy_settings_map2_ret
=
473 http_server_props_manager_
->spdy_settings_map();
474 ASSERT_EQ(0U, spdy_settings_map2_ret
.size());
476 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
479 TEST_F(HttpServerPropertiesManagerTest
, GetAlternativeService
) {
482 HostPortPair
spdy_server_mail("mail.google.com", 80);
483 EXPECT_FALSE(HasAlternativeService(spdy_server_mail
));
484 AlternativeService
alternative_service(NPN_SPDY_4
, "mail.google.com", 443);
485 http_server_props_manager_
->SetAlternativeService(spdy_server_mail
,
486 alternative_service
, 1.0);
489 base::RunLoop().RunUntilIdle();
490 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
492 alternative_service
=
493 http_server_props_manager_
->GetAlternativeService(spdy_server_mail
);
494 EXPECT_EQ(443, alternative_service
.port
);
495 EXPECT_EQ(NPN_SPDY_4
, alternative_service
.protocol
);
498 TEST_F(HttpServerPropertiesManagerTest
, SupportsQuic
) {
501 IPAddressNumber address
;
502 EXPECT_FALSE(http_server_props_manager_
->GetSupportsQuic(&address
));
504 IPAddressNumber actual_address
;
505 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address
));
506 http_server_props_manager_
->SetSupportsQuic(true, actual_address
);
509 base::RunLoop().RunUntilIdle();
510 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
512 EXPECT_TRUE(http_server_props_manager_
->GetSupportsQuic(&address
));
513 EXPECT_EQ(actual_address
, address
);
516 TEST_F(HttpServerPropertiesManagerTest
, ServerNetworkStats
) {
519 HostPortPair
mail_server("mail.google.com", 80);
520 const ServerNetworkStats
* stats
=
521 http_server_props_manager_
->GetServerNetworkStats(mail_server
);
522 EXPECT_EQ(NULL
, stats
);
523 ServerNetworkStats stats1
;
524 stats1
.srtt
= base::TimeDelta::FromMicroseconds(10);
525 http_server_props_manager_
->SetServerNetworkStats(mail_server
, stats1
);
528 base::RunLoop().RunUntilIdle();
529 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
531 const ServerNetworkStats
* stats2
=
532 http_server_props_manager_
->GetServerNetworkStats(mail_server
);
533 EXPECT_EQ(10, stats2
->srtt
.ToInternalValue());
536 TEST_F(HttpServerPropertiesManagerTest
, Clear
) {
539 HostPortPair
spdy_server_mail("mail.google.com", 443);
540 http_server_props_manager_
->SetSupportsSpdy(spdy_server_mail
, true);
541 AlternativeService
alternative_service(NPN_SPDY_4
, "mail.google.com", 443);
542 http_server_props_manager_
->SetAlternativeService(spdy_server_mail
,
543 alternative_service
, 1.0);
544 IPAddressNumber actual_address
;
545 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address
));
546 http_server_props_manager_
->SetSupportsQuic(true, actual_address
);
547 ServerNetworkStats stats
;
548 stats
.srtt
= base::TimeDelta::FromMicroseconds(10);
549 http_server_props_manager_
->SetServerNetworkStats(spdy_server_mail
, stats
);
551 const SpdySettingsIds id1
= SETTINGS_UPLOAD_BANDWIDTH
;
552 const SpdySettingsFlags flags1
= SETTINGS_FLAG_PLEASE_PERSIST
;
553 const uint32 value1
= 31337;
554 http_server_props_manager_
->SetSpdySetting(
555 spdy_server_mail
, id1
, flags1
, value1
);
558 base::RunLoop().RunUntilIdle();
561 http_server_props_manager_
->SupportsRequestPriority(spdy_server_mail
));
562 EXPECT_TRUE(HasAlternativeService(spdy_server_mail
));
563 IPAddressNumber address
;
564 EXPECT_TRUE(http_server_props_manager_
->GetSupportsQuic(&address
));
565 EXPECT_EQ(actual_address
, address
);
566 const ServerNetworkStats
* stats1
=
567 http_server_props_manager_
->GetServerNetworkStats(spdy_server_mail
);
568 EXPECT_EQ(10, stats1
->srtt
.ToInternalValue());
570 // Check SPDY settings values.
571 const SettingsMap
& settings_map1_ret
=
572 http_server_props_manager_
->GetSpdySettings(spdy_server_mail
);
573 ASSERT_EQ(1U, settings_map1_ret
.size());
574 SettingsMap::const_iterator it1_ret
= settings_map1_ret
.find(id1
);
575 EXPECT_TRUE(it1_ret
!= settings_map1_ret
.end());
576 SettingsFlagsAndValue flags_and_value1_ret
= it1_ret
->second
;
577 EXPECT_EQ(SETTINGS_FLAG_PERSISTED
, flags_and_value1_ret
.first
);
578 EXPECT_EQ(value1
, flags_and_value1_ret
.second
);
580 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
584 // Clear http server data, time out if we do not get a completion callback.
585 http_server_props_manager_
->Clear(base::MessageLoop::QuitClosure());
586 base::RunLoop().Run();
589 http_server_props_manager_
->SupportsRequestPriority(spdy_server_mail
));
590 EXPECT_FALSE(HasAlternativeService(spdy_server_mail
));
591 EXPECT_FALSE(http_server_props_manager_
->GetSupportsQuic(&address
));
592 const ServerNetworkStats
* stats2
=
593 http_server_props_manager_
->GetServerNetworkStats(spdy_server_mail
);
594 EXPECT_EQ(NULL
, stats2
);
596 const SettingsMap
& settings_map2_ret
=
597 http_server_props_manager_
->GetSpdySettings(spdy_server_mail
);
598 EXPECT_EQ(0U, settings_map2_ret
.size());
600 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
603 // https://crbug.com/444956: Add 200 alternative_service servers followed by
604 // supports_quic and verify we have read supports_quic from prefs.
605 TEST_F(HttpServerPropertiesManagerTest
, BadSupportsQuic
) {
608 base::DictionaryValue
* servers_dict
= new base::DictionaryValue
;
610 for (int i
= 0; i
< 200; ++i
) {
611 // Set up alternative_service for www.google.com:i.
612 base::DictionaryValue
* alternative_service_dict
= new base::DictionaryValue
;
613 alternative_service_dict
->SetString("protocol_str", "npn-h2");
614 alternative_service_dict
->SetString("host", "");
615 alternative_service_dict
->SetInteger("port", i
);
616 base::ListValue
* alternative_service_list
= new base::ListValue
;
617 alternative_service_list
->Append(alternative_service_dict
);
618 base::DictionaryValue
* server_pref_dict
= new base::DictionaryValue
;
619 server_pref_dict
->SetWithoutPathExpansion("alternative_service",
620 alternative_service_list
);
621 servers_dict
->SetWithoutPathExpansion(StringPrintf("www.google.com:%d", i
),
625 // Set the preference for mail.google.com server.
626 base::DictionaryValue
* server_pref_dict1
= new base::DictionaryValue
;
628 // Set the server preference for mail.google.com:80.
629 servers_dict
->SetWithoutPathExpansion("mail.google.com:80",
632 base::DictionaryValue
* http_server_properties_dict
=
633 new base::DictionaryValue
;
634 HttpServerPropertiesManager::SetVersion(http_server_properties_dict
, -1);
635 http_server_properties_dict
->SetWithoutPathExpansion("servers", servers_dict
);
637 // Set up SupportsQuic for 127.0.0.1
638 base::DictionaryValue
* supports_quic
= new base::DictionaryValue
;
639 supports_quic
->SetBoolean("used_quic", true);
640 supports_quic
->SetString("address", "127.0.0.1");
641 http_server_properties_dict
->SetWithoutPathExpansion("supports_quic",
645 pref_service_
.SetManagedPref(kTestHttpServerProperties
,
646 http_server_properties_dict
);
648 base::RunLoop().RunUntilIdle();
649 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
651 // Verify alternative service.
652 for (int i
= 0; i
< 200; ++i
) {
653 std::string server
= StringPrintf("www.google.com:%d", i
);
654 AlternativeService alternative_service
=
655 http_server_props_manager_
->GetAlternativeService(
656 HostPortPair::FromString(server
));
657 EXPECT_EQ(i
, alternative_service
.port
);
658 EXPECT_EQ(NPN_SPDY_4
, alternative_service
.protocol
);
661 // Verify SupportsQuic.
662 IPAddressNumber address
;
663 ASSERT_TRUE(http_server_props_manager_
->GetSupportsQuic(&address
));
664 EXPECT_EQ("127.0.0.1", IPAddressToString(address
));
667 TEST_F(HttpServerPropertiesManagerTest
, UpdateCacheWithPrefs
) {
668 const HostPortPair
server_www("www.google.com", 80);
669 const HostPortPair
server_mail("mail.google.com", 80);
671 // Set alternate protocol.
672 AlternativeService
www_altsvc(NPN_SPDY_4
, "", 443);
673 AlternativeService
mail_altsvc(NPN_SPDY_3_1
, "mail.google.com", 444);
674 http_server_props_manager_
->SetAlternativeService(server_www
, www_altsvc
,
676 http_server_props_manager_
->SetAlternativeService(server_mail
, mail_altsvc
,
679 // Set ServerNetworkStats.
680 ServerNetworkStats stats
;
681 stats
.srtt
= base::TimeDelta::FromInternalValue(42);
682 http_server_props_manager_
->SetServerNetworkStats(server_mail
, stats
);
685 IPAddressNumber actual_address
;
686 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address
));
687 http_server_props_manager_
->SetSupportsQuic(true, actual_address
);
692 http_server_props_manager_
->ScheduleUpdateCacheOnPrefThread();
693 base::RunLoop().RunUntilIdle();
695 // Verify preferences.
696 const char expected_json
[] =
697 "{\"servers\":{\"mail.google.com:80\":{\"alternative_service\":[{"
699 "\"mail.google.com\",\"port\":444,\"probability\":0.2,\"protocol_str\":"
701 "3.1\"}],\"network_stats\":{\"srtt\":42}},\"www.google.com:80\":"
702 "{\"alternative_service\":[{\"port\":443,\"probability\":1.0,"
703 "\"protocol_str\":\"npn-h2\"}]}},\"supports_quic\":"
704 "{\"address\":\"127.0.0.1\",\"used_quic\":true},\"version\":3}";
706 const base::Value
* http_server_properties
=
707 pref_service_
.GetUserPref(kTestHttpServerProperties
);
708 ASSERT_NE(nullptr, http_server_properties
);
709 std::string preferences_json
;
711 base::JSONWriter::Write(*http_server_properties
, &preferences_json
));
712 EXPECT_EQ(expected_json
, preferences_json
);
715 TEST_F(HttpServerPropertiesManagerTest
, ShutdownWithPendingUpdateCache0
) {
716 // Post an update task to the UI thread.
717 http_server_props_manager_
->ScheduleUpdateCacheOnPrefThread();
718 // Shutdown comes before the task is executed.
719 http_server_props_manager_
->ShutdownOnPrefThread();
720 http_server_props_manager_
.reset();
721 // Run the task after shutdown and deletion.
722 base::RunLoop().RunUntilIdle();
725 TEST_F(HttpServerPropertiesManagerTest
, ShutdownWithPendingUpdateCache1
) {
726 // Post an update task.
727 http_server_props_manager_
->ScheduleUpdateCacheOnPrefThread();
728 // Shutdown comes before the task is executed.
729 http_server_props_manager_
->ShutdownOnPrefThread();
730 // Run the task after shutdown, but before deletion.
731 base::RunLoop().RunUntilIdle();
732 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
733 http_server_props_manager_
.reset();
734 base::RunLoop().RunUntilIdle();
737 TEST_F(HttpServerPropertiesManagerTest
, ShutdownWithPendingUpdateCache2
) {
738 http_server_props_manager_
->UpdateCacheFromPrefsOnUIConcrete();
739 // Shutdown comes before the task is executed.
740 http_server_props_manager_
->ShutdownOnPrefThread();
741 // Run the task after shutdown, but before deletion.
742 base::RunLoop().RunUntilIdle();
743 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
744 http_server_props_manager_
.reset();
745 base::RunLoop().RunUntilIdle();
749 // Tests for shutdown when updating prefs.
751 TEST_F(HttpServerPropertiesManagerTest
, ShutdownWithPendingUpdatePrefs0
) {
752 // Post an update task to the IO thread.
753 http_server_props_manager_
->ScheduleUpdatePrefsOnNetworkThread();
754 // Shutdown comes before the task is executed.
755 http_server_props_manager_
->ShutdownOnPrefThread();
756 http_server_props_manager_
.reset();
757 // Run the task after shutdown and deletion.
758 base::RunLoop().RunUntilIdle();
761 TEST_F(HttpServerPropertiesManagerTest
, ShutdownWithPendingUpdatePrefs1
) {
763 // Post an update task.
764 http_server_props_manager_
->ScheduleUpdatePrefsOnNetworkThread();
765 // Shutdown comes before the task is executed.
766 http_server_props_manager_
->ShutdownOnPrefThread();
767 // Run the task after shutdown, but before deletion.
768 base::RunLoop().RunUntilIdle();
769 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
770 http_server_props_manager_
.reset();
771 base::RunLoop().RunUntilIdle();
774 TEST_F(HttpServerPropertiesManagerTest
, ShutdownWithPendingUpdatePrefs2
) {
775 // This posts a task to the UI thread.
776 http_server_props_manager_
->UpdatePrefsFromCacheOnNetworkThreadConcrete(
778 // Shutdown comes before the task is executed.
779 http_server_props_manager_
->ShutdownOnPrefThread();
780 // Run the task after shutdown, but before deletion.
781 base::RunLoop().RunUntilIdle();
782 Mock::VerifyAndClearExpectations(http_server_props_manager_
.get());
783 http_server_props_manager_
.reset();
784 base::RunLoop().RunUntilIdle();