abarth@chromium.org | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
mark@chromium.org | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/rand_util.h" |
| 6 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
dilmah@chromium.org | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 10 | #include <algorithm> |
mark@chromium.org | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 11 | #include <limits> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 12 | #include <memory> |
mark@chromium.org | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 13 | |
dalecurtis@chromium.org | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 14 | #include "base/logging.h" |
dalecurtis@chromium.org | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 15 | #include "base/time/time.h" |
mark@chromium.org | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace { |
| 19 | |
| 20 | const int kIntMin = std::numeric_limits<int>::min(); |
| 21 | const int kIntMax = std::numeric_limits<int>::max(); |
| 22 | |
| 23 | } // namespace |
| 24 | |
Nico Weber | 0a3852a7 | 2015-10-29 20:42:58 | [diff] [blame] | 25 | TEST(RandUtilTest, RandInt) { |
mark@chromium.org | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 26 | EXPECT_EQ(base::RandInt(0, 0), 0); |
| 27 | EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin); |
| 28 | EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax); |
Nico Weber | 0a3852a7 | 2015-10-29 20:42:58 | [diff] [blame] | 29 | |
| 30 | // Check that the DCHECKS in RandInt() don't fire due to internal overflow. |
| 31 | // There was a 50% chance of that happening, so calling it 40 times means |
| 32 | // the chances of this passing by accident are tiny (9e-13). |
| 33 | for (int i = 0; i < 40; ++i) |
| 34 | base::RandInt(kIntMin, kIntMax); |
mark@chromium.org | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 35 | } |
mark@chromium.org | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 36 | |
| 37 | TEST(RandUtilTest, RandDouble) { |
abarth@chromium.org | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 38 | // Force 64-bit precision, making sure we're not in a 80-bit FPU register. |
| 39 | volatile double number = base::RandDouble(); |
| 40 | EXPECT_GT(1.0, number); |
| 41 | EXPECT_LE(0.0, number); |
| 42 | } |
| 43 | |
qsr@google.com | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 44 | TEST(RandUtilTest, RandBytes) { |
dilmah@chromium.org | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 45 | const size_t buffer_size = 50; |
qsr@google.com | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 46 | char buffer[buffer_size]; |
| 47 | memset(buffer, 0, buffer_size); |
| 48 | base::RandBytes(buffer, buffer_size); |
dilmah@chromium.org | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 49 | std::sort(buffer, buffer + buffer_size); |
| 50 | // Probability of occurrence of less than 25 unique bytes in 50 random bytes |
| 51 | // is below 10^-25. |
| 52 | EXPECT_GT(std::unique(buffer, buffer + buffer_size) - buffer, 25); |
qsr@google.com | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 53 | } |
| 54 | |
Sergey Ulanov | fdc62f8e | 2017-08-01 19:51:00 | [diff] [blame] | 55 | // Verify that calling base::RandBytes with an empty buffer doesn't fail. |
| 56 | TEST(RandUtilTest, RandBytes0) { |
| 57 | base::RandBytes(nullptr, 0); |
| 58 | } |
| 59 | |
abarth@chromium.org | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 60 | TEST(RandUtilTest, RandBytesAsString) { |
pkasting@chromium.org | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 61 | std::string random_string = base::RandBytesAsString(1); |
| 62 | EXPECT_EQ(1U, random_string.size()); |
abarth@chromium.org | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 63 | random_string = base::RandBytesAsString(145); |
| 64 | EXPECT_EQ(145U, random_string.size()); |
| 65 | char accumulator = 0; |
| 66 | for (size_t i = 0; i < random_string.size(); ++i) |
| 67 | accumulator |= random_string[i]; |
| 68 | // In theory this test can fail, but it won't before the universe dies of |
| 69 | // heat death. |
| 70 | EXPECT_NE(0, accumulator); |
mark@chromium.org | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 71 | } |
isherman@chromium.org | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 72 | |
| 73 | // Make sure that it is still appropriate to use RandGenerator in conjunction |
| 74 | // with std::random_shuffle(). |
| 75 | TEST(RandUtilTest, RandGeneratorForRandomShuffle) { |
| 76 | EXPECT_EQ(base::RandGenerator(1), 0U); |
| 77 | EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 78 | std::numeric_limits<int64_t>::max()); |
isherman@chromium.org | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 79 | } |
joi@chromium.org | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 80 | |
| 81 | TEST(RandUtilTest, RandGeneratorIsUniform) { |
| 82 | // Verify that RandGenerator has a uniform distribution. This is a |
| 83 | // regression test that consistently failed when RandGenerator was |
| 84 | // implemented this way: |
| 85 | // |
| 86 | // return base::RandUint64() % max; |
| 87 | // |
| 88 | // A degenerate case for such an implementation is e.g. a top of |
| 89 | // range that is 2/3rds of the way to MAX_UINT64, in which case the |
| 90 | // bottom half of the range would be twice as likely to occur as the |
| 91 | // top half. A bit of calculus care of jar@ shows that the largest |
| 92 | // measurable delta is when the top of the range is 3/4ths of the |
| 93 | // way, so that's what we use in the test. |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 94 | const uint64_t kTopOfRange = |
| 95 | (std::numeric_limits<uint64_t>::max() / 4ULL) * 3ULL; |
| 96 | const uint64_t kExpectedAverage = kTopOfRange / 2ULL; |
| 97 | const uint64_t kAllowedVariance = kExpectedAverage / 50ULL; // +/- 2% |
joi@chromium.org | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 98 | const int kMinAttempts = 1000; |
| 99 | const int kMaxAttempts = 1000000; |
| 100 | |
| 101 | double cumulative_average = 0.0; |
| 102 | int count = 0; |
| 103 | while (count < kMaxAttempts) { |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 104 | uint64_t value = base::RandGenerator(kTopOfRange); |
joi@chromium.org | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 105 | cumulative_average = (count * cumulative_average + value) / (count + 1); |
| 106 | |
| 107 | // Don't quit too quickly for things to start converging, or we may have |
| 108 | // a false positive. |
| 109 | if (count > kMinAttempts && |
| 110 | kExpectedAverage - kAllowedVariance < cumulative_average && |
| 111 | cumulative_average < kExpectedAverage + kAllowedVariance) { |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | ++count; |
| 116 | } |
| 117 | |
| 118 | ASSERT_LT(count, kMaxAttempts) << "Expected average was " << |
| 119 | kExpectedAverage << ", average ended at " << cumulative_average; |
| 120 | } |
| 121 | |
| 122 | TEST(RandUtilTest, RandUint64ProducesBothValuesOfAllBits) { |
| 123 | // This tests to see that our underlying random generator is good |
| 124 | // enough, for some value of good enough. |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 125 | uint64_t kAllZeros = 0ULL; |
| 126 | uint64_t kAllOnes = ~kAllZeros; |
| 127 | uint64_t found_ones = kAllZeros; |
| 128 | uint64_t found_zeros = kAllOnes; |
joi@chromium.org | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 129 | |
| 130 | for (size_t i = 0; i < 1000; ++i) { |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 131 | uint64_t value = base::RandUint64(); |
joi@chromium.org | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 132 | found_ones |= value; |
| 133 | found_zeros &= value; |
| 134 | |
| 135 | if (found_zeros == kAllZeros && found_ones == kAllOnes) |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | FAIL() << "Didn't achieve all bit values in maximum number of tries."; |
| 140 | } |
dalecurtis@chromium.org | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 141 | |
Scott Graham | 4ffd63b | 2017-06-01 18:03:33 | [diff] [blame] | 142 | TEST(RandUtilTest, RandBytesLonger) { |
| 143 | // Fuchsia can only retrieve 256 bytes of entropy at a time, so make sure we |
| 144 | // handle longer requests than that. |
| 145 | std::string random_string0 = base::RandBytesAsString(255); |
| 146 | EXPECT_EQ(255u, random_string0.size()); |
| 147 | std::string random_string1 = base::RandBytesAsString(1023); |
| 148 | EXPECT_EQ(1023u, random_string1.size()); |
| 149 | std::string random_string2 = base::RandBytesAsString(4097); |
| 150 | EXPECT_EQ(4097u, random_string2.size()); |
| 151 | } |
| 152 | |
dalecurtis@chromium.org | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 153 | // Benchmark test for RandBytes(). Disabled since it's intentionally slow and |
| 154 | // does not test anything that isn't already tested by the existing RandBytes() |
| 155 | // tests. |
| 156 | TEST(RandUtilTest, DISABLED_RandBytesPerf) { |
| 157 | // Benchmark the performance of |kTestIterations| of RandBytes() using a |
| 158 | // buffer size of |kTestBufferSize|. |
| 159 | const int kTestIterations = 10; |
| 160 | const size_t kTestBufferSize = 1 * 1024 * 1024; |
| 161 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 162 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[kTestBufferSize]); |
charliea | 3be83970 | 2015-01-26 17:35:41 | [diff] [blame] | 163 | const base::TimeTicks now = base::TimeTicks::Now(); |
dalecurtis@chromium.org | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 164 | for (int i = 0; i < kTestIterations; ++i) |
| 165 | base::RandBytes(buffer.get(), kTestBufferSize); |
charliea | 3be83970 | 2015-01-26 17:35:41 | [diff] [blame] | 166 | const base::TimeTicks end = base::TimeTicks::Now(); |
dalecurtis@chromium.org | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 167 | |
| 168 | LOG(INFO) << "RandBytes(" << kTestBufferSize << ") took: " |
| 169 | << (end - now).InMicroseconds() << "µs"; |
| 170 | } |