| 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 "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 356 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); | 356 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); |
| 357 EXPECT_EQ(lists[0].adds, "1-3"); | 357 EXPECT_EQ(lists[0].adds, "1-3"); |
| 358 EXPECT_EQ(lists[0].subs, "7"); | 358 EXPECT_EQ(lists[0].subs, "7"); |
| 359 EXPECT_TRUE(lists[1].name == safe_browsing_util::kPhishingList); | 359 EXPECT_TRUE(lists[1].name == safe_browsing_util::kPhishingList); |
| 360 EXPECT_EQ(lists[1].adds, "47"); | 360 EXPECT_EQ(lists[1].adds, "47"); |
| 361 EXPECT_EQ(lists[1].subs, "200-201"); | 361 EXPECT_EQ(lists[1].subs, "200-201"); |
| 362 } | 362 } |
| 363 | 363 |
| 364 TEST_F(SafeBrowsingDatabaseTest, ListNameForBrowseAndDownload) { | 364 TEST_F(SafeBrowsingDatabaseTest, ListNameForBrowseAndDownload) { |
| 365 database_.reset(); | 365 database_.reset(); |
| 366 MessageLoop loop(MessageLoop::TYPE_DEFAULT); | 366 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
| 367 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); | 367 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); |
| 368 SafeBrowsingStoreFile* download_store = new SafeBrowsingStoreFile(); | 368 SafeBrowsingStoreFile* download_store = new SafeBrowsingStoreFile(); |
| 369 SafeBrowsingStoreFile* csd_whitelist_store = new SafeBrowsingStoreFile(); | 369 SafeBrowsingStoreFile* csd_whitelist_store = new SafeBrowsingStoreFile(); |
| 370 SafeBrowsingStoreFile* download_whitelist_store = new SafeBrowsingStoreFile(); | 370 SafeBrowsingStoreFile* download_whitelist_store = new SafeBrowsingStoreFile(); |
| 371 SafeBrowsingStoreFile* extension_blacklist_store = | 371 SafeBrowsingStoreFile* extension_blacklist_store = |
| 372 new SafeBrowsingStoreFile(); | 372 new SafeBrowsingStoreFile(); |
| 373 database_.reset(new SafeBrowsingDatabaseNew(browse_store, | 373 database_.reset(new SafeBrowsingDatabaseNew(browse_store, |
| 374 download_store, | 374 download_store, |
| 375 csd_whitelist_store, | 375 csd_whitelist_store, |
| 376 download_whitelist_store, | 376 download_whitelist_store, |
| (...skipping 694 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1071 | 1071 |
| 1072 // Test that corrupt databases are appropriately handled, even if the | 1072 // Test that corrupt databases are appropriately handled, even if the |
| 1073 // corruption is detected in the midst of the update. | 1073 // corruption is detected in the midst of the update. |
| 1074 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. | 1074 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. |
| 1075 // http://crbug.com/56448 | 1075 // http://crbug.com/56448 |
| 1076 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { | 1076 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { |
| 1077 // Re-create the database in a captive message loop so that we can | 1077 // Re-create the database in a captive message loop so that we can |
| 1078 // influence task-posting. Database specifically needs to the | 1078 // influence task-posting. Database specifically needs to the |
| 1079 // file-backed. | 1079 // file-backed. |
| 1080 database_.reset(); | 1080 database_.reset(); |
| 1081 MessageLoop loop(MessageLoop::TYPE_DEFAULT); | 1081 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
| 1082 SafeBrowsingStoreFile* store = new SafeBrowsingStoreFile(); | 1082 SafeBrowsingStoreFile* store = new SafeBrowsingStoreFile(); |
| 1083 database_.reset(new SafeBrowsingDatabaseNew(store, NULL, NULL, NULL, NULL)); | 1083 database_.reset(new SafeBrowsingDatabaseNew(store, NULL, NULL, NULL, NULL)); |
| 1084 database_->Init(database_filename_); | 1084 database_->Init(database_filename_); |
| 1085 | 1085 |
| 1086 // This will cause an empty database to be created. | 1086 // This will cause an empty database to be created. |
| 1087 std::vector<SBListChunkRanges> lists; | 1087 std::vector<SBListChunkRanges> lists; |
| 1088 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1088 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1089 database_->UpdateFinished(true); | 1089 database_->UpdateFinished(true); |
| 1090 | 1090 |
| 1091 // Create a sub chunk to insert. | 1091 // Create a sub chunk to insert. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1121 // Start an update. The insert will fail due to corruption. | 1121 // Start an update. The insert will fail due to corruption. |
| 1122 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1122 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1123 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 1123 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1124 database_->UpdateFinished(true); | 1124 database_->UpdateFinished(true); |
| 1125 | 1125 |
| 1126 // Database file still exists until the corruption handler has run. | 1126 // Database file still exists until the corruption handler has run. |
| 1127 EXPECT_TRUE(file_util::PathExists(database_filename_)); | 1127 EXPECT_TRUE(file_util::PathExists(database_filename_)); |
| 1128 | 1128 |
| 1129 // Flush through the corruption-handler task. | 1129 // Flush through the corruption-handler task. |
| 1130 VLOG(1) << "Expect failed check on: SafeBrowsing database reset"; | 1130 VLOG(1) << "Expect failed check on: SafeBrowsing database reset"; |
| 1131 MessageLoop::current()->RunUntilIdle(); | 1131 base::MessageLoop::current()->RunUntilIdle(); |
| 1132 } | 1132 } |
| 1133 | 1133 |
| 1134 // Database file should not exist. | 1134 // Database file should not exist. |
| 1135 EXPECT_FALSE(file_util::PathExists(database_filename_)); | 1135 EXPECT_FALSE(file_util::PathExists(database_filename_)); |
| 1136 | 1136 |
| 1137 // Run the update again successfully. | 1137 // Run the update again successfully. |
| 1138 EXPECT_TRUE(database_->UpdateStarted(&lists)); | 1138 EXPECT_TRUE(database_->UpdateStarted(&lists)); |
| 1139 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); | 1139 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); |
| 1140 database_->UpdateFinished(true); | 1140 database_->UpdateFinished(true); |
| 1141 EXPECT_TRUE(file_util::PathExists(database_filename_)); | 1141 EXPECT_TRUE(file_util::PathExists(database_filename_)); |
| 1142 | 1142 |
| 1143 database_.reset(); | 1143 database_.reset(); |
| 1144 } | 1144 } |
| 1145 | 1145 |
| 1146 // Checks database reading and writing. | 1146 // Checks database reading and writing. |
| 1147 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrl) { | 1147 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrl) { |
| 1148 database_.reset(); | 1148 database_.reset(); |
| 1149 MessageLoop loop(MessageLoop::TYPE_DEFAULT); | 1149 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
| 1150 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); | 1150 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); |
| 1151 SafeBrowsingStoreFile* download_store = new SafeBrowsingStoreFile(); | 1151 SafeBrowsingStoreFile* download_store = new SafeBrowsingStoreFile(); |
| 1152 SafeBrowsingStoreFile* csd_whitelist_store = new SafeBrowsingStoreFile(); | 1152 SafeBrowsingStoreFile* csd_whitelist_store = new SafeBrowsingStoreFile(); |
| 1153 database_.reset(new SafeBrowsingDatabaseNew(browse_store, | 1153 database_.reset(new SafeBrowsingDatabaseNew(browse_store, |
| 1154 download_store, | 1154 download_store, |
| 1155 csd_whitelist_store, | 1155 csd_whitelist_store, |
| 1156 NULL, | 1156 NULL, |
| 1157 NULL)); | 1157 NULL)); |
| 1158 database_->Init(database_filename_); | 1158 database_->Init(database_filename_); |
| 1159 | 1159 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1242 EXPECT_TRUE(database_->ContainsDownloadUrl(urls, &prefix_hits)); | 1242 EXPECT_TRUE(database_->ContainsDownloadUrl(urls, &prefix_hits)); |
| 1243 ASSERT_EQ(prefix_hits.size(), 2U); | 1243 ASSERT_EQ(prefix_hits.size(), 2U); |
| 1244 EXPECT_EQ(prefix_hits[0], Sha256Prefix(kEvil1Url1)); | 1244 EXPECT_EQ(prefix_hits[0], Sha256Prefix(kEvil1Url1)); |
| 1245 EXPECT_EQ(prefix_hits[1], Sha256Prefix(kEvil1Url2)); | 1245 EXPECT_EQ(prefix_hits[1], Sha256Prefix(kEvil1Url2)); |
| 1246 database_.reset(); | 1246 database_.reset(); |
| 1247 } | 1247 } |
| 1248 | 1248 |
| 1249 // Checks that the whitelists are handled properly. | 1249 // Checks that the whitelists are handled properly. |
| 1250 TEST_F(SafeBrowsingDatabaseTest, Whitelists) { | 1250 TEST_F(SafeBrowsingDatabaseTest, Whitelists) { |
| 1251 database_.reset(); | 1251 database_.reset(); |
| 1252 MessageLoop loop(MessageLoop::TYPE_DEFAULT); | 1252 base::MessageLoop loop(base::MessageLoop::TYPE_DEFAULT); |
| 1253 // We expect all calls to ContainsCsdWhitelistedUrl in particular to be made | 1253 // We expect all calls to ContainsCsdWhitelistedUrl in particular to be made |
| 1254 // from the IO thread. In general the whitelist lookups are thread-safe. | 1254 // from the IO thread. In general the whitelist lookups are thread-safe. |
| 1255 content::TestBrowserThread io_thread(BrowserThread::IO, &loop); | 1255 content::TestBrowserThread io_thread(BrowserThread::IO, &loop); |
| 1256 | 1256 |
| 1257 // If the whitelist is disabled everything should match the whitelist. | 1257 // If the whitelist is disabled everything should match the whitelist. |
| 1258 database_.reset(new SafeBrowsingDatabaseNew(new SafeBrowsingStoreFile(), | 1258 database_.reset(new SafeBrowsingDatabaseNew(new SafeBrowsingStoreFile(), |
| 1259 NULL, NULL, NULL, NULL)); | 1259 NULL, NULL, NULL, NULL)); |
| 1260 database_->Init(database_filename_); | 1260 database_->Init(database_filename_); |
| 1261 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl( | 1261 EXPECT_TRUE(database_->ContainsDownloadWhitelistedUrl( |
| 1262 GURL(std::string("http://www.phishing.com/")))); | 1262 GURL(std::string("http://www.phishing.com/")))); |
| (...skipping 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1658 ASSERT_FALSE(file_util::PathExists(filter_file)); | 1658 ASSERT_FALSE(file_util::PathExists(filter_file)); |
| 1659 database_.reset(new SafeBrowsingDatabaseNew); | 1659 database_.reset(new SafeBrowsingDatabaseNew); |
| 1660 database_->Init(database_filename_); | 1660 database_->Init(database_filename_); |
| 1661 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1661 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1662 GURL("http://www.evil.com/malware.html"), | 1662 GURL("http://www.evil.com/malware.html"), |
| 1663 &matching_list, &prefix_hits, &full_hashes, now)); | 1663 &matching_list, &prefix_hits, &full_hashes, now)); |
| 1664 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1664 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1665 GURL("http://www.good.com/goodware.html"), | 1665 GURL("http://www.good.com/goodware.html"), |
| 1666 &matching_list, &prefix_hits, &full_hashes, now)); | 1666 &matching_list, &prefix_hits, &full_hashes, now)); |
| 1667 } | 1667 } |
| OLD | NEW |