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.
8 #include "base/command_line.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/prefs/pref_service.h"
13 #include "base/run_loop.h"
14 #include "base/values.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/chromeos/login/users/chrome_user_manager_impl.h"
17 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
18 #include "chrome/browser/chromeos/settings/cros_settings.h"
19 #include "chrome/browser/chromeos/settings/device_settings_service.h"
20 #include "chrome/browser/chromeos/settings/stub_cros_settings_provider.h"
21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/test/base/scoped_testing_local_state.h"
23 #include "chrome/test/base/testing_browser_process.h"
24 #include "chrome/test/base/testing_profile.h"
25 #include "chromeos/chromeos_switches.h"
26 #include "chromeos/dbus/dbus_thread_manager.h"
27 #include "chromeos/settings/cros_settings_names.h"
28 #include "chromeos/settings/cros_settings_provider.h"
29 #include "components/user_manager/user.h"
30 #include "components/user_manager/user_manager.h"
31 #include "content/public/common/content_switches.h"
32 #include "content/public/test/test_browser_thread_bundle.h"
33 #include "testing/gtest/include/gtest/gtest.h"
37 class UnittestProfileManager
: public ::ProfileManagerWithoutInit
{
39 explicit UnittestProfileManager(const base::FilePath
& user_data_dir
)
40 : ::ProfileManagerWithoutInit(user_data_dir
) {}
43 Profile
* CreateProfileHelper(const base::FilePath
& file_path
) override
{
44 if (!base::PathExists(file_path
)) {
45 if (!base::CreateDirectory(file_path
))
48 return new TestingProfile(file_path
, NULL
);
53 class UserManagerTest
: public testing::Test
{
55 void SetUp() override
{
56 base::CommandLine
& command_line
= *base::CommandLine::ForCurrentProcess();
57 command_line
.AppendSwitch(::switches::kTestType
);
58 command_line
.AppendSwitch(
59 chromeos::switches::kIgnoreUserProfileMappingForTests
);
61 cros_settings_
= CrosSettings::Get();
63 // Replace the real DeviceSettingsProvider with a stub.
64 device_settings_provider_
=
65 cros_settings_
->GetProvider(chromeos::kReportDeviceVersionInfo
);
66 EXPECT_TRUE(device_settings_provider_
);
68 cros_settings_
->RemoveSettingsProvider(device_settings_provider_
));
69 cros_settings_
->AddSettingsProvider(&stub_settings_provider_
);
71 // Populate the stub DeviceSettingsProvider with valid values.
72 SetDeviceSettings(false, "", false);
74 // Register an in-memory local settings instance.
76 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
78 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
79 TestingBrowserProcess::GetGlobal()->SetProfileManager(
80 new UnittestProfileManager(temp_dir_
.path()));
82 chromeos::DBusThreadManager::Initialize();
87 void TearDown() override
{
88 // Unregister the in-memory local settings instance.
91 // Restore the real DeviceSettingsProvider.
93 cros_settings_
->RemoveSettingsProvider(&stub_settings_provider_
));
94 cros_settings_
->AddSettingsProvider(device_settings_provider_
);
96 // Shut down the DeviceSettingsService.
97 DeviceSettingsService::Get()->UnsetSessionManager();
98 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL
);
100 base::RunLoop().RunUntilIdle();
101 chromeos::DBusThreadManager::Shutdown();
104 ChromeUserManagerImpl
* GetChromeUserManager() const {
105 return static_cast<ChromeUserManagerImpl
*>(
106 user_manager::UserManager::Get());
109 bool GetUserManagerEphemeralUsersEnabled() const {
110 return GetChromeUserManager()->GetEphemeralUsersEnabled();
113 void SetUserManagerEphemeralUsersEnabled(bool ephemeral_users_enabled
) {
114 GetChromeUserManager()->SetEphemeralUsersEnabled(ephemeral_users_enabled
);
117 const std::string
& GetUserManagerOwnerEmail() const {
118 return GetChromeUserManager()->GetOwnerEmail();
121 void SetUserManagerOwnerEmail(const std::string
& owner_email
) {
122 GetChromeUserManager()->SetOwnerEmail(owner_email
);
125 void ResetUserManager() {
126 // Reset the UserManager singleton.
127 user_manager_enabler_
.reset();
128 // Initialize the UserManager singleton to a fresh ChromeUserManagerImpl
130 user_manager_enabler_
.reset(
131 new ScopedUserManagerEnabler(new ChromeUserManagerImpl
));
133 // ChromeUserManagerImpl ctor posts a task to reload policies.
134 base::RunLoop().RunUntilIdle();
137 void SetDeviceSettings(bool ephemeral_users_enabled
,
138 const std::string
&owner
,
139 bool supervised_users_enabled
) {
140 base::FundamentalValue
141 ephemeral_users_enabled_value(ephemeral_users_enabled
);
142 stub_settings_provider_
.Set(kAccountsPrefEphemeralUsersEnabled
,
143 ephemeral_users_enabled_value
);
144 base::StringValue
owner_value(owner
);
145 stub_settings_provider_
.Set(kDeviceOwner
, owner_value
);
146 stub_settings_provider_
.Set(kAccountsPrefSupervisedUsersEnabled
,
147 base::FundamentalValue(supervised_users_enabled
));
150 void RetrieveTrustedDevicePolicies() {
151 GetChromeUserManager()->RetrieveTrustedDevicePolicies();
155 content::TestBrowserThreadBundle thread_bundle_
;
157 CrosSettings
* cros_settings_
;
158 CrosSettingsProvider
* device_settings_provider_
;
159 StubCrosSettingsProvider stub_settings_provider_
;
160 scoped_ptr
<ScopedTestingLocalState
> local_state_
;
162 ScopedTestDeviceSettingsService test_device_settings_service_
;
163 ScopedTestCrosSettings test_cros_settings_
;
165 scoped_ptr
<ScopedUserManagerEnabler
> user_manager_enabler_
;
166 base::ScopedTempDir temp_dir_
;
169 TEST_F(UserManagerTest
, RetrieveTrustedDevicePolicies
) {
170 SetUserManagerEphemeralUsersEnabled(true);
171 SetUserManagerOwnerEmail("");
173 SetDeviceSettings(false, "owner@invalid.domain", false);
174 RetrieveTrustedDevicePolicies();
176 EXPECT_FALSE(GetUserManagerEphemeralUsersEnabled());
177 EXPECT_EQ(GetUserManagerOwnerEmail(), "owner@invalid.domain");
180 TEST_F(UserManagerTest
, RemoveAllExceptOwnerFromList
) {
181 user_manager::UserManager::Get()->UserLoggedIn(
182 "owner@invalid.domain", "owner@invalid.domain", false);
184 user_manager::UserManager::Get()->UserLoggedIn(
185 "user0@invalid.domain", "owner@invalid.domain", false);
187 user_manager::UserManager::Get()->UserLoggedIn(
188 "user1@invalid.domain", "owner@invalid.domain", false);
191 const user_manager::UserList
* users
=
192 &user_manager::UserManager::Get()->GetUsers();
193 ASSERT_EQ(3U, users
->size());
194 EXPECT_EQ((*users
)[0]->email(), "user1@invalid.domain");
195 EXPECT_EQ((*users
)[1]->email(), "user0@invalid.domain");
196 EXPECT_EQ((*users
)[2]->email(), "owner@invalid.domain");
198 SetDeviceSettings(true, "owner@invalid.domain", false);
199 RetrieveTrustedDevicePolicies();
201 users
= &user_manager::UserManager::Get()->GetUsers();
202 EXPECT_EQ(1U, users
->size());
203 EXPECT_EQ((*users
)[0]->email(), "owner@invalid.domain");
206 TEST_F(UserManagerTest
, RegularUserLoggedInAsEphemeral
) {
207 SetDeviceSettings(true, "owner@invalid.domain", false);
208 RetrieveTrustedDevicePolicies();
210 user_manager::UserManager::Get()->UserLoggedIn(
211 "owner@invalid.domain", "user0@invalid.domain", false);
213 user_manager::UserManager::Get()->UserLoggedIn(
214 "user0@invalid.domain", "user0@invalid.domain", false);
217 const user_manager::UserList
* users
=
218 &user_manager::UserManager::Get()->GetUsers();
219 EXPECT_EQ(1U, users
->size());
220 EXPECT_EQ((*users
)[0]->email(), "owner@invalid.domain");
223 } // namespace chromeos