File: known_user_unittest.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • 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 (736 lines) | stat: -rw-r--r-- 28,052 bytes parent folder | download
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
// Copyright 2021 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/user_manager/known_user.h"

#include <memory>
#include <utility>

#include "base/json/values_util.h"
#include "base/memory/raw_ptr.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "components/account_id/account_id.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/prefs/testing_pref_service.h"
#include "components/user_manager/fake_user_manager.h"
#include "components/user_manager/scoped_user_manager.h"
#include "components/user_manager/user_manager_base.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace user_manager {
namespace {
absl::optional<std::string> GetStringPrefValue(KnownUser* known_user,
                                               const AccountId& account_id,
                                               const char* pref_name) {
  if (const std::string* value =
          known_user->FindStringPath(account_id, pref_name)) {
    return *value;
  }
  return absl::nullopt;
}
}  // namespace

// Base class for tests of known_user.
// Sets up global objects necessary for known_user to be able to access
// local_state.
class KnownUserTest : public testing::Test {
 public:
  KnownUserTest() {
    auto fake_user_manager = std::make_unique<FakeUserManager>();
    fake_user_manager_ = fake_user_manager.get();
    scoped_user_manager_ =
        std::make_unique<ScopedUserManager>(std::move(fake_user_manager));

    UserManagerBase::RegisterPrefs(local_state_.registry());
  }
  ~KnownUserTest() override = default;

  KnownUserTest(const KnownUserTest& other) = delete;
  KnownUserTest& operator=(const KnownUserTest& other) = delete;

 protected:
  const AccountId kDefaultAccountId =
      AccountId::FromUserEmailGaiaId("default_account@gmail.com",
                                     "fake-gaia-id");
  FakeUserManager* fake_user_manager() { return fake_user_manager_; }

  PrefService* local_state() { return &local_state_; }

  const base::Value::Dict* FindPrefs(const AccountId& account_id) {
    return KnownUser(local_state()).FindPrefs(account_id);
  }

 private:
  base::test::TaskEnvironment task_environment_{
      base::test::TaskEnvironment::MainThreadType::UI};

  // Owned by |scoped_user_manager_|.
  raw_ptr<FakeUserManager, DanglingUntriaged | ExperimentalAsh>
      fake_user_manager_ = nullptr;
  std::unique_ptr<ScopedUserManager> scoped_user_manager_;
  TestingPrefServiceSimple local_state_;
};

TEST_F(KnownUserTest, FindPrefsNonExisting) {
  EXPECT_FALSE(FindPrefs(kDefaultAccountId));
}

TEST_F(KnownUserTest, FindPrefsExisting) {
  KnownUser known_user(local_state());
  const std::string kCustomPrefName = "custom_pref";
  known_user.SetStringPref(kDefaultAccountId, kCustomPrefName, "value");

  const base::Value::Dict* value = FindPrefs(kDefaultAccountId);
  ASSERT_TRUE(value);

  const std::string* pref_value = value->FindString(kCustomPrefName);
  ASSERT_TRUE(pref_value);
  EXPECT_EQ(*pref_value, "value");
}

TEST_F(KnownUserTest, FindPrefsIgnoresEphemeralGaiaUsers) {
  KnownUser known_user(local_state());
  const AccountId kAccountIdEphemeralGaia =
      AccountId::FromUserEmailGaiaId("account2@gmail.com", "gaia_id_2");
  const AccountId kAccountIdEphemeralAd =
      AccountId::AdFromUserEmailObjGuid("account4@gmail.com", "guid_4");
  fake_user_manager()->SetUserNonCryptohomeDataEphemeral(
      kAccountIdEphemeralGaia,
      /*is_ephemeral=*/true);
  fake_user_manager()->SetUserNonCryptohomeDataEphemeral(kAccountIdEphemeralAd,
                                                         /*is_ephemeral=*/true);
  const std::string kCustomPrefName = "custom_pref";
  known_user.SetStringPref(kAccountIdEphemeralGaia, kCustomPrefName, "value");
  known_user.SetStringPref(kAccountIdEphemeralAd, kCustomPrefName, "value");

  EXPECT_FALSE(FindPrefs(kAccountIdEphemeralGaia));

  EXPECT_TRUE(FindPrefs(kAccountIdEphemeralAd));
}

