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

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

Issue 2817533004: Improve PasswordProtectionService and PasswordProtectionRequest (Closed)
Patch Set: nits Created 3 years, 8 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
« no previous file with comments | « components/safe_browsing/password_protection/password_protection_service.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 content_setting_map.get()), 73 content_setting_map.get()),
74 is_extended_reporting_(true), 74 is_extended_reporting_(true),
75 is_incognito_(false) {} 75 is_incognito_(false) {}
76 76
77 void RequestFinished( 77 void RequestFinished(
78 PasswordProtectionRequest* request, 78 PasswordProtectionRequest* request,
79 std::unique_ptr<LoginReputationClientResponse> response) override { 79 std::unique_ptr<LoginReputationClientResponse> response) override {
80 latest_response_ = std::move(response); 80 latest_response_ = std::move(response);
81 } 81 }
82 82
83 // Intentionally do nothing. 83 // Since referrer chain logic has been thoroughly tested in
84 // SBNavigationObserverBrowserTest class, we intentionally leave this function
85 // as a no-op here.
84 void FillReferrerChain(const GURL& event_url, 86 void FillReferrerChain(const GURL& event_url,
85 int event_tab_id, 87 int event_tab_id,
86 LoginReputationClientRequest::Frame* frame) override {} 88 LoginReputationClientRequest::Frame* frame) override {}
87 89
88 bool IsExtendedReporting() override { return is_extended_reporting_; } 90 bool IsExtendedReporting() override { return is_extended_reporting_; }
89 91
90 bool IsIncognito() override { return is_incognito_; } 92 bool IsIncognito() override { return is_incognito_; }
91 93
92 void set_extended_reporting(bool enabled) { 94 void set_extended_reporting(bool enabled) {
93 is_extended_reporting_ = enabled; 95 is_extended_reporting_ = enabled;
94 } 96 }
95 97
96 void set_incognito(bool enabled) { is_incognito_ = enabled; } 98 void set_incognito(bool enabled) { is_incognito_ = enabled; }
97 99
98 bool IsPingingEnabled() override { return true; } 100 bool IsPingingEnabled() override { return true; }
99 101
100 LoginReputationClientResponse* latest_response() { 102 LoginReputationClientResponse* latest_response() {
101 return latest_response_.get(); 103 return latest_response_.get();
102 } 104 }
103 105
106 ~TestPasswordProtectionService() override {}
107
108 size_t GetPendingRequestsCount() { return requests_.size(); }
109
104 private: 110 private:
105 bool is_extended_reporting_; 111 bool is_extended_reporting_;
106 bool is_incognito_; 112 bool is_incognito_;
107 std::unique_ptr<LoginReputationClientResponse> latest_response_; 113 std::unique_ptr<LoginReputationClientResponse> latest_response_;
108 DISALLOW_COPY_AND_ASSIGN(TestPasswordProtectionService); 114 DISALLOW_COPY_AND_ASSIGN(TestPasswordProtectionService);
109 }; 115 };
110 116
111 class PasswordProtectionServiceTest : public testing::Test { 117 class PasswordProtectionServiceTest : public testing::Test {
112 public: 118 public:
113 PasswordProtectionServiceTest(){}; 119 PasswordProtectionServiceTest(){};
(...skipping 22 matching lines...) Expand all
136 } 142 }
137 143
138 void TearDown() override { content_setting_map_->ShutdownOnUIThread(); } 144 void TearDown() override { content_setting_map_->ShutdownOnUIThread(); }
139 145
140 // Sets up |database_manager_| and |requests_| as needed. 146 // Sets up |database_manager_| and |requests_| as needed.
141 void InitializeAndStartRequest(bool match_whitelist, int timeout_in_ms) { 147 void InitializeAndStartRequest(bool match_whitelist, int timeout_in_ms) {
142 GURL target_url(kTargetUrl); 148 GURL target_url(kTargetUrl);
143 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) 149 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url))
144 .WillRepeatedly(testing::Return(match_whitelist)); 150 .WillRepeatedly(testing::Return(match_whitelist));
145 151
146 request_ = base::MakeUnique<PasswordProtectionRequest>( 152 request_ = new PasswordProtectionRequest(
147 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, 153 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE,
148 password_protection_service_->IsExtendedReporting(), 154 nullptr, password_protection_service_.get(), timeout_in_ms);
149 password_protection_service_->IsIncognito(),
150 password_protection_service_->GetWeakPtr(), timeout_in_ms);
151 request_->Start(); 155 request_->Start();
152 } 156 }
153 157
154 bool PathVariantsMatchCacheExpression(const GURL& url, 158 bool PathVariantsMatchCacheExpression(const GURL& url,
155 const std::string& cache_expression) { 159 const std::string& cache_expression) {
156 std::vector<std::string> paths; 160 std::vector<std::string> paths;
157 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); 161 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths);
158 return PasswordProtectionService::PathVariantsMatchCacheExpression( 162 return PasswordProtectionService::PathVariantsMatchCacheExpression(
159 paths, 163 paths,
160 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); 164 PasswordProtectionService::GetCacheExpressionPath(cache_expression));
(...skipping 16 matching lines...) Expand all
177 181
178 protected: 182 protected:
179 // |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
180 // threads. 184 // threads.
181 content::TestBrowserThreadBundle thread_bundle_; 185 content::TestBrowserThreadBundle thread_bundle_;
182 scoped_refptr<MockSafeBrowsingDatabaseManager> database_manager_; 186 scoped_refptr<MockSafeBrowsingDatabaseManager> database_manager_;
183 sync_preferences::TestingPrefServiceSyncable test_pref_service_; 187 sync_preferences::TestingPrefServiceSyncable test_pref_service_;
184 scoped_refptr<HostContentSettingsMap> content_setting_map_; 188 scoped_refptr<HostContentSettingsMap> content_setting_map_;
185 scoped_refptr<DummyURLRequestContextGetter> dummy_request_context_getter_; 189 scoped_refptr<DummyURLRequestContextGetter> dummy_request_context_getter_;
186 std::unique_ptr<TestPasswordProtectionService> password_protection_service_; 190 std::unique_ptr<TestPasswordProtectionService> password_protection_service_;
187 std::unique_ptr<PasswordProtectionRequest> request_; 191 scoped_refptr<PasswordProtectionRequest> request_;
188 base::HistogramTester histograms_; 192 base::HistogramTester histograms_;
189 }; 193 };
190 194
191 TEST_F(PasswordProtectionServiceTest, 195 TEST_F(PasswordProtectionServiceTest,
192 TestPasswordReuseMatchWhitelistHistogram) { 196 TestPasswordReuseMatchWhitelistHistogram) {
193 const GURL whitelisted_url(kWhitelistedUrl); 197 const GURL whitelisted_url(kWhitelistedUrl);
194 const GURL not_whitelisted_url(kNoneWhitelistedUrl); 198 const GURL not_whitelisted_url(kNoneWhitelistedUrl);
195 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(whitelisted_url)) 199 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(whitelisted_url))
196 .WillOnce(testing::Return(true)); 200 .WillOnce(testing::Return(true));
197 EXPECT_CALL(*database_manager_.get(), 201 EXPECT_CALL(*database_manager_.get(),
198 MatchCsdWhitelistUrl(not_whitelisted_url)) 202 MatchCsdWhitelistUrl(not_whitelisted_url))
199 .WillOnce(testing::Return(false)); 203 .WillOnce(testing::Return(false));
200 histograms_.ExpectTotalCount(kPasswordReuseMatchWhitelistHistogramName, 0); 204 histograms_.ExpectTotalCount(kPasswordReuseMatchWhitelistHistogramName, 0);
201 205
202 // Empty url should not increment metric. 206 // Empty url should increase "True" bucket by 1.
203 password_protection_service_->RecordPasswordReuse(GURL()); 207 password_protection_service_->RecordPasswordReuse(GURL());
204 base::RunLoop().RunUntilIdle(); 208 base::RunLoop().RunUntilIdle();
205 histograms_.ExpectTotalCount(kPasswordReuseMatchWhitelistHistogramName, 0); 209 EXPECT_THAT(
210 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName),
211 testing::ElementsAre(base::Bucket(1, 1)));
206 212
207 // Whitelisted url should increase "True" bucket by 1. 213 // Whitelisted url should increase "True" bucket by 1.
208 password_protection_service_->RecordPasswordReuse(whitelisted_url); 214 password_protection_service_->RecordPasswordReuse(whitelisted_url);
209 base::RunLoop().RunUntilIdle(); 215 base::RunLoop().RunUntilIdle();
210 EXPECT_THAT( 216 EXPECT_THAT(
211 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName), 217 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName),
212 testing::ElementsAre(base::Bucket(1, 1))); 218 testing::ElementsAre(base::Bucket(1, 2)));
213 219
214 // Non-whitelisted url should increase "False" bucket by 1. 220 // Non-whitelisted url should increase "False" bucket by 1.
215 password_protection_service_->RecordPasswordReuse(not_whitelisted_url); 221 password_protection_service_->RecordPasswordReuse(not_whitelisted_url);
216 base::RunLoop().RunUntilIdle(); 222 base::RunLoop().RunUntilIdle();
217 EXPECT_THAT( 223 EXPECT_THAT(
218 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName), 224 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName),
219 testing::ElementsAre(base::Bucket(0, 1), base::Bucket(1, 1))); 225 testing::ElementsAre(base::Bucket(0, 1), base::Bucket(1, 2)));
220 } 226 }
221 227
222 TEST_F(PasswordProtectionServiceTest, TestParseInvalidVerdictEntry) { 228 TEST_F(PasswordProtectionServiceTest, TestParseInvalidVerdictEntry) {
223 std::unique_ptr<base::DictionaryValue> invalid_verdict_entry = 229 std::unique_ptr<base::DictionaryValue> invalid_verdict_entry =
224 base::MakeUnique<base::DictionaryValue>(); 230 base::MakeUnique<base::DictionaryValue>();
225 invalid_verdict_entry->SetString("cache_creation_time", "invalid_time"); 231 invalid_verdict_entry->SetString("cache_creation_time", "invalid_time");
226 232
227 int cache_creation_time; 233 int cache_creation_time;
228 LoginReputationClientResponse response; 234 LoginReputationClientResponse response;
229 // ParseVerdictEntry fails if input is empty. 235 // ParseVerdictEntry fails if input is empty.
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, 392 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED,
387 password_protection_service_->GetCachedVerdict( 393 password_protection_service_->GetCachedVerdict(
388 GURL("http://bar.com"), &actual_verdict)); 394 GURL("http://bar.com"), &actual_verdict));
389 395
390 // If delete all history. All password protection content settings should be 396 // If delete all history. All password protection content settings should be
391 // gone. 397 // gone.
392 password_protection_service_->RemoveContentSettingsOnURLsDeleted( 398 password_protection_service_->RemoveContentSettingsOnURLsDeleted(
393 true /* all_history */, history::URLRows()); 399 true /* all_history */, history::URLRows());
394 EXPECT_EQ(0U, GetStoredVerdictCount()); 400 EXPECT_EQ(0U, GetStoredVerdictCount());
395 } 401 }
402 TEST_F(PasswordProtectionServiceTest,
403 TestNoRequestCreatedIfMainFrameURLIsNotValid) {
404 ASSERT_EQ(0u, password_protection_service_->GetPendingRequestsCount());
405 password_protection_service_->MaybeStartLowReputationRequest(GURL(), nullptr);
406 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount());
407 }
408
409 TEST_F(PasswordProtectionServiceTest,
410 TestNoRequestCreatedIfMainFrameURLIsNotHttpOrHttps) {
411 ASSERT_EQ(0u, password_protection_service_->GetPendingRequestsCount());
412 // If main frame url is data url, don't create request.
413 password_protection_service_->MaybeStartLowReputationRequest(
414 GURL("data:text/html, <p>hellow"), nullptr);
415 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount());
416
417 // If main frame url is ftp, don't create request.
418 password_protection_service_->MaybeStartLowReputationRequest(
419 GURL("ftp://foo.com:21"), nullptr);
420 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount());
421 }
396 422
397 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForIncognito) { 423 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForIncognito) {
398 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 424 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
399 password_protection_service_->set_incognito(true); 425 password_protection_service_->set_incognito(true);
400 password_protection_service_->StartRequest( 426 password_protection_service_->StartRequest(
401 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); 427 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE,
428 nullptr);
402 base::RunLoop().RunUntilIdle(); 429 base::RunLoop().RunUntilIdle();
403 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); 430 EXPECT_EQ(nullptr, password_protection_service_->latest_response());
404 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), 431 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName),
405 testing::ElementsAre(base::Bucket(7 /* INCOGNITO */, 1))); 432 testing::ElementsAre(base::Bucket(7 /* INCOGNITO */, 1)));
406 } 433 }
407 434
408 TEST_F(PasswordProtectionServiceTest, 435 TEST_F(PasswordProtectionServiceTest,
409 TestNoRequestSentForNonExtendedReporting) { 436 TestNoRequestSentForNonExtendedReporting) {
410 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 437 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
411 password_protection_service_->set_extended_reporting(false); 438 password_protection_service_->set_extended_reporting(false);
412 password_protection_service_->StartRequest( 439 password_protection_service_->StartRequest(
413 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); 440 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE,
441 nullptr);
414 442
415 base::RunLoop().RunUntilIdle(); 443 base::RunLoop().RunUntilIdle();
416 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); 444 EXPECT_EQ(nullptr, password_protection_service_->latest_response());
417 EXPECT_THAT( 445 EXPECT_THAT(
418 histograms_.GetAllSamples(kRequestOutcomeHistogramName), 446 histograms_.GetAllSamples(kRequestOutcomeHistogramName),
419 testing::ElementsAre(base::Bucket(6 /* NO_EXTENDED_REPORTING */, 1))); 447 testing::ElementsAre(base::Bucket(6 /* NO_EXTENDED_REPORTING */, 1)));
420 } 448 }
421 449
422 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForWhitelistedURL) { 450 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForWhitelistedURL) {
423 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 451 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 EXPECT_EQ(expected_response.cache_duration_sec(), 541 EXPECT_EQ(expected_response.cache_duration_sec(),
514 actual_response->cache_duration_sec()); 542 actual_response->cache_duration_sec());
515 } 543 }
516 544
517 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { 545 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) {
518 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 546 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
519 GURL target_url(kTargetUrl); 547 GURL target_url(kTargetUrl);
520 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) 548 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url))
521 .WillRepeatedly(testing::Return(false)); 549 .WillRepeatedly(testing::Return(false));
522 password_protection_service_->StartRequest( 550 password_protection_service_->StartRequest(
523 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); 551 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, nullptr);
524 552
525 // Destroy password_protection_service_ while there is one request pending. 553 // Destroy password_protection_service_ while there is one request pending.
526 password_protection_service_.reset(); 554 password_protection_service_.reset();
527 base::RunLoop().RunUntilIdle(); 555 base::RunLoop().RunUntilIdle();
528 556
529 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), 557 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName),
530 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); 558 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1)));
531 } 559 }
532 } // namespace safe_browsing 560 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing/password_protection/password_protection_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698