[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 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 | |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 10 | #include <algorithm> |
Benoit Lize | 73de21b | 2021-07-02 08:17:56 | [diff] [blame^] | 11 | #include <cmath> |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 12 | #include <limits> |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 13 | #include <memory> |
Benoit Lize | 73de21b | 2021-07-02 08:17:56 | [diff] [blame^] | 14 | #include <vector> |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 15 | |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 16 | #include "base/logging.h" |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 17 | #include "base/time/time.h" |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
Benoit Lize | 73de21b | 2021-07-02 08:17:56 | [diff] [blame^] | 20 | namespace base { |
| 21 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | const int kIntMin = std::numeric_limits<int>::min(); |
| 25 | const int kIntMax = std::numeric_limits<int>::max(); |
| 26 | |
| 27 | } // namespace |
| 28 | |
Nico Weber | 0a3852a7 | 2015-10-29 20:42:58 | [diff] [blame] | 29 | TEST(RandUtilTest, RandInt) { |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 30 | EXPECT_EQ(base::RandInt(0, 0), 0); |
| 31 | EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin); |
| 32 | EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax); |
Nico Weber | 0a3852a7 | 2015-10-29 20:42:58 | [diff] [blame] | 33 | |
| 34 | // Check that the DCHECKS in RandInt() don't fire due to internal overflow. |
| 35 | // There was a 50% chance of that happening, so calling it 40 times means |
| 36 | // the chances of this passing by accident are tiny (9e-13). |
| 37 | for (int i = 0; i < 40; ++i) |
| 38 | base::RandInt(kIntMin, kIntMax); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 39 | } |
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 40 | |
| 41 | TEST(RandUtilTest, RandDouble) { |
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 42 | // Force 64-bit precision, making sure we're not in a 80-bit FPU register. |
| 43 | volatile double number = base::RandDouble(); |
| 44 | EXPECT_GT(1.0, number); |
| 45 | EXPECT_LE(0.0, number); |
| 46 | } |
| 47 | |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 48 | TEST(RandUtilTest, RandBytes) { |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 49 | const size_t buffer_size = 50; |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 50 | char buffer[buffer_size]; |
| 51 | memset(buffer, 0, buffer_size); |
| 52 | base::RandBytes(buffer, buffer_size); |
[email protected] | 0173b96 | 2011-08-24 19:58:36 | [diff] [blame] | 53 | std::sort(buffer, buffer + buffer_size); |
| 54 | // Probability of occurrence of less than 25 unique bytes in 50 random bytes |
| 55 | // is below 10^-25. |
| 56 | EXPECT_GT(std::unique(buffer, buffer + buffer_size) - buffer, 25); |
[email protected] | 51a0181 | 2011-05-05 08:46:11 | [diff] [blame] | 57 | } |
| 58 | |
Sergey Ulanov | fdc62f8e | 2017-08-01 19:51:00 | [diff] [blame] | 59 | // Verify that calling base::RandBytes with an empty buffer doesn't fail. |
| 60 | TEST(RandUtilTest, RandBytes0) { |
| 61 | base::RandBytes(nullptr, 0); |
| 62 | } |
| 63 | |
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 64 | TEST(RandUtilTest, RandBytesAsString) { |
[email protected] | fdce478 | 2011-11-29 20:06:18 | [diff] [blame] | 65 | std::string random_string = base::RandBytesAsString(1); |
| 66 | EXPECT_EQ(1U, random_string.size()); |
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 67 | random_string = base::RandBytesAsString(145); |
| 68 | EXPECT_EQ(145U, random_string.size()); |
| 69 | char accumulator = 0; |
jdoerrie | 6c622935 | 2018-10-22 15:55:43 | [diff] [blame] | 70 | for (auto i : random_string) |
| 71 | accumulator |= i; |
[email protected] | 29548d8 | 2011-04-29 21:03:54 | [diff] [blame] | 72 | // In theory this test can fail, but it won't before the universe dies of |
| 73 | // heat death. |
| 74 | EXPECT_NE(0, accumulator); |
[email protected] | 94a0f31 | 2008-09-30 14:26:33 | [diff] [blame] | 75 | } |
[email protected] | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 76 | |
| 77 | // Make sure that it is still appropriate to use RandGenerator in conjunction |
| 78 | // with std::random_shuffle(). |
| 79 | TEST(RandUtilTest, RandGeneratorForRandomShuffle) { |
| 80 | EXPECT_EQ(base::RandGenerator(1), 0U); |
| 81 | EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(), |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 82 | std::numeric_limits<int64_t>::max()); |
[email protected] | a74dcae | 2010-08-30 21:07:05 | [diff] [blame] | 83 | } |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 84 | |
| 85 | TEST(RandUtilTest, RandGeneratorIsUniform) { |
| 86 | // Verify that RandGenerator has a uniform distribution. This is a |
| 87 | // regression test that consistently failed when RandGenerator was |
| 88 | // implemented this way: |
| 89 | // |
| 90 | // return base::RandUint64() % max; |
| 91 | // |
| 92 | // A degenerate case for such an implementation is e.g. a top of |
| 93 | // range that is 2/3rds of the way to MAX_UINT64, in which case the |
| 94 | // bottom half of the range would be twice as likely to occur as the |
| 95 | // top half. A bit of calculus care of jar@ shows that the largest |
| 96 | // measurable delta is when the top of the range is 3/4ths of the |
| 97 | // way, so that's what we use in the test. |
Peter Kasting | fbb9e56 | 2021-06-27 02:57:37 | [diff] [blame] | 98 | constexpr uint64_t kTopOfRange = |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 99 | (std::numeric_limits<uint64_t>::max() / 4ULL) * 3ULL; |
Peter Kasting | fbb9e56 | 2021-06-27 02:57:37 | [diff] [blame] | 100 | constexpr double kExpectedAverage = static_cast<double>(kTopOfRange / 2); |
| 101 | constexpr double kAllowedVariance = kExpectedAverage / 50.0; // +/- 2% |
| 102 | constexpr int kMinAttempts = 1000; |
| 103 | constexpr int kMaxAttempts = 1000000; |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 104 | |
| 105 | double cumulative_average = 0.0; |
| 106 | int count = 0; |
| 107 | while (count < kMaxAttempts) { |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 108 | uint64_t value = base::RandGenerator(kTopOfRange); |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 109 | cumulative_average = (count * cumulative_average + value) / (count + 1); |
| 110 | |
| 111 | // Don't quit too quickly for things to start converging, or we may have |
| 112 | // a false positive. |
| 113 | if (count > kMinAttempts && |
| 114 | kExpectedAverage - kAllowedVariance < cumulative_average && |
| 115 | cumulative_average < kExpectedAverage + kAllowedVariance) { |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | ++count; |
| 120 | } |
| 121 | |
Benoit Lize | 73de21b | 2021-07-02 08:17:56 | [diff] [blame^] | 122 | ASSERT_LT(count, kMaxAttempts) << "Expected average was " << kExpectedAverage |
| 123 | << ", average ended at " << cumulative_average; |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | TEST(RandUtilTest, RandUint64ProducesBothValuesOfAllBits) { |
| 127 | // This tests to see that our underlying random generator is good |
| 128 | // enough, for some value of good enough. |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 129 | uint64_t kAllZeros = 0ULL; |
| 130 | uint64_t kAllOnes = ~kAllZeros; |
| 131 | uint64_t found_ones = kAllZeros; |
| 132 | uint64_t found_zeros = kAllOnes; |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 133 | |
| 134 | for (size_t i = 0; i < 1000; ++i) { |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 135 | uint64_t value = base::RandUint64(); |
[email protected] | af2e192b | 2011-05-30 17:39:09 | [diff] [blame] | 136 | found_ones |= value; |
| 137 | found_zeros &= value; |
| 138 | |
| 139 | if (found_zeros == kAllZeros && found_ones == kAllOnes) |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | FAIL() << "Didn't achieve all bit values in maximum number of tries."; |
| 144 | } |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 145 | |
Scott Graham | 4ffd63b5 | 2017-06-01 18:03:33 | [diff] [blame] | 146 | TEST(RandUtilTest, RandBytesLonger) { |
| 147 | // Fuchsia can only retrieve 256 bytes of entropy at a time, so make sure we |
| 148 | // handle longer requests than that. |
| 149 | std::string random_string0 = base::RandBytesAsString(255); |
| 150 | EXPECT_EQ(255u, random_string0.size()); |
| 151 | std::string random_string1 = base::RandBytesAsString(1023); |
| 152 | EXPECT_EQ(1023u, random_string1.size()); |
| 153 | std::string random_string2 = base::RandBytesAsString(4097); |
| 154 | EXPECT_EQ(4097u, random_string2.size()); |
| 155 | } |
| 156 | |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 157 | // Benchmark test for RandBytes(). Disabled since it's intentionally slow and |
| 158 | // does not test anything that isn't already tested by the existing RandBytes() |
| 159 | // tests. |
| 160 | TEST(RandUtilTest, DISABLED_RandBytesPerf) { |
| 161 | // Benchmark the performance of |kTestIterations| of RandBytes() using a |
| 162 | // buffer size of |kTestBufferSize|. |
| 163 | const int kTestIterations = 10; |
| 164 | const size_t kTestBufferSize = 1 * 1024 * 1024; |
| 165 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 166 | std::unique_ptr<uint8_t[]> buffer(new uint8_t[kTestBufferSize]); |
charliea | 3be83970 | 2015-01-26 17:35:41 | [diff] [blame] | 167 | const base::TimeTicks now = base::TimeTicks::Now(); |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 168 | for (int i = 0; i < kTestIterations; ++i) |
| 169 | base::RandBytes(buffer.get(), kTestBufferSize); |
charliea | 3be83970 | 2015-01-26 17:35:41 | [diff] [blame] | 170 | const base::TimeTicks end = base::TimeTicks::Now(); |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 171 | |
Benoit Lize | 73de21b | 2021-07-02 08:17:56 | [diff] [blame^] | 172 | LOG(INFO) << "RandBytes(" << kTestBufferSize |
| 173 | << ") took: " << (end - now).InMicroseconds() << "µs"; |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 174 | } |
Benoit Lize | 73de21b | 2021-07-02 08:17:56 | [diff] [blame^] | 175 | |
| 176 | TEST(RandUtilTest, InsecureRandomGeneratorProducesBothValuesOfAllBits) { |
| 177 | // This tests to see that our underlying random generator is good |
| 178 | // enough, for some value of good enough. |
| 179 | uint64_t kAllZeros = 0ULL; |
| 180 | uint64_t kAllOnes = ~kAllZeros; |
| 181 | uint64_t found_ones = kAllZeros; |
| 182 | uint64_t found_zeros = kAllOnes; |
| 183 | |
| 184 | InsecureRandomGenerator generator; |
| 185 | generator.Seed(); |
| 186 | |
| 187 | for (size_t i = 0; i < 1000; ++i) { |
| 188 | uint64_t value = generator.RandUint64(); |
| 189 | found_ones |= value; |
| 190 | found_zeros &= value; |
| 191 | |
| 192 | if (found_zeros == kAllZeros && found_ones == kAllOnes) |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | FAIL() << "Didn't achieve all bit values in maximum number of tries."; |
| 197 | } |
| 198 | |
| 199 | namespace { |
| 200 | |
| 201 | constexpr double kXp1Percent = -2.33; |
| 202 | constexpr double kXp99Percent = 2.33; |
| 203 | |
| 204 | double ChiSquaredCriticalValue(double nu, double x_p) { |
| 205 | // From "The Art Of Computer Programming" (TAOCP), Volume 2, Section 3.3.1, |
| 206 | // Table 1. This is the asymptotic value for nu > 30, up to O(1 / sqrt(nu)). |
| 207 | return nu + sqrt(2. * nu) * x_p + 2. / 3. * (x_p * x_p) - 2. / 3.; |
| 208 | } |
| 209 | |
| 210 | int ExtractBits(uint64_t value, int from_bit, int num_bits) { |
| 211 | return (value >> from_bit) & ((1 << num_bits) - 1); |
| 212 | } |
| 213 | |
| 214 | // Performs a Chi-Squared test on a subset of |num_bits| extracted starting from |
| 215 | // |from_bit| in the generated value. |
| 216 | // |
| 217 | // See TAOCP, Volume 2, Section 3.3.1, and |
| 218 | // https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test for details. |
| 219 | // |
| 220 | // This is only one of the many, many random number generator test we could do, |
| 221 | // but they are cumbersome, as they are typically very slow, and expected to |
| 222 | // fail from time to time, due to their probabilistic nature. |
| 223 | // |
| 224 | // The generator we use has however been vetted with the BigCrush test suite |
| 225 | // from Marsaglia, so this should suffice as a smoke test that our |
| 226 | // implementation is wrong. |
| 227 | bool ChiSquaredTest(InsecureRandomGenerator& gen, |
| 228 | size_t n, |
| 229 | int from_bit, |
| 230 | int num_bits) { |
| 231 | const int range = 1 << num_bits; |
| 232 | CHECK_EQ(static_cast<int>(n % range), 0) << "Makes computations simpler"; |
| 233 | std::vector<size_t> samples(range, 0); |
| 234 | |
| 235 | // Count how many samples pf each value are found. All buckets should be |
| 236 | // almost equal if the generator is suitably uniformly random. |
| 237 | for (size_t i = 0; i < n; i++) { |
| 238 | int sample = ExtractBits(gen.RandUint64(), from_bit, num_bits); |
| 239 | samples[sample] += 1; |
| 240 | } |
| 241 | |
| 242 | // Compute the Chi-Squared statistic, which is: |
| 243 | // \Sum_{k=0}^{range-1} \frac{(count - expected)^2}{expected} |
| 244 | double chi_squared = 0.; |
| 245 | double expected_count = n / range; |
| 246 | for (size_t sample_count : samples) { |
| 247 | double deviation = sample_count - expected_count; |
| 248 | chi_squared += (deviation * deviation) / expected_count; |
| 249 | } |
| 250 | |
| 251 | // The generator should produce numbers that are not too far of (chi_squared |
| 252 | // lower than a given quantile), but not too close to the ideal distribution |
| 253 | // either (chi_squared is too low). |
| 254 | // |
| 255 | // See The Art Of Computer Programming, Volume 2, Section 3.3.1 for details. |
| 256 | return chi_squared > ChiSquaredCriticalValue(range - 1, kXp1Percent) && |
| 257 | chi_squared < ChiSquaredCriticalValue(range - 1, kXp99Percent); |
| 258 | } |
| 259 | |
| 260 | } // namespace |
| 261 | |
| 262 | TEST(RandUtilTest, InsecureRandomGeneratorChiSquared) { |
| 263 | constexpr int kIterations = 50; |
| 264 | |
| 265 | // Specifically test the low bits, which are usually weaker in random number |
| 266 | // generators. We don't use them for the 32 bit number generation, but let's |
| 267 | // make sure they are still suitable. |
| 268 | for (int start_bit : {1, 2, 3, 8, 12, 20, 32, 48, 54}) { |
| 269 | int pass_count = 0; |
| 270 | for (int i = 0; i < kIterations; i++) { |
| 271 | size_t samples = 1 << 16; |
| 272 | InsecureRandomGenerator gen; |
| 273 | // Fix the seed to make the test non-flaky. |
| 274 | gen.SeedForTesting(kIterations + 1); |
| 275 | bool pass = ChiSquaredTest(gen, samples, start_bit, 8); |
| 276 | pass_count += pass; |
| 277 | } |
| 278 | |
| 279 | // We exclude 1% on each side, so we expect 98% of tests to pass, meaning 98 |
| 280 | // * kIterations / 100. However this is asymptotic, so add a bit of leeway. |
| 281 | int expected_pass_count = (kIterations * 98) / 100; |
| 282 | EXPECT_GE(pass_count, expected_pass_count - ((kIterations * 2) / 100)) |
| 283 | << "For start_bit = " << start_bit; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | } // namespace base |