Chromium Code Reviews| 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" |
| 11 #include "components/content_settings/core/browser/host_content_settings_map.h" | |
| 11 #include "components/safe_browsing/password_protection/password_protection_reque st.h" | 12 #include "components/safe_browsing/password_protection/password_protection_reque st.h" |
| 12 #include "components/safe_browsing_db/test_database_manager.h" | 13 #include "components/safe_browsing_db/test_database_manager.h" |
| 13 #include "components/sync_preferences/testing_pref_service_syncable.h" | 14 #include "components/sync_preferences/testing_pref_service_syncable.h" |
| 14 #include "content/public/test/test_browser_thread_bundle.h" | 15 #include "content/public/test/test_browser_thread_bundle.h" |
| 15 #include "net/url_request/test_url_fetcher_factory.h" | 16 #include "net/url_request/test_url_fetcher_factory.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 59 | 60 |
| 60 scoped_refptr<base::SingleThreadTaskRunner> dummy_task_runner_; | 61 scoped_refptr<base::SingleThreadTaskRunner> dummy_task_runner_; |
| 61 }; | 62 }; |
| 62 | 63 |
| 63 class TestPasswordProtectionService : public PasswordProtectionService { | 64 class TestPasswordProtectionService : public PasswordProtectionService { |
| 64 public: | 65 public: |
| 65 TestPasswordProtectionService( | 66 TestPasswordProtectionService( |
| 66 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager, | 67 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager, |
| 67 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 68 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 68 scoped_refptr<HostContentSettingsMap> content_setting_map) | 69 scoped_refptr<HostContentSettingsMap> content_setting_map) |
| 69 : PasswordProtectionService(database_manager, request_context_getter), | 70 : PasswordProtectionService(database_manager, |
| 70 content_setting_map_(content_setting_map) {} | 71 request_context_getter, |
| 71 | 72 nullptr, |
| 72 HostContentSettingsMap* GetSettingMapForActiveProfile() override { | 73 content_setting_map.get()) {} |
| 73 return content_setting_map_.get(); | |
| 74 } | |
| 75 | 74 |
| 76 void RequestFinished( | 75 void RequestFinished( |
| 77 PasswordProtectionRequest* request, | 76 PasswordProtectionRequest* request, |
| 78 std::unique_ptr<LoginReputationClientResponse> response) override { | 77 std::unique_ptr<LoginReputationClientResponse> response) override { |
| 79 latest_response_ = std::move(response); | 78 latest_response_ = std::move(response); |
| 80 } | 79 } |
| 81 | 80 |
| 81 // Intentionally do nothing. | |
| 82 void FillReferrerChain(const GURL& event_url, | |
| 83 int event_tab_id, | |
| 84 LoginReputationClientRequest::Frame* frame) override {} | |
| 85 | |
| 86 bool IsExtendedReporting() override { return true; } | |
| 87 | |
| 88 bool IsIncognito() override { return false; } | |
|
Nathan Parker
2017/03/30 21:38:12
Do you test anywhere that these get used correctly
Jialiu Lin
2017/03/30 23:23:13
Yes, these are just default returns. Situations li
| |
| 89 | |
| 90 bool IsPingingEnabled() override { return true; } | |
| 91 | |
| 82 LoginReputationClientResponse* latest_response() { | 92 LoginReputationClientResponse* latest_response() { |
| 83 return latest_response_.get(); | 93 return latest_response_.get(); |
| 84 } | 94 } |
| 85 | 95 |
| 86 private: | 96 private: |
| 87 std::unique_ptr<LoginReputationClientResponse> latest_response_; | 97 std::unique_ptr<LoginReputationClientResponse> latest_response_; |
| 88 scoped_refptr<HostContentSettingsMap> content_setting_map_; | |
| 89 DISALLOW_COPY_AND_ASSIGN(TestPasswordProtectionService); | 98 DISALLOW_COPY_AND_ASSIGN(TestPasswordProtectionService); |
| 90 }; | 99 }; |
| 91 | 100 |
| 92 class PasswordProtectionServiceTest : public testing::Test { | 101 class PasswordProtectionServiceTest : public testing::Test { |
| 93 public: | 102 public: |
| 94 PasswordProtectionServiceTest(){}; | 103 PasswordProtectionServiceTest(){}; |
| 95 | 104 |
| 96 LoginReputationClientResponse CreateVerdictProto( | 105 LoginReputationClientResponse CreateVerdictProto( |
| 97 LoginReputationClientResponse::VerdictType verdict, | 106 LoginReputationClientResponse::VerdictType verdict, |
| 98 int cache_duration_sec, | 107 int cache_duration_sec, |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 155 } | 164 } |
| 156 | 165 |
| 157 void CacheVerdict(const GURL& url, | 166 void CacheVerdict(const GURL& url, |
| 158 LoginReputationClientResponse::VerdictType verdict, | 167 LoginReputationClientResponse::VerdictType verdict, |
| 159 int cache_duration_sec, | 168 int cache_duration_sec, |
| 160 const std::string& cache_expression, | 169 const std::string& cache_expression, |
| 161 bool exact_match, | 170 bool exact_match, |
| 162 const base::Time& verdict_received_time) { | 171 const base::Time& verdict_received_time) { |
| 163 LoginReputationClientResponse response(CreateVerdictProto( | 172 LoginReputationClientResponse response(CreateVerdictProto( |
| 164 verdict, cache_duration_sec, cache_expression, exact_match)); | 173 verdict, cache_duration_sec, cache_expression, exact_match)); |
| 165 password_protection_service_->CacheVerdict( | 174 password_protection_service_->CacheVerdict(url, &response, |
| 166 url, &response, verdict_received_time, content_setting_map_.get()); | 175 verdict_received_time); |
| 167 } | 176 } |
| 168 | 177 |
| 169 size_t GetStoredVerdictCount() { | 178 size_t GetStoredVerdictCount() { |
| 170 return password_protection_service_->GetStoredVerdictCount(); | 179 return password_protection_service_->GetStoredVerdictCount(); |
| 171 } | 180 } |
| 172 | 181 |
| 173 protected: | 182 protected: |
| 174 // |thread_bundle_| is needed here because this test involves both UI and IO | 183 // |thread_bundle_| is needed here because this test involves both UI and IO |
| 175 // threads. | 184 // threads. |
| 176 content::TestBrowserThreadBundle thread_bundle_; | 185 content::TestBrowserThreadBundle thread_bundle_; |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 334 | 343 |
| 335 // Cache another verdict with the some origin and cache_expression should | 344 // Cache another verdict with the some origin and cache_expression should |
| 336 // override the cache. | 345 // override the cache. |
| 337 CacheVerdict(GURL("http://www.test.com/foo/index2.html"), | 346 CacheVerdict(GURL("http://www.test.com/foo/index2.html"), |
| 338 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/foo", | 347 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/foo", |
| 339 false, base::Time::Now()); | 348 false, base::Time::Now()); |
| 340 EXPECT_EQ(1U, GetStoredVerdictCount()); | 349 EXPECT_EQ(1U, GetStoredVerdictCount()); |
| 341 LoginReputationClientResponse out_verdict; | 350 LoginReputationClientResponse out_verdict; |
| 342 EXPECT_EQ(LoginReputationClientResponse::PHISHING, | 351 EXPECT_EQ(LoginReputationClientResponse::PHISHING, |
| 343 password_protection_service_->GetCachedVerdict( | 352 password_protection_service_->GetCachedVerdict( |
| 344 content_setting_map_.get(), | |
| 345 GURL("http://www.test.com/foo/index2.html"), &out_verdict)); | 353 GURL("http://www.test.com/foo/index2.html"), &out_verdict)); |
| 346 | 354 |
| 347 // Cache another verdict with the same origin but different cache_expression | 355 // Cache another verdict with the same origin but different cache_expression |
| 348 // will not increase setting count, but will increase the number of verdicts | 356 // will not increase setting count, but will increase the number of verdicts |
| 349 // in the given origin. | 357 // in the given origin. |
| 350 CacheVerdict(GURL("http://www.test.com/bar/index2.html"), | 358 CacheVerdict(GURL("http://www.test.com/bar/index2.html"), |
| 351 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/bar", | 359 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/bar", |
| 352 false, base::Time::Now()); | 360 false, base::Time::Now()); |
| 353 EXPECT_EQ(2U, GetStoredVerdictCount()); | 361 EXPECT_EQ(2U, GetStoredVerdictCount()); |
| 354 } | 362 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 370 GURL("http://test.com/def/index.jsp"), | 378 GURL("http://test.com/def/index.jsp"), |
| 371 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/def", false, | 379 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/def", false, |
| 372 base::Time::FromDoubleT(now.ToDoubleT() - | 380 base::Time::FromDoubleT(now.ToDoubleT() - |
| 373 24.0 * 60.0 * 60.0)); // Yesterday, expired. | 381 24.0 * 60.0 * 60.0)); // Yesterday, expired. |
| 374 ASSERT_EQ(3U, GetStoredVerdictCount()); | 382 ASSERT_EQ(3U, GetStoredVerdictCount()); |
| 375 | 383 |
| 376 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with unknown origin. | 384 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with unknown origin. |
| 377 LoginReputationClientResponse actual_verdict; | 385 LoginReputationClientResponse actual_verdict; |
| 378 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | 386 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 379 password_protection_service_->GetCachedVerdict( | 387 password_protection_service_->GetCachedVerdict( |
| 380 content_setting_map_.get(), GURL("http://www.unknown.com/"), | 388 GURL("http://www.unknown.com/"), &actual_verdict)); |
| 381 &actual_verdict)); | |
| 382 | 389 |
| 383 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with http://test.com | 390 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with http://test.com |
| 384 // origin, but doesn't match any known cache_expression. | 391 // origin, but doesn't match any known cache_expression. |
| 385 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | 392 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 386 password_protection_service_->GetCachedVerdict( | 393 password_protection_service_->GetCachedVerdict( |
| 387 content_setting_map_.get(), GURL("http://test.com/xyz/foo.jsp"), | 394 GURL("http://test.com/xyz/foo.jsp"), &actual_verdict)); |
| 388 &actual_verdict)); | |
| 389 | 395 |
| 390 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL whose variants match | 396 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL whose variants match |
| 391 // test.com/def, since corresponding entry is expired. | 397 // test.com/def, since corresponding entry is expired. |
| 392 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | 398 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 393 password_protection_service_->GetCachedVerdict( | 399 password_protection_service_->GetCachedVerdict( |
| 394 content_setting_map_.get(), | |
| 395 GURL("http://test.com/def/ghi/index.html"), &actual_verdict)); | 400 GURL("http://test.com/def/ghi/index.html"), &actual_verdict)); |
| 396 | 401 |
| 397 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL whose variants match | 402 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL whose variants match |
| 398 // test.com, but not match it exactly. Return SAFE if it is a exact match of | 403 // test.com, but not match it exactly. Return SAFE if it is a exact match of |
| 399 // test.com. | 404 // test.com. |
| 400 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | 405 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 401 password_protection_service_->GetCachedVerdict( | 406 password_protection_service_->GetCachedVerdict( |
| 402 content_setting_map_.get(), | |
| 403 GURL("http://test.com/ghi/index.html"), &actual_verdict)); | 407 GURL("http://test.com/ghi/index.html"), &actual_verdict)); |
| 404 EXPECT_EQ(LoginReputationClientResponse::SAFE, | 408 EXPECT_EQ(LoginReputationClientResponse::SAFE, |
| 405 password_protection_service_->GetCachedVerdict( | 409 password_protection_service_->GetCachedVerdict( |
| 406 content_setting_map_.get(), | |
| 407 GURL("http://test.com/term_of_service.html"), &actual_verdict)); | 410 GURL("http://test.com/term_of_service.html"), &actual_verdict)); |
| 408 | 411 |
| 409 // Return LOW_REPUTATION if look up for a URL whose variants match | 412 // Return LOW_REPUTATION if look up for a URL whose variants match |
| 410 // test.com/abc. | 413 // test.com/abc. |
| 411 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, | 414 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, |
| 412 password_protection_service_->GetCachedVerdict( | 415 password_protection_service_->GetCachedVerdict( |
| 413 content_setting_map_.get(), GURL("http://test.com/abc/"), | 416 GURL("http://test.com/abc/"), &actual_verdict)); |
| 414 &actual_verdict)); | |
| 415 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, | 417 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, |
| 416 password_protection_service_->GetCachedVerdict( | 418 password_protection_service_->GetCachedVerdict( |
| 417 content_setting_map_.get(), GURL("http://test.com/abc/bar.jsp"), | 419 GURL("http://test.com/abc/bar.jsp"), &actual_verdict)); |
| 418 &actual_verdict)); | |
| 419 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, | 420 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, |
| 420 password_protection_service_->GetCachedVerdict( | 421 password_protection_service_->GetCachedVerdict( |
| 421 content_setting_map_.get(), | |
| 422 GURL("http://test.com/abc/foo/bar.html"), &actual_verdict)); | 422 GURL("http://test.com/abc/foo/bar.html"), &actual_verdict)); |
| 423 } | 423 } |
| 424 | 424 |
| 425 TEST_F(PasswordProtectionServiceTest, TestCleanUpCachedVerdicts) { | 425 TEST_F(PasswordProtectionServiceTest, TestCleanUpCachedVerdicts) { |
| 426 ASSERT_EQ(0U, GetStoredVerdictCount()); | 426 ASSERT_EQ(0U, GetStoredVerdictCount()); |
| 427 // Prepare 2 verdicts. One is for origin "http://foo.com", and the other is | 427 // Prepare 2 verdicts. One is for origin "http://foo.com", and the other is |
| 428 // for "http://bar.com". | 428 // for "http://bar.com". |
| 429 base::Time now = base::Time::Now(); | 429 base::Time now = base::Time::Now(); |
| 430 CacheVerdict(GURL("http://foo.com/abc/index.jsp"), | 430 CacheVerdict(GURL("http://foo.com/abc/index.jsp"), |
| 431 LoginReputationClientResponse::LOW_REPUTATION, 10 * 60, | 431 LoginReputationClientResponse::LOW_REPUTATION, 10 * 60, |
| 432 "foo.com/abc", false, now); | 432 "foo.com/abc", false, now); |
| 433 CacheVerdict(GURL("http://bar.com/index.jsp"), | 433 CacheVerdict(GURL("http://bar.com/index.jsp"), |
| 434 LoginReputationClientResponse::PHISHING, 10 * 60, "bar.com", | 434 LoginReputationClientResponse::PHISHING, 10 * 60, "bar.com", |
| 435 false, now); | 435 false, now); |
| 436 ASSERT_EQ(2U, GetStoredVerdictCount()); | 436 ASSERT_EQ(2U, GetStoredVerdictCount()); |
| 437 | 437 |
| 438 // Delete a bar.com URL. Corresponding content setting keyed on | 438 // Delete a bar.com URL. Corresponding content setting keyed on |
| 439 // origin "http://bar.com" should be removed, | 439 // origin "http://bar.com" should be removed, |
| 440 history::URLRows deleted_urls; | 440 history::URLRows deleted_urls; |
| 441 deleted_urls.push_back(history::URLRow(GURL("http://bar.com"))); | 441 deleted_urls.push_back(history::URLRow(GURL("http://bar.com"))); |
| 442 password_protection_service_->RemoveContentSettingsOnURLsDeleted( | 442 password_protection_service_->RemoveContentSettingsOnURLsDeleted( |
| 443 false /* all_history */, deleted_urls, content_setting_map_.get()); | 443 false /* all_history */, deleted_urls); |
| 444 EXPECT_EQ(1U, GetStoredVerdictCount()); | 444 EXPECT_EQ(1U, GetStoredVerdictCount()); |
| 445 LoginReputationClientResponse actual_verdict; | 445 LoginReputationClientResponse actual_verdict; |
| 446 EXPECT_EQ( | 446 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 447 LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | 447 password_protection_service_->GetCachedVerdict( |
| 448 password_protection_service_->GetCachedVerdict( | 448 GURL("http://bar.com"), &actual_verdict)); |
| 449 content_setting_map_.get(), GURL("http://bar.com"), &actual_verdict)); | |
| 450 | 449 |
| 451 // If delete all history. All password protection content settings should be | 450 // If delete all history. All password protection content settings should be |
| 452 // gone. | 451 // gone. |
| 453 password_protection_service_->RemoveContentSettingsOnURLsDeleted( | 452 password_protection_service_->RemoveContentSettingsOnURLsDeleted( |
| 454 true /* all_history */, history::URLRows(), content_setting_map_.get()); | 453 true /* all_history */, history::URLRows()); |
| 455 EXPECT_EQ(0U, GetStoredVerdictCount()); | 454 EXPECT_EQ(0U, GetStoredVerdictCount()); |
| 456 } | 455 } |
| 457 | 456 |
| 458 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForIncognito) { | 457 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForIncognito) { |
| 459 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 458 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); |
| 460 InitializeAndStartRequest(true /* extended_reporting */, true /* incognito */, | 459 InitializeAndStartRequest(true /* extended_reporting */, true /* incognito */, |
| 461 false /* match whitelist */, 10 /* timeout */); | 460 false /* match whitelist */, 10 /* timeout */); |
| 462 base::RunLoop().RunUntilIdle(); | 461 base::RunLoop().RunUntilIdle(); |
| 463 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); | 462 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); |
| 464 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), | 463 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 582 EXPECT_EQ(expected_response.cache_duration_sec(), | 581 EXPECT_EQ(expected_response.cache_duration_sec(), |
| 583 actual_response->cache_duration_sec()); | 582 actual_response->cache_duration_sec()); |
| 584 } | 583 } |
| 585 | 584 |
| 586 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { | 585 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { |
| 587 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 586 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); |
| 588 GURL target_url(kTargetUrl); | 587 GURL target_url(kTargetUrl); |
| 589 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) | 588 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) |
| 590 .WillRepeatedly(testing::Return(false)); | 589 .WillRepeatedly(testing::Return(false)); |
| 591 password_protection_service_->StartRequest( | 590 password_protection_service_->StartRequest( |
| 592 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, | 591 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); |
| 593 true /* extended_reporting */, false /* incognito */); | |
| 594 | 592 |
| 595 // Destroy password_protection_service_ while there is one request pending. | 593 // Destroy password_protection_service_ while there is one request pending. |
| 596 password_protection_service_.reset(); | 594 password_protection_service_.reset(); |
| 597 base::RunLoop().RunUntilIdle(); | 595 base::RunLoop().RunUntilIdle(); |
| 598 | 596 |
| 599 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), | 597 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), |
| 600 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); | 598 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); |
| 601 } | 599 } |
| 602 } // namespace safe_browsing | 600 } // namespace safe_browsing |
| OLD | NEW |