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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
|
// Copyright 2014 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/chromeos/system/device_disabling_manager.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/prefs/testing_pref_service.h"
#include "base/run_loop.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/chromeos/login/users/fake_user_manager.h"
#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
#include "chrome/browser/chromeos/policy/device_cloud_policy_manager_chromeos.h"
#include "chrome/browser/chromeos/policy/device_policy_builder.h"
#include "chrome/browser/chromeos/policy/server_backed_device_state.h"
#include "chrome/browser/chromeos/policy/stub_enterprise_install_attributes.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chromeos/chromeos_switches.h"
#include "components/ownership/mock_owner_key_util.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "policy/proto/device_management_backend.pb.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::Mock;
namespace chromeos {
namespace system {
namespace {
const char kTestUser[] = "user@example.com";
const char kEnrollmentDomain[] = "example.com";
const char kDisabledMessage1[] = "Device disabled 1.";
const char kDisabledMessage2[] = "Device disabled 2.";
}
class DeviceDisablingManagerTestBase : public testing::Test,
public DeviceDisablingManager::Delegate {
public:
DeviceDisablingManagerTestBase();
// testing::Test:
void TearDown() override;
virtual void CreateDeviceDisablingManager();
virtual void DestroyDeviceDisablingManager();
void UpdateInstallAttributes(const std::string& enrollment_domain,
const std::string& registration_user,
policy::DeviceMode device_mode);
void LogIn();
// DeviceDisablingManager::Delegate:
MOCK_METHOD0(RestartToLoginScreen, void());
MOCK_METHOD0(ShowDeviceDisabledScreen, void());
DeviceDisablingManager* GetDeviceDisablingManager() {
return device_disabling_manager_.get();
}
private:
policy::ScopedStubEnterpriseInstallAttributes install_attributes_;
chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
chromeos::ScopedTestCrosSettings test_cros_settings_;
FakeUserManager fake_user_manager_;
scoped_ptr<DeviceDisablingManager> device_disabling_manager_;
DISALLOW_COPY_AND_ASSIGN(DeviceDisablingManagerTestBase);
};
DeviceDisablingManagerTestBase::DeviceDisablingManagerTestBase()
: install_attributes_("", "", "", policy::DEVICE_MODE_NOT_SET) {
}
void DeviceDisablingManagerTestBase::TearDown() {
DestroyDeviceDisablingManager();
}
void DeviceDisablingManagerTestBase::CreateDeviceDisablingManager() {
device_disabling_manager_.reset(new DeviceDisablingManager(
this,
CrosSettings::Get(),
&fake_user_manager_));
}
void DeviceDisablingManagerTestBase::DestroyDeviceDisablingManager() {
device_disabling_manager_.reset();
}
void DeviceDisablingManagerTestBase::UpdateInstallAttributes(
const std::string& enrollment_domain,
const std::string& registration_user,
policy::DeviceMode device_mode) {
policy::StubEnterpriseInstallAttributes* install_attributes =
static_cast<policy::StubEnterpriseInstallAttributes*>(
TestingBrowserProcess::GetGlobal()->platform_part()->
browser_policy_connector_chromeos()->GetInstallAttributes());
install_attributes->SetDomain(enrollment_domain);
install_attributes->SetRegistrationUser(registration_user);
install_attributes->SetMode(device_mode);
}
void DeviceDisablingManagerTestBase::LogIn() {
fake_user_manager_.AddUser(kTestUser);
}
// Base class for tests that verify device disabling behavior during OOBE, when
// the device is not owned yet.
class DeviceDisablingManagerOOBETest : public DeviceDisablingManagerTestBase {
public:
DeviceDisablingManagerOOBETest();
// DeviceDisablingManagerTestBase:
void SetUp() override;
void TearDown() override;
bool device_disabled() const { return device_disabled_; }
void CheckWhetherDeviceDisabledDuringOOBE();
void SetDeviceDisabled(bool disabled);
private:
void OnDeviceDisabledChecked(bool device_disabled);
TestingPrefServiceSimple local_state_;
base::RunLoop run_loop_;
bool device_disabled_;
DISALLOW_COPY_AND_ASSIGN(DeviceDisablingManagerOOBETest);
};
DeviceDisablingManagerOOBETest::DeviceDisablingManagerOOBETest()
: device_disabled_(false) {
EXPECT_CALL(*this, RestartToLoginScreen()).Times(0);
EXPECT_CALL(*this, ShowDeviceDisabledScreen()).Times(0);
}
void DeviceDisablingManagerOOBETest::SetUp() {
TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_);
policy::DeviceCloudPolicyManagerChromeOS::RegisterPrefs(
local_state_.registry());
CreateDeviceDisablingManager();
}
void DeviceDisablingManagerOOBETest::TearDown() {
DeviceDisablingManagerTestBase::TearDown();
TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr);
}
void DeviceDisablingManagerOOBETest::CheckWhetherDeviceDisabledDuringOOBE() {
GetDeviceDisablingManager()->CheckWhetherDeviceDisabledDuringOOBE(
base::Bind(&DeviceDisablingManagerOOBETest::OnDeviceDisabledChecked,
base::Unretained(this)));
run_loop_.Run();
}
void DeviceDisablingManagerOOBETest::SetDeviceDisabled(bool disabled) {
DictionaryPrefUpdate dict(&local_state_, prefs::kServerBackedDeviceState);
if (disabled) {
dict->SetString(policy::kDeviceStateRestoreMode,
policy::kDeviceStateRestoreModeDisabled);
} else {
dict->Remove(policy::kDeviceStateRestoreMode, nullptr);
}
dict->SetString(policy::kDeviceStateManagementDomain, kEnrollmentDomain);
dict->SetString(policy::kDeviceStateDisabledMessage, kDisabledMessage1);
}
void DeviceDisablingManagerOOBETest::OnDeviceDisabledChecked(
bool device_disabled) {
device_disabled_ = device_disabled;
run_loop_.Quit();
}
// Verifies that the device is not considered disabled during OOBE by default.
TEST_F(DeviceDisablingManagerOOBETest, NotDisabledByDefault) {
CheckWhetherDeviceDisabledDuringOOBE();
EXPECT_FALSE(device_disabled());
}
// Verifies that the device is not considered disabled during OOBE when it is
// explicitly marked as not disabled.
TEST_F(DeviceDisablingManagerOOBETest, NotDisabledWhenExplicitlyNotDisabled) {
SetDeviceDisabled(false);
CheckWhetherDeviceDisabledDuringOOBE();
EXPECT_FALSE(device_disabled());
}
// Verifies that the device is not considered disabled during OOBE when device
// disabling is turned off by flag, even if the device is marked as disabled.
TEST_F(DeviceDisablingManagerOOBETest, NotDisabledWhenTurnedOffByFlag) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDisableDeviceDisabling);
SetDeviceDisabled(true);
CheckWhetherDeviceDisabledDuringOOBE();
EXPECT_FALSE(device_disabled());
}
// Verifies that the device is not considered disabled during OOBE when it is
// already enrolled, even if the device is marked as disabled.
TEST_F(DeviceDisablingManagerOOBETest, NotDisabledWhenEnterpriseOwned) {
UpdateInstallAttributes(kEnrollmentDomain,
kTestUser,
policy::DEVICE_MODE_ENTERPRISE);
SetDeviceDisabled(true);
CheckWhetherDeviceDisabledDuringOOBE();
EXPECT_FALSE(device_disabled());
}
// Verifies that the device is not considered disabled during OOBE when it is
// already owned by a consumer, even if the device is marked as disabled.
TEST_F(DeviceDisablingManagerOOBETest, NotDisabledWhenConsumerOwned) {
UpdateInstallAttributes(std::string() /* enrollment_domain */,
std::string() /* registration_user */,
policy::DEVICE_MODE_CONSUMER);
SetDeviceDisabled(true);
CheckWhetherDeviceDisabledDuringOOBE();
EXPECT_FALSE(device_disabled());
}
// Verifies that the device is considered disabled during OOBE when it is marked
// as disabled, device disabling is not turned off by flag and the device is not
// owned yet.
TEST_F(DeviceDisablingManagerOOBETest, ShowWhenDisabledAndNotOwned) {
SetDeviceDisabled(true);
CheckWhetherDeviceDisabledDuringOOBE();
EXPECT_TRUE(device_disabled());
EXPECT_EQ(kEnrollmentDomain,
GetDeviceDisablingManager()->enrollment_domain());
EXPECT_EQ(kDisabledMessage1, GetDeviceDisablingManager()->disabled_message());
}
// Base class for tests that verify device disabling behavior once the device is
// owned.
class DeviceDisablingManagerTest : public DeviceDisablingManagerTestBase,
public DeviceDisablingManager::Observer {
public:
DeviceDisablingManagerTest();
// DeviceDisablingManagerTestBase:
void TearDown() override;
void CreateDeviceDisablingManager() override;
void DestroyDeviceDisablingManager() override;
//DeviceDisablingManager::Observer:
MOCK_METHOD1(OnDisabledMessageChanged, void(const std::string&));
void SetUnowned();
void SetEnterpriseOwned();
void SetConsumerOwned();
void MakeCrosSettingsTrusted();
void SetDeviceDisabled(bool disabled);
void SetDisabledMessage(const std::string& disabled_message);
private:
void SimulatePolicyFetch();
content::TestBrowserThreadBundle thread_bundle_;
chromeos::DeviceSettingsTestHelper device_settings_test_helper_;
policy::DevicePolicyBuilder device_policy_;
DISALLOW_COPY_AND_ASSIGN(DeviceDisablingManagerTest);
};
DeviceDisablingManagerTest::DeviceDisablingManagerTest() {
}
void DeviceDisablingManagerTest::TearDown() {
chromeos::DeviceSettingsService::Get()->UnsetSessionManager();
DeviceDisablingManagerTestBase::TearDown();
}
void DeviceDisablingManagerTest::CreateDeviceDisablingManager() {
DeviceDisablingManagerTestBase::CreateDeviceDisablingManager();
GetDeviceDisablingManager()->AddObserver(this);
}
void DeviceDisablingManagerTest::DestroyDeviceDisablingManager() {
if (GetDeviceDisablingManager())
GetDeviceDisablingManager()->RemoveObserver(this);
DeviceDisablingManagerTestBase::DestroyDeviceDisablingManager();
}
void DeviceDisablingManagerTest::SetUnowned() {
UpdateInstallAttributes(std::string() /* enrollment_domain */,
std::string() /* registration_user */,
policy::DEVICE_MODE_NOT_SET);
}
void DeviceDisablingManagerTest::SetEnterpriseOwned() {
UpdateInstallAttributes(kEnrollmentDomain,
kTestUser,
policy::DEVICE_MODE_ENTERPRISE);
}
void DeviceDisablingManagerTest::SetConsumerOwned() {
UpdateInstallAttributes(std::string() /* enrollment_domain */,
std::string() /* registration_user */,
policy::DEVICE_MODE_CONSUMER);
}
void DeviceDisablingManagerTest::MakeCrosSettingsTrusted() {
scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util(
new ownership::MockOwnerKeyUtil);
owner_key_util->SetPublicKeyFromPrivateKey(*device_policy_.GetSigningKey());
chromeos::DeviceSettingsService::Get()->SetSessionManager(
&device_settings_test_helper_,
owner_key_util);
SimulatePolicyFetch();
}
void DeviceDisablingManagerTest::SetDeviceDisabled(bool disabled) {
if (disabled) {
device_policy_.policy_data().mutable_device_state()->set_device_mode(
enterprise_management::DeviceState::DEVICE_MODE_DISABLED);
} else {
device_policy_.policy_data().mutable_device_state()->clear_device_mode();
}
SimulatePolicyFetch();
}
void DeviceDisablingManagerTest::SetDisabledMessage(
const std::string& disabled_message) {
device_policy_.policy_data().mutable_device_state()->
mutable_disabled_state()->set_message(disabled_message);
SimulatePolicyFetch();
}
void DeviceDisablingManagerTest::SimulatePolicyFetch() {
device_policy_.Build();
device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
chromeos::DeviceSettingsService::Get()->OwnerKeySet(true);
device_settings_test_helper_.Flush();
}
// Verifies that the device is not considered disabled by default when it is
// enrolled for enterprise management.
TEST_F(DeviceDisablingManagerTest, NotDisabledByDefault) {
SetEnterpriseOwned();
MakeCrosSettingsTrusted();
EXPECT_CALL(*this, RestartToLoginScreen()).Times(0);
EXPECT_CALL(*this, ShowDeviceDisabledScreen()).Times(0);
EXPECT_CALL(*this, OnDisabledMessageChanged(_)).Times(0);
CreateDeviceDisablingManager();
}
// Verifies that the device is not considered disabled when it is explicitly
// marked as not disabled.
TEST_F(DeviceDisablingManagerTest, NotDisabledWhenExplicitlyNotDisabled) {
SetEnterpriseOwned();
MakeCrosSettingsTrusted();
SetDeviceDisabled(false);
EXPECT_CALL(*this, RestartToLoginScreen()).Times(0);
EXPECT_CALL(*this, ShowDeviceDisabledScreen()).Times(0);
EXPECT_CALL(*this, OnDisabledMessageChanged(_)).Times(0);
CreateDeviceDisablingManager();
}
// Verifies that the device is not considered disabled when device disabling is
// turned off by flag, even if the device is marked as disabled.
TEST_F(DeviceDisablingManagerTest, NotDisabledWhenTurnedOffByFlag) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDisableDeviceDisabling);
SetEnterpriseOwned();
MakeCrosSettingsTrusted();
SetDeviceDisabled(true);
EXPECT_CALL(*this, RestartToLoginScreen()).Times(0);
EXPECT_CALL(*this, ShowDeviceDisabledScreen()).Times(0);
EXPECT_CALL(*this, OnDisabledMessageChanged(_)).Times(0);
CreateDeviceDisablingManager();
}
// Verifies that the device is not considered disabled when it is owned by a
// consumer, even if the device is marked as disabled.
TEST_F(DeviceDisablingManagerTest, NotDisabledWhenConsumerOwned) {
SetConsumerOwned();
MakeCrosSettingsTrusted();
SetDeviceDisabled(true);
EXPECT_CALL(*this, RestartToLoginScreen()).Times(0);
EXPECT_CALL(*this, ShowDeviceDisabledScreen()).Times(0);
EXPECT_CALL(*this, OnDisabledMessageChanged(_)).Times(0);
CreateDeviceDisablingManager();
}
// Verifies that the device disabled screen is shown immediately when the device
// is already marked as disabled on start-up.
TEST_F(DeviceDisablingManagerTest, DisabledOnLoginScreen) {
SetEnterpriseOwned();
MakeCrosSettingsTrusted();
SetDisabledMessage(kDisabledMessage1);
SetDeviceDisabled(true);
EXPECT_CALL(*this, RestartToLoginScreen()).Times(0);
EXPECT_CALL(*this, ShowDeviceDisabledScreen()).Times(1);
EXPECT_CALL(*this, OnDisabledMessageChanged(_)).Times(0);
CreateDeviceDisablingManager();
EXPECT_EQ(kEnrollmentDomain,
GetDeviceDisablingManager()->enrollment_domain());
EXPECT_EQ(kDisabledMessage1, GetDeviceDisablingManager()->disabled_message());
}
// Verifies that the device disabled screen is shown immediately when the device
// becomes disabled while the login screen is showing. Also verifies that Chrome
// restarts when the device becomes enabled again.
TEST_F(DeviceDisablingManagerTest, DisableAndReEnableOnLoginScreen) {
SetEnterpriseOwned();
MakeCrosSettingsTrusted();
SetDisabledMessage(kDisabledMessage1);
// Verify that initially, the disabled screen is not shown and Chrome does not
// restart.
EXPECT_CALL(*this, RestartToLoginScreen()).Times(0);
EXPECT_CALL(*this, ShowDeviceDisabledScreen()).Times(0);
EXPECT_CALL(*this, OnDisabledMessageChanged(_)).Times(0);
CreateDeviceDisablingManager();
Mock::VerifyAndClearExpectations(this);
// Mark the device as disabled. Verify that the device disabled screen is
// shown.
EXPECT_CALL(*this, RestartToLoginScreen()).Times(0);
EXPECT_CALL(*this, ShowDeviceDisabledScreen()).Times(1);
EXPECT_CALL(*this, OnDisabledMessageChanged(_)).Times(0);
EXPECT_CALL(*this, OnDisabledMessageChanged(kDisabledMessage1)).Times(1);
SetDeviceDisabled(true);
Mock::VerifyAndClearExpectations(this);
EXPECT_EQ(kEnrollmentDomain,
GetDeviceDisablingManager()->enrollment_domain());
EXPECT_EQ(kDisabledMessage1, GetDeviceDisablingManager()->disabled_message());
// Update the disabled message. Verify that the device disabled screen is
// updated.
EXPECT_CALL(*this, RestartToLoginScreen()).Times(0);
EXPECT_CALL(*this, ShowDeviceDisabledScreen()).Times(0);
EXPECT_CALL(*this, OnDisabledMessageChanged(_)).Times(0);
EXPECT_CALL(*this, OnDisabledMessageChanged(kDisabledMessage2)).Times(1);
SetDisabledMessage(kDisabledMessage2);
Mock::VerifyAndClearExpectations(this);
EXPECT_EQ(kDisabledMessage2, GetDeviceDisablingManager()->disabled_message());
// Mark the device as enabled again. Verify that Chrome restarts.
EXPECT_CALL(*this, RestartToLoginScreen()).Times(1);
EXPECT_CALL(*this, ShowDeviceDisabledScreen()).Times(0);
EXPECT_CALL(*this, OnDisabledMessageChanged(_)).Times(0);
SetDeviceDisabled(false);
}
// Verifies that Chrome restarts when the device becomes disabled while a
// session is in progress.
TEST_F(DeviceDisablingManagerTest, DisableDuringSession) {
SetEnterpriseOwned();
MakeCrosSettingsTrusted();
SetDisabledMessage(kDisabledMessage1);
LogIn();
// Verify that initially, the disabled screen is not shown and Chrome does not
// restart.
EXPECT_CALL(*this, RestartToLoginScreen()).Times(0);
EXPECT_CALL(*this, ShowDeviceDisabledScreen()).Times(0);
EXPECT_CALL(*this, OnDisabledMessageChanged(_)).Times(0);
CreateDeviceDisablingManager();
Mock::VerifyAndClearExpectations(this);
// Mark the device as disabled. Verify that Chrome restarts.
EXPECT_CALL(*this, RestartToLoginScreen()).Times(1);
EXPECT_CALL(*this, ShowDeviceDisabledScreen()).Times(0);
EXPECT_CALL(*this, OnDisabledMessageChanged(_)).Times(0);
EXPECT_CALL(*this, OnDisabledMessageChanged(kDisabledMessage1)).Times(1);
SetDeviceDisabled(true);
}
// Verifies that the HonorDeviceDisablingDuringNormalOperation() method returns
// true iff the device is enterprise enrolled and device disabling is not turned
// off by flag.
TEST_F(DeviceDisablingManagerTest, HonorDeviceDisablingDuringNormalOperation) {
// Not enterprise owned, not disabled by flag.
EXPECT_FALSE(
DeviceDisablingManager::HonorDeviceDisablingDuringNormalOperation());
// Enterprise owned, not disabled by flag.
SetEnterpriseOwned();
EXPECT_TRUE(
DeviceDisablingManager::HonorDeviceDisablingDuringNormalOperation());
// Enterprise owned, disabled by flag.
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDisableDeviceDisabling);
EXPECT_FALSE(
DeviceDisablingManager::HonorDeviceDisablingDuringNormalOperation());
// Not enterprise owned, disabled by flag.
SetUnowned();
EXPECT_FALSE(
DeviceDisablingManager::HonorDeviceDisablingDuringNormalOperation());
}
} // namespace system
} // namespace chromeos
|