TEST_F(KnownUserTest, FindPrefsMatchForUnknownAccountType) {
  KnownUser known_user(local_state());
  // All account ids have the same e-mail
  const AccountId kAccountIdUnknown =
      AccountId::FromUserEmail("account1@gmail.com");
  const AccountId kAccountIdGaia =
      AccountId::FromUserEmailGaiaId("account1@gmail.com", "gaia_id_2");
  const AccountId kAccountIdAd =
      AccountId::AdFromUserEmailObjGuid("account1@gmail.com", "guid");

  known_user.SetStringPref(kAccountIdUnknown, "some_pref", "some_value");

  EXPECT_TRUE(FindPrefs(kAccountIdUnknown));
  EXPECT_TRUE(FindPrefs(kAccountIdGaia));
  EXPECT_TRUE(FindPrefs(kAccountIdAd));
}

TEST_F(KnownUserTest, FindPrefsMatchForGaiaAccountWithEmail) {
  KnownUser known_user(local_state());
  const char* kEmailA = "a@gmail.com";
  const char* kEmailB = "b@gmail.com";
  const char* kGaiaIdA = "a";
  const char* kGaiaIdB = "b";

  known_user.SaveKnownUser(AccountId::FromUserEmailGaiaId(kEmailA, kGaiaIdA));

  // Finding by itself should work
  EXPECT_TRUE(FindPrefs(AccountId::FromUserEmailGaiaId(kEmailA, kGaiaIdA)));
  // Finding by gaia id should also work even if the e-mail doesn't match.
  EXPECT_TRUE(FindPrefs(AccountId::FromUserEmailGaiaId(kEmailB, kGaiaIdA)));
  // Finding by e-mail should also work even if the gaia id doesn't match.
  // TODO(https://crbug.com/1190902): This should likely be EXPECT_FALSE going
  // forward.
  EXPECT_TRUE(FindPrefs(AccountId::FromUserEmailGaiaId(kEmailA, kGaiaIdB)));

  // An unrelated gaia AccountId with the same Account Type doesn't find
  // anything.
  EXPECT_FALSE(FindPrefs(AccountId::FromUserEmailGaiaId(kEmailB, kGaiaIdB)));

  // Looking up an AccountId stored as gaia by an unknown-type AccountId with
  // the same e-mail address succeeds.
  EXPECT_TRUE(FindPrefs(AccountId::FromUserEmail(kEmailA)));

  // Looking up an AccountId stored as gaia by an AccountId with type Ad fails.
  EXPECT_FALSE(FindPrefs(AccountId::AdFromUserEmailObjGuid(kEmailA, "guid")));
}

TEST_F(KnownUserTest, FindPrefsMatchForAdAccountWithEmail) {
  KnownUser known_user(local_state());
  const std::string kEmailA = "a@gmail.com";
  const std::string kEmailB = "b@gmail.com";

  known_user.SaveKnownUser(AccountId::AdFromUserEmailObjGuid(kEmailA, "a"));

  // Finding by itself should work
  EXPECT_TRUE(FindPrefs(AccountId::AdFromUserEmailObjGuid(kEmailA, "a")));
  // Finding by guid should also work even if the e-mail doesn't match.
  EXPECT_TRUE(FindPrefs(AccountId::AdFromUserEmailObjGuid(kEmailB, "a")));
  // Finding by e-mail should also work even if the guid doesn't match.
  EXPECT_TRUE(FindPrefs(AccountId::AdFromUserEmailObjGuid(kEmailA, "b")));

  // An unrelated AD AccountId with the same Account Type doesn't find
  // anything.
  EXPECT_FALSE(FindPrefs(AccountId::AdFromUserEmailObjGuid(kEmailB, "b")));

  // Looking up an AccountId stored as AD by an unknown-type AccountId with
  // the same e-mail address succeeds.
  EXPECT_TRUE(FindPrefs(AccountId::FromUserEmail(kEmailA)));

  // Looking up an AccountId stored as AD by an AccountId with type gaia fails.
  EXPECT_FALSE(FindPrefs(AccountId::FromUserEmailGaiaId(kEmailA, "gaia_id")));
}

