blob: f6ea8c876bb36665e090d940717d49859566c7e9 [file] [log] [blame]
[email protected]29548d82011-04-29 21:03:541// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]05f9b682008-09-29 22:18:012// 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
avi9b6f42932015-12-26 22:15:147#include <stddef.h>
8#include <stdint.h>
9
[email protected]0173b962011-08-24 19:58:3610#include <algorithm>
Benoit Lize73de21b2021-07-02 08:17:5611#include <cmath>
[email protected]05f9b682008-09-29 22:18:0112#include <limits>
dcheng093de9b2016-04-04 21:25:5113#include <memory>
Benoit Lize73de21b2021-07-02 08:17:5614#include <vector>
[email protected]05f9b682008-09-29 22:18:0115
[email protected]c910c5a2014-01-23 02:14:2816#include "base/logging.h"
[email protected]c910c5a2014-01-23 02:14:2817#include "base/time/time.h"
[email protected]05f9b682008-09-29 22:18:0118#include "testing/gtest/include/gtest/gtest.h"
19
Benoit Lize73de21b2021-07-02 08:17:5620namespace base {
21
[email protected]05f9b682008-09-29 22:18:0122namespace {
23
24const int kIntMin = std::numeric_limits<int>::min();
25const int kIntMax = std::numeric_limits<int>::max();
26
27} // namespace
28
Nico Weber0a3852a72015-10-29 20:42:5829TEST(RandUtilTest, RandInt) {
[email protected]05f9b682008-09-29 22:18:0130 EXPECT_EQ(base::RandInt(0, 0), 0);
31 EXPECT_EQ(base::RandInt(kIntMin, kIntMin), kIntMin);
32 EXPECT_EQ(base::RandInt(kIntMax, kIntMax), kIntMax);
Nico Weber0a3852a72015-10-29 20:42:5833
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]05f9b682008-09-29 22:18:0139}
[email protected]94a0f312008-09-30 14:26:3340
41TEST(RandUtilTest, RandDouble) {
[email protected]29548d82011-04-29 21:03:5442 // 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]51a01812011-05-05 08:46:1148TEST(RandUtilTest, RandBytes) {
[email protected]0173b962011-08-24 19:58:3649 const size_t buffer_size = 50;
[email protected]51a01812011-05-05 08:46:1150 char buffer[buffer_size];
51 memset(buffer, 0, buffer_size);
52 base::RandBytes(buffer, buffer_size);
[email protected]0173b962011-08-24 19:58:3653 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]51a01812011-05-05 08:46:1157}
58
Sergey Ulanovfdc62f8e2017-08-01 19:51:0059// Verify that calling base::RandBytes with an empty buffer doesn't fail.
60TEST(RandUtilTest, RandBytes0) {
61 base::RandBytes(nullptr, 0);
62}
63
[email protected]29548d82011-04-29 21:03:5464TEST(RandUtilTest, RandBytesAsString) {
[email protected]fdce4782011-11-29 20:06:1865 std::string random_string = base::RandBytesAsString(1);
66 EXPECT_EQ(1U, random_string.size());
[email protected]29548d82011-04-29 21:03:5467 random_string = base::RandBytesAsString(145);
68 EXPECT_EQ(145U, random_string.size());
69 char accumulator = 0;
jdoerrie6c6229352018-10-22 15:55:4370 for (auto i : random_string)
71 accumulator |= i;
[email protected]29548d82011-04-29 21:03:5472 // 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]94a0f312008-09-30 14:26:3375}
[email protected]a74dcae2010-08-30 21:07:0576
77// Make sure that it is still appropriate to use RandGenerator in conjunction
78// with std::random_shuffle().
79TEST(RandUtilTest, RandGeneratorForRandomShuffle) {
80 EXPECT_EQ(base::RandGenerator(1), 0U);
81 EXPECT_LE(std::numeric_limits<ptrdiff_t>::max(),
avi9b6f42932015-12-26 22:15:1482 std::numeric_limits<int64_t>::max());
[email protected]a74dcae2010-08-30 21:07:0583}
[email protected]af2e192b2011-05-30 17:39:0984
85TEST(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 Kastingfbb9e562021-06-27 02:57:3798 constexpr uint64_t kTopOfRange =
avi9b6f42932015-12-26 22:15:1499 (std::numeric_limits<uint64_t>::max() / 4ULL) * 3ULL;
Peter Kastingfbb9e562021-06-27 02:57:37100 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]af2e192b2011-05-30 17:39:09104
105 double cumulative_average = 0.0;
106 int count = 0;
107 while (count < kMaxAttempts) {
avi9b6f42932015-12-26 22:15:14108 uint64_t value = base::RandGenerator(kTopOfRange);
[email protected]af2e192b2011-05-30 17:39:09109 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 Lize73de21b2021-07-02 08:17:56122 ASSERT_LT(count, kMaxAttempts) << "Expected average was " << kExpectedAverage
123 << ", average ended at " << cumulative_average;
[email protected]af2e192b2011-05-30 17:39:09124}
125
126TEST(RandUtilTest, RandUint64ProducesBothValuesOfAllBits) {
127 // This tests to see that our underlying random generator is good
128 // enough, for some value of good enough.
avi9b6f42932015-12-26 22:15:14129 uint64_t kAllZeros = 0ULL;
130 uint64_t kAllOnes = ~kAllZeros;
131 uint64_t found_ones = kAllZeros;
132 uint64_t found_zeros = kAllOnes;
[email protected]af2e192b2011-05-30 17:39:09133
134 for (size_t i = 0; i < 1000; ++i) {
avi9b6f42932015-12-26 22:15:14135 uint64_t value = base::RandUint64();
[email protected]af2e192b2011-05-30 17:39:09136 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]c910c5a2014-01-23 02:14:28145
Scott Graham4ffd63b52017-06-01 18:03:33146TEST(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]c910c5a2014-01-23 02:14:28157// 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.
160TEST(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
dcheng093de9b2016-04-04 21:25:51166 std::unique_ptr<uint8_t[]> buffer(new uint8_t[kTestBufferSize]);
charliea3be839702015-01-26 17:35:41167 const base::TimeTicks now = base::TimeTicks::Now();
[email protected]c910c5a2014-01-23 02:14:28168 for (int i = 0; i < kTestIterations; ++i)
169 base::RandBytes(buffer.get(), kTestBufferSize);
charliea3be839702015-01-26 17:35:41170 const base::TimeTicks end = base::TimeTicks::Now();
[email protected]c910c5a2014-01-23 02:14:28171
Benoit Lize73de21b2021-07-02 08:17:56172 LOG(INFO) << "RandBytes(" << kTestBufferSize
173 << ") took: " << (end - now).InMicroseconds() << "µs";
[email protected]c910c5a2014-01-23 02:14:28174}
Benoit Lize73de21b2021-07-02 08:17:56175
176TEST(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
199namespace {
200
201constexpr double kXp1Percent = -2.33;
202constexpr double kXp99Percent = 2.33;
203
204double 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
210int 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.
227bool 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
262TEST(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