1 // Copyright 2014 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.
5 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
11 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller_delegate.h"
12 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
13 #include "chrome/browser/chromeos/policy/policy_cert_service.h"
14 #include "chrome/browser/chromeos/policy/policy_cert_service_factory.h"
15 #include "chrome/browser/chromeos/policy/policy_cert_verifier.h"
16 #include "chrome/browser/chromeos/profiles/profile_helper.h"
17 #include "chrome/browser/prefs/browser_prefs.h"
18 #include "chrome/common/pref_names.h"
19 #include "chrome/test/base/scoped_testing_local_state.h"
20 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_pref_service_syncable.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "chrome/test/base/testing_profile_manager.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 "testing/gtest/include/gtest/gtest.h"
33 const char* const kUsers
[] = {"a@gmail.com", "b@gmail.com" };
35 struct BehaviorTestCase
{
37 const char* secondary
;
38 MultiProfileUserController::UserAllowedInSessionReason
39 expected_primary_policy
;
40 MultiProfileUserController::UserAllowedInSessionReason
41 expected_secondary_allowed
;
44 const BehaviorTestCase kBehaviorTestCases
[] = {
46 MultiProfileUserController::kBehaviorUnrestricted
,
47 MultiProfileUserController::kBehaviorUnrestricted
,
48 MultiProfileUserController::ALLOWED
, MultiProfileUserController::ALLOWED
,
51 MultiProfileUserController::kBehaviorUnrestricted
,
52 MultiProfileUserController::kBehaviorPrimaryOnly
,
53 MultiProfileUserController::ALLOWED
,
54 MultiProfileUserController::NOT_ALLOWED_POLICY_FORBIDS
,
57 MultiProfileUserController::kBehaviorUnrestricted
,
58 MultiProfileUserController::kBehaviorNotAllowed
,
59 MultiProfileUserController::ALLOWED
,
60 MultiProfileUserController::NOT_ALLOWED_POLICY_FORBIDS
,
63 MultiProfileUserController::kBehaviorPrimaryOnly
,
64 MultiProfileUserController::kBehaviorUnrestricted
,
65 MultiProfileUserController::ALLOWED
, MultiProfileUserController::ALLOWED
,
68 MultiProfileUserController::kBehaviorPrimaryOnly
,
69 MultiProfileUserController::kBehaviorPrimaryOnly
,
70 MultiProfileUserController::ALLOWED
,
71 MultiProfileUserController::NOT_ALLOWED_POLICY_FORBIDS
,
74 MultiProfileUserController::kBehaviorPrimaryOnly
,
75 MultiProfileUserController::kBehaviorNotAllowed
,
76 MultiProfileUserController::ALLOWED
,
77 MultiProfileUserController::NOT_ALLOWED_POLICY_FORBIDS
,
80 MultiProfileUserController::kBehaviorNotAllowed
,
81 MultiProfileUserController::kBehaviorUnrestricted
,
82 MultiProfileUserController::NOT_ALLOWED_PRIMARY_USER_POLICY_FORBIDS
,
83 MultiProfileUserController::NOT_ALLOWED_PRIMARY_USER_POLICY_FORBIDS
,
86 MultiProfileUserController::kBehaviorNotAllowed
,
87 MultiProfileUserController::kBehaviorPrimaryOnly
,
88 MultiProfileUserController::NOT_ALLOWED_PRIMARY_USER_POLICY_FORBIDS
,
89 MultiProfileUserController::NOT_ALLOWED_PRIMARY_USER_POLICY_FORBIDS
,
92 MultiProfileUserController::kBehaviorNotAllowed
,
93 MultiProfileUserController::kBehaviorNotAllowed
,
94 MultiProfileUserController::NOT_ALLOWED_PRIMARY_USER_POLICY_FORBIDS
,
95 MultiProfileUserController::NOT_ALLOWED_PRIMARY_USER_POLICY_FORBIDS
,
99 // Weak ptr to PolicyCertVerifier - object is freed in test destructor once
100 // we've ensured the profile has been shut down.
101 policy::PolicyCertVerifier
* g_policy_cert_verifier_for_factory
= NULL
;
103 scoped_ptr
<KeyedService
> TestPolicyCertServiceFactory(
104 content::BrowserContext
* context
) {
105 return policy::PolicyCertService::CreateForTesting(
106 kUsers
[0], g_policy_cert_verifier_for_factory
,
107 user_manager::UserManager::Get());
112 class MultiProfileUserControllerTest
113 : public testing::Test
,
114 public MultiProfileUserControllerDelegate
{
116 MultiProfileUserControllerTest()
117 : fake_user_manager_(new FakeChromeUserManager
),
118 user_manager_enabler_(fake_user_manager_
),
119 user_not_allowed_count_(0) {}
120 ~MultiProfileUserControllerTest() override
{}
122 void SetUp() override
{
123 profile_manager_
.reset(
124 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
125 ASSERT_TRUE(profile_manager_
->SetUp());
126 controller_
.reset(new MultiProfileUserController(
127 this, TestingBrowserProcess::GetGlobal()->local_state()));
129 for (size_t i
= 0; i
< arraysize(kUsers
); ++i
) {
130 const std::string
user_email(kUsers
[i
]);
131 const user_manager::User
* user
= fake_user_manager_
->AddUser(user_email
);
133 // Note that user profiles are created after user login in reality.
134 TestingProfile
* user_profile
=
135 profile_manager_
->CreateTestingProfile(user_email
);
136 user_profile
->set_profile_name(user_email
);
137 user_profiles_
.push_back(user_profile
);
139 ProfileHelper::Get()->SetUserToProfileMappingForTesting(user
,
144 void TearDown() override
{
145 // Clear our cached pointer to the PolicyCertVerifier.
146 g_policy_cert_verifier_for_factory
= NULL
;
148 // We must ensure that the PolicyCertVerifier outlives the
149 // PolicyCertService so shutdown the profile here. Additionally, we need
150 // to run the message loop between freeing the PolicyCertService and
151 // freeing the PolicyCertVerifier (see
152 // PolicyCertService::OnTrustAnchorsChanged() which is called from
153 // PolicyCertService::Shutdown()).
155 profile_manager_
.reset();
156 base::RunLoop().RunUntilIdle();
159 void LoginUser(size_t user_index
) {
160 ASSERT_LT(user_index
, arraysize(kUsers
));
161 fake_user_manager_
->LoginUser(kUsers
[user_index
]);
162 controller_
->StartObserving(user_profiles_
[user_index
]);
165 void SetOwner(size_t user_index
) {
166 fake_user_manager_
->set_owner_email(kUsers
[user_index
]);
169 PrefService
* GetUserPrefs(size_t user_index
) {
170 return user_profiles_
[user_index
]->GetPrefs();
173 void SetPrefBehavior(size_t user_index
, const std::string
& behavior
) {
174 GetUserPrefs(user_index
)->SetString(prefs::kMultiProfileUserBehavior
,
178 std::string
GetCachedBehavior(size_t user_index
) {
179 return controller_
->GetCachedValue(kUsers
[user_index
]);
182 void SetCachedBehavior(size_t user_index
,
183 const std::string
& behavior
) {
184 controller_
->SetCachedValue(kUsers
[user_index
], behavior
);
188 user_not_allowed_count_
= 0;
191 // MultiProfileUserControllerDeleagte overrides:
192 void OnUserNotAllowed(const std::string
& user_email
) override
{
193 ++user_not_allowed_count_
;
196 MultiProfileUserController
* controller() { return controller_
.get(); }
197 int user_not_allowed_count() const { return user_not_allowed_count_
; }
199 TestingProfile
* profile(int index
) {
200 return user_profiles_
[index
];
203 content::TestBrowserThreadBundle threads_
;
204 scoped_ptr
<policy::PolicyCertVerifier
> cert_verifier_
;
205 scoped_ptr
<TestingProfileManager
> profile_manager_
;
206 FakeChromeUserManager
* fake_user_manager_
; // Not owned
207 ScopedUserManagerEnabler user_manager_enabler_
;
209 scoped_ptr
<MultiProfileUserController
> controller_
;
211 std::vector
<TestingProfile
*> user_profiles_
;
213 int user_not_allowed_count_
;
216 DISALLOW_COPY_AND_ASSIGN(MultiProfileUserControllerTest
);
219 // Tests that everyone is allowed before a session starts.
220 TEST_F(MultiProfileUserControllerTest
, AllAllowedBeforeLogin
) {
221 const char* const kTestCases
[] = {
222 MultiProfileUserController::kBehaviorUnrestricted
,
223 MultiProfileUserController::kBehaviorPrimaryOnly
,
224 MultiProfileUserController::kBehaviorNotAllowed
,
226 for (size_t i
= 0; i
< arraysize(kTestCases
); ++i
) {
227 SetCachedBehavior(0, kTestCases
[i
]);
228 MultiProfileUserController::UserAllowedInSessionReason reason
;
229 EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers
[0], &reason
))
231 EXPECT_EQ(MultiProfileUserController::ALLOWED
, reason
) << "Case " << i
;
232 EXPECT_EQ(MultiProfileUserController::ALLOWED
,
233 MultiProfileUserController::GetPrimaryUserPolicy())
238 // Tests that invalid cache value would become the default "unrestricted".
239 TEST_F(MultiProfileUserControllerTest
, InvalidCacheBecomesDefault
) {
240 const char kBad
[] = "some invalid value";
241 SetCachedBehavior(0, kBad
);
242 EXPECT_EQ(MultiProfileUserController::kBehaviorUnrestricted
,
243 GetCachedBehavior(0));
246 // Tests that cached behavior value changes with user pref after login.
247 TEST_F(MultiProfileUserControllerTest
, CachedBehaviorUpdate
) {
250 const char* const kTestCases
[] = {
251 MultiProfileUserController::kBehaviorUnrestricted
,
252 MultiProfileUserController::kBehaviorPrimaryOnly
,
253 MultiProfileUserController::kBehaviorNotAllowed
,
254 MultiProfileUserController::kBehaviorUnrestricted
,
256 for (size_t i
= 0; i
< arraysize(kTestCases
); ++i
) {
257 SetPrefBehavior(0, kTestCases
[i
]);
258 EXPECT_EQ(kTestCases
[i
], GetCachedBehavior(0));
262 // Tests that compromised cache value would be fixed and pref value is checked
264 TEST_F(MultiProfileUserControllerTest
, CompromisedCacheFixedOnLogin
) {
265 SetPrefBehavior(0, MultiProfileUserController::kBehaviorPrimaryOnly
);
266 SetCachedBehavior(0, MultiProfileUserController::kBehaviorUnrestricted
);
267 EXPECT_EQ(MultiProfileUserController::kBehaviorUnrestricted
,
268 GetCachedBehavior(0));
270 EXPECT_EQ(MultiProfileUserController::kBehaviorPrimaryOnly
,
271 GetCachedBehavior(0));
273 EXPECT_EQ(0, user_not_allowed_count());
274 SetPrefBehavior(1, MultiProfileUserController::kBehaviorPrimaryOnly
);
275 SetCachedBehavior(1, MultiProfileUserController::kBehaviorUnrestricted
);
276 EXPECT_EQ(MultiProfileUserController::kBehaviorUnrestricted
,
277 GetCachedBehavior(1));
279 EXPECT_EQ(MultiProfileUserController::kBehaviorPrimaryOnly
,
280 GetCachedBehavior(1));
281 EXPECT_EQ(1, user_not_allowed_count());
284 // Tests cases before the second user login.
285 TEST_F(MultiProfileUserControllerTest
, IsSecondaryAllowed
) {
288 for (size_t i
= 0; i
< arraysize(kBehaviorTestCases
); ++i
) {
289 SetPrefBehavior(0, kBehaviorTestCases
[i
].primary
);
290 SetCachedBehavior(1, kBehaviorTestCases
[i
].secondary
);
291 EXPECT_EQ(kBehaviorTestCases
[i
].expected_primary_policy
,
292 MultiProfileUserController::GetPrimaryUserPolicy())
294 MultiProfileUserController::UserAllowedInSessionReason reason
;
295 controller()->IsUserAllowedInSession(kUsers
[1], &reason
);
296 EXPECT_EQ(kBehaviorTestCases
[i
].expected_secondary_allowed
, reason
)
301 // Tests user behavior changes within a two-user session.
302 TEST_F(MultiProfileUserControllerTest
, PrimaryBehaviorChange
) {
306 for (size_t i
= 0; i
< arraysize(kBehaviorTestCases
); ++i
) {
307 SetPrefBehavior(0, MultiProfileUserController::kBehaviorUnrestricted
);
308 SetPrefBehavior(1, MultiProfileUserController::kBehaviorUnrestricted
);
311 SetPrefBehavior(0, kBehaviorTestCases
[i
].primary
);
312 SetPrefBehavior(1, kBehaviorTestCases
[i
].secondary
);
313 if (user_not_allowed_count() == 0) {
314 EXPECT_EQ(kBehaviorTestCases
[i
].expected_secondary_allowed
,
315 MultiProfileUserController::ALLOWED
)
318 EXPECT_NE(kBehaviorTestCases
[i
].expected_secondary_allowed
,
319 MultiProfileUserController::ALLOWED
)
325 TEST_F(MultiProfileUserControllerTest
,
326 UsedPolicyCertificatesAllowedForPrimary
) {
327 // Verifies that any user can sign-in as the primary user, regardless of the
329 policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers
[0]);
330 MultiProfileUserController::UserAllowedInSessionReason reason
;
331 EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers
[0], &reason
));
332 EXPECT_EQ(MultiProfileUserController::ALLOWED
, reason
);
333 EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers
[1], &reason
));
334 EXPECT_EQ(MultiProfileUserController::ALLOWED
, reason
);
335 EXPECT_EQ(MultiProfileUserController::ALLOWED
,
336 MultiProfileUserController::GetPrimaryUserPolicy());
339 TEST_F(MultiProfileUserControllerTest
,
340 UsedPolicyCertificatesDisallowedForSecondary
) {
341 // Verifies that if a regular user is signed-in then other regular users can
342 // be added but tainted users can't.
345 // TODO(xiyuan): Remove the following SetPrefBehavor when default is
346 // changed back to enabled.
347 SetPrefBehavior(1, MultiProfileUserController::kBehaviorUnrestricted
);
349 MultiProfileUserController::UserAllowedInSessionReason reason
;
350 EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers
[0], &reason
));
351 EXPECT_EQ(MultiProfileUserController::ALLOWED
, reason
);
353 policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers
[0]);
354 EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers
[0], &reason
));
355 EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_POLICY_CERT_TAINTED
,
359 TEST_F(MultiProfileUserControllerTest
,
360 UsedPolicyCertificatesDisallowsSecondaries
) {
361 // Verifies that if a tainted user is signed-in then no other users can
363 policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers
[0]);
366 cert_verifier_
.reset(new policy::PolicyCertVerifier(base::Closure()));
367 g_policy_cert_verifier_for_factory
= cert_verifier_
.get();
369 policy::PolicyCertServiceFactory::GetInstance()->SetTestingFactoryAndUse(
370 profile(0), TestPolicyCertServiceFactory
));
372 MultiProfileUserController::UserAllowedInSessionReason reason
;
373 EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers
[1], &reason
));
374 EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED
,
376 EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED
,
377 MultiProfileUserController::GetPrimaryUserPolicy());
378 policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers
[1]);
379 EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers
[1], &reason
));
380 EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_POLICY_CERT_TAINTED
,
382 EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED
,
383 MultiProfileUserController::GetPrimaryUserPolicy());
385 // Flush tasks posted to IO.
386 base::RunLoop().RunUntilIdle();
389 TEST_F(MultiProfileUserControllerTest
,
390 PolicyCertificatesInMemoryDisallowsSecondaries
) {
391 // Verifies that if a user is signed-in and has policy certificates installed
392 // then no other users can be added.
395 // TODO(xiyuan): Remove the following SetPrefBehavor when default is
396 // changed back to enabled.
397 SetPrefBehavior(0, MultiProfileUserController::kBehaviorUnrestricted
);
399 cert_verifier_
.reset(new policy::PolicyCertVerifier(base::Closure()));
400 g_policy_cert_verifier_for_factory
= cert_verifier_
.get();
402 policy::PolicyCertServiceFactory::GetInstance()->SetTestingFactoryAndUse(
403 profile(0), TestPolicyCertServiceFactory
));
404 policy::PolicyCertService
* service
=
405 policy::PolicyCertServiceFactory::GetForProfile(profile(0));
406 ASSERT_TRUE(service
);
408 EXPECT_FALSE(service
->has_policy_certificates());
409 MultiProfileUserController::UserAllowedInSessionReason reason
;
410 EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers
[1], &reason
));
411 EXPECT_EQ(MultiProfileUserController::ALLOWED
, reason
);
412 EXPECT_EQ(MultiProfileUserController::ALLOWED
,
413 MultiProfileUserController::GetPrimaryUserPolicy());
415 net::CertificateList certificates
;
416 certificates
.push_back(new net::X509Certificate(
417 "subject", "issuer", base::Time(), base::Time()));
418 service
->OnTrustAnchorsChanged(certificates
);
419 EXPECT_TRUE(service
->has_policy_certificates());
420 EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers
[1], &reason
));
421 EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED
,
423 EXPECT_EQ(MultiProfileUserController::NOT_ALLOWED_PRIMARY_POLICY_CERT_TAINTED
,
424 MultiProfileUserController::GetPrimaryUserPolicy());
426 // Flush tasks posted to IO.
427 base::RunLoop().RunUntilIdle();
430 } // namespace chromeos