Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / multi_profile_user_controller_unittest.cc
blob892951e40cfdfa2bf753f0f76252d8311f06c5fc
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.
5 #include "chrome/browser/chromeos/login/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/fake_user_manager.h"
11 #include "chrome/browser/chromeos/login/multi_profile_user_controller_delegate.h"
12 #include "chrome/browser/chromeos/login/user_manager.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/prefs/browser_prefs.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/scoped_testing_local_state.h"
19 #include "chrome/test/base/testing_browser_process.h"
20 #include "chrome/test/base/testing_pref_service_syncable.h"
21 #include "chrome/test/base/testing_profile.h"
22 #include "chrome/test/base/testing_profile_manager.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "net/cert/x509_certificate.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 namespace chromeos {
29 namespace {
31 const char* kUsers[] = {"a@gmail.com", "b@gmail.com" };
33 struct BehaviorTestCase {
34 const char* primary;
35 const char* secondary;
36 bool expected_allowed;
39 const BehaviorTestCase kBehaviorTestCases[] = {
41 MultiProfileUserController::kBehaviorUnrestricted,
42 MultiProfileUserController::kBehaviorUnrestricted,
43 true,
46 MultiProfileUserController::kBehaviorUnrestricted,
47 MultiProfileUserController::kBehaviorPrimaryOnly,
48 false,
51 MultiProfileUserController::kBehaviorUnrestricted,
52 MultiProfileUserController::kBehaviorNotAllowed,
53 false,
56 MultiProfileUserController::kBehaviorPrimaryOnly,
57 MultiProfileUserController::kBehaviorUnrestricted,
58 true,
61 MultiProfileUserController::kBehaviorPrimaryOnly,
62 MultiProfileUserController::kBehaviorPrimaryOnly,
63 false,
66 MultiProfileUserController::kBehaviorPrimaryOnly,
67 MultiProfileUserController::kBehaviorNotAllowed,
68 false,
71 MultiProfileUserController::kBehaviorNotAllowed,
72 MultiProfileUserController::kBehaviorUnrestricted,
73 false,
76 MultiProfileUserController::kBehaviorNotAllowed,
77 MultiProfileUserController::kBehaviorPrimaryOnly,
78 false,
81 MultiProfileUserController::kBehaviorNotAllowed,
82 MultiProfileUserController::kBehaviorNotAllowed,
83 false,
87 // Weak ptr to PolicyCertVerifier - object is freed in test destructor once
88 // we've ensured the profile has been shut down.
89 policy::PolicyCertVerifier* g_policy_cert_verifier_for_factory = NULL;
91 BrowserContextKeyedService* TestPolicyCertServiceFactory(
92 content::BrowserContext* context) {
93 return policy::PolicyCertService::CreateForTesting(
94 kUsers[0], g_policy_cert_verifier_for_factory, UserManager::Get())
95 .release();
98 } // namespace
100 class MultiProfileUserControllerTest
101 : public testing::Test,
102 public MultiProfileUserControllerDelegate {
103 public:
104 MultiProfileUserControllerTest()
105 : fake_user_manager_(new FakeUserManager),
106 user_manager_enabler_(fake_user_manager_),
107 user_not_allowed_count_(0) {}
108 virtual ~MultiProfileUserControllerTest() {}
110 virtual void SetUp() OVERRIDE {
111 profile_manager_.reset(
112 new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
113 ASSERT_TRUE(profile_manager_->SetUp());
114 controller_.reset(new MultiProfileUserController(
115 this, TestingBrowserProcess::GetGlobal()->local_state()));
117 for (size_t i = 0; i < arraysize(kUsers); ++i) {
118 const std::string user_email(kUsers[i]);
119 const User* user = fake_user_manager_->AddUser(user_email);
121 // Note that user profiles are created after user login in reality.
122 TestingProfile* user_profile =
123 profile_manager_->CreateTestingProfile(user_email);
124 user_profile->set_profile_name(user_email);
125 user_profiles_.push_back(user_profile);
127 fake_user_manager_->SetProfileForUser(user, user_profile);
131 virtual void TearDown() OVERRIDE {
132 // Clear our cached pointer to the PolicyCertVerifier.
133 g_policy_cert_verifier_for_factory = NULL;
135 // We must ensure that the PolicyCertVerifier outlives the
136 // PolicyCertService so shutdown the profile here. Additionally, we need
137 // to run the message loop between freeing the PolicyCertService and
138 // freeing the PolicyCertVerifier (see
139 // PolicyCertService::OnTrustAnchorsChanged() which is called from
140 // PolicyCertService::Shutdown()).
141 controller_.reset();
142 profile_manager_.reset();
143 base::RunLoop().RunUntilIdle();
146 void LoginUser(size_t user_index) {
147 ASSERT_LT(user_index, arraysize(kUsers));
148 fake_user_manager_->LoginUser(kUsers[user_index]);
149 controller_->StartObserving(user_profiles_[user_index]);
152 void SetOwner(size_t user_index) {
153 fake_user_manager_->set_owner_email(kUsers[user_index]);
156 PrefService* GetUserPrefs(size_t user_index) {
157 return user_profiles_[user_index]->GetPrefs();
160 void SetPrefBehavior(size_t user_index, const std::string& behavior) {
161 GetUserPrefs(user_index)->SetString(prefs::kMultiProfileUserBehavior,
162 behavior);
165 std::string GetCachedBehavior(size_t user_index) {
166 return controller_->GetCachedValue(kUsers[user_index]);
169 void SetCachedBehavior(size_t user_index,
170 const std::string& behavior) {
171 controller_->SetCachedValue(kUsers[user_index], behavior);
174 void ResetCounts() {
175 user_not_allowed_count_ = 0;
178 // MultiProfileUserControllerDeleagte overrides:
179 virtual void OnUserNotAllowed() OVERRIDE {
180 ++user_not_allowed_count_;
183 MultiProfileUserController* controller() { return controller_.get(); }
184 int user_not_allowed_count() const { return user_not_allowed_count_; }
186 TestingProfile* profile(int index) {
187 return user_profiles_[index];
190 content::TestBrowserThreadBundle threads_;
191 scoped_ptr<policy::PolicyCertVerifier> cert_verifier_;
192 scoped_ptr<TestingProfileManager> profile_manager_;
193 FakeUserManager* fake_user_manager_; // Not owned
194 ScopedUserManagerEnabler user_manager_enabler_;
196 scoped_ptr<MultiProfileUserController> controller_;
198 std::vector<TestingProfile*> user_profiles_;
200 int user_not_allowed_count_;
202 DISALLOW_COPY_AND_ASSIGN(MultiProfileUserControllerTest);
205 // Tests that everyone is allowed before a session starts.
206 TEST_F(MultiProfileUserControllerTest, AllAllowedBeforeLogin) {
207 const char* kTestCases[] = {
208 MultiProfileUserController::kBehaviorUnrestricted,
209 MultiProfileUserController::kBehaviorPrimaryOnly,
210 MultiProfileUserController::kBehaviorNotAllowed,
212 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
213 SetCachedBehavior(0, kTestCases[i]);
214 EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers[0]))
215 << "Case " << i;
219 // Tests that invalid cache value would become the default "unrestricted".
220 TEST_F(MultiProfileUserControllerTest, InvalidCacheBecomesDefault) {
221 const char kBad[] = "some invalid value";
222 SetCachedBehavior(0, kBad);
223 EXPECT_EQ(MultiProfileUserController::kBehaviorUnrestricted,
224 GetCachedBehavior(0));
227 // Tests that cached behavior value changes with user pref after login.
228 TEST_F(MultiProfileUserControllerTest, CachedBehaviorUpdate) {
229 LoginUser(0);
231 const char* kTestCases[] = {
232 MultiProfileUserController::kBehaviorUnrestricted,
233 MultiProfileUserController::kBehaviorPrimaryOnly,
234 MultiProfileUserController::kBehaviorNotAllowed,
235 MultiProfileUserController::kBehaviorUnrestricted,
237 for (size_t i = 0; i < arraysize(kTestCases); ++i) {
238 SetPrefBehavior(0, kTestCases[i]);
239 EXPECT_EQ(kTestCases[i], GetCachedBehavior(0));
243 // Tests that compromised cache value would be fixed and pref value is checked
244 // upon login.
245 TEST_F(MultiProfileUserControllerTest, CompromisedCacheFixedOnLogin) {
246 SetPrefBehavior(0, MultiProfileUserController::kBehaviorPrimaryOnly);
247 SetCachedBehavior(0, MultiProfileUserController::kBehaviorUnrestricted);
248 EXPECT_EQ(MultiProfileUserController::kBehaviorUnrestricted,
249 GetCachedBehavior(0));
250 LoginUser(0);
251 EXPECT_EQ(MultiProfileUserController::kBehaviorPrimaryOnly,
252 GetCachedBehavior(0));
254 EXPECT_EQ(0, user_not_allowed_count());
255 SetPrefBehavior(1, MultiProfileUserController::kBehaviorPrimaryOnly);
256 SetCachedBehavior(1, MultiProfileUserController::kBehaviorUnrestricted);
257 EXPECT_EQ(MultiProfileUserController::kBehaviorUnrestricted,
258 GetCachedBehavior(1));
259 LoginUser(1);
260 EXPECT_EQ(MultiProfileUserController::kBehaviorPrimaryOnly,
261 GetCachedBehavior(1));
262 EXPECT_EQ(1, user_not_allowed_count());
265 // Tests cases before the second user login.
266 TEST_F(MultiProfileUserControllerTest, IsSecondaryAllowed) {
267 LoginUser(0);
269 for (size_t i = 0; i < arraysize(kBehaviorTestCases); ++i) {
270 SetPrefBehavior(0, kBehaviorTestCases[i].primary);
271 SetCachedBehavior(1, kBehaviorTestCases[i].secondary);
272 EXPECT_EQ(kBehaviorTestCases[i].expected_allowed,
273 controller()->IsUserAllowedInSession(kUsers[1])) << "Case " << i;
277 // Tests user behavior changes within a two-user session.
278 TEST_F(MultiProfileUserControllerTest, PrimaryBehaviorChange) {
279 LoginUser(0);
280 LoginUser(1);
282 for (size_t i = 0; i < arraysize(kBehaviorTestCases); ++i) {
283 SetPrefBehavior(0, MultiProfileUserController::kBehaviorUnrestricted);
284 SetPrefBehavior(1, MultiProfileUserController::kBehaviorUnrestricted);
285 ResetCounts();
287 SetPrefBehavior(0, kBehaviorTestCases[i].primary);
288 SetPrefBehavior(1, kBehaviorTestCases[i].secondary);
289 EXPECT_EQ(kBehaviorTestCases[i].expected_allowed,
290 user_not_allowed_count() == 0) << "Case " << i;
294 // Tests that owner could not be a secondary user.
295 TEST_F(MultiProfileUserControllerTest, NoSecondaryOwner) {
296 LoginUser(0);
297 SetOwner(1);
299 EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers[1]));
301 EXPECT_EQ(0, user_not_allowed_count());
302 LoginUser(1);
303 EXPECT_EQ(1, user_not_allowed_count());
306 TEST_F(MultiProfileUserControllerTest,
307 UsedPolicyCertificatesAllowedForPrimary) {
308 // Verifies that any user can sign-in as the primary user, regardless of the
309 // tainted state.
310 policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers[0]);
311 EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers[0]));
312 EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers[1]));
315 TEST_F(MultiProfileUserControllerTest,
316 UsedPolicyCertificatesDisallowedForSecondary) {
317 // Verifies that if a regular user is signed-in then other regular users can
318 // be added but tainted users can't.
319 LoginUser(1);
320 EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers[0]));
321 policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers[0]);
322 EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers[0]));
325 TEST_F(MultiProfileUserControllerTest,
326 UsedPolicyCertificatesDisallowsSecondaries) {
327 // Verifies that if a tainted user is signed-in then no other users can
328 // be added.
329 policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers[0]);
330 LoginUser(0);
332 cert_verifier_.reset(new policy::PolicyCertVerifier(base::Closure()));
333 g_policy_cert_verifier_for_factory = cert_verifier_.get();
334 ASSERT_TRUE(
335 policy::PolicyCertServiceFactory::GetInstance()->SetTestingFactoryAndUse(
336 profile(0), TestPolicyCertServiceFactory));
338 EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers[1]));
339 policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(kUsers[1]);
340 EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers[1]));
342 // Flush tasks posted to IO.
343 base::RunLoop().RunUntilIdle();
346 TEST_F(MultiProfileUserControllerTest,
347 PolicyCertificatesInMemoryDisallowsSecondaries) {
348 // Verifies that if a user is signed-in and has policy certificates installed
349 // then no other users can be added.
350 LoginUser(0);
352 cert_verifier_.reset(new policy::PolicyCertVerifier(base::Closure()));
353 g_policy_cert_verifier_for_factory = cert_verifier_.get();
354 ASSERT_TRUE(
355 policy::PolicyCertServiceFactory::GetInstance()->SetTestingFactoryAndUse(
356 profile(0), TestPolicyCertServiceFactory));
357 policy::PolicyCertService* service =
358 policy::PolicyCertServiceFactory::GetForProfile(profile(0));
359 ASSERT_TRUE(service);
361 EXPECT_FALSE(service->has_policy_certificates());
362 EXPECT_TRUE(controller()->IsUserAllowedInSession(kUsers[1]));
364 net::CertificateList certificates;
365 certificates.push_back(new net::X509Certificate(
366 "subject", "issuer", base::Time(), base::Time()));
367 service->OnTrustAnchorsChanged(certificates);
368 EXPECT_TRUE(service->has_policy_certificates());
369 EXPECT_FALSE(controller()->IsUserAllowedInSession(kUsers[1]));
371 // Flush tasks posted to IO.
372 base::RunLoop().RunUntilIdle();
375 } // namespace chromeos