known_user_unittest.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "components/user_manager/known_user.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/json/values_util.h"
  8. #include "base/test/task_environment.h"
  9. #include "base/values.h"
  10. #include "components/account_id/account_id.h"
  11. #include "components/prefs/scoped_user_pref_update.h"
  12. #include "components/prefs/testing_pref_service.h"
  13. #include "components/user_manager/fake_user_manager.h"
  14. #include "components/user_manager/scoped_user_manager.h"
  15. #include "components/user_manager/user_manager_base.h"
  16. #include "testing/gmock/include/gmock/gmock.h"
  17. #include "testing/gtest/include/gtest/gtest.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. namespace user_manager {
  20. namespace {
  21. absl::optional<std::string> GetStringPrefValue(KnownUser* known_user,
  22. const AccountId& account_id,
  23. const char* pref_name) {
  24. if (const std::string* value =
  25. known_user->FindStringPath(account_id, pref_name)) {
  26. return *value;
  27. }
  28. return absl::nullopt;
  29. }
  30. } // namespace
  31. // Base class for tests of known_user.
  32. // Sets up global objects necessary for known_user to be able to access
  33. // local_state.
  34. class KnownUserTest : public testing::Test {
  35. public:
  36. KnownUserTest() {
  37. auto fake_user_manager = std::make_unique<FakeUserManager>();
  38. fake_user_manager_ = fake_user_manager.get();
  39. scoped_user_manager_ =
  40. std::make_unique<ScopedUserManager>(std::move(fake_user_manager));
  41. UserManagerBase::RegisterPrefs(local_state_.registry());
  42. }
  43. ~KnownUserTest() override = default;
  44. KnownUserTest(const KnownUserTest& other) = delete;
  45. KnownUserTest& operator=(const KnownUserTest& other) = delete;
  46. protected:
  47. const AccountId kDefaultAccountId =
  48. AccountId::FromUserEmailGaiaId("default_account@gmail.com",
  49. "fake-gaia-id");
  50. FakeUserManager* fake_user_manager() { return fake_user_manager_; }
  51. PrefService* local_state() { return &local_state_; }
  52. const base::Value* FindPrefs(const AccountId& account_id) {
  53. return KnownUser(local_state()).FindPrefs(account_id);
  54. }
  55. private:
  56. base::test::TaskEnvironment task_environment_{
  57. base::test::TaskEnvironment::MainThreadType::UI};
  58. // Owned by |scoped_user_manager_|.
  59. FakeUserManager* fake_user_manager_ = nullptr;
  60. std::unique_ptr<ScopedUserManager> scoped_user_manager_;
  61. TestingPrefServiceSimple local_state_;
  62. };
  63. TEST_F(KnownUserTest, FindPrefsNonExisting) {
  64. EXPECT_FALSE(FindPrefs(kDefaultAccountId));
  65. }
  66. TEST_F(KnownUserTest, FindPrefsExisting) {
  67. KnownUser known_user(local_state());
  68. const std::string kCustomPrefName = "custom_pref";
  69. known_user.SetStringPref(kDefaultAccountId, kCustomPrefName, "value");
  70. const base::Value* value = FindPrefs(kDefaultAccountId);
  71. ASSERT_TRUE(value);
  72. const std::string* pref_value = value->FindStringKey(kCustomPrefName);
  73. ASSERT_TRUE(pref_value);
  74. EXPECT_EQ(*pref_value, "value");
  75. }
  76. TEST_F(KnownUserTest, FindPrefsIgnoresEphemeralGaiaUsers) {
  77. KnownUser known_user(local_state());
  78. const AccountId kAccountIdEphemeralGaia =
  79. AccountId::FromUserEmailGaiaId("account2@gmail.com", "gaia_id_2");
  80. const AccountId kAccountIdEphemeralAd =
  81. AccountId::AdFromUserEmailObjGuid("account4@gmail.com", "guid_4");
  82. fake_user_manager()->SetUserNonCryptohomeDataEphemeral(
  83. kAccountIdEphemeralGaia,
  84. /*is_ephemeral=*/true);
  85. fake_user_manager()->SetUserNonCryptohomeDataEphemeral(kAccountIdEphemeralAd,
  86. /*is_ephemeral=*/true);
  87. const std::string kCustomPrefName = "custom_pref";
  88. known_user.SetStringPref(kAccountIdEphemeralGaia, kCustomPrefName, "value");
  89. known_user.SetStringPref(kAccountIdEphemeralAd, kCustomPrefName, "value");
  90. EXPECT_FALSE(FindPrefs(kAccountIdEphemeralGaia));
  91. EXPECT_TRUE(FindPrefs(kAccountIdEphemeralAd));
  92. }
  93. TEST_F(KnownUserTest, FindPrefsMatchForUnknownAccountType) {
  94. KnownUser known_user(local_state());
  95. // All account ids have the same e-mail
  96. const AccountId kAccountIdUnknown =
  97. AccountId::FromUserEmail("account1@gmail.com");
  98. const AccountId kAccountIdGaia =
  99. AccountId::FromUserEmailGaiaId("account1@gmail.com", "gaia_id_2");
  100. const AccountId kAccountIdAd =
  101. AccountId::AdFromUserEmailObjGuid("account1@gmail.com", "guid");
  102. known_user.SetStringPref(kAccountIdUnknown, "some_pref", "some_value");
  103. EXPECT_TRUE(FindPrefs(kAccountIdUnknown));
  104. EXPECT_TRUE(FindPrefs(kAccountIdGaia));
  105. EXPECT_TRUE(FindPrefs(kAccountIdAd));
  106. }
  107. TEST_F(KnownUserTest, FindPrefsMatchForGaiaAccountWithEmail) {
  108. KnownUser known_user(local_state());
  109. const char* kEmailA = "a@gmail.com";
  110. const char* kEmailB = "b@gmail.com";
  111. const char* kGaiaIdA = "a";
  112. const char* kGaiaIdB = "b";
  113. known_user.SaveKnownUser(AccountId::FromUserEmailGaiaId(kEmailA, kGaiaIdA));
  114. // Finding by itself should work
  115. EXPECT_TRUE(FindPrefs(AccountId::FromUserEmailGaiaId(kEmailA, kGaiaIdA)));
  116. // Finding by gaia id should also work even if the e-mail doesn't match.
  117. EXPECT_TRUE(FindPrefs(AccountId::FromUserEmailGaiaId(kEmailB, kGaiaIdA)));
  118. // Finding by e-mail should also work even if the gaia id doesn't match.
  119. // TODO(https://crbug.com/1190902): This should likely be EXPECT_FALSE going
  120. // forward.
  121. EXPECT_TRUE(FindPrefs(AccountId::FromUserEmailGaiaId(kEmailA, kGaiaIdB)));
  122. // Finding by just gaia id without any e-mail doesn't work (because the
  123. // resulting AccountId is not considered valid).
  124. EXPECT_FALSE(FindPrefs(AccountId::FromGaiaId(kGaiaIdA)));
  125. // An unrelated gaia AccountId with the same Account Type doesn't find
  126. // anything.
  127. EXPECT_FALSE(FindPrefs(AccountId::FromUserEmailGaiaId(kEmailB, kGaiaIdB)));
  128. // Looking up an AccountId stored as gaia by an unknown-type AccountId with
  129. // the same e-mail address succeeds.
  130. EXPECT_TRUE(FindPrefs(AccountId::FromUserEmail(kEmailA)));
  131. // Looking up an AccountId stored as gaia by an AccountId with type Ad fails.
  132. EXPECT_FALSE(FindPrefs(AccountId::AdFromUserEmailObjGuid(kEmailA, "guid")));
  133. }
  134. TEST_F(KnownUserTest, FindPrefsMatchForAdAccountWithEmail) {
  135. KnownUser known_user(local_state());
  136. const std::string kEmailA = "a@gmail.com";
  137. const std::string kEmailB = "b@gmail.com";
  138. known_user.SaveKnownUser(AccountId::AdFromUserEmailObjGuid(kEmailA, "a"));
  139. // Finding by itself should work
  140. EXPECT_TRUE(FindPrefs(AccountId::AdFromUserEmailObjGuid(kEmailA, "a")));
  141. // Finding by guid should also work even if the e-mail doesn't match.
  142. EXPECT_TRUE(FindPrefs(AccountId::AdFromUserEmailObjGuid(kEmailB, "a")));
  143. // Finding by e-mail should also work even if the guid doesn't match.
  144. EXPECT_TRUE(FindPrefs(AccountId::AdFromUserEmailObjGuid(kEmailA, "b")));
  145. // An unrelated AD AccountId with the same Account Type doesn't find
  146. // anything.
  147. EXPECT_FALSE(FindPrefs(AccountId::AdFromUserEmailObjGuid(kEmailB, "b")));
  148. // Looking up an AccountId stored as AD by an unknown-type AccountId with
  149. // the same e-mail address succeeds.
  150. EXPECT_TRUE(FindPrefs(AccountId::FromUserEmail(kEmailA)));
  151. // Looking up an AccountId stored as AD by an AccountId with type gaia fails.
  152. EXPECT_FALSE(FindPrefs(AccountId::FromUserEmailGaiaId(kEmailA, "gaia_id")));
  153. }
  154. TEST_F(KnownUserTest, UpdatePrefsWithoutClear) {
  155. KnownUser known_user(local_state());
  156. constexpr char kPrefName1[] = "pref1";
  157. constexpr char kPrefName2[] = "pref2";
  158. known_user.SetPath(kDefaultAccountId, kPrefName1,
  159. base::Value("pref1_value1"));
  160. known_user.SetPath(kDefaultAccountId, kPrefName1,
  161. base::Value("pref1_value2"));
  162. known_user.SetPath(kDefaultAccountId, kPrefName2,
  163. base::Value("pref2_value1"));
  164. EXPECT_EQ(absl::make_optional(std::string("pref1_value2")),
  165. GetStringPrefValue(&known_user, kDefaultAccountId, kPrefName1));
  166. EXPECT_EQ(absl::make_optional(std::string("pref2_value1")),
  167. GetStringPrefValue(&known_user, kDefaultAccountId, kPrefName2));
  168. }
  169. TEST_F(KnownUserTest, UpdatePrefsWithClear) {
  170. KnownUser known_user(local_state());
  171. constexpr char kPrefName1[] = "pref1";
  172. constexpr char kPrefName2[] = "pref2";
  173. known_user.SetPath(kDefaultAccountId, kPrefName1,
  174. base::Value("pref1_value1"));
  175. known_user.SetPath(kDefaultAccountId, kPrefName2,
  176. base::Value("pref2_value1"));
  177. known_user.SetPath(kDefaultAccountId, kPrefName1, absl::nullopt);
  178. EXPECT_EQ(absl::nullopt,
  179. GetStringPrefValue(&known_user, kDefaultAccountId, kPrefName1));
  180. EXPECT_EQ(absl::make_optional(std::string("pref2_value1")),
  181. GetStringPrefValue(&known_user, kDefaultAccountId, kPrefName2));
  182. }
  183. TEST_F(KnownUserTest, GetKnownAccountIdsNoAccounts) {
  184. KnownUser known_user(local_state());
  185. EXPECT_THAT(known_user.GetKnownAccountIds(), testing::IsEmpty());
  186. }
  187. TEST_F(KnownUserTest, GetKnownAccountIdsWithAccounts) {
  188. KnownUser known_user(local_state());
  189. const AccountId kAccountIdGaia =
  190. AccountId::FromUserEmailGaiaId("account2@gmail.com", "gaia_id");
  191. const AccountId kAccountIdAd =
  192. AccountId::AdFromUserEmailObjGuid("account3@gmail.com", "obj_guid");
  193. known_user.SaveKnownUser(kAccountIdGaia);
  194. known_user.SaveKnownUser(kAccountIdAd);
  195. EXPECT_THAT(known_user.GetKnownAccountIds(),
  196. testing::UnorderedElementsAre(kAccountIdGaia, kAccountIdAd));
  197. }
  198. TEST_F(KnownUserTest, SaveKnownUserIgnoresUnknownType) {
  199. KnownUser known_user(local_state());
  200. const AccountId kAccountIdUnknown =
  201. AccountId::FromUserEmail("account2@gmail.com");
  202. known_user.SaveKnownUser(kAccountIdUnknown);
  203. EXPECT_THAT(known_user.GetKnownAccountIds(), testing::IsEmpty());
  204. }
  205. TEST_F(KnownUserTest, SaveKnownUserIgnoresEphemeralGaiaUsers) {
  206. KnownUser known_user(local_state());
  207. const AccountId kAccountIdNonEphemeralGaia =
  208. AccountId::FromUserEmailGaiaId("account1@gmail.com", "gaia_id_1");
  209. const AccountId kAccountIdEphemeralGaia =
  210. AccountId::FromUserEmailGaiaId("account2@gmail.com", "gaia_id_2");
  211. const AccountId kAccountIdNonEphemeralAd =
  212. AccountId::AdFromUserEmailObjGuid("account3@gmail.com", "guid_3");
  213. const AccountId kAccountIdEphemeralAd =
  214. AccountId::AdFromUserEmailObjGuid("account4@gmail.com", "guid_4");
  215. fake_user_manager()->SetUserNonCryptohomeDataEphemeral(
  216. kAccountIdEphemeralGaia,
  217. /*is_ephemeral=*/true);
  218. fake_user_manager()->SetUserNonCryptohomeDataEphemeral(kAccountIdEphemeralAd,
  219. /*is_ephemeral=*/true);
  220. known_user.SaveKnownUser(kAccountIdNonEphemeralGaia);
  221. known_user.SaveKnownUser(kAccountIdEphemeralGaia);
  222. known_user.SaveKnownUser(kAccountIdNonEphemeralAd);
  223. known_user.SaveKnownUser(kAccountIdEphemeralAd);
  224. EXPECT_THAT(known_user.GetKnownAccountIds(),
  225. testing::UnorderedElementsAre(kAccountIdNonEphemeralGaia,
  226. kAccountIdNonEphemeralAd,
  227. kAccountIdEphemeralAd));
  228. }
  229. TEST_F(KnownUserTest, UpdateIdForGaiaAccount) {
  230. KnownUser known_user(local_state());
  231. const AccountId kAccountIdUnknown =
  232. AccountId::FromUserEmail("account1@gmail.com");
  233. known_user.SetStringPref(kAccountIdUnknown, "some_pref", "some_value");
  234. EXPECT_THAT(known_user.GetKnownAccountIds(),
  235. testing::UnorderedElementsAre(kAccountIdUnknown));
  236. const AccountId kAccountIdGaia =
  237. AccountId::FromUserEmailGaiaId("account1@gmail.com", "gaia_id");
  238. known_user.UpdateId(kAccountIdGaia);
  239. EXPECT_THAT(known_user.GetKnownAccountIds(),
  240. testing::UnorderedElementsAre(kAccountIdGaia));
  241. }
  242. TEST_F(KnownUserTest, UpdateIdForAdAccount) {
  243. KnownUser known_user(local_state());
  244. const AccountId kAccountIdUnknown =
  245. AccountId::FromUserEmail("account1@gmail.com");
  246. known_user.SetStringPref(kAccountIdUnknown, "some_pref", "some_value");
  247. EXPECT_THAT(known_user.GetKnownAccountIds(),
  248. testing::UnorderedElementsAre(kAccountIdUnknown));
  249. const AccountId kAccountIdAd =
  250. AccountId::AdFromUserEmailObjGuid("account1@gmail.com", "guid");
  251. known_user.UpdateId(kAccountIdAd);
  252. EXPECT_THAT(known_user.GetKnownAccountIds(),
  253. testing::UnorderedElementsAre(kAccountIdAd));
  254. }
  255. TEST_F(KnownUserTest, FindGaiaIdForGaiaAccount) {
  256. KnownUser known_user(local_state());
  257. const AccountId kAccountIdGaia =
  258. AccountId::FromUserEmailGaiaId("account1@gmail.com", "gaia_id");
  259. known_user.SaveKnownUser(kAccountIdGaia);
  260. const std::string* gaia_id = known_user.FindGaiaID(kAccountIdGaia);
  261. ASSERT_TRUE(gaia_id);
  262. EXPECT_EQ(*gaia_id, "gaia_id");
  263. }
  264. TEST_F(KnownUserTest, FindGaiaIdForAdAccount) {
  265. KnownUser known_user(local_state());
  266. const AccountId kAccountIdAd =
  267. AccountId::AdFromUserEmailObjGuid("account1@gmail.com", "guid");
  268. known_user.SaveKnownUser(kAccountIdAd);
  269. EXPECT_FALSE(known_user.FindGaiaID(kAccountIdAd));
  270. }
  271. // TODO(https://crbug.com/1148457): Add tests for GetAccountId.
  272. TEST_F(KnownUserTest, RemovePrefOnCustomPref) {
  273. KnownUser known_user(local_state());
  274. const std::string kCustomPrefName = "custom_pref";
  275. known_user.SetStringPref(kDefaultAccountId, kCustomPrefName, "value");
  276. EXPECT_TRUE(known_user.FindStringPath(kDefaultAccountId, kCustomPrefName));
  277. known_user.RemovePref(kDefaultAccountId, kCustomPrefName);
  278. EXPECT_FALSE(known_user.FindStringPath(kDefaultAccountId, kCustomPrefName));
  279. }
  280. TEST_F(KnownUserTest, RemovePrefOnReservedPref) {
  281. KnownUser known_user(local_state());
  282. const std::string kReservedPrefName = "device_id";
  283. known_user.SetStringPref(kDefaultAccountId, kReservedPrefName, "value");
  284. // Don't verify the message because on some builds CHECK failures do not print
  285. // debug messages (https://crbug.com/1198519).
  286. ASSERT_DEATH(known_user.RemovePref(kDefaultAccountId, kReservedPrefName), "");
  287. }
  288. TEST_F(KnownUserTest, DeviceId) {
  289. KnownUser known_user(local_state());
  290. EXPECT_EQ(known_user.GetDeviceId(kDefaultAccountId), std::string());
  291. known_user.SetDeviceId(kDefaultAccountId, "test");
  292. EXPECT_EQ(known_user.GetDeviceId(kDefaultAccountId), "test");
  293. }
  294. TEST_F(KnownUserTest, GAPSCookie) {
  295. KnownUser known_user(local_state());
  296. EXPECT_EQ(known_user.GetGAPSCookie(kDefaultAccountId), std::string());
  297. known_user.SetGAPSCookie(kDefaultAccountId, "test");
  298. EXPECT_EQ(known_user.GetGAPSCookie(kDefaultAccountId), "test");
  299. }
  300. TEST_F(KnownUserTest, UsingSAML) {
  301. KnownUser known_user(local_state());
  302. EXPECT_FALSE(known_user.IsUsingSAML(kDefaultAccountId));
  303. known_user.UpdateUsingSAML(kDefaultAccountId, /*using_saml=*/true);
  304. EXPECT_TRUE(known_user.IsUsingSAML(kDefaultAccountId));
  305. }
  306. TEST_F(KnownUserTest, UsingSAMLPrincipalsAPI) {
  307. KnownUser known_user(local_state());
  308. EXPECT_FALSE(known_user.GetIsUsingSAMLPrincipalsAPI(kDefaultAccountId));
  309. known_user.UpdateIsUsingSAMLPrincipalsAPI(kDefaultAccountId,
  310. /*using_saml=*/true);
  311. EXPECT_TRUE(known_user.GetIsUsingSAMLPrincipalsAPI(kDefaultAccountId));
  312. }
  313. TEST_F(KnownUserTest, ProfileRequiresPolicy) {
  314. KnownUser known_user(local_state());
  315. EXPECT_EQ(known_user.GetProfileRequiresPolicy(kDefaultAccountId),
  316. ProfileRequiresPolicy::kUnknown);
  317. known_user.SetProfileRequiresPolicy(kDefaultAccountId,
  318. ProfileRequiresPolicy::kPolicyRequired);
  319. EXPECT_EQ(known_user.GetProfileRequiresPolicy(kDefaultAccountId),
  320. ProfileRequiresPolicy::kPolicyRequired);
  321. known_user.SetProfileRequiresPolicy(kDefaultAccountId,
  322. ProfileRequiresPolicy::kNoPolicyRequired);
  323. EXPECT_EQ(known_user.GetProfileRequiresPolicy(kDefaultAccountId),
  324. ProfileRequiresPolicy::kNoPolicyRequired);
  325. known_user.ClearProfileRequiresPolicy(kDefaultAccountId);
  326. EXPECT_EQ(known_user.GetProfileRequiresPolicy(kDefaultAccountId),
  327. ProfileRequiresPolicy::kUnknown);
  328. }
  329. TEST_F(KnownUserTest, ReauthReason) {
  330. KnownUser known_user(local_state());
  331. EXPECT_FALSE(known_user.FindReauthReason(kDefaultAccountId).has_value());
  332. known_user.UpdateReauthReason(kDefaultAccountId, 3);
  333. EXPECT_EQ(known_user.FindReauthReason(kDefaultAccountId), 3);
  334. }
  335. TEST_F(KnownUserTest, ChallengeResponseKeys) {
  336. KnownUser known_user(local_state());
  337. EXPECT_TRUE(known_user.GetChallengeResponseKeys(kDefaultAccountId).is_none());
  338. base::Value::List challenge_response_keys;
  339. challenge_response_keys.Append("key1");
  340. known_user.SetChallengeResponseKeys(
  341. kDefaultAccountId, base::Value(challenge_response_keys.Clone()));
  342. EXPECT_EQ(known_user.GetChallengeResponseKeys(kDefaultAccountId),
  343. challenge_response_keys);
  344. }
  345. TEST_F(KnownUserTest, LastOnlineSignin) {
  346. KnownUser known_user(local_state());
  347. EXPECT_TRUE(known_user.GetLastOnlineSignin(kDefaultAccountId).is_null());
  348. base::Time last_online_signin = base::Time::Now();
  349. known_user.SetLastOnlineSignin(kDefaultAccountId, last_online_signin);
  350. EXPECT_EQ(known_user.GetLastOnlineSignin(kDefaultAccountId),
  351. last_online_signin);
  352. }
  353. TEST_F(KnownUserTest, OfflineSigninLimit) {
  354. KnownUser known_user(local_state());
  355. EXPECT_FALSE(known_user.GetOfflineSigninLimit(kDefaultAccountId).has_value());
  356. base::TimeDelta offline_signin_limit = base::Minutes(80);
  357. known_user.SetOfflineSigninLimit(kDefaultAccountId, offline_signin_limit);
  358. EXPECT_EQ(known_user.GetOfflineSigninLimit(kDefaultAccountId).value(),
  359. offline_signin_limit);
  360. }
  361. TEST_F(KnownUserTest, IsEnterpriseManaged) {
  362. KnownUser known_user(local_state());
  363. EXPECT_FALSE(known_user.GetIsEnterpriseManaged(kDefaultAccountId));
  364. known_user.SetIsEnterpriseManaged(kDefaultAccountId, true);
  365. EXPECT_TRUE(known_user.GetIsEnterpriseManaged(kDefaultAccountId));
  366. }
  367. TEST_F(KnownUserTest, AccountManager) {
  368. KnownUser known_user(local_state());
  369. EXPECT_FALSE(known_user.GetAccountManager(kDefaultAccountId));
  370. known_user.SetAccountManager(kDefaultAccountId, "test");
  371. EXPECT_TRUE(known_user.GetAccountManager(kDefaultAccountId));
  372. }
  373. TEST_F(KnownUserTest, UserLastLoginInputMethodId) {
  374. KnownUser known_user(local_state());
  375. EXPECT_FALSE(known_user.GetUserLastInputMethodId(kDefaultAccountId));
  376. known_user.SetUserLastLoginInputMethodId(kDefaultAccountId, "test");
  377. EXPECT_TRUE(known_user.GetUserLastInputMethodId(kDefaultAccountId));
  378. }
  379. TEST_F(KnownUserTest, UserPinLength) {
  380. KnownUser known_user(local_state());
  381. EXPECT_EQ(known_user.GetUserPinLength(kDefaultAccountId), 0);
  382. known_user.SetUserPinLength(kDefaultAccountId, 8);
  383. EXPECT_EQ(known_user.GetUserPinLength(kDefaultAccountId), 8);
  384. }
  385. TEST_F(KnownUserTest, PinAutosubmitBackfillNeeded) {
  386. KnownUser known_user(local_state());
  387. // If the pref is not set, returns true.
  388. EXPECT_TRUE(known_user.PinAutosubmitIsBackfillNeeded(kDefaultAccountId));
  389. known_user.PinAutosubmitSetBackfillNotNeeded(kDefaultAccountId);
  390. EXPECT_FALSE(known_user.PinAutosubmitIsBackfillNeeded(kDefaultAccountId));
  391. known_user.PinAutosubmitSetBackfillNeededForTests(kDefaultAccountId);
  392. EXPECT_TRUE(known_user.PinAutosubmitIsBackfillNeeded(kDefaultAccountId));
  393. }
  394. TEST_F(KnownUserTest, PasswordSyncToken) {
  395. KnownUser known_user(local_state());
  396. EXPECT_FALSE(known_user.GetPasswordSyncToken(kDefaultAccountId));
  397. known_user.SetPasswordSyncToken(kDefaultAccountId, "test");
  398. EXPECT_EQ(*known_user.GetPasswordSyncToken(kDefaultAccountId), "test");
  399. }
  400. TEST_F(KnownUserTest, CleanEphemeralUsersRemovesEphemeralAdOnly) {
  401. KnownUser known_user(local_state());
  402. const AccountId kAccountIdNonEphemeralGaia =
  403. AccountId::FromUserEmailGaiaId("account1@gmail.com", "gaia_id_1");
  404. const AccountId kAccountIdEphemeralGaia =
  405. AccountId::FromUserEmailGaiaId("account2@gmail.com", "gaia_id_2");
  406. const AccountId kAccountIdNonEphemeralAd =
  407. AccountId::AdFromUserEmailObjGuid("account3@gmail.com", "guid_3");
  408. const AccountId kAccountIdEphemeralAd =
  409. AccountId::AdFromUserEmailObjGuid("account4@gmail.com", "guid_4");
  410. known_user.SaveKnownUser(kAccountIdNonEphemeralGaia);
  411. known_user.SaveKnownUser(kAccountIdEphemeralGaia);
  412. known_user.SaveKnownUser(kAccountIdNonEphemeralAd);
  413. known_user.SaveKnownUser(kAccountIdEphemeralAd);
  414. known_user.SetIsEphemeralUser(kAccountIdEphemeralGaia,
  415. /*is_ephemeral=*/true);
  416. known_user.SetIsEphemeralUser(kAccountIdEphemeralAd, /*is_ephemeral=*/true);
  417. EXPECT_THAT(known_user.GetKnownAccountIds(),
  418. testing::UnorderedElementsAre(
  419. kAccountIdNonEphemeralGaia, kAccountIdEphemeralGaia,
  420. kAccountIdNonEphemeralAd, kAccountIdEphemeralAd));
  421. known_user.CleanEphemeralUsers();
  422. EXPECT_THAT(known_user.GetKnownAccountIds(),
  423. testing::UnorderedElementsAre(kAccountIdNonEphemeralGaia,
  424. kAccountIdEphemeralGaia,
  425. kAccountIdNonEphemeralAd));
  426. }
  427. TEST_F(KnownUserTest, CleanObsoletePrefs) {
  428. KnownUser known_user(local_state());
  429. const std::string kObsoletePrefName = "minimal_migration_attempted";
  430. const std::string kCustomPrefName = "custom_pref";
  431. // Set an obsolete pref.
  432. known_user.SetBooleanPref(kDefaultAccountId, kObsoletePrefName, true);
  433. // Set a custom pref.
  434. known_user.SetBooleanPref(kDefaultAccountId, kCustomPrefName, true);
  435. // Set a reserved, non-obsolete pref.
  436. known_user.SetIsEnterpriseManaged(kDefaultAccountId, true);
  437. known_user.CleanObsoletePrefs();
  438. // Verify that only the obsolete pref has been removed.
  439. EXPECT_FALSE(known_user.FindBoolPath(kDefaultAccountId, kObsoletePrefName)
  440. .has_value());
  441. absl::optional<bool> custom_pref_value =
  442. known_user.FindBoolPath(kDefaultAccountId, kCustomPrefName);
  443. EXPECT_TRUE(custom_pref_value.has_value());
  444. EXPECT_TRUE(custom_pref_value.value());
  445. EXPECT_TRUE(known_user.GetIsEnterpriseManaged(kDefaultAccountId));
  446. }
  447. //
  448. // =============================================================================
  449. // Type-parametrized unittests for Set{String,Boolean,Integer,}Pref and
  450. // Get{String,Boolean,Integer,}Pref.
  451. // For every type (string, boolean, integer, raw base::Value) a PrefTypeInfo
  452. // struct is declared which is then referenced in the generic test code.
  453. // Test type holder for known_user string prefs.
  454. struct PrefTypeInfoString {
  455. using PrefType = std::string;
  456. using PrefTypeForReading = std::string;
  457. static constexpr auto SetFunc = &KnownUser::SetStringPref;
  458. static constexpr auto GetFunc = &KnownUser::GetStringPrefForTest;
  459. static PrefType CreatePrefValue() { return std::string("test"); }
  460. static bool CheckPrefValue(PrefTypeForReading read_value) {
  461. return read_value == "test";
  462. }
  463. static bool CheckPrefValueAsBaseValue(const base::Value& read_value) {
  464. return read_value.is_string() && read_value.GetString() == "test";
  465. }
  466. };
  467. // Test type holder for known_user integer prefs.
  468. struct PrefTypeInfoInteger {
  469. using PrefType = int;
  470. using PrefTypeForReading = int;
  471. static constexpr auto SetFunc = &KnownUser::SetIntegerPref;
  472. static constexpr auto GetFunc = &KnownUser::GetIntegerPrefForTest;
  473. static PrefType CreatePrefValue() { return 7; }
  474. static bool CheckPrefValue(PrefTypeForReading read_value) {
  475. return read_value == 7;
  476. }
  477. static bool CheckPrefValueAsBaseValue(const base::Value& read_value) {
  478. return read_value.is_int() && read_value.GetInt() == 7;
  479. }
  480. };
  481. // Test type holder for known_user boolean prefs.
  482. struct PrefTypeInfoBoolean {
  483. using PrefType = bool;
  484. using PrefTypeForReading = bool;
  485. static constexpr auto SetFunc = &KnownUser::SetBooleanPref;
  486. static constexpr auto GetFunc = &KnownUser::GetBooleanPrefForTest;
  487. static PrefType CreatePrefValue() { return true; }
  488. static bool CheckPrefValue(PrefTypeForReading read_value) {
  489. return read_value == true;
  490. }
  491. static bool CheckPrefValueAsBaseValue(const base::Value& read_value) {
  492. return read_value.is_bool() && read_value.GetBool() == true;
  493. }
  494. };
  495. // Test type holder for known_user base::Value prefs.
  496. struct PrefTypeInfoValue {
  497. using PrefType = base::Value;
  498. using PrefTypeForReading = const base::Value*;
  499. static constexpr auto SetFunc = &KnownUser::SetPath;
  500. static constexpr auto GetFunc = &KnownUser::GetPrefForTest;
  501. static PrefType CreatePrefValue() { return base::Value("test"); }
  502. static bool CheckPrefValue(PrefTypeForReading read_value) {
  503. return *read_value == CreatePrefValue();
  504. }
  505. static bool CheckPrefValueAsBaseValue(const base::Value& read_value) {
  506. return read_value == CreatePrefValue();
  507. }
  508. };
  509. template <typename PrefTypeInfo>
  510. class KnownUserWithPrefTypeTest : public KnownUserTest {
  511. public:
  512. KnownUserWithPrefTypeTest() = default;
  513. ~KnownUserWithPrefTypeTest() = default;
  514. };
  515. TYPED_TEST_SUITE_P(KnownUserWithPrefTypeTest);
  516. TYPED_TEST_P(KnownUserWithPrefTypeTest, ReadOnNonExistingUser) {
  517. KnownUser known_user(KnownUserTest::local_state());
  518. constexpr char kPrefName[] = "some_pref";
  519. const AccountId kNonExistingUser =
  520. AccountId::FromUserEmail("account1@gmail.com");
  521. typename TypeParam::PrefTypeForReading read_result;
  522. bool read_success = (known_user.*TypeParam::GetFunc)(kNonExistingUser,
  523. kPrefName, &read_result);
  524. EXPECT_FALSE(read_success);
  525. }
  526. TYPED_TEST_P(KnownUserWithPrefTypeTest, ReadMissingPrefOnExistingUser) {
  527. KnownUser known_user(KnownUserTest::local_state());
  528. constexpr char kPrefName[] = "some_pref";
  529. const AccountId kUser = AccountId::FromUserEmail("account1@gmail.com");
  530. known_user.SaveKnownUser(kUser);
  531. typename TypeParam::PrefTypeForReading read_result;
  532. bool read_success =
  533. (known_user.*TypeParam::GetFunc)(kUser, kPrefName, &read_result);
  534. EXPECT_FALSE(read_success);
  535. }
  536. TYPED_TEST_P(KnownUserWithPrefTypeTest, ReadExistingPref) {
  537. KnownUser known_user(KnownUserTest::local_state());
  538. constexpr char kPrefName[] = "some_pref";
  539. const AccountId kUser = AccountId::FromUserEmail("account1@gmail.com");
  540. // Set* implicitly creates the known_user user entry.
  541. (known_user.*TypeParam::SetFunc)(kUser, kPrefName,
  542. TypeParam::CreatePrefValue());
  543. typename TypeParam::PrefTypeForReading read_result;
  544. bool read_success =
  545. (known_user.*TypeParam::GetFunc)(kUser, kPrefName, &read_result);
  546. EXPECT_TRUE(read_success);
  547. TypeParam::CheckPrefValue(read_result);
  548. }
  549. TYPED_TEST_P(KnownUserWithPrefTypeTest, ReadExistingPrefAsValue) {
  550. KnownUser known_user(KnownUserTest::local_state());
  551. constexpr char kPrefName[] = "some_pref";
  552. const AccountId kUser = AccountId::FromUserEmail("account1@gmail.com");
  553. // Set* implicitly creates the known_user user entry.
  554. (known_user.*TypeParam::SetFunc)(kUser, kPrefName,
  555. TypeParam::CreatePrefValue());
  556. const base::Value* read_result;
  557. bool read_success = known_user.GetPrefForTest(kUser, kPrefName, &read_result);
  558. EXPECT_TRUE(read_success);
  559. ASSERT_TRUE(read_result);
  560. TypeParam::CheckPrefValueAsBaseValue(*read_result);
  561. }
  562. REGISTER_TYPED_TEST_SUITE_P(KnownUserWithPrefTypeTest,
  563. // All test functions must be listed:
  564. ReadOnNonExistingUser,
  565. ReadMissingPrefOnExistingUser,
  566. ReadExistingPref,
  567. ReadExistingPrefAsValue);
  568. // This must be an alias because the preprocessor does not understand <> so if
  569. // it was directly embedded in the INSTANTIATE_TYPED_TEST_SUITE_P macro the
  570. // prepocessor would be confused on the comma.
  571. using AllTypeInfos = testing::Types<PrefTypeInfoString,
  572. PrefTypeInfoInteger,
  573. PrefTypeInfoBoolean,
  574. PrefTypeInfoValue>;
  575. INSTANTIATE_TYPED_TEST_SUITE_P(AllTypes,
  576. KnownUserWithPrefTypeTest,
  577. AllTypeInfos);
  578. } // namespace user_manager