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

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

Issue 2817533004: Improve PasswordProtectionService and PasswordProtectionRequest (Closed)
Patch Set: rebase 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
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_->GetWeakPtr(), database_manager_,
149 password_protection_service_->IsIncognito(), 155 timeout_in_ms);
150 password_protection_service_->GetWeakPtr(), timeout_in_ms);
151 request_->Start(); 156 request_->Start();
152 } 157 }
153 158
154 bool PathVariantsMatchCacheExpression(const GURL& url, 159 bool PathVariantsMatchCacheExpression(const GURL& url,
155 const std::string& cache_expression) { 160 const std::string& cache_expression) {
156 std::vector<std::string> paths; 161 std::vector<std::string> paths;
157 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); 162 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths);
158 return PasswordProtectionService::PathVariantsMatchCacheExpression( 163 return PasswordProtectionService::PathVariantsMatchCacheExpression(
159 paths, 164 paths,
160 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); 165 PasswordProtectionService::GetCacheExpressionPath(cache_expression));
(...skipping 16 matching lines...) Expand all
177 182
178 protected: 183 protected:
179 // |thread_bundle_| is needed here because this test involves both UI and IO 184 // |thread_bundle_| is needed here because this test involves both UI and IO
180 // threads. 185 // threads.
181 content::TestBrowserThreadBundle thread_bundle_; 186 content::TestBrowserThreadBundle thread_bundle_;
182 scoped_refptr<MockSafeBrowsingDatabaseManager> database_manager_; 187 scoped_refptr<MockSafeBrowsingDatabaseManager> database_manager_;
183 sync_preferences::TestingPrefServiceSyncable test_pref_service_; 188 sync_preferences::TestingPrefServiceSyncable test_pref_service_;
184 scoped_refptr<HostContentSettingsMap> content_setting_map_; 189 scoped_refptr<HostContentSettingsMap> content_setting_map_;
185 scoped_refptr<DummyURLRequestContextGetter> dummy_request_context_getter_; 190 scoped_refptr<DummyURLRequestContextGetter> dummy_request_context_getter_;
186 std::unique_ptr<TestPasswordProtectionService> password_protection_service_; 191 std::unique_ptr<TestPasswordProtectionService> password_protection_service_;
187 std::unique_ptr<PasswordProtectionRequest> request_; 192 scoped_refptr<PasswordProtectionRequest> request_;
188 base::HistogramTester histograms_; 193 base::HistogramTester histograms_;
189 }; 194 };
190 195
191 TEST_F(PasswordProtectionServiceTest, 196 TEST_F(PasswordProtectionServiceTest,
192 TestPasswordReuseMatchWhitelistHistogram) { 197 TestPasswordReuseMatchWhitelistHistogram) {
193 const GURL whitelisted_url(kWhitelistedUrl); 198 const GURL whitelisted_url(kWhitelistedUrl);
194 const GURL not_whitelisted_url(kNoneWhitelistedUrl); 199 const GURL not_whitelisted_url(kNoneWhitelistedUrl);
195 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(whitelisted_url)) 200 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(whitelisted_url))
196 .WillOnce(testing::Return(true)); 201 .WillOnce(testing::Return(true));
197 EXPECT_CALL(*database_manager_.get(), 202 EXPECT_CALL(*database_manager_.get(),
198 MatchCsdWhitelistUrl(not_whitelisted_url)) 203 MatchCsdWhitelistUrl(not_whitelisted_url))
199 .WillOnce(testing::Return(false)); 204 .WillOnce(testing::Return(false));
200 histograms_.ExpectTotalCount(kPasswordReuseMatchWhitelistHistogramName, 0); 205 histograms_.ExpectTotalCount(kPasswordReuseMatchWhitelistHistogramName, 0);
201 206
202 // Empty url should not increment metric. 207 // Empty url should increase "False" bucket by 1.
203 password_protection_service_->RecordPasswordReuse(GURL()); 208 password_protection_service_->RecordPasswordReuse(GURL());
204 base::RunLoop().RunUntilIdle(); 209 base::RunLoop().RunUntilIdle();
205 histograms_.ExpectTotalCount(kPasswordReuseMatchWhitelistHistogramName, 0); 210 EXPECT_THAT(
211 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName),
212 testing::ElementsAre(base::Bucket(0, 1)));
206 213
207 // Whitelisted url should increase "True" bucket by 1. 214 // Whitelisted url should increase "True" bucket by 1.
208 password_protection_service_->RecordPasswordReuse(whitelisted_url); 215 password_protection_service_->RecordPasswordReuse(whitelisted_url);
209 base::RunLoop().RunUntilIdle(); 216 base::RunLoop().RunUntilIdle();
210 EXPECT_THAT( 217 EXPECT_THAT(
211 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName), 218 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName),
212 testing::ElementsAre(base::Bucket(1, 1))); 219 testing::ElementsAre(base::Bucket(0, 1), base::Bucket(1, 1)));
213 220
214 // Non-whitelisted url should increase "False" bucket by 1. 221 // Non-whitelisted url should increase "False" bucket by 1.
215 password_protection_service_->RecordPasswordReuse(not_whitelisted_url); 222 password_protection_service_->RecordPasswordReuse(not_whitelisted_url);
216 base::RunLoop().RunUntilIdle(); 223 base::RunLoop().RunUntilIdle();
217 EXPECT_THAT( 224 EXPECT_THAT(
218 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName), 225 histograms_.GetAllSamples(kPasswordReuseMatchWhitelistHistogramName),
219 testing::ElementsAre(base::Bucket(0, 1), base::Bucket(1, 1))); 226 testing::ElementsAre(base::Bucket(0, 2), base::Bucket(1, 1)));
220 } 227 }
221 228
222 TEST_F(PasswordProtectionServiceTest, TestParseInvalidVerdictEntry) { 229 TEST_F(PasswordProtectionServiceTest, TestParseInvalidVerdictEntry) {
223 std::unique_ptr<base::DictionaryValue> invalid_verdict_entry = 230 std::unique_ptr<base::DictionaryValue> invalid_verdict_entry =
224 base::MakeUnique<base::DictionaryValue>(); 231 base::MakeUnique<base::DictionaryValue>();
225 invalid_verdict_entry->SetString("cache_creation_time", "invalid_time"); 232 invalid_verdict_entry->SetString("cache_creation_time", "invalid_time");
226 233
227 int cache_creation_time; 234 int cache_creation_time;
228 LoginReputationClientResponse response; 235 LoginReputationClientResponse response;
229 // ParseVerdictEntry fails if input is empty. 236 // 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, 393 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED,
387 password_protection_service_->GetCachedVerdict( 394 password_protection_service_->GetCachedVerdict(
388 GURL("http://bar.com"), &actual_verdict)); 395 GURL("http://bar.com"), &actual_verdict));
389 396
390 // If delete all history. All password protection content settings should be 397 // If delete all history. All password protection content settings should be
391 // gone. 398 // gone.
392 password_protection_service_->RemoveContentSettingsOnURLsDeleted( 399 password_protection_service_->RemoveContentSettingsOnURLsDeleted(
393 true /* all_history */, history::URLRows()); 400 true /* all_history */, history::URLRows());
394 EXPECT_EQ(0U, GetStoredVerdictCount()); 401 EXPECT_EQ(0U, GetStoredVerdictCount());
395 } 402 }
403 TEST_F(PasswordProtectionServiceTest,
404 TestNoRequestCreatedIfMainFrameURLIsNotValid) {
405 ASSERT_EQ(0u, password_protection_service_->GetPendingRequestsCount());
406 password_protection_service_->MaybeStartLowReputationRequest(GURL(), nullptr);
407 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount());
408 }
409
410 TEST_F(PasswordProtectionServiceTest,
411 TestNoRequestCreatedIfMainFrameURLIsNotHttpOrHttps) {
412 ASSERT_EQ(0u, password_protection_service_->GetPendingRequestsCount());
413 // If main frame url is data url, don't create request.
414 password_protection_service_->MaybeStartLowReputationRequest(
415 GURL("data:text/html, <p>hellow"), nullptr);
416 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount());
417
418 // If main frame url is ftp, don't create request.
419 password_protection_service_->MaybeStartLowReputationRequest(
420 GURL("ftp://foo.com:21"), nullptr);
421 EXPECT_EQ(0u, password_protection_service_->GetPendingRequestsCount());
422 }
396 423
397 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForIncognito) { 424 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForIncognito) {
398 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 425 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
399 password_protection_service_->set_incognito(true); 426 password_protection_service_->set_incognito(true);
400 password_protection_service_->StartRequest( 427 password_protection_service_->StartRequest(
401 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); 428 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE,
429 nullptr);
402 base::RunLoop().RunUntilIdle(); 430 base::RunLoop().RunUntilIdle();
403 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); 431 EXPECT_EQ(nullptr, password_protection_service_->latest_response());
404 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), 432 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName),
405 testing::ElementsAre(base::Bucket(7 /* INCOGNITO */, 1))); 433 testing::ElementsAre(base::Bucket(7 /* INCOGNITO */, 1)));
406 } 434 }
407 435
408 TEST_F(PasswordProtectionServiceTest, 436 TEST_F(PasswordProtectionServiceTest,
409 TestNoRequestSentForNonExtendedReporting) { 437 TestNoRequestSentForNonExtendedReporting) {
410 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 438 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
411 password_protection_service_->set_extended_reporting(false); 439 password_protection_service_->set_extended_reporting(false);
412 password_protection_service_->StartRequest( 440 password_protection_service_->StartRequest(
413 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); 441 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE,
442 nullptr);
414 443
415 base::RunLoop().RunUntilIdle(); 444 base::RunLoop().RunUntilIdle();
416 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); 445 EXPECT_EQ(nullptr, password_protection_service_->latest_response());
417 EXPECT_THAT( 446 EXPECT_THAT(
418 histograms_.GetAllSamples(kRequestOutcomeHistogramName), 447 histograms_.GetAllSamples(kRequestOutcomeHistogramName),
419 testing::ElementsAre(base::Bucket(6 /* NO_EXTENDED_REPORTING */, 1))); 448 testing::ElementsAre(base::Bucket(6 /* NO_EXTENDED_REPORTING */, 1)));
420 } 449 }
421 450
422 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForWhitelistedURL) { 451 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForWhitelistedURL) {
423 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 452 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
513 EXPECT_EQ(expected_response.cache_duration_sec(), 542 EXPECT_EQ(expected_response.cache_duration_sec(),
514 actual_response->cache_duration_sec()); 543 actual_response->cache_duration_sec());
515 } 544 }
516 545
517 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { 546 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) {
518 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 547 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
519 GURL target_url(kTargetUrl); 548 GURL target_url(kTargetUrl);
520 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) 549 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url))
521 .WillRepeatedly(testing::Return(false)); 550 .WillRepeatedly(testing::Return(false));
522 password_protection_service_->StartRequest( 551 password_protection_service_->StartRequest(
523 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); 552 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, nullptr);
524 553
525 // Destroy password_protection_service_ while there is one request pending. 554 // Destroy password_protection_service_ while there is one request pending.
526 password_protection_service_.reset(); 555 password_protection_service_.reset();
527 base::RunLoop().RunUntilIdle(); 556 base::RunLoop().RunUntilIdle();
528 557
529 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), 558 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName),
530 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); 559 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1)));
531 } 560 }
532 } // namespace safe_browsing 561 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698