This source file includes following definitions.
- ComputeChiSquare
- GenerateSHA1Entropy
- GeneratePermutedEntropy
- GenerateEntropyValue
- GenerateEntropyValue
- PerformEntropyUniformityTest
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "components/variations/entropy_provider.h"
#include <cmath>
#include <limits>
#include <numeric>
#include "base/basictypes.h"
#include "base/guid.h"
#include "base/memory/scoped_ptr.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "components/variations/metrics_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace metrics {
namespace {
const size_t kMaxLowEntropySize = 8000;
const char* const kTestTrialNames[] = { "TestTrial", "AnotherTestTrial",
"NewTabButton" };
double ComputeChiSquare(const std::vector<int>& values,
double expected_value) {
double sum = 0;
for (size_t i = 0; i < values.size(); ++i) {
const double delta = values[i] - expected_value;
sum += (delta * delta) / expected_value;
}
return sum;
}
double GenerateSHA1Entropy(const std::string& entropy_source,
const std::string& trial_name) {
SHA1EntropyProvider sha1_provider(entropy_source);
return sha1_provider.GetEntropyForTrial(trial_name, 0);
}
double GeneratePermutedEntropy(uint16 entropy_source,
size_t entropy_max,
const std::string& trial_name) {
PermutedEntropyProvider permuted_provider(entropy_source, entropy_max);
return permuted_provider.GetEntropyForTrial(trial_name, 0);
}
class TrialEntropyGenerator {
public:
virtual ~TrialEntropyGenerator() {}
virtual double GenerateEntropyValue() const = 0;
};
class SHA1EntropyGenerator : public TrialEntropyGenerator {
public:
explicit SHA1EntropyGenerator(const std::string& trial_name)
: trial_name_(trial_name) {
}
virtual ~SHA1EntropyGenerator() {
}
virtual double GenerateEntropyValue() const OVERRIDE {
const int low_entropy_source =
static_cast<uint16>(base::RandInt(0, kMaxLowEntropySize - 1));
const std::string high_entropy_source =
base::GenerateGUID() + base::IntToString(low_entropy_source);
return GenerateSHA1Entropy(high_entropy_source, trial_name_);
}
private:
std::string trial_name_;
DISALLOW_COPY_AND_ASSIGN(SHA1EntropyGenerator);
};
class PermutedEntropyGenerator : public TrialEntropyGenerator {
public:
explicit PermutedEntropyGenerator(const std::string& trial_name)
: mapping_(kMaxLowEntropySize) {
const uint32 randomization_seed = HashName(trial_name);
internal::PermuteMappingUsingRandomizationSeed(randomization_seed,
&mapping_);
}
virtual ~PermutedEntropyGenerator() {
}
virtual double GenerateEntropyValue() const OVERRIDE {
const int low_entropy_source =
static_cast<uint16>(base::RandInt(0, kMaxLowEntropySize - 1));
return mapping_[low_entropy_source] /
static_cast<double>(kMaxLowEntropySize);
}
private:
std::vector<uint16> mapping_;
DISALLOW_COPY_AND_ASSIGN(PermutedEntropyGenerator);
};
void PerformEntropyUniformityTest(
const std::string& trial_name,
const TrialEntropyGenerator& entropy_generator) {
const size_t kBucketCount = 20;
const size_t kMaxIterationCount = 100000;
const size_t kCheckIterationCount = 10000;
const double kChiSquareThreshold = 43.82;
std::vector<int> distribution(kBucketCount);
for (size_t i = 1; i <= kMaxIterationCount; ++i) {
const double entropy_value = entropy_generator.GenerateEntropyValue();
const size_t bucket = static_cast<size_t>(kBucketCount * entropy_value);
ASSERT_LT(bucket, kBucketCount);
distribution[bucket] += 1;
if ((i % kCheckIterationCount) == 0) {
const double expected_value_per_bucket =
static_cast<double>(i) / kBucketCount;
const double chi_square =
ComputeChiSquare(distribution, expected_value_per_bucket);
if (chi_square < kChiSquareThreshold)
break;
EXPECT_NE(i, kMaxIterationCount) << "Failed for trial " <<
trial_name << " with chi_square = " << chi_square <<
" after " << kMaxIterationCount << " iterations.";
}
}
}
}
TEST(EntropyProviderTest, UseOneTimeRandomizationSHA1) {
base::FieldTrialList field_trial_list(new SHA1EntropyProvider("client_id"));
const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear;
scoped_refptr<base::FieldTrial> trials[] = {
base::FieldTrialList::FactoryGetFieldTrial(
"one", 100, "default", kNoExpirationYear, 1, 1,
base::FieldTrial::ONE_TIME_RANDOMIZED, NULL),
base::FieldTrialList::FactoryGetFieldTrial(
"two", 100, "default", kNoExpirationYear, 1, 1,
base::FieldTrial::ONE_TIME_RANDOMIZED, NULL),
};
for (size_t i = 0; i < arraysize(trials); ++i) {
for (int j = 0; j < 100; ++j)
trials[i]->AppendGroup(std::string(), 1);
}
EXPECT_NE(trials[0]->group(), trials[1]->group());
EXPECT_NE(trials[0]->group_name(), trials[1]->group_name());
}
TEST(EntropyProviderTest, UseOneTimeRandomizationPermuted) {
base::FieldTrialList field_trial_list(
new PermutedEntropyProvider(1234, kMaxLowEntropySize));
const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear;
scoped_refptr<base::FieldTrial> trials[] = {
base::FieldTrialList::FactoryGetFieldTrial(
"one", 100, "default", kNoExpirationYear, 1, 1,
base::FieldTrial::ONE_TIME_RANDOMIZED, NULL),
base::FieldTrialList::FactoryGetFieldTrial(
"two", 100, "default", kNoExpirationYear, 1, 1,
base::FieldTrial::ONE_TIME_RANDOMIZED, NULL),
};
for (size_t i = 0; i < arraysize(trials); ++i) {
for (int j = 0; j < 100; ++j)
trials[i]->AppendGroup(std::string(), 1);
}
EXPECT_NE(trials[0]->group(), trials[1]->group());
EXPECT_NE(trials[0]->group_name(), trials[1]->group_name());
}
TEST(EntropyProviderTest, UseOneTimeRandomizationWithCustomSeedPermuted) {
base::FieldTrialList field_trial_list(
new PermutedEntropyProvider(1234, kMaxLowEntropySize));
const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear;
const uint32 kCustomSeed = 9001;
scoped_refptr<base::FieldTrial> trials[] = {
base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed(
"one", 100, "default", kNoExpirationYear, 1, 1,
base::FieldTrial::ONE_TIME_RANDOMIZED, kCustomSeed, NULL),
base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed(
"two", 100, "default", kNoExpirationYear, 1, 1,
base::FieldTrial::ONE_TIME_RANDOMIZED, kCustomSeed, NULL),
};
for (size_t i = 0; i < arraysize(trials); ++i) {
for (int j = 0; j < 100; ++j)
trials[i]->AppendGroup(std::string(), 1);
}
EXPECT_EQ(trials[0]->group(), trials[1]->group());
EXPECT_EQ(trials[0]->group_name(), trials[1]->group_name());
}
TEST(EntropyProviderTest, SHA1Entropy) {
const double results[] = { GenerateSHA1Entropy("hi", "1"),
GenerateSHA1Entropy("there", "1") };
EXPECT_NE(results[0], results[1]);
for (size_t i = 0; i < arraysize(results); ++i) {
EXPECT_LE(0.0, results[i]);
EXPECT_GT(1.0, results[i]);
}
EXPECT_EQ(GenerateSHA1Entropy("yo", "1"),
GenerateSHA1Entropy("yo", "1"));
EXPECT_NE(GenerateSHA1Entropy("yo", "something"),
GenerateSHA1Entropy("yo", "else"));
}
TEST(EntropyProviderTest, PermutedEntropy) {
const double results[] = {
GeneratePermutedEntropy(1234, kMaxLowEntropySize, "1"),
GeneratePermutedEntropy(4321, kMaxLowEntropySize, "1") };
EXPECT_NE(results[0], results[1]);
for (size_t i = 0; i < arraysize(results); ++i) {
EXPECT_LE(0.0, results[i]);
EXPECT_GT(1.0, results[i]);
}
EXPECT_EQ(GeneratePermutedEntropy(1234, kMaxLowEntropySize, "1"),
GeneratePermutedEntropy(1234, kMaxLowEntropySize, "1"));
EXPECT_NE(GeneratePermutedEntropy(1234, kMaxLowEntropySize, "something"),
GeneratePermutedEntropy(1234, kMaxLowEntropySize, "else"));
}
TEST(EntropyProviderTest, PermutedEntropyProviderResults) {
EXPECT_DOUBLE_EQ(2194 / static_cast<double>(kMaxLowEntropySize),
GeneratePermutedEntropy(1234, kMaxLowEntropySize, "XYZ"));
EXPECT_DOUBLE_EQ(5676 / static_cast<double>(kMaxLowEntropySize),
GeneratePermutedEntropy(1, kMaxLowEntropySize, "Test"));
EXPECT_DOUBLE_EQ(1151 / static_cast<double>(kMaxLowEntropySize),
GeneratePermutedEntropy(5000, kMaxLowEntropySize, "Foo"));
}
TEST(EntropyProviderTest, SHA1EntropyIsUniform) {
for (size_t i = 0; i < arraysize(kTestTrialNames); ++i) {
SHA1EntropyGenerator entropy_generator(kTestTrialNames[i]);
PerformEntropyUniformityTest(kTestTrialNames[i], entropy_generator);
}
}
TEST(EntropyProviderTest, PermutedEntropyIsUniform) {
for (size_t i = 0; i < arraysize(kTestTrialNames); ++i) {
PermutedEntropyGenerator entropy_generator(kTestTrialNames[i]);
PerformEntropyUniformityTest(kTestTrialNames[i], entropy_generator);
}
}
TEST(EntropyProviderTest, SeededRandGeneratorIsUniform) {
const uint32 kTopOfRange = (std::numeric_limits<uint32>::max() / 4ULL) * 3ULL;
const uint32 kExpectedAverage = kTopOfRange / 2ULL;
const uint32 kAllowedVariance = kExpectedAverage / 50ULL;
const int kMinAttempts = 1000;
const int kMaxAttempts = 1000000;
for (size_t i = 0; i < arraysize(kTestTrialNames); ++i) {
const uint32 seed = HashName(kTestTrialNames[i]);
internal::SeededRandGenerator rand_generator(seed);
double cumulative_average = 0.0;
int count = 0;
while (count < kMaxAttempts) {
uint32 value = rand_generator(kTopOfRange);
cumulative_average = (count * cumulative_average + value) / (count + 1);
if (count > kMinAttempts &&
kExpectedAverage - kAllowedVariance < cumulative_average &&
cumulative_average < kExpectedAverage + kAllowedVariance) {
break;
}
++count;
}
ASSERT_LT(count, kMaxAttempts) << "Expected average was " <<
kExpectedAverage << ", average ended at " << cumulative_average <<
", for trial " << kTestTrialNames[i];
}
}
}