TEST_F(KnownUserTest, UpdatePrefsWithoutClear) {
  KnownUser known_user(local_state());
  constexpr char kPrefName1[] = "pref1";
  constexpr char kPrefName2[] = "pref2";

  known_user.SetPath(kDefaultAccountId, kPrefName1,
                     base::Value("pref1_value1"));

  known_user.SetPath(kDefaultAccountId, kPrefName1,
                     base::Value("pref1_value2"));

  known_user.SetPath(kDefaultAccountId, kPrefName2,
                     base::Value("pref2_value1"));

  EXPECT_EQ(absl::make_optional(std::string("pref1_value2")),
            GetStringPrefValue(&known_user, kDefaultAccountId, kPrefName1));
  EXPECT_EQ(absl::make_optional(std::string("pref2_value1")),
            GetStringPrefValue(&known_user, kDefaultAccountId, kPrefName2));
}

TEST_F(KnownUserTest, UpdatePrefsWithClear) {
  KnownUser known_user(local_state());
  constexpr char kPrefName1[] = "pref1";
  constexpr char kPrefName2[] = "pref2";

  known_user.SetPath(kDefaultAccountId, kPrefName1,
                     base::Value("pref1_value1"));

  known_user.SetPath(kDefaultAccountId, kPrefName2,
                     base::Value("pref2_value1"));

  known_user.SetPath(kDefaultAccountId, kPrefName1, absl::nullopt);

  EXPECT_EQ(absl::nullopt,
            GetStringPrefValue(&known_user, kDefaultAccountId, kPrefName1));
  EXPECT_EQ(absl::make_optional(std::string("pref2_value1")),
            GetStringPrefValue(&known_user, kDefaultAccountId, kPrefName2));
}

TEST_F(KnownUserTest, GetKnownAccountIdsNoAccounts) {
  KnownUser known_user(local_state());
  EXPECT_THAT(known_user.GetKnownAccountIds(), testing::IsEmpty());
}

TEST_F(KnownUserTest, GetKnownAccountIdsWithAccounts) {
  KnownUser known_user(local_state());
  const AccountId kAccountIdGaia =
      AccountId::FromUserEmailGaiaId("account2@gmail.com", "gaia_id");
  const AccountId kAccountIdAd =
      AccountId::AdFromUserEmailObjGuid("account3@gmail.com", "obj_guid");

  known_user.SaveKnownUser(kAccountIdGaia);
  known_user.SaveKnownUser(kAccountIdAd);

  EXPECT_THAT(known_user.GetKnownAccountIds(),
              testing::UnorderedElementsAre(kAccountIdGaia, kAccountIdAd));
}

TEST_F(KnownUserTest, SaveKnownUserIgnoresUnknownType) {
  KnownUser known_user(local_state());
  const AccountId kAccountIdUnknown =
      AccountId::FromUserEmail("account2@gmail.com");

  known_user.SaveKnownUser(kAccountIdUnknown);

  EXPECT_THAT(known_user.GetKnownAccountIds(), testing::IsEmpty());
}

TEST_F(KnownUserTest, SaveKnownUserIgnoresEphemeralGaiaUsers) {
  KnownUser known_user(local_state());
  const AccountId kAccountIdNonEphemeralGaia =
      AccountId::FromUserEmailGaiaId("account1@gmail.com", "gaia_id_1");
  const AccountId kAccountIdEphemeralGaia =
      AccountId::FromUserEmailGaiaId("account2@gmail.com", "gaia_id_2");
  const AccountId kAccountIdNonEphemeralAd =
      AccountId::AdFromUserEmailObjGuid("account3@gmail.com", "guid_3");
  const AccountId kAccountIdEphemeralAd =
      AccountId::AdFromUserEmailObjGuid("account4@gmail.com", "guid_4");

  fake_user_manager()->SetUserNonCryptohomeDataEphemeral(
      kAccountIdEphemeralGaia,
      /*is_ephemeral=*/true);
  fake_user_manager()->SetUserNonCryptohomeDataEphemeral(kAccountIdEphemeralAd,
                                                         /*is_ephemeral=*/true);

  known_user.SaveKnownUser(kAccountIdNonEphemeralGaia);
  known_user.SaveKnownUser(kAccountIdEphemeralGaia);
  known_user.SaveKnownUser(kAccountIdNonEphemeralAd);
  known_user.SaveKnownUser(kAccountIdEphemeralAd);

  EXPECT_THAT(known_user.GetKnownAccountIds(),
              testing::UnorderedElementsAre(kAccountIdNonEphemeralGaia,
                                            kAccountIdNonEphemeralAd,
                                            kAccountIdEphemeralAd));
}

