| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/variations/entropy_provider.h" | 5 #include "components/variations/entropy_provider.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <cmath> | 10 #include <cmath> |
| 11 #include <limits> | 11 #include <limits> |
| 12 #include <numeric> | 12 #include <numeric> |
| 13 | 13 |
| 14 #include "base/guid.h" | 14 #include "base/guid.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ptr_util.h" |
| 16 #include "base/rand_util.h" | 17 #include "base/rand_util.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "components/variations/metrics_util.h" | 19 #include "components/variations/metrics_util.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 namespace metrics { | 22 namespace metrics { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Size of the low entropy source to use for the permuted entropy provider | 26 // Size of the low entropy source to use for the permuted entropy provider |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 183 |
| 183 } // namespace | 184 } // namespace |
| 184 | 185 |
| 185 TEST(EntropyProviderTest, UseOneTimeRandomizationSHA1) { | 186 TEST(EntropyProviderTest, UseOneTimeRandomizationSHA1) { |
| 186 // Simply asserts that two trials using one-time randomization | 187 // Simply asserts that two trials using one-time randomization |
| 187 // that have different names, normally generate different results. | 188 // that have different names, normally generate different results. |
| 188 // | 189 // |
| 189 // Note that depending on the one-time random initialization, they | 190 // Note that depending on the one-time random initialization, they |
| 190 // _might_ actually give the same result, but we know that given | 191 // _might_ actually give the same result, but we know that given |
| 191 // the particular client_id we use for unit tests they won't. | 192 // the particular client_id we use for unit tests they won't. |
| 192 base::FieldTrialList field_trial_list(new SHA1EntropyProvider("client_id")); | 193 base::FieldTrialList field_trial_list( |
| 194 base::MakeUnique<SHA1EntropyProvider>("client_id")); |
| 193 const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear; | 195 const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear; |
| 194 scoped_refptr<base::FieldTrial> trials[] = { | 196 scoped_refptr<base::FieldTrial> trials[] = { |
| 195 base::FieldTrialList::FactoryGetFieldTrial( | 197 base::FieldTrialList::FactoryGetFieldTrial( |
| 196 "one", 100, "default", kNoExpirationYear, 1, 1, | 198 "one", 100, "default", kNoExpirationYear, 1, 1, |
| 197 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), | 199 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), |
| 198 base::FieldTrialList::FactoryGetFieldTrial( | 200 base::FieldTrialList::FactoryGetFieldTrial( |
| 199 "two", 100, "default", kNoExpirationYear, 1, 1, | 201 "two", 100, "default", kNoExpirationYear, 1, 1, |
| 200 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), | 202 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), |
| 201 }; | 203 }; |
| 202 | 204 |
| 203 for (size_t i = 0; i < arraysize(trials); ++i) { | 205 for (size_t i = 0; i < arraysize(trials); ++i) { |
| 204 for (int j = 0; j < 100; ++j) | 206 for (int j = 0; j < 100; ++j) |
| 205 trials[i]->AppendGroup(std::string(), 1); | 207 trials[i]->AppendGroup(std::string(), 1); |
| 206 } | 208 } |
| 207 | 209 |
| 208 // The trials are most likely to give different results since they have | 210 // The trials are most likely to give different results since they have |
| 209 // different names. | 211 // different names. |
| 210 EXPECT_NE(trials[0]->group(), trials[1]->group()); | 212 EXPECT_NE(trials[0]->group(), trials[1]->group()); |
| 211 EXPECT_NE(trials[0]->group_name(), trials[1]->group_name()); | 213 EXPECT_NE(trials[0]->group_name(), trials[1]->group_name()); |
| 212 } | 214 } |
| 213 | 215 |
| 214 TEST(EntropyProviderTest, UseOneTimeRandomizationPermuted) { | 216 TEST(EntropyProviderTest, UseOneTimeRandomizationPermuted) { |
| 215 // Simply asserts that two trials using one-time randomization | 217 // Simply asserts that two trials using one-time randomization |
| 216 // that have different names, normally generate different results. | 218 // that have different names, normally generate different results. |
| 217 // | 219 // |
| 218 // Note that depending on the one-time random initialization, they | 220 // Note that depending on the one-time random initialization, they |
| 219 // _might_ actually give the same result, but we know that given | 221 // _might_ actually give the same result, but we know that given |
| 220 // the particular client_id we use for unit tests they won't. | 222 // the particular client_id we use for unit tests they won't. |
| 221 base::FieldTrialList field_trial_list( | 223 base::FieldTrialList field_trial_list( |
| 222 new PermutedEntropyProvider(1234, kMaxLowEntropySize)); | 224 base::MakeUnique<PermutedEntropyProvider>(1234, kMaxLowEntropySize)); |
| 223 const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear; | 225 const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear; |
| 224 scoped_refptr<base::FieldTrial> trials[] = { | 226 scoped_refptr<base::FieldTrial> trials[] = { |
| 225 base::FieldTrialList::FactoryGetFieldTrial( | 227 base::FieldTrialList::FactoryGetFieldTrial( |
| 226 "one", 100, "default", kNoExpirationYear, 1, 1, | 228 "one", 100, "default", kNoExpirationYear, 1, 1, |
| 227 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), | 229 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), |
| 228 base::FieldTrialList::FactoryGetFieldTrial( | 230 base::FieldTrialList::FactoryGetFieldTrial( |
| 229 "two", 100, "default", kNoExpirationYear, 1, 1, | 231 "two", 100, "default", kNoExpirationYear, 1, 1, |
| 230 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), | 232 base::FieldTrial::ONE_TIME_RANDOMIZED, NULL), |
| 231 }; | 233 }; |
| 232 | 234 |
| 233 for (size_t i = 0; i < arraysize(trials); ++i) { | 235 for (size_t i = 0; i < arraysize(trials); ++i) { |
| 234 for (int j = 0; j < 100; ++j) | 236 for (int j = 0; j < 100; ++j) |
| 235 trials[i]->AppendGroup(std::string(), 1); | 237 trials[i]->AppendGroup(std::string(), 1); |
| 236 } | 238 } |
| 237 | 239 |
| 238 // The trials are most likely to give different results since they have | 240 // The trials are most likely to give different results since they have |
| 239 // different names. | 241 // different names. |
| 240 EXPECT_NE(trials[0]->group(), trials[1]->group()); | 242 EXPECT_NE(trials[0]->group(), trials[1]->group()); |
| 241 EXPECT_NE(trials[0]->group_name(), trials[1]->group_name()); | 243 EXPECT_NE(trials[0]->group_name(), trials[1]->group_name()); |
| 242 } | 244 } |
| 243 | 245 |
| 244 TEST(EntropyProviderTest, UseOneTimeRandomizationWithCustomSeedPermuted) { | 246 TEST(EntropyProviderTest, UseOneTimeRandomizationWithCustomSeedPermuted) { |
| 245 // Ensures that two trials with different names but the same custom seed used | 247 // Ensures that two trials with different names but the same custom seed used |
| 246 // for one time randomization produce the same group assignments. | 248 // for one time randomization produce the same group assignments. |
| 247 base::FieldTrialList field_trial_list( | 249 base::FieldTrialList field_trial_list( |
| 248 new PermutedEntropyProvider(1234, kMaxLowEntropySize)); | 250 base::MakeUnique<PermutedEntropyProvider>(1234, kMaxLowEntropySize)); |
| 249 const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear; | 251 const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear; |
| 250 const uint32_t kCustomSeed = 9001; | 252 const uint32_t kCustomSeed = 9001; |
| 251 scoped_refptr<base::FieldTrial> trials[] = { | 253 scoped_refptr<base::FieldTrial> trials[] = { |
| 252 base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( | 254 base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
| 253 "one", 100, "default", kNoExpirationYear, 1, 1, | 255 "one", 100, "default", kNoExpirationYear, 1, 1, |
| 254 base::FieldTrial::ONE_TIME_RANDOMIZED, kCustomSeed, NULL, NULL), | 256 base::FieldTrial::ONE_TIME_RANDOMIZED, kCustomSeed, NULL, NULL), |
| 255 base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( | 257 base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
| 256 "two", 100, "default", kNoExpirationYear, 1, 1, | 258 "two", 100, "default", kNoExpirationYear, 1, 1, |
| 257 base::FieldTrial::ONE_TIME_RANDOMIZED, kCustomSeed, NULL, NULL), | 259 base::FieldTrial::ONE_TIME_RANDOMIZED, kCustomSeed, NULL, NULL), |
| 258 }; | 260 }; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 ++count; | 363 ++count; |
| 362 } | 364 } |
| 363 | 365 |
| 364 ASSERT_LT(count, kMaxAttempts) << "Expected average was " << | 366 ASSERT_LT(count, kMaxAttempts) << "Expected average was " << |
| 365 kExpectedAverage << ", average ended at " << cumulative_average << | 367 kExpectedAverage << ", average ended at " << cumulative_average << |
| 366 ", for trial " << kTestTrialNames[i]; | 368 ", for trial " << kTestTrialNames[i]; |
| 367 } | 369 } |
| 368 } | 370 } |
| 369 | 371 |
| 370 } // namespace metrics | 372 } // namespace metrics |
| OLD | NEW |