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

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

Issue 1673733002: Support new Safe Browsing list "goog-badresource-shavar" in SafeBrowsingDatabase. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Check resource URLs in database manager Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // Unit tests for the SafeBrowsing storage system. 5 // Unit tests for the SafeBrowsing storage system.
6 6
7 #include "chrome/browser/safe_browsing/safe_browsing_database.h" 7 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
8 8
9 #include <stddef.h> 9 #include <stddef.h>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/files/file_util.h" 12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h" 13 #include "base/files/scoped_temp_dir.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/message_loop/message_loop.h" 15 #include "base/message_loop/message_loop.h"
16 #include "base/sha1.h" 16 #include "base/sha1.h"
17 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h" 18 #include "base/strings/string_split.h"
19 #include "base/test/test_simple_task_runner.h" 19 #include "base/test/test_simple_task_runner.h"
20 #include "base/time/time.h" 20 #include "base/time/time.h"
21 #include "chrome/browser/safe_browsing/chunk.pb.h" 21 #include "chrome/browser/safe_browsing/chunk.pb.h"
22 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" 22 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h"
23 #include "crypto/sha2.h" 23 #include "crypto/sha2.h"
24 #include "net/base/ip_address_number.h" 24 #include "net/base/ip_address_number.h"
25 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 26 #include "testing/gtest/include/gtest/gtest.h"
26 #include "testing/platform_test.h" 27 #include "testing/platform_test.h"
27 #include "url/gurl.h" 28 #include "url/gurl.h"
28 29
29 using base::Time; 30 using base::Time;
30 using base::TimeDelta; 31 using base::TimeDelta;
31 32
32 namespace safe_browsing { 33 namespace safe_browsing {
33 34
34 namespace { 35 namespace {
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 SafeBrowsingStoreFile* download_whitelist_store = 274 SafeBrowsingStoreFile* download_whitelist_store =
274 new SafeBrowsingStoreFile(task_runner_); 275 new SafeBrowsingStoreFile(task_runner_);
275 SafeBrowsingStoreFile* inclusion_whitelist_store = 276 SafeBrowsingStoreFile* inclusion_whitelist_store =
276 new SafeBrowsingStoreFile(task_runner_); 277 new SafeBrowsingStoreFile(task_runner_);
277 SafeBrowsingStoreFile* extension_blacklist_store = 278 SafeBrowsingStoreFile* extension_blacklist_store =
278 new SafeBrowsingStoreFile(task_runner_); 279 new SafeBrowsingStoreFile(task_runner_);
279 SafeBrowsingStoreFile* ip_blacklist_store = 280 SafeBrowsingStoreFile* ip_blacklist_store =
280 new SafeBrowsingStoreFile(task_runner_); 281 new SafeBrowsingStoreFile(task_runner_);
281 SafeBrowsingStoreFile* unwanted_software_store = 282 SafeBrowsingStoreFile* unwanted_software_store =
282 new SafeBrowsingStoreFile(task_runner_); 283 new SafeBrowsingStoreFile(task_runner_);
284 SafeBrowsingStoreFile* resource_blacklist_store =
285 new SafeBrowsingStoreFile(task_runner_);
283 database_.reset(new SafeBrowsingDatabaseNew( 286 database_.reset(new SafeBrowsingDatabaseNew(
284 task_runner_, browse_store, download_store, csd_whitelist_store, 287 task_runner_, browse_store, download_store, csd_whitelist_store,
285 download_whitelist_store, inclusion_whitelist_store, 288 download_whitelist_store, inclusion_whitelist_store,
286 extension_blacklist_store, ip_blacklist_store, 289 extension_blacklist_store, ip_blacklist_store,
287 unwanted_software_store)); 290 unwanted_software_store, resource_blacklist_store));
288 database_->Init(database_filename_); 291 database_->Init(database_filename_);
289 } 292 }
290 293
291 bool ContainsDownloadUrl(const std::vector<GURL>& urls, 294 bool ContainsDownloadUrl(const std::vector<GURL>& urls,
292 std::vector<SBPrefix>* prefix_hits) { 295 std::vector<SBPrefix>* prefix_hits) {
293 std::vector<SBPrefix> prefixes; 296 std::vector<SBPrefix> prefixes;
294 SafeBrowsingDatabase::GetDownloadUrlPrefixes(urls, &prefixes); 297 SafeBrowsingDatabase::GetDownloadUrlPrefixes(urls, &prefixes);
295 return database_->ContainsDownloadUrlPrefixes(prefixes, prefix_hits); 298 return database_->ContainsDownloadUrlPrefixes(prefixes, prefix_hits);
296 } 299 }
297 300
301 bool ContainsResourceUrl(const GURL& url,
302 std::vector<SBPrefix>* prefix_hits) {
303 std::vector<SBFullHash> full_hashes;
304 UrlToFullHashes(url, false, &full_hashes);
305 std::vector<SBPrefix> prefixes(full_hashes.size());
306 for (size_t i = 0; i < full_hashes.size(); ++i)
307 prefixes[i] = full_hashes[i].prefix;
308 return database_->ContainsResourceUrlPrefixes(prefixes, prefix_hits);
309 }
310
298 void GetListsInfo(std::vector<SBListChunkRanges>* lists) { 311 void GetListsInfo(std::vector<SBListChunkRanges>* lists) {
299 lists->clear(); 312 lists->clear();
300 ASSERT_TRUE(database_->UpdateStarted(lists)); 313 ASSERT_TRUE(database_->UpdateStarted(lists));
301 database_->UpdateFinished(true); 314 database_->UpdateFinished(true);
302 } 315 }
303 316
304 // Helper function to do an AddDel or SubDel command. 317 // Helper function to do an AddDel or SubDel command.
305 void DelChunk(const std::string& list, 318 void DelChunk(const std::string& list,
306 int chunk_id, 319 int chunk_id,
307 bool is_sub_del) { 320 bool is_sub_del) {
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 chunks); 446 chunks);
434 447
435 chunks.clear(); 448 chunks.clear();
436 chunks.push_back(AddChunkHashedIpValue(10, "::ffff:192.168.1.0", 120)); 449 chunks.push_back(AddChunkHashedIpValue(10, "::ffff:192.168.1.0", 120));
437 database_->InsertChunks(kIPBlacklist, chunks); 450 database_->InsertChunks(kIPBlacklist, chunks);
438 451
439 chunks.clear(); 452 chunks.clear();
440 chunks.push_back(AddChunkPrefixValue(11, "www.unwanted.com/software.html")); 453 chunks.push_back(AddChunkPrefixValue(11, "www.unwanted.com/software.html"));
441 database_->InsertChunks(kUnwantedUrlList, chunks); 454 database_->InsertChunks(kUnwantedUrlList, chunks);
442 455
456 chunks.clear();
457 chunks.push_back(AddChunkPrefixValue(12, "foo.com/script.js"));
458 database_->InsertChunks(kResourceBlacklist, chunks);
459
443 database_->UpdateFinished(true); 460 database_->UpdateFinished(true);
444 461
445 GetListsInfo(&lists); 462 GetListsInfo(&lists);
446 ASSERT_EQ(9U, lists.size()); 463 ASSERT_EQ(10U, lists.size());
447 EXPECT_EQ(kMalwareList, lists[0].name); 464 EXPECT_EQ(kMalwareList, lists[0].name);
448 EXPECT_EQ("1", lists[0].adds); 465 EXPECT_EQ("1", lists[0].adds);
449 EXPECT_TRUE(lists[0].subs.empty()); 466 EXPECT_TRUE(lists[0].subs.empty());
450 EXPECT_EQ(kPhishingList, lists[1].name); 467 EXPECT_EQ(kPhishingList, lists[1].name);
451 EXPECT_EQ("2", lists[1].adds); 468 EXPECT_EQ("2", lists[1].adds);
452 EXPECT_TRUE(lists[1].subs.empty()); 469 EXPECT_TRUE(lists[1].subs.empty());
453 EXPECT_EQ(kBinUrlList, lists[2].name); 470 EXPECT_EQ(kBinUrlList, lists[2].name);
454 EXPECT_EQ("3", lists[2].adds); 471 EXPECT_EQ("3", lists[2].adds);
455 EXPECT_TRUE(lists[2].subs.empty()); 472 EXPECT_TRUE(lists[2].subs.empty());
456 EXPECT_EQ(kCsdWhiteList, lists[3].name); 473 EXPECT_EQ(kCsdWhiteList, lists[3].name);
457 EXPECT_EQ("5", lists[3].adds); 474 EXPECT_EQ("5", lists[3].adds);
458 EXPECT_TRUE(lists[3].subs.empty()); 475 EXPECT_TRUE(lists[3].subs.empty());
459 EXPECT_EQ(kDownloadWhiteList, lists[4].name); 476 EXPECT_EQ(kDownloadWhiteList, lists[4].name);
460 EXPECT_EQ("6", lists[4].adds); 477 EXPECT_EQ("6", lists[4].adds);
461 EXPECT_TRUE(lists[4].subs.empty()); 478 EXPECT_TRUE(lists[4].subs.empty());
462 EXPECT_EQ(kInclusionWhitelist, lists[5].name); 479 EXPECT_EQ(kInclusionWhitelist, lists[5].name);
463 EXPECT_EQ("7", lists[5].adds); 480 EXPECT_EQ("7", lists[5].adds);
464 EXPECT_TRUE(lists[5].subs.empty()); 481 EXPECT_TRUE(lists[5].subs.empty());
465 EXPECT_EQ(kExtensionBlacklist, lists[6].name); 482 EXPECT_EQ(kExtensionBlacklist, lists[6].name);
466 EXPECT_EQ("8", lists[6].adds); 483 EXPECT_EQ("8", lists[6].adds);
467 EXPECT_TRUE(lists[6].subs.empty()); 484 EXPECT_TRUE(lists[6].subs.empty());
468 EXPECT_EQ(kIPBlacklist, lists[7].name); 485 EXPECT_EQ(kIPBlacklist, lists[7].name);
469 EXPECT_EQ("10", lists[7].adds); 486 EXPECT_EQ("10", lists[7].adds);
470 EXPECT_TRUE(lists[7].subs.empty()); 487 EXPECT_TRUE(lists[7].subs.empty());
471 EXPECT_EQ(kUnwantedUrlList, lists[8].name); 488 EXPECT_EQ(kUnwantedUrlList, lists[8].name);
472 EXPECT_EQ("11", lists[8].adds); 489 EXPECT_EQ("11", lists[8].adds);
473 EXPECT_TRUE(lists[8].subs.empty()); 490 EXPECT_TRUE(lists[8].subs.empty());
491 EXPECT_EQ(kResourceBlacklist, lists[9].name);
492 EXPECT_EQ("12", lists[9].adds);
493 EXPECT_TRUE(lists[9].subs.empty());
474 494
475 database_.reset(); 495 database_.reset();
476 } 496 }
477 497
478 // Checks database reading and writing for browse and unwanted PrefixSets. 498 // Checks database reading and writing for browse and unwanted PrefixSets.
479 TEST_F(SafeBrowsingDatabaseTest, BrowseAndUnwantedDatabasesAndPrefixSets) { 499 TEST_F(SafeBrowsingDatabaseTest, BrowseAndUnwantedDatabasesAndPrefixSets) {
480 500
481 struct TestCase { 501 struct TestCase {
482 using TestListContainsBadUrl = bool (SafeBrowsingDatabase::*)( 502 using TestListContainsBadUrl = bool (SafeBrowsingDatabase::*)(
483 const GURL& url, 503 const GURL& url,
(...skipping 662 matching lines...) Expand 10 before | Expand all | Expand 10 after
1146 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. 1166 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved.
1147 // http://crbug.com/56448 1167 // http://crbug.com/56448
1148 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { 1168 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) {
1149 // Re-create the database in a captive message loop so that we can 1169 // Re-create the database in a captive message loop so that we can
1150 // influence task-posting. Database specifically needs to the 1170 // influence task-posting. Database specifically needs to the
1151 // file-backed. 1171 // file-backed.
1152 database_.reset(); 1172 database_.reset();
1153 base::MessageLoop loop; 1173 base::MessageLoop loop;
1154 SafeBrowsingStoreFile* store = new SafeBrowsingStoreFile(task_runner_); 1174 SafeBrowsingStoreFile* store = new SafeBrowsingStoreFile(task_runner_);
1155 database_.reset(new SafeBrowsingDatabaseNew( 1175 database_.reset(new SafeBrowsingDatabaseNew(
1156 task_runner_, store, NULL, NULL, NULL, NULL, NULL, NULL, NULL)); 1176 task_runner_, store, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL));
1157 database_->Init(database_filename_); 1177 database_->Init(database_filename_);
1158 1178
1159 // This will cause an empty database to be created. 1179 // This will cause an empty database to be created.
1160 std::vector<SBListChunkRanges> lists; 1180 std::vector<SBListChunkRanges> lists;
1161 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1181 ASSERT_TRUE(database_->UpdateStarted(&lists));
1162 database_->UpdateFinished(true); 1182 database_->UpdateFinished(true);
1163 1183
1164 // Create a sub chunk to insert. 1184 // Create a sub chunk to insert.
1165 std::vector<scoped_ptr<SBChunkData>> chunks; 1185 std::vector<scoped_ptr<SBChunkData>> chunks;
1166 chunks.push_back(SubChunkPrefixValue(7, 1186 chunks.push_back(SubChunkPrefixValue(7,
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
1287 urls.clear(); 1307 urls.clear();
1288 urls.push_back(GURL(std::string("http://") + kEvil1Url1)); 1308 urls.push_back(GURL(std::string("http://") + kEvil1Url1));
1289 urls.push_back(GURL(std::string("https://") + kEvil1Url2)); 1309 urls.push_back(GURL(std::string("https://") + kEvil1Url2));
1290 EXPECT_TRUE(ContainsDownloadUrl(urls, &prefix_hits)); 1310 EXPECT_TRUE(ContainsDownloadUrl(urls, &prefix_hits));
1291 ASSERT_EQ(2U, prefix_hits.size()); 1311 ASSERT_EQ(2U, prefix_hits.size());
1292 EXPECT_EQ(SBPrefixForString(kEvil1Url1), prefix_hits[0]); 1312 EXPECT_EQ(SBPrefixForString(kEvil1Url1), prefix_hits[0]);
1293 EXPECT_EQ(SBPrefixForString(kEvil1Url2), prefix_hits[1]); 1313 EXPECT_EQ(SBPrefixForString(kEvil1Url2), prefix_hits[1]);
1294 database_.reset(); 1314 database_.reset();
1295 } 1315 }
1296 1316
1317 TEST_F(SafeBrowsingDatabaseTest, ContainsResourceUrlPrefixes) {
1318 const char* kBadUrl1 = "bad1.com/";
1319 const char* kBadUrl2 = "bad2.com/script.js";
1320 const SBPrefix kBadPrefix1 = SBPrefixForString(kBadUrl1);
1321 const SBPrefix kBadPrefix2 = SBPrefixForString(kBadUrl2);
1322
1323 // Populate database
1324 std::vector<scoped_ptr<SBChunkData>> chunks;
1325 chunks.push_back(AddChunkPrefix2Value(1, kBadUrl1, kBadUrl2));
1326
1327 std::vector<SBListChunkRanges> lists;
1328 ASSERT_TRUE(database_->UpdateStarted(&lists));
1329 database_->InsertChunks(kResourceBlacklist, chunks);
1330 database_->UpdateFinished(true);
1331
1332 struct {
1333 std::string url;
1334 bool found_in_db;
1335 std::vector<SBPrefix> prefix_hits;
1336 } test_cases[] = {
1337 {std::string("http://") + kBadUrl1, true, {kBadPrefix1}},
1338 {std::string("https://") + kBadUrl2, true, {kBadPrefix2}},
1339 {std::string("ftp://") + kBadUrl1, true, {kBadPrefix1}},
1340 {std::string("http://") + kBadUrl1 + "a/b/?arg=value", true, {kBadPrefix1}},
1341 {std::string("http://") + kBadUrl1 + "script.js", true, {kBadPrefix1}},
1342 {std::string("http://www.domain.") + kBadUrl2, true, {kBadPrefix2}},
1343 {"http://www.good.org/script.js", false, std::vector<SBPrefix>()},
1344 };
1345
1346 std::vector<SBPrefix> prefix_hits;
1347 for (const auto& test_case : test_cases) {
1348 EXPECT_EQ(test_case.found_in_db,
1349 ContainsResourceUrl(GURL(test_case.url), &prefix_hits));
1350 EXPECT_THAT(prefix_hits, testing::ElementsAreArray(test_case.prefix_hits));
1351 }
1352
1353 database_.reset();
1354 }
1355
1297 // Checks that the whitelists are handled properly. 1356 // Checks that the whitelists are handled properly.
1298 TEST_F(SafeBrowsingDatabaseTest, Whitelists) { 1357 TEST_F(SafeBrowsingDatabaseTest, Whitelists) {
1299 struct TestCase { 1358 struct TestCase {
1300 using TestListContainsWhitelistedUrl = 1359 using TestListContainsWhitelistedUrl =
1301 bool (SafeBrowsingDatabase::*)(const GURL& url); 1360 bool (SafeBrowsingDatabase::*)(const GURL& url);
1302 using TestListContainsWhitelistedString = 1361 using TestListContainsWhitelistedString =
1303 bool (SafeBrowsingDatabase::*)(const std::string& str); 1362 bool (SafeBrowsingDatabase::*)(const std::string& str);
1304 1363
1305 // Returns true if strings should be tested in this test case (i.e. 1364 // Returns true if strings should be tested in this test case (i.e.
1306 // |test_list_contains_whitelisted_string| is not null). 1365 // |test_list_contains_whitelisted_string| is not null).
(...skipping 14 matching lines...) Expand all
1321 &SafeBrowsingDatabase::ContainsDownloadWhitelistedUrl, 1380 &SafeBrowsingDatabase::ContainsDownloadWhitelistedUrl,
1322 &SafeBrowsingDatabase::ContainsDownloadWhitelistedString}, 1381 &SafeBrowsingDatabase::ContainsDownloadWhitelistedString},
1323 {kInclusionWhitelist, 1382 {kInclusionWhitelist,
1324 &SafeBrowsingDatabase::ContainsInclusionWhitelistedUrl, 1383 &SafeBrowsingDatabase::ContainsInclusionWhitelistedUrl,
1325 nullptr}, 1384 nullptr},
1326 }; 1385 };
1327 1386
1328 // If the whitelist is disabled everything should match the whitelist. 1387 // If the whitelist is disabled everything should match the whitelist.
1329 database_.reset(new SafeBrowsingDatabaseNew( 1388 database_.reset(new SafeBrowsingDatabaseNew(
1330 task_runner_, new SafeBrowsingStoreFile(task_runner_), NULL, NULL, NULL, 1389 task_runner_, new SafeBrowsingStoreFile(task_runner_), NULL, NULL, NULL,
1331 NULL, NULL, NULL, NULL)); 1390 NULL, NULL, NULL, NULL, NULL));
1332 database_->Init(database_filename_); 1391 database_->Init(database_filename_);
1333 for (const auto& test_case : kTestCases) { 1392 for (const auto& test_case : kTestCases) {
1334 SCOPED_TRACE(std::string("Tested list at fault => ") + 1393 SCOPED_TRACE(std::string("Tested list at fault => ") +
1335 test_case.test_list_name); 1394 test_case.test_list_name);
1336 1395
1337 EXPECT_TRUE( 1396 EXPECT_TRUE(
1338 (database_.get()->*test_case.test_list_contains_whitelisted_url)( 1397 (database_.get()->*test_case.test_list_contains_whitelisted_url)(
1339 GURL(std::string("http://www.phishing.com/")))); 1398 GURL(std::string("http://www.phishing.com/"))));
1340 if (test_case.TestStrings()) { 1399 if (test_case.TestStrings()) {
1341 EXPECT_TRUE( 1400 EXPECT_TRUE(
(...skipping 935 matching lines...) Expand 10 before | Expand all | Expand 10 after
2277 ASSERT_EQ(1U, prefix_hits.size()); 2336 ASSERT_EQ(1U, prefix_hits.size());
2278 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); 2337 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]);
2279 EXPECT_TRUE(cache_hits.empty()); 2338 EXPECT_TRUE(cache_hits.empty());
2280 2339
2281 // This prefix collides, but no full hash match. 2340 // This prefix collides, but no full hash match.
2282 EXPECT_FALSE(database_->ContainsBrowseUrl( 2341 EXPECT_FALSE(database_->ContainsBrowseUrl(
2283 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); 2342 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits));
2284 } 2343 }
2285 2344
2286 } // namespace safe_browsing 2345 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698