File: metrics_state_manager_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (768 lines) | stat: -rw-r--r-- 31,903 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/metrics/metrics_state_manager.h"

#include <stddef.h>
#include <stdint.h>

#include <memory>
#include <string>
#include <utility>

#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "components/metrics/client_info.h"
#include "components/metrics/metrics_data_validation.h"
#include "components/metrics/metrics_log.h"
#include "components/metrics/metrics_pref_names.h"
#include "components/metrics/metrics_service.h"
#include "components/metrics/metrics_switches.h"
#include "components/metrics/test/test_enabled_state_provider.h"
#include "components/prefs/testing_pref_service.h"
#include "components/variations/pref_names.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/strings/ascii.h"

namespace metrics {
namespace {

// Verifies that the client id follows the expected pattern.
void VerifyClientId(const std::string& client_id) {
  EXPECT_EQ(36U, client_id.length());

  for (size_t i = 0; i < client_id.length(); ++i) {
    char current = client_id[i];
    if (i == 8 || i == 13 || i == 18 || i == 23)
      EXPECT_EQ('-', current);
    else
      EXPECT_TRUE(absl::ascii_isxdigit(static_cast<unsigned char>(current)));
  }
}

MATCHER(HaveClonedInstallInfo, "") {
  return (
      !arg.FindPreference(prefs::kClonedResetCount)->IsDefaultValue() &&
      !arg.FindPreference(prefs::kFirstClonedResetTimestamp)
           ->IsDefaultValue() &&
      !arg.FindPreference(prefs::kLastClonedResetTimestamp)->IsDefaultValue());
}

MATCHER(HaveNoClonedInstallInfo, "") {
  return (
      arg.FindPreference(prefs::kClonedResetCount)->IsDefaultValue() &&
      arg.FindPreference(prefs::kFirstClonedResetTimestamp)->IsDefaultValue() &&
      arg.FindPreference(prefs::kLastClonedResetTimestamp)->IsDefaultValue());
}

}  // namespace

class MetricsStateManagerTest : public testing::Test {
 public:
  MetricsStateManagerTest()
      : test_begin_time_(base::Time::Now().ToTimeT()),
        enabled_state_provider_(new TestEnabledStateProvider(false, false)) {
    MetricsService::RegisterPrefs(prefs_.registry());
  }

  MetricsStateManagerTest(const MetricsStateManagerTest&) = delete;
  MetricsStateManagerTest& operator=(const MetricsStateManagerTest&) = delete;

  std::unique_ptr<MetricsStateManager> CreateStateManager(
      const std::string& external_client_id = "") {
    std::unique_ptr<MetricsStateManager> state_manager =
        MetricsStateManager::Create(
            &prefs_, enabled_state_provider_.get(), std::wstring(),
            base::FilePath(), StartupVisibility::kUnknown, {},
            base::BindRepeating(
                &MetricsStateManagerTest::MockStoreClientInfoBackup,
                base::Unretained(this)),
            base::BindRepeating(
                &MetricsStateManagerTest::LoadFakeClientInfoBackup,
                base::Unretained(this)),
            external_client_id);
    state_manager->InstantiateFieldTrialList();
    return state_manager;
  }

  // Sets metrics reporting as enabled for testing.
  void EnableMetricsReporting() {
    enabled_state_provider_->set_consent(true);
    enabled_state_provider_->set_enabled(true);
  }

  void SetClientInfoPrefs(const ClientInfo& client_info) {
    prefs_.SetString(prefs::kMetricsClientID, client_info.client_id);
    prefs_.SetInt64(prefs::kInstallDate, client_info.installation_date);
    prefs_.SetInt64(prefs::kMetricsReportingEnabledTimestamp,
                    client_info.reporting_enabled_date);
  }

  void SetFakeClientInfoBackup(const ClientInfo& client_info) {
    fake_client_info_backup_ = std::make_unique<ClientInfo>();
    fake_client_info_backup_->client_id = client_info.client_id;
    fake_client_info_backup_->installation_date = client_info.installation_date;
    fake_client_info_backup_->reporting_enabled_date =
        client_info.reporting_enabled_date;
  }

