Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / chromeos / login / users / user_manager_unittest.cc
blobb2e6368ece6f74a8757f46e6ac2a702deecf8607
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 <cstdlib>
6 #include <cstring>
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 "chrome/browser/browser_process.h"
15 #include "chrome/browser/chromeos/login/users/chrome_user_manager_impl.h"
16 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
17 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
18 #include "chrome/browser/chromeos/settings/scoped_cros_settings_test_helper.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/test/base/scoped_testing_local_state.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "chromeos/chromeos_switches.h"
24 #include "chromeos/dbus/dbus_thread_manager.h"
25 #include "chromeos/settings/cros_settings_names.h"
26 #include "components/user_manager/user.h"
27 #include "components/user_manager/user_manager.h"
28 #include "content/public/common/content_switches.h"
29 #include "content/public/test/test_browser_thread_bundle.h"
30 #include "testing/gtest/include/gtest/gtest.h"
32 namespace chromeos {
34 class UnittestProfileManager : public ::ProfileManagerWithoutInit {
35 public:
36 explicit UnittestProfileManager(const base::FilePath& user_data_dir)
37 : ::ProfileManagerWithoutInit(user_data_dir) {}
39 protected:
40 Profile* CreateProfileHelper(const base::FilePath& file_path) override {
41 if (!base::PathExists(file_path)) {
42 if (!base::CreateDirectory(file_path))
43 return NULL;
45 return new TestingProfile(file_path, NULL);
49 class UserManagerTest : public testing::Test {
50 protected:
51 void SetUp() override {
52 base::CommandLine& command_line = *base::CommandLine::ForCurrentProcess();
53 command_line.AppendSwitch(::switches::kTestType);
54 command_line.AppendSwitch(
55 chromeos::switches::kIgnoreUserProfileMappingForTests);
57 settings_helper_.ReplaceProvider(kDeviceOwner);
59 // Populate the stub DeviceSettingsProvider with valid values.
60 SetDeviceSettings(false, "", false);
62 // Register an in-memory local settings instance.
63 local_state_.reset(
64 new ScopedTestingLocalState(TestingBrowserProcess::GetGlobal()));
66 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
67 TestingBrowserProcess::GetGlobal()->SetProfileManager(
68 new UnittestProfileManager(temp_dir_.path()));
70 chromeos::DBusThreadManager::Initialize();
72 ResetUserManager();
73 WallpaperManager::Initialize();
76 void TearDown() override {
77 // Unregister the in-memory local settings instance.
78 local_state_.reset();
80 // Shut down the DeviceSettingsService.
81 DeviceSettingsService::Get()->UnsetSessionManager();
82 TestingBrowserProcess::GetGlobal()->SetProfileManager(NULL);
84 base::RunLoop().RunUntilIdle();
85 chromeos::DBusThreadManager::Shutdown();
86 WallpaperManager::Shutdown();
89 ChromeUserManagerImpl* GetChromeUserManager() const {
90 return static_cast<ChromeUserManagerImpl*>(
91 user_manager::UserManager::Get());
94 bool GetUserManagerEphemeralUsersEnabled() const {
95 return GetChromeUserManager()->GetEphemeralUsersEnabled();
98 void SetUserManagerEphemeralUsersEnabled(bool ephemeral_users_enabled) {
99 GetChromeUserManager()->SetEphemeralUsersEnabled(ephemeral_users_enabled);
102 const std::string& GetUserManagerOwnerEmail() const {
103 return GetChromeUserManager()->GetOwnerEmail();
106 void SetUserManagerOwnerEmail(const std::string& owner_email) {
107 GetChromeUserManager()->SetOwnerEmail(owner_email);
110 void ResetUserManager() {
111 // Reset the UserManager singleton.
112 user_manager_enabler_.reset();
113 // Initialize the UserManager singleton to a fresh ChromeUserManagerImpl
114 // instance.
115 user_manager_enabler_.reset(
116 new ScopedUserManagerEnabler(new ChromeUserManagerImpl));
118 // ChromeUserManagerImpl ctor posts a task to reload policies.
119 base::RunLoop().RunUntilIdle();
122 void SetDeviceSettings(bool ephemeral_users_enabled,
123 const std::string &owner,
124 bool supervised_users_enabled) {
125 settings_helper_.SetBoolean(kAccountsPrefEphemeralUsersEnabled,
126 ephemeral_users_enabled);
127 settings_helper_.SetString(kDeviceOwner, owner);
128 settings_helper_.SetBoolean(kAccountsPrefSupervisedUsersEnabled,
129 supervised_users_enabled);
132 void RetrieveTrustedDevicePolicies() {
133 GetChromeUserManager()->RetrieveTrustedDevicePolicies();
136 protected:
137 content::TestBrowserThreadBundle thread_bundle_;
139 ScopedCrosSettingsTestHelper settings_helper_;
140 scoped_ptr<ScopedTestingLocalState> local_state_;
142 scoped_ptr<ScopedUserManagerEnabler> user_manager_enabler_;
143 base::ScopedTempDir temp_dir_;
146 TEST_F(UserManagerTest, RetrieveTrustedDevicePolicies) {
147 SetUserManagerEphemeralUsersEnabled(true);
148 SetUserManagerOwnerEmail("");
150 SetDeviceSettings(false, "owner@invalid.domain", false);
151 RetrieveTrustedDevicePolicies();
153 EXPECT_FALSE(GetUserManagerEphemeralUsersEnabled());
154 EXPECT_EQ(GetUserManagerOwnerEmail(), "owner@invalid.domain");
157 TEST_F(UserManagerTest, RemoveAllExceptOwnerFromList) {
158 user_manager::UserManager::Get()->UserLoggedIn(
159 "owner@invalid.domain", "owner@invalid.domain", false);
160 ResetUserManager();
161 user_manager::UserManager::Get()->UserLoggedIn(
162 "user0@invalid.domain", "owner@invalid.domain", false);
163 ResetUserManager();
164 user_manager::UserManager::Get()->UserLoggedIn(
165 "user1@invalid.domain", "owner@invalid.domain", false);
166 ResetUserManager();
168 const user_manager::UserList* users =
169 &user_manager::UserManager::Get()->GetUsers();
170 ASSERT_EQ(3U, users->size());
171 EXPECT_EQ((*users)[0]->email(), "user1@invalid.domain");
172 EXPECT_EQ((*users)[1]->email(), "user0@invalid.domain");
173 EXPECT_EQ((*users)[2]->email(), "owner@invalid.domain");
175 SetDeviceSettings(true, "owner@invalid.domain", false);
176 RetrieveTrustedDevicePolicies();
178 users = &user_manager::UserManager::Get()->GetUsers();
179 EXPECT_EQ(1U, users->size());
180 EXPECT_EQ((*users)[0]->email(), "owner@invalid.domain");
183 TEST_F(UserManagerTest, RegularUserLoggedInAsEphemeral) {
184 SetDeviceSettings(true, "owner@invalid.domain", false);
185 RetrieveTrustedDevicePolicies();
187 user_manager::UserManager::Get()->UserLoggedIn(
188 "owner@invalid.domain", "user0@invalid.domain", false);
189 ResetUserManager();
190 user_manager::UserManager::Get()->UserLoggedIn(
191 "user0@invalid.domain", "user0@invalid.domain", false);
192 ResetUserManager();
194 const user_manager::UserList* users =
195 &user_manager::UserManager::Get()->GetUsers();
196 EXPECT_EQ(1U, users->size());
197 EXPECT_EQ((*users)[0]->email(), "owner@invalid.domain");
200 } // namespace chromeos