[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 | |
Paul Miller | c4267fb | 2019-01-03 03:20:35 | [diff] [blame] | 27 | // Size of the low entropy source for testing. |
[email protected] | 9556a89 | 2013-06-21 16:53:20 | [diff] [blame] | 28 | const size_t kMaxLowEntropySize = 8000; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 29 | |
| 30 | // Field trial names used in unit tests. |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 31 | const char* const kTestTrialNames[] = { "TestTrial", "AnotherTestTrial", |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 32 | "NewTabButton" }; |
| 33 | |
| 34 | // Computes the Chi-Square statistic for |values| assuming they follow a uniform |
| 35 | // distribution, where each entry has expected value |expected_value|. |
| 36 | // |
| 37 | // The Chi-Square statistic is defined as Sum((O-E)^2/E) where O is the observed |
| 38 | // value and E is the expected value. |
| 39 | double ComputeChiSquare(const std::vector<int>& values, |
| 40 | double expected_value) { |
| 41 | double sum = 0; |
| 42 | for (size_t i = 0; i < values.size(); ++i) { |
| 43 | const double delta = values[i] - expected_value; |
| 44 | sum += (delta * delta) / expected_value; |
| 45 | } |
| 46 | return sum; |
| 47 | } |
| 48 | |
| 49 | // Computes SHA1-based entropy for the given |trial_name| based on |
| 50 | // |entropy_source| |
| 51 | double GenerateSHA1Entropy(const std::string& entropy_source, |
| 52 | const std::string& trial_name) { |
| 53 | SHA1EntropyProvider sha1_provider(entropy_source); |
[email protected] | 6fded22 | 2013-04-11 20:59:50 | [diff] [blame] | 54 | return sha1_provider.GetEntropyForTrial(trial_name, 0); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 55 | } |
| 56 | |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 57 | // Generates normalized MurmurHash-based entropy for the given |trial_name| |
| 58 | // based on |entropy_source| which must be in the range [0, entropy_max). |
| 59 | double GenerateNormalizedMurmurHashEntropy(uint16_t entropy_source, |
| 60 | size_t entropy_max, |
| 61 | const std::string& trial_name) { |
| 62 | NormalizedMurmurHashEntropyProvider provider(entropy_source, entropy_max); |
| 63 | return provider.GetEntropyForTrial(trial_name, 0); |
| 64 | } |
| 65 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 66 | // Helper interface for testing used to generate entropy values for a given |
| 67 | // field trial. Unlike EntropyProvider, which keeps the low/high entropy source |
| 68 | // value constant and generates entropy for different trial names, instances |
| 69 | // of TrialEntropyGenerator keep the trial name constant and generate low/high |
| 70 | // entropy source values internally to produce each output entropy value. |
| 71 | class TrialEntropyGenerator { |
| 72 | public: |
| 73 | virtual ~TrialEntropyGenerator() {} |
| 74 | virtual double GenerateEntropyValue() const = 0; |
| 75 | }; |
| 76 | |
| 77 | // An TrialEntropyGenerator that uses the SHA1EntropyProvider with the high |
| 78 | // entropy source (random GUID with 128 bits of entropy + 13 additional bits of |
| 79 | // entropy corresponding to a low entropy source). |
| 80 | class SHA1EntropyGenerator : public TrialEntropyGenerator { |
| 81 | public: |
| 82 | explicit SHA1EntropyGenerator(const std::string& trial_name) |
| 83 | : trial_name_(trial_name) { |
| 84 | } |
| 85 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 86 | ~SHA1EntropyGenerator() override {} |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 87 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 88 | double GenerateEntropyValue() const override { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 89 | // Use a random GUID + 13 additional bits of entropy to match how the |
| 90 | // SHA1EntropyProvider is used in metrics_service.cc. |
| 91 | const int low_entropy_source = |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 92 | static_cast<uint16_t>(base::RandInt(0, kMaxLowEntropySize - 1)); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 93 | const std::string high_entropy_source = |
Raul Tambre | f88e510 | 2019-02-06 10:54:03 | [diff] [blame] | 94 | base::GenerateGUID() + base::NumberToString(low_entropy_source); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 95 | return GenerateSHA1Entropy(high_entropy_source, trial_name_); |
| 96 | } |
| 97 | |
| 98 | private: |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 99 | const std::string trial_name_; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 100 | |
| 101 | DISALLOW_COPY_AND_ASSIGN(SHA1EntropyGenerator); |
| 102 | }; |
| 103 | |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 104 | // An TrialEntropyGenerator that uses the normalized MurmurHash entropy provider |
| 105 | // algorithm, using 13-bit low entropy source values. |
| 106 | class NormalizedMurmurHashEntropyGenerator : public TrialEntropyGenerator { |
| 107 | public: |
| 108 | explicit NormalizedMurmurHashEntropyGenerator(const std::string& trial_name) |
| 109 | : trial_name_(trial_name) {} |
| 110 | |
| 111 | ~NormalizedMurmurHashEntropyGenerator() override {} |
| 112 | |
| 113 | double GenerateEntropyValue() const override { |
| 114 | const int low_entropy_source = |
| 115 | static_cast<uint16_t>(base::RandInt(0, kMaxLowEntropySize - 1)); |
| 116 | return GenerateNormalizedMurmurHashEntropy(low_entropy_source, |
| 117 | kMaxLowEntropySize, trial_name_); |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | const std::string trial_name_; |
| 122 | |
| 123 | DISALLOW_COPY_AND_ASSIGN(NormalizedMurmurHashEntropyGenerator); |
| 124 | }; |
| 125 | |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 126 | // Tests uniformity of a given |entropy_generator| using the Chi-Square Goodness |
| 127 | // of Fit Test. |
| 128 | void PerformEntropyUniformityTest( |
| 129 | const std::string& trial_name, |
| 130 | const TrialEntropyGenerator& entropy_generator) { |
| 131 | // Number of buckets in the simulated field trials. |
| 132 | const size_t kBucketCount = 20; |
| 133 | // Max number of iterations to perform before giving up and failing. |
| 134 | const size_t kMaxIterationCount = 100000; |
| 135 | // The number of iterations to perform before each time the statistical |
| 136 | // significance of the results is checked. |
| 137 | const size_t kCheckIterationCount = 10000; |
| 138 | // This is the Chi-Square threshold from the Chi-Square statistic table for |
| 139 | // 19 degrees of freedom (based on |kBucketCount|) with a 99.9% confidence |
| 140 | // level. See: http://www.medcalc.org/manual/chi-square-table.php |
| 141 | const double kChiSquareThreshold = 43.82; |
| 142 | |
| 143 | std::vector<int> distribution(kBucketCount); |
| 144 | |
| 145 | for (size_t i = 1; i <= kMaxIterationCount; ++i) { |
| 146 | const double entropy_value = entropy_generator.GenerateEntropyValue(); |
| 147 | const size_t bucket = static_cast<size_t>(kBucketCount * entropy_value); |
| 148 | ASSERT_LT(bucket, kBucketCount); |
| 149 | distribution[bucket] += 1; |
| 150 | |
| 151 | // After |kCheckIterationCount| iterations, compute the Chi-Square |
| 152 | // statistic of the distribution. If the resulting statistic is greater |
| 153 | // than |kChiSquareThreshold|, we can conclude with 99.9% confidence |
| 154 | // that the observed samples do not follow a uniform distribution. |
| 155 | // |
| 156 | // However, since 99.9% would still result in a false negative every |
| 157 | // 1000 runs of the test, do not treat it as a failure (else the test |
| 158 | // will be flaky). Instead, perform additional iterations to determine |
| 159 | // if the distribution will converge, up to |kMaxIterationCount|. |
| 160 | if ((i % kCheckIterationCount) == 0) { |
| 161 | const double expected_value_per_bucket = |
| 162 | static_cast<double>(i) / kBucketCount; |
| 163 | const double chi_square = |
| 164 | ComputeChiSquare(distribution, expected_value_per_bucket); |
| 165 | if (chi_square < kChiSquareThreshold) |
| 166 | break; |
| 167 | |
| 168 | // If |i == kMaxIterationCount|, the Chi-Square statistic did not |
| 169 | // converge after |kMaxIterationCount|. |
| 170 | EXPECT_NE(i, kMaxIterationCount) << "Failed for trial " << |
| 171 | trial_name << " with chi_square = " << chi_square << |
| 172 | " after " << kMaxIterationCount << " iterations."; |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | } // namespace |
| 178 | |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 179 | TEST(EntropyProviderTest, UseOneTimeRandomizationSHA1) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 180 | // Simply asserts that two trials using one-time randomization |
| 181 | // that have different names, normally generate different results. |
| 182 | // |
| 183 | // Note that depending on the one-time random initialization, they |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 184 | // _might_ actually give the same result, but we know that given the |
| 185 | // particular client_id we use for unit tests they won't. |
robliao | 79393ffb | 2016-09-21 18:45:29 | [diff] [blame] | 186 | base::FieldTrialList field_trial_list( |
Jinho Bang | c3bcb5c | 2018-01-15 16:13:00 | [diff] [blame] | 187 | std::make_unique<SHA1EntropyProvider>("client_id")); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 188 | scoped_refptr<base::FieldTrial> trials[] = { |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 189 | base::FieldTrialList::FactoryGetFieldTrial( |
Alexei Svitkine | cde0b63 | 2019-05-29 14:22:35 | [diff] [blame] | 190 | "one", 100, "default", base::FieldTrial::ONE_TIME_RANDOMIZED, |
| 191 | nullptr), |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 192 | base::FieldTrialList::FactoryGetFieldTrial( |
Alexei Svitkine | cde0b63 | 2019-05-29 14:22:35 | [diff] [blame] | 193 | "two", 100, "default", base::FieldTrial::ONE_TIME_RANDOMIZED, |
| 194 | nullptr), |
[email protected] | ebcf69f0 | 2013-07-30 15:11:29 | [diff] [blame] | 195 | }; |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 196 | |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 197 | for (size_t i = 0; i < base::size(trials); ++i) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 198 | for (int j = 0; j < 100; ++j) |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 199 | trials[i]->AppendGroup(std::string(), 1); |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | // The trials are most likely to give different results since they have |
| 203 | // different names. |
| 204 | EXPECT_NE(trials[0]->group(), trials[1]->group()); |
| 205 | EXPECT_NE(trials[0]->group_name(), trials[1]->group_name()); |
| 206 | } |
| 207 | |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 208 | TEST(EntropyProviderTest, UseOneTimeRandomizationNormalizedMurmurHash) { |
| 209 | // Simply asserts that two trials using one-time randomization |
| 210 | // that have different names, normally generate different results. |
| 211 | // |
| 212 | // Note that depending on the one-time random initialization, they |
| 213 | // _might_ actually give the same result, but we know that given |
| 214 | // the particular low_entropy_source we use for unit tests they won't. |
| 215 | base::FieldTrialList field_trial_list( |
| 216 | std::make_unique<NormalizedMurmurHashEntropyProvider>( |
| 217 | 1234, kMaxLowEntropySize)); |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 218 | scoped_refptr<base::FieldTrial> trials[] = { |
| 219 | base::FieldTrialList::FactoryGetFieldTrial( |
Alexei Svitkine | cde0b63 | 2019-05-29 14:22:35 | [diff] [blame] | 220 | "one", 100, "default", base::FieldTrial::ONE_TIME_RANDOMIZED, |
| 221 | nullptr), |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 222 | base::FieldTrialList::FactoryGetFieldTrial( |
Alexei Svitkine | cde0b63 | 2019-05-29 14:22:35 | [diff] [blame] | 223 | "two", 100, "default", base::FieldTrial::ONE_TIME_RANDOMIZED, |
| 224 | nullptr), |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | for (size_t i = 0; i < base::size(trials); ++i) { |
| 228 | for (int j = 0; j < 100; ++j) |
| 229 | trials[i]->AppendGroup(std::string(), 1); |
| 230 | } |
| 231 | |
| 232 | // The trials are most likely to give different results since they have |
| 233 | // different names. |
| 234 | EXPECT_NE(trials[0]->group(), trials[1]->group()); |
| 235 | EXPECT_NE(trials[0]->group_name(), trials[1]->group_name()); |
| 236 | } |
| 237 | |
jwd | c6e07e2 | 2016-11-21 16:36:54 | [diff] [blame] | 238 | TEST(EntropyProviderTest, UseOneTimeRandomizationWithCustomSeedSHA1) { |
| 239 | // Ensures that two trials with different names but the same custom seed used |
| 240 | // for one time randomization produce the same group assignments. |
| 241 | base::FieldTrialList field_trial_list( |
Jinho Bang | c3bcb5c | 2018-01-15 16:13:00 | [diff] [blame] | 242 | std::make_unique<SHA1EntropyProvider>("client_id")); |
jwd | c6e07e2 | 2016-11-21 16:36:54 | [diff] [blame] | 243 | const uint32_t kCustomSeed = 9001; |
| 244 | scoped_refptr<base::FieldTrial> trials[] = { |
| 245 | base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
Alexei Svitkine | cde0b63 | 2019-05-29 14:22:35 | [diff] [blame] | 246 | "one", 100, "default", base::FieldTrial::ONE_TIME_RANDOMIZED, |
| 247 | kCustomSeed, nullptr, nullptr), |
jwd | c6e07e2 | 2016-11-21 16:36:54 | [diff] [blame] | 248 | base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
Alexei Svitkine | cde0b63 | 2019-05-29 14:22:35 | [diff] [blame] | 249 | "two", 100, "default", base::FieldTrial::ONE_TIME_RANDOMIZED, |
| 250 | kCustomSeed, nullptr, nullptr), |
jwd | c6e07e2 | 2016-11-21 16:36:54 | [diff] [blame] | 251 | }; |
| 252 | |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 253 | for (size_t i = 0; i < base::size(trials); ++i) { |
jwd | c6e07e2 | 2016-11-21 16:36:54 | [diff] [blame] | 254 | for (int j = 0; j < 100; ++j) |
| 255 | trials[i]->AppendGroup(std::string(), 1); |
| 256 | } |
| 257 | |
| 258 | // Normally, these trials should produce different groups, but if the same |
| 259 | // custom seed is used, they should produce the same group assignment. |
| 260 | EXPECT_EQ(trials[0]->group(), trials[1]->group()); |
| 261 | EXPECT_EQ(trials[0]->group_name(), trials[1]->group_name()); |
| 262 | } |
| 263 | |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 264 | TEST(EntropyProviderTest, |
| 265 | UseOneTimeRandomizationWithCustomSeedNormalizedMurmurHash) { |
| 266 | // Ensures that two trials with different names but the same custom seed used |
| 267 | // for one time randomization produce the same group assignments. |
| 268 | base::FieldTrialList field_trial_list( |
| 269 | std::make_unique<NormalizedMurmurHashEntropyProvider>( |
| 270 | 1234, kMaxLowEntropySize)); |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 271 | const uint32_t kCustomSeed = 9001; |
| 272 | scoped_refptr<base::FieldTrial> trials[] = { |
| 273 | base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
Alexei Svitkine | cde0b63 | 2019-05-29 14:22:35 | [diff] [blame] | 274 | "one", 100, "default", base::FieldTrial::ONE_TIME_RANDOMIZED, |
| 275 | kCustomSeed, nullptr, nullptr), |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 276 | base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
Alexei Svitkine | cde0b63 | 2019-05-29 14:22:35 | [diff] [blame] | 277 | "two", 100, "default", base::FieldTrial::ONE_TIME_RANDOMIZED, |
| 278 | kCustomSeed, nullptr, nullptr), |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 279 | }; |
| 280 | |
| 281 | for (size_t i = 0; i < base::size(trials); ++i) { |
| 282 | for (int j = 0; j < 100; ++j) |
| 283 | trials[i]->AppendGroup(std::string(), 1); |
| 284 | } |
| 285 | |
| 286 | // Normally, these trials should produce different groups, but if the same |
| 287 | // custom seed is used, they should produce the same group assignment. |
| 288 | EXPECT_EQ(trials[0]->group(), trials[1]->group()); |
| 289 | EXPECT_EQ(trials[0]->group_name(), trials[1]->group_name()); |
| 290 | } |
| 291 | |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 292 | TEST(EntropyProviderTest, SHA1Entropy) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 293 | const double results[] = { GenerateSHA1Entropy("hi", "1"), |
| 294 | GenerateSHA1Entropy("there", "1") }; |
| 295 | |
| 296 | EXPECT_NE(results[0], results[1]); |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 297 | for (size_t i = 0; i < base::size(results); ++i) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 298 | EXPECT_LE(0.0, results[i]); |
| 299 | EXPECT_GT(1.0, results[i]); |
| 300 | } |
| 301 | |
| 302 | EXPECT_EQ(GenerateSHA1Entropy("yo", "1"), |
| 303 | GenerateSHA1Entropy("yo", "1")); |
| 304 | EXPECT_NE(GenerateSHA1Entropy("yo", "something"), |
| 305 | GenerateSHA1Entropy("yo", "else")); |
| 306 | } |
| 307 | |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 308 | TEST(EntropyProviderTest, NormalizedMurmurHashEntropy) { |
| 309 | const double results[] = { |
| 310 | GenerateNormalizedMurmurHashEntropy(1234, kMaxLowEntropySize, "1"), |
| 311 | GenerateNormalizedMurmurHashEntropy(4321, kMaxLowEntropySize, "1")}; |
| 312 | |
| 313 | EXPECT_NE(results[0], results[1]); |
| 314 | for (size_t i = 0; i < base::size(results); ++i) { |
| 315 | EXPECT_LE(0.0, results[i]); |
| 316 | EXPECT_GT(1.0, results[i]); |
| 317 | } |
| 318 | |
| 319 | EXPECT_EQ(GenerateNormalizedMurmurHashEntropy(1234, kMaxLowEntropySize, "1"), |
| 320 | GenerateNormalizedMurmurHashEntropy(1234, kMaxLowEntropySize, "1")); |
| 321 | EXPECT_NE(GenerateNormalizedMurmurHashEntropy(1234, kMaxLowEntropySize, |
| 322 | "something"), |
| 323 | GenerateNormalizedMurmurHashEntropy(1234, kMaxLowEntropySize, |
| 324 | "else")); |
| 325 | } |
| 326 | |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 327 | TEST(EntropyProviderTest, NormalizedMurmurHashEntropyProviderResults) { |
| 328 | // Verifies that NormalizedMurmurHashEntropyProvider produces expected |
| 329 | // results. This ensures that the results are the same between platforms and |
| 330 | // ensures that changes to the implementation do not regress this |
| 331 | // accidentally. |
| 332 | |
| 333 | EXPECT_DOUBLE_EQ( |
| 334 | 1612 / static_cast<double>(kMaxLowEntropySize), |
| 335 | GenerateNormalizedMurmurHashEntropy(1234, kMaxLowEntropySize, "XYZ")); |
| 336 | EXPECT_DOUBLE_EQ( |
| 337 | 7066 / static_cast<double>(kMaxLowEntropySize), |
| 338 | GenerateNormalizedMurmurHashEntropy(1, kMaxLowEntropySize, "Test")); |
| 339 | EXPECT_DOUBLE_EQ( |
| 340 | 5668 / static_cast<double>(kMaxLowEntropySize), |
| 341 | GenerateNormalizedMurmurHashEntropy(5000, kMaxLowEntropySize, "Foo")); |
| 342 | } |
| 343 | |
[email protected] | c277e2b | 2013-08-02 15:41:08 | [diff] [blame] | 344 | TEST(EntropyProviderTest, SHA1EntropyIsUniform) { |
Byoungkown | 1bb5022 | 2018-09-11 01:14:41 | [diff] [blame] | 345 | for (size_t i = 0; i < base::size(kTestTrialNames); ++i) { |
[email protected] | 20f999b5 | 2012-08-24 22:32:59 | [diff] [blame] | 346 | SHA1EntropyGenerator entropy_generator(kTestTrialNames[i]); |
| 347 | PerformEntropyUniformityTest(kTestTrialNames[i], entropy_generator); |
| 348 | } |
| 349 | } |
| 350 | |
Paul Miller | 7c0efea | 2018-11-13 23:49:00 | [diff] [blame] | 351 | TEST(EntropyProviderTest, NormalizedMurmurHashEntropyIsUniform) { |
| 352 | for (size_t i = 0; i < base::size(kTestTrialNames); ++i) { |
| 353 | NormalizedMurmurHashEntropyGenerator entropy_generator(kTestTrialNames[i]); |
| 354 | PerformEntropyUniformityTest(kTestTrialNames[i], entropy_generator); |
| 355 | } |
| 356 | } |
| 357 | |
Alexei Svitkine | 9de32cb | 2018-02-06 20:21:21 | [diff] [blame] | 358 | } // namespace variations |