  // The number of times that the code tries to load ClientInfo.
  int client_info_load_count_ = 0;

 protected:
  TestingPrefServiceSimple prefs_;

  // Last ClientInfo stored by the MetricsStateManager via
  // MockStoreClientInfoBackup.
  std::unique_ptr<ClientInfo> stored_client_info_backup_;

  // If set, will be returned via LoadFakeClientInfoBackup if requested by the
  // MetricsStateManager.
  std::unique_ptr<ClientInfo> fake_client_info_backup_;

  const int64_t test_begin_time_;

 private:
  // Stores the |client_info| in |stored_client_info_backup_| for verification
  // by the tests later.
  void MockStoreClientInfoBackup(const ClientInfo& client_info) {
    stored_client_info_backup_ = std::make_unique<ClientInfo>();
    stored_client_info_backup_->client_id = client_info.client_id;
    stored_client_info_backup_->installation_date =
        client_info.installation_date;
    stored_client_info_backup_->reporting_enabled_date =
        client_info.reporting_enabled_date;

    // Respect the contract that storing an empty client_id voids the existing
    // backup (required for the last section of the ForceClientIdCreation test
    // below).
    if (client_info.client_id.empty())
      fake_client_info_backup_.reset();
  }

  // Hands out a copy of |fake_client_info_backup_| if it is set.
  std::unique_ptr<ClientInfo> LoadFakeClientInfoBackup() {
    ++client_info_load_count_;
    if (!fake_client_info_backup_)
      return nullptr;

    std::unique_ptr<ClientInfo> backup_copy(new ClientInfo);
    backup_copy->client_id = fake_client_info_backup_->client_id;
    backup_copy->installation_date =
        fake_client_info_backup_->installation_date;
    backup_copy->reporting_enabled_date =
        fake_client_info_backup_->reporting_enabled_date;
    return backup_copy;
  }

  std::unique_ptr<TestEnabledStateProvider> enabled_state_provider_;
};

TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted_ConsentInitially) {
  // With consent set initially, client id should be created in the constructor.
  EnableMetricsReporting();
  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());

  const std::string client_id = state_manager->client_id();
  VerifyClientId(client_id);
}

TEST_F(MetricsStateManagerTest, ClientIdCorrectlyFormatted_ConsentLater) {
  // With consent set initially, client id should be created on consent grant.
  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
  EXPECT_EQ(std::string(), state_manager->client_id());

  EnableMetricsReporting();
  state_manager->ForceClientIdCreation();
  const std::string client_id = state_manager->client_id();
  VerifyClientId(client_id);
}

TEST_F(MetricsStateManagerTest, EntropySourceUsed_Low) {
  // Set the install date pref, which makes sure we don't trigger the first run
  // behavior where a provisional client id is generated and used to return a
  // high entropy source.
  prefs_.SetInt64(prefs::kInstallDate, base::Time::Now().ToTimeT());

  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
  state_manager->CreateEntropyProviders();
  EXPECT_EQ(state_manager->entropy_source_returned(),
            MetricsStateManager::ENTROPY_SOURCE_LOW);
  EXPECT_EQ(state_manager->initial_client_id_for_testing(), "");
}

TEST_F(MetricsStateManagerTest, EntropySourceUsed_High) {
  EnableMetricsReporting();
  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
  state_manager->CreateEntropyProviders();
  EXPECT_EQ(state_manager->entropy_source_returned(),
            MetricsStateManager::ENTROPY_SOURCE_HIGH);
  EXPECT_EQ(state_manager->initial_client_id_for_testing(),
            state_manager->client_id());
}

