OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/ash/session_controller_client.h" | 5 #include "chrome/browser/ui/ash/session_controller_client.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
13 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" | 13 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" |
14 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" | 14 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h" |
15 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | 15 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" |
16 #include "chrome/browser/chromeos/policy/policy_cert_service.h" | 16 #include "chrome/browser/chromeos/policy/policy_cert_service.h" |
17 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h" | 17 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h" |
18 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h" | 18 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h" |
19 #include "chrome/browser/chromeos/profiles/profile_helper.h" | 19 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 20 #include "chrome/browser/supervised_user/supervised_user_service.h" |
| 21 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
20 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
21 #include "chrome/test/base/testing_browser_process.h" | 23 #include "chrome/test/base/testing_browser_process.h" |
22 #include "chrome/test/base/testing_profile_manager.h" | 24 #include "chrome/test/base/testing_profile_manager.h" |
| 25 #include "components/session_manager/core/session_manager.h" |
23 #include "components/signin/core/account_id/account_id.h" | 26 #include "components/signin/core/account_id/account_id.h" |
24 #include "components/user_manager/user_manager.h" | 27 #include "components/user_manager/user_manager.h" |
25 #include "content/public/test/test_browser_thread_bundle.h" | 28 #include "content/public/test/test_browser_thread_bundle.h" |
26 #include "net/cert/x509_certificate.h" | 29 #include "net/cert/x509_certificate.h" |
27 #include "net/test/cert_test_util.h" | 30 #include "net/test/cert_test_util.h" |
28 #include "net/test/test_data_directory.h" | 31 #include "net/test/test_data_directory.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 32 #include "testing/gtest/include/gtest/gtest.h" |
30 | 33 |
31 using chromeos::FakeChromeUserManager; | 34 using chromeos::FakeChromeUserManager; |
32 | 35 |
33 namespace { | 36 namespace { |
34 | 37 |
35 const char* kUser = "user@test.com"; | 38 const char* kUser = "user@test.com"; |
36 | 39 |
37 // Weak ptr to PolicyCertVerifier - object is freed in test destructor once | 40 // Weak ptr to PolicyCertVerifier - object is freed in test destructor once |
38 // we've ensured the profile has been shut down. | 41 // we've ensured the profile has been shut down. |
39 policy::PolicyCertVerifier* g_policy_cert_verifier_for_factory = nullptr; | 42 policy::PolicyCertVerifier* g_policy_cert_verifier_for_factory = nullptr; |
40 | 43 |
41 std::unique_ptr<KeyedService> CreateTestPolicyCertService( | 44 std::unique_ptr<KeyedService> CreateTestPolicyCertService( |
42 content::BrowserContext* context) { | 45 content::BrowserContext* context) { |
43 return policy::PolicyCertService::CreateForTesting( | 46 return policy::PolicyCertService::CreateForTesting( |
44 kUser, g_policy_cert_verifier_for_factory, | 47 kUser, g_policy_cert_verifier_for_factory, |
45 user_manager::UserManager::Get()); | 48 user_manager::UserManager::Get()); |
46 } | 49 } |
47 | 50 |
| 51 // A user manager that does not set profiles as loaded and notifies observers |
| 52 // when users being added to a session. |
| 53 class TestChromeUserManager : public FakeChromeUserManager { |
| 54 public: |
| 55 TestChromeUserManager() = default; |
| 56 ~TestChromeUserManager() override = default; |
| 57 |
| 58 // user_manager::UserManager: |
| 59 void UserLoggedIn(const AccountId& account_id, |
| 60 const std::string& user_id_hash, |
| 61 bool browser_restart) override { |
| 62 FakeChromeUserManager::UserLoggedIn(account_id, user_id_hash, |
| 63 browser_restart); |
| 64 active_user_ = const_cast<user_manager::User*>(FindUser(account_id)); |
| 65 NotifyOnLogin(); |
| 66 } |
| 67 |
| 68 private: |
| 69 DISALLOW_COPY_AND_ASSIGN(TestChromeUserManager); |
| 70 }; |
| 71 |
| 72 // A session controller interface implementation that tracks sessions and users. |
| 73 class TestSessionController : public ash::mojom::SessionController { |
| 74 public: |
| 75 TestSessionController() : binding_(this) {} |
| 76 ~TestSessionController() override {} |
| 77 |
| 78 ash::mojom::SessionControllerPtr CreateInterfacePtrAndBind() { |
| 79 return binding_.CreateInterfacePtrAndBind(); |
| 80 } |
| 81 |
| 82 ash::mojom::SessionInfo* last_session_info() { |
| 83 return last_session_info_.get(); |
| 84 } |
| 85 |
| 86 ash::mojom::UserSession* last_user_session() { |
| 87 return last_user_session_.get(); |
| 88 } |
| 89 |
| 90 // ash::mojom::SessionController: |
| 91 void SetClient(ash::mojom::SessionControllerClientPtr client) override {} |
| 92 void SetSessionInfo(ash::mojom::SessionInfoPtr info) override { |
| 93 last_session_info_ = info->Clone(); |
| 94 } |
| 95 void UpdateUserSession(ash::mojom::UserSessionPtr user_session) override { |
| 96 last_user_session_ = user_session->Clone(); |
| 97 } |
| 98 void SetUserSessionOrder( |
| 99 const std::vector<uint32_t>& user_session_order) override {} |
| 100 void StartLock(const StartLockCallback& callback) override {} |
| 101 void RunUnlockAnimation(const RunUnlockAnimationCallback& callback) override { |
| 102 } |
| 103 void NotifyChromeTerminating() override {} |
| 104 |
| 105 private: |
| 106 mojo::Binding<ash::mojom::SessionController> binding_; |
| 107 |
| 108 ash::mojom::SessionInfoPtr last_session_info_; |
| 109 ash::mojom::UserSessionPtr last_user_session_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(TestSessionController); |
| 112 }; |
| 113 |
48 } // namespace | 114 } // namespace |
49 | 115 |
50 class SessionControllerClientTest : public testing::Test { | 116 class SessionControllerClientTest : public testing::Test { |
51 protected: | 117 protected: |
52 SessionControllerClientTest() {} | 118 SessionControllerClientTest() {} |
53 ~SessionControllerClientTest() override {} | 119 ~SessionControllerClientTest() override {} |
54 | 120 |
55 void SetUp() override { | 121 void SetUp() override { |
56 // Initialize the UserManager singleton to a fresh FakeChromeUserManager | 122 testing::Test::SetUp(); |
57 // instance. | 123 |
58 user_manager_ = new FakeChromeUserManager; | 124 // Initialize the UserManager singleton. |
| 125 user_manager_ = new TestChromeUserManager; |
59 user_manager_enabler_.reset( | 126 user_manager_enabler_.reset( |
60 new chromeos::ScopedUserManagerEnabler(user_manager_)); | 127 new chromeos::ScopedUserManagerEnabler(user_manager_)); |
61 | 128 |
62 testing::Test::SetUp(); | 129 profile_manager_.reset( |
| 130 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 131 ASSERT_TRUE(profile_manager_->SetUp()); |
63 } | 132 } |
64 | 133 |
65 void TearDown() override { | 134 void TearDown() override { |
66 testing::Test::TearDown(); | |
67 user_manager_enabler_.reset(); | 135 user_manager_enabler_.reset(); |
68 user_manager_ = nullptr; | 136 user_manager_ = nullptr; |
69 // Clear our cached pointer to the PolicyCertVerifier. | 137 // Clear our cached pointer to the PolicyCertVerifier. |
70 g_policy_cert_verifier_for_factory = nullptr; | 138 g_policy_cert_verifier_for_factory = nullptr; |
71 profile_manager_.reset(); | 139 profile_manager_.reset(); |
72 | 140 |
73 // We must ensure that the PolicyCertVerifier outlives the | 141 // We must ensure that the PolicyCertVerifier outlives the |
74 // PolicyCertService so shutdown the profile here. Additionally, we need | 142 // PolicyCertService so shutdown the profile here. Additionally, we need |
75 // to run the message loop between freeing the PolicyCertService and | 143 // to run the message loop between freeing the PolicyCertService and |
76 // freeing the PolicyCertVerifier (see | 144 // freeing the PolicyCertVerifier (see |
77 // PolicyCertService::OnTrustAnchorsChanged() which is called from | 145 // PolicyCertService::OnTrustAnchorsChanged() which is called from |
78 // PolicyCertService::Shutdown()). | 146 // PolicyCertService::Shutdown()). |
79 base::RunLoop().RunUntilIdle(); | 147 base::RunLoop().RunUntilIdle(); |
| 148 |
| 149 testing::Test::TearDown(); |
80 } | 150 } |
81 | 151 |
82 // Add and log in a user to the session. | 152 // Add and log in a user to the session. |
83 void UserAddedToSession(std::string user) { | 153 void UserAddedToSession(std::string user) { |
84 user_manager()->AddUser(AccountId::FromUserEmail(user)); | 154 user_manager()->AddUser(AccountId::FromUserEmail(user)); |
85 user_manager()->LoginUser(AccountId::FromUserEmail(user)); | 155 user_manager()->LoginUser(AccountId::FromUserEmail(user)); |
86 } | 156 } |
87 | 157 |
88 // Get the active user. | 158 // Get the active user. |
89 const std::string& GetActiveUserEmail() { | 159 const std::string& GetActiveUserEmail() { |
90 return user_manager::UserManager::Get() | 160 return user_manager::UserManager::Get() |
91 ->GetActiveUser() | 161 ->GetActiveUser() |
92 ->GetAccountId() | 162 ->GetAccountId() |
93 .GetUserEmail(); | 163 .GetUserEmail(); |
94 } | 164 } |
95 | 165 |
96 FakeChromeUserManager* user_manager() { return user_manager_; } | 166 TestChromeUserManager* user_manager() { return user_manager_; } |
97 | 167 |
| 168 // Adds a regular user with a profile. |
98 void InitForMultiProfile() { | 169 void InitForMultiProfile() { |
99 profile_manager_.reset( | |
100 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); | |
101 ASSERT_TRUE(profile_manager_->SetUp()); | |
102 | |
103 const AccountId account_id(AccountId::FromUserEmail(kUser)); | 170 const AccountId account_id(AccountId::FromUserEmail(kUser)); |
104 const user_manager::User* user = user_manager()->AddUser(account_id); | 171 const user_manager::User* user = user_manager()->AddUser(account_id); |
105 | 172 |
106 // Note that user profiles are created after user login in reality. | 173 // Note that user profiles are created after user login in reality. |
| 174 CreateTestingProfile(user); |
| 175 } |
| 176 |
| 177 // Calls private methods to create a testing profile. |
| 178 void CreateTestingProfile(const user_manager::User* user) { |
| 179 const AccountId& account_id = user->GetAccountId(); |
107 user_profile_ = | 180 user_profile_ = |
108 profile_manager_->CreateTestingProfile(account_id.GetUserEmail()); | 181 profile_manager_->CreateTestingProfile(account_id.GetUserEmail()); |
109 user_profile_->set_profile_name(account_id.GetUserEmail()); | 182 user_profile_->set_profile_name(account_id.GetUserEmail()); |
110 chromeos::ProfileHelper::Get()->SetUserToProfileMappingForTesting( | 183 chromeos::ProfileHelper::Get()->SetUserToProfileMappingForTesting( |
111 user, user_profile_); | 184 user, user_profile_); |
112 } | 185 } |
113 | 186 |
114 content::TestBrowserThreadBundle threads_; | 187 content::TestBrowserThreadBundle threads_; |
115 std::unique_ptr<policy::PolicyCertVerifier> cert_verifier_; | 188 std::unique_ptr<policy::PolicyCertVerifier> cert_verifier_; |
116 std::unique_ptr<TestingProfileManager> profile_manager_; | 189 std::unique_ptr<TestingProfileManager> profile_manager_; |
117 TestingProfile* user_profile_; | 190 TestingProfile* user_profile_; |
| 191 session_manager::SessionManager session_manager_; |
118 | 192 |
119 private: | 193 private: |
120 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; | 194 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; |
121 | 195 |
122 // Owned by |user_manager_enabler_|. | 196 // Owned by |user_manager_enabler_|. |
123 FakeChromeUserManager* user_manager_ = nullptr; | 197 TestChromeUserManager* user_manager_ = nullptr; |
124 | 198 |
125 DISALLOW_COPY_AND_ASSIGN(SessionControllerClientTest); | 199 DISALLOW_COPY_AND_ASSIGN(SessionControllerClientTest); |
126 }; | 200 }; |
127 | 201 |
128 // Make sure that cycling one user does not cause any harm. | 202 // Make sure that cycling one user does not cause any harm. |
129 TEST_F(SessionControllerClientTest, CyclingOneUser) { | 203 TEST_F(SessionControllerClientTest, CyclingOneUser) { |
130 UserAddedToSession("firstuser@test.com"); | 204 UserAddedToSession("firstuser@test.com"); |
131 | 205 |
132 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); | 206 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); |
133 SessionControllerClient::DoCycleActiveUser(ash::CycleUserDirection::NEXT); | 207 SessionControllerClient::DoCycleActiveUser(ash::CycleUserDirection::NEXT); |
134 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); | 208 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); |
135 SessionControllerClient::DoCycleActiveUser(ash::CycleUserDirection::PREVIOUS); | 209 SessionControllerClient::DoCycleActiveUser(ash::CycleUserDirection::PREVIOUS); |
136 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); | 210 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); |
137 } | 211 } |
138 | 212 |
139 // Cycle three users forwards and backwards to see that it works. | 213 // Cycle three users forwards and backwards to see that it works. |
140 TEST_F(SessionControllerClientTest, CyclingThreeUsers) { | 214 TEST_F(SessionControllerClientTest, CyclingThreeUsers) { |
141 UserAddedToSession("firstuser@test.com"); | 215 UserAddedToSession("firstuser@test.com"); |
142 UserAddedToSession("seconduser@test.com"); | 216 UserAddedToSession("seconduser@test.com"); |
143 UserAddedToSession("thirduser@test.com"); | 217 UserAddedToSession("thirduser@test.com"); |
144 const ash::CycleUserDirection forward = ash::CycleUserDirection::NEXT; | 218 user_manager()->SwitchActiveUser( |
| 219 AccountId::FromUserEmail("firstuser@test.com")); |
145 | 220 |
146 // Cycle forward. | 221 // Cycle forward. |
| 222 const ash::CycleUserDirection forward = ash::CycleUserDirection::NEXT; |
147 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); | 223 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); |
148 SessionControllerClient::DoCycleActiveUser(forward); | 224 SessionControllerClient::DoCycleActiveUser(forward); |
149 EXPECT_EQ("seconduser@test.com", GetActiveUserEmail()); | 225 EXPECT_EQ("seconduser@test.com", GetActiveUserEmail()); |
150 SessionControllerClient::DoCycleActiveUser(forward); | 226 SessionControllerClient::DoCycleActiveUser(forward); |
151 EXPECT_EQ("thirduser@test.com", GetActiveUserEmail()); | 227 EXPECT_EQ("thirduser@test.com", GetActiveUserEmail()); |
152 SessionControllerClient::DoCycleActiveUser(forward); | 228 SessionControllerClient::DoCycleActiveUser(forward); |
153 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); | 229 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); |
154 | 230 |
155 // Cycle backwards. | 231 // Cycle backwards. |
156 const ash::CycleUserDirection backward = ash::CycleUserDirection::PREVIOUS; | 232 const ash::CycleUserDirection backward = ash::CycleUserDirection::PREVIOUS; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 SessionControllerClient::GetAddUserSessionPolicy()); | 352 SessionControllerClient::GetAddUserSessionPolicy()); |
277 const AccountId account_id(AccountId::FromUserEmail(kUser)); | 353 const AccountId account_id(AccountId::FromUserEmail(kUser)); |
278 user_manager()->LoginUser(account_id); | 354 user_manager()->LoginUser(account_id); |
279 user_profile_->GetPrefs()->SetString( | 355 user_profile_->GetPrefs()->SetString( |
280 prefs::kMultiProfileUserBehavior, | 356 prefs::kMultiProfileUserBehavior, |
281 chromeos::MultiProfileUserController::kBehaviorNotAllowed); | 357 chromeos::MultiProfileUserController::kBehaviorNotAllowed); |
282 user_manager()->AddUser(AccountId::FromUserEmail("bb@b.b")); | 358 user_manager()->AddUser(AccountId::FromUserEmail("bb@b.b")); |
283 EXPECT_EQ(ash::AddUserSessionPolicy::ERROR_NOT_ALLOWED_PRIMARY_USER, | 359 EXPECT_EQ(ash::AddUserSessionPolicy::ERROR_NOT_ALLOWED_PRIMARY_USER, |
284 SessionControllerClient::GetAddUserSessionPolicy()); | 360 SessionControllerClient::GetAddUserSessionPolicy()); |
285 } | 361 } |
| 362 |
| 363 TEST_F(SessionControllerClientTest, SupervisedUser) { |
| 364 // Create an object to test and connect it to our test interface. |
| 365 SessionControllerClient client; |
| 366 TestSessionController session_controller; |
| 367 client.session_controller_ = session_controller.CreateInterfacePtrAndBind(); |
| 368 client.Init(); |
| 369 SessionControllerClient::FlushForTesting(); |
| 370 |
| 371 // Simulate the login screen. No user session yet. |
| 372 session_manager_.SetSessionState( |
| 373 session_manager::SessionState::LOGIN_PRIMARY); |
| 374 EXPECT_FALSE(session_controller.last_user_session()); |
| 375 |
| 376 // Simulate a supervised user logging in. |
| 377 const AccountId account_id(AccountId::FromUserEmail("child@test.com")); |
| 378 const user_manager::User* user = |
| 379 user_manager()->AddSupervisedUser(account_id); |
| 380 ASSERT_TRUE(user); |
| 381 |
| 382 // Start session. This logs in the user and sends an active user notification. |
| 383 // The hash must match the one used by FakeChromeUserManager. |
| 384 session_manager_.CreateSession( |
| 385 account_id, chromeos::ProfileHelper::GetUserIdHashByUserIdForTesting( |
| 386 "child@test.com")); |
| 387 SessionControllerClient::FlushForTesting(); |
| 388 |
| 389 // The session controller received session info and user session. |
| 390 EXPECT_LT(0u, session_controller.last_user_session()->session_id); |
| 391 EXPECT_EQ(user_manager::USER_TYPE_SUPERVISED, |
| 392 session_controller.last_user_session()->type); |
| 393 |
| 394 // Simulate profile creation after login. |
| 395 CreateTestingProfile(user); |
| 396 user_profile_->SetSupervisedUserId("child-id"); |
| 397 |
| 398 // Simulate supervised user custodians. |
| 399 PrefService* prefs = user_profile_->GetPrefs(); |
| 400 prefs->SetString(prefs::kSupervisedUserCustodianEmail, "parent1@test.com"); |
| 401 prefs->SetString(prefs::kSupervisedUserSecondCustodianEmail, |
| 402 "parent2@test.com"); |
| 403 |
| 404 // Simulate the notification that the profile is ready. |
| 405 client.OnLoginUserProfilePrepared(user_profile_); |
| 406 base::RunLoop().RunUntilIdle(); // For PostTask and mojo interface. |
| 407 |
| 408 // The custodians were sent over the mojo interface. |
| 409 EXPECT_EQ("parent1@test.com", |
| 410 session_controller.last_user_session()->custodian_email); |
| 411 EXPECT_EQ("parent2@test.com", |
| 412 session_controller.last_user_session()->second_custodian_email); |
| 413 |
| 414 // Simulate an update to the custodian information. |
| 415 prefs->SetString(prefs::kSupervisedUserCustodianEmail, "parent3@test.com"); |
| 416 client.OnCustodianInfoChanged(); |
| 417 SessionControllerClient::FlushForTesting(); |
| 418 |
| 419 // The updated custodian was sent over the mojo interface. |
| 420 EXPECT_EQ("parent3@test.com", |
| 421 session_controller.last_user_session()->custodian_email); |
| 422 } |
OLD | NEW |