| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 | 4 |
| 5 #include "components/safe_browsing_db/database_manager.h" | 5 #include "components/safe_browsing_db/database_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 const std::vector<std::string>& permissions3 = | 448 const std::vector<std::string>& permissions3 = |
| 449 client3.GetBlockedPermissions(); | 449 client3.GetBlockedPermissions(); |
| 450 EXPECT_EQ(3ul, permissions3.size()); | 450 EXPECT_EQ(3ul, permissions3.size()); |
| 451 // TODO(kcarattini): Fix the metadata storage of permissions to avoid | 451 // TODO(kcarattini): Fix the metadata storage of permissions to avoid |
| 452 // duplicates. | 452 // duplicates. |
| 453 EXPECT_EQ("GEOLOCATION", permissions3[0]); | 453 EXPECT_EQ("GEOLOCATION", permissions3[0]); |
| 454 EXPECT_EQ("NOTIFICATIONS", permissions3[1]); | 454 EXPECT_EQ("NOTIFICATIONS", permissions3[1]); |
| 455 EXPECT_EQ("GEOLOCATION", permissions3[2]); | 455 EXPECT_EQ("GEOLOCATION", permissions3[2]); |
| 456 } | 456 } |
| 457 | 457 |
| 458 TEST_F(SafeBrowsingDatabaseManagerTest, CachedResultsAreEvicted) { |
| 459 base::Time epoch = base::Time::UnixEpoch(); |
| 460 SBFullHashResult full_hash_result; |
| 461 full_hash_result.hash = SBFullHashForString("example.com/"); |
| 462 full_hash_result.cache_expire_after = epoch; |
| 463 |
| 464 SafeBrowsingDatabaseManager::PrefixToFullHashResultsMap& cache = |
| 465 db_manager_->v4_full_hash_cache_[SB_THREAT_TYPE_API_ABUSE]; |
| 466 |
| 467 // Fill the cache with some expired entries. |
| 468 // Both negative cache and full hash expired. |
| 469 cache[full_hash_result.hash.prefix] = SBCachedFullHashResult(epoch); |
| 470 cache[full_hash_result.hash.prefix].full_hashes.push_back(full_hash_result); |
| 471 |
| 472 TestClient client; |
| 473 const GURL url("https://www.example.com/more"); |
| 474 |
| 475 EXPECT_EQ(1ul, cache.size()); |
| 476 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 477 base::RunLoop().RunUntilIdle(); |
| 478 |
| 479 // Cache should be empty. |
| 480 EXPECT_TRUE(client.callback_invoked()); |
| 481 EXPECT_TRUE(cache.empty()); |
| 482 |
| 483 // Negative cache still valid and full hash expired. |
| 484 cache[full_hash_result.hash.prefix] = |
| 485 SBCachedFullHashResult(base::Time::Max()); |
| 486 cache[full_hash_result.hash.prefix].full_hashes.push_back(full_hash_result); |
| 487 |
| 488 EXPECT_EQ(1ul, cache.size()); |
| 489 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 490 base::RunLoop().RunUntilIdle(); |
| 491 |
| 492 // Cache entry should still be there. |
| 493 EXPECT_EQ(1ul, cache.size()); |
| 494 auto entry = cache.find(full_hash_result.hash.prefix); |
| 495 EXPECT_NE(cache.end(), entry); |
| 496 EXPECT_EQ(base::Time::Max(), entry->second.expire_after); |
| 497 EXPECT_EQ(1ul, entry->second.full_hashes.size()); |
| 498 EXPECT_TRUE(SBFullHashEqual(full_hash_result.hash, |
| 499 entry->second.full_hashes[0].hash)); |
| 500 EXPECT_EQ(full_hash_result.cache_expire_after, |
| 501 entry->second.full_hashes[0].cache_expire_after); |
| 502 |
| 503 // Negative cache still valid and full hash still valid. |
| 504 cache[full_hash_result.hash.prefix].full_hashes[0]. |
| 505 cache_expire_after = base::Time::Max(); |
| 506 |
| 507 EXPECT_EQ(1ul, cache.size()); |
| 508 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 509 base::RunLoop().RunUntilIdle(); |
| 510 |
| 511 // Cache entry should still be there. |
| 512 EXPECT_EQ(1ul, cache.size()); |
| 513 entry = cache.find(full_hash_result.hash.prefix); |
| 514 EXPECT_NE(cache.end(), entry); |
| 515 EXPECT_EQ(base::Time::Max(), entry->second.expire_after); |
| 516 EXPECT_EQ(1ul, entry->second.full_hashes.size()); |
| 517 EXPECT_TRUE(SBFullHashEqual(full_hash_result.hash, |
| 518 entry->second.full_hashes[0].hash)); |
| 519 EXPECT_EQ(base::Time::Max(), |
| 520 entry->second.full_hashes[0].cache_expire_after); |
| 521 |
| 522 // Negative cache expired and full hash still valid. |
| 523 cache[full_hash_result.hash.prefix].expire_after = epoch; |
| 524 |
| 525 EXPECT_EQ(1ul, cache.size()); |
| 526 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 527 base::RunLoop().RunUntilIdle(); |
| 528 |
| 529 // Cache entry should still be there. |
| 530 EXPECT_EQ(1ul, cache.size()); |
| 531 entry = cache.find(full_hash_result.hash.prefix); |
| 532 EXPECT_NE(cache.end(), entry); |
| 533 EXPECT_EQ(epoch, entry->second.expire_after); |
| 534 EXPECT_EQ(1ul, entry->second.full_hashes.size()); |
| 535 EXPECT_TRUE(SBFullHashEqual(full_hash_result.hash, |
| 536 entry->second.full_hashes[0].hash)); |
| 537 EXPECT_EQ(base::Time::Max(), |
| 538 entry->second.full_hashes[0].cache_expire_after); |
| 539 } |
| 540 |
| 458 } // namespace safe_browsing | 541 } // namespace safe_browsing |
| OLD | NEW |