[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 16a3091 | 2014-06-04 00:20:04 | [diff] [blame] | 5 | #include "components/metrics/metrics_state_manager.h" |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 6 | |
| 7 | #include <ctype.h> |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 10 | #include <string> |
dcheng | 5160635 | 2015-12-26 21:16:23 | [diff] [blame] | 11 | #include <utility> |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 12 | |
[email protected] | 3c70256f | 2014-05-22 03:02:12 | [diff] [blame] | 13 | #include "base/bind.h" |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 14 | #include "base/command_line.h" |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 15 | #include "base/macros.h" |
Ilya Sherman | 6c6c833 | 2017-07-11 22:39:22 | [diff] [blame] | 16 | #include "base/strings/string16.h" |
Paul Miller | 4355667 | 2018-12-19 07:12:58 | [diff] [blame^] | 17 | #include "base/strings/string_number_conversions.h" |
| 18 | #include "base/strings/string_util.h" |
Devlin Cronin | 69228f4 | 2018-06-01 17:25:10 | [diff] [blame] | 19 | #include "base/test/metrics/histogram_tester.h" |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 20 | #include "components/metrics/client_info.h" |
Mark Pearson | d4f91d11 | 2017-11-08 01:45:49 | [diff] [blame] | 21 | #include "components/metrics/metrics_log.h" |
[email protected] | 66d176a | 2014-05-22 13:49:39 | [diff] [blame] | 22 | #include "components/metrics/metrics_pref_names.h" |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 23 | #include "components/metrics/metrics_service.h" |
[email protected] | 16a3091 | 2014-06-04 00:20:04 | [diff] [blame] | 24 | #include "components/metrics/metrics_switches.h" |
jwd | a5d1883 | 2016-05-12 19:43:31 | [diff] [blame] | 25 | #include "components/metrics/test_enabled_state_provider.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 26 | #include "components/prefs/testing_pref_service.h" |
[email protected] | 88a7831f | 2014-05-19 15:19:13 | [diff] [blame] | 27 | #include "components/variations/caching_permuted_entropy_provider.h" |
| 28 | #include "components/variations/pref_names.h" |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 29 | #include "testing/gtest/include/gtest/gtest.h" |
| 30 | |
| 31 | namespace metrics { |
| 32 | |
| 33 | class MetricsStateManagerTest : public testing::Test { |
| 34 | public: |
jwd | a5d1883 | 2016-05-12 19:43:31 | [diff] [blame] | 35 | MetricsStateManagerTest() |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 36 | : test_begin_time_(base::Time::Now().ToTimeT()), |
| 37 | enabled_state_provider_(new TestEnabledStateProvider(false, false)) { |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 38 | MetricsService::RegisterPrefs(prefs_.registry()); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 39 | } |
| 40 | |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 41 | std::unique_ptr<MetricsStateManager> CreateStateManager() { |
[email protected] | 3c70256f | 2014-05-22 03:02:12 | [diff] [blame] | 42 | return MetricsStateManager::Create( |
Ilya Sherman | 6c6c833 | 2017-07-11 22:39:22 | [diff] [blame] | 43 | &prefs_, enabled_state_provider_.get(), base::string16(), |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 44 | base::Bind(&MetricsStateManagerTest::MockStoreClientInfoBackup, |
| 45 | base::Unretained(this)), |
| 46 | base::Bind(&MetricsStateManagerTest::LoadFakeClientInfoBackup, |
dcheng | 5160635 | 2015-12-26 21:16:23 | [diff] [blame] | 47 | base::Unretained(this))); |
[email protected] | 3c70256f | 2014-05-22 03:02:12 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // Sets metrics reporting as enabled for testing. |
| 51 | void EnableMetricsReporting() { |
jwd | a5d1883 | 2016-05-12 19:43:31 | [diff] [blame] | 52 | enabled_state_provider_->set_consent(true); |
| 53 | enabled_state_provider_->set_enabled(true); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 54 | } |
| 55 | |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 56 | void SetClientInfoPrefs(const ClientInfo& client_info) { |
| 57 | prefs_.SetString(prefs::kMetricsClientID, client_info.client_id); |
| 58 | prefs_.SetInt64(prefs::kInstallDate, client_info.installation_date); |
| 59 | prefs_.SetInt64(prefs::kMetricsReportingEnabledTimestamp, |
| 60 | client_info.reporting_enabled_date); |
| 61 | } |
| 62 | |
| 63 | void SetFakeClientInfoBackup(const ClientInfo& client_info) { |
| 64 | fake_client_info_backup_.reset(new ClientInfo); |
| 65 | fake_client_info_backup_->client_id = client_info.client_id; |
| 66 | fake_client_info_backup_->installation_date = client_info.installation_date; |
| 67 | fake_client_info_backup_->reporting_enabled_date = |
| 68 | client_info.reporting_enabled_date; |
| 69 | } |
| 70 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 71 | protected: |
| 72 | TestingPrefServiceSimple prefs_; |
| 73 | |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 74 | // Last ClientInfo stored by the MetricsStateManager via |
| 75 | // MockStoreClientInfoBackup. |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 76 | std::unique_ptr<ClientInfo> stored_client_info_backup_; |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 77 | |
| 78 | // If set, will be returned via LoadFakeClientInfoBackup if requested by the |
| 79 | // MetricsStateManager. |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 80 | std::unique_ptr<ClientInfo> fake_client_info_backup_; |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 81 | |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 82 | const int64_t test_begin_time_; |
| 83 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 84 | private: |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 85 | // Stores the |client_info| in |stored_client_info_backup_| for verification |
| 86 | // by the tests later. |
| 87 | void MockStoreClientInfoBackup(const ClientInfo& client_info) { |
| 88 | stored_client_info_backup_.reset(new ClientInfo); |
| 89 | stored_client_info_backup_->client_id = client_info.client_id; |
| 90 | stored_client_info_backup_->installation_date = |
| 91 | client_info.installation_date; |
| 92 | stored_client_info_backup_->reporting_enabled_date = |
| 93 | client_info.reporting_enabled_date; |
| 94 | |
| 95 | // Respect the contract that storing an empty client_id voids the existing |
| 96 | // backup (required for the last section of the ForceClientIdCreation test |
| 97 | // below). |
| 98 | if (client_info.client_id.empty()) |
| 99 | fake_client_info_backup_.reset(); |
| 100 | } |
| 101 | |
| 102 | // Hands out a copy of |fake_client_info_backup_| if it is set. |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 103 | std::unique_ptr<ClientInfo> LoadFakeClientInfoBackup() { |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 104 | if (!fake_client_info_backup_) |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 105 | return std::unique_ptr<ClientInfo>(); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 106 | |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 107 | std::unique_ptr<ClientInfo> backup_copy(new ClientInfo); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 108 | backup_copy->client_id = fake_client_info_backup_->client_id; |
| 109 | backup_copy->installation_date = |
| 110 | fake_client_info_backup_->installation_date; |
| 111 | backup_copy->reporting_enabled_date = |
| 112 | fake_client_info_backup_->reporting_enabled_date; |
dcheng | 5160635 | 2015-12-26 21:16:23 | [diff] [blame] | 113 | return backup_copy; |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 114 | } |
| 115 | |
jwd | a5d1883 | 2016-05-12 19:43:31 | [diff] [blame] | 116 | std::unique_ptr<TestEnabledStateProvider> enabled_state_provider_; |
[email protected] | 3c70256f | 2014-05-22 03:02:12 | [diff] [blame] | 117 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 118 | DISALLOW_COPY_AND_ASSIGN(MetricsStateManagerTest); |
| 119 | }; |
| 120 | |
| 121 | // Ensure the ClientId is formatted as expected. |
| 122 | TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted) { |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 123 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 124 | state_manager->ForceClientIdCreation(); |
| 125 | |
| 126 | const std::string client_id = state_manager->client_id(); |
| 127 | EXPECT_EQ(36U, client_id.length()); |
| 128 | |
| 129 | for (size_t i = 0; i < client_id.length(); ++i) { |
| 130 | char current = client_id[i]; |
| 131 | if (i == 8 || i == 13 || i == 18 || i == 23) |
| 132 | EXPECT_EQ('-', current); |
| 133 | else |
| 134 | EXPECT_TRUE(isxdigit(current)); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | TEST_F(MetricsStateManagerTest, EntropySourceUsed_Low) { |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 139 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
jwd | 67c08f75 | 2016-05-18 21:04:59 | [diff] [blame] | 140 | state_manager->CreateDefaultEntropyProvider(); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 141 | EXPECT_EQ(MetricsStateManager::ENTROPY_SOURCE_LOW, |
| 142 | state_manager->entropy_source_returned()); |
| 143 | } |
| 144 | |
| 145 | TEST_F(MetricsStateManagerTest, EntropySourceUsed_High) { |
[email protected] | 3c70256f | 2014-05-22 03:02:12 | [diff] [blame] | 146 | EnableMetricsReporting(); |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 147 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
jwd | 67c08f75 | 2016-05-18 21:04:59 | [diff] [blame] | 148 | state_manager->CreateDefaultEntropyProvider(); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 149 | EXPECT_EQ(MetricsStateManager::ENTROPY_SOURCE_HIGH, |
| 150 | state_manager->entropy_source_returned()); |
| 151 | } |
| 152 | |
| 153 | TEST_F(MetricsStateManagerTest, LowEntropySource0NotReset) { |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 154 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 155 | |
| 156 | // Get the low entropy source once, to initialize it. |
| 157 | state_manager->GetLowEntropySource(); |
| 158 | |
| 159 | // Now, set it to 0 and ensure it doesn't get reset. |
| 160 | state_manager->low_entropy_source_ = 0; |
| 161 | EXPECT_EQ(0, state_manager->GetLowEntropySource()); |
| 162 | // Call it another time, just to make sure. |
| 163 | EXPECT_EQ(0, state_manager->GetLowEntropySource()); |
| 164 | } |
| 165 | |
Paul Miller | 4355667 | 2018-12-19 07:12:58 | [diff] [blame^] | 166 | TEST_F(MetricsStateManagerTest, HaveNoLowEntropySource) { |
| 167 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 168 | state_manager->client_id_ = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF"; |
| 169 | // If we have neither the new nor old low entropy sources in prefs, then the |
| 170 | // new source should be created... |
| 171 | int new_low_source = state_manager->GetLowEntropySource(); |
| 172 | EXPECT_TRUE(MetricsStateManager::IsValidLowEntropySource(new_low_source)) |
| 173 | << new_low_source; |
| 174 | // ...but the old source should not... |
| 175 | EXPECT_EQ(MetricsStateManager::kLowEntropySourceNotSet, |
| 176 | state_manager->GetOldLowEntropySource()); |
| 177 | // ...and the high entropy source should include the *new* low entropy source. |
| 178 | std::string high_source = state_manager->GetHighEntropySource(); |
| 179 | EXPECT_TRUE(base::EndsWith(high_source, base::IntToString(new_low_source), |
| 180 | base::CompareCase::SENSITIVE)) |
| 181 | << high_source; |
| 182 | } |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 183 | |
Paul Miller | 4355667 | 2018-12-19 07:12:58 | [diff] [blame^] | 184 | TEST_F(MetricsStateManagerTest, HaveOnlyNewLowEntropySource) { |
| 185 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 186 | state_manager->client_id_ = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF"; |
| 187 | // If we have the new low entropy sources in prefs, but not the old one... |
| 188 | const int new_low_source = 1234; |
| 189 | prefs_.SetInteger(prefs::kMetricsLowEntropySource, new_low_source); |
| 190 | // ...then the new source should be loaded... |
| 191 | EXPECT_EQ(new_low_source, state_manager->GetLowEntropySource()); |
| 192 | // ...but the old source should not be created... |
| 193 | EXPECT_EQ(MetricsStateManager::kLowEntropySourceNotSet, |
| 194 | state_manager->GetOldLowEntropySource()); |
| 195 | // ...and the high entropy source should include the *new* low entropy source. |
| 196 | std::string high_source = state_manager->GetHighEntropySource(); |
| 197 | EXPECT_TRUE(base::EndsWith(high_source, base::IntToString(new_low_source), |
| 198 | base::CompareCase::SENSITIVE)) |
| 199 | << high_source; |
| 200 | } |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 201 | |
Paul Miller | 4355667 | 2018-12-19 07:12:58 | [diff] [blame^] | 202 | TEST_F(MetricsStateManagerTest, HaveOnlyOldLowEntropySource) { |
| 203 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 204 | state_manager->client_id_ = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF"; |
| 205 | // If we have the old low entropy sources in prefs, but not the new one... |
| 206 | const int old_low_source = 5678; |
| 207 | prefs_.SetInteger(prefs::kMetricsOldLowEntropySource, old_low_source); |
| 208 | // ...then the new source should be created... |
| 209 | int new_low_source = state_manager->GetLowEntropySource(); |
| 210 | EXPECT_TRUE(MetricsStateManager::IsValidLowEntropySource(new_low_source)) |
| 211 | << new_low_source; |
| 212 | // ...and the old source should be loaded... |
| 213 | EXPECT_EQ(old_low_source, state_manager->GetOldLowEntropySource()); |
| 214 | // ...and the high entropy source should include the *old* low entropy source. |
| 215 | std::string high_source = state_manager->GetHighEntropySource(); |
| 216 | EXPECT_TRUE(base::EndsWith(high_source, base::IntToString(old_low_source), |
| 217 | base::CompareCase::SENSITIVE)) |
| 218 | << high_source; |
| 219 | } |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 220 | |
Paul Miller | 4355667 | 2018-12-19 07:12:58 | [diff] [blame^] | 221 | TEST_F(MetricsStateManagerTest, HaveBothLowEntropySources) { |
| 222 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 223 | state_manager->client_id_ = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF"; |
| 224 | // If we have the new and old low entropy sources in prefs... |
| 225 | const int new_low_source = 1234; |
| 226 | const int old_low_source = 5678; |
| 227 | prefs_.SetInteger(prefs::kMetricsLowEntropySource, new_low_source); |
| 228 | prefs_.SetInteger(prefs::kMetricsOldLowEntropySource, old_low_source); |
| 229 | // ...then both should be loaded... |
| 230 | EXPECT_EQ(new_low_source, state_manager->GetLowEntropySource()); |
| 231 | EXPECT_EQ(old_low_source, state_manager->GetOldLowEntropySource()); |
| 232 | // ...and the high entropy source should include the *old* low entropy source. |
| 233 | std::string high_source = state_manager->GetHighEntropySource(); |
| 234 | EXPECT_TRUE(base::EndsWith(high_source, base::IntToString(old_low_source), |
| 235 | base::CompareCase::SENSITIVE)) |
| 236 | << high_source; |
| 237 | } |
| 238 | |
| 239 | TEST_F(MetricsStateManagerTest, CorruptNewLowEntropySources) { |
| 240 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 241 | const int corrupt_sources[] = {-12345, -1, 8000, 12345}; |
| 242 | for (int corrupt_source : corrupt_sources) { |
| 243 | // If the new low entropy source has been corrupted... |
| 244 | EXPECT_FALSE(MetricsStateManager::IsValidLowEntropySource(corrupt_source)) |
| 245 | << corrupt_source; |
| 246 | prefs_.SetInteger(prefs::kMetricsLowEntropySource, corrupt_source); |
| 247 | // ...then a new source should be created. |
| 248 | int loaded_source = state_manager->GetLowEntropySource(); |
| 249 | EXPECT_TRUE(MetricsStateManager::IsValidLowEntropySource(loaded_source)) |
| 250 | << loaded_source; |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 251 | } |
Paul Miller | 4355667 | 2018-12-19 07:12:58 | [diff] [blame^] | 252 | } |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 253 | |
Paul Miller | 4355667 | 2018-12-19 07:12:58 | [diff] [blame^] | 254 | TEST_F(MetricsStateManagerTest, CorruptOldLowEntropySources) { |
| 255 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 256 | const int corrupt_sources[] = {-12345, -1, 8000, 12345}; |
| 257 | for (int corrupt_source : corrupt_sources) { |
| 258 | // If the old low entropy source has been corrupted... |
| 259 | EXPECT_FALSE(MetricsStateManager::IsValidLowEntropySource(corrupt_source)) |
| 260 | << corrupt_source; |
| 261 | prefs_.SetInteger(prefs::kMetricsOldLowEntropySource, corrupt_source); |
| 262 | // ...then it should be ignored. |
| 263 | EXPECT_EQ(MetricsStateManager::kLowEntropySourceNotSet, |
| 264 | state_manager->GetOldLowEntropySource()); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | // Check that setting the kMetricsResetIds pref to true causes the client id to |
| 269 | // be reset. We do not check that the low entropy source is reset because we |
| 270 | // cannot ensure that metrics state manager won't generate the same id again. |
| 271 | TEST_F(MetricsStateManagerTest, ResetMetricsIDs) { |
| 272 | // Set an initial client id in prefs. It should not be possible for the |
| 273 | // metrics state manager to generate this id randomly. |
| 274 | const std::string kInitialClientId = "initial client id"; |
[email protected] | 16a3091 | 2014-06-04 00:20:04 | [diff] [blame] | 275 | prefs_.SetString(prefs::kMetricsClientID, kInitialClientId); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 276 | |
| 277 | // Make sure the initial client id isn't reset by the metrics state manager. |
| 278 | { |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 279 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 280 | state_manager->ForceClientIdCreation(); |
| 281 | EXPECT_EQ(kInitialClientId, state_manager->client_id()); |
Mark Pearson | d4f91d11 | 2017-11-08 01:45:49 | [diff] [blame] | 282 | EXPECT_FALSE(state_manager->metrics_ids_were_reset_); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // Set the reset pref to cause the IDs to be reset. |
[email protected] | 66d176a | 2014-05-22 13:49:39 | [diff] [blame] | 286 | prefs_.SetBoolean(prefs::kMetricsResetIds, true); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 287 | |
| 288 | // Cause the actual reset to happen. |
| 289 | { |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 290 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 291 | state_manager->ForceClientIdCreation(); |
| 292 | EXPECT_NE(kInitialClientId, state_manager->client_id()); |
Mark Pearson | d4f91d11 | 2017-11-08 01:45:49 | [diff] [blame] | 293 | EXPECT_TRUE(state_manager->metrics_ids_were_reset_); |
| 294 | EXPECT_EQ(kInitialClientId, state_manager->previous_client_id_); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 295 | |
| 296 | state_manager->GetLowEntropySource(); |
| 297 | |
[email protected] | 66d176a | 2014-05-22 13:49:39 | [diff] [blame] | 298 | EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds)); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 299 | } |
| 300 | |
[email protected] | 16a3091 | 2014-06-04 00:20:04 | [diff] [blame] | 301 | EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID)); |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 302 | } |
| 303 | |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 304 | TEST_F(MetricsStateManagerTest, ForceClientIdCreation) { |
avi | 2606292 | 2015-12-26 00:14:18 | [diff] [blame] | 305 | const int64_t kFakeInstallationDate = 12345; |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 306 | prefs_.SetInt64(prefs::kInstallDate, kFakeInstallationDate); |
| 307 | |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 308 | { |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 309 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 310 | |
| 311 | // client_id shouldn't be auto-generated if metrics reporting is not |
| 312 | // enabled. |
| 313 | EXPECT_EQ(std::string(), state_manager->client_id()); |
| 314 | EXPECT_EQ(0, prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp)); |
| 315 | |
| 316 | // Confirm that the initial ForceClientIdCreation call creates the client id |
| 317 | // and backs it up via MockStoreClientInfoBackup. |
| 318 | EXPECT_FALSE(stored_client_info_backup_); |
| 319 | state_manager->ForceClientIdCreation(); |
| 320 | EXPECT_NE(std::string(), state_manager->client_id()); |
[email protected] | 986ee1a7 | 2014-08-05 23:03:32 | [diff] [blame] | 321 | EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 322 | test_begin_time_); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 323 | |
| 324 | ASSERT_TRUE(stored_client_info_backup_); |
| 325 | EXPECT_EQ(state_manager->client_id(), |
| 326 | stored_client_info_backup_->client_id); |
| 327 | EXPECT_EQ(kFakeInstallationDate, |
| 328 | stored_client_info_backup_->installation_date); |
| 329 | EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), |
| 330 | stored_client_info_backup_->reporting_enabled_date); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 331 | } |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | TEST_F(MetricsStateManagerTest, LoadPrefs) { |
| 335 | ClientInfo client_info; |
| 336 | client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF"; |
| 337 | client_info.installation_date = 1112; |
| 338 | client_info.reporting_enabled_date = 2223; |
| 339 | SetClientInfoPrefs(client_info); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 340 | |
| 341 | EnableMetricsReporting(); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 342 | { |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 343 | EXPECT_FALSE(fake_client_info_backup_); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 344 | EXPECT_FALSE(stored_client_info_backup_); |
| 345 | |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 346 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 347 | |
| 348 | // client_id should be auto-obtained from the constructor when metrics |
| 349 | // reporting is enabled. |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 350 | EXPECT_EQ(client_info.client_id, state_manager->client_id()); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 351 | |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 352 | // The backup should not be modified. |
| 353 | ASSERT_FALSE(stored_client_info_backup_); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 354 | |
| 355 | // Re-forcing client id creation shouldn't cause another backup and |
| 356 | // shouldn't affect the existing client id. |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 357 | state_manager->ForceClientIdCreation(); |
| 358 | EXPECT_FALSE(stored_client_info_backup_); |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 359 | EXPECT_EQ(client_info.client_id, state_manager->client_id()); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 360 | } |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 361 | } |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 362 | |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 363 | TEST_F(MetricsStateManagerTest, PreferPrefs) { |
| 364 | ClientInfo client_info; |
| 365 | client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF"; |
| 366 | client_info.installation_date = 1112; |
| 367 | client_info.reporting_enabled_date = 2223; |
| 368 | SetClientInfoPrefs(client_info); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 369 | |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 370 | ClientInfo client_info2; |
| 371 | client_info2.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"; |
| 372 | client_info2.installation_date = 1111; |
| 373 | client_info2.reporting_enabled_date = 2222; |
| 374 | SetFakeClientInfoBackup(client_info2); |
| 375 | |
| 376 | EnableMetricsReporting(); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 377 | { |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 378 | // The backup should be ignored if we already have a client id. |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 379 | |
| 380 | EXPECT_FALSE(stored_client_info_backup_); |
| 381 | |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 382 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 383 | EXPECT_EQ(client_info.client_id, state_manager->client_id()); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 384 | |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 385 | // The backup should not be modified. |
| 386 | ASSERT_FALSE(stored_client_info_backup_); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 387 | } |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | TEST_F(MetricsStateManagerTest, RestoreBackup) { |
| 391 | ClientInfo client_info; |
| 392 | client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF"; |
| 393 | client_info.installation_date = 1112; |
| 394 | client_info.reporting_enabled_date = 2223; |
| 395 | SetClientInfoPrefs(client_info); |
| 396 | |
| 397 | ClientInfo client_info2; |
| 398 | client_info2.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"; |
| 399 | client_info2.installation_date = 1111; |
| 400 | client_info2.reporting_enabled_date = 2222; |
| 401 | SetFakeClientInfoBackup(client_info2); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 402 | |
| 403 | prefs_.ClearPref(prefs::kMetricsClientID); |
| 404 | prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp); |
| 405 | |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 406 | EnableMetricsReporting(); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 407 | { |
| 408 | // The backup should kick in if the client id has gone missing. It should |
| 409 | // replace remaining and missing dates as well. |
| 410 | |
| 411 | EXPECT_FALSE(stored_client_info_backup_); |
| 412 | |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 413 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 414 | EXPECT_EQ(client_info2.client_id, state_manager->client_id()); |
| 415 | EXPECT_EQ(client_info2.installation_date, |
| 416 | prefs_.GetInt64(prefs::kInstallDate)); |
| 417 | EXPECT_EQ(client_info2.reporting_enabled_date, |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 418 | prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp)); |
| 419 | |
| 420 | EXPECT_TRUE(stored_client_info_backup_); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 421 | } |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 422 | } |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 423 | |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 424 | TEST_F(MetricsStateManagerTest, ResetBackup) { |
| 425 | ClientInfo client_info; |
| 426 | client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"; |
| 427 | client_info.installation_date = 1111; |
| 428 | client_info.reporting_enabled_date = 2222; |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 429 | |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 430 | SetFakeClientInfoBackup(client_info); |
| 431 | SetClientInfoPrefs(client_info); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 432 | |
| 433 | prefs_.SetBoolean(prefs::kMetricsResetIds, true); |
| 434 | |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 435 | EnableMetricsReporting(); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 436 | { |
| 437 | // Upon request to reset metrics ids, the existing backup should not be |
| 438 | // restored. |
| 439 | |
dcheng | d99c42a | 2016-04-21 21:54:13 | [diff] [blame] | 440 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 441 | |
| 442 | // A brand new client id should have been generated. |
| 443 | EXPECT_NE(std::string(), state_manager->client_id()); |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 444 | EXPECT_NE(client_info.client_id, state_manager->client_id()); |
Mark Pearson | d4f91d11 | 2017-11-08 01:45:49 | [diff] [blame] | 445 | EXPECT_TRUE(state_manager->metrics_ids_were_reset_); |
| 446 | EXPECT_EQ(client_info.client_id, state_manager->previous_client_id_); |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 447 | EXPECT_TRUE(stored_client_info_backup_); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 448 | |
[email protected] | 986ee1a7 | 2014-08-05 23:03:32 | [diff] [blame] | 449 | // The installation date should not have been affected. |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 450 | EXPECT_EQ(client_info.installation_date, |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 451 | prefs_.GetInt64(prefs::kInstallDate)); |
[email protected] | 986ee1a7 | 2014-08-05 23:03:32 | [diff] [blame] | 452 | |
| 453 | // The metrics-reporting-enabled date will be reset to Now(). |
| 454 | EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), |
holte | 17f4b3fb | 2017-03-16 02:24:44 | [diff] [blame] | 455 | test_begin_time_); |
[email protected] | 8e885de | 2014-07-22 23:36:53 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 459 | TEST_F(MetricsStateManagerTest, CheckProvider) { |
| 460 | int64_t kInstallDate = 1373051956; |
| 461 | int64_t kInstallDateExpected = 1373050800; // Computed from kInstallDate. |
| 462 | int64_t kEnabledDate = 1373001211; |
| 463 | int64_t kEnabledDateExpected = 1373000400; // Computed from kEnabledDate. |
| 464 | |
| 465 | ClientInfo client_info; |
| 466 | client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"; |
| 467 | client_info.installation_date = kInstallDate; |
| 468 | client_info.reporting_enabled_date = kEnabledDate; |
| 469 | |
| 470 | SetFakeClientInfoBackup(client_info); |
| 471 | SetClientInfoPrefs(client_info); |
| 472 | |
| 473 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 474 | std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider(); |
| 475 | SystemProfileProto system_profile; |
| 476 | provider->ProvideSystemProfileMetrics(&system_profile); |
| 477 | EXPECT_EQ(system_profile.install_date(), kInstallDateExpected); |
| 478 | EXPECT_EQ(system_profile.uma_enabled_date(), kEnabledDateExpected); |
Mark Pearson | d4f91d11 | 2017-11-08 01:45:49 | [diff] [blame] | 479 | |
| 480 | base::HistogramTester histogram_tester; |
| 481 | ChromeUserMetricsExtension uma_proto; |
| 482 | provider->ProvidePreviousSessionData(&uma_proto); |
| 483 | // The client_id field in the proto should not be overwritten. |
| 484 | EXPECT_FALSE(uma_proto.has_client_id()); |
| 485 | // Nothing should have been emitted to the cloned install histogram. |
| 486 | histogram_tester.ExpectTotalCount("UMA.IsClonedInstall", 0); |
| 487 | } |
| 488 | |
| 489 | TEST_F(MetricsStateManagerTest, CheckProviderResetIds) { |
| 490 | int64_t kInstallDate = 1373051956; |
| 491 | int64_t kInstallDateExpected = 1373050800; // Computed from kInstallDate. |
| 492 | int64_t kEnabledDate = 1373001211; |
| 493 | int64_t kEnabledDateExpected = 1373000400; // Computed from kEnabledDate. |
| 494 | |
| 495 | ClientInfo client_info; |
| 496 | client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"; |
| 497 | client_info.installation_date = kInstallDate; |
| 498 | client_info.reporting_enabled_date = kEnabledDate; |
| 499 | |
| 500 | SetFakeClientInfoBackup(client_info); |
| 501 | SetClientInfoPrefs(client_info); |
| 502 | |
| 503 | // Set the reset pref to cause the IDs to be reset. |
| 504 | prefs_.SetBoolean(prefs::kMetricsResetIds, true); |
| 505 | std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager()); |
| 506 | EXPECT_NE(client_info.client_id, state_manager->client_id()); |
| 507 | EXPECT_TRUE(state_manager->metrics_ids_were_reset_); |
| 508 | EXPECT_EQ(client_info.client_id, state_manager->previous_client_id_); |
| 509 | |
| 510 | std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider(); |
| 511 | SystemProfileProto system_profile; |
| 512 | provider->ProvideSystemProfileMetrics(&system_profile); |
| 513 | EXPECT_EQ(system_profile.install_date(), kInstallDateExpected); |
| 514 | EXPECT_EQ(system_profile.uma_enabled_date(), kEnabledDateExpected); |
| 515 | |
| 516 | base::HistogramTester histogram_tester; |
| 517 | ChromeUserMetricsExtension uma_proto; |
| 518 | provider->ProvidePreviousSessionData(&uma_proto); |
| 519 | EXPECT_EQ(MetricsLog::Hash(state_manager->previous_client_id_), |
| 520 | uma_proto.client_id()); |
| 521 | histogram_tester.ExpectUniqueSample("UMA.IsClonedInstall", 1, 1); |
Steven Holte | 8e9db0ca | 2017-08-11 01:20:08 | [diff] [blame] | 522 | } |
| 523 | |
[email protected] | 3907664 | 2014-05-05 20:32:55 | [diff] [blame] | 524 | } // namespace metrics |