| 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/password_protection/password_protection_reque
st.h" | 13 #include "components/safe_browsing/password_protection/password_protection_reque
st.h" |
| 14 #include "components/safe_browsing_db/test_database_manager.h" | 14 #include "components/safe_browsing_db/test_database_manager.h" |
| 15 #include "components/sync_preferences/testing_pref_service_syncable.h" | 15 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 16 #include "content/public/test/test_browser_thread_bundle.h" | 16 #include "content/public/test/test_browser_thread_bundle.h" |
| 17 #include "net/url_request/test_url_fetcher_factory.h" | 17 #include "net/url_request/test_url_fetcher_factory.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 const char kPasswordReuseMatchWhitelistHistogramName[] = | 23 const char kFormActionUrl[] = "https://form_action.com/"; |
| 24 "PasswordManager.PasswordReuse.MainFrameMatchCsdWhitelist"; | 24 const char kPasswordFrameUrl[] = "https://password_frame.com/"; |
| 25 const char kWhitelistedUrl[] = "http://inwhitelist.com"; | 25 const char kSavedDomain[] = "saved_domain.com"; |
| 26 const char kNoneWhitelistedUrl[] = "http://notinwhitelist.com"; | 26 const char kTargetUrl[] = "http://foo.com/"; |
| 27 const char kTargetUrl[] = "http://foo.com"; | |
| 28 const char kVerdictHistogramName[] = | 27 const char kVerdictHistogramName[] = |
| 29 "PasswordProtection.Verdict.PasswordFieldOnFocus"; | 28 "PasswordProtection.Verdict.PasswordFieldOnFocus"; |
| 30 | 29 |
| 31 } // namespace | 30 } // namespace |
| 32 | 31 |
| 33 namespace safe_browsing { | 32 namespace safe_browsing { |
| 34 | 33 |
| 35 class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { | 34 class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { |
| 36 public: | 35 public: |
| 37 MockSafeBrowsingDatabaseManager() {} | 36 MockSafeBrowsingDatabaseManager() {} |
| (...skipping 29 matching lines...) Expand all Loading... |
| 67 public: | 66 public: |
| 68 TestPasswordProtectionService( | 67 TestPasswordProtectionService( |
| 69 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager, | 68 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager, |
| 70 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 69 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 71 scoped_refptr<HostContentSettingsMap> content_setting_map) | 70 scoped_refptr<HostContentSettingsMap> content_setting_map) |
| 72 : PasswordProtectionService(database_manager, | 71 : PasswordProtectionService(database_manager, |
| 73 request_context_getter, | 72 request_context_getter, |
| 74 nullptr, | 73 nullptr, |
| 75 content_setting_map.get()), | 74 content_setting_map.get()), |
| 76 is_extended_reporting_(true), | 75 is_extended_reporting_(true), |
| 77 is_incognito_(false) {} | 76 is_incognito_(false), |
| 77 latest_request_(nullptr) {} |
| 78 | 78 |
| 79 void RequestFinished( | 79 void RequestFinished( |
| 80 PasswordProtectionRequest* request, | 80 PasswordProtectionRequest* request, |
| 81 std::unique_ptr<LoginReputationClientResponse> response) override { | 81 std::unique_ptr<LoginReputationClientResponse> response) override { |
| 82 latest_request_ = request; |
| 82 latest_response_ = std::move(response); | 83 latest_response_ = std::move(response); |
| 83 } | 84 } |
| 84 | 85 |
| 85 // Since referrer chain logic has been thoroughly tested in | 86 // Since referrer chain logic has been thoroughly tested in |
| 86 // SBNavigationObserverBrowserTest class, we intentionally leave this function | 87 // SBNavigationObserverBrowserTest class, we intentionally leave this function |
| 87 // as a no-op here. | 88 // as a no-op here. |
| 88 void FillReferrerChain(const GURL& event_url, | 89 void FillReferrerChain(const GURL& event_url, |
| 89 int event_tab_id, | 90 int event_tab_id, |
| 90 LoginReputationClientRequest::Frame* frame) override {} | 91 LoginReputationClientRequest::Frame* frame) override {} |
| 91 | 92 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 107 bool IsHistorySyncEnabled() override { return false; } | 108 bool IsHistorySyncEnabled() override { return false; } |
| 108 | 109 |
| 109 LoginReputationClientResponse* latest_response() { | 110 LoginReputationClientResponse* latest_response() { |
| 110 return latest_response_.get(); | 111 return latest_response_.get(); |
| 111 } | 112 } |
| 112 | 113 |
| 113 ~TestPasswordProtectionService() override {} | 114 ~TestPasswordProtectionService() override {} |
| 114 | 115 |
| 115 size_t GetPendingRequestsCount() { return requests_.size(); } | 116 size_t GetPendingRequestsCount() { return requests_.size(); } |
| 116 | 117 |
| 118 LoginReputationClientRequest* GetLatestRequestProto() { |
| 119 return latest_request_ ? latest_request_->request_proto() : nullptr; |
| 120 } |
| 121 |
| 117 private: | 122 private: |
| 118 bool is_extended_reporting_; | 123 bool is_extended_reporting_; |
| 119 bool is_incognito_; | 124 bool is_incognito_; |
| 125 PasswordProtectionRequest* latest_request_; |
| 120 std::unique_ptr<LoginReputationClientResponse> latest_response_; | 126 std::unique_ptr<LoginReputationClientResponse> latest_response_; |
| 121 DISALLOW_COPY_AND_ASSIGN(TestPasswordProtectionService); | 127 DISALLOW_COPY_AND_ASSIGN(TestPasswordProtectionService); |
| 122 }; | 128 }; |
| 123 | 129 |
| 124 class PasswordProtectionServiceTest : public testing::Test { | 130 class PasswordProtectionServiceTest : public testing::Test { |
| 125 public: | 131 public: |
| 126 PasswordProtectionServiceTest(){}; | 132 PasswordProtectionServiceTest(){}; |
| 127 | 133 |
| 128 LoginReputationClientResponse CreateVerdictProto( | 134 LoginReputationClientResponse CreateVerdictProto( |
| 129 LoginReputationClientResponse::VerdictType verdict, | 135 LoginReputationClientResponse::VerdictType verdict, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 145 dummy_request_context_getter_ = new DummyURLRequestContextGetter(); | 151 dummy_request_context_getter_ = new DummyURLRequestContextGetter(); |
| 146 password_protection_service_ = | 152 password_protection_service_ = |
| 147 base::MakeUnique<TestPasswordProtectionService>( | 153 base::MakeUnique<TestPasswordProtectionService>( |
| 148 database_manager_, dummy_request_context_getter_, | 154 database_manager_, dummy_request_context_getter_, |
| 149 content_setting_map_); | 155 content_setting_map_); |
| 150 } | 156 } |
| 151 | 157 |
| 152 void TearDown() override { content_setting_map_->ShutdownOnUIThread(); } | 158 void TearDown() override { content_setting_map_->ShutdownOnUIThread(); } |
| 153 | 159 |
| 154 // Sets up |database_manager_| and |requests_| as needed. | 160 // Sets up |database_manager_| and |requests_| as needed. |
| 155 void InitializeAndStartRequest(bool match_whitelist, int timeout_in_ms) { | 161 void InitializeAndStartPasswordOnFocusRequest(bool match_whitelist, |
| 162 int timeout_in_ms) { |
| 156 GURL target_url(kTargetUrl); | 163 GURL target_url(kTargetUrl); |
| 157 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) | 164 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) |
| 158 .WillRepeatedly(testing::Return(match_whitelist)); | 165 .WillRepeatedly(testing::Return(match_whitelist)); |
| 159 | 166 |
| 160 request_ = new PasswordProtectionRequest( | 167 request_ = new PasswordProtectionRequest( |
| 161 target_url, GURL(), GURL(), | 168 target_url, GURL(kFormActionUrl), GURL(kPasswordFrameUrl), |
| 162 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, | 169 std::string(), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 163 password_protection_service_.get(), timeout_in_ms); | 170 password_protection_service_.get(), timeout_in_ms); |
| 164 request_->Start(); | 171 request_->Start(); |
| 165 } | 172 } |
| 173 |
| 174 void InitializeAndStartPasswordEntryRequest(bool match_whitelist, |
| 175 int timeout_in_ms) { |
| 176 GURL target_url(kTargetUrl); |
| 177 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) |
| 178 .WillRepeatedly(testing::Return(match_whitelist)); |
| 179 |
| 180 request_ = new PasswordProtectionRequest( |
| 181 target_url, GURL(), GURL(), std::string(kSavedDomain), |
| 182 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 183 password_protection_service_.get(), timeout_in_ms); |
| 184 request_->Start(); |
| 185 } |
| 166 | 186 |
| 167 bool PathVariantsMatchCacheExpression(const GURL& url, | 187 bool PathVariantsMatchCacheExpression(const GURL& url, |
| 168 const std::string& cache_expression) { | 188 const std::string& cache_expression) { |
| 169 std::vector<std::string> paths; | 189 std::vector<std::string> paths; |
| 170 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); | 190 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); |
| 171 return PasswordProtectionService::PathVariantsMatchCacheExpression( | 191 return PasswordProtectionService::PathVariantsMatchCacheExpression( |
| 172 paths, | 192 paths, |
| 173 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); | 193 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); |
| 174 } | 194 } |
| 175 | 195 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 194 content::TestBrowserThreadBundle thread_bundle_; | 214 content::TestBrowserThreadBundle thread_bundle_; |
| 195 scoped_refptr<MockSafeBrowsingDatabaseManager> database_manager_; | 215 scoped_refptr<MockSafeBrowsingDatabaseManager> database_manager_; |
| 196 sync_preferences::TestingPrefServiceSyncable test_pref_service_; | 216 sync_preferences::TestingPrefServiceSyncable test_pref_service_; |
| 197 scoped_refptr<HostContentSettingsMap> content_setting_map_; | 217 scoped_refptr<HostContentSettingsMap> content_setting_map_; |
| 198 scoped_refptr<DummyURLRequestContextGetter> dummy_request_context_getter_; | 218 scoped_refptr<DummyURLRequestContextGetter> dummy_request_context_getter_; |
| 199 std::unique_ptr<TestPasswordProtectionService> password_protection_service_; | 219 std::unique_ptr<TestPasswordProtectionService> password_protection_service_; |
| 200 scoped_refptr<PasswordProtectionRequest> request_; | 220 scoped_refptr<PasswordProtectionRequest> request_; |
| 201 base::HistogramTester histograms_; | 221 base::HistogramTester histograms_; |
| 202 }; | 222 }; |
| 203 | 223 |
| 204 TEST_F(PasswordProtectionServiceTest, | |
| 205 TestPasswordReuseMatchWhitelistHistogram) { | |
| 206 const GURL whitelisted_url(kWhitelistedUrl); | |
| 207 const GURL not_whitelisted_url(kNoneWhitelistedUrl); | |
| 208 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(whitelisted_url)) | |
| 209 .WillOnce(testing::Return(true)); | |
| 210 EXPECT_CALL(*database_manager_.get(), | |
| 211 MatchCsdWhitelistUrl(not_whitelisted_url)) | |
| 212 .WillOnce(testing::Return(false)); | |
| 213 histograms_.ExpectTotalCount(kPasswordReuseMatchWhitelistHistogramName, 0); | |
| 214 | |
| 215 // Empty url should increase "True" bucket by 1. | |
| 216 password_protection_service_->RecordPasswordReuse(GURL()); | |
| 217 base::RunLoop().RunUntilIdle(); | |
| 218 EXPECT_THAT( | |
| 219 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName), | |
| 220 testing::ElementsAre(base::Bucket(1, 1))); | |
| 221 | |
| 222 // Whitelisted url should increase "True" bucket by 1. | |
| 223 password_protection_service_->RecordPasswordReuse(whitelisted_url); | |
| 224 base::RunLoop().RunUntilIdle(); | |
| 225 EXPECT_THAT( | |
| 226 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName), | |
| 227 testing::ElementsAre(base::Bucket(1, 2))); | |
| 228 | |
| 229 // Non-whitelisted url should increase "False" bucket by 1. | |
| 230 password_protection_service_->RecordPasswordReuse(not_whitelisted_url); | |
| 231 base::RunLoop().RunUntilIdle(); | |
| 232 EXPECT_THAT( | |
| 233 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName), | |
| 234 testing::ElementsAre(base::Bucket(0, 1), base::Bucket(1, 2))); | |
| 235 } | |
| 236 | |
| 237 TEST_F(PasswordProtectionServiceTest, TestParseInvalidVerdictEntry) { | 224 TEST_F(PasswordProtectionServiceTest, TestParseInvalidVerdictEntry) { |
| 238 std::unique_ptr<base::DictionaryValue> invalid_verdict_entry = | 225 std::unique_ptr<base::DictionaryValue> invalid_verdict_entry = |
| 239 base::MakeUnique<base::DictionaryValue>(); | 226 base::MakeUnique<base::DictionaryValue>(); |
| 240 invalid_verdict_entry->SetString("cache_creation_time", "invalid_time"); | 227 invalid_verdict_entry->SetString("cache_creation_time", "invalid_time"); |
| 241 | 228 |
| 242 int cache_creation_time; | 229 int cache_creation_time; |
| 243 LoginReputationClientResponse response; | 230 LoginReputationClientResponse response; |
| 244 // ParseVerdictEntry fails if input is empty. | 231 // ParseVerdictEntry fails if input is empty. |
| 245 EXPECT_FALSE(PasswordProtectionService::ParseVerdictEntry( | 232 EXPECT_FALSE(PasswordProtectionService::ParseVerdictEntry( |
| 246 nullptr, &cache_creation_time, &response)); | 233 nullptr, &cache_creation_time, &response)); |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 | 415 |
| 429 // If main frame url is ftp, don't create request. | 416 // If main frame url is ftp, don't create request. |
| 430 password_protection_service_->MaybeStartPasswordFieldOnFocusRequest( | 417 password_protection_service_->MaybeStartPasswordFieldOnFocusRequest( |
| 431 GURL("ftp://foo.com:21"), GURL("http://foo.com/submit"), | 418 GURL("ftp://foo.com:21"), GURL("http://foo.com/submit"), |
| 432 GURL("http://foo.com/frame")); | 419 GURL("http://foo.com/frame")); |
| 433 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); | 420 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount()); |
| 434 } | 421 } |
| 435 | 422 |
| 436 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForWhitelistedURL) { | 423 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForWhitelistedURL) { |
| 437 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); | 424 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); |
| 438 InitializeAndStartRequest(true /* match whitelist */, | 425 InitializeAndStartPasswordOnFocusRequest(true /* match whitelist */, |
| 439 10000 /* timeout in ms*/); | 426 10000 /* timeout in ms*/); |
| 440 base::RunLoop().RunUntilIdle(); | 427 base::RunLoop().RunUntilIdle(); |
| 441 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); | 428 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); |
| 442 EXPECT_THAT( | 429 EXPECT_THAT( |
| 443 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), | 430 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), |
| 444 testing::ElementsAre(base::Bucket(4 /* MATCHED_WHITELIST */, 1))); | 431 testing::ElementsAre(base::Bucket(4 /* MATCHED_WHITELIST */, 1))); |
| 445 } | 432 } |
| 446 | 433 |
| 447 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentIfVerdictAlreadyCached) { | 434 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentIfVerdictAlreadyCached) { |
| 448 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); | 435 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); |
| 449 CacheVerdict(GURL(kTargetUrl), LoginReputationClientResponse::LOW_REPUTATION, | 436 CacheVerdict(GURL(kTargetUrl), LoginReputationClientResponse::LOW_REPUTATION, |
| 450 600, GURL(kTargetUrl).host(), base::Time::Now()); | 437 600, GURL(kTargetUrl).host(), base::Time::Now()); |
| 451 InitializeAndStartRequest(false /* match whitelist */, | 438 InitializeAndStartPasswordOnFocusRequest(false /* match whitelist */, |
| 452 10000 /* timeout in ms*/); | 439 10000 /* timeout in ms*/); |
| 453 base::RunLoop().RunUntilIdle(); | 440 base::RunLoop().RunUntilIdle(); |
| 454 EXPECT_THAT( | 441 EXPECT_THAT( |
| 455 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), | 442 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), |
| 456 testing::ElementsAre(base::Bucket(5 /* RESPONSE_ALREADY_CACHED */, 1))); | 443 testing::ElementsAre(base::Bucket(5 /* RESPONSE_ALREADY_CACHED */, 1))); |
| 457 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, | 444 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, |
| 458 password_protection_service_->latest_response()->verdict_type()); | 445 password_protection_service_->latest_response()->verdict_type()); |
| 459 } | 446 } |
| 460 | 447 |
| 461 TEST_F(PasswordProtectionServiceTest, TestResponseFetchFailed) { | 448 TEST_F(PasswordProtectionServiceTest, TestResponseFetchFailed) { |
| 462 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); | 449 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); |
| 463 net::TestURLFetcher failed_fetcher(0, GURL("http://bar.com"), nullptr); | 450 net::TestURLFetcher failed_fetcher(0, GURL("http://bar.com"), nullptr); |
| 464 // Set up failed response. | 451 // Set up failed response. |
| 465 failed_fetcher.set_status( | 452 failed_fetcher.set_status( |
| 466 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED)); | 453 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED)); |
| 467 | 454 |
| 468 InitializeAndStartRequest(false /* match whitelist */, | 455 InitializeAndStartPasswordOnFocusRequest(false /* match whitelist */, |
| 469 10000 /* timeout in ms*/); | 456 10000 /* timeout in ms*/); |
| 470 request_->OnURLFetchComplete(&failed_fetcher); | 457 request_->OnURLFetchComplete(&failed_fetcher); |
| 471 base::RunLoop().RunUntilIdle(); | 458 base::RunLoop().RunUntilIdle(); |
| 472 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); | 459 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); |
| 473 EXPECT_THAT( | 460 EXPECT_THAT( |
| 474 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), | 461 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), |
| 475 testing::ElementsAre(base::Bucket(9 /* FETCH_FAILED */, 1))); | 462 testing::ElementsAre(base::Bucket(9 /* FETCH_FAILED */, 1))); |
| 476 } | 463 } |
| 477 | 464 |
| 478 TEST_F(PasswordProtectionServiceTest, TestMalformedResponse) { | 465 TEST_F(PasswordProtectionServiceTest, TestMalformedResponse) { |
| 479 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); | 466 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); |
| 480 // Set up malformed response. | 467 // Set up malformed response. |
| 481 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); | 468 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); |
| 482 fetcher.set_status( | 469 fetcher.set_status( |
| 483 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 470 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| 484 fetcher.set_response_code(200); | 471 fetcher.set_response_code(200); |
| 485 fetcher.SetResponseString("invalid response"); | 472 fetcher.SetResponseString("invalid response"); |
| 486 | 473 |
| 487 InitializeAndStartRequest(false /* match whitelist */, | 474 InitializeAndStartPasswordOnFocusRequest(false /* match whitelist */, |
| 488 10000 /* timeout in ms*/); | 475 10000 /* timeout in ms*/); |
| 489 request_->OnURLFetchComplete(&fetcher); | 476 request_->OnURLFetchComplete(&fetcher); |
| 490 base::RunLoop().RunUntilIdle(); | 477 base::RunLoop().RunUntilIdle(); |
| 491 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); | 478 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); |
| 492 EXPECT_THAT( | 479 EXPECT_THAT( |
| 493 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), | 480 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), |
| 494 testing::ElementsAre(base::Bucket(10 /* RESPONSE_MALFORMED */, 1))); | 481 testing::ElementsAre(base::Bucket(10 /* RESPONSE_MALFORMED */, 1))); |
| 495 } | 482 } |
| 496 | 483 |
| 497 TEST_F(PasswordProtectionServiceTest, TestRequestTimedout) { | 484 TEST_F(PasswordProtectionServiceTest, TestRequestTimedout) { |
| 498 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); | 485 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); |
| 499 InitializeAndStartRequest(false /* match whitelist */, | 486 InitializeAndStartPasswordOnFocusRequest(false /* match whitelist */, |
| 500 0 /* timeout immediately */); | 487 0 /* timeout immediately */); |
| 501 base::RunLoop().RunUntilIdle(); | 488 base::RunLoop().RunUntilIdle(); |
| 502 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); | 489 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); |
| 503 EXPECT_THAT( | 490 EXPECT_THAT( |
| 504 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), | 491 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), |
| 505 testing::ElementsAre(base::Bucket(3 /* TIMEDOUT */, 1))); | 492 testing::ElementsAre(base::Bucket(3 /* TIMEDOUT */, 1))); |
| 506 } | 493 } |
| 507 | 494 |
| 508 TEST_F(PasswordProtectionServiceTest, TestRequestAndResponseSuccessfull) { | 495 TEST_F(PasswordProtectionServiceTest, TestRequestAndResponseSuccessfull) { |
| 509 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); | 496 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); |
| 510 // Set up valid response. | 497 // Set up valid response. |
| 511 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); | 498 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); |
| 512 fetcher.set_status( | 499 fetcher.set_status( |
| 513 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 500 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| 514 fetcher.set_response_code(200); | 501 fetcher.set_response_code(200); |
| 515 LoginReputationClientResponse expected_response = CreateVerdictProto( | 502 LoginReputationClientResponse expected_response = CreateVerdictProto( |
| 516 LoginReputationClientResponse::PHISHING, 600, GURL(kTargetUrl).host()); | 503 LoginReputationClientResponse::PHISHING, 600, GURL(kTargetUrl).host()); |
| 517 fetcher.SetResponseString(expected_response.SerializeAsString()); | 504 fetcher.SetResponseString(expected_response.SerializeAsString()); |
| 518 | 505 |
| 519 InitializeAndStartRequest(false /* match whitelist */, | 506 InitializeAndStartPasswordOnFocusRequest(false /* match whitelist */, |
| 520 10000 /* timeout in ms*/); | 507 10000 /* timeout in ms*/); |
| 521 request_->OnURLFetchComplete(&fetcher); | 508 request_->OnURLFetchComplete(&fetcher); |
| 522 base::RunLoop().RunUntilIdle(); | 509 base::RunLoop().RunUntilIdle(); |
| 523 EXPECT_THAT( | 510 EXPECT_THAT( |
| 524 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), | 511 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), |
| 525 testing::ElementsAre(base::Bucket(1 /* SUCCEEDED */, 1))); | 512 testing::ElementsAre(base::Bucket(1 /* SUCCEEDED */, 1))); |
| 526 EXPECT_THAT(histograms_.GetAllSamples(kVerdictHistogramName), | 513 EXPECT_THAT(histograms_.GetAllSamples(kVerdictHistogramName), |
| 527 testing::ElementsAre(base::Bucket(3 /* PHISHING */, 1))); | 514 testing::ElementsAre(base::Bucket(3 /* PHISHING */, 1))); |
| 528 LoginReputationClientResponse* actual_response = | 515 LoginReputationClientResponse* actual_response = |
| 529 password_protection_service_->latest_response(); | 516 password_protection_service_->latest_response(); |
| 530 EXPECT_EQ(expected_response.verdict_type(), actual_response->verdict_type()); | 517 EXPECT_EQ(expected_response.verdict_type(), actual_response->verdict_type()); |
| 531 EXPECT_EQ(expected_response.cache_expression(), | 518 EXPECT_EQ(expected_response.cache_expression(), |
| 532 actual_response->cache_expression()); | 519 actual_response->cache_expression()); |
| 533 EXPECT_EQ(expected_response.cache_duration_sec(), | 520 EXPECT_EQ(expected_response.cache_duration_sec(), |
| 534 actual_response->cache_duration_sec()); | 521 actual_response->cache_duration_sec()); |
| 535 } | 522 } |
| 536 | 523 |
| 537 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { | 524 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { |
| 538 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); | 525 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); |
| 539 GURL target_url(kTargetUrl); | 526 GURL target_url(kTargetUrl); |
| 540 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) | 527 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) |
| 541 .WillRepeatedly(testing::Return(false)); | 528 .WillRepeatedly(testing::Return(false)); |
| 542 password_protection_service_->StartRequest( | 529 password_protection_service_->StartRequest( |
| 543 target_url, GURL("http://foo.com/submit"), GURL("http://foo.com/frame"), | 530 target_url, GURL("http://foo.com/submit"), GURL("http://foo.com/frame"), |
| 544 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); | 531 std::string(), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); |
| 545 | 532 |
| 546 // Destroy password_protection_service_ while there is one request pending. | 533 // Destroy password_protection_service_ while there is one request pending. |
| 547 password_protection_service_.reset(); | 534 password_protection_service_.reset(); |
| 548 base::RunLoop().RunUntilIdle(); | 535 base::RunLoop().RunUntilIdle(); |
| 549 | 536 |
| 550 EXPECT_THAT( | 537 EXPECT_THAT( |
| 551 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), | 538 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), |
| 552 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); | 539 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); |
| 553 } | 540 } |
| 554 | 541 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 583 // No cached verdict for foo.com/def. | 570 // No cached verdict for foo.com/def. |
| 584 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | 571 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 585 password_protection_service_->GetCachedVerdict( | 572 password_protection_service_->GetCachedVerdict( |
| 586 GURL("https://foo.com/def/index.jsp"), &actual_verdict)); | 573 GURL("https://foo.com/def/index.jsp"), &actual_verdict)); |
| 587 // Nothing in content setting for bar.com. | 574 // Nothing in content setting for bar.com. |
| 588 EXPECT_EQ(nullptr, content_setting_map_->GetWebsiteSetting( | 575 EXPECT_EQ(nullptr, content_setting_map_->GetWebsiteSetting( |
| 589 GURL("https://bar.com"), GURL(), | 576 GURL("https://bar.com"), GURL(), |
| 590 CONTENT_SETTINGS_TYPE_PASSWORD_PROTECTION, | 577 CONTENT_SETTINGS_TYPE_PASSWORD_PROTECTION, |
| 591 std::string(), nullptr)); | 578 std::string(), nullptr)); |
| 592 } | 579 } |
| 580 |
| 581 TEST_F(PasswordProtectionServiceTest, VerifyPasswordOnFocusRequestProto) { |
| 582 // Set up valid response. |
| 583 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); |
| 584 fetcher.set_status( |
| 585 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| 586 fetcher.set_response_code(200); |
| 587 LoginReputationClientResponse expected_response = CreateVerdictProto( |
| 588 LoginReputationClientResponse::PHISHING, 600, GURL(kTargetUrl).host()); |
| 589 fetcher.SetResponseString(expected_response.SerializeAsString()); |
| 590 |
| 591 InitializeAndStartPasswordOnFocusRequest(false /* match whitelist */, |
| 592 100000 /* timeout in ms*/); |
| 593 base::RunLoop().RunUntilIdle(); |
| 594 request_->OnURLFetchComplete(&fetcher); |
| 595 base::RunLoop().RunUntilIdle(); |
| 596 |
| 597 LoginReputationClientRequest* actual_request = |
| 598 password_protection_service_->GetLatestRequestProto(); |
| 599 EXPECT_EQ(kTargetUrl, actual_request->page_url()); |
| 600 EXPECT_EQ(LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 601 actual_request->trigger_type()); |
| 602 ASSERT_EQ(2, actual_request->frames_size()); |
| 603 EXPECT_EQ(kTargetUrl, actual_request->frames(0).url()); |
| 604 EXPECT_EQ(kPasswordFrameUrl, actual_request->frames(1).url()); |
| 605 EXPECT_EQ(true, actual_request->frames(1).has_password_field()); |
| 606 ASSERT_EQ(1, actual_request->frames(1).forms_size()); |
| 607 EXPECT_EQ(kFormActionUrl, actual_request->frames(1).forms(0).action_url()); |
| 608 } |
| 609 |
| 610 TEST_F(PasswordProtectionServiceTest, |
| 611 VerifyProtectedPasswordEntryRequestProto) { |
| 612 // Set up valid response. |
| 613 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); |
| 614 fetcher.set_status( |
| 615 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| 616 fetcher.set_response_code(200); |
| 617 LoginReputationClientResponse expected_response = CreateVerdictProto( |
| 618 LoginReputationClientResponse::PHISHING, 600, GURL(kTargetUrl).host()); |
| 619 fetcher.SetResponseString(expected_response.SerializeAsString()); |
| 620 InitializeAndStartPasswordEntryRequest(false /* match whitelist */, |
| 621 100000 /* timeout in ms*/); |
| 622 base::RunLoop().RunUntilIdle(); |
| 623 request_->OnURLFetchComplete(&fetcher); |
| 624 base::RunLoop().RunUntilIdle(); |
| 625 |
| 626 LoginReputationClientRequest* actual_request = |
| 627 password_protection_service_->GetLatestRequestProto(); |
| 628 EXPECT_EQ(kTargetUrl, actual_request->page_url()); |
| 629 EXPECT_EQ(LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 630 actual_request->trigger_type()); |
| 631 EXPECT_EQ(1, actual_request->frames_size()); |
| 632 EXPECT_EQ(kTargetUrl, actual_request->frames(0).url()); |
| 633 ASSERT_TRUE(actual_request->has_password_reuse_event()); |
| 634 ASSERT_EQ(1, actual_request->password_reuse_event() |
| 635 .password_reused_original_origins_size()); |
| 636 EXPECT_EQ(kSavedDomain, actual_request->password_reuse_event() |
| 637 .password_reused_original_origins(0)); |
| 638 } |
| 639 |
| 593 } // namespace safe_browsing | 640 } // namespace safe_browsing |
| OLD | NEW |