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(new chromeos::FakeOwnerSettingsService(
63 profile_
.get(), owner_key_util_
, nullptr));
64 chromeos::OwnerSettingsServiceChromeOS::ManagementSettings settings
;
65 settings
.management_mode
= policy::MANAGEMENT_MODE_CONSUMER_MANAGED
;
66 settings
.request_token
= "fake_request_token";
67 settings
.device_id
= "fake_device_id";
68 fake_owner_settings_service_
->SetManagementSettings(
70 base::Bind(&ConsumerUnenrollmentHandlerTest::OnManagementSettingsSet
,
71 base::Unretained(this)));
74 void OnManagementSettingsSet(bool success
) {
78 void RunUnenrollment() {
79 handler_
.reset(new ConsumerUnenrollmentHandler(
80 &device_settings_service_
,
83 fake_owner_settings_service_
.get()));
85 FlushDeviceSettings();
88 scoped_ptr
<FakeConsumerManagementService
> fake_service_
;
89 scoped_ptr
<chromeos::FakeCryptohomeClient
> fake_cryptohome_client_
;
90 scoped_ptr
<EnterpriseInstallAttributes
> install_attributes_
;
91 scoped_ptr
<FakeDeviceCloudPolicyManager
> fake_manager_
;
92 scoped_ptr
<chromeos::FakeOwnerSettingsService
> fake_owner_settings_service_
;
94 scoped_ptr
<ConsumerUnenrollmentHandler
> handler_
;
97 TEST_F(ConsumerUnenrollmentHandlerTest
, UnenrollmentSucceeds
) {
98 EXPECT_EQ(ConsumerManagementStage::None(), fake_service_
->GetStage());
102 EXPECT_EQ(ConsumerManagementStage::UnenrollmentSuccess(),
103 fake_service_
->GetStage());
104 const chromeos::OwnerSettingsServiceChromeOS::ManagementSettings
& settings
=
105 fake_owner_settings_service_
->last_settings();
106 EXPECT_EQ(policy::MANAGEMENT_MODE_LOCAL_OWNER
, settings
.management_mode
);
107 EXPECT_EQ("", settings
.request_token
);
108 EXPECT_EQ("", settings
.device_id
);
111 TEST_F(ConsumerUnenrollmentHandlerTest
,
112 UnenrollmentFailsOnServerError
) {
113 EXPECT_EQ(ConsumerManagementStage::None(), fake_service_
->GetStage());
114 fake_manager_
->set_unregister_result(false);
118 EXPECT_EQ(ConsumerManagementStage::UnenrollmentDMServerFailed(),
119 fake_service_
->GetStage());
120 const chromeos::OwnerSettingsServiceChromeOS::ManagementSettings
& settings
=
121 fake_owner_settings_service_
->last_settings();
122 EXPECT_EQ(policy::MANAGEMENT_MODE_CONSUMER_MANAGED
, settings
.management_mode
);
123 EXPECT_EQ("fake_request_token", settings
.request_token
);
124 EXPECT_EQ("fake_device_id", settings
.device_id
);
127 } // namespace policy