| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include <map> | 5 #include <map> |
| 6 #include <queue> | 6 #include <queue> |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 MOCK_METHOD1(EndFetchModel, void(ClientModelStatus)); | 39 MOCK_METHOD1(EndFetchModel, void(ClientModelStatus)); |
| 40 MOCK_METHOD1(ScheduleFetchModel, void(int64)); | 40 MOCK_METHOD1(ScheduleFetchModel, void(int64)); |
| 41 | 41 |
| 42 void Schedule(int64) { | 42 void Schedule(int64) { |
| 43 // Ignore the delay when testing. | 43 // Ignore the delay when testing. |
| 44 StartFetchModel(); | 44 StartFetchModel(); |
| 45 } | 45 } |
| 46 | 46 |
| 47 void Disable(int) { | 47 void Disable(int) { |
| 48 // Ignore the status. | 48 // Ignore the status. |
| 49 SetEnabled(false); | 49 SetEnabledAndRefreshState(false); |
| 50 } | 50 } |
| 51 | 51 |
| 52 private: | 52 private: |
| 53 DISALLOW_COPY_AND_ASSIGN(MockClientSideDetectionService); | 53 DISALLOW_COPY_AND_ASSIGN(MockClientSideDetectionService); |
| 54 }; | 54 }; |
| 55 | 55 |
| 56 ACTION(QuitCurrentMessageLoop) { | 56 ACTION(QuitCurrentMessageLoop) { |
| 57 MessageLoop::current()->Quit(); | 57 MessageLoop::current()->Quit(); |
| 58 } | 58 } |
| 59 | 59 |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 GURL phishing_url_; | 195 GURL phishing_url_; |
| 196 bool is_phishing_; | 196 bool is_phishing_; |
| 197 }; | 197 }; |
| 198 | 198 |
| 199 TEST_F(ClientSideDetectionServiceTest, FetchModelTest) { | 199 TEST_F(ClientSideDetectionServiceTest, FetchModelTest) { |
| 200 // We don't want to use a real service class here because we can't call | 200 // We don't want to use a real service class here because we can't call |
| 201 // the real EndFetchModel. It would reschedule a reload which might | 201 // the real EndFetchModel. It would reschedule a reload which might |
| 202 // make the test flaky. | 202 // make the test flaky. |
| 203 MockClientSideDetectionService service; | 203 MockClientSideDetectionService service; |
| 204 EXPECT_CALL(service, ScheduleFetchModel(_)).Times(1); | 204 EXPECT_CALL(service, ScheduleFetchModel(_)).Times(1); |
| 205 service.SetEnabled(true); | 205 service.SetEnabledAndRefreshState(true); |
| 206 | 206 |
| 207 // The model fetch failed. | 207 // The model fetch failed. |
| 208 SetModelFetchResponse("blamodel", false /* failure */); | 208 SetModelFetchResponse("blamodel", false /* failure */); |
| 209 EXPECT_CALL(service, EndFetchModel( | 209 EXPECT_CALL(service, EndFetchModel( |
| 210 ClientSideDetectionService::MODEL_FETCH_FAILED)) | 210 ClientSideDetectionService::MODEL_FETCH_FAILED)) |
| 211 .WillOnce(QuitCurrentMessageLoop()); | 211 .WillOnce(QuitCurrentMessageLoop()); |
| 212 service.StartFetchModel(); | 212 service.StartFetchModel(); |
| 213 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 213 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
| 214 Mock::VerifyAndClearExpectations(&service); | 214 Mock::VerifyAndClearExpectations(&service); |
| 215 | 215 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 ClientSideDetectionService::MODEL_NOT_CHANGED)) | 305 ClientSideDetectionService::MODEL_NOT_CHANGED)) |
| 306 .WillOnce(QuitCurrentMessageLoop()); | 306 .WillOnce(QuitCurrentMessageLoop()); |
| 307 service.StartFetchModel(); | 307 service.StartFetchModel(); |
| 308 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 308 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
| 309 Mock::VerifyAndClearExpectations(&service); | 309 Mock::VerifyAndClearExpectations(&service); |
| 310 } | 310 } |
| 311 | 311 |
| 312 TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) { | 312 TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) { |
| 313 SetModelFetchResponse("bogus model", true /* success */); | 313 SetModelFetchResponse("bogus model", true /* success */); |
| 314 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | 314 csd_service_.reset(ClientSideDetectionService::Create(NULL)); |
| 315 csd_service_->SetEnabled(true); | 315 csd_service_->SetEnabledAndRefreshState(true); |
| 316 EXPECT_TRUE(csd_service_.get() != NULL); | 316 EXPECT_TRUE(csd_service_.get() != NULL); |
| 317 // We delete the client-side detection service class even though the callbacks | 317 // We delete the client-side detection service class even though the callbacks |
| 318 // haven't run yet. | 318 // haven't run yet. |
| 319 csd_service_.reset(); | 319 csd_service_.reset(); |
| 320 // Waiting for the callbacks to run should not crash even if the service | 320 // Waiting for the callbacks to run should not crash even if the service |
| 321 // object is gone. | 321 // object is gone. |
| 322 msg_loop_.RunAllPending(); | 322 msg_loop_.RunAllPending(); |
| 323 } | 323 } |
| 324 | 324 |
| 325 TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) { | 325 TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) { |
| 326 SetModelFetchResponse("bogus model", true /* success */); | 326 SetModelFetchResponse("bogus model", true /* success */); |
| 327 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | 327 csd_service_.reset(ClientSideDetectionService::Create(NULL)); |
| 328 csd_service_->SetEnabled(true); | 328 csd_service_->SetEnabledAndRefreshState(true); |
| 329 | 329 |
| 330 GURL url("http://a.com/"); | 330 GURL url("http://a.com/"); |
| 331 float score = 0.4f; // Some random client score. | 331 float score = 0.4f; // Some random client score. |
| 332 | 332 |
| 333 base::Time before = base::Time::Now(); | 333 base::Time before = base::Time::Now(); |
| 334 | 334 |
| 335 // Invalid response body from the server. | 335 // Invalid response body from the server. |
| 336 SetClientReportPhishingResponse("invalid proto response", true /* success */); | 336 SetClientReportPhishingResponse("invalid proto response", true /* success */); |
| 337 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); | 337 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); |
| 338 | 338 |
| (...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 rule->set_weight(0.2f); | 586 rule->set_weight(0.2f); |
| 587 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); | 587 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); |
| 588 | 588 |
| 589 rule->set_feature(2, 2); | 589 rule->set_feature(2, 2); |
| 590 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); | 590 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model)); |
| 591 | 591 |
| 592 rule->set_feature(2, 1); | 592 rule->set_feature(2, 1); |
| 593 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model)); | 593 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model)); |
| 594 } | 594 } |
| 595 | 595 |
| 596 TEST_F(ClientSideDetectionServiceTest, SetEnabled) { | 596 TEST_F(ClientSideDetectionServiceTest, SetEnabledAndRefreshState) { |
| 597 // Check that the model isn't downloaded until the service is enabled. | 597 // Check that the model isn't downloaded until the service is enabled. |
| 598 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | 598 csd_service_.reset(ClientSideDetectionService::Create(NULL)); |
| 599 EXPECT_FALSE(csd_service_->enabled()); | 599 EXPECT_FALSE(csd_service_->enabled()); |
| 600 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL); | 600 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL); |
| 601 | 601 |
| 602 // Use a MockClientSideDetectionService for the rest of the test, to avoid | 602 // Use a MockClientSideDetectionService for the rest of the test, to avoid |
| 603 // the scheduling delay. | 603 // the scheduling delay. |
| 604 MockClientSideDetectionService* service = | 604 MockClientSideDetectionService* service = |
| 605 new StrictMock<MockClientSideDetectionService>(); | 605 new StrictMock<MockClientSideDetectionService>(); |
| 606 csd_service_.reset(service); | 606 csd_service_.reset(service); |
| 607 EXPECT_FALSE(csd_service_->enabled()); | 607 EXPECT_FALSE(csd_service_->enabled()); |
| 608 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL); | 608 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL); |
| 609 // No calls expected yet. | 609 // No calls expected yet. |
| 610 Mock::VerifyAndClearExpectations(service); | 610 Mock::VerifyAndClearExpectations(service); |
| 611 | 611 |
| 612 ClientSideModel model; | 612 ClientSideModel model; |
| 613 model.set_version(10); | 613 model.set_version(10); |
| 614 model.set_max_words_per_term(4); | 614 model.set_max_words_per_term(4); |
| 615 SetModelFetchResponse(model.SerializeAsString(), true /* success */); | 615 SetModelFetchResponse(model.SerializeAsString(), true /* success */); |
| 616 EXPECT_CALL(*service, ScheduleFetchModel(_)) | 616 EXPECT_CALL(*service, ScheduleFetchModel(_)) |
| 617 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); | 617 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); |
| 618 EXPECT_CALL(*service, EndFetchModel( | 618 EXPECT_CALL(*service, EndFetchModel( |
| 619 ClientSideDetectionService::MODEL_SUCCESS)) | 619 ClientSideDetectionService::MODEL_SUCCESS)) |
| 620 .WillOnce(QuitCurrentMessageLoop()); | 620 .WillOnce(QuitCurrentMessageLoop()); |
| 621 csd_service_->SetEnabled(true); | 621 csd_service_->SetEnabledAndRefreshState(true); |
| 622 EXPECT_TRUE(csd_service_->model_fetcher_.get() != NULL); | 622 EXPECT_TRUE(csd_service_->model_fetcher_.get() != NULL); |
| 623 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 623 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
| 624 Mock::VerifyAndClearExpectations(service); | 624 Mock::VerifyAndClearExpectations(service); |
| 625 | 625 |
| 626 // Check that enabling again doesn't request the model. | 626 // Check that enabling again doesn't request the model. |
| 627 csd_service_->SetEnabled(true); | 627 csd_service_->SetEnabledAndRefreshState(true); |
| 628 // No calls expected. | 628 // No calls expected. |
| 629 Mock::VerifyAndClearExpectations(service); | 629 Mock::VerifyAndClearExpectations(service); |
| 630 | 630 |
| 631 // Check that disabling the service cancels pending requests. | 631 // Check that disabling the service cancels pending requests. |
| 632 EXPECT_CALL(*service, ScheduleFetchModel(_)) | 632 EXPECT_CALL(*service, ScheduleFetchModel(_)) |
| 633 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); | 633 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); |
| 634 csd_service_->SetEnabled(false); | 634 csd_service_->SetEnabledAndRefreshState(false); |
| 635 csd_service_->SetEnabled(true); | 635 csd_service_->SetEnabledAndRefreshState(true); |
| 636 Mock::VerifyAndClearExpectations(service); | 636 Mock::VerifyAndClearExpectations(service); |
| 637 EXPECT_TRUE(csd_service_->model_fetcher_.get() != NULL); | 637 EXPECT_TRUE(csd_service_->model_fetcher_.get() != NULL); |
| 638 csd_service_->SetEnabled(false); | 638 csd_service_->SetEnabledAndRefreshState(false); |
| 639 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL); | 639 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL); |
| 640 msg_loop_.RunAllPending(); | 640 msg_loop_.RunAllPending(); |
| 641 // No calls expected. | 641 // No calls expected. |
| 642 Mock::VerifyAndClearExpectations(service); | 642 Mock::VerifyAndClearExpectations(service); |
| 643 | 643 |
| 644 // Requests always return false when the service is disabled. | 644 // Requests always return false when the service is disabled. |
| 645 ClientPhishingResponse response; | 645 ClientPhishingResponse response; |
| 646 response.set_phishy(true); | 646 response.set_phishy(true); |
| 647 SetClientReportPhishingResponse(response.SerializeAsString(), | 647 SetClientReportPhishingResponse(response.SerializeAsString(), |
| 648 true /* success */); | 648 true /* success */); |
| 649 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f)); | 649 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f)); |
| 650 | 650 |
| 651 // Pending requests also return false if the service is disabled before they | 651 // Pending requests also return false if the service is disabled before they |
| 652 // report back. | 652 // report back. |
| 653 EXPECT_CALL(*service, ScheduleFetchModel(_)) | 653 EXPECT_CALL(*service, ScheduleFetchModel(_)) |
| 654 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); | 654 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); |
| 655 EXPECT_CALL(*service, EndFetchModel( | 655 EXPECT_CALL(*service, EndFetchModel( |
| 656 ClientSideDetectionService::MODEL_NOT_CHANGED)) | 656 ClientSideDetectionService::MODEL_NOT_CHANGED)) |
| 657 .WillOnce(Invoke(service, &MockClientSideDetectionService::Disable)); | 657 .WillOnce(Invoke(service, &MockClientSideDetectionService::Disable)); |
| 658 csd_service_->SetEnabled(true); | 658 csd_service_->SetEnabledAndRefreshState(true); |
| 659 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f)); | 659 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f)); |
| 660 Mock::VerifyAndClearExpectations(service); | 660 Mock::VerifyAndClearExpectations(service); |
| 661 } | 661 } |
| 662 | 662 |
| 663 TEST_F(ClientSideDetectionServiceTest, IsFalsePositiveResponse) { | 663 TEST_F(ClientSideDetectionServiceTest, IsFalsePositiveResponse) { |
| 664 GURL url("http://www.google.com/"); | 664 GURL url("http://www.google.com/"); |
| 665 ClientPhishingResponse response; | 665 ClientPhishingResponse response; |
| 666 | 666 |
| 667 // If the response is not phishing is should never be a false positive. | 667 // If the response is not phishing is should never be a false positive. |
| 668 response.set_phishy(false); | 668 response.set_phishy(false); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 686 EXPECT_TRUE(ClientSideDetectionService::IsFalsePositiveResponse( | 686 EXPECT_TRUE(ClientSideDetectionService::IsFalsePositiveResponse( |
| 687 url, response)); | 687 url, response)); |
| 688 | 688 |
| 689 // If an entry in the whitelist matches the URL it should return true. | 689 // If an entry in the whitelist matches the URL it should return true. |
| 690 response.clear_whitelist_expression(); | 690 response.clear_whitelist_expression(); |
| 691 response.add_whitelist_expression("www.google.com/a/b.html"); | 691 response.add_whitelist_expression("www.google.com/a/b.html"); |
| 692 EXPECT_TRUE(ClientSideDetectionService::IsFalsePositiveResponse( | 692 EXPECT_TRUE(ClientSideDetectionService::IsFalsePositiveResponse( |
| 693 url, response)); | 693 url, response)); |
| 694 } | 694 } |
| 695 } // namespace safe_browsing | 695 } // namespace safe_browsing |
| OLD | NEW |