Rewrite AndroidSyncSettings to be significantly simpler.
[chromium-blink-merge.git] / net / http / http_server_properties_manager_unittest.cc
blobf2e36499e736850c19b757f9e8146e5d6adbf96c
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 HasAlternateProtocol(const HostPortPair& server) {
139 const AlternateProtocolInfo alternate =
140 http_server_props_manager_->GetAlternateProtocol(server);
141 return alternate.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 AlternateProtocolInfo port_alternate_protocol =
234 http_server_props_manager_->GetAlternateProtocol(google_server);
235 EXPECT_EQ(443, port_alternate_protocol.port);
236 EXPECT_EQ(NPN_SPDY_3, port_alternate_protocol.protocol);
237 port_alternate_protocol =
238 http_server_props_manager_->GetAlternateProtocol(mail_server);
239 EXPECT_EQ(444, port_alternate_protocol.port);
240 EXPECT_EQ(NPN_SPDY_3_1, port_alternate_protocol.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 HasAlternateProtocol(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 HasAlternateProtocol(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, GetAlternateProtocol) {
466 ExpectPrefsUpdate();
468 HostPortPair spdy_server_mail("mail.google.com", 80);
469 EXPECT_FALSE(HasAlternateProtocol(spdy_server_mail));
470 http_server_props_manager_->SetAlternateProtocol(spdy_server_mail, 443,
471 NPN_SPDY_3, 1.0);
473 // Run the task.
474 base::RunLoop().RunUntilIdle();
475 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
477 const AlternateProtocolInfo alternate_protocol =
478 http_server_props_manager_->GetAlternateProtocol(spdy_server_mail);
479 EXPECT_EQ(443, alternate_protocol.port);
480 EXPECT_EQ(NPN_SPDY_3, alternate_protocol.protocol);
481 EXPECT_EQ(1.0, alternate_protocol.probability);
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 http_server_props_manager_->SetAlternateProtocol(spdy_server_mail, 443,
528 NPN_SPDY_3, 1.0);
529 IPAddressNumber actual_address;
530 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
531 http_server_props_manager_->SetSupportsQuic(true, actual_address);
532 ServerNetworkStats stats;
533 stats.srtt = base::TimeDelta::FromMicroseconds(10);
534 http_server_props_manager_->SetServerNetworkStats(spdy_server_mail, stats);
536 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
537 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
538 const uint32 value1 = 31337;
539 http_server_props_manager_->SetSpdySetting(
540 spdy_server_mail, id1, flags1, value1);
542 // Run the task.
543 base::RunLoop().RunUntilIdle();
545 EXPECT_TRUE(
546 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
547 EXPECT_TRUE(HasAlternateProtocol(spdy_server_mail));
548 IPAddressNumber address;
549 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
550 EXPECT_EQ(actual_address, address);
551 const ServerNetworkStats* stats1 =
552 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
553 EXPECT_EQ(10, stats1->srtt.ToInternalValue());
555 // Check SPDY settings values.
556 const SettingsMap& settings_map1_ret =
557 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
558 ASSERT_EQ(1U, settings_map1_ret.size());
559 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
560 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
561 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
562 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
563 EXPECT_EQ(value1, flags_and_value1_ret.second);
565 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
567 ExpectPrefsUpdate();
569 // Clear http server data, time out if we do not get a completion callback.
570 http_server_props_manager_->Clear(base::MessageLoop::QuitClosure());
571 base::RunLoop().Run();
573 EXPECT_FALSE(
574 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
575 EXPECT_FALSE(HasAlternateProtocol(spdy_server_mail));
576 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address));
577 const ServerNetworkStats* stats2 =
578 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
579 EXPECT_EQ(NULL, stats2);
581 const SettingsMap& settings_map2_ret =
582 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
583 EXPECT_EQ(0U, settings_map2_ret.size());
585 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
588 // https://crbug.com/444956: Add 200 alternate_protocol servers followed by
589 // supports_quic and verify we have read supports_quic from prefs.
590 TEST_F(HttpServerPropertiesManagerTest, BadSupportsQuic) {
591 ExpectCacheUpdate();
593 base::DictionaryValue* servers_dict = new base::DictionaryValue;
595 for (int i = 0; i < 200; ++i) {
596 // Set up alternate_protocol for www.google.com:i.
597 base::DictionaryValue* alternate_protocol = new base::DictionaryValue;
598 alternate_protocol->SetInteger("port", i);
599 alternate_protocol->SetString("protocol_str", "npn-spdy/3");
600 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
601 server_pref_dict->SetWithoutPathExpansion("alternate_protocol",
602 alternate_protocol);
603 servers_dict->SetWithoutPathExpansion(StringPrintf("www.google.com:%d", i),
604 server_pref_dict);
607 // Set the preference for mail.google.com server.
608 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
610 // Set the server preference for mail.google.com:80.
611 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
612 server_pref_dict1);
614 base::DictionaryValue* http_server_properties_dict =
615 new base::DictionaryValue;
616 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
617 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
619 // Set up SupportsQuic for 127.0.0.1
620 base::DictionaryValue* supports_quic = new base::DictionaryValue;
621 supports_quic->SetBoolean("used_quic", true);
622 supports_quic->SetString("address", "127.0.0.1");
623 http_server_properties_dict->SetWithoutPathExpansion("supports_quic",
624 supports_quic);
626 // Set up the pref.
627 pref_service_.SetManagedPref(kTestHttpServerProperties,
628 http_server_properties_dict);
630 base::RunLoop().RunUntilIdle();
631 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
633 // Verify AlternateProtocol.
634 for (int i = 0; i < 200; ++i) {
635 std::string server = StringPrintf("www.google.com:%d", i);
636 AlternateProtocolInfo port_alternate_protocol =
637 http_server_props_manager_->GetAlternateProtocol(
638 HostPortPair::FromString(server));
639 EXPECT_EQ(i, port_alternate_protocol.port);
640 EXPECT_EQ(NPN_SPDY_3, port_alternate_protocol.protocol);
643 // Verify SupportsQuic.
644 IPAddressNumber address;
645 ASSERT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
646 EXPECT_EQ("127.0.0.1", IPAddressToString(address));
649 TEST_F(HttpServerPropertiesManagerTest, UpdateCacheWithPrefs) {
650 const HostPortPair server_www("www.google.com", 80);
651 const HostPortPair server_mail("mail.google.com", 80);
653 // Set alternate protocol.
654 http_server_props_manager_->SetAlternateProtocol(server_www, 443, NPN_SPDY_3,
655 1.0);
656 http_server_props_manager_->SetAlternateProtocol(server_mail, 444,
657 NPN_SPDY_3_1, 0.2);
659 // Set ServerNetworkStats.
660 ServerNetworkStats stats;
661 stats.srtt = base::TimeDelta::FromInternalValue(42);
662 http_server_props_manager_->SetServerNetworkStats(server_mail, stats);
664 // Set SupportsQuic.
665 IPAddressNumber actual_address;
666 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
667 http_server_props_manager_->SetSupportsQuic(true, actual_address);
669 // Update cache.
670 ExpectPrefsUpdate();
671 ExpectCacheUpdate();
672 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
673 base::RunLoop().RunUntilIdle();
675 // Verify preferences.
676 const char expected_json[] = "{"
677 "\"servers\":{"
678 "\"mail.google.com:80\":{"
679 "\"alternate_protocol\":{"
680 "\"port\":444,\"probability\":0.2,\"protocol_str\":\"npn-spdy/3.1\""
681 "},"
682 "\"network_stats\":{"
683 "\"srtt\":42"
685 "},"
686 "\"www.google.com:80\":{"
687 "\"alternate_protocol\":{"
688 "\"port\":443,\"probability\":1.0,\"protocol_str\":\"npn-spdy/3\""
691 "},"
692 "\"supports_quic\":{"
693 "\"address\":\"127.0.0.1\",\"used_quic\":true"
694 "},"
695 "\"version\":3"
696 "}";
698 const base::Value* http_server_properties =
699 pref_service_.GetUserPref(kTestHttpServerProperties);
700 ASSERT_NE(nullptr, http_server_properties);
701 std::string preferences_json;
702 EXPECT_TRUE(
703 base::JSONWriter::Write(http_server_properties, &preferences_json));
704 EXPECT_EQ(expected_json, preferences_json);
707 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache0) {
708 // Post an update task to the UI thread.
709 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
710 // Shutdown comes before the task is executed.
711 http_server_props_manager_->ShutdownOnPrefThread();
712 http_server_props_manager_.reset();
713 // Run the task after shutdown and deletion.
714 base::RunLoop().RunUntilIdle();
717 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache1) {
718 // Post an update task.
719 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
720 // Shutdown comes before the task is executed.
721 http_server_props_manager_->ShutdownOnPrefThread();
722 // Run the task after shutdown, but before deletion.
723 base::RunLoop().RunUntilIdle();
724 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
725 http_server_props_manager_.reset();
726 base::RunLoop().RunUntilIdle();
729 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache2) {
730 http_server_props_manager_->UpdateCacheFromPrefsOnUIConcrete();
731 // Shutdown comes before the task is executed.
732 http_server_props_manager_->ShutdownOnPrefThread();
733 // Run the task after shutdown, but before deletion.
734 base::RunLoop().RunUntilIdle();
735 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
736 http_server_props_manager_.reset();
737 base::RunLoop().RunUntilIdle();
741 // Tests for shutdown when updating prefs.
743 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs0) {
744 // Post an update task to the IO thread.
745 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
746 // Shutdown comes before the task is executed.
747 http_server_props_manager_->ShutdownOnPrefThread();
748 http_server_props_manager_.reset();
749 // Run the task after shutdown and deletion.
750 base::RunLoop().RunUntilIdle();
753 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs1) {
754 ExpectPrefsUpdate();
755 // Post an update task.
756 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
757 // Shutdown comes before the task is executed.
758 http_server_props_manager_->ShutdownOnPrefThread();
759 // Run the task after shutdown, but before deletion.
760 base::RunLoop().RunUntilIdle();
761 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
762 http_server_props_manager_.reset();
763 base::RunLoop().RunUntilIdle();
766 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs2) {
767 // This posts a task to the UI thread.
768 http_server_props_manager_->UpdatePrefsFromCacheOnNetworkThreadConcrete(
769 base::Closure());
770 // Shutdown comes before the task is executed.
771 http_server_props_manager_->ShutdownOnPrefThread();
772 // Run the task after shutdown, but before deletion.
773 base::RunLoop().RunUntilIdle();
774 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
775 http_server_props_manager_.reset();
776 base::RunLoop().RunUntilIdle();
779 } // namespace
781 } // namespace net