| 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/test/test_simple_task_runner.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "chrome/browser/safe_browsing/chunk.pb.h" | 19 #include "chrome/browser/safe_browsing/chunk.pb.h" |
| 20 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" |
| 21 #include "crypto/sha2.h" | 21 #include "crypto/sha2.h" |
| 22 #include "net/base/ip_address_number.h" | 22 #include "net/base/ip_address_number.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
| 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 safe_browsing { |
| 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)); |
| 46 memcpy(full_hash.full_hash + sizeof(SBPrefix), suffix.data(), suffix.size()); | 46 memcpy(full_hash.full_hash + sizeof(SBPrefix), suffix.data(), suffix.size()); |
| 47 return full_hash; | 47 return full_hash; |
| 48 } | 48 } |
| 49 | 49 |
| 50 std::string HashedIpPrefix(const std::string& ip_prefix, size_t prefix_size) { | 50 std::string HashedIpPrefix(const std::string& ip_prefix, size_t prefix_size) { |
| 51 net::IPAddressNumber ip_number; | 51 net::IPAddressNumber ip_number; |
| 52 EXPECT_TRUE(net::ParseIPLiteralToNumber(ip_prefix, &ip_number)); | 52 EXPECT_TRUE(net::ParseIPLiteralToNumber(ip_prefix, &ip_number)); |
| 53 EXPECT_EQ(net::kIPv6AddressSize, ip_number.size()); | 53 EXPECT_EQ(net::kIPv6AddressSize, ip_number.size()); |
| 54 const std::string hashed_ip_prefix = base::SHA1HashString( | 54 const std::string hashed_ip_prefix = base::SHA1HashString( |
| 55 net::IPAddressToPackedString(ip_number)); | 55 net::IPAddressToPackedString(ip_number)); |
| 56 std::string hash(crypto::kSHA256Length, '\0'); | 56 std::string hash(crypto::kSHA256Length, '\0'); |
| 57 hash.replace(0, hashed_ip_prefix.size(), hashed_ip_prefix); | 57 hash.replace(0, hashed_ip_prefix.size(), hashed_ip_prefix); |
| 58 hash[base::kSHA1Length] = static_cast<char>(prefix_size); | 58 hash[base::kSHA1Length] = static_cast<char>(prefix_size); |
| 59 return hash; | 59 return hash; |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Helper to build a chunk. Caller takes ownership. | 62 // Helper to build a chunk. Caller takes ownership. |
| 63 SBChunkData* BuildChunk(int chunk_number, | 63 SBChunkData* BuildChunk(int chunk_number, |
| 64 safe_browsing::ChunkData::ChunkType chunk_type, | 64 ChunkData::ChunkType chunk_type, |
| 65 safe_browsing::ChunkData::PrefixType prefix_type, | 65 ChunkData::PrefixType prefix_type, |
| 66 const void* data, size_t data_size, | 66 const void* data, |
| 67 size_t data_size, |
| 67 const std::vector<int>& add_chunk_numbers) { | 68 const std::vector<int>& add_chunk_numbers) { |
| 68 scoped_ptr<safe_browsing::ChunkData> raw_data(new safe_browsing::ChunkData); | 69 scoped_ptr<ChunkData> raw_data(new ChunkData); |
| 69 raw_data->set_chunk_number(chunk_number); | 70 raw_data->set_chunk_number(chunk_number); |
| 70 raw_data->set_chunk_type(chunk_type); | 71 raw_data->set_chunk_type(chunk_type); |
| 71 raw_data->set_prefix_type(prefix_type); | 72 raw_data->set_prefix_type(prefix_type); |
| 72 raw_data->set_hashes(data, data_size); | 73 raw_data->set_hashes(data, data_size); |
| 73 raw_data->clear_add_numbers(); | 74 raw_data->clear_add_numbers(); |
| 74 for (size_t i = 0; i < add_chunk_numbers.size(); ++i) { | 75 for (size_t i = 0; i < add_chunk_numbers.size(); ++i) { |
| 75 raw_data->add_add_numbers(add_chunk_numbers[i]); | 76 raw_data->add_add_numbers(add_chunk_numbers[i]); |
| 76 } | 77 } |
| 77 | 78 |
| 78 return new SBChunkData(raw_data.release()); | 79 return new SBChunkData(raw_data.release()); |
| 79 } | 80 } |
| 80 | 81 |
| 81 // Create add chunk with a single prefix. | 82 // Create add chunk with a single prefix. |
| 82 SBChunkData* AddChunkPrefix(int chunk_number, SBPrefix prefix) { | 83 SBChunkData* AddChunkPrefix(int chunk_number, SBPrefix prefix) { |
| 83 return BuildChunk(chunk_number, safe_browsing::ChunkData::ADD, | 84 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::PREFIX_4B, &prefix, |
| 84 safe_browsing::ChunkData::PREFIX_4B, | 85 sizeof(prefix), std::vector<int>()); |
| 85 &prefix, sizeof(prefix), | |
| 86 std::vector<int>()); | |
| 87 } | 86 } |
| 88 | 87 |
| 89 // Create add chunk with a single prefix generated from |value|. | 88 // Create add chunk with a single prefix generated from |value|. |
| 90 SBChunkData* AddChunkPrefixValue(int chunk_number, | 89 SBChunkData* AddChunkPrefixValue(int chunk_number, |
| 91 const std::string& value) { | 90 const std::string& value) { |
| 92 return AddChunkPrefix(chunk_number, SBPrefixForString(value)); | 91 return AddChunkPrefix(chunk_number, SBPrefixForString(value)); |
| 93 } | 92 } |
| 94 | 93 |
| 95 // Generate an add chunk with two prefixes. | 94 // Generate an add chunk with two prefixes. |
| 96 SBChunkData* AddChunkPrefix2Value(int chunk_number, | 95 SBChunkData* AddChunkPrefix2Value(int chunk_number, |
| 97 const std::string& value1, | 96 const std::string& value1, |
| 98 const std::string& value2) { | 97 const std::string& value2) { |
| 99 const SBPrefix prefixes[2] = { | 98 const SBPrefix prefixes[2] = { |
| 100 SBPrefixForString(value1), | 99 SBPrefixForString(value1), |
| 101 SBPrefixForString(value2), | 100 SBPrefixForString(value2), |
| 102 }; | 101 }; |
| 103 return BuildChunk(chunk_number, safe_browsing::ChunkData::ADD, | 102 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::PREFIX_4B, |
| 104 safe_browsing::ChunkData::PREFIX_4B, | 103 &prefixes[0], sizeof(prefixes), std::vector<int>()); |
| 105 &prefixes[0], sizeof(prefixes), | |
| 106 std::vector<int>()); | |
| 107 } | 104 } |
| 108 | 105 |
| 109 // Generate an add chunk with four prefixes. | 106 // Generate an add chunk with four prefixes. |
| 110 SBChunkData* AddChunkPrefix4Value(int chunk_number, | 107 SBChunkData* AddChunkPrefix4Value(int chunk_number, |
| 111 const std::string& value1, | 108 const std::string& value1, |
| 112 const std::string& value2, | 109 const std::string& value2, |
| 113 const std::string& value3, | 110 const std::string& value3, |
| 114 const std::string& value4) { | 111 const std::string& value4) { |
| 115 const SBPrefix prefixes[4] = { | 112 const SBPrefix prefixes[4] = { |
| 116 SBPrefixForString(value1), | 113 SBPrefixForString(value1), |
| 117 SBPrefixForString(value2), | 114 SBPrefixForString(value2), |
| 118 SBPrefixForString(value3), | 115 SBPrefixForString(value3), |
| 119 SBPrefixForString(value4), | 116 SBPrefixForString(value4), |
| 120 }; | 117 }; |
| 121 return BuildChunk(chunk_number, safe_browsing::ChunkData::ADD, | 118 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::PREFIX_4B, |
| 122 safe_browsing::ChunkData::PREFIX_4B, | 119 &prefixes[0], sizeof(prefixes), std::vector<int>()); |
| 123 &prefixes[0], sizeof(prefixes), | |
| 124 std::vector<int>()); | |
| 125 } | 120 } |
| 126 | 121 |
| 127 // Generate an add chunk with a full hash. | 122 // Generate an add chunk with a full hash. |
| 128 SBChunkData* AddChunkFullHash(int chunk_number, SBFullHash full_hash) { | 123 SBChunkData* AddChunkFullHash(int chunk_number, SBFullHash full_hash) { |
| 129 return BuildChunk(chunk_number, safe_browsing::ChunkData::ADD, | 124 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::FULL_32B, |
| 130 safe_browsing::ChunkData::FULL_32B, | 125 &full_hash, sizeof(full_hash), std::vector<int>()); |
| 131 &full_hash, sizeof(full_hash), | |
| 132 std::vector<int>()); | |
| 133 } | 126 } |
| 134 | 127 |
| 135 // Generate an add chunk with a full hash generated from |value|. | 128 // Generate an add chunk with a full hash generated from |value|. |
| 136 SBChunkData* AddChunkFullHashValue(int chunk_number, | 129 SBChunkData* AddChunkFullHashValue(int chunk_number, |
| 137 const std::string& value) { | 130 const std::string& value) { |
| 138 return AddChunkFullHash(chunk_number, | 131 return AddChunkFullHash(chunk_number, |
| 139 safe_browsing::SBFullHashForString(value)); | 132 SBFullHashForString(value)); |
| 140 } | 133 } |
| 141 | 134 |
| 142 // Generate an add chunk with two full hashes. | 135 // Generate an add chunk with two full hashes. |
| 143 SBChunkData* AddChunkFullHash2Value(int chunk_number, | 136 SBChunkData* AddChunkFullHash2Value(int chunk_number, |
| 144 const std::string& value1, | 137 const std::string& value1, |
| 145 const std::string& value2) { | 138 const std::string& value2) { |
| 146 const SBFullHash full_hashes[2] = { | 139 const SBFullHash full_hashes[2] = { |
| 147 safe_browsing::SBFullHashForString(value1), | 140 SBFullHashForString(value1), |
| 148 safe_browsing::SBFullHashForString(value2), | 141 SBFullHashForString(value2), |
| 149 }; | 142 }; |
| 150 return BuildChunk(chunk_number, safe_browsing::ChunkData::ADD, | 143 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::FULL_32B, |
| 151 safe_browsing::ChunkData::FULL_32B, | 144 &full_hashes[0], sizeof(full_hashes), std::vector<int>()); |
| 152 &full_hashes[0], sizeof(full_hashes), | |
| 153 std::vector<int>()); | |
| 154 } | 145 } |
| 155 | 146 |
| 156 // Generate a sub chunk with a prefix generated from |value|. | 147 // Generate a sub chunk with a prefix generated from |value|. |
| 157 SBChunkData* SubChunkPrefixValue(int chunk_number, | 148 SBChunkData* SubChunkPrefixValue(int chunk_number, |
| 158 const std::string& value, | 149 const std::string& value, |
| 159 int add_chunk_number) { | 150 int add_chunk_number) { |
| 160 const SBPrefix prefix = SBPrefixForString(value); | 151 const SBPrefix prefix = SBPrefixForString(value); |
| 161 return BuildChunk(chunk_number, safe_browsing::ChunkData::SUB, | 152 return BuildChunk(chunk_number, ChunkData::SUB, ChunkData::PREFIX_4B, &prefix, |
| 162 safe_browsing::ChunkData::PREFIX_4B, | 153 sizeof(prefix), std::vector<int>(1, add_chunk_number)); |
| 163 &prefix, sizeof(prefix), | |
| 164 std::vector<int>(1, add_chunk_number)); | |
| 165 } | 154 } |
| 166 | 155 |
| 167 // Generate a sub chunk with two prefixes. | 156 // Generate a sub chunk with two prefixes. |
| 168 SBChunkData* SubChunkPrefix2Value(int chunk_number, | 157 SBChunkData* SubChunkPrefix2Value(int chunk_number, |
| 169 const std::string& value1, | 158 const std::string& value1, |
| 170 int add_chunk_number1, | 159 int add_chunk_number1, |
| 171 const std::string& value2, | 160 const std::string& value2, |
| 172 int add_chunk_number2) { | 161 int add_chunk_number2) { |
| 173 const SBPrefix prefixes[2] = { | 162 const SBPrefix prefixes[2] = { |
| 174 SBPrefixForString(value1), | 163 SBPrefixForString(value1), |
| 175 SBPrefixForString(value2), | 164 SBPrefixForString(value2), |
| 176 }; | 165 }; |
| 177 std::vector<int> add_chunk_numbers; | 166 std::vector<int> add_chunk_numbers; |
| 178 add_chunk_numbers.push_back(add_chunk_number1); | 167 add_chunk_numbers.push_back(add_chunk_number1); |
| 179 add_chunk_numbers.push_back(add_chunk_number2); | 168 add_chunk_numbers.push_back(add_chunk_number2); |
| 180 return BuildChunk(chunk_number, safe_browsing::ChunkData::SUB, | 169 return BuildChunk(chunk_number, ChunkData::SUB, ChunkData::PREFIX_4B, |
| 181 safe_browsing::ChunkData::PREFIX_4B, | 170 &prefixes[0], sizeof(prefixes), add_chunk_numbers); |
| 182 &prefixes[0], sizeof(prefixes), | |
| 183 add_chunk_numbers); | |
| 184 } | 171 } |
| 185 | 172 |
| 186 // Generate a sub chunk with a full hash. | 173 // Generate a sub chunk with a full hash. |
| 187 SBChunkData* SubChunkFullHash(int chunk_number, | 174 SBChunkData* SubChunkFullHash(int chunk_number, |
| 188 SBFullHash full_hash, | 175 SBFullHash full_hash, |
| 189 int add_chunk_number) { | 176 int add_chunk_number) { |
| 190 return BuildChunk(chunk_number, safe_browsing::ChunkData::SUB, | 177 return BuildChunk(chunk_number, ChunkData::SUB, ChunkData::FULL_32B, |
| 191 safe_browsing::ChunkData::FULL_32B, | |
| 192 &full_hash, sizeof(full_hash), | 178 &full_hash, sizeof(full_hash), |
| 193 std::vector<int>(1, add_chunk_number)); | 179 std::vector<int>(1, add_chunk_number)); |
| 194 } | 180 } |
| 195 | 181 |
| 196 // Generate a sub chunk with a full hash generated from |value|. | 182 // Generate a sub chunk with a full hash generated from |value|. |
| 197 SBChunkData* SubChunkFullHashValue(int chunk_number, | 183 SBChunkData* SubChunkFullHashValue(int chunk_number, |
| 198 const std::string& value, | 184 const std::string& value, |
| 199 int add_chunk_number) { | 185 int add_chunk_number) { |
| 200 return SubChunkFullHash(chunk_number, | 186 return SubChunkFullHash(chunk_number, |
| 201 safe_browsing::SBFullHashForString(value), | 187 SBFullHashForString(value), |
| 202 add_chunk_number); | 188 add_chunk_number); |
| 203 } | 189 } |
| 204 | 190 |
| 205 // Generate an add chunk with a single full hash for the ip blacklist. | 191 // Generate an add chunk with a single full hash for the ip blacklist. |
| 206 SBChunkData* AddChunkHashedIpValue(int chunk_number, | 192 SBChunkData* AddChunkHashedIpValue(int chunk_number, |
| 207 const std::string& ip_str, | 193 const std::string& ip_str, |
| 208 size_t prefix_size) { | 194 size_t prefix_size) { |
| 209 const std::string full_hash_str = HashedIpPrefix(ip_str, prefix_size); | 195 const std::string full_hash_str = HashedIpPrefix(ip_str, prefix_size); |
| 210 EXPECT_EQ(sizeof(SBFullHash), full_hash_str.size()); | 196 EXPECT_EQ(sizeof(SBFullHash), full_hash_str.size()); |
| 211 SBFullHash full_hash; | 197 SBFullHash full_hash; |
| 212 std::memcpy(&(full_hash.full_hash), full_hash_str.data(), sizeof(SBFullHash)); | 198 std::memcpy(&(full_hash.full_hash), full_hash_str.data(), sizeof(SBFullHash)); |
| 213 return BuildChunk(chunk_number, safe_browsing::ChunkData::ADD, | 199 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::FULL_32B, |
| 214 safe_browsing::ChunkData::FULL_32B, | 200 &full_hash, sizeof(full_hash), std::vector<int>()); |
| 215 &full_hash, sizeof(full_hash), | |
| 216 std::vector<int>()); | |
| 217 } | 201 } |
| 218 | 202 |
| 219 // Prevent DCHECK from killing tests. | 203 // Prevent DCHECK from killing tests. |
| 220 // TODO(shess): Pawel disputes the use of this, so the test which uses | 204 // TODO(shess): Pawel disputes the use of this, so the test which uses |
| 221 // it is DISABLED. http://crbug.com/56448 | 205 // it is DISABLED. http://crbug.com/56448 |
| 222 class ScopedLogMessageIgnorer { | 206 class ScopedLogMessageIgnorer { |
| 223 public: | 207 public: |
| 224 ScopedLogMessageIgnorer() { | 208 ScopedLogMessageIgnorer() { |
| 225 logging::SetLogMessageHandler(&LogMessageIgnorer); | 209 logging::SetLogMessageHandler(&LogMessageIgnorer); |
| 226 } | 210 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 242 fprintf(stderr, "%s", msg.c_str()); | 226 fprintf(stderr, "%s", msg.c_str()); |
| 243 fflush(stderr); | 227 fflush(stderr); |
| 244 } | 228 } |
| 245 return true; | 229 return true; |
| 246 } | 230 } |
| 247 | 231 |
| 248 return false; | 232 return false; |
| 249 } | 233 } |
| 250 }; | 234 }; |
| 251 | 235 |
| 252 } // namespace | |
| 253 | |
| 254 class SafeBrowsingDatabaseTest : public PlatformTest { | 236 class SafeBrowsingDatabaseTest : public PlatformTest { |
| 255 public: | 237 public: |
| 256 SafeBrowsingDatabaseTest() : task_runner_(new base::TestSimpleTaskRunner) {} | 238 SafeBrowsingDatabaseTest() : task_runner_(new base::TestSimpleTaskRunner) {} |
| 257 | 239 |
| 258 void SetUp() override { | 240 void SetUp() override { |
| 259 PlatformTest::SetUp(); | 241 PlatformTest::SetUp(); |
| 260 | 242 |
| 261 // Setup a database in a temporary directory. | 243 // Setup a database in a temporary directory. |
| 262 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 244 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 263 database_filename_ = | 245 database_filename_ = |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 // Tests retrieving list name information. | 327 // Tests retrieving list name information. |
| 346 TEST_F(SafeBrowsingDatabaseTest, BrowseListsInfo) { | 328 TEST_F(SafeBrowsingDatabaseTest, BrowseListsInfo) { |
| 347 std::vector<SBListChunkRanges> lists; | 329 std::vector<SBListChunkRanges> lists; |
| 348 ScopedVector<SBChunkData> chunks; | 330 ScopedVector<SBChunkData> chunks; |
| 349 | 331 |
| 350 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); | 332 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); |
| 351 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); | 333 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); |
| 352 chunks.push_back(AddChunkPrefixValue(3, "www.whatever.com/malware.html")); | 334 chunks.push_back(AddChunkPrefixValue(3, "www.whatever.com/malware.html")); |
| 353 | 335 |
| 354 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 336 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 355 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 337 database_->InsertChunks(kMalwareList, chunks.get()); |
| 356 database_->UpdateFinished(true); | 338 database_->UpdateFinished(true); |
| 357 | 339 |
| 358 GetListsInfo(&lists); | 340 GetListsInfo(&lists); |
| 359 ASSERT_LE(1U, lists.size()); | 341 ASSERT_LE(1U, lists.size()); |
| 360 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 342 EXPECT_EQ(kMalwareList, lists[0].name); |
| 361 EXPECT_EQ("1-3", lists[0].adds); | 343 EXPECT_EQ("1-3", lists[0].adds); |
| 362 EXPECT_TRUE(lists[0].subs.empty()); | 344 EXPECT_TRUE(lists[0].subs.empty()); |
| 363 | 345 |
| 364 // Insert a malware sub chunk. | 346 // Insert a malware sub chunk. |
| 365 chunks.clear(); | 347 chunks.clear(); |
| 366 chunks.push_back(SubChunkPrefixValue(7, "www.subbed.com/noteveil1.html", 19)); | 348 chunks.push_back(SubChunkPrefixValue(7, "www.subbed.com/noteveil1.html", 19)); |
| 367 | 349 |
| 368 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 350 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 369 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 351 database_->InsertChunks(kMalwareList, chunks.get()); |
| 370 database_->UpdateFinished(true); | 352 database_->UpdateFinished(true); |
| 371 | 353 |
| 372 GetListsInfo(&lists); | 354 GetListsInfo(&lists); |
| 373 ASSERT_LE(1U, lists.size()); | 355 ASSERT_LE(1U, lists.size()); |
| 374 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 356 EXPECT_EQ(kMalwareList, lists[0].name); |
| 375 EXPECT_EQ("1-3", lists[0].adds); | 357 EXPECT_EQ("1-3", lists[0].adds); |
| 376 EXPECT_EQ("7", lists[0].subs); | 358 EXPECT_EQ("7", lists[0].subs); |
| 377 if (lists.size() == 2) { | 359 if (lists.size() == 2) { |
| 378 // Old style database won't have the second entry since it creates the lists | 360 // 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 | 361 // when it receives an update containing that list. The filter-based |
| 380 // database has these values hard coded. | 362 // database has these values hard coded. |
| 381 EXPECT_EQ(safe_browsing::kPhishingList, lists[1].name); | 363 EXPECT_EQ(kPhishingList, lists[1].name); |
| 382 EXPECT_TRUE(lists[1].adds.empty()); | 364 EXPECT_TRUE(lists[1].adds.empty()); |
| 383 EXPECT_TRUE(lists[1].subs.empty()); | 365 EXPECT_TRUE(lists[1].subs.empty()); |
| 384 } | 366 } |
| 385 | 367 |
| 386 // Add phishing chunks. | 368 // Add phishing chunks. |
| 387 chunks.clear(); | 369 chunks.clear(); |
| 388 chunks.push_back(AddChunkPrefixValue(47, "www.evil.com/phishing.html")); | 370 chunks.push_back(AddChunkPrefixValue(47, "www.evil.com/phishing.html")); |
| 389 chunks.push_back( | 371 chunks.push_back( |
| 390 SubChunkPrefixValue(200, "www.phishy.com/notevil1.html", 1999)); | 372 SubChunkPrefixValue(200, "www.phishy.com/notevil1.html", 1999)); |
| 391 chunks.push_back( | 373 chunks.push_back( |
| 392 SubChunkPrefixValue(201, "www.phishy2.com/notevil1.html", 1999)); | 374 SubChunkPrefixValue(201, "www.phishy2.com/notevil1.html", 1999)); |
| 393 | 375 |
| 394 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 376 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 395 database_->InsertChunks(safe_browsing::kPhishingList, chunks.get()); | 377 database_->InsertChunks(kPhishingList, chunks.get()); |
| 396 database_->UpdateFinished(true); | 378 database_->UpdateFinished(true); |
| 397 | 379 |
| 398 GetListsInfo(&lists); | 380 GetListsInfo(&lists); |
| 399 ASSERT_LE(2U, lists.size()); | 381 ASSERT_LE(2U, lists.size()); |
| 400 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 382 EXPECT_EQ(kMalwareList, lists[0].name); |
| 401 EXPECT_EQ("1-3", lists[0].adds); | 383 EXPECT_EQ("1-3", lists[0].adds); |
| 402 EXPECT_EQ("7", lists[0].subs); | 384 EXPECT_EQ("7", lists[0].subs); |
| 403 EXPECT_EQ(safe_browsing::kPhishingList, lists[1].name); | 385 EXPECT_EQ(kPhishingList, lists[1].name); |
| 404 EXPECT_EQ("47", lists[1].adds); | 386 EXPECT_EQ("47", lists[1].adds); |
| 405 EXPECT_EQ("200-201", lists[1].subs); | 387 EXPECT_EQ("200-201", lists[1].subs); |
| 406 } | 388 } |
| 407 | 389 |
| 408 TEST_F(SafeBrowsingDatabaseTest, ListNames) { | 390 TEST_F(SafeBrowsingDatabaseTest, ListNames) { |
| 409 ScopedVector<SBChunkData> chunks; | 391 ScopedVector<SBChunkData> chunks; |
| 410 | 392 |
| 411 std::vector<SBListChunkRanges> lists; | 393 std::vector<SBListChunkRanges> lists; |
| 412 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 394 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 413 | 395 |
| 414 // Insert malware, phish, binurl and bindownload add chunks. | 396 // Insert malware, phish, binurl and bindownload add chunks. |
| 415 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); | 397 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); |
| 416 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 398 database_->InsertChunks(kMalwareList, chunks.get()); |
| 417 | 399 |
| 418 chunks.clear(); | 400 chunks.clear(); |
| 419 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); | 401 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); |
| 420 database_->InsertChunks(safe_browsing::kPhishingList, chunks.get()); | 402 database_->InsertChunks(kPhishingList, chunks.get()); |
| 421 | 403 |
| 422 chunks.clear(); | 404 chunks.clear(); |
| 423 chunks.push_back(AddChunkPrefixValue(3, "www.whatever.com/download.html")); | 405 chunks.push_back(AddChunkPrefixValue(3, "www.whatever.com/download.html")); |
| 424 database_->InsertChunks(safe_browsing::kBinUrlList, chunks.get()); | 406 database_->InsertChunks(kBinUrlList, chunks.get()); |
| 425 | 407 |
| 426 chunks.clear(); | 408 chunks.clear(); |
| 427 chunks.push_back(AddChunkFullHashValue(5, "www.forwhitelist.com/a.html")); | 409 chunks.push_back(AddChunkFullHashValue(5, "www.forwhitelist.com/a.html")); |
| 428 database_->InsertChunks(safe_browsing::kCsdWhiteList, chunks.get()); | 410 database_->InsertChunks(kCsdWhiteList, chunks.get()); |
| 429 | 411 |
| 430 chunks.clear(); | 412 chunks.clear(); |
| 431 chunks.push_back(AddChunkFullHashValue(6, "www.download.com/")); | 413 chunks.push_back(AddChunkFullHashValue(6, "www.download.com/")); |
| 432 database_->InsertChunks(safe_browsing::kDownloadWhiteList, chunks.get()); | 414 database_->InsertChunks(kDownloadWhiteList, chunks.get()); |
| 433 | 415 |
| 434 chunks.clear(); | 416 chunks.clear(); |
| 435 chunks.push_back(AddChunkFullHashValue(7, "www.inclusion.com/")); | 417 chunks.push_back(AddChunkFullHashValue(7, "www.inclusion.com/")); |
| 436 database_->InsertChunks(safe_browsing::kInclusionWhitelist, | 418 database_->InsertChunks(kInclusionWhitelist, |
| 437 chunks.get()); | 419 chunks.get()); |
| 438 | 420 |
| 439 chunks.clear(); | 421 chunks.clear(); |
| 440 chunks.push_back(AddChunkFullHashValue(8, | 422 chunks.push_back(AddChunkFullHashValue(8, |
| 441 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | 423 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 442 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); | 424 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); |
| 443 database_->InsertChunks(safe_browsing::kExtensionBlacklist, | 425 database_->InsertChunks(kExtensionBlacklist, |
| 444 chunks.get()); | 426 chunks.get()); |
| 445 | 427 |
| 446 chunks.clear(); | 428 chunks.clear(); |
| 447 chunks.push_back(AddChunkHashedIpValue(10, "::ffff:192.168.1.0", 120)); | 429 chunks.push_back(AddChunkHashedIpValue(10, "::ffff:192.168.1.0", 120)); |
| 448 database_->InsertChunks(safe_browsing::kIPBlacklist, chunks.get()); | 430 database_->InsertChunks(kIPBlacklist, chunks.get()); |
| 449 | 431 |
| 450 chunks.clear(); | 432 chunks.clear(); |
| 451 chunks.push_back(AddChunkPrefixValue(11, "www.unwanted.com/software.html")); | 433 chunks.push_back(AddChunkPrefixValue(11, "www.unwanted.com/software.html")); |
| 452 database_->InsertChunks(safe_browsing::kUnwantedUrlList, chunks.get()); | 434 database_->InsertChunks(kUnwantedUrlList, chunks.get()); |
| 453 | 435 |
| 454 database_->UpdateFinished(true); | 436 database_->UpdateFinished(true); |
| 455 | 437 |
| 456 GetListsInfo(&lists); | 438 GetListsInfo(&lists); |
| 457 ASSERT_EQ(9U, lists.size()); | 439 ASSERT_EQ(9U, lists.size()); |
| 458 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 440 EXPECT_EQ(kMalwareList, lists[0].name); |
| 459 EXPECT_EQ("1", lists[0].adds); | 441 EXPECT_EQ("1", lists[0].adds); |
| 460 EXPECT_TRUE(lists[0].subs.empty()); | 442 EXPECT_TRUE(lists[0].subs.empty()); |
| 461 EXPECT_EQ(safe_browsing::kPhishingList, lists[1].name); | 443 EXPECT_EQ(kPhishingList, lists[1].name); |
| 462 EXPECT_EQ("2", lists[1].adds); | 444 EXPECT_EQ("2", lists[1].adds); |
| 463 EXPECT_TRUE(lists[1].subs.empty()); | 445 EXPECT_TRUE(lists[1].subs.empty()); |
| 464 EXPECT_EQ(safe_browsing::kBinUrlList, lists[2].name); | 446 EXPECT_EQ(kBinUrlList, lists[2].name); |
| 465 EXPECT_EQ("3", lists[2].adds); | 447 EXPECT_EQ("3", lists[2].adds); |
| 466 EXPECT_TRUE(lists[2].subs.empty()); | 448 EXPECT_TRUE(lists[2].subs.empty()); |
| 467 EXPECT_EQ(safe_browsing::kCsdWhiteList, lists[3].name); | 449 EXPECT_EQ(kCsdWhiteList, lists[3].name); |
| 468 EXPECT_EQ("5", lists[3].adds); | 450 EXPECT_EQ("5", lists[3].adds); |
| 469 EXPECT_TRUE(lists[3].subs.empty()); | 451 EXPECT_TRUE(lists[3].subs.empty()); |
| 470 EXPECT_EQ(safe_browsing::kDownloadWhiteList, lists[4].name); | 452 EXPECT_EQ(kDownloadWhiteList, lists[4].name); |
| 471 EXPECT_EQ("6", lists[4].adds); | 453 EXPECT_EQ("6", lists[4].adds); |
| 472 EXPECT_TRUE(lists[4].subs.empty()); | 454 EXPECT_TRUE(lists[4].subs.empty()); |
| 473 EXPECT_EQ(safe_browsing::kInclusionWhitelist, lists[5].name); | 455 EXPECT_EQ(kInclusionWhitelist, lists[5].name); |
| 474 EXPECT_EQ("7", lists[5].adds); | 456 EXPECT_EQ("7", lists[5].adds); |
| 475 EXPECT_TRUE(lists[5].subs.empty()); | 457 EXPECT_TRUE(lists[5].subs.empty()); |
| 476 EXPECT_EQ(safe_browsing::kExtensionBlacklist, lists[6].name); | 458 EXPECT_EQ(kExtensionBlacklist, lists[6].name); |
| 477 EXPECT_EQ("8", lists[6].adds); | 459 EXPECT_EQ("8", lists[6].adds); |
| 478 EXPECT_TRUE(lists[6].subs.empty()); | 460 EXPECT_TRUE(lists[6].subs.empty()); |
| 479 EXPECT_EQ(safe_browsing::kIPBlacklist, lists[7].name); | 461 EXPECT_EQ(kIPBlacklist, lists[7].name); |
| 480 EXPECT_EQ("10", lists[7].adds); | 462 EXPECT_EQ("10", lists[7].adds); |
| 481 EXPECT_TRUE(lists[7].subs.empty()); | 463 EXPECT_TRUE(lists[7].subs.empty()); |
| 482 EXPECT_EQ(safe_browsing::kUnwantedUrlList, lists[8].name); | 464 EXPECT_EQ(kUnwantedUrlList, lists[8].name); |
| 483 EXPECT_EQ("11", lists[8].adds); | 465 EXPECT_EQ("11", lists[8].adds); |
| 484 EXPECT_TRUE(lists[8].subs.empty()); | 466 EXPECT_TRUE(lists[8].subs.empty()); |
| 485 | 467 |
| 486 database_.reset(); | 468 database_.reset(); |
| 487 } | 469 } |
| 488 | 470 |
| 489 // Checks database reading and writing for browse and unwanted PrefixSets. | 471 // Checks database reading and writing for browse and unwanted PrefixSets. |
| 490 TEST_F(SafeBrowsingDatabaseTest, BrowseAndUnwantedDatabasesAndPrefixSets) { | 472 TEST_F(SafeBrowsingDatabaseTest, BrowseAndUnwantedDatabasesAndPrefixSets) { |
| 491 struct TestCase { | 473 struct TestCase { |
| 492 using TestListContainsBadUrl = bool (SafeBrowsingDatabase::*)( | 474 using TestListContainsBadUrl = bool (SafeBrowsingDatabase::*)( |
| 493 const GURL& url, | 475 const GURL& url, |
| 494 std::vector<SBPrefix>* prefix_hits, | 476 std::vector<SBPrefix>* prefix_hits, |
| 495 std::vector<SBFullHashResult>* cache_hits); | 477 std::vector<SBFullHashResult>* cache_hits); |
| 496 | 478 |
| 497 const char* test_list_name; | 479 const char* test_list_name; |
| 498 size_t expected_list_index; | 480 size_t expected_list_index; |
| 499 TestListContainsBadUrl test_list_contains_bad_url; | 481 TestListContainsBadUrl test_list_contains_bad_url; |
| 500 } const kTestCases[] { | 482 } const kTestCases[]{ |
| 501 { safe_browsing::kMalwareList, 0U, | 483 {kMalwareList, 0U, &SafeBrowsingDatabase::ContainsBrowseUrl}, |
| 502 &SafeBrowsingDatabase::ContainsBrowseUrl }, | 484 {kPhishingList, 1U, &SafeBrowsingDatabase::ContainsBrowseUrl}, |
| 503 { safe_browsing::kPhishingList, 1U, | 485 {kUnwantedUrlList, 8U, |
| 504 &SafeBrowsingDatabase::ContainsBrowseUrl }, | 486 &SafeBrowsingDatabase::ContainsUnwantedSoftwareUrl}, |
| 505 { safe_browsing::kUnwantedUrlList, 8U, | |
| 506 &SafeBrowsingDatabase::ContainsUnwantedSoftwareUrl }, | |
| 507 }; | 487 }; |
| 508 | 488 |
| 509 for (const auto& test_case : kTestCases) { | 489 for (const auto& test_case : kTestCases) { |
| 510 SCOPED_TRACE(std::string("Tested list at fault => ") + | 490 SCOPED_TRACE(std::string("Tested list at fault => ") + |
| 511 test_case.test_list_name); | 491 test_case.test_list_name); |
| 512 | 492 |
| 513 std::vector<SBListChunkRanges> lists; | 493 std::vector<SBListChunkRanges> lists; |
| 514 ScopedVector<SBChunkData> chunks; | 494 ScopedVector<SBChunkData> chunks; |
| 515 | 495 |
| 516 chunks.push_back(AddChunkPrefix2Value(1, | 496 chunks.push_back(AddChunkPrefix2Value(1, |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 | 731 |
| 752 // Populate with a couple of normal chunks. | 732 // Populate with a couple of normal chunks. |
| 753 chunks.push_back(AddChunkPrefix2Value(1, | 733 chunks.push_back(AddChunkPrefix2Value(1, |
| 754 "www.test.com/test1.html", | 734 "www.test.com/test1.html", |
| 755 "www.test.com/test2.html")); | 735 "www.test.com/test2.html")); |
| 756 chunks.push_back(AddChunkPrefix2Value(10, | 736 chunks.push_back(AddChunkPrefix2Value(10, |
| 757 "www.random.com/random1.html", | 737 "www.random.com/random1.html", |
| 758 "www.random.com/random2.html")); | 738 "www.random.com/random2.html")); |
| 759 | 739 |
| 760 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 740 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 761 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 741 database_->InsertChunks(kMalwareList, chunks.get()); |
| 762 database_->UpdateFinished(true); | 742 database_->UpdateFinished(true); |
| 763 | 743 |
| 764 // Add an empty ADD and SUB chunk. | 744 // Add an empty ADD and SUB chunk. |
| 765 GetListsInfo(&lists); | 745 GetListsInfo(&lists); |
| 766 ASSERT_LE(1U, lists.size()); | 746 ASSERT_LE(1U, lists.size()); |
| 767 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 747 EXPECT_EQ(kMalwareList, lists[0].name); |
| 768 EXPECT_EQ("1,10", lists[0].adds); | 748 EXPECT_EQ("1,10", lists[0].adds); |
| 769 EXPECT_TRUE(lists[0].subs.empty()); | 749 EXPECT_TRUE(lists[0].subs.empty()); |
| 770 | 750 |
| 771 chunks.clear(); | 751 chunks.clear(); |
| 772 chunks.push_back(BuildChunk(19, safe_browsing::ChunkData::ADD, | 752 chunks.push_back(BuildChunk(19, ChunkData::ADD, ChunkData::PREFIX_4B, NULL, 0, |
| 773 safe_browsing::ChunkData::PREFIX_4B, | 753 std::vector<int>())); |
| 774 NULL, 0, std::vector<int>())); | 754 chunks.push_back(BuildChunk(7, ChunkData::SUB, ChunkData::PREFIX_4B, NULL, 0, |
| 775 chunks.push_back(BuildChunk(7, safe_browsing::ChunkData::SUB, | 755 std::vector<int>())); |
| 776 safe_browsing::ChunkData::PREFIX_4B, | |
| 777 NULL, 0, std::vector<int>())); | |
| 778 | 756 |
| 779 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 757 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 780 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 758 database_->InsertChunks(kMalwareList, chunks.get()); |
| 781 database_->UpdateFinished(true); | 759 database_->UpdateFinished(true); |
| 782 | 760 |
| 783 GetListsInfo(&lists); | 761 GetListsInfo(&lists); |
| 784 ASSERT_LE(1U, lists.size()); | 762 ASSERT_LE(1U, lists.size()); |
| 785 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 763 EXPECT_EQ(kMalwareList, lists[0].name); |
| 786 EXPECT_EQ("1,10,19", lists[0].adds); | 764 EXPECT_EQ("1,10,19", lists[0].adds); |
| 787 EXPECT_EQ("7", lists[0].subs); | 765 EXPECT_EQ("7", lists[0].subs); |
| 788 | 766 |
| 789 // Add an empty chunk along with a couple that contain data. This should | 767 // Add an empty chunk along with a couple that contain data. This should |
| 790 // result in the chunk range being reduced in size. | 768 // result in the chunk range being reduced in size. |
| 791 chunks.clear(); | 769 chunks.clear(); |
| 792 chunks.push_back(AddChunkPrefixValue(20, "www.notempty.com/full1.html")); | 770 chunks.push_back(AddChunkPrefixValue(20, "www.notempty.com/full1.html")); |
| 793 chunks.push_back(BuildChunk(21, safe_browsing::ChunkData::ADD, | 771 chunks.push_back(BuildChunk(21, ChunkData::ADD, ChunkData::PREFIX_4B, NULL, 0, |
| 794 safe_browsing::ChunkData::PREFIX_4B, | 772 std::vector<int>())); |
| 795 NULL, 0, std::vector<int>())); | |
| 796 chunks.push_back(AddChunkPrefixValue(22, "www.notempty.com/full2.html")); | 773 chunks.push_back(AddChunkPrefixValue(22, "www.notempty.com/full2.html")); |
| 797 | 774 |
| 798 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 775 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 799 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 776 database_->InsertChunks(kMalwareList, chunks.get()); |
| 800 database_->UpdateFinished(true); | 777 database_->UpdateFinished(true); |
| 801 | 778 |
| 802 std::vector<SBPrefix> prefix_hits; | 779 std::vector<SBPrefix> prefix_hits; |
| 803 std::vector<SBFullHashResult> cache_hits; | 780 std::vector<SBFullHashResult> cache_hits; |
| 804 EXPECT_TRUE(database_->ContainsBrowseUrl( | 781 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 805 GURL("http://www.notempty.com/full1.html"), &prefix_hits, &cache_hits)); | 782 GURL("http://www.notempty.com/full1.html"), &prefix_hits, &cache_hits)); |
| 806 EXPECT_TRUE(database_->ContainsBrowseUrl( | 783 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 807 GURL("http://www.notempty.com/full2.html"), &prefix_hits, &cache_hits)); | 784 GURL("http://www.notempty.com/full2.html"), &prefix_hits, &cache_hits)); |
| 808 | 785 |
| 809 GetListsInfo(&lists); | 786 GetListsInfo(&lists); |
| 810 ASSERT_LE(1U, lists.size()); | 787 ASSERT_LE(1U, lists.size()); |
| 811 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 788 EXPECT_EQ(kMalwareList, lists[0].name); |
| 812 EXPECT_EQ("1,10,19-22", lists[0].adds); | 789 EXPECT_EQ("1,10,19-22", lists[0].adds); |
| 813 EXPECT_EQ("7", lists[0].subs); | 790 EXPECT_EQ("7", lists[0].subs); |
| 814 | 791 |
| 815 // Handle AddDel and SubDel commands for empty chunks. | 792 // Handle AddDel and SubDel commands for empty chunks. |
| 816 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 793 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 817 AddDelChunk(safe_browsing::kMalwareList, 21); | 794 AddDelChunk(kMalwareList, 21); |
| 818 database_->UpdateFinished(true); | 795 database_->UpdateFinished(true); |
| 819 | 796 |
| 820 GetListsInfo(&lists); | 797 GetListsInfo(&lists); |
| 821 ASSERT_LE(1U, lists.size()); | 798 ASSERT_LE(1U, lists.size()); |
| 822 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 799 EXPECT_EQ(kMalwareList, lists[0].name); |
| 823 EXPECT_EQ("1,10,19-20,22", lists[0].adds); | 800 EXPECT_EQ("1,10,19-20,22", lists[0].adds); |
| 824 EXPECT_EQ("7", lists[0].subs); | 801 EXPECT_EQ("7", lists[0].subs); |
| 825 | 802 |
| 826 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 803 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 827 SubDelChunk(safe_browsing::kMalwareList, 7); | 804 SubDelChunk(kMalwareList, 7); |
| 828 database_->UpdateFinished(true); | 805 database_->UpdateFinished(true); |
| 829 | 806 |
| 830 GetListsInfo(&lists); | 807 GetListsInfo(&lists); |
| 831 ASSERT_LE(1U, lists.size()); | 808 ASSERT_LE(1U, lists.size()); |
| 832 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 809 EXPECT_EQ(kMalwareList, lists[0].name); |
| 833 EXPECT_EQ("1,10,19-20,22", lists[0].adds); | 810 EXPECT_EQ("1,10,19-20,22", lists[0].adds); |
| 834 EXPECT_TRUE(lists[0].subs.empty()); | 811 EXPECT_TRUE(lists[0].subs.empty()); |
| 835 } | 812 } |
| 836 | 813 |
| 837 // Utility function for setting up the database for the caching test. | 814 // Utility function for setting up the database for the caching test. |
| 838 void SafeBrowsingDatabaseTest::PopulateDatabaseForCacheTest() { | 815 void SafeBrowsingDatabaseTest::PopulateDatabaseForCacheTest() { |
| 839 // Add a couple prefixes. | 816 // Add a couple prefixes. |
| 840 ScopedVector<SBChunkData> chunks; | 817 ScopedVector<SBChunkData> chunks; |
| 841 chunks.push_back(AddChunkPrefix2Value(1, | 818 chunks.push_back(AddChunkPrefix2Value(1, |
| 842 "www.evil.com/phishing.html", | 819 "www.evil.com/phishing.html", |
| 843 "www.evil.com/malware.html")); | 820 "www.evil.com/malware.html")); |
| 844 | 821 |
| 845 std::vector<SBListChunkRanges> lists; | 822 std::vector<SBListChunkRanges> lists; |
| 846 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 823 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 847 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 824 database_->InsertChunks(kMalwareList, chunks.get()); |
| 848 database_->UpdateFinished(true); | 825 database_->UpdateFinished(true); |
| 849 | 826 |
| 850 // Cache should be cleared after updating. | 827 // Cache should be cleared after updating. |
| 851 EXPECT_TRUE( | 828 EXPECT_TRUE( |
| 852 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); | 829 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
| 853 | 830 |
| 854 SBFullHashResult full_hash; | 831 SBFullHashResult full_hash; |
| 855 full_hash.list_id = safe_browsing::MALWARE; | 832 full_hash.list_id = MALWARE; |
| 856 | 833 |
| 857 std::vector<SBFullHashResult> results; | 834 std::vector<SBFullHashResult> results; |
| 858 std::vector<SBPrefix> prefixes; | 835 std::vector<SBPrefix> prefixes; |
| 859 | 836 |
| 860 // Add a fullhash result for each prefix. | 837 // Add a fullhash result for each prefix. |
| 861 full_hash.hash = | 838 full_hash.hash = |
| 862 safe_browsing::SBFullHashForString("www.evil.com/phishing.html"); | 839 SBFullHashForString("www.evil.com/phishing.html"); |
| 863 results.push_back(full_hash); | 840 results.push_back(full_hash); |
| 864 prefixes.push_back(full_hash.hash.prefix); | 841 prefixes.push_back(full_hash.hash.prefix); |
| 865 | 842 |
| 866 full_hash.hash = | 843 full_hash.hash = |
| 867 safe_browsing::SBFullHashForString("www.evil.com/malware.html"); | 844 SBFullHashForString("www.evil.com/malware.html"); |
| 868 results.push_back(full_hash); | 845 results.push_back(full_hash); |
| 869 prefixes.push_back(full_hash.hash.prefix); | 846 prefixes.push_back(full_hash.hash.prefix); |
| 870 | 847 |
| 871 database_->CacheHashResults(prefixes, results, kCacheLifetime); | 848 database_->CacheHashResults(prefixes, results, kCacheLifetime); |
| 872 } | 849 } |
| 873 | 850 |
| 874 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { | 851 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { |
| 875 PopulateDatabaseForCacheTest(); | 852 PopulateDatabaseForCacheTest(); |
| 876 | 853 |
| 877 // We should have both full hashes in the cache. | 854 // We should have both full hashes in the cache. |
| 878 EXPECT_EQ(2U, | 855 EXPECT_EQ(2U, |
| 879 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->size()); | 856 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->size()); |
| 880 | 857 |
| 881 // Test the cache lookup for the first prefix. | 858 // Test the cache lookup for the first prefix. |
| 882 std::vector<SBPrefix> prefix_hits; | 859 std::vector<SBPrefix> prefix_hits; |
| 883 std::vector<SBFullHashResult> cache_hits; | 860 std::vector<SBFullHashResult> cache_hits; |
| 884 EXPECT_TRUE(database_->ContainsBrowseUrl( | 861 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 885 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 862 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 886 EXPECT_TRUE(prefix_hits.empty()); | 863 EXPECT_TRUE(prefix_hits.empty()); |
| 887 ASSERT_EQ(1U, cache_hits.size()); | 864 ASSERT_EQ(1U, cache_hits.size()); |
| 888 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 865 EXPECT_TRUE(SBFullHashEqual( |
| 889 cache_hits[0].hash, | 866 cache_hits[0].hash, |
| 890 safe_browsing::SBFullHashForString("www.evil.com/phishing.html"))); | 867 SBFullHashForString("www.evil.com/phishing.html"))); |
| 891 | 868 |
| 892 prefix_hits.clear(); | 869 prefix_hits.clear(); |
| 893 cache_hits.clear(); | 870 cache_hits.clear(); |
| 894 | 871 |
| 895 // Test the cache lookup for the second prefix. | 872 // Test the cache lookup for the second prefix. |
| 896 EXPECT_TRUE(database_->ContainsBrowseUrl( | 873 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 897 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 874 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 898 EXPECT_TRUE(prefix_hits.empty()); | 875 EXPECT_TRUE(prefix_hits.empty()); |
| 899 ASSERT_EQ(1U, cache_hits.size()); | 876 ASSERT_EQ(1U, cache_hits.size()); |
| 900 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 877 EXPECT_TRUE(SBFullHashEqual( |
| 901 cache_hits[0].hash, | 878 cache_hits[0].hash, |
| 902 safe_browsing::SBFullHashForString("www.evil.com/malware.html"))); | 879 SBFullHashForString("www.evil.com/malware.html"))); |
| 903 | 880 |
| 904 prefix_hits.clear(); | 881 prefix_hits.clear(); |
| 905 cache_hits.clear(); | 882 cache_hits.clear(); |
| 906 | 883 |
| 907 // Test removing a prefix via a sub chunk. | 884 // Test removing a prefix via a sub chunk. |
| 908 ScopedVector<SBChunkData> chunks; | 885 ScopedVector<SBChunkData> chunks; |
| 909 chunks.push_back(SubChunkPrefixValue(2, "www.evil.com/phishing.html", 1)); | 886 chunks.push_back(SubChunkPrefixValue(2, "www.evil.com/phishing.html", 1)); |
| 910 | 887 |
| 911 std::vector<SBListChunkRanges> lists; | 888 std::vector<SBListChunkRanges> lists; |
| 912 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 889 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 913 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 890 database_->InsertChunks(kMalwareList, chunks.get()); |
| 914 database_->UpdateFinished(true); | 891 database_->UpdateFinished(true); |
| 915 | 892 |
| 916 // This prefix should still be there, but cached fullhash should be gone. | 893 // This prefix should still be there, but cached fullhash should be gone. |
| 917 EXPECT_TRUE(database_->ContainsBrowseUrl( | 894 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 918 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 895 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 919 ASSERT_EQ(1U, prefix_hits.size()); | 896 ASSERT_EQ(1U, prefix_hits.size()); |
| 920 EXPECT_EQ(SBPrefixForString("www.evil.com/malware.html"), prefix_hits[0]); | 897 EXPECT_EQ(SBPrefixForString("www.evil.com/malware.html"), prefix_hits[0]); |
| 921 EXPECT_TRUE(cache_hits.empty()); | 898 EXPECT_TRUE(cache_hits.empty()); |
| 922 prefix_hits.clear(); | 899 prefix_hits.clear(); |
| 923 cache_hits.clear(); | 900 cache_hits.clear(); |
| 924 | 901 |
| 925 // This prefix should be gone. | 902 // This prefix should be gone. |
| 926 EXPECT_FALSE(database_->ContainsBrowseUrl( | 903 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 927 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 904 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 928 prefix_hits.clear(); | 905 prefix_hits.clear(); |
| 929 cache_hits.clear(); | 906 cache_hits.clear(); |
| 930 | 907 |
| 931 // Test that an AddDel for the original chunk removes the last cached entry. | 908 // Test that an AddDel for the original chunk removes the last cached entry. |
| 932 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 909 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 933 AddDelChunk(safe_browsing::kMalwareList, 1); | 910 AddDelChunk(kMalwareList, 1); |
| 934 database_->UpdateFinished(true); | 911 database_->UpdateFinished(true); |
| 935 EXPECT_FALSE(database_->ContainsBrowseUrl( | 912 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 936 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 913 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 937 EXPECT_TRUE( | 914 EXPECT_TRUE( |
| 938 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); | 915 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
| 939 prefix_hits.clear(); | 916 prefix_hits.clear(); |
| 940 cache_hits.clear(); | 917 cache_hits.clear(); |
| 941 | 918 |
| 942 // Test that the cache won't return expired values. First we have to adjust | 919 // 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 | 920 // 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. | 942 // This entry should still exist. |
| 966 EXPECT_TRUE(database_->ContainsBrowseUrl( | 943 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 967 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); | 944 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 968 EXPECT_TRUE(prefix_hits.empty()); | 945 EXPECT_TRUE(prefix_hits.empty()); |
| 969 EXPECT_EQ(1U, cache_hits.size()); | 946 EXPECT_EQ(1U, cache_hits.size()); |
| 970 | 947 |
| 971 // Testing prefix miss caching. First, we clear out the existing database, | 948 // Testing prefix miss caching. First, we clear out the existing database, |
| 972 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate | 949 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate |
| 973 // chunks. | 950 // chunks. |
| 974 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 951 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 975 AddDelChunk(safe_browsing::kMalwareList, 1); | 952 AddDelChunk(kMalwareList, 1); |
| 976 database_->UpdateFinished(true); | 953 database_->UpdateFinished(true); |
| 977 | 954 |
| 978 // Cache should be cleared after updating. | 955 // Cache should be cleared after updating. |
| 979 EXPECT_TRUE(hash_cache->empty()); | 956 EXPECT_TRUE(hash_cache->empty()); |
| 980 | 957 |
| 981 std::vector<SBPrefix> prefix_misses; | 958 std::vector<SBPrefix> prefix_misses; |
| 982 std::vector<SBFullHashResult> empty_full_hash; | 959 std::vector<SBFullHashResult> empty_full_hash; |
| 983 prefix_misses.push_back(SBPrefixForString("http://www.bad.com/malware.html")); | 960 prefix_misses.push_back(SBPrefixForString("http://www.bad.com/malware.html")); |
| 984 prefix_misses.push_back( | 961 prefix_misses.push_back( |
| 985 SBPrefixForString("http://www.bad.com/phishing.html")); | 962 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)); | 991 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits)); |
| 1015 prefix_hits.clear(); | 992 prefix_hits.clear(); |
| 1016 cache_hits.clear(); | 993 cache_hits.clear(); |
| 1017 | 994 |
| 1018 // Test receiving a full add chunk. | 995 // Test receiving a full add chunk. |
| 1019 chunks.clear(); | 996 chunks.clear(); |
| 1020 chunks.push_back(AddChunkFullHash2Value(20, | 997 chunks.push_back(AddChunkFullHash2Value(20, |
| 1021 "www.fullevil.com/bad1.html", | 998 "www.fullevil.com/bad1.html", |
| 1022 "www.fullevil.com/bad2.html")); | 999 "www.fullevil.com/bad2.html")); |
| 1023 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1000 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1024 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1001 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1025 database_->UpdateFinished(true); | 1002 database_->UpdateFinished(true); |
| 1026 | 1003 |
| 1027 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1004 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1028 GURL("http://www.fullevil.com/bad1.html"), &prefix_hits, &cache_hits)); | 1005 GURL("http://www.fullevil.com/bad1.html"), &prefix_hits, &cache_hits)); |
| 1029 ASSERT_EQ(1U, prefix_hits.size()); | 1006 ASSERT_EQ(1U, prefix_hits.size()); |
| 1030 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad1.html"), prefix_hits[0]); | 1007 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad1.html"), prefix_hits[0]); |
| 1031 EXPECT_TRUE(cache_hits.empty()); | 1008 EXPECT_TRUE(cache_hits.empty()); |
| 1032 prefix_hits.clear(); | 1009 prefix_hits.clear(); |
| 1033 cache_hits.clear(); | 1010 cache_hits.clear(); |
| 1034 | 1011 |
| 1035 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1012 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1036 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); | 1013 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); |
| 1037 ASSERT_EQ(1U, prefix_hits.size()); | 1014 ASSERT_EQ(1U, prefix_hits.size()); |
| 1038 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad2.html"), prefix_hits[0]); | 1015 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad2.html"), prefix_hits[0]); |
| 1039 EXPECT_TRUE(cache_hits.empty()); | 1016 EXPECT_TRUE(cache_hits.empty()); |
| 1040 prefix_hits.clear(); | 1017 prefix_hits.clear(); |
| 1041 cache_hits.clear(); | 1018 cache_hits.clear(); |
| 1042 | 1019 |
| 1043 // Test receiving a full sub chunk, which will remove one of the full adds. | 1020 // Test receiving a full sub chunk, which will remove one of the full adds. |
| 1044 chunks.clear(); | 1021 chunks.clear(); |
| 1045 chunks.push_back(SubChunkFullHashValue(200, | 1022 chunks.push_back(SubChunkFullHashValue(200, |
| 1046 "www.fullevil.com/bad1.html", | 1023 "www.fullevil.com/bad1.html", |
| 1047 20)); | 1024 20)); |
| 1048 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1025 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1049 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1026 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1050 database_->UpdateFinished(true); | 1027 database_->UpdateFinished(true); |
| 1051 | 1028 |
| 1052 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1029 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1053 GURL("http://www.fullevil.com/bad1.html"), &prefix_hits, &cache_hits)); | 1030 GURL("http://www.fullevil.com/bad1.html"), &prefix_hits, &cache_hits)); |
| 1054 | 1031 |
| 1055 // There should be one remaining full add. | 1032 // There should be one remaining full add. |
| 1056 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1033 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1057 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); | 1034 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); |
| 1058 ASSERT_EQ(1U, prefix_hits.size()); | 1035 ASSERT_EQ(1U, prefix_hits.size()); |
| 1059 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad2.html"), prefix_hits[0]); | 1036 EXPECT_EQ(SBPrefixForString("www.fullevil.com/bad2.html"), prefix_hits[0]); |
| 1060 EXPECT_TRUE(cache_hits.empty()); | 1037 EXPECT_TRUE(cache_hits.empty()); |
| 1061 prefix_hits.clear(); | 1038 prefix_hits.clear(); |
| 1062 cache_hits.clear(); | 1039 cache_hits.clear(); |
| 1063 | 1040 |
| 1064 // Now test an AddDel for the remaining full add. | 1041 // Now test an AddDel for the remaining full add. |
| 1065 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1042 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1066 AddDelChunk(safe_browsing::kMalwareList, 20); | 1043 AddDelChunk(kMalwareList, 20); |
| 1067 database_->UpdateFinished(true); | 1044 database_->UpdateFinished(true); |
| 1068 | 1045 |
| 1069 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1046 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1070 GURL("http://www.fullevil.com/bad1.html"), &prefix_hits, &cache_hits)); | 1047 GURL("http://www.fullevil.com/bad1.html"), &prefix_hits, &cache_hits)); |
| 1071 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1048 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1072 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); | 1049 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); |
| 1073 | 1050 |
| 1074 // Add a fullhash which has a prefix collision for a known url. | 1051 // Add a fullhash which has a prefix collision for a known url. |
| 1075 static const char kExampleFine[] = "www.example.com/fine.html"; | 1052 static const char kExampleFine[] = "www.example.com/fine.html"; |
| 1076 static const char kExampleCollision[] = | 1053 static const char kExampleCollision[] = |
| 1077 "www.example.com/3123364814/malware.htm"; | 1054 "www.example.com/3123364814/malware.htm"; |
| 1078 ASSERT_EQ(SBPrefixForString(kExampleFine), | 1055 ASSERT_EQ(SBPrefixForString(kExampleFine), |
| 1079 SBPrefixForString(kExampleCollision)); | 1056 SBPrefixForString(kExampleCollision)); |
| 1080 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1057 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1081 { | 1058 { |
| 1082 ScopedVector<SBChunkData> chunks; | 1059 ScopedVector<SBChunkData> chunks; |
| 1083 chunks.push_back(AddChunkPrefixValue(21, kExampleCollision)); | 1060 chunks.push_back(AddChunkPrefixValue(21, kExampleCollision)); |
| 1084 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1061 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1085 } | 1062 } |
| 1086 database_->UpdateFinished(true); | 1063 database_->UpdateFinished(true); |
| 1087 | 1064 |
| 1088 // Expect a prefix hit due to the collision between |kExampleFine| and | 1065 // Expect a prefix hit due to the collision between |kExampleFine| and |
| 1089 // |kExampleCollision|. | 1066 // |kExampleCollision|. |
| 1090 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1067 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1091 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); | 1068 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); |
| 1092 ASSERT_EQ(1U, prefix_hits.size()); | 1069 ASSERT_EQ(1U, prefix_hits.size()); |
| 1093 EXPECT_EQ(SBPrefixForString(kExampleFine), prefix_hits[0]); | 1070 EXPECT_EQ(SBPrefixForString(kExampleFine), prefix_hits[0]); |
| 1094 EXPECT_TRUE(cache_hits.empty()); | 1071 EXPECT_TRUE(cache_hits.empty()); |
| 1095 | 1072 |
| 1096 // Cache gethash response for |kExampleCollision|. | 1073 // Cache gethash response for |kExampleCollision|. |
| 1097 { | 1074 { |
| 1098 SBFullHashResult result; | 1075 SBFullHashResult result; |
| 1099 result.hash = safe_browsing::SBFullHashForString(kExampleCollision); | 1076 result.hash = SBFullHashForString(kExampleCollision); |
| 1100 result.list_id = safe_browsing::MALWARE; | 1077 result.list_id = MALWARE; |
| 1101 database_->CacheHashResults(std::vector<SBPrefix>(1, result.hash.prefix), | 1078 database_->CacheHashResults(std::vector<SBPrefix>(1, result.hash.prefix), |
| 1102 std::vector<SBFullHashResult>(1, result), | 1079 std::vector<SBFullHashResult>(1, result), |
| 1103 kCacheLifetime); | 1080 kCacheLifetime); |
| 1104 } | 1081 } |
| 1105 | 1082 |
| 1106 // The cached response means the collision no longer causes a hit. | 1083 // The cached response means the collision no longer causes a hit. |
| 1107 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1084 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1108 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); | 1085 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); |
| 1109 } | 1086 } |
| 1110 | 1087 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1143 fputc('!', fp); | 1120 fputc('!', fp); |
| 1144 } | 1121 } |
| 1145 fclose(fp); | 1122 fclose(fp); |
| 1146 | 1123 |
| 1147 { | 1124 { |
| 1148 // The following code will cause DCHECKs, so suppress the crashes. | 1125 // The following code will cause DCHECKs, so suppress the crashes. |
| 1149 ScopedLogMessageIgnorer ignorer; | 1126 ScopedLogMessageIgnorer ignorer; |
| 1150 | 1127 |
| 1151 // Start an update. The insert will fail due to corruption. | 1128 // Start an update. The insert will fail due to corruption. |
| 1152 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1129 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1153 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1130 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1154 database_->UpdateFinished(true); | 1131 database_->UpdateFinished(true); |
| 1155 | 1132 |
| 1156 // Database file still exists until the corruption handler has run. | 1133 // Database file still exists until the corruption handler has run. |
| 1157 EXPECT_TRUE(base::PathExists(database_filename_)); | 1134 EXPECT_TRUE(base::PathExists(database_filename_)); |
| 1158 | 1135 |
| 1159 // Flush through the corruption-handler task. | 1136 // Flush through the corruption-handler task. |
| 1160 DVLOG(1) << "Expect failed check on: SafeBrowsing database reset"; | 1137 DVLOG(1) << "Expect failed check on: SafeBrowsing database reset"; |
| 1161 base::MessageLoop::current()->RunUntilIdle(); | 1138 base::MessageLoop::current()->RunUntilIdle(); |
| 1162 } | 1139 } |
| 1163 | 1140 |
| 1164 // Database file should not exist. | 1141 // Database file should not exist. |
| 1165 EXPECT_FALSE(base::PathExists(database_filename_)); | 1142 EXPECT_FALSE(base::PathExists(database_filename_)); |
| 1166 | 1143 |
| 1167 // Run the update again successfully. | 1144 // Run the update again successfully. |
| 1168 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1145 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1169 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1146 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1170 database_->UpdateFinished(true); | 1147 database_->UpdateFinished(true); |
| 1171 EXPECT_TRUE(base::PathExists(database_filename_)); | 1148 EXPECT_TRUE(base::PathExists(database_filename_)); |
| 1172 | 1149 |
| 1173 database_.reset(); | 1150 database_.reset(); |
| 1174 } | 1151 } |
| 1175 | 1152 |
| 1176 // Checks database reading and writing. | 1153 // Checks database reading and writing. |
| 1177 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrlPrefixes) { | 1154 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrlPrefixes) { |
| 1178 const char kEvil1Url1[] = "www.evil1.com/download1/"; | 1155 const char kEvil1Url1[] = "www.evil1.com/download1/"; |
| 1179 const char kEvil1Url2[] = "www.evil1.com/download2.html"; | 1156 const char kEvil1Url2[] = "www.evil1.com/download2.html"; |
| 1180 | 1157 |
| 1181 // Add a simple chunk with one hostkey for download url list. | 1158 // Add a simple chunk with one hostkey for download url list. |
| 1182 ScopedVector<SBChunkData> chunks; | 1159 ScopedVector<SBChunkData> chunks; |
| 1183 chunks.push_back(AddChunkPrefix2Value(1, kEvil1Url1, kEvil1Url2)); | 1160 chunks.push_back(AddChunkPrefix2Value(1, kEvil1Url1, kEvil1Url2)); |
| 1184 | 1161 |
| 1185 std::vector<SBListChunkRanges> lists; | 1162 std::vector<SBListChunkRanges> lists; |
| 1186 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1163 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1187 database_->InsertChunks(safe_browsing::kBinUrlList, chunks.get()); | 1164 database_->InsertChunks(kBinUrlList, chunks.get()); |
| 1188 database_->UpdateFinished(true); | 1165 database_->UpdateFinished(true); |
| 1189 | 1166 |
| 1190 std::vector<SBPrefix> prefix_hits; | 1167 std::vector<SBPrefix> prefix_hits; |
| 1191 std::vector<GURL> urls(1); | 1168 std::vector<GURL> urls(1); |
| 1192 | 1169 |
| 1193 urls[0] = GURL(std::string("http://") + kEvil1Url1); | 1170 urls[0] = GURL(std::string("http://") + kEvil1Url1); |
| 1194 EXPECT_TRUE(ContainsDownloadUrl(urls, &prefix_hits)); | 1171 EXPECT_TRUE(ContainsDownloadUrl(urls, &prefix_hits)); |
| 1195 ASSERT_EQ(1U, prefix_hits.size()); | 1172 ASSERT_EQ(1U, prefix_hits.size()); |
| 1196 EXPECT_EQ(SBPrefixForString(kEvil1Url1), prefix_hits[0]); | 1173 EXPECT_EQ(SBPrefixForString(kEvil1Url1), prefix_hits[0]); |
| 1197 | 1174 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1274 bool TestStrings() const { | 1251 bool TestStrings() const { |
| 1275 return test_list_contains_whitelisted_string != nullptr; | 1252 return test_list_contains_whitelisted_string != nullptr; |
| 1276 } | 1253 } |
| 1277 | 1254 |
| 1278 const char* test_list_name; | 1255 const char* test_list_name; |
| 1279 TestListContainsWhitelistedUrl test_list_contains_whitelisted_url; | 1256 TestListContainsWhitelistedUrl test_list_contains_whitelisted_url; |
| 1280 // Optional test case field, if set the tested whitelist will also be tested | 1257 // Optional test case field, if set the tested whitelist will also be tested |
| 1281 // for strings. | 1258 // for strings. |
| 1282 TestListContainsWhitelistedString test_list_contains_whitelisted_string; | 1259 TestListContainsWhitelistedString test_list_contains_whitelisted_string; |
| 1283 } const kTestCases[]{ | 1260 } const kTestCases[]{ |
| 1284 {safe_browsing::kCsdWhiteList, | 1261 {kCsdWhiteList, |
| 1285 &SafeBrowsingDatabase::ContainsCsdWhitelistedUrl, | 1262 &SafeBrowsingDatabase::ContainsCsdWhitelistedUrl, |
| 1286 nullptr}, | 1263 nullptr}, |
| 1287 {safe_browsing::kDownloadWhiteList, | 1264 {kDownloadWhiteList, |
| 1288 &SafeBrowsingDatabase::ContainsDownloadWhitelistedUrl, | 1265 &SafeBrowsingDatabase::ContainsDownloadWhitelistedUrl, |
| 1289 &SafeBrowsingDatabase::ContainsDownloadWhitelistedString}, | 1266 &SafeBrowsingDatabase::ContainsDownloadWhitelistedString}, |
| 1290 {safe_browsing::kInclusionWhitelist, | 1267 {kInclusionWhitelist, |
| 1291 &SafeBrowsingDatabase::ContainsInclusionWhitelistedUrl, | 1268 &SafeBrowsingDatabase::ContainsInclusionWhitelistedUrl, |
| 1292 nullptr}, | 1269 nullptr}, |
| 1293 }; | 1270 }; |
| 1294 | 1271 |
| 1295 // If the whitelist is disabled everything should match the whitelist. | 1272 // If the whitelist is disabled everything should match the whitelist. |
| 1296 database_.reset(new SafeBrowsingDatabaseNew( | 1273 database_.reset(new SafeBrowsingDatabaseNew( |
| 1297 task_runner_, new SafeBrowsingStoreFile(task_runner_), NULL, NULL, NULL, | 1274 task_runner_, new SafeBrowsingStoreFile(task_runner_), NULL, NULL, NULL, |
| 1298 NULL, NULL, NULL, NULL)); | 1275 NULL, NULL, NULL, NULL)); |
| 1299 database_->Init(database_filename_); | 1276 database_->Init(database_filename_); |
| 1300 for (const auto& test_case : kTestCases) { | 1277 for (const auto& test_case : kTestCases) { |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1415 EXPECT_TRUE( | 1392 EXPECT_TRUE( |
| 1416 (database_.get()->*test_case.test_list_contains_whitelisted_string)( | 1393 (database_.get()->*test_case.test_list_contains_whitelisted_string)( |
| 1417 kGoodString)); | 1394 kGoodString)); |
| 1418 } | 1395 } |
| 1419 | 1396 |
| 1420 EXPECT_FALSE( | 1397 EXPECT_FALSE( |
| 1421 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1398 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1422 GURL(std::string("http://www.google.com/")))); | 1399 GURL(std::string("http://www.google.com/")))); |
| 1423 | 1400 |
| 1424 // The malware kill switch is for the CSD whitelist only. | 1401 // The malware kill switch is for the CSD whitelist only. |
| 1425 if (test_case.test_list_name == safe_browsing::kCsdWhiteList) { | 1402 if (test_case.test_list_name == kCsdWhiteList) { |
| 1426 // The CSD whitelist killswitch is not present. | 1403 // The CSD whitelist killswitch is not present. |
| 1427 EXPECT_FALSE(database_->IsCsdWhitelistKillSwitchOn()); | 1404 EXPECT_FALSE(database_->IsCsdWhitelistKillSwitchOn()); |
| 1428 | 1405 |
| 1429 // Test only add the malware IP killswitch | 1406 // Test only add the malware IP killswitch |
| 1430 chunks.clear(); | 1407 chunks.clear(); |
| 1431 chunks.push_back(AddChunkFullHashValue( | 1408 chunks.push_back(AddChunkFullHashValue( |
| 1432 15, "sb-ssl.google.com/safebrowsing/csd/killswitch_malware")); | 1409 15, "sb-ssl.google.com/safebrowsing/csd/killswitch_malware")); |
| 1433 | 1410 |
| 1434 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1411 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1435 database_->InsertChunks(safe_browsing::kCsdWhiteList, chunks.get()); | 1412 database_->InsertChunks(kCsdWhiteList, chunks.get()); |
| 1436 database_->UpdateFinished(true); | 1413 database_->UpdateFinished(true); |
| 1437 | 1414 |
| 1438 EXPECT_TRUE(database_->IsMalwareIPMatchKillSwitchOn()); | 1415 EXPECT_TRUE(database_->IsMalwareIPMatchKillSwitchOn()); |
| 1439 // The CSD whitelist killswitch is not present. | 1416 // The CSD whitelist killswitch is not present. |
| 1440 EXPECT_FALSE(database_->IsCsdWhitelistKillSwitchOn()); | 1417 EXPECT_FALSE(database_->IsCsdWhitelistKillSwitchOn()); |
| 1441 } | 1418 } |
| 1442 | 1419 |
| 1443 // Test that the generic whitelist kill-switch works as intended. | 1420 // Test that the generic whitelist kill-switch works as intended. |
| 1444 chunks.clear(); | 1421 chunks.clear(); |
| 1445 lists.clear(); | 1422 lists.clear(); |
| 1446 chunks.push_back(AddChunkFullHashValue( | 1423 chunks.push_back(AddChunkFullHashValue( |
| 1447 5, "sb-ssl.google.com/safebrowsing/csd/killswitch")); | 1424 5, "sb-ssl.google.com/safebrowsing/csd/killswitch")); |
| 1448 | 1425 |
| 1449 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1426 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1450 database_->InsertChunks(test_case.test_list_name, chunks.get()); | 1427 database_->InsertChunks(test_case.test_list_name, chunks.get()); |
| 1451 database_->UpdateFinished(true); | 1428 database_->UpdateFinished(true); |
| 1452 | 1429 |
| 1453 // Test CSD whitelist specific methods. | 1430 // Test CSD whitelist specific methods. |
| 1454 if (test_case.test_list_name == safe_browsing::kCsdWhiteList) { | 1431 if (test_case.test_list_name == kCsdWhiteList) { |
| 1455 // The CSD whitelist killswitch is present. | 1432 // The CSD whitelist killswitch is present. |
| 1456 EXPECT_TRUE(database_->IsCsdWhitelistKillSwitchOn()); | 1433 EXPECT_TRUE(database_->IsCsdWhitelistKillSwitchOn()); |
| 1457 EXPECT_TRUE(database_->IsMalwareIPMatchKillSwitchOn()); | 1434 EXPECT_TRUE(database_->IsMalwareIPMatchKillSwitchOn()); |
| 1458 } | 1435 } |
| 1459 | 1436 |
| 1460 EXPECT_TRUE( | 1437 EXPECT_TRUE( |
| 1461 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1438 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1462 GURL(std::string("https://") + kGood1Url2 + "/c.html"))); | 1439 GURL(std::string("https://") + kGood1Url2 + "/c.html"))); |
| 1463 EXPECT_TRUE( | 1440 EXPECT_TRUE( |
| 1464 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1441 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1465 GURL(std::string("http://www.google.com/")))); | 1442 GURL(std::string("http://www.google.com/")))); |
| 1466 EXPECT_TRUE( | 1443 EXPECT_TRUE( |
| 1467 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1444 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1468 GURL(std::string("http://www.phishing_url.com/")))); | 1445 GURL(std::string("http://www.phishing_url.com/")))); |
| 1469 | 1446 |
| 1470 if (test_case.TestStrings()) { | 1447 if (test_case.TestStrings()) { |
| 1471 EXPECT_TRUE( | 1448 EXPECT_TRUE( |
| 1472 (database_.get()->*test_case.test_list_contains_whitelisted_string)( | 1449 (database_.get()->*test_case.test_list_contains_whitelisted_string)( |
| 1473 "asdf")); | 1450 "asdf")); |
| 1474 EXPECT_TRUE( | 1451 EXPECT_TRUE( |
| 1475 (database_.get()->*test_case.test_list_contains_whitelisted_string)( | 1452 (database_.get()->*test_case.test_list_contains_whitelisted_string)( |
| 1476 kGoodString)); | 1453 kGoodString)); |
| 1477 } | 1454 } |
| 1478 | 1455 |
| 1479 // Remove the kill-switch and verify that we can recover. | 1456 // Remove the kill-switch and verify that we can recover. |
| 1480 chunks.clear(); | 1457 chunks.clear(); |
| 1481 lists.clear(); | 1458 lists.clear(); |
| 1482 chunks.push_back(SubChunkFullHashValue( | 1459 chunks.push_back(SubChunkFullHashValue( |
| 1483 1, "sb-ssl.google.com/safebrowsing/csd/killswitch", 5)); | 1460 1, "sb-ssl.google.com/safebrowsing/csd/killswitch", 5)); |
| 1484 if (test_case.test_list_name == safe_browsing::kCsdWhiteList) { | 1461 if (test_case.test_list_name == kCsdWhiteList) { |
| 1485 chunks.push_back(SubChunkFullHashValue( | 1462 chunks.push_back(SubChunkFullHashValue( |
| 1486 10, "sb-ssl.google.com/safebrowsing/csd/killswitch_malware", 15)); | 1463 10, "sb-ssl.google.com/safebrowsing/csd/killswitch_malware", 15)); |
| 1487 } | 1464 } |
| 1488 | 1465 |
| 1489 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1466 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1490 database_->InsertChunks(test_case.test_list_name, chunks.get()); | 1467 database_->InsertChunks(test_case.test_list_name, chunks.get()); |
| 1491 database_->UpdateFinished(true); | 1468 database_->UpdateFinished(true); |
| 1492 | 1469 |
| 1493 if (test_case.test_list_name == safe_browsing::kCsdWhiteList) { | 1470 if (test_case.test_list_name == kCsdWhiteList) { |
| 1494 EXPECT_FALSE(database_->IsMalwareIPMatchKillSwitchOn()); | 1471 EXPECT_FALSE(database_->IsMalwareIPMatchKillSwitchOn()); |
| 1495 EXPECT_FALSE(database_->IsCsdWhitelistKillSwitchOn()); | 1472 EXPECT_FALSE(database_->IsCsdWhitelistKillSwitchOn()); |
| 1496 } | 1473 } |
| 1497 EXPECT_TRUE( | 1474 EXPECT_TRUE( |
| 1498 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1475 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1499 GURL(std::string("https://") + kGood1Url2 + "/c.html"))); | 1476 GURL(std::string("https://") + kGood1Url2 + "/c.html"))); |
| 1500 EXPECT_TRUE( | 1477 EXPECT_TRUE( |
| 1501 (database_.get()->*test_case.test_list_contains_whitelisted_url)( | 1478 (database_.get()->*test_case.test_list_contains_whitelisted_url)( |
| 1502 GURL(std::string("https://") + kGood2Url1 + "/c/bla"))); | 1479 GURL(std::string("https://") + kGood2Url1 + "/c/bla"))); |
| 1503 EXPECT_TRUE( | 1480 EXPECT_TRUE( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1526 ScopedVector<SBChunkData> chunks; | 1503 ScopedVector<SBChunkData> chunks; |
| 1527 | 1504 |
| 1528 // Add a malware add chunk with two entries of the same host. | 1505 // Add a malware add chunk with two entries of the same host. |
| 1529 chunks.push_back(AddChunkPrefix2Value(1, | 1506 chunks.push_back(AddChunkPrefix2Value(1, |
| 1530 "www.evil.com/malware1.html", | 1507 "www.evil.com/malware1.html", |
| 1531 "www.evil.com/malware2.html")); | 1508 "www.evil.com/malware2.html")); |
| 1532 | 1509 |
| 1533 // Insert the testing chunks into database. | 1510 // Insert the testing chunks into database. |
| 1534 std::vector<SBListChunkRanges> lists; | 1511 std::vector<SBListChunkRanges> lists; |
| 1535 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1512 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1536 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1513 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1537 database_->UpdateFinished(true); | 1514 database_->UpdateFinished(true); |
| 1538 | 1515 |
| 1539 GetListsInfo(&lists); | 1516 GetListsInfo(&lists); |
| 1540 ASSERT_LE(1U, lists.size()); | 1517 ASSERT_LE(1U, lists.size()); |
| 1541 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 1518 EXPECT_EQ(kMalwareList, lists[0].name); |
| 1542 EXPECT_EQ("1", lists[0].adds); | 1519 EXPECT_EQ("1", lists[0].adds); |
| 1543 EXPECT_TRUE(lists[0].subs.empty()); | 1520 EXPECT_TRUE(lists[0].subs.empty()); |
| 1544 | 1521 |
| 1545 // Add a phishing add chunk with two entries of the same host. | 1522 // Add a phishing add chunk with two entries of the same host. |
| 1546 chunks.clear(); | 1523 chunks.clear(); |
| 1547 chunks.push_back(AddChunkPrefix2Value(47, | 1524 chunks.push_back(AddChunkPrefix2Value(47, |
| 1548 "www.evil.com/phishing1.html", | 1525 "www.evil.com/phishing1.html", |
| 1549 "www.evil.com/phishing2.html")); | 1526 "www.evil.com/phishing2.html")); |
| 1550 | 1527 |
| 1551 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1528 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1552 database_->InsertChunks(safe_browsing::kPhishingList, chunks.get()); | 1529 database_->InsertChunks(kPhishingList, chunks.get()); |
| 1553 database_->UpdateFinished(true); | 1530 database_->UpdateFinished(true); |
| 1554 | 1531 |
| 1555 GetListsInfo(&lists); | 1532 GetListsInfo(&lists); |
| 1556 ASSERT_LE(2U, lists.size()); | 1533 ASSERT_LE(2U, lists.size()); |
| 1557 EXPECT_EQ(safe_browsing::kMalwareList, lists[0].name); | 1534 EXPECT_EQ(kMalwareList, lists[0].name); |
| 1558 EXPECT_EQ("1", lists[0].adds); | 1535 EXPECT_EQ("1", lists[0].adds); |
| 1559 EXPECT_TRUE(lists[0].subs.empty()); | 1536 EXPECT_TRUE(lists[0].subs.empty()); |
| 1560 EXPECT_EQ(safe_browsing::kPhishingList, lists[1].name); | 1537 EXPECT_EQ(kPhishingList, lists[1].name); |
| 1561 EXPECT_EQ("47", lists[1].adds); | 1538 EXPECT_EQ("47", lists[1].adds); |
| 1562 EXPECT_TRUE(lists[1].subs.empty()); | 1539 EXPECT_TRUE(lists[1].subs.empty()); |
| 1563 | 1540 |
| 1564 std::vector<SBPrefix> prefix_hits; | 1541 std::vector<SBPrefix> prefix_hits; |
| 1565 std::vector<SBFullHashResult> cache_hits; | 1542 std::vector<SBFullHashResult> cache_hits; |
| 1566 | 1543 |
| 1567 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1544 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1568 GURL("http://www.evil.com/malware1.html"), &prefix_hits, &cache_hits)); | 1545 GURL("http://www.evil.com/malware1.html"), &prefix_hits, &cache_hits)); |
| 1569 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1546 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1570 GURL("http://www.evil.com/malware2.html"), &prefix_hits, &cache_hits)); | 1547 GURL("http://www.evil.com/malware2.html"), &prefix_hits, &cache_hits)); |
| 1571 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1548 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1572 GURL("http://www.evil.com/phishing1.html"), &prefix_hits, &cache_hits)); | 1549 GURL("http://www.evil.com/phishing1.html"), &prefix_hits, &cache_hits)); |
| 1573 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1550 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1574 GURL("http://www.evil.com/phishing2.html"), &prefix_hits, &cache_hits)); | 1551 GURL("http://www.evil.com/phishing2.html"), &prefix_hits, &cache_hits)); |
| 1575 | 1552 |
| 1576 // Test removing a single prefix from the add chunk. | 1553 // Test removing a single prefix from the add chunk. |
| 1577 // Remove the prefix that added first. | 1554 // Remove the prefix that added first. |
| 1578 chunks.clear(); | 1555 chunks.clear(); |
| 1579 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/malware1.html", 1)); | 1556 chunks.push_back(SubChunkPrefixValue(4, "www.evil.com/malware1.html", 1)); |
| 1580 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1557 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1581 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1558 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1582 database_->UpdateFinished(true); | 1559 database_->UpdateFinished(true); |
| 1583 | 1560 |
| 1584 // Remove the prefix that added last. | 1561 // Remove the prefix that added last. |
| 1585 chunks.clear(); | 1562 chunks.clear(); |
| 1586 chunks.push_back(SubChunkPrefixValue(5, "www.evil.com/phishing2.html", 47)); | 1563 chunks.push_back(SubChunkPrefixValue(5, "www.evil.com/phishing2.html", 47)); |
| 1587 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1564 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1588 database_->InsertChunks(safe_browsing::kPhishingList, chunks.get()); | 1565 database_->InsertChunks(kPhishingList, chunks.get()); |
| 1589 database_->UpdateFinished(true); | 1566 database_->UpdateFinished(true); |
| 1590 | 1567 |
| 1591 // Verify that the database contains urls expected. | 1568 // Verify that the database contains urls expected. |
| 1592 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1569 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1593 GURL("http://www.evil.com/malware1.html"), &prefix_hits, &cache_hits)); | 1570 GURL("http://www.evil.com/malware1.html"), &prefix_hits, &cache_hits)); |
| 1594 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1571 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1595 GURL("http://www.evil.com/malware2.html"), &prefix_hits, &cache_hits)); | 1572 GURL("http://www.evil.com/malware2.html"), &prefix_hits, &cache_hits)); |
| 1596 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1573 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1597 GURL("http://www.evil.com/phishing1.html"), &prefix_hits, &cache_hits)); | 1574 GURL("http://www.evil.com/phishing1.html"), &prefix_hits, &cache_hits)); |
| 1598 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1575 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1599 GURL("http://www.evil.com/phishing2.html"), &prefix_hits, &cache_hits)); | 1576 GURL("http://www.evil.com/phishing2.html"), &prefix_hits, &cache_hits)); |
| 1600 } | 1577 } |
| 1601 | 1578 |
| 1602 // Test that an empty update doesn't actually update the database. | 1579 // Test that an empty update doesn't actually update the database. |
| 1603 // This isn't a functionality requirement, but it is a useful | 1580 // This isn't a functionality requirement, but it is a useful |
| 1604 // optimization. | 1581 // optimization. |
| 1605 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { | 1582 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { |
| 1606 ScopedVector<SBChunkData> chunks; | 1583 ScopedVector<SBChunkData> chunks; |
| 1607 | 1584 |
| 1608 base::FilePath filename = database_->BrowseDBFilename(database_filename_); | 1585 base::FilePath filename = database_->BrowseDBFilename(database_filename_); |
| 1609 | 1586 |
| 1610 // Prime the database. | 1587 // Prime the database. |
| 1611 std::vector<SBListChunkRanges> lists; | 1588 std::vector<SBListChunkRanges> lists; |
| 1612 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1589 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1613 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); | 1590 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); |
| 1614 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1591 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1615 database_->UpdateFinished(true); | 1592 database_->UpdateFinished(true); |
| 1616 | 1593 |
| 1617 // Get an older time to reset the lastmod time for detecting whether | 1594 // Get an older time to reset the lastmod time for detecting whether |
| 1618 // the file has been updated. | 1595 // the file has been updated. |
| 1619 base::File::Info before_info, after_info; | 1596 base::File::Info before_info, after_info; |
| 1620 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); | 1597 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); |
| 1621 const Time old_last_modified = | 1598 const Time old_last_modified = |
| 1622 before_info.last_modified - TimeDelta::FromSeconds(10); | 1599 before_info.last_modified - TimeDelta::FromSeconds(10); |
| 1623 | 1600 |
| 1624 // Inserting another chunk updates the database file. The sleep is | 1601 // Inserting another chunk updates the database file. The sleep is |
| 1625 // needed because otherwise the entire test can finish w/in the | 1602 // needed because otherwise the entire test can finish w/in the |
| 1626 // resolution of the lastmod time. | 1603 // resolution of the lastmod time. |
| 1627 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); | 1604 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); |
| 1628 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); | 1605 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); |
| 1629 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1606 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1630 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); | 1607 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); |
| 1631 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1608 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1632 database_->UpdateFinished(true); | 1609 database_->UpdateFinished(true); |
| 1633 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); | 1610 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); |
| 1634 EXPECT_LT(before_info.last_modified, after_info.last_modified); | 1611 EXPECT_LT(before_info.last_modified, after_info.last_modified); |
| 1635 | 1612 |
| 1636 // Deleting a chunk updates the database file. | 1613 // Deleting a chunk updates the database file. |
| 1637 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); | 1614 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); |
| 1638 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); | 1615 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); |
| 1639 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1616 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1640 AddDelChunk(safe_browsing::kMalwareList, 2); | 1617 AddDelChunk(kMalwareList, 2); |
| 1641 database_->UpdateFinished(true); | 1618 database_->UpdateFinished(true); |
| 1642 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); | 1619 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); |
| 1643 EXPECT_LT(before_info.last_modified, after_info.last_modified); | 1620 EXPECT_LT(before_info.last_modified, after_info.last_modified); |
| 1644 | 1621 |
| 1645 // Simply calling |UpdateStarted()| then |UpdateFinished()| does not | 1622 // Simply calling |UpdateStarted()| then |UpdateFinished()| does not |
| 1646 // update the database file. | 1623 // update the database file. |
| 1647 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); | 1624 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); |
| 1648 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); | 1625 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); |
| 1649 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1626 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1650 database_->UpdateFinished(true); | 1627 database_->UpdateFinished(true); |
| 1651 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); | 1628 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); |
| 1652 EXPECT_EQ(before_info.last_modified, after_info.last_modified); | 1629 EXPECT_EQ(before_info.last_modified, after_info.last_modified); |
| 1653 } | 1630 } |
| 1654 | 1631 |
| 1655 // Test that a filter file is written out during update and read back | 1632 // Test that a filter file is written out during update and read back |
| 1656 // in during setup. | 1633 // in during setup. |
| 1657 TEST_F(SafeBrowsingDatabaseTest, FilterFile) { | 1634 TEST_F(SafeBrowsingDatabaseTest, FilterFile) { |
| 1658 // Create a database with trivial example data and write it out. | 1635 // Create a database with trivial example data and write it out. |
| 1659 { | 1636 { |
| 1660 // Prime the database. | 1637 // Prime the database. |
| 1661 std::vector<SBListChunkRanges> lists; | 1638 std::vector<SBListChunkRanges> lists; |
| 1662 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1639 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1663 | 1640 |
| 1664 ScopedVector<SBChunkData> chunks; | 1641 ScopedVector<SBChunkData> chunks; |
| 1665 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); | 1642 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); |
| 1666 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1643 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1667 database_->UpdateFinished(true); | 1644 database_->UpdateFinished(true); |
| 1668 } | 1645 } |
| 1669 | 1646 |
| 1670 // Find the malware url in the database, don't find a good url. | 1647 // Find the malware url in the database, don't find a good url. |
| 1671 std::vector<SBPrefix> prefix_hits; | 1648 std::vector<SBPrefix> prefix_hits; |
| 1672 std::vector<SBFullHashResult> cache_hits; | 1649 std::vector<SBFullHashResult> cache_hits; |
| 1673 EXPECT_TRUE(database_->ContainsBrowseUrl( | 1650 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 1674 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 1651 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| 1675 EXPECT_FALSE(database_->ContainsBrowseUrl( | 1652 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 1676 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); | 1653 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits)); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1706 const SBFullHash kFullHash2_1 = | 1683 const SBFullHash kFullHash2_1 = |
| 1707 SBFullHashForPrefixAndSuffix(kPrefix2, "\x01"); | 1684 SBFullHashForPrefixAndSuffix(kPrefix2, "\x01"); |
| 1708 | 1685 |
| 1709 // Insert prefix kPrefix1 and kPrefix2 into database. | 1686 // Insert prefix kPrefix1 and kPrefix2 into database. |
| 1710 ScopedVector<SBChunkData> chunks; | 1687 ScopedVector<SBChunkData> chunks; |
| 1711 chunks.push_back(AddChunkPrefix(1, kPrefix1)); | 1688 chunks.push_back(AddChunkPrefix(1, kPrefix1)); |
| 1712 chunks.push_back(AddChunkPrefix(2, kPrefix2)); | 1689 chunks.push_back(AddChunkPrefix(2, kPrefix2)); |
| 1713 | 1690 |
| 1714 std::vector<SBListChunkRanges> lists; | 1691 std::vector<SBListChunkRanges> lists; |
| 1715 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1692 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1716 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1693 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1717 database_->UpdateFinished(true); | 1694 database_->UpdateFinished(true); |
| 1718 | 1695 |
| 1719 { | 1696 { |
| 1720 // Cache a full miss result for kPrefix1. | 1697 // Cache a full miss result for kPrefix1. |
| 1721 std::vector<SBPrefix> prefixes(1, kPrefix1); | 1698 std::vector<SBPrefix> prefixes(1, kPrefix1); |
| 1722 std::vector<SBFullHashResult> cache_results; | 1699 std::vector<SBFullHashResult> cache_results; |
| 1723 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); | 1700 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); |
| 1724 } | 1701 } |
| 1725 | 1702 |
| 1726 { | 1703 { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1761 const SBFullHash kFullHash3_1 = | 1738 const SBFullHash kFullHash3_1 = |
| 1762 SBFullHashForPrefixAndSuffix(kPrefix3, "\x01"); | 1739 SBFullHashForPrefixAndSuffix(kPrefix3, "\x01"); |
| 1763 | 1740 |
| 1764 // Insert prefix kPrefix1 and kPrefix2 into database. | 1741 // Insert prefix kPrefix1 and kPrefix2 into database. |
| 1765 ScopedVector<SBChunkData> chunks; | 1742 ScopedVector<SBChunkData> chunks; |
| 1766 chunks.push_back(AddChunkPrefix(1, kPrefix1)); | 1743 chunks.push_back(AddChunkPrefix(1, kPrefix1)); |
| 1767 chunks.push_back(AddChunkPrefix(2, kPrefix2)); | 1744 chunks.push_back(AddChunkPrefix(2, kPrefix2)); |
| 1768 | 1745 |
| 1769 std::vector<SBListChunkRanges> lists; | 1746 std::vector<SBListChunkRanges> lists; |
| 1770 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1747 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1771 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1748 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1772 database_->UpdateFinished(true); | 1749 database_->UpdateFinished(true); |
| 1773 | 1750 |
| 1774 { | 1751 { |
| 1775 // kFullHash1_1 has a prefix hit of kPrefix1. | 1752 // kFullHash1_1 has a prefix hit of kPrefix1. |
| 1776 std::vector<SBFullHash> full_hashes; | 1753 std::vector<SBFullHash> full_hashes; |
| 1777 full_hashes.push_back(kFullHash1_1); | 1754 full_hashes.push_back(kFullHash1_1); |
| 1778 std::vector<SBPrefix> prefix_hits; | 1755 std::vector<SBPrefix> prefix_hits; |
| 1779 std::vector<SBFullHashResult> cache_hits; | 1756 std::vector<SBFullHashResult> cache_hits; |
| 1780 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1757 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1781 full_hashes, &prefix_hits, &cache_hits)); | 1758 full_hashes, &prefix_hits, &cache_hits)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1805 EXPECT_EQ(kPrefix2, prefix_hits[1]); | 1782 EXPECT_EQ(kPrefix2, prefix_hits[1]); |
| 1806 EXPECT_TRUE(cache_hits.empty()); | 1783 EXPECT_TRUE(cache_hits.empty()); |
| 1807 } | 1784 } |
| 1808 | 1785 |
| 1809 { | 1786 { |
| 1810 // Cache a fullhash result for two kPrefix1 full hashes. | 1787 // Cache a fullhash result for two kPrefix1 full hashes. |
| 1811 std::vector<SBPrefix> prefixes(1, kPrefix1); | 1788 std::vector<SBPrefix> prefixes(1, kPrefix1); |
| 1812 std::vector<SBFullHashResult> cache_results; | 1789 std::vector<SBFullHashResult> cache_results; |
| 1813 | 1790 |
| 1814 SBFullHashResult full_hash_result; | 1791 SBFullHashResult full_hash_result; |
| 1815 full_hash_result.list_id = safe_browsing::MALWARE; | 1792 full_hash_result.list_id = MALWARE; |
| 1816 | 1793 |
| 1817 full_hash_result.hash = kFullHash1_1; | 1794 full_hash_result.hash = kFullHash1_1; |
| 1818 cache_results.push_back(full_hash_result); | 1795 cache_results.push_back(full_hash_result); |
| 1819 | 1796 |
| 1820 full_hash_result.hash = kFullHash1_3; | 1797 full_hash_result.hash = kFullHash1_3; |
| 1821 cache_results.push_back(full_hash_result); | 1798 cache_results.push_back(full_hash_result); |
| 1822 | 1799 |
| 1823 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); | 1800 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); |
| 1824 } | 1801 } |
| 1825 | 1802 |
| 1826 { | 1803 { |
| 1827 // kFullHash1_1 should now see a cache hit. | 1804 // kFullHash1_1 should now see a cache hit. |
| 1828 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); | 1805 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); |
| 1829 std::vector<SBPrefix> prefix_hits; | 1806 std::vector<SBPrefix> prefix_hits; |
| 1830 std::vector<SBFullHashResult> cache_hits; | 1807 std::vector<SBFullHashResult> cache_hits; |
| 1831 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1808 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1832 full_hashes, &prefix_hits, &cache_hits)); | 1809 full_hashes, &prefix_hits, &cache_hits)); |
| 1833 EXPECT_TRUE(prefix_hits.empty()); | 1810 EXPECT_TRUE(prefix_hits.empty()); |
| 1834 ASSERT_EQ(1U, cache_hits.size()); | 1811 ASSERT_EQ(1U, cache_hits.size()); |
| 1835 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 1812 EXPECT_TRUE(SBFullHashEqual( |
| 1836 kFullHash1_1, cache_hits[0].hash)); | 1813 kFullHash1_1, cache_hits[0].hash)); |
| 1837 | 1814 |
| 1838 // Adding kFullHash2_1 will see the existing cache hit plus the prefix hit | 1815 // Adding kFullHash2_1 will see the existing cache hit plus the prefix hit |
| 1839 // for kPrefix2. | 1816 // for kPrefix2. |
| 1840 full_hashes.push_back(kFullHash2_1); | 1817 full_hashes.push_back(kFullHash2_1); |
| 1841 prefix_hits.clear(); | 1818 prefix_hits.clear(); |
| 1842 cache_hits.clear(); | 1819 cache_hits.clear(); |
| 1843 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1820 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1844 full_hashes, &prefix_hits, &cache_hits)); | 1821 full_hashes, &prefix_hits, &cache_hits)); |
| 1845 ASSERT_EQ(1U, prefix_hits.size()); | 1822 ASSERT_EQ(1U, prefix_hits.size()); |
| 1846 EXPECT_EQ(kPrefix2, prefix_hits[0]); | 1823 EXPECT_EQ(kPrefix2, prefix_hits[0]); |
| 1847 ASSERT_EQ(1U, cache_hits.size()); | 1824 ASSERT_EQ(1U, cache_hits.size()); |
| 1848 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 1825 EXPECT_TRUE(SBFullHashEqual( |
| 1849 kFullHash1_1, cache_hits[0].hash)); | 1826 kFullHash1_1, cache_hits[0].hash)); |
| 1850 | 1827 |
| 1851 // kFullHash1_3 also gets a cache hit. | 1828 // kFullHash1_3 also gets a cache hit. |
| 1852 full_hashes.push_back(kFullHash1_3); | 1829 full_hashes.push_back(kFullHash1_3); |
| 1853 prefix_hits.clear(); | 1830 prefix_hits.clear(); |
| 1854 cache_hits.clear(); | 1831 cache_hits.clear(); |
| 1855 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1832 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1856 full_hashes, &prefix_hits, &cache_hits)); | 1833 full_hashes, &prefix_hits, &cache_hits)); |
| 1857 ASSERT_EQ(1U, prefix_hits.size()); | 1834 ASSERT_EQ(1U, prefix_hits.size()); |
| 1858 EXPECT_EQ(kPrefix2, prefix_hits[0]); | 1835 EXPECT_EQ(kPrefix2, prefix_hits[0]); |
| 1859 ASSERT_EQ(2U, cache_hits.size()); | 1836 ASSERT_EQ(2U, cache_hits.size()); |
| 1860 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 1837 EXPECT_TRUE(SBFullHashEqual( |
| 1861 kFullHash1_1, cache_hits[0].hash)); | 1838 kFullHash1_1, cache_hits[0].hash)); |
| 1862 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 1839 EXPECT_TRUE(SBFullHashEqual( |
| 1863 kFullHash1_3, cache_hits[1].hash)); | 1840 kFullHash1_3, cache_hits[1].hash)); |
| 1864 } | 1841 } |
| 1865 | 1842 |
| 1866 { | 1843 { |
| 1867 // Check if DB contains only kFullHash1_3. Should return a cache hit. | 1844 // Check if DB contains only kFullHash1_3. Should return a cache hit. |
| 1868 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); | 1845 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); |
| 1869 std::vector<SBPrefix> prefix_hits; | 1846 std::vector<SBPrefix> prefix_hits; |
| 1870 std::vector<SBFullHashResult> cache_hits; | 1847 std::vector<SBFullHashResult> cache_hits; |
| 1871 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1848 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1872 full_hashes, &prefix_hits, &cache_hits)); | 1849 full_hashes, &prefix_hits, &cache_hits)); |
| 1873 EXPECT_TRUE(prefix_hits.empty()); | 1850 EXPECT_TRUE(prefix_hits.empty()); |
| 1874 ASSERT_EQ(1U, cache_hits.size()); | 1851 ASSERT_EQ(1U, cache_hits.size()); |
| 1875 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 1852 EXPECT_TRUE(SBFullHashEqual( |
| 1876 kFullHash1_3, cache_hits[0].hash)); | 1853 kFullHash1_3, cache_hits[0].hash)); |
| 1877 } | 1854 } |
| 1878 | 1855 |
| 1879 { | 1856 { |
| 1880 // kFullHash1_2 has no cache hit, and no prefix hit because of the cache for | 1857 // kFullHash1_2 has no cache hit, and no prefix hit because of the cache for |
| 1881 // kPrefix1. | 1858 // kPrefix1. |
| 1882 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 1859 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
| 1883 std::vector<SBPrefix> prefix_hits; | 1860 std::vector<SBPrefix> prefix_hits; |
| 1884 std::vector<SBFullHashResult> cache_hits; | 1861 std::vector<SBFullHashResult> cache_hits; |
| 1885 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 1862 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 1906 const SBFullHash kFullHash1_3 = | 1883 const SBFullHash kFullHash1_3 = |
| 1907 SBFullHashForPrefixAndSuffix(kPrefix1, "\x03"); | 1884 SBFullHashForPrefixAndSuffix(kPrefix1, "\x03"); |
| 1908 | 1885 |
| 1909 // Insert two full hashes with a shared prefix. | 1886 // Insert two full hashes with a shared prefix. |
| 1910 ScopedVector<SBChunkData> chunks; | 1887 ScopedVector<SBChunkData> chunks; |
| 1911 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); | 1888 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); |
| 1912 chunks.push_back(AddChunkFullHash(2, kFullHash1_2)); | 1889 chunks.push_back(AddChunkFullHash(2, kFullHash1_2)); |
| 1913 | 1890 |
| 1914 std::vector<SBListChunkRanges> lists; | 1891 std::vector<SBListChunkRanges> lists; |
| 1915 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1892 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1916 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1893 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1917 database_->UpdateFinished(true); | 1894 database_->UpdateFinished(true); |
| 1918 | 1895 |
| 1919 { | 1896 { |
| 1920 // Check a full hash which isn't present. | 1897 // Check a full hash which isn't present. |
| 1921 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); | 1898 std::vector<SBFullHash> full_hashes(1, kFullHash1_3); |
| 1922 std::vector<SBPrefix> prefix_hits; | 1899 std::vector<SBPrefix> prefix_hits; |
| 1923 std::vector<SBFullHashResult> cache_hits; | 1900 std::vector<SBFullHashResult> cache_hits; |
| 1924 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 1901 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 1925 full_hashes, &prefix_hits, &cache_hits)); | 1902 full_hashes, &prefix_hits, &cache_hits)); |
| 1926 | 1903 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 1941 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1918 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1942 full_hashes, &prefix_hits, &cache_hits)); | 1919 full_hashes, &prefix_hits, &cache_hits)); |
| 1943 ASSERT_EQ(1U, prefix_hits.size()); | 1920 ASSERT_EQ(1U, prefix_hits.size()); |
| 1944 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 1921 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 1945 EXPECT_TRUE(cache_hits.empty()); | 1922 EXPECT_TRUE(cache_hits.empty()); |
| 1946 } | 1923 } |
| 1947 | 1924 |
| 1948 { | 1925 { |
| 1949 // Cache a gethash result for kFullHash1_2. | 1926 // Cache a gethash result for kFullHash1_2. |
| 1950 SBFullHashResult full_hash_result; | 1927 SBFullHashResult full_hash_result; |
| 1951 full_hash_result.list_id = safe_browsing::MALWARE; | 1928 full_hash_result.list_id = MALWARE; |
| 1952 full_hash_result.hash = kFullHash1_2; | 1929 full_hash_result.hash = kFullHash1_2; |
| 1953 | 1930 |
| 1954 std::vector<SBPrefix> prefixes(1, kPrefix1); | 1931 std::vector<SBPrefix> prefixes(1, kPrefix1); |
| 1955 std::vector<SBFullHashResult> cache_results(1, full_hash_result); | 1932 std::vector<SBFullHashResult> cache_results(1, full_hash_result); |
| 1956 | 1933 |
| 1957 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); | 1934 database_->CacheHashResults(prefixes, cache_results, kCacheLifetime); |
| 1958 } | 1935 } |
| 1959 | 1936 |
| 1960 { | 1937 { |
| 1961 // kFullHash1_3 should still return false, because the cached | 1938 // kFullHash1_3 should still return false, because the cached |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1975 full_hashes, &prefix_hits, &cache_hits)); | 1952 full_hashes, &prefix_hits, &cache_hits)); |
| 1976 | 1953 |
| 1977 // kFullHash1_2 is in the cached result. | 1954 // kFullHash1_2 is in the cached result. |
| 1978 full_hashes.push_back(kFullHash1_2); | 1955 full_hashes.push_back(kFullHash1_2); |
| 1979 prefix_hits.clear(); | 1956 prefix_hits.clear(); |
| 1980 cache_hits.clear(); | 1957 cache_hits.clear(); |
| 1981 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 1958 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 1982 full_hashes, &prefix_hits, &cache_hits)); | 1959 full_hashes, &prefix_hits, &cache_hits)); |
| 1983 EXPECT_TRUE(prefix_hits.empty()); | 1960 EXPECT_TRUE(prefix_hits.empty()); |
| 1984 ASSERT_EQ(1U, cache_hits.size()); | 1961 ASSERT_EQ(1U, cache_hits.size()); |
| 1985 EXPECT_TRUE(safe_browsing::SBFullHashEqual( | 1962 EXPECT_TRUE(SBFullHashEqual( |
| 1986 kFullHash1_2, cache_hits[0].hash)); | 1963 kFullHash1_2, cache_hits[0].hash)); |
| 1987 } | 1964 } |
| 1988 | 1965 |
| 1989 // Remove kFullHash1_1 from the database. | 1966 // Remove kFullHash1_1 from the database. |
| 1990 chunks.clear(); | 1967 chunks.clear(); |
| 1991 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); | 1968 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); |
| 1992 | 1969 |
| 1993 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 1970 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 1994 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 1971 database_->InsertChunks(kMalwareList, chunks.get()); |
| 1995 database_->UpdateFinished(true); | 1972 database_->UpdateFinished(true); |
| 1996 | 1973 |
| 1997 // Cache should be cleared after updating. | 1974 // Cache should be cleared after updating. |
| 1998 EXPECT_TRUE( | 1975 EXPECT_TRUE( |
| 1999 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); | 1976 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
| 2000 | 1977 |
| 2001 { | 1978 { |
| 2002 // Now the database doesn't contain kFullHash1_1. | 1979 // Now the database doesn't contain kFullHash1_1. |
| 2003 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); | 1980 std::vector<SBFullHash> full_hashes(1, kFullHash1_1); |
| 2004 std::vector<SBPrefix> prefix_hits; | 1981 std::vector<SBPrefix> prefix_hits; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 2022 ASSERT_EQ(1U, prefix_hits.size()); | 1999 ASSERT_EQ(1U, prefix_hits.size()); |
| 2023 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 2000 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 2024 EXPECT_TRUE(cache_hits.empty()); | 2001 EXPECT_TRUE(cache_hits.empty()); |
| 2025 } | 2002 } |
| 2026 | 2003 |
| 2027 // Remove kFullHash1_2 from the database. | 2004 // Remove kFullHash1_2 from the database. |
| 2028 chunks.clear(); | 2005 chunks.clear(); |
| 2029 chunks.push_back(SubChunkFullHash(12, kFullHash1_2, 2)); | 2006 chunks.push_back(SubChunkFullHash(12, kFullHash1_2, 2)); |
| 2030 | 2007 |
| 2031 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2008 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 2032 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2009 database_->InsertChunks(kMalwareList, chunks.get()); |
| 2033 database_->UpdateFinished(true); | 2010 database_->UpdateFinished(true); |
| 2034 | 2011 |
| 2035 // Cache should be cleared after updating. | 2012 // Cache should be cleared after updating. |
| 2036 EXPECT_TRUE( | 2013 EXPECT_TRUE( |
| 2037 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); | 2014 database_->GetUnsynchronizedPrefixGetHashCacheForTesting()->empty()); |
| 2038 | 2015 |
| 2039 { | 2016 { |
| 2040 // None are present. | 2017 // None are present. |
| 2041 std::vector<SBFullHash> full_hashes; | 2018 std::vector<SBFullHash> full_hashes; |
| 2042 std::vector<SBPrefix> prefix_hits; | 2019 std::vector<SBPrefix> prefix_hits; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 2054 const SBFullHash kFullHash1_1 = | 2031 const SBFullHash kFullHash1_1 = |
| 2055 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); | 2032 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); |
| 2056 const SBFullHash kFullHash1_2 = | 2033 const SBFullHash kFullHash1_2 = |
| 2057 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02"); | 2034 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02"); |
| 2058 | 2035 |
| 2059 ScopedVector<SBChunkData> chunks; | 2036 ScopedVector<SBChunkData> chunks; |
| 2060 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); | 2037 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); |
| 2061 | 2038 |
| 2062 std::vector<SBListChunkRanges> lists; | 2039 std::vector<SBListChunkRanges> lists; |
| 2063 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2040 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 2064 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2041 database_->InsertChunks(kMalwareList, chunks.get()); |
| 2065 database_->UpdateFinished(true); | 2042 database_->UpdateFinished(true); |
| 2066 | 2043 |
| 2067 { | 2044 { |
| 2068 // kFullHash1_2 does not match kFullHash1_1. | 2045 // kFullHash1_2 does not match kFullHash1_1. |
| 2069 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 2046 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
| 2070 std::vector<SBPrefix> prefix_hits; | 2047 std::vector<SBPrefix> prefix_hits; |
| 2071 std::vector<SBFullHashResult> cache_hits; | 2048 std::vector<SBFullHashResult> cache_hits; |
| 2072 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( | 2049 EXPECT_FALSE(database_->ContainsBrowseUrlHashesForTesting( |
| 2073 full_hashes, &prefix_hits, &cache_hits)); | 2050 full_hashes, &prefix_hits, &cache_hits)); |
| 2074 } | 2051 } |
| 2075 | 2052 |
| 2076 // Add a prefix match. | 2053 // Add a prefix match. |
| 2077 chunks.clear(); | 2054 chunks.clear(); |
| 2078 chunks.push_back(AddChunkPrefix(2, kPrefix1)); | 2055 chunks.push_back(AddChunkPrefix(2, kPrefix1)); |
| 2079 | 2056 |
| 2080 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2057 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 2081 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2058 database_->InsertChunks(kMalwareList, chunks.get()); |
| 2082 database_->UpdateFinished(true); | 2059 database_->UpdateFinished(true); |
| 2083 | 2060 |
| 2084 { | 2061 { |
| 2085 // kFullHash1_2 does match kPrefix1. | 2062 // kFullHash1_2 does match kPrefix1. |
| 2086 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 2063 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
| 2087 std::vector<SBPrefix> prefix_hits; | 2064 std::vector<SBPrefix> prefix_hits; |
| 2088 std::vector<SBFullHashResult> cache_hits; | 2065 std::vector<SBFullHashResult> cache_hits; |
| 2089 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 2066 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 2090 full_hashes, &prefix_hits, &cache_hits)); | 2067 full_hashes, &prefix_hits, &cache_hits)); |
| 2091 ASSERT_EQ(1U, prefix_hits.size()); | 2068 ASSERT_EQ(1U, prefix_hits.size()); |
| 2092 EXPECT_EQ(kPrefix1, prefix_hits[0]); | 2069 EXPECT_EQ(kPrefix1, prefix_hits[0]); |
| 2093 EXPECT_TRUE(cache_hits.empty()); | 2070 EXPECT_TRUE(cache_hits.empty()); |
| 2094 } | 2071 } |
| 2095 | 2072 |
| 2096 // Remove the full hash. | 2073 // Remove the full hash. |
| 2097 chunks.clear(); | 2074 chunks.clear(); |
| 2098 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); | 2075 chunks.push_back(SubChunkFullHash(11, kFullHash1_1, 1)); |
| 2099 | 2076 |
| 2100 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2077 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 2101 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2078 database_->InsertChunks(kMalwareList, chunks.get()); |
| 2102 database_->UpdateFinished(true); | 2079 database_->UpdateFinished(true); |
| 2103 | 2080 |
| 2104 { | 2081 { |
| 2105 // kFullHash1_2 still returns true due to the prefix hit. | 2082 // kFullHash1_2 still returns true due to the prefix hit. |
| 2106 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); | 2083 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); |
| 2107 std::vector<SBPrefix> prefix_hits; | 2084 std::vector<SBPrefix> prefix_hits; |
| 2108 std::vector<SBFullHashResult> cache_hits; | 2085 std::vector<SBFullHashResult> cache_hits; |
| 2109 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( | 2086 EXPECT_TRUE(database_->ContainsBrowseUrlHashesForTesting( |
| 2110 full_hashes, &prefix_hits, &cache_hits)); | 2087 full_hashes, &prefix_hits, &cache_hits)); |
| 2111 ASSERT_EQ(1U, prefix_hits.size()); | 2088 ASSERT_EQ(1U, prefix_hits.size()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 2131 | 2108 |
| 2132 // IPv6 prefix match for: 2620:0:1000:3103::/64. | 2109 // IPv6 prefix match for: 2620:0:1000:3103::/64. |
| 2133 chunks.push_back(AddChunkHashedIpValue(4, "2620:0:1000:3103::", 64)); | 2110 chunks.push_back(AddChunkHashedIpValue(4, "2620:0:1000:3103::", 64)); |
| 2134 | 2111 |
| 2135 // IPv4 prefix match for ::ffff:192.1.122.0/119. | 2112 // IPv4 prefix match for ::ffff:192.1.122.0/119. |
| 2136 chunks.push_back(AddChunkHashedIpValue(5, "::ffff:192.1.122.0", 119)); | 2113 chunks.push_back(AddChunkHashedIpValue(5, "::ffff:192.1.122.0", 119)); |
| 2137 | 2114 |
| 2138 // IPv4 prefix match for ::ffff:192.1.128.0/113. | 2115 // IPv4 prefix match for ::ffff:192.1.128.0/113. |
| 2139 chunks.push_back(AddChunkHashedIpValue(6, "::ffff:192.1.128.0", 113)); | 2116 chunks.push_back(AddChunkHashedIpValue(6, "::ffff:192.1.128.0", 113)); |
| 2140 | 2117 |
| 2141 database_->InsertChunks(safe_browsing::kIPBlacklist, chunks.get()); | 2118 database_->InsertChunks(kIPBlacklist, chunks.get()); |
| 2142 database_->UpdateFinished(true); | 2119 database_->UpdateFinished(true); |
| 2143 | 2120 |
| 2144 EXPECT_FALSE(database_->ContainsMalwareIP("192.168.0.255")); | 2121 EXPECT_FALSE(database_->ContainsMalwareIP("192.168.0.255")); |
| 2145 EXPECT_TRUE(database_->ContainsMalwareIP("192.168.1.0")); | 2122 EXPECT_TRUE(database_->ContainsMalwareIP("192.168.1.0")); |
| 2146 EXPECT_TRUE(database_->ContainsMalwareIP("192.168.1.255")); | 2123 EXPECT_TRUE(database_->ContainsMalwareIP("192.168.1.255")); |
| 2147 EXPECT_TRUE(database_->ContainsMalwareIP("192.168.1.10")); | 2124 EXPECT_TRUE(database_->ContainsMalwareIP("192.168.1.10")); |
| 2148 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.168.1.2")); | 2125 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.168.1.2")); |
| 2149 EXPECT_FALSE(database_->ContainsMalwareIP("192.168.2.0")); | 2126 EXPECT_FALSE(database_->ContainsMalwareIP("192.168.2.0")); |
| 2150 | 2127 |
| 2151 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.1.0")); | 2128 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.1.0")); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 2181 } | 2158 } |
| 2182 | 2159 |
| 2183 TEST_F(SafeBrowsingDatabaseTest, ContainsBrowseURL) { | 2160 TEST_F(SafeBrowsingDatabaseTest, ContainsBrowseURL) { |
| 2184 std::vector<SBListChunkRanges> lists; | 2161 std::vector<SBListChunkRanges> lists; |
| 2185 ASSERT_TRUE(database_->UpdateStarted(&lists)); | 2162 ASSERT_TRUE(database_->UpdateStarted(&lists)); |
| 2186 | 2163 |
| 2187 // Add a host-level hit. | 2164 // Add a host-level hit. |
| 2188 { | 2165 { |
| 2189 ScopedVector<SBChunkData> chunks; | 2166 ScopedVector<SBChunkData> chunks; |
| 2190 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/")); | 2167 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/")); |
| 2191 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2168 database_->InsertChunks(kMalwareList, chunks.get()); |
| 2192 } | 2169 } |
| 2193 | 2170 |
| 2194 // Add a specific fullhash. | 2171 // Add a specific fullhash. |
| 2195 static const char kWhateverMalware[] = "www.whatever.com/malware.html"; | 2172 static const char kWhateverMalware[] = "www.whatever.com/malware.html"; |
| 2196 { | 2173 { |
| 2197 ScopedVector<SBChunkData> chunks; | 2174 ScopedVector<SBChunkData> chunks; |
| 2198 chunks.push_back(AddChunkFullHashValue(2, kWhateverMalware)); | 2175 chunks.push_back(AddChunkFullHashValue(2, kWhateverMalware)); |
| 2199 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2176 database_->InsertChunks(kMalwareList, chunks.get()); |
| 2200 } | 2177 } |
| 2201 | 2178 |
| 2202 // Add a fullhash which has a prefix collision for a known url. | 2179 // Add a fullhash which has a prefix collision for a known url. |
| 2203 static const char kExampleFine[] = "www.example.com/fine.html"; | 2180 static const char kExampleFine[] = "www.example.com/fine.html"; |
| 2204 static const char kExampleCollision[] = | 2181 static const char kExampleCollision[] = |
| 2205 "www.example.com/3123364814/malware.htm"; | 2182 "www.example.com/3123364814/malware.htm"; |
| 2206 ASSERT_EQ(SBPrefixForString(kExampleFine), | 2183 ASSERT_EQ(SBPrefixForString(kExampleFine), |
| 2207 SBPrefixForString(kExampleCollision)); | 2184 SBPrefixForString(kExampleCollision)); |
| 2208 { | 2185 { |
| 2209 ScopedVector<SBChunkData> chunks; | 2186 ScopedVector<SBChunkData> chunks; |
| 2210 chunks.push_back(AddChunkFullHashValue(3, kExampleCollision)); | 2187 chunks.push_back(AddChunkFullHashValue(3, kExampleCollision)); |
| 2211 database_->InsertChunks(safe_browsing::kMalwareList, chunks.get()); | 2188 database_->InsertChunks(kMalwareList, chunks.get()); |
| 2212 } | 2189 } |
| 2213 | 2190 |
| 2214 database_->UpdateFinished(true); | 2191 database_->UpdateFinished(true); |
| 2215 | 2192 |
| 2216 std::vector<SBPrefix> prefix_hits; | 2193 std::vector<SBPrefix> prefix_hits; |
| 2217 std::vector<SBFullHashResult> cache_hits; | 2194 std::vector<SBFullHashResult> cache_hits; |
| 2218 | 2195 |
| 2219 // Anything will hit the host prefix. | 2196 // Anything will hit the host prefix. |
| 2220 EXPECT_TRUE(database_->ContainsBrowseUrl( | 2197 EXPECT_TRUE(database_->ContainsBrowseUrl( |
| 2221 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); | 2198 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 2242 GURL(std::string("http://") + kExampleCollision), | 2219 GURL(std::string("http://") + kExampleCollision), |
| 2243 &prefix_hits, &cache_hits)); | 2220 &prefix_hits, &cache_hits)); |
| 2244 ASSERT_EQ(1U, prefix_hits.size()); | 2221 ASSERT_EQ(1U, prefix_hits.size()); |
| 2245 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); | 2222 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); |
| 2246 EXPECT_TRUE(cache_hits.empty()); | 2223 EXPECT_TRUE(cache_hits.empty()); |
| 2247 | 2224 |
| 2248 // This prefix collides, but no full hash match. | 2225 // This prefix collides, but no full hash match. |
| 2249 EXPECT_FALSE(database_->ContainsBrowseUrl( | 2226 EXPECT_FALSE(database_->ContainsBrowseUrl( |
| 2250 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); | 2227 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); |
| 2251 } | 2228 } |
| 2229 |
| 2230 } // namespace safe_browsing |
| OLD | NEW |