[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 50ae9f1 | 2013-08-29 18:03:22 | [diff] [blame] | 5 | #include "components/variations/entropy_provider.h" |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 6 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 10 | #include <cmath> |
| 11 | #include <limits> |
Jinho Bang | c3bcb5c | 2018-01-15 16:13:00 | [diff] [blame] | 12 | #include <memory> |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 13 | #include <numeric> |
| 14 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 15 | #include "base/guid.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 17 | #include "base/rand_util.h" |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 18 | #include "base/stl_util.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 19 | #include "base/strings/string_number_conversions.h" |
Alexei Svitkine | 9de32cb | 2018-02-06 20:21:21 | [diff] [blame] | 20 | #include "components/variations/hashing.h" |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
| 22 | |
Alexei Svitkine | 9de32cb | 2018-02-06 20:21:21 | [diff] [blame] | 23 | namespace variations { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 24 | |
| 25 | namespace { |
| 26 | |
| 27 | // Size of the low entropy source to use for the permuted entropy provider |
| 28 | // in tests. |
[email protected] | 9556a89 | 2013-06-21 16:53:20 | [diff] [blame] | 29 | const size_t kMaxLowEntropySize = 8000; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 30 | |
| 31 | // Field trial names used in unit tests. |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 32 | const char* const kTestTrialNames[] = { "TestTrial", "AnotherTestTrial", |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 33 | "NewTabButton" }; |
| 34 | |
| 35 | // Computes the Chi-Square statistic for |values| assuming they follow a uniform |
| 36 | // distribution, where each entry has expected value |expected_value|. |
| 37 | // |
| 38 | // The Chi-Square statistic is defined as Sum((O-E)^2/E) where O is the observed |
| 39 | // value and E is the expected value. |
| 40 | double ComputeChiSquare(const std::vector<int>& values, |
| 41 | double expected_value) { |
| 42 | double sum = 0; |
| 43 | for (size_t i = 0; i < values.size(); ++i) { |
| 44 | const double delta = values[i] - expected_value; |
| 45 | sum += (delta * delta) / expected_value; |
| 46 | } |
| 47 | return sum; |
| 48 | } |
| 49 | |
| 50 | // Computes SHA1-based entropy for the given |trial_name| based on |
| 51 | // |entropy_source| |
| 52 | double GenerateSHA1Entropy(const std::string& entropy_source, |
| 53 | const std::string& trial_name) { |
| 54 | SHA1EntropyProvider sha1_provider(entropy_source); |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 55 | return sha1_provider.GetEntropyForTrial(trial_name, 0); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Generates permutation-based entropy for the given |trial_name| based on |
| 59 | // |entropy_source| which must be in the range [0, entropy_max). |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 60 | double GeneratePermutedEntropy(uint16_t entropy_source, |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 61 | size_t entropy_max, |
| 62 | const std::string& trial_name) { |
| 63 | PermutedEntropyProvider permuted_provider(entropy_source, entropy_max); |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 64 | return permuted_provider.GetEntropyForTrial(trial_name, 0); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 65 | } |
| 66 | |
Steven Holte | c59ddd9f | 2018-09-05 00:02:27 | [diff] [blame] | 67 | // Make a vector of consecutive integers for shuffling. |
| 68 | std::vector<uint16_t> MakeRange(size_t vector_size) { |
| 69 | std::vector<uint16_t> range(vector_size); |
| 70 | for (size_t i = 0; i < vector_size; ++i) |
| 71 | range[i] = i; |
| 72 | return range; |
| 73 | } |
| 74 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 75 | // Helper interface for testing used to generate entropy values for a given |
| 76 | // field trial. Unlike EntropyProvider, which keeps the low/high entropy source |
| 77 | // value constant and generates entropy for different trial names, instances |
| 78 | // of TrialEntropyGenerator keep the trial name constant and generate low/high |
| 79 | // entropy source values internally to produce each output entropy value. |
| 80 | class TrialEntropyGenerator { |
| 81 | public: |
| 82 | virtual ~TrialEntropyGenerator() {} |
| 83 | virtual double GenerateEntropyValue() const = 0; |
| 84 | }; |
| 85 | |
| 86 | // An TrialEntropyGenerator that uses the SHA1EntropyProvider with the high |
| 87 | // entropy source (random GUID with 128 bits of entropy + 13 additional bits of |
| 88 | // entropy corresponding to a low entropy source). |
| 89 | class SHA1EntropyGenerator : public TrialEntropyGenerator { |
| 90 | public: |
| 91 | explicit SHA1EntropyGenerator(const std::string& trial_name) |
| 92 | : trial_name_(trial_name) { |
| 93 | } |
| 94 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 95 | ~SHA1EntropyGenerator() override {} |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 96 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 97 | double GenerateEntropyValue() const override { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 98 | // Use a random GUID + 13 additional bits of entropy to match how the |
| 99 | // SHA1EntropyProvider is used in metrics_service.cc. |
| 100 | const int low_entropy_source = |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 101 | static_cast<uint16_t>(base::RandInt(0, kMaxLowEntropySize - 1)); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 102 | const std::string high_entropy_source = |
| 103 | base::GenerateGUID() + base::IntToString(low_entropy_source); |
| 104 | return GenerateSHA1Entropy(high_entropy_source, trial_name_); |
| 105 | } |
| 106 | |
| 107 | private: |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 108 | std::string trial_name_; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 109 | |
| 110 | DISALLOW_COPY_AND_ASSIGN(SHA1EntropyGenerator); |
| 111 | }; |
| 112 | |
| 113 | // An TrialEntropyGenerator that uses the permuted entropy provider algorithm, |
| 114 | // using 13-bit low entropy source values. |
| 115 | class PermutedEntropyGenerator : public TrialEntropyGenerator { |
| 116 | public: |
| 117 | explicit PermutedEntropyGenerator(const std::string& trial_name) |
| 118 | : mapping_(kMaxLowEntropySize) { |
| 119 | // Note: Given a trial name, the computed mapping will be the same. |
| 120 | // As a performance optimization, pre-compute the mapping once per trial |
| 121 | // name and index into it for each entropy value. |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 122 | const uint32_t randomization_seed = HashName(trial_name); |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 123 | internal::PermuteMappingUsingRandomizationSeed(randomization_seed, |
| 124 | &mapping_); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 125 | } |
| 126 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 127 | ~PermutedEntropyGenerator() override {} |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 128 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 129 | double GenerateEntropyValue() const override { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 130 | const int low_entropy_source = |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 131 | static_cast<uint16_t>(base::RandInt(0, kMaxLowEntropySize - 1)); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 132 | return mapping_[low_entropy_source] / |
| 133 | static_cast<double>(kMaxLowEntropySize); |
| 134 | } |
| 135 | |
| 136 | private: |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 137 | std::vector<uint16_t> mapping_; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 138 | |
| 139 | DISALLOW_COPY_AND_ASSIGN(PermutedEntropyGenerator); |
| 140 | }; |
| 141 | |
| 142 | // Tests uniformity of a given |entropy_generator| using the Chi-Square Goodness |
| 143 | // of Fit Test. |
| 144 | void PerformEntropyUniformityTest( |
| 145 | const std::string& trial_name, |
| 146 | const TrialEntropyGenerator& entropy_generator) { |
| 147 | // Number of buckets in the simulated field trials. |
| 148 | const size_t kBucketCount = 20; |
| 149 | // Max number of iterations to perform before giving up and failing. |
| 150 | const size_t kMaxIterationCount = 100000; |
| 151 | // The number of iterations to perform before each time the statistical |
| 152 | // significance of the results is checked. |
| 153 | const size_t kCheckIterationCount = 10000; |
| 154 | // This is the Chi-Square threshold from the Chi-Square statistic table for |
| 155 | // 19 degrees of freedom (based on |kBucketCount|) with a 99.9% confidence |
| 156 | // level. See: http://www.medcalc.org/manual/chi-square-table.php |
| 157 | const double kChiSquareThreshold = 43.82; |
| 158 | |
| 159 | std::vector<int> distribution(kBucketCount); |
| 160 | |
| 161 | for (size_t i = 1; i <= kMaxIterationCount; ++i) { |
| 162 | const double entropy_value = entropy_generator.GenerateEntropyValue(); |
| 163 | const size_t bucket = static_cast<size_t>(kBucketCount * entropy_value); |
| 164 | ASSERT_LT(bucket, kBucketCount); |
| 165 | distribution[bucket] += 1; |
| 166 | |
| 167 | // After |kCheckIterationCount| iterations, compute the Chi-Square |
| 168 | // statistic of the distribution. If the resulting statistic is greater |
| 169 | // than |kChiSquareThreshold|, we can conclude with 99.9% confidence |
| 170 | // that the observed samples do not follow a uniform distribution. |
| 171 | // |
| 172 | // However, since 99.9% would still result in a false negative every |
| 173 | // 1000 runs of the test, do not treat it as a failure (else the test |
| 174 | // will be flaky). Instead, perform additional iterations to determine |
| 175 | // if the distribution will converge, up to |kMaxIterationCount|. |
| 176 | if ((i % kCheckIterationCount) == 0) { |
| 177 | const double expected_value_per_bucket = |
| 178 | static_cast<double>(i) / kBucketCount; |
| 179 | const double chi_square = |
| 180 | ComputeChiSquare(distribution, expected_value_per_bucket); |
| 181 | if (chi_square < kChiSquareThreshold) |
| 182 | break; |
| 183 | |
| 184 | // If |i == kMaxIterationCount|, the Chi-Square statistic did not |
| 185 | // converge after |kMaxIterationCount|. |
| 186 | EXPECT_NE(i, kMaxIterationCount) << "Failed for trial " << |
| 187 | trial_name << " with chi_square = " << chi_square << |
| 188 | " after " << kMaxIterationCount << " iterations."; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | } // namespace |
| 194 | |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 195 | TEST(EntropyProviderTest, UseOneTimeRandomizationSHA1) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 196 | // Simply asserts that two trials using one-time randomization |
| 197 | // that have different names, normally generate different results. |
| 198 | // |
| 199 | // Note that depending on the one-time random initialization, they |
| 200 | // _might_ actually give the same result, but we know that given |
| 201 | // the particular client_id we use for unit tests they won't. |
robliao | 79393ffb | 2016-09-21 18:45:29 | [diff] [blame] | 202 | base::FieldTrialList field_trial_list( |
Jinho Bang | c3bcb5c | 2018-01-15 16:13:00 | [diff] [blame] | 203 | std::make_unique<SHA1EntropyProvider>("client_id")); |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 204 | const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 205 | scoped_refptr<base::FieldTrial> trials[] = { |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 206 | base::FieldTrialList::FactoryGetFieldTrial( |
| 207 | "one", 100, "default", kNoExpirationYear, 1, 1, |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 208 | base::FieldTrial::ONE_TIME_RANDOMIZED, nullptr), |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 209 | base::FieldTrialList::FactoryGetFieldTrial( |
| 210 | "two", 100, "default", kNoExpirationYear, 1, 1, |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 211 | base::FieldTrial::ONE_TIME_RANDOMIZED, nullptr), |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 212 | }; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 213 | |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 214 | for (size_t i = 0; i < base::size(trials); ++i) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 215 | for (int j = 0; j < 100; ++j) |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 216 | trials[i]->AppendGroup(std::string(), 1); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // The trials are most likely to give different results since they have |
| 220 | // different names. |
| 221 | EXPECT_NE(trials[0]->group(), trials[1]->group()); |
| 222 | EXPECT_NE(trials[0]->group_name(), trials[1]->group_name()); |
| 223 | } |
| 224 | |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 225 | TEST(EntropyProviderTest, UseOneTimeRandomizationPermuted) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 226 | // Simply asserts that two trials using one-time randomization |
| 227 | // that have different names, normally generate different results. |
| 228 | // |
| 229 | // Note that depending on the one-time random initialization, they |
| 230 | // _might_ actually give the same result, but we know that given |
| 231 | // the particular client_id we use for unit tests they won't. |
| 232 | base::FieldTrialList field_trial_list( |
Jinho Bang | c3bcb5c | 2018-01-15 16:13:00 | [diff] [blame] | 233 | std::make_unique<PermutedEntropyProvider>(1234, kMaxLowEntropySize)); |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 234 | const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 235 | scoped_refptr<base::FieldTrial> trials[] = { |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 236 | base::FieldTrialList::FactoryGetFieldTrial( |
| 237 | "one", 100, "default", kNoExpirationYear, 1, 1, |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 238 | base::FieldTrial::ONE_TIME_RANDOMIZED, nullptr), |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 239 | base::FieldTrialList::FactoryGetFieldTrial( |
| 240 | "two", 100, "default", kNoExpirationYear, 1, 1, |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 241 | base::FieldTrial::ONE_TIME_RANDOMIZED, nullptr), |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 242 | }; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 243 | |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 244 | for (size_t i = 0; i < base::size(trials); ++i) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 245 | for (int j = 0; j < 100; ++j) |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 246 | trials[i]->AppendGroup(std::string(), 1); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | // The trials are most likely to give different results since they have |
| 250 | // different names. |
| 251 | EXPECT_NE(trials[0]->group(), trials[1]->group()); |
| 252 | EXPECT_NE(trials[0]->group_name(), trials[1]->group_name()); |
| 253 | } |
| 254 | |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 255 | TEST(EntropyProviderTest, UseOneTimeRandomizationWithCustomSeedPermuted) { |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 256 | // Ensures that two trials with different names but the same custom seed used |
| 257 | // for one time randomization produce the same group assignments. |
| 258 | base::FieldTrialList field_trial_list( |
Jinho Bang | c3bcb5c | 2018-01-15 16:13:00 | [diff] [blame] | 259 | std::make_unique<PermutedEntropyProvider>(1234, kMaxLowEntropySize)); |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 260 | const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear; |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 261 | const uint32_t kCustomSeed = 9001; |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 262 | scoped_refptr<base::FieldTrial> trials[] = { |
| 263 | base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
| 264 | "one", 100, "default", kNoExpirationYear, 1, 1, |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 265 | base::FieldTrial::ONE_TIME_RANDOMIZED, kCustomSeed, nullptr, nullptr), |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 266 | base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
| 267 | "two", 100, "default", kNoExpirationYear, 1, 1, |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 268 | base::FieldTrial::ONE_TIME_RANDOMIZED, kCustomSeed, nullptr, nullptr), |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 269 | }; |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 270 | |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 271 | for (size_t i = 0; i < base::size(trials); ++i) { |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 272 | for (int j = 0; j < 100; ++j) |
| 273 | trials[i]->AppendGroup(std::string(), 1); |
| 274 | } |
| 275 | |
| 276 | // Normally, these trials should produce different groups, but if the same |
| 277 | // custom seed is used, they should produce the same group assignment. |
| 278 | EXPECT_EQ(trials[0]->group(), trials[1]->group()); |
| 279 | EXPECT_EQ(trials[0]->group_name(), trials[1]->group_name()); |
| 280 | } |
| 281 | |
jwd | c6e07e2 | 2016-11-21 16:36:54 | [diff] [blame] | 282 | TEST(EntropyProviderTest, UseOneTimeRandomizationWithCustomSeedSHA1) { |
| 283 | // Ensures that two trials with different names but the same custom seed used |
| 284 | // for one time randomization produce the same group assignments. |
| 285 | base::FieldTrialList field_trial_list( |
Jinho Bang | c3bcb5c | 2018-01-15 16:13:00 | [diff] [blame] | 286 | std::make_unique<SHA1EntropyProvider>("client_id")); |
jwd | c6e07e2 | 2016-11-21 16:36:54 | [diff] [blame] | 287 | const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear; |
| 288 | const uint32_t kCustomSeed = 9001; |
| 289 | scoped_refptr<base::FieldTrial> trials[] = { |
| 290 | base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
| 291 | "one", 100, "default", kNoExpirationYear, 1, 1, |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 292 | base::FieldTrial::ONE_TIME_RANDOMIZED, kCustomSeed, nullptr, nullptr), |
jwd | c6e07e2 | 2016-11-21 16:36:54 | [diff] [blame] | 293 | base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
| 294 | "two", 100, "default", kNoExpirationYear, 1, 1, |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 295 | base::FieldTrial::ONE_TIME_RANDOMIZED, kCustomSeed, nullptr, nullptr), |
jwd | c6e07e2 | 2016-11-21 16:36:54 | [diff] [blame] | 296 | }; |
| 297 | |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 298 | for (size_t i = 0; i < base::size(trials); ++i) { |
jwd | c6e07e2 | 2016-11-21 16:36:54 | [diff] [blame] | 299 | for (int j = 0; j < 100; ++j) |
| 300 | trials[i]->AppendGroup(std::string(), 1); |
| 301 | } |
| 302 | |
| 303 | // Normally, these trials should produce different groups, but if the same |
| 304 | // custom seed is used, they should produce the same group assignment. |
| 305 | EXPECT_EQ(trials[0]->group(), trials[1]->group()); |
| 306 | EXPECT_EQ(trials[0]->group_name(), trials[1]->group_name()); |
| 307 | } |
| 308 | |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 309 | TEST(EntropyProviderTest, SHA1Entropy) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 310 | const double results[] = { GenerateSHA1Entropy("hi", "1"), |
| 311 | GenerateSHA1Entropy("there", "1") }; |
| 312 | |
| 313 | EXPECT_NE(results[0], results[1]); |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 314 | for (size_t i = 0; i < base::size(results); ++i) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 315 | EXPECT_LE(0.0, results[i]); |
| 316 | EXPECT_GT(1.0, results[i]); |
| 317 | } |
| 318 | |
| 319 | EXPECT_EQ(GenerateSHA1Entropy("yo", "1"), |
| 320 | GenerateSHA1Entropy("yo", "1")); |
| 321 | EXPECT_NE(GenerateSHA1Entropy("yo", "something"), |
| 322 | GenerateSHA1Entropy("yo", "else")); |
| 323 | } |
| 324 | |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 325 | TEST(EntropyProviderTest, PermutedEntropy) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 326 | const double results[] = { |
| 327 | GeneratePermutedEntropy(1234, kMaxLowEntropySize, "1"), |
| 328 | GeneratePermutedEntropy(4321, kMaxLowEntropySize, "1") }; |
| 329 | |
| 330 | EXPECT_NE(results[0], results[1]); |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 331 | for (size_t i = 0; i < base::size(results); ++i) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 332 | EXPECT_LE(0.0, results[i]); |
| 333 | EXPECT_GT(1.0, results[i]); |
| 334 | } |
| 335 | |
| 336 | EXPECT_EQ(GeneratePermutedEntropy(1234, kMaxLowEntropySize, "1"), |
| 337 | GeneratePermutedEntropy(1234, kMaxLowEntropySize, "1")); |
| 338 | EXPECT_NE(GeneratePermutedEntropy(1234, kMaxLowEntropySize, "something"), |
| 339 | GeneratePermutedEntropy(1234, kMaxLowEntropySize, "else")); |
| 340 | } |
| 341 | |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 342 | TEST(EntropyProviderTest, PermutedEntropyProviderResults) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 343 | // Verifies that PermutedEntropyProvider produces expected results. This |
| 344 | // ensures that the results are the same between platforms and ensures that |
| 345 | // changes to the implementation do not regress this accidentally. |
| 346 | |
| 347 | EXPECT_DOUBLE_EQ(2194 / static_cast<double>(kMaxLowEntropySize), |
| 348 | GeneratePermutedEntropy(1234, kMaxLowEntropySize, "XYZ")); |
| 349 | EXPECT_DOUBLE_EQ(5676 / static_cast<double>(kMaxLowEntropySize), |
| 350 | GeneratePermutedEntropy(1, kMaxLowEntropySize, "Test")); |
| 351 | EXPECT_DOUBLE_EQ(1151 / static_cast<double>(kMaxLowEntropySize), |
| 352 | GeneratePermutedEntropy(5000, kMaxLowEntropySize, "Foo")); |
| 353 | } |
| 354 | |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 355 | TEST(EntropyProviderTest, SHA1EntropyIsUniform) { |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 356 | for (size_t i = 0; i < base::size(kTestTrialNames); ++i) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 357 | SHA1EntropyGenerator entropy_generator(kTestTrialNames[i]); |
| 358 | PerformEntropyUniformityTest(kTestTrialNames[i], entropy_generator); |
| 359 | } |
| 360 | } |
| 361 | |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 362 | TEST(EntropyProviderTest, PermutedEntropyIsUniform) { |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 363 | for (size_t i = 0; i < base::size(kTestTrialNames); ++i) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 364 | PermutedEntropyGenerator entropy_generator(kTestTrialNames[i]); |
| 365 | PerformEntropyUniformityTest(kTestTrialNames[i], entropy_generator); |
| 366 | } |
| 367 | } |
| 368 | |
Steven Holte | c59ddd9f | 2018-09-05 00:02:27 | [diff] [blame] | 369 | TEST(EntropyProviderTest, PermutedEntropyConsistency) { |
| 370 | std::vector<uint16_t> to_shuffle = MakeRange(10); |
| 371 | std::vector<uint16_t> expected = {7, 6, 8, 3, 2, 0, 1, 4, 9, 5}; |
| 372 | internal::PermuteMappingUsingRandomizationSeed(5, &to_shuffle); |
| 373 | EXPECT_EQ(expected, to_shuffle); |
| 374 | } |
| 375 | |
| 376 | TEST(EntropyProviderTest, PermutedEntropyConsistencyWithReroll) { |
| 377 | // Test that the permuted entropy provider is consistent with previously |
| 378 | // shipped versions, with a case that requires rerolls. |
| 379 | std::vector<uint16_t> to_shuffle = MakeRange(1000); |
| 380 | std::vector<uint16_t> expected = {776, 561, 72, 966, 500}; |
| 381 | internal::PermuteMappingUsingRandomizationSeed(10694, &to_shuffle); |
| 382 | std::vector<uint16_t> slice(to_shuffle.begin(), to_shuffle.begin() + 5); |
| 383 | EXPECT_EQ(expected, slice); |
| 384 | } |
| 385 | |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 386 | TEST(EntropyProviderTest, SeededRandGeneratorIsUniform) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 387 | // Verifies that SeededRandGenerator has a uniform distribution. |
| 388 | // |
| 389 | // Mirrors RandUtilTest.RandGeneratorIsUniform in base/rand_util_unittest.cc. |
| 390 | |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 391 | const uint32_t kTopOfRange = |
| 392 | (std::numeric_limits<uint32_t>::max() / 4ULL) * 3ULL; |
| 393 | const uint32_t kExpectedAverage = kTopOfRange / 2ULL; |
| 394 | const uint32_t kAllowedVariance = kExpectedAverage / 50ULL; // +/- 2% |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 395 | const int kMinAttempts = 1000; |
| 396 | const int kMaxAttempts = 1000000; |
| 397 | |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 398 | for (size_t i = 0; i < base::size(kTestTrialNames); ++i) { |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 399 | const uint32_t seed = HashName(kTestTrialNames[i]); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 400 | internal::SeededRandGenerator rand_generator(seed); |
| 401 | |
| 402 | double cumulative_average = 0.0; |
| 403 | int count = 0; |
| 404 | while (count < kMaxAttempts) { |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 405 | uint32_t value = rand_generator(kTopOfRange); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 406 | cumulative_average = (count * cumulative_average + value) / (count + 1); |
| 407 | |
| 408 | // Don't quit too quickly for things to start converging, or we may have |
| 409 | // a false positive. |
| 410 | if (count > kMinAttempts && |
| 411 | kExpectedAverage - kAllowedVariance < cumulative_average && |
| 412 | cumulative_average < kExpectedAverage + kAllowedVariance) { |
| 413 | break; |
| 414 | } |
| 415 | |
| 416 | ++count; |
| 417 | } |
| 418 | |
| 419 | ASSERT_LT(count, kMaxAttempts) << "Expected average was " << |
| 420 | kExpectedAverage << ", average ended at " << cumulative_average << |
| 421 | ", for trial " << kTestTrialNames[i]; |
| 422 | } |
| 423 | } |
| 424 | |
Alexei Svitkine | 9de32cb | 2018-02-06 20:21:21 | [diff] [blame] | 425 | } // namespace variations |