OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 #include "components/safe_browsing/password_protection/password_protection_servi
ce.h" |
| 5 |
| 6 #include "base/memory/ptr_util.h" |
| 7 #include "base/run_loop.h" |
| 8 #include "base/test/histogram_tester.h" |
| 9 #include "components/safe_browsing_db/test_database_manager.h" |
| 10 #include "content/public/test/test_browser_thread_bundle.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 const char kPasswordReuseMatchWhitelistHistogramName[] = |
| 17 "PasswordManager.PasswordReuse.MainFrameMatchCsdWhitelist"; |
| 18 const char kWhitelistedUrl[] = "http://inwhitelist.com"; |
| 19 const char kNoneWhitelistedUrl[] = "http://notinwhitelist.com"; |
| 20 |
| 21 } // namespace |
| 22 |
| 23 namespace safe_browsing { |
| 24 |
| 25 class MockSafeBrowsingDatabaseManager : public TestSafeBrowsingDatabaseManager { |
| 26 public: |
| 27 MockSafeBrowsingDatabaseManager() {} |
| 28 |
| 29 MOCK_METHOD1(MatchCsdWhitelistUrl, bool(const GURL&)); |
| 30 |
| 31 protected: |
| 32 ~MockSafeBrowsingDatabaseManager() override {} |
| 33 |
| 34 private: |
| 35 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingDatabaseManager); |
| 36 }; |
| 37 |
| 38 class PasswordProtectionServiceTest : public testing::Test { |
| 39 public: |
| 40 PasswordProtectionServiceTest(){}; |
| 41 |
| 42 void SetUp() override { |
| 43 database_manager_ = new MockSafeBrowsingDatabaseManager(); |
| 44 password_protection_service_ = |
| 45 base::MakeUnique<PasswordProtectionService>(database_manager_); |
| 46 } |
| 47 |
| 48 protected: |
| 49 // |thread_bundle_| is needed here because this test involves both UI and IO |
| 50 // threads. |
| 51 content::TestBrowserThreadBundle thread_bundle_; |
| 52 scoped_refptr<MockSafeBrowsingDatabaseManager> database_manager_; |
| 53 std::unique_ptr<PasswordProtectionService> password_protection_service_; |
| 54 }; |
| 55 |
| 56 TEST_F(PasswordProtectionServiceTest, |
| 57 TestPasswordReuseMatchWhitelistHistogram) { |
| 58 const GURL whitelisted_url(kWhitelistedUrl); |
| 59 const GURL not_whitelisted_url(kNoneWhitelistedUrl); |
| 60 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(whitelisted_url)) |
| 61 .WillOnce(testing::Return(true)); |
| 62 EXPECT_CALL(*database_manager_.get(), |
| 63 MatchCsdWhitelistUrl(not_whitelisted_url)) |
| 64 .WillOnce(testing::Return(false)); |
| 65 base::HistogramTester histograms; |
| 66 histograms.ExpectTotalCount(kPasswordReuseMatchWhitelistHistogramName, 0); |
| 67 |
| 68 // Empty url should not increment metric. |
| 69 password_protection_service_->RecordPasswordReuse(GURL()); |
| 70 base::RunLoop().RunUntilIdle(); |
| 71 histograms.ExpectTotalCount(kPasswordReuseMatchWhitelistHistogramName, 0); |
| 72 |
| 73 // Whitelisted url should increase "True" bucket by 1. |
| 74 password_protection_service_->RecordPasswordReuse(whitelisted_url); |
| 75 base::RunLoop().RunUntilIdle(); |
| 76 EXPECT_THAT( |
| 77 histograms.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName), |
| 78 testing::ElementsAre(base::Bucket(1, 1))); |
| 79 |
| 80 // Non-whitelisted url should increase "False" bucket by 1. |
| 81 password_protection_service_->RecordPasswordReuse(not_whitelisted_url); |
| 82 base::RunLoop().RunUntilIdle(); |
| 83 EXPECT_THAT( |
| 84 histograms.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName), |
| 85 testing::ElementsAre(base::Bucket(0, 1), base::Bucket(1, 1))); |
| 86 } |
| 87 |
| 88 } // namespace safe_browsing |
OLD | NEW |