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