// Check that setting the kMetricsResetIds pref to true causes the client id to
// be reset. We do not check that the low entropy source is reset because we
// cannot ensure that metrics state manager won't generate the same id again.
TEST_F(MetricsStateManagerTest, ResetMetricsIDs) {
  // Set an initial client id in prefs. It should not be possible for the
  // metrics state manager to generate this id randomly.
  const std::string kInitialClientId = "initial client id";
  prefs_.SetString(prefs::kMetricsClientID, kInitialClientId);

  EnableMetricsReporting();

  // No cloned install info should have been stored.
  EXPECT_THAT(prefs_, HaveNoClonedInstallInfo());

  // Make sure the initial client id isn't reset by the metrics state manager.
  {
    std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
    state_manager->ForceClientIdCreation();
    EXPECT_EQ(state_manager->client_id(), kInitialClientId);
    EXPECT_FALSE(state_manager->metrics_ids_were_reset_);
    EXPECT_THAT(prefs_, HaveNoClonedInstallInfo());
  }

  // Set the reset pref to cause the IDs to be reset.
  prefs_.SetBoolean(prefs::kMetricsResetIds, true);

  // Cause the actual reset to happen.
  {
    std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
    state_manager->ForceClientIdCreation();
    EXPECT_NE(state_manager->client_id(), kInitialClientId);
    EXPECT_TRUE(state_manager->metrics_ids_were_reset_);
    EXPECT_EQ(state_manager->previous_client_id_, kInitialClientId);
    EXPECT_EQ(client_info_load_count_, 0);

    state_manager->GetLowEntropySource();

    EXPECT_FALSE(prefs_.GetBoolean(prefs::kMetricsResetIds));

    EXPECT_THAT(prefs_, HaveClonedInstallInfo());
    EXPECT_EQ(prefs_.GetInteger(prefs::kClonedResetCount), 1);
    EXPECT_EQ(prefs_.GetInt64(prefs::kFirstClonedResetTimestamp),
              prefs_.GetInt64(prefs::kLastClonedResetTimestamp));
  }

  EXPECT_NE(prefs_.GetString(prefs::kMetricsClientID), kInitialClientId);
}

TEST_F(MetricsStateManagerTest, LogHasSessionShutdownCleanly) {
  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
  prefs_.SetBoolean(prefs::kStabilityExitedCleanly, false);
  state_manager->LogHasSessionShutdownCleanly(
      /*has_session_shutdown_cleanly=*/true);
  EXPECT_TRUE(prefs_.GetBoolean(prefs::kStabilityExitedCleanly));
}

TEST_F(MetricsStateManagerTest, LogSessionHasNotYetShutdownCleanly) {
  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
  ASSERT_TRUE(prefs_.GetBoolean(prefs::kStabilityExitedCleanly));
  state_manager->LogHasSessionShutdownCleanly(
      /*has_session_shutdown_cleanly=*/false);
  EXPECT_FALSE(prefs_.GetBoolean(prefs::kStabilityExitedCleanly));
}

TEST_F(MetricsStateManagerTest, ForceClientIdCreation) {
  const int64_t kFakeInstallationDate = 12345;
  prefs_.SetInt64(prefs::kInstallDate, kFakeInstallationDate);

  {
    std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());

    // client_id shouldn't be auto-generated if metrics reporting is not
    // enabled.
    EXPECT_EQ(state_manager->client_id(), std::string());
    EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp), 0);

    // Confirm that the initial ForceClientIdCreation call creates the client id
    // and backs it up via MockStoreClientInfoBackup.
    EXPECT_FALSE(stored_client_info_backup_);
    EnableMetricsReporting();
    state_manager->ForceClientIdCreation();
    EXPECT_NE(state_manager->client_id(), std::string());
    EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
              test_begin_time_);

    ASSERT_TRUE(stored_client_info_backup_);
    EXPECT_EQ(client_info_load_count_, 1);
    EXPECT_EQ(state_manager->client_id(),
              stored_client_info_backup_->client_id);
    EXPECT_EQ(stored_client_info_backup_->installation_date,
              kFakeInstallationDate);
    EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
              stored_client_info_backup_->reporting_enabled_date);
  }
}

