Roll src/third_party/WebKit 6cdd902:94953d6 (svn 197985:197991)
[chromium-blink-merge.git] / net / http / http_server_properties_manager_unittest.cc
blobe4f2141977ef55ed41ce9b04efa8cb15da284235
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 AlternativeService alternative_service =
166 http_server_props_manager_->GetAlternativeService(server);
167 return alternative_service.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL;
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_service for www.google.com:80.
193 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
194 alternative_service_dict->SetString("protocol_str", "npn-h2");
195 alternative_service_dict->SetString("host", "maps.google.com");
196 alternative_service_dict->SetInteger("port", 443);
197 base::ListValue* alternative_service_list = new base::ListValue;
198 alternative_service_list->Append(alternative_service_dict);
199 server_pref_dict->SetWithoutPathExpansion("alternative_service",
200 alternative_service_list);
202 // Set up ServerNetworkStats for www.google.com:80.
203 base::DictionaryValue* stats = new base::DictionaryValue;
204 stats->SetInteger("srtt", 10);
205 server_pref_dict->SetWithoutPathExpansion("network_stats", stats);
207 // Set the server preference for www.google.com:80.
208 base::DictionaryValue* servers_dict = new base::DictionaryValue;
209 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict);
211 // Set the preference for mail.google.com server.
212 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
214 // Set supports_spdy for mail.google.com:80
215 server_pref_dict1->SetBoolean("supports_spdy", true);
217 // Set up alternate_protocol for mail.google.com:80 to test migration to
218 // alternative-service.
219 base::DictionaryValue* alternate_protocol_dict = new base::DictionaryValue;
220 alternate_protocol_dict->SetString("protocol_str", "npn-spdy/3.1");
221 alternate_protocol_dict->SetInteger("port", 444);
222 server_pref_dict1->SetWithoutPathExpansion("alternate_protocol",
223 alternate_protocol_dict);
225 // Set up ServerNetworkStats for mail.google.com:80.
226 base::DictionaryValue* stats1 = new base::DictionaryValue;
227 stats1->SetInteger("srtt", 20);
228 server_pref_dict1->SetWithoutPathExpansion("network_stats", stats1);
229 // Set the server preference for mail.google.com:80.
230 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
231 server_pref_dict1);
233 base::DictionaryValue* http_server_properties_dict =
234 new base::DictionaryValue;
235 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
236 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
237 base::DictionaryValue* supports_quic = new base::DictionaryValue;
238 supports_quic->SetBoolean("used_quic", true);
239 supports_quic->SetString("address", "127.0.0.1");
240 http_server_properties_dict->SetWithoutPathExpansion("supports_quic",
241 supports_quic);
243 // Set the same value for kHttpServerProperties multiple times.
244 pref_service_.SetManagedPref(kTestHttpServerProperties,
245 http_server_properties_dict);
246 base::DictionaryValue* http_server_properties_dict2 =
247 http_server_properties_dict->DeepCopy();
248 pref_service_.SetManagedPref(kTestHttpServerProperties,
249 http_server_properties_dict2);
251 base::RunLoop().RunUntilIdle();
252 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
254 // Verify SupportsSpdy.
255 EXPECT_TRUE(
256 http_server_props_manager_->SupportsRequestPriority(google_server));
257 EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(mail_server));
258 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority(
259 HostPortPair::FromString("foo.google.com:1337")));
261 // Verify alternative service.
262 AlternativeService alternative_service =
263 http_server_props_manager_->GetAlternativeService(google_server);
264 EXPECT_EQ(NPN_SPDY_4, alternative_service.protocol);
265 EXPECT_EQ("maps.google.com", alternative_service.host);
266 EXPECT_EQ(443, alternative_service.port);
267 alternative_service =
268 http_server_props_manager_->GetAlternativeService(mail_server);
269 EXPECT_EQ(NPN_SPDY_3_1, alternative_service.protocol);
270 EXPECT_EQ("mail.google.com", alternative_service.host);
271 EXPECT_EQ(444, alternative_service.port);
273 // Verify SupportsQuic.
274 IPAddressNumber last_address;
275 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&last_address));
276 EXPECT_EQ("127.0.0.1", IPAddressToString(last_address));
278 // Verify ServerNetworkStats.
279 const ServerNetworkStats* stats2 =
280 http_server_props_manager_->GetServerNetworkStats(google_server);
281 EXPECT_EQ(10, stats2->srtt.ToInternalValue());
282 const ServerNetworkStats* stats3 =
283 http_server_props_manager_->GetServerNetworkStats(mail_server);
284 EXPECT_EQ(20, stats3->srtt.ToInternalValue());
287 TEST_F(HttpServerPropertiesManagerTest, BadCachedHostPortPair) {
288 ExpectCacheUpdate();
289 // The prefs are automaticalls updated in the case corruption is detected.
290 ExpectPrefsUpdate();
291 ExpectScheduleUpdatePrefsOnNetworkThread();
293 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
295 // Set supports_spdy for www.google.com:65536.
296 server_pref_dict->SetBoolean("supports_spdy", true);
298 // Set up alternative_service for www.google.com:65536.
300 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
301 alternative_service_dict->SetString("protocol_str", "npn-spdy/3");
302 alternative_service_dict->SetInteger("port", 80);
303 base::ListValue* alternative_service_list = new base::ListValue;
304 alternative_service_list->Append(alternative_service_dict);
305 server_pref_dict->SetWithoutPathExpansion("alternative_service",
306 alternative_service_list);
308 // Set up ServerNetworkStats for www.google.com:65536.
309 base::DictionaryValue* stats = new base::DictionaryValue;
310 stats->SetInteger("srtt", 10);
311 server_pref_dict->SetWithoutPathExpansion("network_stats", stats);
313 // Set the server preference for www.google.com:65536.
314 base::DictionaryValue* servers_dict = new base::DictionaryValue;
315 servers_dict->SetWithoutPathExpansion("www.google.com:65536",
316 server_pref_dict);
318 base::DictionaryValue* http_server_properties_dict =
319 new base::DictionaryValue;
320 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
321 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
323 // Set up the pref.
324 pref_service_.SetManagedPref(kTestHttpServerProperties,
325 http_server_properties_dict);
327 base::RunLoop().RunUntilIdle();
328 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
330 // Verify that nothing is set.
331 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority(
332 HostPortPair::FromString("www.google.com:65536")));
333 EXPECT_FALSE(
334 HasAlternativeService(HostPortPair::FromString("www.google.com:65536")));
335 const ServerNetworkStats* stats1 =
336 http_server_props_manager_->GetServerNetworkStats(
337 HostPortPair::FromString("www.google.com:65536"));
338 EXPECT_EQ(NULL, stats1);
341 TEST_F(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) {
342 ExpectCacheUpdate();
343 // The prefs are automaticalls updated in the case corruption is detected.
344 ExpectPrefsUpdate();
345 ExpectScheduleUpdatePrefsOnNetworkThread();
347 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
349 // Set supports_spdy for www.google.com:80.
350 server_pref_dict->SetBoolean("supports_spdy", true);
352 // Set up alternative_service for www.google.com:80.
353 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
354 alternative_service_dict->SetString("protocol_str", "npn-spdy/3");
355 alternative_service_dict->SetInteger("port", 65536);
356 base::ListValue* alternative_service_list = new base::ListValue;
357 alternative_service_list->Append(alternative_service_dict);
358 server_pref_dict->SetWithoutPathExpansion("alternative_service",
359 alternative_service_list);
361 // Set the server preference for www.google.com:80.
362 base::DictionaryValue* servers_dict = new base::DictionaryValue;
363 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict);
365 base::DictionaryValue* http_server_properties_dict =
366 new base::DictionaryValue;
367 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
368 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
370 // Set up the pref.
371 pref_service_.SetManagedPref(kTestHttpServerProperties,
372 http_server_properties_dict);
374 base::RunLoop().RunUntilIdle();
375 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
377 // Verify alternative service is not set.
378 EXPECT_FALSE(
379 HasAlternativeService(HostPortPair::FromString("www.google.com:80")));
382 TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) {
383 ExpectPrefsUpdate();
384 ExpectScheduleUpdatePrefsOnNetworkThread();
386 // Post an update task to the network thread. SetSupportsSpdy calls
387 // ScheduleUpdatePrefsOnNetworkThread.
389 // Add mail.google.com:443 as a supporting spdy server.
390 HostPortPair spdy_server_mail("mail.google.com", 443);
391 EXPECT_FALSE(
392 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
393 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
394 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
395 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
397 // Run the task.
398 base::RunLoop().RunUntilIdle();
400 EXPECT_TRUE(
401 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
402 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
405 TEST_F(HttpServerPropertiesManagerTest, SetSpdySetting) {
406 ExpectPrefsUpdate();
407 ExpectScheduleUpdatePrefsOnNetworkThread();
409 // Add SpdySetting for mail.google.com:443.
410 HostPortPair spdy_server_mail("mail.google.com", 443);
411 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
412 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
413 const uint32 value1 = 31337;
414 http_server_props_manager_->SetSpdySetting(
415 spdy_server_mail, id1, flags1, value1);
417 // Run the task.
418 base::RunLoop().RunUntilIdle();
420 const SettingsMap& settings_map1_ret =
421 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
422 ASSERT_EQ(1U, settings_map1_ret.size());
423 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
424 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
425 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
426 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
427 EXPECT_EQ(value1, flags_and_value1_ret.second);
429 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
432 TEST_F(HttpServerPropertiesManagerTest, ClearSpdySetting) {
433 ExpectPrefsUpdateRepeatedly();
434 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly();
436 // Add SpdySetting for mail.google.com:443.
437 HostPortPair spdy_server_mail("mail.google.com", 443);
438 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
439 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
440 const uint32 value1 = 31337;
441 http_server_props_manager_->SetSpdySetting(
442 spdy_server_mail, id1, flags1, value1);
444 // Run the task.
445 base::RunLoop().RunUntilIdle();
447 const SettingsMap& settings_map1_ret =
448 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
449 ASSERT_EQ(1U, settings_map1_ret.size());
450 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
451 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
452 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
453 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
454 EXPECT_EQ(value1, flags_and_value1_ret.second);
456 // Clear SpdySetting for mail.google.com:443.
457 http_server_props_manager_->ClearSpdySettings(spdy_server_mail);
459 // Run the task.
460 base::RunLoop().RunUntilIdle();
462 // Verify that there are no entries in the settings map for
463 // mail.google.com:443.
464 const SettingsMap& settings_map2_ret =
465 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
466 ASSERT_EQ(0U, settings_map2_ret.size());
468 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
471 TEST_F(HttpServerPropertiesManagerTest, ClearAllSpdySetting) {
472 ExpectPrefsUpdateRepeatedly();
473 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly();
475 // Add SpdySetting for mail.google.com:443.
476 HostPortPair spdy_server_mail("mail.google.com", 443);
477 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
478 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
479 const uint32 value1 = 31337;
480 http_server_props_manager_->SetSpdySetting(
481 spdy_server_mail, id1, flags1, value1);
483 // Run the task.
484 base::RunLoop().RunUntilIdle();
486 const SettingsMap& settings_map1_ret =
487 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
488 ASSERT_EQ(1U, settings_map1_ret.size());
489 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
490 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
491 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
492 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
493 EXPECT_EQ(value1, flags_and_value1_ret.second);
495 // Clear All SpdySettings.
496 http_server_props_manager_->ClearAllSpdySettings();
498 // Run the task.
499 base::RunLoop().RunUntilIdle();
501 // Verify that there are no entries in the settings map.
502 const SpdySettingsMap& spdy_settings_map2_ret =
503 http_server_props_manager_->spdy_settings_map();
504 ASSERT_EQ(0U, spdy_settings_map2_ret.size());
506 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
509 TEST_F(HttpServerPropertiesManagerTest, GetAlternativeService) {
510 ExpectPrefsUpdate();
511 ExpectScheduleUpdatePrefsOnNetworkThread();
513 HostPortPair spdy_server_mail("mail.google.com", 80);
514 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
515 AlternativeService alternative_service(NPN_SPDY_4, "mail.google.com", 443);
516 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
517 alternative_service, 1.0);
518 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
519 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
520 alternative_service, 1.0);
522 // Run the task.
523 base::RunLoop().RunUntilIdle();
524 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
526 alternative_service =
527 http_server_props_manager_->GetAlternativeService(spdy_server_mail);
528 EXPECT_EQ(443, alternative_service.port);
529 EXPECT_EQ(NPN_SPDY_4, alternative_service.protocol);
532 TEST_F(HttpServerPropertiesManagerTest, ClearAlternativeService) {
533 ExpectPrefsUpdate();
534 ExpectScheduleUpdatePrefsOnNetworkThread();
536 HostPortPair spdy_server_mail("mail.google.com", 80);
537 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
538 AlternativeService alternative_service(NPN_SPDY_4, "mail.google.com", 443);
539 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
540 alternative_service, 1.0);
541 ExpectScheduleUpdatePrefsOnNetworkThread();
542 http_server_props_manager_->ClearAlternativeService(spdy_server_mail);
543 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
544 http_server_props_manager_->ClearAlternativeService(spdy_server_mail);
546 // Run the task.
547 base::RunLoop().RunUntilIdle();
548 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
550 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
553 TEST_F(HttpServerPropertiesManagerTest, ConfirmAlternativeService) {
554 ExpectPrefsUpdate();
556 HostPortPair spdy_server_mail("mail.google.com", 80);
557 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
558 AlternativeService alternative_service(NPN_SPDY_4, "mail.google.com", 443);
560 ExpectScheduleUpdatePrefsOnNetworkThread();
561 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
562 alternative_service, 1.0);
564 EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
565 alternative_service));
566 EXPECT_FALSE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
567 alternative_service));
569 ExpectScheduleUpdatePrefsOnNetworkThread();
570 http_server_props_manager_->MarkAlternativeServiceBroken(alternative_service);
571 EXPECT_TRUE(http_server_props_manager_->IsAlternativeServiceBroken(
572 alternative_service));
573 EXPECT_TRUE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
574 alternative_service));
576 ExpectScheduleUpdatePrefsOnNetworkThread();
577 http_server_props_manager_->ConfirmAlternativeService(alternative_service);
578 EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
579 alternative_service));
580 EXPECT_FALSE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
581 alternative_service));
582 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
583 http_server_props_manager_->ConfirmAlternativeService(alternative_service);
584 EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
585 alternative_service));
586 EXPECT_FALSE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
587 alternative_service));
589 // Run the task.
590 base::RunLoop().RunUntilIdle();
591 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
593 EXPECT_FALSE(http_server_props_manager_->IsAlternativeServiceBroken(
594 alternative_service));
595 EXPECT_FALSE(http_server_props_manager_->WasAlternativeServiceRecentlyBroken(
596 alternative_service));
599 TEST_F(HttpServerPropertiesManagerTest, SupportsQuic) {
600 ExpectPrefsUpdate();
601 ExpectScheduleUpdatePrefsOnNetworkThread();
603 IPAddressNumber address;
604 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address));
606 IPAddressNumber actual_address;
607 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
608 http_server_props_manager_->SetSupportsQuic(true, actual_address);
609 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
610 http_server_props_manager_->SetSupportsQuic(true, actual_address);
612 // Run the task.
613 base::RunLoop().RunUntilIdle();
614 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
616 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
617 EXPECT_EQ(actual_address, address);
620 TEST_F(HttpServerPropertiesManagerTest, ServerNetworkStats) {
621 ExpectPrefsUpdate();
622 ExpectScheduleUpdatePrefsOnNetworkThread();
624 HostPortPair mail_server("mail.google.com", 80);
625 const ServerNetworkStats* stats =
626 http_server_props_manager_->GetServerNetworkStats(mail_server);
627 EXPECT_EQ(NULL, stats);
628 ServerNetworkStats stats1;
629 stats1.srtt = base::TimeDelta::FromMicroseconds(10);
630 http_server_props_manager_->SetServerNetworkStats(mail_server, stats1);
631 // ExpectScheduleUpdatePrefsOnNetworkThread() should be called only once.
632 http_server_props_manager_->SetServerNetworkStats(mail_server, stats1);
634 // Run the task.
635 base::RunLoop().RunUntilIdle();
636 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
638 const ServerNetworkStats* stats2 =
639 http_server_props_manager_->GetServerNetworkStats(mail_server);
640 EXPECT_EQ(10, stats2->srtt.ToInternalValue());
643 TEST_F(HttpServerPropertiesManagerTest, Clear) {
644 ExpectPrefsUpdate();
645 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly();
647 HostPortPair spdy_server_mail("mail.google.com", 443);
648 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
649 AlternativeService alternative_service(NPN_SPDY_4, "mail.google.com", 443);
650 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
651 alternative_service, 1.0);
652 IPAddressNumber actual_address;
653 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
654 http_server_props_manager_->SetSupportsQuic(true, actual_address);
655 ServerNetworkStats stats;
656 stats.srtt = base::TimeDelta::FromMicroseconds(10);
657 http_server_props_manager_->SetServerNetworkStats(spdy_server_mail, stats);
659 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
660 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
661 const uint32 value1 = 31337;
662 http_server_props_manager_->SetSpdySetting(
663 spdy_server_mail, id1, flags1, value1);
665 // Run the task.
666 base::RunLoop().RunUntilIdle();
668 EXPECT_TRUE(
669 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
670 EXPECT_TRUE(HasAlternativeService(spdy_server_mail));
671 IPAddressNumber address;
672 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
673 EXPECT_EQ(actual_address, address);
674 const ServerNetworkStats* stats1 =
675 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
676 EXPECT_EQ(10, stats1->srtt.ToInternalValue());
678 // Check SPDY settings values.
679 const SettingsMap& settings_map1_ret =
680 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
681 ASSERT_EQ(1U, settings_map1_ret.size());
682 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
683 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
684 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
685 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
686 EXPECT_EQ(value1, flags_and_value1_ret.second);
688 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
690 ExpectPrefsUpdate();
692 // Clear http server data, time out if we do not get a completion callback.
693 http_server_props_manager_->Clear(base::MessageLoop::QuitClosure());
694 base::RunLoop().Run();
696 EXPECT_FALSE(
697 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
698 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
699 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address));
700 const ServerNetworkStats* stats2 =
701 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
702 EXPECT_EQ(NULL, stats2);
704 const SettingsMap& settings_map2_ret =
705 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
706 EXPECT_EQ(0U, settings_map2_ret.size());
708 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
711 // https://crbug.com/444956: Add 200 alternative_service servers followed by
712 // supports_quic and verify we have read supports_quic from prefs.
713 TEST_F(HttpServerPropertiesManagerTest, BadSupportsQuic) {
714 ExpectCacheUpdate();
716 base::DictionaryValue* servers_dict = new base::DictionaryValue;
718 for (int i = 0; i < 200; ++i) {
719 // Set up alternative_service for www.google.com:i.
720 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
721 alternative_service_dict->SetString("protocol_str", "npn-h2");
722 alternative_service_dict->SetString("host", "");
723 alternative_service_dict->SetInteger("port", i);
724 base::ListValue* alternative_service_list = new base::ListValue;
725 alternative_service_list->Append(alternative_service_dict);
726 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
727 server_pref_dict->SetWithoutPathExpansion("alternative_service",
728 alternative_service_list);
729 servers_dict->SetWithoutPathExpansion(StringPrintf("www.google.com:%d", i),
730 server_pref_dict);
733 // Set the preference for mail.google.com server.
734 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
736 // Set the server preference for mail.google.com:80.
737 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
738 server_pref_dict1);
740 base::DictionaryValue* http_server_properties_dict =
741 new base::DictionaryValue;
742 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
743 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
745 // Set up SupportsQuic for 127.0.0.1
746 base::DictionaryValue* supports_quic = new base::DictionaryValue;
747 supports_quic->SetBoolean("used_quic", true);
748 supports_quic->SetString("address", "127.0.0.1");
749 http_server_properties_dict->SetWithoutPathExpansion("supports_quic",
750 supports_quic);
752 // Set up the pref.
753 pref_service_.SetManagedPref(kTestHttpServerProperties,
754 http_server_properties_dict);
756 base::RunLoop().RunUntilIdle();
757 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
759 // Verify alternative service.
760 for (int i = 0; i < 200; ++i) {
761 std::string server = StringPrintf("www.google.com:%d", i);
762 AlternativeService alternative_service =
763 http_server_props_manager_->GetAlternativeService(
764 HostPortPair::FromString(server));
765 EXPECT_EQ(i, alternative_service.port);
766 EXPECT_EQ(NPN_SPDY_4, alternative_service.protocol);
769 // Verify SupportsQuic.
770 IPAddressNumber address;
771 ASSERT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
772 EXPECT_EQ("127.0.0.1", IPAddressToString(address));
775 TEST_F(HttpServerPropertiesManagerTest, UpdateCacheWithPrefs) {
776 ExpectScheduleUpdatePrefsOnNetworkThreadRepeatedly();
778 const HostPortPair server_www("www.google.com", 80);
779 const HostPortPair server_mail("mail.google.com", 80);
781 // Set alternate protocol.
782 AlternativeService www_altsvc(NPN_SPDY_4, "", 443);
783 AlternativeService mail_altsvc(NPN_SPDY_3_1, "mail.google.com", 444);
784 http_server_props_manager_->SetAlternativeService(server_www, www_altsvc,
785 1.0);
786 http_server_props_manager_->SetAlternativeService(server_mail, mail_altsvc,
787 0.2);
789 // Set ServerNetworkStats.
790 ServerNetworkStats stats;
791 stats.srtt = base::TimeDelta::FromInternalValue(42);
792 http_server_props_manager_->SetServerNetworkStats(server_mail, stats);
794 // Set SupportsQuic.
795 IPAddressNumber actual_address;
796 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
797 http_server_props_manager_->SetSupportsQuic(true, actual_address);
799 // Update cache.
800 ExpectPrefsUpdate();
801 ExpectCacheUpdate();
802 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
803 base::RunLoop().RunUntilIdle();
805 // Verify preferences.
806 const char expected_json[] =
807 "{\"servers\":{\"mail.google.com:80\":{\"alternative_service\":[{"
808 "\"host\":"
809 "\"mail.google.com\",\"port\":444,\"probability\":0.2,\"protocol_str\":"
810 "\"npn-spdy/"
811 "3.1\"}],\"network_stats\":{\"srtt\":42}},\"www.google.com:80\":"
812 "{\"alternative_service\":[{\"port\":443,\"probability\":1.0,"
813 "\"protocol_str\":\"npn-h2\"}]}},\"supports_quic\":"
814 "{\"address\":\"127.0.0.1\",\"used_quic\":true},\"version\":3}";
816 const base::Value* http_server_properties =
817 pref_service_.GetUserPref(kTestHttpServerProperties);
818 ASSERT_NE(nullptr, http_server_properties);
819 std::string preferences_json;
820 EXPECT_TRUE(
821 base::JSONWriter::Write(*http_server_properties, &preferences_json));
822 EXPECT_EQ(expected_json, preferences_json);
825 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache0) {
826 // Post an update task to the UI thread.
827 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
828 // Shutdown comes before the task is executed.
829 http_server_props_manager_->ShutdownOnPrefThread();
830 http_server_props_manager_.reset();
831 // Run the task after shutdown and deletion.
832 base::RunLoop().RunUntilIdle();
835 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache1) {
836 // Post an update task.
837 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
838 // Shutdown comes before the task is executed.
839 http_server_props_manager_->ShutdownOnPrefThread();
840 // Run the task after shutdown, but before deletion.
841 base::RunLoop().RunUntilIdle();
842 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
843 http_server_props_manager_.reset();
844 base::RunLoop().RunUntilIdle();
847 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache2) {
848 http_server_props_manager_->UpdateCacheFromPrefsOnUIConcrete();
849 // Shutdown comes before the task is executed.
850 http_server_props_manager_->ShutdownOnPrefThread();
851 // Run the task after shutdown, but before deletion.
852 base::RunLoop().RunUntilIdle();
853 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
854 http_server_props_manager_.reset();
855 base::RunLoop().RunUntilIdle();
859 // Tests for shutdown when updating prefs.
861 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs0) {
862 // Post an update task to the IO thread.
863 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
864 // Shutdown comes before the task is executed.
865 http_server_props_manager_->ShutdownOnPrefThread();
866 http_server_props_manager_.reset();
867 // Run the task after shutdown and deletion.
868 base::RunLoop().RunUntilIdle();
871 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs1) {
872 ExpectPrefsUpdate();
873 // Post an update task.
874 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
875 // Shutdown comes before the task is executed.
876 http_server_props_manager_->ShutdownOnPrefThread();
877 // Run the task after shutdown, but before deletion.
878 base::RunLoop().RunUntilIdle();
879 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
880 http_server_props_manager_.reset();
881 base::RunLoop().RunUntilIdle();
884 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs2) {
885 // This posts a task to the UI thread.
886 http_server_props_manager_->UpdatePrefsFromCacheOnNetworkThreadConcrete(
887 base::Closure());
888 // Shutdown comes before the task is executed.
889 http_server_props_manager_->ShutdownOnPrefThread();
890 // Run the task after shutdown, but before deletion.
891 base::RunLoop().RunUntilIdle();
892 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
893 http_server_props_manager_.reset();
894 base::RunLoop().RunUntilIdle();
897 } // namespace
899 } // namespace net