OLD | NEW |
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 11 matching lines...) Expand all Loading... |
22 protected: | 22 protected: |
23 // Constants for the v1 format. | 23 // Constants for the v1 format. |
24 static const size_t kMagicOffset = 0 * sizeof(uint32); | 24 static const size_t kMagicOffset = 0 * sizeof(uint32); |
25 static const size_t kVersionOffset = 1 * sizeof(uint32); | 25 static const size_t kVersionOffset = 1 * sizeof(uint32); |
26 static const size_t kIndexSizeOffset = 2 * sizeof(uint32); | 26 static const size_t kIndexSizeOffset = 2 * sizeof(uint32); |
27 static const size_t kDeltasSizeOffset = 3 * sizeof(uint32); | 27 static const size_t kDeltasSizeOffset = 3 * sizeof(uint32); |
28 static const size_t kPayloadOffset = 4 * sizeof(uint32); | 28 static const size_t kPayloadOffset = 4 * sizeof(uint32); |
29 | 29 |
30 // Generate a set of random prefixes to share between tests. For | 30 // Generate a set of random prefixes to share between tests. For |
31 // most tests this generation was a large fraction of the test time. | 31 // most tests this generation was a large fraction of the test time. |
| 32 // |
| 33 // The set should contain sparse areas where adjacent items are more |
| 34 // than 2^16 apart, and dense areas where adjacent items are less |
| 35 // than 2^16 apart. |
32 static void SetUpTestCase() { | 36 static void SetUpTestCase() { |
33 for (size_t i = 0; i < 50000; ++i) { | 37 // Distribute clusters of prefixes. |
| 38 for (size_t i = 0; i < 250; ++i) { |
| 39 // Unsigned for overflow characteristics. |
| 40 const uint32 base = static_cast<uint32>(base::RandUint64()); |
| 41 for (size_t j = 0; j < 10; ++j) { |
| 42 const uint32 delta = static_cast<uint32>(base::RandUint64() & 0xFFFF); |
| 43 const SBPrefix prefix = static_cast<SBPrefix>(base + delta); |
| 44 shared_prefixes_.push_back(prefix); |
| 45 } |
| 46 } |
| 47 |
| 48 // Lay down a sparsely-distributed layer. |
| 49 const size_t count = shared_prefixes_.size(); |
| 50 for (size_t i = 0; i < count; ++i) { |
34 const SBPrefix prefix = static_cast<SBPrefix>(base::RandUint64()); | 51 const SBPrefix prefix = static_cast<SBPrefix>(base::RandUint64()); |
35 shared_prefixes_.push_back(prefix); | 52 shared_prefixes_.push_back(prefix); |
36 } | 53 } |
37 | 54 |
38 // Sort for use with PrefixSet constructor. | 55 // Sort for use with PrefixSet constructor. |
39 std::sort(shared_prefixes_.begin(), shared_prefixes_.end()); | 56 std::sort(shared_prefixes_.begin(), shared_prefixes_.end()); |
40 } | 57 } |
41 | 58 |
42 // Check that all elements of |prefixes| are in |prefix_set|, and | 59 // 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 | 60 // that nearby elements are not (for lack of a more sensible set of |
(...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 file_util::ScopedFILE file(file_util::OpenFile(filename, "ab")); | 476 file_util::ScopedFILE file(file_util::OpenFile(filename, "ab")); |
460 const char buf[] = "im in ur base, killing ur d00dz."; | 477 const char buf[] = "im in ur base, killing ur d00dz."; |
461 ASSERT_EQ(strlen(buf), fwrite(buf, 1, strlen(buf), file.get())); | 478 ASSERT_EQ(strlen(buf), fwrite(buf, 1, strlen(buf), file.get())); |
462 file.reset(); | 479 file.reset(); |
463 scoped_ptr<safe_browsing::PrefixSet> | 480 scoped_ptr<safe_browsing::PrefixSet> |
464 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); | 481 prefix_set(safe_browsing::PrefixSet::LoadFile(filename)); |
465 ASSERT_FALSE(prefix_set.get()); | 482 ASSERT_FALSE(prefix_set.get()); |
466 } | 483 } |
467 | 484 |
468 } // namespace | 485 } // namespace |
OLD | NEW |