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/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 | 157 |
158 // Test that the empty set doesn't appear to have anything in it. | 158 // Test that the empty set doesn't appear to have anything in it. |
159 TEST_F(PrefixSetTest, Empty) { | 159 TEST_F(PrefixSetTest, Empty) { |
160 const std::vector<SBPrefix> empty; | 160 const std::vector<SBPrefix> empty; |
161 safe_browsing::PrefixSet prefix_set(empty); | 161 safe_browsing::PrefixSet prefix_set(empty); |
162 for (size_t i = 0; i < shared_prefixes_.size(); ++i) { | 162 for (size_t i = 0; i < shared_prefixes_.size(); ++i) { |
163 EXPECT_FALSE(prefix_set.Exists(shared_prefixes_[i])); | 163 EXPECT_FALSE(prefix_set.Exists(shared_prefixes_[i])); |
164 } | 164 } |
165 } | 165 } |
166 | 166 |
| 167 // Single-element set should work fine. |
| 168 TEST_F(PrefixSetTest, OneElement) { |
| 169 const std::vector<SBPrefix> prefixes(100, 0); |
| 170 safe_browsing::PrefixSet prefix_set(prefixes); |
| 171 EXPECT_FALSE(prefix_set.Exists(-1)); |
| 172 EXPECT_TRUE(prefix_set.Exists(prefixes[0])); |
| 173 EXPECT_FALSE(prefix_set.Exists(1)); |
| 174 |
| 175 // Check that |GetPrefixes()| returns the same set of prefixes as |
| 176 // was passed in. |
| 177 std::vector<SBPrefix> prefixes_copy; |
| 178 prefix_set.GetPrefixes(&prefixes_copy); |
| 179 EXPECT_EQ(1U, prefixes_copy.size()); |
| 180 EXPECT_EQ(prefixes[0], prefixes_copy[0]); |
| 181 } |
| 182 |
| 183 // Edges of the 32-bit integer range. |
| 184 TEST_F(PrefixSetTest, IntMinMax) { |
| 185 std::vector<SBPrefix> prefixes; |
| 186 |
| 187 // Using bit patterns rather than portable constants because this |
| 188 // really is testing how the entire 32-bit integer range is handled. |
| 189 prefixes.push_back(0x00000000); |
| 190 prefixes.push_back(0x0000FFFF); |
| 191 prefixes.push_back(0x7FFF0000); |
| 192 prefixes.push_back(0x7FFFFFFF); |
| 193 prefixes.push_back(0x80000000); |
| 194 prefixes.push_back(0x8000FFFF); |
| 195 prefixes.push_back(0xFFFF0000); |
| 196 prefixes.push_back(0xFFFFFFFF); |
| 197 |
| 198 std::sort(prefixes.begin(), prefixes.end()); |
| 199 safe_browsing::PrefixSet prefix_set(prefixes); |
| 200 |
| 201 // Check that |GetPrefixes()| returns the same set of prefixes as |
| 202 // was passed in. |
| 203 std::vector<SBPrefix> prefixes_copy; |
| 204 prefix_set.GetPrefixes(&prefixes_copy); |
| 205 ASSERT_EQ(prefixes_copy.size(), prefixes.size()); |
| 206 EXPECT_TRUE(std::equal(prefixes.begin(), prefixes.end(), |
| 207 prefixes_copy.begin())); |
| 208 } |
| 209 |
| 210 // A range with only large deltas. |
| 211 TEST_F(PrefixSetTest, AllBig) { |
| 212 std::vector<SBPrefix> prefixes; |
| 213 |
| 214 const SBPrefix kVeryPositive = 1000 * 1000 * 1000; |
| 215 const SBPrefix kVeryNegative = -kVeryPositive; |
| 216 const unsigned kDelta = 10 * 1000 * 1000; |
| 217 |
| 218 for (SBPrefix prefix = kVeryNegative; |
| 219 prefix < kVeryPositive; prefix += kDelta) { |
| 220 prefixes.push_back(prefix); |
| 221 } |
| 222 |
| 223 std::sort(prefixes.begin(), prefixes.end()); |
| 224 safe_browsing::PrefixSet prefix_set(prefixes); |
| 225 |
| 226 // Check that |GetPrefixes()| returns the same set of prefixes as |
| 227 // was passed in. |
| 228 std::vector<SBPrefix> prefixes_copy; |
| 229 prefix_set.GetPrefixes(&prefixes_copy); |
| 230 prefixes.erase(std::unique(prefixes.begin(), prefixes.end()), prefixes.end()); |
| 231 EXPECT_EQ(prefixes_copy.size(), prefixes.size()); |
| 232 EXPECT_TRUE(std::equal(prefixes.begin(), prefixes.end(), |
| 233 prefixes_copy.begin())); |
| 234 } |
| 235 |
167 // Use artificial inputs to test various edge cases in Exists(). | 236 // Use artificial inputs to test various edge cases in Exists(). |
168 // Items before the lowest item aren't present. Items after the | 237 // Items before the lowest item aren't present. Items after the |
169 // largest item aren't present. Create a sequence of items with | 238 // largest item aren't present. Create a sequence of items with |
170 // deltas above and below 2^16, and make sure they're all present. | 239 // deltas above and below 2^16, and make sure they're all present. |
171 // Create a very long sequence with deltas below 2^16 to test crossing | 240 // Create a very long sequence with deltas below 2^16 to test crossing |
172 // |kMaxRun|. | 241 // |kMaxRun|. |
173 TEST_F(PrefixSetTest, EdgeCases) { | 242 TEST_F(PrefixSetTest, EdgeCases) { |
174 std::vector<SBPrefix> prefixes; | 243 std::vector<SBPrefix> prefixes; |
175 | 244 |
176 const SBPrefix kVeryPositive = 1000 * 1000 * 1000; | 245 const SBPrefix kVeryPositive = 1000 * 1000 * 1000; |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 file_util::ScopedFILE file(file_util::OpenFile(filename, "ab")); | 426 file_util::ScopedFILE file(file_util::OpenFile(filename, "ab")); |
358 const char buf[] = "im in ur base, killing ur d00dz."; | 427 const char buf[] = "im in ur base, killing ur d00dz."; |
359 ASSERT_EQ(strlen(buf), fwrite(buf, 1, strlen(buf), file.get())); | 428 ASSERT_EQ(strlen(buf), fwrite(buf, 1, strlen(buf), file.get())); |
360 file.reset(); | 429 file.reset(); |
361 scoped_ptr<safe_browsing::PrefixSet> | 430 scoped_ptr<safe_browsing::PrefixSet> |
362 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); | 431 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); |
363 ASSERT_FALSE(prefix_set.get()); | 432 ASSERT_FALSE(prefix_set.get()); |
364 } | 433 } |
365 | 434 |
366 } // namespace | 435 } // namespace |
OLD | NEW |