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" |
11 #include "google_apis/google_api_keys.h" | 11 #include "google_apis/google_api_keys.h" |
12 #include "net/base/escape.h" | 12 #include "net/base/escape.h" |
13 #include "net/base/load_flags.h" | 13 #include "net/base/load_flags.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "net/url_request/test_url_fetcher_factory.h" | 15 #include "net/url_request/test_url_fetcher_factory.h" |
16 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
17 #include "testing/gmock_mutant.h" | 17 #include "testing/gmock_mutant.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 using base::Time; | 20 using base::Time; |
21 using base::TimeDelta; | 21 using base::TimeDelta; |
22 using testing::_; | 22 using testing::_; |
23 using testing::Invoke; | 23 using testing::Invoke; |
24 | 24 |
25 static const char kUrlPrefix[] = "https://prefix.com/foo"; | 25 static const char kUrlPrefix[] = "https://prefix.com/foo"; |
| 26 static const char kBackupConnectUrlPrefix[] = "https://alt1-prefix.com/foo"; |
| 27 static const char kBackupHttpUrlPrefix[] = "https://alt2-prefix.com/foo"; |
| 28 static const char kBackupNetworkUrlPrefix[] = "https://alt3-prefix.com/foo"; |
26 static const char kClient[] = "unittest"; | 29 static const char kClient[] = "unittest"; |
27 static const char kAppVer[] = "1.0"; | 30 static const char kAppVer[] = "1.0"; |
28 static const char kAdditionalQuery[] = "additional_query"; | 31 static const char kAdditionalQuery[] = "additional_query"; |
29 | 32 |
30 class SafeBrowsingProtocolManagerTest : public testing::Test { | 33 class SafeBrowsingProtocolManagerTest : public testing::Test { |
31 protected: | 34 protected: |
32 std::string key_param_; | 35 std::string key_param_; |
33 | 36 |
34 virtual void SetUp() { | 37 virtual void SetUp() { |
35 std::string key = google_apis::GetAPIKey(); | 38 std::string key = google_apis::GetAPIKey(); |
36 if (!key.empty()) { | 39 if (!key.empty()) { |
37 key_param_ = base::StringPrintf( | 40 key_param_ = base::StringPrintf( |
38 "&key=%s", | 41 "&key=%s", |
39 net::EscapeQueryParamValue(key, true).c_str()); | 42 net::EscapeQueryParamValue(key, true).c_str()); |
40 } | 43 } |
41 } | 44 } |
42 | 45 |
43 scoped_ptr<SafeBrowsingProtocolManager> CreateProtocolManager( | 46 scoped_ptr<SafeBrowsingProtocolManager> CreateProtocolManager( |
44 SafeBrowsingProtocolManagerDelegate* delegate) { | 47 SafeBrowsingProtocolManagerDelegate* delegate) { |
45 SafeBrowsingProtocolConfig config; | 48 SafeBrowsingProtocolConfig config; |
46 config.client_name = kClient; | 49 config.client_name = kClient; |
47 config.url_prefix = kUrlPrefix; | 50 config.url_prefix = kUrlPrefix; |
| 51 config.backup_connect_error_url_prefix = kBackupConnectUrlPrefix; |
| 52 config.backup_http_error_url_prefix = kBackupHttpUrlPrefix; |
| 53 config.backup_network_error_url_prefix = kBackupNetworkUrlPrefix; |
48 config.version = kAppVer; | 54 config.version = kAppVer; |
49 | 55 |
50 return scoped_ptr<SafeBrowsingProtocolManager>( | 56 return scoped_ptr<SafeBrowsingProtocolManager>( |
51 SafeBrowsingProtocolManager::Create(delegate, NULL, config)); | 57 SafeBrowsingProtocolManager::Create(delegate, NULL, config)); |
52 } | 58 } |
53 | 59 |
54 void ValidateUpdateFetcherRequest(const net::TestURLFetcher* url_fetcher) { | 60 void ValidateUpdateFetcherRequest( |
| 61 const net::TestURLFetcher* url_fetcher, |
| 62 const std::string& expected_prefix) { |
55 ASSERT_TRUE(url_fetcher); | 63 ASSERT_TRUE(url_fetcher); |
56 EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags()); | 64 EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags()); |
57 EXPECT_EQ("goog-phish-shavar;\ngoog-malware-shavar;\n", | 65 EXPECT_EQ("goog-phish-shavar;\ngoog-malware-shavar;\n", |
58 url_fetcher->upload_data()); | 66 url_fetcher->upload_data()); |
59 EXPECT_EQ(GURL("https://prefix.com/foo/downloads?client=unittest&appver=1.0" | 67 EXPECT_EQ(GURL(expected_prefix + "/downloads?client=unittest&appver=1.0" |
60 "&pver=2.2" + key_param_), | 68 "&pver=2.2" + key_param_), |
61 url_fetcher->GetOriginalURL()); | 69 url_fetcher->GetOriginalURL()); |
62 } | 70 } |
63 | 71 |
| 72 void ValidateUpdateFetcherRequest(const net::TestURLFetcher* url_fetcher) { |
| 73 ValidateUpdateFetcherRequest(url_fetcher, kUrlPrefix); |
| 74 } |
| 75 |
64 void ValidateRedirectFetcherRequest(const net::TestURLFetcher* url_fetcher, | 76 void ValidateRedirectFetcherRequest(const net::TestURLFetcher* url_fetcher, |
65 const std::string& expected_url) { | 77 const std::string& expected_url) { |
66 ASSERT_TRUE(url_fetcher); | 78 ASSERT_TRUE(url_fetcher); |
67 EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags()); | 79 EXPECT_EQ(net::LOAD_DISABLE_CACHE, url_fetcher->GetLoadFlags()); |
68 EXPECT_EQ("", url_fetcher->upload_data()); | 80 EXPECT_EQ("", url_fetcher->upload_data()); |
69 EXPECT_EQ(GURL(expected_url), url_fetcher->GetOriginalURL()); | 81 EXPECT_EQ(GURL(expected_url), url_fetcher->GetOriginalURL()); |
70 } | 82 } |
71 }; | 83 }; |
72 | 84 |
73 // Ensure that we respect section 5 of the SafeBrowsing protocol specification. | 85 // Ensure that we respect section 5 of the SafeBrowsing protocol specification. |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 | 411 |
400 // The update response is successful, but an invalid body. | 412 // The update response is successful, but an invalid body. |
401 url_fetcher->set_status(net::URLRequestStatus()); | 413 url_fetcher->set_status(net::URLRequestStatus()); |
402 url_fetcher->set_response_code(200); | 414 url_fetcher->set_response_code(200); |
403 url_fetcher->SetResponseString("THIS_IS_A_BAD_RESPONSE"); | 415 url_fetcher->SetResponseString("THIS_IS_A_BAD_RESPONSE"); |
404 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | 416 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
405 | 417 |
406 EXPECT_TRUE(pm->IsUpdateScheduled()); | 418 EXPECT_TRUE(pm->IsUpdateScheduled()); |
407 } | 419 } |
408 | 420 |
409 // Tests what happens when there is an error in the update response. | 421 // Tests what happens when there is an HTTP error response to the update |
410 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpError) { | 422 // request, as well as an error response to the backup update request. |
| 423 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpErrorBackupError) { |
411 scoped_refptr<base::TestSimpleTaskRunner> runner( | 424 scoped_refptr<base::TestSimpleTaskRunner> runner( |
412 new base::TestSimpleTaskRunner()); | 425 new base::TestSimpleTaskRunner()); |
413 base::ThreadTaskRunnerHandle runner_handler(runner); | 426 base::ThreadTaskRunnerHandle runner_handler(runner); |
| 427 net::TestURLFetcherFactory url_fetcher_factory; |
| 428 |
| 429 testing::StrictMock<MockProtocolDelegate> test_delegate; |
| 430 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
| 431 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
| 432 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
| 433 std::vector<SBListChunkRanges>(), |
| 434 false))); |
| 435 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); |
| 436 |
| 437 scoped_ptr<SafeBrowsingProtocolManager> pm( |
| 438 CreateProtocolManager(&test_delegate)); |
| 439 |
| 440 // Kick off initialization. This returns chunks from the DB synchronously. |
| 441 pm->ForceScheduleNextUpdate(TimeDelta()); |
| 442 runner->RunPendingTasks(); |
| 443 |
| 444 // We should have an URLFetcher at this point in time. |
| 445 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
| 446 ValidateUpdateFetcherRequest(url_fetcher); |
| 447 |
| 448 // Go ahead and respond to it. |
| 449 url_fetcher->set_status(net::URLRequestStatus()); |
| 450 url_fetcher->set_response_code(404); |
| 451 url_fetcher->SetResponseString(""); |
| 452 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 453 |
| 454 // There should now be a backup request. |
| 455 net::TestURLFetcher* backup_url_fetcher = |
| 456 url_fetcher_factory.GetFetcherByID(1); |
| 457 ValidateUpdateFetcherRequest(backup_url_fetcher, kBackupHttpUrlPrefix); |
| 458 |
| 459 // Respond to the backup unsuccessfully. |
| 460 backup_url_fetcher->set_status(net::URLRequestStatus()); |
| 461 backup_url_fetcher->set_response_code(404); |
| 462 backup_url_fetcher->SetResponseString(""); |
| 463 backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher); |
| 464 |
| 465 EXPECT_TRUE(pm->IsUpdateScheduled()); |
| 466 } |
| 467 |
| 468 // Tests what happens when there is an HTTP error response to the update |
| 469 // request, followed by a successful response to the backup update request. |
| 470 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpErrorBackupSuccess) { |
| 471 scoped_refptr<base::TestSimpleTaskRunner> runner( |
| 472 new base::TestSimpleTaskRunner()); |
| 473 base::ThreadTaskRunnerHandle runner_handler(runner); |
| 474 net::TestURLFetcherFactory url_fetcher_factory; |
| 475 |
| 476 testing::StrictMock<MockProtocolDelegate> test_delegate; |
| 477 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
| 478 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
| 479 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
| 480 std::vector<SBListChunkRanges>(), |
| 481 false))); |
| 482 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1); |
| 483 |
| 484 scoped_ptr<SafeBrowsingProtocolManager> pm( |
| 485 CreateProtocolManager(&test_delegate)); |
| 486 |
| 487 // Kick off initialization. This returns chunks from the DB synchronously. |
| 488 pm->ForceScheduleNextUpdate(TimeDelta()); |
| 489 runner->RunPendingTasks(); |
| 490 |
| 491 // We should have an URLFetcher at this point in time. |
| 492 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
| 493 ValidateUpdateFetcherRequest(url_fetcher); |
| 494 |
| 495 // Go ahead and respond to it. |
| 496 url_fetcher->set_status(net::URLRequestStatus()); |
| 497 url_fetcher->set_response_code(404); |
| 498 url_fetcher->SetResponseString(""); |
| 499 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 500 |
| 501 // There should now be a backup request. |
| 502 net::TestURLFetcher* backup_url_fetcher = |
| 503 url_fetcher_factory.GetFetcherByID(1); |
| 504 ValidateUpdateFetcherRequest(backup_url_fetcher, |
| 505 kBackupHttpUrlPrefix); |
| 506 |
| 507 // Respond to the backup successfully. |
| 508 backup_url_fetcher->set_status(net::URLRequestStatus()); |
| 509 backup_url_fetcher->set_response_code(200); |
| 510 backup_url_fetcher->SetResponseString(""); |
| 511 backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher); |
| 512 |
| 513 EXPECT_TRUE(pm->IsUpdateScheduled()); |
| 514 } |
| 515 |
| 516 // Tests what happens when there is an HTTP error response to the update |
| 517 // request, and a timeout on the backup update request. |
| 518 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseHttpErrorBackupTimeout) { |
| 519 scoped_refptr<base::TestSimpleTaskRunner> runner( |
| 520 new base::TestSimpleTaskRunner()); |
| 521 base::ThreadTaskRunnerHandle runner_handler(runner); |
414 net::TestURLFetcherFactory url_fetcher_factory; | 522 net::TestURLFetcherFactory url_fetcher_factory; |
415 | 523 |
416 testing::StrictMock<MockProtocolDelegate> test_delegate; | 524 testing::StrictMock<MockProtocolDelegate> test_delegate; |
417 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); | 525 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
418 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( | 526 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
419 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, | 527 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
420 std::vector<SBListChunkRanges>(), | 528 std::vector<SBListChunkRanges>(), |
421 false))); | 529 false))); |
422 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); | 530 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); |
423 | 531 |
424 scoped_ptr<SafeBrowsingProtocolManager> pm( | 532 scoped_ptr<SafeBrowsingProtocolManager> pm( |
425 CreateProtocolManager(&test_delegate)); | 533 CreateProtocolManager(&test_delegate)); |
426 | 534 |
427 // Kick off initialization. This returns chunks from the DB synchronously. | 535 // Kick off initialization. This returns chunks from the DB synchronously. |
428 pm->ForceScheduleNextUpdate(TimeDelta()); | 536 pm->ForceScheduleNextUpdate(TimeDelta()); |
429 runner->RunPendingTasks(); | 537 runner->RunPendingTasks(); |
430 | 538 |
431 // We should have an URLFetcher at this point in time. | 539 // We should have an URLFetcher at this point in time. |
432 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); | 540 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
433 ValidateUpdateFetcherRequest(url_fetcher); | 541 ValidateUpdateFetcherRequest(url_fetcher); |
434 | 542 |
435 // Go ahead and respond to it. | 543 // Go ahead and respond to it. |
436 url_fetcher->set_status(net::URLRequestStatus()); | 544 url_fetcher->set_status(net::URLRequestStatus()); |
437 url_fetcher->set_response_code(404); | 545 url_fetcher->set_response_code(404); |
438 url_fetcher->SetResponseString(""); | 546 url_fetcher->SetResponseString(""); |
439 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | 547 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
440 | 548 |
| 549 // There should now be a backup request. |
| 550 net::TestURLFetcher* backup_url_fetcher = |
| 551 url_fetcher_factory.GetFetcherByID(1); |
| 552 ValidateUpdateFetcherRequest(backup_url_fetcher, kBackupHttpUrlPrefix); |
| 553 |
| 554 // This call of RunPendingTasks will invoke the timeout. |
| 555 runner->RunPendingTasks(); |
| 556 |
| 557 // Respond to the backup unsuccessfully. |
| 558 backup_url_fetcher->set_status(net::URLRequestStatus()); |
| 559 backup_url_fetcher->set_response_code(404); |
| 560 backup_url_fetcher->SetResponseString(""); |
| 561 backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher); |
| 562 |
441 EXPECT_TRUE(pm->IsUpdateScheduled()); | 563 EXPECT_TRUE(pm->IsUpdateScheduled()); |
442 } | 564 } |
443 | 565 |
444 // Tests what happens when there is an error with the connection. | 566 // Tests what happens when there is a connection error when issuing the update |
445 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseConnectionError) { | 567 // request, and an error with the backup update request. |
| 568 TEST_F(SafeBrowsingProtocolManagerTest, |
| 569 UpdateResponseConnectionErrorBackupError) { |
446 scoped_refptr<base::TestSimpleTaskRunner> runner( | 570 scoped_refptr<base::TestSimpleTaskRunner> runner( |
447 new base::TestSimpleTaskRunner()); | 571 new base::TestSimpleTaskRunner()); |
448 base::ThreadTaskRunnerHandle runner_handler(runner); | 572 base::ThreadTaskRunnerHandle runner_handler(runner); |
449 net::TestURLFetcherFactory url_fetcher_factory; | 573 net::TestURLFetcherFactory url_fetcher_factory; |
450 | 574 |
451 testing::StrictMock<MockProtocolDelegate> test_delegate; | 575 testing::StrictMock<MockProtocolDelegate> test_delegate; |
452 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); | 576 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
453 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( | 577 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
454 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, | 578 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
455 std::vector<SBListChunkRanges>(), | 579 std::vector<SBListChunkRanges>(), |
456 false))); | 580 false))); |
457 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); | 581 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); |
458 | 582 |
459 scoped_ptr<SafeBrowsingProtocolManager> pm( | 583 scoped_ptr<SafeBrowsingProtocolManager> pm( |
460 CreateProtocolManager(&test_delegate)); | 584 CreateProtocolManager(&test_delegate)); |
461 | 585 |
462 // Kick off initialization. This returns chunks from the DB synchronously. | 586 // Kick off initialization. This returns chunks from the DB synchronously. |
463 pm->ForceScheduleNextUpdate(TimeDelta()); | 587 pm->ForceScheduleNextUpdate(TimeDelta()); |
464 runner->RunPendingTasks(); | 588 runner->RunPendingTasks(); |
465 | 589 |
466 // We should have an URLFetcher at this point in time. | 590 // We should have an URLFetcher at this point in time. |
467 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); | 591 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
468 ValidateUpdateFetcherRequest(url_fetcher); | 592 ValidateUpdateFetcherRequest(url_fetcher); |
469 | 593 |
470 // Go ahead and respond to it. | 594 // Go ahead and respond to it. |
471 url_fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 595 url_fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
472 net::ERR_CONNECTION_RESET)); | 596 net::ERR_CONNECTION_RESET)); |
473 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); | 597 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
474 | 598 |
| 599 // There should be a backup URLFetcher now. |
| 600 net::TestURLFetcher* backup_url_fetcher = |
| 601 url_fetcher_factory.GetFetcherByID(1); |
| 602 ValidateUpdateFetcherRequest(backup_url_fetcher, |
| 603 kBackupConnectUrlPrefix); |
| 604 |
| 605 // Respond to the backup unsuccessfully. |
| 606 backup_url_fetcher->set_status(net::URLRequestStatus()); |
| 607 backup_url_fetcher->set_response_code(404); |
| 608 backup_url_fetcher->SetResponseString(""); |
| 609 backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher); |
| 610 |
475 EXPECT_TRUE(pm->IsUpdateScheduled()); | 611 EXPECT_TRUE(pm->IsUpdateScheduled()); |
476 } | 612 } |
477 | 613 |
| 614 // Tests what happens when there is a connection error when issuing the update |
| 615 // request, and a successful response to the backup update request. |
| 616 TEST_F(SafeBrowsingProtocolManagerTest, |
| 617 UpdateResponseConnectionErrorBackupSuccess) { |
| 618 scoped_refptr<base::TestSimpleTaskRunner> runner( |
| 619 new base::TestSimpleTaskRunner()); |
| 620 base::ThreadTaskRunnerHandle runner_handler(runner); |
| 621 net::TestURLFetcherFactory url_fetcher_factory; |
| 622 |
| 623 testing::StrictMock<MockProtocolDelegate> test_delegate; |
| 624 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
| 625 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
| 626 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
| 627 std::vector<SBListChunkRanges>(), |
| 628 false))); |
| 629 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1); |
| 630 |
| 631 scoped_ptr<SafeBrowsingProtocolManager> pm( |
| 632 CreateProtocolManager(&test_delegate)); |
| 633 |
| 634 // Kick off initialization. This returns chunks from the DB synchronously. |
| 635 pm->ForceScheduleNextUpdate(TimeDelta()); |
| 636 runner->RunPendingTasks(); |
| 637 |
| 638 // We should have an URLFetcher at this point in time. |
| 639 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
| 640 ValidateUpdateFetcherRequest(url_fetcher); |
| 641 |
| 642 // Go ahead and respond to it. |
| 643 url_fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 644 net::ERR_CONNECTION_RESET)); |
| 645 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 646 |
| 647 // There should be a backup URLFetcher now. |
| 648 net::TestURLFetcher* backup_url_fetcher = |
| 649 url_fetcher_factory.GetFetcherByID(1); |
| 650 ValidateUpdateFetcherRequest(backup_url_fetcher, |
| 651 kBackupConnectUrlPrefix); |
| 652 |
| 653 // Respond to the backup unsuccessfully. |
| 654 backup_url_fetcher->set_status(net::URLRequestStatus()); |
| 655 backup_url_fetcher->set_response_code(200); |
| 656 backup_url_fetcher->SetResponseString(""); |
| 657 backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher); |
| 658 |
| 659 EXPECT_TRUE(pm->IsUpdateScheduled()); |
| 660 } |
| 661 // Tests what happens when there is a network state error when issuing the |
| 662 // update request, and an error with the backup update request. |
| 663 TEST_F(SafeBrowsingProtocolManagerTest, |
| 664 UpdateResponseNetworkErrorBackupError) { |
| 665 scoped_refptr<base::TestSimpleTaskRunner> runner( |
| 666 new base::TestSimpleTaskRunner()); |
| 667 base::ThreadTaskRunnerHandle runner_handler(runner); |
| 668 net::TestURLFetcherFactory url_fetcher_factory; |
| 669 |
| 670 testing::StrictMock<MockProtocolDelegate> test_delegate; |
| 671 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
| 672 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
| 673 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
| 674 std::vector<SBListChunkRanges>(), |
| 675 false))); |
| 676 EXPECT_CALL(test_delegate, UpdateFinished(false)).Times(1); |
| 677 |
| 678 scoped_ptr<SafeBrowsingProtocolManager> pm( |
| 679 CreateProtocolManager(&test_delegate)); |
| 680 |
| 681 // Kick off initialization. This returns chunks from the DB synchronously. |
| 682 pm->ForceScheduleNextUpdate(TimeDelta()); |
| 683 runner->RunPendingTasks(); |
| 684 |
| 685 // We should have an URLFetcher at this point in time. |
| 686 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
| 687 ValidateUpdateFetcherRequest(url_fetcher); |
| 688 |
| 689 // Go ahead and respond to it. |
| 690 url_fetcher->set_status( |
| 691 net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 692 net::ERR_INTERNET_DISCONNECTED)); |
| 693 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 694 |
| 695 // There should be a backup URLFetcher now. |
| 696 net::TestURLFetcher* backup_url_fetcher = |
| 697 url_fetcher_factory.GetFetcherByID(1); |
| 698 ValidateUpdateFetcherRequest(backup_url_fetcher, |
| 699 kBackupNetworkUrlPrefix); |
| 700 |
| 701 // Respond to the backup unsuccessfully. |
| 702 backup_url_fetcher->set_status(net::URLRequestStatus()); |
| 703 backup_url_fetcher->set_response_code(404); |
| 704 backup_url_fetcher->SetResponseString(""); |
| 705 backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher); |
| 706 |
| 707 EXPECT_TRUE(pm->IsUpdateScheduled()); |
| 708 } |
| 709 |
| 710 // Tests what happens when there is a network state error when issuing the |
| 711 // update request, and a successful response to the backup update request. |
| 712 TEST_F(SafeBrowsingProtocolManagerTest, |
| 713 UpdateResponseNetworkErrorBackupSuccess) { |
| 714 scoped_refptr<base::TestSimpleTaskRunner> runner( |
| 715 new base::TestSimpleTaskRunner()); |
| 716 base::ThreadTaskRunnerHandle runner_handler(runner); |
| 717 net::TestURLFetcherFactory url_fetcher_factory; |
| 718 |
| 719 testing::StrictMock<MockProtocolDelegate> test_delegate; |
| 720 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
| 721 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
| 722 Invoke(testing::CreateFunctor(InvokeGetChunksCallback, |
| 723 std::vector<SBListChunkRanges>(), |
| 724 false))); |
| 725 EXPECT_CALL(test_delegate, UpdateFinished(true)).Times(1); |
| 726 |
| 727 scoped_ptr<SafeBrowsingProtocolManager> pm( |
| 728 CreateProtocolManager(&test_delegate)); |
| 729 |
| 730 // Kick off initialization. This returns chunks from the DB synchronously. |
| 731 pm->ForceScheduleNextUpdate(TimeDelta()); |
| 732 runner->RunPendingTasks(); |
| 733 |
| 734 // We should have an URLFetcher at this point in time. |
| 735 net::TestURLFetcher* url_fetcher = url_fetcher_factory.GetFetcherByID(0); |
| 736 ValidateUpdateFetcherRequest(url_fetcher); |
| 737 |
| 738 // Go ahead and respond to it. |
| 739 url_fetcher->set_status( |
| 740 net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 741 net::ERR_INTERNET_DISCONNECTED)); |
| 742 url_fetcher->delegate()->OnURLFetchComplete(url_fetcher); |
| 743 |
| 744 // There should be a backup URLFetcher now. |
| 745 net::TestURLFetcher* backup_url_fetcher = |
| 746 url_fetcher_factory.GetFetcherByID(1); |
| 747 ValidateUpdateFetcherRequest(backup_url_fetcher, |
| 748 kBackupNetworkUrlPrefix); |
| 749 |
| 750 // Respond to the backup unsuccessfully. |
| 751 backup_url_fetcher->set_status(net::URLRequestStatus()); |
| 752 backup_url_fetcher->set_response_code(200); |
| 753 backup_url_fetcher->SetResponseString(""); |
| 754 backup_url_fetcher->delegate()->OnURLFetchComplete(backup_url_fetcher); |
| 755 |
| 756 EXPECT_TRUE(pm->IsUpdateScheduled()); |
| 757 } |
| 758 |
478 // Tests what happens when there is a timeout before an update response. | 759 // Tests what happens when there is a timeout before an update response. |
479 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseTimeout) { | 760 TEST_F(SafeBrowsingProtocolManagerTest, UpdateResponseTimeout) { |
480 scoped_refptr<base::TestSimpleTaskRunner> runner( | 761 scoped_refptr<base::TestSimpleTaskRunner> runner( |
481 new base::TestSimpleTaskRunner()); | 762 new base::TestSimpleTaskRunner()); |
482 base::ThreadTaskRunnerHandle runner_handler(runner); | 763 base::ThreadTaskRunnerHandle runner_handler(runner); |
483 net::TestURLFetcherFactory url_fetcher_factory; | 764 net::TestURLFetcherFactory url_fetcher_factory; |
484 | 765 |
485 testing::StrictMock<MockProtocolDelegate> test_delegate; | 766 testing::StrictMock<MockProtocolDelegate> test_delegate; |
486 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); | 767 EXPECT_CALL(test_delegate, UpdateStarted()).Times(1); |
487 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( | 768 EXPECT_CALL(test_delegate, GetChunks(_)).WillOnce( |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
757 second_chunk_url_fetcher->delegate()->OnURLFetchComplete( | 1038 second_chunk_url_fetcher->delegate()->OnURLFetchComplete( |
758 second_chunk_url_fetcher); | 1039 second_chunk_url_fetcher); |
759 | 1040 |
760 EXPECT_FALSE(pm->IsUpdateScheduled()); | 1041 EXPECT_FALSE(pm->IsUpdateScheduled()); |
761 | 1042 |
762 // Invoke the AddChunksCallback to finish the update. | 1043 // Invoke the AddChunksCallback to finish the update. |
763 runner->RunPendingTasks(); | 1044 runner->RunPendingTasks(); |
764 | 1045 |
765 EXPECT_TRUE(pm->IsUpdateScheduled()); | 1046 EXPECT_TRUE(pm->IsUpdateScheduled()); |
766 } | 1047 } |
OLD | NEW |