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