Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // Unit tests for the SafeBrowsing storage system. | 5 // Unit tests for the SafeBrowsing storage system. |
| 6 | 6 |
| 7 #include "chrome/browser/safe_browsing/safe_browsing_database.h" | 7 #include "chrome/browser/safe_browsing/safe_browsing_database.h" |
| 8 | 8 |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 raw_data->set_hashes(data, data_size); | 74 raw_data->set_hashes(data, data_size); |
| 75 raw_data->clear_add_numbers(); | 75 raw_data->clear_add_numbers(); |
| 76 for (size_t i = 0; i < add_chunk_numbers.size(); ++i) { | 76 for (size_t i = 0; i < add_chunk_numbers.size(); ++i) { |
| 77 raw_data->add_add_numbers(add_chunk_numbers[i]); | 77 raw_data->add_add_numbers(add_chunk_numbers[i]); |
| 78 } | 78 } |
| 79 | 79 |
| 80 return new SBChunkData(raw_data.release()); | 80 return new SBChunkData(raw_data.release()); |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Create add chunk with a single prefix. | 83 // Create add chunk with a single prefix. |
| 84 SBChunkData* AddChunkPrefix(int chunk_number, SBPrefix prefix) { | 84 SBChunkData* AddChunkPrefix(int chunk_number, SBPrefix prefix) { |
| 85 return BuildChunk(chunk_number, safe_browsing::ChunkData::ADD, | 85 return BuildChunk(chunk_number, safe_browsing::ChunkData::ADD, |
| 86 safe_browsing::ChunkData::PREFIX_4B, | 86 safe_browsing::ChunkData::PREFIX_4B, |
| 87 &prefix, sizeof(prefix), | 87 &prefix, sizeof(prefix), |
| 88 std::vector<int>()); | 88 std::vector<int>()); |
| 89 } | 89 } |
| 90 | 90 |
| 91 // Create add chunk with a single prefix generated from |value|. | 91 // Create add chunk with a single prefix generated from |value|. |
| 92 SBChunkData* AddChunkPrefixValue(int chunk_number, | 92 SBChunkData* AddChunkPrefixValue(int chunk_number, |
| 93 const std::string& value) { | 93 const std::string& value) { |
| 94 return AddChunkPrefix(chunk_number, SBPrefixForString(value)); | 94 return AddChunkPrefix(chunk_number, SBPrefixForString(value)); |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 | 252 |
| 253 } // namespace | 253 } // namespace |
| 254 | 254 |
| 255 class SafeBrowsingDatabaseTest : public PlatformTest { | 255 class SafeBrowsingDatabaseTest : public PlatformTest { |
| 256 public: | 256 public: |
| 257 void SetUp() override { | 257 void SetUp() override { |
| 258 PlatformTest::SetUp(); | 258 PlatformTest::SetUp(); |
| 259 | 259 |
| 260 // Setup a database in a temporary directory. | 260 // Setup a database in a temporary directory. |
| 261 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 261 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 262 database_.reset(new SafeBrowsingDatabaseNew); | |
| 263 database_filename_ = | 262 database_filename_ = |
| 264 temp_dir_.path().AppendASCII("SafeBrowsingTestDatabase"); | 263 temp_dir_.path().AppendASCII("SafeBrowsingTestDatabase"); |
| 265 database_->Init(database_filename_); | 264 |
| 265 ResetandReloadFullDatabase(); | |
| 266 } | 266 } |
| 267 | 267 |
| 268 void TearDown() override { | 268 void TearDown() override { |
| 269 database_.reset(); | 269 database_.reset(); |
| 270 | 270 |
| 271 PlatformTest::TearDown(); | 271 PlatformTest::TearDown(); |
| 272 } | 272 } |
| 273 | 273 |
| 274 // Reloads the |database_| in a new SafeBrowsingDatabaseNew object with all | |
| 275 // stores enabled. | |
| 276 void ResetandReloadFullDatabase() { | |
| 277 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); | |
| 278 SafeBrowsingStoreFile* download_store = new SafeBrowsingStoreFile(); | |
| 279 SafeBrowsingStoreFile* csd_whitelist_store = new SafeBrowsingStoreFile(); | |
| 280 SafeBrowsingStoreFile* download_whitelist_store = | |
| 281 new SafeBrowsingStoreFile(); | |
| 282 SafeBrowsingStoreFile* extension_blacklist_store = | |
| 283 new SafeBrowsingStoreFile(); | |
| 284 SafeBrowsingStoreFile* side_effect_free_whitelist_store = | |
| 285 new SafeBrowsingStoreFile(); | |
| 286 SafeBrowsingStoreFile* ip_blacklist_store = new SafeBrowsingStoreFile(); | |
| 287 SafeBrowsingStoreFile* unwanted_software_store = | |
| 288 new SafeBrowsingStoreFile(); | |
| 289 database_.reset( | |
| 290 new SafeBrowsingDatabaseNew(browse_store, | |
| 291 download_store, | |
| 292 csd_whitelist_store, | |
| 293 download_whitelist_store, | |
| 294 extension_blacklist_store, | |
| 295 side_effect_free_whitelist_store, | |
| 296 ip_blacklist_store, | |
| 297 unwanted_software_store)); | |
| 298 database_->Init(database_filename_); | |
| 299 } | |
| 300 | |
| 274 void GetListsInfo(std::vector<SBListChunkRanges>* lists) { | 301 void GetListsInfo(std::vector<SBListChunkRanges>* lists) { |
| 275 lists->clear(); | 302 lists->clear(); |
| 276 ASSERT_TRUE(database_->UpdateStarted(lists)); | 303 ASSERT_TRUE(database_->UpdateStarted(lists)); |
| 277 database_->UpdateFinished(true); | 304 database_->UpdateFinished(true); |
| 278 } | 305 } |
| 279 | 306 |
| 280 // Helper function to do an AddDel or SubDel command. | 307 // Helper function to do an AddDel or SubDel command. |
| 281 void DelChunk(const std::string& list, | 308 void DelChunk(const std::string& list, |
| 282 int chunk_id, | 309 int chunk_id, |
| 283 bool is_sub_del) { | 310 bool is_sub_del) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 chunks.push_back( | 380 chunks.push_back( |
| 354 SubChunkPrefixValue(200, "www.phishy.com/notevil1.html", 1999)); | 381 SubChunkPrefixValue(200, "www.phishy.com/notevil1.html", 1999)); |
| 355 chunks.push_back( | 382 chunks.push_back( |
| 356 SubChunkPrefixValue(201, "www.phishy2.com/notevil1.html", 1999)); | 383 SubChunkPrefixValue(201, "www.phishy2.com/notevil1.html", 1999)); |
| 357 | 384 |
| 358 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 385 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 359 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks.get()); | 386 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks.get()); |
| 360 database_->UpdateFinished(true); | 387 database_->UpdateFinished(true); |
| 361 | 388 |
| 362 GetListsInfo(&lists); | 389 GetListsInfo(&lists); |
| 363 ASSERT_EQ(2U, lists.size()); | 390 ASSERT_LE(2U, lists.size()); |
| 364 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); | 391 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 365 EXPECT_EQ("1-3", lists[0].adds); | 392 EXPECT_EQ("1-3", lists[0].adds); |
| 366 EXPECT_EQ("7", lists[0].subs); | 393 EXPECT_EQ("7", lists[0].subs); |
| 367 EXPECT_EQ(safe_browsing_util::kPhishingList, lists[1].name); | 394 EXPECT_EQ(safe_browsing_util::kPhishingList, lists[1].name); |
| 368 EXPECT_EQ("47", lists[1].adds); | 395 EXPECT_EQ("47", lists[1].adds); |
| 369 EXPECT_EQ("200-201", lists[1].subs); | 396 EXPECT_EQ("200-201", lists[1].subs); |
| 370 } | 397 } |
| 371 | 398 |
| 372 TEST_F(SafeBrowsingDatabaseTest, ListNameForBrowseAndDownload) { | 399 TEST_F(SafeBrowsingDatabaseTest, ListNameForBrowseAndDownload) { |
| 373 database_.reset(); | |
| 374 base::MessageLoop loop; | |
| 375 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); | |
| 376 SafeBrowsingStoreFile* download_store = new SafeBrowsingStoreFile(); | |
| 377 SafeBrowsingStoreFile* csd_whitelist_store = new SafeBrowsingStoreFile(); | |
| 378 SafeBrowsingStoreFile* download_whitelist_store = new SafeBrowsingStoreFile(); | |
| 379 SafeBrowsingStoreFile* extension_blacklist_store = | |
| 380 new SafeBrowsingStoreFile(); | |
| 381 SafeBrowsingStoreFile* ip_blacklist_store = new SafeBrowsingStoreFile(); | |
| 382 database_.reset(new SafeBrowsingDatabaseNew(browse_store, | |
| 383 download_store, | |
| 384 csd_whitelist_store, | |
| 385 download_whitelist_store, | |
| 386 extension_blacklist_store, | |
| 387 NULL, | |
| 388 ip_blacklist_store)); | |
| 389 database_->Init(database_filename_); | |
| 390 | |
| 391 ScopedVector<SBChunkData> chunks; | 400 ScopedVector<SBChunkData> chunks; |
| 392 | 401 |
| 393 std::vector<SBListChunkRanges> lists; | 402 std::vector<SBListChunkRanges> lists; |
| 394 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 403 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 395 | 404 |
| 396 // Insert malware, phish, binurl and bindownload add chunks. | 405 // Insert malware, phish, binurl and bindownload add chunks. |
| 397 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); | 406 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); |
| 398 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 407 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 399 | 408 |
| 400 chunks.clear(); | 409 chunks.clear(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 417 chunks.push_back(AddChunkFullHashValue(8, | 426 chunks.push_back(AddChunkFullHashValue(8, |
| 418 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | 427 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 419 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | 428 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); |
| 420 database_->InsertChunks(safe_browsing_util::kExtensionBlacklist, | 429 database_->InsertChunks(safe_browsing_util::kExtensionBlacklist, |
| 421 chunks.get()); | 430 chunks.get()); |
| 422 | 431 |
| 423 chunks.clear(); | 432 chunks.clear(); |
| 424 chunks.push_back(AddChunkHashedIpValue(9, "::ffff:192.168.1.0", 120)); | 433 chunks.push_back(AddChunkHashedIpValue(9, "::ffff:192.168.1.0", 120)); |
| 425 database_->InsertChunks(safe_browsing_util::kIPBlacklist, chunks.get()); | 434 database_->InsertChunks(safe_browsing_util::kIPBlacklist, chunks.get()); |
| 426 | 435 |
| 436 chunks.clear(); | |
| 437 chunks.push_back(AddChunkPrefixValue(10, "www.unwanted.com/software.html")); | |
| 438 database_->InsertChunks(safe_browsing_util::kUnwantedUrlList, chunks.get()); | |
| 439 | |
| 427 database_->UpdateFinished(true); | 440 database_->UpdateFinished(true); |
| 428 | 441 |
| 429 GetListsInfo(&lists); | 442 GetListsInfo(&lists); |
| 430 ASSERT_EQ(7U, lists.size()); | 443 ASSERT_EQ(9U, lists.size()); |
| 431 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); | 444 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 432 EXPECT_EQ("1", lists[0].adds); | 445 EXPECT_EQ("1", lists[0].adds); |
| 433 EXPECT_TRUE(lists[0].subs.empty()); | 446 EXPECT_TRUE(lists[0].subs.empty()); |
| 434 EXPECT_EQ(safe_browsing_util::kPhishingList, lists[1].name); | 447 EXPECT_EQ(safe_browsing_util::kPhishingList, lists[1].name); |
| 435 EXPECT_EQ("2", lists[1].adds); | 448 EXPECT_EQ("2", lists[1].adds); |
| 436 EXPECT_TRUE(lists[1].subs.empty()); | 449 EXPECT_TRUE(lists[1].subs.empty()); |
| 437 EXPECT_EQ(safe_browsing_util::kBinUrlList, lists[2].name); | 450 EXPECT_EQ(safe_browsing_util::kBinUrlList, lists[2].name); |
| 438 EXPECT_EQ("3", lists[2].adds); | 451 EXPECT_EQ("3", lists[2].adds); |
| 439 EXPECT_TRUE(lists[2].subs.empty()); | 452 EXPECT_TRUE(lists[2].subs.empty()); |
| 440 EXPECT_EQ(safe_browsing_util::kCsdWhiteList, lists[3].name); | 453 EXPECT_EQ(safe_browsing_util::kCsdWhiteList, lists[3].name); |
| 441 EXPECT_EQ("5", lists[3].adds); | 454 EXPECT_EQ("5", lists[3].adds); |
| 442 EXPECT_TRUE(lists[3].subs.empty()); | 455 EXPECT_TRUE(lists[3].subs.empty()); |
| 443 EXPECT_EQ(safe_browsing_util::kDownloadWhiteList, lists[4].name); | 456 EXPECT_EQ(safe_browsing_util::kDownloadWhiteList, lists[4].name); |
| 444 EXPECT_EQ("6", lists[4].adds); | 457 EXPECT_EQ("6", lists[4].adds); |
| 445 EXPECT_TRUE(lists[4].subs.empty()); | 458 EXPECT_TRUE(lists[4].subs.empty()); |
| 446 EXPECT_EQ(safe_browsing_util::kExtensionBlacklist, lists[5].name); | 459 EXPECT_EQ(safe_browsing_util::kExtensionBlacklist, lists[5].name); |
| 447 EXPECT_EQ("8", lists[5].adds); | 460 EXPECT_EQ("8", lists[5].adds); |
| 448 EXPECT_TRUE(lists[5].subs.empty()); | 461 EXPECT_TRUE(lists[5].subs.empty()); |
| 449 EXPECT_EQ(safe_browsing_util::kIPBlacklist, lists[6].name); | 462 EXPECT_EQ(safe_browsing_util::kIPBlacklist, lists[7].name); |
| 450 EXPECT_EQ("9", lists[6].adds); | 463 EXPECT_EQ("9", lists[7].adds); |
| 451 EXPECT_TRUE(lists[6].subs.empty()); | 464 EXPECT_TRUE(lists[7].subs.empty()); |
| 465 EXPECT_EQ(safe_browsing_util::kUnwantedUrlList, lists[8].name); | |
| 466 EXPECT_EQ("10", lists[8].adds); | |
| 467 EXPECT_TRUE(lists[8].subs.empty()); | |
| 452 | 468 |
| 453 database_.reset(); | 469 database_.reset(); |
| 454 } | 470 } |
| 455 | 471 |
| 456 // Checks database reading and writing for browse. | 472 // Checks database reading and writing for browse and unwanted PrefixSets. |
| 457 TEST_F(SafeBrowsingDatabaseTest, BrowseDatabase) { | 473 TEST_F(SafeBrowsingDatabaseTest, BrowseAndUnwantedDatabasesAndPrefixSets) { |
| 458 std::vector<SBListChunkRanges> lists; | 474 struct TestCase { |
| 459 ScopedVector<SBChunkData> chunks; | 475 using TestListContainsBadUrl = bool (SafeBrowsingDatabase::*)( |
| 460 | 476 const GURL& url, |
| 461 chunks.push_back(AddChunkPrefix2Value(1, | 477 std::vector<SBPrefix>* prefix_hits, |
| 462 "www.evil.com/phishing.html", | 478 std::vector<SBFullHashResult>* cache_hits); |
| 463 "www.evil.com/malware.html")); | 479 |
| 464 chunks.push_back(AddChunkPrefix4Value(2, | 480 const char* test_list_name; |
| 465 "www.evil.com/notevil1.html", | 481 size_t expected_list_index; |
| 466 "www.evil.com/notevil2.html", | 482 TestListContainsBadUrl test_list_contains_bad_url; |
| 467 "www.good.com/good1.html", | 483 } const kTestCases[] { |
| 468 "www.good.com/good2.html")); | 484 { safe_browsing_util::kMalwareList, 0U, |
| 469 chunks.push_back(AddChunkPrefixValue(3, "192.168.0.1/malware.html")); | 485 &SafeBrowsingDatabase::ContainsBrowseUrl }, |
| 470 chunks.push_back(AddChunkFullHashValue(7, "www.evil.com/evil.html")); | 486 { safe_browsing_util::kPhishingList, 1U, |
| 471 | 487 &SafeBrowsingDatabase::ContainsBrowseUrl }, |
| 472 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 488 { safe_browsing_util::kUnwantedUrlList, 8U, |
| 473 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 489 &SafeBrowsingDatabase::ContainsUnwantedSoftwareUrl }, |
| 474 database_->UpdateFinished(true); | 490 }; |
| 475 | 491 |
| 476 // Make sure they were added correctly. | 492 for (const auto& test_case : kTestCases) { |
| 477 GetListsInfo(&lists); | 493 LOG(INFO) << "Testing " << test_case.test_list_name; |
|
mattm
2014/11/11 01:29:11
use SCOPED_TRACE
gab
2014/11/11 23:39:12
Oh nice! Didn't know about SCOPED_TRACE, this is e
| |
| 478 ASSERT_LE(1U, lists.size()); | 494 |
| 479 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); | 495 std::vector<SBListChunkRanges> lists; |
| 480 EXPECT_EQ("1-3,7", lists[0].adds); | 496 ScopedVector<SBChunkData> chunks; |
| 481 EXPECT_TRUE(lists[0].subs.empty()); | 497 |
| 482 | 498 chunks.push_back(AddChunkPrefix2Value(1, |
| 483 std::vector<SBPrefix> prefix_hits; | 499 "www.evil.com/phishing.html", |
| 484 std::vector<SBFullHashResult> cache_hits; | 500 "www.evil.com/malware.html")); |
| 485 EXPECT_TRUE(database_->ContainsBrowseUrl( | 501 chunks.push_back(AddChunkPrefix4Value(2, |
| 486 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 502 "www.evil.com/notevil1.html", |
| 487 ASSERT_EQ(1U, prefix_hits.size()); | 503 "www.evil.com/notevil2.html", |
| 488 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); | 504 "www.good.com/good1.html", |
| 489 EXPECT_TRUE(cache_hits.empty()); | 505 "www.good.com/good2.html")); |
| 490 | 506 chunks.push_back(AddChunkPrefixValue(3, "192.168.0.1/malware.html")); |
| 491 EXPECT_TRUE(database_->ContainsBrowseUrl( | 507 chunks.push_back(AddChunkFullHashValue(7, "www.evil.com/evil.html")); |
| 492 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 508 |
| 493 | 509 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 494 EXPECT_TRUE(database_->ContainsBrowseUrl( | 510 database_->InsertChunks(test_case.test_list_name, chunks.get()); |
| 495 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits)); | 511 database_->UpdateFinished(true); |
| 496 | 512 |
| 497 EXPECT_TRUE(database_->ContainsBrowseUrl( | 513 // Make sure they were added correctly. |
| 498 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); | 514 GetListsInfo(&lists); |
| 499 | 515 |
| 500 EXPECT_TRUE(database_->ContainsBrowseUrl( | 516 ASSERT_LE(1U, lists.size()); |
| 501 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); | 517 EXPECT_EQ(test_case.test_list_name, |
| 502 | 518 lists[test_case.expected_list_index].name); |
| 503 EXPECT_TRUE(database_->ContainsBrowseUrl( | 519 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds); |
| 504 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); | 520 EXPECT_TRUE(lists[test_case.expected_list_index].subs.empty()); |
| 505 | 521 |
| 506 EXPECT_TRUE(database_->ContainsBrowseUrl( | 522 std::vector<SBPrefix> prefix_hits; |
| 507 GURL("http://192.168.0.1/malware.html"), &prefix_hits, &cache_hits)); | 523 std::vector<SBFullHashResult> cache_hits; |
| 508 | 524 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 509 EXPECT_FALSE(database_->ContainsBrowseUrl( | 525 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 510 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits)); | 526 ASSERT_EQ(1U, prefix_hits.size()); |
| 511 EXPECT_TRUE(prefix_hits.empty()); | 527 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); |
| 512 EXPECT_TRUE(cache_hits.empty()); | 528 EXPECT_TRUE(cache_hits.empty()); |
| 513 | 529 |
| 514 EXPECT_FALSE(database_->ContainsBrowseUrl( | 530 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 515 GURL("http://www.evil.com/robots.txt"), &prefix_hits, &cache_hits)); | 531 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 516 | 532 |
| 517 EXPECT_TRUE(database_->ContainsBrowseUrl( | 533 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 518 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits)); | 534 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits)); |
| 519 ASSERT_EQ(1U, prefix_hits.size()); | 535 |
| 520 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]); | 536 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 521 | 537 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); |
| 522 // Attempt to re-add the first chunk (should be a no-op). | 538 |
| 523 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 | 539 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 524 chunks.clear(); | 540 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); |
| 525 chunks.push_back(AddChunkPrefix2Value(1, | 541 |
| 526 "www.evil.com/phishing.html", | 542 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 527 "www.evil.com/malware.html")); | 543 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); |
| 528 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 544 |
| 529 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 545 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 530 database_->UpdateFinished(true); | 546 GURL("http://192.168.0.1/malware.html"), &prefix_hits, &cache_hits)); |
| 531 | 547 |
| 532 GetListsInfo(&lists); | 548 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 533 ASSERT_LE(1U, lists.size()); | 549 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits)); |
| 534 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); | 550 EXPECT_TRUE(prefix_hits.empty()); |
| 535 EXPECT_EQ("1-3,7", lists[0].adds); | 551 EXPECT_TRUE(cache_hits.empty()); |
| 536 EXPECT_TRUE(lists[0].subs.empty()); | 552 |
| 537 | 553 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 538 // Test removing a single prefix from the add chunk. | 554 GURL("http://www.evil.com/robots.txt"), &prefix_hits, &cache_hits)); |
| 539 chunks.clear(); | 555 |
| 540 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/notevil1.html", 2)); | 556 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 541 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 557 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits)); |
| 542 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 558 ASSERT_EQ(1U, prefix_hits.size()); |
| 543 database_->UpdateFinished(true); | 559 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]); |
| 544 | 560 |
| 545 EXPECT_TRUE(database_->ContainsBrowseUrl( | 561 // Attempt to re-add the first chunk (should be a no-op). |
| 546 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 562 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 |
| 547 ASSERT_EQ(1U, prefix_hits.size()); | 563 chunks.clear(); |
| 548 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); | 564 chunks.push_back(AddChunkPrefix2Value(1, |
| 549 EXPECT_TRUE(cache_hits.empty()); | 565 "www.evil.com/phishing.html", |
| 550 | 566 "www.evil.com/malware.html")); |
| 551 EXPECT_FALSE(database_->ContainsBrowseUrl( | 567 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 552 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits)); | 568 database_->InsertChunks(test_case.test_list_name, chunks.get()); |
| 553 EXPECT_TRUE(prefix_hits.empty()); | 569 database_->UpdateFinished(true); |
| 554 EXPECT_TRUE(cache_hits.empty()); | 570 |
| 555 | 571 GetListsInfo(&lists); |
| 556 EXPECT_TRUE(database_->ContainsBrowseUrl( | 572 ASSERT_LE(1U, lists.size()); |
| 557 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); | 573 EXPECT_EQ(test_case.test_list_name, |
| 558 | 574 lists[test_case.expected_list_index].name); |
| 559 EXPECT_TRUE(database_->ContainsBrowseUrl( | 575 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds); |
| 560 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); | 576 EXPECT_TRUE(lists[test_case.expected_list_index].subs.empty()); |
| 561 | 577 |
| 562 EXPECT_TRUE(database_->ContainsBrowseUrl( | 578 // Test removing a single prefix from the add chunk. |
| 563 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); | 579 chunks.clear(); |
| 564 | 580 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/notevil1.html", 2)); |
| 565 GetListsInfo(&lists); | 581 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 566 ASSERT_LE(1U, lists.size()); | 582 database_->InsertChunks(test_case.test_list_name, chunks.get()); |
| 567 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); | 583 database_->UpdateFinished(true); |
| 568 EXPECT_EQ("1-3,7", lists[0].adds); | 584 |
| 569 EXPECT_EQ("4", lists[0].subs); | 585 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 570 | 586 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 571 // Test the same sub chunk again. This should be a no-op. | 587 ASSERT_EQ(1U, prefix_hits.size()); |
| 572 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 | 588 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); |
| 573 chunks.clear(); | 589 EXPECT_TRUE(cache_hits.empty()); |
| 574 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/notevil1.html", 2)); | 590 |
| 575 | 591 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 576 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 592 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits)); |
| 577 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 593 EXPECT_TRUE(prefix_hits.empty()); |
| 578 database_->UpdateFinished(true); | 594 EXPECT_TRUE(cache_hits.empty()); |
| 579 | 595 |
| 580 GetListsInfo(&lists); | 596 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 581 ASSERT_LE(1U, lists.size()); | 597 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); |
| 582 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); | 598 |
| 583 EXPECT_EQ("1-3,7", lists[0].adds); | 599 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 584 EXPECT_EQ("4", lists[0].subs); | 600 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); |
| 585 | 601 |
| 586 // Test removing all the prefixes from an add chunk. | 602 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 587 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 603 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); |
| 588 AddDelChunk(safe_browsing_util::kMalwareList, 2); | 604 |
| 589 database_->UpdateFinished(true); | 605 GetListsInfo(&lists); |
| 590 | 606 ASSERT_LE(1U, lists.size()); |
| 591 EXPECT_FALSE(database_->ContainsBrowseUrl( | 607 EXPECT_EQ(test_case.test_list_name, |
| 592 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); | 608 lists[test_case.expected_list_index].name); |
| 593 | 609 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds); |
| 594 EXPECT_FALSE(database_->ContainsBrowseUrl( | 610 EXPECT_EQ("4", lists[test_case.expected_list_index].subs); |
| 595 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); | 611 |
| 596 | 612 // Test the same sub chunk again. This should be a no-op. |
| 597 EXPECT_FALSE(database_->ContainsBrowseUrl( | 613 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 |
| 598 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); | 614 chunks.clear(); |
| 599 | 615 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/notevil1.html", 2)); |
| 600 GetListsInfo(&lists); | 616 |
| 601 ASSERT_LE(1U, lists.size()); | 617 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 602 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); | 618 database_->InsertChunks(test_case.test_list_name, chunks.get()); |
| 603 EXPECT_EQ("1,3,7", lists[0].adds); | 619 database_->UpdateFinished(true); |
| 604 EXPECT_EQ("4", lists[0].subs); | 620 |
| 605 | 621 GetListsInfo(&lists); |
| 606 // The adddel command exposed a bug in the transaction code where any | 622 ASSERT_LE(1U, lists.size()); |
| 607 // transaction after it would fail. Add a dummy entry and remove it to | 623 EXPECT_EQ(test_case.test_list_name, |
| 608 // make sure the transcation works fine. | 624 lists[test_case.expected_list_index].name); |
| 609 chunks.clear(); | 625 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds); |
| 610 chunks.push_back(AddChunkPrefixValue(44, "www.redherring.com/index.html")); | 626 EXPECT_EQ("4", lists[test_case.expected_list_index].subs); |
| 611 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 627 |
| 612 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 628 // Test removing all the prefixes from an add chunk. |
| 613 | 629 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 614 // Now remove the dummy entry. If there are any problems with the | 630 AddDelChunk(test_case.test_list_name, 2); |
| 615 // transactions, asserts will fire. | 631 database_->UpdateFinished(true); |
| 616 AddDelChunk(safe_browsing_util::kMalwareList, 44); | 632 |
| 617 | 633 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 618 // Test the subdel command. | 634 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); |
| 619 SubDelChunk(safe_browsing_util::kMalwareList, 4); | 635 |
| 620 database_->UpdateFinished(true); | 636 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 621 | 637 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); |
| 622 GetListsInfo(&lists); | 638 |
| 623 ASSERT_LE(1U, lists.size()); | 639 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 624 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); | 640 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); |
| 625 EXPECT_EQ("1,3,7", lists[0].adds); | 641 |
| 626 EXPECT_TRUE(lists[0].subs.empty()); | 642 GetListsInfo(&lists); |
| 627 | 643 ASSERT_LE(1U, lists.size()); |
| 628 // Test a sub command coming in before the add. | 644 EXPECT_EQ(test_case.test_list_name, |
| 629 chunks.clear(); | 645 lists[test_case.expected_list_index].name); |
| 630 chunks.push_back(SubChunkPrefix2Value(5, | 646 EXPECT_EQ("1,3,7", lists[test_case.expected_list_index].adds); |
| 631 "www.notevilanymore.com/index.html", | 647 EXPECT_EQ("4", lists[test_case.expected_list_index].subs); |
| 632 10, | 648 |
| 633 "www.notevilanymore.com/good.html", | 649 // The adddel command exposed a bug in the transaction code where any |
| 634 10)); | 650 // transaction after it would fail. Add a dummy entry and remove it to |
| 635 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 651 // make sure the transcation works fine. |
| 636 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 652 chunks.clear(); |
| 637 database_->UpdateFinished(true); | 653 chunks.push_back(AddChunkPrefixValue(44, "www.redherring.com/index.html")); |
| 638 | 654 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 639 EXPECT_FALSE(database_->ContainsBrowseUrl( | 655 database_->InsertChunks(test_case.test_list_name, chunks.get()); |
| 640 GURL("http://www.notevilanymore.com/index.html"), | 656 |
| 641 &prefix_hits, | 657 // Now remove the dummy entry. If there are any problems with the |
| 642 &cache_hits)); | 658 // transactions, asserts will fire. |
| 643 | 659 AddDelChunk(test_case.test_list_name, 44); |
| 644 // Now insert the tardy add chunk and we don't expect them to appear | 660 |
| 645 // in database because of the previous sub chunk. | 661 // Test the subdel command. |
| 646 chunks.clear(); | 662 SubDelChunk(test_case.test_list_name, 4); |
| 647 chunks.push_back(AddChunkPrefix2Value(10, | 663 database_->UpdateFinished(true); |
| 648 "www.notevilanymore.com/index.html", | 664 |
| 649 "www.notevilanymore.com/good.html")); | 665 GetListsInfo(&lists); |
| 650 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 666 ASSERT_LE(1U, lists.size()); |
| 651 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 667 EXPECT_EQ(test_case.test_list_name, |
| 652 database_->UpdateFinished(true); | 668 lists[test_case.expected_list_index].name); |
| 653 | 669 EXPECT_EQ("1,3,7", lists[test_case.expected_list_index].adds); |
| 654 EXPECT_FALSE(database_->ContainsBrowseUrl( | 670 EXPECT_TRUE(lists[test_case.expected_list_index].subs.empty()); |
| 655 GURL("http://www.notevilanymore.com/index.html"), | 671 |
| 656 &prefix_hits, | 672 // Test a sub command coming in before the add. |
| 657 &cache_hits)); | 673 chunks.clear(); |
| 658 | 674 chunks.push_back(SubChunkPrefix2Value(5, |
| 659 EXPECT_FALSE(database_->ContainsBrowseUrl( | 675 "www.notevilanymore.com/index.html", |
| 660 GURL("http://www.notevilanymore.com/good.html"), | 676 10, |
| 661 &prefix_hits, | 677 "www.notevilanymore.com/good.html", |
| 662 &cache_hits)); | 678 10)); |
| 663 | 679 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 664 // Reset and reload the database. The database will rely on the prefix set. | 680 database_->InsertChunks(test_case.test_list_name, chunks.get()); |
| 665 database_.reset(new SafeBrowsingDatabaseNew); | 681 database_->UpdateFinished(true); |
| 666 database_->Init(database_filename_); | 682 |
| 667 | 683 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 668 // Check that a prefix still hits. | 684 GURL("http://www.notevilanymore.com/index.html"), |
| 669 EXPECT_TRUE(database_->ContainsBrowseUrl( | 685 &prefix_hits, |
| 670 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 686 &cache_hits)); |
| 671 ASSERT_EQ(1U, prefix_hits.size()); | 687 |
| 672 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); | 688 // Now insert the tardy add chunk and we don't expect them to appear |
| 673 | 689 // in database because of the previous sub chunk. |
| 674 // Also check that it's not just always returning true in this case. | 690 chunks.clear(); |
| 675 EXPECT_FALSE(database_->ContainsBrowseUrl( | 691 chunks.push_back(AddChunkPrefix2Value(10, |
| 676 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits)); | 692 "www.notevilanymore.com/index.html", |
| 677 | 693 "www.notevilanymore.com/good.html")); |
| 678 // Check that the full hash is still present. | 694 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 679 EXPECT_TRUE(database_->ContainsBrowseUrl( | 695 database_->InsertChunks(test_case.test_list_name, chunks.get()); |
| 680 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits)); | 696 database_->UpdateFinished(true); |
| 681 ASSERT_EQ(1U, prefix_hits.size()); | 697 |
| 682 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]); | 698 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( |
| 699 GURL("http://www.notevilanymore.com/index.html"), | |
| 700 &prefix_hits, | |
| 701 &cache_hits)); | |
| 702 | |
| 703 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( | |
| 704 GURL("http://www.notevilanymore.com/good.html"), | |
| 705 &prefix_hits, | |
| 706 &cache_hits)); | |
| 707 | |
| 708 // Reset and reload the database. The database will rely on the prefix set. | |
| 709 ResetandReloadFullDatabase(); | |
| 710 | |
| 711 // Check that a prefix still hits. | |
| 712 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | |
| 713 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | |
| 714 ASSERT_EQ(1U, prefix_hits.size()); | |
| 715 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); | |
| 716 | |
| 717 // Also check that it's not just always returning true in this case. | |
| 718 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)( | |
| 719 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits)); | |
| 720 | |
| 721 // Check that the full hash is still present. | |
| 722 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)( | |
| 723 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits)); | |
| 724 ASSERT_EQ(1U, prefix_hits.size()); | |
| 725 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]); | |
| 726 } | |
| 683 } | 727 } |
| 684 | 728 |
| 685 // Test adding zero length chunks to the database. | 729 // Test adding zero length chunks to the database. |
| 686 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { | 730 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { |
| 687 std::vector<SBListChunkRanges> lists; | 731 std::vector<SBListChunkRanges> lists; |
| 688 ScopedVector<SBChunkData> chunks; | 732 ScopedVector<SBChunkData> chunks; |
| 689 | 733 |
| 690 // Populate with a couple of normal chunks. | 734 // Populate with a couple of normal chunks. |
| 691 chunks.push_back(AddChunkPrefix2Value(1, | 735 chunks.push_back(AddChunkPrefix2Value(1, |
| 692 "www.test.com/test1.html", | 736 "www.test.com/test1.html", |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 779 chunks.push_back(AddChunkPrefix2Value(1, | 823 chunks.push_back(AddChunkPrefix2Value(1, |
| 780 "www.evil.com/phishing.html", | 824 "www.evil.com/phishing.html", |
| 781 "www.evil.com/malware.html")); | 825 "www.evil.com/malware.html")); |
| 782 | 826 |
| 783 std::vector<SBListChunkRanges> lists; | 827 std::vector<SBListChunkRanges> lists; |
| 784 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 828 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 785 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 829 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 786 database_->UpdateFinished(true); | 830 database_->UpdateFinished(true); |
| 787 | 831 |
| 788 // Cache should be cleared after updating. | 832 // Cache should be cleared after updating. |
| 789 EXPECT_TRUE(database_->browse_gethash_cache_.empty()); | 833 EXPECT_TRUE(database_->prefix_gethash_cache_.empty()); |
| 790 | 834 |
| 791 SBFullHashResult full_hash; | 835 SBFullHashResult full_hash; |
| 792 full_hash.list_id = safe_browsing_util::MALWARE; | 836 full_hash.list_id = safe_browsing_util::MALWARE; |
| 793 | 837 |
| 794 std::vector<SBFullHashResult> results; | 838 std::vector<SBFullHashResult> results; |
| 795 std::vector<SBPrefix> prefixes; | 839 std::vector<SBPrefix> prefixes; |
| 796 | 840 |
| 797 // Add a fullhash result for each prefix. | 841 // Add a fullhash result for each prefix. |
| 798 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html"); | 842 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html"); |
| 799 results.push_back(full_hash); | 843 results.push_back(full_hash); |
| 800 prefixes.push_back(full_hash.hash.prefix); | 844 prefixes.push_back(full_hash.hash.prefix); |
| 801 | 845 |
| 802 full_hash.hash = SBFullHashForString("www.evil.com/malware.html"); | 846 full_hash.hash = SBFullHashForString("www.evil.com/malware.html"); |
| 803 results.push_back(full_hash); | 847 results.push_back(full_hash); |
| 804 prefixes.push_back(full_hash.hash.prefix); | 848 prefixes.push_back(full_hash.hash.prefix); |
| 805 | 849 |
| 806 database_->CacheHashResults(prefixes, results, kCacheLifetime); | 850 database_->CacheHashResults(prefixes, results, kCacheLifetime); |
| 807 } | 851 } |
| 808 | 852 |
| 809 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { | 853 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { |
| 810 PopulateDatabaseForCacheTest(); | 854 PopulateDatabaseForCacheTest(); |
| 811 | 855 |
| 812 // We should have both full hashes in the cache. | 856 // We should have both full hashes in the cache. |
| 813 EXPECT_EQ(2U, database_->browse_gethash_cache_.size()); | 857 EXPECT_EQ(2U, database_->prefix_gethash_cache_.size()); |
| 814 | 858 |
| 815 // Test the cache lookup for the first prefix. | 859 // Test the cache lookup for the first prefix. |
| 816 std::vector<SBPrefix> prefix_hits; | 860 std::vector<SBPrefix> prefix_hits; |
| 817 std::vector<SBFullHashResult> cache_hits; | 861 std::vector<SBFullHashResult> cache_hits; |
| 818 EXPECT_TRUE(database_->ContainsBrowseUrl( | 862 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 819 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 863 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 820 EXPECT_TRUE(prefix_hits.empty()); | 864 EXPECT_TRUE(prefix_hits.empty()); |
| 821 ASSERT_EQ(1U, cache_hits.size()); | 865 ASSERT_EQ(1U, cache_hits.size()); |
| 822 EXPECT_TRUE(SBFullHashEqual( | 866 EXPECT_TRUE(SBFullHashEqual( |
| 823 cache_hits[0].hash, SBFullHashForString("www.evil.com/phishing.html"))); | 867 cache_hits[0].hash, SBFullHashForString("www.evil.com/phishing.html"))); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 859 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 903 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 860 prefix_hits.clear(); | 904 prefix_hits.clear(); |
| 861 cache_hits.clear(); | 905 cache_hits.clear(); |
| 862 | 906 |
| 863 // Test that an AddDel for the original chunk removes the last cached entry. | 907 // Test that an AddDel for the original chunk removes the last cached entry. |
| 864 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 908 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 865 AddDelChunk(safe_browsing_util::kMalwareList, 1); | 909 AddDelChunk(safe_browsing_util::kMalwareList, 1); |
| 866 database_->UpdateFinished(true); | 910 database_->UpdateFinished(true); |
| 867 EXPECT_FALSE(database_->ContainsBrowseUrl( | 911 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 868 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 912 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 869 EXPECT_TRUE(database_->browse_gethash_cache_.empty()); | 913 EXPECT_TRUE(database_->prefix_gethash_cache_.empty()); |
| 870 prefix_hits.clear(); | 914 prefix_hits.clear(); |
| 871 cache_hits.clear(); | 915 cache_hits.clear(); |
| 872 | 916 |
| 873 // Test that the cache won't return expired values. First we have to adjust | 917 // Test that the cache won't return expired values. First we have to adjust |
| 874 // the cached entries' received time to make them older, since the database | 918 // the cached entries' received time to make them older, since the database |
| 875 // cache insert uses Time::Now(). First, store some entries. | 919 // cache insert uses Time::Now(). First, store some entries. |
| 876 PopulateDatabaseForCacheTest(); | 920 PopulateDatabaseForCacheTest(); |
| 877 | 921 |
| 878 std::map<SBPrefix, SBCachedFullHashResult>* hash_cache = | 922 std::map<SBPrefix, SBCachedFullHashResult>* hash_cache = |
| 879 &database_->browse_gethash_cache_; | 923 &database_->prefix_gethash_cache_; |
| 880 EXPECT_EQ(2U, hash_cache->size()); | 924 EXPECT_EQ(2U, hash_cache->size()); |
| 881 | 925 |
| 882 // Now adjust one of the entries times to be in the past. | 926 // Now adjust one of the entries times to be in the past. |
| 883 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html"); | 927 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html"); |
| 884 std::map<SBPrefix, SBCachedFullHashResult>::iterator iter = | 928 std::map<SBPrefix, SBCachedFullHashResult>::iterator iter = |
| 885 hash_cache->find(key); | 929 hash_cache->find(key); |
| 886 ASSERT_TRUE(iter != hash_cache->end()); | 930 ASSERT_TRUE(iter != hash_cache->end()); |
| 887 iter->second.expire_after = Time::Now() - TimeDelta::FromMinutes(1); | 931 iter->second.expire_after = Time::Now() - TimeDelta::FromMinutes(1); |
| 888 | 932 |
| 889 EXPECT_TRUE(database_->ContainsBrowseUrl( | 933 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1044 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. | 1088 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. |
| 1045 // http://crbug.com/56448 | 1089 // http://crbug.com/56448 |
| 1046 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { | 1090 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { |
| 1047 // Re-create the database in a captive message loop so that we can | 1091 // Re-create the database in a captive message loop so that we can |
| 1048 // influence task-posting. Database specifically needs to the | 1092 // influence task-posting. Database specifically needs to the |
| 1049 // file-backed. | 1093 // file-backed. |
| 1050 database_.reset(); | 1094 database_.reset(); |
| 1051 base::MessageLoop loop; | 1095 base::MessageLoop loop; |
| 1052 SafeBrowsingStoreFile* store = new SafeBrowsingStoreFile(); | 1096 SafeBrowsingStoreFile* store = new SafeBrowsingStoreFile(); |
| 1053 database_.reset(new SafeBrowsingDatabaseNew(store, NULL, NULL, NULL, NULL, | 1097 database_.reset(new SafeBrowsingDatabaseNew(store, NULL, NULL, NULL, NULL, |
| 1054 NULL, NULL)); | 1098 NULL, NULL, NULL)); |
| 1055 database_->Init(database_filename_); | 1099 database_->Init(database_filename_); |
| 1056 | 1100 |
| 1057 // This will cause an empty database to be created. | 1101 // This will cause an empty database to be created. |
| 1058 std::vector<SBListChunkRanges> lists; | 1102 std::vector<SBListChunkRanges> lists; |
| 1059 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1103 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1060 database_->UpdateFinished(true); | 1104 database_->UpdateFinished(true); |
| 1061 | 1105 |
| 1062 // Create a sub chunk to insert. | 1106 // Create a sub chunk to insert. |
| 1063 ScopedVector<SBChunkData> chunks; | 1107 ScopedVector<SBChunkData> chunks; |
| 1064 chunks.push_back(SubChunkPrefixValue(7, | 1108 chunks.push_back(SubChunkPrefixValue(7, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1099 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1143 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1100 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 1144 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1101 database_->UpdateFinished(true); | 1145 database_->UpdateFinished(true); |
| 1102 EXPECT_TRUE(base::PathExists(database_filename_)); | 1146 EXPECT_TRUE(base::PathExists(database_filename_)); |
| 1103 | 1147 |
| 1104 database_.reset(); | 1148 database_.reset(); |
| 1105 } | 1149 } |
| 1106 | 1150 |
| 1107 // Checks database reading and writing. | 1151 // Checks database reading and writing. |
| 1108 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrl) { | 1152 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrl) { |
| 1109 database_.reset(); | |
| 1110 base::MessageLoop loop; | |
| 1111 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); | |
| 1112 SafeBrowsingStoreFile* download_store = new SafeBrowsingStoreFile(); | |
| 1113 SafeBrowsingStoreFile* csd_whitelist_store = new SafeBrowsingStoreFile(); | |
| 1114 database_.reset(new SafeBrowsingDatabaseNew(browse_store, | |
| 1115 download_store, | |
| 1116 csd_whitelist_store, | |
| 1117 NULL, | |
| 1118 NULL, | |
| 1119 NULL, | |
| 1120 NULL)); | |
| 1121 database_->Init(database_filename_); | |
| 1122 | |
| 1123 const char kEvil1Url1[] = "www.evil1.com/download1/"; | 1153 const char kEvil1Url1[] = "www.evil1.com/download1/"; |
| 1124 const char kEvil1Url2[] = "www.evil1.com/download2.html"; | 1154 const char kEvil1Url2[] = "www.evil1.com/download2.html"; |
| 1125 | 1155 |
| 1126 // Add a simple chunk with one hostkey for download url list. | 1156 // Add a simple chunk with one hostkey for download url list. |
| 1127 ScopedVector<SBChunkData> chunks; | 1157 ScopedVector<SBChunkData> chunks; |
| 1128 chunks.push_back(AddChunkPrefix2Value(1, kEvil1Url1, kEvil1Url2)); | 1158 chunks.push_back(AddChunkPrefix2Value(1, kEvil1Url1, kEvil1Url2)); |
| 1129 | 1159 |
| 1130 std::vector<SBListChunkRanges> lists; | 1160 std::vector<SBListChunkRanges> lists; |
| 1131 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1161 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1132 database_->InsertChunks(safe_browsing_util::kBinUrlList, chunks.get()); | 1162 database_->InsertChunks(safe_browsing_util::kBinUrlList, chunks.get()); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1210 TEST_F(SafeBrowsingDatabaseTest, Whitelists) { | 1240 TEST_F(SafeBrowsingDatabaseTest, Whitelists) { |
| 1211 database_.reset(); | 1241 database_.reset(); |
| 1212 | 1242 |
| 1213 // We expect all calls to ContainsCsdWhitelistedUrl in particular to be made | 1243 // We expect all calls to ContainsCsdWhitelistedUrl in particular to be made |
| 1214 // from the IO thread. In general the whitelist lookups are thread-safe. | 1244 // from the IO thread. In general the whitelist lookups are thread-safe. |
| 1215 content::TestBrowserThreadBundle thread_bundle_; | 1245 content::TestBrowserThreadBundle thread_bundle_; |
| 1216 | 1246 |
| 1217 // If the whitelist is disabled everything should match the whitelist. | 1247 // If the whitelist is disabled everything should match the whitelist. |
| 1218 database_.reset(new SafeBrowsingDatabaseNew(new SafeBrowsingStoreFile(), | 1248 database_.reset(new SafeBrowsingDatabaseNew(new SafeBrowsingStoreFile(), |
| 1219 NULL, NULL, NULL, NULL, NULL, | 1249 NULL, NULL, NULL, NULL, NULL, |
| 1220 NULL)); | 1250 NULL, NULL)); |
| 1221 database_->Init(database_filename_); | 1251 database_->Init(database_filename_); |
| 1222 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl( | 1252 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl( |
| 1223 GURL(std::string("http://www.phishing.com/")))); | 1253 GURL(std::string("http://www.phishing.com/")))); |
| 1224 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl( | 1254 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl( |
| 1225 GURL(std::string("http://www.phishing.com/")))); | 1255 GURL(std::string("http://www.phishing.com/")))); |
| 1226 EXPECT_TRUE(database_->ContainsDownloadWhitelistedString("asdf")); | 1256 EXPECT_TRUE(database_->ContainsDownloadWhitelistedString("asdf")); |
| 1227 | 1257 |
| 1228 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); | 1258 ResetandReloadFullDatabase(); |
| 1229 SafeBrowsingStoreFile* csd_whitelist_store = new SafeBrowsingStoreFile(); | |
| 1230 SafeBrowsingStoreFile* download_whitelist_store = new SafeBrowsingStoreFile(); | |
| 1231 SafeBrowsingStoreFile* extension_blacklist_store = | |
| 1232 new SafeBrowsingStoreFile(); | |
| 1233 database_.reset(new SafeBrowsingDatabaseNew(browse_store, NULL, | |
| 1234 csd_whitelist_store, | |
| 1235 download_whitelist_store, | |
| 1236 extension_blacklist_store, | |
| 1237 NULL, NULL)); | |
| 1238 database_->Init(database_filename_); | |
| 1239 | 1259 |
| 1240 const char kGood1Host[] = "www.good1.com/"; | 1260 const char kGood1Host[] = "www.good1.com/"; |
| 1241 const char kGood1Url1[] = "www.good1.com/a/b.html"; | 1261 const char kGood1Url1[] = "www.good1.com/a/b.html"; |
| 1242 const char kGood1Url2[] = "www.good1.com/b/"; | 1262 const char kGood1Url2[] = "www.good1.com/b/"; |
| 1243 | 1263 |
| 1244 const char kGood2Url1[] = "www.good2.com/c"; // Should match '/c/bla'. | 1264 const char kGood2Url1[] = "www.good2.com/c"; // Should match '/c/bla'. |
| 1245 | 1265 |
| 1246 // good3.com/a/b/c/d/e/f/g/ should match because it's a whitelist. | 1266 // good3.com/a/b/c/d/e/f/g/ should match because it's a whitelist. |
| 1247 const char kGood3Url1[] = "good3.com/"; | 1267 const char kGood3Url1[] = "good3.com/"; |
| 1248 | 1268 |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1428 chunks.clear(); | 1448 chunks.clear(); |
| 1429 chunks.push_back(AddChunkPrefix2Value(47, | 1449 chunks.push_back(AddChunkPrefix2Value(47, |
| 1430 "www.evil.com/phishing1.html", | 1450 "www.evil.com/phishing1.html", |
| 1431 "www.evil.com/phishing2.html")); | 1451 "www.evil.com/phishing2.html")); |
| 1432 | 1452 |
| 1433 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1453 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1434 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks.get()); | 1454 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks.get()); |
| 1435 database_->UpdateFinished(true); | 1455 database_->UpdateFinished(true); |
| 1436 | 1456 |
| 1437 GetListsInfo(&lists); | 1457 GetListsInfo(&lists); |
| 1438 ASSERT_EQ(2U, lists.size()); | 1458 ASSERT_LE(2U, lists.size()); |
| 1439 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); | 1459 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 1440 EXPECT_EQ("1", lists[0].adds); | 1460 EXPECT_EQ("1", lists[0].adds); |
| 1441 EXPECT_TRUE(lists[0].subs.empty()); | 1461 EXPECT_TRUE(lists[0].subs.empty()); |
| 1442 EXPECT_EQ(safe_browsing_util::kPhishingList, lists[1].name); | 1462 EXPECT_EQ(safe_browsing_util::kPhishingList, lists[1].name); |
| 1443 EXPECT_EQ("47", lists[1].adds); | 1463 EXPECT_EQ("47", lists[1].adds); |
| 1444 EXPECT_TRUE(lists[1].subs.empty()); | 1464 EXPECT_TRUE(lists[1].subs.empty()); |
| 1445 | 1465 |
| 1446 std::vector<SBPrefix> prefix_hits; | 1466 std::vector<SBPrefix> prefix_hits; |
| 1447 std::vector<SBFullHashResult> cache_hits; | 1467 std::vector<SBFullHashResult> cache_hits; |
| 1448 | 1468 |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1556 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 1576 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 1557 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1577 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1558 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); | 1578 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); |
| 1559 | 1579 |
| 1560 base::FilePath filter_file = database_->PrefixSetForFilename( | 1580 base::FilePath filter_file = database_->PrefixSetForFilename( |
| 1561 database_->BrowseDBFilename(database_filename_)); | 1581 database_->BrowseDBFilename(database_filename_)); |
| 1562 | 1582 |
| 1563 // After re-creating the database, it should have a filter read from | 1583 // After re-creating the database, it should have a filter read from |
| 1564 // a file, so it should find the same results. | 1584 // a file, so it should find the same results. |
| 1565 ASSERT_TRUE(base::PathExists(filter_file)); | 1585 ASSERT_TRUE(base::PathExists(filter_file)); |
| 1566 database_.reset(new SafeBrowsingDatabaseNew); | 1586 ResetandReloadFullDatabase(); |
| 1567 database_->Init(database_filename_); | |
| 1568 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1587 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1569 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 1588 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 1570 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1589 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1571 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); | 1590 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); |
| 1572 | 1591 |
| 1573 // If there is no filter file, the database cannot find malware urls. | 1592 // If there is no filter file, the database cannot find malware urls. |
| 1574 base::DeleteFile(filter_file, false); | 1593 base::DeleteFile(filter_file, false); |
| 1575 ASSERT_FALSE(base::PathExists(filter_file)); | 1594 ASSERT_FALSE(base::PathExists(filter_file)); |
| 1576 database_.reset(new SafeBrowsingDatabaseNew); | 1595 ResetandReloadFullDatabase(); |
| 1577 database_->Init(database_filename_); | |
| 1578 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1596 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1579 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 1597 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 1580 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1598 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1581 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); | 1599 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); |
| 1582 } | 1600 } |
| 1583 | 1601 |
| 1584 TEST_F(SafeBrowsingDatabaseTest, CachedFullMiss) { | 1602 TEST_F(SafeBrowsingDatabaseTest, CachedFullMiss) { |
| 1585 const SBPrefix kPrefix1 = 1001U; | 1603 const SBPrefix kPrefix1 = 1001U; |
| 1586 const SBFullHash kFullHash1_1 = | 1604 const SBFullHash kFullHash1_1 = |
| 1587 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); | 1605 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 1606 std::vector<SBFullHashResult> cache_results; | 1624 std::vector<SBFullHashResult> cache_results; |
| 1607 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); | 1625 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); |
| 1608 } | 1626 } |
| 1609 | 1627 |
| 1610 { | 1628 { |
| 1611 // kFullHash1_1 gets no prefix hit because of the cached item, and also does | 1629 // kFullHash1_1 gets no prefix hit because of the cached item, and also does |
| 1612 // not have a cache hit. | 1630 // not have a cache hit. |
| 1613 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); | 1631 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); |
| 1614 std::vector<SBPrefix> prefix_hits; | 1632 std::vector<SBPrefix> prefix_hits; |
| 1615 std::vector<SBFullHashResult> cache_hits; | 1633 std::vector<SBFullHashResult> cache_hits; |
| 1616 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( | 1634 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1617 full_hashes, &prefix_hits, &cache_hits)); | 1635 full_hashes, &prefix_hits, &cache_hits)); |
| 1618 | 1636 |
| 1619 // kFullHash2_1 gets a hit from the prefix in the database. | 1637 // kFullHash2_1 gets a hit from the prefix in the database. |
| 1620 full_hashes.push_back(kFullHash2_1); | 1638 full_hashes.push_back(kFullHash2_1); |
| 1621 prefix_hits.clear(); | 1639 prefix_hits.clear(); |
| 1622 cache_hits.clear(); | 1640 cache_hits.clear(); |
| 1623 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1641 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1624 full_hashes, &prefix_hits, &cache_hits)); | 1642 full_hashes, &prefix_hits, &cache_hits)); |
| 1625 ASSERT_EQ(1U, prefix_hits.size()); | 1643 ASSERT_EQ(1U, prefix_hits.size()); |
| 1626 EXPECT_EQ(kPrefix2, prefix_hits[0]); | 1644 EXPECT_EQ(kPrefix2, prefix_hits[0]); |
| 1627 EXPECT_TRUE(cache_hits.empty()); | 1645 EXPECT_TRUE(cache_hits.empty()); |
| 1628 } | 1646 } |
| 1629 } | 1647 } |
| 1630 | 1648 |
| 1631 TEST_F(SafeBrowsingDatabaseTest, CachedPrefixHitFullMiss) { | 1649 TEST_F(SafeBrowsingDatabaseTest, CachedPrefixHitFullMiss) { |
| 1632 const SBPrefix kPrefix1 = 1001U; | 1650 const SBPrefix kPrefix1 = 1001U; |
| 1633 const SBFullHash kFullHash1_1 = | 1651 const SBFullHash kFullHash1_1 = |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 1654 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1672 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1655 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 1673 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1656 database_->UpdateFinished(true); | 1674 database_->UpdateFinished(true); |
| 1657 | 1675 |
| 1658 { | 1676 { |
| 1659 // kFullHash1_1 has a prefix hit of kPrefix1. | 1677 // kFullHash1_1 has a prefix hit of kPrefix1. |
| 1660 std::vector<SBFullHash> full_hashes; | 1678 std::vector<SBFullHash> full_hashes; |
| 1661 full_hashes.push_back(kFullHash1_1); | 1679 full_hashes.push_back(kFullHash1_1); |
| 1662 std::vector<SBPrefix> prefix_hits; | 1680 std::vector<SBPrefix> prefix_hits; |
| 1663 std::vector<SBFullHashResult> cache_hits; | 1681 std::vector<SBFullHashResult> cache_hits; |
| 1664 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1682 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1665 full_hashes, &prefix_hits, &cache_hits)); | 1683 full_hashes, &prefix_hits, &cache_hits)); |
| 1666 ASSERT_EQ(1U, prefix_hits.size()); | 1684 ASSERT_EQ(1U, prefix_hits.size()); |
| 1667 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1685 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 1668 EXPECT_TRUE(cache_hits.empty()); | 1686 EXPECT_TRUE(cache_hits.empty()); |
| 1669 | 1687 |
| 1670 // kFullHash2_1 has a prefix hit of kPrefix2. | 1688 // kFullHash2_1 has a prefix hit of kPrefix2. |
| 1671 full_hashes.push_back(kFullHash2_1); | 1689 full_hashes.push_back(kFullHash2_1); |
| 1672 prefix_hits.clear(); | 1690 prefix_hits.clear(); |
| 1673 cache_hits.clear(); | 1691 cache_hits.clear(); |
| 1674 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1692 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1675 full_hashes, &prefix_hits, &cache_hits)); | 1693 full_hashes, &prefix_hits, &cache_hits)); |
| 1676 ASSERT_EQ(2U, prefix_hits.size()); | 1694 ASSERT_EQ(2U, prefix_hits.size()); |
| 1677 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1695 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 1678 EXPECT_EQ(kPrefix2, prefix_hits[1]); | 1696 EXPECT_EQ(kPrefix2, prefix_hits[1]); |
| 1679 EXPECT_TRUE(cache_hits.empty()); | 1697 EXPECT_TRUE(cache_hits.empty()); |
| 1680 | 1698 |
| 1681 // kFullHash3_1 has no hits. | 1699 // kFullHash3_1 has no hits. |
| 1682 full_hashes.push_back(kFullHash3_1); | 1700 full_hashes.push_back(kFullHash3_1); |
| 1683 prefix_hits.clear(); | 1701 prefix_hits.clear(); |
| 1684 cache_hits.clear(); | 1702 cache_hits.clear(); |
| 1685 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1703 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1686 full_hashes, &prefix_hits, &cache_hits)); | 1704 full_hashes, &prefix_hits, &cache_hits)); |
| 1687 ASSERT_EQ(2U, prefix_hits.size()); | 1705 ASSERT_EQ(2U, prefix_hits.size()); |
| 1688 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1706 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 1689 EXPECT_EQ(kPrefix2, prefix_hits[1]); | 1707 EXPECT_EQ(kPrefix2, prefix_hits[1]); |
| 1690 EXPECT_TRUE(cache_hits.empty()); | 1708 EXPECT_TRUE(cache_hits.empty()); |
| 1691 } | 1709 } |
| 1692 | 1710 |
| 1693 { | 1711 { |
| 1694 // Cache a fullhash result for two kPrefix1 full hashes. | 1712 // Cache a fullhash result for two kPrefix1 full hashes. |
| 1695 std::vector<SBPrefix> prefixes(1, kPrefix1); | 1713 std::vector<SBPrefix> prefixes(1, kPrefix1); |
| 1696 std::vector<SBFullHashResult> cache_results; | 1714 std::vector<SBFullHashResult> cache_results; |
| 1697 | 1715 |
| 1698 SBFullHashResult full_hash_result; | 1716 SBFullHashResult full_hash_result; |
| 1699 full_hash_result.list_id = safe_browsing_util::MALWARE; | 1717 full_hash_result.list_id = safe_browsing_util::MALWARE; |
| 1700 | 1718 |
| 1701 full_hash_result.hash = kFullHash1_1; | 1719 full_hash_result.hash = kFullHash1_1; |
| 1702 cache_results.push_back(full_hash_result); | 1720 cache_results.push_back(full_hash_result); |
| 1703 | 1721 |
| 1704 full_hash_result.hash = kFullHash1_3; | 1722 full_hash_result.hash = kFullHash1_3; |
| 1705 cache_results.push_back(full_hash_result); | 1723 cache_results.push_back(full_hash_result); |
| 1706 | 1724 |
| 1707 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); | 1725 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); |
| 1708 } | 1726 } |
| 1709 | 1727 |
| 1710 { | 1728 { |
| 1711 // kFullHash1_1 should now see a cache hit. | 1729 // kFullHash1_1 should now see a cache hit. |
| 1712 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); | 1730 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); |
| 1713 std::vector<SBPrefix> prefix_hits; | 1731 std::vector<SBPrefix> prefix_hits; |
| 1714 std::vector<SBFullHashResult> cache_hits; | 1732 std::vector<SBFullHashResult> cache_hits; |
| 1715 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1733 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1716 full_hashes, &prefix_hits, &cache_hits)); | 1734 full_hashes, &prefix_hits, &cache_hits)); |
| 1717 EXPECT_TRUE(prefix_hits.empty()); | 1735 EXPECT_TRUE(prefix_hits.empty()); |
| 1718 ASSERT_EQ(1U, cache_hits.size()); | 1736 ASSERT_EQ(1U, cache_hits.size()); |
| 1719 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash)); | 1737 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash)); |
| 1720 | 1738 |
| 1721 // Adding kFullHash2_1 will see the existing cache hit plus the prefix hit | 1739 // Adding kFullHash2_1 will see the existing cache hit plus the prefix hit |
| 1722 // for kPrefix2. | 1740 // for kPrefix2. |
| 1723 full_hashes.push_back(kFullHash2_1); | 1741 full_hashes.push_back(kFullHash2_1); |
| 1724 prefix_hits.clear(); | 1742 prefix_hits.clear(); |
| 1725 cache_hits.clear(); | 1743 cache_hits.clear(); |
| 1726 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1744 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1727 full_hashes, &prefix_hits, &cache_hits)); | 1745 full_hashes, &prefix_hits, &cache_hits)); |
| 1728 ASSERT_EQ(1U, prefix_hits.size()); | 1746 ASSERT_EQ(1U, prefix_hits.size()); |
| 1729 EXPECT_EQ(kPrefix2, prefix_hits[0]); | 1747 EXPECT_EQ(kPrefix2, prefix_hits[0]); |
| 1730 ASSERT_EQ(1U, cache_hits.size()); | 1748 ASSERT_EQ(1U, cache_hits.size()); |
| 1731 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash)); | 1749 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash)); |
| 1732 | 1750 |
| 1733 // kFullHash1_3 also gets a cache hit. | 1751 // kFullHash1_3 also gets a cache hit. |
| 1734 full_hashes.push_back(kFullHash1_3); | 1752 full_hashes.push_back(kFullHash1_3); |
| 1735 prefix_hits.clear(); | 1753 prefix_hits.clear(); |
| 1736 cache_hits.clear(); | 1754 cache_hits.clear(); |
| 1737 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1755 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1738 full_hashes, &prefix_hits, &cache_hits)); | 1756 full_hashes, &prefix_hits, &cache_hits)); |
| 1739 ASSERT_EQ(1U, prefix_hits.size()); | 1757 ASSERT_EQ(1U, prefix_hits.size()); |
| 1740 EXPECT_EQ(kPrefix2, prefix_hits[0]); | 1758 EXPECT_EQ(kPrefix2, prefix_hits[0]); |
| 1741 ASSERT_EQ(2U, cache_hits.size()); | 1759 ASSERT_EQ(2U, cache_hits.size()); |
| 1742 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash)); | 1760 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash)); |
| 1743 EXPECT_TRUE(SBFullHashEqual(kFullHash1_3, cache_hits[1].hash)); | 1761 EXPECT_TRUE(SBFullHashEqual(kFullHash1_3, cache_hits[1].hash)); |
| 1744 } | 1762 } |
| 1745 | 1763 |
| 1746 { | 1764 { |
| 1747 // Check if DB contains only kFullHash1_3. Should return a cache hit. | 1765 // Check if DB contains only kFullHash1_3. Should return a cache hit. |
| 1748 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); | 1766 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); |
| 1749 std::vector<SBPrefix> prefix_hits; | 1767 std::vector<SBPrefix> prefix_hits; |
| 1750 std::vector<SBFullHashResult> cache_hits; | 1768 std::vector<SBFullHashResult> cache_hits; |
| 1751 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1769 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1752 full_hashes, &prefix_hits, &cache_hits)); | 1770 full_hashes, &prefix_hits, &cache_hits)); |
| 1753 EXPECT_TRUE(prefix_hits.empty()); | 1771 EXPECT_TRUE(prefix_hits.empty()); |
| 1754 ASSERT_EQ(1U, cache_hits.size()); | 1772 ASSERT_EQ(1U, cache_hits.size()); |
| 1755 EXPECT_TRUE(SBFullHashEqual(kFullHash1_3, cache_hits[0].hash)); | 1773 EXPECT_TRUE(SBFullHashEqual(kFullHash1_3, cache_hits[0].hash)); |
| 1756 } | 1774 } |
| 1757 | 1775 |
| 1758 { | 1776 { |
| 1759 // kFullHash1_2 has no cache hit, and no prefix hit because of the cache for | 1777 // kFullHash1_2 has no cache hit, and no prefix hit because of the cache for |
| 1760 // kPrefix1. | 1778 // kPrefix1. |
| 1761 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 1779 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
| 1762 std::vector<SBPrefix> prefix_hits; | 1780 std::vector<SBPrefix> prefix_hits; |
| 1763 std::vector<SBFullHashResult> cache_hits; | 1781 std::vector<SBFullHashResult> cache_hits; |
| 1764 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( | 1782 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1765 full_hashes, &prefix_hits, &cache_hits)); | 1783 full_hashes, &prefix_hits, &cache_hits)); |
| 1766 | 1784 |
| 1767 // Other prefix hits possible when kFullHash1_2 hits nothing. | 1785 // Other prefix hits possible when kFullHash1_2 hits nothing. |
| 1768 full_hashes.push_back(kFullHash2_1); | 1786 full_hashes.push_back(kFullHash2_1); |
| 1769 prefix_hits.clear(); | 1787 prefix_hits.clear(); |
| 1770 cache_hits.clear(); | 1788 cache_hits.clear(); |
| 1771 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1789 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1772 full_hashes, &prefix_hits, &cache_hits)); | 1790 full_hashes, &prefix_hits, &cache_hits)); |
| 1773 ASSERT_EQ(1U, prefix_hits.size()); | 1791 ASSERT_EQ(1U, prefix_hits.size()); |
| 1774 EXPECT_EQ(kPrefix2, prefix_hits[0]); | 1792 EXPECT_EQ(kPrefix2, prefix_hits[0]); |
| 1775 EXPECT_TRUE(cache_hits.empty()); | 1793 EXPECT_TRUE(cache_hits.empty()); |
| 1776 } | 1794 } |
| 1777 } | 1795 } |
| 1778 | 1796 |
| 1779 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashMatching) { | 1797 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashMatching) { |
| 1780 const SBPrefix kPrefix1 = 1001U; | 1798 const SBPrefix kPrefix1 = 1001U; |
| 1781 const SBFullHash kFullHash1_1 = | 1799 const SBFullHash kFullHash1_1 = |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 1793 std::vector<SBListChunkRanges> lists; | 1811 std::vector<SBListChunkRanges> lists; |
| 1794 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1812 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1795 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 1813 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1796 database_->UpdateFinished(true); | 1814 database_->UpdateFinished(true); |
| 1797 | 1815 |
| 1798 { | 1816 { |
| 1799 // Check a full hash which isn't present. | 1817 // Check a full hash which isn't present. |
| 1800 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); | 1818 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); |
| 1801 std::vector<SBPrefix> prefix_hits; | 1819 std::vector<SBPrefix> prefix_hits; |
| 1802 std::vector<SBFullHashResult> cache_hits; | 1820 std::vector<SBFullHashResult> cache_hits; |
| 1803 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( | 1821 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1804 full_hashes, &prefix_hits, &cache_hits)); | 1822 full_hashes, &prefix_hits, &cache_hits)); |
| 1805 | 1823 |
| 1806 // Also one which is present, should have a prefix hit. | 1824 // Also one which is present, should have a prefix hit. |
| 1807 full_hashes.push_back(kFullHash1_1); | 1825 full_hashes.push_back(kFullHash1_1); |
| 1808 prefix_hits.clear(); | 1826 prefix_hits.clear(); |
| 1809 cache_hits.clear(); | 1827 cache_hits.clear(); |
| 1810 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1828 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1811 full_hashes, &prefix_hits, &cache_hits)); | 1829 full_hashes, &prefix_hits, &cache_hits)); |
| 1812 ASSERT_EQ(1U, prefix_hits.size()); | 1830 ASSERT_EQ(1U, prefix_hits.size()); |
| 1813 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1831 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 1814 EXPECT_TRUE(cache_hits.empty()); | 1832 EXPECT_TRUE(cache_hits.empty()); |
| 1815 | 1833 |
| 1816 // Two full hash matches with the same prefix should return one prefix hit. | 1834 // Two full hash matches with the same prefix should return one prefix hit. |
| 1817 full_hashes.push_back(kFullHash1_2); | 1835 full_hashes.push_back(kFullHash1_2); |
| 1818 prefix_hits.clear(); | 1836 prefix_hits.clear(); |
| 1819 cache_hits.clear(); | 1837 cache_hits.clear(); |
| 1820 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1838 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1821 full_hashes, &prefix_hits, &cache_hits)); | 1839 full_hashes, &prefix_hits, &cache_hits)); |
| 1822 ASSERT_EQ(1U, prefix_hits.size()); | 1840 ASSERT_EQ(1U, prefix_hits.size()); |
| 1823 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1841 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 1824 EXPECT_TRUE(cache_hits.empty()); | 1842 EXPECT_TRUE(cache_hits.empty()); |
| 1825 } | 1843 } |
| 1826 | 1844 |
| 1827 { | 1845 { |
| 1828 // Cache a gethash result for kFullHash1_2. | 1846 // Cache a gethash result for kFullHash1_2. |
| 1829 SBFullHashResult full_hash_result; | 1847 SBFullHashResult full_hash_result; |
| 1830 full_hash_result.list_id = safe_browsing_util::MALWARE; | 1848 full_hash_result.list_id = safe_browsing_util::MALWARE; |
| 1831 full_hash_result.hash = kFullHash1_2; | 1849 full_hash_result.hash = kFullHash1_2; |
| 1832 | 1850 |
| 1833 std::vector<SBPrefix> prefixes(1, kPrefix1); | 1851 std::vector<SBPrefix> prefixes(1, kPrefix1); |
| 1834 std::vector<SBFullHashResult> cache_results(1, full_hash_result); | 1852 std::vector<SBFullHashResult> cache_results(1, full_hash_result); |
| 1835 | 1853 |
| 1836 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); | 1854 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); |
| 1837 } | 1855 } |
| 1838 | 1856 |
| 1839 { | 1857 { |
| 1840 // kFullHash1_3 should still return false, because the cached | 1858 // kFullHash1_3 should still return false, because the cached |
| 1841 // result for kPrefix1 doesn't contain kFullHash1_3. | 1859 // result for kPrefix1 doesn't contain kFullHash1_3. |
| 1842 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); | 1860 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); |
| 1843 std::vector<SBPrefix> prefix_hits; | 1861 std::vector<SBPrefix> prefix_hits; |
| 1844 std::vector<SBFullHashResult> cache_hits; | 1862 std::vector<SBFullHashResult> cache_hits; |
| 1845 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( | 1863 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1846 full_hashes, &prefix_hits, &cache_hits)); | 1864 full_hashes, &prefix_hits, &cache_hits)); |
| 1847 | 1865 |
| 1848 // kFullHash1_1 is also not in the cached result, which takes | 1866 // kFullHash1_1 is also not in the cached result, which takes |
| 1849 // priority over the database. | 1867 // priority over the database. |
| 1850 prefix_hits.clear(); | 1868 prefix_hits.clear(); |
| 1851 full_hashes.push_back(kFullHash1_1); | 1869 full_hashes.push_back(kFullHash1_1); |
| 1852 cache_hits.clear(); | 1870 cache_hits.clear(); |
| 1853 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( | 1871 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1854 full_hashes, &prefix_hits, &cache_hits)); | 1872 full_hashes, &prefix_hits, &cache_hits)); |
| 1855 | 1873 |
| 1856 // kFullHash1_2 is in the cached result. | 1874 // kFullHash1_2 is in the cached result. |
| 1857 full_hashes.push_back(kFullHash1_2); | 1875 full_hashes.push_back(kFullHash1_2); |
| 1858 prefix_hits.clear(); | 1876 prefix_hits.clear(); |
| 1859 cache_hits.clear(); | 1877 cache_hits.clear(); |
| 1860 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1878 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1861 full_hashes, &prefix_hits, &cache_hits)); | 1879 full_hashes, &prefix_hits, &cache_hits)); |
| 1862 EXPECT_TRUE(prefix_hits.empty()); | 1880 EXPECT_TRUE(prefix_hits.empty()); |
| 1863 ASSERT_EQ(1U, cache_hits.size()); | 1881 ASSERT_EQ(1U, cache_hits.size()); |
| 1864 EXPECT_TRUE(SBFullHashEqual(kFullHash1_2, cache_hits[0].hash)); | 1882 EXPECT_TRUE(SBFullHashEqual(kFullHash1_2, cache_hits[0].hash)); |
| 1865 } | 1883 } |
| 1866 | 1884 |
| 1867 // Remove kFullHash1_1 from the database. | 1885 // Remove kFullHash1_1 from the database. |
| 1868 chunks.clear(); | 1886 chunks.clear(); |
| 1869 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); | 1887 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); |
| 1870 | 1888 |
| 1871 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1889 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1872 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 1890 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1873 database_->UpdateFinished(true); | 1891 database_->UpdateFinished(true); |
| 1874 | 1892 |
| 1875 // Cache should be cleared after updating. | 1893 // Cache should be cleared after updating. |
| 1876 EXPECT_TRUE(database_->browse_gethash_cache_.empty()); | 1894 EXPECT_TRUE(database_->prefix_gethash_cache_.empty()); |
| 1877 | 1895 |
| 1878 { | 1896 { |
| 1879 // Now the database doesn't contain kFullHash1_1. | 1897 // Now the database doesn't contain kFullHash1_1. |
| 1880 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); | 1898 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); |
| 1881 std::vector<SBPrefix> prefix_hits; | 1899 std::vector<SBPrefix> prefix_hits; |
| 1882 std::vector<SBFullHashResult> cache_hits; | 1900 std::vector<SBFullHashResult> cache_hits; |
| 1883 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( | 1901 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1884 full_hashes, &prefix_hits, &cache_hits)); | 1902 full_hashes, &prefix_hits, &cache_hits)); |
| 1885 | 1903 |
| 1886 // Nor kFullHash1_3. | 1904 // Nor kFullHash1_3. |
| 1887 full_hashes.push_back(kFullHash1_3); | 1905 full_hashes.push_back(kFullHash1_3); |
| 1888 prefix_hits.clear(); | 1906 prefix_hits.clear(); |
| 1889 cache_hits.clear(); | 1907 cache_hits.clear(); |
| 1890 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( | 1908 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1891 full_hashes, &prefix_hits, &cache_hits)); | 1909 full_hashes, &prefix_hits, &cache_hits)); |
| 1892 | 1910 |
| 1893 // Still has kFullHash1_2. | 1911 // Still has kFullHash1_2. |
| 1894 full_hashes.push_back(kFullHash1_2); | 1912 full_hashes.push_back(kFullHash1_2); |
| 1895 prefix_hits.clear(); | 1913 prefix_hits.clear(); |
| 1896 cache_hits.clear(); | 1914 cache_hits.clear(); |
| 1897 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1915 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1898 full_hashes, &prefix_hits, &cache_hits)); | 1916 full_hashes, &prefix_hits, &cache_hits)); |
| 1899 ASSERT_EQ(1U, prefix_hits.size()); | 1917 ASSERT_EQ(1U, prefix_hits.size()); |
| 1900 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1918 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 1901 EXPECT_TRUE(cache_hits.empty()); | 1919 EXPECT_TRUE(cache_hits.empty()); |
| 1902 } | 1920 } |
| 1903 | 1921 |
| 1904 // Remove kFullHash1_2 from the database. | 1922 // Remove kFullHash1_2 from the database. |
| 1905 chunks.clear(); | 1923 chunks.clear(); |
| 1906 chunks.push_back(SubChunkFullHash(12, kFullHash1_2, 2)); | 1924 chunks.push_back(SubChunkFullHash(12, kFullHash1_2, 2)); |
| 1907 | 1925 |
| 1908 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1926 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1909 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 1927 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1910 database_->UpdateFinished(true); | 1928 database_->UpdateFinished(true); |
| 1911 | 1929 |
| 1912 // Cache should be cleared after updating. | 1930 // Cache should be cleared after updating. |
| 1913 EXPECT_TRUE(database_->browse_gethash_cache_.empty()); | 1931 EXPECT_TRUE(database_->prefix_gethash_cache_.empty()); |
| 1914 | 1932 |
| 1915 { | 1933 { |
| 1916 // None are present. | 1934 // None are present. |
| 1917 std::vector<SBFullHash> full_hashes; | 1935 std::vector<SBFullHash> full_hashes; |
| 1918 std::vector<SBPrefix> prefix_hits; | 1936 std::vector<SBPrefix> prefix_hits; |
| 1919 std::vector<SBFullHashResult> cache_hits; | 1937 std::vector<SBFullHashResult> cache_hits; |
| 1920 full_hashes.push_back(kFullHash1_1); | 1938 full_hashes.push_back(kFullHash1_1); |
| 1921 full_hashes.push_back(kFullHash1_2); | 1939 full_hashes.push_back(kFullHash1_2); |
| 1922 full_hashes.push_back(kFullHash1_3); | 1940 full_hashes.push_back(kFullHash1_3); |
| 1923 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( | 1941 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1924 full_hashes, &prefix_hits, &cache_hits)); | 1942 full_hashes, &prefix_hits, &cache_hits)); |
| 1925 } | 1943 } |
| 1926 } | 1944 } |
| 1927 | 1945 |
| 1928 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashAndPrefixMatching) { | 1946 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashAndPrefixMatching) { |
| 1929 const SBPrefix kPrefix1 = 1001U; | 1947 const SBPrefix kPrefix1 = 1001U; |
| 1930 const SBFullHash kFullHash1_1 = | 1948 const SBFullHash kFullHash1_1 = |
| 1931 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); | 1949 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); |
| 1932 const SBFullHash kFullHash1_2 = | 1950 const SBFullHash kFullHash1_2 = |
| 1933 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02"); | 1951 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02"); |
| 1934 | 1952 |
| 1935 ScopedVector<SBChunkData> chunks; | 1953 ScopedVector<SBChunkData> chunks; |
| 1936 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); | 1954 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); |
| 1937 | 1955 |
| 1938 std::vector<SBListChunkRanges> lists; | 1956 std::vector<SBListChunkRanges> lists; |
| 1939 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1957 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1940 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 1958 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1941 database_->UpdateFinished(true); | 1959 database_->UpdateFinished(true); |
| 1942 | 1960 |
| 1943 { | 1961 { |
| 1944 // kFullHash1_2 does not match kFullHash1_1. | 1962 // kFullHash1_2 does not match kFullHash1_1. |
| 1945 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 1963 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
| 1946 std::vector<SBPrefix> prefix_hits; | 1964 std::vector<SBPrefix> prefix_hits; |
| 1947 std::vector<SBFullHashResult> cache_hits; | 1965 std::vector<SBFullHashResult> cache_hits; |
| 1948 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( | 1966 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1949 full_hashes, &prefix_hits, &cache_hits)); | 1967 full_hashes, &prefix_hits, &cache_hits)); |
| 1950 } | 1968 } |
| 1951 | 1969 |
| 1952 // Add a prefix match. | 1970 // Add a prefix match. |
| 1953 chunks.clear(); | 1971 chunks.clear(); |
| 1954 chunks.push_back(AddChunkPrefix(2, kPrefix1)); | 1972 chunks.push_back(AddChunkPrefix(2, kPrefix1)); |
| 1955 | 1973 |
| 1956 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1974 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1957 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 1975 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1958 database_->UpdateFinished(true); | 1976 database_->UpdateFinished(true); |
| 1959 | 1977 |
| 1960 { | 1978 { |
| 1961 // kFullHash1_2 does match kPrefix1. | 1979 // kFullHash1_2 does match kPrefix1. |
| 1962 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 1980 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
| 1963 std::vector<SBPrefix> prefix_hits; | 1981 std::vector<SBPrefix> prefix_hits; |
| 1964 std::vector<SBFullHashResult> cache_hits; | 1982 std::vector<SBFullHashResult> cache_hits; |
| 1965 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 1983 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1966 full_hashes, &prefix_hits, &cache_hits)); | 1984 full_hashes, &prefix_hits, &cache_hits)); |
| 1967 ASSERT_EQ(1U, prefix_hits.size()); | 1985 ASSERT_EQ(1U, prefix_hits.size()); |
| 1968 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1986 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 1969 EXPECT_TRUE(cache_hits.empty()); | 1987 EXPECT_TRUE(cache_hits.empty()); |
| 1970 } | 1988 } |
| 1971 | 1989 |
| 1972 // Remove the full hash. | 1990 // Remove the full hash. |
| 1973 chunks.clear(); | 1991 chunks.clear(); |
| 1974 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); | 1992 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); |
| 1975 | 1993 |
| 1976 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1994 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1977 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); | 1995 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1978 database_->UpdateFinished(true); | 1996 database_->UpdateFinished(true); |
| 1979 | 1997 |
| 1980 { | 1998 { |
| 1981 // kFullHash1_2 still returns true due to the prefix hit. | 1999 // kFullHash1_2 still returns true due to the prefix hit. |
| 1982 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 2000 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
| 1983 std::vector<SBPrefix> prefix_hits; | 2001 std::vector<SBPrefix> prefix_hits; |
| 1984 std::vector<SBFullHashResult> cache_hits; | 2002 std::vector<SBFullHashResult> cache_hits; |
| 1985 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( | 2003 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1986 full_hashes, &prefix_hits, &cache_hits)); | 2004 full_hashes, &prefix_hits, &cache_hits)); |
| 1987 ASSERT_EQ(1U, prefix_hits.size()); | 2005 ASSERT_EQ(1U, prefix_hits.size()); |
| 1988 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 2006 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 1989 EXPECT_TRUE(cache_hits.empty()); | 2007 EXPECT_TRUE(cache_hits.empty()); |
| 1990 } | 2008 } |
| 1991 } | 2009 } |
| 1992 | 2010 |
| 1993 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) { | 2011 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) { |
| 1994 database_.reset(); | |
| 1995 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); | |
| 1996 SafeBrowsingStoreFile* ip_blacklist_store = new SafeBrowsingStoreFile(); | |
| 1997 database_.reset(new SafeBrowsingDatabaseNew(browse_store, | |
| 1998 NULL, | |
| 1999 NULL, | |
| 2000 NULL, | |
| 2001 NULL, | |
| 2002 NULL, | |
| 2003 ip_blacklist_store)); | |
| 2004 database_->Init(database_filename_); | |
| 2005 std::vector<SBListChunkRanges> lists; | 2012 std::vector<SBListChunkRanges> lists; |
| 2006 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2013 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 2007 | 2014 |
| 2008 ScopedVector<SBChunkData> chunks; | 2015 ScopedVector<SBChunkData> chunks; |
| 2009 | 2016 |
| 2010 // IPv4 prefix match for ::ffff:192.168.1.0/120. | 2017 // IPv4 prefix match for ::ffff:192.168.1.0/120. |
| 2011 chunks.push_back(AddChunkHashedIpValue(1, "::ffff:192.168.1.0", 120)); | 2018 chunks.push_back(AddChunkHashedIpValue(1, "::ffff:192.168.1.0", 120)); |
| 2012 | 2019 |
| 2013 // IPv4 exact match for ::ffff:192.1.1.1. | 2020 // IPv4 exact match for ::ffff:192.1.1.1. |
| 2014 chunks.push_back(AddChunkHashedIpValue(2, "::ffff:192.1.1.1", 128)); | 2021 chunks.push_back(AddChunkHashedIpValue(2, "::ffff:192.1.1.1", 128)); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2129 GURL(std::string("http://") + kExampleCollision), | 2136 GURL(std::string("http://") + kExampleCollision), |
| 2130 &prefix_hits, &cache_hits)); | 2137 &prefix_hits, &cache_hits)); |
| 2131 ASSERT_EQ(1U, prefix_hits.size()); | 2138 ASSERT_EQ(1U, prefix_hits.size()); |
| 2132 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); | 2139 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); |
| 2133 EXPECT_TRUE(cache_hits.empty()); | 2140 EXPECT_TRUE(cache_hits.empty()); |
| 2134 | 2141 |
| 2135 // This prefix collides, but no full hash match. | 2142 // This prefix collides, but no full hash match. |
| 2136 EXPECT_FALSE(database_->ContainsBrowseUrl( | 2143 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 2137 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); | 2144 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); |
| 2138 } | 2145 } |
| OLD | NEW |