TEST_F(KnownUserTest, UpdateIdForGaiaAccount) {
  KnownUser known_user(local_state());
  const AccountId kAccountIdUnknown =
      AccountId::FromUserEmail("account1@gmail.com");
  known_user.SetStringPref(kAccountIdUnknown, "some_pref", "some_value");
  EXPECT_THAT(known_user.GetKnownAccountIds(),
              testing::UnorderedElementsAre(kAccountIdUnknown));

  const AccountId kAccountIdGaia =
      AccountId::FromUserEmailGaiaId("account1@gmail.com", "gaia_id");
  known_user.UpdateId(kAccountIdGaia);
  EXPECT_THAT(known_user.GetKnownAccountIds(),
              testing::UnorderedElementsAre(kAccountIdGaia));
}

TEST_F(KnownUserTest, UpdateIdForAdAccount) {
  KnownUser known_user(local_state());
  const AccountId kAccountIdUnknown =
      AccountId::FromUserEmail("account1@gmail.com");
  known_user.SetStringPref(kAccountIdUnknown, "some_pref", "some_value");
  EXPECT_THAT(known_user.GetKnownAccountIds(),
              testing::UnorderedElementsAre(kAccountIdUnknown));

  const AccountId kAccountIdAd =
      AccountId::AdFromUserEmailObjGuid("account1@gmail.com", "guid");
  known_user.UpdateId(kAccountIdAd);
  EXPECT_THAT(known_user.GetKnownAccountIds(),
              testing::UnorderedElementsAre(kAccountIdAd));
}

TEST_F(KnownUserTest, FindGaiaIdForGaiaAccount) {
  KnownUser known_user(local_state());
  const AccountId kAccountIdGaia =
      AccountId::FromUserEmailGaiaId("account1@gmail.com", "gaia_id");
  known_user.SaveKnownUser(kAccountIdGaia);

  const std::string* gaia_id = known_user.FindGaiaID(kAccountIdGaia);
  ASSERT_TRUE(gaia_id);
  EXPECT_EQ(*gaia_id, "gaia_id");
}

TEST_F(KnownUserTest, FindGaiaIdForAdAccount) {
  KnownUser known_user(local_state());
  const AccountId kAccountIdAd =
      AccountId::AdFromUserEmailObjGuid("account1@gmail.com", "guid");
  known_user.SaveKnownUser(kAccountIdAd);

  EXPECT_FALSE(known_user.FindGaiaID(kAccountIdAd));
}

// TODO(https://crbug.com/1148457): Add tests for GetAccountId.

TEST_F(KnownUserTest, RemovePrefOnCustomPref) {
  KnownUser known_user(local_state());
  const std::string kCustomPrefName = "custom_pref";

  known_user.SetStringPref(kDefaultAccountId, kCustomPrefName, "value");
  EXPECT_TRUE(known_user.FindStringPath(kDefaultAccountId, kCustomPrefName));

  known_user.RemovePref(kDefaultAccountId, kCustomPrefName);
  EXPECT_FALSE(known_user.FindStringPath(kDefaultAccountId, kCustomPrefName));
}

TEST_F(KnownUserTest, RemovePrefOnReservedPref) {
  KnownUser known_user(local_state());
  const std::string kReservedPrefName = "device_id";

  known_user.SetStringPref(kDefaultAccountId, kReservedPrefName, "value");
  // Don't verify the message because on some builds CHECK failures do not print
  // debug messages (https://crbug.com/1198519).
  ASSERT_DEATH(known_user.RemovePref(kDefaultAccountId, kReservedPrefName), "");
}

