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

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

Issue 10896048: Transition safe browsing from bloom filter to prefix set. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix prefix set read/write for empty/sparse sets. Created 8 years, 3 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 #include "chrome/browser/safe_browsing/prefix_set.h" 5 #include "chrome/browser/safe_browsing/prefix_set.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 9
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 24 matching lines...) Expand all
35 shared_prefixes_.push_back(prefix); 35 shared_prefixes_.push_back(prefix);
36 } 36 }
37 37
38 // Sort for use with PrefixSet constructor. 38 // Sort for use with PrefixSet constructor.
39 std::sort(shared_prefixes_.begin(), shared_prefixes_.end()); 39 std::sort(shared_prefixes_.begin(), shared_prefixes_.end());
40 } 40 }
41 41
42 // Check that all elements of |prefixes| are in |prefix_set|, and 42 // Check that all elements of |prefixes| are in |prefix_set|, and
43 // that nearby elements are not (for lack of a more sensible set of 43 // that nearby elements are not (for lack of a more sensible set of
44 // items to check for absence). 44 // items to check for absence).
45 static void CheckPrefixes(safe_browsing::PrefixSet* prefix_set, 45 static void CheckPrefixes(const safe_browsing::PrefixSet& prefix_set,
46 const std::vector<SBPrefix> &prefixes) { 46 const std::vector<SBPrefix> &prefixes) {
47 // The set can generate the prefixes it believes it has, so that's 47 // The set can generate the prefixes it believes it has, so that's
48 // a good starting point. 48 // a good starting point.
49 std::set<SBPrefix> check(prefixes.begin(), prefixes.end()); 49 std::set<SBPrefix> check(prefixes.begin(), prefixes.end());
50 std::vector<SBPrefix> prefixes_copy; 50 std::vector<SBPrefix> prefixes_copy;
51 prefix_set->GetPrefixes(&prefixes_copy); 51 prefix_set.GetPrefixes(&prefixes_copy);
52 EXPECT_EQ(prefixes_copy.size(), check.size()); 52 EXPECT_EQ(prefixes_copy.size(), check.size());
53 EXPECT_TRUE(std::equal(check.begin(), check.end(), prefixes_copy.begin())); 53 EXPECT_TRUE(std::equal(check.begin(), check.end(), prefixes_copy.begin()));
54 54
55 for (size_t i = 0; i < prefixes.size(); ++i) { 55 for (size_t i = 0; i < prefixes.size(); ++i) {
56 EXPECT_TRUE(prefix_set->Exists(prefixes[i])); 56 EXPECT_TRUE(prefix_set.Exists(prefixes[i]));
57 57
58 const SBPrefix left_sibling = prefixes[i] - 1; 58 const SBPrefix left_sibling = prefixes[i] - 1;
59 if (check.count(left_sibling) == 0) 59 if (check.count(left_sibling) == 0)
60 EXPECT_FALSE(prefix_set->Exists(left_sibling)); 60 EXPECT_FALSE(prefix_set.Exists(left_sibling));
61 61
62 const SBPrefix right_sibling = prefixes[i] + 1; 62 const SBPrefix right_sibling = prefixes[i] + 1;
63 if (check.count(right_sibling) == 0) 63 if (check.count(right_sibling) == 0)
64 EXPECT_FALSE(prefix_set->Exists(right_sibling)); 64 EXPECT_FALSE(prefix_set.Exists(right_sibling));
65 } 65 }
66 } 66 }
67 67
68 // Generate a |PrefixSet| file from |shared_prefixes_|, store it in 68 // Generate a |PrefixSet| file from |shared_prefixes_|, store it in
69 // a temporary file, and return the filename in |filenamep|. 69 // a temporary file, and return the filename in |filenamep|.
70 // Returns |true| on success. 70 // Returns |true| on success.
71 bool GetPrefixSetFile(FilePath* filenamep) { 71 bool GetPrefixSetFile(FilePath* filenamep) {
72 if (!temp_dir_.IsValid() && !temp_dir_.CreateUniqueTempDir()) 72 if (!temp_dir_.IsValid() && !temp_dir_.CreateUniqueTempDir())
73 return false; 73 return false;
74 74
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 static std::vector<SBPrefix> shared_prefixes_; 147 static std::vector<SBPrefix> shared_prefixes_;
148 148
149 ScopedTempDir temp_dir_; 149 ScopedTempDir temp_dir_;
150 }; 150 };
151 151
152 std::vector<SBPrefix> PrefixSetTest::shared_prefixes_; 152 std::vector<SBPrefix> PrefixSetTest::shared_prefixes_;
153 153
154 // Test that a small sparse random input works. 154 // Test that a small sparse random input works.
155 TEST_F(PrefixSetTest, Baseline) { 155 TEST_F(PrefixSetTest, Baseline) {
156 safe_browsing::PrefixSet prefix_set(shared_prefixes_); 156 safe_browsing::PrefixSet prefix_set(shared_prefixes_);
157 CheckPrefixes(&prefix_set, shared_prefixes_); 157 CheckPrefixes(prefix_set, shared_prefixes_);
158 } 158 }
159 159
160 // Test that the empty set doesn't appear to have anything in it. 160 // Test that the empty set doesn't appear to have anything in it.
161 TEST_F(PrefixSetTest, Empty) { 161 TEST_F(PrefixSetTest, Empty) {
162 const std::vector<SBPrefix> empty; 162 const std::vector<SBPrefix> empty;
163 safe_browsing::PrefixSet prefix_set(empty); 163 safe_browsing::PrefixSet prefix_set(empty);
164 for (size_t i = 0; i < shared_prefixes_.size(); ++i) { 164 for (size_t i = 0; i < shared_prefixes_.size(); ++i) {
165 EXPECT_FALSE(prefix_set.Exists(shared_prefixes_[i])); 165 EXPECT_FALSE(prefix_set.Exists(shared_prefixes_[i]));
166 } 166 }
167 } 167 }
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 // check items just above and below the inputs to make sure they 299 // check items just above and below the inputs to make sure they
300 // aren't present. 300 // aren't present.
301 for (size_t i = 0; i < prefixes.size(); ++i) { 301 for (size_t i = 0; i < prefixes.size(); ++i) {
302 EXPECT_TRUE(prefix_set.Exists(prefixes[i])); 302 EXPECT_TRUE(prefix_set.Exists(prefixes[i]));
303 303
304 EXPECT_FALSE(prefix_set.Exists(prefixes[i] - 1)); 304 EXPECT_FALSE(prefix_set.Exists(prefixes[i] - 1));
305 EXPECT_FALSE(prefix_set.Exists(prefixes[i] + 1)); 305 EXPECT_FALSE(prefix_set.Exists(prefixes[i] + 1));
306 } 306 }
307 } 307 }
308 308
309 // Similar to Baseline test, but write the set out to a file and read 309 // Test writing a prefix set to disk and reading it back in.
310 // it back in before testing.
311 TEST_F(PrefixSetTest, ReadWrite) { 310 TEST_F(PrefixSetTest, ReadWrite) {
312 FilePath filename; 311 FilePath filename;
313 ASSERT_TRUE(GetPrefixSetFile(&filename));
314 312
315 scoped_ptr<safe_browsing::PrefixSet> 313 // Write the sample prefix set out, read it back in, and check all
316 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); 314 // the prefixes. Leaves the path in |filename|.
317 ASSERT_TRUE(prefix_set.get()); 315 {
316 ASSERT_TRUE(GetPrefixSetFile(&filename));
317 scoped_ptr<safe_browsing::PrefixSet>
318 prefix_set(safe_browsing::PrefixSet::LoadFile(filename));
319 ASSERT_TRUE(prefix_set.get());
320 CheckPrefixes(*prefix_set, shared_prefixes_);
321 }
318 322
319 CheckPrefixes(prefix_set.get(), shared_prefixes_); 323 // Test writing and reading a very sparse set containing no deltas.
324 {
325 const SBPrefix kVeryPositive = 1000 * 1000 * 1000;
326 const SBPrefix kVeryNegative = -kVeryPositive;
327
328 std::vector<SBPrefix> prefixes;
329 prefixes.push_back(kVeryNegative);
330 prefixes.push_back(kVeryPositive);
331
332 safe_browsing::PrefixSet prefix_set_to_write(prefixes);
333 ASSERT_TRUE(prefix_set_to_write.WriteFile(filename));
334 scoped_ptr<safe_browsing::PrefixSet>
335 prefix_set(safe_browsing::PrefixSet::LoadFile(filename));
336 ASSERT_TRUE(prefix_set.get());
337 CheckPrefixes(*prefix_set, prefixes);
338 }
339
340 // Test writing and reading an empty set.
341 {
342 std::vector<SBPrefix> prefixes;
343 safe_browsing::PrefixSet prefix_set_to_write(prefixes);
344 ASSERT_TRUE(prefix_set_to_write.WriteFile(filename));
345 scoped_ptr<safe_browsing::PrefixSet>
346 prefix_set(safe_browsing::PrefixSet::LoadFile(filename));
347 ASSERT_TRUE(prefix_set.get());
348 CheckPrefixes(*prefix_set, prefixes);
349 }
320 } 350 }
321 351
322 // Check that |CleanChecksum()| makes an acceptable checksum. 352 // Check that |CleanChecksum()| makes an acceptable checksum.
323 TEST_F(PrefixSetTest, CorruptionHelpers) { 353 TEST_F(PrefixSetTest, CorruptionHelpers) {
324 FilePath filename; 354 FilePath filename;
325 ASSERT_TRUE(GetPrefixSetFile(&filename)); 355 ASSERT_TRUE(GetPrefixSetFile(&filename));
326 356
327 // This will modify data in |index_|, which will fail the digest check. 357 // This will modify data in |index_|, which will fail the digest check.
328 file_util::ScopedFILE file(file_util::OpenFile(filename, "r+b")); 358 file_util::ScopedFILE file(file_util::OpenFile(filename, "r+b"));
329 IncrementIntAt(file.get(), kPayloadOffset, 1); 359 IncrementIntAt(file.get(), kPayloadOffset, 1);
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 file_util::ScopedFILE file(file_util::OpenFile(filename, "ab")); 458 file_util::ScopedFILE file(file_util::OpenFile(filename, "ab"));
429 const char buf[] = "im in ur base, killing ur d00dz."; 459 const char buf[] = "im in ur base, killing ur d00dz.";
430 ASSERT_EQ(strlen(buf), fwrite(buf, 1, strlen(buf), file.get())); 460 ASSERT_EQ(strlen(buf), fwrite(buf, 1, strlen(buf), file.get()));
431 file.reset(); 461 file.reset();
432 scoped_ptr<safe_browsing::PrefixSet> 462 scoped_ptr<safe_browsing::PrefixSet>
433 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); 463 prefix_set(safe_browsing::PrefixSet::LoadFile(filename));
434 ASSERT_FALSE(prefix_set.get()); 464 ASSERT_FALSE(prefix_set.get());
435 } 465 }
436 466
437 } // namespace 467 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/prefix_set.cc ('k') | chrome/browser/safe_browsing/safe_browsing_database.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698