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