TEST_F(KnownUserTest, DeviceId) {
  KnownUser known_user(local_state());
  EXPECT_EQ(known_user.GetDeviceId(kDefaultAccountId), std::string());

  known_user.SetDeviceId(kDefaultAccountId, "test");

  EXPECT_EQ(known_user.GetDeviceId(kDefaultAccountId), "test");
}

TEST_F(KnownUserTest, GAPSCookie) {
  KnownUser known_user(local_state());
  EXPECT_EQ(known_user.GetGAPSCookie(kDefaultAccountId), std::string());

  known_user.SetGAPSCookie(kDefaultAccountId, "test");

  EXPECT_EQ(known_user.GetGAPSCookie(kDefaultAccountId), "test");
}

TEST_F(KnownUserTest, UsingSAML) {
  KnownUser known_user(local_state());
  EXPECT_FALSE(known_user.IsUsingSAML(kDefaultAccountId));

  known_user.UpdateUsingSAML(kDefaultAccountId, /*using_saml=*/true);
  EXPECT_TRUE(known_user.IsUsingSAML(kDefaultAccountId));
}

TEST_F(KnownUserTest, UsingSAMLPrincipalsAPI) {
  KnownUser known_user(local_state());
  EXPECT_FALSE(known_user.GetIsUsingSAMLPrincipalsAPI(kDefaultAccountId));

  known_user.UpdateIsUsingSAMLPrincipalsAPI(kDefaultAccountId,
                                            /*using_saml=*/true);
  EXPECT_TRUE(known_user.GetIsUsingSAMLPrincipalsAPI(kDefaultAccountId));
}

TEST_F(KnownUserTest, ProfileRequiresPolicy) {
  KnownUser known_user(local_state());
  EXPECT_EQ(known_user.GetProfileRequiresPolicy(kDefaultAccountId),
            ProfileRequiresPolicy::kUnknown);

  known_user.SetProfileRequiresPolicy(kDefaultAccountId,
                                      ProfileRequiresPolicy::kPolicyRequired);
  EXPECT_EQ(known_user.GetProfileRequiresPolicy(kDefaultAccountId),
            ProfileRequiresPolicy::kPolicyRequired);

  known_user.SetProfileRequiresPolicy(kDefaultAccountId,
                                      ProfileRequiresPolicy::kNoPolicyRequired);
  EXPECT_EQ(known_user.GetProfileRequiresPolicy(kDefaultAccountId),
            ProfileRequiresPolicy::kNoPolicyRequired);

  known_user.ClearProfileRequiresPolicy(kDefaultAccountId);
  EXPECT_EQ(known_user.GetProfileRequiresPolicy(kDefaultAccountId),
            ProfileRequiresPolicy::kUnknown);
}

TEST_F(KnownUserTest, ReauthReason) {
  KnownUser known_user(local_state());
  EXPECT_FALSE(known_user.FindReauthReason(kDefaultAccountId).has_value());

  known_user.UpdateReauthReason(kDefaultAccountId, 3);
  EXPECT_EQ(known_user.FindReauthReason(kDefaultAccountId), 3);
}

TEST_F(KnownUserTest, ChallengeResponseKeys) {
  KnownUser known_user(local_state());
  EXPECT_TRUE(known_user.GetChallengeResponseKeys(kDefaultAccountId).empty());

  base::Value::List challenge_response_keys;
  challenge_response_keys.Append("key1");
  known_user.SetChallengeResponseKeys(kDefaultAccountId,
                                      challenge_response_keys.Clone());

  EXPECT_EQ(known_user.GetChallengeResponseKeys(kDefaultAccountId),
            challenge_response_keys);
}

TEST_F(KnownUserTest, LastOnlineSignin) {
  KnownUser known_user(local_state());
  EXPECT_TRUE(known_user.GetLastOnlineSignin(kDefaultAccountId).is_null());

  base::Time last_online_signin = base::Time::Now();
  known_user.SetLastOnlineSignin(kDefaultAccountId, last_online_signin);

  EXPECT_EQ(known_user.GetLastOnlineSignin(kDefaultAccountId),
            last_online_signin);
}

