| 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/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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 DISALLOW_COPY_AND_ASSIGN(TestPasswordProtectionService); | 108 DISALLOW_COPY_AND_ASSIGN(TestPasswordProtectionService); |
| 109 }; | 109 }; |
| 110 | 110 |
| 111 class PasswordProtectionServiceTest : public testing::Test { | 111 class PasswordProtectionServiceTest : public testing::Test { |
| 112 public: | 112 public: |
| 113 PasswordProtectionServiceTest(){}; | 113 PasswordProtectionServiceTest(){}; |
| 114 | 114 |
| 115 LoginReputationClientResponse CreateVerdictProto( | 115 LoginReputationClientResponse CreateVerdictProto( |
| 116 LoginReputationClientResponse::VerdictType verdict, | 116 LoginReputationClientResponse::VerdictType verdict, |
| 117 int cache_duration_sec, | 117 int cache_duration_sec, |
| 118 const std::string& cache_expression, | 118 const std::string& cache_expression) { |
| 119 bool exact_match) { | |
| 120 LoginReputationClientResponse verdict_proto; | 119 LoginReputationClientResponse verdict_proto; |
| 121 verdict_proto.set_verdict_type(verdict); | 120 verdict_proto.set_verdict_type(verdict); |
| 122 verdict_proto.set_cache_duration_sec(cache_duration_sec); | 121 verdict_proto.set_cache_duration_sec(cache_duration_sec); |
| 123 verdict_proto.set_cache_expression(cache_expression); | 122 verdict_proto.set_cache_expression(cache_expression); |
| 124 verdict_proto.set_cache_expression_exact_match(exact_match); | |
| 125 return verdict_proto; | 123 return verdict_proto; |
| 126 } | 124 } |
| 127 | 125 |
| 128 void SetUp() override { | 126 void SetUp() override { |
| 129 HostContentSettingsMap::RegisterProfilePrefs(test_pref_service_.registry()); | 127 HostContentSettingsMap::RegisterProfilePrefs(test_pref_service_.registry()); |
| 130 content_setting_map_ = new HostContentSettingsMap( | 128 content_setting_map_ = new HostContentSettingsMap( |
| 131 &test_pref_service_, false /* incognito */, false /* guest_profile */); | 129 &test_pref_service_, false /* incognito */, false /* guest_profile */); |
| 132 database_manager_ = new MockSafeBrowsingDatabaseManager(); | 130 database_manager_ = new MockSafeBrowsingDatabaseManager(); |
| 133 dummy_request_context_getter_ = new DummyURLRequestContextGetter(); | 131 dummy_request_context_getter_ = new DummyURLRequestContextGetter(); |
| 134 password_protection_service_ = | 132 password_protection_service_ = |
| (...skipping 20 matching lines...) Expand all Loading... |
| 155 | 153 |
| 156 bool PathVariantsMatchCacheExpression(const GURL& url, | 154 bool PathVariantsMatchCacheExpression(const GURL& url, |
| 157 const std::string& cache_expression) { | 155 const std::string& cache_expression) { |
| 158 std::vector<std::string> paths; | 156 std::vector<std::string> paths; |
| 159 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); | 157 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); |
| 160 return PasswordProtectionService::PathVariantsMatchCacheExpression( | 158 return PasswordProtectionService::PathVariantsMatchCacheExpression( |
| 161 paths, | 159 paths, |
| 162 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); | 160 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); |
| 163 } | 161 } |
| 164 | 162 |
| 165 bool PathMatchCacheExpressionExactly(const GURL& url, | |
| 166 const std::string& cache_expression) { | |
| 167 std::vector<std::string> paths; | |
| 168 PasswordProtectionService::GeneratePathVariantsWithoutQuery(url, &paths); | |
| 169 return PasswordProtectionService::PathMatchCacheExpressionExactly( | |
| 170 paths, | |
| 171 PasswordProtectionService::GetCacheExpressionPath(cache_expression)); | |
| 172 } | |
| 173 | |
| 174 void CacheVerdict(const GURL& url, | 163 void CacheVerdict(const GURL& url, |
| 175 LoginReputationClientResponse::VerdictType verdict, | 164 LoginReputationClientResponse::VerdictType verdict, |
| 176 int cache_duration_sec, | 165 int cache_duration_sec, |
| 177 const std::string& cache_expression, | 166 const std::string& cache_expression, |
| 178 bool exact_match, | |
| 179 const base::Time& verdict_received_time) { | 167 const base::Time& verdict_received_time) { |
| 180 LoginReputationClientResponse response(CreateVerdictProto( | 168 LoginReputationClientResponse response( |
| 181 verdict, cache_duration_sec, cache_expression, exact_match)); | 169 CreateVerdictProto(verdict, cache_duration_sec, cache_expression)); |
| 182 password_protection_service_->CacheVerdict(url, &response, | 170 password_protection_service_->CacheVerdict(url, &response, |
| 183 verdict_received_time); | 171 verdict_received_time); |
| 184 } | 172 } |
| 185 | 173 |
| 186 size_t GetStoredVerdictCount() { | 174 size_t GetStoredVerdictCount() { |
| 187 return password_protection_service_->GetStoredVerdictCount(); | 175 return password_protection_service_->GetStoredVerdictCount(); |
| 188 } | 176 } |
| 189 | 177 |
| 190 protected: | 178 protected: |
| 191 // |thread_bundle_| is needed here because this test involves both UI and IO | 179 // |thread_bundle_| is needed here because this test involves both UI and IO |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 nullptr, &cache_creation_time, &response)); | 231 nullptr, &cache_creation_time, &response)); |
| 244 | 232 |
| 245 // ParseVerdictEntry fails if the input dict value is invalid. | 233 // ParseVerdictEntry fails if the input dict value is invalid. |
| 246 EXPECT_FALSE(PasswordProtectionService::ParseVerdictEntry( | 234 EXPECT_FALSE(PasswordProtectionService::ParseVerdictEntry( |
| 247 invalid_verdict_entry.get(), &cache_creation_time, &response)); | 235 invalid_verdict_entry.get(), &cache_creation_time, &response)); |
| 248 } | 236 } |
| 249 | 237 |
| 250 TEST_F(PasswordProtectionServiceTest, TestParseValidVerdictEntry) { | 238 TEST_F(PasswordProtectionServiceTest, TestParseValidVerdictEntry) { |
| 251 base::Time expected_creation_time = base::Time::Now(); | 239 base::Time expected_creation_time = base::Time::Now(); |
| 252 LoginReputationClientResponse expected_verdict(CreateVerdictProto( | 240 LoginReputationClientResponse expected_verdict(CreateVerdictProto( |
| 253 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/foo", true)); | 241 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/foo")); |
| 254 std::unique_ptr<base::DictionaryValue> valid_verdict_entry = | 242 std::unique_ptr<base::DictionaryValue> valid_verdict_entry = |
| 255 PasswordProtectionService::CreateDictionaryFromVerdict( | 243 PasswordProtectionService::CreateDictionaryFromVerdict( |
| 256 &expected_verdict, expected_creation_time); | 244 &expected_verdict, expected_creation_time); |
| 257 | 245 |
| 258 int actual_cache_creation_time; | 246 int actual_cache_creation_time; |
| 259 LoginReputationClientResponse actual_verdict; | 247 LoginReputationClientResponse actual_verdict; |
| 260 ASSERT_TRUE(PasswordProtectionService::ParseVerdictEntry( | 248 ASSERT_TRUE(PasswordProtectionService::ParseVerdictEntry( |
| 261 valid_verdict_entry.get(), &actual_cache_creation_time, &actual_verdict)); | 249 valid_verdict_entry.get(), &actual_cache_creation_time, &actual_verdict)); |
| 262 | 250 |
| 263 EXPECT_EQ(static_cast<int>(expected_creation_time.ToDoubleT()), | 251 EXPECT_EQ(static_cast<int>(expected_creation_time.ToDoubleT()), |
| 264 actual_cache_creation_time); | 252 actual_cache_creation_time); |
| 265 EXPECT_EQ(expected_verdict.cache_duration_sec(), | 253 EXPECT_EQ(expected_verdict.cache_duration_sec(), |
| 266 actual_verdict.cache_duration_sec()); | 254 actual_verdict.cache_duration_sec()); |
| 267 EXPECT_EQ(expected_verdict.verdict_type(), actual_verdict.verdict_type()); | 255 EXPECT_EQ(expected_verdict.verdict_type(), actual_verdict.verdict_type()); |
| 268 EXPECT_EQ(expected_verdict.cache_expression(), | 256 EXPECT_EQ(expected_verdict.cache_expression(), |
| 269 actual_verdict.cache_expression()); | 257 actual_verdict.cache_expression()); |
| 270 EXPECT_EQ(expected_verdict.cache_expression_exact_match(), | |
| 271 actual_verdict.cache_expression_exact_match()); | |
| 272 } | 258 } |
| 273 | 259 |
| 274 TEST_F(PasswordProtectionServiceTest, TestPathVariantsMatchCacheExpression) { | 260 TEST_F(PasswordProtectionServiceTest, TestPathVariantsMatchCacheExpression) { |
| 275 // Cache expression without path. | 261 // Cache expression without path. |
| 276 std::string cache_expression("google.com"); | 262 std::string cache_expression("google.com"); |
| 277 std::string cache_expression_with_slash("google.com/"); | 263 std::string cache_expression_with_slash("google.com/"); |
| 278 EXPECT_TRUE(PathVariantsMatchCacheExpression(GURL("https://www.google.com"), | 264 EXPECT_TRUE(PathVariantsMatchCacheExpression(GURL("https://www.google.com"), |
| 279 cache_expression)); | 265 cache_expression)); |
| 280 EXPECT_TRUE(PathVariantsMatchCacheExpression(GURL("https://www.google.com"), | 266 EXPECT_TRUE(PathVariantsMatchCacheExpression(GURL("https://www.google.com"), |
| 281 cache_expression_with_slash)); | 267 cache_expression_with_slash)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 302 EXPECT_TRUE(PathVariantsMatchCacheExpression( | 288 EXPECT_TRUE(PathVariantsMatchCacheExpression( |
| 303 GURL("http://evil.com/bad/foo/index.html"), cache_expression)); | 289 GURL("http://evil.com/bad/foo/index.html"), cache_expression)); |
| 304 EXPECT_TRUE(PathVariantsMatchCacheExpression( | 290 EXPECT_TRUE(PathVariantsMatchCacheExpression( |
| 305 GURL("http://evil.com/bad/foo/index.html"), cache_expression_with_slash)); | 291 GURL("http://evil.com/bad/foo/index.html"), cache_expression_with_slash)); |
| 306 EXPECT_FALSE(PathVariantsMatchCacheExpression( | 292 EXPECT_FALSE(PathVariantsMatchCacheExpression( |
| 307 GURL("http://evil.com/worse/index.html"), cache_expression)); | 293 GURL("http://evil.com/worse/index.html"), cache_expression)); |
| 308 EXPECT_FALSE(PathVariantsMatchCacheExpression( | 294 EXPECT_FALSE(PathVariantsMatchCacheExpression( |
| 309 GURL("http://evil.com/worse/index.html"), cache_expression_with_slash)); | 295 GURL("http://evil.com/worse/index.html"), cache_expression_with_slash)); |
| 310 } | 296 } |
| 311 | 297 |
| 312 TEST_F(PasswordProtectionServiceTest, TestPathMatchCacheExpressionExactly) { | |
| 313 // Cache expression without path. | |
| 314 std::string cache_expression("www.google.com"); | |
| 315 EXPECT_TRUE(PathMatchCacheExpressionExactly(GURL("https://www.google.com"), | |
| 316 cache_expression)); | |
| 317 EXPECT_TRUE(PathMatchCacheExpressionExactly(GURL("https://www.google.com/"), | |
| 318 cache_expression)); | |
| 319 EXPECT_TRUE(PathMatchCacheExpressionExactly( | |
| 320 GURL("https://www.google.com/index.html"), cache_expression)); | |
| 321 EXPECT_FALSE(PathMatchCacheExpressionExactly( | |
| 322 GURL("https://www.google.com/abc/"), cache_expression)); | |
| 323 EXPECT_FALSE(PathMatchCacheExpressionExactly( | |
| 324 GURL("https://www.google.com/def/login"), cache_expression)); | |
| 325 | |
| 326 // Cache expression with path. | |
| 327 cache_expression = "evil.com/bad"; | |
| 328 EXPECT_FALSE(PathMatchCacheExpressionExactly(GURL("http://evil.com"), | |
| 329 cache_expression)); | |
| 330 EXPECT_FALSE(PathMatchCacheExpressionExactly(GURL("http://evil.com/"), | |
| 331 cache_expression)); | |
| 332 EXPECT_TRUE(PathMatchCacheExpressionExactly(GURL("http://evil.com/bad/"), | |
| 333 cache_expression)); | |
| 334 EXPECT_TRUE(PathMatchCacheExpressionExactly( | |
| 335 GURL("http://evil.com/bad/index.html"), cache_expression)); | |
| 336 EXPECT_FALSE(PathMatchCacheExpressionExactly(GURL("http://evil.com/bad/abc/"), | |
| 337 cache_expression)); | |
| 338 EXPECT_FALSE(PathMatchCacheExpressionExactly( | |
| 339 GURL("http://evil.com/bad/abc/login.jsp"), cache_expression)); | |
| 340 } | |
| 341 | |
| 342 TEST_F(PasswordProtectionServiceTest, TestCachedVerdicts) { | 298 TEST_F(PasswordProtectionServiceTest, TestCachedVerdicts) { |
| 343 ASSERT_EQ(0U, GetStoredVerdictCount()); | 299 ASSERT_EQ(0U, GetStoredVerdictCount()); |
| 344 // Assume each verdict has a TTL of 10 minutes. | 300 // Assume each verdict has a TTL of 10 minutes. |
| 345 // Cache a verdict for http://www.test.com/foo/index.html | 301 // Cache a verdict for http://www.test.com/foo/index.html |
| 346 CacheVerdict(GURL("http://www.test.com/foo/index.html"), | 302 CacheVerdict(GURL("http://www.test.com/foo/index.html"), |
| 347 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/foo", | 303 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/foo", |
| 348 false, base::Time::Now()); | 304 base::Time::Now()); |
| 349 | 305 |
| 350 EXPECT_EQ(1U, GetStoredVerdictCount()); | 306 EXPECT_EQ(1U, GetStoredVerdictCount()); |
| 351 | 307 |
| 352 // Cache another verdict with the some origin and cache_expression should | 308 // Cache another verdict with the some origin and cache_expression should |
| 353 // override the cache. | 309 // override the cache. |
| 354 CacheVerdict(GURL("http://www.test.com/foo/index2.html"), | 310 CacheVerdict(GURL("http://www.test.com/foo/index2.html"), |
| 355 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/foo", | 311 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/foo", |
| 356 false, base::Time::Now()); | 312 base::Time::Now()); |
| 357 EXPECT_EQ(1U, GetStoredVerdictCount()); | 313 EXPECT_EQ(1U, GetStoredVerdictCount()); |
| 358 LoginReputationClientResponse out_verdict; | 314 LoginReputationClientResponse out_verdict; |
| 359 EXPECT_EQ(LoginReputationClientResponse::PHISHING, | 315 EXPECT_EQ(LoginReputationClientResponse::PHISHING, |
| 360 password_protection_service_->GetCachedVerdict( | 316 password_protection_service_->GetCachedVerdict( |
| 361 GURL("http://www.test.com/foo/index2.html"), &out_verdict)); | 317 GURL("http://www.test.com/foo/index2.html"), &out_verdict)); |
| 362 | 318 |
| 363 // Cache another verdict with the same origin but different cache_expression | 319 // Cache another verdict with the same origin but different cache_expression |
| 364 // will not increase setting count, but will increase the number of verdicts | 320 // will not increase setting count, but will increase the number of verdicts |
| 365 // in the given origin. | 321 // in the given origin. |
| 366 CacheVerdict(GURL("http://www.test.com/bar/index2.html"), | 322 CacheVerdict(GURL("http://www.test.com/bar/index2.html"), |
| 367 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/bar", | 323 LoginReputationClientResponse::SAFE, 10 * 60, "test.com/bar", |
| 368 false, base::Time::Now()); | 324 base::Time::Now()); |
| 369 EXPECT_EQ(2U, GetStoredVerdictCount()); | 325 EXPECT_EQ(2U, GetStoredVerdictCount()); |
| 370 } | 326 } |
| 371 | 327 |
| 372 TEST_F(PasswordProtectionServiceTest, TestGetCachedVerdicts) { | 328 TEST_F(PasswordProtectionServiceTest, TestGetCachedVerdicts) { |
| 373 ASSERT_EQ(0U, GetStoredVerdictCount()); | 329 ASSERT_EQ(0U, GetStoredVerdictCount()); |
| 374 // Prepare 3 verdicts of the same origin with different cache expressions: | 330 // Prepare 2 verdicts of the same origin with different cache expressions, |
| 375 // (1) require exact match, not expired. | 331 // one is expired, the other is not. |
| 376 // (2) not require exact match, not expired. | |
| 377 // (3) require exact match, expired. | |
| 378 base::Time now = base::Time::Now(); | 332 base::Time now = base::Time::Now(); |
| 379 CacheVerdict(GURL("http://test.com/login.html"), | 333 CacheVerdict(GURL("http://test.com/login.html"), |
| 380 LoginReputationClientResponse::SAFE, 10 * 60, "test.com", true, | 334 LoginReputationClientResponse::SAFE, 10 * 60, "test.com", now); |
| 381 now); | |
| 382 CacheVerdict(GURL("http://test.com/abc/index.jsp"), | |
| 383 LoginReputationClientResponse::LOW_REPUTATION, 10 * 60, | |
| 384 "test.com/abc", false, now); | |
| 385 CacheVerdict( | 335 CacheVerdict( |
| 386 GURL("http://test.com/def/index.jsp"), | 336 GURL("http://test.com/def/index.jsp"), |
| 387 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/def", false, | 337 LoginReputationClientResponse::PHISHING, 10 * 60, "test.com/def", |
| 388 base::Time::FromDoubleT(now.ToDoubleT() - | 338 base::Time::FromDoubleT(now.ToDoubleT() - |
| 389 24.0 * 60.0 * 60.0)); // Yesterday, expired. | 339 24.0 * 60.0 * 60.0)); // Yesterday, expired. |
| 390 ASSERT_EQ(3U, GetStoredVerdictCount()); | 340 ASSERT_EQ(2U, GetStoredVerdictCount()); |
| 391 | 341 |
| 392 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with unknown origin. | 342 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with unknown origin. |
| 393 LoginReputationClientResponse actual_verdict; | 343 LoginReputationClientResponse actual_verdict; |
| 394 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | 344 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 395 password_protection_service_->GetCachedVerdict( | 345 password_protection_service_->GetCachedVerdict( |
| 396 GURL("http://www.unknown.com/"), &actual_verdict)); | 346 GURL("http://www.unknown.com/"), &actual_verdict)); |
| 397 | 347 |
| 398 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL with http://test.com | 348 // Return SAFE if look up for a URL that matches "test.com" cache expression. |
| 399 // origin, but doesn't match any known cache_expression. | 349 EXPECT_EQ(LoginReputationClientResponse::SAFE, |
| 400 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | |
| 401 password_protection_service_->GetCachedVerdict( | 350 password_protection_service_->GetCachedVerdict( |
| 402 GURL("http://test.com/xyz/foo.jsp"), &actual_verdict)); | 351 GURL("http://test.com/xyz/foo.jsp"), &actual_verdict)); |
| 403 | 352 |
| 404 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL whose variants match | 353 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL whose variants match |
| 405 // test.com/def, since corresponding entry is expired. | 354 // test.com/def, but the corresponding verdict is expired. |
| 406 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | 355 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, |
| 407 password_protection_service_->GetCachedVerdict( | 356 password_protection_service_->GetCachedVerdict( |
| 408 GURL("http://test.com/def/ghi/index.html"), &actual_verdict)); | 357 GURL("http://test.com/def/ghi/index.html"), &actual_verdict)); |
| 409 | |
| 410 // Return VERDICT_TYPE_UNSPECIFIED if look up for a URL whose variants match | |
| 411 // test.com, but not match it exactly. Return SAFE if it is a exact match of | |
| 412 // test.com. | |
| 413 EXPECT_EQ(LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED, | |
| 414 password_protection_service_->GetCachedVerdict( | |
| 415 GURL("http://test.com/ghi/index.html"), &actual_verdict)); | |
| 416 EXPECT_EQ(LoginReputationClientResponse::SAFE, | |
| 417 password_protection_service_->GetCachedVerdict( | |
| 418 GURL("http://test.com/term_of_service.html"), &actual_verdict)); | |
| 419 | |
| 420 // Return LOW_REPUTATION if look up for a URL whose variants match | |
| 421 // test.com/abc. | |
| 422 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, | |
| 423 password_protection_service_->GetCachedVerdict( | |
| 424 GURL("http://test.com/abc/"), &actual_verdict)); | |
| 425 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, | |
| 426 password_protection_service_->GetCachedVerdict( | |
| 427 GURL("http://test.com/abc/bar.jsp"), &actual_verdict)); | |
| 428 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, | |
| 429 password_protection_service_->GetCachedVerdict( | |
| 430 GURL("http://test.com/abc/foo/bar.html"), &actual_verdict)); | |
| 431 } | 358 } |
| 432 | 359 |
| 433 TEST_F(PasswordProtectionServiceTest, TestCleanUpCachedVerdicts) { | 360 TEST_F(PasswordProtectionServiceTest, TestCleanUpCachedVerdicts) { |
| 434 ASSERT_EQ(0U, GetStoredVerdictCount()); | 361 ASSERT_EQ(0U, GetStoredVerdictCount()); |
| 435 // Prepare 2 verdicts. One is for origin "http://foo.com", and the other is | 362 // Prepare 2 verdicts. One is for origin "http://foo.com", and the other is |
| 436 // for "http://bar.com". | 363 // for "http://bar.com". |
| 437 base::Time now = base::Time::Now(); | 364 base::Time now = base::Time::Now(); |
| 438 CacheVerdict(GURL("http://foo.com/abc/index.jsp"), | 365 CacheVerdict(GURL("http://foo.com/abc/index.jsp"), |
| 439 LoginReputationClientResponse::LOW_REPUTATION, 10 * 60, | 366 LoginReputationClientResponse::LOW_REPUTATION, 10 * 60, |
| 440 "foo.com/abc", false, now); | 367 "foo.com/abc", now); |
| 441 CacheVerdict(GURL("http://bar.com/index.jsp"), | 368 CacheVerdict(GURL("http://bar.com/index.jsp"), |
| 442 LoginReputationClientResponse::PHISHING, 10 * 60, "bar.com", | 369 LoginReputationClientResponse::PHISHING, 10 * 60, "bar.com", |
| 443 false, now); | 370 now); |
| 444 ASSERT_EQ(2U, GetStoredVerdictCount()); | 371 ASSERT_EQ(2U, GetStoredVerdictCount()); |
| 445 | 372 |
| 446 // Delete a bar.com URL. Corresponding content setting keyed on | 373 // Delete a bar.com URL. Corresponding content setting keyed on |
| 447 // origin "http://bar.com" should be removed, | 374 // origin "http://bar.com" should be removed, |
| 448 history::URLRows deleted_urls; | 375 history::URLRows deleted_urls; |
| 449 deleted_urls.push_back(history::URLRow(GURL("http://bar.com"))); | 376 deleted_urls.push_back(history::URLRow(GURL("http://bar.com"))); |
| 450 password_protection_service_->RemoveContentSettingsOnURLsDeleted( | 377 password_protection_service_->RemoveContentSettingsOnURLsDeleted( |
| 451 false /* all_history */, deleted_urls); | 378 false /* all_history */, deleted_urls); |
| 452 EXPECT_EQ(1U, GetStoredVerdictCount()); | 379 EXPECT_EQ(1U, GetStoredVerdictCount()); |
| 453 LoginReputationClientResponse actual_verdict; | 380 LoginReputationClientResponse actual_verdict; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 493 10000 /* timeout in ms*/); | 420 10000 /* timeout in ms*/); |
| 494 base::RunLoop().RunUntilIdle(); | 421 base::RunLoop().RunUntilIdle(); |
| 495 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); | 422 EXPECT_EQ(nullptr, password_protection_service_->latest_response()); |
| 496 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), | 423 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), |
| 497 testing::ElementsAre(base::Bucket(4 /* MATCHED_WHITELIST */, 1))); | 424 testing::ElementsAre(base::Bucket(4 /* MATCHED_WHITELIST */, 1))); |
| 498 } | 425 } |
| 499 | 426 |
| 500 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentIfVerdictAlreadyCached) { | 427 TEST_F(PasswordProtectionServiceTest, TestNoRequestSentIfVerdictAlreadyCached) { |
| 501 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 428 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); |
| 502 CacheVerdict(GURL(kTargetUrl), LoginReputationClientResponse::LOW_REPUTATION, | 429 CacheVerdict(GURL(kTargetUrl), LoginReputationClientResponse::LOW_REPUTATION, |
| 503 600, GURL(kTargetUrl).host(), true, base::Time::Now()); | 430 600, GURL(kTargetUrl).host(), base::Time::Now()); |
| 504 InitializeAndStartRequest(false /* match whitelist */, | 431 InitializeAndStartRequest(false /* match whitelist */, |
| 505 10000 /* timeout in ms*/); | 432 10000 /* timeout in ms*/); |
| 506 base::RunLoop().RunUntilIdle(); | 433 base::RunLoop().RunUntilIdle(); |
| 507 EXPECT_THAT( | 434 EXPECT_THAT( |
| 508 histograms_.GetAllSamples(kRequestOutcomeHistogramName), | 435 histograms_.GetAllSamples(kRequestOutcomeHistogramName), |
| 509 testing::ElementsAre(base::Bucket(5 /* RESPONSE_ALREADY_CACHED */, 1))); | 436 testing::ElementsAre(base::Bucket(5 /* RESPONSE_ALREADY_CACHED */, 1))); |
| 510 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, | 437 EXPECT_EQ(LoginReputationClientResponse::LOW_REPUTATION, |
| 511 password_protection_service_->latest_response()->verdict_type()); | 438 password_protection_service_->latest_response()->verdict_type()); |
| 512 } | 439 } |
| 513 | 440 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 testing::ElementsAre(base::Bucket(3 /* TIMEDOUT */, 1))); | 483 testing::ElementsAre(base::Bucket(3 /* TIMEDOUT */, 1))); |
| 557 } | 484 } |
| 558 | 485 |
| 559 TEST_F(PasswordProtectionServiceTest, TestRequestAndResponseSuccessfull) { | 486 TEST_F(PasswordProtectionServiceTest, TestRequestAndResponseSuccessfull) { |
| 560 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 487 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); |
| 561 // Set up valid response. | 488 // Set up valid response. |
| 562 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); | 489 net::TestURLFetcher fetcher(0, GURL("http://bar.com"), nullptr); |
| 563 fetcher.set_status( | 490 fetcher.set_status( |
| 564 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 491 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| 565 fetcher.set_response_code(200); | 492 fetcher.set_response_code(200); |
| 566 LoginReputationClientResponse expected_response = | 493 LoginReputationClientResponse expected_response = CreateVerdictProto( |
| 567 CreateVerdictProto(LoginReputationClientResponse::PHISHING, 600, | 494 LoginReputationClientResponse::PHISHING, 600, GURL(kTargetUrl).host()); |
| 568 GURL(kTargetUrl).host(), true); | |
| 569 fetcher.SetResponseString(expected_response.SerializeAsString()); | 495 fetcher.SetResponseString(expected_response.SerializeAsString()); |
| 570 | 496 |
| 571 InitializeAndStartRequest(false /* match whitelist */, | 497 InitializeAndStartRequest(false /* match whitelist */, |
| 572 10000 /* timeout in ms*/); | 498 10000 /* timeout in ms*/); |
| 573 request_->OnURLFetchComplete(&fetcher); | 499 request_->OnURLFetchComplete(&fetcher); |
| 574 base::RunLoop().RunUntilIdle(); | 500 base::RunLoop().RunUntilIdle(); |
| 575 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), | 501 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), |
| 576 testing::ElementsAre(base::Bucket(1 /* SUCCEEDED */, 1))); | 502 testing::ElementsAre(base::Bucket(1 /* SUCCEEDED */, 1))); |
| 577 LoginReputationClientResponse* actual_response = | 503 LoginReputationClientResponse* actual_response = |
| 578 password_protection_service_->latest_response(); | 504 password_protection_service_->latest_response(); |
| 579 EXPECT_EQ(expected_response.verdict_type(), actual_response->verdict_type()); | 505 EXPECT_EQ(expected_response.verdict_type(), actual_response->verdict_type()); |
| 580 EXPECT_EQ(expected_response.cache_expression(), | 506 EXPECT_EQ(expected_response.cache_expression(), |
| 581 actual_response->cache_expression()); | 507 actual_response->cache_expression()); |
| 582 EXPECT_EQ(expected_response.cache_expression_exact_match(), | |
| 583 actual_response->cache_expression_exact_match()); | |
| 584 EXPECT_EQ(expected_response.cache_duration_sec(), | 508 EXPECT_EQ(expected_response.cache_duration_sec(), |
| 585 actual_response->cache_duration_sec()); | 509 actual_response->cache_duration_sec()); |
| 586 } | 510 } |
| 587 | 511 |
| 588 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { | 512 TEST_F(PasswordProtectionServiceTest, TestTearDownWithPendingRequests) { |
| 589 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); | 513 histograms_.ExpectTotalCount(kRequestOutcomeHistogramName, 0); |
| 590 GURL target_url(kTargetUrl); | 514 GURL target_url(kTargetUrl); |
| 591 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) | 515 EXPECT_CALL(*database_manager_.get(), MatchCsdWhitelistUrl(target_url)) |
| 592 .WillRepeatedly(testing::Return(false)); | 516 .WillRepeatedly(testing::Return(false)); |
| 593 password_protection_service_->StartRequest( | 517 password_protection_service_->StartRequest( |
| 594 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); | 518 target_url, LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE); |
| 595 | 519 |
| 596 // Destroy password_protection_service_ while there is one request pending. | 520 // Destroy password_protection_service_ while there is one request pending. |
| 597 password_protection_service_.reset(); | 521 password_protection_service_.reset(); |
| 598 base::RunLoop().RunUntilIdle(); | 522 base::RunLoop().RunUntilIdle(); |
| 599 | 523 |
| 600 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), | 524 EXPECT_THAT(histograms_.GetAllSamples(kRequestOutcomeHistogramName), |
| 601 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); | 525 testing::ElementsAre(base::Bucket(2 /* CANCELED */, 1))); |
| 602 } | 526 } |
| 603 } // namespace safe_browsing | 527 } // namespace safe_browsing |
| OLD | NEW |