| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/sha1.h" | 14 #include "base/sha1.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_split.h" | 16 #include "base/strings/string_split.h" |
| 17 #include "base/test/test_simple_task_runner.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 #include "chrome/browser/safe_browsing/chunk.pb.h" | 19 #include "chrome/browser/safe_browsing/chunk.pb.h" |
| 19 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" |
| 20 #include "crypto/sha2.h" | 21 #include "crypto/sha2.h" |
| 21 #include "net/base/net_util.h" | 22 #include "net/base/net_util.h" |
| 22 #include "sql/connection.h" | 23 #include "sql/connection.h" |
| 23 #include "sql/statement.h" | 24 #include "sql/statement.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "testing/platform_test.h" | 26 #include "testing/platform_test.h" |
| 26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 } | 247 } |
| 247 | 248 |
| 248 return false; | 249 return false; |
| 249 } | 250 } |
| 250 }; | 251 }; |
| 251 | 252 |
| 252 } // namespace | 253 } // namespace |
| 253 | 254 |
| 254 class SafeBrowsingDatabaseTest : public PlatformTest { | 255 class SafeBrowsingDatabaseTest : public PlatformTest { |
| 255 public: | 256 public: |
| 257 SafeBrowsingDatabaseTest() : task_runner_(new base::TestSimpleTaskRunner) {} |
| 258 |
| 256 void SetUp() override { | 259 void SetUp() override { |
| 257 PlatformTest::SetUp(); | 260 PlatformTest::SetUp(); |
| 258 | 261 |
| 259 // Setup a database in a temporary directory. | 262 // Setup a database in a temporary directory. |
| 260 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 263 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 261 database_filename_ = | 264 database_filename_ = |
| 262 temp_dir_.path().AppendASCII("SafeBrowsingTestDatabase"); | 265 temp_dir_.path().AppendASCII("SafeBrowsingTestDatabase"); |
| 263 | 266 |
| 264 ResetAndReloadFullDatabase(); | 267 ResetAndReloadFullDatabase(); |
| 265 } | 268 } |
| 266 | 269 |
| 267 void TearDown() override { | 270 void TearDown() override { |
| 268 database_.reset(); | 271 database_.reset(); |
| 269 | 272 |
| 270 PlatformTest::TearDown(); | 273 PlatformTest::TearDown(); |
| 271 } | 274 } |
| 272 | 275 |
| 273 // Reloads the |database_| in a new SafeBrowsingDatabaseNew object with all | 276 // Reloads the |database_| in a new SafeBrowsingDatabaseNew object with all |
| 274 // stores enabled. | 277 // stores enabled. |
| 275 void ResetAndReloadFullDatabase() { | 278 void ResetAndReloadFullDatabase() { |
| 276 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); | 279 SafeBrowsingStoreFile* browse_store = |
| 277 SafeBrowsingStoreFile* download_store = new SafeBrowsingStoreFile(); | 280 new SafeBrowsingStoreFile(task_runner_); |
| 278 SafeBrowsingStoreFile* csd_whitelist_store = new SafeBrowsingStoreFile(); | 281 SafeBrowsingStoreFile* download_store = |
| 282 new SafeBrowsingStoreFile(task_runner_); |
| 283 SafeBrowsingStoreFile* csd_whitelist_store = |
| 284 new SafeBrowsingStoreFile(task_runner_); |
| 279 SafeBrowsingStoreFile* download_whitelist_store = | 285 SafeBrowsingStoreFile* download_whitelist_store = |
| 280 new SafeBrowsingStoreFile(); | 286 new SafeBrowsingStoreFile(task_runner_); |
| 281 SafeBrowsingStoreFile* inclusion_whitelist_store = | 287 SafeBrowsingStoreFile* inclusion_whitelist_store = |
| 282 new SafeBrowsingStoreFile(); | 288 new SafeBrowsingStoreFile(task_runner_); |
| 283 SafeBrowsingStoreFile* extension_blacklist_store = | 289 SafeBrowsingStoreFile* extension_blacklist_store = |
| 284 new SafeBrowsingStoreFile(); | 290 new SafeBrowsingStoreFile(task_runner_); |
| 285 SafeBrowsingStoreFile* side_effect_free_whitelist_store = | 291 SafeBrowsingStoreFile* side_effect_free_whitelist_store = |
| 286 new SafeBrowsingStoreFile(); | 292 new SafeBrowsingStoreFile(task_runner_); |
| 287 SafeBrowsingStoreFile* ip_blacklist_store = new SafeBrowsingStoreFile(); | 293 SafeBrowsingStoreFile* ip_blacklist_store = |
| 294 new SafeBrowsingStoreFile(task_runner_); |
| 288 SafeBrowsingStoreFile* unwanted_software_store = | 295 SafeBrowsingStoreFile* unwanted_software_store = |
| 289 new SafeBrowsingStoreFile(); | 296 new SafeBrowsingStoreFile(task_runner_); |
| 290 database_.reset( | 297 database_.reset(new SafeBrowsingDatabaseNew( |
| 291 new SafeBrowsingDatabaseNew(browse_store, | 298 task_runner_, browse_store, download_store, csd_whitelist_store, |
| 292 download_store, | 299 download_whitelist_store, inclusion_whitelist_store, |
| 293 csd_whitelist_store, | 300 extension_blacklist_store, side_effect_free_whitelist_store, |
| 294 download_whitelist_store, | 301 ip_blacklist_store, unwanted_software_store)); |
| 295 inclusion_whitelist_store, | |
| 296 extension_blacklist_store, | |
| 297 side_effect_free_whitelist_store, | |
| 298 ip_blacklist_store, | |
| 299 unwanted_software_store)); | |
| 300 database_->Init(database_filename_); | 302 database_->Init(database_filename_); |
| 301 } | 303 } |
| 302 | 304 |
| 303 void GetListsInfo(std::vector<SBListChunkRanges>* lists) { | 305 void GetListsInfo(std::vector<SBListChunkRanges>* lists) { |
| 304 lists->clear(); | 306 lists->clear(); |
| 305 ASSERT_TRUE(database_->UpdateStarted(lists)); | 307 ASSERT_TRUE(database_->UpdateStarted(lists)); |
| 306 database_->UpdateFinished(true); | 308 database_->UpdateFinished(true); |
| 307 } | 309 } |
| 308 | 310 |
| 309 // Helper function to do an AddDel or SubDel command. | 311 // Helper function to do an AddDel or SubDel command. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 323 DelChunk(list, chunk_id, false); | 325 DelChunk(list, chunk_id, false); |
| 324 } | 326 } |
| 325 | 327 |
| 326 void SubDelChunk(const std::string& list, int chunk_id) { | 328 void SubDelChunk(const std::string& list, int chunk_id) { |
| 327 DelChunk(list, chunk_id, true); | 329 DelChunk(list, chunk_id, true); |
| 328 } | 330 } |
| 329 | 331 |
| 330 // Utility function for setting up the database for the caching test. | 332 // Utility function for setting up the database for the caching test. |
| 331 void PopulateDatabaseForCacheTest(); | 333 void PopulateDatabaseForCacheTest(); |
| 332 | 334 |
| 335 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 333 scoped_ptr<SafeBrowsingDatabaseNew> database_; | 336 scoped_ptr<SafeBrowsingDatabaseNew> database_; |
| 334 base::FilePath database_filename_; | 337 base::FilePath database_filename_; |
| 335 base::ScopedTempDir temp_dir_; | 338 base::ScopedTempDir temp_dir_; |
| 336 }; | 339 }; |
| 337 | 340 |
| 338 // Tests retrieving list name information. | 341 // Tests retrieving list name information. |
| 339 TEST_F(SafeBrowsingDatabaseTest, BrowseListsInfo) { | 342 TEST_F(SafeBrowsingDatabaseTest, BrowseListsInfo) { |
| 340 std::vector<SBListChunkRanges> lists; | 343 std::vector<SBListChunkRanges> lists; |
| 341 ScopedVector<SBChunkData> chunks; | 344 ScopedVector<SBChunkData> chunks; |
| 342 | 345 |
| (...skipping 765 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1108 // Test that corrupt databases are appropriately handled, even if the | 1111 // Test that corrupt databases are appropriately handled, even if the |
| 1109 // corruption is detected in the midst of the update. | 1112 // corruption is detected in the midst of the update. |
| 1110 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. | 1113 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. |
| 1111 // http://crbug.com/56448 | 1114 // http://crbug.com/56448 |
| 1112 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { | 1115 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { |
| 1113 // Re-create the database in a captive message loop so that we can | 1116 // Re-create the database in a captive message loop so that we can |
| 1114 // influence task-posting. Database specifically needs to the | 1117 // influence task-posting. Database specifically needs to the |
| 1115 // file-backed. | 1118 // file-backed. |
| 1116 database_.reset(); | 1119 database_.reset(); |
| 1117 base::MessageLoop loop; | 1120 base::MessageLoop loop; |
| 1118 SafeBrowsingStoreFile* store = new SafeBrowsingStoreFile(); | 1121 SafeBrowsingStoreFile* store = new SafeBrowsingStoreFile(task_runner_); |
| 1119 database_.reset(new SafeBrowsingDatabaseNew(store, NULL, NULL, NULL, NULL, | 1122 database_.reset(new SafeBrowsingDatabaseNew( |
| 1120 NULL, NULL, NULL, NULL)); | 1123 task_runner_, store, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)); |
| 1121 database_->Init(database_filename_); | 1124 database_->Init(database_filename_); |
| 1122 | 1125 |
| 1123 // This will cause an empty database to be created. | 1126 // This will cause an empty database to be created. |
| 1124 std::vector<SBListChunkRanges> lists; | 1127 std::vector<SBListChunkRanges> lists; |
| 1125 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1128 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1126 database_->UpdateFinished(true); | 1129 database_->UpdateFinished(true); |
| 1127 | 1130 |
| 1128 // Create a sub chunk to insert. | 1131 // Create a sub chunk to insert. |
| 1129 ScopedVector<SBChunkData> chunks; | 1132 ScopedVector<SBChunkData> chunks; |
| 1130 chunks.push_back(SubChunkPrefixValue(7, | 1133 chunks.push_back(SubChunkPrefixValue(7, |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1283 nullptr}, | 1286 nullptr}, |
| 1284 {safe_browsing_util::kDownloadWhiteList, | 1287 {safe_browsing_util::kDownloadWhiteList, |
| 1285 &SafeBrowsingDatabase::ContainsDownloadWhitelistedUrl, | 1288 &SafeBrowsingDatabase::ContainsDownloadWhitelistedUrl, |
| 1286 &SafeBrowsingDatabase::ContainsDownloadWhitelistedString}, | 1289 &SafeBrowsingDatabase::ContainsDownloadWhitelistedString}, |
| 1287 {safe_browsing_util::kInclusionWhitelist, | 1290 {safe_browsing_util::kInclusionWhitelist, |
| 1288 &SafeBrowsingDatabase::ContainsInclusionWhitelistedUrl, | 1291 &SafeBrowsingDatabase::ContainsInclusionWhitelistedUrl, |
| 1289 nullptr}, | 1292 nullptr}, |
| 1290 }; | 1293 }; |
| 1291 | 1294 |
| 1292 // If the whitelist is disabled everything should match the whitelist. | 1295 // If the whitelist is disabled everything should match the whitelist. |
| 1293 database_.reset(new SafeBrowsingDatabaseNew(new SafeBrowsingStoreFile(), NULL, | 1296 database_.reset(new SafeBrowsingDatabaseNew( |
| 1294 NULL, NULL, NULL, NULL, NULL, | 1297 task_runner_, new SafeBrowsingStoreFile(task_runner_), NULL, NULL, NULL, |
| 1295 NULL, NULL)); | 1298 NULL, NULL, NULL, NULL, NULL)); |
| 1296 database_->Init(database_filename_); | 1299 database_->Init(database_filename_); |
| 1297 for (const auto& test_case : kTestCases) { | 1300 for (const auto& test_case : kTestCases) { |
| 1298 SCOPED_TRACE(std::string("Tested list at fault => ") + | 1301 SCOPED_TRACE(std::string("Tested list at fault => ") + |
| 1299 test_case.test_list_name); | 1302 test_case.test_list_name); |
| 1300 | 1303 |
| 1301 EXPECT_TRUE( | 1304 EXPECT_TRUE( |
| 1302 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1305 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1303 GURL(std::string("http://www.phishing.com/")))); | 1306 GURL(std::string("http://www.phishing.com/")))); |
| 1304 if (test_case.TestStrings()) { | 1307 if (test_case.TestStrings()) { |
| 1305 EXPECT_TRUE( | 1308 EXPECT_TRUE( |
| (...skipping 927 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2233 GURL(std::string("http://") + kExampleCollision), | 2236 GURL(std::string("http://") + kExampleCollision), |
| 2234 &prefix_hits, &cache_hits)); | 2237 &prefix_hits, &cache_hits)); |
| 2235 ASSERT_EQ(1U, prefix_hits.size()); | 2238 ASSERT_EQ(1U, prefix_hits.size()); |
| 2236 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); | 2239 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); |
| 2237 EXPECT_TRUE(cache_hits.empty()); | 2240 EXPECT_TRUE(cache_hits.empty()); |
| 2238 | 2241 |
| 2239 // This prefix collides, but no full hash match. | 2242 // This prefix collides, but no full hash match. |
| 2240 EXPECT_FALSE(database_->ContainsBrowseUrl( | 2243 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 2241 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); | 2244 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); |
| 2242 } | 2245 } |
| OLD | NEW |