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

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

Issue 2783773002: Link PasswordProtectionService to Profile and SB Service (Closed)
Patch Set: refine histogram description 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"
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
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 is_extended_reporting_(true),
74 } 75 is_incognito_(false) {}
75 76
76 void RequestFinished( 77 void RequestFinished(
77 PasswordProtectionRequest* request, 78 PasswordProtectionRequest* request,
78 std::unique_ptr<LoginReputationClientResponse> response) override { 79 std::unique_ptr<LoginReputationClientResponse> response) override {
79 latest_response_ = std::move(response); 80 latest_response_ = std::move(response);
80 } 81 }
81 82
83 // Intentionally do nothing.
84 void FillReferrerChain(const GURL& event_url,
85 int event_tab_id,
86 LoginReputationClientRequest::Frame* frame) override {}
87
88 bool IsExtendedReporting() override { return is_extended_reporting_; }
89
90 bool IsIncognito() override { return is_incognito_; }
91
92 void set_extended_reporting(bool enabled) {
93 is_extended_reporting_ = enabled;
94 }
95
96 void set_incognito(bool enabled) { is_incognito_ = enabled; }
97
98 bool IsPingingEnabled() override { return true; }
99
82 LoginReputationClientResponse* latest_response() { 100 LoginReputationClientResponse* latest_response() {
83 return latest_response_.get(); 101 return latest_response_.get();
84 } 102 }
85 103
86 private: 104 private:
105 bool is_extended_reporting_;
106 bool is_incognito_;
87 std::unique_ptr<LoginReputationClientResponse> latest_response_; 107 std::unique_ptr<LoginReputationClientResponse> latest_response_;
88 scoped_refptr<HostContentSettingsMap> content_setting_map_;
89 DISALLOW_COPY_AND_ASSIGN(TestPasswordProtectionService); 108 DISALLOW_COPY_AND_ASSIGN(TestPasswordProtectionService);
90 }; 109 };
91 110
92 class PasswordProtectionServiceTest : public testing::Test { 111 class PasswordProtectionServiceTest : public testing::Test {
93 public: 112 public:
94 PasswordProtectionServiceTest(){}; 113 PasswordProtectionServiceTest(){};
95 114
96 LoginReputationClientResponse CreateVerdictProto( 115 LoginReputationClientResponse CreateVerdictProto(
97 LoginReputationClientResponse::VerdictType verdict, 116 LoginReputationClientResponse::VerdictType verdict,
98 int cache_duration_sec, 117 int cache_duration_sec,
(...skipping 15 matching lines...) Expand all
114 dummy_request_context_getter_ = new DummyURLRequestContextGetter(); 133 dummy_request_context_getter_ = new DummyURLRequestContextGetter();
115 password_protection_service_ = 134 password_protection_service_ =
116 base::MakeUnique<TestPasswordProtectionService>( 135 base::MakeUnique<TestPasswordProtectionService>(
117 database_manager_, dummy_request_context_getter_, 136 database_manager_, dummy_request_context_getter_,
118 content_setting_map_); 137 content_setting_map_);
119 } 138 }
120 139
121 void TearDown() override { content_setting_map_->ShutdownOnUIThread(); } 140 void TearDown() override { content_setting_map_->ShutdownOnUIThread(); }
122 141
123 // Sets up |database_manager_| and |requests_| as needed. 142 // Sets up |database_manager_| and |requests_| as needed.
124 void InitializeAndStartRequest(bool is_extended_reporting, 143 void InitializeAndStartRequest(bool match_whitelist, int timeout_in_ms) {
125 bool incognito,
126 bool match_whitelist,
127 int timeout_in_ms) {
128 GURL target_url(kTargetUrl); 144 GURL target_url(kTargetUrl);
129 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) 145 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url))
130 .WillRepeatedly(testing::Return(match_whitelist)); 146 .WillRepeatedly(testing::Return(match_whitelist));
131 147
132 request_ = base::MakeUnique<PasswordProtectionRequest>( 148 request_ = base::MakeUnique<PasswordProtectionRequest>(
133 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, 149 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE,
134 is_extended_reporting, incognito, 150 password_protection_service_->IsExtendedReporting(),
151 password_protection_service_->IsIncognito(),
135 password_protection_service_->GetWeakPtr(), timeout_in_ms); 152 password_protection_service_->GetWeakPtr(), timeout_in_ms);
136 request_->Start(); 153 request_->Start();
137 } 154 }
138 155
139 bool PathVariantsMatchCacheExpression(const GURL& url, 156 bool PathVariantsMatchCacheExpression(const GURL& url,
140 const std::string& cache_expression) { 157 const std::string& cache_expression) {
141 std::vector<std::string> paths; 158 std::vector<std::string> paths;
142 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); 159 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths);
143 return PasswordProtectionService::PathVariantsMatchCacheExpression( 160 return PasswordProtectionService::PathVariantsMatchCacheExpression(
144 paths, 161 paths,
(...skipping 10 matching lines...) Expand all
155 } 172 }
156 173
157 void CacheVerdict(const GURL& url, 174 void CacheVerdict(const GURL& url,
158 LoginReputationClientResponse::VerdictType verdict, 175 LoginReputationClientResponse::VerdictType verdict,
159 int cache_duration_sec, 176 int cache_duration_sec,
160 const std::string& cache_expression, 177 const std::string& cache_expression,
161 bool exact_match, 178 bool exact_match,
162 const base::Time& verdict_received_time) { 179 const base::Time& verdict_received_time) {
163 LoginReputationClientResponse response(CreateVerdictProto( 180 LoginReputationClientResponse response(CreateVerdictProto(
164 verdict, cache_duration_sec, cache_expression, exact_match)); 181 verdict, cache_duration_sec, cache_expression, exact_match));
165 password_protection_service_->CacheVerdict( 182 password_protection_service_->CacheVerdict(url, &response,
166 url, &response, verdict_received_time, content_setting_map_.get()); 183 verdict_received_time);
167 } 184 }
168 185
169 size_t GetStoredVerdictCount() { 186 size_t GetStoredVerdictCount() {
170 return password_protection_service_->GetStoredVerdictCount(); 187 return password_protection_service_->GetStoredVerdictCount();
171 } 188 }
172 189
173 protected: 190 protected:
174 // |thread_bundle_| is needed here because this test involves both UI and IO 191 // |thread_bundle_| is needed here because this test involves both UI and IO
175 // threads. 192 // threads.
176 content::TestBrowserThreadBundle thread_bundle_; 193 content::TestBrowserThreadBundle thread_bundle_;
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 351
335 // Cache another verdict with the some origin and cache_expression should 352 // Cache another verdict with the some origin and cache_expression should
336 // override the cache. 353 // override the cache.
337 CacheVerdict(GURL("http://www.test.com/foo/index2.html"), 354 CacheVerdict(GURL("http://www.test.com/foo/index2.html"),
338 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/foo", 355 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/foo",
339 false, base::Time::Now()); 356 false, base::Time::Now());
340 EXPECT_EQ(1U, GetStoredVerdictCount()); 357 EXPECT_EQ(1U, GetStoredVerdictCount());
341 LoginReputationClientResponse out_verdict; 358 LoginReputationClientResponse out_verdict;
342 EXPECT_EQ(LoginReputationClientResponse::PHISHING, 359 EXPECT_EQ(LoginReputationClientResponse::PHISHING,
343 password_protection_service_->GetCachedVerdict( 360 password_protection_service_->GetCachedVerdict(
344 content_setting_map_.get(),
345 GURL("http://www.test.com/foo/index2.html"), &out_verdict)); 361 GURL("http://www.test.com/foo/index2.html"), &out_verdict));
346 362
347 // Cache another verdict with the same origin but different cache_expression 363 // Cache another verdict with the same origin but different cache_expression
348 // will not increase setting count, but will increase the number of verdicts 364 // will not increase setting count, but will increase the number of verdicts
349 // in the given origin. 365 // in the given origin.
350 CacheVerdict(GURL("http://www.test.com/bar/index2.html"), 366 CacheVerdict(GURL("http://www.test.com/bar/index2.html"),
351 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/bar", 367 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/bar",
352 false, base::Time::Now()); 368 false, base::Time::Now());
353 EXPECT_EQ(2U, GetStoredVerdictCount()); 369 EXPECT_EQ(2U, GetStoredVerdictCount());
354 } 370 }
(...skipping 15 matching lines...) Expand all
370 GURL("http://test.com/def/index.jsp"), 386 GURL("http://test.com/def/index.jsp"),
371 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/def", false, 387 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/def", false,
372 base::Time::FromDoubleT(now.ToDoubleT() - 388 base::Time::FromDoubleT(now.ToDoubleT() -
373 24.0 * 60.0 * 60.0)); // Yesterday, expired. 389 24.0 * 60.0 * 60.0)); // Yesterday, expired.
374 ASSERT_EQ(3U, GetStoredVerdictCount()); 390 ASSERT_EQ(3U, GetStoredVerdictCount());
375 391
376 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with unknown origin. 392 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with unknown origin.
377 LoginReputationClientResponse actual_verdict; 393 LoginReputationClientResponse actual_verdict;
378 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, 394 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED,
379 password_protection_service_->GetCachedVerdict( 395 password_protection_service_->GetCachedVerdict(
380 content_setting_map_.get(), GURL("http://www.unknown.com/"), 396 GURL("http://www.unknown.com/"), &actual_verdict));
381 &actual_verdict));
382 397
383 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with http://test.com 398 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with http://test.com
384 // origin, but doesn't match any known cache_expression. 399 // origin, but doesn't match any known cache_expression.
385 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, 400 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED,
386 password_protection_service_->GetCachedVerdict( 401 password_protection_service_->GetCachedVerdict(
387 content_setting_map_.get(), GURL("http://test.com/xyz/foo.jsp"), 402 GURL("http://test.com/xyz/foo.jsp"), &actual_verdict));
388 &actual_verdict));
389 403
390 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL whose variants match 404 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL whose variants match
391 // test.com/def, since corresponding entry is expired. 405 // test.com/def, since corresponding entry is expired.
392 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, 406 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED,
393 password_protection_service_->GetCachedVerdict( 407 password_protection_service_->GetCachedVerdict(
394 content_setting_map_.get(),
395 GURL("http://test.com/def/ghi/index.html"), &actual_verdict)); 408 GURL("http://test.com/def/ghi/index.html"), &actual_verdict));
396 409
397 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL whose variants match 410 // 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 411 // test.com, but not match it exactly. Return SAFE if it is a exact match of
399 // test.com. 412 // test.com.
400 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, 413 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED,
401 password_protection_service_->GetCachedVerdict( 414 password_protection_service_->GetCachedVerdict(
402 content_setting_map_.get(),
403 GURL("http://test.com/ghi/index.html"), &actual_verdict)); 415 GURL("http://test.com/ghi/index.html"), &actual_verdict));
404 EXPECT_EQ(LoginReputationClientResponse::SAFE, 416 EXPECT_EQ(LoginReputationClientResponse::SAFE,
405 password_protection_service_->GetCachedVerdict( 417 password_protection_service_->GetCachedVerdict(
406 content_setting_map_.get(),
407 GURL("http://test.com/term_of_service.html"), &actual_verdict)); 418 GURL("http://test.com/term_of_service.html"), &actual_verdict));
408 419
409 // Return LOW_REPUTATION if look up for a URL whose variants match 420 // Return LOW_REPUTATION if look up for a URL whose variants match
410 // test.com/abc. 421 // test.com/abc.
411 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, 422 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION,
412 password_protection_service_->GetCachedVerdict( 423 password_protection_service_->GetCachedVerdict(
413 content_setting_map_.get(), GURL("http://test.com/abc/"), 424 GURL("http://test.com/abc/"), &actual_verdict));
414 &actual_verdict));
415 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, 425 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION,
416 password_protection_service_->GetCachedVerdict( 426 password_protection_service_->GetCachedVerdict(
417 content_setting_map_.get(), GURL("http://test.com/abc/bar.jsp"), 427 GURL("http://test.com/abc/bar.jsp"), &actual_verdict));
418 &actual_verdict));
419 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, 428 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION,
420 password_protection_service_->GetCachedVerdict( 429 password_protection_service_->GetCachedVerdict(
421 content_setting_map_.get(),
422 GURL("http://test.com/abc/foo/bar.html"), &actual_verdict)); 430 GURL("http://test.com/abc/foo/bar.html"), &actual_verdict));
423 } 431 }
424 432
425 TEST_F(PasswordProtectionServiceTest, TestCleanUpCachedVerdicts) { 433 TEST_F(PasswordProtectionServiceTest, TestCleanUpCachedVerdicts) {
426 ASSERT_EQ(0U, GetStoredVerdictCount()); 434 ASSERT_EQ(0U, GetStoredVerdictCount());
427 // Prepare 2 verdicts. One is for origin "http://foo.com", and the other is 435 // Prepare 2 verdicts. One is for origin "http://foo.com", and the other is
428 // for "http://bar.com". 436 // for "http://bar.com".
429 base::Time now = base::Time::Now(); 437 base::Time now = base::Time::Now();
430 CacheVerdict(GURL("http://foo.com/abc/index.jsp"), 438 CacheVerdict(GURL("http://foo.com/abc/index.jsp"),
431 LoginReputationClientResponse::LOW_REPUTATION, 10 * 60, 439 LoginReputationClientResponse::LOW_REPUTATION, 10 * 60,
432 "foo.com/abc", false, now); 440 "foo.com/abc", false, now);
433 CacheVerdict(GURL("http://bar.com/index.jsp"), 441 CacheVerdict(GURL("http://bar.com/index.jsp"),
434 LoginReputationClientResponse::PHISHING, 10 * 60, "bar.com", 442 LoginReputationClientResponse::PHISHING, 10 * 60, "bar.com",
435 false, now); 443 false, now);
436 ASSERT_EQ(2U, GetStoredVerdictCount()); 444 ASSERT_EQ(2U, GetStoredVerdictCount());
437 445
438 // Delete a bar.com URL. Corresponding content setting keyed on 446 // Delete a bar.com URL. Corresponding content setting keyed on
439 // origin "http://bar.com" should be removed, 447 // origin "http://bar.com" should be removed,
440 history::URLRows deleted_urls; 448 history::URLRows deleted_urls;
441 deleted_urls.push_back(history::URLRow(GURL("http://bar.com"))); 449 deleted_urls.push_back(history::URLRow(GURL("http://bar.com")));
442 password_protection_service_->RemoveContentSettingsOnURLsDeleted( 450 password_protection_service_->RemoveContentSettingsOnURLsDeleted(
443 false /* all_history */, deleted_urls, content_setting_map_.get()); 451 false /* all_history */, deleted_urls);
444 EXPECT_EQ(1U, GetStoredVerdictCount()); 452 EXPECT_EQ(1U, GetStoredVerdictCount());
445 LoginReputationClientResponse actual_verdict; 453 LoginReputationClientResponse actual_verdict;
446 EXPECT_EQ( 454 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED,
447 LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, 455 password_protection_service_->GetCachedVerdict(
448 password_protection_service_->GetCachedVerdict( 456 GURL("http://bar.com"), &actual_verdict));
449 content_setting_map_.get(), GURL("http://bar.com"), &actual_verdict));
450 457
451 // If delete all history. All password protection content settings should be 458 // If delete all history. All password protection content settings should be
452 // gone. 459 // gone.
453 password_protection_service_->RemoveContentSettingsOnURLsDeleted( 460 password_protection_service_->RemoveContentSettingsOnURLsDeleted(
454 true /* all_history */, history::URLRows(), content_setting_map_.get()); 461 true /* all_history */, history::URLRows());
455 EXPECT_EQ(0U, GetStoredVerdictCount()); 462 EXPECT_EQ(0U, GetStoredVerdictCount());
456 } 463 }
457 464
458 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForIncognito) { 465 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForIncognito) {
459 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 466 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
460 InitializeAndStartRequest(true /* extended_reporting */, true /* incognito */, 467 password_protection_service_->set_incognito(true);
461 false /* match whitelist */, 10 /* timeout */); 468 password_protection_service_->StartRequest(
469 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE);
462 base::RunLoop().RunUntilIdle(); 470 base::RunLoop().RunUntilIdle();
463 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); 471 EXPECT_EQ(nullptr, password_protection_service_->latest_response());
464 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), 472 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName),
465 testing::ElementsAre(base::Bucket(7 /* INCOGNITO */, 1))); 473 testing::ElementsAre(base::Bucket(7 /* INCOGNITO */, 1)));
466 } 474 }
467 475
468 TEST_F(PasswordProtectionServiceTest, 476 TEST_F(PasswordProtectionServiceTest,
469 TestNoRequestSentForNonExtendedReporting) { 477 TestNoRequestSentForNonExtendedReporting) {
470 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 478 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
479 password_protection_service_->set_extended_reporting(false);
480 password_protection_service_->StartRequest(
481 GURL(kTargetUrl), LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE);
471 482
472 InitializeAndStartRequest(false /* extended_reporting */,
473 false /* incognito */, false /* match whitelist */,
474 10000 /* timeout in ms*/);
475 base::RunLoop().RunUntilIdle(); 483 base::RunLoop().RunUntilIdle();
476 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); 484 EXPECT_EQ(nullptr, password_protection_service_->latest_response());
477 EXPECT_THAT( 485 EXPECT_THAT(
478 histograms_.GetAllSamples(kRequestOutcomeHistogramName), 486 histograms_.GetAllSamples(kRequestOutcomeHistogramName),
479 testing::ElementsAre(base::Bucket(6 /* NO_EXTENDED_REPORTING */, 1))); 487 testing::ElementsAre(base::Bucket(6 /* NO_EXTENDED_REPORTING */, 1)));
480 } 488 }
481 489
482 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForWhitelistedURL) { 490 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentForWhitelistedURL) {
483 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 491 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
484 InitializeAndStartRequest(true /* extended_reporting */, 492 InitializeAndStartRequest(true /* match whitelist */,
485 false /* incognito */, true /* match whitelist */,
486 10000 /* timeout in ms*/); 493 10000 /* timeout in ms*/);
487 base::RunLoop().RunUntilIdle(); 494 base::RunLoop().RunUntilIdle();
488 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); 495 EXPECT_EQ(nullptr, password_protection_service_->latest_response());
489 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), 496 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName),
490 testing::ElementsAre(base::Bucket(4 /* MATCHED_WHITELIST */, 1))); 497 testing::ElementsAre(base::Bucket(4 /* MATCHED_WHITELIST */, 1)));
491 } 498 }
492 499
493 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentIfVerdictAlreadyCached) { 500 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentIfVerdictAlreadyCached) {
494 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 501 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
495 CacheVerdict(GURL(kTargetUrl), LoginReputationClientResponse::LOW_REPUTATION, 502 CacheVerdict(GURL(kTargetUrl), LoginReputationClientResponse::LOW_REPUTATION,
496 600, GURL(kTargetUrl).host(), true, base::Time::Now()); 503 600, GURL(kTargetUrl).host(), true, base::Time::Now());
497 InitializeAndStartRequest(true /* extended_reporting */, 504 InitializeAndStartRequest(false /* match whitelist */,
498 false /* incognito */, false /* match whitelist */,
499 10000 /* timeout in ms*/); 505 10000 /* timeout in ms*/);
500 base::RunLoop().RunUntilIdle(); 506 base::RunLoop().RunUntilIdle();
501 EXPECT_THAT( 507 EXPECT_THAT(
502 histograms_.GetAllSamples(kRequestOutcomeHistogramName), 508 histograms_.GetAllSamples(kRequestOutcomeHistogramName),
503 testing::ElementsAre(base::Bucket(5 /* RESPONSE_ALREADY_CACHED */, 1))); 509 testing::ElementsAre(base::Bucket(5 /* RESPONSE_ALREADY_CACHED */, 1)));
504 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, 510 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION,
505 password_protection_service_->latest_response()->verdict_type()); 511 password_protection_service_->latest_response()->verdict_type());
506 } 512 }
507 513
508 TEST_F(PasswordProtectionServiceTest, TestResponseFetchFailed) { 514 TEST_F(PasswordProtectionServiceTest, TestResponseFetchFailed) {
509 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 515 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
510 net::TestURLFetcher failed_fetcher(0, GURL("http://bar.com"), nullptr); 516 net::TestURLFetcher failed_fetcher(0, GURL("http://bar.com"), nullptr);
511 // Set up failed response. 517 // Set up failed response.
512 failed_fetcher.set_status( 518 failed_fetcher.set_status(
513 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED)); 519 net::URLRequestStatus(net::URLRequestStatus::FAILED, net::ERR_FAILED));
514 520
515 InitializeAndStartRequest(true /* extended_reporting */, 521 InitializeAndStartRequest(false /* match whitelist */,
516 false /* incognito */, false /* match whitelist */,
517 10000 /* timeout in ms*/); 522 10000 /* timeout in ms*/);
518 request_->OnURLFetchComplete(&failed_fetcher); 523 request_->OnURLFetchComplete(&failed_fetcher);
519 base::RunLoop().RunUntilIdle(); 524 base::RunLoop().RunUntilIdle();
520 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); 525 EXPECT_EQ(nullptr, password_protection_service_->latest_response());
521 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), 526 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName),
522 testing::ElementsAre(base::Bucket(9 /* FETCH_FAILED */, 1))); 527 testing::ElementsAre(base::Bucket(9 /* FETCH_FAILED */, 1)));
523 } 528 }
524 529
525 TEST_F(PasswordProtectionServiceTest, TestMalformedResponse) { 530 TEST_F(PasswordProtectionServiceTest, TestMalformedResponse) {
526 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 531 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
527 // Set up malformed response. 532 // Set up malformed response.
528 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); 533 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr);
529 fetcher.set_status( 534 fetcher.set_status(
530 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); 535 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
531 fetcher.set_response_code(200); 536 fetcher.set_response_code(200);
532 fetcher.SetResponseString("invalid response"); 537 fetcher.SetResponseString("invalid response");
533 538
534 InitializeAndStartRequest(true /* extended_reporting */, 539 InitializeAndStartRequest(false /* match whitelist */,
535 false /* incognito */, false /* match whitelist */,
536 10000 /* timeout in ms*/); 540 10000 /* timeout in ms*/);
537 request_->OnURLFetchComplete(&fetcher); 541 request_->OnURLFetchComplete(&fetcher);
538 base::RunLoop().RunUntilIdle(); 542 base::RunLoop().RunUntilIdle();
539 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); 543 EXPECT_EQ(nullptr, password_protection_service_->latest_response());
540 EXPECT_THAT( 544 EXPECT_THAT(
541 histograms_.GetAllSamples(kRequestOutcomeHistogramName), 545 histograms_.GetAllSamples(kRequestOutcomeHistogramName),
542 testing::ElementsAre(base::Bucket(10 /* RESPONSE_MALFORMED */, 1))); 546 testing::ElementsAre(base::Bucket(10 /* RESPONSE_MALFORMED */, 1)));
543 } 547 }
544 548
545 TEST_F(PasswordProtectionServiceTest, TestRequestTimedout) { 549 TEST_F(PasswordProtectionServiceTest, TestRequestTimedout) {
546 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 550 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
547 InitializeAndStartRequest(true /* extended_reporting */, 551 InitializeAndStartRequest(false /* match whitelist */,
548 false /* incognito */, false /* match whitelist */,
549 0 /* timeout immediately */); 552 0 /* timeout immediately */);
550 base::RunLoop().RunUntilIdle(); 553 base::RunLoop().RunUntilIdle();
551 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); 554 EXPECT_EQ(nullptr, password_protection_service_->latest_response());
552 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), 555 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName),
553 testing::ElementsAre(base::Bucket(3 /* TIMEDOUT */, 1))); 556 testing::ElementsAre(base::Bucket(3 /* TIMEDOUT */, 1)));
554 } 557 }
555 558
556 TEST_F(PasswordProtectionServiceTest, TestRequestAndResponseSuccessfull) { 559 TEST_F(PasswordProtectionServiceTest, TestRequestAndResponseSuccessfull) {
557 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 560 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
558 // Set up valid response. 561 // Set up valid response.
559 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); 562 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr);
560 fetcher.set_status( 563 fetcher.set_status(
561 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); 564 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK));
562 fetcher.set_response_code(200); 565 fetcher.set_response_code(200);
563 LoginReputationClientResponse expected_response = 566 LoginReputationClientResponse expected_response =
564 CreateVerdictProto(LoginReputationClientResponse::PHISHING, 600, 567 CreateVerdictProto(LoginReputationClientResponse::PHISHING, 600,
565 GURL(kTargetUrl).host(), true); 568 GURL(kTargetUrl).host(), true);
566 fetcher.SetResponseString(expected_response.SerializeAsString()); 569 fetcher.SetResponseString(expected_response.SerializeAsString());
567 570
568 InitializeAndStartRequest(true /* extended_reporting */, 571 InitializeAndStartRequest(false /* match whitelist */,
569 false /* incognito */, false /* match whitelist */,
570 10000 /* timeout in ms*/); 572 10000 /* timeout in ms*/);
571 request_->OnURLFetchComplete(&fetcher); 573 request_->OnURLFetchComplete(&fetcher);
572 base::RunLoop().RunUntilIdle(); 574 base::RunLoop().RunUntilIdle();
573 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), 575 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName),
574 testing::ElementsAre(base::Bucket(1 /* SUCCEEDED */, 1))); 576 testing::ElementsAre(base::Bucket(1 /* SUCCEEDED */, 1)));
575 LoginReputationClientResponse* actual_response = 577 LoginReputationClientResponse* actual_response =
576 password_protection_service_->latest_response(); 578 password_protection_service_->latest_response();
577 EXPECT_EQ(expected_response.verdict_type(), actual_response->verdict_type()); 579 EXPECT_EQ(expected_response.verdict_type(), actual_response->verdict_type());
578 EXPECT_EQ(expected_response.cache_expression(), 580 EXPECT_EQ(expected_response.cache_expression(),
579 actual_response->cache_expression()); 581 actual_response->cache_expression());
580 EXPECT_EQ(expected_response.cache_expression_exact_match(), 582 EXPECT_EQ(expected_response.cache_expression_exact_match(),
581 actual_response->cache_expression_exact_match()); 583 actual_response->cache_expression_exact_match());
582 EXPECT_EQ(expected_response.cache_duration_sec(), 584 EXPECT_EQ(expected_response.cache_duration_sec(),
583 actual_response->cache_duration_sec()); 585 actual_response->cache_duration_sec());
584 } 586 }
585 587
586 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { 588 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) {
587 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); 589 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0);
588 GURL target_url(kTargetUrl); 590 GURL target_url(kTargetUrl);
589 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) 591 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url))
590 .WillRepeatedly(testing::Return(false)); 592 .WillRepeatedly(testing::Return(false));
591 password_protection_service_->StartRequest( 593 password_protection_service_->StartRequest(
592 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, 594 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE);
593 true /* extended_reporting */, false /* incognito */);
594 595
595 // Destroy password_protection_service_ while there is one request pending. 596 // Destroy password_protection_service_ while there is one request pending.
596 password_protection_service_.reset(); 597 password_protection_service_.reset();
597 base::RunLoop().RunUntilIdle(); 598 base::RunLoop().RunUntilIdle();
598 599
599 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), 600 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName),
600 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); 601 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1)));
601 } 602 }
602 } // namespace safe_browsing 603 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing/password_protection/password_protection_service.cc ('k') | tools/metrics/histograms/histograms.xml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698