TEST_F(MetricsStateManagerTest,
       ForceClientIdCreation_ConsentIntitially_NoInstallDate) {
  // Confirm that the initial ForceClientIdCreation call creates the install
  // date and then backs it up via MockStoreClientInfoBackup.
  EXPECT_FALSE(stored_client_info_backup_);
  EnableMetricsReporting();
  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());

  ASSERT_TRUE(stored_client_info_backup_);
  EXPECT_NE(stored_client_info_backup_->installation_date, 0);
  EXPECT_EQ(client_info_load_count_, 1);
}

#if !BUILDFLAG(IS_WIN)
TEST_F(MetricsStateManagerTest, ProvisionalClientId_PromotedToClientId) {
  // Force enable the creation of a provisional client ID on first run for
  // consistency between Chromium and Chrome builds.
  MetricsStateManager::enable_provisional_client_id_for_testing_ = true;

  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());

  // Verify that there was a provisional client id created.
  std::string provisional_client_id =
      prefs_.GetString(prefs::kMetricsProvisionalClientID);
  VerifyClientId(provisional_client_id);
  // No client id should have been stored.
  EXPECT_TRUE(prefs_.FindPreference(prefs::kMetricsClientID)->IsDefaultValue());
  int low_entropy_source = state_manager->GetLowEntropySource();
  // The default entropy provider should be the high entropy one since we a
  // the provisional client ID.
  state_manager->CreateEntropyProviders();
  EXPECT_EQ(state_manager->entropy_source_returned(),
            MetricsStateManager::ENTROPY_SOURCE_HIGH);

  // Forcing client id creation should promote the provisional client id to
  // become the real client id and keep the low entropy source.
  EnableMetricsReporting();
  state_manager->ForceClientIdCreation();
  std::string client_id = state_manager->client_id();
  EXPECT_EQ(provisional_client_id, client_id);
  EXPECT_EQ(prefs_.GetString(prefs::kMetricsClientID), client_id);
  EXPECT_TRUE(prefs_.FindPreference(prefs::kMetricsProvisionalClientID)
                  ->IsDefaultValue());
  EXPECT_TRUE(prefs_.GetString(prefs::kMetricsProvisionalClientID).empty());
  EXPECT_EQ(state_manager->GetLowEntropySource(), low_entropy_source);
  EXPECT_EQ(client_info_load_count_, 1);
}

TEST_F(MetricsStateManagerTest, ProvisionalClientId_PersistedAcrossFirstRuns) {
  // Force enable the creation of a provisional client ID on first run for
  // consistency between Chromium and Chrome builds.
  MetricsStateManager::enable_provisional_client_id_for_testing_ = true;

  std::string provisional_client_id;

  // Simulate a first run, and verify that a provisional client id is generated.
  // We also do not enable nor disable UMA in order to simulate exiting during
  // the first run flow.
  {
    std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
    // Verify that there was a provisional client id created.
    provisional_client_id =
        prefs_.GetString(prefs::kMetricsProvisionalClientID);
    VerifyClientId(provisional_client_id);
    // No client id should have been stored.
    EXPECT_TRUE(
        prefs_.FindPreference(prefs::kMetricsClientID)->IsDefaultValue());
    // The default entropy provider should be the high entropy one since we a
    // the provisional client ID.
    state_manager->CreateEntropyProviders();
    EXPECT_EQ(state_manager->entropy_source_returned(),
              MetricsStateManager::ENTROPY_SOURCE_HIGH);
  }

  // Now, simulate a second run, and verify that the provisional client ID is
  // the same.
  {
    std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
    // Verify that the same provisional client ID as the first run is used.
    EXPECT_EQ(provisional_client_id,
              prefs_.GetString(prefs::kMetricsProvisionalClientID));
    // There still should not be any stored client ID.
    EXPECT_TRUE(prefs_.FindPreference(prefs::kMetricsClientID));
    // The default entropy provider should be the high entropy one since we a
    // the provisional client ID.
    state_manager->CreateEntropyProviders();
    EXPECT_EQ(state_manager->entropy_source_returned(),
              MetricsStateManager::ENTROPY_SOURCE_HIGH);
  }
}
#endif  // !BUILDFLAG(IS_WIN)

