| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/policy/consumer_management_service.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/callback.h" | |
| 10 #include "chrome/browser/browser_process.h" | |
| 11 #include "chrome/browser/chromeos/policy/device_policy_builder.h" | |
| 12 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" | |
| 13 #include "chrome/common/pref_names.h" | |
| 14 #include "chrome/test/base/browser_with_test_window_test.h" | |
| 15 #include "chrome/test/base/scoped_testing_local_state.h" | |
| 16 #include "chrome/test/base/testing_browser_process.h" | |
| 17 #include "chrome/test/base/testing_profile_manager.h" | |
| 18 #include "chromeos/dbus/cryptohome/rpc.pb.h" | |
| 19 #include "chromeos/dbus/cryptohome_client.h" | |
| 20 #include "chromeos/dbus/mock_cryptohome_client.h" | |
| 21 #include "components/prefs/pref_registry_simple.h" | |
| 22 #include "components/prefs/pref_service.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" | |
| 26 | |
| 27 using testing::Invoke; | |
| 28 using testing::NiceMock; | |
| 29 using testing::_; | |
| 30 | |
| 31 namespace em = enterprise_management; | |
| 32 | |
| 33 namespace { | |
| 34 const char kAttributeOwnerId[] = "consumer_management.owner_id"; | |
| 35 const char kTestOwner[] = "test@chromium.org.test"; | |
| 36 } | |
| 37 | |
| 38 namespace policy { | |
| 39 | |
| 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)); | |
| 56 } | |
| 57 | |
| 58 void SetUp() override { | |
| 59 BrowserWithTestWindowTest::SetUp(); | |
| 60 | |
| 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)); | |
| 66 } | |
| 67 | |
| 68 void TearDown() override { | |
| 69 testing_profile_manager_.reset(); | |
| 70 service_.reset(); | |
| 71 | |
| 72 BrowserWithTestWindowTest::TearDown(); | |
| 73 } | |
| 74 | |
| 75 ConsumerManagementStage GetStageFromLocalState() { | |
| 76 return ConsumerManagementStage::FromInternalValue( | |
| 77 g_browser_process->local_state()->GetInteger( | |
| 78 prefs::kConsumerManagementStage)); | |
| 79 } | |
| 80 | |
| 81 void SetStageInLocalState(ConsumerManagementStage stage) { | |
| 82 g_browser_process->local_state()->SetInteger( | |
| 83 prefs::kConsumerManagementStage, stage.ToInternalValue()); | |
| 84 } | |
| 85 | |
| 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_); | |
| 91 } | |
| 92 | |
| 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_); | |
| 98 } | |
| 99 | |
| 100 void MockFlushAndSignBootAttributes( | |
| 101 const cryptohome::FlushAndSignBootAttributesRequest& request, | |
| 102 const chromeos::CryptohomeClient::ProtobufMethodCallback& callback) { | |
| 103 callback.Run(cryptohome_status_, cryptohome_result_, cryptohome_reply_); | |
| 104 } | |
| 105 | |
| 106 void OnGetOwnerDone(const std::string& owner) { | |
| 107 owner_ = owner; | |
| 108 } | |
| 109 | |
| 110 void OnSetOwnerDone(bool status) { | |
| 111 set_owner_status_ = status; | |
| 112 } | |
| 113 | |
| 114 NiceMock<chromeos::MockCryptohomeClient> mock_cryptohome_client_; | |
| 115 std::unique_ptr<ConsumerManagementService> service_; | |
| 116 std::unique_ptr<TestingProfileManager> testing_profile_manager_; | |
| 117 | |
| 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_; | |
| 127 }; | |
| 128 | |
| 129 TEST_F(ConsumerManagementServiceTest, CanGetStage) { | |
| 130 EXPECT_EQ(ConsumerManagementStage::None(), service_->GetStage()); | |
| 131 | |
| 132 SetStageInLocalState(ConsumerManagementStage::EnrollmentRequested()); | |
| 133 | |
| 134 EXPECT_EQ(ConsumerManagementStage::EnrollmentRequested(), | |
| 135 service_->GetStage()); | |
| 136 } | |
| 137 | |
| 138 TEST_F(ConsumerManagementServiceTest, CanSetStage) { | |
| 139 EXPECT_EQ(ConsumerManagementStage::None(), GetStageFromLocalState()); | |
| 140 | |
| 141 service_->SetStage(ConsumerManagementStage::EnrollmentRequested()); | |
| 142 | |
| 143 EXPECT_EQ(ConsumerManagementStage::EnrollmentRequested(), | |
| 144 GetStageFromLocalState()); | |
| 145 } | |
| 146 | |
| 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); | |
| 152 | |
| 153 service_->GetOwner(base::Bind(&ConsumerManagementServiceTest::OnGetOwnerDone, | |
| 154 base::Unretained(this))); | |
| 155 | |
| 156 EXPECT_EQ(kAttributeOwnerId, get_boot_attribute_request_.name()); | |
| 157 EXPECT_EQ(kTestOwner, owner_); | |
| 158 } | |
| 159 | |
| 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); | |
| 165 | |
| 166 service_->GetOwner(base::Bind(&ConsumerManagementServiceTest::OnGetOwnerDone, | |
| 167 base::Unretained(this))); | |
| 168 | |
| 169 EXPECT_EQ("", owner_); | |
| 170 } | |
| 171 | |
| 172 TEST_F(ConsumerManagementServiceTest, CanSetOwner) { | |
| 173 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_SUCCESS; | |
| 174 cryptohome_result_ = true; | |
| 175 | |
| 176 service_->SetOwner(kTestOwner, | |
| 177 base::Bind(&ConsumerManagementServiceTest::OnSetOwnerDone, | |
| 178 base::Unretained(this))); | |
| 179 | |
| 180 EXPECT_EQ(kAttributeOwnerId, set_boot_attribute_request_.name()); | |
| 181 EXPECT_EQ(kTestOwner, set_boot_attribute_request_.value()); | |
| 182 EXPECT_TRUE(set_owner_status_); | |
| 183 } | |
| 184 | |
| 185 TEST_F(ConsumerManagementServiceTest, SetOwnerReturnsFalseWhenItFails) { | |
| 186 cryptohome_status_ = chromeos::DBUS_METHOD_CALL_FAILURE; | |
| 187 cryptohome_result_ = false; | |
| 188 | |
| 189 service_->SetOwner(kTestOwner, | |
| 190 base::Bind(&ConsumerManagementServiceTest::OnSetOwnerDone, | |
| 191 base::Unretained(this))); | |
| 192 | |
| 193 EXPECT_FALSE(set_owner_status_); | |
| 194 } | |
| 195 | |
| 196 class ConsumerManagementServiceStatusTest | |
| 197 : public chromeos::DeviceSettingsTestBase { | |
| 198 public: | |
| 199 ConsumerManagementServiceStatusTest() | |
| 200 : testing_local_state_(TestingBrowserProcess::GetGlobal()), | |
| 201 service_(NULL, &device_settings_service_) { | |
| 202 } | |
| 203 | |
| 204 void SetStageInLocalState(ConsumerManagementStage stage) { | |
| 205 testing_local_state_.Get()->SetInteger( | |
| 206 prefs::kConsumerManagementStage, stage.ToInternalValue()); | |
| 207 } | |
| 208 | |
| 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(); | |
| 214 } | |
| 215 | |
| 216 ScopedTestingLocalState testing_local_state_; | |
| 217 ConsumerManagementService service_; | |
| 218 }; | |
| 219 | |
| 220 TEST_F(ConsumerManagementServiceStatusTest, | |
| 221 GetStatusAndGetStatusStringNotificationWork) { | |
| 222 EXPECT_EQ(ConsumerManagementService::STATUS_UNKNOWN, service_.GetStatus()); | |
| 223 EXPECT_EQ("StatusUnknown", service_.GetStatusString()); | |
| 224 | |
| 225 SetManagementMode(em::PolicyData::LOCAL_OWNER); | |
| 226 SetStageInLocalState(ConsumerManagementStage::None()); | |
| 227 | |
| 228 EXPECT_EQ(ConsumerManagementService::STATUS_UNENROLLED, service_.GetStatus()); | |
| 229 EXPECT_EQ("StatusUnenrolled", service_.GetStatusString()); | |
| 230 | |
| 231 SetStageInLocalState(ConsumerManagementStage::EnrollmentRequested()); | |
| 232 | |
| 233 EXPECT_EQ(ConsumerManagementService::STATUS_ENROLLING, service_.GetStatus()); | |
| 234 EXPECT_EQ("StatusEnrolling", service_.GetStatusString()); | |
| 235 | |
| 236 SetManagementMode(em::PolicyData::CONSUMER_MANAGED); | |
| 237 SetStageInLocalState(ConsumerManagementStage::EnrollmentSuccess()); | |
| 238 | |
| 239 EXPECT_EQ(ConsumerManagementService::STATUS_ENROLLED, service_.GetStatus()); | |
| 240 EXPECT_EQ("StatusEnrolled", service_.GetStatusString()); | |
| 241 | |
| 242 SetStageInLocalState(ConsumerManagementStage::UnenrollmentRequested()); | |
| 243 EXPECT_EQ(ConsumerManagementService::STATUS_UNENROLLING, | |
| 244 service_.GetStatus()); | |
| 245 EXPECT_EQ("StatusUnenrolling", service_.GetStatusString()); | |
| 246 | |
| 247 SetManagementMode(em::PolicyData::LOCAL_OWNER); | |
| 248 SetStageInLocalState(ConsumerManagementStage::UnenrollmentSuccess()); | |
| 249 EXPECT_EQ(ConsumerManagementService::STATUS_UNENROLLED, service_.GetStatus()); | |
| 250 EXPECT_EQ("StatusUnenrolled", service_.GetStatusString()); | |
| 251 } | |
| 252 | |
| 253 } // namespace policy | |
| OLD | NEW |