• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/metrics/metrics_state_manager.h"
6 
7 #include <ctype.h>
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <memory>
12 #include <string>
13 #include <utility>
14 
15 #include "base/command_line.h"
16 #include "base/files/file_path.h"
17 #include "base/functional/bind.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/strings/string_util.h"
20 #include "base/test/metrics/histogram_tester.h"
21 #include "base/test/scoped_feature_list.h"
22 #include "build/build_config.h"
23 #include "components/metrics/client_info.h"
24 #include "components/metrics/metrics_data_validation.h"
25 #include "components/metrics/metrics_log.h"
26 #include "components/metrics/metrics_pref_names.h"
27 #include "components/metrics/metrics_service.h"
28 #include "components/metrics/metrics_switches.h"
29 #include "components/metrics/test/test_enabled_state_provider.h"
30 #include "components/prefs/testing_pref_service.h"
31 #include "components/variations/pref_names.h"
32 #include "testing/gmock/include/gmock/gmock.h"
33 #include "testing/gtest/include/gtest/gtest.h"
34 
35 namespace metrics {
36 namespace {
37 
38 // Verifies that the client id follows the expected pattern.
VerifyClientId(const std::string & client_id)39 void VerifyClientId(const std::string& client_id) {
40   EXPECT_EQ(36U, client_id.length());
41 
42   for (size_t i = 0; i < client_id.length(); ++i) {
43     char current = client_id[i];
44     if (i == 8 || i == 13 || i == 18 || i == 23)
45       EXPECT_EQ('-', current);
46     else
47       EXPECT_TRUE(isxdigit(current));
48   }
49 }
50 
51 MATCHER(HaveClonedInstallInfo, "") {
52   return (
53       !arg.FindPreference(prefs::kClonedResetCount)->IsDefaultValue() &&
54       !arg.FindPreference(prefs::kFirstClonedResetTimestamp)
55            ->IsDefaultValue() &&
56       !arg.FindPreference(prefs::kLastClonedResetTimestamp)->IsDefaultValue());
57 }
58 
59 MATCHER(HaveNoClonedInstallInfo, "") {
60   return (
61       arg.FindPreference(prefs::kClonedResetCount)->IsDefaultValue() &&
62       arg.FindPreference(prefs::kFirstClonedResetTimestamp)->IsDefaultValue() &&
63       arg.FindPreference(prefs::kLastClonedResetTimestamp)->IsDefaultValue());
64 }
65 
66 }  // namespace
67 
68 class MetricsStateManagerTest : public testing::Test {
69  public:
MetricsStateManagerTest()70   MetricsStateManagerTest()
71       : test_begin_time_(base::Time::Now().ToTimeT()),
72         enabled_state_provider_(new TestEnabledStateProvider(false, false)) {
73     MetricsService::RegisterPrefs(prefs_.registry());
74   }
75 
76   MetricsStateManagerTest(const MetricsStateManagerTest&) = delete;
77   MetricsStateManagerTest& operator=(const MetricsStateManagerTest&) = delete;
78 
CreateStateManager(const std::string & external_client_id="")79   std::unique_ptr<MetricsStateManager> CreateStateManager(
80       const std::string& external_client_id = "") {
81     std::unique_ptr<MetricsStateManager> state_manager =
82         MetricsStateManager::Create(
83             &prefs_, enabled_state_provider_.get(), std::wstring(),
84             base::FilePath(), StartupVisibility::kUnknown, {},
85             base::BindRepeating(
86                 &MetricsStateManagerTest::MockStoreClientInfoBackup,
87                 base::Unretained(this)),
88             base::BindRepeating(
89                 &MetricsStateManagerTest::LoadFakeClientInfoBackup,
90                 base::Unretained(this)),
91             external_client_id);
92     state_manager->InstantiateFieldTrialList();
93     return state_manager;
94   }
95 
96   // Sets metrics reporting as enabled for testing.
EnableMetricsReporting()97   void EnableMetricsReporting() {
98     enabled_state_provider_->set_consent(true);
99     enabled_state_provider_->set_enabled(true);
100   }
101 
SetClientInfoPrefs(const ClientInfo & client_info)102   void SetClientInfoPrefs(const ClientInfo& client_info) {
103     prefs_.SetString(prefs::kMetricsClientID, client_info.client_id);
104     prefs_.SetInt64(prefs::kInstallDate, client_info.installation_date);
105     prefs_.SetInt64(prefs::kMetricsReportingEnabledTimestamp,
106                     client_info.reporting_enabled_date);
107   }
108 
SetFakeClientInfoBackup(const ClientInfo & client_info)109   void SetFakeClientInfoBackup(const ClientInfo& client_info) {
110     fake_client_info_backup_ = std::make_unique<ClientInfo>();
111     fake_client_info_backup_->client_id = client_info.client_id;
112     fake_client_info_backup_->installation_date = client_info.installation_date;
113     fake_client_info_backup_->reporting_enabled_date =
114         client_info.reporting_enabled_date;
115   }
116 
117   // The number of times that the code tries to load ClientInfo.
118   int client_info_load_count_ = 0;
119 
120  protected:
121   TestingPrefServiceSimple prefs_;
122 
123   // Last ClientInfo stored by the MetricsStateManager via
124   // MockStoreClientInfoBackup.
125   std::unique_ptr<ClientInfo> stored_client_info_backup_;
126 
127   // If set, will be returned via LoadFakeClientInfoBackup if requested by the
128   // MetricsStateManager.
129   std::unique_ptr<ClientInfo> fake_client_info_backup_;
130 
131   const int64_t test_begin_time_;
132 
133  private:
134   // Stores the |client_info| in |stored_client_info_backup_| for verification
135   // by the tests later.
MockStoreClientInfoBackup(const ClientInfo & client_info)136   void MockStoreClientInfoBackup(const ClientInfo& client_info) {
137     stored_client_info_backup_ = std::make_unique<ClientInfo>();
138     stored_client_info_backup_->client_id = client_info.client_id;
139     stored_client_info_backup_->installation_date =
140         client_info.installation_date;
141     stored_client_info_backup_->reporting_enabled_date =
142         client_info.reporting_enabled_date;
143 
144     // Respect the contract that storing an empty client_id voids the existing
145     // backup (required for the last section of the ForceClientIdCreation test
146     // below).
147     if (client_info.client_id.empty())
148       fake_client_info_backup_.reset();
149   }
150 
151   // Hands out a copy of |fake_client_info_backup_| if it is set.
LoadFakeClientInfoBackup()152   std::unique_ptr<ClientInfo> LoadFakeClientInfoBackup() {
153     ++client_info_load_count_;
154     if (!fake_client_info_backup_)
155       return nullptr;
156 
157     std::unique_ptr<ClientInfo> backup_copy(new ClientInfo);
158     backup_copy->client_id = fake_client_info_backup_->client_id;
159     backup_copy->installation_date =
160         fake_client_info_backup_->installation_date;
161     backup_copy->reporting_enabled_date =
162         fake_client_info_backup_->reporting_enabled_date;
163     return backup_copy;
164   }
165 
166   std::unique_ptr<TestEnabledStateProvider> enabled_state_provider_;
167 };
168 
TEST_F(MetricsStateManagerTest,ClientIdCorrectlyFormatted_ConsentInitially)169 TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted_ConsentInitially) {
170   // With consent set initially, client id should be created in the constructor.
171   EnableMetricsReporting();
172   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
173 
174   const std::string client_id = state_manager->client_id();
175   VerifyClientId(client_id);
176 }
177 
TEST_F(MetricsStateManagerTest,ClientIdCorrectlyFormatted_ConsentLater)178 TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted_ConsentLater) {
179   // With consent set initially, client id should be created on consent grant.
180   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
181   EXPECT_EQ(std::string(), state_manager->client_id());
182 
183   EnableMetricsReporting();
184   state_manager->ForceClientIdCreation();
185   const std::string client_id = state_manager->client_id();
186   VerifyClientId(client_id);
187 }
188 
TEST_F(MetricsStateManagerTest,EntropySourceUsed_Low)189 TEST_F(MetricsStateManagerTest, EntropySourceUsed_Low) {
190   // Set the install date pref, which makes sure we don't trigger the first run
191   // behavior where a provisional client id is generated and used to return a
192   // high entropy source.
193   prefs_.SetInt64(prefs::kInstallDate, base::Time::Now().ToTimeT());
194 
195   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
196   state_manager->CreateEntropyProviders();
197   EXPECT_EQ(state_manager->entropy_source_returned(),
198             MetricsStateManager::ENTROPY_SOURCE_LOW);
199   EXPECT_EQ(state_manager->initial_client_id_for_testing(), "");
200 }
201 
TEST_F(MetricsStateManagerTest,EntropySourceUsed_High)202 TEST_F(MetricsStateManagerTest, EntropySourceUsed_High) {
203   EnableMetricsReporting();
204   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
205   state_manager->CreateEntropyProviders();
206   EXPECT_EQ(state_manager->entropy_source_returned(),
207             MetricsStateManager::ENTROPY_SOURCE_HIGH);
208   EXPECT_EQ(state_manager->initial_client_id_for_testing(),
209             state_manager->client_id());
210 }
211 
212 // Check that setting the kMetricsResetIds pref to true causes the client id to
213 // be reset. We do not check that the low entropy source is reset because we
214 // cannot ensure that metrics state manager won't generate the same id again.
TEST_F(MetricsStateManagerTest,ResetMetricsIDs)215 TEST_F(MetricsStateManagerTest, ResetMetricsIDs) {
216   // Set an initial client id in prefs. It should not be possible for the
217   // metrics state manager to generate this id randomly.
218   const std::string kInitialClientId = "initial client id";
219   prefs_.SetString(prefs::kMetricsClientID, kInitialClientId);
220 
221   EnableMetricsReporting();
222 
223   // No cloned install info should have been stored.
224   EXPECT_THAT(prefs_, HaveNoClonedInstallInfo());
225 
226   // Make sure the initial client id isn't reset by the metrics state manager.
227   {
228     std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
229     state_manager->ForceClientIdCreation();
230     EXPECT_EQ(state_manager->client_id(), kInitialClientId);
231     EXPECT_FALSE(state_manager->metrics_ids_were_reset_);
232     EXPECT_THAT(prefs_, HaveNoClonedInstallInfo());
233   }
234 
235   // Set the reset pref to cause the IDs to be reset.
236   prefs_.SetBoolean(prefs::kMetricsResetIds, true);
237 
238   // Cause the actual reset to happen.
239   {
240     std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
241     state_manager->ForceClientIdCreation();
242     EXPECT_NE(state_manager->client_id(), kInitialClientId);
243     EXPECT_TRUE(state_manager->metrics_ids_were_reset_);
244     EXPECT_EQ(state_manager->previous_client_id_, kInitialClientId);
245     EXPECT_EQ(client_info_load_count_, 0);
246 
247     state_manager->GetLowEntropySource();
248 
249     EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds));
250 
251     EXPECT_THAT(prefs_, HaveClonedInstallInfo());
252     EXPECT_EQ(prefs_.GetInteger(prefs::kClonedResetCount), 1);
253     EXPECT_EQ(prefs_.GetInt64(prefs::kFirstClonedResetTimestamp),
254               prefs_.GetInt64(prefs::kLastClonedResetTimestamp));
255   }
256 
257   EXPECT_NE(prefs_.GetString(prefs::kMetricsClientID), kInitialClientId);
258 }
259 
TEST_F(MetricsStateManagerTest,LogHasSessionShutdownCleanly)260 TEST_F(MetricsStateManagerTest, LogHasSessionShutdownCleanly) {
261   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
262   prefs_.SetBoolean(prefs::kStabilityExitedCleanly, false);
263   state_manager->LogHasSessionShutdownCleanly(
264       /*has_session_shutdown_cleanly=*/true);
265   EXPECT_TRUE(prefs_.GetBoolean(prefs::kStabilityExitedCleanly));
266 }
267 
TEST_F(MetricsStateManagerTest,LogSessionHasNotYetShutdownCleanly)268 TEST_F(MetricsStateManagerTest, LogSessionHasNotYetShutdownCleanly) {
269   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
270   ASSERT_TRUE(prefs_.GetBoolean(prefs::kStabilityExitedCleanly));
271   state_manager->LogHasSessionShutdownCleanly(
272       /*has_session_shutdown_cleanly=*/false);
273   EXPECT_FALSE(prefs_.GetBoolean(prefs::kStabilityExitedCleanly));
274 }
275 
TEST_F(MetricsStateManagerTest,ForceClientIdCreation)276 TEST_F(MetricsStateManagerTest, ForceClientIdCreation) {
277   const int64_t kFakeInstallationDate = 12345;
278   prefs_.SetInt64(prefs::kInstallDate, kFakeInstallationDate);
279 
280   {
281     std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
282 
283     // client_id shouldn't be auto-generated if metrics reporting is not
284     // enabled.
285     EXPECT_EQ(state_manager->client_id(), std::string());
286     EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 0);
287 
288     // Confirm that the initial ForceClientIdCreation call creates the client id
289     // and backs it up via MockStoreClientInfoBackup.
290     EXPECT_FALSE(stored_client_info_backup_);
291     EnableMetricsReporting();
292     state_manager->ForceClientIdCreation();
293     EXPECT_NE(state_manager->client_id(), std::string());
294     EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
295               test_begin_time_);
296 
297     ASSERT_TRUE(stored_client_info_backup_);
298     EXPECT_EQ(client_info_load_count_, 1);
299     EXPECT_EQ(state_manager->client_id(),
300               stored_client_info_backup_->client_id);
301     EXPECT_EQ(stored_client_info_backup_->installation_date,
302               kFakeInstallationDate);
303     EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
304               stored_client_info_backup_->reporting_enabled_date);
305   }
306 }
307 
TEST_F(MetricsStateManagerTest,ForceClientIdCreation_ConsentIntitially_NoInstallDate)308 TEST_F(MetricsStateManagerTest,
309        ForceClientIdCreation_ConsentIntitially_NoInstallDate) {
310   // Confirm that the initial ForceClientIdCreation call creates the install
311   // date and then backs it up via MockStoreClientInfoBackup.
312   EXPECT_FALSE(stored_client_info_backup_);
313   EnableMetricsReporting();
314   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
315 
316   ASSERT_TRUE(stored_client_info_backup_);
317   EXPECT_NE(stored_client_info_backup_->installation_date, 0);
318   EXPECT_EQ(client_info_load_count_, 1);
319 }
320 
321 #if !BUILDFLAG(IS_WIN)
TEST_F(MetricsStateManagerTest,ProvisionalClientId_PromotedToClientId)322 TEST_F(MetricsStateManagerTest, ProvisionalClientId_PromotedToClientId) {
323   // Force enable the creation of a provisional client ID on first run for
324   // consistency between Chromium and Chrome builds.
325   MetricsStateManager::enable_provisional_client_id_for_testing_ = true;
326 
327   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
328 
329   // Verify that there was a provisional client id created.
330   std::string provisional_client_id =
331       prefs_.GetString(prefs::kMetricsProvisionalClientID);
332   VerifyClientId(provisional_client_id);
333   // No client id should have been stored.
334   EXPECT_TRUE(prefs_.FindPreference(prefs::kMetricsClientID)->IsDefaultValue());
335   int low_entropy_source = state_manager->GetLowEntropySource();
336   // The default entropy provider should be the high entropy one since we a
337   // the provisional client ID.
338   state_manager->CreateEntropyProviders();
339   EXPECT_EQ(state_manager->entropy_source_returned(),
340             MetricsStateManager::ENTROPY_SOURCE_HIGH);
341 
342   // Forcing client id creation should promote the provisional client id to
343   // become the real client id and keep the low entropy source.
344   EnableMetricsReporting();
345   state_manager->ForceClientIdCreation();
346   std::string client_id = state_manager->client_id();
347   EXPECT_EQ(provisional_client_id, client_id);
348   EXPECT_EQ(prefs_.GetString(prefs::kMetricsClientID), client_id);
349   EXPECT_TRUE(prefs_.FindPreference(prefs::kMetricsProvisionalClientID)
350                   ->IsDefaultValue());
351   EXPECT_TRUE(prefs_.GetString(prefs::kMetricsProvisionalClientID).empty());
352   EXPECT_EQ(state_manager->GetLowEntropySource(), low_entropy_source);
353   EXPECT_EQ(client_info_load_count_, 1);
354 }
355 
TEST_F(MetricsStateManagerTest,ProvisionalClientId_PersistedAcrossFirstRuns)356 TEST_F(MetricsStateManagerTest, ProvisionalClientId_PersistedAcrossFirstRuns) {
357   // Force enable the creation of a provisional client ID on first run for
358   // consistency between Chromium and Chrome builds.
359   MetricsStateManager::enable_provisional_client_id_for_testing_ = true;
360 
361   std::string provisional_client_id;
362 
363   // Simulate a first run, and verify that a provisional client id is generated.
364   // We also do not enable nor disable UMA in order to simulate exiting during
365   // the first run flow.
366   {
367     std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
368     // Verify that there was a provisional client id created.
369     provisional_client_id =
370         prefs_.GetString(prefs::kMetricsProvisionalClientID);
371     VerifyClientId(provisional_client_id);
372     // No client id should have been stored.
373     EXPECT_TRUE(
374         prefs_.FindPreference(prefs::kMetricsClientID)->IsDefaultValue());
375     // The default entropy provider should be the high entropy one since we a
376     // the provisional client ID.
377     state_manager->CreateEntropyProviders();
378     EXPECT_EQ(state_manager->entropy_source_returned(),
379               MetricsStateManager::ENTROPY_SOURCE_HIGH);
380   }
381 
382   // Now, simulate a second run, and verify that the provisional client ID is
383   // the same.
384   {
385     std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
386     // Verify that the same provisional client ID as the first run is used.
387     EXPECT_EQ(provisional_client_id,
388               prefs_.GetString(prefs::kMetricsProvisionalClientID));
389     // There still should not be any stored client ID.
390     EXPECT_TRUE(prefs_.FindPreference(prefs::kMetricsClientID));
391     // The default entropy provider should be the high entropy one since we a
392     // the provisional client ID.
393     state_manager->CreateEntropyProviders();
394     EXPECT_EQ(state_manager->entropy_source_returned(),
395               MetricsStateManager::ENTROPY_SOURCE_HIGH);
396   }
397 }
398 #endif  // !BUILDFLAG(IS_WIN)
399 
TEST_F(MetricsStateManagerTest,LoadPrefs)400 TEST_F(MetricsStateManagerTest, LoadPrefs) {
401   ClientInfo client_info;
402   client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF";
403   client_info.installation_date = 1112;
404   client_info.reporting_enabled_date = 2223;
405   SetClientInfoPrefs(client_info);
406 
407   EnableMetricsReporting();
408   {
409     EXPECT_FALSE(fake_client_info_backup_);
410     EXPECT_FALSE(stored_client_info_backup_);
411 
412     std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
413 
414     // client_id should be auto-obtained from the constructor when metrics
415     // reporting is enabled.
416     EXPECT_EQ(state_manager->client_id(), client_info.client_id);
417 
418     // The backup should not be modified.
419     ASSERT_FALSE(stored_client_info_backup_);
420 
421     // Re-forcing client id creation shouldn't cause another backup and
422     // shouldn't affect the existing client id.
423     state_manager->ForceClientIdCreation();
424     EXPECT_FALSE(stored_client_info_backup_);
425     EXPECT_EQ(state_manager->client_id(), client_info.client_id);
426     EXPECT_EQ(client_info_load_count_, 0);
427   }
428 }
429 
TEST_F(MetricsStateManagerTest,PreferPrefs)430 TEST_F(MetricsStateManagerTest, PreferPrefs) {
431   ClientInfo client_info;
432   client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF";
433   client_info.installation_date = 1112;
434   client_info.reporting_enabled_date = 2223;
435   SetClientInfoPrefs(client_info);
436 
437   ClientInfo client_info2;
438   client_info2.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
439   client_info2.installation_date = 1111;
440   client_info2.reporting_enabled_date = 2222;
441   SetFakeClientInfoBackup(client_info2);
442 
443   EnableMetricsReporting();
444   {
445     // The backup should be ignored if we already have a client id.
446 
447     EXPECT_FALSE(stored_client_info_backup_);
448 
449     std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
450     EXPECT_EQ(state_manager->client_id(), client_info.client_id);
451 
452     // The backup should not be modified.
453     ASSERT_FALSE(stored_client_info_backup_);
454     EXPECT_EQ(client_info_load_count_, 0);
455   }
456 }
457 
TEST_F(MetricsStateManagerTest,RestoreBackup)458 TEST_F(MetricsStateManagerTest, RestoreBackup) {
459   ClientInfo client_info;
460   client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF";
461   client_info.installation_date = 1112;
462   client_info.reporting_enabled_date = 2223;
463   SetClientInfoPrefs(client_info);
464 
465   ClientInfo client_info2;
466   client_info2.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
467   client_info2.installation_date = 1111;
468   client_info2.reporting_enabled_date = 2222;
469   SetFakeClientInfoBackup(client_info2);
470 
471   prefs_.ClearPref(prefs::kMetricsClientID);
472   prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp);
473 
474   EnableMetricsReporting();
475   {
476     // The backup should kick in if the client id has gone missing. It should
477     // replace remaining and missing dates as well.
478 
479     EXPECT_FALSE(stored_client_info_backup_);
480 
481     std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
482     EXPECT_EQ(state_manager->client_id(), client_info2.client_id);
483     EXPECT_EQ(prefs_.GetInt64(prefs::kInstallDate),
484               client_info2.installation_date);
485     EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
486               client_info2.reporting_enabled_date);
487 
488     EXPECT_TRUE(stored_client_info_backup_);
489     EXPECT_EQ(client_info_load_count_, 1);
490   }
491 }
492 
TEST_F(MetricsStateManagerTest,ResetBackup)493 TEST_F(MetricsStateManagerTest, ResetBackup) {
494   ClientInfo client_info;
495   client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
496   client_info.installation_date = 1111;
497   client_info.reporting_enabled_date = 2222;
498 
499   SetFakeClientInfoBackup(client_info);
500   SetClientInfoPrefs(client_info);
501 
502   prefs_.SetBoolean(prefs::kMetricsResetIds, true);
503 
504   EnableMetricsReporting();
505   {
506     // Upon request to reset metrics ids, the existing backup should not be
507     // restored.
508 
509     std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
510 
511     // A brand new client id should have been generated.
512     EXPECT_NE(state_manager->client_id(), std::string());
513     EXPECT_NE(state_manager->client_id(), client_info.client_id);
514     EXPECT_TRUE(state_manager->metrics_ids_were_reset_);
515     EXPECT_EQ(state_manager->previous_client_id_, client_info.client_id);
516     EXPECT_TRUE(stored_client_info_backup_);
517     EXPECT_EQ(client_info_load_count_, 0);
518 
519     // The installation date should not have been affected.
520     EXPECT_EQ(prefs_.GetInt64(prefs::kInstallDate),
521               client_info.installation_date);
522 
523     // The metrics-reporting-enabled date will be reset to Now().
524     EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
525               test_begin_time_);
526   }
527 }
528 
TEST_F(MetricsStateManagerTest,CheckProvider)529 TEST_F(MetricsStateManagerTest, CheckProvider) {
530   int64_t kInstallDate = 1373051956;
531   int64_t kInstallDateExpected = 1373050800;  // Computed from kInstallDate.
532   int64_t kEnabledDate = 1373001211;
533   int64_t kEnabledDateExpected = 1373000400;  // Computed from kEnabledDate.
534 
535   ClientInfo client_info;
536   client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
537   client_info.installation_date = kInstallDate;
538   client_info.reporting_enabled_date = kEnabledDate;
539 
540   SetFakeClientInfoBackup(client_info);
541   SetClientInfoPrefs(client_info);
542 
543   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
544   std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider();
545   SystemProfileProto system_profile;
546   provider->ProvideSystemProfileMetrics(&system_profile);
547   EXPECT_EQ(system_profile.install_date(), kInstallDateExpected);
548   EXPECT_EQ(system_profile.uma_enabled_date(), kEnabledDateExpected);
549 
550   base::HistogramTester histogram_tester;
551   ChromeUserMetricsExtension uma_proto;
552   provider->ProvidePreviousSessionData(&uma_proto);
553   // The client_id field in the proto should not be overwritten.
554   EXPECT_FALSE(uma_proto.has_client_id());
555   // Nothing should have been emitted to the cloned install histogram.
556   histogram_tester.ExpectTotalCount("UMA.IsClonedInstall", 0);
557 }
558 
TEST_F(MetricsStateManagerTest,CheckProviderLogNormal)559 TEST_F(MetricsStateManagerTest, CheckProviderLogNormal) {
560   base::test::ScopedFeatureList scoped_feature_list;
561   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
562   // Set the random seed to have a deterministic test.
563   std::unique_ptr<MetricsProvider> provider =
564       state_manager->GetProviderAndSetRandomSeedForTesting(42);
565 
566   base::HistogramTester histogram_tester;
567   ChromeUserMetricsExtension uma_proto;
568   provider->ProvideCurrentSessionData(&uma_proto);
569   histogram_tester.ExpectUniqueSample("UMA.DataValidation.LogNormal", 189, 1);
570 }
571 
TEST_F(MetricsStateManagerTest,CheckProviderLogNormalWithParams)572 TEST_F(MetricsStateManagerTest, CheckProviderLogNormalWithParams) {
573   base::test::ScopedFeatureList scoped_feature_list;
574   scoped_feature_list.InitAndEnableFeatureWithParameters(
575       kNonUniformityValidationFeature, {{"delta", "10.0"}});
576   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
577   // Set the random seed to have a deterministic test.
578   std::unique_ptr<MetricsProvider> provider =
579       state_manager->GetProviderAndSetRandomSeedForTesting(42);
580 
581   base::HistogramTester histogram_tester;
582   ChromeUserMetricsExtension uma_proto;
583   provider->ProvideCurrentSessionData(&uma_proto);
584   histogram_tester.ExpectUniqueSample("UMA.DataValidation.LogNormal", 2081, 1);
585 }
586 
TEST_F(MetricsStateManagerTest,CheckClientIdWasNotUsedToAssignFieldTrial)587 TEST_F(MetricsStateManagerTest, CheckClientIdWasNotUsedToAssignFieldTrial) {
588   EnableMetricsReporting();
589   ClientInfo client_info;
590   client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
591   client_info.installation_date = 1373051956;
592   client_info.reporting_enabled_date = 1373001211;
593 
594   SetFakeClientInfoBackup(client_info);
595   SetClientInfoPrefs(client_info);
596 
597   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
598   std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider();
599   // The client_id in the new log doesn't match the initial_client_id we used to
600   // assign field trials.
601   prefs_.SetString(prefs::kMetricsClientID, "New client id");
602   SystemProfileProto system_profile;
603   provider->ProvideSystemProfileMetrics(&system_profile);
604   EXPECT_TRUE(system_profile.has_client_id_was_used_for_trial_assignment());
605   EXPECT_FALSE(system_profile.client_id_was_used_for_trial_assignment());
606 }
607 
TEST_F(MetricsStateManagerTest,CheckClientIdWasUsedToAssignFieldTrial)608 TEST_F(MetricsStateManagerTest, CheckClientIdWasUsedToAssignFieldTrial) {
609   EnableMetricsReporting();
610   ClientInfo client_info;
611   client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
612   client_info.installation_date = 1373051956;
613   client_info.reporting_enabled_date = 1373001211;
614 
615   SetFakeClientInfoBackup(client_info);
616   SetClientInfoPrefs(client_info);
617 
618   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
619   std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider();
620   SystemProfileProto system_profile;
621   provider->ProvideSystemProfileMetrics(&system_profile);
622   EXPECT_TRUE(system_profile.client_id_was_used_for_trial_assignment());
623 }
624 
TEST_F(MetricsStateManagerTest,CheckProviderResetIds)625 TEST_F(MetricsStateManagerTest, CheckProviderResetIds) {
626   int64_t kInstallDate = 1373001211;
627   int64_t kInstallDateExpected = 1373000400;  // Computed from kInstallDate.
628   int64_t kEnabledDate = 1373051956;
629   int64_t kEnabledDateExpected = 1373050800;  // Computed from kEnabledDate.
630 
631   ClientInfo client_info;
632   client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
633   client_info.installation_date = kInstallDate;
634   client_info.reporting_enabled_date = kEnabledDate;
635 
636   SetFakeClientInfoBackup(client_info);
637   SetClientInfoPrefs(client_info);
638 
639   // Set the reset pref to cause the IDs to be reset.
640   prefs_.SetBoolean(prefs::kMetricsResetIds, true);
641   std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
642   // Verify that MetricsStateManager has the a new client_id after reset and has
643   // the right previous_client_id (equals to the client_id before being reset).
644   EXPECT_NE(state_manager->client_id(), client_info.client_id);
645   EXPECT_TRUE(state_manager->metrics_ids_were_reset_);
646   EXPECT_EQ(state_manager->previous_client_id_, client_info.client_id);
647   EXPECT_EQ(client_info_load_count_, 0);
648 
649   uint64_t hashed_previous_client_id =
650       MetricsLog::Hash(state_manager->previous_client_id_);
651   std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider();
652   SystemProfileProto system_profile;
653   provider->ProvideSystemProfileMetrics(&system_profile);
654   EXPECT_EQ(system_profile.install_date(), kInstallDateExpected);
655   EXPECT_EQ(system_profile.uma_enabled_date(), kEnabledDateExpected);
656   auto cloned_install_info = system_profile.cloned_install_info();
657   EXPECT_EQ(cloned_install_info.count(), 1);
658   EXPECT_EQ(cloned_install_info.cloned_from_client_id(),
659             hashed_previous_client_id);
660   // Make sure the first_timestamp is updated and is the same as the
661   // last_timestamp.
662   EXPECT_EQ(cloned_install_info.last_timestamp(),
663             cloned_install_info.first_timestamp());
664   EXPECT_NE(cloned_install_info.first_timestamp(), 0);
665 
666   base::HistogramTester histogram_tester;
667   ChromeUserMetricsExtension uma_proto;
668   // The system_profile in the |uma_proto| is provided in
669   // https://source.chromium.org/chromium/chromium/src/+/main:components/metrics/metrics_service.cc;drc=4b86ff6c58f5651a4e2f44abb22d93c3593155cb;l=759
670   // and it's hard to be tested here. For logs from the previous session:
671   // 1. if the previous session is the detection session, the
672   // |uma_proto.system_profile| won't contain the latest cloned_install_info
673   // message.
674   // 2. if the previous session is a normal session, the
675   // |uma_proto.system_profile| should contain the cloned_install_info message
676   // as long as it's saved in prefs.
677   provider->ProvidePreviousSessionData(&uma_proto);
678   EXPECT_EQ(uma_proto.client_id(), hashed_previous_client_id);
679   histogram_tester.ExpectUniqueSample("UMA.IsClonedInstall", 1, 1);
680 
681   // Since we set the pref and didn't call SaveMachineId(), this should do
682   // nothing
683   provider->ProvideCurrentSessionData(&uma_proto);
684   histogram_tester.ExpectUniqueSample("UMA.IsClonedInstall", 1, 1);
685 
686   // Set the pref through SaveMachineId and expect previous to do nothing and
687   // current to log the histogram
688   prefs_.SetInteger(prefs::kMetricsMachineId, 2216820);
689   state_manager->cloned_install_detector_.SaveMachineId(&prefs_, "test");
690   provider->ProvideCurrentSessionData(&uma_proto);
691   histogram_tester.ExpectUniqueSample("UMA.IsClonedInstall", 1, 2);
692 }
693 
TEST_F(MetricsStateManagerTest,CheckProviderResetIds_PreviousIdOnlyReportInResetSession)694 TEST_F(MetricsStateManagerTest,
695        CheckProviderResetIds_PreviousIdOnlyReportInResetSession) {
696   int64_t kInstallDate = 1373001211;
697   int64_t kInstallDateExpected = 1373000400;  // Computed from kInstallDate.
698   int64_t kEnabledDate = 1373051956;
699   int64_t kEnabledDateExpected = 1373050800;  // Computed from kEnabledDate.
700 
701   ClientInfo client_info;
702   client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
703   client_info.installation_date = kInstallDate;
704   client_info.reporting_enabled_date = kEnabledDate;
705 
706   SetFakeClientInfoBackup(client_info);
707   SetClientInfoPrefs(client_info);
708 
709   // In the reset session:
710   // Set the reset pref to cause the IDs to be reset.
711   prefs_.SetBoolean(prefs::kMetricsResetIds, true);
712 
713   {
714     std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
715     EXPECT_NE(state_manager->client_id(), client_info.client_id);
716     EXPECT_TRUE(state_manager->metrics_ids_were_reset_);
717     // Verify that MetricsStateManager has the right previous_client_id (the ID
718     // that was there before being reset).
719     EXPECT_EQ(state_manager->previous_client_id_, client_info.client_id);
720     EXPECT_EQ(client_info_load_count_, 0);
721 
722     std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider();
723     SystemProfileProto system_profile;
724     provider->ProvideSystemProfileMetrics(&system_profile);
725     EXPECT_EQ(system_profile.install_date(), kInstallDateExpected);
726     EXPECT_EQ(system_profile.uma_enabled_date(), kEnabledDateExpected);
727     auto cloned_install_info = system_profile.cloned_install_info();
728     // |cloned_from_client_id| should be uploaded in the reset session.
729     EXPECT_EQ(cloned_install_info.cloned_from_client_id(),
730               MetricsLog::Hash(state_manager->previous_client_id_));
731     // Make sure the first_timestamp is updated and is the same as the
732     // last_timestamp.
733     EXPECT_EQ(cloned_install_info.count(), 1);
734     EXPECT_EQ(cloned_install_info.last_timestamp(),
735               cloned_install_info.first_timestamp());
736     EXPECT_NE(cloned_install_info.last_timestamp(), 0);
737   }
738   // In the normal session:
739   {
740     std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
741     EXPECT_FALSE(state_manager->metrics_ids_were_reset_);
742     std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider();
743     SystemProfileProto system_profile;
744     provider->ProvideSystemProfileMetrics(&system_profile);
745 
746     auto cloned_install_info = system_profile.cloned_install_info();
747     // |cloned_from_client_id| shouldn't be reported in the normal session.
748     EXPECT_FALSE(cloned_install_info.has_cloned_from_client_id());
749     // Other cloned_install_info fields should continue be reported once set.
750     EXPECT_EQ(cloned_install_info.count(), 1);
751     EXPECT_EQ(cloned_install_info.last_timestamp(),
752               cloned_install_info.first_timestamp());
753     EXPECT_NE(cloned_install_info.last_timestamp(), 0);
754   }
755 }
756 
TEST_F(MetricsStateManagerTest,UseExternalClientId)757 TEST_F(MetricsStateManagerTest, UseExternalClientId) {
758   base::HistogramTester histogram_tester;
759   std::string external_client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
760   std::unique_ptr<MetricsStateManager> state_manager(
761       CreateStateManager(external_client_id));
762   EnableMetricsReporting();
763   state_manager->ForceClientIdCreation();
764   EXPECT_EQ(external_client_id, state_manager->client_id());
765   histogram_tester.ExpectUniqueSample("UMA.ClientIdSource", 5, 1);
766 }
767 
768 }  // namespace metrics
769