TEST_F(KnownUserTest, OfflineSigninLimit) {
  KnownUser known_user(local_state());
  EXPECT_FALSE(known_user.GetOfflineSigninLimit(kDefaultAccountId).has_value());

  base::TimeDelta offline_signin_limit = base::Minutes(80);
  known_user.SetOfflineSigninLimit(kDefaultAccountId, offline_signin_limit);

  EXPECT_EQ(known_user.GetOfflineSigninLimit(kDefaultAccountId).value(),
            offline_signin_limit);
}

TEST_F(KnownUserTest, IsEnterpriseManaged) {
  KnownUser known_user(local_state());
  EXPECT_FALSE(known_user.GetIsEnterpriseManaged(kDefaultAccountId));

  known_user.SetIsEnterpriseManaged(kDefaultAccountId, true);

  EXPECT_TRUE(known_user.GetIsEnterpriseManaged(kDefaultAccountId));
}

TEST_F(KnownUserTest, AccountManager) {
  KnownUser known_user(local_state());
  EXPECT_FALSE(known_user.GetAccountManager(kDefaultAccountId));

  known_user.SetAccountManager(kDefaultAccountId, "test");

  EXPECT_TRUE(known_user.GetAccountManager(kDefaultAccountId));
}

TEST_F(KnownUserTest, UserLastLoginInputMethodId) {
  KnownUser known_user(local_state());
  EXPECT_FALSE(known_user.GetUserLastInputMethodId(kDefaultAccountId));

  known_user.SetUserLastLoginInputMethodId(kDefaultAccountId, "test");

  EXPECT_TRUE(known_user.GetUserLastInputMethodId(kDefaultAccountId));
}

TEST_F(KnownUserTest, UserPinLength) {
  KnownUser known_user(local_state());
  EXPECT_EQ(known_user.GetUserPinLength(kDefaultAccountId), 0);

  known_user.SetUserPinLength(kDefaultAccountId, 8);

  EXPECT_EQ(known_user.GetUserPinLength(kDefaultAccountId), 8);
}

TEST_F(KnownUserTest, PinAutosubmitBackfillNeeded) {
  KnownUser known_user(local_state());
  // If the pref is not set, returns true.
  EXPECT_TRUE(known_user.PinAutosubmitIsBackfillNeeded(kDefaultAccountId));

  known_user.PinAutosubmitSetBackfillNotNeeded(kDefaultAccountId);

  EXPECT_FALSE(known_user.PinAutosubmitIsBackfillNeeded(kDefaultAccountId));

  known_user.PinAutosubmitSetBackfillNeededForTests(kDefaultAccountId);

  EXPECT_TRUE(known_user.PinAutosubmitIsBackfillNeeded(kDefaultAccountId));
}

TEST_F(KnownUserTest, PasswordSyncToken) {
  KnownUser known_user(local_state());
  EXPECT_FALSE(known_user.GetPasswordSyncToken(kDefaultAccountId));

  known_user.SetPasswordSyncToken(kDefaultAccountId, "test");

  EXPECT_EQ(*known_user.GetPasswordSyncToken(kDefaultAccountId), "test");
}

TEST_F(KnownUserTest, CleanEphemeralUsersRemovesEphemeralAdOnly) {
  KnownUser known_user(local_state());
  const AccountId kAccountIdNonEphemeralGaia =
      AccountId::FromUserEmailGaiaId("account1@gmail.com", "gaia_id_1");
  const AccountId kAccountIdEphemeralGaia =
      AccountId::FromUserEmailGaiaId("account2@gmail.com", "gaia_id_2");
  const AccountId kAccountIdNonEphemeralAd =
      AccountId::AdFromUserEmailObjGuid("account3@gmail.com", "guid_3");
  const AccountId kAccountIdEphemeralAd =
      AccountId::AdFromUserEmailObjGuid("account4@gmail.com", "guid_4");

  known_user.SaveKnownUser(kAccountIdNonEphemeralGaia);
  known_user.SaveKnownUser(kAccountIdEphemeralGaia);
  known_user.SaveKnownUser(kAccountIdNonEphemeralAd);
  known_user.SaveKnownUser(kAccountIdEphemeralAd);
  known_user.SetIsEphemeralUser(kAccountIdEphemeralGaia,
                                /*is_ephemeral=*/true);
  known_user.SetIsEphemeralUser(kAccountIdEphemeralAd, /*is_ephemeral=*/true);

  EXPECT_THAT(known_user.GetKnownAccountIds(),
              testing::UnorderedElementsAre(
                  kAccountIdNonEphemeralGaia, kAccountIdEphemeralGaia,
                  kAccountIdNonEphemeralAd, kAccountIdEphemeralAd));

  known_user.CleanEphemeralUsers();

  EXPECT_THAT(known_user.GetKnownAccountIds(),
              testing::UnorderedElementsAre(kAccountIdNonEphemeralGaia,
                                            kAccountIdEphemeralGaia,
                                            kAccountIdNonEphemeralAd));
}

