| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/metrics/metrics_state_manager.h" | 5 #include "chrome/browser/metrics/metrics_state_manager.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/prefs/testing_pref_service.h" | 11 #include "base/prefs/testing_pref_service.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/metrics/caching_permuted_entropy_provider.h" | |
| 14 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 14 #include "components/variations/caching_permuted_entropy_provider.h" |
| 15 #include "components/variations/pref_names.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace metrics { | 18 namespace metrics { |
| 18 | 19 |
| 19 class MetricsStateManagerTest : public testing::Test { | 20 class MetricsStateManagerTest : public testing::Test { |
| 20 public: | 21 public: |
| 21 MetricsStateManagerTest() { | 22 MetricsStateManagerTest() { |
| 22 MetricsStateManager::RegisterPrefs(prefs_.registry()); | 23 MetricsStateManager::RegisterPrefs(prefs_.registry()); |
| 23 } | 24 } |
| 24 | 25 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 // Now, set it to 0 and ensure it doesn't get reset. | 77 // Now, set it to 0 and ensure it doesn't get reset. |
| 77 state_manager->low_entropy_source_ = 0; | 78 state_manager->low_entropy_source_ = 0; |
| 78 EXPECT_EQ(0, state_manager->GetLowEntropySource()); | 79 EXPECT_EQ(0, state_manager->GetLowEntropySource()); |
| 79 // Call it another time, just to make sure. | 80 // Call it another time, just to make sure. |
| 80 EXPECT_EQ(0, state_manager->GetLowEntropySource()); | 81 EXPECT_EQ(0, state_manager->GetLowEntropySource()); |
| 81 } | 82 } |
| 82 | 83 |
| 83 TEST_F(MetricsStateManagerTest, | 84 TEST_F(MetricsStateManagerTest, |
| 84 PermutedEntropyCacheClearedWhenLowEntropyReset) { | 85 PermutedEntropyCacheClearedWhenLowEntropyReset) { |
| 85 const PrefService::Preference* low_entropy_pref = | 86 const PrefService::Preference* low_entropy_pref = |
| 86 prefs_.FindPreference(prefs::kMetricsLowEntropySource); | 87 prefs_.FindPreference(::prefs::kMetricsLowEntropySource); |
| 87 const char* kCachePrefName = prefs::kMetricsPermutedEntropyCache; | 88 const char* kCachePrefName = prefs::kVariationsPermutedEntropyCache; |
| 88 int low_entropy_value = -1; | 89 int low_entropy_value = -1; |
| 89 | 90 |
| 90 // First, generate an initial low entropy source value. | 91 // First, generate an initial low entropy source value. |
| 91 { | 92 { |
| 92 EXPECT_TRUE(low_entropy_pref->IsDefaultValue()); | 93 EXPECT_TRUE(low_entropy_pref->IsDefaultValue()); |
| 93 | 94 |
| 94 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 95 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 95 state_manager->GetLowEntropySource(); | 96 state_manager->GetLowEntropySource(); |
| 96 | 97 |
| 97 EXPECT_FALSE(low_entropy_pref->IsDefaultValue()); | 98 EXPECT_FALSE(low_entropy_pref->IsDefaultValue()); |
| 98 EXPECT_TRUE(low_entropy_pref->GetValue()->GetAsInteger(&low_entropy_value)); | 99 EXPECT_TRUE(low_entropy_pref->GetValue()->GetAsInteger(&low_entropy_value)); |
| 99 } | 100 } |
| 100 | 101 |
| 101 // Now, set a dummy value in the permuted entropy cache pref and verify that | 102 // Now, set a dummy value in the permuted entropy cache pref and verify that |
| 102 // another call to GetLowEntropySource() doesn't clobber it when | 103 // another call to GetLowEntropySource() doesn't clobber it when |
| 103 // --reset-variation-state wasn't specified. | 104 // --reset-variation-state wasn't specified. |
| 104 { | 105 { |
| 105 prefs_.SetString(kCachePrefName, "test"); | 106 prefs_.SetString(kCachePrefName, "test"); |
| 106 | 107 |
| 107 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 108 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 108 state_manager->GetLowEntropySource(); | 109 state_manager->GetLowEntropySource(); |
| 109 | 110 |
| 110 EXPECT_EQ("test", prefs_.GetString(kCachePrefName)); | 111 EXPECT_EQ("test", prefs_.GetString(kCachePrefName)); |
| 111 EXPECT_EQ(low_entropy_value, | 112 EXPECT_EQ(low_entropy_value, |
| 112 prefs_.GetInteger(prefs::kMetricsLowEntropySource)); | 113 prefs_.GetInteger(::prefs::kMetricsLowEntropySource)); |
| 113 } | 114 } |
| 114 | 115 |
| 115 // Verify that the cache does get reset if --reset-variations-state is passed. | 116 // Verify that the cache does get reset if --reset-variations-state is passed. |
| 116 { | 117 { |
| 117 CommandLine::ForCurrentProcess()->AppendSwitch( | 118 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 118 switches::kResetVariationState); | 119 switches::kResetVariationState); |
| 119 | 120 |
| 120 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 121 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 121 state_manager->GetLowEntropySource(); | 122 state_manager->GetLowEntropySource(); |
| 122 | 123 |
| 123 EXPECT_TRUE(prefs_.GetString(kCachePrefName).empty()); | 124 EXPECT_TRUE(prefs_.GetString(kCachePrefName).empty()); |
| 124 } | 125 } |
| 125 } | 126 } |
| 126 | 127 |
| 127 // Check that setting the kMetricsResetIds pref to true causes the client id to | 128 // Check that setting the kMetricsResetIds pref to true causes the client id to |
| 128 // be reset. We do not check that the low entropy source is reset because we | 129 // be reset. We do not check that the low entropy source is reset because we |
| 129 // cannot ensure that metrics state manager won't generate the same id again. | 130 // cannot ensure that metrics state manager won't generate the same id again. |
| 130 TEST_F(MetricsStateManagerTest, ResetMetricsIDs) { | 131 TEST_F(MetricsStateManagerTest, ResetMetricsIDs) { |
| 131 // Set an initial client id in prefs. It should not be possible for the | 132 // Set an initial client id in prefs. It should not be possible for the |
| 132 // metrics state manager to generate this id randomly. | 133 // metrics state manager to generate this id randomly. |
| 133 const std::string kInitialClientId = "initial client id"; | 134 const std::string kInitialClientId = "initial client id"; |
| 134 prefs_.SetString(prefs::kMetricsClientID, kInitialClientId); | 135 prefs_.SetString(::prefs::kMetricsClientID, kInitialClientId); |
| 135 | 136 |
| 136 // Make sure the initial client id isn't reset by the metrics state manager. | 137 // Make sure the initial client id isn't reset by the metrics state manager. |
| 137 { | 138 { |
| 138 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 139 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 139 state_manager->ForceClientIdCreation(); | 140 state_manager->ForceClientIdCreation(); |
| 140 EXPECT_EQ(kInitialClientId, state_manager->client_id()); | 141 EXPECT_EQ(kInitialClientId, state_manager->client_id()); |
| 141 } | 142 } |
| 142 | 143 |
| 143 // Set the reset pref to cause the IDs to be reset. | 144 // Set the reset pref to cause the IDs to be reset. |
| 144 prefs_.SetBoolean(prefs::kMetricsResetIds, true); | 145 prefs_.SetBoolean(::prefs::kMetricsResetIds, true); |
| 145 | 146 |
| 146 // Cause the actual reset to happen. | 147 // Cause the actual reset to happen. |
| 147 { | 148 { |
| 148 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); | 149 scoped_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 149 state_manager->ForceClientIdCreation(); | 150 state_manager->ForceClientIdCreation(); |
| 150 EXPECT_NE(kInitialClientId, state_manager->client_id()); | 151 EXPECT_NE(kInitialClientId, state_manager->client_id()); |
| 151 | 152 |
| 152 state_manager->GetLowEntropySource(); | 153 state_manager->GetLowEntropySource(); |
| 153 | 154 |
| 154 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds)); | 155 EXPECT_FALSE(prefs_.GetBoolean(::prefs::kMetricsResetIds)); |
| 155 } | 156 } |
| 156 | 157 |
| 157 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID)); | 158 EXPECT_NE(kInitialClientId, prefs_.GetString(::prefs::kMetricsClientID)); |
| 158 } | 159 } |
| 159 | 160 |
| 160 } // namespace metrics | 161 } // namespace metrics |
| OLD | NEW |