Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_database_unittest.cc

Issue 611603002: Add the goog-unwanted-shavar list to a new SafeBrowsing PrefixSet. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review:mattm Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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() {
mattm 2014/11/12 22:26:57 nit: capitalize And
gab 2014/11/13 02:45:24 Done.
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
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
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 SCOPED_TRACE(std::string("Tested list at fault => ") +
478 ASSERT_LE(1U, lists.size()); 494 test_case.test_list_name);
479 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); 495
480 EXPECT_EQ("1-3,7", lists[0].adds); 496 std::vector<SBListChunkRanges> lists;
481 EXPECT_TRUE(lists[0].subs.empty()); 497 ScopedVector<SBChunkData> chunks;
482 498
483 std::vector<SBPrefix> prefix_hits; 499 chunks.push_back(AddChunkPrefix2Value(1,
484 std::vector<SBFullHashResult> cache_hits; 500 "www.evil.com/phishing.html",
485 EXPECT_TRUE(database_->ContainsBrowseUrl( 501 "www.evil.com/malware.html"));
486 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); 502 chunks.push_back(AddChunkPrefix4Value(2,
487 ASSERT_EQ(1U, prefix_hits.size()); 503 "www.evil.com/notevil1.html",
488 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); 504 "www.evil.com/notevil2.html",
489 EXPECT_TRUE(cache_hits.empty()); 505 "www.good.com/good1.html",
490 506 "www.good.com/good2.html"));
491 EXPECT_TRUE(database_->ContainsBrowseUrl( 507 chunks.push_back(AddChunkPrefixValue(3, "192.168.0.1/malware.html"));
492 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); 508 chunks.push_back(AddChunkFullHashValue(7, "www.evil.com/evil.html"));
493 509
494 EXPECT_TRUE(database_->ContainsBrowseUrl( 510 ASSERT_TRUE(database_->UpdateStarted(&lists));
495 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits)); 511 database_->InsertChunks(test_case.test_list_name, chunks.get());
496 512 database_->UpdateFinished(true);
497 EXPECT_TRUE(database_->ContainsBrowseUrl( 513
498 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); 514 // Make sure they were added correctly.
499 515 GetListsInfo(&lists);
500 EXPECT_TRUE(database_->ContainsBrowseUrl( 516
501 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); 517 ASSERT_LE(1U, lists.size());
502 518 EXPECT_EQ(test_case.test_list_name,
503 EXPECT_TRUE(database_->ContainsBrowseUrl( 519 lists[test_case.expected_list_index].name);
504 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); 520 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds);
505 521 EXPECT_TRUE(lists[test_case.expected_list_index].subs.empty());
506 EXPECT_TRUE(database_->ContainsBrowseUrl( 522
507 GURL("http://192.168.0.1/malware.html"), &prefix_hits, &cache_hits)); 523 std::vector<SBPrefix> prefix_hits;
508 524 std::vector<SBFullHashResult> cache_hits;
509 EXPECT_FALSE(database_->ContainsBrowseUrl( 525 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
510 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits)); 526 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits));
511 EXPECT_TRUE(prefix_hits.empty()); 527 ASSERT_EQ(1U, prefix_hits.size());
512 EXPECT_TRUE(cache_hits.empty()); 528 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]);
513 529 EXPECT_TRUE(cache_hits.empty());
514 EXPECT_FALSE(database_->ContainsBrowseUrl( 530
515 GURL("http://www.evil.com/robots.txt"), &prefix_hits, &cache_hits)); 531 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
516 532 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits));
517 EXPECT_TRUE(database_->ContainsBrowseUrl( 533
518 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits)); 534 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
519 ASSERT_EQ(1U, prefix_hits.size()); 535 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits));
520 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]); 536
521 537 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
522 // Attempt to re-add the first chunk (should be a no-op). 538 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits));
523 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 539
524 chunks.clear(); 540 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
525 chunks.push_back(AddChunkPrefix2Value(1, 541 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits));
526 "www.evil.com/phishing.html", 542
527 "www.evil.com/malware.html")); 543 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
528 ASSERT_TRUE(database_->UpdateStarted(&lists)); 544 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits));
529 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 545
530 database_->UpdateFinished(true); 546 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
531 547 GURL("http://192.168.0.1/malware.html"), &prefix_hits, &cache_hits));
532 GetListsInfo(&lists); 548
533 ASSERT_LE(1U, lists.size()); 549 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)(
534 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); 550 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits));
535 EXPECT_EQ("1-3,7", lists[0].adds); 551 EXPECT_TRUE(prefix_hits.empty());
536 EXPECT_TRUE(lists[0].subs.empty()); 552 EXPECT_TRUE(cache_hits.empty());
537 553
538 // Test removing a single prefix from the add chunk. 554 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)(
539 chunks.clear(); 555 GURL("http://www.evil.com/robots.txt"), &prefix_hits, &cache_hits));
540 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/notevil1.html", 2)); 556
541 ASSERT_TRUE(database_->UpdateStarted(&lists)); 557 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
542 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 558 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits));
543 database_->UpdateFinished(true); 559 ASSERT_EQ(1U, prefix_hits.size());
544 560 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]);
545 EXPECT_TRUE(database_->ContainsBrowseUrl( 561
546 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); 562 // Attempt to re-add the first chunk (should be a no-op).
547 ASSERT_EQ(1U, prefix_hits.size()); 563 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522
548 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); 564 chunks.clear();
549 EXPECT_TRUE(cache_hits.empty()); 565 chunks.push_back(AddChunkPrefix2Value(1,
550 566 "www.evil.com/phishing.html",
551 EXPECT_FALSE(database_->ContainsBrowseUrl( 567 "www.evil.com/malware.html"));
552 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits)); 568 ASSERT_TRUE(database_->UpdateStarted(&lists));
553 EXPECT_TRUE(prefix_hits.empty()); 569 database_->InsertChunks(test_case.test_list_name, chunks.get());
554 EXPECT_TRUE(cache_hits.empty()); 570 database_->UpdateFinished(true);
555 571
556 EXPECT_TRUE(database_->ContainsBrowseUrl( 572 GetListsInfo(&lists);
557 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); 573 ASSERT_LE(1U, lists.size());
558 574 EXPECT_EQ(test_case.test_list_name,
559 EXPECT_TRUE(database_->ContainsBrowseUrl( 575 lists[test_case.expected_list_index].name);
560 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); 576 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds);
561 577 EXPECT_TRUE(lists[test_case.expected_list_index].subs.empty());
562 EXPECT_TRUE(database_->ContainsBrowseUrl( 578
563 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); 579 // Test removing a single prefix from the add chunk.
564 580 chunks.clear();
565 GetListsInfo(&lists); 581 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/notevil1.html", 2));
566 ASSERT_LE(1U, lists.size()); 582 ASSERT_TRUE(database_->UpdateStarted(&lists));
567 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); 583 database_->InsertChunks(test_case.test_list_name, chunks.get());
568 EXPECT_EQ("1-3,7", lists[0].adds); 584 database_->UpdateFinished(true);
569 EXPECT_EQ("4", lists[0].subs); 585
570 586 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
571 // Test the same sub chunk again. This should be a no-op. 587 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits));
572 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 588 ASSERT_EQ(1U, prefix_hits.size());
573 chunks.clear(); 589 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]);
574 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/notevil1.html", 2)); 590 EXPECT_TRUE(cache_hits.empty());
575 591
576 ASSERT_TRUE(database_->UpdateStarted(&lists)); 592 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)(
577 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 593 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits));
578 database_->UpdateFinished(true); 594 EXPECT_TRUE(prefix_hits.empty());
579 595 EXPECT_TRUE(cache_hits.empty());
580 GetListsInfo(&lists); 596
581 ASSERT_LE(1U, lists.size()); 597 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
582 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); 598 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits));
583 EXPECT_EQ("1-3,7", lists[0].adds); 599
584 EXPECT_EQ("4", lists[0].subs); 600 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
585 601 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits));
586 // Test removing all the prefixes from an add chunk. 602
587 ASSERT_TRUE(database_->UpdateStarted(&lists)); 603 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
588 AddDelChunk(safe_browsing_util::kMalwareList, 2); 604 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits));
589 database_->UpdateFinished(true); 605
590 606 GetListsInfo(&lists);
591 EXPECT_FALSE(database_->ContainsBrowseUrl( 607 ASSERT_LE(1U, lists.size());
592 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits)); 608 EXPECT_EQ(test_case.test_list_name,
593 609 lists[test_case.expected_list_index].name);
594 EXPECT_FALSE(database_->ContainsBrowseUrl( 610 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds);
595 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits)); 611 EXPECT_EQ("4", lists[test_case.expected_list_index].subs);
596 612
597 EXPECT_FALSE(database_->ContainsBrowseUrl( 613 // Test the same sub chunk again. This should be a no-op.
598 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits)); 614 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522
599 615 chunks.clear();
600 GetListsInfo(&lists); 616 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/notevil1.html", 2));
601 ASSERT_LE(1U, lists.size()); 617
602 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); 618 ASSERT_TRUE(database_->UpdateStarted(&lists));
603 EXPECT_EQ("1,3,7", lists[0].adds); 619 database_->InsertChunks(test_case.test_list_name, chunks.get());
604 EXPECT_EQ("4", lists[0].subs); 620 database_->UpdateFinished(true);
605 621
606 // The adddel command exposed a bug in the transaction code where any 622 GetListsInfo(&lists);
607 // transaction after it would fail. Add a dummy entry and remove it to 623 ASSERT_LE(1U, lists.size());
608 // make sure the transcation works fine. 624 EXPECT_EQ(test_case.test_list_name,
609 chunks.clear(); 625 lists[test_case.expected_list_index].name);
610 chunks.push_back(AddChunkPrefixValue(44, "www.redherring.com/index.html")); 626 EXPECT_EQ("1-3,7", lists[test_case.expected_list_index].adds);
611 ASSERT_TRUE(database_->UpdateStarted(&lists)); 627 EXPECT_EQ("4", lists[test_case.expected_list_index].subs);
612 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 628
613 629 // Test removing all the prefixes from an add chunk.
614 // Now remove the dummy entry. If there are any problems with the 630 ASSERT_TRUE(database_->UpdateStarted(&lists));
615 // transactions, asserts will fire. 631 AddDelChunk(test_case.test_list_name, 2);
616 AddDelChunk(safe_browsing_util::kMalwareList, 44); 632 database_->UpdateFinished(true);
617 633
618 // Test the subdel command. 634 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)(
619 SubDelChunk(safe_browsing_util::kMalwareList, 4); 635 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits));
620 database_->UpdateFinished(true); 636
621 637 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)(
622 GetListsInfo(&lists); 638 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits));
623 ASSERT_LE(1U, lists.size()); 639
624 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); 640 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)(
625 EXPECT_EQ("1,3,7", lists[0].adds); 641 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits));
626 EXPECT_TRUE(lists[0].subs.empty()); 642
627 643 GetListsInfo(&lists);
628 // Test a sub command coming in before the add. 644 ASSERT_LE(1U, lists.size());
629 chunks.clear(); 645 EXPECT_EQ(test_case.test_list_name,
630 chunks.push_back(SubChunkPrefix2Value(5, 646 lists[test_case.expected_list_index].name);
631 "www.notevilanymore.com/index.html", 647 EXPECT_EQ("1,3,7", lists[test_case.expected_list_index].adds);
632 10, 648 EXPECT_EQ("4", lists[test_case.expected_list_index].subs);
633 "www.notevilanymore.com/good.html", 649
634 10)); 650 // The adddel command exposed a bug in the transaction code where any
635 ASSERT_TRUE(database_->UpdateStarted(&lists)); 651 // transaction after it would fail. Add a dummy entry and remove it to
636 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 652 // make sure the transcation works fine.
637 database_->UpdateFinished(true); 653 chunks.clear();
638 654 chunks.push_back(AddChunkPrefixValue(44, "www.redherring.com/index.html"));
639 EXPECT_FALSE(database_->ContainsBrowseUrl( 655 ASSERT_TRUE(database_->UpdateStarted(&lists));
640 GURL("http://www.notevilanymore.com/index.html"), 656 database_->InsertChunks(test_case.test_list_name, chunks.get());
641 &prefix_hits, 657
642 &cache_hits)); 658 // Now remove the dummy entry. If there are any problems with the
643 659 // transactions, asserts will fire.
644 // Now insert the tardy add chunk and we don't expect them to appear 660 AddDelChunk(test_case.test_list_name, 44);
645 // in database because of the previous sub chunk. 661
646 chunks.clear(); 662 // Test the subdel command.
647 chunks.push_back(AddChunkPrefix2Value(10, 663 SubDelChunk(test_case.test_list_name, 4);
648 "www.notevilanymore.com/index.html", 664 database_->UpdateFinished(true);
649 "www.notevilanymore.com/good.html")); 665
650 ASSERT_TRUE(database_->UpdateStarted(&lists)); 666 GetListsInfo(&lists);
651 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 667 ASSERT_LE(1U, lists.size());
652 database_->UpdateFinished(true); 668 EXPECT_EQ(test_case.test_list_name,
653 669 lists[test_case.expected_list_index].name);
654 EXPECT_FALSE(database_->ContainsBrowseUrl( 670 EXPECT_EQ("1,3,7", lists[test_case.expected_list_index].adds);
655 GURL("http://www.notevilanymore.com/index.html"), 671 EXPECT_TRUE(lists[test_case.expected_list_index].subs.empty());
656 &prefix_hits, 672
657 &cache_hits)); 673 // Test a sub command coming in before the add.
658 674 chunks.clear();
659 EXPECT_FALSE(database_->ContainsBrowseUrl( 675 chunks.push_back(SubChunkPrefix2Value(5,
660 GURL("http://www.notevilanymore.com/good.html"), 676 "www.notevilanymore.com/index.html",
661 &prefix_hits, 677 10,
662 &cache_hits)); 678 "www.notevilanymore.com/good.html",
663 679 10));
664 // Reset and reload the database. The database will rely on the prefix set. 680 ASSERT_TRUE(database_->UpdateStarted(&lists));
665 database_.reset(new SafeBrowsingDatabaseNew); 681 database_->InsertChunks(test_case.test_list_name, chunks.get());
666 database_->Init(database_filename_); 682 database_->UpdateFinished(true);
667 683
668 // Check that a prefix still hits. 684 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)(
669 EXPECT_TRUE(database_->ContainsBrowseUrl( 685 GURL("http://www.notevilanymore.com/index.html"),
670 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); 686 &prefix_hits,
671 ASSERT_EQ(1U, prefix_hits.size()); 687 &cache_hits));
672 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]); 688
673 689 // Now insert the tardy add chunk and we don't expect them to appear
674 // Also check that it's not just always returning true in this case. 690 // in database because of the previous sub chunk.
675 EXPECT_FALSE(database_->ContainsBrowseUrl( 691 chunks.clear();
676 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits)); 692 chunks.push_back(AddChunkPrefix2Value(10,
677 693 "www.notevilanymore.com/index.html",
678 // Check that the full hash is still present. 694 "www.notevilanymore.com/good.html"));
679 EXPECT_TRUE(database_->ContainsBrowseUrl( 695 ASSERT_TRUE(database_->UpdateStarted(&lists));
680 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits)); 696 database_->InsertChunks(test_case.test_list_name, chunks.get());
681 ASSERT_EQ(1U, prefix_hits.size()); 697 database_->UpdateFinished(true);
682 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]); 698
699 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)(
700 GURL("http://www.notevilanymore.com/index.html"),
701 &prefix_hits,
702 &cache_hits));
703
704 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)(
705 GURL("http://www.notevilanymore.com/good.html"),
706 &prefix_hits,
707 &cache_hits));
708
709 // Reset and reload the database. The database will rely on the prefix set.
710 ResetandReloadFullDatabase();
711
712 // Check that a prefix still hits.
713 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
714 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits));
715 ASSERT_EQ(1U, prefix_hits.size());
716 EXPECT_EQ(SBPrefixForString("www.evil.com/phishing.html"), prefix_hits[0]);
717
718 // Also check that it's not just always returning true in this case.
719 EXPECT_FALSE((database_.get()->*test_case.test_list_contains_bad_url)(
720 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits));
721
722 // Check that the full hash is still present.
723 EXPECT_TRUE((database_.get()->*test_case.test_list_contains_bad_url)(
724 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits));
725 ASSERT_EQ(1U, prefix_hits.size());
726 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]);
727 }
683 } 728 }
684 729
685 // Test adding zero length chunks to the database. 730 // Test adding zero length chunks to the database.
686 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { 731 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) {
687 std::vector<SBListChunkRanges> lists; 732 std::vector<SBListChunkRanges> lists;
688 ScopedVector<SBChunkData> chunks; 733 ScopedVector<SBChunkData> chunks;
689 734
690 // Populate with a couple of normal chunks. 735 // Populate with a couple of normal chunks.
691 chunks.push_back(AddChunkPrefix2Value(1, 736 chunks.push_back(AddChunkPrefix2Value(1,
692 "www.test.com/test1.html", 737 "www.test.com/test1.html",
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 chunks.push_back(AddChunkPrefix2Value(1, 824 chunks.push_back(AddChunkPrefix2Value(1,
780 "www.evil.com/phishing.html", 825 "www.evil.com/phishing.html",
781 "www.evil.com/malware.html")); 826 "www.evil.com/malware.html"));
782 827
783 std::vector<SBListChunkRanges> lists; 828 std::vector<SBListChunkRanges> lists;
784 ASSERT_TRUE(database_->UpdateStarted(&lists)); 829 ASSERT_TRUE(database_->UpdateStarted(&lists));
785 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 830 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get());
786 database_->UpdateFinished(true); 831 database_->UpdateFinished(true);
787 832
788 // Cache should be cleared after updating. 833 // Cache should be cleared after updating.
789 EXPECT_TRUE(database_->browse_gethash_cache_.empty()); 834 EXPECT_TRUE(database_->prefix_gethash_cache_.empty());
790 835
791 SBFullHashResult full_hash; 836 SBFullHashResult full_hash;
792 full_hash.list_id = safe_browsing_util::MALWARE; 837 full_hash.list_id = safe_browsing_util::MALWARE;
793 838
794 std::vector<SBFullHashResult> results; 839 std::vector<SBFullHashResult> results;
795 std::vector<SBPrefix> prefixes; 840 std::vector<SBPrefix> prefixes;
796 841
797 // Add a fullhash result for each prefix. 842 // Add a fullhash result for each prefix.
798 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html"); 843 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html");
799 results.push_back(full_hash); 844 results.push_back(full_hash);
800 prefixes.push_back(full_hash.hash.prefix); 845 prefixes.push_back(full_hash.hash.prefix);
801 846
802 full_hash.hash = SBFullHashForString("www.evil.com/malware.html"); 847 full_hash.hash = SBFullHashForString("www.evil.com/malware.html");
803 results.push_back(full_hash); 848 results.push_back(full_hash);
804 prefixes.push_back(full_hash.hash.prefix); 849 prefixes.push_back(full_hash.hash.prefix);
805 850
806 database_->CacheHashResults(prefixes, results, kCacheLifetime); 851 database_->CacheHashResults(prefixes, results, kCacheLifetime);
807 } 852 }
808 853
809 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { 854 TEST_F(SafeBrowsingDatabaseTest, HashCaching) {
810 PopulateDatabaseForCacheTest(); 855 PopulateDatabaseForCacheTest();
811 856
812 // We should have both full hashes in the cache. 857 // We should have both full hashes in the cache.
813 EXPECT_EQ(2U, database_->browse_gethash_cache_.size()); 858 EXPECT_EQ(2U, database_->prefix_gethash_cache_.size());
814 859
815 // Test the cache lookup for the first prefix. 860 // Test the cache lookup for the first prefix.
816 std::vector<SBPrefix> prefix_hits; 861 std::vector<SBPrefix> prefix_hits;
817 std::vector<SBFullHashResult> cache_hits; 862 std::vector<SBFullHashResult> cache_hits;
818 EXPECT_TRUE(database_->ContainsBrowseUrl( 863 EXPECT_TRUE(database_->ContainsBrowseUrl(
819 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); 864 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits));
820 EXPECT_TRUE(prefix_hits.empty()); 865 EXPECT_TRUE(prefix_hits.empty());
821 ASSERT_EQ(1U, cache_hits.size()); 866 ASSERT_EQ(1U, cache_hits.size());
822 EXPECT_TRUE(SBFullHashEqual( 867 EXPECT_TRUE(SBFullHashEqual(
823 cache_hits[0].hash, SBFullHashForString("www.evil.com/phishing.html"))); 868 cache_hits[0].hash, SBFullHashForString("www.evil.com/phishing.html")));
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
859 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); 904 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits));
860 prefix_hits.clear(); 905 prefix_hits.clear();
861 cache_hits.clear(); 906 cache_hits.clear();
862 907
863 // Test that an AddDel for the original chunk removes the last cached entry. 908 // Test that an AddDel for the original chunk removes the last cached entry.
864 ASSERT_TRUE(database_->UpdateStarted(&lists)); 909 ASSERT_TRUE(database_->UpdateStarted(&lists));
865 AddDelChunk(safe_browsing_util::kMalwareList, 1); 910 AddDelChunk(safe_browsing_util::kMalwareList, 1);
866 database_->UpdateFinished(true); 911 database_->UpdateFinished(true);
867 EXPECT_FALSE(database_->ContainsBrowseUrl( 912 EXPECT_FALSE(database_->ContainsBrowseUrl(
868 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); 913 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits));
869 EXPECT_TRUE(database_->browse_gethash_cache_.empty()); 914 EXPECT_TRUE(database_->prefix_gethash_cache_.empty());
870 prefix_hits.clear(); 915 prefix_hits.clear();
871 cache_hits.clear(); 916 cache_hits.clear();
872 917
873 // Test that the cache won't return expired values. First we have to adjust 918 // 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 919 // the cached entries' received time to make them older, since the database
875 // cache insert uses Time::Now(). First, store some entries. 920 // cache insert uses Time::Now(). First, store some entries.
876 PopulateDatabaseForCacheTest(); 921 PopulateDatabaseForCacheTest();
877 922
878 std::map<SBPrefix, SBCachedFullHashResult>* hash_cache = 923 std::map<SBPrefix, SBCachedFullHashResult>* hash_cache =
879 &database_->browse_gethash_cache_; 924 &database_->prefix_gethash_cache_;
880 EXPECT_EQ(2U, hash_cache->size()); 925 EXPECT_EQ(2U, hash_cache->size());
881 926
882 // Now adjust one of the entries times to be in the past. 927 // Now adjust one of the entries times to be in the past.
883 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html"); 928 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html");
884 std::map<SBPrefix, SBCachedFullHashResult>::iterator iter = 929 std::map<SBPrefix, SBCachedFullHashResult>::iterator iter =
885 hash_cache->find(key); 930 hash_cache->find(key);
886 ASSERT_TRUE(iter != hash_cache->end()); 931 ASSERT_TRUE(iter != hash_cache->end());
887 iter->second.expire_after = Time::Now() - TimeDelta::FromMinutes(1); 932 iter->second.expire_after = Time::Now() - TimeDelta::FromMinutes(1);
888 933
889 EXPECT_TRUE(database_->ContainsBrowseUrl( 934 EXPECT_TRUE(database_->ContainsBrowseUrl(
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
1044 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. 1089 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved.
1045 // http://crbug.com/56448 1090 // http://crbug.com/56448
1046 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { 1091 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) {
1047 // Re-create the database in a captive message loop so that we can 1092 // Re-create the database in a captive message loop so that we can
1048 // influence task-posting. Database specifically needs to the 1093 // influence task-posting. Database specifically needs to the
1049 // file-backed. 1094 // file-backed.
1050 database_.reset(); 1095 database_.reset();
1051 base::MessageLoop loop; 1096 base::MessageLoop loop;
1052 SafeBrowsingStoreFile* store = new SafeBrowsingStoreFile(); 1097 SafeBrowsingStoreFile* store = new SafeBrowsingStoreFile();
1053 database_.reset(new SafeBrowsingDatabaseNew(store, NULL, NULL, NULL, NULL, 1098 database_.reset(new SafeBrowsingDatabaseNew(store, NULL, NULL, NULL, NULL,
1054 NULL, NULL)); 1099 NULL, NULL, NULL));
1055 database_->Init(database_filename_); 1100 database_->Init(database_filename_);
1056 1101
1057 // This will cause an empty database to be created. 1102 // This will cause an empty database to be created.
1058 std::vector<SBListChunkRanges> lists; 1103 std::vector<SBListChunkRanges> lists;
1059 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1104 ASSERT_TRUE(database_->UpdateStarted(&lists));
1060 database_->UpdateFinished(true); 1105 database_->UpdateFinished(true);
1061 1106
1062 // Create a sub chunk to insert. 1107 // Create a sub chunk to insert.
1063 ScopedVector<SBChunkData> chunks; 1108 ScopedVector<SBChunkData> chunks;
1064 chunks.push_back(SubChunkPrefixValue(7, 1109 chunks.push_back(SubChunkPrefixValue(7,
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1099 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1144 ASSERT_TRUE(database_->UpdateStarted(&lists));
1100 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 1145 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get());
1101 database_->UpdateFinished(true); 1146 database_->UpdateFinished(true);
1102 EXPECT_TRUE(base::PathExists(database_filename_)); 1147 EXPECT_TRUE(base::PathExists(database_filename_));
1103 1148
1104 database_.reset(); 1149 database_.reset();
1105 } 1150 }
1106 1151
1107 // Checks database reading and writing. 1152 // Checks database reading and writing.
1108 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrl) { 1153 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/"; 1154 const char kEvil1Url1[] = "www.evil1.com/download1/";
1124 const char kEvil1Url2[] = "www.evil1.com/download2.html"; 1155 const char kEvil1Url2[] = "www.evil1.com/download2.html";
1125 1156
1126 // Add a simple chunk with one hostkey for download url list. 1157 // Add a simple chunk with one hostkey for download url list.
1127 ScopedVector<SBChunkData> chunks; 1158 ScopedVector<SBChunkData> chunks;
1128 chunks.push_back(AddChunkPrefix2Value(1, kEvil1Url1, kEvil1Url2)); 1159 chunks.push_back(AddChunkPrefix2Value(1, kEvil1Url1, kEvil1Url2));
1129 1160
1130 std::vector<SBListChunkRanges> lists; 1161 std::vector<SBListChunkRanges> lists;
1131 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1162 ASSERT_TRUE(database_->UpdateStarted(&lists));
1132 database_->InsertChunks(safe_browsing_util::kBinUrlList, chunks.get()); 1163 database_->InsertChunks(safe_browsing_util::kBinUrlList, chunks.get());
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1210 TEST_F(SafeBrowsingDatabaseTest, Whitelists) { 1241 TEST_F(SafeBrowsingDatabaseTest, Whitelists) {
1211 database_.reset(); 1242 database_.reset();
1212 1243
1213 // We expect all calls to ContainsCsdWhitelistedUrl in particular to be made 1244 // We expect all calls to ContainsCsdWhitelistedUrl in particular to be made
1214 // from the IO thread. In general the whitelist lookups are thread-safe. 1245 // from the IO thread. In general the whitelist lookups are thread-safe.
1215 content::TestBrowserThreadBundle thread_bundle_; 1246 content::TestBrowserThreadBundle thread_bundle_;
1216 1247
1217 // If the whitelist is disabled everything should match the whitelist. 1248 // If the whitelist is disabled everything should match the whitelist.
1218 database_.reset(new SafeBrowsingDatabaseNew(new SafeBrowsingStoreFile(), 1249 database_.reset(new SafeBrowsingDatabaseNew(new SafeBrowsingStoreFile(),
1219 NULL, NULL, NULL, NULL, NULL, 1250 NULL, NULL, NULL, NULL, NULL,
1220 NULL)); 1251 NULL, NULL));
1221 database_->Init(database_filename_); 1252 database_->Init(database_filename_);
1222 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl( 1253 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl(
1223 GURL(std::string("http://www.phishing.com/")))); 1254 GURL(std::string("http://www.phishing.com/"))));
1224 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl( 1255 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl(
1225 GURL(std::string("http://www.phishing.com/")))); 1256 GURL(std::string("http://www.phishing.com/"))));
1226 EXPECT_TRUE(database_->ContainsDownloadWhitelistedString("asdf")); 1257 EXPECT_TRUE(database_->ContainsDownloadWhitelistedString("asdf"));
1227 1258
1228 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); 1259 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 1260
1240 const char kGood1Host[] = "www.good1.com/"; 1261 const char kGood1Host[] = "www.good1.com/";
1241 const char kGood1Url1[] = "www.good1.com/a/b.html"; 1262 const char kGood1Url1[] = "www.good1.com/a/b.html";
1242 const char kGood1Url2[] = "www.good1.com/b/"; 1263 const char kGood1Url2[] = "www.good1.com/b/";
1243 1264
1244 const char kGood2Url1[] = "www.good2.com/c"; // Should match '/c/bla'. 1265 const char kGood2Url1[] = "www.good2.com/c"; // Should match '/c/bla'.
1245 1266
1246 // good3.com/a/b/c/d/e/f/g/ should match because it's a whitelist. 1267 // good3.com/a/b/c/d/e/f/g/ should match because it's a whitelist.
1247 const char kGood3Url1[] = "good3.com/"; 1268 const char kGood3Url1[] = "good3.com/";
1248 1269
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
1428 chunks.clear(); 1449 chunks.clear();
1429 chunks.push_back(AddChunkPrefix2Value(47, 1450 chunks.push_back(AddChunkPrefix2Value(47,
1430 "www.evil.com/phishing1.html", 1451 "www.evil.com/phishing1.html",
1431 "www.evil.com/phishing2.html")); 1452 "www.evil.com/phishing2.html"));
1432 1453
1433 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1454 ASSERT_TRUE(database_->UpdateStarted(&lists));
1434 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks.get()); 1455 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks.get());
1435 database_->UpdateFinished(true); 1456 database_->UpdateFinished(true);
1436 1457
1437 GetListsInfo(&lists); 1458 GetListsInfo(&lists);
1438 ASSERT_EQ(2U, lists.size()); 1459 ASSERT_LE(2U, lists.size());
1439 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); 1460 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name);
1440 EXPECT_EQ("1", lists[0].adds); 1461 EXPECT_EQ("1", lists[0].adds);
1441 EXPECT_TRUE(lists[0].subs.empty()); 1462 EXPECT_TRUE(lists[0].subs.empty());
1442 EXPECT_EQ(safe_browsing_util::kPhishingList, lists[1].name); 1463 EXPECT_EQ(safe_browsing_util::kPhishingList, lists[1].name);
1443 EXPECT_EQ("47", lists[1].adds); 1464 EXPECT_EQ("47", lists[1].adds);
1444 EXPECT_TRUE(lists[1].subs.empty()); 1465 EXPECT_TRUE(lists[1].subs.empty());
1445 1466
1446 std::vector<SBPrefix> prefix_hits; 1467 std::vector<SBPrefix> prefix_hits;
1447 std::vector<SBFullHashResult> cache_hits; 1468 std::vector<SBFullHashResult> cache_hits;
1448 1469
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
1556 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); 1577 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits));
1557 EXPECT_FALSE(database_->ContainsBrowseUrl( 1578 EXPECT_FALSE(database_->ContainsBrowseUrl(
1558 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); 1579 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits));
1559 1580
1560 base::FilePath filter_file = database_->PrefixSetForFilename( 1581 base::FilePath filter_file = database_->PrefixSetForFilename(
1561 database_->BrowseDBFilename(database_filename_)); 1582 database_->BrowseDBFilename(database_filename_));
1562 1583
1563 // After re-creating the database, it should have a filter read from 1584 // After re-creating the database, it should have a filter read from
1564 // a file, so it should find the same results. 1585 // a file, so it should find the same results.
1565 ASSERT_TRUE(base::PathExists(filter_file)); 1586 ASSERT_TRUE(base::PathExists(filter_file));
1566 database_.reset(new SafeBrowsingDatabaseNew); 1587 ResetandReloadFullDatabase();
1567 database_->Init(database_filename_);
1568 EXPECT_TRUE(database_->ContainsBrowseUrl( 1588 EXPECT_TRUE(database_->ContainsBrowseUrl(
1569 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); 1589 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits));
1570 EXPECT_FALSE(database_->ContainsBrowseUrl( 1590 EXPECT_FALSE(database_->ContainsBrowseUrl(
1571 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); 1591 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits));
1572 1592
1573 // If there is no filter file, the database cannot find malware urls. 1593 // If there is no filter file, the database cannot find malware urls.
1574 base::DeleteFile(filter_file, false); 1594 base::DeleteFile(filter_file, false);
1575 ASSERT_FALSE(base::PathExists(filter_file)); 1595 ASSERT_FALSE(base::PathExists(filter_file));
1576 database_.reset(new SafeBrowsingDatabaseNew); 1596 ResetandReloadFullDatabase();
1577 database_->Init(database_filename_);
1578 EXPECT_FALSE(database_->ContainsBrowseUrl( 1597 EXPECT_FALSE(database_->ContainsBrowseUrl(
1579 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); 1598 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits));
1580 EXPECT_FALSE(database_->ContainsBrowseUrl( 1599 EXPECT_FALSE(database_->ContainsBrowseUrl(
1581 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); 1600 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits));
1582 } 1601 }
1583 1602
1584 TEST_F(SafeBrowsingDatabaseTest, CachedFullMiss) { 1603 TEST_F(SafeBrowsingDatabaseTest, CachedFullMiss) {
1585 const SBPrefix kPrefix1 = 1001U; 1604 const SBPrefix kPrefix1 = 1001U;
1586 const SBFullHash kFullHash1_1 = 1605 const SBFullHash kFullHash1_1 =
1587 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); 1606 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01");
(...skipping 18 matching lines...) Expand all
1606 std::vector<SBFullHashResult> cache_results; 1625 std::vector<SBFullHashResult> cache_results;
1607 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); 1626 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime);
1608 } 1627 }
1609 1628
1610 { 1629 {
1611 // kFullHash1_1 gets no prefix hit because of the cached item, and also does 1630 // kFullHash1_1 gets no prefix hit because of the cached item, and also does
1612 // not have a cache hit. 1631 // not have a cache hit.
1613 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); 1632 std::vector<SBFullHash> full_hashes(1, kFullHash1_1);
1614 std::vector<SBPrefix> prefix_hits; 1633 std::vector<SBPrefix> prefix_hits;
1615 std::vector<SBFullHashResult> cache_hits; 1634 std::vector<SBFullHashResult> cache_hits;
1616 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( 1635 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting(
1617 full_hashes, &prefix_hits, &cache_hits)); 1636 full_hashes, &prefix_hits, &cache_hits));
1618 1637
1619 // kFullHash2_1 gets a hit from the prefix in the database. 1638 // kFullHash2_1 gets a hit from the prefix in the database.
1620 full_hashes.push_back(kFullHash2_1); 1639 full_hashes.push_back(kFullHash2_1);
1621 prefix_hits.clear(); 1640 prefix_hits.clear();
1622 cache_hits.clear(); 1641 cache_hits.clear();
1623 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1642 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1624 full_hashes, &prefix_hits, &cache_hits)); 1643 full_hashes, &prefix_hits, &cache_hits));
1625 ASSERT_EQ(1U, prefix_hits.size()); 1644 ASSERT_EQ(1U, prefix_hits.size());
1626 EXPECT_EQ(kPrefix2, prefix_hits[0]); 1645 EXPECT_EQ(kPrefix2, prefix_hits[0]);
1627 EXPECT_TRUE(cache_hits.empty()); 1646 EXPECT_TRUE(cache_hits.empty());
1628 } 1647 }
1629 } 1648 }
1630 1649
1631 TEST_F(SafeBrowsingDatabaseTest, CachedPrefixHitFullMiss) { 1650 TEST_F(SafeBrowsingDatabaseTest, CachedPrefixHitFullMiss) {
1632 const SBPrefix kPrefix1 = 1001U; 1651 const SBPrefix kPrefix1 = 1001U;
1633 const SBFullHash kFullHash1_1 = 1652 const SBFullHash kFullHash1_1 =
(...skipping 20 matching lines...) Expand all
1654 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1673 ASSERT_TRUE(database_->UpdateStarted(&lists));
1655 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 1674 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get());
1656 database_->UpdateFinished(true); 1675 database_->UpdateFinished(true);
1657 1676
1658 { 1677 {
1659 // kFullHash1_1 has a prefix hit of kPrefix1. 1678 // kFullHash1_1 has a prefix hit of kPrefix1.
1660 std::vector<SBFullHash> full_hashes; 1679 std::vector<SBFullHash> full_hashes;
1661 full_hashes.push_back(kFullHash1_1); 1680 full_hashes.push_back(kFullHash1_1);
1662 std::vector<SBPrefix> prefix_hits; 1681 std::vector<SBPrefix> prefix_hits;
1663 std::vector<SBFullHashResult> cache_hits; 1682 std::vector<SBFullHashResult> cache_hits;
1664 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1683 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1665 full_hashes, &prefix_hits, &cache_hits)); 1684 full_hashes, &prefix_hits, &cache_hits));
1666 ASSERT_EQ(1U, prefix_hits.size()); 1685 ASSERT_EQ(1U, prefix_hits.size());
1667 EXPECT_EQ(kPrefix1, prefix_hits[0]); 1686 EXPECT_EQ(kPrefix1, prefix_hits[0]);
1668 EXPECT_TRUE(cache_hits.empty()); 1687 EXPECT_TRUE(cache_hits.empty());
1669 1688
1670 // kFullHash2_1 has a prefix hit of kPrefix2. 1689 // kFullHash2_1 has a prefix hit of kPrefix2.
1671 full_hashes.push_back(kFullHash2_1); 1690 full_hashes.push_back(kFullHash2_1);
1672 prefix_hits.clear(); 1691 prefix_hits.clear();
1673 cache_hits.clear(); 1692 cache_hits.clear();
1674 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1693 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1675 full_hashes, &prefix_hits, &cache_hits)); 1694 full_hashes, &prefix_hits, &cache_hits));
1676 ASSERT_EQ(2U, prefix_hits.size()); 1695 ASSERT_EQ(2U, prefix_hits.size());
1677 EXPECT_EQ(kPrefix1, prefix_hits[0]); 1696 EXPECT_EQ(kPrefix1, prefix_hits[0]);
1678 EXPECT_EQ(kPrefix2, prefix_hits[1]); 1697 EXPECT_EQ(kPrefix2, prefix_hits[1]);
1679 EXPECT_TRUE(cache_hits.empty()); 1698 EXPECT_TRUE(cache_hits.empty());
1680 1699
1681 // kFullHash3_1 has no hits. 1700 // kFullHash3_1 has no hits.
1682 full_hashes.push_back(kFullHash3_1); 1701 full_hashes.push_back(kFullHash3_1);
1683 prefix_hits.clear(); 1702 prefix_hits.clear();
1684 cache_hits.clear(); 1703 cache_hits.clear();
1685 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1704 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1686 full_hashes, &prefix_hits, &cache_hits)); 1705 full_hashes, &prefix_hits, &cache_hits));
1687 ASSERT_EQ(2U, prefix_hits.size()); 1706 ASSERT_EQ(2U, prefix_hits.size());
1688 EXPECT_EQ(kPrefix1, prefix_hits[0]); 1707 EXPECT_EQ(kPrefix1, prefix_hits[0]);
1689 EXPECT_EQ(kPrefix2, prefix_hits[1]); 1708 EXPECT_EQ(kPrefix2, prefix_hits[1]);
1690 EXPECT_TRUE(cache_hits.empty()); 1709 EXPECT_TRUE(cache_hits.empty());
1691 } 1710 }
1692 1711
1693 { 1712 {
1694 // Cache a fullhash result for two kPrefix1 full hashes. 1713 // Cache a fullhash result for two kPrefix1 full hashes.
1695 std::vector<SBPrefix> prefixes(1, kPrefix1); 1714 std::vector<SBPrefix> prefixes(1, kPrefix1);
1696 std::vector<SBFullHashResult> cache_results; 1715 std::vector<SBFullHashResult> cache_results;
1697 1716
1698 SBFullHashResult full_hash_result; 1717 SBFullHashResult full_hash_result;
1699 full_hash_result.list_id = safe_browsing_util::MALWARE; 1718 full_hash_result.list_id = safe_browsing_util::MALWARE;
1700 1719
1701 full_hash_result.hash = kFullHash1_1; 1720 full_hash_result.hash = kFullHash1_1;
1702 cache_results.push_back(full_hash_result); 1721 cache_results.push_back(full_hash_result);
1703 1722
1704 full_hash_result.hash = kFullHash1_3; 1723 full_hash_result.hash = kFullHash1_3;
1705 cache_results.push_back(full_hash_result); 1724 cache_results.push_back(full_hash_result);
1706 1725
1707 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); 1726 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime);
1708 } 1727 }
1709 1728
1710 { 1729 {
1711 // kFullHash1_1 should now see a cache hit. 1730 // kFullHash1_1 should now see a cache hit.
1712 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); 1731 std::vector<SBFullHash> full_hashes(1, kFullHash1_1);
1713 std::vector<SBPrefix> prefix_hits; 1732 std::vector<SBPrefix> prefix_hits;
1714 std::vector<SBFullHashResult> cache_hits; 1733 std::vector<SBFullHashResult> cache_hits;
1715 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1734 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1716 full_hashes, &prefix_hits, &cache_hits)); 1735 full_hashes, &prefix_hits, &cache_hits));
1717 EXPECT_TRUE(prefix_hits.empty()); 1736 EXPECT_TRUE(prefix_hits.empty());
1718 ASSERT_EQ(1U, cache_hits.size()); 1737 ASSERT_EQ(1U, cache_hits.size());
1719 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash)); 1738 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash));
1720 1739
1721 // Adding kFullHash2_1 will see the existing cache hit plus the prefix hit 1740 // Adding kFullHash2_1 will see the existing cache hit plus the prefix hit
1722 // for kPrefix2. 1741 // for kPrefix2.
1723 full_hashes.push_back(kFullHash2_1); 1742 full_hashes.push_back(kFullHash2_1);
1724 prefix_hits.clear(); 1743 prefix_hits.clear();
1725 cache_hits.clear(); 1744 cache_hits.clear();
1726 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1745 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1727 full_hashes, &prefix_hits, &cache_hits)); 1746 full_hashes, &prefix_hits, &cache_hits));
1728 ASSERT_EQ(1U, prefix_hits.size()); 1747 ASSERT_EQ(1U, prefix_hits.size());
1729 EXPECT_EQ(kPrefix2, prefix_hits[0]); 1748 EXPECT_EQ(kPrefix2, prefix_hits[0]);
1730 ASSERT_EQ(1U, cache_hits.size()); 1749 ASSERT_EQ(1U, cache_hits.size());
1731 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash)); 1750 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash));
1732 1751
1733 // kFullHash1_3 also gets a cache hit. 1752 // kFullHash1_3 also gets a cache hit.
1734 full_hashes.push_back(kFullHash1_3); 1753 full_hashes.push_back(kFullHash1_3);
1735 prefix_hits.clear(); 1754 prefix_hits.clear();
1736 cache_hits.clear(); 1755 cache_hits.clear();
1737 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1756 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1738 full_hashes, &prefix_hits, &cache_hits)); 1757 full_hashes, &prefix_hits, &cache_hits));
1739 ASSERT_EQ(1U, prefix_hits.size()); 1758 ASSERT_EQ(1U, prefix_hits.size());
1740 EXPECT_EQ(kPrefix2, prefix_hits[0]); 1759 EXPECT_EQ(kPrefix2, prefix_hits[0]);
1741 ASSERT_EQ(2U, cache_hits.size()); 1760 ASSERT_EQ(2U, cache_hits.size());
1742 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash)); 1761 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash));
1743 EXPECT_TRUE(SBFullHashEqual(kFullHash1_3, cache_hits[1].hash)); 1762 EXPECT_TRUE(SBFullHashEqual(kFullHash1_3, cache_hits[1].hash));
1744 } 1763 }
1745 1764
1746 { 1765 {
1747 // Check if DB contains only kFullHash1_3. Should return a cache hit. 1766 // Check if DB contains only kFullHash1_3. Should return a cache hit.
1748 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); 1767 std::vector<SBFullHash> full_hashes(1, kFullHash1_3);
1749 std::vector<SBPrefix> prefix_hits; 1768 std::vector<SBPrefix> prefix_hits;
1750 std::vector<SBFullHashResult> cache_hits; 1769 std::vector<SBFullHashResult> cache_hits;
1751 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1770 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1752 full_hashes, &prefix_hits, &cache_hits)); 1771 full_hashes, &prefix_hits, &cache_hits));
1753 EXPECT_TRUE(prefix_hits.empty()); 1772 EXPECT_TRUE(prefix_hits.empty());
1754 ASSERT_EQ(1U, cache_hits.size()); 1773 ASSERT_EQ(1U, cache_hits.size());
1755 EXPECT_TRUE(SBFullHashEqual(kFullHash1_3, cache_hits[0].hash)); 1774 EXPECT_TRUE(SBFullHashEqual(kFullHash1_3, cache_hits[0].hash));
1756 } 1775 }
1757 1776
1758 { 1777 {
1759 // kFullHash1_2 has no cache hit, and no prefix hit because of the cache for 1778 // kFullHash1_2 has no cache hit, and no prefix hit because of the cache for
1760 // kPrefix1. 1779 // kPrefix1.
1761 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); 1780 std::vector<SBFullHash> full_hashes(1, kFullHash1_2);
1762 std::vector<SBPrefix> prefix_hits; 1781 std::vector<SBPrefix> prefix_hits;
1763 std::vector<SBFullHashResult> cache_hits; 1782 std::vector<SBFullHashResult> cache_hits;
1764 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( 1783 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting(
1765 full_hashes, &prefix_hits, &cache_hits)); 1784 full_hashes, &prefix_hits, &cache_hits));
1766 1785
1767 // Other prefix hits possible when kFullHash1_2 hits nothing. 1786 // Other prefix hits possible when kFullHash1_2 hits nothing.
1768 full_hashes.push_back(kFullHash2_1); 1787 full_hashes.push_back(kFullHash2_1);
1769 prefix_hits.clear(); 1788 prefix_hits.clear();
1770 cache_hits.clear(); 1789 cache_hits.clear();
1771 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1790 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1772 full_hashes, &prefix_hits, &cache_hits)); 1791 full_hashes, &prefix_hits, &cache_hits));
1773 ASSERT_EQ(1U, prefix_hits.size()); 1792 ASSERT_EQ(1U, prefix_hits.size());
1774 EXPECT_EQ(kPrefix2, prefix_hits[0]); 1793 EXPECT_EQ(kPrefix2, prefix_hits[0]);
1775 EXPECT_TRUE(cache_hits.empty()); 1794 EXPECT_TRUE(cache_hits.empty());
1776 } 1795 }
1777 } 1796 }
1778 1797
1779 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashMatching) { 1798 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashMatching) {
1780 const SBPrefix kPrefix1 = 1001U; 1799 const SBPrefix kPrefix1 = 1001U;
1781 const SBFullHash kFullHash1_1 = 1800 const SBFullHash kFullHash1_1 =
(...skipping 11 matching lines...) Expand all
1793 std::vector<SBListChunkRanges> lists; 1812 std::vector<SBListChunkRanges> lists;
1794 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1813 ASSERT_TRUE(database_->UpdateStarted(&lists));
1795 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 1814 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get());
1796 database_->UpdateFinished(true); 1815 database_->UpdateFinished(true);
1797 1816
1798 { 1817 {
1799 // Check a full hash which isn't present. 1818 // Check a full hash which isn't present.
1800 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); 1819 std::vector<SBFullHash> full_hashes(1, kFullHash1_3);
1801 std::vector<SBPrefix> prefix_hits; 1820 std::vector<SBPrefix> prefix_hits;
1802 std::vector<SBFullHashResult> cache_hits; 1821 std::vector<SBFullHashResult> cache_hits;
1803 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( 1822 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting(
1804 full_hashes, &prefix_hits, &cache_hits)); 1823 full_hashes, &prefix_hits, &cache_hits));
1805 1824
1806 // Also one which is present, should have a prefix hit. 1825 // Also one which is present, should have a prefix hit.
1807 full_hashes.push_back(kFullHash1_1); 1826 full_hashes.push_back(kFullHash1_1);
1808 prefix_hits.clear(); 1827 prefix_hits.clear();
1809 cache_hits.clear(); 1828 cache_hits.clear();
1810 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1829 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1811 full_hashes, &prefix_hits, &cache_hits)); 1830 full_hashes, &prefix_hits, &cache_hits));
1812 ASSERT_EQ(1U, prefix_hits.size()); 1831 ASSERT_EQ(1U, prefix_hits.size());
1813 EXPECT_EQ(kPrefix1, prefix_hits[0]); 1832 EXPECT_EQ(kPrefix1, prefix_hits[0]);
1814 EXPECT_TRUE(cache_hits.empty()); 1833 EXPECT_TRUE(cache_hits.empty());
1815 1834
1816 // Two full hash matches with the same prefix should return one prefix hit. 1835 // Two full hash matches with the same prefix should return one prefix hit.
1817 full_hashes.push_back(kFullHash1_2); 1836 full_hashes.push_back(kFullHash1_2);
1818 prefix_hits.clear(); 1837 prefix_hits.clear();
1819 cache_hits.clear(); 1838 cache_hits.clear();
1820 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1839 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1821 full_hashes, &prefix_hits, &cache_hits)); 1840 full_hashes, &prefix_hits, &cache_hits));
1822 ASSERT_EQ(1U, prefix_hits.size()); 1841 ASSERT_EQ(1U, prefix_hits.size());
1823 EXPECT_EQ(kPrefix1, prefix_hits[0]); 1842 EXPECT_EQ(kPrefix1, prefix_hits[0]);
1824 EXPECT_TRUE(cache_hits.empty()); 1843 EXPECT_TRUE(cache_hits.empty());
1825 } 1844 }
1826 1845
1827 { 1846 {
1828 // Cache a gethash result for kFullHash1_2. 1847 // Cache a gethash result for kFullHash1_2.
1829 SBFullHashResult full_hash_result; 1848 SBFullHashResult full_hash_result;
1830 full_hash_result.list_id = safe_browsing_util::MALWARE; 1849 full_hash_result.list_id = safe_browsing_util::MALWARE;
1831 full_hash_result.hash = kFullHash1_2; 1850 full_hash_result.hash = kFullHash1_2;
1832 1851
1833 std::vector<SBPrefix> prefixes(1, kPrefix1); 1852 std::vector<SBPrefix> prefixes(1, kPrefix1);
1834 std::vector<SBFullHashResult> cache_results(1, full_hash_result); 1853 std::vector<SBFullHashResult> cache_results(1, full_hash_result);
1835 1854
1836 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); 1855 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime);
1837 } 1856 }
1838 1857
1839 { 1858 {
1840 // kFullHash1_3 should still return false, because the cached 1859 // kFullHash1_3 should still return false, because the cached
1841 // result for kPrefix1 doesn't contain kFullHash1_3. 1860 // result for kPrefix1 doesn't contain kFullHash1_3.
1842 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); 1861 std::vector<SBFullHash> full_hashes(1, kFullHash1_3);
1843 std::vector<SBPrefix> prefix_hits; 1862 std::vector<SBPrefix> prefix_hits;
1844 std::vector<SBFullHashResult> cache_hits; 1863 std::vector<SBFullHashResult> cache_hits;
1845 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( 1864 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting(
1846 full_hashes, &prefix_hits, &cache_hits)); 1865 full_hashes, &prefix_hits, &cache_hits));
1847 1866
1848 // kFullHash1_1 is also not in the cached result, which takes 1867 // kFullHash1_1 is also not in the cached result, which takes
1849 // priority over the database. 1868 // priority over the database.
1850 prefix_hits.clear(); 1869 prefix_hits.clear();
1851 full_hashes.push_back(kFullHash1_1); 1870 full_hashes.push_back(kFullHash1_1);
1852 cache_hits.clear(); 1871 cache_hits.clear();
1853 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( 1872 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting(
1854 full_hashes, &prefix_hits, &cache_hits)); 1873 full_hashes, &prefix_hits, &cache_hits));
1855 1874
1856 // kFullHash1_2 is in the cached result. 1875 // kFullHash1_2 is in the cached result.
1857 full_hashes.push_back(kFullHash1_2); 1876 full_hashes.push_back(kFullHash1_2);
1858 prefix_hits.clear(); 1877 prefix_hits.clear();
1859 cache_hits.clear(); 1878 cache_hits.clear();
1860 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1879 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1861 full_hashes, &prefix_hits, &cache_hits)); 1880 full_hashes, &prefix_hits, &cache_hits));
1862 EXPECT_TRUE(prefix_hits.empty()); 1881 EXPECT_TRUE(prefix_hits.empty());
1863 ASSERT_EQ(1U, cache_hits.size()); 1882 ASSERT_EQ(1U, cache_hits.size());
1864 EXPECT_TRUE(SBFullHashEqual(kFullHash1_2, cache_hits[0].hash)); 1883 EXPECT_TRUE(SBFullHashEqual(kFullHash1_2, cache_hits[0].hash));
1865 } 1884 }
1866 1885
1867 // Remove kFullHash1_1 from the database. 1886 // Remove kFullHash1_1 from the database.
1868 chunks.clear(); 1887 chunks.clear();
1869 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); 1888 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1));
1870 1889
1871 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1890 ASSERT_TRUE(database_->UpdateStarted(&lists));
1872 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 1891 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get());
1873 database_->UpdateFinished(true); 1892 database_->UpdateFinished(true);
1874 1893
1875 // Cache should be cleared after updating. 1894 // Cache should be cleared after updating.
1876 EXPECT_TRUE(database_->browse_gethash_cache_.empty()); 1895 EXPECT_TRUE(database_->prefix_gethash_cache_.empty());
1877 1896
1878 { 1897 {
1879 // Now the database doesn't contain kFullHash1_1. 1898 // Now the database doesn't contain kFullHash1_1.
1880 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); 1899 std::vector<SBFullHash> full_hashes(1, kFullHash1_1);
1881 std::vector<SBPrefix> prefix_hits; 1900 std::vector<SBPrefix> prefix_hits;
1882 std::vector<SBFullHashResult> cache_hits; 1901 std::vector<SBFullHashResult> cache_hits;
1883 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( 1902 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting(
1884 full_hashes, &prefix_hits, &cache_hits)); 1903 full_hashes, &prefix_hits, &cache_hits));
1885 1904
1886 // Nor kFullHash1_3. 1905 // Nor kFullHash1_3.
1887 full_hashes.push_back(kFullHash1_3); 1906 full_hashes.push_back(kFullHash1_3);
1888 prefix_hits.clear(); 1907 prefix_hits.clear();
1889 cache_hits.clear(); 1908 cache_hits.clear();
1890 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( 1909 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting(
1891 full_hashes, &prefix_hits, &cache_hits)); 1910 full_hashes, &prefix_hits, &cache_hits));
1892 1911
1893 // Still has kFullHash1_2. 1912 // Still has kFullHash1_2.
1894 full_hashes.push_back(kFullHash1_2); 1913 full_hashes.push_back(kFullHash1_2);
1895 prefix_hits.clear(); 1914 prefix_hits.clear();
1896 cache_hits.clear(); 1915 cache_hits.clear();
1897 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1916 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1898 full_hashes, &prefix_hits, &cache_hits)); 1917 full_hashes, &prefix_hits, &cache_hits));
1899 ASSERT_EQ(1U, prefix_hits.size()); 1918 ASSERT_EQ(1U, prefix_hits.size());
1900 EXPECT_EQ(kPrefix1, prefix_hits[0]); 1919 EXPECT_EQ(kPrefix1, prefix_hits[0]);
1901 EXPECT_TRUE(cache_hits.empty()); 1920 EXPECT_TRUE(cache_hits.empty());
1902 } 1921 }
1903 1922
1904 // Remove kFullHash1_2 from the database. 1923 // Remove kFullHash1_2 from the database.
1905 chunks.clear(); 1924 chunks.clear();
1906 chunks.push_back(SubChunkFullHash(12, kFullHash1_2, 2)); 1925 chunks.push_back(SubChunkFullHash(12, kFullHash1_2, 2));
1907 1926
1908 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1927 ASSERT_TRUE(database_->UpdateStarted(&lists));
1909 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 1928 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get());
1910 database_->UpdateFinished(true); 1929 database_->UpdateFinished(true);
1911 1930
1912 // Cache should be cleared after updating. 1931 // Cache should be cleared after updating.
1913 EXPECT_TRUE(database_->browse_gethash_cache_.empty()); 1932 EXPECT_TRUE(database_->prefix_gethash_cache_.empty());
1914 1933
1915 { 1934 {
1916 // None are present. 1935 // None are present.
1917 std::vector<SBFullHash> full_hashes; 1936 std::vector<SBFullHash> full_hashes;
1918 std::vector<SBPrefix> prefix_hits; 1937 std::vector<SBPrefix> prefix_hits;
1919 std::vector<SBFullHashResult> cache_hits; 1938 std::vector<SBFullHashResult> cache_hits;
1920 full_hashes.push_back(kFullHash1_1); 1939 full_hashes.push_back(kFullHash1_1);
1921 full_hashes.push_back(kFullHash1_2); 1940 full_hashes.push_back(kFullHash1_2);
1922 full_hashes.push_back(kFullHash1_3); 1941 full_hashes.push_back(kFullHash1_3);
1923 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( 1942 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting(
1924 full_hashes, &prefix_hits, &cache_hits)); 1943 full_hashes, &prefix_hits, &cache_hits));
1925 } 1944 }
1926 } 1945 }
1927 1946
1928 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashAndPrefixMatching) { 1947 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashAndPrefixMatching) {
1929 const SBPrefix kPrefix1 = 1001U; 1948 const SBPrefix kPrefix1 = 1001U;
1930 const SBFullHash kFullHash1_1 = 1949 const SBFullHash kFullHash1_1 =
1931 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); 1950 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01");
1932 const SBFullHash kFullHash1_2 = 1951 const SBFullHash kFullHash1_2 =
1933 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02"); 1952 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02");
1934 1953
1935 ScopedVector<SBChunkData> chunks; 1954 ScopedVector<SBChunkData> chunks;
1936 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); 1955 chunks.push_back(AddChunkFullHash(1, kFullHash1_1));
1937 1956
1938 std::vector<SBListChunkRanges> lists; 1957 std::vector<SBListChunkRanges> lists;
1939 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1958 ASSERT_TRUE(database_->UpdateStarted(&lists));
1940 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 1959 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get());
1941 database_->UpdateFinished(true); 1960 database_->UpdateFinished(true);
1942 1961
1943 { 1962 {
1944 // kFullHash1_2 does not match kFullHash1_1. 1963 // kFullHash1_2 does not match kFullHash1_1.
1945 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); 1964 std::vector<SBFullHash> full_hashes(1, kFullHash1_2);
1946 std::vector<SBPrefix> prefix_hits; 1965 std::vector<SBPrefix> prefix_hits;
1947 std::vector<SBFullHashResult> cache_hits; 1966 std::vector<SBFullHashResult> cache_hits;
1948 EXPECT_FALSE(database_->ContainsBrowseUrlHashes( 1967 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting(
1949 full_hashes, &prefix_hits, &cache_hits)); 1968 full_hashes, &prefix_hits, &cache_hits));
1950 } 1969 }
1951 1970
1952 // Add a prefix match. 1971 // Add a prefix match.
1953 chunks.clear(); 1972 chunks.clear();
1954 chunks.push_back(AddChunkPrefix(2, kPrefix1)); 1973 chunks.push_back(AddChunkPrefix(2, kPrefix1));
1955 1974
1956 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1975 ASSERT_TRUE(database_->UpdateStarted(&lists));
1957 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 1976 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get());
1958 database_->UpdateFinished(true); 1977 database_->UpdateFinished(true);
1959 1978
1960 { 1979 {
1961 // kFullHash1_2 does match kPrefix1. 1980 // kFullHash1_2 does match kPrefix1.
1962 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); 1981 std::vector<SBFullHash> full_hashes(1, kFullHash1_2);
1963 std::vector<SBPrefix> prefix_hits; 1982 std::vector<SBPrefix> prefix_hits;
1964 std::vector<SBFullHashResult> cache_hits; 1983 std::vector<SBFullHashResult> cache_hits;
1965 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 1984 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1966 full_hashes, &prefix_hits, &cache_hits)); 1985 full_hashes, &prefix_hits, &cache_hits));
1967 ASSERT_EQ(1U, prefix_hits.size()); 1986 ASSERT_EQ(1U, prefix_hits.size());
1968 EXPECT_EQ(kPrefix1, prefix_hits[0]); 1987 EXPECT_EQ(kPrefix1, prefix_hits[0]);
1969 EXPECT_TRUE(cache_hits.empty()); 1988 EXPECT_TRUE(cache_hits.empty());
1970 } 1989 }
1971 1990
1972 // Remove the full hash. 1991 // Remove the full hash.
1973 chunks.clear(); 1992 chunks.clear();
1974 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); 1993 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1));
1975 1994
1976 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1995 ASSERT_TRUE(database_->UpdateStarted(&lists));
1977 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); 1996 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get());
1978 database_->UpdateFinished(true); 1997 database_->UpdateFinished(true);
1979 1998
1980 { 1999 {
1981 // kFullHash1_2 still returns true due to the prefix hit. 2000 // kFullHash1_2 still returns true due to the prefix hit.
1982 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); 2001 std::vector<SBFullHash> full_hashes(1, kFullHash1_2);
1983 std::vector<SBPrefix> prefix_hits; 2002 std::vector<SBPrefix> prefix_hits;
1984 std::vector<SBFullHashResult> cache_hits; 2003 std::vector<SBFullHashResult> cache_hits;
1985 EXPECT_TRUE(database_->ContainsBrowseUrlHashes( 2004 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting(
1986 full_hashes, &prefix_hits, &cache_hits)); 2005 full_hashes, &prefix_hits, &cache_hits));
1987 ASSERT_EQ(1U, prefix_hits.size()); 2006 ASSERT_EQ(1U, prefix_hits.size());
1988 EXPECT_EQ(kPrefix1, prefix_hits[0]); 2007 EXPECT_EQ(kPrefix1, prefix_hits[0]);
1989 EXPECT_TRUE(cache_hits.empty()); 2008 EXPECT_TRUE(cache_hits.empty());
1990 } 2009 }
1991 } 2010 }
1992 2011
1993 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) { 2012 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; 2013 std::vector<SBListChunkRanges> lists;
2006 ASSERT_TRUE(database_->UpdateStarted(&lists)); 2014 ASSERT_TRUE(database_->UpdateStarted(&lists));
2007 2015
2008 ScopedVector<SBChunkData> chunks; 2016 ScopedVector<SBChunkData> chunks;
2009 2017
2010 // IPv4 prefix match for ::ffff:192.168.1.0/120. 2018 // IPv4 prefix match for ::ffff:192.168.1.0/120.
2011 chunks.push_back(AddChunkHashedIpValue(1, "::ffff:192.168.1.0", 120)); 2019 chunks.push_back(AddChunkHashedIpValue(1, "::ffff:192.168.1.0", 120));
2012 2020
2013 // IPv4 exact match for ::ffff:192.1.1.1. 2021 // IPv4 exact match for ::ffff:192.1.1.1.
2014 chunks.push_back(AddChunkHashedIpValue(2, "::ffff:192.1.1.1", 128)); 2022 chunks.push_back(AddChunkHashedIpValue(2, "::ffff:192.1.1.1", 128));
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
2129 GURL(std::string("http://") + kExampleCollision), 2137 GURL(std::string("http://") + kExampleCollision),
2130 &prefix_hits, &cache_hits)); 2138 &prefix_hits, &cache_hits));
2131 ASSERT_EQ(1U, prefix_hits.size()); 2139 ASSERT_EQ(1U, prefix_hits.size());
2132 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); 2140 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]);
2133 EXPECT_TRUE(cache_hits.empty()); 2141 EXPECT_TRUE(cache_hits.empty());
2134 2142
2135 // This prefix collides, but no full hash match. 2143 // This prefix collides, but no full hash match.
2136 EXPECT_FALSE(database_->ContainsBrowseUrl( 2144 EXPECT_FALSE(database_->ContainsBrowseUrl(
2137 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); 2145 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits));
2138 } 2146 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698