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

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

Issue 220493003: Safebrowsing: change gethash caching to match api 2.3 rules, fix some corner cases. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: changes to fullhash / prefix handling Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // Unit tests for the SafeBrowsing storage system. 5 // Unit tests for the SafeBrowsing storage system.
6 6
7 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
8
7 #include "base/file_util.h" 9 #include "base/file_util.h"
8 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
9 #include "base/logging.h" 11 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h" 12 #include "base/message_loop/message_loop.h"
11 #include "base/sha1.h" 13 #include "base/sha1.h"
12 #include "base/time/time.h" 14 #include "base/time/time.h"
13 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
14 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" 15 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h"
15 #include "content/public/test/test_browser_thread_bundle.h" 16 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "crypto/sha2.h" 17 #include "crypto/sha2.h"
17 #include "net/base/net_util.h" 18 #include "net/base/net_util.h"
18 #include "sql/connection.h" 19 #include "sql/connection.h"
19 #include "sql/statement.h" 20 #include "sql/statement.h"
20 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
21 #include "testing/platform_test.h" 22 #include "testing/platform_test.h"
22 #include "url/gurl.h" 23 #include "url/gurl.h"
23 24
24 using base::Time; 25 using base::Time;
25 26
26 namespace { 27 namespace {
27 28
28 SBPrefix SBPrefixForString(const std::string& str) { 29 SBPrefix SBPrefixForString(const std::string& str) {
29 return SBFullHashForString(str).prefix; 30 return SBFullHashForString(str).prefix;
30 } 31 }
31 32
33 SBFullHash SBFullHashForPrefixAndSuffix(SBPrefix prefix, std::string suffix) {
34 SBFullHash full_hash;
35 memset(&full_hash, 0, sizeof(SBFullHash));
36 full_hash.prefix = prefix;
37 CHECK_LE(suffix.size() + sizeof(SBPrefix), sizeof(SBFullHash));
38 memcpy(full_hash.full_hash + sizeof(SBPrefix), suffix.data(), suffix.size());
39 return full_hash;
40 }
41
32 std::string HashedIpPrefix(const std::string& ip_prefix, size_t prefix_size) { 42 std::string HashedIpPrefix(const std::string& ip_prefix, size_t prefix_size) {
33 net::IPAddressNumber ip_number; 43 net::IPAddressNumber ip_number;
34 EXPECT_TRUE(net::ParseIPLiteralToNumber(ip_prefix, &ip_number)); 44 EXPECT_TRUE(net::ParseIPLiteralToNumber(ip_prefix, &ip_number));
35 EXPECT_EQ(net::kIPv6AddressSize, ip_number.size()); 45 EXPECT_EQ(net::kIPv6AddressSize, ip_number.size());
36 const std::string hashed_ip_prefix = base::SHA1HashString( 46 const std::string hashed_ip_prefix = base::SHA1HashString(
37 net::IPAddressToPackedString(ip_number)); 47 net::IPAddressToPackedString(ip_number));
38 std::string hash(crypto::kSHA256Length, '\0'); 48 std::string hash(crypto::kSHA256Length, '\0');
39 hash.replace(0, hashed_ip_prefix.size(), hashed_ip_prefix); 49 hash.replace(0, hashed_ip_prefix.size(), hashed_ip_prefix);
40 hash[base::kSHA1Length] = static_cast<char>(prefix_size); 50 hash[base::kSHA1Length] = static_cast<char>(prefix_size);
41 return hash; 51 return hash;
(...skipping 19 matching lines...) Expand all
61 // one url for prefix. 71 // one url for prefix.
62 void InsertAddChunkHostPrefixUrl(SBChunk* chunk, 72 void InsertAddChunkHostPrefixUrl(SBChunk* chunk,
63 int chunk_number, 73 int chunk_number,
64 const std::string& host_name, 74 const std::string& host_name,
65 const std::string& url) { 75 const std::string& url) {
66 InsertAddChunkHostPrefixValue(chunk, chunk_number, 76 InsertAddChunkHostPrefixValue(chunk, chunk_number,
67 SBPrefixForString(host_name), 77 SBPrefixForString(host_name),
68 SBPrefixForString(url)); 78 SBPrefixForString(url));
69 } 79 }
70 80
81 // Same as InsertAddChunkHostFullHashes, but with pre-computed
82 // prefix and fullhash values.
83 void InsertAddChunkHostFullHashValue(SBChunk* chunk,
84 int chunk_number,
85 const SBPrefix& host_prefix,
86 const SBFullHash& url_fullhash) {
87 chunk->chunk_number = chunk_number;
88 chunk->is_add = true;
89 SBChunkHost host;
90 host.host = host_prefix;
91 host.entry = SBEntry::Create(SBEntry::ADD_FULL_HASH, 1);
92 host.entry->set_chunk_id(chunk->chunk_number);
93 host.entry->SetFullHashAt(0, url_fullhash);
94 chunk->hosts.push_back(host);
95 }
96
71 // Same as InsertAddChunkHostPrefixUrl, but with full hashes. 97 // Same as InsertAddChunkHostPrefixUrl, but with full hashes.
72 void InsertAddChunkHostFullHashes(SBChunk* chunk, 98 void InsertAddChunkHostFullHashes(SBChunk* chunk,
73 int chunk_number, 99 int chunk_number,
74 const std::string& host_name, 100 const std::string& host_name,
75 const std::string& url) { 101 const std::string& url) {
76 chunk->chunk_number = chunk_number; 102 InsertAddChunkHostFullHashValue(chunk,
77 chunk->is_add = true; 103 chunk_number,
78 SBChunkHost host; 104 SBPrefixForString(host_name),
79 host.host = SBPrefixForString(host_name); 105 SBFullHashForString(url));
80 host.entry = SBEntry::Create(SBEntry::ADD_FULL_HASH, 1);
81 host.entry->set_chunk_id(chunk->chunk_number);
82 host.entry->SetFullHashAt(0, SBFullHashForString(url));
83 chunk->hosts.push_back(host);
84 } 106 }
85 107
86 void InsertAddChunkFullHash(SBChunk* chunk, 108 void InsertAddChunkFullHash(SBChunk* chunk,
87 int chunk_number, 109 int chunk_number,
88 const std::string& ip_str, 110 const std::string& ip_str,
89 size_t prefix_size) { 111 size_t prefix_size) {
90 const std::string full_hash_str = HashedIpPrefix(ip_str, prefix_size); 112 const std::string full_hash_str = HashedIpPrefix(ip_str, prefix_size);
91 EXPECT_EQ(sizeof(SBFullHash), full_hash_str.size()); 113 EXPECT_EQ(sizeof(SBFullHash), full_hash_str.size());
92 SBFullHash full_hash; 114 SBFullHash full_hash;
93 std::memcpy(&(full_hash.full_hash), full_hash_str.data(), sizeof(SBFullHash)); 115 std::memcpy(&(full_hash.full_hash), full_hash_str.data(), sizeof(SBFullHash));
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 host.host = SBPrefixForString(host_name); 202 host.host = SBPrefixForString(host_name);
181 host.entry = SBEntry::Create(SBEntry::SUB_PREFIX, 2); 203 host.entry = SBEntry::Create(SBEntry::SUB_PREFIX, 2);
182 host.entry->set_chunk_id(chunk->chunk_number); 204 host.entry->set_chunk_id(chunk->chunk_number);
183 host.entry->SetPrefixAt(0, SBPrefixForString(url1)); 205 host.entry->SetPrefixAt(0, SBPrefixForString(url1));
184 host.entry->SetChunkIdAtPrefix(0, chunk_id_to_sub); 206 host.entry->SetChunkIdAtPrefix(0, chunk_id_to_sub);
185 host.entry->SetPrefixAt(1, SBPrefixForString(url2)); 207 host.entry->SetPrefixAt(1, SBPrefixForString(url2));
186 host.entry->SetChunkIdAtPrefix(1, chunk_id_to_sub); 208 host.entry->SetChunkIdAtPrefix(1, chunk_id_to_sub);
187 chunk->hosts.push_back(host); 209 chunk->hosts.push_back(host);
188 } 210 }
189 211
190 // Same as InsertSubChunkHost2PrefixUrls, but with full hashes. 212 // Same as InsertSubChunkHostFullHash but with pre-computed hashes.
213 void InsertSubChunkHostFullHashValue(SBChunk* chunk,
214 int chunk_number,
215 int chunk_id_to_sub,
216 const SBPrefix& host_prefix,
217 const SBFullHash& url_fullhash) {
218 chunk->chunk_number = chunk_number;
219 chunk->is_add = false;
220 SBChunkHost host;
221 host.host = host_prefix;
222 host.entry = SBEntry::Create(SBEntry::SUB_FULL_HASH, 1);
223 host.entry->set_chunk_id(chunk->chunk_number);
224 host.entry->SetFullHashAt(0, url_fullhash);
225 host.entry->SetChunkIdAtPrefix(0, chunk_id_to_sub);
226 chunk->hosts.push_back(host);
227 }
228
229 // Same as InsertSubChunkHostPrefixUrl, but with full hash.
191 void InsertSubChunkHostFullHash(SBChunk* chunk, 230 void InsertSubChunkHostFullHash(SBChunk* chunk,
192 int chunk_number, 231 int chunk_number,
193 int chunk_id_to_sub, 232 int chunk_id_to_sub,
194 const std::string& host_name, 233 const std::string& host_name,
195 const std::string& url) { 234 const std::string& url) {
196 chunk->chunk_number = chunk_number; 235 InsertSubChunkHostFullHashValue(chunk,
197 chunk->is_add = false; 236 chunk_number,
198 SBChunkHost host; 237 chunk_id_to_sub,
199 host.host = SBPrefixForString(host_name); 238 SBPrefixForString(host_name),
200 host.entry = SBEntry::Create(SBEntry::SUB_FULL_HASH, 2); 239 SBFullHashForString(url));
201 host.entry->set_chunk_id(chunk->chunk_number);
202 host.entry->SetFullHashAt(0, SBFullHashForString(url));
203 host.entry->SetChunkIdAtPrefix(0, chunk_id_to_sub);
204 chunk->hosts.push_back(host);
205 } 240 }
206 241
207 // Prevent DCHECK from killing tests. 242 // Prevent DCHECK from killing tests.
208 // TODO(shess): Pawel disputes the use of this, so the test which uses 243 // TODO(shess): Pawel disputes the use of this, so the test which uses
209 // it is DISABLED. http://crbug.com/56448 244 // it is DISABLED. http://crbug.com/56448
210 class ScopedLogMessageIgnorer { 245 class ScopedLogMessageIgnorer {
211 public: 246 public:
212 ScopedLogMessageIgnorer() { 247 ScopedLogMessageIgnorer() {
213 logging::SetLogMessageHandler(&LogMessageIgnorer); 248 logging::SetLogMessageHandler(&LogMessageIgnorer);
214 } 249 }
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 } 288 }
254 289
255 virtual void TearDown() { 290 virtual void TearDown() {
256 database_.reset(); 291 database_.reset();
257 292
258 PlatformTest::TearDown(); 293 PlatformTest::TearDown();
259 } 294 }
260 295
261 void GetListsInfo(std::vector<SBListChunkRanges>* lists) { 296 void GetListsInfo(std::vector<SBListChunkRanges>* lists) {
262 lists->clear(); 297 lists->clear();
263 EXPECT_TRUE(database_->UpdateStarted(lists)); 298 ASSERT_TRUE(database_->UpdateStarted(lists));
264 database_->UpdateFinished(true); 299 database_->UpdateFinished(true);
265 } 300 }
266 301
267 // Helper function to do an AddDel or SubDel command. 302 // Helper function to do an AddDel or SubDel command.
268 void DelChunk(const std::string& list, 303 void DelChunk(const std::string& list,
269 int chunk_id, 304 int chunk_id,
270 bool is_sub_del) { 305 bool is_sub_del) {
271 std::vector<SBChunkDelete> deletes; 306 std::vector<SBChunkDelete> deletes;
272 SBChunkDelete chunk_delete; 307 SBChunkDelete chunk_delete;
273 chunk_delete.list_name = list; 308 chunk_delete.list_name = list;
274 chunk_delete.is_sub_del = is_sub_del; 309 chunk_delete.is_sub_del = is_sub_del;
275 chunk_delete.chunk_del.push_back(ChunkRange(chunk_id)); 310 chunk_delete.chunk_del.push_back(ChunkRange(chunk_id));
276 deletes.push_back(chunk_delete); 311 deletes.push_back(chunk_delete);
277 database_->DeleteChunks(deletes); 312 database_->DeleteChunks(deletes);
278 } 313 }
279 314
280 void AddDelChunk(const std::string& list, int chunk_id) { 315 void AddDelChunk(const std::string& list, int chunk_id) {
281 DelChunk(list, chunk_id, false); 316 DelChunk(list, chunk_id, false);
282 } 317 }
283 318
284 void SubDelChunk(const std::string& list, int chunk_id) { 319 void SubDelChunk(const std::string& list, int chunk_id) {
285 DelChunk(list, chunk_id, true); 320 DelChunk(list, chunk_id, true);
286 } 321 }
287 322
323 bool ContainsBrowseUrlHashes(std::vector<SBFullHash>* full_hashes,
324 std::vector<SBPrefix>* prefix_hits,
325 std::vector<SBFullHashResult>* cache_hits) {
326 std::sort(full_hashes->begin(), full_hashes->end(), SBFullHashLess);
327 return database_->ContainsBrowseUrlHashes(
328 *full_hashes, prefix_hits, cache_hits);
329 }
330
288 // Utility function for setting up the database for the caching test. 331 // Utility function for setting up the database for the caching test.
289 void PopulateDatabaseForCacheTest(); 332 void PopulateDatabaseForCacheTest();
290 333
291 scoped_ptr<SafeBrowsingDatabaseNew> database_; 334 scoped_ptr<SafeBrowsingDatabaseNew> database_;
292 base::FilePath database_filename_; 335 base::FilePath database_filename_;
293 base::ScopedTempDir temp_dir_; 336 base::ScopedTempDir temp_dir_;
294 }; 337 };
295 338
296 // Tests retrieving list name information. 339 // Tests retrieving list name information.
297 TEST_F(SafeBrowsingDatabaseTest, ListNameForBrowse) { 340 TEST_F(SafeBrowsingDatabaseTest, ListNameForBrowse) {
298 SBChunkList chunks; 341 SBChunkList chunks;
299 SBChunk chunk; 342 SBChunk chunk;
300 343
301 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/", 344 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/",
302 "www.evil.com/malware.html"); 345 "www.evil.com/malware.html");
303 chunks.clear(); 346 chunks.clear();
304 chunks.push_back(chunk); 347 chunks.push_back(chunk);
305 std::vector<SBListChunkRanges> lists; 348 std::vector<SBListChunkRanges> lists;
306 EXPECT_TRUE(database_->UpdateStarted(&lists)); 349 ASSERT_TRUE(database_->UpdateStarted(&lists));
307 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 350 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
308 351
309 chunk.hosts.clear(); 352 chunk.hosts.clear();
310 InsertAddChunkHostPrefixUrl(&chunk, 2, "www.foo.com/", 353 InsertAddChunkHostPrefixUrl(&chunk, 2, "www.foo.com/",
311 "www.foo.com/malware.html"); 354 "www.foo.com/malware.html");
312 chunks.clear(); 355 chunks.clear();
313 chunks.push_back(chunk); 356 chunks.push_back(chunk);
314 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 357 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
315 358
316 chunk.hosts.clear(); 359 chunk.hosts.clear();
317 InsertAddChunkHostPrefixUrl(&chunk, 3, "www.whatever.com/", 360 InsertAddChunkHostPrefixUrl(&chunk, 3, "www.whatever.com/",
318 "www.whatever.com/malware.html"); 361 "www.whatever.com/malware.html");
319 chunks.clear(); 362 chunks.clear();
320 chunks.push_back(chunk); 363 chunks.push_back(chunk);
321 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 364 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
322 database_->UpdateFinished(true); 365 database_->UpdateFinished(true);
323 366
324 GetListsInfo(&lists); 367 GetListsInfo(&lists);
325 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); 368 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList);
326 EXPECT_EQ(lists[0].adds, "1-3"); 369 EXPECT_EQ(lists[0].adds, "1-3");
327 EXPECT_TRUE(lists[0].subs.empty()); 370 EXPECT_TRUE(lists[0].subs.empty());
328 371
329 // Insert a malware sub chunk. 372 // Insert a malware sub chunk.
330 chunk.hosts.clear(); 373 chunk.hosts.clear();
331 InsertSubChunkHostPrefixUrl(&chunk, 7, 19, "www.subbed.com/", 374 InsertSubChunkHostPrefixUrl(&chunk, 7, 19, "www.subbed.com/",
332 "www.subbed.com/noteveil1.html"); 375 "www.subbed.com/noteveil1.html");
333 chunks.clear(); 376 chunks.clear();
334 chunks.push_back(chunk); 377 chunks.push_back(chunk);
335 378
336 EXPECT_TRUE(database_->UpdateStarted(&lists)); 379 ASSERT_TRUE(database_->UpdateStarted(&lists));
337 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 380 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
338 database_->UpdateFinished(true); 381 database_->UpdateFinished(true);
339 382
340 GetListsInfo(&lists); 383 GetListsInfo(&lists);
341 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); 384 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList);
342 EXPECT_EQ(lists[0].adds, "1-3"); 385 EXPECT_EQ(lists[0].adds, "1-3");
343 EXPECT_EQ(lists[0].subs, "7"); 386 EXPECT_EQ(lists[0].subs, "7");
344 if (lists.size() == 2) { 387 if (lists.size() == 2) {
345 // Old style database won't have the second entry since it creates the lists 388 // Old style database won't have the second entry since it creates the lists
346 // when it receives an update containing that list. The filter-based 389 // when it receives an update containing that list. The filter-based
347 // database has these values hard coded. 390 // database has these values hard coded.
348 EXPECT_TRUE(lists[1].name == safe_browsing_util::kPhishingList); 391 EXPECT_TRUE(lists[1].name == safe_browsing_util::kPhishingList);
349 EXPECT_TRUE(lists[1].adds.empty()); 392 EXPECT_TRUE(lists[1].adds.empty());
350 EXPECT_TRUE(lists[1].subs.empty()); 393 EXPECT_TRUE(lists[1].subs.empty());
351 } 394 }
352 395
353 // Add a phishing add chunk. 396 // Add a phishing add chunk.
354 chunk.hosts.clear(); 397 chunk.hosts.clear();
355 InsertAddChunkHostPrefixUrl(&chunk, 47, "www.evil.com/", 398 InsertAddChunkHostPrefixUrl(&chunk, 47, "www.evil.com/",
356 "www.evil.com/phishing.html"); 399 "www.evil.com/phishing.html");
357 chunks.clear(); 400 chunks.clear();
358 chunks.push_back(chunk); 401 chunks.push_back(chunk);
359 EXPECT_TRUE(database_->UpdateStarted(&lists)); 402 ASSERT_TRUE(database_->UpdateStarted(&lists));
360 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); 403 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks);
361 404
362 // Insert some phishing sub chunks. 405 // Insert some phishing sub chunks.
363 chunk.hosts.clear(); 406 chunk.hosts.clear();
364 InsertSubChunkHostPrefixUrl(&chunk, 200, 1999, "www.phishy.com/", 407 InsertSubChunkHostPrefixUrl(&chunk, 200, 1999, "www.phishy.com/",
365 "www.phishy.com/notevil1.html"); 408 "www.phishy.com/notevil1.html");
366 chunks.clear(); 409 chunks.clear();
367 chunks.push_back(chunk); 410 chunks.push_back(chunk);
368 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); 411 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks);
369 412
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
404 database_->Init(database_filename_); 447 database_->Init(database_filename_);
405 448
406 SBChunkList chunks; 449 SBChunkList chunks;
407 SBChunk chunk; 450 SBChunk chunk;
408 451
409 // Insert malware, phish, binurl and bindownload add chunks. 452 // Insert malware, phish, binurl and bindownload add chunks.
410 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/", 453 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/",
411 "www.evil.com/malware.html"); 454 "www.evil.com/malware.html");
412 chunks.push_back(chunk); 455 chunks.push_back(chunk);
413 std::vector<SBListChunkRanges> lists; 456 std::vector<SBListChunkRanges> lists;
414 EXPECT_TRUE(database_->UpdateStarted(&lists)); 457 ASSERT_TRUE(database_->UpdateStarted(&lists));
415 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 458 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
416 459
417 chunk.hosts.clear(); 460 chunk.hosts.clear();
418 InsertAddChunkHostPrefixUrl(&chunk, 2, "www.foo.com/", 461 InsertAddChunkHostPrefixUrl(&chunk, 2, "www.foo.com/",
419 "www.foo.com/malware.html"); 462 "www.foo.com/malware.html");
420 chunks.clear(); 463 chunks.clear();
421 chunks.push_back(chunk); 464 chunks.push_back(chunk);
422 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); 465 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks);
423 466
424 chunk.hosts.clear(); 467 chunk.hosts.clear();
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 TEST_F(SafeBrowsingDatabaseTest, BrowseDatabase) { 535 TEST_F(SafeBrowsingDatabaseTest, BrowseDatabase) {
493 SBChunkList chunks; 536 SBChunkList chunks;
494 SBChunk chunk; 537 SBChunk chunk;
495 538
496 // Add a simple chunk with one hostkey. 539 // Add a simple chunk with one hostkey.
497 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/", 540 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/",
498 "www.evil.com/phishing.html", 541 "www.evil.com/phishing.html",
499 "www.evil.com/malware.html"); 542 "www.evil.com/malware.html");
500 chunks.push_back(chunk); 543 chunks.push_back(chunk);
501 std::vector<SBListChunkRanges> lists; 544 std::vector<SBListChunkRanges> lists;
502 EXPECT_TRUE(database_->UpdateStarted(&lists)); 545 ASSERT_TRUE(database_->UpdateStarted(&lists));
503 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 546 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
504 547
505 chunk.hosts.clear(); 548 chunk.hosts.clear();
506 InsertAddChunkHost2PrefixUrls(&chunk, 2, "www.evil.com/", 549 InsertAddChunkHost2PrefixUrls(&chunk, 2, "www.evil.com/",
507 "www.evil.com/notevil1.html", 550 "www.evil.com/notevil1.html",
508 "www.evil.com/notevil2.html"); 551 "www.evil.com/notevil2.html");
509 InsertAddChunkHost2PrefixUrls(&chunk, 2, "www.good.com/", 552 InsertAddChunkHost2PrefixUrls(&chunk, 2, "www.good.com/",
510 "www.good.com/good1.html", 553 "www.good.com/good1.html",
511 "www.good.com/good2.html"); 554 "www.good.com/good2.html");
512 chunks.clear(); 555 chunks.clear();
513 chunks.push_back(chunk); 556 chunks.push_back(chunk);
514 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 557 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
515 558
516 // and a chunk with an IP-based host 559 // and a chunk with an IP-based host
517 chunk.hosts.clear(); 560 chunk.hosts.clear();
518 InsertAddChunkHostPrefixUrl(&chunk, 3, "192.168.0.1/", 561 InsertAddChunkHostPrefixUrl(&chunk, 3, "192.168.0.1/",
519 "192.168.0.1/malware.html"); 562 "192.168.0.1/malware.html");
520 chunks.clear(); 563 chunks.clear();
521 chunks.push_back(chunk); 564 chunks.push_back(chunk);
522 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 565 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
523 database_->UpdateFinished(true); 566 database_->UpdateFinished(true);
524 567
525 // Make sure they were added correctly. 568 // Make sure they were added correctly.
526 GetListsInfo(&lists); 569 GetListsInfo(&lists);
527 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); 570 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList);
528 EXPECT_EQ(lists[0].adds, "1-3"); 571 EXPECT_EQ(lists[0].adds, "1-3");
529 EXPECT_TRUE(lists[0].subs.empty()); 572 EXPECT_TRUE(lists[0].subs.empty());
530 573
531 const Time now = Time::Now(); 574 std::vector<SBFullHashResult> cache_hits;
532 std::vector<SBFullHashResult> full_hashes;
533 std::vector<SBPrefix> prefix_hits; 575 std::vector<SBPrefix> prefix_hits;
534 std::string matching_list;
535 EXPECT_TRUE(database_->ContainsBrowseUrl( 576 EXPECT_TRUE(database_->ContainsBrowseUrl(
536 GURL("http://www.evil.com/phishing.html"), 577 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits));
537 &matching_list, &prefix_hits,
538 &full_hashes, now));
539 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); 578 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html"));
540 EXPECT_EQ(prefix_hits.size(), 1U); 579 EXPECT_EQ(prefix_hits.size(), 1U);
580 EXPECT_TRUE(cache_hits.empty());
541 581
542 EXPECT_TRUE(database_->ContainsBrowseUrl( 582 EXPECT_TRUE(database_->ContainsBrowseUrl(
543 GURL("http://www.evil.com/malware.html"), 583 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits));
544 &matching_list, &prefix_hits,
545 &full_hashes, now));
546 584
547 EXPECT_TRUE(database_->ContainsBrowseUrl( 585 EXPECT_TRUE(database_->ContainsBrowseUrl(
548 GURL("http://www.evil.com/notevil1.html"), 586 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits));
549 &matching_list, &prefix_hits,
550 &full_hashes, now));
551 587
552 EXPECT_TRUE(database_->ContainsBrowseUrl( 588 EXPECT_TRUE(database_->ContainsBrowseUrl(
553 GURL("http://www.evil.com/notevil2.html"), 589 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits));
554 &matching_list, &prefix_hits,
555 &full_hashes, now));
556 590
557 EXPECT_TRUE(database_->ContainsBrowseUrl( 591 EXPECT_TRUE(database_->ContainsBrowseUrl(
558 GURL("http://www.good.com/good1.html"), 592 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits));
559 &matching_list, &prefix_hits,
560 &full_hashes, now));
561 593
562 EXPECT_TRUE(database_->ContainsBrowseUrl( 594 EXPECT_TRUE(database_->ContainsBrowseUrl(
563 GURL("http://www.good.com/good2.html"), 595 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits));
564 &matching_list, &prefix_hits,
565 &full_hashes, now));
566 596
567 EXPECT_TRUE(database_->ContainsBrowseUrl( 597 EXPECT_TRUE(database_->ContainsBrowseUrl(
568 GURL("http://192.168.0.1/malware.html"), 598 GURL("http://192.168.0.1/malware.html"), &prefix_hits, &cache_hits));
569 &matching_list, &prefix_hits,
570 &full_hashes, now));
571 599
572 EXPECT_FALSE(database_->ContainsBrowseUrl( 600 EXPECT_FALSE(database_->ContainsBrowseUrl(
573 GURL("http://www.evil.com/"), 601 GURL("http://www.evil.com/"), &prefix_hits, &cache_hits));
574 &matching_list, &prefix_hits,
575 &full_hashes, now));
576 EXPECT_TRUE(prefix_hits.empty()); 602 EXPECT_TRUE(prefix_hits.empty());
603 EXPECT_TRUE(cache_hits.empty());
577 604
578 EXPECT_FALSE(database_->ContainsBrowseUrl( 605 EXPECT_FALSE(database_->ContainsBrowseUrl(
579 GURL("http://www.evil.com/robots.txt"), 606 GURL("http://www.evil.com/robots.txt"), &prefix_hits, &cache_hits));
580 &matching_list, &prefix_hits,
581 &full_hashes, now));
582 607
583 // Attempt to re-add the first chunk (should be a no-op). 608 // Attempt to re-add the first chunk (should be a no-op).
584 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 609 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522
585 chunk.hosts.clear(); 610 chunk.hosts.clear();
586 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/", 611 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/",
587 "www.evil.com/phishing.html", 612 "www.evil.com/phishing.html",
588 "www.evil.com/malware.html"); 613 "www.evil.com/malware.html");
589 chunks.clear(); 614 chunks.clear();
590 chunks.push_back(chunk); 615 chunks.push_back(chunk);
591 EXPECT_TRUE(database_->UpdateStarted(&lists)); 616 ASSERT_TRUE(database_->UpdateStarted(&lists));
592 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 617 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
593 database_->UpdateFinished(true); 618 database_->UpdateFinished(true);
594 619
595 GetListsInfo(&lists); 620 GetListsInfo(&lists);
596 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); 621 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList);
597 EXPECT_EQ(lists[0].adds, "1-3"); 622 EXPECT_EQ(lists[0].adds, "1-3");
598 EXPECT_TRUE(lists[0].subs.empty()); 623 EXPECT_TRUE(lists[0].subs.empty());
599 624
600 // Test removing a single prefix from the add chunk. 625 // Test removing a single prefix from the add chunk.
601 chunk.hosts.clear(); 626 chunk.hosts.clear();
602 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/", 627 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/",
603 "www.evil.com/notevil1.html"); 628 "www.evil.com/notevil1.html");
604 chunks.clear(); 629 chunks.clear();
605 chunks.push_back(chunk); 630 chunks.push_back(chunk);
606 EXPECT_TRUE(database_->UpdateStarted(&lists)); 631 ASSERT_TRUE(database_->UpdateStarted(&lists));
607 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 632 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
608 633
609 database_->UpdateFinished(true); 634 database_->UpdateFinished(true);
610 635
611 EXPECT_TRUE(database_->ContainsBrowseUrl( 636 EXPECT_TRUE(database_->ContainsBrowseUrl(
612 GURL("http://www.evil.com/phishing.html"), 637 GURL("http://www.evil.com/phishing.html"), &prefix_hits, &cache_hits));
613 &matching_list, &prefix_hits,
614 &full_hashes, now));
615 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html")); 638 EXPECT_EQ(prefix_hits[0], SBPrefixForString("www.evil.com/phishing.html"));
616 EXPECT_EQ(prefix_hits.size(), 1U); 639 EXPECT_EQ(prefix_hits.size(), 1U);
640 EXPECT_TRUE(cache_hits.empty());
617 641
618 EXPECT_FALSE(database_->ContainsBrowseUrl( 642 EXPECT_FALSE(database_->ContainsBrowseUrl(
619 GURL("http://www.evil.com/notevil1.html"), 643 GURL("http://www.evil.com/notevil1.html"), &prefix_hits, &cache_hits));
620 &matching_list, &prefix_hits,
621 &full_hashes, now));
622 EXPECT_TRUE(prefix_hits.empty()); 644 EXPECT_TRUE(prefix_hits.empty());
645 EXPECT_TRUE(cache_hits.empty());
623 646
624 EXPECT_TRUE(database_->ContainsBrowseUrl( 647 EXPECT_TRUE(database_->ContainsBrowseUrl(
625 GURL("http://www.evil.com/notevil2.html"), 648 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits));
626 &matching_list, &prefix_hits,
627 &full_hashes, now));
628 649
629 EXPECT_TRUE(database_->ContainsBrowseUrl( 650 EXPECT_TRUE(database_->ContainsBrowseUrl(
630 GURL("http://www.good.com/good1.html"), 651 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits));
631 &matching_list, &prefix_hits,
632 &full_hashes, now));
633 652
634 EXPECT_TRUE(database_->ContainsBrowseUrl( 653 EXPECT_TRUE(database_->ContainsBrowseUrl(
635 GURL("http://www.good.com/good2.html"), 654 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits));
636 &matching_list, &prefix_hits,
637 &full_hashes, now));
638 655
639 GetListsInfo(&lists); 656 GetListsInfo(&lists);
640 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); 657 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList);
641 EXPECT_EQ(lists[0].subs, "4"); 658 EXPECT_EQ(lists[0].subs, "4");
642 659
643 // Test the same sub chunk again. This should be a no-op. 660 // Test the same sub chunk again. This should be a no-op.
644 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522 661 // see bug: http://code.google.com/p/chromium/issues/detail?id=4522
645 chunk.hosts.clear(); 662 chunk.hosts.clear();
646 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/", 663 InsertSubChunkHostPrefixUrl(&chunk, 4, 2, "www.evil.com/",
647 "www.evil.com/notevil1.html"); 664 "www.evil.com/notevil1.html");
648 chunks.clear(); 665 chunks.clear();
649 chunks.push_back(chunk); 666 chunks.push_back(chunk);
650 667
651 EXPECT_TRUE(database_->UpdateStarted(&lists)); 668 ASSERT_TRUE(database_->UpdateStarted(&lists));
652 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 669 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
653 database_->UpdateFinished(true); 670 database_->UpdateFinished(true);
654 671
655 GetListsInfo(&lists); 672 GetListsInfo(&lists);
656 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); 673 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList);
657 EXPECT_EQ(lists[0].subs, "4"); 674 EXPECT_EQ(lists[0].subs, "4");
658 675
659 // Test removing all the prefixes from an add chunk. 676 // Test removing all the prefixes from an add chunk.
660 EXPECT_TRUE(database_->UpdateStarted(&lists)); 677 ASSERT_TRUE(database_->UpdateStarted(&lists));
661 AddDelChunk(safe_browsing_util::kMalwareList, 2); 678 AddDelChunk(safe_browsing_util::kMalwareList, 2);
662 database_->UpdateFinished(true); 679 database_->UpdateFinished(true);
663 680
664 EXPECT_FALSE(database_->ContainsBrowseUrl( 681 EXPECT_FALSE(database_->ContainsBrowseUrl(
665 GURL("http://www.evil.com/notevil2.html"), 682 GURL("http://www.evil.com/notevil2.html"), &prefix_hits, &cache_hits));
666 &matching_list, &prefix_hits,
667 &full_hashes, now));
668 683
669 EXPECT_FALSE(database_->ContainsBrowseUrl( 684 EXPECT_FALSE(database_->ContainsBrowseUrl(
670 GURL("http://www.good.com/good1.html"), 685 GURL("http://www.good.com/good1.html"), &prefix_hits, &cache_hits));
671 &matching_list, &prefix_hits,
672 &full_hashes, now));
673 686
674 EXPECT_FALSE(database_->ContainsBrowseUrl( 687 EXPECT_FALSE(database_->ContainsBrowseUrl(
675 GURL("http://www.good.com/good2.html"), 688 GURL("http://www.good.com/good2.html"), &prefix_hits, &cache_hits));
676 &matching_list, &prefix_hits,
677 &full_hashes, now));
678 689
679 GetListsInfo(&lists); 690 GetListsInfo(&lists);
680 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); 691 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList);
681 EXPECT_EQ(lists[0].adds, "1,3"); 692 EXPECT_EQ(lists[0].adds, "1,3");
682 EXPECT_EQ(lists[0].subs, "4"); 693 EXPECT_EQ(lists[0].subs, "4");
683 694
684 // The adddel command exposed a bug in the transaction code where any 695 // The adddel command exposed a bug in the transaction code where any
685 // transaction after it would fail. Add a dummy entry and remove it to 696 // transaction after it would fail. Add a dummy entry and remove it to
686 // make sure the transcation works fine. 697 // make sure the transcation works fine.
687 chunk.hosts.clear(); 698 chunk.hosts.clear();
688 InsertAddChunkHostPrefixUrl(&chunk, 44, "www.redherring.com/", 699 InsertAddChunkHostPrefixUrl(&chunk, 44, "www.redherring.com/",
689 "www.redherring.com/index.html"); 700 "www.redherring.com/index.html");
690 chunks.clear(); 701 chunks.clear();
691 chunks.push_back(chunk); 702 chunks.push_back(chunk);
692 EXPECT_TRUE(database_->UpdateStarted(&lists)); 703 ASSERT_TRUE(database_->UpdateStarted(&lists));
693 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 704 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
694 705
695 // Now remove the dummy entry. If there are any problems with the 706 // Now remove the dummy entry. If there are any problems with the
696 // transactions, asserts will fire. 707 // transactions, asserts will fire.
697 AddDelChunk(safe_browsing_util::kMalwareList, 44); 708 AddDelChunk(safe_browsing_util::kMalwareList, 44);
698 709
699 // Test the subdel command. 710 // Test the subdel command.
700 SubDelChunk(safe_browsing_util::kMalwareList, 4); 711 SubDelChunk(safe_browsing_util::kMalwareList, 4);
701 database_->UpdateFinished(true); 712 database_->UpdateFinished(true);
702 713
703 GetListsInfo(&lists); 714 GetListsInfo(&lists);
704 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList); 715 EXPECT_TRUE(lists[0].name == safe_browsing_util::kMalwareList);
705 EXPECT_EQ(lists[0].adds, "1,3"); 716 EXPECT_EQ(lists[0].adds, "1,3");
706 EXPECT_EQ(lists[0].subs, ""); 717 EXPECT_EQ(lists[0].subs, "");
707 718
708 // Test a sub command coming in before the add. 719 // Test a sub command coming in before the add.
709 chunk.hosts.clear(); 720 chunk.hosts.clear();
710 InsertSubChunkHost2PrefixUrls(&chunk, 5, 10, 721 InsertSubChunkHost2PrefixUrls(&chunk, 5, 10,
711 "www.notevilanymore.com/", 722 "www.notevilanymore.com/",
712 "www.notevilanymore.com/index.html", 723 "www.notevilanymore.com/index.html",
713 "www.notevilanymore.com/good.html"); 724 "www.notevilanymore.com/good.html");
714 chunks.clear(); 725 chunks.clear();
715 chunks.push_back(chunk); 726 chunks.push_back(chunk);
716 EXPECT_TRUE(database_->UpdateStarted(&lists)); 727 ASSERT_TRUE(database_->UpdateStarted(&lists));
717 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 728 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
718 database_->UpdateFinished(true); 729 database_->UpdateFinished(true);
719 730
720 EXPECT_FALSE(database_->ContainsBrowseUrl( 731 EXPECT_FALSE(database_->ContainsBrowseUrl(
721 GURL("http://www.notevilanymore.com/index.html"), 732 GURL("http://www.notevilanymore.com/index.html"),
722 &matching_list, &prefix_hits, &full_hashes, now)); 733 &prefix_hits,
734 &cache_hits));
723 735
724 // Now insert the tardy add chunk and we don't expect them to appear 736 // Now insert the tardy add chunk and we don't expect them to appear
725 // in database because of the previous sub chunk. 737 // in database because of the previous sub chunk.
726 chunk.hosts.clear(); 738 chunk.hosts.clear();
727 InsertAddChunkHost2PrefixUrls(&chunk, 10, "www.notevilanymore.com/", 739 InsertAddChunkHost2PrefixUrls(&chunk, 10, "www.notevilanymore.com/",
728 "www.notevilanymore.com/index.html", 740 "www.notevilanymore.com/index.html",
729 "www.notevilanymore.com/good.html"); 741 "www.notevilanymore.com/good.html");
730 chunks.clear(); 742 chunks.clear();
731 chunks.push_back(chunk); 743 chunks.push_back(chunk);
732 EXPECT_TRUE(database_->UpdateStarted(&lists)); 744 ASSERT_TRUE(database_->UpdateStarted(&lists));
733 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 745 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
734 database_->UpdateFinished(true); 746 database_->UpdateFinished(true);
735 747
736 EXPECT_FALSE(database_->ContainsBrowseUrl( 748 EXPECT_FALSE(database_->ContainsBrowseUrl(
737 GURL("http://www.notevilanymore.com/index.html"), 749 GURL("http://www.notevilanymore.com/index.html"),
738 &matching_list, &prefix_hits, &full_hashes, now)); 750 &prefix_hits,
751 &cache_hits));
739 752
740 EXPECT_FALSE(database_->ContainsBrowseUrl( 753 EXPECT_FALSE(database_->ContainsBrowseUrl(
741 GURL("http://www.notevilanymore.com/good.html"), 754 GURL("http://www.notevilanymore.com/good.html"),
742 &matching_list, &prefix_hits, &full_hashes, now)); 755 &prefix_hits,
756 &cache_hits));
743 } 757 }
744 758
745 759
746 // Test adding zero length chunks to the database. 760 // Test adding zero length chunks to the database.
747 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { 761 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) {
748 SBChunkList chunks; 762 SBChunkList chunks;
749 SBChunk chunk; 763 SBChunk chunk;
750 764
751 // Populate with a couple of normal chunks. 765 // Populate with a couple of normal chunks.
752 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.test.com/", 766 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.test.com/",
753 "www.test.com/test1.html", 767 "www.test.com/test1.html",
754 "www.test.com/test2.html"); 768 "www.test.com/test2.html");
755 chunks.clear(); 769 chunks.clear();
756 chunks.push_back(chunk); 770 chunks.push_back(chunk);
757 771
758 chunk.hosts.clear(); 772 chunk.hosts.clear();
759 InsertAddChunkHost2PrefixUrls(&chunk, 10, "www.random.com/", 773 InsertAddChunkHost2PrefixUrls(&chunk, 10, "www.random.com/",
760 "www.random.com/random1.html", 774 "www.random.com/random1.html",
761 "www.random.com/random2.html"); 775 "www.random.com/random2.html");
762 chunks.push_back(chunk); 776 chunks.push_back(chunk);
763 777
764 std::vector<SBListChunkRanges> lists; 778 std::vector<SBListChunkRanges> lists;
765 EXPECT_TRUE(database_->UpdateStarted(&lists)); 779 ASSERT_TRUE(database_->UpdateStarted(&lists));
766 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 780 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
767 database_->UpdateFinished(true); 781 database_->UpdateFinished(true);
768 782
769 // Add an empty ADD and SUB chunk. 783 // Add an empty ADD and SUB chunk.
770 GetListsInfo(&lists); 784 GetListsInfo(&lists);
771 EXPECT_EQ(lists[0].adds, "1,10"); 785 EXPECT_EQ(lists[0].adds, "1,10");
772 786
773 SBChunk empty_chunk; 787 SBChunk empty_chunk;
774 empty_chunk.chunk_number = 19; 788 empty_chunk.chunk_number = 19;
775 empty_chunk.is_add = true; 789 empty_chunk.is_add = true;
776 chunks.clear(); 790 chunks.clear();
777 chunks.push_back(empty_chunk); 791 chunks.push_back(empty_chunk);
778 EXPECT_TRUE(database_->UpdateStarted(&lists)); 792 ASSERT_TRUE(database_->UpdateStarted(&lists));
779 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 793 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
780 chunks.clear(); 794 chunks.clear();
781 empty_chunk.chunk_number = 7; 795 empty_chunk.chunk_number = 7;
782 empty_chunk.is_add = false; 796 empty_chunk.is_add = false;
783 chunks.push_back(empty_chunk); 797 chunks.push_back(empty_chunk);
784 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 798 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
785 database_->UpdateFinished(true); 799 database_->UpdateFinished(true);
786 800
787 GetListsInfo(&lists); 801 GetListsInfo(&lists);
788 EXPECT_EQ(lists[0].adds, "1,10,19"); 802 EXPECT_EQ(lists[0].adds, "1,10,19");
(...skipping 10 matching lines...) Expand all
799 empty_chunk.chunk_number = 21; 813 empty_chunk.chunk_number = 21;
800 empty_chunk.is_add = true; 814 empty_chunk.is_add = true;
801 empty_chunk.hosts.clear(); 815 empty_chunk.hosts.clear();
802 chunks.push_back(empty_chunk); 816 chunks.push_back(empty_chunk);
803 817
804 empty_chunk.hosts.clear(); 818 empty_chunk.hosts.clear();
805 InsertAddChunkHostPrefixUrl(&empty_chunk, 22, "www.notempy.com/", 819 InsertAddChunkHostPrefixUrl(&empty_chunk, 22, "www.notempy.com/",
806 "www.notempty.com/full2.html"); 820 "www.notempty.com/full2.html");
807 chunks.push_back(empty_chunk); 821 chunks.push_back(empty_chunk);
808 822
809 EXPECT_TRUE(database_->UpdateStarted(&lists)); 823 ASSERT_TRUE(database_->UpdateStarted(&lists));
810 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 824 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
811 database_->UpdateFinished(true); 825 database_->UpdateFinished(true);
812 826
813 const Time now = Time::Now(); 827 std::vector<SBFullHashResult> cache_hits;
814 std::vector<SBFullHashResult> full_hashes;
815 std::vector<SBPrefix> prefix_hits; 828 std::vector<SBPrefix> prefix_hits;
816 std::string matching_list;
817 EXPECT_TRUE(database_->ContainsBrowseUrl( 829 EXPECT_TRUE(database_->ContainsBrowseUrl(
818 GURL("http://www.notempty.com/full1.html"), 830 GURL("http://www.notempty.com/full1.html"), &prefix_hits, &cache_hits));
819 &matching_list, &prefix_hits,
820 &full_hashes, now));
821 EXPECT_TRUE(database_->ContainsBrowseUrl( 831 EXPECT_TRUE(database_->ContainsBrowseUrl(
822 GURL("http://www.notempty.com/full2.html"), 832 GURL("http://www.notempty.com/full2.html"), &prefix_hits, &cache_hits));
823 &matching_list, &prefix_hits,
824 &full_hashes, now));
825 833
826 GetListsInfo(&lists); 834 GetListsInfo(&lists);
827 EXPECT_EQ(lists[0].adds, "1,10,19-22"); 835 EXPECT_EQ(lists[0].adds, "1,10,19-22");
828 EXPECT_EQ(lists[0].subs, "7"); 836 EXPECT_EQ(lists[0].subs, "7");
829 837
830 // Handle AddDel and SubDel commands for empty chunks. 838 // Handle AddDel and SubDel commands for empty chunks.
831 EXPECT_TRUE(database_->UpdateStarted(&lists)); 839 ASSERT_TRUE(database_->UpdateStarted(&lists));
832 AddDelChunk(safe_browsing_util::kMalwareList, 21); 840 AddDelChunk(safe_browsing_util::kMalwareList, 21);
833 database_->UpdateFinished(true); 841 database_->UpdateFinished(true);
834 842
835 GetListsInfo(&lists); 843 GetListsInfo(&lists);
836 EXPECT_EQ(lists[0].adds, "1,10,19-20,22"); 844 EXPECT_EQ(lists[0].adds, "1,10,19-20,22");
837 EXPECT_EQ(lists[0].subs, "7"); 845 EXPECT_EQ(lists[0].subs, "7");
838 846
839 EXPECT_TRUE(database_->UpdateStarted(&lists)); 847 ASSERT_TRUE(database_->UpdateStarted(&lists));
840 SubDelChunk(safe_browsing_util::kMalwareList, 7); 848 SubDelChunk(safe_browsing_util::kMalwareList, 7);
841 database_->UpdateFinished(true); 849 database_->UpdateFinished(true);
842 850
843 GetListsInfo(&lists); 851 GetListsInfo(&lists);
844 EXPECT_EQ(lists[0].adds, "1,10,19-20,22"); 852 EXPECT_EQ(lists[0].adds, "1,10,19-20,22");
845 EXPECT_EQ(lists[0].subs, ""); 853 EXPECT_EQ(lists[0].subs, "");
846 } 854 }
847 855
848 // Utility function for setting up the database for the caching test. 856 // Utility function for setting up the database for the caching test.
849 void SafeBrowsingDatabaseTest::PopulateDatabaseForCacheTest() { 857 void SafeBrowsingDatabaseTest::PopulateDatabaseForCacheTest() {
850 SBChunkList chunks; 858 SBChunkList chunks;
851 SBChunk chunk; 859 SBChunk chunk;
852 // Add a simple chunk with one hostkey and cache it. 860 // Add a simple chunk with one hostkey and cache it.
853 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/", 861 InsertAddChunkHost2PrefixUrls(&chunk, 1, "www.evil.com/",
854 "www.evil.com/phishing.html", 862 "www.evil.com/phishing.html",
855 "www.evil.com/malware.html"); 863 "www.evil.com/malware.html");
856 chunks.push_back(chunk); 864 chunks.push_back(chunk);
857 865
858 std::vector<SBListChunkRanges> lists; 866 std::vector<SBListChunkRanges> lists;
859 EXPECT_TRUE(database_->UpdateStarted(&lists)); 867 ASSERT_TRUE(database_->UpdateStarted(&lists));
860 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 868 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
861 database_->UpdateFinished(true); 869 database_->UpdateFinished(true);
862 870
871 // Cache should be cleared after updating.
872 EXPECT_TRUE(database_->browse_gethash_cache_.empty());
873
863 // Add the GetHash results to the cache. 874 // Add the GetHash results to the cache.
864 SBFullHashResult full_hash; 875 SBFullHashResult full_hash;
865 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html"); 876 full_hash.hash = SBFullHashForString("www.evil.com/phishing.html");
866 full_hash.list_name = safe_browsing_util::kMalwareList; 877 full_hash.list_name = safe_browsing_util::kMalwareList;
867 full_hash.add_chunk_id = 1;
868 878
869 std::vector<SBFullHashResult> results; 879 std::vector<SBFullHashResult> results;
880 std::vector<SBPrefix> prefixes;
870 results.push_back(full_hash); 881 results.push_back(full_hash);
882 prefixes.push_back(full_hash.hash.prefix);
883 database_->CacheHashResults(
884 prefixes, results, base::TimeDelta::FromMinutes(45));
871 885
872 full_hash.hash = SBFullHashForString("www.evil.com/malware.html"); 886 full_hash.hash = SBFullHashForString("www.evil.com/malware.html");
887 results.clear();
888 prefixes.clear();
873 results.push_back(full_hash); 889 results.push_back(full_hash);
874 890 prefixes.push_back(full_hash.hash.prefix);
875 std::vector<SBPrefix> prefixes; 891 database_->CacheHashResults(
876 database_->CacheHashResults(prefixes, results); 892 prefixes, results, base::TimeDelta::FromMinutes(45));
877 } 893 }
878 894
879 TEST_F(SafeBrowsingDatabaseTest, HashCaching) { 895 TEST_F(SafeBrowsingDatabaseTest, HashCaching) {
880 PopulateDatabaseForCacheTest(); 896 PopulateDatabaseForCacheTest();
881 897
882 // We should have both full hashes in the cache. 898 // We should have both full hashes in the cache.
883 EXPECT_EQ(database_->pending_browse_hashes_.size(), 2U); 899 EXPECT_EQ(database_->browse_gethash_cache_.size(), 2U);
884 900
885 // Test the cache lookup for the first prefix. 901 // Test the cache lookup for the first prefix.
886 std::string listname; 902 std::string listname;
887 std::vector<SBPrefix> prefixes; 903 std::vector<SBPrefix> prefixes;
888 std::vector<SBFullHashResult> full_hashes; 904 std::vector<SBFullHashResult> cache_hits;
889 database_->ContainsBrowseUrl( 905 database_->ContainsBrowseUrl(
890 GURL("http://www.evil.com/phishing.html"), 906 GURL("http://www.evil.com/phishing.html"), &prefixes, &cache_hits);
891 &listname, &prefixes, &full_hashes, Time::Now()); 907 EXPECT_TRUE(prefixes.empty());
892 EXPECT_EQ(full_hashes.size(), 1U); 908 ASSERT_EQ(cache_hits.size(), 1U);
893 EXPECT_TRUE( 909 EXPECT_TRUE(SBFullHashEqual(
894 SBFullHashEqual(full_hashes[0].hash, 910 cache_hits[0].hash, SBFullHashForString("www.evil.com/phishing.html")));
895 SBFullHashForString("www.evil.com/phishing.html")));
896 911
897 prefixes.clear(); 912 prefixes.clear();
898 full_hashes.clear(); 913 cache_hits.clear();
899 914
900 // Test the cache lookup for the second prefix. 915 // Test the cache lookup for the second prefix.
901 database_->ContainsBrowseUrl( 916 database_->ContainsBrowseUrl(
902 GURL("http://www.evil.com/malware.html"), 917 GURL("http://www.evil.com/malware.html"), &prefixes, &cache_hits);
903 &listname, &prefixes, &full_hashes, Time::Now()); 918 EXPECT_TRUE(prefixes.empty());
904 EXPECT_EQ(full_hashes.size(), 1U); 919 ASSERT_EQ(cache_hits.size(), 1U);
905 EXPECT_TRUE( 920 EXPECT_TRUE(SBFullHashEqual(
906 SBFullHashEqual(full_hashes[0].hash, 921 cache_hits[0].hash, SBFullHashForString("www.evil.com/malware.html")));
907 SBFullHashForString("www.evil.com/malware.html")));
908 922
909 prefixes.clear(); 923 prefixes.clear();
910 full_hashes.clear(); 924 cache_hits.clear();
911 925
912 // Test removing a prefix via a sub chunk. 926 // Test removing a prefix via a sub chunk.
913 SBChunk chunk; 927 SBChunk chunk;
914 SBChunkList chunks; 928 SBChunkList chunks;
915 InsertSubChunkHostPrefixUrl(&chunk, 2, 1, "www.evil.com/", 929 InsertSubChunkHostPrefixUrl(&chunk, 2, 1, "www.evil.com/",
916 "www.evil.com/phishing.html"); 930 "www.evil.com/phishing.html");
917 chunks.push_back(chunk); 931 chunks.push_back(chunk);
918 932
919 std::vector<SBListChunkRanges> lists; 933 std::vector<SBListChunkRanges> lists;
920 EXPECT_TRUE(database_->UpdateStarted(&lists)); 934 ASSERT_TRUE(database_->UpdateStarted(&lists));
921 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 935 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
922 database_->UpdateFinished(true); 936 database_->UpdateFinished(true);
923 937
924 // This prefix should still be there. 938 // This prefix should still be there, but cached fullhash should be gone.
925 database_->ContainsBrowseUrl( 939 database_->ContainsBrowseUrl(
926 GURL("http://www.evil.com/malware.html"), 940 GURL("http://www.evil.com/malware.html"), &prefixes, &cache_hits);
927 &listname, &prefixes, &full_hashes, Time::Now()); 941 EXPECT_TRUE(cache_hits.empty());
928 EXPECT_EQ(full_hashes.size(), 1U); 942 ASSERT_EQ(prefixes.size(), 1U);
929 EXPECT_TRUE( 943 EXPECT_EQ(prefixes[0], SBPrefixForString("www.evil.com/malware.html"));
930 SBFullHashEqual(full_hashes[0].hash,
931 SBFullHashForString("www.evil.com/malware.html")));
932 prefixes.clear(); 944 prefixes.clear();
933 full_hashes.clear(); 945 cache_hits.clear();
934 946
935 // This prefix should be gone. 947 // This prefix should be gone.
936 database_->ContainsBrowseUrl( 948 database_->ContainsBrowseUrl(
937 GURL("http://www.evil.com/phishing.html"), 949 GURL("http://www.evil.com/phishing.html"), &prefixes, &cache_hits);
938 &listname, &prefixes, &full_hashes, Time::Now()); 950 EXPECT_TRUE(prefixes.empty());
939 EXPECT_TRUE(full_hashes.empty()); 951 EXPECT_TRUE(cache_hits.empty());
940 952
941 prefixes.clear(); 953 prefixes.clear();
942 full_hashes.clear(); 954 cache_hits.clear();
943 955
944 // Test that an AddDel for the original chunk removes the last cached entry. 956 // Test that an AddDel for the original chunk removes the last entry.
945 EXPECT_TRUE(database_->UpdateStarted(&lists)); 957 ASSERT_TRUE(database_->UpdateStarted(&lists));
946 AddDelChunk(safe_browsing_util::kMalwareList, 1); 958 AddDelChunk(safe_browsing_util::kMalwareList, 1);
947 database_->UpdateFinished(true); 959 database_->UpdateFinished(true);
948 database_->ContainsBrowseUrl( 960 database_->ContainsBrowseUrl(
949 GURL("http://www.evil.com/malware.html"), 961 GURL("http://www.evil.com/malware.html"), &prefixes, &cache_hits);
950 &listname, &prefixes, &full_hashes, Time::Now()); 962 EXPECT_TRUE(cache_hits.empty());
951 EXPECT_TRUE(full_hashes.empty());
952 EXPECT_TRUE(database_->full_browse_hashes_.empty()); 963 EXPECT_TRUE(database_->full_browse_hashes_.empty());
953 EXPECT_TRUE(database_->pending_browse_hashes_.empty()); 964 EXPECT_TRUE(database_->browse_gethash_cache_.empty());
954 965
955 prefixes.clear(); 966 prefixes.clear();
956 full_hashes.clear(); 967 cache_hits.clear();
957 968
958 // Test that the cache won't return expired values. First we have to adjust 969 // Test that the cache won't return expired values. First we have to adjust
959 // the cached entries' received time to make them older, since the database 970 // the cached entries' received time to make them older, since the database
960 // cache insert uses Time::Now(). First, store some entries. 971 // cache insert uses Time::Now(). First, store some entries.
961 PopulateDatabaseForCacheTest(); 972 PopulateDatabaseForCacheTest();
962 973
963 std::vector<SBAddFullHash>* hash_cache = &database_->pending_browse_hashes_; 974 std::map<SBPrefix, SBCachedFullHashResult>* hash_cache =
975 &database_->browse_gethash_cache_;
964 EXPECT_EQ(hash_cache->size(), 2U); 976 EXPECT_EQ(hash_cache->size(), 2U);
965 977
966 // Now adjust one of the entries times to be in the past. 978 // Now adjust one of the entries times to be in the past.
967 base::Time expired = base::Time::Now() - base::TimeDelta::FromMinutes(60);
968 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html"); 979 const SBPrefix key = SBPrefixForString("www.evil.com/malware.html");
969 std::vector<SBAddFullHash>::iterator iter; 980 std::map<SBPrefix, SBCachedFullHashResult>::iterator iter =
970 for (iter = hash_cache->begin(); iter != hash_cache->end(); ++iter) { 981 hash_cache->find(key);
971 if (iter->full_hash.prefix == key) { 982 ASSERT_TRUE(iter != hash_cache->end());
972 iter->received = static_cast<int32>(expired.ToTimeT()); 983 iter->second.expire_after =
973 break; 984 base::Time::Now() - base::TimeDelta::FromMinutes(1);
974 }
975 }
976 EXPECT_TRUE(iter != hash_cache->end());
977 985
978 database_->ContainsBrowseUrl( 986 database_->ContainsBrowseUrl(
979 GURL("http://www.evil.com/malware.html"), 987 GURL("http://www.evil.com/malware.html"), &prefixes, &cache_hits);
980 &listname, &prefixes, &full_hashes, expired); 988 EXPECT_EQ(prefixes.size(), 1U);
981 EXPECT_TRUE(full_hashes.empty()); 989 EXPECT_TRUE(cache_hits.empty());
990 // Expired entry should have been removed from cache.
991 EXPECT_EQ(hash_cache->size(), 1U);
982 992
983 // This entry should still exist. 993 // This entry should still exist.
984 database_->ContainsBrowseUrl( 994 database_->ContainsBrowseUrl(
985 GURL("http://www.evil.com/phishing.html"), 995 GURL("http://www.evil.com/phishing.html"), &prefixes, &cache_hits);
986 &listname, &prefixes, &full_hashes, expired); 996 EXPECT_TRUE(prefixes.empty());
987 EXPECT_EQ(full_hashes.size(), 1U); 997 EXPECT_EQ(cache_hits.size(), 1U);
988
989 998
990 // Testing prefix miss caching. First, we clear out the existing database, 999 // Testing prefix miss caching. First, we clear out the existing database,
991 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate 1000 // Since PopulateDatabaseForCacheTest() doesn't handle adding duplicate
992 // chunks. 1001 // chunks.
993 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1002 ASSERT_TRUE(database_->UpdateStarted(&lists));
994 AddDelChunk(safe_browsing_util::kMalwareList, 1); 1003 AddDelChunk(safe_browsing_util::kMalwareList, 1);
995 database_->UpdateFinished(true); 1004 database_->UpdateFinished(true);
996 1005
1006 // Cache should be cleared after updating.
1007 EXPECT_TRUE(hash_cache->empty());
1008
997 std::vector<SBPrefix> prefix_misses; 1009 std::vector<SBPrefix> prefix_misses;
998 std::vector<SBFullHashResult> empty_full_hash; 1010 std::vector<SBFullHashResult> empty_full_hash;
999 prefix_misses.push_back(SBPrefixForString("http://www.bad.com/malware.html")); 1011 prefix_misses.push_back(SBPrefixForString("http://www.bad.com/malware.html"));
1000 prefix_misses.push_back( 1012 prefix_misses.push_back(
1001 SBPrefixForString("http://www.bad.com/phishing.html")); 1013 SBPrefixForString("http://www.bad.com/phishing.html"));
1002 database_->CacheHashResults(prefix_misses, empty_full_hash); 1014 database_->CacheHashResults(
1015 prefix_misses, empty_full_hash, base::TimeDelta::FromMinutes(45));
1003 1016
1004 // Prefixes with no full results are misses. 1017 // Prefixes with no full results are misses.
1005 EXPECT_EQ(database_->prefix_miss_cache_.size(), 2U); 1018 EXPECT_EQ(hash_cache->size(), prefix_misses.size());
1019 ASSERT_TRUE(
1020 hash_cache->count(SBPrefixForString("http://www.bad.com/malware.html")));
1021 EXPECT_TRUE(
1022 hash_cache->find(SBPrefixForString("http://www.bad.com/malware.html"))
1023 ->second.full_hashes.empty());
1024 ASSERT_TRUE(
1025 hash_cache->count(SBPrefixForString("http://www.bad.com/phishing.html")));
1026 EXPECT_TRUE(
1027 hash_cache->find(SBPrefixForString("http://www.bad.com/phishing.html"))
1028 ->second.full_hashes.empty());
1006 1029
1007 // Update the database. 1030 // Update the database.
1008 PopulateDatabaseForCacheTest(); 1031 PopulateDatabaseForCacheTest();
1009 1032
1010 // Prefix miss cache should be cleared.
1011 EXPECT_TRUE(database_->prefix_miss_cache_.empty());
1012
1013 // Cache a GetHash miss for a particular prefix, and even though the prefix is 1033 // Cache a GetHash miss for a particular prefix, and even though the prefix is
1014 // in the database, it is flagged as a miss so looking up the associated URL 1034 // in the database, it is flagged as a miss so looking up the associated URL
1015 // will not succeed. 1035 // will not succeed.
1016 prefixes.clear(); 1036 prefixes.clear();
1017 full_hashes.clear(); 1037 cache_hits.clear();
1018 prefix_misses.clear(); 1038 prefix_misses.clear();
1019 empty_full_hash.clear(); 1039 empty_full_hash.clear();
1020 prefix_misses.push_back(SBPrefixForString("www.evil.com/phishing.html")); 1040 prefix_misses.push_back(SBPrefixForString("www.evil.com/phishing.html"));
1021 database_->CacheHashResults(prefix_misses, empty_full_hash); 1041 database_->CacheHashResults(
1042 prefix_misses, empty_full_hash, base::TimeDelta::FromMinutes(45));
1022 EXPECT_FALSE(database_->ContainsBrowseUrl( 1043 EXPECT_FALSE(database_->ContainsBrowseUrl(
1023 GURL("http://www.evil.com/phishing.html"), 1044 GURL("http://www.evil.com/phishing.html"), &prefixes, &cache_hits));
1024 &listname, &prefixes, 1045 EXPECT_TRUE(prefixes.empty());
1025 &full_hashes, Time::Now())); 1046 EXPECT_TRUE(cache_hits.empty());
1026 1047
1027 prefixes.clear(); 1048 prefixes.clear();
1028 full_hashes.clear(); 1049 cache_hits.clear();
1029 1050
1030 // Test receiving a full add chunk. 1051 // Test receiving a full add chunk.
1031 chunk.hosts.clear(); 1052 chunk.hosts.clear();
1032 InsertAddChunkHost2FullHashes(&chunk, 20, "www.fullevil.com/", 1053 InsertAddChunkHost2FullHashes(&chunk, 20, "www.fullevil.com/",
1033 "www.fullevil.com/bad1.html", 1054 "www.fullevil.com/bad1.html",
1034 "www.fullevil.com/bad2.html"); 1055 "www.fullevil.com/bad2.html");
1035 chunks.clear(); 1056 chunks.clear();
1036 chunks.push_back(chunk); 1057 chunks.push_back(chunk);
1037 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1058 ASSERT_TRUE(database_->UpdateStarted(&lists));
1038 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1059 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1039 database_->UpdateFinished(true); 1060 database_->UpdateFinished(true);
1040 1061
1041 EXPECT_TRUE(database_->ContainsBrowseUrl( 1062 EXPECT_TRUE(database_->ContainsBrowseUrl(
1042 GURL("http://www.fullevil.com/bad1.html"), 1063 GURL("http://www.fullevil.com/bad1.html"), &prefixes, &cache_hits));
1043 &listname, &prefixes, &full_hashes, 1064 ASSERT_EQ(prefixes.size(), 1U);
1044 Time::Now())); 1065 EXPECT_EQ(prefixes[0], SBPrefixForString("www.fullevil.com/bad1.html"));
1045 EXPECT_EQ(full_hashes.size(), 1U); 1066 EXPECT_TRUE(cache_hits.empty());
1046 EXPECT_TRUE(
1047 SBFullHashEqual(full_hashes[0].hash,
1048 SBFullHashForString("www.fullevil.com/bad1.html")));
1049 prefixes.clear(); 1067 prefixes.clear();
1050 full_hashes.clear(); 1068 cache_hits.clear();
1051 1069
1052 EXPECT_TRUE(database_->ContainsBrowseUrl( 1070 EXPECT_TRUE(database_->ContainsBrowseUrl(
1053 GURL("http://www.fullevil.com/bad2.html"), 1071 GURL("http://www.fullevil.com/bad2.html"), &prefixes, &cache_hits));
1054 &listname, &prefixes, &full_hashes, 1072 ASSERT_EQ(prefixes.size(), 1U);
1055 Time::Now())); 1073 EXPECT_EQ(prefixes[0], SBPrefixForString("www.fullevil.com/bad2.html"));
1056 EXPECT_EQ(full_hashes.size(), 1U); 1074 EXPECT_TRUE(cache_hits.empty());
1057 EXPECT_TRUE(
1058 SBFullHashEqual(full_hashes[0].hash,
1059 SBFullHashForString("www.fullevil.com/bad2.html")));
1060 prefixes.clear(); 1075 prefixes.clear();
1061 full_hashes.clear(); 1076 cache_hits.clear();
1062 1077
1063 // Test receiving a full sub chunk, which will remove one of the full adds. 1078 // Test receiving a full sub chunk, which will remove one of the full adds.
1064 chunk.hosts.clear(); 1079 chunk.hosts.clear();
1065 InsertSubChunkHostFullHash(&chunk, 200, 20, 1080 InsertSubChunkHostFullHash(&chunk, 200, 20,
1066 "www.fullevil.com/", 1081 "www.fullevil.com/",
1067 "www.fullevil.com/bad1.html"); 1082 "www.fullevil.com/bad1.html");
1068 chunks.clear(); 1083 chunks.clear();
1069 chunks.push_back(chunk); 1084 chunks.push_back(chunk);
1070 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1085 ASSERT_TRUE(database_->UpdateStarted(&lists));
1071 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1086 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1072 database_->UpdateFinished(true); 1087 database_->UpdateFinished(true);
1073 1088
1074 EXPECT_FALSE(database_->ContainsBrowseUrl( 1089 EXPECT_FALSE(database_->ContainsBrowseUrl(
1075 GURL("http://www.fullevil.com/bad1.html"), 1090 GURL("http://www.fullevil.com/bad1.html"), &prefixes, &cache_hits));
1076 &listname, &prefixes, &full_hashes, 1091 EXPECT_TRUE(prefixes.empty());
1077 Time::Now())); 1092 EXPECT_TRUE(cache_hits.empty());
1078 EXPECT_TRUE(full_hashes.empty());
1079 1093
1080 // There should be one remaining full add. 1094 // There should be one remaining full add.
1081 EXPECT_TRUE(database_->ContainsBrowseUrl( 1095 EXPECT_TRUE(database_->ContainsBrowseUrl(
1082 GURL("http://www.fullevil.com/bad2.html"), 1096 GURL("http://www.fullevil.com/bad2.html"), &prefixes, &cache_hits));
1083 &listname, &prefixes, &full_hashes, 1097 ASSERT_EQ(prefixes.size(), 1U);
1084 Time::Now())); 1098 EXPECT_EQ(prefixes[0], SBPrefixForString("www.fullevil.com/bad2.html"));
1085 EXPECT_EQ(full_hashes.size(), 1U); 1099 EXPECT_TRUE(cache_hits.empty());
1086 EXPECT_TRUE(
1087 SBFullHashEqual(full_hashes[0].hash,
1088 SBFullHashForString("www.fullevil.com/bad2.html")));
1089 prefixes.clear(); 1100 prefixes.clear();
1090 full_hashes.clear(); 1101 cache_hits.clear();
1091 1102
1092 // Now test an AddDel for the remaining full add. 1103 // Now test an AddDel for the remaining full add.
1093 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1104 ASSERT_TRUE(database_->UpdateStarted(&lists));
1094 AddDelChunk(safe_browsing_util::kMalwareList, 20); 1105 AddDelChunk(safe_browsing_util::kMalwareList, 20);
1095 database_->UpdateFinished(true); 1106 database_->UpdateFinished(true);
1096 1107
1097 EXPECT_FALSE(database_->ContainsBrowseUrl( 1108 EXPECT_FALSE(database_->ContainsBrowseUrl(
1098 GURL("http://www.fullevil.com/bad1.html"), 1109 GURL("http://www.fullevil.com/bad1.html"), &prefixes, &cache_hits));
1099 &listname, &prefixes, &full_hashes,
1100 Time::Now()));
1101 EXPECT_FALSE(database_->ContainsBrowseUrl( 1110 EXPECT_FALSE(database_->ContainsBrowseUrl(
1102 GURL("http://www.fullevil.com/bad2.html"), 1111 GURL("http://www.fullevil.com/bad2.html"), &prefixes, &cache_hits));
1103 &listname, &prefixes, &full_hashes,
1104 Time::Now()));
1105 } 1112 }
1106 1113
1107 // Test that corrupt databases are appropriately handled, even if the 1114 // Test that corrupt databases are appropriately handled, even if the
1108 // corruption is detected in the midst of the update. 1115 // corruption is detected in the midst of the update.
1109 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved. 1116 // TODO(shess): Disabled until ScopedLogMessageIgnorer resolved.
1110 // http://crbug.com/56448 1117 // http://crbug.com/56448
1111 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) { 1118 TEST_F(SafeBrowsingDatabaseTest, DISABLED_FileCorruptionHandling) {
1112 // Re-create the database in a captive message loop so that we can 1119 // Re-create the database in a captive message loop so that we can
1113 // influence task-posting. Database specifically needs to the 1120 // influence task-posting. Database specifically needs to the
1114 // file-backed. 1121 // file-backed.
1115 database_.reset(); 1122 database_.reset();
1116 base::MessageLoop loop; 1123 base::MessageLoop loop;
1117 SafeBrowsingStoreFile* store = new SafeBrowsingStoreFile(); 1124 SafeBrowsingStoreFile* store = new SafeBrowsingStoreFile();
1118 database_.reset(new SafeBrowsingDatabaseNew(store, NULL, NULL, NULL, NULL, 1125 database_.reset(new SafeBrowsingDatabaseNew(store, NULL, NULL, NULL, NULL,
1119 NULL, NULL)); 1126 NULL, NULL));
1120 database_->Init(database_filename_); 1127 database_->Init(database_filename_);
1121 1128
1122 // This will cause an empty database to be created. 1129 // This will cause an empty database to be created.
1123 std::vector<SBListChunkRanges> lists; 1130 std::vector<SBListChunkRanges> lists;
1124 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1131 ASSERT_TRUE(database_->UpdateStarted(&lists));
1125 database_->UpdateFinished(true); 1132 database_->UpdateFinished(true);
1126 1133
1127 // Create a sub chunk to insert. 1134 // Create a sub chunk to insert.
1128 SBChunkList chunks; 1135 SBChunkList chunks;
1129 SBChunk chunk; 1136 SBChunk chunk;
1130 SBChunkHost host; 1137 SBChunkHost host;
1131 host.host = SBPrefixForString("www.subbed.com/"); 1138 host.host = SBPrefixForString("www.subbed.com/");
1132 host.entry = SBEntry::Create(SBEntry::SUB_PREFIX, 1); 1139 host.entry = SBEntry::Create(SBEntry::SUB_PREFIX, 1);
1133 host.entry->set_chunk_id(7); 1140 host.entry->set_chunk_id(7);
1134 host.entry->SetChunkIdAtPrefix(0, 19); 1141 host.entry->SetChunkIdAtPrefix(0, 19);
(...skipping 13 matching lines...) Expand all
1148 for (size_t i = 0; i < 8; ++i) { 1155 for (size_t i = 0; i < 8; ++i) {
1149 fputc('!', fp); 1156 fputc('!', fp);
1150 } 1157 }
1151 fclose(fp); 1158 fclose(fp);
1152 1159
1153 { 1160 {
1154 // The following code will cause DCHECKs, so suppress the crashes. 1161 // The following code will cause DCHECKs, so suppress the crashes.
1155 ScopedLogMessageIgnorer ignorer; 1162 ScopedLogMessageIgnorer ignorer;
1156 1163
1157 // Start an update. The insert will fail due to corruption. 1164 // Start an update. The insert will fail due to corruption.
1158 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1165 ASSERT_TRUE(database_->UpdateStarted(&lists));
1159 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1166 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1160 database_->UpdateFinished(true); 1167 database_->UpdateFinished(true);
1161 1168
1162 // Database file still exists until the corruption handler has run. 1169 // Database file still exists until the corruption handler has run.
1163 EXPECT_TRUE(base::PathExists(database_filename_)); 1170 EXPECT_TRUE(base::PathExists(database_filename_));
1164 1171
1165 // Flush through the corruption-handler task. 1172 // Flush through the corruption-handler task.
1166 VLOG(1) << "Expect failed check on: SafeBrowsing database reset"; 1173 VLOG(1) << "Expect failed check on: SafeBrowsing database reset";
1167 base::MessageLoop::current()->RunUntilIdle(); 1174 base::MessageLoop::current()->RunUntilIdle();
1168 } 1175 }
1169 1176
1170 // Database file should not exist. 1177 // Database file should not exist.
1171 EXPECT_FALSE(base::PathExists(database_filename_)); 1178 EXPECT_FALSE(base::PathExists(database_filename_));
1172 1179
1173 // Run the update again successfully. 1180 // Run the update again successfully.
1174 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1181 ASSERT_TRUE(database_->UpdateStarted(&lists));
1175 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1182 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1176 database_->UpdateFinished(true); 1183 database_->UpdateFinished(true);
1177 EXPECT_TRUE(base::PathExists(database_filename_)); 1184 EXPECT_TRUE(base::PathExists(database_filename_));
1178 1185
1179 database_.reset(); 1186 database_.reset();
1180 } 1187 }
1181 1188
1182 // Checks database reading and writing. 1189 // Checks database reading and writing.
1183 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrl) { 1190 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrl) {
1184 database_.reset(); 1191 database_.reset();
(...skipping 14 matching lines...) Expand all
1199 const char kEvil1Url1[] = "www.evil1.com/download1/"; 1206 const char kEvil1Url1[] = "www.evil1.com/download1/";
1200 const char kEvil1Url2[] = "www.evil1.com/download2.html"; 1207 const char kEvil1Url2[] = "www.evil1.com/download2.html";
1201 1208
1202 SBChunkList chunks; 1209 SBChunkList chunks;
1203 SBChunk chunk; 1210 SBChunk chunk;
1204 // Add a simple chunk with one hostkey for download url list. 1211 // Add a simple chunk with one hostkey for download url list.
1205 InsertAddChunkHost2PrefixUrls(&chunk, 1, kEvil1Host, 1212 InsertAddChunkHost2PrefixUrls(&chunk, 1, kEvil1Host,
1206 kEvil1Url1, kEvil1Url2); 1213 kEvil1Url1, kEvil1Url2);
1207 chunks.push_back(chunk); 1214 chunks.push_back(chunk);
1208 std::vector<SBListChunkRanges> lists; 1215 std::vector<SBListChunkRanges> lists;
1209 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1216 ASSERT_TRUE(database_->UpdateStarted(&lists));
1210 database_->InsertChunks(safe_browsing_util::kBinUrlList, chunks); 1217 database_->InsertChunks(safe_browsing_util::kBinUrlList, chunks);
1211 database_->UpdateFinished(true); 1218 database_->UpdateFinished(true);
1212 1219
1213 std::vector<SBPrefix> prefix_hits; 1220 std::vector<SBPrefix> prefix_hits;
1214 std::vector<GURL> urls(1); 1221 std::vector<GURL> urls(1);
1215 1222
1216 urls[0] = GURL(std::string("http://") + kEvil1Url1); 1223 urls[0] = GURL(std::string("http://") + kEvil1Url1);
1217 EXPECT_TRUE(database_->ContainsDownloadUrl(urls, &prefix_hits)); 1224 EXPECT_TRUE(database_->ContainsDownloadUrl(urls, &prefix_hits));
1218 ASSERT_EQ(prefix_hits.size(), 1U); 1225 ASSERT_EQ(prefix_hits.size(), 1U);
1219 EXPECT_EQ(prefix_hits[0], SBPrefixForString(kEvil1Url1)); 1226 EXPECT_EQ(prefix_hits[0], SBPrefixForString(kEvil1Url1));
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
1344 1351
1345 chunk.hosts.clear(); 1352 chunk.hosts.clear();
1346 InsertAddChunkHostFullHashes(&chunk, 3, kGoodString, kGoodString); 1353 InsertAddChunkHostFullHashes(&chunk, 3, kGoodString, kGoodString);
1347 download_chunks.push_back(chunk); 1354 download_chunks.push_back(chunk);
1348 1355
1349 chunk.hosts.clear(); 1356 chunk.hosts.clear();
1350 InsertAddChunkHostFullHashes(&chunk, 4, kGood3Host, kGood3Url1); 1357 InsertAddChunkHostFullHashes(&chunk, 4, kGood3Host, kGood3Url1);
1351 download_chunks.push_back(chunk); 1358 download_chunks.push_back(chunk);
1352 1359
1353 std::vector<SBListChunkRanges> lists; 1360 std::vector<SBListChunkRanges> lists;
1354 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1361 ASSERT_TRUE(database_->UpdateStarted(&lists));
1355 database_->InsertChunks(safe_browsing_util::kCsdWhiteList, 1362 database_->InsertChunks(safe_browsing_util::kCsdWhiteList,
1356 csd_chunks); 1363 csd_chunks);
1357 database_->InsertChunks(safe_browsing_util::kDownloadWhiteList, 1364 database_->InsertChunks(safe_browsing_util::kDownloadWhiteList,
1358 download_chunks); 1365 download_chunks);
1359 database_->UpdateFinished(true); 1366 database_->UpdateFinished(true);
1360 1367
1361 EXPECT_FALSE(database_->ContainsCsdWhitelistedUrl( 1368 EXPECT_FALSE(database_->ContainsCsdWhitelistedUrl(
1362 GURL(std::string("http://") + kGood1Host))); 1369 GURL(std::string("http://") + kGood1Host)));
1363 1370
1364 EXPECT_TRUE(database_->ContainsCsdWhitelistedUrl( 1371 EXPECT_TRUE(database_->ContainsCsdWhitelistedUrl(
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1402 EXPECT_FALSE(database_->ContainsDownloadWhitelistedUrl( 1409 EXPECT_FALSE(database_->ContainsDownloadWhitelistedUrl(
1403 GURL(std::string("http://www.google.com/")))); 1410 GURL(std::string("http://www.google.com/"))));
1404 1411
1405 // Test only add the malware IP killswitch 1412 // Test only add the malware IP killswitch
1406 csd_chunks.clear(); 1413 csd_chunks.clear();
1407 chunk.hosts.clear(); 1414 chunk.hosts.clear();
1408 InsertAddChunkHostFullHashes( 1415 InsertAddChunkHostFullHashes(
1409 &chunk, 15, "sb-ssl.google.com/", 1416 &chunk, 15, "sb-ssl.google.com/",
1410 "sb-ssl.google.com/safebrowsing/csd/killswitch_malware"); 1417 "sb-ssl.google.com/safebrowsing/csd/killswitch_malware");
1411 csd_chunks.push_back(chunk); 1418 csd_chunks.push_back(chunk);
1412 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1419 ASSERT_TRUE(database_->UpdateStarted(&lists));
1413 database_->InsertChunks(safe_browsing_util::kCsdWhiteList, csd_chunks); 1420 database_->InsertChunks(safe_browsing_util::kCsdWhiteList, csd_chunks);
1414 database_->UpdateFinished(true); 1421 database_->UpdateFinished(true);
1415 1422
1416 EXPECT_TRUE(database_->IsMalwareIPMatchKillSwitchOn()); 1423 EXPECT_TRUE(database_->IsMalwareIPMatchKillSwitchOn());
1417 1424
1418 // Test that the kill-switch works as intended. 1425 // Test that the kill-switch works as intended.
1419 csd_chunks.clear(); 1426 csd_chunks.clear();
1420 download_chunks.clear(); 1427 download_chunks.clear();
1421 lists.clear(); 1428 lists.clear();
1422 chunk.hosts.clear(); 1429 chunk.hosts.clear();
1423 InsertAddChunkHostFullHashes(&chunk, 5, "sb-ssl.google.com/", 1430 InsertAddChunkHostFullHashes(&chunk, 5, "sb-ssl.google.com/",
1424 "sb-ssl.google.com/safebrowsing/csd/killswitch"); 1431 "sb-ssl.google.com/safebrowsing/csd/killswitch");
1425 csd_chunks.push_back(chunk); 1432 csd_chunks.push_back(chunk);
1426 chunk.hosts.clear(); 1433 chunk.hosts.clear();
1427 InsertAddChunkHostFullHashes(&chunk, 5, "sb-ssl.google.com/", 1434 InsertAddChunkHostFullHashes(&chunk, 5, "sb-ssl.google.com/",
1428 "sb-ssl.google.com/safebrowsing/csd/killswitch"); 1435 "sb-ssl.google.com/safebrowsing/csd/killswitch");
1429 download_chunks.push_back(chunk); 1436 download_chunks.push_back(chunk);
1430 1437
1431 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1438 ASSERT_TRUE(database_->UpdateStarted(&lists));
1432 database_->InsertChunks(safe_browsing_util::kCsdWhiteList, csd_chunks); 1439 database_->InsertChunks(safe_browsing_util::kCsdWhiteList, csd_chunks);
1433 database_->InsertChunks(safe_browsing_util::kDownloadWhiteList, 1440 database_->InsertChunks(safe_browsing_util::kDownloadWhiteList,
1434 download_chunks); 1441 download_chunks);
1435 database_->UpdateFinished(true); 1442 database_->UpdateFinished(true);
1436 1443
1437 EXPECT_TRUE(database_->IsMalwareIPMatchKillSwitchOn()); 1444 EXPECT_TRUE(database_->IsMalwareIPMatchKillSwitchOn());
1438 EXPECT_TRUE(database_->ContainsCsdWhitelistedUrl( 1445 EXPECT_TRUE(database_->ContainsCsdWhitelistedUrl(
1439 GURL(std::string("https://") + kGood1Url2 + "/c.html"))); 1446 GURL(std::string("https://") + kGood1Url2 + "/c.html")));
1440 EXPECT_TRUE(database_->ContainsCsdWhitelistedUrl( 1447 EXPECT_TRUE(database_->ContainsCsdWhitelistedUrl(
1441 GURL(std::string("http://www.google.com/")))); 1448 GURL(std::string("http://www.google.com/"))));
(...skipping 25 matching lines...) Expand all
1467 &sub_chunk, 10, 15, "sb-ssl.google.com/", 1474 &sub_chunk, 10, 15, "sb-ssl.google.com/",
1468 "sb-ssl.google.com/safebrowsing/csd/killswitch_malware"); 1475 "sb-ssl.google.com/safebrowsing/csd/killswitch_malware");
1469 csd_chunks.push_back(sub_chunk); 1476 csd_chunks.push_back(sub_chunk);
1470 1477
1471 sub_chunk.hosts.clear(); 1478 sub_chunk.hosts.clear();
1472 InsertSubChunkHostFullHash(&sub_chunk, 1, 5, 1479 InsertSubChunkHostFullHash(&sub_chunk, 1, 5,
1473 "sb-ssl.google.com/", 1480 "sb-ssl.google.com/",
1474 "sb-ssl.google.com/safebrowsing/csd/killswitch"); 1481 "sb-ssl.google.com/safebrowsing/csd/killswitch");
1475 download_chunks.push_back(sub_chunk); 1482 download_chunks.push_back(sub_chunk);
1476 1483
1477 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1484 ASSERT_TRUE(database_->UpdateStarted(&lists));
1478 database_->InsertChunks(safe_browsing_util::kCsdWhiteList, csd_chunks); 1485 database_->InsertChunks(safe_browsing_util::kCsdWhiteList, csd_chunks);
1479 database_->InsertChunks(safe_browsing_util::kDownloadWhiteList, 1486 database_->InsertChunks(safe_browsing_util::kDownloadWhiteList,
1480 download_chunks); 1487 download_chunks);
1481 database_->UpdateFinished(true); 1488 database_->UpdateFinished(true);
1482 1489
1483 EXPECT_FALSE(database_->IsMalwareIPMatchKillSwitchOn()); 1490 EXPECT_FALSE(database_->IsMalwareIPMatchKillSwitchOn());
1484 EXPECT_TRUE(database_->ContainsCsdWhitelistedUrl( 1491 EXPECT_TRUE(database_->ContainsCsdWhitelistedUrl(
1485 GURL(std::string("https://") + kGood1Url2 + "/c.html"))); 1492 GURL(std::string("https://") + kGood1Url2 + "/c.html")));
1486 EXPECT_TRUE(database_->ContainsCsdWhitelistedUrl( 1493 EXPECT_TRUE(database_->ContainsCsdWhitelistedUrl(
1487 GURL(std::string("https://") + kGood2Url1 + "/c/bla"))); 1494 GURL(std::string("https://") + kGood2Url1 + "/c/bla")));
(...skipping 24 matching lines...) Expand all
1512 // Add a malware add chunk with two entries of the same host. 1519 // Add a malware add chunk with two entries of the same host.
1513 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/", 1520 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/",
1514 "www.evil.com/malware1.html"); 1521 "www.evil.com/malware1.html");
1515 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/", 1522 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/",
1516 "www.evil.com/malware2.html"); 1523 "www.evil.com/malware2.html");
1517 SBChunkList chunks; 1524 SBChunkList chunks;
1518 chunks.push_back(chunk); 1525 chunks.push_back(chunk);
1519 1526
1520 // Insert the testing chunks into database. 1527 // Insert the testing chunks into database.
1521 std::vector<SBListChunkRanges> lists; 1528 std::vector<SBListChunkRanges> lists;
1522 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1529 ASSERT_TRUE(database_->UpdateStarted(&lists));
1523 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1530 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1524 database_->UpdateFinished(true); 1531 database_->UpdateFinished(true);
1525 1532
1526 GetListsInfo(&lists); 1533 GetListsInfo(&lists);
1527 EXPECT_EQ(std::string(safe_browsing_util::kMalwareList), lists[0].name); 1534 EXPECT_EQ(std::string(safe_browsing_util::kMalwareList), lists[0].name);
1528 EXPECT_EQ("1", lists[0].adds); 1535 EXPECT_EQ("1", lists[0].adds);
1529 EXPECT_TRUE(lists[0].subs.empty()); 1536 EXPECT_TRUE(lists[0].subs.empty());
1530 1537
1531 // Add a phishing add chunk with two entries of the same host. 1538 // Add a phishing add chunk with two entries of the same host.
1532 chunk.hosts.clear(); 1539 chunk.hosts.clear();
1533 InsertAddChunkHostPrefixUrl(&chunk, 47, "www.evil.com/", 1540 InsertAddChunkHostPrefixUrl(&chunk, 47, "www.evil.com/",
1534 "www.evil.com/phishing1.html"); 1541 "www.evil.com/phishing1.html");
1535 InsertAddChunkHostPrefixUrl(&chunk, 47, "www.evil.com/", 1542 InsertAddChunkHostPrefixUrl(&chunk, 47, "www.evil.com/",
1536 "www.evil.com/phishing2.html"); 1543 "www.evil.com/phishing2.html");
1537 chunks.clear(); 1544 chunks.clear();
1538 chunks.push_back(chunk); 1545 chunks.push_back(chunk);
1539 1546
1540 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1547 ASSERT_TRUE(database_->UpdateStarted(&lists));
1541 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); 1548 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks);
1542 database_->UpdateFinished(true); 1549 database_->UpdateFinished(true);
1543 1550
1544 GetListsInfo(&lists); 1551 GetListsInfo(&lists);
1545 EXPECT_EQ(std::string(safe_browsing_util::kMalwareList), lists[0].name); 1552 EXPECT_EQ(std::string(safe_browsing_util::kMalwareList), lists[0].name);
1546 EXPECT_EQ("1", lists[0].adds); 1553 EXPECT_EQ("1", lists[0].adds);
1547 EXPECT_EQ(std::string(safe_browsing_util::kPhishingList), lists[1].name); 1554 EXPECT_EQ(std::string(safe_browsing_util::kPhishingList), lists[1].name);
1548 EXPECT_EQ("47", lists[1].adds); 1555 EXPECT_EQ("47", lists[1].adds);
1549 1556
1550 const Time now = Time::Now();
1551 std::vector<SBPrefix> prefixes; 1557 std::vector<SBPrefix> prefixes;
1552 std::vector<SBFullHashResult> full_hashes; 1558 std::vector<SBFullHashResult> cache_hits;
1553 std::vector<SBPrefix> prefix_hits;
1554 std::string matching_list;
1555 std::string listname; 1559 std::string listname;
1556 1560
1557 EXPECT_TRUE(database_->ContainsBrowseUrl( 1561 EXPECT_TRUE(database_->ContainsBrowseUrl(
1558 GURL("http://www.evil.com/malware1.html"), 1562 GURL("http://www.evil.com/malware1.html"), &prefixes, &cache_hits));
1559 &listname, &prefixes, &full_hashes, now));
1560 EXPECT_TRUE(database_->ContainsBrowseUrl( 1563 EXPECT_TRUE(database_->ContainsBrowseUrl(
1561 GURL("http://www.evil.com/malware2.html"), 1564 GURL("http://www.evil.com/malware2.html"), &prefixes, &cache_hits));
1562 &listname, &prefixes, &full_hashes, now));
1563 EXPECT_TRUE(database_->ContainsBrowseUrl( 1565 EXPECT_TRUE(database_->ContainsBrowseUrl(
1564 GURL("http://www.evil.com/phishing1.html"), 1566 GURL("http://www.evil.com/phishing1.html"), &prefixes, &cache_hits));
1565 &listname, &prefixes, &full_hashes, now));
1566 EXPECT_TRUE(database_->ContainsBrowseUrl( 1567 EXPECT_TRUE(database_->ContainsBrowseUrl(
1567 GURL("http://www.evil.com/phishing2.html"), 1568 GURL("http://www.evil.com/phishing2.html"), &prefixes, &cache_hits));
1568 &listname, &prefixes, &full_hashes, now));
1569 1569
1570 // Test removing a single prefix from the add chunk. 1570 // Test removing a single prefix from the add chunk.
1571 // Remove the prefix that added first. 1571 // Remove the prefix that added first.
1572 chunk.hosts.clear(); 1572 chunk.hosts.clear();
1573 InsertSubChunkHostPrefixUrl(&chunk, 4, 1, "www.evil.com/", 1573 InsertSubChunkHostPrefixUrl(&chunk, 4, 1, "www.evil.com/",
1574 "www.evil.com/malware1.html"); 1574 "www.evil.com/malware1.html");
1575 chunks.clear(); 1575 chunks.clear();
1576 chunks.push_back(chunk); 1576 chunks.push_back(chunk);
1577 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1577 ASSERT_TRUE(database_->UpdateStarted(&lists));
1578 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1578 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1579 database_->UpdateFinished(true); 1579 database_->UpdateFinished(true);
1580 1580
1581 // Remove the prefix that added last. 1581 // Remove the prefix that added last.
1582 chunk.hosts.clear(); 1582 chunk.hosts.clear();
1583 InsertSubChunkHostPrefixUrl(&chunk, 5, 47, "www.evil.com/", 1583 InsertSubChunkHostPrefixUrl(&chunk, 5, 47, "www.evil.com/",
1584 "www.evil.com/phishing2.html"); 1584 "www.evil.com/phishing2.html");
1585 chunks.clear(); 1585 chunks.clear();
1586 chunks.push_back(chunk); 1586 chunks.push_back(chunk);
1587 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1587 ASSERT_TRUE(database_->UpdateStarted(&lists));
1588 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks); 1588 database_->InsertChunks(safe_browsing_util::kPhishingList, chunks);
1589 database_->UpdateFinished(true); 1589 database_->UpdateFinished(true);
1590 1590
1591 // Verify that the database contains urls expected. 1591 // Verify that the database contains urls expected.
1592 EXPECT_FALSE(database_->ContainsBrowseUrl( 1592 EXPECT_FALSE(database_->ContainsBrowseUrl(
1593 GURL("http://www.evil.com/malware1.html"), 1593 GURL("http://www.evil.com/malware1.html"), &prefixes, &cache_hits));
1594 &listname, &prefixes, &full_hashes, now));
1595 EXPECT_TRUE(database_->ContainsBrowseUrl( 1594 EXPECT_TRUE(database_->ContainsBrowseUrl(
1596 GURL("http://www.evil.com/malware2.html"), 1595 GURL("http://www.evil.com/malware2.html"), &prefixes, &cache_hits));
1597 &listname, &prefixes, &full_hashes, now));
1598 EXPECT_TRUE(database_->ContainsBrowseUrl( 1596 EXPECT_TRUE(database_->ContainsBrowseUrl(
1599 GURL("http://www.evil.com/phishing1.html"), 1597 GURL("http://www.evil.com/phishing1.html"), &prefixes, &cache_hits));
1600 &listname, &prefixes, &full_hashes, now));
1601 EXPECT_FALSE(database_->ContainsBrowseUrl( 1598 EXPECT_FALSE(database_->ContainsBrowseUrl(
1602 GURL("http://www.evil.com/phishing2.html"), 1599 GURL("http://www.evil.com/phishing2.html"), &prefixes, &cache_hits));
1603 &listname, &prefixes, &full_hashes, now));
1604 } 1600 }
1605 1601
1606 // Test that an empty update doesn't actually update the database. 1602 // Test that an empty update doesn't actually update the database.
1607 // This isn't a functionality requirement, but it is a useful 1603 // This isn't a functionality requirement, but it is a useful
1608 // optimization. 1604 // optimization.
1609 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { 1605 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) {
1610 SBChunkList chunks; 1606 SBChunkList chunks;
1611 SBChunk chunk; 1607 SBChunk chunk;
1612 1608
1613 base::FilePath filename = database_->BrowseDBFilename(database_filename_); 1609 base::FilePath filename = database_->BrowseDBFilename(database_filename_);
1614 1610
1615 // Prime the database. 1611 // Prime the database.
1616 std::vector<SBListChunkRanges> lists; 1612 std::vector<SBListChunkRanges> lists;
1617 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1613 ASSERT_TRUE(database_->UpdateStarted(&lists));
1618 1614
1619 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/", 1615 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/",
1620 "www.evil.com/malware.html"); 1616 "www.evil.com/malware.html");
1621 chunks.clear(); 1617 chunks.clear();
1622 chunks.push_back(chunk); 1618 chunks.push_back(chunk);
1623 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1619 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1624 database_->UpdateFinished(true); 1620 database_->UpdateFinished(true);
1625 1621
1626 // Get an older time to reset the lastmod time for detecting whether 1622 // Get an older time to reset the lastmod time for detecting whether
1627 // the file has been updated. 1623 // the file has been updated.
1628 base::File::Info before_info, after_info; 1624 base::File::Info before_info, after_info;
1629 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); 1625 ASSERT_TRUE(base::GetFileInfo(filename, &before_info));
1630 const base::Time old_last_modified = 1626 const base::Time old_last_modified =
1631 before_info.last_modified - base::TimeDelta::FromSeconds(10); 1627 before_info.last_modified - base::TimeDelta::FromSeconds(10);
1632 1628
1633 // Inserting another chunk updates the database file. The sleep is 1629 // Inserting another chunk updates the database file. The sleep is
1634 // needed because otherwise the entire test can finish w/in the 1630 // needed because otherwise the entire test can finish w/in the
1635 // resolution of the lastmod time. 1631 // resolution of the lastmod time.
1636 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); 1632 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified));
1637 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); 1633 ASSERT_TRUE(base::GetFileInfo(filename, &before_info));
1638 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1634 ASSERT_TRUE(database_->UpdateStarted(&lists));
1639 chunk.hosts.clear(); 1635 chunk.hosts.clear();
1640 InsertAddChunkHostPrefixUrl(&chunk, 2, "www.foo.com/", 1636 InsertAddChunkHostPrefixUrl(&chunk, 2, "www.foo.com/",
1641 "www.foo.com/malware.html"); 1637 "www.foo.com/malware.html");
1642 chunks.clear(); 1638 chunks.clear();
1643 chunks.push_back(chunk); 1639 chunks.push_back(chunk);
1644 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1640 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1645 database_->UpdateFinished(true); 1641 database_->UpdateFinished(true);
1646 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); 1642 ASSERT_TRUE(base::GetFileInfo(filename, &after_info));
1647 EXPECT_LT(before_info.last_modified, after_info.last_modified); 1643 EXPECT_LT(before_info.last_modified, after_info.last_modified);
1648 1644
1649 // Deleting a chunk updates the database file. 1645 // Deleting a chunk updates the database file.
1650 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); 1646 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified));
1651 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); 1647 ASSERT_TRUE(base::GetFileInfo(filename, &before_info));
1652 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1648 ASSERT_TRUE(database_->UpdateStarted(&lists));
1653 AddDelChunk(safe_browsing_util::kMalwareList, chunk.chunk_number); 1649 AddDelChunk(safe_browsing_util::kMalwareList, chunk.chunk_number);
1654 database_->UpdateFinished(true); 1650 database_->UpdateFinished(true);
1655 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); 1651 ASSERT_TRUE(base::GetFileInfo(filename, &after_info));
1656 EXPECT_LT(before_info.last_modified, after_info.last_modified); 1652 EXPECT_LT(before_info.last_modified, after_info.last_modified);
1657 1653
1658 // Simply calling |UpdateStarted()| then |UpdateFinished()| does not 1654 // Simply calling |UpdateStarted()| then |UpdateFinished()| does not
1659 // update the database file. 1655 // update the database file.
1660 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified)); 1656 ASSERT_TRUE(base::TouchFile(filename, old_last_modified, old_last_modified));
1661 ASSERT_TRUE(base::GetFileInfo(filename, &before_info)); 1657 ASSERT_TRUE(base::GetFileInfo(filename, &before_info));
1662 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1658 ASSERT_TRUE(database_->UpdateStarted(&lists));
1663 database_->UpdateFinished(true); 1659 database_->UpdateFinished(true);
1664 ASSERT_TRUE(base::GetFileInfo(filename, &after_info)); 1660 ASSERT_TRUE(base::GetFileInfo(filename, &after_info));
1665 EXPECT_EQ(before_info.last_modified, after_info.last_modified); 1661 EXPECT_EQ(before_info.last_modified, after_info.last_modified);
1666 } 1662 }
1667 1663
1668 // Test that a filter file is written out during update and read back 1664 // Test that a filter file is written out during update and read back
1669 // in during setup. 1665 // in during setup.
1670 TEST_F(SafeBrowsingDatabaseTest, FilterFile) { 1666 TEST_F(SafeBrowsingDatabaseTest, FilterFile) {
1671 // Create a database with trivial example data and write it out. 1667 // Create a database with trivial example data and write it out.
1672 { 1668 {
1673 SBChunkList chunks; 1669 SBChunkList chunks;
1674 SBChunk chunk; 1670 SBChunk chunk;
1675 1671
1676 // Prime the database. 1672 // Prime the database.
1677 std::vector<SBListChunkRanges> lists; 1673 std::vector<SBListChunkRanges> lists;
1678 EXPECT_TRUE(database_->UpdateStarted(&lists)); 1674 ASSERT_TRUE(database_->UpdateStarted(&lists));
1679 1675
1680 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/", 1676 InsertAddChunkHostPrefixUrl(&chunk, 1, "www.evil.com/",
1681 "www.evil.com/malware.html"); 1677 "www.evil.com/malware.html");
1682 chunks.clear(); 1678 chunks.clear();
1683 chunks.push_back(chunk); 1679 chunks.push_back(chunk);
1684 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks); 1680 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1685 database_->UpdateFinished(true); 1681 database_->UpdateFinished(true);
1686 } 1682 }
1687 1683
1688 // Find the malware url in the database, don't find a good url. 1684 // Find the malware url in the database, don't find a good url.
1689 const Time now = Time::Now(); 1685 std::vector<SBFullHashResult> cache_hits;
1690 std::vector<SBFullHashResult> full_hashes;
1691 std::vector<SBPrefix> prefix_hits; 1686 std::vector<SBPrefix> prefix_hits;
1692 std::string matching_list;
1693 EXPECT_TRUE(database_->ContainsBrowseUrl( 1687 EXPECT_TRUE(database_->ContainsBrowseUrl(
1694 GURL("http://www.evil.com/malware.html"), 1688 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits));
1695 &matching_list, &prefix_hits, &full_hashes, now));
1696 EXPECT_FALSE(database_->ContainsBrowseUrl( 1689 EXPECT_FALSE(database_->ContainsBrowseUrl(
1697 GURL("http://www.good.com/goodware.html"), 1690 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits));
1698 &matching_list, &prefix_hits, &full_hashes, now));
1699 1691
1700 base::FilePath filter_file = database_->PrefixSetForFilename( 1692 base::FilePath filter_file = database_->PrefixSetForFilename(
1701 database_->BrowseDBFilename(database_filename_)); 1693 database_->BrowseDBFilename(database_filename_));
1702 1694
1703 // After re-creating the database, it should have a filter read from 1695 // After re-creating the database, it should have a filter read from
1704 // a file, so it should find the same results. 1696 // a file, so it should find the same results.
1705 ASSERT_TRUE(base::PathExists(filter_file)); 1697 ASSERT_TRUE(base::PathExists(filter_file));
1706 database_.reset(new SafeBrowsingDatabaseNew); 1698 database_.reset(new SafeBrowsingDatabaseNew);
1707 database_->Init(database_filename_); 1699 database_->Init(database_filename_);
1708 EXPECT_TRUE(database_->ContainsBrowseUrl( 1700 EXPECT_TRUE(database_->ContainsBrowseUrl(
1709 GURL("http://www.evil.com/malware.html"), 1701 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits));
1710 &matching_list, &prefix_hits, &full_hashes, now));
1711 EXPECT_FALSE(database_->ContainsBrowseUrl( 1702 EXPECT_FALSE(database_->ContainsBrowseUrl(
1712 GURL("http://www.good.com/goodware.html"), 1703 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits));
1713 &matching_list, &prefix_hits, &full_hashes, now));
1714 1704
1715 // If there is no filter file, the database cannot find malware urls. 1705 // If there is no filter file, the database cannot find malware urls.
1716 base::DeleteFile(filter_file, false); 1706 base::DeleteFile(filter_file, false);
1717 ASSERT_FALSE(base::PathExists(filter_file)); 1707 ASSERT_FALSE(base::PathExists(filter_file));
1718 database_.reset(new SafeBrowsingDatabaseNew); 1708 database_.reset(new SafeBrowsingDatabaseNew);
1719 database_->Init(database_filename_); 1709 database_->Init(database_filename_);
1720 EXPECT_FALSE(database_->ContainsBrowseUrl( 1710 EXPECT_FALSE(database_->ContainsBrowseUrl(
1721 GURL("http://www.evil.com/malware.html"), 1711 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits));
1722 &matching_list, &prefix_hits, &full_hashes, now));
1723 EXPECT_FALSE(database_->ContainsBrowseUrl( 1712 EXPECT_FALSE(database_->ContainsBrowseUrl(
1724 GURL("http://www.good.com/goodware.html"), 1713 GURL("http://www.good.com/goodware.html"), &prefix_hits, &cache_hits));
1725 &matching_list, &prefix_hits, &full_hashes, now));
1726 } 1714 }
1727 1715
1716 TEST_F(SafeBrowsingDatabaseTest, TestCachedFullMiss) {
1717 std::vector<SBListChunkRanges> lists;
1718 ASSERT_TRUE(database_->UpdateStarted(&lists));
1719
1720 SBChunkList chunks;
1721 {
1722 // Insert prefix 1001 into database.
1723 SBChunk chunk;
1724 InsertAddChunkHostPrefixValue(&chunk, 1, SBPrefix(100), SBPrefix(1001));
1725 chunks.push_back(chunk);
1726 }
1727 {
1728 // Insert prefix 1002 into database.
1729 SBChunk chunk;
1730 InsertAddChunkHostPrefixValue(&chunk, 2, SBPrefix(101), SBPrefix(1002));
1731 chunks.push_back(chunk);
1732 }
1733
1734 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1735 database_->UpdateFinished(true);
1736
1737 {
1738 // Cache a full miss result for 1001.
1739 std::vector<SBPrefix> prefixes;
1740 prefixes.push_back(SBPrefix(1001));
1741 std::vector<SBFullHashResult> cache_results;
1742 database_->CacheHashResults(
1743 prefixes, cache_results, base::TimeDelta::FromMinutes(60));
1744 }
1745
1746 {
1747 // Check if DB contains (prefix 1001, suffix 01). Should return false, since
1748 // we have a cached miss for that prefix.
1749 std::vector<SBFullHash> full_hashes;
1750 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x01"));
1751 std::vector<SBPrefix> prefix_hits;
1752 std::vector<SBFullHashResult> cache_hits;
1753 EXPECT_FALSE(
1754 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1755 EXPECT_TRUE(prefix_hits.empty());
1756 EXPECT_TRUE(cache_hits.empty());
1757
1758 // Add (1002,01) to search. Should return a prefix hit for 1002 only.
1759 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1002, "\x01"));
1760 prefix_hits.clear();
1761 cache_hits.clear();
1762 EXPECT_TRUE(
1763 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1764 ASSERT_EQ(1U, prefix_hits.size());
1765 EXPECT_EQ(1002U, prefix_hits[0]);
1766 EXPECT_TRUE(cache_hits.empty());
1767 }
1768 }
1769
1770 TEST_F(SafeBrowsingDatabaseTest, TestCachedPrefixHitFullMiss) {
1771 std::vector<SBListChunkRanges> lists;
1772 ASSERT_TRUE(database_->UpdateStarted(&lists));
1773
1774 SBChunkList chunks;
1775 {
1776 // Insert prefix 1001 into database.
1777 SBChunk chunk;
1778 InsertAddChunkHostPrefixValue(&chunk, 1, SBPrefix(100), SBPrefix(1001));
1779 chunks.push_back(chunk);
1780 }
1781 {
1782 // Insert prefix 1002 into database.
1783 SBChunk chunk;
1784 InsertAddChunkHostPrefixValue(&chunk, 2, SBPrefix(101), SBPrefix(1002));
1785 chunks.push_back(chunk);
1786 }
1787
1788 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1789 database_->UpdateFinished(true);
1790
1791 {
1792 // Check if DB contains (prefix 1001, suffix 01). Should return a prefix
1793 // hit.
1794 std::vector<SBFullHash> full_hashes;
1795 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x01"));
1796 std::vector<SBPrefix> prefix_hits;
1797 std::vector<SBFullHashResult> cache_hits;
1798 EXPECT_TRUE(
1799 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1800 ASSERT_EQ(1U, prefix_hits.size());
1801 EXPECT_EQ(1001U, prefix_hits[0]);
1802 EXPECT_TRUE(cache_hits.empty());
1803
1804 // Add (1002,01) to search. Should return a prefix hit for both 1001 and
1805 // 1002.
1806 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1002, "\x01"));
1807 prefix_hits.clear();
1808 cache_hits.clear();
1809 EXPECT_TRUE(
1810 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1811 ASSERT_EQ(2U, prefix_hits.size());
1812 EXPECT_EQ(1001U, prefix_hits[0]);
1813 EXPECT_EQ(1002U, prefix_hits[1]);
1814 EXPECT_TRUE(cache_hits.empty());
1815
1816 // Add (1003,01) to search. Should still return a prefix hit for both 1001
1817 // and 1002.
1818 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1003, "\x01"));
1819 prefix_hits.clear();
1820 cache_hits.clear();
1821 EXPECT_TRUE(
1822 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1823 ASSERT_EQ(2U, prefix_hits.size());
1824 EXPECT_EQ(1001U, prefix_hits[0]);
1825 EXPECT_EQ(1002U, prefix_hits[1]);
1826 EXPECT_TRUE(cache_hits.empty());
1827 }
1828
1829 {
1830 // Cache a fullhash result for (1001,01) and (1001,03).
1831 std::vector<SBPrefix> prefixes;
1832 prefixes.push_back(SBPrefix(1001));
1833 std::vector<SBFullHashResult> cache_results;
1834
1835 SBFullHashResult full_hash_result;
1836 full_hash_result.hash = SBFullHashForPrefixAndSuffix(1001, "\x01");
1837 full_hash_result.list_name = safe_browsing_util::kMalwareList;
1838 cache_results.push_back(full_hash_result);
1839
1840 full_hash_result.hash = SBFullHashForPrefixAndSuffix(1001, "\x03");
1841 cache_results.push_back(full_hash_result);
1842
1843 database_->CacheHashResults(
1844 prefixes, cache_results, base::TimeDelta::FromMinutes(60));
1845 }
1846
1847 {
1848 // Check again if DB contains (prefix 1001, suffix 01). Should return a
1849 // cache hit now.
1850 std::vector<SBFullHash> full_hashes;
1851 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x01"));
1852 std::vector<SBPrefix> prefix_hits;
1853 std::vector<SBFullHashResult> cache_hits;
1854 EXPECT_TRUE(
1855 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1856 EXPECT_TRUE(prefix_hits.empty());
1857 ASSERT_EQ(1U, cache_hits.size());
1858 EXPECT_TRUE(SBFullHashEqual(SBFullHashForPrefixAndSuffix(1001, "\x01"),
1859 cache_hits[0].hash));
1860
1861 // Add (1002,01) to search. Should return a cache hit for 1001 and prefix
1862 // hit for 1002.
1863 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1002, "\x01"));
1864 prefix_hits.clear();
1865 cache_hits.clear();
1866 EXPECT_TRUE(
1867 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1868 ASSERT_EQ(1U, prefix_hits.size());
1869 EXPECT_EQ(1002U, prefix_hits[0]);
1870 ASSERT_EQ(1U, cache_hits.size());
1871 EXPECT_TRUE(SBFullHashEqual(SBFullHashForPrefixAndSuffix(1001, "\x01"),
1872 cache_hits[0].hash));
1873
1874 // Add (1001,03) to search. Should return 2 cache hits for 1001 and a prefix
1875 // hit for 1002.
1876 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x03"));
1877 prefix_hits.clear();
1878 cache_hits.clear();
1879 EXPECT_TRUE(
1880 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1881 ASSERT_EQ(1U, prefix_hits.size());
1882 EXPECT_EQ(1002U, prefix_hits[0]);
1883 ASSERT_EQ(2U, cache_hits.size());
1884 EXPECT_TRUE(SBFullHashEqual(SBFullHashForPrefixAndSuffix(1001, "\x01"),
1885 cache_hits[0].hash));
1886 EXPECT_TRUE(SBFullHashEqual(SBFullHashForPrefixAndSuffix(1001, "\x03"),
1887 cache_hits[1].hash));
1888 }
1889
1890 {
1891 // Check if DB contains only (prefix 1001, suffix 03). Should return a cache
1892 // hit.
1893 std::vector<SBFullHash> full_hashes;
1894 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x03"));
1895 std::vector<SBPrefix> prefix_hits;
1896 std::vector<SBFullHashResult> cache_hits;
1897 EXPECT_TRUE(
1898 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1899 EXPECT_TRUE(prefix_hits.empty());
1900 ASSERT_EQ(1U, cache_hits.size());
1901 EXPECT_TRUE(SBFullHashEqual(SBFullHashForPrefixAndSuffix(1001, "\x03"),
1902 cache_hits[0].hash));
1903 }
1904
1905 {
1906 // Check if DB contains (prefix 1001, suffix 02). Should return false, since
1907 // we have a cached fullhash for that prefix but which does not match the
1908 // fullhash.
1909 std::vector<SBFullHash> full_hashes;
1910 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x02"));
1911 std::vector<SBPrefix> prefix_hits;
1912 std::vector<SBFullHashResult> cache_hits;
1913 EXPECT_FALSE(
1914 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1915 EXPECT_TRUE(prefix_hits.empty());
1916 EXPECT_TRUE(cache_hits.empty());
1917
1918 // Add (1002,01) to search. Should return a prefix hit for 1002.
1919 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1002, "\x01"));
1920 prefix_hits.clear();
1921 cache_hits.clear();
1922 EXPECT_TRUE(
1923 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1924 ASSERT_EQ(1U, prefix_hits.size());
1925 EXPECT_EQ(1002U, prefix_hits[0]);
1926 EXPECT_TRUE(cache_hits.empty());
1927 }
1928 }
1929
1930 TEST_F(SafeBrowsingDatabaseTest, TestBrowseFullHashMatching) {
1931 std::vector<SBListChunkRanges> lists;
1932 ASSERT_TRUE(database_->UpdateStarted(&lists));
1933
1934 SBChunkList chunks;
1935 {
1936 // Insert fullhash (1001,1) into database.
1937 SBChunk chunk;
1938 InsertAddChunkHostFullHashValue(
1939 &chunk, 1, SBPrefix(100), SBFullHashForPrefixAndSuffix(1001, "\x01"));
1940 chunks.push_back(chunk);
1941 }
1942 {
1943 // Insert fullhash (1001,2) into database.
1944 SBChunk chunk;
1945 InsertAddChunkHostFullHashValue(
1946 &chunk, 2, SBPrefix(100), SBFullHashForPrefixAndSuffix(1001, "\x02"));
1947 chunks.push_back(chunk);
1948 }
1949
1950 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
1951 database_->UpdateFinished(true);
1952
1953 {
1954 // Check if DB contains (prefix 1001, suffix 03). Should return false,
1955 // since the DB contains a matching prefix but the full hashes do not match.
1956 std::vector<SBFullHash> full_hashes;
1957 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x03"));
1958 std::vector<SBPrefix> prefix_hits;
1959 std::vector<SBFullHashResult> cache_hits;
1960 EXPECT_FALSE(
1961 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1962 EXPECT_TRUE(prefix_hits.empty());
1963 EXPECT_TRUE(cache_hits.empty());
1964
1965 // Add (1001,1). Should now return a prefix hit.
1966 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x01"));
1967 prefix_hits.clear();
1968 cache_hits.clear();
1969 EXPECT_TRUE(
1970 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1971 ASSERT_EQ(1U, prefix_hits.size());
1972 EXPECT_EQ(1001U, prefix_hits[0]);
1973 EXPECT_TRUE(cache_hits.empty());
1974
1975 // Add (1001,2). Should still return a single prefix hit.
1976 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x02"));
1977 prefix_hits.clear();
1978 cache_hits.clear();
1979 EXPECT_TRUE(
1980 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1981 ASSERT_EQ(1U, prefix_hits.size());
1982 EXPECT_EQ(1001U, prefix_hits[0]);
1983 EXPECT_TRUE(cache_hits.empty());
1984 }
1985
1986 {
1987 // Check if DB contains (prefix 1001, suffix 02) alone. Should return a
1988 // prefix hit.
1989 std::vector<SBFullHash> full_hashes;
1990 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x02"));
1991 std::vector<SBPrefix> prefix_hits;
1992 std::vector<SBFullHashResult> cache_hits;
1993 EXPECT_TRUE(
1994 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
1995 ASSERT_EQ(1U, prefix_hits.size());
1996 EXPECT_EQ(1001U, prefix_hits[0]);
1997 EXPECT_TRUE(cache_hits.empty());
1998 }
1999
2000 {
2001 // Cache a fullhash result for (prefix 1001, suffix 02).
2002 std::vector<SBPrefix> prefixes;
2003 prefixes.push_back(SBPrefix(1001));
2004 std::vector<SBFullHashResult> cache_results;
2005
2006 SBFullHashResult full_hash_result;
2007 full_hash_result.hash = SBFullHashForPrefixAndSuffix(1001, "\x02");
2008 full_hash_result.list_name = safe_browsing_util::kMalwareList;
2009
2010 cache_results.push_back(full_hash_result);
2011
2012 database_->CacheHashResults(
2013 prefixes, cache_results, base::TimeDelta::FromMinutes(60));
2014 }
2015
2016 {
2017 // Check if DB contains (prefix 1001, suffix 03). Should return false,
2018 // since the DB contains a cached hit with matching prefix but not
2019 // matching the fullhash.
2020 std::vector<SBFullHash> full_hashes;
2021 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x03"));
2022 std::vector<SBPrefix> prefix_hits;
2023 std::vector<SBFullHashResult> cache_hits;
2024 EXPECT_FALSE(
2025 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
2026 EXPECT_TRUE(prefix_hits.empty());
2027 EXPECT_TRUE(cache_hits.empty());
2028
2029 // Add (1001,1). Should still return false.
2030 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x01"));
2031 prefix_hits.clear();
2032 cache_hits.clear();
2033 EXPECT_FALSE(
2034 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
2035 EXPECT_TRUE(prefix_hits.empty());
2036 EXPECT_TRUE(cache_hits.empty());
2037
2038 // Add (1001,2). Should now return a cache hit.
2039 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x02"));
2040 prefix_hits.clear();
2041 cache_hits.clear();
2042 EXPECT_TRUE(
2043 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
2044 EXPECT_TRUE(prefix_hits.empty());
2045 ASSERT_EQ(1U, cache_hits.size());
2046 EXPECT_TRUE(SBFullHashEqual(SBFullHashForPrefixAndSuffix(1001, "\x02"),
2047 cache_hits[0].hash));
2048 }
2049
2050 ASSERT_TRUE(database_->UpdateStarted(&lists));
2051 chunks.clear();
2052 {
2053 // Sub fullhash (1001,1) from database.
2054 SBChunk chunk;
2055 InsertSubChunkHostFullHashValue(&chunk,
2056 11,
2057 1,
2058 SBPrefix(100),
2059 SBFullHashForPrefixAndSuffix(1001, "\x01"));
2060 chunks.push_back(chunk);
2061 }
2062 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
2063 database_->UpdateFinished(true);
2064
2065 {
2066 // Check if DB contains (prefix 1001, suffix 01). Should return false,
2067 // since it was just subbed.
2068 std::vector<SBFullHash> full_hashes;
2069 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x01"));
2070 std::vector<SBPrefix> prefix_hits;
2071 std::vector<SBFullHashResult> cache_hits;
2072 EXPECT_FALSE(
2073 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
2074 EXPECT_TRUE(prefix_hits.empty());
2075 EXPECT_TRUE(cache_hits.empty());
2076
2077 // Add (1001,3). Should still return false.
2078 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x03"));
2079 prefix_hits.clear();
2080 cache_hits.clear();
2081 EXPECT_FALSE(
2082 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
2083 EXPECT_TRUE(prefix_hits.empty());
2084 EXPECT_TRUE(cache_hits.empty());
2085
2086 // Add (1001,2). Should now return a prefix hit.
2087 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x02"));
2088 prefix_hits.clear();
2089 cache_hits.clear();
2090 EXPECT_TRUE(
2091 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
2092 ASSERT_EQ(1U, prefix_hits.size());
2093 EXPECT_EQ(1001U, prefix_hits[0]);
2094 EXPECT_TRUE(cache_hits.empty());
2095 }
2096
2097 ASSERT_TRUE(database_->UpdateStarted(&lists));
2098 chunks.clear();
2099 {
2100 // Sub fullhash (1001,2) from database.
2101 SBChunk chunk;
2102 InsertSubChunkHostFullHashValue(&chunk,
2103 12,
2104 2,
2105 SBPrefix(100),
2106 SBFullHashForPrefixAndSuffix(1001, "\x02"));
2107 chunks.push_back(chunk);
2108 }
2109 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
2110 database_->UpdateFinished(true);
2111
2112 {
2113 // Check if DB contains (prefix 1001, suffix 01). Should return false,
2114 // since it was just subbed.
2115 std::vector<SBFullHash> full_hashes;
2116 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x01"));
2117 std::vector<SBPrefix> prefix_hits;
2118 std::vector<SBFullHashResult> cache_hits;
2119 EXPECT_FALSE(
2120 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
2121 EXPECT_TRUE(prefix_hits.empty());
2122 EXPECT_TRUE(cache_hits.empty());
2123
2124 // Add (1001,3). Should still return false.
2125 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x03"));
2126 prefix_hits.clear();
2127 cache_hits.clear();
2128 EXPECT_FALSE(
2129 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
2130 EXPECT_TRUE(prefix_hits.empty());
2131 EXPECT_TRUE(cache_hits.empty());
2132
2133 // Add (1001,2). Should still return false.
2134 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x02"));
2135 prefix_hits.clear();
2136 cache_hits.clear();
2137 EXPECT_FALSE(
2138 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
2139 EXPECT_TRUE(prefix_hits.empty());
2140 EXPECT_TRUE(cache_hits.empty());
2141 }
2142 }
2143
2144 TEST_F(SafeBrowsingDatabaseTest, TestBrowseAddFullHashAddPrefix) {
2145 std::vector<SBListChunkRanges> lists;
2146
2147 // Update 1: add a fullhash.
2148 ASSERT_TRUE(database_->UpdateStarted(&lists));
2149 SBChunkList chunks;
2150 {
2151 SBChunk chunk;
2152 InsertAddChunkHostFullHashValue(
2153 &chunk, 1, SBPrefix(100), SBFullHashForPrefixAndSuffix(1001, "\x01"));
2154 chunks.push_back(chunk);
2155 }
2156 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
2157 database_->UpdateFinished(true);
2158
2159 // Update 2: add same prefix.
2160 ASSERT_TRUE(database_->UpdateStarted(&lists));
2161 chunks.clear();
2162 {
2163 SBChunk chunk;
2164 InsertAddChunkHostPrefixValue(
2165 &chunk, 2, SBPrefix(100), SBPrefix(1001));
2166 chunks.push_back(chunk);
2167 }
2168 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
2169 database_->UpdateFinished(true);
2170
2171 {
2172 // Check if DB contains (prefix 1001, suffix 02). Should return true,
2173 // since we added the prefix.
2174 std::vector<SBFullHash> full_hashes;
2175 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x02"));
2176 std::vector<SBPrefix> prefix_hits;
2177 std::vector<SBFullHashResult> cache_hits;
2178 EXPECT_TRUE(
2179 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
2180 ASSERT_EQ(1U, prefix_hits.size());
2181 EXPECT_EQ(1001U, prefix_hits[0]);
2182 EXPECT_TRUE(cache_hits.empty());
2183 }
2184
2185 // Update 3: sub the fullhash.
2186 ASSERT_TRUE(database_->UpdateStarted(&lists));
2187 chunks.clear();
2188 {
2189 // Sub fullhash (1001,1) from database.
2190 SBChunk chunk;
2191 InsertSubChunkHostFullHashValue(&chunk,
2192 12,
2193 2,
2194 SBPrefix(100),
2195 SBFullHashForPrefixAndSuffix(1001, "\x01"));
2196 chunks.push_back(chunk);
2197 }
2198 database_->InsertChunks(safe_browsing_util::kMalwareList, chunks);
2199 database_->UpdateFinished(true);
2200
2201 {
2202 // Check if DB contains (prefix 1001, suffix 02). Should still return true,
2203 // since we added the prefix.
2204 std::vector<SBFullHash> full_hashes;
2205 full_hashes.push_back(SBFullHashForPrefixAndSuffix(1001, "\x02"));
2206 std::vector<SBPrefix> prefix_hits;
2207 std::vector<SBFullHashResult> cache_hits;
2208 EXPECT_TRUE(
2209 ContainsBrowseUrlHashes(&full_hashes, &prefix_hits, &cache_hits));
2210 ASSERT_EQ(1U, prefix_hits.size());
2211 EXPECT_EQ(1001U, prefix_hits[0]);
2212 EXPECT_TRUE(cache_hits.empty());
2213 }
2214 }
2215
1728 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) { 2216 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) {
1729 database_.reset(); 2217 database_.reset();
1730 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile(); 2218 SafeBrowsingStoreFile* browse_store = new SafeBrowsingStoreFile();
1731 SafeBrowsingStoreFile* ip_blacklist_store = new SafeBrowsingStoreFile(); 2219 SafeBrowsingStoreFile* ip_blacklist_store = new SafeBrowsingStoreFile();
1732 database_.reset(new SafeBrowsingDatabaseNew(browse_store, 2220 database_.reset(new SafeBrowsingDatabaseNew(browse_store,
1733 NULL, 2221 NULL,
1734 NULL, 2222 NULL,
1735 NULL, 2223 NULL,
1736 NULL, 2224 NULL,
1737 NULL, 2225 NULL,
1738 ip_blacklist_store)); 2226 ip_blacklist_store));
1739 database_->Init(database_filename_); 2227 database_->Init(database_filename_);
1740 std::vector<SBListChunkRanges> lists; 2228 std::vector<SBListChunkRanges> lists;
1741 EXPECT_TRUE(database_->UpdateStarted(&lists)); 2229 ASSERT_TRUE(database_->UpdateStarted(&lists));
1742 2230
1743 // IPv4 prefix match for ::ffff:192.168.1.0/120. 2231 // IPv4 prefix match for ::ffff:192.168.1.0/120.
1744 SBChunkList chunks; 2232 SBChunkList chunks;
1745 SBChunk chunk; 2233 SBChunk chunk;
1746 InsertAddChunkFullHash(&chunk, 1, "::ffff:192.168.1.0", 120); 2234 InsertAddChunkFullHash(&chunk, 1, "::ffff:192.168.1.0", 120);
1747 chunks.push_back(chunk); 2235 chunks.push_back(chunk);
1748 database_->InsertChunks(safe_browsing_util::kIPBlacklist, chunks); 2236 database_->InsertChunks(safe_browsing_util::kIPBlacklist, chunks);
1749 2237
1750 // IPv4 exact match for ::ffff:192.1.1.1. 2238 // IPv4 exact match for ::ffff:192.1.1.1.
1751 chunks.clear(); 2239 chunks.clear();
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
1815 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.124.0")); 2303 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.124.0"));
1816 2304
1817 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.127.255")); 2305 EXPECT_FALSE(database_->ContainsMalwareIP("192.1.127.255"));
1818 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.0")); 2306 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.0"));
1819 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.1.128.1")); 2307 EXPECT_TRUE(database_->ContainsMalwareIP("::ffff:192.1.128.1"));
1820 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.255")); 2308 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.128.255"));
1821 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.0")); 2309 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.0"));
1822 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.255")); 2310 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.255"));
1823 EXPECT_FALSE(database_->ContainsMalwareIP("192.2.0.0")); 2311 EXPECT_FALSE(database_->ContainsMalwareIP("192.2.0.0"));
1824 } 2312 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698