| OLD | NEW |
| 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/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/test/histogram_tester.h" | 10 #include "base/test/histogram_tester.h" |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 bool PathVariantsMatchCacheExpression(const GURL& url, | 194 bool PathVariantsMatchCacheExpression(const GURL& url, |
| 195 const std::string& cache_expression) { | 195 const std::string& cache_expression) { |
| 196 std::vector<std::string> paths; | 196 std::vector<std::string> paths; |
| 197 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); | 197 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); |
| 198 return PasswordProtectionService::PathVariantsMatchCacheExpression( | 198 return PasswordProtectionService::PathVariantsMatchCacheExpression( |
| 199 paths, | 199 paths, |
| 200 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); | 200 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); |
| 201 } | 201 } |
| 202 | 202 |
| 203 void CacheVerdict(const GURL& url, | 203 void CacheVerdict(const GURL& url, |
| 204 LoginReputationClientRequest::TriggerType trigger, |
| 204 LoginReputationClientResponse::VerdictType verdict, | 205 LoginReputationClientResponse::VerdictType verdict, |
| 205 int cache_duration_sec, | 206 int cache_duration_sec, |
| 206 const std::string& cache_expression, | 207 const std::string& cache_expression, |
| 207 const base::Time& verdict_received_time) { | 208 const base::Time& verdict_received_time) { |
| 208 LoginReputationClientResponse response( | 209 LoginReputationClientResponse response( |
| 209 CreateVerdictProto(verdict, cache_duration_sec, cache_expression)); | 210 CreateVerdictProto(verdict, cache_duration_sec, cache_expression)); |
| 210 password_protection_service_->CacheVerdict(url, &response, | 211 password_protection_service_->CacheVerdict(url, trigger, &response, |
| 211 verdict_received_time); | 212 verdict_received_time); |
| 212 } | 213 } |
| 213 | 214 |
| 214 size_t GetStoredVerdictCount() { | 215 size_t GetStoredVerdictCount(LoginReputationClientRequest::TriggerType type) { |
| 215 return password_protection_service_->GetStoredVerdictCount(); | 216 return password_protection_service_->GetStoredVerdictCount(type); |
| 216 } | 217 } |
| 217 | 218 |
| 218 protected: | 219 protected: |
| 219 // |thread_bundle_| is needed here because this test involves both UI and IO | 220 // |thread_bundle_| is needed here because this test involves both UI and IO |
| 220 // threads. | 221 // threads. |
| 221 content::TestBrowserThreadBundle thread_bundle_; | 222 content::TestBrowserThreadBundle thread_bundle_; |
| 222 scoped_refptr<MockSafeBrowsingDatabaseManager> database_manager_; | 223 scoped_refptr<MockSafeBrowsingDatabaseManager> database_manager_; |
| 223 sync_preferences::TestingPrefServiceSyncable test_pref_service_; | 224 sync_preferences::TestingPrefServiceSyncable test_pref_service_; |
| 224 scoped_refptr<HostContentSettingsMap> content_setting_map_; | 225 scoped_refptr<HostContentSettingsMap> content_setting_map_; |
| 225 scoped_refptr<DummyURLRequestContextGetter> dummy_request_context_getter_; | 226 scoped_refptr<DummyURLRequestContextGetter> dummy_request_context_getter_; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 EXPECT_TRUE(PathVariantsMatchCacheExpression( | 298 EXPECT_TRUE(PathVariantsMatchCacheExpression( |
| 298 GURL("http://evil.com/bad/foo/index.html"), cache_expression)); | 299 GURL("http://evil.com/bad/foo/index.html"), cache_expression)); |
| 299 EXPECT_TRUE(PathVariantsMatchCacheExpression( | 300 EXPECT_TRUE(PathVariantsMatchCacheExpression( |
| 300 GURL("http://evil.com/bad/foo/index.html"), cache_expression_with_slash)); | 301 GURL("http://evil.com/bad/foo/index.html"), cache_expression_with_slash)); |
| 301 EXPECT_FALSE(PathVariantsMatchCacheExpression( | 302 EXPECT_FALSE(PathVariantsMatchCacheExpression( |
| 302 GURL("http://evil.com/worse/index.html"), cache_expression)); | 303 GURL("http://evil.com/worse/index.html"), cache_expression)); |
| 303 EXPECT_FALSE(PathVariantsMatchCacheExpression( | 304 EXPECT_FALSE(PathVariantsMatchCacheExpression( |
| 304 GURL("http://evil.com/worse/index.html"), cache_expression_with_slash)); | 305 GURL("http://evil.com/worse/index.html"), cache_expression_with_slash)); |
| 305 } | 306 } |
| 306 | 307 |
| 307 TEST_F(PasswordProtectionServiceTest, TestCachedVerdicts) { | 308 TEST_F(PasswordProtectionServiceTest, TestCachePasswordReuseVerdicts) { |
| 308 ASSERT_EQ(0U, GetStoredVerdictCount()); | 309 ASSERT_EQ(0U, GetStoredVerdictCount( |
| 310 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 311 |
| 309 // Assume each verdict has a TTL of 10 minutes. | 312 // Assume each verdict has a TTL of 10 minutes. |
| 310 // Cache a verdict for http://www.test.com/foo/index.html | 313 // Cache a verdict for http://www.test.com/foo/index.html |
| 311 CacheVerdict(GURL("http://www.test.com/foo/index.html"), | 314 CacheVerdict(GURL("http://www.test.com/foo/index.html"), |
| 315 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 312 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/foo", | 316 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/foo", |
| 313 base::Time::Now()); | 317 base::Time::Now()); |
| 314 | 318 |
| 315 EXPECT_EQ(1U, GetStoredVerdictCount()); | 319 EXPECT_EQ(1U, GetStoredVerdictCount( |
| 320 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 316 | 321 |
| 317 // Cache another verdict with the some origin and cache_expression should | 322 // Cache another verdict with the some origin and cache_expression should |
| 318 // override the cache. | 323 // override the cache. |
| 319 CacheVerdict(GURL("http://www.test.com/foo/index2.html"), | 324 CacheVerdict(GURL("http://www.test.com/foo/index2.html"), |
| 325 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 320 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/foo", | 326 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/foo", |
| 321 base::Time::Now()); | 327 base::Time::Now()); |
| 322 EXPECT_EQ(1U, GetStoredVerdictCount()); | 328 EXPECT_EQ(1U, GetStoredVerdictCount( |
| 329 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 323 LoginReputationClientResponse out_verdict; | 330 LoginReputationClientResponse out_verdict; |
| 324 EXPECT_EQ(LoginReputationClientResponse::PHISHING, | 331 EXPECT_EQ( |
| 325 password_protection_service_->GetCachedVerdict( | 332 LoginReputationClientResponse::PHISHING, |
| 326 GURL("http://www.test.com/foo/index2.html"), &out_verdict)); | 333 password_protection_service_->GetCachedVerdict( |
| 334 GURL("http://www.test.com/foo/index2.html"), |
| 335 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, &out_verdict)); |
| 327 | 336 |
| 328 // Cache another verdict with the same origin but different cache_expression | 337 // Cache another verdict with the same origin but different cache_expression |
| 329 // will not increase setting count, but will increase the number of verdicts | 338 // will not increase setting count, but will increase the number of verdicts |
| 330 // in the given origin. | 339 // in the given origin. |
| 331 CacheVerdict(GURL("http://www.test.com/bar/index2.html"), | 340 CacheVerdict(GURL("http://www.test.com/bar/index2.html"), |
| 341 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 332 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/bar", | 342 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/bar", |
| 333 base::Time::Now()); | 343 base::Time::Now()); |
| 334 EXPECT_EQ(2U, GetStoredVerdictCount()); | 344 EXPECT_EQ(2U, GetStoredVerdictCount( |
| 345 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 346 |
| 347 // Now cache a UNFAMILIAR_LOGIN_PAGE verdict, stored verdict count for |
| 348 // PASSWORD_REUSE_EVENT should be the same. |
| 349 CacheVerdict(GURL("http://www.test.com/foobar/index3.html"), |
| 350 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 351 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/foobar", |
| 352 base::Time::Now()); |
| 353 EXPECT_EQ(2U, GetStoredVerdictCount( |
| 354 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 355 EXPECT_EQ(1U, GetStoredVerdictCount( |
| 356 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 357 } |
| 358 |
| 359 TEST_F(PasswordProtectionServiceTest, TestCacheUnfamiliarLoginVerdicts) { |
| 360 ASSERT_EQ(0U, GetStoredVerdictCount( |
| 361 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 362 |
| 363 // Assume each verdict has a TTL of 10 minutes. |
| 364 // Cache a verdict for http://www.test.com/foo/index.html |
| 365 CacheVerdict(GURL("http://www.test.com/foo/index.html"), |
| 366 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 367 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/foo", |
| 368 base::Time::Now()); |
| 369 |
| 370 EXPECT_EQ(1U, GetStoredVerdictCount( |
| 371 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 372 |
| 373 // Cache another verdict with the same origin but different cache_expression |
| 374 // will not increase setting count, but will increase the number of verdicts |
| 375 // in the given origin. |
| 376 CacheVerdict(GURL("http://www.test.com/bar/index2.html"), |
| 377 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 378 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/bar", |
| 379 base::Time::Now()); |
| 380 EXPECT_EQ(2U, GetStoredVerdictCount( |
| 381 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 382 |
| 383 // Now cache a PASSWORD_REUSE_EVENT verdict, stored verdict count for |
| 384 // UNFAMILIAR_LOGIN_PAGE should be the same. |
| 385 CacheVerdict(GURL("http://www.test.com/foobar/index3.html"), |
| 386 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 387 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/foobar", |
| 388 base::Time::Now()); |
| 389 EXPECT_EQ(2U, GetStoredVerdictCount( |
| 390 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 391 EXPECT_EQ(1U, GetStoredVerdictCount( |
| 392 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 335 } | 393 } |
| 336 | 394 |
| 337 TEST_F(PasswordProtectionServiceTest, TestGetCachedVerdicts) { | 395 TEST_F(PasswordProtectionServiceTest, TestGetCachedVerdicts) { |
| 338 ASSERT_EQ(0U, GetStoredVerdictCount()); | 396 ASSERT_EQ(0U, GetStoredVerdictCount( |
| 339 // Prepare 2 verdicts of the same origin with different cache expressions, | 397 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 340 // one is expired, the other is not. | 398 ASSERT_EQ(0U, GetStoredVerdictCount( |
| 399 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 400 // Prepare 3 verdicts of the same origin with different cache expressions, |
| 401 // one is expired, one is not, the other is of a different type. |
| 341 base::Time now = base::Time::Now(); | 402 base::Time now = base::Time::Now(); |
| 342 CacheVerdict(GURL("http://test.com/login.html"), | 403 CacheVerdict(GURL("http://test.com/login.html"), |
| 404 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 343 LoginReputationClientResponse::SAFE, 10 * 60, "test.com", now); | 405 LoginReputationClientResponse::SAFE, 10 * 60, "test.com", now); |
| 344 CacheVerdict( | 406 CacheVerdict( |
| 345 GURL("http://test.com/def/index.jsp"), | 407 GURL("http://test.com/def/index.jsp"), |
| 408 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 346 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/def", | 409 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/def", |
| 347 base::Time::FromDoubleT(now.ToDoubleT() - | 410 base::Time::FromDoubleT(now.ToDoubleT() - |
| 348 24.0 * 60.0 * 60.0)); // Yesterday, expired. | 411 24.0 * 60.0 * 60.0)); // Yesterday, expired. |
| 349 ASSERT_EQ(2U, GetStoredVerdictCount()); | 412 CacheVerdict(GURL("http://test.com/bar/login.html"), |
| 413 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 414 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/bar", |
| 415 now); |
| 416 |
| 417 ASSERT_EQ(2U, GetStoredVerdictCount( |
| 418 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 419 ASSERT_EQ(1U, GetStoredVerdictCount( |
| 420 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 350 | 421 |
| 351 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with unknown origin. | 422 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with unknown origin. |
| 352 LoginReputationClientResponse actual_verdict; | 423 LoginReputationClientResponse actual_verdict; |
| 353 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | 424 EXPECT_EQ( |
| 354 password_protection_service_->GetCachedVerdict( | 425 LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 355 GURL("http://www.unknown.com/"), &actual_verdict)); | 426 password_protection_service_->GetCachedVerdict( |
| 427 GURL("http://www.unknown.com/"), |
| 428 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, &actual_verdict)); |
| 356 | 429 |
| 357 // Return SAFE if look up for a URL that matches "test.com" cache expression. | 430 // Return SAFE if look up for a URL that matches "test.com" cache expression. |
| 358 EXPECT_EQ(LoginReputationClientResponse::SAFE, | 431 EXPECT_EQ( |
| 359 password_protection_service_->GetCachedVerdict( | 432 LoginReputationClientResponse::SAFE, |
| 360 GURL("http://test.com/xyz/foo.jsp"), &actual_verdict)); | 433 password_protection_service_->GetCachedVerdict( |
| 434 GURL("http://test.com/xyz/foo.jsp"), |
| 435 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, &actual_verdict)); |
| 361 | 436 |
| 362 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL whose variants match | 437 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL whose variants match |
| 363 // test.com/def, but the corresponding verdict is expired. | 438 // test.com/def, but the corresponding verdict is expired. |
| 364 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | 439 EXPECT_EQ( |
| 365 password_protection_service_->GetCachedVerdict( | 440 LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 366 GURL("http://test.com/def/ghi/index.html"), &actual_verdict)); | 441 password_protection_service_->GetCachedVerdict( |
| 442 GURL("http://test.com/def/ghi/index.html"), |
| 443 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, &actual_verdict)); |
| 367 } | 444 } |
| 368 | 445 |
| 369 TEST_F(PasswordProtectionServiceTest, TestRemoveCachedVerdictOnURLsDeleted) { | 446 TEST_F(PasswordProtectionServiceTest, TestRemoveCachedVerdictOnURLsDeleted) { |
| 370 ASSERT_EQ(0U, GetStoredVerdictCount()); | 447 ASSERT_EQ(0U, GetStoredVerdictCount( |
| 448 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 449 ASSERT_EQ(0U, GetStoredVerdictCount( |
| 450 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 371 // Prepare 2 verdicts. One is for origin "http://foo.com", and the other is | 451 // Prepare 2 verdicts. One is for origin "http://foo.com", and the other is |
| 372 // for "http://bar.com". | 452 // for "http://bar.com". |
| 373 base::Time now = base::Time::Now(); | 453 base::Time now = base::Time::Now(); |
| 374 CacheVerdict(GURL("http://foo.com/abc/index.jsp"), | 454 CacheVerdict(GURL("http://foo.com/abc/index.jsp"), |
| 455 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 375 LoginReputationClientResponse::LOW_REPUTATION, 10 * 60, | 456 LoginReputationClientResponse::LOW_REPUTATION, 10 * 60, |
| 376 "foo.com/abc", now); | 457 "foo.com/abc", now); |
| 377 CacheVerdict(GURL("http://bar.com/index.jsp"), | 458 CacheVerdict(GURL("http://bar.com/index.jsp"), |
| 459 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 378 LoginReputationClientResponse::PHISHING, 10 * 60, "bar.com", | 460 LoginReputationClientResponse::PHISHING, 10 * 60, "bar.com", |
| 379 now); | 461 now); |
| 380 ASSERT_EQ(2U, GetStoredVerdictCount()); | 462 ASSERT_EQ(2U, GetStoredVerdictCount( |
| 463 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 464 |
| 465 CacheVerdict(GURL("http://foo.com/abc/index.jsp"), |
| 466 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 467 LoginReputationClientResponse::LOW_REPUTATION, 10 * 60, |
| 468 "foo.com/abc", now); |
| 469 CacheVerdict(GURL("http://bar.com/index.jsp"), |
| 470 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 471 LoginReputationClientResponse::PHISHING, 10 * 60, "bar.com", |
| 472 now); |
| 473 ASSERT_EQ(2U, GetStoredVerdictCount( |
| 474 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 381 | 475 |
| 382 // Delete a bar.com URL. Corresponding content setting keyed on | 476 // Delete a bar.com URL. Corresponding content setting keyed on |
| 383 // origin "http://bar.com" should be removed, | 477 // origin "http://bar.com" should be removed, |
| 384 history::URLRows deleted_urls; | 478 history::URLRows deleted_urls; |
| 385 deleted_urls.push_back(history::URLRow(GURL("http://bar.com"))); | 479 deleted_urls.push_back(history::URLRow(GURL("http://bar.com"))); |
| 386 | 480 |
| 387 // Delete an arbitrary data URL, to ensure the service is robust against | 481 // Delete an arbitrary data URL, to ensure the service is robust against |
| 388 // filtering only http/s URLs. See crbug.com/709758. | 482 // filtering only http/s URLs. See crbug.com/709758. |
| 389 deleted_urls.push_back(history::URLRow(GURL("data:text/html, <p>hellow"))); | 483 deleted_urls.push_back(history::URLRow(GURL("data:text/html, <p>hellow"))); |
| 390 | 484 |
| 391 password_protection_service_->RemoveContentSettingsOnURLsDeleted( | 485 password_protection_service_->RemoveContentSettingsOnURLsDeleted( |
| 392 false /* all_history */, deleted_urls); | 486 false /* all_history */, deleted_urls); |
| 393 EXPECT_EQ(1U, GetStoredVerdictCount()); | 487 EXPECT_EQ(1U, GetStoredVerdictCount( |
| 488 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 489 EXPECT_EQ(1U, GetStoredVerdictCount( |
| 490 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 491 |
| 394 LoginReputationClientResponse actual_verdict; | 492 LoginReputationClientResponse actual_verdict; |
| 493 EXPECT_EQ( |
| 494 LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 495 password_protection_service_->GetCachedVerdict( |
| 496 GURL("http://bar.com"), |
| 497 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, &actual_verdict)); |
| 395 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | 498 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 396 password_protection_service_->GetCachedVerdict( | 499 password_protection_service_->GetCachedVerdict( |
| 397 GURL("http://bar.com"), &actual_verdict)); | 500 GURL("http://bar.com"), |
| 501 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 502 &actual_verdict)); |
| 398 | 503 |
| 399 // If delete all history. All password protection content settings should be | 504 // If delete all history. All password protection content settings should be |
| 400 // gone. | 505 // gone. |
| 401 password_protection_service_->RemoveContentSettingsOnURLsDeleted( | 506 password_protection_service_->RemoveContentSettingsOnURLsDeleted( |
| 402 true /* all_history */, history::URLRows()); | 507 true /* all_history */, history::URLRows()); |
| 403 EXPECT_EQ(0U, GetStoredVerdictCount()); | 508 EXPECT_EQ(0U, GetStoredVerdictCount( |
| 509 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 510 EXPECT_EQ(0U, GetStoredVerdictCount( |
| 511 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 404 } | 512 } |
| 405 | 513 |
| 406 TEST_F(PasswordProtectionServiceTest, VerifyCanGetReputationOfURL) { | 514 TEST_F(PasswordProtectionServiceTest, VerifyCanGetReputationOfURL) { |
| 407 // Invalid main frame URL. | 515 // Invalid main frame URL. |
| 408 EXPECT_FALSE(PasswordProtectionService::CanGetReputationOfURL(GURL())); | 516 EXPECT_FALSE(PasswordProtectionService::CanGetReputationOfURL(GURL())); |
| 409 | 517 |
| 410 // Main frame URL scheme is not HTTP or HTTPS. | 518 // Main frame URL scheme is not HTTP or HTTPS. |
| 411 EXPECT_FALSE(PasswordProtectionService::CanGetReputationOfURL( | 519 EXPECT_FALSE(PasswordProtectionService::CanGetReputationOfURL( |
| 412 GURL("data:text/html, <p>hellow"))); | 520 GURL("data:text/html, <p>hellow"))); |
| 413 | 521 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 10000 /* timeout in ms*/); | 555 10000 /* timeout in ms*/); |
| 448 base::RunLoop().RunUntilIdle(); | 556 base::RunLoop().RunUntilIdle(); |
| 449 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); | 557 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); |
| 450 EXPECT_THAT( | 558 EXPECT_THAT( |
| 451 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), | 559 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), |
| 452 testing::ElementsAre(base::Bucket(4 /* MATCHED_WHITELIST */, 1))); | 560 testing::ElementsAre(base::Bucket(4 /* MATCHED_WHITELIST */, 1))); |
| 453 } | 561 } |
| 454 | 562 |
| 455 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentIfVerdictAlreadyCached) { | 563 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentIfVerdictAlreadyCached) { |
| 456 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); | 564 histograms_.ExpectTotalCount(kPasswordOnFocusRequestOutcomeHistogramName, 0); |
| 457 CacheVerdict(GURL(kTargetUrl), LoginReputationClientResponse::LOW_REPUTATION, | 565 CacheVerdict(GURL(kTargetUrl), |
| 458 600, GURL(kTargetUrl).host(), base::Time::Now()); | 566 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 567 LoginReputationClientResponse::LOW_REPUTATION, 600, |
| 568 GURL(kTargetUrl).host(), base::Time::Now()); |
| 459 InitializeAndStartPasswordOnFocusRequest(false /* match whitelist */, | 569 InitializeAndStartPasswordOnFocusRequest(false /* match whitelist */, |
| 460 10000 /* timeout in ms*/); | 570 10000 /* timeout in ms*/); |
| 461 base::RunLoop().RunUntilIdle(); | 571 base::RunLoop().RunUntilIdle(); |
| 462 EXPECT_THAT( | 572 EXPECT_THAT( |
| 463 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), | 573 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), |
| 464 testing::ElementsAre(base::Bucket(5 /* RESPONSE_ALREADY_CACHED */, 1))); | 574 testing::ElementsAre(base::Bucket(5 /* RESPONSE_ALREADY_CACHED */, 1))); |
| 465 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, | 575 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, |
| 466 password_protection_service_->latest_response()->verdict_type()); | 576 password_protection_service_->latest_response()->verdict_type()); |
| 467 } | 577 } |
| 468 | 578 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 // Destroy password_protection_service_ while there is one request pending. | 665 // Destroy password_protection_service_ while there is one request pending. |
| 556 password_protection_service_.reset(); | 666 password_protection_service_.reset(); |
| 557 base::RunLoop().RunUntilIdle(); | 667 base::RunLoop().RunUntilIdle(); |
| 558 | 668 |
| 559 EXPECT_THAT( | 669 EXPECT_THAT( |
| 560 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), | 670 histograms_.GetAllSamples(kPasswordOnFocusRequestOutcomeHistogramName), |
| 561 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); | 671 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); |
| 562 } | 672 } |
| 563 | 673 |
| 564 TEST_F(PasswordProtectionServiceTest, TestCleanUpExpiredVerdict) { | 674 TEST_F(PasswordProtectionServiceTest, TestCleanUpExpiredVerdict) { |
| 565 ASSERT_EQ(0U, GetStoredVerdictCount()); | 675 // Prepare 4 verdicts for PASSWORD_REUSE_EVENT: |
| 566 // Prepare 4 verdicts: | |
| 567 // (1) "foo.com/abc" valid | 676 // (1) "foo.com/abc" valid |
| 568 // (2) "foo.com/def" expired | 677 // (2) "foo.com/def" expired |
| 569 // (3) "bar.com/abc" expired | 678 // (3) "bar.com/abc" expired |
| 570 // (4) "bar.com/def" expired | 679 // (4) "bar.com/def" expired |
| 571 base::Time now = base::Time::Now(); | 680 base::Time now = base::Time::Now(); |
| 572 CacheVerdict(GURL("https://foo.com/abc/index.jsp"), | 681 CacheVerdict(GURL("https://foo.com/abc/index.jsp"), |
| 682 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 573 LoginReputationClientResponse::LOW_REPUTATION, 10 * 60, | 683 LoginReputationClientResponse::LOW_REPUTATION, 10 * 60, |
| 574 "foo.com/abc", now); | 684 "foo.com/abc", now); |
| 575 CacheVerdict(GURL("https://foo.com/def/index.jsp"), | 685 CacheVerdict(GURL("https://foo.com/def/index.jsp"), |
| 686 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 576 LoginReputationClientResponse::LOW_REPUTATION, 0, "foo.com/def", | 687 LoginReputationClientResponse::LOW_REPUTATION, 0, "foo.com/def", |
| 577 now); | 688 now); |
| 578 CacheVerdict(GURL("https://bar.com/abc/index.jsp"), | 689 CacheVerdict(GURL("https://bar.com/abc/index.jsp"), |
| 690 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 579 LoginReputationClientResponse::PHISHING, 0, "bar.com/abc", now); | 691 LoginReputationClientResponse::PHISHING, 0, "bar.com/abc", now); |
| 580 CacheVerdict(GURL("https://bar.com/def/index.jsp"), | 692 CacheVerdict(GURL("https://bar.com/def/index.jsp"), |
| 693 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, |
| 581 LoginReputationClientResponse::PHISHING, 0, "bar.com/def", now); | 694 LoginReputationClientResponse::PHISHING, 0, "bar.com/def", now); |
| 582 ASSERT_EQ(4U, GetStoredVerdictCount()); | 695 ASSERT_EQ(4U, GetStoredVerdictCount( |
| 696 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 697 |
| 698 // Prepare 2 verdicts for UNFAMILIAR_LOGIN_PAGE: |
| 699 // (1) "bar.com/def" valid |
| 700 // (2) "bar.com/xyz" expired |
| 701 CacheVerdict(GURL("https://bar.com/def/index.jsp"), |
| 702 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 703 LoginReputationClientResponse::SAFE, 10 * 60, "bar.com/def", |
| 704 now); |
| 705 CacheVerdict(GURL("https://bar.com/xyz/index.jsp"), |
| 706 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 707 LoginReputationClientResponse::PHISHING, 0, "bar.com/xyz", now); |
| 708 ASSERT_EQ(2U, GetStoredVerdictCount( |
| 709 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 583 | 710 |
| 584 password_protection_service_->CleanUpExpiredVerdicts(); | 711 password_protection_service_->CleanUpExpiredVerdicts(); |
| 585 | 712 |
| 586 ASSERT_EQ(1U, GetStoredVerdictCount()); | 713 ASSERT_EQ(1U, GetStoredVerdictCount( |
| 714 LoginReputationClientRequest::PASSWORD_REUSE_EVENT)); |
| 715 ASSERT_EQ(1U, GetStoredVerdictCount( |
| 716 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE)); |
| 587 LoginReputationClientResponse actual_verdict; | 717 LoginReputationClientResponse actual_verdict; |
| 588 // Has cached verdict for foo.com/abc. | 718 // Has cached PASSWORD_REUSE_EVENT verdict for foo.com/abc. |
| 589 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, | 719 EXPECT_EQ( |
| 720 LoginReputationClientResponse::LOW_REPUTATION, |
| 721 password_protection_service_->GetCachedVerdict( |
| 722 GURL("https://foo.com/abc/test.jsp"), |
| 723 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, &actual_verdict)); |
| 724 // No cached PASSWORD_REUSE_EVENT verdict for foo.com/def. |
| 725 EXPECT_EQ( |
| 726 LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 727 password_protection_service_->GetCachedVerdict( |
| 728 GURL("https://foo.com/def/index.jsp"), |
| 729 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, &actual_verdict)); |
| 730 // No cached PASSWORD_REUSE_EVENT verdict for bar.com/abc. |
| 731 EXPECT_EQ( |
| 732 LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 733 password_protection_service_->GetCachedVerdict( |
| 734 GURL("https://bar.com/abc/index.jsp"), |
| 735 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, &actual_verdict)); |
| 736 // No cached PASSWORD_REUSE_EVENT verdict for bar.com/def. |
| 737 EXPECT_EQ( |
| 738 LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 739 password_protection_service_->GetCachedVerdict( |
| 740 GURL("https://bar.com/def/index.jsp"), |
| 741 LoginReputationClientRequest::PASSWORD_REUSE_EVENT, &actual_verdict)); |
| 742 |
| 743 // Has cached UNFAMILIAR_LOGIN_PAGE verdict for bar.com/def. |
| 744 EXPECT_EQ(LoginReputationClientResponse::SAFE, |
| 590 password_protection_service_->GetCachedVerdict( | 745 password_protection_service_->GetCachedVerdict( |
| 591 GURL("https://foo.com/abc/test.jsp"), &actual_verdict)); | 746 GURL("https://bar.com/def/index.jsp"), |
| 592 // No cached verdict for foo.com/def. | 747 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 748 &actual_verdict)); |
| 749 |
| 750 // No cached UNFAMILIAR_LOGIN_PAGE verdict for bar.com/xyz. |
| 593 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | 751 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 594 password_protection_service_->GetCachedVerdict( | 752 password_protection_service_->GetCachedVerdict( |
| 595 GURL("https://foo.com/def/index.jsp"), &actual_verdict)); | 753 GURL("https://bar.com/xyz/index.jsp"), |
| 596 // Nothing in content setting for bar.com. | 754 LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE, |
| 597 EXPECT_EQ(nullptr, content_setting_map_->GetWebsiteSetting( | 755 &actual_verdict)); |
| 598 GURL("https://bar.com"), GURL(), | |
| 599 CONTENT_SETTINGS_TYPE_PASSWORD_PROTECTION, | |
| 600 std::string(), nullptr)); | |
| 601 } | 756 } |
| 602 | 757 |
| 603 TEST_F(PasswordProtectionServiceTest, VerifyPasswordOnFocusRequestProto) { | 758 TEST_F(PasswordProtectionServiceTest, VerifyPasswordOnFocusRequestProto) { |
| 604 // Set up valid response. | 759 // Set up valid response. |
| 605 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); | 760 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); |
| 606 fetcher.set_status( | 761 fetcher.set_status( |
| 607 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 762 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| 608 fetcher.set_response_code(200); | 763 fetcher.set_response_code(200); |
| 609 LoginReputationClientResponse expected_response = CreateVerdictProto( | 764 LoginReputationClientResponse expected_response = CreateVerdictProto( |
| 610 LoginReputationClientResponse::PHISHING, 600, GURL(kTargetUrl).host()); | 765 LoginReputationClientResponse::PHISHING, 600, GURL(kTargetUrl).host()); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 request_->OnURLFetchComplete(&fetcher); | 821 request_->OnURLFetchComplete(&fetcher); |
| 667 base::RunLoop().RunUntilIdle(); | 822 base::RunLoop().RunUntilIdle(); |
| 668 | 823 |
| 669 actual_request = password_protection_service_->GetLatestRequestProto(); | 824 actual_request = password_protection_service_->GetLatestRequestProto(); |
| 670 ASSERT_TRUE(actual_request->has_password_reuse_event()); | 825 ASSERT_TRUE(actual_request->has_password_reuse_event()); |
| 671 ASSERT_FALSE( | 826 ASSERT_FALSE( |
| 672 actual_request->password_reuse_event().is_chrome_signin_password()); | 827 actual_request->password_reuse_event().is_chrome_signin_password()); |
| 673 } | 828 } |
| 674 | 829 |
| 675 } // namespace safe_browsing | 830 } // namespace safe_browsing |
| OLD | NEW |