Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / http / http_server_properties_manager_unittest.cc
blobc1eb8d0e87b76c09e65ee93ba247bbd23d09566b
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/single_thread_task_runner.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/test/test_simple_task_runner.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "base/values.h"
19 #include "net/base/ip_address_number.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "url/gurl.h"
24 namespace net {
26 namespace {
28 using base::StringPrintf;
29 using ::testing::_;
30 using ::testing::Invoke;
31 using ::testing::Mock;
32 using ::testing::StrictMock;
34 const char kTestHttpServerProperties[] = "TestHttpServerProperties";
36 class TestingHttpServerPropertiesManager : public HttpServerPropertiesManager {
37 public:
38 TestingHttpServerPropertiesManager(
39 PrefService* pref_service,
40 const char* pref_path,
41 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
42 : HttpServerPropertiesManager(pref_service, pref_path, io_task_runner) {
43 InitializeOnNetworkThread();
46 ~TestingHttpServerPropertiesManager() override {}
48 // Make these methods public for testing.
49 using HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread;
51 // Post tasks without a delay during tests.
52 void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay) override {
53 HttpServerPropertiesManager::StartPrefsUpdateTimerOnNetworkThread(
54 base::TimeDelta());
57 void UpdateCacheFromPrefsOnUIConcrete() {
58 HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread();
61 // Post tasks without a delay during tests.
62 void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay) override {
63 HttpServerPropertiesManager::StartCacheUpdateTimerOnPrefThread(
64 base::TimeDelta());
67 void UpdatePrefsFromCacheOnNetworkThreadConcrete(
68 const base::Closure& callback) {
69 HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread(callback);
72 void ScheduleUpdatePrefsOnNetworkThreadConcrete(Location location) {
73 HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread(location);
76 void ScheduleUpdatePrefsOnNetworkThread() {
77 // Picked a random Location as caller.
78 HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread(
79 DETECTED_CORRUPTED_PREFS);
82 MOCK_METHOD0(UpdateCacheFromPrefsOnPrefThread, void());
83 MOCK_METHOD1(UpdatePrefsFromCacheOnNetworkThread, void(const base::Closure&));
84 MOCK_METHOD1(ScheduleUpdatePrefsOnNetworkThread, void(Location location));
85 MOCK_METHOD6(UpdateCacheFromPrefsOnNetworkThread,
86 void(std::vector<std::string>* spdy_servers,
87 SpdySettingsMap* spdy_settings_map,
88 AlternativeServiceMap* alternative_service_map,
89 IPAddressNumber* last_quic_address,
90 ServerNetworkStatsMap* server_network_stats_map,
91 bool detected_corrupted_prefs));
92 MOCK_METHOD5(UpdatePrefsOnPref,
93 void(base::ListValue* spdy_server_list,
94 SpdySettingsMap* spdy_settings_map,
95 AlternativeServiceMap* alternative_service_map,
96 IPAddressNumber* last_quic_address,
97 ServerNetworkStatsMap* server_network_stats_map));
99 private:
100 DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager);
103 class HttpServerPropertiesManagerTest : public testing::Test {
104 protected:
105 HttpServerPropertiesManagerTest() {}
107 void SetUp() override {
108 pref_service_.registry()->RegisterDictionaryPref(kTestHttpServerProperties);
109 http_server_props_manager_.reset(
110 new StrictMock<TestingHttpServerPropertiesManager>(
111 &pref_service_, kTestHttpServerProperties,
112 base::ThreadTaskRunnerHandle::Get()));
113 ExpectCacheUpdate();
114 base::RunLoop().RunUntilIdle();
117 void TearDown() override {
118 if (http_server_props_manager_.get())
119 http_server_props_manager_->ShutdownOnPrefThread();
120 base::RunLoop().RunUntilIdle();
121 http_server_props_manager_.reset();
124 void ExpectCacheUpdate() {
125 EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefsOnPrefThread())
126 .WillOnce(Invoke(http_server_props_manager_.get(),
127 &TestingHttpServerPropertiesManager::
128 UpdateCacheFromPrefsOnUIConcrete));
131 void ExpectScheduleUpdatePrefsOnNetworkThread() {
132 EXPECT_CALL(*http_server_props_manager_,
133 ScheduleUpdatePrefsOnNetworkThread(_))
134 .WillOnce(Invoke(http_server_props_manager_.get(),
135 &TestingHttpServerPropertiesManager::
136 ScheduleUpdatePrefsOnNetworkThreadConcrete));
139 void ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly() {
140 EXPECT_CALL(*http_server_props_manager_,
141 ScheduleUpdatePrefsOnNetworkThread(_))
142 .WillRepeatedly(Invoke(http_server_props_manager_.get(),
143 &TestingHttpServerPropertiesManager::
144 ScheduleUpdatePrefsOnNetworkThreadConcrete));
147 void ExpectPrefsUpdate() {
148 EXPECT_CALL(*http_server_props_manager_,
149 UpdatePrefsFromCacheOnNetworkThread(_))
150 .WillOnce(Invoke(http_server_props_manager_.get(),
151 &TestingHttpServerPropertiesManager::
152 UpdatePrefsFromCacheOnNetworkThreadConcrete));
155 void ExpectPrefsUpdateRepeatedly() {
156 EXPECT_CALL(*http_server_props_manager_,
157 UpdatePrefsFromCacheOnNetworkThread(_))
158 .WillRepeatedly(
159 Invoke(http_server_props_manager_.get(),
160 &TestingHttpServerPropertiesManager::
161 UpdatePrefsFromCacheOnNetworkThreadConcrete));
164 bool HasAlternativeService(const HostPortPair& server) {
165 const AlternativeServiceVector alternative_service_vector =
166 http_server_props_manager_->GetAlternativeServices(server);
167 return !alternative_service_vector.empty();
170 //base::RunLoop loop_;
171 TestingPrefServiceSimple pref_service_;
172 scoped_ptr<TestingHttpServerPropertiesManager> http_server_props_manager_;
174 private:
175 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest);
178 TEST_F(HttpServerPropertiesManagerTest,
179 SingleUpdateForTwoSpdyServerPrefChanges) {
180 ExpectCacheUpdate();
182 // Set up the prefs for www.google.com:80 and mail.google.com:80 and then set
183 // it twice. Only expect a single cache update.
185 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
186 HostPortPair google_server("www.google.com", 80);
187 HostPortPair mail_server("mail.google.com", 80);
189 // Set supports_spdy for www.google.com:80.
190 server_pref_dict->SetBoolean("supports_spdy", true);
192 // Set up alternative_services for www.google.com:80.
193 base::DictionaryValue* alternative_service_dict0 = new base::DictionaryValue;
194 alternative_service_dict0->SetInteger("port", 443);
195 alternative_service_dict0->SetString("protocol_str", "npn-h2");
196 base::DictionaryValue* alternative_service_dict1 = new base::DictionaryValue;
197 alternative_service_dict1->SetInteger("port", 1234);
198 alternative_service_dict1->SetString("protocol_str", "quic");
199 base::ListValue* alternative_service_list = new base::ListValue;
200 alternative_service_list->Append(alternative_service_dict0);
201 alternative_service_list->Append(alternative_service_dict1);
202 server_pref_dict->SetWithoutPathExpansion("alternative_service",
203 alternative_service_list);
205 // Set up ServerNetworkStats for www.google.com:80.
206 base::DictionaryValue* stats = new base::DictionaryValue;
207 stats->SetInteger("srtt", 10);
208 server_pref_dict->SetWithoutPathExpansion("network_stats", stats);
210 // Set the server preference for www.google.com:80.
211 base::DictionaryValue* servers_dict = new base::DictionaryValue;
212 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict);
214 // Set the preference for mail.google.com server.
215 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
217 // Set supports_spdy for mail.google.com:80
218 server_pref_dict1->SetBoolean("supports_spdy", true);
220 // Set up alternate_protocol for mail.google.com:80 to test migration to
221 // alternative_service.
222 base::DictionaryValue* alternate_protocol_dict = new base::DictionaryValue;
223 alternate_protocol_dict->SetString("protocol_str", "npn-spdy/3.1");
224 alternate_protocol_dict->SetInteger("port", 444);
225 server_pref_dict1->SetWithoutPathExpansion("alternate_protocol",
226 alternate_protocol_dict);
228 // Set up ServerNetworkStats for mail.google.com:80.
229 base::DictionaryValue* stats1 = new base::DictionaryValue;
230 stats1->SetInteger("srtt", 20);
231 server_pref_dict1->SetWithoutPathExpansion("network_stats", stats1);
232 // Set the server preference for mail.google.com:80.
233 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
234 server_pref_dict1);
236 base::DictionaryValue* http_server_properties_dict =
237 new base::DictionaryValue;
238 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
239 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
240 base::DictionaryValue* supports_quic = new base::DictionaryValue;
241 supports_quic->SetBoolean("used_quic", true);
242 supports_quic->SetString("address", "127.0.0.1");
243 http_server_properties_dict->SetWithoutPathExpansion("supports_quic",
244 supports_quic);
246 // Set the same value for kHttpServerProperties multiple times.
247 pref_service_.SetManagedPref(kTestHttpServerProperties,
248 http_server_properties_dict);
249 base::DictionaryValue* http_server_properties_dict2 =
250 http_server_properties_dict->DeepCopy();
251 pref_service_.SetManagedPref(kTestHttpServerProperties,
252 http_server_properties_dict2);
254 base::RunLoop().RunUntilIdle();
255 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
257 // Verify SupportsSpdy.
258 EXPECT_TRUE(
259 http_server_props_manager_->SupportsRequestPriority(google_server));
260 EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(mail_server));
261 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority(
262 HostPortPair::FromString("foo.google.com:1337")));
264 // Verify alternative service.
265 const AlternativeServiceMap& map =
266 http_server_props_manager_->alternative_service_map();
267 ASSERT_EQ(2u, map.size());
268 AlternativeServiceMap::const_iterator map_it = map.begin();
269 EXPECT_EQ("www.google.com", map_it->first.host());
270 ASSERT_EQ(2u, map_it->second.size());
271 EXPECT_EQ(NPN_HTTP_2, map_it->second[0].alternative_service.protocol);
272 EXPECT_TRUE(map_it->second[0].alternative_service.host.empty());
273 EXPECT_EQ(443, map_it->second[0].alternative_service.port);
274 EXPECT_EQ(QUIC, map_it->second[1].alternative_service.protocol);
275 EXPECT_TRUE(map_it->second[1].alternative_service.host.empty());
276 EXPECT_EQ(1234, map_it->second[1].alternative_service.port);
277 ++map_it;
278 EXPECT_EQ("mail.google.com", map_it->first.host());
279 ASSERT_EQ(1u, map_it->second.size());
280 EXPECT_EQ(NPN_SPDY_3_1, map_it->second[0].alternative_service.protocol);
281 EXPECT_TRUE(map_it->second[0].alternative_service.host.empty());
282 EXPECT_EQ(444, map_it->second[0].alternative_service.port);
284 // Verify SupportsQuic.
285 IPAddressNumber last_address;
286 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&last_address));
287 EXPECT_EQ("127.0.0.1", IPAddressToString(last_address));
289 // Verify ServerNetworkStats.
290 const ServerNetworkStats* stats2 =
291 http_server_props_manager_->GetServerNetworkStats(google_server);
292 EXPECT_EQ(10, stats2->srtt.ToInternalValue());
293 const ServerNetworkStats* stats3 =
294 http_server_props_manager_->GetServerNetworkStats(mail_server);
295 EXPECT_EQ(20, stats3->srtt.ToInternalValue());
298 TEST_F(HttpServerPropertiesManagerTest, BadCachedHostPortPair) {
299 ExpectCacheUpdate();
300 // The prefs are automaticalls updated in the case corruption is detected.
301 ExpectPrefsUpdate();
302 ExpectScheduleUpdatePrefsOnNetworkThread();
304 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
306 // Set supports_spdy for www.google.com:65536.
307 server_pref_dict->SetBoolean("supports_spdy", true);
309 // Set up alternative_service for www.google.com:65536.
310 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
311 alternative_service_dict->SetString("protocol_str", "npn-h2");
312 alternative_service_dict->SetInteger("port", 80);
313 base::ListValue* alternative_service_list = new base::ListValue;
314 alternative_service_list->Append(alternative_service_dict);
315 server_pref_dict->SetWithoutPathExpansion("alternative_service",
316 alternative_service_list);
318 // Set up ServerNetworkStats for www.google.com:65536.
319 base::DictionaryValue* stats = new base::DictionaryValue;
320 stats->SetInteger("srtt", 10);
321 server_pref_dict->SetWithoutPathExpansion("network_stats", stats);
323 // Set the server preference for www.google.com:65536.
324 base::DictionaryValue* servers_dict = new base::DictionaryValue;
325 servers_dict->SetWithoutPathExpansion("www.google.com:65536",
326 server_pref_dict);
328 base::DictionaryValue* http_server_properties_dict =
329 new base::DictionaryValue;
330 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
331 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
333 // Set up the pref.
334 pref_service_.SetManagedPref(kTestHttpServerProperties,
335 http_server_properties_dict);
337 base::RunLoop().RunUntilIdle();
338 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
340 // Verify that nothing is set.
341 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority(
342 HostPortPair::FromString("www.google.com:65536")));
343 EXPECT_FALSE(
344 HasAlternativeService(HostPortPair::FromString("www.google.com:65536")));
345 const ServerNetworkStats* stats1 =
346 http_server_props_manager_->GetServerNetworkStats(
347 HostPortPair::FromString("www.google.com:65536"));
348 EXPECT_EQ(NULL, stats1);
351 TEST_F(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) {
352 ExpectCacheUpdate();
353 // The prefs are automaticalls updated in the case corruption is detected.
354 ExpectPrefsUpdate();
355 ExpectScheduleUpdatePrefsOnNetworkThread();
357 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
359 // Set supports_spdy for www.google.com:80.
360 server_pref_dict->SetBoolean("supports_spdy", true);
362 // Set up alternative_service for www.google.com:80.
363 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
364 alternative_service_dict->SetString("protocol_str", "npn-h2");
365 alternative_service_dict->SetInteger("port", 65536);
366 base::ListValue* alternative_service_list = new base::ListValue;
367 alternative_service_list->Append(alternative_service_dict);
368 server_pref_dict->SetWithoutPathExpansion("alternative_service",
369 alternative_service_list);
371 // Set the server preference for www.google.com:80.
372 base::DictionaryValue* servers_dict = new base::DictionaryValue;
373 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict);
375 base::DictionaryValue* http_server_properties_dict =
376 new base::DictionaryValue;
377 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
378 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
380 // Set up the pref.
381 pref_service_.SetManagedPref(kTestHttpServerProperties,
382 http_server_properties_dict);
384 base::RunLoop().RunUntilIdle();
385 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
387 // Verify alternative service is not set.
388 EXPECT_FALSE(
389 HasAlternativeService(HostPortPair::FromString("www.google.com:80")));
392 TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) {
393 ExpectPrefsUpdate();
394 ExpectScheduleUpdatePrefsOnNetworkThread();
396 // Post an update task to the network thread. SetSupportsSpdy calls
397 // ScheduleUpdatePrefsOnNetworkThread.
399 // Add mail.google.com:443 as a supporting spdy server.
400 HostPortPair spdy_server_mail("mail.google.com", 443);
401 EXPECT_FALSE(
402 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
403 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
404 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
405 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
407 // Run the task.
408 base::RunLoop().RunUntilIdle();
410 EXPECT_TRUE(
411 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
412 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
415 TEST_F(HttpServerPropertiesManagerTest, SetSpdySetting) {
416 ExpectPrefsUpdate();
417 ExpectScheduleUpdatePrefsOnNetworkThread();
419 // Add SpdySetting for mail.google.com:443.
420 HostPortPair spdy_server_mail("mail.google.com", 443);
421 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
422 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
423 const uint32 value1 = 31337;
424 http_server_props_manager_->SetSpdySetting(
425 spdy_server_mail, id1, flags1, value1);
427 // Run the task.
428 base::RunLoop().RunUntilIdle();
430 const SettingsMap& settings_map1_ret =
431 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
432 ASSERT_EQ(1U, settings_map1_ret.size());
433 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
434 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
435 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
436 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
437 EXPECT_EQ(value1, flags_and_value1_ret.second);
439 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
442 TEST_F(HttpServerPropertiesManagerTest, ClearSpdySetting) {
443 ExpectPrefsUpdateRepeatedly();
444 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly();
446 // Add SpdySetting for mail.google.com:443.
447 HostPortPair spdy_server_mail("mail.google.com", 443);
448 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
449 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
450 const uint32 value1 = 31337;
451 http_server_props_manager_->SetSpdySetting(
452 spdy_server_mail, id1, flags1, value1);
454 // Run the task.
455 base::RunLoop().RunUntilIdle();
457 const SettingsMap& settings_map1_ret =
458 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
459 ASSERT_EQ(1U, settings_map1_ret.size());
460 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
461 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
462 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
463 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
464 EXPECT_EQ(value1, flags_and_value1_ret.second);
466 // Clear SpdySetting for mail.google.com:443.
467 http_server_props_manager_->ClearSpdySettings(spdy_server_mail);
469 // Run the task.
470 base::RunLoop().RunUntilIdle();
472 // Verify that there are no entries in the settings map for
473 // mail.google.com:443.
474 const SettingsMap& settings_map2_ret =
475 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
476 ASSERT_EQ(0U, settings_map2_ret.size());
478 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
481 TEST_F(HttpServerPropertiesManagerTest, ClearAllSpdySetting) {
482 ExpectPrefsUpdateRepeatedly();
483 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly();
485 // Add SpdySetting for mail.google.com:443.
486 HostPortPair spdy_server_mail("mail.google.com", 443);
487 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
488 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
489 const uint32 value1 = 31337;
490 http_server_props_manager_->SetSpdySetting(
491 spdy_server_mail, id1, flags1, value1);
493 // Run the task.
494 base::RunLoop().RunUntilIdle();
496 const SettingsMap& settings_map1_ret =
497 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
498 ASSERT_EQ(1U, settings_map1_ret.size());
499 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
500 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
501 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
502 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
503 EXPECT_EQ(value1, flags_and_value1_ret.second);
505 // Clear All SpdySettings.
506 http_server_props_manager_->ClearAllSpdySettings();
508 // Run the task.
509 base::RunLoop().RunUntilIdle();
511 // Verify that there are no entries in the settings map.
512 const SpdySettingsMap& spdy_settings_map2_ret =
513 http_server_props_manager_->spdy_settings_map();
514 ASSERT_EQ(0U, spdy_settings_map2_ret.size());
516 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
519 TEST_F(HttpServerPropertiesManagerTest, GetAlternativeServices) {
520 ExpectPrefsUpdate();
521 ExpectScheduleUpdatePrefsOnNetworkThread();
523 HostPortPair spdy_server_mail("mail.google.com", 80);
524 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
525 const AlternativeService alternative_service(NPN_HTTP_2, "mail.google.com",
526 443);
527 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
528 alternative_service, 1.0);
529 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
530 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
531 alternative_service, 1.0);
533 // Run the task.
534 base::RunLoop().RunUntilIdle();
535 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
537 AlternativeServiceVector alternative_service_vector =
538 http_server_props_manager_->GetAlternativeServices(spdy_server_mail);
539 ASSERT_EQ(1u, alternative_service_vector.size());
540 EXPECT_EQ(alternative_service, alternative_service_vector[0]);
543 TEST_F(HttpServerPropertiesManagerTest, SetAlternativeServices) {
544 ExpectPrefsUpdate();
545 ExpectScheduleUpdatePrefsOnNetworkThread();
547 HostPortPair spdy_server_mail("mail.google.com", 80);
548 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
549 AlternativeServiceInfoVector alternative_service_info_vector;
550 const AlternativeService alternative_service1(NPN_HTTP_2, "mail.google.com",
551 443);
552 alternative_service_info_vector.push_back(
553 AlternativeServiceInfo(alternative_service1, 1.0));
554 const AlternativeService alternative_service2(QUIC, "mail.google.com", 1234);
555 alternative_service_info_vector.push_back(
556 AlternativeServiceInfo(alternative_service2, 1.0));
557 http_server_props_manager_->SetAlternativeServices(
558 spdy_server_mail, alternative_service_info_vector);
559 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
560 http_server_props_manager_->SetAlternativeServices(
561 spdy_server_mail, alternative_service_info_vector);
563 // Run the task.
564 base::RunLoop().RunUntilIdle();
565 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
567 AlternativeServiceVector alternative_service_vector =
568 http_server_props_manager_->GetAlternativeServices(spdy_server_mail);
569 ASSERT_EQ(2u, alternative_service_vector.size());
570 EXPECT_EQ(alternative_service1, alternative_service_vector[0]);
571 EXPECT_EQ(alternative_service2, alternative_service_vector[1]);
574 TEST_F(HttpServerPropertiesManagerTest, SetAlternativeServicesEmpty) {
575 HostPortPair spdy_server_mail("mail.google.com", 80);
576 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
577 const AlternativeService alternative_service(NPN_HTTP_2, "mail.google.com",
578 443);
579 http_server_props_manager_->SetAlternativeServices(
580 spdy_server_mail, AlternativeServiceInfoVector());
581 // ExpectScheduleUpdatePrefsOnNetworkThread() should not be called.
583 // Run the task.
584 base::RunLoop().RunUntilIdle();
585 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
587 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
590 TEST_F(HttpServerPropertiesManagerTest, ClearAlternativeServices) {
591 ExpectPrefsUpdate();
592 ExpectScheduleUpdatePrefsOnNetworkThread();
594 HostPortPair spdy_server_mail("mail.google.com", 80);
595 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
596 AlternativeService alternative_service(NPN_HTTP_2, "mail.google.com", 443);
597 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
598 alternative_service, 1.0);
599 ExpectScheduleUpdatePrefsOnNetworkThread();
600 http_server_props_manager_->ClearAlternativeServices(spdy_server_mail);
601 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
602 http_server_props_manager_->ClearAlternativeServices(spdy_server_mail);
604 // Run the task.
605 base::RunLoop().RunUntilIdle();
606 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
608 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
611 TEST_F(HttpServerPropertiesManagerTest, ConfirmAlternativeService) {
612 ExpectPrefsUpdate();
614 HostPortPair spdy_server_mail("mail.google.com", 80);
615 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
616 AlternativeService alternative_service(NPN_HTTP_2, "mail.google.com", 443);
618 ExpectScheduleUpdatePrefsOnNetworkThread();
619 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
620 alternative_service, 1.0);
622 EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
623 alternative_service));
624 EXPECT_FALSE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
625 alternative_service));
627 ExpectScheduleUpdatePrefsOnNetworkThread();
628 http_server_props_manager_->MarkAlternativeServiceBroken(alternative_service);
629 EXPECT_TRUE(http_server_props_manager_->IsAlternativeServiceBroken(
630 alternative_service));
631 EXPECT_TRUE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
632 alternative_service));
634 ExpectScheduleUpdatePrefsOnNetworkThread();
635 http_server_props_manager_->ConfirmAlternativeService(alternative_service);
636 EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
637 alternative_service));
638 EXPECT_FALSE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
639 alternative_service));
640 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
641 http_server_props_manager_->ConfirmAlternativeService(alternative_service);
642 EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
643 alternative_service));
644 EXPECT_FALSE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
645 alternative_service));
647 // Run the task.
648 base::RunLoop().RunUntilIdle();
649 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
651 EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
652 alternative_service));
653 EXPECT_FALSE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
654 alternative_service));
657 TEST_F(HttpServerPropertiesManagerTest, SupportsQuic) {
658 ExpectPrefsUpdate();
659 ExpectScheduleUpdatePrefsOnNetworkThread();
661 IPAddressNumber address;
662 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address));
664 IPAddressNumber actual_address;
665 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
666 http_server_props_manager_->SetSupportsQuic(true, actual_address);
667 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
668 http_server_props_manager_->SetSupportsQuic(true, actual_address);
670 // Run the task.
671 base::RunLoop().RunUntilIdle();
672 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
674 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
675 EXPECT_EQ(actual_address, address);
678 TEST_F(HttpServerPropertiesManagerTest, ServerNetworkStats) {
679 ExpectPrefsUpdate();
680 ExpectScheduleUpdatePrefsOnNetworkThread();
682 HostPortPair mail_server("mail.google.com", 80);
683 const ServerNetworkStats* stats =
684 http_server_props_manager_->GetServerNetworkStats(mail_server);
685 EXPECT_EQ(NULL, stats);
686 ServerNetworkStats stats1;
687 stats1.srtt = base::TimeDelta::FromMicroseconds(10);
688 http_server_props_manager_->SetServerNetworkStats(mail_server, stats1);
689 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
690 http_server_props_manager_->SetServerNetworkStats(mail_server, stats1);
692 // Run the task.
693 base::RunLoop().RunUntilIdle();
694 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
696 const ServerNetworkStats* stats2 =
697 http_server_props_manager_->GetServerNetworkStats(mail_server);
698 EXPECT_EQ(10, stats2->srtt.ToInternalValue());
701 TEST_F(HttpServerPropertiesManagerTest, Clear) {
702 ExpectPrefsUpdate();
703 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly();
705 HostPortPair spdy_server_mail("mail.google.com", 443);
706 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
707 AlternativeService alternative_service(NPN_HTTP_2, "mail.google.com", 1234);
708 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
709 alternative_service, 1.0);
710 IPAddressNumber actual_address;
711 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
712 http_server_props_manager_->SetSupportsQuic(true, actual_address);
713 ServerNetworkStats stats;
714 stats.srtt = base::TimeDelta::FromMicroseconds(10);
715 http_server_props_manager_->SetServerNetworkStats(spdy_server_mail, stats);
717 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
718 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
719 const uint32 value1 = 31337;
720 http_server_props_manager_->SetSpdySetting(
721 spdy_server_mail, id1, flags1, value1);
723 // Run the task.
724 base::RunLoop().RunUntilIdle();
726 EXPECT_TRUE(
727 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
728 EXPECT_TRUE(HasAlternativeService(spdy_server_mail));
729 IPAddressNumber address;
730 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
731 EXPECT_EQ(actual_address, address);
732 const ServerNetworkStats* stats1 =
733 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
734 EXPECT_EQ(10, stats1->srtt.ToInternalValue());
736 // Check SPDY settings values.
737 const SettingsMap& settings_map1_ret =
738 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
739 ASSERT_EQ(1U, settings_map1_ret.size());
740 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
741 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
742 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
743 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
744 EXPECT_EQ(value1, flags_and_value1_ret.second);
746 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
748 ExpectPrefsUpdate();
750 // Clear http server data, time out if we do not get a completion callback.
751 http_server_props_manager_->Clear(base::MessageLoop::QuitClosure());
752 base::RunLoop().Run();
754 EXPECT_FALSE(
755 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
756 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
757 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address));
758 const ServerNetworkStats* stats2 =
759 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
760 EXPECT_EQ(NULL, stats2);
762 const SettingsMap& settings_map2_ret =
763 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
764 EXPECT_EQ(0U, settings_map2_ret.size());
766 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
769 // https://crbug.com/444956: Add 200 alternative_service servers followed by
770 // supports_quic and verify we have read supports_quic from prefs.
771 TEST_F(HttpServerPropertiesManagerTest, BadSupportsQuic) {
772 ExpectCacheUpdate();
774 base::DictionaryValue* servers_dict = new base::DictionaryValue;
776 for (int i = 0; i < 200; ++i) {
777 // Set up alternative_service for www.google.com:i.
778 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
779 alternative_service_dict->SetString("protocol_str", "quic");
780 alternative_service_dict->SetInteger("port", i);
781 base::ListValue* alternative_service_list = new base::ListValue;
782 alternative_service_list->Append(alternative_service_dict);
783 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
784 server_pref_dict->SetWithoutPathExpansion("alternative_service",
785 alternative_service_list);
786 servers_dict->SetWithoutPathExpansion(StringPrintf("www.google.com:%d", i),
787 server_pref_dict);
790 // Set the preference for mail.google.com server.
791 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
793 // Set the server preference for mail.google.com:80.
794 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
795 server_pref_dict1);
797 base::DictionaryValue* http_server_properties_dict =
798 new base::DictionaryValue;
799 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
800 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
802 // Set up SupportsQuic for 127.0.0.1
803 base::DictionaryValue* supports_quic = new base::DictionaryValue;
804 supports_quic->SetBoolean("used_quic", true);
805 supports_quic->SetString("address", "127.0.0.1");
806 http_server_properties_dict->SetWithoutPathExpansion("supports_quic",
807 supports_quic);
809 // Set up the pref.
810 pref_service_.SetManagedPref(kTestHttpServerProperties,
811 http_server_properties_dict);
813 base::RunLoop().RunUntilIdle();
814 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
816 // Verify alternative service.
817 for (int i = 0; i < 200; ++i) {
818 std::string server = StringPrintf("www.google.com:%d", i);
819 AlternativeServiceVector alternative_service_vector =
820 http_server_props_manager_->GetAlternativeServices(
821 HostPortPair::FromString(server));
822 ASSERT_EQ(1u, alternative_service_vector.size());
823 EXPECT_EQ(QUIC, alternative_service_vector[0].protocol);
824 EXPECT_EQ(i, alternative_service_vector[0].port);
827 // Verify SupportsQuic.
828 IPAddressNumber address;
829 ASSERT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
830 EXPECT_EQ("127.0.0.1", IPAddressToString(address));
833 TEST_F(HttpServerPropertiesManagerTest, UpdateCacheWithPrefs) {
834 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly();
836 const HostPortPair server_www("www.google.com", 80);
837 const HostPortPair server_mail("mail.google.com", 80);
839 // Set alternate protocol.
840 AlternativeServiceInfoVector alternative_service_info_vector;
841 AlternativeService www_alternative_service1(NPN_HTTP_2, "", 443);
842 alternative_service_info_vector.push_back(
843 AlternativeServiceInfo(www_alternative_service1, 1.0));
844 AlternativeService www_alternative_service2(NPN_HTTP_2, "www.google.com",
845 1234);
846 alternative_service_info_vector.push_back(
847 AlternativeServiceInfo(www_alternative_service2, 0.7));
848 http_server_props_manager_->SetAlternativeServices(
849 server_www, alternative_service_info_vector);
851 AlternativeService mail_alternative_service(NPN_SPDY_3_1, "foo.google.com",
852 444);
853 http_server_props_manager_->SetAlternativeService(
854 server_mail, mail_alternative_service, 0.2);
856 // Set ServerNetworkStats.
857 ServerNetworkStats stats;
858 stats.srtt = base::TimeDelta::FromInternalValue(42);
859 http_server_props_manager_->SetServerNetworkStats(server_mail, stats);
861 // Set SupportsQuic.
862 IPAddressNumber actual_address;
863 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
864 http_server_props_manager_->SetSupportsQuic(true, actual_address);
866 // Update cache.
867 ExpectPrefsUpdate();
868 ExpectCacheUpdate();
869 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
870 base::RunLoop().RunUntilIdle();
872 // Verify preferences.
873 const char expected_json[] =
874 "{\"servers\":{\"mail.google.com:80\":{\"alternative_service\":[{"
875 "\"host\":\"foo.google.com\",\"port\":444,\"probability\":0.2,"
876 "\"protocol_str\":\"npn-spdy/3.1\"}],\"network_stats\":{\"srtt\":42}},"
877 "\"www.google.com:80\":{\"alternative_service\":[{\"port\":443,"
878 "\"probability\":1.0,\"protocol_str\":\"npn-h2\"},"
879 "{\"host\":\"www.google.com\",\"port\":1234,\"probability\":0.7,"
880 "\"protocol_str\":\"npn-h2\"}]}},\"supports_quic\":"
881 "{\"address\":\"127.0.0.1\",\"used_quic\":true},\"version\":3}";
883 const base::Value* http_server_properties =
884 pref_service_.GetUserPref(kTestHttpServerProperties);
885 ASSERT_NE(nullptr, http_server_properties);
886 std::string preferences_json;
887 EXPECT_TRUE(
888 base::JSONWriter::Write(*http_server_properties, &preferences_json));
889 EXPECT_EQ(expected_json, preferences_json);
892 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache0) {
893 // Post an update task to the UI thread.
894 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
895 // Shutdown comes before the task is executed.
896 http_server_props_manager_->ShutdownOnPrefThread();
897 http_server_props_manager_.reset();
898 // Run the task after shutdown and deletion.
899 base::RunLoop().RunUntilIdle();
902 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache1) {
903 // Post an update task.
904 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
905 // Shutdown comes before the task is executed.
906 http_server_props_manager_->ShutdownOnPrefThread();
907 // Run the task after shutdown, but before deletion.
908 base::RunLoop().RunUntilIdle();
909 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
910 http_server_props_manager_.reset();
911 base::RunLoop().RunUntilIdle();
914 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache2) {
915 http_server_props_manager_->UpdateCacheFromPrefsOnUIConcrete();
916 // Shutdown comes before the task is executed.
917 http_server_props_manager_->ShutdownOnPrefThread();
918 // Run the task after shutdown, but before deletion.
919 base::RunLoop().RunUntilIdle();
920 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
921 http_server_props_manager_.reset();
922 base::RunLoop().RunUntilIdle();
926 // Tests for shutdown when updating prefs.
928 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs0) {
929 // Post an update task to the IO thread.
930 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
931 // Shutdown comes before the task is executed.
932 http_server_props_manager_->ShutdownOnPrefThread();
933 http_server_props_manager_.reset();
934 // Run the task after shutdown and deletion.
935 base::RunLoop().RunUntilIdle();
938 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs1) {
939 ExpectPrefsUpdate();
940 // Post an update task.
941 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
942 // Shutdown comes before the task is executed.
943 http_server_props_manager_->ShutdownOnPrefThread();
944 // Run the task after shutdown, but before deletion.
945 base::RunLoop().RunUntilIdle();
946 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
947 http_server_props_manager_.reset();
948 base::RunLoop().RunUntilIdle();
951 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs2) {
952 // This posts a task to the UI thread.
953 http_server_props_manager_->UpdatePrefsFromCacheOnNetworkThreadConcrete(
954 base::Closure());
955 // Shutdown comes before the task is executed.
956 http_server_props_manager_->ShutdownOnPrefThread();
957 // Run the task after shutdown, but before deletion.
958 base::RunLoop().RunUntilIdle();
959 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
960 http_server_props_manager_.reset();
961 base::RunLoop().RunUntilIdle();
964 } // namespace
966 } // namespace net