TEST_F(MetricsStateManagerTest, LoadPrefs) {
  ClientInfo client_info;
  client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF";
  client_info.installation_date = 1112;
  client_info.reporting_enabled_date = 2223;
  SetClientInfoPrefs(client_info);

  EnableMetricsReporting();
  {
    EXPECT_FALSE(fake_client_info_backup_);
    EXPECT_FALSE(stored_client_info_backup_);

    std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());

    // client_id should be auto-obtained from the constructor when metrics
    // reporting is enabled.
    EXPECT_EQ(state_manager->client_id(), client_info.client_id);

    // The backup should not be modified.
    ASSERT_FALSE(stored_client_info_backup_);

    // Re-forcing client id creation shouldn't cause another backup and
    // shouldn't affect the existing client id.
    state_manager->ForceClientIdCreation();
    EXPECT_FALSE(stored_client_info_backup_);
    EXPECT_EQ(state_manager->client_id(), client_info.client_id);
    EXPECT_EQ(client_info_load_count_, 0);
  }
}

TEST_F(MetricsStateManagerTest, PreferPrefs) {
  ClientInfo client_info;
  client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF";
  client_info.installation_date = 1112;
  client_info.reporting_enabled_date = 2223;
  SetClientInfoPrefs(client_info);

  ClientInfo client_info2;
  client_info2.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
  client_info2.installation_date = 1111;
  client_info2.reporting_enabled_date = 2222;
  SetFakeClientInfoBackup(client_info2);

  EnableMetricsReporting();
  {
    // The backup should be ignored if we already have a client id.

    EXPECT_FALSE(stored_client_info_backup_);

    std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
    EXPECT_EQ(state_manager->client_id(), client_info.client_id);

    // The backup should not be modified.
    ASSERT_FALSE(stored_client_info_backup_);
    EXPECT_EQ(client_info_load_count_, 0);
  }
}

TEST_F(MetricsStateManagerTest, RestoreBackup) {
  ClientInfo client_info;
  client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEF";
  client_info.installation_date = 1112;
  client_info.reporting_enabled_date = 2223;
  SetClientInfoPrefs(client_info);

  ClientInfo client_info2;
  client_info2.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
  client_info2.installation_date = 1111;
  client_info2.reporting_enabled_date = 2222;
  SetFakeClientInfoBackup(client_info2);

  prefs_.ClearPref(prefs::kMetricsClientID);
  prefs_.ClearPref(prefs::kMetricsReportingEnabledTimestamp);

  EnableMetricsReporting();
  {
    // The backup should kick in if the client id has gone missing. It should
    // replace remaining and missing dates as well.

    EXPECT_FALSE(stored_client_info_backup_);

    std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
    EXPECT_EQ(state_manager->client_id(), client_info2.client_id);
    EXPECT_EQ(prefs_.GetInt64(prefs::kInstallDate),
              client_info2.installation_date);
    EXPECT_EQ(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
              client_info2.reporting_enabled_date);

    EXPECT_TRUE(stored_client_info_backup_);
    EXPECT_EQ(client_info_load_count_, 1);
  }
}

TEST_F(MetricsStateManagerTest, ResetBackup) {
  ClientInfo client_info;
  client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
  client_info.installation_date = 1111;
  client_info.reporting_enabled_date = 2222;

  SetFakeClientInfoBackup(client_info);
  SetClientInfoPrefs(client_info);

  prefs_.SetBoolean(prefs::kMetricsResetIds, true);

  EnableMetricsReporting();
  {
    // Upon request to reset metrics ids, the existing backup should not be
    // restored.

    std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());

    // A brand new client id should have been generated.
    EXPECT_NE(state_manager->client_id(), std::string());
    EXPECT_NE(state_manager->client_id(), client_info.client_id);
    EXPECT_TRUE(state_manager->metrics_ids_were_reset_);
    EXPECT_EQ(state_manager->previous_client_id_, client_info.client_id);
    EXPECT_TRUE(stored_client_info_backup_);
    EXPECT_EQ(client_info_load_count_, 0);

    // The installation date should not have been affected.
    EXPECT_EQ(prefs_.GetInt64(prefs::kInstallDate),
              client_info.installation_date);

    // The metrics-reporting-enabled date will be reset to Now().
    EXPECT_GE(prefs_.GetInt64(prefs::kMetricsReportingEnabledTimestamp),
              test_begin_time_);
  }
}

