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

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

Issue 1870003002: Convert //chrome/browser/safe_browsing from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and address comments Created 4 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // Unit tests for the SafeBrowsing storage system. 5 // Unit tests for the SafeBrowsing storage system.
6 6
7 #include "chrome/browser/safe_browsing/safe_browsing_database.h" 7 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
8 8
9 #include <stddef.h> 9 #include <stddef.h>
10
10 #include <utility> 11 #include <utility>
11 12
12 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h" 14 #include "base/files/scoped_temp_dir.h"
14 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/memory/ptr_util.h"
15 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
16 #include "base/sha1.h" 18 #include "base/sha1.h"
17 #include "base/strings/string_number_conversions.h" 19 #include "base/strings/string_number_conversions.h"
18 #include "base/strings/string_split.h" 20 #include "base/strings/string_split.h"
19 #include "base/test/test_simple_task_runner.h" 21 #include "base/test/test_simple_task_runner.h"
20 #include "base/time/time.h" 22 #include "base/time/time.h"
21 #include "chrome/browser/safe_browsing/chunk.pb.h" 23 #include "chrome/browser/safe_browsing/chunk.pb.h"
22 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h" 24 #include "chrome/browser/safe_browsing/safe_browsing_store_file.h"
23 #include "crypto/sha2.h" 25 #include "crypto/sha2.h"
24 #include "net/base/ip_address.h" 26 #include "net/base/ip_address.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 EXPECT_TRUE(ip_address.IsIPv6()); 60 EXPECT_TRUE(ip_address.IsIPv6());
59 const std::string hashed_ip_prefix = 61 const std::string hashed_ip_prefix =
60 base::SHA1HashString(net::IPAddressToPackedString(ip_address)); 62 base::SHA1HashString(net::IPAddressToPackedString(ip_address));
61 std::string hash(crypto::kSHA256Length, '\0'); 63 std::string hash(crypto::kSHA256Length, '\0');
62 hash.replace(0, hashed_ip_prefix.size(), hashed_ip_prefix); 64 hash.replace(0, hashed_ip_prefix.size(), hashed_ip_prefix);
63 hash[base::kSHA1Length] = static_cast<char>(prefix_size); 65 hash[base::kSHA1Length] = static_cast<char>(prefix_size);
64 return hash; 66 return hash;
65 } 67 }
66 68
67 // Helper to build a chunk. Caller takes ownership. 69 // Helper to build a chunk. Caller takes ownership.
68 scoped_ptr<SBChunkData> BuildChunk(int chunk_number, 70 std::unique_ptr<SBChunkData> BuildChunk(
69 ChunkData::ChunkType chunk_type, 71 int chunk_number,
70 ChunkData::PrefixType prefix_type, 72 ChunkData::ChunkType chunk_type,
71 const void* data, 73 ChunkData::PrefixType prefix_type,
72 size_t data_size, 74 const void* data,
73 const std::vector<int>& add_chunk_numbers) { 75 size_t data_size,
74 scoped_ptr<ChunkData> raw_data(new ChunkData); 76 const std::vector<int>& add_chunk_numbers) {
77 std::unique_ptr<ChunkData> raw_data(new ChunkData);
75 raw_data->set_chunk_number(chunk_number); 78 raw_data->set_chunk_number(chunk_number);
76 raw_data->set_chunk_type(chunk_type); 79 raw_data->set_chunk_type(chunk_type);
77 raw_data->set_prefix_type(prefix_type); 80 raw_data->set_prefix_type(prefix_type);
78 raw_data->set_hashes(data, data_size); 81 raw_data->set_hashes(data, data_size);
79 raw_data->clear_add_numbers(); 82 raw_data->clear_add_numbers();
80 for (size_t i = 0; i < add_chunk_numbers.size(); ++i) { 83 for (size_t i = 0; i < add_chunk_numbers.size(); ++i) {
81 raw_data->add_add_numbers(add_chunk_numbers[i]); 84 raw_data->add_add_numbers(add_chunk_numbers[i]);
82 } 85 }
83 86
84 return make_scoped_ptr(new SBChunkData(std::move(raw_data))); 87 return base::WrapUnique(new SBChunkData(std::move(raw_data)));
85 } 88 }
86 89
87 // Create add chunk with a single prefix. 90 // Create add chunk with a single prefix.
88 scoped_ptr<SBChunkData> AddChunkPrefix(int chunk_number, SBPrefix prefix) { 91 std::unique_ptr<SBChunkData> AddChunkPrefix(int chunk_number, SBPrefix prefix) {
89 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::PREFIX_4B, &prefix, 92 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::PREFIX_4B, &prefix,
90 sizeof(prefix), std::vector<int>()); 93 sizeof(prefix), std::vector<int>());
91 } 94 }
92 95
93 // Create add chunk with a single prefix generated from |value|. 96 // Create add chunk with a single prefix generated from |value|.
94 scoped_ptr<SBChunkData> AddChunkPrefixValue(int chunk_number, 97 std::unique_ptr<SBChunkData> AddChunkPrefixValue(int chunk_number,
95 const std::string& value) { 98 const std::string& value) {
96 return AddChunkPrefix(chunk_number, SBPrefixForString(value)); 99 return AddChunkPrefix(chunk_number, SBPrefixForString(value));
97 } 100 }
98 101
99 // Generate an add chunk with two prefixes. 102 // Generate an add chunk with two prefixes.
100 scoped_ptr<SBChunkData> AddChunkPrefix2Value(int chunk_number, 103 std::unique_ptr<SBChunkData> AddChunkPrefix2Value(int chunk_number,
101 const std::string& value1, 104 const std::string& value1,
102 const std::string& value2) { 105 const std::string& value2) {
103 const SBPrefix prefixes[2] = { 106 const SBPrefix prefixes[2] = {
104 SBPrefixForString(value1), 107 SBPrefixForString(value1),
105 SBPrefixForString(value2), 108 SBPrefixForString(value2),
106 }; 109 };
107 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::PREFIX_4B, 110 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::PREFIX_4B,
108 &prefixes[0], sizeof(prefixes), std::vector<int>()); 111 &prefixes[0], sizeof(prefixes), std::vector<int>());
109 } 112 }
110 113
111 // Generate an add chunk with four prefixes. 114 // Generate an add chunk with four prefixes.
112 scoped_ptr<SBChunkData> AddChunkPrefix4Value(int chunk_number, 115 std::unique_ptr<SBChunkData> AddChunkPrefix4Value(int chunk_number,
113 const std::string& value1, 116 const std::string& value1,
114 const std::string& value2, 117 const std::string& value2,
115 const std::string& value3, 118 const std::string& value3,
116 const std::string& value4) { 119 const std::string& value4) {
117 const SBPrefix prefixes[4] = { 120 const SBPrefix prefixes[4] = {
118 SBPrefixForString(value1), 121 SBPrefixForString(value1),
119 SBPrefixForString(value2), 122 SBPrefixForString(value2),
120 SBPrefixForString(value3), 123 SBPrefixForString(value3),
121 SBPrefixForString(value4), 124 SBPrefixForString(value4),
122 }; 125 };
123 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::PREFIX_4B, 126 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::PREFIX_4B,
124 &prefixes[0], sizeof(prefixes), std::vector<int>()); 127 &prefixes[0], sizeof(prefixes), std::vector<int>());
125 } 128 }
126 129
127 // Generate an add chunk with a full hash. 130 // Generate an add chunk with a full hash.
128 scoped_ptr<SBChunkData> AddChunkFullHash(int chunk_number, 131 std::unique_ptr<SBChunkData> AddChunkFullHash(int chunk_number,
129 SBFullHash full_hash) { 132 SBFullHash full_hash) {
130 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::FULL_32B, 133 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::FULL_32B,
131 &full_hash, sizeof(full_hash), std::vector<int>()); 134 &full_hash, sizeof(full_hash), std::vector<int>());
132 } 135 }
133 136
134 // Generate an add chunk with a full hash generated from |value|. 137 // Generate an add chunk with a full hash generated from |value|.
135 scoped_ptr<SBChunkData> AddChunkFullHashValue(int chunk_number, 138 std::unique_ptr<SBChunkData> AddChunkFullHashValue(int chunk_number,
136 const std::string& value) { 139 const std::string& value) {
137 return AddChunkFullHash(chunk_number, 140 return AddChunkFullHash(chunk_number,
138 SBFullHashForString(value)); 141 SBFullHashForString(value));
139 } 142 }
140 143
141 // Generate an add chunk with two full hashes. 144 // Generate an add chunk with two full hashes.
142 scoped_ptr<SBChunkData> AddChunkFullHash2Value(int chunk_number, 145 std::unique_ptr<SBChunkData> AddChunkFullHash2Value(int chunk_number,
143 const std::string& value1, 146 const std::string& value1,
144 const std::string& value2) { 147 const std::string& value2) {
145 const SBFullHash full_hashes[2] = { 148 const SBFullHash full_hashes[2] = {
146 SBFullHashForString(value1), 149 SBFullHashForString(value1),
147 SBFullHashForString(value2), 150 SBFullHashForString(value2),
148 }; 151 };
149 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::FULL_32B, 152 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::FULL_32B,
150 &full_hashes[0], sizeof(full_hashes), std::vector<int>()); 153 &full_hashes[0], sizeof(full_hashes), std::vector<int>());
151 } 154 }
152 155
153 // Generate a sub chunk with a prefix generated from |value|. 156 // Generate a sub chunk with a prefix generated from |value|.
154 scoped_ptr<SBChunkData> SubChunkPrefixValue(int chunk_number, 157 std::unique_ptr<SBChunkData> SubChunkPrefixValue(int chunk_number,
155 const std::string& value, 158 const std::string& value,
156 int add_chunk_number) { 159 int add_chunk_number) {
157 const SBPrefix prefix = SBPrefixForString(value); 160 const SBPrefix prefix = SBPrefixForString(value);
158 return BuildChunk(chunk_number, ChunkData::SUB, ChunkData::PREFIX_4B, &prefix, 161 return BuildChunk(chunk_number, ChunkData::SUB, ChunkData::PREFIX_4B, &prefix,
159 sizeof(prefix), std::vector<int>(1, add_chunk_number)); 162 sizeof(prefix), std::vector<int>(1, add_chunk_number));
160 } 163 }
161 164
162 // Generate a sub chunk with two prefixes. 165 // Generate a sub chunk with two prefixes.
163 scoped_ptr<SBChunkData> SubChunkPrefix2Value(int chunk_number, 166 std::unique_ptr<SBChunkData> SubChunkPrefix2Value(int chunk_number,
164 const std::string& value1, 167 const std::string& value1,
165 int add_chunk_number1, 168 int add_chunk_number1,
166 const std::string& value2, 169 const std::string& value2,
167 int add_chunk_number2) { 170 int add_chunk_number2) {
168 const SBPrefix prefixes[2] = { 171 const SBPrefix prefixes[2] = {
169 SBPrefixForString(value1), 172 SBPrefixForString(value1),
170 SBPrefixForString(value2), 173 SBPrefixForString(value2),
171 }; 174 };
172 std::vector<int> add_chunk_numbers; 175 std::vector<int> add_chunk_numbers;
173 add_chunk_numbers.push_back(add_chunk_number1); 176 add_chunk_numbers.push_back(add_chunk_number1);
174 add_chunk_numbers.push_back(add_chunk_number2); 177 add_chunk_numbers.push_back(add_chunk_number2);
175 return BuildChunk(chunk_number, ChunkData::SUB, ChunkData::PREFIX_4B, 178 return BuildChunk(chunk_number, ChunkData::SUB, ChunkData::PREFIX_4B,
176 &prefixes[0], sizeof(prefixes), add_chunk_numbers); 179 &prefixes[0], sizeof(prefixes), add_chunk_numbers);
177 } 180 }
178 181
179 // Generate a sub chunk with a full hash. 182 // Generate a sub chunk with a full hash.
180 scoped_ptr<SBChunkData> SubChunkFullHash(int chunk_number, 183 std::unique_ptr<SBChunkData> SubChunkFullHash(int chunk_number,
181 SBFullHash full_hash, 184 SBFullHash full_hash,
182 int add_chunk_number) { 185 int add_chunk_number) {
183 return BuildChunk(chunk_number, ChunkData::SUB, ChunkData::FULL_32B, 186 return BuildChunk(chunk_number, ChunkData::SUB, ChunkData::FULL_32B,
184 &full_hash, sizeof(full_hash), 187 &full_hash, sizeof(full_hash),
185 std::vector<int>(1, add_chunk_number)); 188 std::vector<int>(1, add_chunk_number));
186 } 189 }
187 190
188 // Generate a sub chunk with a full hash generated from |value|. 191 // Generate a sub chunk with a full hash generated from |value|.
189 scoped_ptr<SBChunkData> SubChunkFullHashValue(int chunk_number, 192 std::unique_ptr<SBChunkData> SubChunkFullHashValue(int chunk_number,
190 const std::string& value, 193 const std::string& value,
191 int add_chunk_number) { 194 int add_chunk_number) {
192 return SubChunkFullHash(chunk_number, 195 return SubChunkFullHash(chunk_number,
193 SBFullHashForString(value), 196 SBFullHashForString(value),
194 add_chunk_number); 197 add_chunk_number);
195 } 198 }
196 199
197 // Generate an add chunk with a single full hash for the ip blacklist. 200 // Generate an add chunk with a single full hash for the ip blacklist.
198 scoped_ptr<SBChunkData> AddChunkHashedIpValue(int chunk_number, 201 std::unique_ptr<SBChunkData> AddChunkHashedIpValue(int chunk_number,
199 const std::string& ip_str, 202 const std::string& ip_str,
200 size_t prefix_size) { 203 size_t prefix_size) {
201 const std::string full_hash_str = HashedIpPrefix(ip_str, prefix_size); 204 const std::string full_hash_str = HashedIpPrefix(ip_str, prefix_size);
202 EXPECT_EQ(sizeof(SBFullHash), full_hash_str.size()); 205 EXPECT_EQ(sizeof(SBFullHash), full_hash_str.size());
203 SBFullHash full_hash; 206 SBFullHash full_hash;
204 std::memcpy(&(full_hash.full_hash), full_hash_str.data(), sizeof(SBFullHash)); 207 std::memcpy(&(full_hash.full_hash), full_hash_str.data(), sizeof(SBFullHash));
205 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::FULL_32B, 208 return BuildChunk(chunk_number, ChunkData::ADD, ChunkData::FULL_32B,
206 &full_hash, sizeof(full_hash), std::vector<int>()); 209 &full_hash, sizeof(full_hash), std::vector<int>());
207 } 210 }
208 211
209 // Prevent DCHECK from killing tests. 212 // Prevent DCHECK from killing tests.
210 // TODO(shess): Pawel disputes the use of this, so the test which uses 213 // TODO(shess): Pawel disputes the use of this, so the test which uses
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
334 } 337 }
335 338
336 void SubDelChunk(const std::string& list, int chunk_id) { 339 void SubDelChunk(const std::string& list, int chunk_id) {
337 DelChunk(list, chunk_id, true); 340 DelChunk(list, chunk_id, true);
338 } 341 }
339 342
340 // Utility function for setting up the database for the caching test. 343 // Utility function for setting up the database for the caching test.
341 void PopulateDatabaseForCacheTest(); 344 void PopulateDatabaseForCacheTest();
342 345
343 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 346 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
344 scoped_ptr<SafeBrowsingDatabaseNew> database_; 347 std::unique_ptr<SafeBrowsingDatabaseNew> database_;
345 base::FilePath database_filename_; 348 base::FilePath database_filename_;
346 base::ScopedTempDir temp_dir_; 349 base::ScopedTempDir temp_dir_;
347 }; 350 };
348 351
349 // Tests retrieving list name information. 352 // Tests retrieving list name information.
350 TEST_F(SafeBrowsingDatabaseTest, BrowseListsInfo) { 353 TEST_F(SafeBrowsingDatabaseTest, BrowseListsInfo) {
351 std::vector<SBListChunkRanges> lists; 354 std::vector<SBListChunkRanges> lists;
352 std::vector<scoped_ptr<SBChunkData>> chunks; 355 std::vector<std::unique_ptr<SBChunkData>> chunks;
353 356
354 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); 357 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html"));
355 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); 358 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html"));
356 chunks.push_back(AddChunkPrefixValue(3, "www.whatever.com/malware.html")); 359 chunks.push_back(AddChunkPrefixValue(3, "www.whatever.com/malware.html"));
357 360
358 ASSERT_TRUE(database_->UpdateStarted(&lists)); 361 ASSERT_TRUE(database_->UpdateStarted(&lists));
359 database_->InsertChunks(kMalwareList, chunks); 362 database_->InsertChunks(kMalwareList, chunks);
360 database_->UpdateFinished(true); 363 database_->UpdateFinished(true);
361 364
362 GetListsInfo(&lists); 365 GetListsInfo(&lists);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 ASSERT_LE(2U, lists.size()); 406 ASSERT_LE(2U, lists.size());
404 EXPECT_EQ(kMalwareList, lists[0].name); 407 EXPECT_EQ(kMalwareList, lists[0].name);
405 EXPECT_EQ("1-3", lists[0].adds); 408 EXPECT_EQ("1-3", lists[0].adds);
406 EXPECT_EQ("7", lists[0].subs); 409 EXPECT_EQ("7", lists[0].subs);
407 EXPECT_EQ(kPhishingList, lists[1].name); 410 EXPECT_EQ(kPhishingList, lists[1].name);
408 EXPECT_EQ("47", lists[1].adds); 411 EXPECT_EQ("47", lists[1].adds);
409 EXPECT_EQ("200-201", lists[1].subs); 412 EXPECT_EQ("200-201", lists[1].subs);
410 } 413 }
411 414
412 TEST_F(SafeBrowsingDatabaseTest, ListNames) { 415 TEST_F(SafeBrowsingDatabaseTest, ListNames) {
413 std::vector<scoped_ptr<SBChunkData>> chunks; 416 std::vector<std::unique_ptr<SBChunkData>> chunks;
414 417
415 std::vector<SBListChunkRanges> lists; 418 std::vector<SBListChunkRanges> lists;
416 ASSERT_TRUE(database_->UpdateStarted(&lists)); 419 ASSERT_TRUE(database_->UpdateStarted(&lists));
417 420
418 // Insert malware, phish, binurl and bindownload add chunks. 421 // Insert malware, phish, binurl and bindownload add chunks.
419 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); 422 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html"));
420 database_->InsertChunks(kMalwareList, chunks); 423 database_->InsertChunks(kMalwareList, chunks);
421 424
422 chunks.clear(); 425 chunks.clear();
423 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html")); 426 chunks.push_back(AddChunkPrefixValue(2, "www.foo.com/malware.html"));
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 &SafeBrowsingDatabase::ContainsUnwantedSoftwareUrl, 571 &SafeBrowsingDatabase::ContainsUnwantedSoftwareUrl,
569 &SafeBrowsingDatabase::ContainsUnwantedSoftwareHashes 572 &SafeBrowsingDatabase::ContainsUnwantedSoftwareHashes
570 }, 573 },
571 }; 574 };
572 575
573 for (const auto& test_case : kTestCases) { 576 for (const auto& test_case : kTestCases) {
574 SCOPED_TRACE(std::string("Tested list at fault => ") + 577 SCOPED_TRACE(std::string("Tested list at fault => ") +
575 test_case.test_list_name); 578 test_case.test_list_name);
576 579
577 std::vector<SBListChunkRanges> lists; 580 std::vector<SBListChunkRanges> lists;
578 std::vector<scoped_ptr<SBChunkData>> chunks; 581 std::vector<std::unique_ptr<SBChunkData>> chunks;
579 582
580 chunks.push_back(AddChunkPrefix2Value(1, 583 chunks.push_back(AddChunkPrefix2Value(1,
581 "www.evil.com/phishing.html", 584 "www.evil.com/phishing.html",
582 "www.evil.com/malware.html")); 585 "www.evil.com/malware.html"));
583 chunks.push_back(AddChunkPrefix4Value(2, 586 chunks.push_back(AddChunkPrefix4Value(2,
584 "www.evil.com/notevil1.html", 587 "www.evil.com/notevil1.html",
585 "www.evil.com/notevil2.html", 588 "www.evil.com/notevil2.html",
586 "www.good.com/good1.html", 589 "www.good.com/good1.html",
587 "www.good.com/good2.html")); 590 "www.good.com/good2.html"));
588 chunks.push_back(AddChunkPrefixValue(3, "192.168.0.1/malware.html")); 591 chunks.push_back(AddChunkPrefixValue(3, "192.168.0.1/malware.html"));
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after
805 test_case.TestContainsFunctions(*database_, true, 808 test_case.TestContainsFunctions(*database_, true,
806 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits); 809 GURL("http://www.evil.com/evil.html"), &prefix_hits, &cache_hits);
807 ASSERT_EQ(1U, prefix_hits.size()); 810 ASSERT_EQ(1U, prefix_hits.size());
808 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]); 811 EXPECT_EQ(SBPrefixForString("www.evil.com/evil.html"), prefix_hits[0]);
809 } 812 }
810 } 813 }
811 814
812 // Test adding zero length chunks to the database. 815 // Test adding zero length chunks to the database.
813 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) { 816 TEST_F(SafeBrowsingDatabaseTest, ZeroSizeChunk) {
814 std::vector<SBListChunkRanges> lists; 817 std::vector<SBListChunkRanges> lists;
815 std::vector<scoped_ptr<SBChunkData>> chunks; 818 std::vector<std::unique_ptr<SBChunkData>> chunks;
816 819
817 // Populate with a couple of normal chunks. 820 // Populate with a couple of normal chunks.
818 chunks.push_back(AddChunkPrefix2Value(1, 821 chunks.push_back(AddChunkPrefix2Value(1,
819 "www.test.com/test1.html", 822 "www.test.com/test1.html",
820 "www.test.com/test2.html")); 823 "www.test.com/test2.html"));
821 chunks.push_back(AddChunkPrefix2Value(10, 824 chunks.push_back(AddChunkPrefix2Value(10,
822 "www.random.com/random1.html", 825 "www.random.com/random1.html",
823 "www.random.com/random2.html")); 826 "www.random.com/random2.html"));
824 827
825 ASSERT_TRUE(database_->UpdateStarted(&lists)); 828 ASSERT_TRUE(database_->UpdateStarted(&lists));
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
892 GetListsInfo(&lists); 895 GetListsInfo(&lists);
893 ASSERT_LE(1U, lists.size()); 896 ASSERT_LE(1U, lists.size());
894 EXPECT_EQ(kMalwareList, lists[0].name); 897 EXPECT_EQ(kMalwareList, lists[0].name);
895 EXPECT_EQ("1,10,19-20,22", lists[0].adds); 898 EXPECT_EQ("1,10,19-20,22", lists[0].adds);
896 EXPECT_TRUE(lists[0].subs.empty()); 899 EXPECT_TRUE(lists[0].subs.empty());
897 } 900 }
898 901
899 // Utility function for setting up the database for the caching test. 902 // Utility function for setting up the database for the caching test.
900 void SafeBrowsingDatabaseTest::PopulateDatabaseForCacheTest() { 903 void SafeBrowsingDatabaseTest::PopulateDatabaseForCacheTest() {
901 // Add a couple prefixes. 904 // Add a couple prefixes.
902 std::vector<scoped_ptr<SBChunkData>> chunks; 905 std::vector<std::unique_ptr<SBChunkData>> chunks;
903 chunks.push_back(AddChunkPrefix2Value(1, 906 chunks.push_back(AddChunkPrefix2Value(1,
904 "www.evil.com/phishing.html", 907 "www.evil.com/phishing.html",
905 "www.evil.com/malware.html")); 908 "www.evil.com/malware.html"));
906 909
907 std::vector<SBListChunkRanges> lists; 910 std::vector<SBListChunkRanges> lists;
908 ASSERT_TRUE(database_->UpdateStarted(&lists)); 911 ASSERT_TRUE(database_->UpdateStarted(&lists));
909 database_->InsertChunks(kMalwareList, chunks); 912 database_->InsertChunks(kMalwareList, chunks);
910 database_->UpdateFinished(true); 913 database_->UpdateFinished(true);
911 914
912 // Cache should be cleared after updating. 915 // Cache should be cleared after updating.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
960 EXPECT_TRUE(prefix_hits.empty()); 963 EXPECT_TRUE(prefix_hits.empty());
961 ASSERT_EQ(1U, cache_hits.size()); 964 ASSERT_EQ(1U, cache_hits.size());
962 EXPECT_TRUE(SBFullHashEqual( 965 EXPECT_TRUE(SBFullHashEqual(
963 cache_hits[0].hash, 966 cache_hits[0].hash,
964 SBFullHashForString("www.evil.com/malware.html"))); 967 SBFullHashForString("www.evil.com/malware.html")));
965 968
966 prefix_hits.clear(); 969 prefix_hits.clear();
967 cache_hits.clear(); 970 cache_hits.clear();
968 971
969 // Test removing a prefix via a sub chunk. 972 // Test removing a prefix via a sub chunk.
970 std::vector<scoped_ptr<SBChunkData>> chunks; 973 std::vector<std::unique_ptr<SBChunkData>> chunks;
971 chunks.push_back(SubChunkPrefixValue(2, "www.evil.com/phishing.html", 1)); 974 chunks.push_back(SubChunkPrefixValue(2, "www.evil.com/phishing.html", 1));
972 975
973 std::vector<SBListChunkRanges> lists; 976 std::vector<SBListChunkRanges> lists;
974 ASSERT_TRUE(database_->UpdateStarted(&lists)); 977 ASSERT_TRUE(database_->UpdateStarted(&lists));
975 database_->InsertChunks(kMalwareList, chunks); 978 database_->InsertChunks(kMalwareList, chunks);
976 database_->UpdateFinished(true); 979 database_->UpdateFinished(true);
977 980
978 // This prefix should still be there, but cached fullhash should be gone. 981 // This prefix should still be there, but cached fullhash should be gone.
979 EXPECT_TRUE(database_->ContainsBrowseUrl( 982 EXPECT_TRUE(database_->ContainsBrowseUrl(
980 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); 983 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits));
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
1134 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits)); 1137 GURL("http://www.fullevil.com/bad2.html"), &prefix_hits, &cache_hits));
1135 1138
1136 // Add a fullhash which has a prefix collision for a known url. 1139 // Add a fullhash which has a prefix collision for a known url.
1137 static const char kExampleFine[] = "www.example.com/fine.html"; 1140 static const char kExampleFine[] = "www.example.com/fine.html";
1138 static const char kExampleCollision[] = 1141 static const char kExampleCollision[] =
1139 "www.example.com/3123364814/malware.htm"; 1142 "www.example.com/3123364814/malware.htm";
1140 ASSERT_EQ(SBPrefixForString(kExampleFine), 1143 ASSERT_EQ(SBPrefixForString(kExampleFine),
1141 SBPrefixForString(kExampleCollision)); 1144 SBPrefixForString(kExampleCollision));
1142 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1145 ASSERT_TRUE(database_->UpdateStarted(&lists));
1143 { 1146 {
1144 std::vector<scoped_ptr<SBChunkData>> chunks; 1147 std::vector<std::unique_ptr<SBChunkData>> chunks;
1145 chunks.push_back(AddChunkPrefixValue(21, kExampleCollision)); 1148 chunks.push_back(AddChunkPrefixValue(21, kExampleCollision));
1146 database_->InsertChunks(kMalwareList, chunks); 1149 database_->InsertChunks(kMalwareList, chunks);
1147 } 1150 }
1148 database_->UpdateFinished(true); 1151 database_->UpdateFinished(true);
1149 1152
1150 // Expect a prefix hit due to the collision between |kExampleFine| and 1153 // Expect a prefix hit due to the collision between |kExampleFine| and
1151 // |kExampleCollision|. 1154 // |kExampleCollision|.
1152 EXPECT_TRUE(database_->ContainsBrowseUrl( 1155 EXPECT_TRUE(database_->ContainsBrowseUrl(
1153 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); 1156 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits));
1154 ASSERT_EQ(1U, prefix_hits.size()); 1157 ASSERT_EQ(1U, prefix_hits.size());
(...skipping 30 matching lines...) Expand all
1185 task_runner_, store, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 1188 task_runner_, store, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
1186 NULL)); 1189 NULL));
1187 database_->Init(database_filename_); 1190 database_->Init(database_filename_);
1188 1191
1189 // This will cause an empty database to be created. 1192 // This will cause an empty database to be created.
1190 std::vector<SBListChunkRanges> lists; 1193 std::vector<SBListChunkRanges> lists;
1191 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1194 ASSERT_TRUE(database_->UpdateStarted(&lists));
1192 database_->UpdateFinished(true); 1195 database_->UpdateFinished(true);
1193 1196
1194 // Create a sub chunk to insert. 1197 // Create a sub chunk to insert.
1195 std::vector<scoped_ptr<SBChunkData>> chunks; 1198 std::vector<std::unique_ptr<SBChunkData>> chunks;
1196 chunks.push_back(SubChunkPrefixValue(7, 1199 chunks.push_back(SubChunkPrefixValue(7,
1197 "www.subbed.com/notevil1.html", 1200 "www.subbed.com/notevil1.html",
1198 19)); 1201 19));
1199 1202
1200 // Corrupt the file by corrupting the checksum, which is not checked 1203 // Corrupt the file by corrupting the checksum, which is not checked
1201 // until the entire table is read in |UpdateFinished()|. 1204 // until the entire table is read in |UpdateFinished()|.
1202 FILE* fp = base::OpenFile(database_filename_, "r+"); 1205 FILE* fp = base::OpenFile(database_filename_, "r+");
1203 ASSERT_TRUE(fp); 1206 ASSERT_TRUE(fp);
1204 ASSERT_NE(-1, fseek(fp, -8, SEEK_END)); 1207 ASSERT_NE(-1, fseek(fp, -8, SEEK_END));
1205 for (size_t i = 0; i < 8; ++i) { 1208 for (size_t i = 0; i < 8; ++i) {
(...skipping 29 matching lines...) Expand all
1235 1238
1236 database_.reset(); 1239 database_.reset();
1237 } 1240 }
1238 1241
1239 // Checks database reading and writing. 1242 // Checks database reading and writing.
1240 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrlPrefixes) { 1243 TEST_F(SafeBrowsingDatabaseTest, ContainsDownloadUrlPrefixes) {
1241 const char kEvil1Url1[] = "www.evil1.com/download1/"; 1244 const char kEvil1Url1[] = "www.evil1.com/download1/";
1242 const char kEvil1Url2[] = "www.evil1.com/download2.html"; 1245 const char kEvil1Url2[] = "www.evil1.com/download2.html";
1243 1246
1244 // Add a simple chunk with one hostkey for download url list. 1247 // Add a simple chunk with one hostkey for download url list.
1245 std::vector<scoped_ptr<SBChunkData>> chunks; 1248 std::vector<std::unique_ptr<SBChunkData>> chunks;
1246 chunks.push_back(AddChunkPrefix2Value(1, kEvil1Url1, kEvil1Url2)); 1249 chunks.push_back(AddChunkPrefix2Value(1, kEvil1Url1, kEvil1Url2));
1247 1250
1248 std::vector<SBListChunkRanges> lists; 1251 std::vector<SBListChunkRanges> lists;
1249 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1252 ASSERT_TRUE(database_->UpdateStarted(&lists));
1250 database_->InsertChunks(kBinUrlList, chunks); 1253 database_->InsertChunks(kBinUrlList, chunks);
1251 database_->UpdateFinished(true); 1254 database_->UpdateFinished(true);
1252 1255
1253 std::vector<SBPrefix> prefix_hits; 1256 std::vector<SBPrefix> prefix_hits;
1254 std::vector<GURL> urls(1); 1257 std::vector<GURL> urls(1);
1255 1258
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1324 database_.reset(); 1327 database_.reset();
1325 } 1328 }
1326 1329
1327 TEST_F(SafeBrowsingDatabaseTest, ContainsResourceUrlPrefixes) { 1330 TEST_F(SafeBrowsingDatabaseTest, ContainsResourceUrlPrefixes) {
1328 const char* kBadUrl1 = "bad1.com/"; 1331 const char* kBadUrl1 = "bad1.com/";
1329 const char* kBadUrl2 = "bad2.com/script.js"; 1332 const char* kBadUrl2 = "bad2.com/script.js";
1330 const SBPrefix kBadPrefix1 = SBPrefixForString(kBadUrl1); 1333 const SBPrefix kBadPrefix1 = SBPrefixForString(kBadUrl1);
1331 const SBPrefix kBadPrefix2 = SBPrefixForString(kBadUrl2); 1334 const SBPrefix kBadPrefix2 = SBPrefixForString(kBadUrl2);
1332 1335
1333 // Populate database 1336 // Populate database
1334 std::vector<scoped_ptr<SBChunkData>> chunks; 1337 std::vector<std::unique_ptr<SBChunkData>> chunks;
1335 chunks.push_back(AddChunkPrefix2Value(1, kBadUrl1, kBadUrl2)); 1338 chunks.push_back(AddChunkPrefix2Value(1, kBadUrl1, kBadUrl2));
1336 1339
1337 std::vector<SBListChunkRanges> lists; 1340 std::vector<SBListChunkRanges> lists;
1338 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1341 ASSERT_TRUE(database_->UpdateStarted(&lists));
1339 database_->InsertChunks(kResourceBlacklist, chunks); 1342 database_->InsertChunks(kResourceBlacklist, chunks);
1340 database_->UpdateFinished(true); 1343 database_->UpdateFinished(true);
1341 1344
1342 struct { 1345 struct {
1343 std::string url; 1346 std::string url;
1344 bool found_in_db; 1347 bool found_in_db;
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
1467 EXPECT_TRUE( 1470 EXPECT_TRUE(
1468 (database_.get()->*test_case.test_list_contains_whitelisted_string)( 1471 (database_.get()->*test_case.test_list_contains_whitelisted_string)(
1469 "asdf")); 1472 "asdf"));
1470 } else { 1473 } else {
1471 EXPECT_FALSE( 1474 EXPECT_FALSE(
1472 (database_.get()->*test_case.test_list_contains_whitelisted_string)( 1475 (database_.get()->*test_case.test_list_contains_whitelisted_string)(
1473 kGoodString)); 1476 kGoodString));
1474 } 1477 }
1475 } 1478 }
1476 1479
1477 std::vector<scoped_ptr<SBChunkData>> chunks; 1480 std::vector<std::unique_ptr<SBChunkData>> chunks;
1478 1481
1479 // Add a few test chunks to the whitelist under test. 1482 // Add a few test chunks to the whitelist under test.
1480 if (test_case.TestUrls()) { 1483 if (test_case.TestUrls()) {
1481 chunks.push_back(AddChunkFullHash2Value(1, kGood1Url1, kGood1Url2)); 1484 chunks.push_back(AddChunkFullHash2Value(1, kGood1Url1, kGood1Url2));
1482 chunks.push_back(AddChunkFullHashValue(2, kGood2Url1)); 1485 chunks.push_back(AddChunkFullHashValue(2, kGood2Url1));
1483 chunks.push_back(AddChunkFullHashValue(4, kGood3Url1)); 1486 chunks.push_back(AddChunkFullHashValue(4, kGood3Url1));
1484 } 1487 }
1485 if (test_case.TestStrings()) 1488 if (test_case.TestStrings())
1486 chunks.push_back(AddChunkFullHashValue(3, kGoodString)); 1489 chunks.push_back(AddChunkFullHashValue(3, kGoodString));
1487 1490
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
1643 EXPECT_FALSE( 1646 EXPECT_FALSE(
1644 (database_.get()->*test_case.test_list_contains_whitelisted_string)( 1647 (database_.get()->*test_case.test_list_contains_whitelisted_string)(
1645 "asdf")); 1648 "asdf"));
1646 } 1649 }
1647 } 1650 }
1648 } 1651 }
1649 1652
1650 // Test to make sure we could insert chunk list that 1653 // Test to make sure we could insert chunk list that
1651 // contains entries for the same host. 1654 // contains entries for the same host.
1652 TEST_F(SafeBrowsingDatabaseTest, SameHostEntriesOkay) { 1655 TEST_F(SafeBrowsingDatabaseTest, SameHostEntriesOkay) {
1653 std::vector<scoped_ptr<SBChunkData>> chunks; 1656 std::vector<std::unique_ptr<SBChunkData>> chunks;
1654 1657
1655 // Add a malware add chunk with two entries of the same host. 1658 // Add a malware add chunk with two entries of the same host.
1656 chunks.push_back(AddChunkPrefix2Value(1, 1659 chunks.push_back(AddChunkPrefix2Value(1,
1657 "www.evil.com/malware1.html", 1660 "www.evil.com/malware1.html",
1658 "www.evil.com/malware2.html")); 1661 "www.evil.com/malware2.html"));
1659 1662
1660 // Insert the testing chunks into database. 1663 // Insert the testing chunks into database.
1661 std::vector<SBListChunkRanges> lists; 1664 std::vector<SBListChunkRanges> lists;
1662 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1665 ASSERT_TRUE(database_->UpdateStarted(&lists));
1663 database_->InsertChunks(kMalwareList, chunks); 1666 database_->InsertChunks(kMalwareList, chunks);
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
1723 EXPECT_TRUE(database_->ContainsBrowseUrl( 1726 EXPECT_TRUE(database_->ContainsBrowseUrl(
1724 GURL("http://www.evil.com/phishing1.html"), &prefix_hits, &cache_hits)); 1727 GURL("http://www.evil.com/phishing1.html"), &prefix_hits, &cache_hits));
1725 EXPECT_FALSE(database_->ContainsBrowseUrl( 1728 EXPECT_FALSE(database_->ContainsBrowseUrl(
1726 GURL("http://www.evil.com/phishing2.html"), &prefix_hits, &cache_hits)); 1729 GURL("http://www.evil.com/phishing2.html"), &prefix_hits, &cache_hits));
1727 } 1730 }
1728 1731
1729 // Test that an empty update doesn't actually update the database. 1732 // Test that an empty update doesn't actually update the database.
1730 // This isn't a functionality requirement, but it is a useful 1733 // This isn't a functionality requirement, but it is a useful
1731 // optimization. 1734 // optimization.
1732 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) { 1735 TEST_F(SafeBrowsingDatabaseTest, EmptyUpdate) {
1733 std::vector<scoped_ptr<SBChunkData>> chunks; 1736 std::vector<std::unique_ptr<SBChunkData>> chunks;
1734 1737
1735 base::FilePath filename = database_->BrowseDBFilename(database_filename_); 1738 base::FilePath filename = database_->BrowseDBFilename(database_filename_);
1736 1739
1737 // Prime the database. 1740 // Prime the database.
1738 std::vector<SBListChunkRanges> lists; 1741 std::vector<SBListChunkRanges> lists;
1739 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1742 ASSERT_TRUE(database_->UpdateStarted(&lists));
1740 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); 1743 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html"));
1741 database_->InsertChunks(kMalwareList, chunks); 1744 database_->InsertChunks(kMalwareList, chunks);
1742 database_->UpdateFinished(true); 1745 database_->UpdateFinished(true);
1743 1746
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1781 1784
1782 // Test that a filter file is written out during update and read back 1785 // Test that a filter file is written out during update and read back
1783 // in during setup. 1786 // in during setup.
1784 TEST_F(SafeBrowsingDatabaseTest, FilterFile) { 1787 TEST_F(SafeBrowsingDatabaseTest, FilterFile) {
1785 // Create a database with trivial example data and write it out. 1788 // Create a database with trivial example data and write it out.
1786 { 1789 {
1787 // Prime the database. 1790 // Prime the database.
1788 std::vector<SBListChunkRanges> lists; 1791 std::vector<SBListChunkRanges> lists;
1789 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1792 ASSERT_TRUE(database_->UpdateStarted(&lists));
1790 1793
1791 std::vector<scoped_ptr<SBChunkData>> chunks; 1794 std::vector<std::unique_ptr<SBChunkData>> chunks;
1792 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html")); 1795 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/malware.html"));
1793 database_->InsertChunks(kMalwareList, chunks); 1796 database_->InsertChunks(kMalwareList, chunks);
1794 database_->UpdateFinished(true); 1797 database_->UpdateFinished(true);
1795 } 1798 }
1796 1799
1797 // Find the malware url in the database, don't find a good url. 1800 // Find the malware url in the database, don't find a good url.
1798 std::vector<SBPrefix> prefix_hits; 1801 std::vector<SBPrefix> prefix_hits;
1799 std::vector<SBFullHashResult> cache_hits; 1802 std::vector<SBFullHashResult> cache_hits;
1800 EXPECT_TRUE(database_->ContainsBrowseUrl( 1803 EXPECT_TRUE(database_->ContainsBrowseUrl(
1801 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits)); 1804 GURL("http://www.evil.com/malware.html"), &prefix_hits, &cache_hits));
(...skipping 25 matching lines...) Expand all
1827 TEST_F(SafeBrowsingDatabaseTest, CachedFullMiss) { 1830 TEST_F(SafeBrowsingDatabaseTest, CachedFullMiss) {
1828 const SBPrefix kPrefix1 = 1001U; 1831 const SBPrefix kPrefix1 = 1001U;
1829 const SBFullHash kFullHash1_1 = 1832 const SBFullHash kFullHash1_1 =
1830 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); 1833 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01");
1831 1834
1832 const SBPrefix kPrefix2 = 1002U; 1835 const SBPrefix kPrefix2 = 1002U;
1833 const SBFullHash kFullHash2_1 = 1836 const SBFullHash kFullHash2_1 =
1834 SBFullHashForPrefixAndSuffix(kPrefix2, "\x01"); 1837 SBFullHashForPrefixAndSuffix(kPrefix2, "\x01");
1835 1838
1836 // Insert prefix kPrefix1 and kPrefix2 into database. 1839 // Insert prefix kPrefix1 and kPrefix2 into database.
1837 std::vector<scoped_ptr<SBChunkData>> chunks; 1840 std::vector<std::unique_ptr<SBChunkData>> chunks;
1838 chunks.push_back(AddChunkPrefix(1, kPrefix1)); 1841 chunks.push_back(AddChunkPrefix(1, kPrefix1));
1839 chunks.push_back(AddChunkPrefix(2, kPrefix2)); 1842 chunks.push_back(AddChunkPrefix(2, kPrefix2));
1840 1843
1841 std::vector<SBListChunkRanges> lists; 1844 std::vector<SBListChunkRanges> lists;
1842 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1845 ASSERT_TRUE(database_->UpdateStarted(&lists));
1843 database_->InsertChunks(kMalwareList, chunks); 1846 database_->InsertChunks(kMalwareList, chunks);
1844 database_->UpdateFinished(true); 1847 database_->UpdateFinished(true);
1845 1848
1846 { 1849 {
1847 // Cache a full miss result for kPrefix1. 1850 // Cache a full miss result for kPrefix1.
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
1882 1885
1883 const SBPrefix kPrefix2 = 1002U; 1886 const SBPrefix kPrefix2 = 1002U;
1884 const SBFullHash kFullHash2_1 = 1887 const SBFullHash kFullHash2_1 =
1885 SBFullHashForPrefixAndSuffix(kPrefix2, "\x01"); 1888 SBFullHashForPrefixAndSuffix(kPrefix2, "\x01");
1886 1889
1887 const SBPrefix kPrefix3 = 1003U; 1890 const SBPrefix kPrefix3 = 1003U;
1888 const SBFullHash kFullHash3_1 = 1891 const SBFullHash kFullHash3_1 =
1889 SBFullHashForPrefixAndSuffix(kPrefix3, "\x01"); 1892 SBFullHashForPrefixAndSuffix(kPrefix3, "\x01");
1890 1893
1891 // Insert prefix kPrefix1 and kPrefix2 into database. 1894 // Insert prefix kPrefix1 and kPrefix2 into database.
1892 std::vector<scoped_ptr<SBChunkData>> chunks; 1895 std::vector<std::unique_ptr<SBChunkData>> chunks;
1893 chunks.push_back(AddChunkPrefix(1, kPrefix1)); 1896 chunks.push_back(AddChunkPrefix(1, kPrefix1));
1894 chunks.push_back(AddChunkPrefix(2, kPrefix2)); 1897 chunks.push_back(AddChunkPrefix(2, kPrefix2));
1895 1898
1896 std::vector<SBListChunkRanges> lists; 1899 std::vector<SBListChunkRanges> lists;
1897 ASSERT_TRUE(database_->UpdateStarted(&lists)); 1900 ASSERT_TRUE(database_->UpdateStarted(&lists));
1898 database_->InsertChunks(kMalwareList, chunks); 1901 database_->InsertChunks(kMalwareList, chunks);
1899 database_->UpdateFinished(true); 1902 database_->UpdateFinished(true);
1900 1903
1901 { 1904 {
1902 // kFullHash1_1 has a prefix hit of kPrefix1. 1905 // kFullHash1_1 has a prefix hit of kPrefix1.
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
2027 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashMatching) { 2030 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashMatching) {
2028 const SBPrefix kPrefix1 = 1001U; 2031 const SBPrefix kPrefix1 = 1001U;
2029 const SBFullHash kFullHash1_1 = 2032 const SBFullHash kFullHash1_1 =
2030 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); 2033 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01");
2031 const SBFullHash kFullHash1_2 = 2034 const SBFullHash kFullHash1_2 =
2032 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02"); 2035 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02");
2033 const SBFullHash kFullHash1_3 = 2036 const SBFullHash kFullHash1_3 =
2034 SBFullHashForPrefixAndSuffix(kPrefix1, "\x03"); 2037 SBFullHashForPrefixAndSuffix(kPrefix1, "\x03");
2035 2038
2036 // Insert two full hashes with a shared prefix. 2039 // Insert two full hashes with a shared prefix.
2037 std::vector<scoped_ptr<SBChunkData>> chunks; 2040 std::vector<std::unique_ptr<SBChunkData>> chunks;
2038 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); 2041 chunks.push_back(AddChunkFullHash(1, kFullHash1_1));
2039 chunks.push_back(AddChunkFullHash(2, kFullHash1_2)); 2042 chunks.push_back(AddChunkFullHash(2, kFullHash1_2));
2040 2043
2041 std::vector<SBListChunkRanges> lists; 2044 std::vector<SBListChunkRanges> lists;
2042 ASSERT_TRUE(database_->UpdateStarted(&lists)); 2045 ASSERT_TRUE(database_->UpdateStarted(&lists));
2043 database_->InsertChunks(kMalwareList, chunks); 2046 database_->InsertChunks(kMalwareList, chunks);
2044 database_->UpdateFinished(true); 2047 database_->UpdateFinished(true);
2045 2048
2046 { 2049 {
2047 // Check a full hash which isn't present. 2050 // Check a full hash which isn't present.
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
2176 } 2179 }
2177 } 2180 }
2178 2181
2179 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashAndPrefixMatching) { 2182 TEST_F(SafeBrowsingDatabaseTest, BrowseFullHashAndPrefixMatching) {
2180 const SBPrefix kPrefix1 = 1001U; 2183 const SBPrefix kPrefix1 = 1001U;
2181 const SBFullHash kFullHash1_1 = 2184 const SBFullHash kFullHash1_1 =
2182 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01"); 2185 SBFullHashForPrefixAndSuffix(kPrefix1, "\x01");
2183 const SBFullHash kFullHash1_2 = 2186 const SBFullHash kFullHash1_2 =
2184 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02"); 2187 SBFullHashForPrefixAndSuffix(kPrefix1, "\x02");
2185 2188
2186 std::vector<scoped_ptr<SBChunkData>> chunks; 2189 std::vector<std::unique_ptr<SBChunkData>> chunks;
2187 chunks.push_back(AddChunkFullHash(1, kFullHash1_1)); 2190 chunks.push_back(AddChunkFullHash(1, kFullHash1_1));
2188 2191
2189 std::vector<SBListChunkRanges> lists; 2192 std::vector<SBListChunkRanges> lists;
2190 ASSERT_TRUE(database_->UpdateStarted(&lists)); 2193 ASSERT_TRUE(database_->UpdateStarted(&lists));
2191 database_->InsertChunks(kMalwareList, chunks); 2194 database_->InsertChunks(kMalwareList, chunks);
2192 database_->UpdateFinished(true); 2195 database_->UpdateFinished(true);
2193 2196
2194 { 2197 {
2195 // kFullHash1_2 does not match kFullHash1_1. 2198 // kFullHash1_2 does not match kFullHash1_1.
2196 std::vector<SBFullHash> full_hashes(1, kFullHash1_2); 2199 std::vector<SBFullHash> full_hashes(1, kFullHash1_2);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
2238 ASSERT_EQ(1U, prefix_hits.size()); 2241 ASSERT_EQ(1U, prefix_hits.size());
2239 EXPECT_EQ(kPrefix1, prefix_hits[0]); 2242 EXPECT_EQ(kPrefix1, prefix_hits[0]);
2240 EXPECT_TRUE(cache_hits.empty()); 2243 EXPECT_TRUE(cache_hits.empty());
2241 } 2244 }
2242 } 2245 }
2243 2246
2244 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) { 2247 TEST_F(SafeBrowsingDatabaseTest, MalwareIpBlacklist) {
2245 std::vector<SBListChunkRanges> lists; 2248 std::vector<SBListChunkRanges> lists;
2246 ASSERT_TRUE(database_->UpdateStarted(&lists)); 2249 ASSERT_TRUE(database_->UpdateStarted(&lists));
2247 2250
2248 std::vector<scoped_ptr<SBChunkData>> chunks; 2251 std::vector<std::unique_ptr<SBChunkData>> chunks;
2249 2252
2250 // IPv4 prefix match for ::ffff:192.168.1.0/120. 2253 // IPv4 prefix match for ::ffff:192.168.1.0/120.
2251 chunks.push_back(AddChunkHashedIpValue(1, "::ffff:192.168.1.0", 120)); 2254 chunks.push_back(AddChunkHashedIpValue(1, "::ffff:192.168.1.0", 120));
2252 2255
2253 // IPv4 exact match for ::ffff:192.1.1.1. 2256 // IPv4 exact match for ::ffff:192.1.1.1.
2254 chunks.push_back(AddChunkHashedIpValue(2, "::ffff:192.1.1.1", 128)); 2257 chunks.push_back(AddChunkHashedIpValue(2, "::ffff:192.1.1.1", 128));
2255 2258
2256 // IPv6 exact match for: fe80::31a:a0ff:fe10:786e/128. 2259 // IPv6 exact match for: fe80::31a:a0ff:fe10:786e/128.
2257 chunks.push_back(AddChunkHashedIpValue(3, "fe80::31a:a0ff:fe10:786e", 128)); 2260 chunks.push_back(AddChunkHashedIpValue(3, "fe80::31a:a0ff:fe10:786e", 128));
2258 2261
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
2306 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.255")); 2309 EXPECT_TRUE(database_->ContainsMalwareIP("192.1.255.255"));
2307 EXPECT_FALSE(database_->ContainsMalwareIP("192.2.0.0")); 2310 EXPECT_FALSE(database_->ContainsMalwareIP("192.2.0.0"));
2308 } 2311 }
2309 2312
2310 TEST_F(SafeBrowsingDatabaseTest, ContainsBrowseURL) { 2313 TEST_F(SafeBrowsingDatabaseTest, ContainsBrowseURL) {
2311 std::vector<SBListChunkRanges> lists; 2314 std::vector<SBListChunkRanges> lists;
2312 ASSERT_TRUE(database_->UpdateStarted(&lists)); 2315 ASSERT_TRUE(database_->UpdateStarted(&lists));
2313 2316
2314 // Add a host-level hit. 2317 // Add a host-level hit.
2315 { 2318 {
2316 std::vector<scoped_ptr<SBChunkData>> chunks; 2319 std::vector<std::unique_ptr<SBChunkData>> chunks;
2317 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/")); 2320 chunks.push_back(AddChunkPrefixValue(1, "www.evil.com/"));
2318 database_->InsertChunks(kMalwareList, chunks); 2321 database_->InsertChunks(kMalwareList, chunks);
2319 } 2322 }
2320 2323
2321 // Add a specific fullhash. 2324 // Add a specific fullhash.
2322 static const char kWhateverMalware[] = "www.whatever.com/malware.html"; 2325 static const char kWhateverMalware[] = "www.whatever.com/malware.html";
2323 { 2326 {
2324 std::vector<scoped_ptr<SBChunkData>> chunks; 2327 std::vector<std::unique_ptr<SBChunkData>> chunks;
2325 chunks.push_back(AddChunkFullHashValue(2, kWhateverMalware)); 2328 chunks.push_back(AddChunkFullHashValue(2, kWhateverMalware));
2326 database_->InsertChunks(kMalwareList, chunks); 2329 database_->InsertChunks(kMalwareList, chunks);
2327 } 2330 }
2328 2331
2329 // Add a fullhash which has a prefix collision for a known url. 2332 // Add a fullhash which has a prefix collision for a known url.
2330 static const char kExampleFine[] = "www.example.com/fine.html"; 2333 static const char kExampleFine[] = "www.example.com/fine.html";
2331 static const char kExampleCollision[] = 2334 static const char kExampleCollision[] =
2332 "www.example.com/3123364814/malware.htm"; 2335 "www.example.com/3123364814/malware.htm";
2333 ASSERT_EQ(SBPrefixForString(kExampleFine), 2336 ASSERT_EQ(SBPrefixForString(kExampleFine),
2334 SBPrefixForString(kExampleCollision)); 2337 SBPrefixForString(kExampleCollision));
2335 { 2338 {
2336 std::vector<scoped_ptr<SBChunkData>> chunks; 2339 std::vector<std::unique_ptr<SBChunkData>> chunks;
2337 chunks.push_back(AddChunkFullHashValue(3, kExampleCollision)); 2340 chunks.push_back(AddChunkFullHashValue(3, kExampleCollision));
2338 database_->InsertChunks(kMalwareList, chunks); 2341 database_->InsertChunks(kMalwareList, chunks);
2339 } 2342 }
2340 2343
2341 database_->UpdateFinished(true); 2344 database_->UpdateFinished(true);
2342 2345
2343 std::vector<SBPrefix> prefix_hits; 2346 std::vector<SBPrefix> prefix_hits;
2344 std::vector<SBFullHashResult> cache_hits; 2347 std::vector<SBFullHashResult> cache_hits;
2345 2348
2346 // Anything will hit the host prefix. 2349 // Anything will hit the host prefix.
(...skipping 24 matching lines...) Expand all
2371 ASSERT_EQ(1U, prefix_hits.size()); 2374 ASSERT_EQ(1U, prefix_hits.size());
2372 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]); 2375 EXPECT_EQ(SBPrefixForString(kExampleCollision), prefix_hits[0]);
2373 EXPECT_TRUE(cache_hits.empty()); 2376 EXPECT_TRUE(cache_hits.empty());
2374 2377
2375 // This prefix collides, but no full hash match. 2378 // This prefix collides, but no full hash match.
2376 EXPECT_FALSE(database_->ContainsBrowseUrl( 2379 EXPECT_FALSE(database_->ContainsBrowseUrl(
2377 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits)); 2380 GURL(std::string("http://") + kExampleFine), &prefix_hits, &cache_hits));
2378 } 2381 }
2379 2382
2380 } // namespace safe_browsing 2383 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/safe_browsing_database.cc ('k') | chrome/browser/safe_browsing/safe_browsing_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698