OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/ui/ash/session_state_delegate_chromeos.h" | |
6 | |
7 #include <memory> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/run_loop.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" | |
15 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" | |
16 #include "chrome/browser/chromeos/policy/policy_cert_service.h" | |
17 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h" | |
18 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h" | |
19 #include "chrome/browser/chromeos/profiles/profile_helper.h" | |
20 #include "chrome/common/pref_names.h" | |
21 #include "chrome/test/base/testing_browser_process.h" | |
22 #include "chrome/test/base/testing_profile_manager.h" | |
23 #include "components/signin/core/account_id/account_id.h" | |
24 #include "components/user_manager/user_manager.h" | |
25 #include "content/public/test/test_browser_thread_bundle.h" | |
26 #include "net/cert/x509_certificate.h" | |
27 #include "net/test/cert_test_util.h" | |
28 #include "net/test/test_data_directory.h" | |
29 #include "testing/gtest/include/gtest/gtest.h" | |
30 | |
31 namespace chromeos { | |
32 | |
33 namespace { | |
34 | |
35 const char* kUser = "user@test.com"; | |
36 | |
37 // Weak ptr to PolicyCertVerifier - object is freed in test destructor once | |
38 // we've ensured the profile has been shut down. | |
39 policy::PolicyCertVerifier* g_policy_cert_verifier_for_factory = NULL; | |
40 | |
41 std::unique_ptr<KeyedService> CreateTestPolicyCertService( | |
42 content::BrowserContext* context) { | |
43 return policy::PolicyCertService::CreateForTesting( | |
44 kUser, g_policy_cert_verifier_for_factory, | |
45 user_manager::UserManager::Get()); | |
46 } | |
47 | |
48 } // namespace | |
49 | |
50 class SessionStateDelegateChromeOSTest : public testing::Test { | |
51 protected: | |
52 SessionStateDelegateChromeOSTest() : user_manager_(NULL) { | |
53 } | |
54 | |
55 ~SessionStateDelegateChromeOSTest() override {} | |
56 | |
57 void SetUp() override { | |
58 // Initialize the UserManager singleton to a fresh FakeChromeUserManager | |
59 // instance. | |
60 user_manager_ = new FakeChromeUserManager; | |
61 user_manager_enabler_.reset( | |
62 new chromeos::ScopedUserManagerEnabler(user_manager_)); | |
63 | |
64 // Create our SessionStateDelegate to experiment with. | |
65 session_state_delegate_.reset(new SessionStateDelegateChromeos()); | |
66 testing::Test::SetUp(); | |
67 } | |
68 | |
69 void TearDown() override { | |
70 testing::Test::TearDown(); | |
71 session_state_delegate_.reset(); | |
72 user_manager_enabler_.reset(); | |
73 user_manager_ = NULL; | |
74 // Clear our cached pointer to the PolicyCertVerifier. | |
75 g_policy_cert_verifier_for_factory = NULL; | |
76 profile_manager_.reset(); | |
77 | |
78 // We must ensure that the PolicyCertVerifier outlives the | |
79 // PolicyCertService so shutdown the profile here. Additionally, we need | |
80 // to run the message loop between freeing the PolicyCertService and | |
81 // freeing the PolicyCertVerifier (see | |
82 // PolicyCertService::OnTrustAnchorsChanged() which is called from | |
83 // PolicyCertService::Shutdown()). | |
84 base::RunLoop().RunUntilIdle(); | |
85 } | |
86 | |
87 // Add and log in a user to the session. | |
88 void UserAddedToSession(std::string user) { | |
89 user_manager()->AddUser(AccountId::FromUserEmail(user)); | |
90 user_manager()->LoginUser(AccountId::FromUserEmail(user)); | |
91 } | |
92 | |
93 // Get the active user. | |
94 const std::string& GetActiveUserEmail() { | |
95 return user_manager::UserManager::Get() | |
96 ->GetActiveUser() | |
97 ->GetAccountId() | |
98 .GetUserEmail(); | |
99 } | |
100 | |
101 FakeChromeUserManager* user_manager() { return user_manager_; } | |
102 SessionStateDelegateChromeos* session_state_delegate() { | |
103 return session_state_delegate_.get(); | |
104 } | |
105 | |
106 void InitForMultiProfile() { | |
107 profile_manager_.reset( | |
108 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); | |
109 ASSERT_TRUE(profile_manager_->SetUp()); | |
110 | |
111 const AccountId account_id(AccountId::FromUserEmail(kUser)); | |
112 const user_manager::User* user = user_manager()->AddUser(account_id); | |
113 | |
114 // Note that user profiles are created after user login in reality. | |
115 user_profile_ = | |
116 profile_manager_->CreateTestingProfile(account_id.GetUserEmail()); | |
117 user_profile_->set_profile_name(account_id.GetUserEmail()); | |
118 chromeos::ProfileHelper::Get()->SetUserToProfileMappingForTesting( | |
119 user, user_profile_); | |
120 } | |
121 | |
122 content::TestBrowserThreadBundle threads_; | |
123 std::unique_ptr<policy::PolicyCertVerifier> cert_verifier_; | |
124 std::unique_ptr<TestingProfileManager> profile_manager_; | |
125 TestingProfile* user_profile_; | |
126 | |
127 private: | |
128 std::unique_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; | |
129 std::unique_ptr<SessionStateDelegateChromeos> session_state_delegate_; | |
130 | |
131 // Not owned. | |
132 FakeChromeUserManager* user_manager_; | |
133 | |
134 DISALLOW_COPY_AND_ASSIGN(SessionStateDelegateChromeOSTest); | |
135 }; | |
136 | |
137 // Make sure that cycling one user does not cause any harm. | |
138 TEST_F(SessionStateDelegateChromeOSTest, CyclingOneUser) { | |
139 UserAddedToSession("firstuser@test.com"); | |
140 | |
141 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); | |
142 session_state_delegate()->CycleActiveUser( | |
143 ash::SessionStateDelegate::CYCLE_TO_NEXT_USER); | |
144 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); | |
145 session_state_delegate()->CycleActiveUser( | |
146 ash::SessionStateDelegate::CYCLE_TO_PREVIOUS_USER); | |
147 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); | |
148 } | |
149 | |
150 // Cycle three users forwards and backwards to see that it works. | |
151 TEST_F(SessionStateDelegateChromeOSTest, CyclingThreeUsers) { | |
152 UserAddedToSession("firstuser@test.com"); | |
153 UserAddedToSession("seconduser@test.com"); | |
154 UserAddedToSession("thirduser@test.com"); | |
155 const ash::SessionStateDelegate::CycleUser forward = | |
156 ash::SessionStateDelegate::CYCLE_TO_NEXT_USER; | |
157 | |
158 // Cycle forward. | |
159 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); | |
160 session_state_delegate()->CycleActiveUser(forward); | |
161 EXPECT_EQ("seconduser@test.com", GetActiveUserEmail()); | |
162 session_state_delegate()->CycleActiveUser(forward); | |
163 EXPECT_EQ("thirduser@test.com", GetActiveUserEmail()); | |
164 session_state_delegate()->CycleActiveUser(forward); | |
165 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); | |
166 | |
167 // Cycle backwards. | |
168 const ash::SessionStateDelegate::CycleUser backward = | |
169 ash::SessionStateDelegate::CYCLE_TO_PREVIOUS_USER; | |
170 session_state_delegate()->CycleActiveUser(backward); | |
171 EXPECT_EQ("thirduser@test.com", GetActiveUserEmail()); | |
172 session_state_delegate()->CycleActiveUser(backward); | |
173 EXPECT_EQ("seconduser@test.com", GetActiveUserEmail()); | |
174 session_state_delegate()->CycleActiveUser(backward); | |
175 EXPECT_EQ("firstuser@test.com", GetActiveUserEmail()); | |
176 } | |
177 | |
178 // Make sure MultiProfile disabled by primary user policy. | |
179 TEST_F(SessionStateDelegateChromeOSTest, MultiProfileDisallowedByUserPolicy) { | |
180 InitForMultiProfile(); | |
181 EXPECT_TRUE( | |
182 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); | |
183 const AccountId account_id(AccountId::FromUserEmail(kUser)); | |
184 user_manager()->LoginUser(account_id); | |
185 EXPECT_TRUE( | |
186 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); | |
187 | |
188 user_profile_->GetPrefs()->SetString( | |
189 prefs::kMultiProfileUserBehavior, | |
190 chromeos::MultiProfileUserController::kBehaviorNotAllowed); | |
191 EXPECT_FALSE( | |
192 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); | |
193 } | |
194 | |
195 // Make sure MultiProfile disabled by primary user policy certificates. | |
196 TEST_F(SessionStateDelegateChromeOSTest, | |
197 MultiProfileDisallowedByPolicyCertificates) { | |
198 InitForMultiProfile(); | |
199 const AccountId account_id(AccountId::FromUserEmail(kUser)); | |
200 user_manager()->LoginUser(account_id); | |
201 EXPECT_TRUE( | |
202 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); | |
203 policy::PolicyCertServiceFactory::SetUsedPolicyCertificates( | |
204 account_id.GetUserEmail()); | |
205 EXPECT_FALSE( | |
206 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); | |
207 | |
208 // Flush tasks posted to IO. | |
209 base::RunLoop().RunUntilIdle(); | |
210 } | |
211 | |
212 // Make sure MultiProfile disabled by primary user certificates in memory. | |
213 TEST_F(SessionStateDelegateChromeOSTest, | |
214 MultiProfileDisallowedByPrimaryUserCertificatesInMemory) { | |
215 InitForMultiProfile(); | |
216 const AccountId account_id(AccountId::FromUserEmail(kUser)); | |
217 user_manager()->LoginUser(account_id); | |
218 EXPECT_TRUE( | |
219 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); | |
220 cert_verifier_.reset(new policy::PolicyCertVerifier(base::Closure())); | |
221 g_policy_cert_verifier_for_factory = cert_verifier_.get(); | |
222 ASSERT_TRUE( | |
223 policy::PolicyCertServiceFactory::GetInstance()->SetTestingFactoryAndUse( | |
224 user_profile_, CreateTestPolicyCertService)); | |
225 policy::PolicyCertService* service = | |
226 policy::PolicyCertServiceFactory::GetForProfile(user_profile_); | |
227 ASSERT_TRUE(service); | |
228 | |
229 EXPECT_FALSE(service->has_policy_certificates()); | |
230 net::CertificateList certificates; | |
231 certificates.push_back( | |
232 net::ImportCertFromFile(net::GetTestCertsDirectory(), "ok_cert.pem")); | |
233 service->OnTrustAnchorsChanged(certificates); | |
234 EXPECT_TRUE(service->has_policy_certificates()); | |
235 EXPECT_FALSE( | |
236 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); | |
237 | |
238 // Flush tasks posted to IO. | |
239 base::RunLoop().RunUntilIdle(); | |
240 } | |
241 | |
242 // Make sure adding users to multiprofiles disabled by reaching maximum | |
243 // number of users in sessions. | |
244 TEST_F(SessionStateDelegateChromeOSTest, | |
245 AddUserToMultiprofileDisallowedByMaximumUsers) { | |
246 InitForMultiProfile(); | |
247 | |
248 EXPECT_EQ(ash::AddUserSessionPolicy::ALLOWED, | |
249 session_state_delegate()->GetAddUserSessionPolicy()); | |
250 const AccountId account_id(AccountId::FromUserEmail(kUser)); | |
251 user_manager()->LoginUser(account_id); | |
252 while (session_state_delegate()->NumberOfLoggedInUsers() < | |
253 session_state_delegate()->GetMaximumNumberOfLoggedInUsers()) { | |
254 UserAddedToSession("bb@b.b"); | |
255 } | |
256 EXPECT_EQ(ash::AddUserSessionPolicy::ERROR_MAXIMUM_USERS_REACHED, | |
257 session_state_delegate()->GetAddUserSessionPolicy()); | |
258 } | |
259 | |
260 // Make sure adding users to multiprofiles disabled by logging in all possible | |
261 // users. | |
262 TEST_F(SessionStateDelegateChromeOSTest, | |
263 AddUserToMultiprofileDisallowedByAllUsersLogged) { | |
264 InitForMultiProfile(); | |
265 | |
266 EXPECT_EQ(ash::AddUserSessionPolicy::ALLOWED, | |
267 session_state_delegate()->GetAddUserSessionPolicy()); | |
268 const AccountId account_id(AccountId::FromUserEmail(kUser)); | |
269 user_manager()->LoginUser(account_id); | |
270 UserAddedToSession("bb@b.b"); | |
271 EXPECT_EQ(ash::AddUserSessionPolicy::ERROR_NO_ELIGIBLE_USERS, | |
272 session_state_delegate()->GetAddUserSessionPolicy()); | |
273 } | |
274 | |
275 // Make sure adding users to multiprofiles disabled by primary user policy. | |
276 TEST_F(SessionStateDelegateChromeOSTest, | |
277 AddUserToMultiprofileDisallowedByPrimaryUserPolicy) { | |
278 InitForMultiProfile(); | |
279 | |
280 EXPECT_EQ(ash::AddUserSessionPolicy::ALLOWED, | |
281 session_state_delegate()->GetAddUserSessionPolicy()); | |
282 const AccountId account_id(AccountId::FromUserEmail(kUser)); | |
283 user_manager()->LoginUser(account_id); | |
284 user_profile_->GetPrefs()->SetString( | |
285 prefs::kMultiProfileUserBehavior, | |
286 chromeos::MultiProfileUserController::kBehaviorNotAllowed); | |
287 user_manager()->AddUser(AccountId::FromUserEmail("bb@b.b")); | |
288 EXPECT_EQ(ash::AddUserSessionPolicy::ERROR_NOT_ALLOWED_PRIMARY_USER, | |
289 session_state_delegate()->GetAddUserSessionPolicy()); | |
290 } | |
291 | |
292 } // namespace chromeos | |
OLD | NEW |