Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / net / http / http_server_properties_manager_unittest.cc
blob55dc7b8cc04a41559b4628ed5d6c28b15ca611c0
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 ~TestingHttpServerPropertiesManager() override {}
45 // Make these methods public for testing.
46 using HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread;
47 using HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread;
49 // Post tasks without a delay during tests.
50 void StartPrefsUpdateTimerOnNetworkThread(base::TimeDelta delay) override {
51 HttpServerPropertiesManager::StartPrefsUpdateTimerOnNetworkThread(
52 base::TimeDelta());
55 void UpdateCacheFromPrefsOnUIConcrete() {
56 HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread();
59 // Post tasks without a delay during tests.
60 void StartCacheUpdateTimerOnPrefThread(base::TimeDelta delay) override {
61 HttpServerPropertiesManager::StartCacheUpdateTimerOnPrefThread(
62 base::TimeDelta());
65 void UpdatePrefsFromCacheOnNetworkThreadConcrete(
66 const base::Closure& callback) {
67 HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread(callback);
70 MOCK_METHOD0(UpdateCacheFromPrefsOnPrefThread, void());
71 MOCK_METHOD1(UpdatePrefsFromCacheOnNetworkThread, void(const base::Closure&));
72 MOCK_METHOD6(UpdateCacheFromPrefsOnNetworkThread,
73 void(std::vector<std::string>* spdy_servers,
74 SpdySettingsMap* spdy_settings_map,
75 AlternativeServiceMap* alternative_service_map,
76 IPAddressNumber* last_quic_address,
77 ServerNetworkStatsMap* server_network_stats_map,
78 bool detected_corrupted_prefs));
79 MOCK_METHOD5(UpdatePrefsOnPref,
80 void(base::ListValue* spdy_server_list,
81 SpdySettingsMap* spdy_settings_map,
82 AlternativeServiceMap* alternative_service_map,
83 IPAddressNumber* last_quic_address,
84 ServerNetworkStatsMap* server_network_stats_map));
86 private:
87 DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager);
90 class HttpServerPropertiesManagerTest : public testing::Test {
91 protected:
92 HttpServerPropertiesManagerTest() {}
94 void SetUp() override {
95 pref_service_.registry()->RegisterDictionaryPref(kTestHttpServerProperties);
96 http_server_props_manager_.reset(
97 new StrictMock<TestingHttpServerPropertiesManager>(
98 &pref_service_,
99 kTestHttpServerProperties,
100 base::MessageLoop::current()->message_loop_proxy()));
101 ExpectCacheUpdate();
102 base::RunLoop().RunUntilIdle();
105 void TearDown() override {
106 if (http_server_props_manager_.get())
107 http_server_props_manager_->ShutdownOnPrefThread();
108 base::RunLoop().RunUntilIdle();
109 http_server_props_manager_.reset();
112 void ExpectCacheUpdate() {
113 EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefsOnPrefThread())
114 .WillOnce(Invoke(http_server_props_manager_.get(),
115 &TestingHttpServerPropertiesManager::
116 UpdateCacheFromPrefsOnUIConcrete));
119 void ExpectPrefsUpdate() {
120 EXPECT_CALL(*http_server_props_manager_,
121 UpdatePrefsFromCacheOnNetworkThread(_))
122 .WillOnce(Invoke(http_server_props_manager_.get(),
123 &TestingHttpServerPropertiesManager::
124 UpdatePrefsFromCacheOnNetworkThreadConcrete));
127 void ExpectPrefsUpdateRepeatedly() {
128 EXPECT_CALL(*http_server_props_manager_,
129 UpdatePrefsFromCacheOnNetworkThread(_))
130 .WillRepeatedly(
131 Invoke(http_server_props_manager_.get(),
132 &TestingHttpServerPropertiesManager::
133 UpdatePrefsFromCacheOnNetworkThreadConcrete));
136 bool HasAlternativeService(const HostPortPair& server) {
137 const AlternativeService alternative_service =
138 http_server_props_manager_->GetAlternativeService(server);
139 return alternative_service.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL;
142 //base::RunLoop loop_;
143 TestingPrefServiceSimple pref_service_;
144 scoped_ptr<TestingHttpServerPropertiesManager> http_server_props_manager_;
146 private:
147 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest);
150 TEST_F(HttpServerPropertiesManagerTest,
151 SingleUpdateForTwoSpdyServerPrefChanges) {
152 ExpectCacheUpdate();
154 // Set up the prefs for www.google.com:80 and mail.google.com:80 and then set
155 // it twice. Only expect a single cache update.
157 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
158 HostPortPair google_server("www.google.com", 80);
159 HostPortPair mail_server("mail.google.com", 80);
161 // Set supports_spdy for www.google.com:80.
162 server_pref_dict->SetBoolean("supports_spdy", true);
164 // Set up alternative_service for www.google.com:80.
165 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
166 alternative_service_dict->SetString("protocol_str", "npn-h2");
167 alternative_service_dict->SetString("host", "maps.google.com");
168 alternative_service_dict->SetInteger("port", 443);
169 base::ListValue* alternative_service_list = new base::ListValue;
170 alternative_service_list->Append(alternative_service_dict);
171 server_pref_dict->SetWithoutPathExpansion("alternative_service",
172 alternative_service_list);
174 // Set up ServerNetworkStats for www.google.com:80.
175 base::DictionaryValue* stats = new base::DictionaryValue;
176 stats->SetInteger("srtt", 10);
177 server_pref_dict->SetWithoutPathExpansion("network_stats", stats);
179 // Set the server preference for www.google.com:80.
180 base::DictionaryValue* servers_dict = new base::DictionaryValue;
181 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict);
183 // Set the preference for mail.google.com server.
184 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
186 // Set supports_spdy for mail.google.com:80
187 server_pref_dict1->SetBoolean("supports_spdy", true);
189 // Set up alternate_protocol for mail.google.com:80 to test migration to
190 // alternative-service.
191 base::DictionaryValue* alternate_protocol_dict = new base::DictionaryValue;
192 alternate_protocol_dict->SetString("protocol_str", "npn-spdy/3.1");
193 alternate_protocol_dict->SetInteger("port", 444);
194 server_pref_dict1->SetWithoutPathExpansion("alternate_protocol",
195 alternate_protocol_dict);
197 // Set up ServerNetworkStats for mail.google.com:80.
198 base::DictionaryValue* stats1 = new base::DictionaryValue;
199 stats1->SetInteger("srtt", 20);
200 server_pref_dict1->SetWithoutPathExpansion("network_stats", stats1);
201 // Set the server preference for mail.google.com:80.
202 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
203 server_pref_dict1);
205 base::DictionaryValue* http_server_properties_dict =
206 new base::DictionaryValue;
207 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
208 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
209 base::DictionaryValue* supports_quic = new base::DictionaryValue;
210 supports_quic->SetBoolean("used_quic", true);
211 supports_quic->SetString("address", "127.0.0.1");
212 http_server_properties_dict->SetWithoutPathExpansion("supports_quic",
213 supports_quic);
215 // Set the same value for kHttpServerProperties multiple times.
216 pref_service_.SetManagedPref(kTestHttpServerProperties,
217 http_server_properties_dict);
218 base::DictionaryValue* http_server_properties_dict2 =
219 http_server_properties_dict->DeepCopy();
220 pref_service_.SetManagedPref(kTestHttpServerProperties,
221 http_server_properties_dict2);
223 base::RunLoop().RunUntilIdle();
224 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
226 // Verify SupportsSpdy.
227 EXPECT_TRUE(
228 http_server_props_manager_->SupportsRequestPriority(google_server));
229 EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(mail_server));
230 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority(
231 HostPortPair::FromString("foo.google.com:1337")));
233 // Verify alternative service.
234 AlternativeService alternative_service =
235 http_server_props_manager_->GetAlternativeService(google_server);
236 EXPECT_EQ(NPN_SPDY_4, alternative_service.protocol);
237 EXPECT_EQ("maps.google.com", alternative_service.host);
238 EXPECT_EQ(443, alternative_service.port);
239 alternative_service =
240 http_server_props_manager_->GetAlternativeService(mail_server);
241 EXPECT_EQ(NPN_SPDY_3_1, alternative_service.protocol);
242 EXPECT_EQ("mail.google.com", alternative_service.host);
243 EXPECT_EQ(444, alternative_service.port);
245 // Verify SupportsQuic.
246 IPAddressNumber last_address;
247 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&last_address));
248 EXPECT_EQ("127.0.0.1", IPAddressToString(last_address));
250 // Verify ServerNetworkStats.
251 const ServerNetworkStats* stats2 =
252 http_server_props_manager_->GetServerNetworkStats(google_server);
253 EXPECT_EQ(10, stats2->srtt.ToInternalValue());
254 const ServerNetworkStats* stats3 =
255 http_server_props_manager_->GetServerNetworkStats(mail_server);
256 EXPECT_EQ(20, stats3->srtt.ToInternalValue());
259 TEST_F(HttpServerPropertiesManagerTest, BadCachedHostPortPair) {
260 ExpectCacheUpdate();
261 // The prefs are automaticalls updated in the case corruption is detected.
262 ExpectPrefsUpdate();
264 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
266 // Set supports_spdy for www.google.com:65536.
267 server_pref_dict->SetBoolean("supports_spdy", true);
269 // Set up alternative_service for www.google.com:65536.
271 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
272 alternative_service_dict->SetString("protocol_str", "npn-spdy/3");
273 alternative_service_dict->SetInteger("port", 80);
274 base::ListValue* alternative_service_list = new base::ListValue;
275 alternative_service_list->Append(alternative_service_dict);
276 server_pref_dict->SetWithoutPathExpansion("alternative_service",
277 alternative_service_list);
279 // Set up ServerNetworkStats for www.google.com:65536.
280 base::DictionaryValue* stats = new base::DictionaryValue;
281 stats->SetInteger("srtt", 10);
282 server_pref_dict->SetWithoutPathExpansion("network_stats", stats);
284 // Set the server preference for www.google.com:65536.
285 base::DictionaryValue* servers_dict = new base::DictionaryValue;
286 servers_dict->SetWithoutPathExpansion("www.google.com:65536",
287 server_pref_dict);
289 base::DictionaryValue* http_server_properties_dict =
290 new base::DictionaryValue;
291 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
292 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
294 // Set up the pref.
295 pref_service_.SetManagedPref(kTestHttpServerProperties,
296 http_server_properties_dict);
298 base::RunLoop().RunUntilIdle();
299 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
301 // Verify that nothing is set.
302 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority(
303 HostPortPair::FromString("www.google.com:65536")));
304 EXPECT_FALSE(
305 HasAlternativeService(HostPortPair::FromString("www.google.com:65536")));
306 const ServerNetworkStats* stats1 =
307 http_server_props_manager_->GetServerNetworkStats(
308 HostPortPair::FromString("www.google.com:65536"));
309 EXPECT_EQ(NULL, stats1);
312 TEST_F(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) {
313 ExpectCacheUpdate();
314 // The prefs are automaticalls updated in the case corruption is detected.
315 ExpectPrefsUpdate();
317 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
319 // Set supports_spdy for www.google.com:80.
320 server_pref_dict->SetBoolean("supports_spdy", true);
322 // Set up alternative_service for www.google.com:80.
323 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
324 alternative_service_dict->SetString("protocol_str", "npn-spdy/3");
325 alternative_service_dict->SetInteger("port", 65536);
326 base::ListValue* alternative_service_list = new base::ListValue;
327 alternative_service_list->Append(alternative_service_dict);
328 server_pref_dict->SetWithoutPathExpansion("alternative_service",
329 alternative_service_list);
331 // Set the server preference for www.google.com:80.
332 base::DictionaryValue* servers_dict = new base::DictionaryValue;
333 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict);
335 base::DictionaryValue* http_server_properties_dict =
336 new base::DictionaryValue;
337 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
338 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
340 // Set up the pref.
341 pref_service_.SetManagedPref(kTestHttpServerProperties,
342 http_server_properties_dict);
344 base::RunLoop().RunUntilIdle();
345 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
347 // Verify alternative service is not set.
348 EXPECT_FALSE(
349 HasAlternativeService(HostPortPair::FromString("www.google.com:80")));
352 TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) {
353 ExpectPrefsUpdate();
355 // Post an update task to the network thread. SetSupportsSpdy calls
356 // ScheduleUpdatePrefsOnNetworkThread.
358 // Add mail.google.com:443 as a supporting spdy server.
359 HostPortPair spdy_server_mail("mail.google.com", 443);
360 EXPECT_FALSE(
361 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
362 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
364 // Run the task.
365 base::RunLoop().RunUntilIdle();
367 EXPECT_TRUE(
368 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
369 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
372 TEST_F(HttpServerPropertiesManagerTest, SetSpdySetting) {
373 ExpectPrefsUpdate();
375 // Add SpdySetting for mail.google.com:443.
376 HostPortPair spdy_server_mail("mail.google.com", 443);
377 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
378 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
379 const uint32 value1 = 31337;
380 http_server_props_manager_->SetSpdySetting(
381 spdy_server_mail, id1, flags1, value1);
383 // Run the task.
384 base::RunLoop().RunUntilIdle();
386 const SettingsMap& settings_map1_ret =
387 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
388 ASSERT_EQ(1U, settings_map1_ret.size());
389 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
390 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
391 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
392 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
393 EXPECT_EQ(value1, flags_and_value1_ret.second);
395 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
398 TEST_F(HttpServerPropertiesManagerTest, ClearSpdySetting) {
399 ExpectPrefsUpdateRepeatedly();
401 // Add SpdySetting for mail.google.com:443.
402 HostPortPair spdy_server_mail("mail.google.com", 443);
403 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
404 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
405 const uint32 value1 = 31337;
406 http_server_props_manager_->SetSpdySetting(
407 spdy_server_mail, id1, flags1, value1);
409 // Run the task.
410 base::RunLoop().RunUntilIdle();
412 const SettingsMap& settings_map1_ret =
413 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
414 ASSERT_EQ(1U, settings_map1_ret.size());
415 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
416 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
417 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
418 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
419 EXPECT_EQ(value1, flags_and_value1_ret.second);
421 // Clear SpdySetting for mail.google.com:443.
422 http_server_props_manager_->ClearSpdySettings(spdy_server_mail);
424 // Run the task.
425 base::RunLoop().RunUntilIdle();
427 // Verify that there are no entries in the settings map for
428 // mail.google.com:443.
429 const SettingsMap& settings_map2_ret =
430 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
431 ASSERT_EQ(0U, settings_map2_ret.size());
433 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
436 TEST_F(HttpServerPropertiesManagerTest, ClearAllSpdySetting) {
437 ExpectPrefsUpdateRepeatedly();
439 // Add SpdySetting for mail.google.com:443.
440 HostPortPair spdy_server_mail("mail.google.com", 443);
441 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
442 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
443 const uint32 value1 = 31337;
444 http_server_props_manager_->SetSpdySetting(
445 spdy_server_mail, id1, flags1, value1);
447 // Run the task.
448 base::RunLoop().RunUntilIdle();
450 const SettingsMap& settings_map1_ret =
451 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
452 ASSERT_EQ(1U, settings_map1_ret.size());
453 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
454 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
455 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
456 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
457 EXPECT_EQ(value1, flags_and_value1_ret.second);
459 // Clear All SpdySettings.
460 http_server_props_manager_->ClearAllSpdySettings();
462 // Run the task.
463 base::RunLoop().RunUntilIdle();
465 // Verify that there are no entries in the settings map.
466 const SpdySettingsMap& spdy_settings_map2_ret =
467 http_server_props_manager_->spdy_settings_map();
468 ASSERT_EQ(0U, spdy_settings_map2_ret.size());
470 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
473 TEST_F(HttpServerPropertiesManagerTest, GetAlternativeService) {
474 ExpectPrefsUpdate();
476 HostPortPair spdy_server_mail("mail.google.com", 80);
477 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
478 AlternativeService alternative_service(NPN_SPDY_4, "mail.google.com", 443);
479 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
480 alternative_service, 1.0);
482 // Run the task.
483 base::RunLoop().RunUntilIdle();
484 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
486 alternative_service =
487 http_server_props_manager_->GetAlternativeService(spdy_server_mail);
488 EXPECT_EQ(443, alternative_service.port);
489 EXPECT_EQ(NPN_SPDY_4, alternative_service.protocol);
492 TEST_F(HttpServerPropertiesManagerTest, SupportsQuic) {
493 ExpectPrefsUpdate();
495 IPAddressNumber address;
496 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address));
498 IPAddressNumber actual_address;
499 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
500 http_server_props_manager_->SetSupportsQuic(true, actual_address);
502 // Run the task.
503 base::RunLoop().RunUntilIdle();
504 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
506 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
507 EXPECT_EQ(actual_address, address);
510 TEST_F(HttpServerPropertiesManagerTest, ServerNetworkStats) {
511 ExpectPrefsUpdate();
513 HostPortPair mail_server("mail.google.com", 80);
514 const ServerNetworkStats* stats =
515 http_server_props_manager_->GetServerNetworkStats(mail_server);
516 EXPECT_EQ(NULL, stats);
517 ServerNetworkStats stats1;
518 stats1.srtt = base::TimeDelta::FromMicroseconds(10);
519 http_server_props_manager_->SetServerNetworkStats(mail_server, stats1);
521 // Run the task.
522 base::RunLoop().RunUntilIdle();
523 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
525 const ServerNetworkStats* stats2 =
526 http_server_props_manager_->GetServerNetworkStats(mail_server);
527 EXPECT_EQ(10, stats2->srtt.ToInternalValue());
530 TEST_F(HttpServerPropertiesManagerTest, Clear) {
531 ExpectPrefsUpdate();
533 HostPortPair spdy_server_mail("mail.google.com", 443);
534 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
535 AlternativeService alternative_service(NPN_SPDY_4, "mail.google.com", 443);
536 http_server_props_manager_->SetAlternativeService(spdy_server_mail,
537 alternative_service, 1.0);
538 IPAddressNumber actual_address;
539 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
540 http_server_props_manager_->SetSupportsQuic(true, actual_address);
541 ServerNetworkStats stats;
542 stats.srtt = base::TimeDelta::FromMicroseconds(10);
543 http_server_props_manager_->SetServerNetworkStats(spdy_server_mail, stats);
545 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
546 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
547 const uint32 value1 = 31337;
548 http_server_props_manager_->SetSpdySetting(
549 spdy_server_mail, id1, flags1, value1);
551 // Run the task.
552 base::RunLoop().RunUntilIdle();
554 EXPECT_TRUE(
555 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
556 EXPECT_TRUE(HasAlternativeService(spdy_server_mail));
557 IPAddressNumber address;
558 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
559 EXPECT_EQ(actual_address, address);
560 const ServerNetworkStats* stats1 =
561 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
562 EXPECT_EQ(10, stats1->srtt.ToInternalValue());
564 // Check SPDY settings values.
565 const SettingsMap& settings_map1_ret =
566 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
567 ASSERT_EQ(1U, settings_map1_ret.size());
568 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
569 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
570 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
571 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
572 EXPECT_EQ(value1, flags_and_value1_ret.second);
574 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
576 ExpectPrefsUpdate();
578 // Clear http server data, time out if we do not get a completion callback.
579 http_server_props_manager_->Clear(base::MessageLoop::QuitClosure());
580 base::RunLoop().Run();
582 EXPECT_FALSE(
583 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
584 EXPECT_FALSE(HasAlternativeService(spdy_server_mail));
585 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address));
586 const ServerNetworkStats* stats2 =
587 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
588 EXPECT_EQ(NULL, stats2);
590 const SettingsMap& settings_map2_ret =
591 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
592 EXPECT_EQ(0U, settings_map2_ret.size());
594 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
597 // https://crbug.com/444956: Add 200 alternative_service servers followed by
598 // supports_quic and verify we have read supports_quic from prefs.
599 TEST_F(HttpServerPropertiesManagerTest, BadSupportsQuic) {
600 ExpectCacheUpdate();
602 base::DictionaryValue* servers_dict = new base::DictionaryValue;
604 for (int i = 0; i < 200; ++i) {
605 // Set up alternative_service for www.google.com:i.
606 base::DictionaryValue* alternative_service_dict = new base::DictionaryValue;
607 alternative_service_dict->SetString("protocol_str", "npn-h2");
608 alternative_service_dict->SetString("host", "");
609 alternative_service_dict->SetInteger("port", i);
610 base::ListValue* alternative_service_list = new base::ListValue;
611 alternative_service_list->Append(alternative_service_dict);
612 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
613 server_pref_dict->SetWithoutPathExpansion("alternative_service",
614 alternative_service_list);
615 servers_dict->SetWithoutPathExpansion(StringPrintf("www.google.com:%d", i),
616 server_pref_dict);
619 // Set the preference for mail.google.com server.
620 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
622 // Set the server preference for mail.google.com:80.
623 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
624 server_pref_dict1);
626 base::DictionaryValue* http_server_properties_dict =
627 new base::DictionaryValue;
628 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
629 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
631 // Set up SupportsQuic for 127.0.0.1
632 base::DictionaryValue* supports_quic = new base::DictionaryValue;
633 supports_quic->SetBoolean("used_quic", true);
634 supports_quic->SetString("address", "127.0.0.1");
635 http_server_properties_dict->SetWithoutPathExpansion("supports_quic",
636 supports_quic);
638 // Set up the pref.
639 pref_service_.SetManagedPref(kTestHttpServerProperties,
640 http_server_properties_dict);
642 base::RunLoop().RunUntilIdle();
643 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
645 // Verify alternative service.
646 for (int i = 0; i < 200; ++i) {
647 std::string server = StringPrintf("www.google.com:%d", i);
648 AlternativeService alternative_service =
649 http_server_props_manager_->GetAlternativeService(
650 HostPortPair::FromString(server));
651 EXPECT_EQ(i, alternative_service.port);
652 EXPECT_EQ(NPN_SPDY_4, alternative_service.protocol);
655 // Verify SupportsQuic.
656 IPAddressNumber address;
657 ASSERT_TRUE(http_server_props_manager_->GetSupportsQuic(&address));
658 EXPECT_EQ("127.0.0.1", IPAddressToString(address));
661 TEST_F(HttpServerPropertiesManagerTest, UpdateCacheWithPrefs) {
662 const HostPortPair server_www("www.google.com", 80);
663 const HostPortPair server_mail("mail.google.com", 80);
665 // Set alternate protocol.
666 AlternativeService www_altsvc(NPN_SPDY_4, "", 443);
667 AlternativeService mail_altsvc(NPN_SPDY_3_1, "mail.google.com", 444);
668 http_server_props_manager_->SetAlternativeService(server_www, www_altsvc,
669 1.0);
670 http_server_props_manager_->SetAlternativeService(server_mail, mail_altsvc,
671 0.2);
673 // Set ServerNetworkStats.
674 ServerNetworkStats stats;
675 stats.srtt = base::TimeDelta::FromInternalValue(42);
676 http_server_props_manager_->SetServerNetworkStats(server_mail, stats);
678 // Set SupportsQuic.
679 IPAddressNumber actual_address;
680 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address));
681 http_server_props_manager_->SetSupportsQuic(true, actual_address);
683 // Update cache.
684 ExpectPrefsUpdate();
685 ExpectCacheUpdate();
686 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
687 base::RunLoop().RunUntilIdle();
689 // Verify preferences.
690 const char expected_json[] =
691 "{\"servers\":{\"mail.google.com:80\":{\"alternative_service\":[{"
692 "\"host\":"
693 "\"mail.google.com\",\"port\":444,\"probability\":0.2,\"protocol_str\":"
694 "\"npn-spdy/"
695 "3.1\"}],\"network_stats\":{\"srtt\":42}},\"www.google.com:80\":"
696 "{\"alternative_service\":[{\"port\":443,\"probability\":1.0,"
697 "\"protocol_str\":\"npn-h2\"}]}},\"supports_quic\":"
698 "{\"address\":\"127.0.0.1\",\"used_quic\":true},\"version\":3}";
700 const base::Value* http_server_properties =
701 pref_service_.GetUserPref(kTestHttpServerProperties);
702 ASSERT_NE(nullptr, http_server_properties);
703 std::string preferences_json;
704 EXPECT_TRUE(
705 base::JSONWriter::Write(http_server_properties, &preferences_json));
706 EXPECT_EQ(expected_json, preferences_json);
709 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache0) {
710 // Post an update task to the UI thread.
711 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
712 // Shutdown comes before the task is executed.
713 http_server_props_manager_->ShutdownOnPrefThread();
714 http_server_props_manager_.reset();
715 // Run the task after shutdown and deletion.
716 base::RunLoop().RunUntilIdle();
719 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache1) {
720 // Post an update task.
721 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
722 // Shutdown comes before the task is executed.
723 http_server_props_manager_->ShutdownOnPrefThread();
724 // Run the task after shutdown, but before deletion.
725 base::RunLoop().RunUntilIdle();
726 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
727 http_server_props_manager_.reset();
728 base::RunLoop().RunUntilIdle();
731 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache2) {
732 http_server_props_manager_->UpdateCacheFromPrefsOnUIConcrete();
733 // Shutdown comes before the task is executed.
734 http_server_props_manager_->ShutdownOnPrefThread();
735 // Run the task after shutdown, but before deletion.
736 base::RunLoop().RunUntilIdle();
737 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
738 http_server_props_manager_.reset();
739 base::RunLoop().RunUntilIdle();
743 // Tests for shutdown when updating prefs.
745 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs0) {
746 // Post an update task to the IO thread.
747 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
748 // Shutdown comes before the task is executed.
749 http_server_props_manager_->ShutdownOnPrefThread();
750 http_server_props_manager_.reset();
751 // Run the task after shutdown and deletion.
752 base::RunLoop().RunUntilIdle();
755 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs1) {
756 ExpectPrefsUpdate();
757 // Post an update task.
758 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
759 // Shutdown comes before the task is executed.
760 http_server_props_manager_->ShutdownOnPrefThread();
761 // Run the task after shutdown, but before deletion.
762 base::RunLoop().RunUntilIdle();
763 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
764 http_server_props_manager_.reset();
765 base::RunLoop().RunUntilIdle();
768 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs2) {
769 // This posts a task to the UI thread.
770 http_server_props_manager_->UpdatePrefsFromCacheOnNetworkThreadConcrete(
771 base::Closure());
772 // Shutdown comes before the task is executed.
773 http_server_props_manager_->ShutdownOnPrefThread();
774 // Run the task after shutdown, but before deletion.
775 base::RunLoop().RunUntilIdle();
776 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
777 http_server_props_manager_.reset();
778 base::RunLoop().RunUntilIdle();
781 } // namespace
783 } // namespace net