OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "net/http/http_server_properties_manager.h" | |
6 | |
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" | |
20 | |
21 namespace net { | |
22 | |
23 namespace { | |
24 | |
25 using base::StringPrintf; | |
26 using ::testing::_; | |
27 using ::testing::Invoke; | |
28 using ::testing::Mock; | |
29 using ::testing::StrictMock; | |
30 | |
31 const char kTestHttpServerProperties[] = "TestHttpServerProperties"; | |
32 | |
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(); | |
41 } | |
42 | |
43 virtual ~TestingHttpServerPropertiesManager() {} | |
44 | |
45 // Make these methods public for testing. | |
46 using HttpServerPropertiesManager::ScheduleUpdateCacheOnPrefThread; | |
47 using HttpServerPropertiesManager::ScheduleUpdatePrefsOnNetworkThread; | |
48 | |
49 // Post tasks without a delay during tests. | |
50 virtual void StartPrefsUpdateTimerOnNetworkThread( | |
51 base::TimeDelta delay) override { | |
52 HttpServerPropertiesManager::StartPrefsUpdateTimerOnNetworkThread( | |
53 base::TimeDelta()); | |
54 } | |
55 | |
56 void UpdateCacheFromPrefsOnUIConcrete() { | |
57 HttpServerPropertiesManager::UpdateCacheFromPrefsOnPrefThread(); | |
58 } | |
59 | |
60 // Post tasks without a delay during tests. | |
61 virtual void StartCacheUpdateTimerOnPrefThread( | |
62 base::TimeDelta delay) override { | |
63 HttpServerPropertiesManager::StartCacheUpdateTimerOnPrefThread( | |
64 base::TimeDelta()); | |
65 } | |
66 | |
67 void UpdatePrefsFromCacheOnNetworkThreadConcrete( | |
68 const base::Closure& callback) { | |
69 HttpServerPropertiesManager::UpdatePrefsFromCacheOnNetworkThread(callback); | |
70 } | |
71 | |
72 MOCK_METHOD0(UpdateCacheFromPrefsOnPrefThread, void()); | |
73 MOCK_METHOD1(UpdatePrefsFromCacheOnNetworkThread, void(const base::Closure&)); | |
74 MOCK_METHOD6(UpdateCacheFromPrefsOnNetworkThread, | |
75 void(std::vector<std::string>* spdy_servers, | |
76 SpdySettingsMap* spdy_settings_map, | |
77 AlternateProtocolMap* alternate_protocol_map, | |
78 IPAddressNumber* last_quic_address, | |
79 ServerNetworkStatsMap* server_network_stats_map, | |
80 bool detected_corrupted_prefs)); | |
81 MOCK_METHOD5(UpdatePrefsOnPref, | |
82 void(base::ListValue* spdy_server_list, | |
83 SpdySettingsMap* spdy_settings_map, | |
84 AlternateProtocolMap* alternate_protocol_map, | |
85 IPAddressNumber* last_quic_address, | |
86 ServerNetworkStatsMap* server_network_stats_map)); | |
87 | |
88 private: | |
89 DISALLOW_COPY_AND_ASSIGN(TestingHttpServerPropertiesManager); | |
90 }; | |
91 | |
92 class HttpServerPropertiesManagerTest : public testing::Test { | |
93 protected: | |
94 HttpServerPropertiesManagerTest() {} | |
95 | |
96 void SetUp() override { | |
97 pref_service_.registry()->RegisterDictionaryPref(kTestHttpServerProperties); | |
98 http_server_props_manager_.reset( | |
99 new StrictMock<TestingHttpServerPropertiesManager>( | |
100 &pref_service_, | |
101 kTestHttpServerProperties, | |
102 base::MessageLoop::current()->message_loop_proxy())); | |
103 ExpectCacheUpdate(); | |
104 base::RunLoop().RunUntilIdle(); | |
105 } | |
106 | |
107 void TearDown() override { | |
108 if (http_server_props_manager_.get()) | |
109 http_server_props_manager_->ShutdownOnPrefThread(); | |
110 base::RunLoop().RunUntilIdle(); | |
111 http_server_props_manager_.reset(); | |
112 } | |
113 | |
114 void ExpectCacheUpdate() { | |
115 EXPECT_CALL(*http_server_props_manager_, UpdateCacheFromPrefsOnPrefThread()) | |
116 .WillOnce(Invoke(http_server_props_manager_.get(), | |
117 &TestingHttpServerPropertiesManager:: | |
118 UpdateCacheFromPrefsOnUIConcrete)); | |
119 } | |
120 | |
121 void ExpectPrefsUpdate() { | |
122 EXPECT_CALL(*http_server_props_manager_, | |
123 UpdatePrefsFromCacheOnNetworkThread(_)) | |
124 .WillOnce(Invoke(http_server_props_manager_.get(), | |
125 &TestingHttpServerPropertiesManager:: | |
126 UpdatePrefsFromCacheOnNetworkThreadConcrete)); | |
127 } | |
128 | |
129 void ExpectPrefsUpdateRepeatedly() { | |
130 EXPECT_CALL(*http_server_props_manager_, | |
131 UpdatePrefsFromCacheOnNetworkThread(_)) | |
132 .WillRepeatedly( | |
133 Invoke(http_server_props_manager_.get(), | |
134 &TestingHttpServerPropertiesManager:: | |
135 UpdatePrefsFromCacheOnNetworkThreadConcrete)); | |
136 } | |
137 | |
138 bool HasAlternateProtocol(const HostPortPair& server) { | |
139 const AlternateProtocolInfo alternate = | |
140 http_server_props_manager_->GetAlternateProtocol(server); | |
141 return alternate.protocol != UNINITIALIZED_ALTERNATE_PROTOCOL; | |
142 } | |
143 | |
144 //base::RunLoop loop_; | |
145 TestingPrefServiceSimple pref_service_; | |
146 scoped_ptr<TestingHttpServerPropertiesManager> http_server_props_manager_; | |
147 | |
148 private: | |
149 DISALLOW_COPY_AND_ASSIGN(HttpServerPropertiesManagerTest); | |
150 }; | |
151 | |
152 TEST_F(HttpServerPropertiesManagerTest, | |
153 SingleUpdateForTwoSpdyServerPrefChanges) { | |
154 ExpectCacheUpdate(); | |
155 | |
156 // Set up the prefs for www.google.com:80 and mail.google.com:80 and then set | |
157 // it twice. Only expect a single cache update. | |
158 | |
159 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; | |
160 HostPortPair google_server("www.google.com", 80); | |
161 HostPortPair mail_server("mail.google.com", 80); | |
162 | |
163 // Set supports_spdy for www.google.com:80. | |
164 server_pref_dict->SetBoolean("supports_spdy", true); | |
165 | |
166 // Set up alternate_protocol for www.google.com:80. | |
167 base::DictionaryValue* alternate_protocol = new base::DictionaryValue; | |
168 alternate_protocol->SetInteger("port", 443); | |
169 alternate_protocol->SetString("protocol_str", "npn-spdy/3"); | |
170 server_pref_dict->SetWithoutPathExpansion("alternate_protocol", | |
171 alternate_protocol); | |
172 | |
173 // Set up ServerNetworkStats for www.google.com:80. | |
174 base::DictionaryValue* stats = new base::DictionaryValue; | |
175 stats->SetInteger("srtt", 10); | |
176 server_pref_dict->SetWithoutPathExpansion("network_stats", stats); | |
177 | |
178 // Set the server preference for www.google.com:80. | |
179 base::DictionaryValue* servers_dict = new base::DictionaryValue; | |
180 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict); | |
181 | |
182 // Set the preference for mail.google.com server. | |
183 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue; | |
184 | |
185 // Set supports_spdy for mail.google.com:80 | |
186 server_pref_dict1->SetBoolean("supports_spdy", true); | |
187 | |
188 // Set up alternate_protocol for mail.google.com:80 | |
189 base::DictionaryValue* alternate_protocol1 = new base::DictionaryValue; | |
190 alternate_protocol1->SetInteger("port", 444); | |
191 alternate_protocol1->SetString("protocol_str", "npn-spdy/3.1"); | |
192 | |
193 server_pref_dict1->SetWithoutPathExpansion("alternate_protocol", | |
194 alternate_protocol1); | |
195 | |
196 // Set up ServerNetworkStats for mail.google.com:80. | |
197 base::DictionaryValue* stats1 = new base::DictionaryValue; | |
198 stats1->SetInteger("srtt", 20); | |
199 server_pref_dict1->SetWithoutPathExpansion("network_stats", stats1); | |
200 // Set the server preference for mail.google.com:80. | |
201 servers_dict->SetWithoutPathExpansion("mail.google.com:80", | |
202 server_pref_dict1); | |
203 | |
204 base::DictionaryValue* http_server_properties_dict = | |
205 new base::DictionaryValue; | |
206 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1); | |
207 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict); | |
208 base::DictionaryValue* supports_quic = new base::DictionaryValue; | |
209 supports_quic->SetBoolean("used_quic", true); | |
210 supports_quic->SetString("address", "127.0.0.1"); | |
211 http_server_properties_dict->SetWithoutPathExpansion("supports_quic", | |
212 supports_quic); | |
213 | |
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); | |
221 | |
222 base::RunLoop().RunUntilIdle(); | |
223 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
224 | |
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"))); | |
231 | |
232 // Verify AlternateProtocol. | |
233 AlternateProtocolInfo port_alternate_protocol = | |
234 http_server_props_manager_->GetAlternateProtocol(google_server); | |
235 EXPECT_EQ(443, port_alternate_protocol.port); | |
236 EXPECT_EQ(NPN_SPDY_3, port_alternate_protocol.protocol); | |
237 port_alternate_protocol = | |
238 http_server_props_manager_->GetAlternateProtocol(mail_server); | |
239 EXPECT_EQ(444, port_alternate_protocol.port); | |
240 EXPECT_EQ(NPN_SPDY_3_1, port_alternate_protocol.protocol); | |
241 | |
242 // Verify SupportsQuic. | |
243 IPAddressNumber last_address; | |
244 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&last_address)); | |
245 EXPECT_EQ("127.0.0.1", IPAddressToString(last_address)); | |
246 | |
247 // Verify ServerNetworkStats. | |
248 const ServerNetworkStats* stats2 = | |
249 http_server_props_manager_->GetServerNetworkStats(google_server); | |
250 EXPECT_EQ(10, stats2->srtt.ToInternalValue()); | |
251 const ServerNetworkStats* stats3 = | |
252 http_server_props_manager_->GetServerNetworkStats(mail_server); | |
253 EXPECT_EQ(20, stats3->srtt.ToInternalValue()); | |
254 } | |
255 | |
256 TEST_F(HttpServerPropertiesManagerTest, BadCachedHostPortPair) { | |
257 ExpectCacheUpdate(); | |
258 // The prefs are automaticalls updated in the case corruption is detected. | |
259 ExpectPrefsUpdate(); | |
260 | |
261 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; | |
262 | |
263 // Set supports_spdy for www.google.com:65536. | |
264 server_pref_dict->SetBoolean("supports_spdy", true); | |
265 | |
266 // Set up alternate_protocol for www.google.com:65536. | |
267 base::DictionaryValue* alternate_protocol = new base::DictionaryValue; | |
268 alternate_protocol->SetInteger("port", 80); | |
269 alternate_protocol->SetString("protocol_str", "npn-spdy/3"); | |
270 server_pref_dict->SetWithoutPathExpansion("alternate_protocol", | |
271 alternate_protocol); | |
272 | |
273 // Set up ServerNetworkStats for www.google.com:65536. | |
274 base::DictionaryValue* stats = new base::DictionaryValue; | |
275 stats->SetInteger("srtt", 10); | |
276 server_pref_dict->SetWithoutPathExpansion("network_stats", stats); | |
277 | |
278 // Set the server preference for www.google.com:65536. | |
279 base::DictionaryValue* servers_dict = new base::DictionaryValue; | |
280 servers_dict->SetWithoutPathExpansion("www.google.com:65536", | |
281 server_pref_dict); | |
282 | |
283 base::DictionaryValue* http_server_properties_dict = | |
284 new base::DictionaryValue; | |
285 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1); | |
286 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict); | |
287 | |
288 // Set up the pref. | |
289 pref_service_.SetManagedPref(kTestHttpServerProperties, | |
290 http_server_properties_dict); | |
291 | |
292 base::RunLoop().RunUntilIdle(); | |
293 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
294 | |
295 // Verify that nothing is set. | |
296 EXPECT_FALSE(http_server_props_manager_->SupportsRequestPriority( | |
297 HostPortPair::FromString("www.google.com:65536"))); | |
298 EXPECT_FALSE( | |
299 HasAlternateProtocol(HostPortPair::FromString("www.google.com:65536"))); | |
300 const ServerNetworkStats* stats1 = | |
301 http_server_props_manager_->GetServerNetworkStats( | |
302 HostPortPair::FromString("www.google.com:65536")); | |
303 EXPECT_EQ(NULL, stats1); | |
304 } | |
305 | |
306 TEST_F(HttpServerPropertiesManagerTest, BadCachedAltProtocolPort) { | |
307 ExpectCacheUpdate(); | |
308 // The prefs are automaticalls updated in the case corruption is detected. | |
309 ExpectPrefsUpdate(); | |
310 | |
311 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; | |
312 | |
313 // Set supports_spdy for www.google.com:80. | |
314 server_pref_dict->SetBoolean("supports_spdy", true); | |
315 | |
316 // Set up alternate_protocol for www.google.com:80. | |
317 base::DictionaryValue* alternate_protocol = new base::DictionaryValue; | |
318 alternate_protocol->SetInteger("port", 65536); | |
319 alternate_protocol->SetString("protocol_str", "npn-spdy/3"); | |
320 server_pref_dict->SetWithoutPathExpansion("alternate_protocol", | |
321 alternate_protocol); | |
322 | |
323 // Set the server preference for www.google.com:80. | |
324 base::DictionaryValue* servers_dict = new base::DictionaryValue; | |
325 servers_dict->SetWithoutPathExpansion("www.google.com:80", server_pref_dict); | |
326 | |
327 base::DictionaryValue* http_server_properties_dict = | |
328 new base::DictionaryValue; | |
329 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1); | |
330 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict); | |
331 | |
332 // Set up the pref. | |
333 pref_service_.SetManagedPref(kTestHttpServerProperties, | |
334 http_server_properties_dict); | |
335 | |
336 base::RunLoop().RunUntilIdle(); | |
337 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
338 | |
339 // Verify AlternateProtocol is not set. | |
340 EXPECT_FALSE( | |
341 HasAlternateProtocol(HostPortPair::FromString("www.google.com:80"))); | |
342 } | |
343 | |
344 TEST_F(HttpServerPropertiesManagerTest, SupportsSpdy) { | |
345 ExpectPrefsUpdate(); | |
346 | |
347 // Post an update task to the network thread. SetSupportsSpdy calls | |
348 // ScheduleUpdatePrefsOnNetworkThread. | |
349 | |
350 // Add mail.google.com:443 as a supporting spdy server. | |
351 HostPortPair spdy_server_mail("mail.google.com", 443); | |
352 EXPECT_FALSE( | |
353 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail)); | |
354 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true); | |
355 | |
356 // Run the task. | |
357 base::RunLoop().RunUntilIdle(); | |
358 | |
359 EXPECT_TRUE( | |
360 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail)); | |
361 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
362 } | |
363 | |
364 TEST_F(HttpServerPropertiesManagerTest, SetSpdySetting) { | |
365 ExpectPrefsUpdate(); | |
366 | |
367 // Add SpdySetting for mail.google.com:443. | |
368 HostPortPair spdy_server_mail("mail.google.com", 443); | |
369 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; | |
370 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST; | |
371 const uint32 value1 = 31337; | |
372 http_server_props_manager_->SetSpdySetting( | |
373 spdy_server_mail, id1, flags1, value1); | |
374 | |
375 // Run the task. | |
376 base::RunLoop().RunUntilIdle(); | |
377 | |
378 const SettingsMap& settings_map1_ret = | |
379 http_server_props_manager_->GetSpdySettings(spdy_server_mail); | |
380 ASSERT_EQ(1U, settings_map1_ret.size()); | |
381 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); | |
382 EXPECT_TRUE(it1_ret != settings_map1_ret.end()); | |
383 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; | |
384 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); | |
385 EXPECT_EQ(value1, flags_and_value1_ret.second); | |
386 | |
387 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
388 } | |
389 | |
390 TEST_F(HttpServerPropertiesManagerTest, ClearSpdySetting) { | |
391 ExpectPrefsUpdateRepeatedly(); | |
392 | |
393 // Add SpdySetting for mail.google.com:443. | |
394 HostPortPair spdy_server_mail("mail.google.com", 443); | |
395 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; | |
396 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST; | |
397 const uint32 value1 = 31337; | |
398 http_server_props_manager_->SetSpdySetting( | |
399 spdy_server_mail, id1, flags1, value1); | |
400 | |
401 // Run the task. | |
402 base::RunLoop().RunUntilIdle(); | |
403 | |
404 const SettingsMap& settings_map1_ret = | |
405 http_server_props_manager_->GetSpdySettings(spdy_server_mail); | |
406 ASSERT_EQ(1U, settings_map1_ret.size()); | |
407 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); | |
408 EXPECT_TRUE(it1_ret != settings_map1_ret.end()); | |
409 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; | |
410 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); | |
411 EXPECT_EQ(value1, flags_and_value1_ret.second); | |
412 | |
413 // Clear SpdySetting for mail.google.com:443. | |
414 http_server_props_manager_->ClearSpdySettings(spdy_server_mail); | |
415 | |
416 // Run the task. | |
417 base::RunLoop().RunUntilIdle(); | |
418 | |
419 // Verify that there are no entries in the settings map for | |
420 // mail.google.com:443. | |
421 const SettingsMap& settings_map2_ret = | |
422 http_server_props_manager_->GetSpdySettings(spdy_server_mail); | |
423 ASSERT_EQ(0U, settings_map2_ret.size()); | |
424 | |
425 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
426 } | |
427 | |
428 TEST_F(HttpServerPropertiesManagerTest, ClearAllSpdySetting) { | |
429 ExpectPrefsUpdateRepeatedly(); | |
430 | |
431 // Add SpdySetting for mail.google.com:443. | |
432 HostPortPair spdy_server_mail("mail.google.com", 443); | |
433 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; | |
434 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST; | |
435 const uint32 value1 = 31337; | |
436 http_server_props_manager_->SetSpdySetting( | |
437 spdy_server_mail, id1, flags1, value1); | |
438 | |
439 // Run the task. | |
440 base::RunLoop().RunUntilIdle(); | |
441 | |
442 const SettingsMap& settings_map1_ret = | |
443 http_server_props_manager_->GetSpdySettings(spdy_server_mail); | |
444 ASSERT_EQ(1U, settings_map1_ret.size()); | |
445 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); | |
446 EXPECT_TRUE(it1_ret != settings_map1_ret.end()); | |
447 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; | |
448 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); | |
449 EXPECT_EQ(value1, flags_and_value1_ret.second); | |
450 | |
451 // Clear All SpdySettings. | |
452 http_server_props_manager_->ClearAllSpdySettings(); | |
453 | |
454 // Run the task. | |
455 base::RunLoop().RunUntilIdle(); | |
456 | |
457 // Verify that there are no entries in the settings map. | |
458 const SpdySettingsMap& spdy_settings_map2_ret = | |
459 http_server_props_manager_->spdy_settings_map(); | |
460 ASSERT_EQ(0U, spdy_settings_map2_ret.size()); | |
461 | |
462 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
463 } | |
464 | |
465 TEST_F(HttpServerPropertiesManagerTest, GetAlternateProtocol) { | |
466 ExpectPrefsUpdate(); | |
467 | |
468 HostPortPair spdy_server_mail("mail.google.com", 80); | |
469 EXPECT_FALSE(HasAlternateProtocol(spdy_server_mail)); | |
470 http_server_props_manager_->SetAlternateProtocol(spdy_server_mail, 443, | |
471 NPN_SPDY_3, 1.0); | |
472 | |
473 // Run the task. | |
474 base::RunLoop().RunUntilIdle(); | |
475 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
476 | |
477 const AlternateProtocolInfo alternate_protocol = | |
478 http_server_props_manager_->GetAlternateProtocol(spdy_server_mail); | |
479 EXPECT_EQ(443, alternate_protocol.port); | |
480 EXPECT_EQ(NPN_SPDY_3, alternate_protocol.protocol); | |
481 EXPECT_EQ(1.0, alternate_protocol.probability); | |
482 } | |
483 | |
484 TEST_F(HttpServerPropertiesManagerTest, SupportsQuic) { | |
485 ExpectPrefsUpdate(); | |
486 | |
487 IPAddressNumber address; | |
488 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address)); | |
489 | |
490 IPAddressNumber actual_address; | |
491 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address)); | |
492 http_server_props_manager_->SetSupportsQuic(true, actual_address); | |
493 | |
494 // Run the task. | |
495 base::RunLoop().RunUntilIdle(); | |
496 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
497 | |
498 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address)); | |
499 EXPECT_EQ(actual_address, address); | |
500 } | |
501 | |
502 TEST_F(HttpServerPropertiesManagerTest, ServerNetworkStats) { | |
503 ExpectPrefsUpdate(); | |
504 | |
505 HostPortPair mail_server("mail.google.com", 80); | |
506 const ServerNetworkStats* stats = | |
507 http_server_props_manager_->GetServerNetworkStats(mail_server); | |
508 EXPECT_EQ(NULL, stats); | |
509 ServerNetworkStats stats1; | |
510 stats1.srtt = base::TimeDelta::FromMicroseconds(10); | |
511 http_server_props_manager_->SetServerNetworkStats(mail_server, stats1); | |
512 | |
513 // Run the task. | |
514 base::RunLoop().RunUntilIdle(); | |
515 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
516 | |
517 const ServerNetworkStats* stats2 = | |
518 http_server_props_manager_->GetServerNetworkStats(mail_server); | |
519 EXPECT_EQ(10, stats2->srtt.ToInternalValue()); | |
520 } | |
521 | |
522 TEST_F(HttpServerPropertiesManagerTest, Clear) { | |
523 ExpectPrefsUpdate(); | |
524 | |
525 HostPortPair spdy_server_mail("mail.google.com", 443); | |
526 http_server_props_manager_->SetSupportsSpdy(spdy_server_mail, true); | |
527 http_server_props_manager_->SetAlternateProtocol(spdy_server_mail, 443, | |
528 NPN_SPDY_3, 1.0); | |
529 IPAddressNumber actual_address; | |
530 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address)); | |
531 http_server_props_manager_->SetSupportsQuic(true, actual_address); | |
532 ServerNetworkStats stats; | |
533 stats.srtt = base::TimeDelta::FromMicroseconds(10); | |
534 http_server_props_manager_->SetServerNetworkStats(spdy_server_mail, stats); | |
535 | |
536 const SpdySettingsIds id1 = SETTINGS_UPLOAD_BANDWIDTH; | |
537 const SpdySettingsFlags flags1 = SETTINGS_FLAG_PLEASE_PERSIST; | |
538 const uint32 value1 = 31337; | |
539 http_server_props_manager_->SetSpdySetting( | |
540 spdy_server_mail, id1, flags1, value1); | |
541 | |
542 // Run the task. | |
543 base::RunLoop().RunUntilIdle(); | |
544 | |
545 EXPECT_TRUE( | |
546 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail)); | |
547 EXPECT_TRUE(HasAlternateProtocol(spdy_server_mail)); | |
548 IPAddressNumber address; | |
549 EXPECT_TRUE(http_server_props_manager_->GetSupportsQuic(&address)); | |
550 EXPECT_EQ(actual_address, address); | |
551 const ServerNetworkStats* stats1 = | |
552 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail); | |
553 EXPECT_EQ(10, stats1->srtt.ToInternalValue()); | |
554 | |
555 // Check SPDY settings values. | |
556 const SettingsMap& settings_map1_ret = | |
557 http_server_props_manager_->GetSpdySettings(spdy_server_mail); | |
558 ASSERT_EQ(1U, settings_map1_ret.size()); | |
559 SettingsMap::const_iterator it1_ret = settings_map1_ret.find(id1); | |
560 EXPECT_TRUE(it1_ret != settings_map1_ret.end()); | |
561 SettingsFlagsAndValue flags_and_value1_ret = it1_ret->second; | |
562 EXPECT_EQ(SETTINGS_FLAG_PERSISTED, flags_and_value1_ret.first); | |
563 EXPECT_EQ(value1, flags_and_value1_ret.second); | |
564 | |
565 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
566 | |
567 ExpectPrefsUpdate(); | |
568 | |
569 // Clear http server data, time out if we do not get a completion callback. | |
570 http_server_props_manager_->Clear(base::MessageLoop::QuitClosure()); | |
571 base::RunLoop().Run(); | |
572 | |
573 EXPECT_FALSE( | |
574 http_server_props_manager_->SupportsRequestPriority(spdy_server_mail)); | |
575 EXPECT_FALSE(HasAlternateProtocol(spdy_server_mail)); | |
576 EXPECT_FALSE(http_server_props_manager_->GetSupportsQuic(&address)); | |
577 const ServerNetworkStats* stats2 = | |
578 http_server_props_manager_->GetServerNetworkStats(spdy_server_mail); | |
579 EXPECT_EQ(NULL, stats2); | |
580 | |
581 const SettingsMap& settings_map2_ret = | |
582 http_server_props_manager_->GetSpdySettings(spdy_server_mail); | |
583 EXPECT_EQ(0U, settings_map2_ret.size()); | |
584 | |
585 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
586 } | |
587 | |
588 // https://crbug.com/444956: Add 200 alternate_protocol servers followed by | |
589 // supports_quic and verify we have read supports_quic from prefs. | |
590 TEST_F(HttpServerPropertiesManagerTest, BadSupportsQuic) { | |
591 ExpectCacheUpdate(); | |
592 | |
593 base::DictionaryValue* servers_dict = new base::DictionaryValue; | |
594 | |
595 for (int i = 0; i < 200; ++i) { | |
596 // Set up alternate_protocol for www.google.com:i. | |
597 base::DictionaryValue* alternate_protocol = new base::DictionaryValue; | |
598 alternate_protocol->SetInteger("port", i); | |
599 alternate_protocol->SetString("protocol_str", "npn-spdy/3"); | |
600 base::DictionaryValue* server_pref_dict = new base::DictionaryValue; | |
601 server_pref_dict->SetWithoutPathExpansion("alternate_protocol", | |
602 alternate_protocol); | |
603 servers_dict->SetWithoutPathExpansion(StringPrintf("www.google.com:%d", i), | |
604 server_pref_dict); | |
605 } | |
606 | |
607 // Set the preference for mail.google.com server. | |
608 base::DictionaryValue* server_pref_dict1 = new base::DictionaryValue; | |
609 | |
610 // Set the server preference for mail.google.com:80. | |
611 servers_dict->SetWithoutPathExpansion("mail.google.com:80", | |
612 server_pref_dict1); | |
613 | |
614 base::DictionaryValue* http_server_properties_dict = | |
615 new base::DictionaryValue; | |
616 HttpServerPropertiesManager::SetVersion(http_server_properties_dict, -1); | |
617 http_server_properties_dict->SetWithoutPathExpansion("servers", servers_dict); | |
618 | |
619 // Set up SupportsQuic for 127.0.0.1 | |
620 base::DictionaryValue* supports_quic = new base::DictionaryValue; | |
621 supports_quic->SetBoolean("used_quic", true); | |
622 supports_quic->SetString("address", "127.0.0.1"); | |
623 http_server_properties_dict->SetWithoutPathExpansion("supports_quic", | |
624 supports_quic); | |
625 | |
626 // Set up the pref. | |
627 pref_service_.SetManagedPref(kTestHttpServerProperties, | |
628 http_server_properties_dict); | |
629 | |
630 base::RunLoop().RunUntilIdle(); | |
631 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
632 | |
633 // Verify AlternateProtocol. | |
634 for (int i = 0; i < 200; ++i) { | |
635 std::string server = StringPrintf("www.google.com:%d", i); | |
636 AlternateProtocolInfo port_alternate_protocol = | |
637 http_server_props_manager_->GetAlternateProtocol( | |
638 HostPortPair::FromString(server)); | |
639 EXPECT_EQ(i, port_alternate_protocol.port); | |
640 EXPECT_EQ(NPN_SPDY_3, port_alternate_protocol.protocol); | |
641 } | |
642 | |
643 // Verify SupportsQuic. | |
644 IPAddressNumber address; | |
645 ASSERT_TRUE(http_server_props_manager_->GetSupportsQuic(&address)); | |
646 EXPECT_EQ("127.0.0.1", IPAddressToString(address)); | |
647 } | |
648 | |
649 TEST_F(HttpServerPropertiesManagerTest, UpdateCacheWithPrefs) { | |
650 const HostPortPair server_www("www.google.com", 80); | |
651 const HostPortPair server_mail("mail.google.com", 80); | |
652 | |
653 // Set alternate protocol. | |
654 http_server_props_manager_->SetAlternateProtocol(server_www, 443, NPN_SPDY_3, | |
655 1.0); | |
656 http_server_props_manager_->SetAlternateProtocol(server_mail, 444, | |
657 NPN_SPDY_3_1, 0.2); | |
658 | |
659 // Set ServerNetworkStats. | |
660 ServerNetworkStats stats; | |
661 stats.srtt = base::TimeDelta::FromInternalValue(42); | |
662 http_server_props_manager_->SetServerNetworkStats(server_mail, stats); | |
663 | |
664 // Set SupportsQuic. | |
665 IPAddressNumber actual_address; | |
666 CHECK(ParseIPLiteralToNumber("127.0.0.1", &actual_address)); | |
667 http_server_props_manager_->SetSupportsQuic(true, actual_address); | |
668 | |
669 // Update cache. | |
670 ExpectPrefsUpdate(); | |
671 ExpectCacheUpdate(); | |
672 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread(); | |
673 base::RunLoop().RunUntilIdle(); | |
674 | |
675 // Verify preferences. | |
676 const char expected_json[] = "{" | |
677 "\"servers\":{" | |
678 "\"mail.google.com:80\":{" | |
679 "\"alternate_protocol\":{" | |
680 "\"port\":444,\"probability\":0.2,\"protocol_str\":\"npn-spdy/3.1\"" | |
681 "}," | |
682 "\"network_stats\":{" | |
683 "\"srtt\":42" | |
684 "}" | |
685 "}," | |
686 "\"www.google.com:80\":{" | |
687 "\"alternate_protocol\":{" | |
688 "\"port\":443,\"probability\":1.0,\"protocol_str\":\"npn-spdy/3\"" | |
689 "}" | |
690 "}" | |
691 "}," | |
692 "\"supports_quic\":{" | |
693 "\"address\":\"127.0.0.1\",\"used_quic\":true" | |
694 "}," | |
695 "\"version\":3" | |
696 "}"; | |
697 | |
698 const base::Value* http_server_properties = | |
699 pref_service_.GetUserPref(kTestHttpServerProperties); | |
700 ASSERT_NE(nullptr, http_server_properties); | |
701 std::string preferences_json; | |
702 EXPECT_TRUE( | |
703 base::JSONWriter::Write(http_server_properties, &preferences_json)); | |
704 EXPECT_EQ(expected_json, preferences_json); | |
705 } | |
706 | |
707 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache0) { | |
708 // Post an update task to the UI thread. | |
709 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread(); | |
710 // Shutdown comes before the task is executed. | |
711 http_server_props_manager_->ShutdownOnPrefThread(); | |
712 http_server_props_manager_.reset(); | |
713 // Run the task after shutdown and deletion. | |
714 base::RunLoop().RunUntilIdle(); | |
715 } | |
716 | |
717 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache1) { | |
718 // Post an update task. | |
719 http_server_props_manager_->ScheduleUpdateCacheOnPrefThread(); | |
720 // Shutdown comes before the task is executed. | |
721 http_server_props_manager_->ShutdownOnPrefThread(); | |
722 // Run the task after shutdown, but before deletion. | |
723 base::RunLoop().RunUntilIdle(); | |
724 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
725 http_server_props_manager_.reset(); | |
726 base::RunLoop().RunUntilIdle(); | |
727 } | |
728 | |
729 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdateCache2) { | |
730 http_server_props_manager_->UpdateCacheFromPrefsOnUIConcrete(); | |
731 // Shutdown comes before the task is executed. | |
732 http_server_props_manager_->ShutdownOnPrefThread(); | |
733 // Run the task after shutdown, but before deletion. | |
734 base::RunLoop().RunUntilIdle(); | |
735 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
736 http_server_props_manager_.reset(); | |
737 base::RunLoop().RunUntilIdle(); | |
738 } | |
739 | |
740 // | |
741 // Tests for shutdown when updating prefs. | |
742 // | |
743 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs0) { | |
744 // Post an update task to the IO thread. | |
745 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread(); | |
746 // Shutdown comes before the task is executed. | |
747 http_server_props_manager_->ShutdownOnPrefThread(); | |
748 http_server_props_manager_.reset(); | |
749 // Run the task after shutdown and deletion. | |
750 base::RunLoop().RunUntilIdle(); | |
751 } | |
752 | |
753 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs1) { | |
754 ExpectPrefsUpdate(); | |
755 // Post an update task. | |
756 http_server_props_manager_->ScheduleUpdatePrefsOnNetworkThread(); | |
757 // Shutdown comes before the task is executed. | |
758 http_server_props_manager_->ShutdownOnPrefThread(); | |
759 // Run the task after shutdown, but before deletion. | |
760 base::RunLoop().RunUntilIdle(); | |
761 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
762 http_server_props_manager_.reset(); | |
763 base::RunLoop().RunUntilIdle(); | |
764 } | |
765 | |
766 TEST_F(HttpServerPropertiesManagerTest, ShutdownWithPendingUpdatePrefs2) { | |
767 // This posts a task to the UI thread. | |
768 http_server_props_manager_->UpdatePrefsFromCacheOnNetworkThreadConcrete( | |
769 base::Closure()); | |
770 // Shutdown comes before the task is executed. | |
771 http_server_props_manager_->ShutdownOnPrefThread(); | |
772 // Run the task after shutdown, but before deletion. | |
773 base::RunLoop().RunUntilIdle(); | |
774 Mock::VerifyAndClearExpectations(http_server_props_manager_.get()); | |
775 http_server_props_manager_.reset(); | |
776 base::RunLoop().RunUntilIdle(); | |
777 } | |
778 | |
779 } // namespace | |
780 | |
781 } // namespace net | |
OLD | NEW |