| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #include "components/safe_browsing/password_protection/password_protection_servi
ce.h" | 4 #include "components/safe_browsing/password_protection/password_protection_servi
ce.h" |
| 5 | 5 |
| 6 #include "base/memory/ptr_util.h" | 6 #include "base/memory/ptr_util.h" |
| 7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/test/histogram_tester.h" | 9 #include "base/test/histogram_tester.h" |
| 10 #include "base/test/null_task_runner.h" | 10 #include "base/test/null_task_runner.h" |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 | 143 |
| 144 void TearDown() override { content_setting_map_->ShutdownOnUIThread(); } | 144 void TearDown() override { content_setting_map_->ShutdownOnUIThread(); } |
| 145 | 145 |
| 146 // Sets up |database_manager_| and |requests_| as needed. | 146 // Sets up |database_manager_| and |requests_| as needed. |
| 147 void InitializeAndStartRequest(bool match_whitelist, int timeout_in_ms) { | 147 void InitializeAndStartRequest(bool match_whitelist, int timeout_in_ms) { |
| 148 GURL target_url(kTargetUrl); | 148 GURL target_url(kTargetUrl); |
| 149 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) | 149 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) |
| 150 .WillRepeatedly(testing::Return(match_whitelist)); | 150 .WillRepeatedly(testing::Return(match_whitelist)); |
| 151 | 151 |
| 152 request_ = new PasswordProtectionRequest( | 152 request_ = new PasswordProtectionRequest( |
| 153 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, | 153 target_url, GURL(), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 154 nullptr, password_protection_service_.get(), timeout_in_ms); | 154 password_protection_service_.get(), timeout_in_ms); |
| 155 request_->Start(); | 155 request_->Start(); |
| 156 } | 156 } |
| 157 | 157 |
| 158 bool PathVariantsMatchCacheExpression(const GURL& url, | 158 bool PathVariantsMatchCacheExpression(const GURL& url, |
| 159 const std::string& cache_expression) { | 159 const std::string& cache_expression) { |
| 160 std::vector<std::string> paths; | 160 std::vector<std::string> paths; |
| 161 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); | 161 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); |
| 162 return PasswordProtectionService::PathVariantsMatchCacheExpression( | 162 return PasswordProtectionService::PathVariantsMatchCacheExpression( |
| 163 paths, | 163 paths, |
| 164 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); | 164 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 | 395 |
| 396 // If delete all history. All password protection content settings should be | 396 // If delete all history. All password protection content settings should be |
| 397 // gone. | 397 // gone. |
| 398 password_protection_service_->RemoveContentSettingsOnURLsDeleted( | 398 password_protection_service_->RemoveContentSettingsOnURLsDeleted( |
| 399 true /* all_history */, history::URLRows()); | 399 true /* all_history */, history::URLRows()); |
| 400 EXPECT_EQ(0U, GetStoredVerdictCount()); | 400 EXPECT_EQ(0U, GetStoredVerdictCount()); |
| 401 } | 401 } |
| 402 TEST_F(PasswordProtectionServiceTest, | 402 TEST_F(PasswordProtectionServiceTest, |
| 403 TestNoRequestCreatedIfMainFrameURLIsNotValid) { | 403 TestNoRequestCreatedIfMainFrameURLIsNotValid) { |
| 404 ASSERT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); | 404 ASSERT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); |
| 405 password_protection_service_->MaybeStartLowReputationRequest(GURL(), nullptr); | 405 password_protection_service_->MaybeStartLowReputationRequest( |
| 406 GURL(), GURL("http://foo.com/submit")); |
| 406 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); | 407 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); |
| 407 } | 408 } |
| 408 | 409 |
| 409 TEST_F(PasswordProtectionServiceTest, | 410 TEST_F(PasswordProtectionServiceTest, |
| 410 TestNoRequestCreatedIfMainFrameURLIsNotHttpOrHttps) { | 411 TestNoRequestCreatedIfMainFrameURLIsNotHttpOrHttps) { |
| 411 ASSERT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); | 412 ASSERT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); |
| 412 // If main frame url is data url, don't create request. | 413 // If main frame url is data url, don't create request. |
| 413 password_protection_service_->MaybeStartLowReputationRequest( | 414 password_protection_service_->MaybeStartLowReputationRequest( |
| 414 GURL("data:text/html, <p>hellow"), nullptr); | 415 GURL("data:text/html, <p>hellow"), GURL("http://foo.com/submit")); |
| 415 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); | 416 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); |
| 416 | 417 |
| 417 // If main frame url is ftp, don't create request. | 418 // If main frame url is ftp, don't create request. |
| 418 password_protection_service_->MaybeStartLowReputationRequest( | 419 password_protection_service_->MaybeStartLowReputationRequest( |
| 419 GURL("ftp://foo.com:21"), nullptr); | 420 GURL("ftp://foo.com:21"), GURL("http://foo.com/submit")); |
| 420 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); | 421 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); |
| 421 } | 422 } |
| 422 | 423 |
| 423 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForIncognito) { | 424 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForIncognito) { |
| 424 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 425 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); |
| 425 password_protection_service_->set_incognito(true); | 426 password_protection_service_->set_incognito(true); |
| 426 password_protection_service_->StartRequest( | 427 password_protection_service_->StartRequest( |
| 427 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, | 428 GURL(kTargetUrl), GURL("http://foo.com/submit"), |
| 428 nullptr); | 429 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); |
| 429 base::RunLoop().RunUntilIdle(); | 430 base::RunLoop().RunUntilIdle(); |
| 430 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); | 431 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); |
| 431 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), | 432 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), |
| 432 testing::ElementsAre(base::Bucket(7 /* INCOGNITO */, 1))); | 433 testing::ElementsAre(base::Bucket(7 /* INCOGNITO */, 1))); |
| 433 } | 434 } |
| 434 | 435 |
| 435 TEST_F(PasswordProtectionServiceTest, | 436 TEST_F(PasswordProtectionServiceTest, |
| 436 TestNoRequestSentForNonExtendedReporting) { | 437 TestNoRequestSentForNonExtendedReporting) { |
| 437 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 438 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); |
| 438 password_protection_service_->set_extended_reporting(false); | 439 password_protection_service_->set_extended_reporting(false); |
| 439 password_protection_service_->StartRequest( | 440 password_protection_service_->StartRequest( |
| 440 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, | 441 GURL(kTargetUrl), GURL("http://foo.com/submit"), |
| 441 nullptr); | 442 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); |
| 442 | 443 |
| 443 base::RunLoop().RunUntilIdle(); | 444 base::RunLoop().RunUntilIdle(); |
| 444 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); | 445 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); |
| 445 EXPECT_THAT( | 446 EXPECT_THAT( |
| 446 histograms_.GetAllSamples(kRequestOutcomeHistogramName), | 447 histograms_.GetAllSamples(kRequestOutcomeHistogramName), |
| 447 testing::ElementsAre(base::Bucket(6 /* NO_EXTENDED_REPORTING */, 1))); | 448 testing::ElementsAre(base::Bucket(6 /* NO_EXTENDED_REPORTING */, 1))); |
| 448 } | 449 } |
| 449 | 450 |
| 450 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForWhitelistedURL) { | 451 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForWhitelistedURL) { |
| 451 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 452 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 EXPECT_EQ(expected_response.cache_duration_sec(), | 542 EXPECT_EQ(expected_response.cache_duration_sec(), |
| 542 actual_response->cache_duration_sec()); | 543 actual_response->cache_duration_sec()); |
| 543 } | 544 } |
| 544 | 545 |
| 545 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { | 546 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { |
| 546 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 547 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); |
| 547 GURL target_url(kTargetUrl); | 548 GURL target_url(kTargetUrl); |
| 548 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) | 549 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) |
| 549 .WillRepeatedly(testing::Return(false)); | 550 .WillRepeatedly(testing::Return(false)); |
| 550 password_protection_service_->StartRequest( | 551 password_protection_service_->StartRequest( |
| 551 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, nullptr); | 552 target_url, GURL("http://foo.com/submit"), |
| 553 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); |
| 552 | 554 |
| 553 // Destroy password_protection_service_ while there is one request pending. | 555 // Destroy password_protection_service_ while there is one request pending. |
| 554 password_protection_service_.reset(); | 556 password_protection_service_.reset(); |
| 555 base::RunLoop().RunUntilIdle(); | 557 base::RunLoop().RunUntilIdle(); |
| 556 | 558 |
| 557 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), | 559 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), |
| 558 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); | 560 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); |
| 559 } | 561 } |
| 560 } // namespace safe_browsing | 562 } // namespace safe_browsing |
| OLD | NEW |