TEST_F(KnownUserTest, CleanObsoletePrefs) {
  KnownUser known_user(local_state());
  const std::string kObsoletePrefName = "minimal_migration_attempted";
  const std::string kCustomPrefName = "custom_pref";

  // Set an obsolete pref.
  known_user.SetBooleanPref(kDefaultAccountId, kObsoletePrefName, true);
  // Set a custom pref.
  known_user.SetBooleanPref(kDefaultAccountId, kCustomPrefName, true);
  // Set a reserved, non-obsolete pref.
  known_user.SetIsEnterpriseManaged(kDefaultAccountId, true);

  known_user.CleanObsoletePrefs();

  // Verify that only the obsolete pref has been removed.
  EXPECT_FALSE(known_user.FindBoolPath(kDefaultAccountId, kObsoletePrefName)
                   .has_value());

  absl::optional<bool> custom_pref_value =
      known_user.FindBoolPath(kDefaultAccountId, kCustomPrefName);

  EXPECT_TRUE(custom_pref_value.has_value());
  EXPECT_TRUE(custom_pref_value.value());

  EXPECT_TRUE(known_user.GetIsEnterpriseManaged(kDefaultAccountId));
}

//
// =============================================================================
// Type-parametrized unittests for Set{String,Boolean,Integer,}Pref and
// Get{String,Boolean,Integer,}Pref.
// For every type (string, boolean, integer, raw base::Value) a PrefTypeInfo
// struct is declared which is then referenced in the generic test code.

// Test type holder for known_user string prefs.
struct PrefTypeInfoString {
  using PrefType = std::string;
  using PrefTypeForReading = std::string;

  static constexpr auto SetFunc = &KnownUser::SetStringPref;
  static constexpr auto GetFunc = &KnownUser::GetStringPrefForTest;

  static PrefType CreatePrefValue() { return std::string("test"); }
  static bool CheckPrefValue(PrefTypeForReading read_value) {
    return read_value == "test";
  }
  static bool CheckPrefValueAsBaseValue(const base::Value& read_value) {
    return read_value.is_string() && read_value.GetString() == "test";
  }
};

// Test type holder for known_user integer prefs.
struct PrefTypeInfoInteger {
  using PrefType = int;
  using PrefTypeForReading = int;

  static constexpr auto SetFunc = &KnownUser::SetIntegerPref;
  static constexpr auto GetFunc = &KnownUser::GetIntegerPrefForTest;

  static PrefType CreatePrefValue() { return 7; }
  static bool CheckPrefValue(PrefTypeForReading read_value) {
    return read_value == 7;
  }
  static bool CheckPrefValueAsBaseValue(const base::Value& read_value) {
    return read_value.is_int() && read_value.GetInt() == 7;
  }
};

// Test type holder for known_user boolean prefs.
struct PrefTypeInfoBoolean {
  using PrefType = bool;
  using PrefTypeForReading = bool;

  static constexpr auto SetFunc = &KnownUser::SetBooleanPref;
  static constexpr auto GetFunc = &KnownUser::GetBooleanPrefForTest;

  static PrefType CreatePrefValue() { return true; }
  static bool CheckPrefValue(PrefTypeForReading read_value) {
    return read_value == true;
  }
  static bool CheckPrefValueAsBaseValue(const base::Value& read_value) {
    return read_value.is_bool() && read_value.GetBool() == true;
  }
};

// Test type holder for known_user base::Value prefs.
struct PrefTypeInfoValue {
  using PrefType = base::Value;
  using PrefTypeForReading = const base::Value*;

  static constexpr auto SetFunc = &KnownUser::SetPath;
  static constexpr auto GetFunc = &KnownUser::GetPrefForTest;

