OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 | 5 |
6 #include "base/stringprintf.h" | 6 #include "base/stringprintf.h" |
7 #include "base/test/test_simple_task_runner.h" | 7 #include "base/test/test_simple_task_runner.h" |
8 #include "base/thread_task_runner_handle.h" | 8 #include "base/thread_task_runner_handle.h" |
9 #include "base/time.h" | 9 #include "base/time.h" |
10 #include "chrome/browser/safe_browsing/protocol_manager.h" | 10 #include "chrome/browser/safe_browsing/protocol_manager.h" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 | 53 |
54 void ValidateUpdateFetcherRequest(const net::TestURLFetcher* url_fetcher) { | 54 void ValidateUpdateFetcherRequest(const net::TestURLFetcher* url_fetcher) { |
55 ASSERT_TRUE(url_fetcher); | 55 ASSERT_TRUE(url_fetcher); |
56 EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags()); | 56 EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags()); |
57 EXPECT_EQ("goog-phish-shavar;\ngoog-malware-shavar;\n", | 57 EXPECT_EQ("goog-phish-shavar;\ngoog-malware-shavar;\n", |
58 url_fetcher->upload_data()); | 58 url_fetcher->upload_data()); |
59 EXPECT_EQ(GURL("https://prefix.com/foo/downloads?client=unittest&appver=1.0" | 59 EXPECT_EQ(GURL("https://prefix.com/foo/downloads?client=unittest&appver=1.0" |
60 "&pver=2.2" + key_param_), | 60 "&pver=2.2" + key_param_), |
61 url_fetcher->GetOriginalURL()); | 61 url_fetcher->GetOriginalURL()); |
62 } | 62 } |
| 63 |
| 64 void ValidateRedirectFetcherRequest(const net::TestURLFetcher* url_fetcher, |
| 65 const std::string& expected_url) { |
| 66 ASSERT_TRUE(url_fetcher); |
| 67 EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags()); |
| 68 EXPECT_EQ("", url_fetcher->upload_data()); |
| 69 EXPECT_EQ(GURL(expected_url), url_fetcher->GetOriginalURL()); |
| 70 } |
63 }; | 71 }; |
64 | 72 |
65 // Ensure that we respect section 5 of the SafeBrowsing protocol specification. | 73 // Ensure that we respect section 5 of the SafeBrowsing protocol specification. |
66 TEST_F(SafeBrowsingProtocolManagerTest, TestBackOffTimes) { | 74 TEST_F(SafeBrowsingProtocolManagerTest, TestBackOffTimes) { |
67 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL)); | 75 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL)); |
68 | 76 |
69 pm->next_update_interval_ = TimeDelta::FromSeconds(1800); | 77 pm->next_update_interval_ = TimeDelta::FromSeconds(1800); |
70 ASSERT_TRUE(pm->back_off_fuzz_ >= 0.0 && pm->back_off_fuzz_ <= 1.0); | 78 ASSERT_TRUE(pm->back_off_fuzz_ >= 0.0 && pm->back_off_fuzz_ <= 1.0); |
71 | 79 |
72 TimeDelta next; | 80 TimeDelta next; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 | 253 |
246 class MockProtocolDelegate : public SafeBrowsingProtocolManagerDelegate { | 254 class MockProtocolDelegate : public SafeBrowsingProtocolManagerDelegate { |
247 public: | 255 public: |
248 MockProtocolDelegate() {} | 256 MockProtocolDelegate() {} |
249 virtual ~MockProtocolDelegate() {} | 257 virtual ~MockProtocolDelegate() {} |
250 | 258 |
251 MOCK_METHOD0(UpdateStarted, void()); | 259 MOCK_METHOD0(UpdateStarted, void()); |
252 MOCK_METHOD1(UpdateFinished, void(bool)); | 260 MOCK_METHOD1(UpdateFinished, void(bool)); |
253 MOCK_METHOD0(ResetDatabase, void()); | 261 MOCK_METHOD0(ResetDatabase, void()); |
254 MOCK_METHOD1(GetChunks, void(GetChunksCallback)); | 262 MOCK_METHOD1(GetChunks, void(GetChunksCallback)); |
255 MOCK_METHOD2(AddChunks, void(const std::string&, SBChunkList*)); | 263 MOCK_METHOD3(AddChunks, void(const std::string&, SBChunkList*, |
| 264 AddChunksCallback)); |
256 MOCK_METHOD1(DeleteChunks, void(std::vector<SBChunkDelete>*)); | 265 MOCK_METHOD1(DeleteChunks, void(std::vector<SBChunkDelete>*)); |
257 }; | 266 }; |
258 | 267 |
259 // |InvokeGetChunksCallback| is required because GMock's InvokeArgument action | 268 // |InvokeGetChunksCallback| is required because GMock's InvokeArgument action |
260 // expects to use operator(), and a Callback only provides Run(). | 269 // expects to use operator(), and a Callback only provides Run(). |
261 // TODO(cbentzel): Use ACTION or ACTION_TEMPLATE instead? | 270 // TODO(cbentzel): Use ACTION or ACTION_TEMPLATE instead? |
262 void InvokeGetChunksCallback( | 271 void InvokeGetChunksCallback( |
263 const std::vector<SBListChunkRanges>& ranges, | 272 const std::vector<SBListChunkRanges>& ranges, |
264 bool database_error, | 273 bool database_error, |
265 SafeBrowsingProtocolManagerDelegate::GetChunksCallback callback) { | 274 SafeBrowsingProtocolManagerDelegate::GetChunksCallback callback) { |
266 callback.Run(ranges, database_error); | 275 callback.Run(ranges, database_error); |
267 } | 276 } |
268 | 277 |
| 278 // |HandleAddChunks| deletes the chunks and asynchronously invokes |
| 279 // |callback| since SafeBrowsingProtocolManager is not re-entrant at the time |
| 280 // this is called. This guarantee is part of the |
| 281 // SafeBrowsingProtocolManagerDelegate contract. |
| 282 void HandleAddChunks( |
| 283 const std::string& unused_list, |
| 284 SBChunkList* chunks, |
| 285 SafeBrowsingProtocolManagerDelegate::AddChunksCallback callback) { |
| 286 delete chunks; |
| 287 scoped_refptr<base::SingleThreadTaskRunner> task_runner( |
| 288 base::ThreadTaskRunnerHandle::Get()); |
| 289 if (!task_runner) |
| 290 return; |
| 291 task_runner->PostTask(FROM_HERE, callback); |
| 292 } |
| 293 |
269 } // namespace | 294 } // namespace |
270 | 295 |
271 // Tests that the Update protocol will be skipped if there are problems | 296 // Tests that the Update protocol will be skipped if there are problems |
272 // accessing the database. | 297 // accessing the database. |
273 TEST_F(SafeBrowsingProtocolManagerTest, ProblemAccessingDatabase) { | 298 TEST_F(SafeBrowsingProtocolManagerTest, ProblemAccessingDatabase) { |
274 scoped_refptr<base::TestSimpleTaskRunner> runner( | 299 scoped_refptr<base::TestSimpleTaskRunner> runner( |
275 new base::TestSimpleTaskRunner()); | 300 new base::TestSimpleTaskRunner()); |
276 base::ThreadTaskRunnerHandle runner_handler(runner); | 301 base::ThreadTaskRunnerHandle runner_handler(runner); |
277 | 302 |
278 testing::StrictMock<MockProtocolDelegate> test_delegate; | 303 testing::StrictMock<MockProtocolDelegate> test_delegate; |
279 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); | 304 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
280 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( | 305 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
281 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, | 306 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
282 std::vector<SBListChunkRanges>(), | 307 std::vector<SBListChunkRanges>(), |
283 true))); | 308 true))); |
284 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); | 309 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); |
285 | 310 |
286 scoped_ptr<SafeBrowsingProtocolManager> pm( | 311 scoped_ptr<SafeBrowsingProtocolManager> pm( |
287 CreateProtocolManager(&test_delegate)); | 312 CreateProtocolManager(&test_delegate)); |
288 | 313 |
289 pm->ForceScheduleNextUpdate(TimeDelta()); | 314 pm->ForceScheduleNextUpdate(TimeDelta()); |
290 runner->RunPendingTasks(); | 315 runner->RunPendingTasks(); |
| 316 |
| 317 EXPECT_TRUE(pm->IsUpdateScheduled()); |
291 } | 318 } |
292 | 319 |
293 // Tests the contents of the POST body when there are contents in the | 320 // Tests the contents of the POST body when there are contents in the |
294 // local database. This is not exhaustive, as the actual list formatting | 321 // local database. This is not exhaustive, as the actual list formatting |
295 // is covered by SafeBrowsingProtocolManagerTest.TestChunkStrings. | 322 // is covered by SafeBrowsingProtocolManagerTest.TestChunkStrings. |
296 TEST_F(SafeBrowsingProtocolManagerTest, ExistingDatabase) { | 323 TEST_F(SafeBrowsingProtocolManagerTest, ExistingDatabase) { |
297 scoped_refptr<base::TestSimpleTaskRunner> runner( | 324 scoped_refptr<base::TestSimpleTaskRunner> runner( |
298 new base::TestSimpleTaskRunner()); | 325 new base::TestSimpleTaskRunner()); |
299 base::ThreadTaskRunnerHandle runner_handler(runner); | 326 base::ThreadTaskRunnerHandle runner_handler(runner); |
300 net::TestURLFetcherFactory url_fetcher_factory; | 327 net::TestURLFetcherFactory url_fetcher_factory; |
301 | 328 |
302 std::vector<SBListChunkRanges> ranges; | 329 std::vector<SBListChunkRanges> ranges; |
303 SBListChunkRanges range_phish(safe_browsing_util::kPhishingList); | 330 SBListChunkRanges range_phish(safe_browsing_util::kPhishingList); |
304 range_phish.adds = "adds_phish"; | 331 range_phish.adds = "adds_phish"; |
305 range_phish.subs = "subs_phish"; | 332 range_phish.subs = "subs_phish"; |
306 ranges.push_back(range_phish); | 333 ranges.push_back(range_phish); |
307 | 334 |
308 SBListChunkRanges range_unknown("unknown_list"); | 335 SBListChunkRanges range_unknown("unknown_list"); |
309 range_unknown.adds = "adds_unknown"; | 336 range_unknown.adds = "adds_unknown"; |
310 range_unknown.subs = "subs_unknown"; | 337 range_unknown.subs = "subs_unknown"; |
311 ranges.push_back(range_unknown); | 338 ranges.push_back(range_unknown); |
312 | 339 |
313 testing::StrictMock<MockProtocolDelegate> test_delegate; | 340 testing::StrictMock<MockProtocolDelegate> test_delegate; |
314 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); | 341 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
315 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( | 342 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
316 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, | 343 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
317 ranges, | 344 ranges, |
318 false))); | 345 false))); |
| 346 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1); |
319 | 347 |
320 scoped_ptr<SafeBrowsingProtocolManager> pm( | 348 scoped_ptr<SafeBrowsingProtocolManager> pm( |
321 CreateProtocolManager(&test_delegate)); | 349 CreateProtocolManager(&test_delegate)); |
322 | 350 |
323 // Kick off initialization. This returns chunks from the DB synchronously. | 351 // Kick off initialization. This returns chunks from the DB synchronously. |
324 pm->ForceScheduleNextUpdate(TimeDelta()); | 352 pm->ForceScheduleNextUpdate(TimeDelta()); |
325 runner->RunPendingTasks(); | 353 runner->RunPendingTasks(); |
326 | 354 |
327 // We should have an URLFetcher at this point in time. | 355 // We should have an URLFetcher at this point in time. |
328 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); | 356 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
329 ASSERT_TRUE(url_fetcher); | 357 ASSERT_TRUE(url_fetcher); |
330 EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags()); | 358 EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags()); |
331 EXPECT_EQ("goog-phish-shavar;a:adds_phish:s:subs_phish\n" | 359 EXPECT_EQ("goog-phish-shavar;a:adds_phish:s:subs_phish\n" |
332 "unknown_list;a:adds_unknown:s:subs_unknown\n" | 360 "unknown_list;a:adds_unknown:s:subs_unknown\n" |
333 "goog-malware-shavar;\n", | 361 "goog-malware-shavar;\n", |
334 url_fetcher->upload_data()); | 362 url_fetcher->upload_data()); |
335 EXPECT_EQ(GURL("https://prefix.com/foo/downloads?client=unittest&appver=1.0" | 363 EXPECT_EQ(GURL("https://prefix.com/foo/downloads?client=unittest&appver=1.0" |
336 "&pver=2.2" + key_param_), | 364 "&pver=2.2" + key_param_), |
337 url_fetcher->GetOriginalURL()); | 365 url_fetcher->GetOriginalURL()); |
338 } | |
339 | 366 |
340 // Tests what happens when there is a response with no chunks. | |
341 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseEmptyBody) { | |
342 scoped_refptr<base::TestSimpleTaskRunner> runner( | |
343 new base::TestSimpleTaskRunner()); | |
344 base::ThreadTaskRunnerHandle runner_handler(runner); | |
345 net::TestURLFetcherFactory url_fetcher_factory; | |
346 | |
347 testing::StrictMock<MockProtocolDelegate> test_delegate; | |
348 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); | |
349 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( | |
350 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, | |
351 std::vector<SBListChunkRanges>(), | |
352 false))); | |
353 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1); | |
354 | |
355 scoped_ptr<SafeBrowsingProtocolManager> pm( | |
356 CreateProtocolManager(&test_delegate)); | |
357 | |
358 // Kick off initialization. This returns chunks from the DB synchronously. | |
359 pm->ForceScheduleNextUpdate(TimeDelta()); | |
360 runner->RunPendingTasks(); | |
361 | |
362 // We should have an URLFetcher at this point in time. | |
363 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); | |
364 ValidateUpdateFetcherRequest(url_fetcher); | |
365 | |
366 // The update response is successful, but an empty body. | |
367 url_fetcher->set_status(net::URLRequestStatus()); | 367 url_fetcher->set_status(net::URLRequestStatus()); |
368 url_fetcher->set_response_code(200); | 368 url_fetcher->set_response_code(200); |
369 url_fetcher->SetResponseString(""); | 369 url_fetcher->SetResponseString(""); |
370 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | 370 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 371 |
| 372 EXPECT_TRUE(pm->IsUpdateScheduled()); |
371 } | 373 } |
372 | 374 |
373 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseBadBody) { | 375 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseBadBody) { |
374 scoped_refptr<base::TestSimpleTaskRunner> runner( | 376 scoped_refptr<base::TestSimpleTaskRunner> runner( |
375 new base::TestSimpleTaskRunner()); | 377 new base::TestSimpleTaskRunner()); |
376 base::ThreadTaskRunnerHandle runner_handler(runner); | 378 base::ThreadTaskRunnerHandle runner_handler(runner); |
377 net::TestURLFetcherFactory url_fetcher_factory; | 379 net::TestURLFetcherFactory url_fetcher_factory; |
378 | 380 |
379 testing::StrictMock<MockProtocolDelegate> test_delegate; | 381 testing::StrictMock<MockProtocolDelegate> test_delegate; |
380 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); | 382 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
(...skipping 12 matching lines...) Expand all Loading... |
393 | 395 |
394 // We should have an URLFetcher at this point in time. | 396 // We should have an URLFetcher at this point in time. |
395 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); | 397 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
396 ValidateUpdateFetcherRequest(url_fetcher); | 398 ValidateUpdateFetcherRequest(url_fetcher); |
397 | 399 |
398 // The update response is successful, but an invalid body. | 400 // The update response is successful, but an invalid body. |
399 url_fetcher->set_status(net::URLRequestStatus()); | 401 url_fetcher->set_status(net::URLRequestStatus()); |
400 url_fetcher->set_response_code(200); | 402 url_fetcher->set_response_code(200); |
401 url_fetcher->SetResponseString("THIS_IS_A_BAD_RESPONSE"); | 403 url_fetcher->SetResponseString("THIS_IS_A_BAD_RESPONSE"); |
402 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | 404 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 405 |
| 406 EXPECT_TRUE(pm->IsUpdateScheduled()); |
403 } | 407 } |
404 | 408 |
405 // Tests what happens when there is an error in the update response. | 409 // Tests what happens when there is an error in the update response. |
406 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpError) { | 410 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpError) { |
407 scoped_refptr<base::TestSimpleTaskRunner> runner( | 411 scoped_refptr<base::TestSimpleTaskRunner> runner( |
408 new base::TestSimpleTaskRunner()); | 412 new base::TestSimpleTaskRunner()); |
409 base::ThreadTaskRunnerHandle runner_handler(runner); | 413 base::ThreadTaskRunnerHandle runner_handler(runner); |
410 net::TestURLFetcherFactory url_fetcher_factory; | 414 net::TestURLFetcherFactory url_fetcher_factory; |
411 | 415 |
412 testing::StrictMock<MockProtocolDelegate> test_delegate; | 416 testing::StrictMock<MockProtocolDelegate> test_delegate; |
(...skipping 13 matching lines...) Expand all Loading... |
426 | 430 |
427 // We should have an URLFetcher at this point in time. | 431 // We should have an URLFetcher at this point in time. |
428 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); | 432 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
429 ValidateUpdateFetcherRequest(url_fetcher); | 433 ValidateUpdateFetcherRequest(url_fetcher); |
430 | 434 |
431 // Go ahead and respond to it. | 435 // Go ahead and respond to it. |
432 url_fetcher->set_status(net::URLRequestStatus()); | 436 url_fetcher->set_status(net::URLRequestStatus()); |
433 url_fetcher->set_response_code(404); | 437 url_fetcher->set_response_code(404); |
434 url_fetcher->SetResponseString(""); | 438 url_fetcher->SetResponseString(""); |
435 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | 439 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 440 |
| 441 EXPECT_TRUE(pm->IsUpdateScheduled()); |
436 } | 442 } |
437 | 443 |
438 // Tests what happens when there is an error with the connection. | 444 // Tests what happens when there is an error with the connection. |
439 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseConnectionError) { | 445 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseConnectionError) { |
440 scoped_refptr<base::TestSimpleTaskRunner> runner( | 446 scoped_refptr<base::TestSimpleTaskRunner> runner( |
441 new base::TestSimpleTaskRunner()); | 447 new base::TestSimpleTaskRunner()); |
442 base::ThreadTaskRunnerHandle runner_handler(runner); | 448 base::ThreadTaskRunnerHandle runner_handler(runner); |
443 net::TestURLFetcherFactory url_fetcher_factory; | 449 net::TestURLFetcherFactory url_fetcher_factory; |
444 | 450 |
445 testing::StrictMock<MockProtocolDelegate> test_delegate; | 451 testing::StrictMock<MockProtocolDelegate> test_delegate; |
(...skipping 12 matching lines...) Expand all Loading... |
458 runner->RunPendingTasks(); | 464 runner->RunPendingTasks(); |
459 | 465 |
460 // We should have an URLFetcher at this point in time. | 466 // We should have an URLFetcher at this point in time. |
461 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); | 467 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
462 ValidateUpdateFetcherRequest(url_fetcher); | 468 ValidateUpdateFetcherRequest(url_fetcher); |
463 | 469 |
464 // Go ahead and respond to it. | 470 // Go ahead and respond to it. |
465 url_fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 471 url_fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
466 net::ERR_CONNECTION_RESET)); | 472 net::ERR_CONNECTION_RESET)); |
467 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | 473 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 474 |
| 475 EXPECT_TRUE(pm->IsUpdateScheduled()); |
468 } | 476 } |
469 | 477 |
470 // Tests what happens when there is a timeout before an update response. | 478 // Tests what happens when there is a timeout before an update response. |
471 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseTimeout) { | 479 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseTimeout) { |
472 scoped_refptr<base::TestSimpleTaskRunner> runner( | 480 scoped_refptr<base::TestSimpleTaskRunner> runner( |
473 new base::TestSimpleTaskRunner()); | 481 new base::TestSimpleTaskRunner()); |
474 base::ThreadTaskRunnerHandle runner_handler(runner); | 482 base::ThreadTaskRunnerHandle runner_handler(runner); |
475 net::TestURLFetcherFactory url_fetcher_factory; | 483 net::TestURLFetcherFactory url_fetcher_factory; |
476 | 484 |
477 testing::StrictMock<MockProtocolDelegate> test_delegate; | 485 testing::StrictMock<MockProtocolDelegate> test_delegate; |
478 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); | 486 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
479 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( | 487 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
480 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, | 488 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
481 std::vector<SBListChunkRanges>(), | 489 std::vector<SBListChunkRanges>(), |
482 false))); | 490 false))); |
483 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); | 491 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); |
484 | 492 |
485 scoped_ptr<SafeBrowsingProtocolManager> pm( | 493 scoped_ptr<SafeBrowsingProtocolManager> pm( |
486 CreateProtocolManager(&test_delegate)); | 494 CreateProtocolManager(&test_delegate)); |
487 | 495 |
488 // Kick off initialization. This returns chunks from the DB synchronously. | 496 // Kick off initialization. This returns chunks from the DB synchronously. |
489 pm->ForceScheduleNextUpdate(TimeDelta()); | 497 pm->ForceScheduleNextUpdate(TimeDelta()); |
490 runner->RunPendingTasks(); | 498 runner->RunPendingTasks(); |
491 | 499 |
492 // We should have an URLFetcher at this point in time. | 500 // We should have an URLFetcher at this point in time. |
493 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); | 501 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
494 ValidateUpdateFetcherRequest(url_fetcher); | 502 ValidateUpdateFetcherRequest(url_fetcher); |
495 | 503 |
496 // The first time RunTasks is called above, the update timeout timer is not | 504 // The first time RunPendingTasks is called above, the update timeout timer is |
497 // handled. This call of RunTasks will handle the update. | 505 // not handled. This call of RunPendingTasks will handle the update. |
498 runner->RunPendingTasks(); | 506 runner->RunPendingTasks(); |
| 507 |
| 508 EXPECT_TRUE(pm->IsUpdateScheduled()); |
499 } | 509 } |
500 | 510 |
501 // Tests what happens when there is a reset command in the response. | 511 // Tests what happens when there is a reset command in the response. |
502 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseReset) { | 512 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseReset) { |
503 scoped_refptr<base::TestSimpleTaskRunner> runner( | 513 scoped_refptr<base::TestSimpleTaskRunner> runner( |
504 new base::TestSimpleTaskRunner()); | 514 new base::TestSimpleTaskRunner()); |
505 base::ThreadTaskRunnerHandle runner_handler(runner); | 515 base::ThreadTaskRunnerHandle runner_handler(runner); |
506 net::TestURLFetcherFactory url_fetcher_factory; | 516 net::TestURLFetcherFactory url_fetcher_factory; |
507 | 517 |
508 testing::StrictMock<MockProtocolDelegate> test_delegate; | 518 testing::StrictMock<MockProtocolDelegate> test_delegate; |
509 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); | 519 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
510 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( | 520 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
511 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, | 521 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
512 std::vector<SBListChunkRanges>(), | 522 std::vector<SBListChunkRanges>(), |
513 false))); | 523 false))); |
514 EXPECT_CALL(test_delegate, ResetDatabase()).Times(1); | 524 EXPECT_CALL(test_delegate, ResetDatabase()).Times(1); |
515 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1); | 525 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1); |
516 | 526 |
517 scoped_ptr<SafeBrowsingProtocolManager> pm( | 527 scoped_ptr<SafeBrowsingProtocolManager> pm( |
518 CreateProtocolManager(&test_delegate)); | 528 CreateProtocolManager(&test_delegate)); |
519 | 529 |
520 // Kick off initialization. This returns chunks from the DB synchronously. | 530 // Kick off initialization. This returns chunks from the DB synchronously. |
521 pm->ForceScheduleNextUpdate(TimeDelta()); | 531 pm->ForceScheduleNextUpdate(TimeDelta()); |
522 runner->RunPendingTasks(); | 532 runner->RunPendingTasks(); |
523 | 533 |
524 // We should have an URLFetcher at this point in time. | |
525 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); | 534 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
526 ValidateUpdateFetcherRequest(url_fetcher); | 535 ValidateUpdateFetcherRequest(url_fetcher); |
527 | 536 |
528 // The update response is successful, and has a reset command. | 537 // The update response is successful, and has a reset command. |
529 url_fetcher->set_status(net::URLRequestStatus()); | 538 url_fetcher->set_status(net::URLRequestStatus()); |
530 url_fetcher->set_response_code(200); | 539 url_fetcher->set_response_code(200); |
531 url_fetcher->SetResponseString("r:pleasereset\n"); | 540 url_fetcher->SetResponseString("r:pleasereset\n"); |
532 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | 541 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
533 } | 542 |
| 543 EXPECT_TRUE(pm->IsUpdateScheduled()); |
| 544 } |
| 545 |
| 546 // Tests a single valid update response, followed by a single redirect response |
| 547 // that has an valid, but empty body. |
| 548 TEST_F(SafeBrowsingProtocolManagerTest, EmptyRedirectResponse) { |
| 549 scoped_refptr<base::TestSimpleTaskRunner> runner( |
| 550 new base::TestSimpleTaskRunner()); |
| 551 base::ThreadTaskRunnerHandle runner_handler(runner); |
| 552 net::TestURLFetcherFactory url_fetcher_factory; |
| 553 |
| 554 testing::StrictMock<MockProtocolDelegate> test_delegate; |
| 555 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
| 556 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
| 557 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
| 558 std::vector<SBListChunkRanges>(), |
| 559 false))); |
| 560 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1); |
| 561 |
| 562 scoped_ptr<SafeBrowsingProtocolManager> pm( |
| 563 CreateProtocolManager(&test_delegate)); |
| 564 |
| 565 // Kick off initialization. This returns chunks from the DB synchronously. |
| 566 pm->ForceScheduleNextUpdate(TimeDelta()); |
| 567 runner->RunPendingTasks(); |
| 568 |
| 569 // The update response contains a single redirect command. |
| 570 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
| 571 ValidateUpdateFetcherRequest(url_fetcher); |
| 572 url_fetcher->set_status(net::URLRequestStatus()); |
| 573 url_fetcher->set_response_code(200); |
| 574 url_fetcher->SetResponseString( |
| 575 "i:goog-phish-shavar\n" |
| 576 "u:redirect-server.example.com/path\n"); |
| 577 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 578 |
| 579 // The redirect response contains an empty body. |
| 580 net::TestURLFetcher* chunk_url_fetcher = |
| 581 url_fetcher_factory.GetFetcherByID(1); |
| 582 ValidateRedirectFetcherRequest( |
| 583 chunk_url_fetcher, "https://redirect-server.example.com/path"); |
| 584 chunk_url_fetcher->set_status(net::URLRequestStatus()); |
| 585 chunk_url_fetcher->set_response_code(200); |
| 586 chunk_url_fetcher->SetResponseString(""); |
| 587 chunk_url_fetcher->delegate()->OnURLFetchComplete(chunk_url_fetcher); |
| 588 |
| 589 EXPECT_TRUE(pm->IsUpdateScheduled()); |
| 590 } |
| 591 |
| 592 // Tests a single valid update response, followed by a single redirect response |
| 593 // that has an invalid body. |
| 594 TEST_F(SafeBrowsingProtocolManagerTest, InvalidRedirectResponse) { |
| 595 scoped_refptr<base::TestSimpleTaskRunner> runner( |
| 596 new base::TestSimpleTaskRunner()); |
| 597 base::ThreadTaskRunnerHandle runner_handler(runner); |
| 598 net::TestURLFetcherFactory url_fetcher_factory; |
| 599 |
| 600 testing::StrictMock<MockProtocolDelegate> test_delegate; |
| 601 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
| 602 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
| 603 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
| 604 std::vector<SBListChunkRanges>(), |
| 605 false))); |
| 606 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); |
| 607 |
| 608 scoped_ptr<SafeBrowsingProtocolManager> pm( |
| 609 CreateProtocolManager(&test_delegate)); |
| 610 |
| 611 // Kick off initialization. This returns chunks from the DB synchronously. |
| 612 pm->ForceScheduleNextUpdate(TimeDelta()); |
| 613 runner->RunPendingTasks(); |
| 614 |
| 615 // The update response contains a single redirect command. |
| 616 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
| 617 ValidateUpdateFetcherRequest(url_fetcher); |
| 618 url_fetcher->set_status(net::URLRequestStatus()); |
| 619 url_fetcher->set_response_code(200); |
| 620 url_fetcher->SetResponseString( |
| 621 "i:goog-phish-shavar\n" |
| 622 "u:redirect-server.example.com/path\n"); |
| 623 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 624 |
| 625 // The redirect response contains an invalid body. |
| 626 net::TestURLFetcher* chunk_url_fetcher = |
| 627 url_fetcher_factory.GetFetcherByID(1); |
| 628 ValidateRedirectFetcherRequest( |
| 629 chunk_url_fetcher, "https://redirect-server.example.com/path"); |
| 630 chunk_url_fetcher->set_status(net::URLRequestStatus()); |
| 631 chunk_url_fetcher->set_response_code(200); |
| 632 chunk_url_fetcher->SetResponseString("THIS IS AN INVALID RESPONSE"); |
| 633 chunk_url_fetcher->delegate()->OnURLFetchComplete(chunk_url_fetcher); |
| 634 |
| 635 EXPECT_TRUE(pm->IsUpdateScheduled()); |
| 636 } |
| 637 |
| 638 // Tests a single valid update response, followed by a single redirect response |
| 639 // containing chunks. |
| 640 TEST_F(SafeBrowsingProtocolManagerTest, SingleRedirectResponseWithChunks) { |
| 641 scoped_refptr<base::TestSimpleTaskRunner> runner( |
| 642 new base::TestSimpleTaskRunner()); |
| 643 base::ThreadTaskRunnerHandle runner_handler(runner); |
| 644 net::TestURLFetcherFactory url_fetcher_factory; |
| 645 |
| 646 testing::StrictMock<MockProtocolDelegate> test_delegate; |
| 647 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
| 648 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
| 649 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
| 650 std::vector<SBListChunkRanges>(), |
| 651 false))); |
| 652 EXPECT_CALL(test_delegate, AddChunks("goog-phish-shavar", _, _)).WillOnce( |
| 653 Invoke(HandleAddChunks)); |
| 654 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1); |
| 655 |
| 656 scoped_ptr<SafeBrowsingProtocolManager> pm( |
| 657 CreateProtocolManager(&test_delegate)); |
| 658 |
| 659 // Kick off initialization. This returns chunks from the DB synchronously. |
| 660 pm->ForceScheduleNextUpdate(TimeDelta()); |
| 661 runner->RunPendingTasks(); |
| 662 |
| 663 // The update response contains a single redirect command. |
| 664 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
| 665 ValidateUpdateFetcherRequest(url_fetcher); |
| 666 url_fetcher->set_status(net::URLRequestStatus()); |
| 667 url_fetcher->set_response_code(200); |
| 668 url_fetcher->SetResponseString( |
| 669 "i:goog-phish-shavar\n" |
| 670 "u:redirect-server.example.com/path\n"); |
| 671 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 672 |
| 673 // The redirect response contains a single chunk. |
| 674 net::TestURLFetcher* chunk_url_fetcher = |
| 675 url_fetcher_factory.GetFetcherByID(1); |
| 676 ValidateRedirectFetcherRequest( |
| 677 chunk_url_fetcher, "https://redirect-server.example.com/path"); |
| 678 chunk_url_fetcher->set_status(net::URLRequestStatus()); |
| 679 chunk_url_fetcher->set_response_code(200); |
| 680 chunk_url_fetcher->SetResponseString( |
| 681 "a:4:4:9\n" |
| 682 "host\1fdaf"); |
| 683 chunk_url_fetcher->delegate()->OnURLFetchComplete(chunk_url_fetcher); |
| 684 |
| 685 EXPECT_FALSE(pm->IsUpdateScheduled()); |
| 686 |
| 687 // The AddChunksCallback needs to be invoked. |
| 688 runner->RunPendingTasks(); |
| 689 |
| 690 EXPECT_TRUE(pm->IsUpdateScheduled()); |
| 691 } |
| 692 |
| 693 // Tests a single valid update response, followed by multiple redirect responses |
| 694 // containing chunks. |
| 695 TEST_F(SafeBrowsingProtocolManagerTest, MultipleRedirectResponsesWithChunks) { |
| 696 scoped_refptr<base::TestSimpleTaskRunner> runner( |
| 697 new base::TestSimpleTaskRunner()); |
| 698 base::ThreadTaskRunnerHandle runner_handler(runner); |
| 699 net::TestURLFetcherFactory url_fetcher_factory; |
| 700 |
| 701 testing::StrictMock<MockProtocolDelegate> test_delegate; |
| 702 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
| 703 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
| 704 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
| 705 std::vector<SBListChunkRanges>(), |
| 706 false))); |
| 707 EXPECT_CALL(test_delegate, AddChunks("goog-phish-shavar", _, _)). |
| 708 WillRepeatedly(Invoke(HandleAddChunks)); |
| 709 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1); |
| 710 |
| 711 scoped_ptr<SafeBrowsingProtocolManager> pm( |
| 712 CreateProtocolManager(&test_delegate)); |
| 713 |
| 714 // Kick off initialization. This returns chunks from the DB synchronously. |
| 715 pm->ForceScheduleNextUpdate(TimeDelta()); |
| 716 runner->RunPendingTasks(); |
| 717 |
| 718 // The update response contains multiple redirect commands. |
| 719 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
| 720 ValidateUpdateFetcherRequest(url_fetcher); |
| 721 url_fetcher->set_status(net::URLRequestStatus()); |
| 722 url_fetcher->set_response_code(200); |
| 723 url_fetcher->SetResponseString( |
| 724 "i:goog-phish-shavar\n" |
| 725 "u:redirect-server.example.com/one\n" |
| 726 "u:redirect-server.example.com/two\n"); |
| 727 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 728 |
| 729 // The first redirect response contains a single chunk. |
| 730 net::TestURLFetcher* first_chunk_url_fetcher = |
| 731 url_fetcher_factory.GetFetcherByID(1); |
| 732 ValidateRedirectFetcherRequest( |
| 733 first_chunk_url_fetcher, "https://redirect-server.example.com/one"); |
| 734 first_chunk_url_fetcher->set_status(net::URLRequestStatus()); |
| 735 first_chunk_url_fetcher->set_response_code(200); |
| 736 first_chunk_url_fetcher->SetResponseString( |
| 737 "a:4:4:9\n" |
| 738 "host\1aaaa"); |
| 739 first_chunk_url_fetcher->delegate()->OnURLFetchComplete( |
| 740 first_chunk_url_fetcher); |
| 741 |
| 742 // Invoke the AddChunksCallback to trigger the second request. |
| 743 runner->RunPendingTasks(); |
| 744 |
| 745 EXPECT_FALSE(pm->IsUpdateScheduled()); |
| 746 |
| 747 // The second redirect response contains a single chunk. |
| 748 net::TestURLFetcher* second_chunk_url_fetcher = |
| 749 url_fetcher_factory.GetFetcherByID(2); |
| 750 ValidateRedirectFetcherRequest( |
| 751 second_chunk_url_fetcher, "https://redirect-server.example.com/two"); |
| 752 second_chunk_url_fetcher->set_status(net::URLRequestStatus()); |
| 753 second_chunk_url_fetcher->set_response_code(200); |
| 754 second_chunk_url_fetcher->SetResponseString( |
| 755 "a:5:4:9\n" |
| 756 "host\1bbbb"); |
| 757 second_chunk_url_fetcher->delegate()->OnURLFetchComplete( |
| 758 second_chunk_url_fetcher); |
| 759 |
| 760 EXPECT_FALSE(pm->IsUpdateScheduled()); |
| 761 |
| 762 // Invoke the AddChunksCallback to finish the update. |
| 763 runner->RunPendingTasks(); |
| 764 |
| 765 EXPECT_TRUE(pm->IsUpdateScheduled()); |
| 766 } |
OLD | NEW |