| 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(), GURL(), |
| 154 nullptr, password_protection_service_.get(), timeout_in_ms); | 154 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 155 password_protection_service_.get(), timeout_in_ms); |
| 155 request_->Start(); | 156 request_->Start(); |
| 156 } | 157 } |
| 157 | 158 |
| 158 bool PathVariantsMatchCacheExpression(const GURL& url, | 159 bool PathVariantsMatchCacheExpression(const GURL& url, |
| 159 const std::string& cache_expression) { | 160 const std::string& cache_expression) { |
| 160 std::vector<std::string> paths; | 161 std::vector<std::string> paths; |
| 161 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); | 162 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); |
| 162 return PasswordProtectionService::PathVariantsMatchCacheExpression( | 163 return PasswordProtectionService::PathVariantsMatchCacheExpression( |
| 163 paths, | 164 paths, |
| 164 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); | 165 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 | 396 |
| 396 // If delete all history. All password protection content settings should be | 397 // If delete all history. All password protection content settings should be |
| 397 // gone. | 398 // gone. |
| 398 password_protection_service_->RemoveContentSettingsOnURLsDeleted( | 399 password_protection_service_->RemoveContentSettingsOnURLsDeleted( |
| 399 true /* all_history */, history::URLRows()); | 400 true /* all_history */, history::URLRows()); |
| 400 EXPECT_EQ(0U, GetStoredVerdictCount()); | 401 EXPECT_EQ(0U, GetStoredVerdictCount()); |
| 401 } | 402 } |
| 402 TEST_F(PasswordProtectionServiceTest, | 403 TEST_F(PasswordProtectionServiceTest, |
| 403 TestNoRequestCreatedIfMainFrameURLIsNotValid) { | 404 TestNoRequestCreatedIfMainFrameURLIsNotValid) { |
| 404 ASSERT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); | 405 ASSERT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); |
| 405 password_protection_service_->MaybeStartLowReputationRequest(GURL(), nullptr); | 406 password_protection_service_->MaybeStartLowReputationRequest( |
| 407 GURL(), GURL("http://foo.com/submit"), GURL("http://foo.com/frame")); |
| 406 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); | 408 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); |
| 407 } | 409 } |
| 408 | 410 |
| 409 TEST_F(PasswordProtectionServiceTest, | 411 TEST_F(PasswordProtectionServiceTest, |
| 410 TestNoRequestCreatedIfMainFrameURLIsNotHttpOrHttps) { | 412 TestNoRequestCreatedIfMainFrameURLIsNotHttpOrHttps) { |
| 411 ASSERT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); | 413 ASSERT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); |
| 412 // If main frame url is data url, don't create request. | 414 // If main frame url is data url, don't create request. |
| 413 password_protection_service_->MaybeStartLowReputationRequest( | 415 password_protection_service_->MaybeStartLowReputationRequest( |
| 414 GURL("data:text/html, <p>hellow"), nullptr); | 416 GURL("data:text/html, <p>hellow"), GURL("http://foo.com/submit"), |
| 417 GURL("http://foo.com/frame")); |
| 415 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); | 418 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); |
| 416 | 419 |
| 417 // If main frame url is ftp, don't create request. | 420 // If main frame url is ftp, don't create request. |
| 418 password_protection_service_->MaybeStartLowReputationRequest( | 421 password_protection_service_->MaybeStartLowReputationRequest( |
| 419 GURL("ftp://foo.com:21"), nullptr); | 422 GURL("ftp://foo.com:21"), GURL("http://foo.com/submit"), |
| 423 GURL("http://foo.com/frame")); |
| 420 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); | 424 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); |
| 421 } | 425 } |
| 422 | 426 |
| 423 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForIncognito) { | 427 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForIncognito) { |
| 424 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 428 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); |
| 425 password_protection_service_->set_incognito(true); | 429 password_protection_service_->set_incognito(true); |
| 426 password_protection_service_->StartRequest( | 430 password_protection_service_->StartRequest( |
| 427 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, | 431 GURL(kTargetUrl), GURL("http://foo.com/submit"), |
| 428 nullptr); | 432 GURL("http://foo.com/frame"), |
| 433 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); |
| 429 base::RunLoop().RunUntilIdle(); | 434 base::RunLoop().RunUntilIdle(); |
| 430 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); | 435 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); |
| 431 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), | 436 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), |
| 432 testing::ElementsAre(base::Bucket(7 /* INCOGNITO */, 1))); | 437 testing::ElementsAre(base::Bucket(7 /* INCOGNITO */, 1))); |
| 433 } | 438 } |
| 434 | 439 |
| 435 TEST_F(PasswordProtectionServiceTest, | 440 TEST_F(PasswordProtectionServiceTest, |
| 436 TestNoRequestSentForNonExtendedReporting) { | 441 TestNoRequestSentForNonExtendedReporting) { |
| 437 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 442 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); |
| 438 password_protection_service_->set_extended_reporting(false); | 443 password_protection_service_->set_extended_reporting(false); |
| 439 password_protection_service_->StartRequest( | 444 password_protection_service_->StartRequest( |
| 440 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, | 445 GURL(kTargetUrl), GURL("http://foo.com/submit"), |
| 441 nullptr); | 446 GURL("http://foo.com/frame"), |
| 447 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); |
| 442 | 448 |
| 443 base::RunLoop().RunUntilIdle(); | 449 base::RunLoop().RunUntilIdle(); |
| 444 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); | 450 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); |
| 445 EXPECT_THAT( | 451 EXPECT_THAT( |
| 446 histograms_.GetAllSamples(kRequestOutcomeHistogramName), | 452 histograms_.GetAllSamples(kRequestOutcomeHistogramName), |
| 447 testing::ElementsAre(base::Bucket(6 /* NO_EXTENDED_REPORTING */, 1))); | 453 testing::ElementsAre(base::Bucket(6 /* NO_EXTENDED_REPORTING */, 1))); |
| 448 } | 454 } |
| 449 | 455 |
| 450 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForWhitelistedURL) { | 456 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForWhitelistedURL) { |
| 451 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 457 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(), | 547 EXPECT_EQ(expected_response.cache_duration_sec(), |
| 542 actual_response->cache_duration_sec()); | 548 actual_response->cache_duration_sec()); |
| 543 } | 549 } |
| 544 | 550 |
| 545 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { | 551 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { |
| 546 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 552 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); |
| 547 GURL target_url(kTargetUrl); | 553 GURL target_url(kTargetUrl); |
| 548 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) | 554 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) |
| 549 .WillRepeatedly(testing::Return(false)); | 555 .WillRepeatedly(testing::Return(false)); |
| 550 password_protection_service_->StartRequest( | 556 password_protection_service_->StartRequest( |
| 551 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, nullptr); | 557 target_url, GURL("http://foo.com/submit"), GURL("http://foo.com/frame"), |
| 558 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); |
| 552 | 559 |
| 553 // Destroy password_protection_service_ while there is one request pending. | 560 // Destroy password_protection_service_ while there is one request pending. |
| 554 password_protection_service_.reset(); | 561 password_protection_service_.reset(); |
| 555 base::RunLoop().RunUntilIdle(); | 562 base::RunLoop().RunUntilIdle(); |
| 556 | 563 |
| 557 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), | 564 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), |
| 558 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); | 565 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); |
| 559 } | 566 } |
| 560 } // namespace safe_browsing | 567 } // namespace safe_browsing |
| OLD | NEW |