TEST_F(MetricsStateManagerTest, CheckProvider) {
  int64_t kInstallDate = 1373051956;
  int64_t kInstallDateExpected = 1373050800;  // Computed from kInstallDate.
  int64_t kEnabledDate = 1373001211;
  int64_t kEnabledDateExpected = 1373000400;  // Computed from kEnabledDate.

  ClientInfo client_info;
  client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
  client_info.installation_date = kInstallDate;
  client_info.reporting_enabled_date = kEnabledDate;

  SetFakeClientInfoBackup(client_info);
  SetClientInfoPrefs(client_info);

  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
  std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider();
  SystemProfileProto system_profile;
  provider->ProvideSystemProfileMetrics(&system_profile);
  EXPECT_EQ(system_profile.install_date(), kInstallDateExpected);
  EXPECT_EQ(system_profile.uma_enabled_date(), kEnabledDateExpected);

  base::HistogramTester histogram_tester;
  ChromeUserMetricsExtension uma_proto;
  provider->ProvidePreviousSessionData(&uma_proto);
  // The client_id field in the proto should not be overwritten.
  EXPECT_FALSE(uma_proto.has_client_id());
  // Nothing should have been emitted to the cloned install histogram.
  histogram_tester.ExpectTotalCount("UMA.IsClonedInstall", 0);
}

TEST_F(MetricsStateManagerTest, CheckProviderLogNormal) {
  base::test::ScopedFeatureList scoped_feature_list;
  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
  // Set the random seed to have a deterministic test.
  std::unique_ptr<MetricsProvider> provider =
      state_manager->GetProviderAndSetRandomSeedForTesting(42);

  base::HistogramTester histogram_tester;
  ChromeUserMetricsExtension uma_proto;
  provider->ProvideCurrentSessionData(&uma_proto);
  histogram_tester.ExpectUniqueSample("UMA.DataValidation.LogNormal", 189, 1);
}

TEST_F(MetricsStateManagerTest, CheckProviderLogNormalWithParams) {
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeatureWithParameters(
      kNonUniformityValidationFeature, {{"delta", "10.0"}});
  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
  // Set the random seed to have a deterministic test.
  std::unique_ptr<MetricsProvider> provider =
      state_manager->GetProviderAndSetRandomSeedForTesting(42);

  base::HistogramTester histogram_tester;
  ChromeUserMetricsExtension uma_proto;
  provider->ProvideCurrentSessionData(&uma_proto);
  histogram_tester.ExpectUniqueSample("UMA.DataValidation.LogNormal", 2081, 1);
}

TEST_F(MetricsStateManagerTest, CheckClientIdWasNotUsedToAssignFieldTrial) {
  EnableMetricsReporting();
  ClientInfo client_info;
  client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
  client_info.installation_date = 1373051956;
  client_info.reporting_enabled_date = 1373001211;

  SetFakeClientInfoBackup(client_info);
  SetClientInfoPrefs(client_info);

  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
  std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider();
  // The client_id in the new log doesn't match the initial_client_id we used to
  // assign field trials.
  prefs_.SetString(prefs::kMetricsClientID, "New client id");
  SystemProfileProto system_profile;
  provider->ProvideSystemProfileMetrics(&system_profile);
  EXPECT_TRUE(system_profile.has_client_id_was_used_for_trial_assignment());
  EXPECT_FALSE(system_profile.client_id_was_used_for_trial_assignment());
}