  static PrefType CreatePrefValue() { return base::Value("test"); }
  static bool CheckPrefValue(PrefTypeForReading read_value) {
    return *read_value == CreatePrefValue();
  }
  static bool CheckPrefValueAsBaseValue(const base::Value& read_value) {
    return read_value == CreatePrefValue();
  }
};

template <typename PrefTypeInfo>
class KnownUserWithPrefTypeTest : public KnownUserTest {
 public:
  KnownUserWithPrefTypeTest() = default;
  ~KnownUserWithPrefTypeTest() = default;
};

TYPED_TEST_SUITE_P(KnownUserWithPrefTypeTest);

TYPED_TEST_P(KnownUserWithPrefTypeTest, ReadOnNonExistingUser) {
  KnownUser known_user(KnownUserTest::local_state());

  constexpr char kPrefName[] = "some_pref";
  const AccountId kNonExistingUser =
      AccountId::FromUserEmail("account1@gmail.com");

  typename TypeParam::PrefTypeForReading read_result;
  bool read_success = (known_user.*TypeParam::GetFunc)(kNonExistingUser,
                                                       kPrefName, &read_result);
  EXPECT_FALSE(read_success);
}

TYPED_TEST_P(KnownUserWithPrefTypeTest, ReadMissingPrefOnExistingUser) {
  KnownUser known_user(KnownUserTest::local_state());

  constexpr char kPrefName[] = "some_pref";
  const AccountId kUser = AccountId::FromUserEmail("account1@gmail.com");
  known_user.SaveKnownUser(kUser);

  typename TypeParam::PrefTypeForReading read_result;
  bool read_success =
      (known_user.*TypeParam::GetFunc)(kUser, kPrefName, &read_result);
  EXPECT_FALSE(read_success);
}

TYPED_TEST_P(KnownUserWithPrefTypeTest, ReadExistingPref) {
  KnownUser known_user(KnownUserTest::local_state());

  constexpr char kPrefName[] = "some_pref";
  const AccountId kUser = AccountId::FromUserEmail("account1@gmail.com");

  // Set* implicitly creates the known_user user entry.
  (known_user.*TypeParam::SetFunc)(kUser, kPrefName,
                                   TypeParam::CreatePrefValue());

  typename TypeParam::PrefTypeForReading read_result;
  bool read_success =
      (known_user.*TypeParam::GetFunc)(kUser, kPrefName, &read_result);
  EXPECT_TRUE(read_success);
  TypeParam::CheckPrefValue(read_result);
}

TYPED_TEST_P(KnownUserWithPrefTypeTest, ReadExistingPrefAsValue) {
  KnownUser known_user(KnownUserTest::local_state());

  constexpr char kPrefName[] = "some_pref";
  const AccountId kUser = AccountId::FromUserEmail("account1@gmail.com");

  // Set* implicitly creates the known_user user entry.
  (known_user.*TypeParam::SetFunc)(kUser, kPrefName,
                                   TypeParam::CreatePrefValue());

  const base::Value* read_result;
  bool read_success = known_user.GetPrefForTest(kUser, kPrefName, &read_result);
  EXPECT_TRUE(read_success);
  ASSERT_TRUE(read_result);
  TypeParam::CheckPrefValueAsBaseValue(*read_result);
}

REGISTER_TYPED_TEST_SUITE_P(KnownUserWithPrefTypeTest,
                            // All test functions must be listed:
                            ReadOnNonExistingUser,
                            ReadMissingPrefOnExistingUser,
                            ReadExistingPref,
                            ReadExistingPrefAsValue);

// This must be an alias because the preprocessor does not understand <> so if
// it was directly embedded in the INSTANTIATE_TYPED_TEST_SUITE_P macro the
// prepocessor would be confused on the comma.
using AllTypeInfos = testing::Types<PrefTypeInfoString,
                                    PrefTypeInfoInteger,
                                    PrefTypeInfoBoolean,
                                    PrefTypeInfoValue>;

INSTANTIATE_TYPED_TEST_SUITE_P(AllTypes,
                               KnownUserWithPrefTypeTest,
                               AllTypeInfos);

}  // namespace user_manager