| 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 14 matching lines...) Expand all Loading... |
| 25 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 26 | 26 |
| 27 using base::Time; | 27 using base::Time; |
| 28 using base::TimeDelta; | 28 using base::TimeDelta; |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 const TimeDelta kCacheLifetime = TimeDelta::FromMinutes(45); | 32 const TimeDelta kCacheLifetime = TimeDelta::FromMinutes(45); |
| 33 | 33 |
| 34 SBPrefix SBPrefixForString(const std::string& str) { | 34 SBPrefix SBPrefixForString(const std::string& str) { |
| 35 return safe_browsing::SBFullHashForString(str).prefix; | 35 return SBFullHashForString(str).prefix; |
| 36 } | 36 } |
| 37 | 37 |
| 38 // Construct a full hash which has the given prefix, with the given | 38 // Construct a full hash which has the given prefix, with the given |
| 39 // suffix data coming after the prefix. | 39 // suffix data coming after the prefix. |
| 40 SBFullHash SBFullHashForPrefixAndSuffix(SBPrefix prefix, | 40 SBFullHash SBFullHashForPrefixAndSuffix(SBPrefix prefix, |
| 41 const base::StringPiece& suffix) { | 41 const base::StringPiece& suffix) { |
| 42 SBFullHash full_hash; | 42 SBFullHash full_hash; |
| 43 memset(&full_hash, 0, sizeof(SBFullHash)); | 43 memset(&full_hash, 0, sizeof(SBFullHash)); |
| 44 full_hash.prefix = prefix; | 44 full_hash.prefix = prefix; |
| 45 CHECK_LE(suffix.size() + sizeof(SBPrefix), sizeof(SBFullHash)); | 45 CHECK_LE(suffix.size() + sizeof(SBPrefix), sizeof(SBFullHash)); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 SBChunkData* AddChunkFullHash(int chunk_number, SBFullHash full_hash) { | 128 SBChunkData* AddChunkFullHash(int chunk_number, SBFullHash full_hash) { |
| 129 return BuildChunk(chunk_number, safe_browsing::ChunkData::ADD, | 129 return BuildChunk(chunk_number, safe_browsing::ChunkData::ADD, |
| 130 safe_browsing::ChunkData::FULL_32B, | 130 safe_browsing::ChunkData::FULL_32B, |
| 131 &full_hash, sizeof(full_hash), | 131 &full_hash, sizeof(full_hash), |
| 132 std::vector<int>()); | 132 std::vector<int>()); |
| 133 } | 133 } |
| 134 | 134 |
| 135 // Generate an add chunk with a full hash generated from |value|. | 135 // Generate an add chunk with a full hash generated from |value|. |
| 136 SBChunkData* AddChunkFullHashValue(int chunk_number, | 136 SBChunkData* AddChunkFullHashValue(int chunk_number, |
| 137 const std::string& value) { | 137 const std::string& value) { |
| 138 return AddChunkFullHash(chunk_number, | 138 return AddChunkFullHash(chunk_number, SBFullHashForString(value)); |
| 139 safe_browsing::SBFullHashForString(value)); | |
| 140 } | 139 } |
| 141 | 140 |
| 142 // Generate an add chunk with two full hashes. | 141 // Generate an add chunk with two full hashes. |
| 143 SBChunkData* AddChunkFullHash2Value(int chunk_number, | 142 SBChunkData* AddChunkFullHash2Value(int chunk_number, |
| 144 const std::string& value1, | 143 const std::string& value1, |
| 145 const std::string& value2) { | 144 const std::string& value2) { |
| 146 const SBFullHash full_hashes[2] = { | 145 const SBFullHash full_hashes[2] = { |
| 147 safe_browsing::SBFullHashForString(value1), | 146 SBFullHashForString(value1), |
| 148 safe_browsing::SBFullHashForString(value2), | 147 SBFullHashForString(value2), |
| 149 }; | 148 }; |
| 150 return BuildChunk(chunk_number, safe_browsing::ChunkData::ADD, | 149 return BuildChunk(chunk_number, safe_browsing::ChunkData::ADD, |
| 151 safe_browsing::ChunkData::FULL_32B, | 150 safe_browsing::ChunkData::FULL_32B, |
| 152 &full_hashes[0], sizeof(full_hashes), | 151 &full_hashes[0], sizeof(full_hashes), |
| 153 std::vector<int>()); | 152 std::vector<int>()); |
| 154 } | 153 } |
| 155 | 154 |
| 156 // Generate a sub chunk with a prefix generated from |value|. | 155 // Generate a sub chunk with a prefix generated from |value|. |
| 157 SBChunkData* SubChunkPrefixValue(int chunk_number, | 156 SBChunkData* SubChunkPrefixValue(int chunk_number, |
| 158 const std::string& value, | 157 const std::string& value, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 safe_browsing::ChunkData::FULL_32B, | 190 safe_browsing::ChunkData::FULL_32B, |
| 192 &full_hash, sizeof(full_hash), | 191 &full_hash, sizeof(full_hash), |
| 193 std::vector<int>(1, add_chunk_number)); | 192 std::vector<int>(1, add_chunk_number)); |
| 194 } | 193 } |
| 195 | 194 |
| 196 // Generate a sub chunk with a full hash generated from |value|. | 195 // Generate a sub chunk with a full hash generated from |value|. |
| 197 SBChunkData* SubChunkFullHashValue(int chunk_number, | 196 SBChunkData* SubChunkFullHashValue(int chunk_number, |
| 198 const std::string& value, | 197 const std::string& value, |
| 199 int add_chunk_number) { | 198 int add_chunk_number) { |
| 200 return SubChunkFullHash(chunk_number, | 199 return SubChunkFullHash(chunk_number, |
| 201 safe_browsing::SBFullHashForString(value), | 200 SBFullHashForString(value), |
| 202 add_chunk_number); | 201 add_chunk_number); |
| 203 } | 202 } |
| 204 | 203 |
| 205 // Generate an add chunk with a single full hash for the ip blacklist. | 204 // Generate an add chunk with a single full hash for the ip blacklist. |
| 206 SBChunkData* AddChunkHashedIpValue(int chunk_number, | 205 SBChunkData* AddChunkHashedIpValue(int chunk_number, |
| 207 const std::string& ip_str, | 206 const std::string& ip_str, |
| 208 size_t prefix_size) { | 207 size_t prefix_size) { |
| 209 const std::string full_hash_str = HashedIpPrefix(ip_str, prefix_size); | 208 const std::string full_hash_str = HashedIpPrefix(ip_str, prefix_size); |
| 210 EXPECT_EQ(sizeof(SBFullHash), full_hash_str.size()); | 209 EXPECT_EQ(sizeof(SBFullHash), full_hash_str.size()); |
| 211 SBFullHash full_hash; | 210 SBFullHash full_hash; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 // Tests retrieving list name information. | 344 // Tests retrieving list name information. |
| 346 TEST_F(SafeBrowsingDatabaseTest, BrowseListsInfo) { | 345 TEST_F(SafeBrowsingDatabaseTest, BrowseListsInfo) { |
| 347 std::vector<SBListChunkRanges> lists; | 346 std::vector<SBListChunkRanges> lists; |
| 348 ScopedVector<SBChunkData> chunks; | 347 ScopedVector<SBChunkData> chunks; |
| 349 | 348 |
| 350 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); | 349 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); |
| 351 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); | 350 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); |
| 352 chunks.push_back(AddChunkPrefixValue(3, "www.whatever.com/malware.html")); | 351 chunks.push_back(AddChunkPrefixValue(3, "www.whatever.com/malware.html")); |
| 353 | 352 |
| 354 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 353 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 355 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 354 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 356 database_->UpdateFinished(true); | 355 database_->UpdateFinished(true); |
| 357 | 356 |
| 358 GetListsInfo(&lists); | 357 GetListsInfo(&lists); |
| 359 ASSERT_LE(1U, lists.size()); | 358 ASSERT_LE(1U, lists.size()); |
| 360 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 359 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 361 EXPECT_EQ("1-3", lists[0].adds); | 360 EXPECT_EQ("1-3", lists[0].adds); |
| 362 EXPECT_TRUE(lists[0].subs.empty()); | 361 EXPECT_TRUE(lists[0].subs.empty()); |
| 363 | 362 |
| 364 // Insert a malware sub chunk. | 363 // Insert a malware sub chunk. |
| 365 chunks.clear(); | 364 chunks.clear(); |
| 366 chunks.push_back(SubChunkPrefixValue(7, "www.subbed.com/noteveil1.html", 19)); | 365 chunks.push_back(SubChunkPrefixValue(7, "www.subbed.com/noteveil1.html", 19)); |
| 367 | 366 |
| 368 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 367 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 369 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 368 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 370 database_->UpdateFinished(true); | 369 database_->UpdateFinished(true); |
| 371 | 370 |
| 372 GetListsInfo(&lists); | 371 GetListsInfo(&lists); |
| 373 ASSERT_LE(1U, lists.size()); | 372 ASSERT_LE(1U, lists.size()); |
| 374 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 373 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 375 EXPECT_EQ("1-3", lists[0].adds); | 374 EXPECT_EQ("1-3", lists[0].adds); |
| 376 EXPECT_EQ("7", lists[0].subs); | 375 EXPECT_EQ("7", lists[0].subs); |
| 377 if (lists.size() == 2) { | 376 if (lists.size() == 2) { |
| 378 // Old style database won't have the second entry since it creates the lists | 377 // Old style database won't have the second entry since it creates the lists |
| 379 // when it receives an update containing that list. The filter-based | 378 // when it receives an update containing that list. The filter-based |
| 380 // database has these values hard coded. | 379 // database has these values hard coded. |
| 381 EXPECT_EQ(safe_browsing::kPhishingList, lists[1].name); | 380 EXPECT_EQ(safe_browsing_util::kPhishingList, lists[1].name); |
| 382 EXPECT_TRUE(lists[1].adds.empty()); | 381 EXPECT_TRUE(lists[1].adds.empty()); |
| 383 EXPECT_TRUE(lists[1].subs.empty()); | 382 EXPECT_TRUE(lists[1].subs.empty()); |
| 384 } | 383 } |
| 385 | 384 |
| 386 // Add phishing chunks. | 385 // Add phishing chunks. |
| 387 chunks.clear(); | 386 chunks.clear(); |
| 388 chunks.push_back(AddChunkPrefixValue(47, "www.evil.com/phishing.html")); | 387 chunks.push_back(AddChunkPrefixValue(47, "www.evil.com/phishing.html")); |
| 389 chunks.push_back( | 388 chunks.push_back( |
| 390 SubChunkPrefixValue(200, "www.phishy.com/notevil1.html", 1999)); | 389 SubChunkPrefixValue(200, "www.phishy.com/notevil1.html", 1999)); |
| 391 chunks.push_back( | 390 chunks.push_back( |
| 392 SubChunkPrefixValue(201, "www.phishy2.com/notevil1.html", 1999)); | 391 SubChunkPrefixValue(201, "www.phishy2.com/notevil1.html", 1999)); |
| 393 | 392 |
| 394 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 393 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 395 database_->InsertChunks(safe_browsing::kPhishingList, chunks.get()); | 394 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks.get()); |
| 396 database_->UpdateFinished(true); | 395 database_->UpdateFinished(true); |
| 397 | 396 |
| 398 GetListsInfo(&lists); | 397 GetListsInfo(&lists); |
| 399 ASSERT_LE(2U, lists.size()); | 398 ASSERT_LE(2U, lists.size()); |
| 400 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 399 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 401 EXPECT_EQ("1-3", lists[0].adds); | 400 EXPECT_EQ("1-3", lists[0].adds); |
| 402 EXPECT_EQ("7", lists[0].subs); | 401 EXPECT_EQ("7", lists[0].subs); |
| 403 EXPECT_EQ(safe_browsing::kPhishingList, lists[1].name); | 402 EXPECT_EQ(safe_browsing_util::kPhishingList, lists[1].name); |
| 404 EXPECT_EQ("47", lists[1].adds); | 403 EXPECT_EQ("47", lists[1].adds); |
| 405 EXPECT_EQ("200-201", lists[1].subs); | 404 EXPECT_EQ("200-201", lists[1].subs); |
| 406 } | 405 } |
| 407 | 406 |
| 408 TEST_F(SafeBrowsingDatabaseTest, ListNames) { | 407 TEST_F(SafeBrowsingDatabaseTest, ListNames) { |
| 409 ScopedVector<SBChunkData> chunks; | 408 ScopedVector<SBChunkData> chunks; |
| 410 | 409 |
| 411 std::vector<SBListChunkRanges> lists; | 410 std::vector<SBListChunkRanges> lists; |
| 412 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 411 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 413 | 412 |
| 414 // Insert malware, phish, binurl and bindownload add chunks. | 413 // Insert malware, phish, binurl and bindownload add chunks. |
| 415 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); | 414 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); |
| 416 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 415 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 417 | 416 |
| 418 chunks.clear(); | 417 chunks.clear(); |
| 419 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); | 418 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); |
| 420 database_->InsertChunks(safe_browsing::kPhishingList, chunks.get()); | 419 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks.get()); |
| 421 | 420 |
| 422 chunks.clear(); | 421 chunks.clear(); |
| 423 chunks.push_back(AddChunkPrefixValue(3, "www.whatever.com/download.html")); | 422 chunks.push_back(AddChunkPrefixValue(3, "www.whatever.com/download.html")); |
| 424 database_->InsertChunks(safe_browsing::kBinUrlList, chunks.get()); | 423 database_->InsertChunks(safe_browsing_util::kBinUrlList, chunks.get()); |
| 425 | 424 |
| 426 chunks.clear(); | 425 chunks.clear(); |
| 427 chunks.push_back(AddChunkFullHashValue(5, "www.forwhitelist.com/a.html")); | 426 chunks.push_back(AddChunkFullHashValue(5, "www.forwhitelist.com/a.html")); |
| 428 database_->InsertChunks(safe_browsing::kCsdWhiteList, chunks.get()); | 427 database_->InsertChunks(safe_browsing_util::kCsdWhiteList, chunks.get()); |
| 429 | 428 |
| 430 chunks.clear(); | 429 chunks.clear(); |
| 431 chunks.push_back(AddChunkFullHashValue(6, "www.download.com/")); | 430 chunks.push_back(AddChunkFullHashValue(6, "www.download.com/")); |
| 432 database_->InsertChunks(safe_browsing::kDownloadWhiteList, chunks.get()); | 431 database_->InsertChunks(safe_browsing_util::kDownloadWhiteList, chunks.get()); |
| 433 | 432 |
| 434 chunks.clear(); | 433 chunks.clear(); |
| 435 chunks.push_back(AddChunkFullHashValue(7, "www.inclusion.com/")); | 434 chunks.push_back(AddChunkFullHashValue(7, "www.inclusion.com/")); |
| 436 database_->InsertChunks(safe_browsing::kInclusionWhitelist, | 435 database_->InsertChunks(safe_browsing_util::kInclusionWhitelist, |
| 437 chunks.get()); | 436 chunks.get()); |
| 438 | 437 |
| 439 chunks.clear(); | 438 chunks.clear(); |
| 440 chunks.push_back(AddChunkFullHashValue(8, | 439 chunks.push_back(AddChunkFullHashValue(8, |
| 441 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | 440 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 442 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | 441 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); |
| 443 database_->InsertChunks(safe_browsing::kExtensionBlacklist, | 442 database_->InsertChunks(safe_browsing_util::kExtensionBlacklist, |
| 444 chunks.get()); | 443 chunks.get()); |
| 445 | 444 |
| 446 chunks.clear(); | 445 chunks.clear(); |
| 447 chunks.push_back(AddChunkHashedIpValue(10, "::ffff:192.168.1.0", 120)); | 446 chunks.push_back(AddChunkHashedIpValue(10, "::ffff:192.168.1.0", 120)); |
| 448 database_->InsertChunks(safe_browsing::kIPBlacklist, chunks.get()); | 447 database_->InsertChunks(safe_browsing_util::kIPBlacklist, chunks.get()); |
| 449 | 448 |
| 450 chunks.clear(); | 449 chunks.clear(); |
| 451 chunks.push_back(AddChunkPrefixValue(11, "www.unwanted.com/software.html")); | 450 chunks.push_back(AddChunkPrefixValue(11, "www.unwanted.com/software.html")); |
| 452 database_->InsertChunks(safe_browsing::kUnwantedUrlList, chunks.get()); | 451 database_->InsertChunks(safe_browsing_util::kUnwantedUrlList, chunks.get()); |
| 453 | 452 |
| 454 database_->UpdateFinished(true); | 453 database_->UpdateFinished(true); |
| 455 | 454 |
| 456 GetListsInfo(&lists); | 455 GetListsInfo(&lists); |
| 457 ASSERT_EQ(9U, lists.size()); | 456 ASSERT_EQ(9U, lists.size()); |
| 458 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 457 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 459 EXPECT_EQ("1", lists[0].adds); | 458 EXPECT_EQ("1", lists[0].adds); |
| 460 EXPECT_TRUE(lists[0].subs.empty()); | 459 EXPECT_TRUE(lists[0].subs.empty()); |
| 461 EXPECT_EQ(safe_browsing::kPhishingList, lists[1].name); | 460 EXPECT_EQ(safe_browsing_util::kPhishingList, lists[1].name); |
| 462 EXPECT_EQ("2", lists[1].adds); | 461 EXPECT_EQ("2", lists[1].adds); |
| 463 EXPECT_TRUE(lists[1].subs.empty()); | 462 EXPECT_TRUE(lists[1].subs.empty()); |
| 464 EXPECT_EQ(safe_browsing::kBinUrlList, lists[2].name); | 463 EXPECT_EQ(safe_browsing_util::kBinUrlList, lists[2].name); |
| 465 EXPECT_EQ("3", lists[2].adds); | 464 EXPECT_EQ("3", lists[2].adds); |
| 466 EXPECT_TRUE(lists[2].subs.empty()); | 465 EXPECT_TRUE(lists[2].subs.empty()); |
| 467 EXPECT_EQ(safe_browsing::kCsdWhiteList, lists[3].name); | 466 EXPECT_EQ(safe_browsing_util::kCsdWhiteList, lists[3].name); |
| 468 EXPECT_EQ("5", lists[3].adds); | 467 EXPECT_EQ("5", lists[3].adds); |
| 469 EXPECT_TRUE(lists[3].subs.empty()); | 468 EXPECT_TRUE(lists[3].subs.empty()); |
| 470 EXPECT_EQ(safe_browsing::kDownloadWhiteList, lists[4].name); | 469 EXPECT_EQ(safe_browsing_util::kDownloadWhiteList, lists[4].name); |
| 471 EXPECT_EQ("6", lists[4].adds); | 470 EXPECT_EQ("6", lists[4].adds); |
| 472 EXPECT_TRUE(lists[4].subs.empty()); | 471 EXPECT_TRUE(lists[4].subs.empty()); |
| 473 EXPECT_EQ(safe_browsing::kInclusionWhitelist, lists[5].name); | 472 EXPECT_EQ(safe_browsing_util::kInclusionWhitelist, lists[5].name); |
| 474 EXPECT_EQ("7", lists[5].adds); | 473 EXPECT_EQ("7", lists[5].adds); |
| 475 EXPECT_TRUE(lists[5].subs.empty()); | 474 EXPECT_TRUE(lists[5].subs.empty()); |
| 476 EXPECT_EQ(safe_browsing::kExtensionBlacklist, lists[6].name); | 475 EXPECT_EQ(safe_browsing_util::kExtensionBlacklist, lists[6].name); |
| 477 EXPECT_EQ("8", lists[6].adds); | 476 EXPECT_EQ("8", lists[6].adds); |
| 478 EXPECT_TRUE(lists[6].subs.empty()); | 477 EXPECT_TRUE(lists[6].subs.empty()); |
| 479 EXPECT_EQ(safe_browsing::kIPBlacklist, lists[7].name); | 478 EXPECT_EQ(safe_browsing_util::kIPBlacklist, lists[7].name); |
| 480 EXPECT_EQ("10", lists[7].adds); | 479 EXPECT_EQ("10", lists[7].adds); |
| 481 EXPECT_TRUE(lists[7].subs.empty()); | 480 EXPECT_TRUE(lists[7].subs.empty()); |
| 482 EXPECT_EQ(safe_browsing::kUnwantedUrlList, lists[8].name); | 481 EXPECT_EQ(safe_browsing_util::kUnwantedUrlList, lists[8].name); |
| 483 EXPECT_EQ("11", lists[8].adds); | 482 EXPECT_EQ("11", lists[8].adds); |
| 484 EXPECT_TRUE(lists[8].subs.empty()); | 483 EXPECT_TRUE(lists[8].subs.empty()); |
| 485 | 484 |
| 486 database_.reset(); | 485 database_.reset(); |
| 487 } | 486 } |
| 488 | 487 |
| 489 // Checks database reading and writing for browse and unwanted PrefixSets. | 488 // Checks database reading and writing for browse and unwanted PrefixSets. |
| 490 TEST_F(SafeBrowsingDatabaseTest, BrowseAndUnwantedDatabasesAndPrefixSets) { | 489 TEST_F(SafeBrowsingDatabaseTest, BrowseAndUnwantedDatabasesAndPrefixSets) { |
| 491 struct TestCase { | 490 struct TestCase { |
| 492 using TestListContainsBadUrl = bool (SafeBrowsingDatabase::*)( | 491 using TestListContainsBadUrl = bool (SafeBrowsingDatabase::*)( |
| 493 const GURL& url, | 492 const GURL& url, |
| 494 std::vector<SBPrefix>* prefix_hits, | 493 std::vector<SBPrefix>* prefix_hits, |
| 495 std::vector<SBFullHashResult>* cache_hits); | 494 std::vector<SBFullHashResult>* cache_hits); |
| 496 | 495 |
| 497 const char* test_list_name; | 496 const char* test_list_name; |
| 498 size_t expected_list_index; | 497 size_t expected_list_index; |
| 499 TestListContainsBadUrl test_list_contains_bad_url; | 498 TestListContainsBadUrl test_list_contains_bad_url; |
| 500 } const kTestCases[] { | 499 } const kTestCases[] { |
| 501 { safe_browsing::kMalwareList, 0U, | 500 { safe_browsing_util::kMalwareList, 0U, |
| 502 &SafeBrowsingDatabase::ContainsBrowseUrl }, | 501 &SafeBrowsingDatabase::ContainsBrowseUrl }, |
| 503 { safe_browsing::kPhishingList, 1U, | 502 { safe_browsing_util::kPhishingList, 1U, |
| 504 &SafeBrowsingDatabase::ContainsBrowseUrl }, | 503 &SafeBrowsingDatabase::ContainsBrowseUrl }, |
| 505 { safe_browsing::kUnwantedUrlList, 8U, | 504 { safe_browsing_util::kUnwantedUrlList, 8U, |
| 506 &SafeBrowsingDatabase::ContainsUnwantedSoftwareUrl }, | 505 &SafeBrowsingDatabase::ContainsUnwantedSoftwareUrl }, |
| 507 }; | 506 }; |
| 508 | 507 |
| 509 for (const auto& test_case : kTestCases) { | 508 for (const auto& test_case : kTestCases) { |
| 510 SCOPED_TRACE(std::string("Tested list at fault => ") + | 509 SCOPED_TRACE(std::string("Tested list at fault => ") + |
| 511 test_case.test_list_name); | 510 test_case.test_list_name); |
| 512 | 511 |
| 513 std::vector<SBListChunkRanges> lists; | 512 std::vector<SBListChunkRanges> lists; |
| 514 ScopedVector<SBChunkData> chunks; | 513 ScopedVector<SBChunkData> chunks; |
| 515 | 514 |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 | 750 |
| 752 // Populate with a couple of normal chunks. | 751 // Populate with a couple of normal chunks. |
| 753 chunks.push_back(AddChunkPrefix2Value(1, | 752 chunks.push_back(AddChunkPrefix2Value(1, |
| 754 "www.test.com/test1.html", | 753 "www.test.com/test1.html", |
| 755 "www.test.com/test2.html")); | 754 "www.test.com/test2.html")); |
| 756 chunks.push_back(AddChunkPrefix2Value(10, | 755 chunks.push_back(AddChunkPrefix2Value(10, |
| 757 "www.random.com/random1.html", | 756 "www.random.com/random1.html", |
| 758 "www.random.com/random2.html")); | 757 "www.random.com/random2.html")); |
| 759 | 758 |
| 760 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 759 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 761 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 760 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 762 database_->UpdateFinished(true); | 761 database_->UpdateFinished(true); |
| 763 | 762 |
| 764 // Add an empty ADD and SUB chunk. | 763 // Add an empty ADD and SUB chunk. |
| 765 GetListsInfo(&lists); | 764 GetListsInfo(&lists); |
| 766 ASSERT_LE(1U, lists.size()); | 765 ASSERT_LE(1U, lists.size()); |
| 767 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 766 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 768 EXPECT_EQ("1,10", lists[0].adds); | 767 EXPECT_EQ("1,10", lists[0].adds); |
| 769 EXPECT_TRUE(lists[0].subs.empty()); | 768 EXPECT_TRUE(lists[0].subs.empty()); |
| 770 | 769 |
| 771 chunks.clear(); | 770 chunks.clear(); |
| 772 chunks.push_back(BuildChunk(19, safe_browsing::ChunkData::ADD, | 771 chunks.push_back(BuildChunk(19, safe_browsing::ChunkData::ADD, |
| 773 safe_browsing::ChunkData::PREFIX_4B, | 772 safe_browsing::ChunkData::PREFIX_4B, |
| 774 NULL, 0, std::vector<int>())); | 773 NULL, 0, std::vector<int>())); |
| 775 chunks.push_back(BuildChunk(7, safe_browsing::ChunkData::SUB, | 774 chunks.push_back(BuildChunk(7, safe_browsing::ChunkData::SUB, |
| 776 safe_browsing::ChunkData::PREFIX_4B, | 775 safe_browsing::ChunkData::PREFIX_4B, |
| 777 NULL, 0, std::vector<int>())); | 776 NULL, 0, std::vector<int>())); |
| 778 | 777 |
| 779 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 778 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 780 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 779 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 781 database_->UpdateFinished(true); | 780 database_->UpdateFinished(true); |
| 782 | 781 |
| 783 GetListsInfo(&lists); | 782 GetListsInfo(&lists); |
| 784 ASSERT_LE(1U, lists.size()); | 783 ASSERT_LE(1U, lists.size()); |
| 785 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 784 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 786 EXPECT_EQ("1,10,19", lists[0].adds); | 785 EXPECT_EQ("1,10,19", lists[0].adds); |
| 787 EXPECT_EQ("7", lists[0].subs); | 786 EXPECT_EQ("7", lists[0].subs); |
| 788 | 787 |
| 789 // Add an empty chunk along with a couple that contain data. This should | 788 // Add an empty chunk along with a couple that contain data. This should |
| 790 // result in the chunk range being reduced in size. | 789 // result in the chunk range being reduced in size. |
| 791 chunks.clear(); | 790 chunks.clear(); |
| 792 chunks.push_back(AddChunkPrefixValue(20, "www.notempty.com/full1.html")); | 791 chunks.push_back(AddChunkPrefixValue(20, "www.notempty.com/full1.html")); |
| 793 chunks.push_back(BuildChunk(21, safe_browsing::ChunkData::ADD, | 792 chunks.push_back(BuildChunk(21, safe_browsing::ChunkData::ADD, |
| 794 safe_browsing::ChunkData::PREFIX_4B, | 793 safe_browsing::ChunkData::PREFIX_4B, |
| 795 NULL, 0, std::vector<int>())); | 794 NULL, 0, std::vector<int>())); |
| 796 chunks.push_back(AddChunkPrefixValue(22, "www.notempty.com/full2.html")); | 795 chunks.push_back(AddChunkPrefixValue(22, "www.notempty.com/full2.html")); |
| 797 | 796 |
| 798 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 797 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 799 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 798 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 800 database_->UpdateFinished(true); | 799 database_->UpdateFinished(true); |
| 801 | 800 |
| 802 std::vector<SBPrefix> prefix_hits; | 801 std::vector<SBPrefix> prefix_hits; |
| 803 std::vector<SBFullHashResult> cache_hits; | 802 std::vector<SBFullHashResult> cache_hits; |
| 804 EXPECT_TRUE(database_->ContainsBrowseUrl( | 803 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 805 GURL("http://www.notempty.com/full1.html"), &prefix_hits, &cache_hits)); | 804 GURL("http://www.notempty.com/full1.html"), &prefix_hits, &cache_hits)); |
| 806 EXPECT_TRUE(database_->ContainsBrowseUrl( | 805 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 807 GURL("http://www.notempty.com/full2.html"), &prefix_hits, &cache_hits)); | 806 GURL("http://www.notempty.com/full2.html"), &prefix_hits, &cache_hits)); |
| 808 | 807 |
| 809 GetListsInfo(&lists); | 808 GetListsInfo(&lists); |
| 810 ASSERT_LE(1U, lists.size()); | 809 ASSERT_LE(1U, lists.size()); |
| 811 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 810 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 812 EXPECT_EQ("1,10,19-22", lists[0].adds); | 811 EXPECT_EQ("1,10,19-22", lists[0].adds); |
| 813 EXPECT_EQ("7", lists[0].subs); | 812 EXPECT_EQ("7", lists[0].subs); |
| 814 | 813 |
| 815 // Handle AddDel and SubDel commands for empty chunks. | 814 // Handle AddDel and SubDel commands for empty chunks. |
| 816 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 815 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 817 AddDelChunk(safe_browsing::kMalwareList, 21); | 816 AddDelChunk(safe_browsing_util::kMalwareList, 21); |
| 818 database_->UpdateFinished(true); | 817 database_->UpdateFinished(true); |
| 819 | 818 |
| 820 GetListsInfo(&lists); | 819 GetListsInfo(&lists); |
| 821 ASSERT_LE(1U, lists.size()); | 820 ASSERT_LE(1U, lists.size()); |
| 822 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 821 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 823 EXPECT_EQ("1,10,19-20,22", lists[0].adds); | 822 EXPECT_EQ("1,10,19-20,22", lists[0].adds); |
| 824 EXPECT_EQ("7", lists[0].subs); | 823 EXPECT_EQ("7", lists[0].subs); |
| 825 | 824 |
| 826 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 825 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 827 SubDelChunk(safe_browsing::kMalwareList, 7); | 826 SubDelChunk(safe_browsing_util::kMalwareList, 7); |
| 828 database_->UpdateFinished(true); | 827 database_->UpdateFinished(true); |
| 829 | 828 |
| 830 GetListsInfo(&lists); | 829 GetListsInfo(&lists); |
| 831 ASSERT_LE(1U, lists.size()); | 830 ASSERT_LE(1U, lists.size()); |
| 832 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 831 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 833 EXPECT_EQ("1,10,19-20,22", lists[0].adds); | 832 EXPECT_EQ("1,10,19-20,22", lists[0].adds); |
| 834 EXPECT_TRUE(lists[0].subs.empty()); | 833 EXPECT_TRUE(lists[0].subs.empty()); |
| 835 } | 834 } |
| 836 | 835 |
| 837 // Utility function for setting up the database for the caching test. | 836 // Utility function for setting up the database for the caching test. |
| 838 void SafeBrowsingDatabaseTest::PopulateDatabaseForCacheTest() { | 837 void SafeBrowsingDatabaseTest::PopulateDatabaseForCacheTest() { |
| 839 // Add a couple prefixes. | 838 // Add a couple prefixes. |
| 840 ScopedVector<SBChunkData> chunks; | 839 ScopedVector<SBChunkData> chunks; |
| 841 chunks.push_back(AddChunkPrefix2Value(1, | 840 chunks.push_back(AddChunkPrefix2Value(1, |
| 842 "www.evil.com/phishing.html", | 841 "www.evil.com/phishing.html", |
| 843 "www.evil.com/malware.html")); | 842 "www.evil.com/malware.html")); |
| 844 | 843 |
| 845 std::vector<SBListChunkRanges> lists; | 844 std::vector<SBListChunkRanges> lists; |
| 846 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 845 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 847 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 846 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 848 database_->UpdateFinished(true); | 847 database_->UpdateFinished(true); |
| 849 | 848 |
| 850 // Cache should be cleared after updating. | 849 // Cache should be cleared after updating. |
| 851 EXPECT_TRUE( | 850 EXPECT_TRUE( |
| 852 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); | 851 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
| 853 | 852 |
| 854 SBFullHashResult full_hash; | 853 SBFullHashResult full_hash; |
| 855 full_hash.list_id = safe_browsing::MALWARE; | 854 full_hash.list_id = safe_browsing_util::MALWARE; |
| 856 | 855 |
| 857 std::vector<SBFullHashResult> results; | 856 std::vector<SBFullHashResult> results; |
| 858 std::vector<SBPrefix> prefixes; | 857 std::vector<SBPrefix> prefixes; |
| 859 | 858 |
| 860 // Add a fullhash result for each prefix. | 859 // Add a fullhash result for each prefix. |
| 861 full_hash.hash = | 860 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html"); |
| 862 safe_browsing::SBFullHashForString("www.evil.com/phishing.html"); | |
| 863 results.push_back(full_hash); | 861 results.push_back(full_hash); |
| 864 prefixes.push_back(full_hash.hash.prefix); | 862 prefixes.push_back(full_hash.hash.prefix); |
| 865 | 863 |
| 866 full_hash.hash = | 864 full_hash.hash = SBFullHashForString("www.evil.com/malware.html"); |
| 867 safe_browsing::SBFullHashForString("www.evil.com/malware.html"); | |
| 868 results.push_back(full_hash); | 865 results.push_back(full_hash); |
| 869 prefixes.push_back(full_hash.hash.prefix); | 866 prefixes.push_back(full_hash.hash.prefix); |
| 870 | 867 |
| 871 database_->CacheHashResults(prefixes, results, kCacheLifetime); | 868 database_->CacheHashResults(prefixes, results, kCacheLifetime); |
| 872 } | 869 } |
| 873 | 870 |
| 874 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { | 871 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { |
| 875 PopulateDatabaseForCacheTest(); | 872 PopulateDatabaseForCacheTest(); |
| 876 | 873 |
| 877 // We should have both full hashes in the cache. | 874 // We should have both full hashes in the cache. |
| 878 EXPECT_EQ(2U, | 875 EXPECT_EQ(2U, |
| 879 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->size()); | 876 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->size()); |
| 880 | 877 |
| 881 // Test the cache lookup for the first prefix. | 878 // Test the cache lookup for the first prefix. |
| 882 std::vector<SBPrefix> prefix_hits; | 879 std::vector<SBPrefix> prefix_hits; |
| 883 std::vector<SBFullHashResult> cache_hits; | 880 std::vector<SBFullHashResult> cache_hits; |
| 884 EXPECT_TRUE(database_->ContainsBrowseUrl( | 881 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 885 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 882 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 886 EXPECT_TRUE(prefix_hits.empty()); | 883 EXPECT_TRUE(prefix_hits.empty()); |
| 887 ASSERT_EQ(1U, cache_hits.size()); | 884 ASSERT_EQ(1U, cache_hits.size()); |
| 888 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 885 EXPECT_TRUE(SBFullHashEqual( |
| 889 cache_hits[0].hash, | 886 cache_hits[0].hash, SBFullHashForString("www.evil.com/phishing.html"))); |
| 890 safe_browsing::SBFullHashForString("www.evil.com/phishing.html"))); | |
| 891 | 887 |
| 892 prefix_hits.clear(); | 888 prefix_hits.clear(); |
| 893 cache_hits.clear(); | 889 cache_hits.clear(); |
| 894 | 890 |
| 895 // Test the cache lookup for the second prefix. | 891 // Test the cache lookup for the second prefix. |
| 896 EXPECT_TRUE(database_->ContainsBrowseUrl( | 892 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 897 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 893 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 898 EXPECT_TRUE(prefix_hits.empty()); | 894 EXPECT_TRUE(prefix_hits.empty()); |
| 899 ASSERT_EQ(1U, cache_hits.size()); | 895 ASSERT_EQ(1U, cache_hits.size()); |
| 900 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 896 EXPECT_TRUE(SBFullHashEqual( |
| 901 cache_hits[0].hash, | 897 cache_hits[0].hash, SBFullHashForString("www.evil.com/malware.html"))); |
| 902 safe_browsing::SBFullHashForString("www.evil.com/malware.html"))); | |
| 903 | 898 |
| 904 prefix_hits.clear(); | 899 prefix_hits.clear(); |
| 905 cache_hits.clear(); | 900 cache_hits.clear(); |
| 906 | 901 |
| 907 // Test removing a prefix via a sub chunk. | 902 // Test removing a prefix via a sub chunk. |
| 908 ScopedVector<SBChunkData> chunks; | 903 ScopedVector<SBChunkData> chunks; |
| 909 chunks.push_back(SubChunkPrefixValue(2, "www.evil.com/phishing.html", 1)); | 904 chunks.push_back(SubChunkPrefixValue(2, "www.evil.com/phishing.html", 1)); |
| 910 | 905 |
| 911 std::vector<SBListChunkRanges> lists; | 906 std::vector<SBListChunkRanges> lists; |
| 912 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 907 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 913 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 908 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 914 database_->UpdateFinished(true); | 909 database_->UpdateFinished(true); |
| 915 | 910 |
| 916 // This prefix should still be there, but cached fullhash should be gone. | 911 // This prefix should still be there, but cached fullhash should be gone. |
| 917 EXPECT_TRUE(database_->ContainsBrowseUrl( | 912 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 918 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 913 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 919 ASSERT_EQ(1U, prefix_hits.size()); | 914 ASSERT_EQ(1U, prefix_hits.size()); |
| 920 EXPECT_EQ(SBPrefixForString("www.evil.com/malware.html"), prefix_hits[0]); | 915 EXPECT_EQ(SBPrefixForString("www.evil.com/malware.html"), prefix_hits[0]); |
| 921 EXPECT_TRUE(cache_hits.empty()); | 916 EXPECT_TRUE(cache_hits.empty()); |
| 922 prefix_hits.clear(); | 917 prefix_hits.clear(); |
| 923 cache_hits.clear(); | 918 cache_hits.clear(); |
| 924 | 919 |
| 925 // This prefix should be gone. | 920 // This prefix should be gone. |
| 926 EXPECT_FALSE(database_->ContainsBrowseUrl( | 921 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 927 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 922 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 928 prefix_hits.clear(); | 923 prefix_hits.clear(); |
| 929 cache_hits.clear(); | 924 cache_hits.clear(); |
| 930 | 925 |
| 931 // Test that an AddDel for the original chunk removes the last cached entry. | 926 // Test that an AddDel for the original chunk removes the last cached entry. |
| 932 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 927 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 933 AddDelChunk(safe_browsing::kMalwareList, 1); | 928 AddDelChunk(safe_browsing_util::kMalwareList, 1); |
| 934 database_->UpdateFinished(true); | 929 database_->UpdateFinished(true); |
| 935 EXPECT_FALSE(database_->ContainsBrowseUrl( | 930 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 936 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 931 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 937 EXPECT_TRUE( | 932 EXPECT_TRUE( |
| 938 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); | 933 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
| 939 prefix_hits.clear(); | 934 prefix_hits.clear(); |
| 940 cache_hits.clear(); | 935 cache_hits.clear(); |
| 941 | 936 |
| 942 // Test that the cache won't return expired values. First we have to adjust | 937 // Test that the cache won't return expired values. First we have to adjust |
| 943 // the cached entries' received time to make them older, since the database | 938 // the cached entries' received time to make them older, since the database |
| (...skipping 21 matching lines...) Expand all Loading... |
| 965 // This entry should still exist. | 960 // This entry should still exist. |
| 966 EXPECT_TRUE(database_->ContainsBrowseUrl( | 961 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 967 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 962 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 968 EXPECT_TRUE(prefix_hits.empty()); | 963 EXPECT_TRUE(prefix_hits.empty()); |
| 969 EXPECT_EQ(1U, cache_hits.size()); | 964 EXPECT_EQ(1U, cache_hits.size()); |
| 970 | 965 |
| 971 // Testing prefix miss caching. First, we clear out the existing database, | 966 // Testing prefix miss caching. First, we clear out the existing database, |
| 972 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate | 967 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate |
| 973 // chunks. | 968 // chunks. |
| 974 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 969 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 975 AddDelChunk(safe_browsing::kMalwareList, 1); | 970 AddDelChunk(safe_browsing_util::kMalwareList, 1); |
| 976 database_->UpdateFinished(true); | 971 database_->UpdateFinished(true); |
| 977 | 972 |
| 978 // Cache should be cleared after updating. | 973 // Cache should be cleared after updating. |
| 979 EXPECT_TRUE(hash_cache->empty()); | 974 EXPECT_TRUE(hash_cache->empty()); |
| 980 | 975 |
| 981 std::vector<SBPrefix> prefix_misses; | 976 std::vector<SBPrefix> prefix_misses; |
| 982 std::vector<SBFullHashResult> empty_full_hash; | 977 std::vector<SBFullHashResult> empty_full_hash; |
| 983 prefix_misses.push_back(SBPrefixForString("http://www.bad.com/malware.html")); | 978 prefix_misses.push_back(SBPrefixForString("http://www.bad.com/malware.html")); |
| 984 prefix_misses.push_back( | 979 prefix_misses.push_back( |
| 985 SBPrefixForString("http://www.bad.com/phishing.html")); | 980 SBPrefixForString("http://www.bad.com/phishing.html")); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1014 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 1009 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 1015 prefix_hits.clear(); | 1010 prefix_hits.clear(); |
| 1016 cache_hits.clear(); | 1011 cache_hits.clear(); |
| 1017 | 1012 |
| 1018 // Test receiving a full add chunk. | 1013 // Test receiving a full add chunk. |
| 1019 chunks.clear(); | 1014 chunks.clear(); |
| 1020 chunks.push_back(AddChunkFullHash2Value(20, | 1015 chunks.push_back(AddChunkFullHash2Value(20, |
| 1021 "www.fullevil.com/bad1.html", | 1016 "www.fullevil.com/bad1.html", |
| 1022 "www.fullevil.com/bad2.html")); | 1017 "www.fullevil.com/bad2.html")); |
| 1023 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1018 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1024 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1019 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1025 database_->UpdateFinished(true); | 1020 database_->UpdateFinished(true); |
| 1026 | 1021 |
| 1027 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1022 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1028 GURL("http://www.fullevil.com/bad1.html"), &prefix_hits, &cache_hits)); | 1023 GURL("http://www.fullevil.com/bad1.html"), &prefix_hits, &cache_hits)); |
| 1029 ASSERT_EQ(1U, prefix_hits.size()); | 1024 ASSERT_EQ(1U, prefix_hits.size()); |
| 1030 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad1.html"), prefix_hits[0]); | 1025 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad1.html"), prefix_hits[0]); |
| 1031 EXPECT_TRUE(cache_hits.empty()); | 1026 EXPECT_TRUE(cache_hits.empty()); |
| 1032 prefix_hits.clear(); | 1027 prefix_hits.clear(); |
| 1033 cache_hits.clear(); | 1028 cache_hits.clear(); |
| 1034 | 1029 |
| 1035 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1030 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1036 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); | 1031 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); |
| 1037 ASSERT_EQ(1U, prefix_hits.size()); | 1032 ASSERT_EQ(1U, prefix_hits.size()); |
| 1038 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad2.html"), prefix_hits[0]); | 1033 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad2.html"), prefix_hits[0]); |
| 1039 EXPECT_TRUE(cache_hits.empty()); | 1034 EXPECT_TRUE(cache_hits.empty()); |
| 1040 prefix_hits.clear(); | 1035 prefix_hits.clear(); |
| 1041 cache_hits.clear(); | 1036 cache_hits.clear(); |
| 1042 | 1037 |
| 1043 // Test receiving a full sub chunk, which will remove one of the full adds. | 1038 // Test receiving a full sub chunk, which will remove one of the full adds. |
| 1044 chunks.clear(); | 1039 chunks.clear(); |
| 1045 chunks.push_back(SubChunkFullHashValue(200, | 1040 chunks.push_back(SubChunkFullHashValue(200, |
| 1046 "www.fullevil.com/bad1.html", | 1041 "www.fullevil.com/bad1.html", |
| 1047 20)); | 1042 20)); |
| 1048 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1043 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1049 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1044 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1050 database_->UpdateFinished(true); | 1045 database_->UpdateFinished(true); |
| 1051 | 1046 |
| 1052 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1047 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1053 GURL("http://www.fullevil.com/bad1.html"), &prefix_hits, &cache_hits)); | 1048 GURL("http://www.fullevil.com/bad1.html"), &prefix_hits, &cache_hits)); |
| 1054 | 1049 |
| 1055 // There should be one remaining full add. | 1050 // There should be one remaining full add. |
| 1056 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1051 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1057 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); | 1052 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); |
| 1058 ASSERT_EQ(1U, prefix_hits.size()); | 1053 ASSERT_EQ(1U, prefix_hits.size()); |
| 1059 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad2.html"), prefix_hits[0]); | 1054 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad2.html"), prefix_hits[0]); |
| 1060 EXPECT_TRUE(cache_hits.empty()); | 1055 EXPECT_TRUE(cache_hits.empty()); |
| 1061 prefix_hits.clear(); | 1056 prefix_hits.clear(); |
| 1062 cache_hits.clear(); | 1057 cache_hits.clear(); |
| 1063 | 1058 |
| 1064 // Now test an AddDel for the remaining full add. | 1059 // Now test an AddDel for the remaining full add. |
| 1065 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1060 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1066 AddDelChunk(safe_browsing::kMalwareList, 20); | 1061 AddDelChunk(safe_browsing_util::kMalwareList, 20); |
| 1067 database_->UpdateFinished(true); | 1062 database_->UpdateFinished(true); |
| 1068 | 1063 |
| 1069 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1064 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1070 GURL("http://www.fullevil.com/bad1.html"), &prefix_hits, &cache_hits)); | 1065 GURL("http://www.fullevil.com/bad1.html"), &prefix_hits, &cache_hits)); |
| 1071 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1066 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1072 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); | 1067 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); |
| 1073 | 1068 |
| 1074 // Add a fullhash which has a prefix collision for a known url. | 1069 // Add a fullhash which has a prefix collision for a known url. |
| 1075 static const char kExampleFine[] = "www.example.com/fine.html"; | 1070 static const char kExampleFine[] = "www.example.com/fine.html"; |
| 1076 static const char kExampleCollision[] = | 1071 static const char kExampleCollision[] = |
| 1077 "www.example.com/3123364814/malware.htm"; | 1072 "www.example.com/3123364814/malware.htm"; |
| 1078 ASSERT_EQ(SBPrefixForString(kExampleFine), | 1073 ASSERT_EQ(SBPrefixForString(kExampleFine), |
| 1079 SBPrefixForString(kExampleCollision)); | 1074 SBPrefixForString(kExampleCollision)); |
| 1080 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1075 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1081 { | 1076 { |
| 1082 ScopedVector<SBChunkData> chunks; | 1077 ScopedVector<SBChunkData> chunks; |
| 1083 chunks.push_back(AddChunkPrefixValue(21, kExampleCollision)); | 1078 chunks.push_back(AddChunkPrefixValue(21, kExampleCollision)); |
| 1084 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1079 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1085 } | 1080 } |
| 1086 database_->UpdateFinished(true); | 1081 database_->UpdateFinished(true); |
| 1087 | 1082 |
| 1088 // Expect a prefix hit due to the collision between |kExampleFine| and | 1083 // Expect a prefix hit due to the collision between |kExampleFine| and |
| 1089 // |kExampleCollision|. | 1084 // |kExampleCollision|. |
| 1090 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1085 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1091 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); | 1086 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); |
| 1092 ASSERT_EQ(1U, prefix_hits.size()); | 1087 ASSERT_EQ(1U, prefix_hits.size()); |
| 1093 EXPECT_EQ(SBPrefixForString(kExampleFine), prefix_hits[0]); | 1088 EXPECT_EQ(SBPrefixForString(kExampleFine), prefix_hits[0]); |
| 1094 EXPECT_TRUE(cache_hits.empty()); | 1089 EXPECT_TRUE(cache_hits.empty()); |
| 1095 | 1090 |
| 1096 // Cache gethash response for |kExampleCollision|. | 1091 // Cache gethash response for |kExampleCollision|. |
| 1097 { | 1092 { |
| 1098 SBFullHashResult result; | 1093 SBFullHashResult result; |
| 1099 result.hash = safe_browsing::SBFullHashForString(kExampleCollision); | 1094 result.hash = SBFullHashForString(kExampleCollision); |
| 1100 result.list_id = safe_browsing::MALWARE; | 1095 result.list_id = safe_browsing_util::MALWARE; |
| 1101 database_->CacheHashResults(std::vector<SBPrefix>(1, result.hash.prefix), | 1096 database_->CacheHashResults(std::vector<SBPrefix>(1, result.hash.prefix), |
| 1102 std::vector<SBFullHashResult>(1, result), | 1097 std::vector<SBFullHashResult>(1, result), |
| 1103 kCacheLifetime); | 1098 kCacheLifetime); |
| 1104 } | 1099 } |
| 1105 | 1100 |
| 1106 // The cached response means the collision no longer causes a hit. | 1101 // The cached response means the collision no longer causes a hit. |
| 1107 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1102 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1108 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); | 1103 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); |
| 1109 } | 1104 } |
| 1110 | 1105 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1143 fputc('!', fp); | 1138 fputc('!', fp); |
| 1144 } | 1139 } |
| 1145 fclose(fp); | 1140 fclose(fp); |
| 1146 | 1141 |
| 1147 { | 1142 { |
| 1148 // The following code will cause DCHECKs, so suppress the crashes. | 1143 // The following code will cause DCHECKs, so suppress the crashes. |
| 1149 ScopedLogMessageIgnorer ignorer; | 1144 ScopedLogMessageIgnorer ignorer; |
| 1150 | 1145 |
| 1151 // Start an update. The insert will fail due to corruption. | 1146 // Start an update. The insert will fail due to corruption. |
| 1152 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1147 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1153 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1148 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1154 database_->UpdateFinished(true); | 1149 database_->UpdateFinished(true); |
| 1155 | 1150 |
| 1156 // Database file still exists until the corruption handler has run. | 1151 // Database file still exists until the corruption handler has run. |
| 1157 EXPECT_TRUE(base::PathExists(database_filename_)); | 1152 EXPECT_TRUE(base::PathExists(database_filename_)); |
| 1158 | 1153 |
| 1159 // Flush through the corruption-handler task. | 1154 // Flush through the corruption-handler task. |
| 1160 DVLOG(1) << "Expect failed check on: SafeBrowsing database reset"; | 1155 DVLOG(1) << "Expect failed check on: SafeBrowsing database reset"; |
| 1161 base::MessageLoop::current()->RunUntilIdle(); | 1156 base::MessageLoop::current()->RunUntilIdle(); |
| 1162 } | 1157 } |
| 1163 | 1158 |
| 1164 // Database file should not exist. | 1159 // Database file should not exist. |
| 1165 EXPECT_FALSE(base::PathExists(database_filename_)); | 1160 EXPECT_FALSE(base::PathExists(database_filename_)); |
| 1166 | 1161 |
| 1167 // Run the update again successfully. | 1162 // Run the update again successfully. |
| 1168 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1163 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1169 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1164 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1170 database_->UpdateFinished(true); | 1165 database_->UpdateFinished(true); |
| 1171 EXPECT_TRUE(base::PathExists(database_filename_)); | 1166 EXPECT_TRUE(base::PathExists(database_filename_)); |
| 1172 | 1167 |
| 1173 database_.reset(); | 1168 database_.reset(); |
| 1174 } | 1169 } |
| 1175 | 1170 |
| 1176 // Checks database reading and writing. | 1171 // Checks database reading and writing. |
| 1177 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrlPrefixes) { | 1172 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrlPrefixes) { |
| 1178 const char kEvil1Url1[] = "www.evil1.com/download1/"; | 1173 const char kEvil1Url1[] = "www.evil1.com/download1/"; |
| 1179 const char kEvil1Url2[] = "www.evil1.com/download2.html"; | 1174 const char kEvil1Url2[] = "www.evil1.com/download2.html"; |
| 1180 | 1175 |
| 1181 // Add a simple chunk with one hostkey for download url list. | 1176 // Add a simple chunk with one hostkey for download url list. |
| 1182 ScopedVector<SBChunkData> chunks; | 1177 ScopedVector<SBChunkData> chunks; |
| 1183 chunks.push_back(AddChunkPrefix2Value(1, kEvil1Url1, kEvil1Url2)); | 1178 chunks.push_back(AddChunkPrefix2Value(1, kEvil1Url1, kEvil1Url2)); |
| 1184 | 1179 |
| 1185 std::vector<SBListChunkRanges> lists; | 1180 std::vector<SBListChunkRanges> lists; |
| 1186 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1181 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1187 database_->InsertChunks(safe_browsing::kBinUrlList, chunks.get()); | 1182 database_->InsertChunks(safe_browsing_util::kBinUrlList, chunks.get()); |
| 1188 database_->UpdateFinished(true); | 1183 database_->UpdateFinished(true); |
| 1189 | 1184 |
| 1190 std::vector<SBPrefix> prefix_hits; | 1185 std::vector<SBPrefix> prefix_hits; |
| 1191 std::vector<GURL> urls(1); | 1186 std::vector<GURL> urls(1); |
| 1192 | 1187 |
| 1193 urls[0] = GURL(std::string("http://") + kEvil1Url1); | 1188 urls[0] = GURL(std::string("http://") + kEvil1Url1); |
| 1194 EXPECT_TRUE(ContainsDownloadUrl(urls, &prefix_hits)); | 1189 EXPECT_TRUE(ContainsDownloadUrl(urls, &prefix_hits)); |
| 1195 ASSERT_EQ(1U, prefix_hits.size()); | 1190 ASSERT_EQ(1U, prefix_hits.size()); |
| 1196 EXPECT_EQ(SBPrefixForString(kEvil1Url1), prefix_hits[0]); | 1191 EXPECT_EQ(SBPrefixForString(kEvil1Url1), prefix_hits[0]); |
| 1197 | 1192 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1274 bool TestStrings() const { | 1269 bool TestStrings() const { |
| 1275 return test_list_contains_whitelisted_string != nullptr; | 1270 return test_list_contains_whitelisted_string != nullptr; |
| 1276 } | 1271 } |
| 1277 | 1272 |
| 1278 const char* test_list_name; | 1273 const char* test_list_name; |
| 1279 TestListContainsWhitelistedUrl test_list_contains_whitelisted_url; | 1274 TestListContainsWhitelistedUrl test_list_contains_whitelisted_url; |
| 1280 // Optional test case field, if set the tested whitelist will also be tested | 1275 // Optional test case field, if set the tested whitelist will also be tested |
| 1281 // for strings. | 1276 // for strings. |
| 1282 TestListContainsWhitelistedString test_list_contains_whitelisted_string; | 1277 TestListContainsWhitelistedString test_list_contains_whitelisted_string; |
| 1283 } const kTestCases[]{ | 1278 } const kTestCases[]{ |
| 1284 {safe_browsing::kCsdWhiteList, | 1279 {safe_browsing_util::kCsdWhiteList, |
| 1285 &SafeBrowsingDatabase::ContainsCsdWhitelistedUrl, | 1280 &SafeBrowsingDatabase::ContainsCsdWhitelistedUrl, |
| 1286 nullptr}, | 1281 nullptr}, |
| 1287 {safe_browsing::kDownloadWhiteList, | 1282 {safe_browsing_util::kDownloadWhiteList, |
| 1288 &SafeBrowsingDatabase::ContainsDownloadWhitelistedUrl, | 1283 &SafeBrowsingDatabase::ContainsDownloadWhitelistedUrl, |
| 1289 &SafeBrowsingDatabase::ContainsDownloadWhitelistedString}, | 1284 &SafeBrowsingDatabase::ContainsDownloadWhitelistedString}, |
| 1290 {safe_browsing::kInclusionWhitelist, | 1285 {safe_browsing_util::kInclusionWhitelist, |
| 1291 &SafeBrowsingDatabase::ContainsInclusionWhitelistedUrl, | 1286 &SafeBrowsingDatabase::ContainsInclusionWhitelistedUrl, |
| 1292 nullptr}, | 1287 nullptr}, |
| 1293 }; | 1288 }; |
| 1294 | 1289 |
| 1295 // If the whitelist is disabled everything should match the whitelist. | 1290 // If the whitelist is disabled everything should match the whitelist. |
| 1296 database_.reset(new SafeBrowsingDatabaseNew( | 1291 database_.reset(new SafeBrowsingDatabaseNew( |
| 1297 task_runner_, new SafeBrowsingStoreFile(task_runner_), NULL, NULL, NULL, | 1292 task_runner_, new SafeBrowsingStoreFile(task_runner_), NULL, NULL, NULL, |
| 1298 NULL, NULL, NULL, NULL)); | 1293 NULL, NULL, NULL, NULL)); |
| 1299 database_->Init(database_filename_); | 1294 database_->Init(database_filename_); |
| 1300 for (const auto& test_case : kTestCases) { | 1295 for (const auto& test_case : kTestCases) { |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1415 EXPECT_TRUE( | 1410 EXPECT_TRUE( |
| 1416 (database_.get()->*test_case.test_list_contains_whitelisted_string)( | 1411 (database_.get()->*test_case.test_list_contains_whitelisted_string)( |
| 1417 kGoodString)); | 1412 kGoodString)); |
| 1418 } | 1413 } |
| 1419 | 1414 |
| 1420 EXPECT_FALSE( | 1415 EXPECT_FALSE( |
| 1421 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1416 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1422 GURL(std::string("http://www.google.com/")))); | 1417 GURL(std::string("http://www.google.com/")))); |
| 1423 | 1418 |
| 1424 // The malware kill switch is for the CSD whitelist only. | 1419 // The malware kill switch is for the CSD whitelist only. |
| 1425 if (test_case.test_list_name == safe_browsing::kCsdWhiteList) { | 1420 if (test_case.test_list_name == safe_browsing_util::kCsdWhiteList) { |
| 1426 // The CSD whitelist killswitch is not present. | 1421 // The CSD whitelist killswitch is not present. |
| 1427 EXPECT_FALSE(database_->IsCsdWhitelistKillSwitchOn()); | 1422 EXPECT_FALSE(database_->IsCsdWhitelistKillSwitchOn()); |
| 1428 | 1423 |
| 1429 // Test only add the malware IP killswitch | 1424 // Test only add the malware IP killswitch |
| 1430 chunks.clear(); | 1425 chunks.clear(); |
| 1431 chunks.push_back(AddChunkFullHashValue( | 1426 chunks.push_back(AddChunkFullHashValue( |
| 1432 15, "sb-ssl.google.com/safebrowsing/csd/killswitch_malware")); | 1427 15, "sb-ssl.google.com/safebrowsing/csd/killswitch_malware")); |
| 1433 | 1428 |
| 1434 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1429 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1435 database_->InsertChunks(safe_browsing::kCsdWhiteList, chunks.get()); | 1430 database_->InsertChunks(safe_browsing_util::kCsdWhiteList, chunks.get()); |
| 1436 database_->UpdateFinished(true); | 1431 database_->UpdateFinished(true); |
| 1437 | 1432 |
| 1438 EXPECT_TRUE(database_->IsMalwareIPMatchKillSwitchOn()); | 1433 EXPECT_TRUE(database_->IsMalwareIPMatchKillSwitchOn()); |
| 1439 // The CSD whitelist killswitch is not present. | 1434 // The CSD whitelist killswitch is not present. |
| 1440 EXPECT_FALSE(database_->IsCsdWhitelistKillSwitchOn()); | 1435 EXPECT_FALSE(database_->IsCsdWhitelistKillSwitchOn()); |
| 1441 } | 1436 } |
| 1442 | 1437 |
| 1443 // Test that the generic whitelist kill-switch works as intended. | 1438 // Test that the generic whitelist kill-switch works as intended. |
| 1444 chunks.clear(); | 1439 chunks.clear(); |
| 1445 lists.clear(); | 1440 lists.clear(); |
| 1446 chunks.push_back(AddChunkFullHashValue( | 1441 chunks.push_back(AddChunkFullHashValue( |
| 1447 5, "sb-ssl.google.com/safebrowsing/csd/killswitch")); | 1442 5, "sb-ssl.google.com/safebrowsing/csd/killswitch")); |
| 1448 | 1443 |
| 1449 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1444 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1450 database_->InsertChunks(test_case.test_list_name, chunks.get()); | 1445 database_->InsertChunks(test_case.test_list_name, chunks.get()); |
| 1451 database_->UpdateFinished(true); | 1446 database_->UpdateFinished(true); |
| 1452 | 1447 |
| 1453 // Test CSD whitelist specific methods. | 1448 // Test CSD whitelist specific methods. |
| 1454 if (test_case.test_list_name == safe_browsing::kCsdWhiteList) { | 1449 if (test_case.test_list_name == safe_browsing_util::kCsdWhiteList) { |
| 1455 // The CSD whitelist killswitch is present. | 1450 // The CSD whitelist killswitch is present. |
| 1456 EXPECT_TRUE(database_->IsCsdWhitelistKillSwitchOn()); | 1451 EXPECT_TRUE(database_->IsCsdWhitelistKillSwitchOn()); |
| 1457 EXPECT_TRUE(database_->IsMalwareIPMatchKillSwitchOn()); | 1452 EXPECT_TRUE(database_->IsMalwareIPMatchKillSwitchOn()); |
| 1458 } | 1453 } |
| 1459 | 1454 |
| 1460 EXPECT_TRUE( | 1455 EXPECT_TRUE( |
| 1461 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1456 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1462 GURL(std::string("https://") + kGood1Url2 + "/c.html"))); | 1457 GURL(std::string("https://") + kGood1Url2 + "/c.html"))); |
| 1463 EXPECT_TRUE( | 1458 EXPECT_TRUE( |
| 1464 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1459 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1465 GURL(std::string("http://www.google.com/")))); | 1460 GURL(std::string("http://www.google.com/")))); |
| 1466 EXPECT_TRUE( | 1461 EXPECT_TRUE( |
| 1467 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1462 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1468 GURL(std::string("http://www.phishing_url.com/")))); | 1463 GURL(std::string("http://www.phishing_url.com/")))); |
| 1469 | 1464 |
| 1470 if (test_case.TestStrings()) { | 1465 if (test_case.TestStrings()) { |
| 1471 EXPECT_TRUE( | 1466 EXPECT_TRUE( |
| 1472 (database_.get()->*test_case.test_list_contains_whitelisted_string)( | 1467 (database_.get()->*test_case.test_list_contains_whitelisted_string)( |
| 1473 "asdf")); | 1468 "asdf")); |
| 1474 EXPECT_TRUE( | 1469 EXPECT_TRUE( |
| 1475 (database_.get()->*test_case.test_list_contains_whitelisted_string)( | 1470 (database_.get()->*test_case.test_list_contains_whitelisted_string)( |
| 1476 kGoodString)); | 1471 kGoodString)); |
| 1477 } | 1472 } |
| 1478 | 1473 |
| 1479 // Remove the kill-switch and verify that we can recover. | 1474 // Remove the kill-switch and verify that we can recover. |
| 1480 chunks.clear(); | 1475 chunks.clear(); |
| 1481 lists.clear(); | 1476 lists.clear(); |
| 1482 chunks.push_back(SubChunkFullHashValue( | 1477 chunks.push_back(SubChunkFullHashValue( |
| 1483 1, "sb-ssl.google.com/safebrowsing/csd/killswitch", 5)); | 1478 1, "sb-ssl.google.com/safebrowsing/csd/killswitch", 5)); |
| 1484 if (test_case.test_list_name == safe_browsing::kCsdWhiteList) { | 1479 if (test_case.test_list_name == safe_browsing_util::kCsdWhiteList) { |
| 1485 chunks.push_back(SubChunkFullHashValue( | 1480 chunks.push_back(SubChunkFullHashValue( |
| 1486 10, "sb-ssl.google.com/safebrowsing/csd/killswitch_malware", 15)); | 1481 10, "sb-ssl.google.com/safebrowsing/csd/killswitch_malware", 15)); |
| 1487 } | 1482 } |
| 1488 | 1483 |
| 1489 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1484 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1490 database_->InsertChunks(test_case.test_list_name, chunks.get()); | 1485 database_->InsertChunks(test_case.test_list_name, chunks.get()); |
| 1491 database_->UpdateFinished(true); | 1486 database_->UpdateFinished(true); |
| 1492 | 1487 |
| 1493 if (test_case.test_list_name == safe_browsing::kCsdWhiteList) { | 1488 if (test_case.test_list_name == safe_browsing_util::kCsdWhiteList) { |
| 1494 EXPECT_FALSE(database_->IsMalwareIPMatchKillSwitchOn()); | 1489 EXPECT_FALSE(database_->IsMalwareIPMatchKillSwitchOn()); |
| 1495 EXPECT_FALSE(database_->IsCsdWhitelistKillSwitchOn()); | 1490 EXPECT_FALSE(database_->IsCsdWhitelistKillSwitchOn()); |
| 1496 } | 1491 } |
| 1497 EXPECT_TRUE( | 1492 EXPECT_TRUE( |
| 1498 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1493 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1499 GURL(std::string("https://") + kGood1Url2 + "/c.html"))); | 1494 GURL(std::string("https://") + kGood1Url2 + "/c.html"))); |
| 1500 EXPECT_TRUE( | 1495 EXPECT_TRUE( |
| 1501 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1496 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1502 GURL(std::string("https://") + kGood2Url1 + "/c/bla"))); | 1497 GURL(std::string("https://") + kGood2Url1 + "/c/bla"))); |
| 1503 EXPECT_TRUE( | 1498 EXPECT_TRUE( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1526 ScopedVector<SBChunkData> chunks; | 1521 ScopedVector<SBChunkData> chunks; |
| 1527 | 1522 |
| 1528 // Add a malware add chunk with two entries of the same host. | 1523 // Add a malware add chunk with two entries of the same host. |
| 1529 chunks.push_back(AddChunkPrefix2Value(1, | 1524 chunks.push_back(AddChunkPrefix2Value(1, |
| 1530 "www.evil.com/malware1.html", | 1525 "www.evil.com/malware1.html", |
| 1531 "www.evil.com/malware2.html")); | 1526 "www.evil.com/malware2.html")); |
| 1532 | 1527 |
| 1533 // Insert the testing chunks into database. | 1528 // Insert the testing chunks into database. |
| 1534 std::vector<SBListChunkRanges> lists; | 1529 std::vector<SBListChunkRanges> lists; |
| 1535 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1530 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1536 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1531 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1537 database_->UpdateFinished(true); | 1532 database_->UpdateFinished(true); |
| 1538 | 1533 |
| 1539 GetListsInfo(&lists); | 1534 GetListsInfo(&lists); |
| 1540 ASSERT_LE(1U, lists.size()); | 1535 ASSERT_LE(1U, lists.size()); |
| 1541 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 1536 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 1542 EXPECT_EQ("1", lists[0].adds); | 1537 EXPECT_EQ("1", lists[0].adds); |
| 1543 EXPECT_TRUE(lists[0].subs.empty()); | 1538 EXPECT_TRUE(lists[0].subs.empty()); |
| 1544 | 1539 |
| 1545 // Add a phishing add chunk with two entries of the same host. | 1540 // Add a phishing add chunk with two entries of the same host. |
| 1546 chunks.clear(); | 1541 chunks.clear(); |
| 1547 chunks.push_back(AddChunkPrefix2Value(47, | 1542 chunks.push_back(AddChunkPrefix2Value(47, |
| 1548 "www.evil.com/phishing1.html", | 1543 "www.evil.com/phishing1.html", |
| 1549 "www.evil.com/phishing2.html")); | 1544 "www.evil.com/phishing2.html")); |
| 1550 | 1545 |
| 1551 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1546 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1552 database_->InsertChunks(safe_browsing::kPhishingList, chunks.get()); | 1547 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks.get()); |
| 1553 database_->UpdateFinished(true); | 1548 database_->UpdateFinished(true); |
| 1554 | 1549 |
| 1555 GetListsInfo(&lists); | 1550 GetListsInfo(&lists); |
| 1556 ASSERT_LE(2U, lists.size()); | 1551 ASSERT_LE(2U, lists.size()); |
| 1557 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 1552 EXPECT_EQ(safe_browsing_util::kMalwareList, lists[0].name); |
| 1558 EXPECT_EQ("1", lists[0].adds); | 1553 EXPECT_EQ("1", lists[0].adds); |
| 1559 EXPECT_TRUE(lists[0].subs.empty()); | 1554 EXPECT_TRUE(lists[0].subs.empty()); |
| 1560 EXPECT_EQ(safe_browsing::kPhishingList, lists[1].name); | 1555 EXPECT_EQ(safe_browsing_util::kPhishingList, lists[1].name); |
| 1561 EXPECT_EQ("47", lists[1].adds); | 1556 EXPECT_EQ("47", lists[1].adds); |
| 1562 EXPECT_TRUE(lists[1].subs.empty()); | 1557 EXPECT_TRUE(lists[1].subs.empty()); |
| 1563 | 1558 |
| 1564 std::vector<SBPrefix> prefix_hits; | 1559 std::vector<SBPrefix> prefix_hits; |
| 1565 std::vector<SBFullHashResult> cache_hits; | 1560 std::vector<SBFullHashResult> cache_hits; |
| 1566 | 1561 |
| 1567 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1562 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1568 GURL("http://www.evil.com/malware1.html"), &prefix_hits, &cache_hits)); | 1563 GURL("http://www.evil.com/malware1.html"), &prefix_hits, &cache_hits)); |
| 1569 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1564 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1570 GURL("http://www.evil.com/malware2.html"), &prefix_hits, &cache_hits)); | 1565 GURL("http://www.evil.com/malware2.html"), &prefix_hits, &cache_hits)); |
| 1571 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1566 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1572 GURL("http://www.evil.com/phishing1.html"), &prefix_hits, &cache_hits)); | 1567 GURL("http://www.evil.com/phishing1.html"), &prefix_hits, &cache_hits)); |
| 1573 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1568 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1574 GURL("http://www.evil.com/phishing2.html"), &prefix_hits, &cache_hits)); | 1569 GURL("http://www.evil.com/phishing2.html"), &prefix_hits, &cache_hits)); |
| 1575 | 1570 |
| 1576 // Test removing a single prefix from the add chunk. | 1571 // Test removing a single prefix from the add chunk. |
| 1577 // Remove the prefix that added first. | 1572 // Remove the prefix that added first. |
| 1578 chunks.clear(); | 1573 chunks.clear(); |
| 1579 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/malware1.html", 1)); | 1574 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/malware1.html", 1)); |
| 1580 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1575 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1581 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1576 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1582 database_->UpdateFinished(true); | 1577 database_->UpdateFinished(true); |
| 1583 | 1578 |
| 1584 // Remove the prefix that added last. | 1579 // Remove the prefix that added last. |
| 1585 chunks.clear(); | 1580 chunks.clear(); |
| 1586 chunks.push_back(SubChunkPrefixValue(5, "www.evil.com/phishing2.html", 47)); | 1581 chunks.push_back(SubChunkPrefixValue(5, "www.evil.com/phishing2.html", 47)); |
| 1587 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1582 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1588 database_->InsertChunks(safe_browsing::kPhishingList, chunks.get()); | 1583 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks.get()); |
| 1589 database_->UpdateFinished(true); | 1584 database_->UpdateFinished(true); |
| 1590 | 1585 |
| 1591 // Verify that the database contains urls expected. | 1586 // Verify that the database contains urls expected. |
| 1592 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1587 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1593 GURL("http://www.evil.com/malware1.html"), &prefix_hits, &cache_hits)); | 1588 GURL("http://www.evil.com/malware1.html"), &prefix_hits, &cache_hits)); |
| 1594 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1589 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1595 GURL("http://www.evil.com/malware2.html"), &prefix_hits, &cache_hits)); | 1590 GURL("http://www.evil.com/malware2.html"), &prefix_hits, &cache_hits)); |
| 1596 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1591 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1597 GURL("http://www.evil.com/phishing1.html"), &prefix_hits, &cache_hits)); | 1592 GURL("http://www.evil.com/phishing1.html"), &prefix_hits, &cache_hits)); |
| 1598 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1593 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1599 GURL("http://www.evil.com/phishing2.html"), &prefix_hits, &cache_hits)); | 1594 GURL("http://www.evil.com/phishing2.html"), &prefix_hits, &cache_hits)); |
| 1600 } | 1595 } |
| 1601 | 1596 |
| 1602 // Test that an empty update doesn't actually update the database. | 1597 // Test that an empty update doesn't actually update the database. |
| 1603 // This isn't a functionality requirement, but it is a useful | 1598 // This isn't a functionality requirement, but it is a useful |
| 1604 // optimization. | 1599 // optimization. |
| 1605 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { | 1600 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { |
| 1606 ScopedVector<SBChunkData> chunks; | 1601 ScopedVector<SBChunkData> chunks; |
| 1607 | 1602 |
| 1608 base::FilePath filename = database_->BrowseDBFilename(database_filename_); | 1603 base::FilePath filename = database_->BrowseDBFilename(database_filename_); |
| 1609 | 1604 |
| 1610 // Prime the database. | 1605 // Prime the database. |
| 1611 std::vector<SBListChunkRanges> lists; | 1606 std::vector<SBListChunkRanges> lists; |
| 1612 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1607 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1613 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); | 1608 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); |
| 1614 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1609 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1615 database_->UpdateFinished(true); | 1610 database_->UpdateFinished(true); |
| 1616 | 1611 |
| 1617 // Get an older time to reset the lastmod time for detecting whether | 1612 // Get an older time to reset the lastmod time for detecting whether |
| 1618 // the file has been updated. | 1613 // the file has been updated. |
| 1619 base::File::Info before_info, after_info; | 1614 base::File::Info before_info, after_info; |
| 1620 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); | 1615 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); |
| 1621 const Time old_last_modified = | 1616 const Time old_last_modified = |
| 1622 before_info.last_modified - TimeDelta::FromSeconds(10); | 1617 before_info.last_modified - TimeDelta::FromSeconds(10); |
| 1623 | 1618 |
| 1624 // Inserting another chunk updates the database file. The sleep is | 1619 // Inserting another chunk updates the database file. The sleep is |
| 1625 // needed because otherwise the entire test can finish w/in the | 1620 // needed because otherwise the entire test can finish w/in the |
| 1626 // resolution of the lastmod time. | 1621 // resolution of the lastmod time. |
| 1627 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); | 1622 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); |
| 1628 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); | 1623 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); |
| 1629 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1624 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1630 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); | 1625 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); |
| 1631 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1626 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1632 database_->UpdateFinished(true); | 1627 database_->UpdateFinished(true); |
| 1633 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); | 1628 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); |
| 1634 EXPECT_LT(before_info.last_modified, after_info.last_modified); | 1629 EXPECT_LT(before_info.last_modified, after_info.last_modified); |
| 1635 | 1630 |
| 1636 // Deleting a chunk updates the database file. | 1631 // Deleting a chunk updates the database file. |
| 1637 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); | 1632 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); |
| 1638 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); | 1633 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); |
| 1639 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1634 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1640 AddDelChunk(safe_browsing::kMalwareList, 2); | 1635 AddDelChunk(safe_browsing_util::kMalwareList, 2); |
| 1641 database_->UpdateFinished(true); | 1636 database_->UpdateFinished(true); |
| 1642 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); | 1637 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); |
| 1643 EXPECT_LT(before_info.last_modified, after_info.last_modified); | 1638 EXPECT_LT(before_info.last_modified, after_info.last_modified); |
| 1644 | 1639 |
| 1645 // Simply calling |UpdateStarted()| then |UpdateFinished()| does not | 1640 // Simply calling |UpdateStarted()| then |UpdateFinished()| does not |
| 1646 // update the database file. | 1641 // update the database file. |
| 1647 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); | 1642 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); |
| 1648 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); | 1643 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); |
| 1649 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1644 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1650 database_->UpdateFinished(true); | 1645 database_->UpdateFinished(true); |
| 1651 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); | 1646 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); |
| 1652 EXPECT_EQ(before_info.last_modified, after_info.last_modified); | 1647 EXPECT_EQ(before_info.last_modified, after_info.last_modified); |
| 1653 } | 1648 } |
| 1654 | 1649 |
| 1655 // Test that a filter file is written out during update and read back | 1650 // Test that a filter file is written out during update and read back |
| 1656 // in during setup. | 1651 // in during setup. |
| 1657 TEST_F(SafeBrowsingDatabaseTest, FilterFile) { | 1652 TEST_F(SafeBrowsingDatabaseTest, FilterFile) { |
| 1658 // Create a database with trivial example data and write it out. | 1653 // Create a database with trivial example data and write it out. |
| 1659 { | 1654 { |
| 1660 // Prime the database. | 1655 // Prime the database. |
| 1661 std::vector<SBListChunkRanges> lists; | 1656 std::vector<SBListChunkRanges> lists; |
| 1662 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1657 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1663 | 1658 |
| 1664 ScopedVector<SBChunkData> chunks; | 1659 ScopedVector<SBChunkData> chunks; |
| 1665 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); | 1660 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); |
| 1666 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1661 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1667 database_->UpdateFinished(true); | 1662 database_->UpdateFinished(true); |
| 1668 } | 1663 } |
| 1669 | 1664 |
| 1670 // Find the malware url in the database, don't find a good url. | 1665 // Find the malware url in the database, don't find a good url. |
| 1671 std::vector<SBPrefix> prefix_hits; | 1666 std::vector<SBPrefix> prefix_hits; |
| 1672 std::vector<SBFullHashResult> cache_hits; | 1667 std::vector<SBFullHashResult> cache_hits; |
| 1673 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1668 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1674 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 1669 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 1675 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1670 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1676 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); | 1671 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1706 const SBFullHash kFullHash2_1 = | 1701 const SBFullHash kFullHash2_1 = |
| 1707 SBFullHashForPrefixAndSuffix(kPrefix2, "\x01"); | 1702 SBFullHashForPrefixAndSuffix(kPrefix2, "\x01"); |
| 1708 | 1703 |
| 1709 // Insert prefix kPrefix1 and kPrefix2 into database. | 1704 // Insert prefix kPrefix1 and kPrefix2 into database. |
| 1710 ScopedVector<SBChunkData> chunks; | 1705 ScopedVector<SBChunkData> chunks; |
| 1711 chunks.push_back(AddChunkPrefix(1, kPrefix1)); | 1706 chunks.push_back(AddChunkPrefix(1, kPrefix1)); |
| 1712 chunks.push_back(AddChunkPrefix(2, kPrefix2)); | 1707 chunks.push_back(AddChunkPrefix(2, kPrefix2)); |
| 1713 | 1708 |
| 1714 std::vector<SBListChunkRanges> lists; | 1709 std::vector<SBListChunkRanges> lists; |
| 1715 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1710 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1716 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1711 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1717 database_->UpdateFinished(true); | 1712 database_->UpdateFinished(true); |
| 1718 | 1713 |
| 1719 { | 1714 { |
| 1720 // Cache a full miss result for kPrefix1. | 1715 // Cache a full miss result for kPrefix1. |
| 1721 std::vector<SBPrefix> prefixes(1, kPrefix1); | 1716 std::vector<SBPrefix> prefixes(1, kPrefix1); |
| 1722 std::vector<SBFullHashResult> cache_results; | 1717 std::vector<SBFullHashResult> cache_results; |
| 1723 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); | 1718 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); |
| 1724 } | 1719 } |
| 1725 | 1720 |
| 1726 { | 1721 { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1761 const SBFullHash kFullHash3_1 = | 1756 const SBFullHash kFullHash3_1 = |
| 1762 SBFullHashForPrefixAndSuffix(kPrefix3, "\x01"); | 1757 SBFullHashForPrefixAndSuffix(kPrefix3, "\x01"); |
| 1763 | 1758 |
| 1764 // Insert prefix kPrefix1 and kPrefix2 into database. | 1759 // Insert prefix kPrefix1 and kPrefix2 into database. |
| 1765 ScopedVector<SBChunkData> chunks; | 1760 ScopedVector<SBChunkData> chunks; |
| 1766 chunks.push_back(AddChunkPrefix(1, kPrefix1)); | 1761 chunks.push_back(AddChunkPrefix(1, kPrefix1)); |
| 1767 chunks.push_back(AddChunkPrefix(2, kPrefix2)); | 1762 chunks.push_back(AddChunkPrefix(2, kPrefix2)); |
| 1768 | 1763 |
| 1769 std::vector<SBListChunkRanges> lists; | 1764 std::vector<SBListChunkRanges> lists; |
| 1770 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1765 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1771 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1766 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1772 database_->UpdateFinished(true); | 1767 database_->UpdateFinished(true); |
| 1773 | 1768 |
| 1774 { | 1769 { |
| 1775 // kFullHash1_1 has a prefix hit of kPrefix1. | 1770 // kFullHash1_1 has a prefix hit of kPrefix1. |
| 1776 std::vector<SBFullHash> full_hashes; | 1771 std::vector<SBFullHash> full_hashes; |
| 1777 full_hashes.push_back(kFullHash1_1); | 1772 full_hashes.push_back(kFullHash1_1); |
| 1778 std::vector<SBPrefix> prefix_hits; | 1773 std::vector<SBPrefix> prefix_hits; |
| 1779 std::vector<SBFullHashResult> cache_hits; | 1774 std::vector<SBFullHashResult> cache_hits; |
| 1780 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1775 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1781 full_hashes, &prefix_hits, &cache_hits)); | 1776 full_hashes, &prefix_hits, &cache_hits)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1805 EXPECT_EQ(kPrefix2, prefix_hits[1]); | 1800 EXPECT_EQ(kPrefix2, prefix_hits[1]); |
| 1806 EXPECT_TRUE(cache_hits.empty()); | 1801 EXPECT_TRUE(cache_hits.empty()); |
| 1807 } | 1802 } |
| 1808 | 1803 |
| 1809 { | 1804 { |
| 1810 // Cache a fullhash result for two kPrefix1 full hashes. | 1805 // Cache a fullhash result for two kPrefix1 full hashes. |
| 1811 std::vector<SBPrefix> prefixes(1, kPrefix1); | 1806 std::vector<SBPrefix> prefixes(1, kPrefix1); |
| 1812 std::vector<SBFullHashResult> cache_results; | 1807 std::vector<SBFullHashResult> cache_results; |
| 1813 | 1808 |
| 1814 SBFullHashResult full_hash_result; | 1809 SBFullHashResult full_hash_result; |
| 1815 full_hash_result.list_id = safe_browsing::MALWARE; | 1810 full_hash_result.list_id = safe_browsing_util::MALWARE; |
| 1816 | 1811 |
| 1817 full_hash_result.hash = kFullHash1_1; | 1812 full_hash_result.hash = kFullHash1_1; |
| 1818 cache_results.push_back(full_hash_result); | 1813 cache_results.push_back(full_hash_result); |
| 1819 | 1814 |
| 1820 full_hash_result.hash = kFullHash1_3; | 1815 full_hash_result.hash = kFullHash1_3; |
| 1821 cache_results.push_back(full_hash_result); | 1816 cache_results.push_back(full_hash_result); |
| 1822 | 1817 |
| 1823 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); | 1818 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); |
| 1824 } | 1819 } |
| 1825 | 1820 |
| 1826 { | 1821 { |
| 1827 // kFullHash1_1 should now see a cache hit. | 1822 // kFullHash1_1 should now see a cache hit. |
| 1828 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); | 1823 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); |
| 1829 std::vector<SBPrefix> prefix_hits; | 1824 std::vector<SBPrefix> prefix_hits; |
| 1830 std::vector<SBFullHashResult> cache_hits; | 1825 std::vector<SBFullHashResult> cache_hits; |
| 1831 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1826 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1832 full_hashes, &prefix_hits, &cache_hits)); | 1827 full_hashes, &prefix_hits, &cache_hits)); |
| 1833 EXPECT_TRUE(prefix_hits.empty()); | 1828 EXPECT_TRUE(prefix_hits.empty()); |
| 1834 ASSERT_EQ(1U, cache_hits.size()); | 1829 ASSERT_EQ(1U, cache_hits.size()); |
| 1835 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 1830 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash)); |
| 1836 kFullHash1_1, cache_hits[0].hash)); | |
| 1837 | 1831 |
| 1838 // Adding kFullHash2_1 will see the existing cache hit plus the prefix hit | 1832 // Adding kFullHash2_1 will see the existing cache hit plus the prefix hit |
| 1839 // for kPrefix2. | 1833 // for kPrefix2. |
| 1840 full_hashes.push_back(kFullHash2_1); | 1834 full_hashes.push_back(kFullHash2_1); |
| 1841 prefix_hits.clear(); | 1835 prefix_hits.clear(); |
| 1842 cache_hits.clear(); | 1836 cache_hits.clear(); |
| 1843 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1837 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1844 full_hashes, &prefix_hits, &cache_hits)); | 1838 full_hashes, &prefix_hits, &cache_hits)); |
| 1845 ASSERT_EQ(1U, prefix_hits.size()); | 1839 ASSERT_EQ(1U, prefix_hits.size()); |
| 1846 EXPECT_EQ(kPrefix2, prefix_hits[0]); | 1840 EXPECT_EQ(kPrefix2, prefix_hits[0]); |
| 1847 ASSERT_EQ(1U, cache_hits.size()); | 1841 ASSERT_EQ(1U, cache_hits.size()); |
| 1848 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 1842 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash)); |
| 1849 kFullHash1_1, cache_hits[0].hash)); | |
| 1850 | 1843 |
| 1851 // kFullHash1_3 also gets a cache hit. | 1844 // kFullHash1_3 also gets a cache hit. |
| 1852 full_hashes.push_back(kFullHash1_3); | 1845 full_hashes.push_back(kFullHash1_3); |
| 1853 prefix_hits.clear(); | 1846 prefix_hits.clear(); |
| 1854 cache_hits.clear(); | 1847 cache_hits.clear(); |
| 1855 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1848 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1856 full_hashes, &prefix_hits, &cache_hits)); | 1849 full_hashes, &prefix_hits, &cache_hits)); |
| 1857 ASSERT_EQ(1U, prefix_hits.size()); | 1850 ASSERT_EQ(1U, prefix_hits.size()); |
| 1858 EXPECT_EQ(kPrefix2, prefix_hits[0]); | 1851 EXPECT_EQ(kPrefix2, prefix_hits[0]); |
| 1859 ASSERT_EQ(2U, cache_hits.size()); | 1852 ASSERT_EQ(2U, cache_hits.size()); |
| 1860 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 1853 EXPECT_TRUE(SBFullHashEqual(kFullHash1_1, cache_hits[0].hash)); |
| 1861 kFullHash1_1, cache_hits[0].hash)); | 1854 EXPECT_TRUE(SBFullHashEqual(kFullHash1_3, cache_hits[1].hash)); |
| 1862 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | |
| 1863 kFullHash1_3, cache_hits[1].hash)); | |
| 1864 } | 1855 } |
| 1865 | 1856 |
| 1866 { | 1857 { |
| 1867 // Check if DB contains only kFullHash1_3. Should return a cache hit. | 1858 // Check if DB contains only kFullHash1_3. Should return a cache hit. |
| 1868 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); | 1859 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); |
| 1869 std::vector<SBPrefix> prefix_hits; | 1860 std::vector<SBPrefix> prefix_hits; |
| 1870 std::vector<SBFullHashResult> cache_hits; | 1861 std::vector<SBFullHashResult> cache_hits; |
| 1871 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1862 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1872 full_hashes, &prefix_hits, &cache_hits)); | 1863 full_hashes, &prefix_hits, &cache_hits)); |
| 1873 EXPECT_TRUE(prefix_hits.empty()); | 1864 EXPECT_TRUE(prefix_hits.empty()); |
| 1874 ASSERT_EQ(1U, cache_hits.size()); | 1865 ASSERT_EQ(1U, cache_hits.size()); |
| 1875 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 1866 EXPECT_TRUE(SBFullHashEqual(kFullHash1_3, cache_hits[0].hash)); |
| 1876 kFullHash1_3, cache_hits[0].hash)); | |
| 1877 } | 1867 } |
| 1878 | 1868 |
| 1879 { | 1869 { |
| 1880 // kFullHash1_2 has no cache hit, and no prefix hit because of the cache for | 1870 // kFullHash1_2 has no cache hit, and no prefix hit because of the cache for |
| 1881 // kPrefix1. | 1871 // kPrefix1. |
| 1882 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 1872 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
| 1883 std::vector<SBPrefix> prefix_hits; | 1873 std::vector<SBPrefix> prefix_hits; |
| 1884 std::vector<SBFullHashResult> cache_hits; | 1874 std::vector<SBFullHashResult> cache_hits; |
| 1885 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 1875 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1886 full_hashes, &prefix_hits, &cache_hits)); | 1876 full_hashes, &prefix_hits, &cache_hits)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1906 const SBFullHash kFullHash1_3 = | 1896 const SBFullHash kFullHash1_3 = |
| 1907 SBFullHashForPrefixAndSuffix(kPrefix1, "\x03"); | 1897 SBFullHashForPrefixAndSuffix(kPrefix1, "\x03"); |
| 1908 | 1898 |
| 1909 // Insert two full hashes with a shared prefix. | 1899 // Insert two full hashes with a shared prefix. |
| 1910 ScopedVector<SBChunkData> chunks; | 1900 ScopedVector<SBChunkData> chunks; |
| 1911 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); | 1901 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); |
| 1912 chunks.push_back(AddChunkFullHash(2, kFullHash1_2)); | 1902 chunks.push_back(AddChunkFullHash(2, kFullHash1_2)); |
| 1913 | 1903 |
| 1914 std::vector<SBListChunkRanges> lists; | 1904 std::vector<SBListChunkRanges> lists; |
| 1915 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1905 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1916 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1906 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1917 database_->UpdateFinished(true); | 1907 database_->UpdateFinished(true); |
| 1918 | 1908 |
| 1919 { | 1909 { |
| 1920 // Check a full hash which isn't present. | 1910 // Check a full hash which isn't present. |
| 1921 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); | 1911 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); |
| 1922 std::vector<SBPrefix> prefix_hits; | 1912 std::vector<SBPrefix> prefix_hits; |
| 1923 std::vector<SBFullHashResult> cache_hits; | 1913 std::vector<SBFullHashResult> cache_hits; |
| 1924 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 1914 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1925 full_hashes, &prefix_hits, &cache_hits)); | 1915 full_hashes, &prefix_hits, &cache_hits)); |
| 1926 | 1916 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1941 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1931 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1942 full_hashes, &prefix_hits, &cache_hits)); | 1932 full_hashes, &prefix_hits, &cache_hits)); |
| 1943 ASSERT_EQ(1U, prefix_hits.size()); | 1933 ASSERT_EQ(1U, prefix_hits.size()); |
| 1944 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1934 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 1945 EXPECT_TRUE(cache_hits.empty()); | 1935 EXPECT_TRUE(cache_hits.empty()); |
| 1946 } | 1936 } |
| 1947 | 1937 |
| 1948 { | 1938 { |
| 1949 // Cache a gethash result for kFullHash1_2. | 1939 // Cache a gethash result for kFullHash1_2. |
| 1950 SBFullHashResult full_hash_result; | 1940 SBFullHashResult full_hash_result; |
| 1951 full_hash_result.list_id = safe_browsing::MALWARE; | 1941 full_hash_result.list_id = safe_browsing_util::MALWARE; |
| 1952 full_hash_result.hash = kFullHash1_2; | 1942 full_hash_result.hash = kFullHash1_2; |
| 1953 | 1943 |
| 1954 std::vector<SBPrefix> prefixes(1, kPrefix1); | 1944 std::vector<SBPrefix> prefixes(1, kPrefix1); |
| 1955 std::vector<SBFullHashResult> cache_results(1, full_hash_result); | 1945 std::vector<SBFullHashResult> cache_results(1, full_hash_result); |
| 1956 | 1946 |
| 1957 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); | 1947 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); |
| 1958 } | 1948 } |
| 1959 | 1949 |
| 1960 { | 1950 { |
| 1961 // kFullHash1_3 should still return false, because the cached | 1951 // kFullHash1_3 should still return false, because the cached |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1975 full_hashes, &prefix_hits, &cache_hits)); | 1965 full_hashes, &prefix_hits, &cache_hits)); |
| 1976 | 1966 |
| 1977 // kFullHash1_2 is in the cached result. | 1967 // kFullHash1_2 is in the cached result. |
| 1978 full_hashes.push_back(kFullHash1_2); | 1968 full_hashes.push_back(kFullHash1_2); |
| 1979 prefix_hits.clear(); | 1969 prefix_hits.clear(); |
| 1980 cache_hits.clear(); | 1970 cache_hits.clear(); |
| 1981 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1971 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1982 full_hashes, &prefix_hits, &cache_hits)); | 1972 full_hashes, &prefix_hits, &cache_hits)); |
| 1983 EXPECT_TRUE(prefix_hits.empty()); | 1973 EXPECT_TRUE(prefix_hits.empty()); |
| 1984 ASSERT_EQ(1U, cache_hits.size()); | 1974 ASSERT_EQ(1U, cache_hits.size()); |
| 1985 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 1975 EXPECT_TRUE(SBFullHashEqual(kFullHash1_2, cache_hits[0].hash)); |
| 1986 kFullHash1_2, cache_hits[0].hash)); | |
| 1987 } | 1976 } |
| 1988 | 1977 |
| 1989 // Remove kFullHash1_1 from the database. | 1978 // Remove kFullHash1_1 from the database. |
| 1990 chunks.clear(); | 1979 chunks.clear(); |
| 1991 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); | 1980 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); |
| 1992 | 1981 |
| 1993 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1982 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1994 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1983 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 1995 database_->UpdateFinished(true); | 1984 database_->UpdateFinished(true); |
| 1996 | 1985 |
| 1997 // Cache should be cleared after updating. | 1986 // Cache should be cleared after updating. |
| 1998 EXPECT_TRUE( | 1987 EXPECT_TRUE( |
| 1999 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); | 1988 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
| 2000 | 1989 |
| 2001 { | 1990 { |
| 2002 // Now the database doesn't contain kFullHash1_1. | 1991 // Now the database doesn't contain kFullHash1_1. |
| 2003 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); | 1992 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); |
| 2004 std::vector<SBPrefix> prefix_hits; | 1993 std::vector<SBPrefix> prefix_hits; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 2022 ASSERT_EQ(1U, prefix_hits.size()); | 2011 ASSERT_EQ(1U, prefix_hits.size()); |
| 2023 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 2012 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 2024 EXPECT_TRUE(cache_hits.empty()); | 2013 EXPECT_TRUE(cache_hits.empty()); |
| 2025 } | 2014 } |
| 2026 | 2015 |
| 2027 // Remove kFullHash1_2 from the database. | 2016 // Remove kFullHash1_2 from the database. |
| 2028 chunks.clear(); | 2017 chunks.clear(); |
| 2029 chunks.push_back(SubChunkFullHash(12, kFullHash1_2, 2)); | 2018 chunks.push_back(SubChunkFullHash(12, kFullHash1_2, 2)); |
| 2030 | 2019 |
| 2031 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2020 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 2032 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2021 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 2033 database_->UpdateFinished(true); | 2022 database_->UpdateFinished(true); |
| 2034 | 2023 |
| 2035 // Cache should be cleared after updating. | 2024 // Cache should be cleared after updating. |
| 2036 EXPECT_TRUE( | 2025 EXPECT_TRUE( |
| 2037 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); | 2026 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
| 2038 | 2027 |
| 2039 { | 2028 { |
| 2040 // None are present. | 2029 // None are present. |
| 2041 std::vector<SBFullHash> full_hashes; | 2030 std::vector<SBFullHash> full_hashes; |
| 2042 std::vector<SBPrefix> prefix_hits; | 2031 std::vector<SBPrefix> prefix_hits; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 2054 const SBFullHash kFullHash1_1 = | 2043 const SBFullHash kFullHash1_1 = |
| 2055 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); | 2044 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); |
| 2056 const SBFullHash kFullHash1_2 = | 2045 const SBFullHash kFullHash1_2 = |
| 2057 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02"); | 2046 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02"); |
| 2058 | 2047 |
| 2059 ScopedVector<SBChunkData> chunks; | 2048 ScopedVector<SBChunkData> chunks; |
| 2060 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); | 2049 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); |
| 2061 | 2050 |
| 2062 std::vector<SBListChunkRanges> lists; | 2051 std::vector<SBListChunkRanges> lists; |
| 2063 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2052 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 2064 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2053 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 2065 database_->UpdateFinished(true); | 2054 database_->UpdateFinished(true); |
| 2066 | 2055 |
| 2067 { | 2056 { |
| 2068 // kFullHash1_2 does not match kFullHash1_1. | 2057 // kFullHash1_2 does not match kFullHash1_1. |
| 2069 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 2058 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
| 2070 std::vector<SBPrefix> prefix_hits; | 2059 std::vector<SBPrefix> prefix_hits; |
| 2071 std::vector<SBFullHashResult> cache_hits; | 2060 std::vector<SBFullHashResult> cache_hits; |
| 2072 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 2061 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 2073 full_hashes, &prefix_hits, &cache_hits)); | 2062 full_hashes, &prefix_hits, &cache_hits)); |
| 2074 } | 2063 } |
| 2075 | 2064 |
| 2076 // Add a prefix match. | 2065 // Add a prefix match. |
| 2077 chunks.clear(); | 2066 chunks.clear(); |
| 2078 chunks.push_back(AddChunkPrefix(2, kPrefix1)); | 2067 chunks.push_back(AddChunkPrefix(2, kPrefix1)); |
| 2079 | 2068 |
| 2080 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2069 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 2081 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2070 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 2082 database_->UpdateFinished(true); | 2071 database_->UpdateFinished(true); |
| 2083 | 2072 |
| 2084 { | 2073 { |
| 2085 // kFullHash1_2 does match kPrefix1. | 2074 // kFullHash1_2 does match kPrefix1. |
| 2086 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 2075 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
| 2087 std::vector<SBPrefix> prefix_hits; | 2076 std::vector<SBPrefix> prefix_hits; |
| 2088 std::vector<SBFullHashResult> cache_hits; | 2077 std::vector<SBFullHashResult> cache_hits; |
| 2089 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 2078 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 2090 full_hashes, &prefix_hits, &cache_hits)); | 2079 full_hashes, &prefix_hits, &cache_hits)); |
| 2091 ASSERT_EQ(1U, prefix_hits.size()); | 2080 ASSERT_EQ(1U, prefix_hits.size()); |
| 2092 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 2081 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 2093 EXPECT_TRUE(cache_hits.empty()); | 2082 EXPECT_TRUE(cache_hits.empty()); |
| 2094 } | 2083 } |
| 2095 | 2084 |
| 2096 // Remove the full hash. | 2085 // Remove the full hash. |
| 2097 chunks.clear(); | 2086 chunks.clear(); |
| 2098 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); | 2087 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); |
| 2099 | 2088 |
| 2100 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2089 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 2101 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2090 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 2102 database_->UpdateFinished(true); | 2091 database_->UpdateFinished(true); |
| 2103 | 2092 |
| 2104 { | 2093 { |
| 2105 // kFullHash1_2 still returns true due to the prefix hit. | 2094 // kFullHash1_2 still returns true due to the prefix hit. |
| 2106 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 2095 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
| 2107 std::vector<SBPrefix> prefix_hits; | 2096 std::vector<SBPrefix> prefix_hits; |
| 2108 std::vector<SBFullHashResult> cache_hits; | 2097 std::vector<SBFullHashResult> cache_hits; |
| 2109 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 2098 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 2110 full_hashes, &prefix_hits, &cache_hits)); | 2099 full_hashes, &prefix_hits, &cache_hits)); |
| 2111 ASSERT_EQ(1U, prefix_hits.size()); | 2100 ASSERT_EQ(1U, prefix_hits.size()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 2131 | 2120 |
| 2132 // IPv6 prefix match for: 2620:0:1000:3103::/64. | 2121 // IPv6 prefix match for: 2620:0:1000:3103::/64. |
| 2133 chunks.push_back(AddChunkHashedIpValue(4, "2620:0:1000:3103::", 64)); | 2122 chunks.push_back(AddChunkHashedIpValue(4, "2620:0:1000:3103::", 64)); |
| 2134 | 2123 |
| 2135 // IPv4 prefix match for ::ffff:192.1.122.0/119. | 2124 // IPv4 prefix match for ::ffff:192.1.122.0/119. |
| 2136 chunks.push_back(AddChunkHashedIpValue(5, "::ffff:192.1.122.0", 119)); | 2125 chunks.push_back(AddChunkHashedIpValue(5, "::ffff:192.1.122.0", 119)); |
| 2137 | 2126 |
| 2138 // IPv4 prefix match for ::ffff:192.1.128.0/113. | 2127 // IPv4 prefix match for ::ffff:192.1.128.0/113. |
| 2139 chunks.push_back(AddChunkHashedIpValue(6, "::ffff:192.1.128.0", 113)); | 2128 chunks.push_back(AddChunkHashedIpValue(6, "::ffff:192.1.128.0", 113)); |
| 2140 | 2129 |
| 2141 database_->InsertChunks(safe_browsing::kIPBlacklist, chunks.get()); | 2130 database_->InsertChunks(safe_browsing_util::kIPBlacklist, chunks.get()); |
| 2142 database_->UpdateFinished(true); | 2131 database_->UpdateFinished(true); |
| 2143 | 2132 |
| 2144 EXPECT_FALSE(database_->ContainsMalwareIP("192.168.0.255")); | 2133 EXPECT_FALSE(database_->ContainsMalwareIP("192.168.0.255")); |
| 2145 EXPECT_TRUE(database_->ContainsMalwareIP("192.168.1.0")); | 2134 EXPECT_TRUE(database_->ContainsMalwareIP("192.168.1.0")); |
| 2146 EXPECT_TRUE(database_->ContainsMalwareIP("192.168.1.255")); | 2135 EXPECT_TRUE(database_->ContainsMalwareIP("192.168.1.255")); |
| 2147 EXPECT_TRUE(database_->ContainsMalwareIP("192.168.1.10")); | 2136 EXPECT_TRUE(database_->ContainsMalwareIP("192.168.1.10")); |
| 2148 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.168.1.2")); | 2137 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.168.1.2")); |
| 2149 EXPECT_FALSE(database_->ContainsMalwareIP("192.168.2.0")); | 2138 EXPECT_FALSE(database_->ContainsMalwareIP("192.168.2.0")); |
| 2150 | 2139 |
| 2151 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.1.0")); | 2140 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.1.0")); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 2181 } | 2170 } |
| 2182 | 2171 |
| 2183 TEST_F(SafeBrowsingDatabaseTest, ContainsBrowseURL) { | 2172 TEST_F(SafeBrowsingDatabaseTest, ContainsBrowseURL) { |
| 2184 std::vector<SBListChunkRanges> lists; | 2173 std::vector<SBListChunkRanges> lists; |
| 2185 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2174 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 2186 | 2175 |
| 2187 // Add a host-level hit. | 2176 // Add a host-level hit. |
| 2188 { | 2177 { |
| 2189 ScopedVector<SBChunkData> chunks; | 2178 ScopedVector<SBChunkData> chunks; |
| 2190 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/")); | 2179 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/")); |
| 2191 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2180 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 2192 } | 2181 } |
| 2193 | 2182 |
| 2194 // Add a specific fullhash. | 2183 // Add a specific fullhash. |
| 2195 static const char kWhateverMalware[] = "www.whatever.com/malware.html"; | 2184 static const char kWhateverMalware[] = "www.whatever.com/malware.html"; |
| 2196 { | 2185 { |
| 2197 ScopedVector<SBChunkData> chunks; | 2186 ScopedVector<SBChunkData> chunks; |
| 2198 chunks.push_back(AddChunkFullHashValue(2, kWhateverMalware)); | 2187 chunks.push_back(AddChunkFullHashValue(2, kWhateverMalware)); |
| 2199 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2188 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 2200 } | 2189 } |
| 2201 | 2190 |
| 2202 // Add a fullhash which has a prefix collision for a known url. | 2191 // Add a fullhash which has a prefix collision for a known url. |
| 2203 static const char kExampleFine[] = "www.example.com/fine.html"; | 2192 static const char kExampleFine[] = "www.example.com/fine.html"; |
| 2204 static const char kExampleCollision[] = | 2193 static const char kExampleCollision[] = |
| 2205 "www.example.com/3123364814/malware.htm"; | 2194 "www.example.com/3123364814/malware.htm"; |
| 2206 ASSERT_EQ(SBPrefixForString(kExampleFine), | 2195 ASSERT_EQ(SBPrefixForString(kExampleFine), |
| 2207 SBPrefixForString(kExampleCollision)); | 2196 SBPrefixForString(kExampleCollision)); |
| 2208 { | 2197 { |
| 2209 ScopedVector<SBChunkData> chunks; | 2198 ScopedVector<SBChunkData> chunks; |
| 2210 chunks.push_back(AddChunkFullHashValue(3, kExampleCollision)); | 2199 chunks.push_back(AddChunkFullHashValue(3, kExampleCollision)); |
| 2211 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2200 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks.get()); |
| 2212 } | 2201 } |
| 2213 | 2202 |
| 2214 database_->UpdateFinished(true); | 2203 database_->UpdateFinished(true); |
| 2215 | 2204 |
| 2216 std::vector<SBPrefix> prefix_hits; | 2205 std::vector<SBPrefix> prefix_hits; |
| 2217 std::vector<SBFullHashResult> cache_hits; | 2206 std::vector<SBFullHashResult> cache_hits; |
| 2218 | 2207 |
| 2219 // Anything will hit the host prefix. | 2208 // Anything will hit the host prefix. |
| 2220 EXPECT_TRUE(database_->ContainsBrowseUrl( | 2209 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 2221 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 2210 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 2242 GURL(std::string("http://") + kExampleCollision), | 2231 GURL(std::string("http://") + kExampleCollision), |
| 2243 &prefix_hits, &cache_hits)); | 2232 &prefix_hits, &cache_hits)); |
| 2244 ASSERT_EQ(1U, prefix_hits.size()); | 2233 ASSERT_EQ(1U, prefix_hits.size()); |
| 2245 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); | 2234 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); |
| 2246 EXPECT_TRUE(cache_hits.empty()); | 2235 EXPECT_TRUE(cache_hits.empty()); |
| 2247 | 2236 |
| 2248 // This prefix collides, but no full hash match. | 2237 // This prefix collides, but no full hash match. |
| 2249 EXPECT_FALSE(database_->ContainsBrowseUrl( | 2238 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 2250 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); | 2239 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); |
| 2251 } | 2240 } |
| OLD | NEW |