TEST_F(MetricsStateManagerTest, CheckClientIdWasUsedToAssignFieldTrial) {
  EnableMetricsReporting();
  ClientInfo client_info;
  client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
  client_info.installation_date = 1373051956;
  client_info.reporting_enabled_date = 1373001211;

  SetFakeClientInfoBackup(client_info);
  SetClientInfoPrefs(client_info);

  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
  std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider();
  SystemProfileProto system_profile;
  provider->ProvideSystemProfileMetrics(&system_profile);
  EXPECT_TRUE(system_profile.client_id_was_used_for_trial_assignment());
}

TEST_F(MetricsStateManagerTest, CheckProviderResetIds) {
  int64_t kInstallDate = 1373001211;
  int64_t kInstallDateExpected = 1373000400;  // Computed from kInstallDate.
  int64_t kEnabledDate = 1373051956;
  int64_t kEnabledDateExpected = 1373050800;  // Computed from kEnabledDate.

  ClientInfo client_info;
  client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
  client_info.installation_date = kInstallDate;
  client_info.reporting_enabled_date = kEnabledDate;

  SetFakeClientInfoBackup(client_info);
  SetClientInfoPrefs(client_info);

  // Set the reset pref to cause the IDs to be reset.
  prefs_.SetBoolean(prefs::kMetricsResetIds, true);
  std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
  // Verify that MetricsStateManager has the a new client_id after reset and has
  // the right previous_client_id (equals to the client_id before being reset).
  EXPECT_NE(state_manager->client_id(), client_info.client_id);
  EXPECT_TRUE(state_manager->metrics_ids_were_reset_);
  EXPECT_EQ(state_manager->previous_client_id_, client_info.client_id);
  EXPECT_EQ(client_info_load_count_, 0);

  uint64_t hashed_previous_client_id =
      MetricsLog::Hash(state_manager->previous_client_id_);
  std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider();
  SystemProfileProto system_profile;
  provider->ProvideSystemProfileMetrics(&system_profile);
  EXPECT_EQ(system_profile.install_date(), kInstallDateExpected);
  EXPECT_EQ(system_profile.uma_enabled_date(), kEnabledDateExpected);
  auto cloned_install_info = system_profile.cloned_install_info();
  EXPECT_EQ(cloned_install_info.count(), 1);
  EXPECT_EQ(cloned_install_info.cloned_from_client_id(),
            hashed_previous_client_id);
  // Make sure the first_timestamp is updated and is the same as the
  // last_timestamp.
  EXPECT_EQ(cloned_install_info.last_timestamp(),
            cloned_install_info.first_timestamp());
  EXPECT_NE(cloned_install_info.first_timestamp(), 0);

  base::HistogramTester histogram_tester;
  ChromeUserMetricsExtension uma_proto;
  // The system_profile in the |uma_proto| is provided in
  // https://source.chromium.org/chromium/chromium/src/+/main:components/metrics/metrics_service.cc;drc=4b86ff6c58f5651a4e2f44abb22d93c3593155cb;l=759
  // and it's hard to be tested here. For logs from the previous session:
  // 1. if the previous session is the detection session, the
  // |uma_proto.system_profile| won't contain the latest cloned_install_info
  // message.
  // 2. if the previous session is a normal session, the
  // |uma_proto.system_profile| should contain the cloned_install_info message
  // as long as it's saved in prefs.
  provider->ProvidePreviousSessionData(&uma_proto);
  EXPECT_EQ(uma_proto.client_id(), hashed_previous_client_id);
  histogram_tester.ExpectUniqueSample("UMA.IsClonedInstall", 1, 1);

  // Since we set the pref and didn't call SaveMachineId(), this should do
  // nothing
  provider->ProvideCurrentSessionData(&uma_proto);
  histogram_tester.ExpectUniqueSample("UMA.IsClonedInstall", 1, 1);

  // Set the pref through SaveMachineId and expect previous to do nothing and
  // current to log the histogram
  prefs_.SetInteger(prefs::kMetricsMachineId, 2216820);
  state_manager->cloned_install_detector_.SaveMachineId(&prefs_, "test");
  provider->ProvideCurrentSessionData(&uma_proto);
  histogram_tester.ExpectUniqueSample("UMA.IsClonedInstall", 1, 2);
}

