OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 SBPrefix GenPrefix() { | 15 SBPrefix GenPrefix() { |
16 return static_cast<SBPrefix>(base::RandUint64()); | 16 return static_cast<SBPrefix>(base::RandUint64()); |
17 } | 17 } |
18 | 18 |
19 // Test that a small sparse random input works. | 19 // Test that a small sparse random input works. |
20 TEST(PrefixSetTest, Baseline) { | 20 TEST(PrefixSetTest, Baseline) { |
21 std::vector<SBPrefix> prefixes; | 21 std::vector<SBPrefix> prefixes; |
22 | 22 |
23 static const size_t kCount = 50000; | 23 static const size_t kCount = 50000; |
24 | 24 |
25 for (size_t i = 0; i < kCount; ++i) { | 25 for (size_t i = 0; i < kCount; ++i) { |
26 prefixes.push_back(GenPrefix()); | 26 prefixes.push_back(GenPrefix()); |
27 } | 27 } |
28 | 28 |
| 29 std::sort(prefixes.begin(), prefixes.end()); |
29 safe_browsing::PrefixSet prefix_set(prefixes); | 30 safe_browsing::PrefixSet prefix_set(prefixes); |
30 | 31 |
| 32 // Check that |GetPrefixes()| returns exactly the same set of |
| 33 // prefixes as was passed in. |
| 34 std::set<SBPrefix> check(prefixes.begin(), prefixes.end()); |
| 35 std::vector<SBPrefix> prefixes_copy; |
| 36 prefix_set.GetPrefixes(&prefixes_copy); |
| 37 EXPECT_EQ(prefixes_copy.size(), check.size()); |
| 38 EXPECT_TRUE(std::equal(check.begin(), check.end(), prefixes_copy.begin())); |
| 39 |
31 // Check that the set flags all of the inputs, and also check items | 40 // Check that the set flags all of the inputs, and also check items |
32 // just above and below the inputs to make sure they aren't there. | 41 // just above and below the inputs to make sure they aren't there. |
33 std::set<SBPrefix> check(prefixes.begin(), prefixes.end()); | |
34 for (size_t i = 0; i < prefixes.size(); ++i) { | 42 for (size_t i = 0; i < prefixes.size(); ++i) { |
35 EXPECT_TRUE(prefix_set.Exists(prefixes[i])); | 43 EXPECT_TRUE(prefix_set.Exists(prefixes[i])); |
36 | 44 |
37 const SBPrefix left_sibling = prefixes[i] - 1; | 45 const SBPrefix left_sibling = prefixes[i] - 1; |
38 if (check.count(left_sibling) == 0) | 46 if (check.count(left_sibling) == 0) |
39 EXPECT_FALSE(prefix_set.Exists(left_sibling)); | 47 EXPECT_FALSE(prefix_set.Exists(left_sibling)); |
40 | 48 |
41 const SBPrefix right_sibling = prefixes[i] + 1; | 49 const SBPrefix right_sibling = prefixes[i] + 1; |
42 if (check.count(right_sibling) == 0) | 50 if (check.count(right_sibling) == 0) |
43 EXPECT_FALSE(prefix_set.Exists(right_sibling)); | 51 EXPECT_FALSE(prefix_set.Exists(right_sibling)); |
44 } | 52 } |
45 } | 53 } |
46 | 54 |
47 // Test that the empty set doesn't appear to have anything in it. | 55 // Test that the empty set doesn't appear to have anything in it. |
48 TEST(PrefixSetTest, xEmpty) { | 56 TEST(PrefixSetTest, Empty) { |
49 std::vector<SBPrefix> prefixes; | 57 std::vector<SBPrefix> prefixes; |
50 safe_browsing::PrefixSet prefix_set(prefixes); | 58 safe_browsing::PrefixSet prefix_set(prefixes); |
51 for (size_t i = 0; i < 500; ++i) { | 59 for (size_t i = 0; i < 500; ++i) { |
52 EXPECT_FALSE(prefix_set.Exists(GenPrefix())); | 60 EXPECT_FALSE(prefix_set.Exists(GenPrefix())); |
53 } | 61 } |
54 } | 62 } |
55 | 63 |
56 // Use artificial inputs to test various edge cases in Exists(). | 64 // Use artificial inputs to test various edge cases in Exists(). |
57 // Items before the lowest item aren't present. Items after the | 65 // Items before the lowest item aren't present. Items after the |
58 // largest item aren't present. Create a sequence of items with | 66 // largest item aren't present. Create a sequence of items with |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // so a new index item will be injected. | 98 // so a new index item will be injected. |
91 delta = 256 * 256 - 1; | 99 delta = 256 * 256 - 1; |
92 prefix = kVeryPositive - delta * 1000; | 100 prefix = kVeryPositive - delta * 1000; |
93 prefixes.push_back(prefix); | 101 prefixes.push_back(prefix); |
94 for (int i = 0; i < 1000; ++i) { | 102 for (int i = 0; i < 1000; ++i) { |
95 prefix += delta; | 103 prefix += delta; |
96 prefixes.push_back(prefix); | 104 prefixes.push_back(prefix); |
97 delta--; | 105 delta--; |
98 } | 106 } |
99 | 107 |
100 // Shuffle things up for giggles. | 108 std::sort(prefixes.begin(), prefixes.end()); |
101 std::random_shuffle(prefixes.begin(), prefixes.end()); | 109 safe_browsing::PrefixSet prefix_set(prefixes); |
102 | 110 |
103 safe_browsing::PrefixSet prefix_set(prefixes); | 111 // Check that |GetPrefixes()| returns the same set of prefixes as |
| 112 // was passed in. |
| 113 std::vector<SBPrefix> prefixes_copy; |
| 114 prefix_set.GetPrefixes(&prefixes_copy); |
| 115 prefixes.erase(std::unique(prefixes.begin(), prefixes.end()), prefixes.end()); |
| 116 EXPECT_EQ(prefixes_copy.size(), prefixes.size()); |
| 117 EXPECT_TRUE(std::equal(prefixes.begin(), prefixes.end(), |
| 118 prefixes_copy.begin())); |
104 | 119 |
105 // Items before and after the set are not present, and don't crash. | 120 // Items before and after the set are not present, and don't crash. |
106 EXPECT_FALSE(prefix_set.Exists(kVeryNegative - 100)); | 121 EXPECT_FALSE(prefix_set.Exists(kVeryNegative - 100)); |
107 EXPECT_FALSE(prefix_set.Exists(kVeryPositive + 100)); | 122 EXPECT_FALSE(prefix_set.Exists(kVeryPositive + 100)); |
108 | 123 |
109 // Check that the set correctly flags all of the inputs, and also | 124 // Check that the set correctly flags all of the inputs, and also |
110 // check items just above and below the inputs to make sure they | 125 // check items just above and below the inputs to make sure they |
111 // aren't present. | 126 // aren't present. |
112 for (size_t i = 0; i < prefixes.size(); ++i) { | 127 for (size_t i = 0; i < prefixes.size(); ++i) { |
113 EXPECT_TRUE(prefix_set.Exists(prefixes[i])); | 128 EXPECT_TRUE(prefix_set.Exists(prefixes[i])); |
114 | 129 |
115 EXPECT_FALSE(prefix_set.Exists(prefixes[i] - 1)); | 130 EXPECT_FALSE(prefix_set.Exists(prefixes[i] - 1)); |
116 EXPECT_FALSE(prefix_set.Exists(prefixes[i] + 1)); | 131 EXPECT_FALSE(prefix_set.Exists(prefixes[i] + 1)); |
117 } | 132 } |
118 } | 133 } |
119 | 134 |
120 } // namespace | 135 } // namespace |
OLD | NEW |