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 "chrome/browser/chromeos/policy/consumer_unenrollment_handler.h"
8 #include "base/bind_helpers.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chrome/browser/chromeos/ownership/fake_owner_settings_service.h"
12 #include "chrome/browser/chromeos/policy/consumer_management_service.h"
13 #include "chrome/browser/chromeos/policy/consumer_management_stage.h"
14 #include "chrome/browser/chromeos/policy/device_cloud_policy_store_chromeos.h"
15 #include "chrome/browser/chromeos/policy/enterprise_install_attributes.h"
16 #include "chrome/browser/chromeos/policy/fake_consumer_management_service.h"
17 #include "chrome/browser/chromeos/policy/fake_device_cloud_policy_manager.h"
18 #include "chrome/browser/chromeos/settings/device_settings_service.h"
19 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
20 #include "chrome/test/base/testing_profile.h"
21 #include "chromeos/dbus/fake_cryptohome_client.h"
22 #include "components/ownership/mock_owner_key_util.h"
23 #include "policy/proto/device_management_backend.pb.h"
24 #include "testing/gtest/include/gtest/gtest.h"
28 class ConsumerUnenrollmentHandlerTest
29 : public chromeos::DeviceSettingsTestBase
{
31 ConsumerUnenrollmentHandlerTest()
32 : fake_service_(new FakeConsumerManagementService()),
33 fake_cryptohome_client_(new chromeos::FakeCryptohomeClient()),
35 new EnterpriseInstallAttributes(fake_cryptohome_client_
.get())) {
36 // Set up FakeConsumerManagementService.
37 fake_service_
->SetStatusAndStage(
38 ConsumerManagementService::STATUS_ENROLLED
,
39 ConsumerManagementStage::None());
42 void SetUp() override
{
43 DeviceSettingsTestBase::SetUp();
45 // Set up the ownership, so that we can modify device settings.
46 owner_key_util_
->SetPrivateKey(device_policy_
.GetSigningKey());
47 InitOwner(device_policy_
.policy_data().username(), true);
48 FlushDeviceSettings();
51 // Set up FakeDeviceCloudPolicyManager.
52 scoped_ptr
<DeviceCloudPolicyStoreChromeOS
> store_(
53 new DeviceCloudPolicyStoreChromeOS(
54 &device_settings_service_
,
55 install_attributes_
.get(),
56 base::ThreadTaskRunnerHandle::Get()));
57 fake_manager_
.reset(new FakeDeviceCloudPolicyManager(
59 base::ThreadTaskRunnerHandle::Get()));
61 // Set up FakeOwnerSettingsService.
62 fake_owner_settings_service_
.reset(
63 new chromeos::FakeOwnerSettingsService(
64 profile_
.get(), owner_key_util_
));
65 chromeos::OwnerSettingsServiceChromeOS::ManagementSettings settings
;
66 settings
.management_mode
= policy::MANAGEMENT_MODE_CONSUMER_MANAGED
;
67 settings
.request_token
= "fake_request_token";
68 settings
.device_id
= "fake_device_id";
69 fake_owner_settings_service_
->SetManagementSettings(
71 base::Bind(&ConsumerUnenrollmentHandlerTest::OnManagementSettingsSet
,
72 base::Unretained(this)));
75 void OnManagementSettingsSet(bool success
) {
79 void RunUnenrollment() {
80 handler_
.reset(new ConsumerUnenrollmentHandler(
81 &device_settings_service_
,
84 fake_owner_settings_service_
.get()));
86 FlushDeviceSettings();
89 scoped_ptr
<FakeConsumerManagementService
> fake_service_
;
90 scoped_ptr
<chromeos::FakeCryptohomeClient
> fake_cryptohome_client_
;
91 scoped_ptr
<EnterpriseInstallAttributes
> install_attributes_
;
92 scoped_ptr
<FakeDeviceCloudPolicyManager
> fake_manager_
;
93 scoped_ptr
<chromeos::FakeOwnerSettingsService
> fake_owner_settings_service_
;
95 scoped_ptr
<ConsumerUnenrollmentHandler
> handler_
;
98 TEST_F(ConsumerUnenrollmentHandlerTest
, UnenrollmentSucceeds
) {
99 EXPECT_EQ(ConsumerManagementStage::None(), fake_service_
->GetStage());
103 EXPECT_EQ(ConsumerManagementStage::UnenrollmentSuccess(),
104 fake_service_
->GetStage());
105 const chromeos::OwnerSettingsServiceChromeOS::ManagementSettings
& settings
=
106 fake_owner_settings_service_
->last_settings();
107 EXPECT_EQ(policy::MANAGEMENT_MODE_LOCAL_OWNER
, settings
.management_mode
);
108 EXPECT_EQ("", settings
.request_token
);
109 EXPECT_EQ("", settings
.device_id
);
112 TEST_F(ConsumerUnenrollmentHandlerTest
,
113 UnenrollmentFailsOnServerError
) {
114 EXPECT_EQ(ConsumerManagementStage::None(), fake_service_
->GetStage());
115 fake_manager_
->set_unregister_result(false);
119 EXPECT_EQ(ConsumerManagementStage::UnenrollmentDMServerFailed(),
120 fake_service_
->GetStage());
121 const chromeos::OwnerSettingsServiceChromeOS::ManagementSettings
& settings
=
122 fake_owner_settings_service_
->last_settings();
123 EXPECT_EQ(policy::MANAGEMENT_MODE_CONSUMER_MANAGED
, settings
.management_mode
);
124 EXPECT_EQ("fake_request_token", settings
.request_token
);
125 EXPECT_EQ("fake_device_id", settings
.device_id
);
128 } // namespace policy