Fix UnlockEndpointIsDelayed again.
[chromium-blink-merge.git] / net / http / http_server_properties_manager_unittest.cc
blobd021fc502190e249ae579edfc5e0a60c9105089a
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/message_loop/message_loop.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/stringprintf.h"
14 #include "base/test/test_simple_task_runner.h"
15 #include "base/values.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "url/gurl.h"
20 namespace net {
22 namespace {
24 using base::StringPrintf;
25 using ::testing::_;
26 using ::testing::Invoke;
27 using ::testing::Mock;
28 using ::testing::StrictMock;
30 const char kTestHttpServerProperties[] = "TestHttpServerProperties";
32 class TestingHttpServerPropertiesManager : public HttpServerPropertiesManager {
33 public:
34 TestingHttpServerPropertiesManager(
35 PrefService* pref_service,
36 const char* pref_path,
37 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
38 : HttpServerPropertiesManager(pref_service, pref_path, io_task_runner) {
39 InitializeOnNetworkThread();
42 virtual ~TestingHttpServerPropertiesManager() {}
44 // Make these methods public for testing.
45 using HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread;
46 using HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread;
48 // Post tasks without a delay during tests.
49 virtual void StartPrefsUpdateTimerOnNetworkThread(
50 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 virtual void StartCacheUpdateTimerOnPrefThread(
61 base::TimeDelta delay) override {
62 HttpServerPropertiesManager::StartCacheUpdateTimerOnPrefThread(
63 base::TimeDelta());
66 void UpdatePrefsFromCacheOnNetworkThreadConcrete(
67 const base::Closure& callback) {
68 HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread(callback);
71 MOCK_METHOD0(UpdateCacheFromPrefsOnPrefThread, void());
72 MOCK_METHOD1(UpdatePrefsFromCacheOnNetworkThread, void(const base::Closure&));
73 MOCK_METHOD6(UpdateCacheFromPrefsOnNetworkThread,
74 void(std::vector<std::string>* spdy_servers,
75 SpdySettingsMap* spdy_settings_map,
76 AlternateProtocolMap* alternate_protocol_map,
77 SupportsQuicMap* supports_quic_map,
78 ServerNetworkStatsMap* server_network_stats_map,
79 bool detected_corrupted_prefs));
80 MOCK_METHOD5(UpdatePrefsOnPref,
81 void(base::ListValue* spdy_server_list,
82 SpdySettingsMap* spdy_settings_map,
83 AlternateProtocolMap* alternate_protocol_map,
84 SupportsQuicMap* supports_quic_map,
85 ServerNetworkStatsMap* server_network_stats_map));
87 private:
88 DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager);
91 class HttpServerPropertiesManagerTest : public testing::Test {
92 protected:
93 HttpServerPropertiesManagerTest() {}
95 void SetUp() override {
96 pref_service_.registry()->RegisterDictionaryPref(kTestHttpServerProperties);
97 http_server_props_manager_.reset(
98 new StrictMock<TestingHttpServerPropertiesManager>(
99 &pref_service_,
100 kTestHttpServerProperties,
101 base::MessageLoop::current()->message_loop_proxy()));
102 ExpectCacheUpdate();
103 base::RunLoop().RunUntilIdle();
106 void TearDown() override {
107 if (http_server_props_manager_.get())
108 http_server_props_manager_->ShutdownOnPrefThread();
109 base::RunLoop().RunUntilIdle();
110 http_server_props_manager_.reset();
113 void ExpectCacheUpdate() {
114 EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefsOnPrefThread())
115 .WillOnce(Invoke(http_server_props_manager_.get(),
116 &TestingHttpServerPropertiesManager::
117 UpdateCacheFromPrefsOnUIConcrete));
120 void ExpectPrefsUpdate() {
121 EXPECT_CALL(*http_server_props_manager_,
122 UpdatePrefsFromCacheOnNetworkThread(_))
123 .WillOnce(Invoke(http_server_props_manager_.get(),
124 &TestingHttpServerPropertiesManager::
125 UpdatePrefsFromCacheOnNetworkThreadConcrete));
128 void ExpectPrefsUpdateRepeatedly() {
129 EXPECT_CALL(*http_server_props_manager_,
130 UpdatePrefsFromCacheOnNetworkThread(_))
131 .WillRepeatedly(
132 Invoke(http_server_props_manager_.get(),
133 &TestingHttpServerPropertiesManager::
134 UpdatePrefsFromCacheOnNetworkThreadConcrete));
137 //base::RunLoop loop_;
138 TestingPrefServiceSimple pref_service_;
139 scoped_ptr<TestingHttpServerPropertiesManager> http_server_props_manager_;
141 private:
142 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest);
145 TEST_F(HttpServerPropertiesManagerTest,
146 SingleUpdateForTwoSpdyServerPrefChanges) {
147 ExpectCacheUpdate();
149 // Set up the prefs for www.google.com:80 and mail.google.com:80 and then set
150 // it twice. Only expect a single cache update.
152 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
153 HostPortPair google_server("www.google.com", 80);
154 HostPortPair mail_server("mail.google.com", 80);
156 // Set supports_spdy for www.google.com:80.
157 server_pref_dict->SetBoolean("supports_spdy", true);
159 // Set up alternate_protocol for www.google.com:80.
160 base::DictionaryValue* alternate_protocol = new base::DictionaryValue;
161 alternate_protocol->SetInteger("port", 443);
162 alternate_protocol->SetString("protocol_str", "npn-spdy/3");
163 server_pref_dict->SetWithoutPathExpansion("alternate_protocol",
164 alternate_protocol);
166 // Set up SupportsQuic for www.google.com:80.
167 base::DictionaryValue* supports_quic = new base::DictionaryValue;
168 supports_quic->SetBoolean("used_quic", true);
169 supports_quic->SetString("address", "foo");
170 server_pref_dict->SetWithoutPathExpansion("supports_quic", supports_quic);
172 // Set up ServerNetworkStats for www.google.com:80.
173 base::DictionaryValue* stats = new base::DictionaryValue;
174 stats->SetInteger("srtt", 10);
175 server_pref_dict->SetWithoutPathExpansion("network_stats", stats);
177 // Set the server preference for www.google.com:80.
178 base::DictionaryValue* servers_dict = new base::DictionaryValue;
179 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict);
181 // Set the preference for mail.google.com server.
182 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
184 // Set supports_spdy for mail.google.com:80
185 server_pref_dict1->SetBoolean("supports_spdy", true);
187 // Set up alternate_protocol for mail.google.com:80
188 base::DictionaryValue* alternate_protocol1 = new base::DictionaryValue;
189 alternate_protocol1->SetInteger("port", 444);
190 alternate_protocol1->SetString("protocol_str", "npn-spdy/3.1");
192 server_pref_dict1->SetWithoutPathExpansion("alternate_protocol",
193 alternate_protocol1);
195 // Set up SupportsQuic for mail.google.com:80
196 base::DictionaryValue* supports_quic1 = new base::DictionaryValue;
197 supports_quic1->SetBoolean("used_quic", false);
198 supports_quic1->SetString("address", "bar");
199 server_pref_dict1->SetWithoutPathExpansion("supports_quic", supports_quic1);
201 // Set up ServerNetworkStats for mail.google.com:80.
202 base::DictionaryValue* stats1 = new base::DictionaryValue;
203 stats1->SetInteger("srtt", 20);
204 server_pref_dict1->SetWithoutPathExpansion("network_stats", stats1);
205 // Set the server preference for mail.google.com:80.
206 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
207 server_pref_dict1);
209 base::DictionaryValue* http_server_properties_dict =
210 new base::DictionaryValue;
211 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
212 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
214 // Set the same value for kHttpServerProperties multiple times.
215 pref_service_.SetManagedPref(kTestHttpServerProperties,
216 http_server_properties_dict);
217 base::DictionaryValue* http_server_properties_dict2 =
218 http_server_properties_dict->DeepCopy();
219 pref_service_.SetManagedPref(kTestHttpServerProperties,
220 http_server_properties_dict2);
222 base::RunLoop().RunUntilIdle();
223 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
225 // Verify SupportsSpdy.
226 EXPECT_TRUE(
227 http_server_props_manager_->SupportsRequestPriority(google_server));
228 EXPECT_TRUE(http_server_props_manager_->SupportsRequestPriority(mail_server));
229 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority(
230 HostPortPair::FromString("foo.google.com:1337")));
232 // Verify AlternateProtocol.
233 ASSERT_TRUE(http_server_props_manager_->HasAlternateProtocol(google_server));
234 ASSERT_TRUE(http_server_props_manager_->HasAlternateProtocol(mail_server));
235 AlternateProtocolInfo port_alternate_protocol =
236 http_server_props_manager_->GetAlternateProtocol(google_server);
237 EXPECT_EQ(443, port_alternate_protocol.port);
238 EXPECT_EQ(NPN_SPDY_3, port_alternate_protocol.protocol);
239 port_alternate_protocol =
240 http_server_props_manager_->GetAlternateProtocol(mail_server);
241 EXPECT_EQ(444, port_alternate_protocol.port);
242 EXPECT_EQ(NPN_SPDY_3_1, port_alternate_protocol.protocol);
244 // Verify SupportsQuic.
245 SupportsQuic supports_quic2 =
246 http_server_props_manager_->GetSupportsQuic(google_server);
247 EXPECT_TRUE(supports_quic2.used_quic);
248 EXPECT_EQ("foo", supports_quic2.address);
249 supports_quic2 = http_server_props_manager_->GetSupportsQuic(mail_server);
250 EXPECT_FALSE(supports_quic2.used_quic);
251 EXPECT_EQ("bar", supports_quic2.address);
253 // Verify ServerNetworkStats.
254 const ServerNetworkStats* stats2 =
255 http_server_props_manager_->GetServerNetworkStats(google_server);
256 EXPECT_EQ(10, stats2->srtt.ToInternalValue());
257 const ServerNetworkStats* stats3 =
258 http_server_props_manager_->GetServerNetworkStats(mail_server);
259 EXPECT_EQ(20, stats3->srtt.ToInternalValue());
262 TEST_F(HttpServerPropertiesManagerTest, BadCachedHostPortPair) {
263 ExpectCacheUpdate();
264 // The prefs are automaticalls updated in the case corruption is detected.
265 ExpectPrefsUpdate();
267 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
269 // Set supports_spdy for www.google.com:65536.
270 server_pref_dict->SetBoolean("supports_spdy", true);
272 // Set up alternate_protocol for www.google.com:65536.
273 base::DictionaryValue* alternate_protocol = new base::DictionaryValue;
274 alternate_protocol->SetInteger("port", 80);
275 alternate_protocol->SetString("protocol_str", "npn-spdy/3");
276 server_pref_dict->SetWithoutPathExpansion("alternate_protocol",
277 alternate_protocol);
279 // Set up SupportsQuic for www.google.com:65536.
280 base::DictionaryValue* supports_quic = new base::DictionaryValue;
281 supports_quic->SetBoolean("used_quic", true);
282 supports_quic->SetString("address", "foo");
283 server_pref_dict->SetWithoutPathExpansion("supports_quic", supports_quic);
285 // Set up ServerNetworkStats for www.google.com:65536.
286 base::DictionaryValue* stats = new base::DictionaryValue;
287 stats->SetInteger("srtt", 10);
288 server_pref_dict->SetWithoutPathExpansion("network_stats", stats);
290 // Set the server preference for www.google.com:65536.
291 base::DictionaryValue* servers_dict = new base::DictionaryValue;
292 servers_dict->SetWithoutPathExpansion("www.google.com:65536",
293 server_pref_dict);
295 base::DictionaryValue* http_server_properties_dict =
296 new base::DictionaryValue;
297 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
298 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
300 // Set up the pref.
301 pref_service_.SetManagedPref(kTestHttpServerProperties,
302 http_server_properties_dict);
304 base::RunLoop().RunUntilIdle();
305 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
307 // Verify that nothing is set.
308 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority(
309 HostPortPair::FromString("www.google.com:65536")));
310 EXPECT_FALSE(http_server_props_manager_->HasAlternateProtocol(
311 HostPortPair::FromString("www.google.com:65536")));
312 SupportsQuic supports_quic2 = http_server_props_manager_->GetSupportsQuic(
313 HostPortPair::FromString("www.google.com:65536"));
314 EXPECT_FALSE(supports_quic2.used_quic);
315 const ServerNetworkStats* stats1 =
316 http_server_props_manager_->GetServerNetworkStats(
317 HostPortPair::FromString("www.google.com:65536"));
318 EXPECT_EQ(NULL, stats1);
321 TEST_F(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) {
322 ExpectCacheUpdate();
323 // The prefs are automaticalls updated in the case corruption is detected.
324 ExpectPrefsUpdate();
326 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
328 // Set supports_spdy for www.google.com:80.
329 server_pref_dict->SetBoolean("supports_spdy", true);
331 // Set up alternate_protocol for www.google.com:80.
332 base::DictionaryValue* alternate_protocol = new base::DictionaryValue;
333 alternate_protocol->SetInteger("port", 65536);
334 alternate_protocol->SetString("protocol_str", "npn-spdy/3");
335 server_pref_dict->SetWithoutPathExpansion("alternate_protocol",
336 alternate_protocol);
338 // Set the server preference for www.google.com:80.
339 base::DictionaryValue* servers_dict = new base::DictionaryValue;
340 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict);
342 base::DictionaryValue* http_server_properties_dict =
343 new base::DictionaryValue;
344 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
345 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
347 // Set up the pref.
348 pref_service_.SetManagedPref(kTestHttpServerProperties,
349 http_server_properties_dict);
351 base::RunLoop().RunUntilIdle();
352 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
354 // Verify AlternateProtocol is not set.
355 EXPECT_FALSE(http_server_props_manager_->HasAlternateProtocol(
356 HostPortPair::FromString("www.google.com:80")));
359 TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) {
360 ExpectPrefsUpdate();
362 // Post an update task to the network thread. SetSupportsSpdy calls
363 // ScheduleUpdatePrefsOnNetworkThread.
365 // Add mail.google.com:443 as a supporting spdy server.
366 HostPortPair spdy_server_mail("mail.google.com", 443);
367 EXPECT_FALSE(
368 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
369 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
371 // Run the task.
372 base::RunLoop().RunUntilIdle();
374 EXPECT_TRUE(
375 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
376 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
379 TEST_F(HttpServerPropertiesManagerTest, SetSpdySetting) {
380 ExpectPrefsUpdate();
382 // Add SpdySetting for mail.google.com:443.
383 HostPortPair spdy_server_mail("mail.google.com", 443);
384 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
385 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
386 const uint32 value1 = 31337;
387 http_server_props_manager_->SetSpdySetting(
388 spdy_server_mail, id1, flags1, value1);
390 // Run the task.
391 base::RunLoop().RunUntilIdle();
393 const SettingsMap& settings_map1_ret =
394 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
395 ASSERT_EQ(1U, settings_map1_ret.size());
396 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
397 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
398 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
399 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
400 EXPECT_EQ(value1, flags_and_value1_ret.second);
402 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
405 TEST_F(HttpServerPropertiesManagerTest, ClearSpdySetting) {
406 ExpectPrefsUpdateRepeatedly();
408 // Add SpdySetting for mail.google.com:443.
409 HostPortPair spdy_server_mail("mail.google.com", 443);
410 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
411 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
412 const uint32 value1 = 31337;
413 http_server_props_manager_->SetSpdySetting(
414 spdy_server_mail, id1, flags1, value1);
416 // Run the task.
417 base::RunLoop().RunUntilIdle();
419 const SettingsMap& settings_map1_ret =
420 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
421 ASSERT_EQ(1U, settings_map1_ret.size());
422 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
423 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
424 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
425 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
426 EXPECT_EQ(value1, flags_and_value1_ret.second);
428 // Clear SpdySetting for mail.google.com:443.
429 http_server_props_manager_->ClearSpdySettings(spdy_server_mail);
431 // Run the task.
432 base::RunLoop().RunUntilIdle();
434 // Verify that there are no entries in the settings map for
435 // mail.google.com:443.
436 const SettingsMap& settings_map2_ret =
437 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
438 ASSERT_EQ(0U, settings_map2_ret.size());
440 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
443 TEST_F(HttpServerPropertiesManagerTest, ClearAllSpdySetting) {
444 ExpectPrefsUpdateRepeatedly();
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 All SpdySettings.
467 http_server_props_manager_->ClearAllSpdySettings();
469 // Run the task.
470 base::RunLoop().RunUntilIdle();
472 // Verify that there are no entries in the settings map.
473 const SpdySettingsMap& spdy_settings_map2_ret =
474 http_server_props_manager_->spdy_settings_map();
475 ASSERT_EQ(0U, spdy_settings_map2_ret.size());
477 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
480 TEST_F(HttpServerPropertiesManagerTest, HasAlternateProtocol) {
481 ExpectPrefsUpdate();
483 HostPortPair spdy_server_mail("mail.google.com", 80);
484 EXPECT_FALSE(
485 http_server_props_manager_->HasAlternateProtocol(spdy_server_mail));
486 http_server_props_manager_->SetAlternateProtocol(spdy_server_mail, 443,
487 NPN_SPDY_3, 1.0);
489 // Run the task.
490 base::RunLoop().RunUntilIdle();
491 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
493 ASSERT_TRUE(
494 http_server_props_manager_->HasAlternateProtocol(spdy_server_mail));
495 AlternateProtocolInfo port_alternate_protocol =
496 http_server_props_manager_->GetAlternateProtocol(spdy_server_mail);
497 EXPECT_EQ(443, port_alternate_protocol.port);
498 EXPECT_EQ(NPN_SPDY_3, port_alternate_protocol.protocol);
501 TEST_F(HttpServerPropertiesManagerTest, SupportsQuic) {
502 ExpectPrefsUpdate();
504 HostPortPair quic_server_mail("mail.google.com", 80);
505 SupportsQuic supports_quic =
506 http_server_props_manager_->GetSupportsQuic(quic_server_mail);
507 EXPECT_FALSE(supports_quic.used_quic);
508 EXPECT_EQ("", supports_quic.address);
509 http_server_props_manager_->SetSupportsQuic(quic_server_mail, true, "foo");
511 // Run the task.
512 base::RunLoop().RunUntilIdle();
513 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
515 SupportsQuic supports_quic1 =
516 http_server_props_manager_->GetSupportsQuic(quic_server_mail);
517 EXPECT_TRUE(supports_quic1.used_quic);
518 EXPECT_EQ("foo", supports_quic1.address);
521 TEST_F(HttpServerPropertiesManagerTest, ServerNetworkStats) {
522 ExpectPrefsUpdate();
524 HostPortPair mail_server("mail.google.com", 80);
525 const ServerNetworkStats* stats =
526 http_server_props_manager_->GetServerNetworkStats(mail_server);
527 EXPECT_EQ(NULL, stats);
528 ServerNetworkStats stats1;
529 stats1.srtt = base::TimeDelta::FromMicroseconds(10);
530 http_server_props_manager_->SetServerNetworkStats(mail_server, stats1);
532 // Run the task.
533 base::RunLoop().RunUntilIdle();
534 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
536 const ServerNetworkStats* stats2 =
537 http_server_props_manager_->GetServerNetworkStats(mail_server);
538 EXPECT_EQ(10, stats2->srtt.ToInternalValue());
541 TEST_F(HttpServerPropertiesManagerTest, Clear) {
542 ExpectPrefsUpdate();
544 HostPortPair spdy_server_mail("mail.google.com", 443);
545 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true);
546 http_server_props_manager_->SetAlternateProtocol(spdy_server_mail, 443,
547 NPN_SPDY_3, 1.0);
548 http_server_props_manager_->SetSupportsQuic(spdy_server_mail, true, "foo");
549 ServerNetworkStats stats;
550 stats.srtt = base::TimeDelta::FromMicroseconds(10);
551 http_server_props_manager_->SetServerNetworkStats(spdy_server_mail, stats);
553 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH;
554 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST;
555 const uint32 value1 = 31337;
556 http_server_props_manager_->SetSpdySetting(
557 spdy_server_mail, id1, flags1, value1);
559 // Run the task.
560 base::RunLoop().RunUntilIdle();
562 EXPECT_TRUE(
563 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
564 EXPECT_TRUE(
565 http_server_props_manager_->HasAlternateProtocol(spdy_server_mail));
566 SupportsQuic supports_quic =
567 http_server_props_manager_->GetSupportsQuic(spdy_server_mail);
568 EXPECT_TRUE(supports_quic.used_quic);
569 EXPECT_EQ("foo", supports_quic.address);
570 const ServerNetworkStats* stats1 =
571 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
572 EXPECT_EQ(10, stats1->srtt.ToInternalValue());
574 // Check SPDY settings values.
575 const SettingsMap& settings_map1_ret =
576 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
577 ASSERT_EQ(1U, settings_map1_ret.size());
578 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1);
579 EXPECT_TRUE(it1_ret != settings_map1_ret.end());
580 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second;
581 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first);
582 EXPECT_EQ(value1, flags_and_value1_ret.second);
584 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
586 ExpectPrefsUpdate();
588 // Clear http server data, time out if we do not get a completion callback.
589 http_server_props_manager_->Clear(base::MessageLoop::QuitClosure());
590 base::RunLoop().Run();
592 EXPECT_FALSE(
593 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail));
594 EXPECT_FALSE(
595 http_server_props_manager_->HasAlternateProtocol(spdy_server_mail));
596 SupportsQuic supports_quic1 =
597 http_server_props_manager_->GetSupportsQuic(spdy_server_mail);
598 EXPECT_FALSE(supports_quic1.used_quic);
599 EXPECT_EQ("", supports_quic1.address);
600 const ServerNetworkStats* stats2 =
601 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail);
602 EXPECT_EQ(NULL, stats2);
604 const SettingsMap& settings_map2_ret =
605 http_server_props_manager_->GetSpdySettings(spdy_server_mail);
606 EXPECT_EQ(0U, settings_map2_ret.size());
608 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
611 // https://crbug.com/444956: Add 200 alternate_protocol servers followed by
612 // supports_quic and verify we have read supports_quic from prefs.
613 TEST_F(HttpServerPropertiesManagerTest, BadSupportsQuic) {
614 ExpectCacheUpdate();
616 base::DictionaryValue* servers_dict = new base::DictionaryValue;
618 for (int i = 0; i < 200; ++i) {
619 // Set up alternate_protocol for www.google.com:i.
620 base::DictionaryValue* alternate_protocol = new base::DictionaryValue;
621 alternate_protocol->SetInteger("port", i);
622 alternate_protocol->SetString("protocol_str", "npn-spdy/3");
623 base::DictionaryValue* server_pref_dict = new base::DictionaryValue;
624 server_pref_dict->SetWithoutPathExpansion("alternate_protocol",
625 alternate_protocol);
626 servers_dict->SetWithoutPathExpansion(StringPrintf("www.google.com:%d", i),
627 server_pref_dict);
630 // Set the preference for mail.google.com server.
631 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue;
632 // Set up SupportsQuic for mail.google.com:80
633 base::DictionaryValue* supports_quic = new base::DictionaryValue;
634 supports_quic->SetBoolean("used_quic", true);
635 supports_quic->SetString("address", "bar");
636 server_pref_dict1->SetWithoutPathExpansion("supports_quic", supports_quic);
638 // Set the server preference for mail.google.com:80.
639 servers_dict->SetWithoutPathExpansion("mail.google.com:80",
640 server_pref_dict1);
642 base::DictionaryValue* http_server_properties_dict =
643 new base::DictionaryValue;
644 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1);
645 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict);
647 // Set up the pref.
648 pref_service_.SetManagedPref(kTestHttpServerProperties,
649 http_server_properties_dict);
651 base::RunLoop().RunUntilIdle();
652 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
654 // Verify AlternateProtocol.
655 for (int i = 0; i < 200; ++i) {
656 std::string server = StringPrintf("www.google.com:%d", i);
657 ASSERT_TRUE(http_server_props_manager_->HasAlternateProtocol(
658 HostPortPair::FromString(server)));
659 AlternateProtocolInfo port_alternate_protocol =
660 http_server_props_manager_->GetAlternateProtocol(
661 HostPortPair::FromString(server));
662 EXPECT_EQ(i, port_alternate_protocol.port);
663 EXPECT_EQ(NPN_SPDY_3, port_alternate_protocol.protocol);
666 // Verify SupportsQuic.
667 SupportsQuic supports_quic1 = http_server_props_manager_->GetSupportsQuic(
668 HostPortPair::FromString("mail.google.com:80"));
669 EXPECT_TRUE(supports_quic1.used_quic);
670 EXPECT_EQ("bar", supports_quic1.address);
673 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache0) {
674 // Post an update task to the UI thread.
675 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
676 // Shutdown comes before the task is executed.
677 http_server_props_manager_->ShutdownOnPrefThread();
678 http_server_props_manager_.reset();
679 // Run the task after shutdown and deletion.
680 base::RunLoop().RunUntilIdle();
683 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache1) {
684 // Post an update task.
685 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread();
686 // Shutdown comes before the task is executed.
687 http_server_props_manager_->ShutdownOnPrefThread();
688 // Run the task after shutdown, but before deletion.
689 base::RunLoop().RunUntilIdle();
690 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
691 http_server_props_manager_.reset();
692 base::RunLoop().RunUntilIdle();
695 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache2) {
696 http_server_props_manager_->UpdateCacheFromPrefsOnUIConcrete();
697 // Shutdown comes before the task is executed.
698 http_server_props_manager_->ShutdownOnPrefThread();
699 // Run the task after shutdown, but before deletion.
700 base::RunLoop().RunUntilIdle();
701 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
702 http_server_props_manager_.reset();
703 base::RunLoop().RunUntilIdle();
707 // Tests for shutdown when updating prefs.
709 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs0) {
710 // Post an update task to the IO thread.
711 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
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, ShutdownWithPendingUpdatePrefs1) {
720 ExpectPrefsUpdate();
721 // Post an update task.
722 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread();
723 // Shutdown comes before the task is executed.
724 http_server_props_manager_->ShutdownOnPrefThread();
725 // Run the task after shutdown, but before deletion.
726 base::RunLoop().RunUntilIdle();
727 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
728 http_server_props_manager_.reset();
729 base::RunLoop().RunUntilIdle();
732 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs2) {
733 // This posts a task to the UI thread.
734 http_server_props_manager_->UpdatePrefsFromCacheOnNetworkThreadConcrete(
735 base::Closure());
736 // Shutdown comes before the task is executed.
737 http_server_props_manager_->ShutdownOnPrefThread();
738 // Run the task after shutdown, but before deletion.
739 base::RunLoop().RunUntilIdle();
740 Mock::VerifyAndClearExpectations(http_server_props_manager_.get());
741 http_server_props_manager_.reset();
742 base::RunLoop().RunUntilIdle();
745 } // namespace
747 } // namespace net