blob: a17d50879e609de79aed8de8676ca04bda30e04c [file] [log] [blame]
[email protected]39076642014-05-05 20:32:551// 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]16a30912014-06-04 00:20:045#include "components/metrics/metrics_state_manager.h"
[email protected]39076642014-05-05 20:32:556
7#include <ctype.h>
avi26062922015-12-26 00:14:188#include <stddef.h>
9#include <stdint.h>
[email protected]39076642014-05-05 20:32:5510#include <string>
dcheng51606352015-12-26 21:16:2311#include <utility>
[email protected]39076642014-05-05 20:32:5512
[email protected]3c70256f2014-05-22 03:02:1213#include "base/bind.h"
[email protected]39076642014-05-05 20:32:5514#include "base/command_line.h"
avi26062922015-12-26 00:14:1815#include "base/macros.h"
Ilya Sherman6c6c8332017-07-11 22:39:2216#include "base/strings/string16.h"
Paul Miller43556672018-12-19 07:12:5817#include "base/strings/string_number_conversions.h"
18#include "base/strings/string_util.h"
Devlin Cronin69228f42018-06-01 17:25:1019#include "base/test/metrics/histogram_tester.h"
[email protected]8e885de2014-07-22 23:36:5320#include "components/metrics/client_info.h"
Mark Pearsond4f91d112017-11-08 01:45:4921#include "components/metrics/metrics_log.h"
[email protected]66d176a2014-05-22 13:49:3922#include "components/metrics/metrics_pref_names.h"
[email protected]8e885de2014-07-22 23:36:5323#include "components/metrics/metrics_service.h"
[email protected]16a30912014-06-04 00:20:0424#include "components/metrics/metrics_switches.h"
jwda5d18832016-05-12 19:43:3125#include "components/metrics/test_enabled_state_provider.h"
brettwf00b9b42016-02-01 22:11:3826#include "components/prefs/testing_pref_service.h"
[email protected]88a7831f2014-05-19 15:19:1327#include "components/variations/caching_permuted_entropy_provider.h"
28#include "components/variations/pref_names.h"
[email protected]39076642014-05-05 20:32:5529#include "testing/gtest/include/gtest/gtest.h"
30
31namespace metrics {
32
33class MetricsStateManagerTest : public testing::Test {
34 public:
jwda5d18832016-05-12 19:43:3135 MetricsStateManagerTest()
holte17f4b3fb2017-03-16 02:24:4436 : test_begin_time_(base::Time::Now().ToTimeT()),
37 enabled_state_provider_(new TestEnabledStateProvider(false, false)) {
[email protected]8e885de2014-07-22 23:36:5338 MetricsService::RegisterPrefs(prefs_.registry());
[email protected]39076642014-05-05 20:32:5539 }
40
dchengd99c42a2016-04-21 21:54:1341 std::unique_ptr<MetricsStateManager> CreateStateManager() {
[email protected]3c70256f2014-05-22 03:02:1242 return MetricsStateManager::Create(
Ilya Sherman6c6c8332017-07-11 22:39:2243 &prefs_, enabled_state_provider_.get(), base::string16(),
[email protected]8e885de2014-07-22 23:36:5344 base::Bind(&MetricsStateManagerTest::MockStoreClientInfoBackup,
45 base::Unretained(this)),
46 base::Bind(&MetricsStateManagerTest::LoadFakeClientInfoBackup,
dcheng51606352015-12-26 21:16:2347 base::Unretained(this)));
[email protected]3c70256f2014-05-22 03:02:1248 }
49
50 // Sets metrics reporting as enabled for testing.
51 void EnableMetricsReporting() {
jwda5d18832016-05-12 19:43:3152 enabled_state_provider_->set_consent(true);
53 enabled_state_provider_->set_enabled(true);
[email protected]39076642014-05-05 20:32:5554 }
55
holte17f4b3fb2017-03-16 02:24:4456 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]39076642014-05-05 20:32:5571 protected:
72 TestingPrefServiceSimple prefs_;
73
[email protected]8e885de2014-07-22 23:36:5374 // Last ClientInfo stored by the MetricsStateManager via
75 // MockStoreClientInfoBackup.
dchengd99c42a2016-04-21 21:54:1376 std::unique_ptr<ClientInfo> stored_client_info_backup_;
[email protected]8e885de2014-07-22 23:36:5377
78 // If set, will be returned via LoadFakeClientInfoBackup if requested by the
79 // MetricsStateManager.
dchengd99c42a2016-04-21 21:54:1380 std::unique_ptr<ClientInfo> fake_client_info_backup_;
[email protected]8e885de2014-07-22 23:36:5381
holte17f4b3fb2017-03-16 02:24:4482 const int64_t test_begin_time_;
83
[email protected]39076642014-05-05 20:32:5584 private:
[email protected]8e885de2014-07-22 23:36:5385 // 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.
dchengd99c42a2016-04-21 21:54:13103 std::unique_ptr<ClientInfo> LoadFakeClientInfoBackup() {
[email protected]8e885de2014-07-22 23:36:53104 if (!fake_client_info_backup_)
dchengd99c42a2016-04-21 21:54:13105 return std::unique_ptr<ClientInfo>();
[email protected]8e885de2014-07-22 23:36:53106
dchengd99c42a2016-04-21 21:54:13107 std::unique_ptr<ClientInfo> backup_copy(new ClientInfo);
[email protected]8e885de2014-07-22 23:36:53108 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;
dcheng51606352015-12-26 21:16:23113 return backup_copy;
[email protected]8e885de2014-07-22 23:36:53114 }
115
jwda5d18832016-05-12 19:43:31116 std::unique_ptr<TestEnabledStateProvider> enabled_state_provider_;
[email protected]3c70256f2014-05-22 03:02:12117
[email protected]39076642014-05-05 20:32:55118 DISALLOW_COPY_AND_ASSIGN(MetricsStateManagerTest);
119};
120
121// Ensure the ClientId is formatted as expected.
122TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted) {
dchengd99c42a2016-04-21 21:54:13123 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]39076642014-05-05 20:32:55124 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
138TEST_F(MetricsStateManagerTest, EntropySourceUsed_Low) {
dchengd99c42a2016-04-21 21:54:13139 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
jwd67c08f752016-05-18 21:04:59140 state_manager->CreateDefaultEntropyProvider();
[email protected]39076642014-05-05 20:32:55141 EXPECT_EQ(MetricsStateManager::ENTROPY_SOURCE_LOW,
142 state_manager->entropy_source_returned());
143}
144
145TEST_F(MetricsStateManagerTest, EntropySourceUsed_High) {
[email protected]3c70256f2014-05-22 03:02:12146 EnableMetricsReporting();
dchengd99c42a2016-04-21 21:54:13147 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
jwd67c08f752016-05-18 21:04:59148 state_manager->CreateDefaultEntropyProvider();
[email protected]39076642014-05-05 20:32:55149 EXPECT_EQ(MetricsStateManager::ENTROPY_SOURCE_HIGH,
150 state_manager->entropy_source_returned());
151}
152
153TEST_F(MetricsStateManagerTest, LowEntropySource0NotReset) {
dchengd99c42a2016-04-21 21:54:13154 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]39076642014-05-05 20:32:55155
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 Miller43556672018-12-19 07:12:58166TEST_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]39076642014-05-05 20:32:55183
Paul Miller43556672018-12-19 07:12:58184TEST_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]39076642014-05-05 20:32:55201
Paul Miller43556672018-12-19 07:12:58202TEST_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]39076642014-05-05 20:32:55220
Paul Miller43556672018-12-19 07:12:58221TEST_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
239TEST_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]39076642014-05-05 20:32:55251 }
Paul Miller43556672018-12-19 07:12:58252}
[email protected]39076642014-05-05 20:32:55253
Paul Miller43556672018-12-19 07:12:58254TEST_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]39076642014-05-05 20:32:55265 }
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.
271TEST_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]16a30912014-06-04 00:20:04275 prefs_.SetString(prefs::kMetricsClientID, kInitialClientId);
[email protected]39076642014-05-05 20:32:55276
277 // Make sure the initial client id isn't reset by the metrics state manager.
278 {
dchengd99c42a2016-04-21 21:54:13279 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]39076642014-05-05 20:32:55280 state_manager->ForceClientIdCreation();
281 EXPECT_EQ(kInitialClientId, state_manager->client_id());
Mark Pearsond4f91d112017-11-08 01:45:49282 EXPECT_FALSE(state_manager->metrics_ids_were_reset_);
[email protected]39076642014-05-05 20:32:55283 }
284
285 // Set the reset pref to cause the IDs to be reset.
[email protected]66d176a2014-05-22 13:49:39286 prefs_.SetBoolean(prefs::kMetricsResetIds, true);
[email protected]39076642014-05-05 20:32:55287
288 // Cause the actual reset to happen.
289 {
dchengd99c42a2016-04-21 21:54:13290 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]39076642014-05-05 20:32:55291 state_manager->ForceClientIdCreation();
292 EXPECT_NE(kInitialClientId, state_manager->client_id());
Mark Pearsond4f91d112017-11-08 01:45:49293 EXPECT_TRUE(state_manager->metrics_ids_were_reset_);
294 EXPECT_EQ(kInitialClientId, state_manager->previous_client_id_);
[email protected]39076642014-05-05 20:32:55295
296 state_manager->GetLowEntropySource();
297
[email protected]66d176a2014-05-22 13:49:39298 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds));
[email protected]39076642014-05-05 20:32:55299 }
300
[email protected]16a30912014-06-04 00:20:04301 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID));
[email protected]39076642014-05-05 20:32:55302}
303
[email protected]8e885de2014-07-22 23:36:53304TEST_F(MetricsStateManagerTest, ForceClientIdCreation) {
avi26062922015-12-26 00:14:18305 const int64_t kFakeInstallationDate = 12345;
[email protected]8e885de2014-07-22 23:36:53306 prefs_.SetInt64(prefs::kInstallDate, kFakeInstallationDate);
307
[email protected]8e885de2014-07-22 23:36:53308 {
dchengd99c42a2016-04-21 21:54:13309 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]8e885de2014-07-22 23:36:53310
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]986ee1a72014-08-05 23:03:32321 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
holte17f4b3fb2017-03-16 02:24:44322 test_begin_time_);
[email protected]8e885de2014-07-22 23:36:53323
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]8e885de2014-07-22 23:36:53331 }
holte17f4b3fb2017-03-16 02:24:44332}
333
334TEST_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]8e885de2014-07-22 23:36:53340
341 EnableMetricsReporting();
[email protected]8e885de2014-07-22 23:36:53342 {
holte17f4b3fb2017-03-16 02:24:44343 EXPECT_FALSE(fake_client_info_backup_);
[email protected]8e885de2014-07-22 23:36:53344 EXPECT_FALSE(stored_client_info_backup_);
345
dchengd99c42a2016-04-21 21:54:13346 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]8e885de2014-07-22 23:36:53347
348 // client_id should be auto-obtained from the constructor when metrics
349 // reporting is enabled.
holte17f4b3fb2017-03-16 02:24:44350 EXPECT_EQ(client_info.client_id, state_manager->client_id());
[email protected]8e885de2014-07-22 23:36:53351
holte17f4b3fb2017-03-16 02:24:44352 // The backup should not be modified.
353 ASSERT_FALSE(stored_client_info_backup_);
[email protected]8e885de2014-07-22 23:36:53354
355 // Re-forcing client id creation shouldn't cause another backup and
356 // shouldn't affect the existing client id.
[email protected]8e885de2014-07-22 23:36:53357 state_manager->ForceClientIdCreation();
358 EXPECT_FALSE(stored_client_info_backup_);
holte17f4b3fb2017-03-16 02:24:44359 EXPECT_EQ(client_info.client_id, state_manager->client_id());
[email protected]8e885de2014-07-22 23:36:53360 }
holte17f4b3fb2017-03-16 02:24:44361}
[email protected]8e885de2014-07-22 23:36:53362
holte17f4b3fb2017-03-16 02:24:44363TEST_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]8e885de2014-07-22 23:36:53369
holte17f4b3fb2017-03-16 02:24:44370 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]8e885de2014-07-22 23:36:53377 {
holte17f4b3fb2017-03-16 02:24:44378 // The backup should be ignored if we already have a client id.
[email protected]8e885de2014-07-22 23:36:53379
380 EXPECT_FALSE(stored_client_info_backup_);
381
dchengd99c42a2016-04-21 21:54:13382 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
holte17f4b3fb2017-03-16 02:24:44383 EXPECT_EQ(client_info.client_id, state_manager->client_id());
[email protected]8e885de2014-07-22 23:36:53384
holte17f4b3fb2017-03-16 02:24:44385 // The backup should not be modified.
386 ASSERT_FALSE(stored_client_info_backup_);
[email protected]8e885de2014-07-22 23:36:53387 }
holte17f4b3fb2017-03-16 02:24:44388}
389
390TEST_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]8e885de2014-07-22 23:36:53402
403 prefs_.ClearPref(prefs::kMetricsClientID);
404 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp);
405
holte17f4b3fb2017-03-16 02:24:44406 EnableMetricsReporting();
[email protected]8e885de2014-07-22 23:36:53407 {
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
dchengd99c42a2016-04-21 21:54:13413 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
holte17f4b3fb2017-03-16 02:24:44414 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]8e885de2014-07-22 23:36:53418 prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp));
419
420 EXPECT_TRUE(stored_client_info_backup_);
[email protected]8e885de2014-07-22 23:36:53421 }
holte17f4b3fb2017-03-16 02:24:44422}
[email protected]8e885de2014-07-22 23:36:53423
holte17f4b3fb2017-03-16 02:24:44424TEST_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]8e885de2014-07-22 23:36:53429
holte17f4b3fb2017-03-16 02:24:44430 SetFakeClientInfoBackup(client_info);
431 SetClientInfoPrefs(client_info);
[email protected]8e885de2014-07-22 23:36:53432
433 prefs_.SetBoolean(prefs::kMetricsResetIds, true);
434
holte17f4b3fb2017-03-16 02:24:44435 EnableMetricsReporting();
[email protected]8e885de2014-07-22 23:36:53436 {
437 // Upon request to reset metrics ids, the existing backup should not be
438 // restored.
439
dchengd99c42a2016-04-21 21:54:13440 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]8e885de2014-07-22 23:36:53441
442 // A brand new client id should have been generated.
443 EXPECT_NE(std::string(), state_manager->client_id());
holte17f4b3fb2017-03-16 02:24:44444 EXPECT_NE(client_info.client_id, state_manager->client_id());
Mark Pearsond4f91d112017-11-08 01:45:49445 EXPECT_TRUE(state_manager->metrics_ids_were_reset_);
446 EXPECT_EQ(client_info.client_id, state_manager->previous_client_id_);
holte17f4b3fb2017-03-16 02:24:44447 EXPECT_TRUE(stored_client_info_backup_);
[email protected]8e885de2014-07-22 23:36:53448
[email protected]986ee1a72014-08-05 23:03:32449 // The installation date should not have been affected.
holte17f4b3fb2017-03-16 02:24:44450 EXPECT_EQ(client_info.installation_date,
[email protected]8e885de2014-07-22 23:36:53451 prefs_.GetInt64(prefs::kInstallDate));
[email protected]986ee1a72014-08-05 23:03:32452
453 // The metrics-reporting-enabled date will be reset to Now().
454 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
holte17f4b3fb2017-03-16 02:24:44455 test_begin_time_);
[email protected]8e885de2014-07-22 23:36:53456 }
457}
458
Steven Holte8e9db0ca2017-08-11 01:20:08459TEST_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 Pearsond4f91d112017-11-08 01:45:49479
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
489TEST_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 Holte8e9db0ca2017-08-11 01:20:08522}
523
[email protected]39076642014-05-05 20:32:55524} // namespace metrics