Update HttpServerProperties::*AlternateProtocol* interface.
[chromium-blink-merge.git] / net / http / http_server_properties_manager_unittest.cc
blobf017815b97caf4386e9044937db1849f0a6659dd
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"
19 #include "url/gurl.h"
21 namespace net {
23 namespace {
25 using base::StringPrintf;
26 using ::testing::_;
27 using ::testing::Invoke;
28 using ::testing::Mock;
29 using ::testing::StrictMock;
31 const char kTestHttpServerProperties[] = "TestHttpServerProperties";
33 class TestingHttpServerPropertiesManager : public HttpServerPropertiesManager {
34 public:
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(
53 base::TimeDelta());
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(
64 base::TimeDelta());
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 AlternateProtocolMap* alternate_protocol_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 AlternateProtocolMap* alternate_protocol_map,
85 IPAddressNumber* last_quic_address,
86 ServerNetworkStatsMap* server_network_stats_map));
88 private:
89 DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager);
92 class HttpServerPropertiesManagerTest : public testing::Test {
93 protected:
94 HttpServerPropertiesManagerTest() {}
96 void SetUp() override {
97 pref_service_.registry()->RegisterDictionaryPref(kTestHttpServerProperties);
98 http_server_props_manager_.reset(
99 new StrictMock<TestingHttpServerPropertiesManager>(
100 &pref_service_,
101 kTestHttpServerProperties,
102 base::MessageLoop::current()->message_loop_proxy()));
103 ExpectCacheUpdate();
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(_))
132 .WillRepeatedly(
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_;
148 private:
149 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest);
152 TEST_F(HttpServerPropertiesManagerTest,
153 SingleUpdateForTwoSpdyServerPrefChanges) {
154 ExpectCacheUpdate();
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 alternate_protocol for www.google.com:80.
167 base::DictionaryValue* alternate_protocol = new base::DictionaryValue;
168 alternate_protocol->SetInteger("port", 443);
169 alternate_protocol->SetString("protocol_str", "npn-spdy/3");
170 server_pref_dict->SetWithoutPathExpansion("alternate_protocol",
171 alternate_protocol);
173 // Set up ServerNetworkStats for www.google.com:80.
174 base::DictionaryValue* stats = new base::DictionaryValue;
175 stats->SetInteger("srtt", 10);
176 server_pref_dict->SetWithoutPathExpansion("network_stats", stats);
178 // Set the server preference for www.google.com:80.
179 base::DictionaryValue* servers_dict = new base::DictionaryValue;
180 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict);
182 // Set the preference for mail.google.com server.
183 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
185 // Set supports_spdy for mail.google.com:80
186 server_pref_dict1->SetBoolean("supports_spdy", true);
188 // Set up alternate_protocol for mail.google.com:80
189 base::DictionaryValue* alternate_protocol1 = new base::DictionaryValue;
190 alternate_protocol1->SetInteger("port", 444);
191 alternate_protocol1->SetString("protocol_str", "npn-spdy/3.1");
193 server_pref_dict1->SetWithoutPathExpansion("alternate_protocol",
194 alternate_protocol1);
196 // Set up ServerNetworkStats for mail.google.com:80.
197 base::DictionaryValue* stats1 = new base::DictionaryValue;
198 stats1->SetInteger("srtt", 20);
199 server_pref_dict1->SetWithoutPathExpansion("network_stats", stats1);
200 // Set the server preference for mail.google.com:80.
201 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
202 server_pref_dict1);
204 base::DictionaryValue* http_server_properties_dict =
205 new base::DictionaryValue;
206 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
207 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
208 base::DictionaryValue* supports_quic = new base::DictionaryValue;
209 supports_quic->SetBoolean("used_quic", true);
210 supports_quic->SetString("address", "127.0.0.1");
211 http_server_properties_dict->SetWithoutPathExpansion("supports_quic",
212 supports_quic);
214 // Set the same value for kHttpServerProperties multiple times.
215 pref_service_.SetManagedPref(kTestHttpServerProperties,
216 http_server_properties_dict);
217 base::DictionaryValue* http_server_properties_dict2 =
218 http_server_properties_dict->DeepCopy();
219 pref_service_.SetManagedPref(kTestHttpServerProperties,
220 http_server_properties_dict2);
222 base::RunLoop().RunUntilIdle();
223 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
225 // Verify SupportsSpdy.
226 EXPECT_TRUE(
227 http_server_props_manager_->SupportsRequestPriority(google_server));
228 EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(mail_server));
229 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority(
230 HostPortPair::FromString("foo.google.com:1337")));
232 // Verify AlternateProtocol.
233 AlternativeService alternative_service =
234 http_server_props_manager_->GetAlternativeService(google_server);
235 EXPECT_EQ(443, alternative_service.port);
236 EXPECT_EQ(NPN_SPDY_3, alternative_service.protocol);
237 alternative_service =
238 http_server_props_manager_->GetAlternativeService(mail_server);
239 EXPECT_EQ(444, alternative_service.port);
240 EXPECT_EQ(NPN_SPDY_3_1, alternative_service.protocol);
242 // Verify SupportsQuic.
243 IPAddressNumber last_address;
244 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&last_address));
245 EXPECT_EQ("127.0.0.1", IPAddressToString(last_address));
247 // Verify ServerNetworkStats.
248 const ServerNetworkStats* stats2 =
249 http_server_props_manager_->GetServerNetworkStats(google_server);
250 EXPECT_EQ(10, stats2->srtt.ToInternalValue());
251 const ServerNetworkStats* stats3 =
252 http_server_props_manager_->GetServerNetworkStats(mail_server);
253 EXPECT_EQ(20, stats3->srtt.ToInternalValue());
256 TEST_F(HttpServerPropertiesManagerTest, BadCachedHostPortPair) {
257 ExpectCacheUpdate();
258 // The prefs are automaticalls updated in the case corruption is detected.
259 ExpectPrefsUpdate();
261 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
263 // Set supports_spdy for www.google.com:65536.
264 server_pref_dict->SetBoolean("supports_spdy", true);
266 // Set up alternate_protocol for www.google.com:65536.
267 base::DictionaryValue* alternate_protocol = new base::DictionaryValue;
268 alternate_protocol->SetInteger("port", 80);
269 alternate_protocol->SetString("protocol_str", "npn-spdy/3");
270 server_pref_dict->SetWithoutPathExpansion("alternate_protocol",
271 alternate_protocol);
273 // Set up ServerNetworkStats for www.google.com:65536.
274 base::DictionaryValue* stats = new base::DictionaryValue;
275 stats->SetInteger("srtt", 10);
276 server_pref_dict->SetWithoutPathExpansion("network_stats", stats);
278 // Set the server preference for www.google.com:65536.
279 base::DictionaryValue* servers_dict = new base::DictionaryValue;
280 servers_dict->SetWithoutPathExpansion("www.google.com:65536",
281 server_pref_dict);
283 base::DictionaryValue* http_server_properties_dict =
284 new base::DictionaryValue;
285 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
286 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
288 // Set up the pref.
289 pref_service_.SetManagedPref(kTestHttpServerProperties,
290 http_server_properties_dict);
292 base::RunLoop().RunUntilIdle();
293 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
295 // Verify that nothing is set.
296 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority(
297 HostPortPair::FromString("www.google.com:65536")));
298 EXPECT_FALSE(
299 HasAlternativeService(HostPortPair::FromString("www.google.com:65536")));
300 const ServerNetworkStats* stats1 =
301 http_server_props_manager_->GetServerNetworkStats(
302 HostPortPair::FromString("www.google.com:65536"));
303 EXPECT_EQ(NULL, stats1);
306 TEST_F(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) {
307 ExpectCacheUpdate();
308 // The prefs are automaticalls updated in the case corruption is detected.
309 ExpectPrefsUpdate();
311 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
313 // Set supports_spdy for www.google.com:80.
314 server_pref_dict->SetBoolean("supports_spdy", true);
316 // Set up alternate_protocol for www.google.com:80.
317 base::DictionaryValue* alternate_protocol = new base::DictionaryValue;
318 alternate_protocol->SetInteger("port", 65536);
319 alternate_protocol->SetString("protocol_str", "npn-spdy/3");
320 server_pref_dict->SetWithoutPathExpansion("alternate_protocol",
321 alternate_protocol);
323 // Set the server preference for www.google.com:80.
324 base::DictionaryValue* servers_dict = new base::DictionaryValue;
325 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict);
327 base::DictionaryValue* http_server_properties_dict =
328 new base::DictionaryValue;
329 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
330 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
332 // Set up the pref.
333 pref_service_.SetManagedPref(kTestHttpServerProperties,
334 http_server_properties_dict);
336 base::RunLoop().RunUntilIdle();
337 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
339 // Verify AlternateProtocol is not set.
340 EXPECT_FALSE(
341 HasAlternativeService(HostPortPair::FromString("www.google.com:80")));
344 TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) {
345 ExpectPrefsUpdate();
347 // Post an update task to the network thread. SetSupportsSpdy calls
348 // ScheduleUpdatePrefsOnNetworkThread.
350 // Add mail.google.com:443 as a supporting spdy server.
351 HostPortPair spdy_server_mail("mail.google.com", 443);
352 EXPECT_FALSE(
353 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
354 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
356 // Run the task.
357 base::RunLoop().RunUntilIdle();
359 EXPECT_TRUE(
360 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
361 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
364 TEST_F(HttpServerPropertiesManagerTest, SetSpdySetting) {
365 ExpectPrefsUpdate();
367 // Add SpdySetting for mail.google.com:443.
368 HostPortPair spdy_server_mail("mail.google.com", 443);
369 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
370 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
371 const uint32 value1 = 31337;
372 http_server_props_manager_->SetSpdySetting(
373 spdy_server_mail, id1, flags1, value1);
375 // Run the task.
376 base::RunLoop().RunUntilIdle();
378 const SettingsMap& settings_map1_ret =
379 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
380 ASSERT_EQ(1U, settings_map1_ret.size());
381 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
382 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
383 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
384 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
385 EXPECT_EQ(value1, flags_and_value1_ret.second);
387 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
390 TEST_F(HttpServerPropertiesManagerTest, ClearSpdySetting) {
391 ExpectPrefsUpdateRepeatedly();
393 // Add SpdySetting for mail.google.com:443.
394 HostPortPair spdy_server_mail("mail.google.com", 443);
395 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
396 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
397 const uint32 value1 = 31337;
398 http_server_props_manager_->SetSpdySetting(
399 spdy_server_mail, id1, flags1, value1);
401 // Run the task.
402 base::RunLoop().RunUntilIdle();
404 const SettingsMap& settings_map1_ret =
405 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
406 ASSERT_EQ(1U, settings_map1_ret.size());
407 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
408 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
409 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
410 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
411 EXPECT_EQ(value1, flags_and_value1_ret.second);
413 // Clear SpdySetting for mail.google.com:443.
414 http_server_props_manager_->ClearSpdySettings(spdy_server_mail);
416 // Run the task.
417 base::RunLoop().RunUntilIdle();
419 // Verify that there are no entries in the settings map for
420 // mail.google.com:443.
421 const SettingsMap& settings_map2_ret =
422 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
423 ASSERT_EQ(0U, settings_map2_ret.size());
425 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
428 TEST_F(HttpServerPropertiesManagerTest, ClearAllSpdySetting) {
429 ExpectPrefsUpdateRepeatedly();
431 // Add SpdySetting for mail.google.com:443.
432 HostPortPair spdy_server_mail("mail.google.com", 443);
433 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
434 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
435 const uint32 value1 = 31337;
436 http_server_props_manager_->SetSpdySetting(
437 spdy_server_mail, id1, flags1, value1);
439 // Run the task.
440 base::RunLoop().RunUntilIdle();
442 const SettingsMap& settings_map1_ret =
443 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
444 ASSERT_EQ(1U, settings_map1_ret.size());
445 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
446 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
447 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
448 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
449 EXPECT_EQ(value1, flags_and_value1_ret.second);
451 // Clear All SpdySettings.
452 http_server_props_manager_->ClearAllSpdySettings();
454 // Run the task.
455 base::RunLoop().RunUntilIdle();
457 // Verify that there are no entries in the settings map.
458 const SpdySettingsMap& spdy_settings_map2_ret =
459 http_server_props_manager_->spdy_settings_map();
460 ASSERT_EQ(0U, spdy_settings_map2_ret.size());
462 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
465 TEST_F(HttpServerPropertiesManagerTest, GetAlternativeService) {
466 ExpectPrefsUpdate();
468 HostPortPair spdy_server_mail("mail.google.com", 80);
469 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
470 AlternativeService alternative_service(NPN_SPDY_3, "mail.google.com", 443);
471 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
472 alternative_service, 1.0);
474 // Run the task.
475 base::RunLoop().RunUntilIdle();
476 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
478 alternative_service =
479 http_server_props_manager_->GetAlternativeService(spdy_server_mail);
480 EXPECT_EQ(443, alternative_service.port);
481 EXPECT_EQ(NPN_SPDY_3, alternative_service.protocol);
484 TEST_F(HttpServerPropertiesManagerTest, SupportsQuic) {
485 ExpectPrefsUpdate();
487 IPAddressNumber address;
488 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address));
490 IPAddressNumber actual_address;
491 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
492 http_server_props_manager_->SetSupportsQuic(true, actual_address);
494 // Run the task.
495 base::RunLoop().RunUntilIdle();
496 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
498 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
499 EXPECT_EQ(actual_address, address);
502 TEST_F(HttpServerPropertiesManagerTest, ServerNetworkStats) {
503 ExpectPrefsUpdate();
505 HostPortPair mail_server("mail.google.com", 80);
506 const ServerNetworkStats* stats =
507 http_server_props_manager_->GetServerNetworkStats(mail_server);
508 EXPECT_EQ(NULL, stats);
509 ServerNetworkStats stats1;
510 stats1.srtt = base::TimeDelta::FromMicroseconds(10);
511 http_server_props_manager_->SetServerNetworkStats(mail_server, stats1);
513 // Run the task.
514 base::RunLoop().RunUntilIdle();
515 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
517 const ServerNetworkStats* stats2 =
518 http_server_props_manager_->GetServerNetworkStats(mail_server);
519 EXPECT_EQ(10, stats2->srtt.ToInternalValue());
522 TEST_F(HttpServerPropertiesManagerTest, Clear) {
523 ExpectPrefsUpdate();
525 HostPortPair spdy_server_mail("mail.google.com", 443);
526 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
527 AlternativeService alternative_service(NPN_SPDY_3, "mail.google.com", 443);
528 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
529 alternative_service, 1.0);
530 IPAddressNumber actual_address;
531 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
532 http_server_props_manager_->SetSupportsQuic(true, actual_address);
533 ServerNetworkStats stats;
534 stats.srtt = base::TimeDelta::FromMicroseconds(10);
535 http_server_props_manager_->SetServerNetworkStats(spdy_server_mail, stats);
537 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
538 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
539 const uint32 value1 = 31337;
540 http_server_props_manager_->SetSpdySetting(
541 spdy_server_mail, id1, flags1, value1);
543 // Run the task.
544 base::RunLoop().RunUntilIdle();
546 EXPECT_TRUE(
547 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
548 EXPECT_TRUE(HasAlternativeService(spdy_server_mail));
549 IPAddressNumber address;
550 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
551 EXPECT_EQ(actual_address, address);
552 const ServerNetworkStats* stats1 =
553 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
554 EXPECT_EQ(10, stats1->srtt.ToInternalValue());
556 // Check SPDY settings values.
557 const SettingsMap& settings_map1_ret =
558 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
559 ASSERT_EQ(1U, settings_map1_ret.size());
560 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
561 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
562 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
563 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
564 EXPECT_EQ(value1, flags_and_value1_ret.second);
566 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
568 ExpectPrefsUpdate();
570 // Clear http server data, time out if we do not get a completion callback.
571 http_server_props_manager_->Clear(base::MessageLoop::QuitClosure());
572 base::RunLoop().Run();
574 EXPECT_FALSE(
575 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
576 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
577 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address));
578 const ServerNetworkStats* stats2 =
579 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
580 EXPECT_EQ(NULL, stats2);
582 const SettingsMap& settings_map2_ret =
583 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
584 EXPECT_EQ(0U, settings_map2_ret.size());
586 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
589 // https://crbug.com/444956: Add 200 alternate_protocol servers followed by
590 // supports_quic and verify we have read supports_quic from prefs.
591 TEST_F(HttpServerPropertiesManagerTest, BadSupportsQuic) {
592 ExpectCacheUpdate();
594 base::DictionaryValue* servers_dict = new base::DictionaryValue;
596 for (int i = 0; i < 200; ++i) {
597 // Set up alternate_protocol for www.google.com:i.
598 base::DictionaryValue* alternate_protocol = new base::DictionaryValue;
599 alternate_protocol->SetInteger("port", i);
600 alternate_protocol->SetString("protocol_str", "npn-spdy/3");
601 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
602 server_pref_dict->SetWithoutPathExpansion("alternate_protocol",
603 alternate_protocol);
604 servers_dict->SetWithoutPathExpansion(StringPrintf("www.google.com:%d", i),
605 server_pref_dict);
608 // Set the preference for mail.google.com server.
609 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
611 // Set the server preference for mail.google.com:80.
612 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
613 server_pref_dict1);
615 base::DictionaryValue* http_server_properties_dict =
616 new base::DictionaryValue;
617 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
618 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
620 // Set up SupportsQuic for 127.0.0.1
621 base::DictionaryValue* supports_quic = new base::DictionaryValue;
622 supports_quic->SetBoolean("used_quic", true);
623 supports_quic->SetString("address", "127.0.0.1");
624 http_server_properties_dict->SetWithoutPathExpansion("supports_quic",
625 supports_quic);
627 // Set up the pref.
628 pref_service_.SetManagedPref(kTestHttpServerProperties,
629 http_server_properties_dict);
631 base::RunLoop().RunUntilIdle();
632 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
634 // Verify AlternateProtocol.
635 for (int i = 0; i < 200; ++i) {
636 std::string server = StringPrintf("www.google.com:%d", i);
637 AlternativeService alternative_service =
638 http_server_props_manager_->GetAlternativeService(
639 HostPortPair::FromString(server));
640 EXPECT_EQ(i, alternative_service.port);
641 EXPECT_EQ(NPN_SPDY_3, alternative_service.protocol);
644 // Verify SupportsQuic.
645 IPAddressNumber address;
646 ASSERT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
647 EXPECT_EQ("127.0.0.1", IPAddressToString(address));
650 TEST_F(HttpServerPropertiesManagerTest, UpdateCacheWithPrefs) {
651 const HostPortPair server_www("www.google.com", 80);
652 const HostPortPair server_mail("mail.google.com", 80);
654 // Set alternate protocol.
655 AlternativeService www_altsvc(NPN_SPDY_3, "www.google.com", 443);
656 AlternativeService mail_altsvc(NPN_SPDY_3_1, "mail.google.com", 444);
657 http_server_props_manager_->SetAlternativeService(server_www, www_altsvc,
658 1.0);
659 http_server_props_manager_->SetAlternativeService(server_mail, mail_altsvc,
660 0.2);
662 // Set ServerNetworkStats.
663 ServerNetworkStats stats;
664 stats.srtt = base::TimeDelta::FromInternalValue(42);
665 http_server_props_manager_->SetServerNetworkStats(server_mail, stats);
667 // Set SupportsQuic.
668 IPAddressNumber actual_address;
669 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
670 http_server_props_manager_->SetSupportsQuic(true, actual_address);
672 // Update cache.
673 ExpectPrefsUpdate();
674 ExpectCacheUpdate();
675 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
676 base::RunLoop().RunUntilIdle();
678 // Verify preferences.
679 const char expected_json[] = "{"
680 "\"servers\":{"
681 "\"mail.google.com:80\":{"
682 "\"alternate_protocol\":{"
683 "\"port\":444,\"probability\":0.2,\"protocol_str\":\"npn-spdy/3.1\""
684 "},"
685 "\"network_stats\":{"
686 "\"srtt\":42"
688 "},"
689 "\"www.google.com:80\":{"
690 "\"alternate_protocol\":{"
691 "\"port\":443,\"probability\":1.0,\"protocol_str\":\"npn-spdy/3\""
694 "},"
695 "\"supports_quic\":{"
696 "\"address\":\"127.0.0.1\",\"used_quic\":true"
697 "},"
698 "\"version\":3"
699 "}";
701 const base::Value* http_server_properties =
702 pref_service_.GetUserPref(kTestHttpServerProperties);
703 ASSERT_NE(nullptr, http_server_properties);
704 std::string preferences_json;
705 EXPECT_TRUE(
706 base::JSONWriter::Write(http_server_properties, &preferences_json));
707 EXPECT_EQ(expected_json, preferences_json);
710 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache0) {
711 // Post an update task to the UI thread.
712 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
713 // Shutdown comes before the task is executed.
714 http_server_props_manager_->ShutdownOnPrefThread();
715 http_server_props_manager_.reset();
716 // Run the task after shutdown and deletion.
717 base::RunLoop().RunUntilIdle();
720 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache1) {
721 // Post an update task.
722 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
723 // Shutdown comes before the task is executed.
724 http_server_props_manager_->ShutdownOnPrefThread();
725 // Run the task after shutdown, but before deletion.
726 base::RunLoop().RunUntilIdle();
727 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
728 http_server_props_manager_.reset();
729 base::RunLoop().RunUntilIdle();
732 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache2) {
733 http_server_props_manager_->UpdateCacheFromPrefsOnUIConcrete();
734 // Shutdown comes before the task is executed.
735 http_server_props_manager_->ShutdownOnPrefThread();
736 // Run the task after shutdown, but before deletion.
737 base::RunLoop().RunUntilIdle();
738 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
739 http_server_props_manager_.reset();
740 base::RunLoop().RunUntilIdle();
744 // Tests for shutdown when updating prefs.
746 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs0) {
747 // Post an update task to the IO thread.
748 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
749 // Shutdown comes before the task is executed.
750 http_server_props_manager_->ShutdownOnPrefThread();
751 http_server_props_manager_.reset();
752 // Run the task after shutdown and deletion.
753 base::RunLoop().RunUntilIdle();
756 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs1) {
757 ExpectPrefsUpdate();
758 // Post an update task.
759 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
760 // Shutdown comes before the task is executed.
761 http_server_props_manager_->ShutdownOnPrefThread();
762 // Run the task after shutdown, but before deletion.
763 base::RunLoop().RunUntilIdle();
764 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
765 http_server_props_manager_.reset();
766 base::RunLoop().RunUntilIdle();
769 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs2) {
770 // This posts a task to the UI thread.
771 http_server_props_manager_->UpdatePrefsFromCacheOnNetworkThreadConcrete(
772 base::Closure());
773 // Shutdown comes before the task is executed.
774 http_server_props_manager_->ShutdownOnPrefThread();
775 // Run the task after shutdown, but before deletion.
776 base::RunLoop().RunUntilIdle();
777 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
778 http_server_props_manager_.reset();
779 base::RunLoop().RunUntilIdle();
782 } // namespace
784 } // namespace net