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_state_delegate_chromeos.h" | 5 #include "chrome/browser/ui/ash/session_state_delegate_chromeos.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
| 10 #include "base/run_loop.h" |
10 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" | 11 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" |
| 12 #include "chrome/browser/chromeos/login/users/multi_profile_user_controller.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/common/pref_names.h" |
| 18 #include "chrome/test/base/testing_browser_process.h" |
11 #include "chrome/test/base/testing_profile_manager.h" | 19 #include "chrome/test/base/testing_profile_manager.h" |
| 20 #include "content/public/test/test_browser_thread_bundle.h" |
| 21 #include "net/cert/x509_certificate.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
13 | 23 |
| 24 namespace chromeos { |
| 25 |
| 26 namespace { |
| 27 |
| 28 const char* kUser = "user@test.com"; |
| 29 |
| 30 // Weak ptr to PolicyCertVerifier - object is freed in test destructor once |
| 31 // we've ensured the profile has been shut down. |
| 32 policy::PolicyCertVerifier* g_policy_cert_verifier_for_factory = NULL; |
| 33 |
| 34 KeyedService* CreateTestPolicyCertService(content::BrowserContext* context) { |
| 35 return policy::PolicyCertService::CreateForTesting( |
| 36 kUser, |
| 37 g_policy_cert_verifier_for_factory, |
| 38 chromeos::UserManager::Get()).release(); |
| 39 } |
| 40 |
| 41 } // namespace |
14 | 42 |
15 class SessionStateDelegateChromeOSTest : public testing::Test { | 43 class SessionStateDelegateChromeOSTest : public testing::Test { |
16 protected: | 44 protected: |
17 SessionStateDelegateChromeOSTest() : user_manager_(NULL) { | 45 SessionStateDelegateChromeOSTest() : user_manager_(NULL) { |
18 } | 46 } |
19 | 47 |
20 virtual ~SessionStateDelegateChromeOSTest() { | 48 virtual ~SessionStateDelegateChromeOSTest() { |
21 } | 49 } |
22 | 50 |
23 virtual void SetUp() OVERRIDE { | 51 virtual void SetUp() OVERRIDE { |
24 // Initialize the UserManager singleton to a fresh FakeUserManager instance. | 52 // Initialize the UserManager singleton to a fresh FakeUserManager instance. |
25 user_manager_ = new chromeos::FakeUserManager; | 53 user_manager_ = new chromeos::FakeUserManager; |
26 user_manager_enabler_.reset( | 54 user_manager_enabler_.reset( |
27 new chromeos::ScopedUserManagerEnabler(user_manager_)); | 55 new chromeos::ScopedUserManagerEnabler(user_manager_)); |
28 | 56 |
29 // Create our SessionStateDelegate to experiment with. | 57 // Create our SessionStateDelegate to experiment with. |
30 session_state_delegate_.reset(new SessionStateDelegateChromeos()); | 58 session_state_delegate_.reset(new SessionStateDelegateChromeos()); |
31 testing::Test::SetUp(); | 59 testing::Test::SetUp(); |
32 } | 60 } |
33 | 61 |
34 virtual void TearDown() OVERRIDE { | 62 virtual void TearDown() OVERRIDE { |
35 testing::Test::TearDown(); | 63 testing::Test::TearDown(); |
36 session_state_delegate_.reset(); | 64 session_state_delegate_.reset(); |
37 user_manager_enabler_.reset(); | 65 user_manager_enabler_.reset(); |
38 user_manager_ = NULL; | 66 user_manager_ = NULL; |
| 67 // Clear our cached pointer to the PolicyCertVerifier. |
| 68 g_policy_cert_verifier_for_factory = NULL; |
| 69 profile_manager_.reset(); |
| 70 |
| 71 // We must ensure that the PolicyCertVerifier outlives the |
| 72 // PolicyCertService so shutdown the profile here. Additionally, we need |
| 73 // to run the message loop between freeing the PolicyCertService and |
| 74 // freeing the PolicyCertVerifier (see |
| 75 // PolicyCertService::OnTrustAnchorsChanged() which is called from |
| 76 // PolicyCertService::Shutdown()). |
| 77 base::RunLoop().RunUntilIdle(); |
39 } | 78 } |
40 | 79 |
41 // Add and log in a user to the session. | 80 // Add and log in a user to the session. |
42 void UserAddedToSession(std::string user) { | 81 void UserAddedToSession(std::string user) { |
43 user_manager()->AddUser(user); | 82 user_manager()->AddUser(user); |
44 user_manager()->LoginUser(user); | 83 user_manager()->LoginUser(user); |
45 } | 84 } |
46 | 85 |
47 // Get the active user. | 86 // Get the active user. |
48 const std::string& GetActiveUser() { | 87 const std::string& GetActiveUser() { |
49 return chromeos::UserManager::Get()->GetActiveUser()->email(); | 88 return chromeos::UserManager::Get()->GetActiveUser()->email(); |
50 } | 89 } |
51 | 90 |
52 chromeos::FakeUserManager* user_manager() { return user_manager_; } | 91 chromeos::FakeUserManager* user_manager() { return user_manager_; } |
53 SessionStateDelegateChromeos* session_state_delegate() { | 92 SessionStateDelegateChromeos* session_state_delegate() { |
54 return session_state_delegate_.get(); | 93 return session_state_delegate_.get(); |
55 } | 94 } |
56 | 95 |
| 96 void InitForMultiProfile() { |
| 97 profile_manager_.reset( |
| 98 new TestingProfileManager(TestingBrowserProcess::GetGlobal())); |
| 99 ASSERT_TRUE(profile_manager_->SetUp()); |
| 100 |
| 101 const std::string user_email(kUser); |
| 102 const user_manager::User* user = user_manager()->AddUser(user_email); |
| 103 |
| 104 // Note that user profiles are created after user login in reality. |
| 105 user_profile_ = profile_manager_->CreateTestingProfile(user_email); |
| 106 user_profile_->set_profile_name(user_email); |
| 107 chromeos::ProfileHelper::Get()->SetUserToProfileMappingForTesting( |
| 108 user, user_profile_); |
| 109 } |
| 110 |
| 111 content::TestBrowserThreadBundle threads_; |
| 112 scoped_ptr<policy::PolicyCertVerifier> cert_verifier_; |
| 113 scoped_ptr<TestingProfileManager> profile_manager_; |
| 114 TestingProfile* user_profile_; |
| 115 |
57 private: | 116 private: |
58 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; | 117 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; |
59 scoped_ptr<SessionStateDelegateChromeos> session_state_delegate_; | 118 scoped_ptr<SessionStateDelegateChromeos> session_state_delegate_; |
60 | 119 |
61 // Not owned. | 120 // Not owned. |
62 chromeos::FakeUserManager* user_manager_; | 121 chromeos::FakeUserManager* user_manager_; |
63 | 122 |
64 DISALLOW_COPY_AND_ASSIGN(SessionStateDelegateChromeOSTest); | 123 DISALLOW_COPY_AND_ASSIGN(SessionStateDelegateChromeOSTest); |
65 }; | 124 }; |
66 | 125 |
(...skipping 30 matching lines...) Expand all Loading... |
97 // Cycle backwards. | 156 // Cycle backwards. |
98 const ash::SessionStateDelegate::CycleUser backward = | 157 const ash::SessionStateDelegate::CycleUser backward = |
99 ash::SessionStateDelegate::CYCLE_TO_PREVIOUS_USER; | 158 ash::SessionStateDelegate::CYCLE_TO_PREVIOUS_USER; |
100 session_state_delegate()->CycleActiveUser(backward); | 159 session_state_delegate()->CycleActiveUser(backward); |
101 EXPECT_EQ("thirduser@test.com", GetActiveUser()); | 160 EXPECT_EQ("thirduser@test.com", GetActiveUser()); |
102 session_state_delegate()->CycleActiveUser(backward); | 161 session_state_delegate()->CycleActiveUser(backward); |
103 EXPECT_EQ("seconduser@test.com", GetActiveUser()); | 162 EXPECT_EQ("seconduser@test.com", GetActiveUser()); |
104 session_state_delegate()->CycleActiveUser(backward); | 163 session_state_delegate()->CycleActiveUser(backward); |
105 EXPECT_EQ("firstuser@test.com", GetActiveUser()); | 164 EXPECT_EQ("firstuser@test.com", GetActiveUser()); |
106 } | 165 } |
| 166 |
| 167 // Make sure MultiProfile disabled by primary user policy. |
| 168 TEST_F(SessionStateDelegateChromeOSTest, MultiProfileDisallowedByUserPolicy) { |
| 169 InitForMultiProfile(); |
| 170 EXPECT_TRUE( |
| 171 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); |
| 172 const std::string user_email(kUser); |
| 173 user_manager()->LoginUser(user_email); |
| 174 EXPECT_TRUE( |
| 175 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); |
| 176 |
| 177 user_profile_->GetPrefs()->SetString( |
| 178 prefs::kMultiProfileUserBehavior, |
| 179 chromeos::MultiProfileUserController::kBehaviorNotAllowed); |
| 180 EXPECT_FALSE( |
| 181 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); |
| 182 } |
| 183 |
| 184 // Make sure MultiProfile disabled by primary user policy certificates. |
| 185 TEST_F(SessionStateDelegateChromeOSTest, |
| 186 MultiProfileDisallowedByPolicyCertificates) { |
| 187 InitForMultiProfile(); |
| 188 const std::string user_email(kUser); |
| 189 user_manager()->LoginUser(user_email); |
| 190 EXPECT_TRUE( |
| 191 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); |
| 192 policy::PolicyCertServiceFactory::SetUsedPolicyCertificates(user_email); |
| 193 EXPECT_FALSE( |
| 194 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); |
| 195 |
| 196 // Flush tasks posted to IO. |
| 197 base::RunLoop().RunUntilIdle(); |
| 198 } |
| 199 |
| 200 // Make sure MultiProfile disabled by primary user certificates in memory. |
| 201 TEST_F(SessionStateDelegateChromeOSTest, |
| 202 MultiProfileDisallowedByPrimaryUserCertificatesInMemory) { |
| 203 InitForMultiProfile(); |
| 204 const std::string user_email(kUser); |
| 205 user_manager()->LoginUser(user_email); |
| 206 EXPECT_TRUE( |
| 207 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); |
| 208 cert_verifier_.reset(new policy::PolicyCertVerifier(base::Closure())); |
| 209 g_policy_cert_verifier_for_factory = cert_verifier_.get(); |
| 210 ASSERT_TRUE( |
| 211 policy::PolicyCertServiceFactory::GetInstance()->SetTestingFactoryAndUse( |
| 212 user_profile_, CreateTestPolicyCertService)); |
| 213 policy::PolicyCertService* service = |
| 214 policy::PolicyCertServiceFactory::GetForProfile(user_profile_); |
| 215 ASSERT_TRUE(service); |
| 216 |
| 217 EXPECT_FALSE(service->has_policy_certificates()); |
| 218 net::CertificateList certificates; |
| 219 certificates.push_back(new net::X509Certificate( |
| 220 "subject", "issuer", base::Time(), base::Time())); |
| 221 service->OnTrustAnchorsChanged(certificates); |
| 222 EXPECT_TRUE(service->has_policy_certificates()); |
| 223 EXPECT_FALSE( |
| 224 session_state_delegate()->IsMultiProfileAllowedByPrimaryUserPolicy()); |
| 225 |
| 226 // Flush tasks posted to IO. |
| 227 base::RunLoop().RunUntilIdle(); |
| 228 } |
| 229 |
| 230 } // namespace chromeos |
OLD | NEW |