Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / consumer_management_service_unittest.cc
blob12565ebcd08b6919543195c44c629cd336c7da75
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_management_service.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/chromeos/policy/device_policy_builder.h"
14 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h"
15 #include "chrome/common/pref_names.h"
16 #include "chrome/test/base/browser_with_test_window_test.h"
17 #include "chrome/test/base/scoped_testing_local_state.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "chrome/test/base/testing_profile_manager.h"
20 #include "chromeos/dbus/cryptohome/rpc.pb.h"
21 #include "chromeos/dbus/cryptohome_client.h"
22 #include "chromeos/dbus/mock_cryptohome_client.h"
23 #include "policy/proto/device_management_backend.pb.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using testing::Invoke;
28 using testing::NiceMock;
29 using testing::_;
31 namespace em = enterprise_management;
33 namespace {
34 const char kAttributeOwnerId[] = "consumer_management.owner_id";
35 const char kTestOwner[] = "test@chromium.org.test";
38 namespace policy {
40 class ConsumerManagementServiceTest : public BrowserWithTestWindowTest {
41 public:
42 ConsumerManagementServiceTest()
43 : cryptohome_result_(false),
44 set_owner_status_(false) {
45 ON_CALL(mock_cryptohome_client_, GetBootAttribute(_, _))
46 .WillByDefault(
47 Invoke(this, &ConsumerManagementServiceTest::MockGetBootAttribute));
48 ON_CALL(mock_cryptohome_client_, SetBootAttribute(_, _))
49 .WillByDefault(
50 Invoke(this, &ConsumerManagementServiceTest::MockSetBootAttribute));
51 ON_CALL(mock_cryptohome_client_, FlushAndSignBootAttributes(_, _))
52 .WillByDefault(
53 Invoke(this,
54 &ConsumerManagementServiceTest::
55 MockFlushAndSignBootAttributes));
58 void SetUp() override {
59 BrowserWithTestWindowTest::SetUp();
61 testing_profile_manager_.reset(new TestingProfileManager(
62 TestingBrowserProcess::GetGlobal()));
63 ASSERT_TRUE(testing_profile_manager_->SetUp());
64 service_.reset(new ConsumerManagementService(&mock_cryptohome_client_,
65 NULL));
68 void TearDown() override {
69 testing_profile_manager_.reset();
70 service_.reset();
72 BrowserWithTestWindowTest::TearDown();
75 ConsumerManagementStage GetStageFromLocalState() {
76 return ConsumerManagementStage::FromInternalValue(
77 g_browser_process->local_state()->GetInteger(
78 prefs::kConsumerManagementStage));
81 void SetStageInLocalState(ConsumerManagementStage stage) {
82 g_browser_process->local_state()->SetInteger(
83 prefs::kConsumerManagementStage, stage.ToInternalValue());
86 void MockGetBootAttribute(
87 const cryptohome::GetBootAttributeRequest& request,
88 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) {
89 get_boot_attribute_request_ = request;
90 callback.Run(cryptohome_status_, cryptohome_result_, cryptohome_reply_);
93 void MockSetBootAttribute(
94 const cryptohome::SetBootAttributeRequest& request,
95 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) {
96 set_boot_attribute_request_ = request;
97 callback.Run(cryptohome_status_, cryptohome_result_, cryptohome_reply_);
100 void MockFlushAndSignBootAttributes(
101 const cryptohome::FlushAndSignBootAttributesRequest& request,
102 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) {
103 callback.Run(cryptohome_status_, cryptohome_result_, cryptohome_reply_);
106 void OnGetOwnerDone(const std::string& owner) {
107 owner_ = owner;
110 void OnSetOwnerDone(bool status) {
111 set_owner_status_ = status;
114 NiceMock<chromeos::MockCryptohomeClient> mock_cryptohome_client_;
115 scoped_ptr<ConsumerManagementService> service_;
116 scoped_ptr<TestingProfileManager> testing_profile_manager_;
118 // Variables for setting the return value or catching the arguments of mock
119 // functions.
120 chromeos::DBusMethodCallStatus cryptohome_status_;
121 bool cryptohome_result_;
122 cryptohome::BaseReply cryptohome_reply_;
123 cryptohome::GetBootAttributeRequest get_boot_attribute_request_;
124 cryptohome::SetBootAttributeRequest set_boot_attribute_request_;
125 std::string owner_;
126 bool set_owner_status_;
129 TEST_F(ConsumerManagementServiceTest, CanGetStage) {
130 EXPECT_EQ(ConsumerManagementStage::None(), service_->GetStage());
132 SetStageInLocalState(ConsumerManagementStage::EnrollmentRequested());
134 EXPECT_EQ(ConsumerManagementStage::EnrollmentRequested(),
135 service_->GetStage());
138 TEST_F(ConsumerManagementServiceTest, CanSetStage) {
139 EXPECT_EQ(ConsumerManagementStage::None(), GetStageFromLocalState());
141 service_->SetStage(ConsumerManagementStage::EnrollmentRequested());
143 EXPECT_EQ(ConsumerManagementStage::EnrollmentRequested(),
144 GetStageFromLocalState());
147 TEST_F(ConsumerManagementServiceTest, CanGetOwner) {
148 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_SUCCESS;
149 cryptohome_result_ = true;
150 cryptohome_reply_.MutableExtension(cryptohome::GetBootAttributeReply::reply)->
151 set_value(kTestOwner);
153 service_->GetOwner(base::Bind(&ConsumerManagementServiceTest::OnGetOwnerDone,
154 base::Unretained(this)));
156 EXPECT_EQ(kAttributeOwnerId, get_boot_attribute_request_.name());
157 EXPECT_EQ(kTestOwner, owner_);
160 TEST_F(ConsumerManagementServiceTest, GetOwnerReturnsAnEmptyStringWhenItFails) {
161 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_FAILURE;
162 cryptohome_result_ = false;
163 cryptohome_reply_.MutableExtension(cryptohome::GetBootAttributeReply::reply)->
164 set_value(kTestOwner);
166 service_->GetOwner(base::Bind(&ConsumerManagementServiceTest::OnGetOwnerDone,
167 base::Unretained(this)));
169 EXPECT_EQ("", owner_);
172 TEST_F(ConsumerManagementServiceTest, CanSetOwner) {
173 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_SUCCESS;
174 cryptohome_result_ = true;
176 service_->SetOwner(kTestOwner,
177 base::Bind(&ConsumerManagementServiceTest::OnSetOwnerDone,
178 base::Unretained(this)));
180 EXPECT_EQ(kAttributeOwnerId, set_boot_attribute_request_.name());
181 EXPECT_EQ(kTestOwner, set_boot_attribute_request_.value());
182 EXPECT_TRUE(set_owner_status_);
185 TEST_F(ConsumerManagementServiceTest, SetOwnerReturnsFalseWhenItFails) {
186 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_FAILURE;
187 cryptohome_result_ = false;
189 service_->SetOwner(kTestOwner,
190 base::Bind(&ConsumerManagementServiceTest::OnSetOwnerDone,
191 base::Unretained(this)));
193 EXPECT_FALSE(set_owner_status_);
196 class ConsumerManagementServiceStatusTest
197 : public chromeos::DeviceSettingsTestBase {
198 public:
199 ConsumerManagementServiceStatusTest()
200 : testing_local_state_(TestingBrowserProcess::GetGlobal()),
201 service_(NULL, &device_settings_service_) {
204 void SetStageInLocalState(ConsumerManagementStage stage) {
205 testing_local_state_.Get()->SetInteger(
206 prefs::kConsumerManagementStage, stage.ToInternalValue());
209 void SetManagementMode(em::PolicyData::ManagementMode mode) {
210 device_policy_.policy_data().set_management_mode(mode);
211 device_policy_.Build();
212 device_settings_test_helper_.set_policy_blob(device_policy_.GetBlob());
213 ReloadDeviceSettings();
216 ScopedTestingLocalState testing_local_state_;
217 ConsumerManagementService service_;
220 TEST_F(ConsumerManagementServiceStatusTest,
221 GetStatusAndGetStatusStringNotificationWork) {
222 EXPECT_EQ(ConsumerManagementService::STATUS_UNKNOWN, service_.GetStatus());
223 EXPECT_EQ("StatusUnknown", service_.GetStatusString());
225 SetManagementMode(em::PolicyData::LOCAL_OWNER);
226 SetStageInLocalState(ConsumerManagementStage::None());
228 EXPECT_EQ(ConsumerManagementService::STATUS_UNENROLLED, service_.GetStatus());
229 EXPECT_EQ("StatusUnenrolled", service_.GetStatusString());
231 SetStageInLocalState(ConsumerManagementStage::EnrollmentRequested());
233 EXPECT_EQ(ConsumerManagementService::STATUS_ENROLLING, service_.GetStatus());
234 EXPECT_EQ("StatusEnrolling", service_.GetStatusString());
236 SetManagementMode(em::PolicyData::CONSUMER_MANAGED);
237 SetStageInLocalState(ConsumerManagementStage::EnrollmentSuccess());
239 EXPECT_EQ(ConsumerManagementService::STATUS_ENROLLED, service_.GetStatus());
240 EXPECT_EQ("StatusEnrolled", service_.GetStatusString());
242 SetStageInLocalState(ConsumerManagementStage::UnenrollmentRequested());
243 EXPECT_EQ(ConsumerManagementService::STATUS_UNENROLLING,
244 service_.GetStatus());
245 EXPECT_EQ("StatusUnenrolling", service_.GetStatusString());
247 SetManagementMode(em::PolicyData::LOCAL_OWNER);
248 SetStageInLocalState(ConsumerManagementStage::UnenrollmentSuccess());
249 EXPECT_EQ(ConsumerManagementService::STATUS_UNENROLLED, service_.GetStatus());
250 EXPECT_EQ("StatusUnenrolled", service_.GetStatusString());
253 } // namespace policy