blob: 1ffb527ed53c67e2df69c0472089197eb58d2141 [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"
Alexei Svitkine0d0820632019-02-14 19:13:5620#include "build/build_config.h"
[email protected]8e885de2014-07-22 23:36:5321#include "components/metrics/client_info.h"
Mark Pearsond4f91d112017-11-08 01:45:4922#include "components/metrics/metrics_log.h"
[email protected]66d176a2014-05-22 13:49:3923#include "components/metrics/metrics_pref_names.h"
[email protected]8e885de2014-07-22 23:36:5324#include "components/metrics/metrics_service.h"
[email protected]16a30912014-06-04 00:20:0425#include "components/metrics/metrics_switches.h"
jwda5d18832016-05-12 19:43:3126#include "components/metrics/test_enabled_state_provider.h"
brettwf00b9b42016-02-01 22:11:3827#include "components/prefs/testing_pref_service.h"
[email protected]88a7831f2014-05-19 15:19:1328#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
Alexei Svitkine0d0820632019-02-14 19:13:5633namespace {
34
35// Verifies that the client id follows the expected pattern.
36void 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]39076642014-05-05 20:32:5550class MetricsStateManagerTest : public testing::Test {
51 public:
jwda5d18832016-05-12 19:43:3152 MetricsStateManagerTest()
holte17f4b3fb2017-03-16 02:24:4453 : test_begin_time_(base::Time::Now().ToTimeT()),
54 enabled_state_provider_(new TestEnabledStateProvider(false, false)) {
[email protected]8e885de2014-07-22 23:36:5355 MetricsService::RegisterPrefs(prefs_.registry());
[email protected]39076642014-05-05 20:32:5556 }
57
dchengd99c42a2016-04-21 21:54:1358 std::unique_ptr<MetricsStateManager> CreateStateManager() {
[email protected]3c70256f2014-05-22 03:02:1259 return MetricsStateManager::Create(
Ilya Sherman6c6c8332017-07-11 22:39:2260 &prefs_, enabled_state_provider_.get(), base::string16(),
[email protected]8e885de2014-07-22 23:36:5361 base::Bind(&MetricsStateManagerTest::MockStoreClientInfoBackup,
62 base::Unretained(this)),
63 base::Bind(&MetricsStateManagerTest::LoadFakeClientInfoBackup,
dcheng51606352015-12-26 21:16:2364 base::Unretained(this)));
[email protected]3c70256f2014-05-22 03:02:1265 }
66
67 // Sets metrics reporting as enabled for testing.
68 void EnableMetricsReporting() {
jwda5d18832016-05-12 19:43:3169 enabled_state_provider_->set_consent(true);
70 enabled_state_provider_->set_enabled(true);
[email protected]39076642014-05-05 20:32:5571 }
72
holte17f4b3fb2017-03-16 02:24:4473 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]39076642014-05-05 20:32:5588 protected:
89 TestingPrefServiceSimple prefs_;
90
[email protected]8e885de2014-07-22 23:36:5391 // Last ClientInfo stored by the MetricsStateManager via
92 // MockStoreClientInfoBackup.
dchengd99c42a2016-04-21 21:54:1393 std::unique_ptr<ClientInfo> stored_client_info_backup_;
[email protected]8e885de2014-07-22 23:36:5394
95 // If set, will be returned via LoadFakeClientInfoBackup if requested by the
96 // MetricsStateManager.
dchengd99c42a2016-04-21 21:54:1397 std::unique_ptr<ClientInfo> fake_client_info_backup_;
[email protected]8e885de2014-07-22 23:36:5398
holte17f4b3fb2017-03-16 02:24:4499 const int64_t test_begin_time_;
100
[email protected]39076642014-05-05 20:32:55101 private:
[email protected]8e885de2014-07-22 23:36:53102 // 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.
dchengd99c42a2016-04-21 21:54:13120 std::unique_ptr<ClientInfo> LoadFakeClientInfoBackup() {
[email protected]8e885de2014-07-22 23:36:53121 if (!fake_client_info_backup_)
Nate Fischerb8403fd2019-10-03 22:49:02122 return nullptr;
[email protected]8e885de2014-07-22 23:36:53123
dchengd99c42a2016-04-21 21:54:13124 std::unique_ptr<ClientInfo> backup_copy(new ClientInfo);
[email protected]8e885de2014-07-22 23:36:53125 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;
dcheng51606352015-12-26 21:16:23130 return backup_copy;
[email protected]8e885de2014-07-22 23:36:53131 }
132
jwda5d18832016-05-12 19:43:31133 std::unique_ptr<TestEnabledStateProvider> enabled_state_provider_;
[email protected]3c70256f2014-05-22 03:02:12134
[email protected]39076642014-05-05 20:32:55135 DISALLOW_COPY_AND_ASSIGN(MetricsStateManagerTest);
136};
137
Alexei Svitkine0d0820632019-02-14 19:13:56138TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted_ConsentInitially) {
139 // With consent set initially, client id should be created in the constructor.
140 EnableMetricsReporting();
dchengd99c42a2016-04-21 21:54:13141 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]39076642014-05-05 20:32:55142
143 const std::string client_id = state_manager->client_id();
Alexei Svitkine0d0820632019-02-14 19:13:56144 VerifyClientId(client_id);
145}
[email protected]39076642014-05-05 20:32:55146
Alexei Svitkine0d0820632019-02-14 19:13:56147TEST_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]39076642014-05-05 20:32:55156}
157
158TEST_F(MetricsStateManagerTest, EntropySourceUsed_Low) {
Alexei Svitkine0d0820632019-02-14 19:13:56159 // 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
dchengd99c42a2016-04-21 21:54:13164 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
jwd67c08f752016-05-18 21:04:59165 state_manager->CreateDefaultEntropyProvider();
[email protected]39076642014-05-05 20:32:55166 EXPECT_EQ(MetricsStateManager::ENTROPY_SOURCE_LOW,
167 state_manager->entropy_source_returned());
168}
169
170TEST_F(MetricsStateManagerTest, EntropySourceUsed_High) {
[email protected]3c70256f2014-05-22 03:02:12171 EnableMetricsReporting();
dchengd99c42a2016-04-21 21:54:13172 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
jwd67c08f752016-05-18 21:04:59173 state_manager->CreateDefaultEntropyProvider();
[email protected]39076642014-05-05 20:32:55174 EXPECT_EQ(MetricsStateManager::ENTROPY_SOURCE_HIGH,
175 state_manager->entropy_source_returned());
176}
177
178TEST_F(MetricsStateManagerTest, LowEntropySource0NotReset) {
dchengd99c42a2016-04-21 21:54:13179 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]39076642014-05-05 20:32:55180
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 Miller43556672018-12-19 07:12:58191TEST_F(MetricsStateManagerTest, HaveNoLowEntropySource) {
Alexei Svitkine0d0820632019-02-14 19:13:56192 prefs_.SetString(prefs::kMetricsClientID,
193 "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF");
194 EnableMetricsReporting();
Paul Miller43556672018-12-19 07:12:58195 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
Paul Miller43556672018-12-19 07:12:58196 // 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 Tambref88e5102019-02-06 10:54:03206 EXPECT_TRUE(base::EndsWith(high_source, base::NumberToString(new_low_source),
Paul Miller43556672018-12-19 07:12:58207 base::CompareCase::SENSITIVE))
208 << high_source;
209}
[email protected]39076642014-05-05 20:32:55210
Paul Miller43556672018-12-19 07:12:58211TEST_F(MetricsStateManagerTest, HaveOnlyNewLowEntropySource) {
Paul Miller43556672018-12-19 07:12:58212 // 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 Svitkine0d0820632019-02-14 19:13:56215 prefs_.SetString(prefs::kMetricsClientID,
216 "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF");
217 EnableMetricsReporting();
218 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
Paul Miller43556672018-12-19 07:12:58219 // ...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 Tambref88e5102019-02-06 10:54:03226 EXPECT_TRUE(base::EndsWith(high_source, base::NumberToString(new_low_source),
Paul Miller43556672018-12-19 07:12:58227 base::CompareCase::SENSITIVE))
228 << high_source;
229}
[email protected]39076642014-05-05 20:32:55230
Paul Miller43556672018-12-19 07:12:58231TEST_F(MetricsStateManagerTest, HaveOnlyOldLowEntropySource) {
Paul Miller43556672018-12-19 07:12:58232 // 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 Svitkine0d0820632019-02-14 19:13:56235 prefs_.SetString(prefs::kMetricsClientID,
236 "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF");
237 EnableMetricsReporting();
Paul Miller43556672018-12-19 07:12:58238 // ...then the new source should be created...
Alexei Svitkine0d0820632019-02-14 19:13:56239 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
Paul Miller43556672018-12-19 07:12:58240 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 Tambref88e5102019-02-06 10:54:03247 EXPECT_TRUE(base::EndsWith(high_source, base::NumberToString(old_low_source),
Paul Miller43556672018-12-19 07:12:58248 base::CompareCase::SENSITIVE))
249 << high_source;
250}
[email protected]39076642014-05-05 20:32:55251
Paul Miller43556672018-12-19 07:12:58252TEST_F(MetricsStateManagerTest, HaveBothLowEntropySources) {
Paul Miller43556672018-12-19 07:12:58253 // 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 Svitkine0d0820632019-02-14 19:13:56258 prefs_.SetString(prefs::kMetricsClientID,
259 "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF");
260 EnableMetricsReporting();
Paul Miller43556672018-12-19 07:12:58261 // ...then both should be loaded...
Alexei Svitkine0d0820632019-02-14 19:13:56262 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
Paul Miller43556672018-12-19 07:12:58263 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 Tambref88e5102019-02-06 10:54:03267 EXPECT_TRUE(base::EndsWith(high_source, base::NumberToString(old_low_source),
Paul Miller43556672018-12-19 07:12:58268 base::CompareCase::SENSITIVE))
269 << high_source;
270}
271
272TEST_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]39076642014-05-05 20:32:55284 }
Paul Miller43556672018-12-19 07:12:58285}
[email protected]39076642014-05-05 20:32:55286
Paul Miller43556672018-12-19 07:12:58287TEST_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]39076642014-05-05 20:32:55298 }
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.
304TEST_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]16a30912014-06-04 00:20:04308 prefs_.SetString(prefs::kMetricsClientID, kInitialClientId);
[email protected]39076642014-05-05 20:32:55309
Alexei Svitkine0d0820632019-02-14 19:13:56310 EnableMetricsReporting();
311
[email protected]39076642014-05-05 20:32:55312 // Make sure the initial client id isn't reset by the metrics state manager.
313 {
dchengd99c42a2016-04-21 21:54:13314 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]39076642014-05-05 20:32:55315 state_manager->ForceClientIdCreation();
316 EXPECT_EQ(kInitialClientId, state_manager->client_id());
Mark Pearsond4f91d112017-11-08 01:45:49317 EXPECT_FALSE(state_manager->metrics_ids_were_reset_);
[email protected]39076642014-05-05 20:32:55318 }
319
320 // Set the reset pref to cause the IDs to be reset.
[email protected]66d176a2014-05-22 13:49:39321 prefs_.SetBoolean(prefs::kMetricsResetIds, true);
[email protected]39076642014-05-05 20:32:55322
323 // Cause the actual reset to happen.
324 {
dchengd99c42a2016-04-21 21:54:13325 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]39076642014-05-05 20:32:55326 state_manager->ForceClientIdCreation();
327 EXPECT_NE(kInitialClientId, state_manager->client_id());
Mark Pearsond4f91d112017-11-08 01:45:49328 EXPECT_TRUE(state_manager->metrics_ids_were_reset_);
329 EXPECT_EQ(kInitialClientId, state_manager->previous_client_id_);
[email protected]39076642014-05-05 20:32:55330
331 state_manager->GetLowEntropySource();
332
[email protected]66d176a2014-05-22 13:49:39333 EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds));
[email protected]39076642014-05-05 20:32:55334 }
335
[email protected]16a30912014-06-04 00:20:04336 EXPECT_NE(kInitialClientId, prefs_.GetString(prefs::kMetricsClientID));
[email protected]39076642014-05-05 20:32:55337}
338
[email protected]8e885de2014-07-22 23:36:53339TEST_F(MetricsStateManagerTest, ForceClientIdCreation) {
avi26062922015-12-26 00:14:18340 const int64_t kFakeInstallationDate = 12345;
[email protected]8e885de2014-07-22 23:36:53341 prefs_.SetInt64(prefs::kInstallDate, kFakeInstallationDate);
342
[email protected]8e885de2014-07-22 23:36:53343 {
dchengd99c42a2016-04-21 21:54:13344 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]8e885de2014-07-22 23:36:53345
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 Svitkine0d0820632019-02-14 19:13:56354 EnableMetricsReporting();
[email protected]8e885de2014-07-22 23:36:53355 state_manager->ForceClientIdCreation();
356 EXPECT_NE(std::string(), state_manager->client_id());
[email protected]986ee1a72014-08-05 23:03:32357 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
holte17f4b3fb2017-03-16 02:24:44358 test_begin_time_);
[email protected]8e885de2014-07-22 23:36:53359
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]8e885de2014-07-22 23:36:53367 }
holte17f4b3fb2017-03-16 02:24:44368}
369
Alexei Svitkine0d0820632019-02-14 19:13:56370#if !defined(OS_WIN)
371TEST_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
396TEST_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
holte17f4b3fb2017-03-16 02:24:44434TEST_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]8e885de2014-07-22 23:36:53440
441 EnableMetricsReporting();
[email protected]8e885de2014-07-22 23:36:53442 {
holte17f4b3fb2017-03-16 02:24:44443 EXPECT_FALSE(fake_client_info_backup_);
[email protected]8e885de2014-07-22 23:36:53444 EXPECT_FALSE(stored_client_info_backup_);
445
dchengd99c42a2016-04-21 21:54:13446 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]8e885de2014-07-22 23:36:53447
448 // client_id should be auto-obtained from the constructor when metrics
449 // reporting is enabled.
holte17f4b3fb2017-03-16 02:24:44450 EXPECT_EQ(client_info.client_id, state_manager->client_id());
[email protected]8e885de2014-07-22 23:36:53451
holte17f4b3fb2017-03-16 02:24:44452 // The backup should not be modified.
453 ASSERT_FALSE(stored_client_info_backup_);
[email protected]8e885de2014-07-22 23:36:53454
455 // Re-forcing client id creation shouldn't cause another backup and
456 // shouldn't affect the existing client id.
[email protected]8e885de2014-07-22 23:36:53457 state_manager->ForceClientIdCreation();
458 EXPECT_FALSE(stored_client_info_backup_);
holte17f4b3fb2017-03-16 02:24:44459 EXPECT_EQ(client_info.client_id, state_manager->client_id());
[email protected]8e885de2014-07-22 23:36:53460 }
holte17f4b3fb2017-03-16 02:24:44461}
[email protected]8e885de2014-07-22 23:36:53462
holte17f4b3fb2017-03-16 02:24:44463TEST_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]8e885de2014-07-22 23:36:53469
holte17f4b3fb2017-03-16 02:24:44470 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]8e885de2014-07-22 23:36:53477 {
holte17f4b3fb2017-03-16 02:24:44478 // The backup should be ignored if we already have a client id.
[email protected]8e885de2014-07-22 23:36:53479
480 EXPECT_FALSE(stored_client_info_backup_);
481
dchengd99c42a2016-04-21 21:54:13482 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
holte17f4b3fb2017-03-16 02:24:44483 EXPECT_EQ(client_info.client_id, state_manager->client_id());
[email protected]8e885de2014-07-22 23:36:53484
holte17f4b3fb2017-03-16 02:24:44485 // The backup should not be modified.
486 ASSERT_FALSE(stored_client_info_backup_);
[email protected]8e885de2014-07-22 23:36:53487 }
holte17f4b3fb2017-03-16 02:24:44488}
489
490TEST_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]8e885de2014-07-22 23:36:53502
503 prefs_.ClearPref(prefs::kMetricsClientID);
504 prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp);
505
holte17f4b3fb2017-03-16 02:24:44506 EnableMetricsReporting();
[email protected]8e885de2014-07-22 23:36:53507 {
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
dchengd99c42a2016-04-21 21:54:13513 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
holte17f4b3fb2017-03-16 02:24:44514 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]8e885de2014-07-22 23:36:53518 prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp));
519
520 EXPECT_TRUE(stored_client_info_backup_);
[email protected]8e885de2014-07-22 23:36:53521 }
holte17f4b3fb2017-03-16 02:24:44522}
[email protected]8e885de2014-07-22 23:36:53523
holte17f4b3fb2017-03-16 02:24:44524TEST_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]8e885de2014-07-22 23:36:53529
holte17f4b3fb2017-03-16 02:24:44530 SetFakeClientInfoBackup(client_info);
531 SetClientInfoPrefs(client_info);
[email protected]8e885de2014-07-22 23:36:53532
533 prefs_.SetBoolean(prefs::kMetricsResetIds, true);
534
holte17f4b3fb2017-03-16 02:24:44535 EnableMetricsReporting();
[email protected]8e885de2014-07-22 23:36:53536 {
537 // Upon request to reset metrics ids, the existing backup should not be
538 // restored.
539
dchengd99c42a2016-04-21 21:54:13540 std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
[email protected]8e885de2014-07-22 23:36:53541
542 // A brand new client id should have been generated.
543 EXPECT_NE(std::string(), state_manager->client_id());
holte17f4b3fb2017-03-16 02:24:44544 EXPECT_NE(client_info.client_id, state_manager->client_id());
Mark Pearsond4f91d112017-11-08 01:45:49545 EXPECT_TRUE(state_manager->metrics_ids_were_reset_);
546 EXPECT_EQ(client_info.client_id, state_manager->previous_client_id_);
holte17f4b3fb2017-03-16 02:24:44547 EXPECT_TRUE(stored_client_info_backup_);
[email protected]8e885de2014-07-22 23:36:53548
[email protected]986ee1a72014-08-05 23:03:32549 // The installation date should not have been affected.
holte17f4b3fb2017-03-16 02:24:44550 EXPECT_EQ(client_info.installation_date,
[email protected]8e885de2014-07-22 23:36:53551 prefs_.GetInt64(prefs::kInstallDate));
[email protected]986ee1a72014-08-05 23:03:32552
553 // The metrics-reporting-enabled date will be reset to Now().
554 EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
holte17f4b3fb2017-03-16 02:24:44555 test_begin_time_);
[email protected]8e885de2014-07-22 23:36:53556 }
557}
558
Steven Holte8e9db0ca2017-08-11 01:20:08559TEST_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 Pearsond4f91d112017-11-08 01:45:49579
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
589TEST_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 Holte8e9db0ca2017-08-11 01:20:08622}
623
[email protected]39076642014-05-05 20:32:55624} // namespace metrics