Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: components/safe_browsing/password_protection/password_protection_service_unittest.cc

Issue 2720643003: Call CSD whitelist checking on UI thread and record UMA (Closed)
Patch Set: Add TODO Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/message_loop/message_loop.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.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 : ui_thread_(content::BrowserThread::UI, &message_loop_),
42 io_thread_(content::BrowserThread::IO, &message_loop_){};
43
44 void SetUp() override {
45 database_manager_ = new MockSafeBrowsingDatabaseManager();
46 password_protection_service_ =
47 new PasswordProtectionService(database_manager_);
48 }
49
50 void TearDown() override {
51 // Run all pending tasks or else some threads hold on to the message loop
52 // and prevent it from being deleted.
53 base::RunLoop().RunUntilIdle();
54 database_manager_ = nullptr;
Nathan Parker 2017/03/01 22:59:22 Do we need to null these, or can we rely on the de
Jialiu Lin 2017/03/02 00:53:07 Right. it is not necessary.
55 password_protection_service_ = nullptr;
56 }
57
58 protected:
59 base::MessageLoopForUI message_loop_;
vakh (use Gerrit instead) 2017/03/01 23:25:20 Not sure how message_loop_, ui_thread_, and io_thr
Jialiu Lin 2017/03/02 00:53:07 Just realize ThreatBrowserThread is deprecated. Ch
60 content::TestBrowserThread ui_thread_;
61 content::TestBrowserThread io_thread_;
62 scoped_refptr<MockSafeBrowsingDatabaseManager> database_manager_;
63 scoped_refptr<PasswordProtectionService> password_protection_service_;
64 };
65
66 TEST_F(PasswordProtectionServiceTest, TestPasswordReuseMatchWhitlistHistogram) {
vakh (use Gerrit instead) 2017/03/01 23:25:20 Typo: whitelist
Jialiu Lin 2017/03/02 00:53:07 Good eye!
67 const GURL whitelisted_url(kWhitelistedUrl);
68 const GURL not_whitelisted_url(kNoneWhitelistedUrl);
69 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(whitelisted_url))
70 .WillOnce(testing::Return(true));
71 EXPECT_CALL(*database_manager_.get(),
72 MatchCsdWhitelistUrl(not_whitelisted_url))
73 .WillOnce(testing::Return(false));
74 base::HistogramTester histograms;
75 histograms.ExpectTotalCount(kPasswordReuseMatchWhitelistHistogramName, 0);
76
77 // Empty url should not increment metric.
78 password_protection_service_->RecordPasswordReuse(GURL());
79 base::RunLoop().RunUntilIdle();
80 histograms.ExpectTotalCount(kPasswordReuseMatchWhitelistHistogramName, 0);
81
82 // Whitelisted url should increase "True" bucket by 1.
83 password_protection_service_->RecordPasswordReuse(GURL(kWhitelistedUrl));
vakh (use Gerrit instead) 2017/03/01 23:25:20 Use whitelisted_url instead
Jialiu Lin 2017/03/02 00:53:07 Done.
84 base::RunLoop().RunUntilIdle();
85 EXPECT_THAT(
86 histograms.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName),
87 testing::ElementsAre(base::Bucket(1, 1)));
88
89 // Non-whitelisted url should increase "False" bucket by 1.
90 password_protection_service_->RecordPasswordReuse(GURL(kNoneWhitelistedUrl));
vakh (use Gerrit instead) 2017/03/01 23:25:20 Use not_whitelisted_url instead
Jialiu Lin 2017/03/02 00:53:07 Done.
91 base::RunLoop().RunUntilIdle();
92 EXPECT_THAT(
93 histograms.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName),
94 testing::ElementsAre(base::Bucket(0, 1), base::Bucket(1, 1)));
95 }
96
97 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698