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/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/test/histogram_tester.h" | 10 #include "base/test/histogram_tester.h" |
11 #include "base/test/null_task_runner.h" | 11 #include "base/test/null_task_runner.h" |
12 #include "components/content_settings/core/browser/host_content_settings_map.h" | 12 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 13 #include "components/safe_browsing/common/safebrowsing_constants.h" |
13 #include "components/safe_browsing/password_protection/password_protection_reque
st.h" | 14 #include "components/safe_browsing/password_protection/password_protection_reque
st.h" |
14 #include "components/safe_browsing_db/test_database_manager.h" | 15 #include "components/safe_browsing_db/test_database_manager.h" |
15 #include "components/sync_preferences/testing_pref_service_syncable.h" | 16 #include "components/sync_preferences/testing_pref_service_syncable.h" |
16 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
17 #include "net/url_request/test_url_fetcher_factory.h" | 18 #include "net/url_request/test_url_fetcher_factory.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) | 165 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) |
165 .WillRepeatedly(testing::Return(match_whitelist)); | 166 .WillRepeatedly(testing::Return(match_whitelist)); |
166 | 167 |
167 request_ = new PasswordProtectionRequest( | 168 request_ = new PasswordProtectionRequest( |
168 nullptr, target_url, GURL(kFormActionUrl), GURL(kPasswordFrameUrl), | 169 nullptr, target_url, GURL(kFormActionUrl), GURL(kPasswordFrameUrl), |
169 std::string(), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, | 170 std::string(), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
170 password_protection_service_.get(), timeout_in_ms); | 171 password_protection_service_.get(), timeout_in_ms); |
171 request_->Start(); | 172 request_->Start(); |
172 } | 173 } |
173 | 174 |
174 void InitializeAndStartPasswordEntryRequest(bool match_whitelist, | 175 void InitializeAndStartPasswordEntryRequest(const std::string& saved_domain, |
| 176 bool match_whitelist, |
175 int timeout_in_ms) { | 177 int timeout_in_ms) { |
176 GURL target_url(kTargetUrl); | 178 GURL target_url(kTargetUrl); |
177 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) | 179 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) |
178 .WillRepeatedly(testing::Return(match_whitelist)); | 180 .WillRepeatedly(testing::Return(match_whitelist)); |
179 | 181 |
180 request_ = new PasswordProtectionRequest( | 182 request_ = new PasswordProtectionRequest( |
181 nullptr, target_url, GURL(), GURL(), std::string(kSavedDomain), | 183 nullptr, target_url, GURL(), GURL(), saved_domain, |
182 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, | 184 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
183 password_protection_service_.get(), timeout_in_ms); | 185 password_protection_service_.get(), timeout_in_ms); |
184 request_->Start(); | 186 request_->Start(); |
185 } | 187 } |
186 | 188 |
187 bool PathVariantsMatchCacheExpression(const GURL& url, | 189 bool PathVariantsMatchCacheExpression(const GURL& url, |
188 const std::string& cache_expression) { | 190 const std::string& cache_expression) { |
189 std::vector<std::string> paths; | 191 std::vector<std::string> paths; |
190 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); | 192 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); |
191 return PasswordProtectionService::PathVariantsMatchCacheExpression( | 193 return PasswordProtectionService::PathVariantsMatchCacheExpression( |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
615 EXPECT_EQ(LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, | 617 EXPECT_EQ(LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
616 actual_request->trigger_type()); | 618 actual_request->trigger_type()); |
617 ASSERT_EQ(2, actual_request->frames_size()); | 619 ASSERT_EQ(2, actual_request->frames_size()); |
618 EXPECT_EQ(kTargetUrl, actual_request->frames(0).url()); | 620 EXPECT_EQ(kTargetUrl, actual_request->frames(0).url()); |
619 EXPECT_EQ(kPasswordFrameUrl, actual_request->frames(1).url()); | 621 EXPECT_EQ(kPasswordFrameUrl, actual_request->frames(1).url()); |
620 EXPECT_EQ(true, actual_request->frames(1).has_password_field()); | 622 EXPECT_EQ(true, actual_request->frames(1).has_password_field()); |
621 ASSERT_EQ(1, actual_request->frames(1).forms_size()); | 623 ASSERT_EQ(1, actual_request->frames(1).forms_size()); |
622 EXPECT_EQ(kFormActionUrl, actual_request->frames(1).forms(0).action_url()); | 624 EXPECT_EQ(kFormActionUrl, actual_request->frames(1).forms(0).action_url()); |
623 } | 625 } |
624 | 626 |
625 TEST_F(PasswordProtectionServiceTest, | 627 TEST_F(PasswordProtectionServiceTest, VerifyPasswordProtectionRequestProto) { |
626 VerifyProtectedPasswordEntryRequestProto) { | |
627 // Set up valid response. | 628 // Set up valid response. |
628 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); | 629 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); |
629 fetcher.set_status( | 630 fetcher.set_status( |
630 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 631 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
631 fetcher.set_response_code(200); | 632 fetcher.set_response_code(200); |
632 LoginReputationClientResponse expected_response = CreateVerdictProto( | 633 LoginReputationClientResponse expected_response = CreateVerdictProto( |
633 LoginReputationClientResponse::PHISHING, 600, GURL(kTargetUrl).host()); | 634 LoginReputationClientResponse::PHISHING, 600, GURL(kTargetUrl).host()); |
634 fetcher.SetResponseString(expected_response.SerializeAsString()); | 635 fetcher.SetResponseString(expected_response.SerializeAsString()); |
635 InitializeAndStartPasswordEntryRequest(false /* match whitelist */, | 636 // Initialize request triggered by chrome sync password reuse. |
| 637 InitializeAndStartPasswordEntryRequest(std::string(kSyncPasswordDomain), |
| 638 false /* match whitelist */, |
636 100000 /* timeout in ms*/); | 639 100000 /* timeout in ms*/); |
637 base::RunLoop().RunUntilIdle(); | 640 base::RunLoop().RunUntilIdle(); |
638 request_->OnURLFetchComplete(&fetcher); | 641 request_->OnURLFetchComplete(&fetcher); |
639 base::RunLoop().RunUntilIdle(); | 642 base::RunLoop().RunUntilIdle(); |
640 | 643 |
641 LoginReputationClientRequest* actual_request = | 644 LoginReputationClientRequest* actual_request = |
642 password_protection_service_->GetLatestRequestProto(); | 645 password_protection_service_->GetLatestRequestProto(); |
643 EXPECT_EQ(kTargetUrl, actual_request->page_url()); | 646 EXPECT_EQ(kTargetUrl, actual_request->page_url()); |
644 EXPECT_EQ(LoginReputationClientRequest::PASSWORD_REUSE_EVENT, | 647 EXPECT_EQ(LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
645 actual_request->trigger_type()); | 648 actual_request->trigger_type()); |
646 EXPECT_EQ(1, actual_request->frames_size()); | 649 EXPECT_EQ(1, actual_request->frames_size()); |
647 EXPECT_EQ(kTargetUrl, actual_request->frames(0).url()); | 650 EXPECT_EQ(kTargetUrl, actual_request->frames(0).url()); |
648 // TODO(jialiul): Update this test when we're ready to fill more fields. | 651 ASSERT_TRUE(actual_request->has_password_reuse_event()); |
649 ASSERT_FALSE(actual_request->has_password_reuse_event()); | 652 ASSERT_TRUE( |
| 653 actual_request->password_reuse_event().is_chrome_signin_password()); |
| 654 |
| 655 // Initialize request triggered by saved password reuse. |
| 656 InitializeAndStartPasswordEntryRequest(std::string(kSavedDomain), |
| 657 false /* match whitelist */, |
| 658 100000 /* timeout in ms*/); |
| 659 base::RunLoop().RunUntilIdle(); |
| 660 request_->OnURLFetchComplete(&fetcher); |
| 661 base::RunLoop().RunUntilIdle(); |
| 662 |
| 663 actual_request = password_protection_service_->GetLatestRequestProto(); |
| 664 ASSERT_TRUE(actual_request->has_password_reuse_event()); |
| 665 ASSERT_FALSE( |
| 666 actual_request->password_reuse_event().is_chrome_signin_password()); |
650 } | 667 } |
651 | 668 |
652 } // namespace safe_browsing | 669 } // namespace safe_browsing |
OLD | NEW |