TEST_F(MetricsStateManagerTest,
       CheckProviderResetIds_PreviousIdOnlyReportInResetSession) {
  int64_t kInstallDate = 1373001211;
  int64_t kInstallDateExpected = 1373000400;  // Computed from kInstallDate.
  int64_t kEnabledDate = 1373051956;
  int64_t kEnabledDateExpected = 1373050800;  // Computed from kEnabledDate.

  ClientInfo client_info;
  client_info.client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
  client_info.installation_date = kInstallDate;
  client_info.reporting_enabled_date = kEnabledDate;

  SetFakeClientInfoBackup(client_info);
  SetClientInfoPrefs(client_info);

  // In the reset session:
  // Set the reset pref to cause the IDs to be reset.
  prefs_.SetBoolean(prefs::kMetricsResetIds, true);

  {
    std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
    EXPECT_NE(state_manager->client_id(), client_info.client_id);
    EXPECT_TRUE(state_manager->metrics_ids_were_reset_);
    // Verify that MetricsStateManager has the right previous_client_id (the ID
    // that was there before being reset).
    EXPECT_EQ(state_manager->previous_client_id_, client_info.client_id);
    EXPECT_EQ(client_info_load_count_, 0);

    std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider();
    SystemProfileProto system_profile;
    provider->ProvideSystemProfileMetrics(&system_profile);
    EXPECT_EQ(system_profile.install_date(), kInstallDateExpected);
    EXPECT_EQ(system_profile.uma_enabled_date(), kEnabledDateExpected);
    auto cloned_install_info = system_profile.cloned_install_info();
    // |cloned_from_client_id| should be uploaded in the reset session.
    EXPECT_EQ(cloned_install_info.cloned_from_client_id(),
              MetricsLog::Hash(state_manager->previous_client_id_));
    // Make sure the first_timestamp is updated and is the same as the
    // last_timestamp.
    EXPECT_EQ(cloned_install_info.count(), 1);
    EXPECT_EQ(cloned_install_info.last_timestamp(),
              cloned_install_info.first_timestamp());
    EXPECT_NE(cloned_install_info.last_timestamp(), 0);
  }
  // In the normal session:
  {
    std::unique_ptr<MetricsStateManager> state_manager(CreateStateManager());
    EXPECT_FALSE(state_manager->metrics_ids_were_reset_);
    std::unique_ptr<MetricsProvider> provider = state_manager->GetProvider();
    SystemProfileProto system_profile;
    provider->ProvideSystemProfileMetrics(&system_profile);

    auto cloned_install_info = system_profile.cloned_install_info();
    // |cloned_from_client_id| shouldn't be reported in the normal session.
    EXPECT_FALSE(cloned_install_info.has_cloned_from_client_id());
    // Other cloned_install_info fields should continue be reported once set.
    EXPECT_EQ(cloned_install_info.count(), 1);
    EXPECT_EQ(cloned_install_info.last_timestamp(),
              cloned_install_info.first_timestamp());
    EXPECT_NE(cloned_install_info.last_timestamp(), 0);
  }
}

TEST_F(MetricsStateManagerTest, UseExternalClientId) {
  base::HistogramTester histogram_tester;
  std::string external_client_id = "AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE";
  std::unique_ptr<MetricsStateManager> state_manager(
      CreateStateManager(external_client_id));
  EnableMetricsReporting();
  state_manager->ForceClientIdCreation();
  EXPECT_EQ(external_client_id, state_manager->client_id());
  histogram_tester.ExpectUniqueSample("UMA.ClientIdSource", 5, 1);
}

}  // namespace metrics