| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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/settings/signed_settings.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/message_loop.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | |
| 11 #include "chrome/browser/chromeos/settings/cros_settings_names.h" | |
| 12 #include "chrome/browser/chromeos/settings/mock_owner_key_utils.h" | |
| 13 #include "chrome/browser/chromeos/settings/mock_ownership_service.h" | |
| 14 #include "chrome/browser/chromeos/settings/owner_manager_unittest.h" | |
| 15 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" | |
| 16 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 17 #include "chromeos/dbus/mock_dbus_thread_manager.h" | |
| 18 #include "chromeos/dbus/mock_session_manager_client.h" | |
| 19 #include "content/public/test/test_browser_thread.h" | |
| 20 #include "testing/gmock/include/gmock/gmock.h" | |
| 21 #include "testing/gtest/include/gtest/gtest.h" | |
| 22 | |
| 23 using ::testing::A; | |
| 24 using ::testing::AnyNumber; | |
| 25 using ::testing::Return; | |
| 26 using ::testing::ReturnRef; | |
| 27 using ::testing::SaveArg; | |
| 28 using ::testing::StrEq; | |
| 29 using ::testing::WithArg; | |
| 30 using ::testing::_; | |
| 31 using content::BrowserThread; | |
| 32 using google::protobuf::RepeatedPtrField; | |
| 33 | |
| 34 namespace em = enterprise_management; | |
| 35 namespace chromeos { | |
| 36 | |
| 37 namespace { | |
| 38 template <class T> | |
| 39 class DummyDelegate : public SignedSettings::Delegate<T> { | |
| 40 public: | |
| 41 explicit DummyDelegate(T to_expect) | |
| 42 : expect_success_(false), | |
| 43 expected_failure_(SignedSettings::SUCCESS), | |
| 44 expected_(to_expect), | |
| 45 run_(false) {} | |
| 46 virtual ~DummyDelegate() { EXPECT_TRUE(run_); } | |
| 47 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, | |
| 48 T value) { | |
| 49 run_ = true; | |
| 50 if (expect_success_) | |
| 51 compare_expected(value); | |
| 52 EXPECT_EQ(expected_failure_, code); | |
| 53 } | |
| 54 virtual void expect_success() { | |
| 55 expect_success_ = true; | |
| 56 expected_failure_ = SignedSettings::SUCCESS; | |
| 57 } | |
| 58 virtual void expect_failure(SignedSettings::ReturnCode code) { | |
| 59 expect_success_ = false; | |
| 60 expected_failure_ = code; | |
| 61 } | |
| 62 | |
| 63 protected: | |
| 64 bool expect_success_; | |
| 65 SignedSettings::ReturnCode expected_failure_; | |
| 66 T expected_; | |
| 67 bool run_; | |
| 68 virtual void compare_expected(T to_compare) = 0; | |
| 69 }; | |
| 70 | |
| 71 template <class T> | |
| 72 class NormalDelegate : public DummyDelegate<T> { | |
| 73 public: | |
| 74 explicit NormalDelegate(T to_expect) : DummyDelegate<T>(to_expect) {} | |
| 75 virtual ~NormalDelegate() {} | |
| 76 protected: | |
| 77 virtual void compare_expected(T to_compare) { | |
| 78 // without this-> this won't build. | |
| 79 EXPECT_EQ(this->expected_, to_compare); | |
| 80 } | |
| 81 }; | |
| 82 | |
| 83 class ProtoDelegate : public DummyDelegate<const em::PolicyFetchResponse&> { | |
| 84 public: | |
| 85 explicit ProtoDelegate(const em::PolicyFetchResponse& e) | |
| 86 : DummyDelegate<const em::PolicyFetchResponse&>(e) { | |
| 87 } | |
| 88 virtual ~ProtoDelegate() {} | |
| 89 protected: | |
| 90 virtual void compare_expected(const em::PolicyFetchResponse& to_compare) { | |
| 91 std::string ex_string, comp_string; | |
| 92 EXPECT_TRUE(expected_.SerializeToString(&ex_string)); | |
| 93 EXPECT_TRUE(to_compare.SerializeToString(&comp_string)); | |
| 94 EXPECT_EQ(ex_string, comp_string); | |
| 95 } | |
| 96 }; | |
| 97 | |
| 98 } // anonymous namespace | |
| 99 | |
| 100 class SignedSettingsTest : public testing::Test { | |
| 101 public: | |
| 102 SignedSettingsTest() | |
| 103 : fake_prop_(kAccountsPrefAllowGuest), | |
| 104 fake_signature_("false"), | |
| 105 fake_value_(false), | |
| 106 fake_value_signature_( | |
| 107 fake_signature_.c_str(), | |
| 108 fake_signature_.c_str() + fake_signature_.length()), | |
| 109 message_loop_(MessageLoop::TYPE_UI), | |
| 110 ui_thread_(BrowserThread::UI, &message_loop_), | |
| 111 file_thread_(BrowserThread::FILE), | |
| 112 mock_(new MockKeyUtils), | |
| 113 injector_(mock_) /* injector_ takes ownership of mock_ */, | |
| 114 mock_dbus_thread_manager_(new MockDBusThreadManager) { | |
| 115 } | |
| 116 | |
| 117 virtual ~SignedSettingsTest() {} | |
| 118 | |
| 119 virtual void SetUp() { | |
| 120 file_thread_.Start(); | |
| 121 EXPECT_CALL(*mock_dbus_thread_manager_, GetSystemBus()) | |
| 122 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL))); | |
| 123 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_); | |
| 124 } | |
| 125 | |
| 126 virtual void TearDown() { | |
| 127 OwnerKeyUtils::set_factory(NULL); | |
| 128 DBusThreadManager::Shutdown(); | |
| 129 } | |
| 130 | |
| 131 void mock_service(SignedSettings* s, MockOwnershipService* m) { | |
| 132 s->set_service(m); | |
| 133 } | |
| 134 | |
| 135 void FailingStorePolicyOp(const OwnerManager::KeyOpCode return_code) { | |
| 136 NormalDelegate<bool> d(false); | |
| 137 d.expect_failure(SignedSettings::MapKeyOpCode(return_code)); | |
| 138 | |
| 139 em::PolicyFetchResponse fake_policy; | |
| 140 fake_policy.set_policy_data(fake_prop_); | |
| 141 std::string serialized; | |
| 142 ASSERT_TRUE(fake_policy.SerializeToString(&serialized)); | |
| 143 | |
| 144 scoped_refptr<SignedSettings> s( | |
| 145 SignedSettings::CreateStorePolicyOp(&fake_policy, &d)); | |
| 146 | |
| 147 mock_service(s.get(), &m_); | |
| 148 EXPECT_CALL(m_, StartSigningAttempt(StrEq(fake_prop_), _)) | |
| 149 .Times(1); | |
| 150 | |
| 151 s->Execute(); | |
| 152 s->OnKeyOpComplete(return_code, std::vector<uint8>()); | |
| 153 message_loop_.RunAllPending(); | |
| 154 } | |
| 155 | |
| 156 em::PolicyData BuildPolicyData(std::vector<std::string> whitelist) { | |
| 157 em::PolicyData to_return; | |
| 158 em::ChromeDeviceSettingsProto pol; | |
| 159 em::GuestModeEnabledProto* allow = pol.mutable_guest_mode_enabled(); | |
| 160 allow->set_guest_mode_enabled(false); | |
| 161 pol.mutable_device_proxy_settings()->set_proxy_mode("direct"); | |
| 162 | |
| 163 if (!whitelist.empty()) { | |
| 164 em::UserWhitelistProto* whitelist_proto = pol.mutable_user_whitelist(); | |
| 165 for (std::vector<std::string>::const_iterator it = whitelist.begin(); | |
| 166 it != whitelist.end(); | |
| 167 ++it) { | |
| 168 whitelist_proto->add_user_whitelist(*it); | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 to_return.set_policy_type(chromeos::kDevicePolicyType); | |
| 173 to_return.set_policy_value(pol.SerializeAsString()); | |
| 174 return to_return; | |
| 175 } | |
| 176 | |
| 177 em::PolicyFetchResponse BuildProto(const std::string& data, | |
| 178 const std::string& sig, | |
| 179 std::string* out_serialized) { | |
| 180 em::PolicyFetchResponse fake_policy; | |
| 181 if (!data.empty()) | |
| 182 fake_policy.set_policy_data(data); | |
| 183 if (!sig.empty()) | |
| 184 fake_policy.set_policy_data_signature(sig); | |
| 185 EXPECT_TRUE(fake_policy.SerializeToString(out_serialized)); | |
| 186 return fake_policy; | |
| 187 } | |
| 188 | |
| 189 const std::string fake_prop_; | |
| 190 const std::string fake_signature_; | |
| 191 const base::FundamentalValue fake_value_; | |
| 192 const std::vector<uint8> fake_value_signature_; | |
| 193 MockOwnershipService m_; | |
| 194 | |
| 195 MessageLoop message_loop_; | |
| 196 content::TestBrowserThread ui_thread_; | |
| 197 content::TestBrowserThread file_thread_; | |
| 198 | |
| 199 MockKeyUtils* mock_; | |
| 200 MockInjector injector_; | |
| 201 MockDBusThreadManager* mock_dbus_thread_manager_; | |
| 202 | |
| 203 ScopedStubCrosEnabler stub_cros_enabler_; | |
| 204 }; | |
| 205 | |
| 206 ACTION_P(Retrieve, policy_blob) { arg0.Run(policy_blob); } | |
| 207 ACTION_P(Store, success) { arg1.Run(success); } | |
| 208 | |
| 209 TEST_F(SignedSettingsTest, SignAndStorePolicy) { | |
| 210 NormalDelegate<bool> d(true); | |
| 211 d.expect_success(); | |
| 212 | |
| 213 em::PolicyData in_pol = BuildPolicyData(std::vector<std::string>()); | |
| 214 std::string data_serialized = in_pol.SerializeAsString(); | |
| 215 std::string serialized; | |
| 216 em::PolicyFetchResponse fake_policy = BuildProto(data_serialized, | |
| 217 std::string(), | |
| 218 &serialized); | |
| 219 scoped_refptr<SignedSettings> s( | |
| 220 SignedSettings::CreateStorePolicyOp(&fake_policy, &d)); | |
| 221 | |
| 222 mock_service(s.get(), &m_); | |
| 223 EXPECT_CALL(m_, StartSigningAttempt(StrEq(data_serialized), _)) | |
| 224 .Times(1); | |
| 225 em::PolicyData out_pol; | |
| 226 | |
| 227 // Ask for signature over unsigned policy. | |
| 228 s->Execute(); | |
| 229 message_loop_.RunAllPending(); | |
| 230 | |
| 231 // Fake out a successful signing. | |
| 232 std::string signed_serialized; | |
| 233 em::PolicyFetchResponse signed_policy = BuildProto(data_serialized, | |
| 234 fake_signature_, | |
| 235 &signed_serialized); | |
| 236 MockSessionManagerClient* client = | |
| 237 mock_dbus_thread_manager_->mock_session_manager_client(); | |
| 238 EXPECT_CALL(*client, StoreDevicePolicy(signed_serialized, _)) | |
| 239 .WillOnce(Store(true)) | |
| 240 .RetiresOnSaturation(); | |
| 241 s->OnKeyOpComplete(OwnerManager::SUCCESS, fake_value_signature_); | |
| 242 message_loop_.RunAllPending(); | |
| 243 } | |
| 244 | |
| 245 TEST_F(SignedSettingsTest, StoreSignedPolicy) { | |
| 246 NormalDelegate<bool> d(true); | |
| 247 d.expect_success(); | |
| 248 | |
| 249 em::PolicyData in_pol = BuildPolicyData(std::vector<std::string>()); | |
| 250 std::string serialized = in_pol.SerializeAsString(); | |
| 251 std::string signed_serialized; | |
| 252 em::PolicyFetchResponse signed_policy = BuildProto(serialized, | |
| 253 fake_signature_, | |
| 254 &signed_serialized); | |
| 255 scoped_refptr<SignedSettings> s( | |
| 256 SignedSettings::CreateStorePolicyOp(&signed_policy, &d)); | |
| 257 MockSessionManagerClient* client = | |
| 258 mock_dbus_thread_manager_->mock_session_manager_client(); | |
| 259 EXPECT_CALL(*client, StoreDevicePolicy(signed_serialized, _)) | |
| 260 .WillOnce(Store(true)) | |
| 261 .RetiresOnSaturation(); | |
| 262 | |
| 263 mock_service(s.get(), &m_); | |
| 264 em::PolicyData out_pol; | |
| 265 | |
| 266 s->Execute(); | |
| 267 message_loop_.RunAllPending(); | |
| 268 } | |
| 269 | |
| 270 TEST_F(SignedSettingsTest, StorePolicyNoKey) { | |
| 271 FailingStorePolicyOp(OwnerManager::KEY_UNAVAILABLE); | |
| 272 } | |
| 273 | |
| 274 TEST_F(SignedSettingsTest, StorePolicyFailed) { | |
| 275 FailingStorePolicyOp(OwnerManager::OPERATION_FAILED); | |
| 276 } | |
| 277 | |
| 278 TEST_F(SignedSettingsTest, StorePolicyNoPolicyData) { | |
| 279 NormalDelegate<bool> d(false); | |
| 280 d.expect_failure(SignedSettings::OPERATION_FAILED); | |
| 281 | |
| 282 std::string serialized; | |
| 283 em::PolicyFetchResponse fake_policy = BuildProto(std::string(), | |
| 284 std::string(), | |
| 285 &serialized); | |
| 286 scoped_refptr<SignedSettings> s( | |
| 287 SignedSettings::CreateStorePolicyOp(&fake_policy, &d)); | |
| 288 | |
| 289 s->Execute(); | |
| 290 message_loop_.RunAllPending(); | |
| 291 } | |
| 292 | |
| 293 TEST_F(SignedSettingsTest, RetrievePolicy) { | |
| 294 em::PolicyData in_pol = BuildPolicyData(std::vector<std::string>()); | |
| 295 std::string serialized = in_pol.SerializeAsString(); | |
| 296 std::string signed_serialized; | |
| 297 em::PolicyFetchResponse signed_policy = BuildProto(serialized, | |
| 298 fake_signature_, | |
| 299 &signed_serialized); | |
| 300 ProtoDelegate d(signed_policy); | |
| 301 d.expect_success(); | |
| 302 scoped_refptr<SignedSettings> s(SignedSettings::CreateRetrievePolicyOp(&d)); | |
| 303 | |
| 304 MockSessionManagerClient* client = | |
| 305 mock_dbus_thread_manager_->mock_session_manager_client(); | |
| 306 EXPECT_CALL(*client, RetrieveDevicePolicy(_)) | |
| 307 .WillOnce(Retrieve(signed_serialized)) | |
| 308 .RetiresOnSaturation(); | |
| 309 | |
| 310 mock_service(s.get(), &m_); | |
| 311 EXPECT_CALL(m_, StartVerifyAttempt(serialized, fake_value_signature_, _)) | |
| 312 .Times(1); | |
| 313 em::PolicyData out_pol; | |
| 314 | |
| 315 s->Execute(); | |
| 316 message_loop_.RunAllPending(); | |
| 317 | |
| 318 s->OnKeyOpComplete(OwnerManager::SUCCESS, std::vector<uint8>()); | |
| 319 message_loop_.RunAllPending(); | |
| 320 } | |
| 321 | |
| 322 TEST_F(SignedSettingsTest, RetrieveNullPolicy) { | |
| 323 em::PolicyFetchResponse policy; | |
| 324 ProtoDelegate d(policy); | |
| 325 d.expect_failure(SignedSettings::NOT_FOUND); | |
| 326 scoped_refptr<SignedSettings> s(SignedSettings::CreateRetrievePolicyOp(&d)); | |
| 327 | |
| 328 MockSessionManagerClient* client = | |
| 329 mock_dbus_thread_manager_->mock_session_manager_client(); | |
| 330 EXPECT_CALL(*client, RetrieveDevicePolicy(_)) | |
| 331 .WillOnce(Retrieve("")) | |
| 332 .RetiresOnSaturation(); | |
| 333 | |
| 334 s->Execute(); | |
| 335 message_loop_.RunAllPending(); | |
| 336 } | |
| 337 | |
| 338 TEST_F(SignedSettingsTest, RetrieveEmptyPolicy) { | |
| 339 std::string serialized; | |
| 340 em::PolicyFetchResponse policy = BuildProto("", "", &serialized); | |
| 341 ProtoDelegate d(policy); | |
| 342 d.expect_failure(SignedSettings::NOT_FOUND); | |
| 343 scoped_refptr<SignedSettings> s(SignedSettings::CreateRetrievePolicyOp(&d)); | |
| 344 | |
| 345 MockSessionManagerClient* client = | |
| 346 mock_dbus_thread_manager_->mock_session_manager_client(); | |
| 347 EXPECT_CALL(*client, RetrieveDevicePolicy(_)) | |
| 348 .WillOnce(Retrieve("")) | |
| 349 .RetiresOnSaturation(); | |
| 350 | |
| 351 s->Execute(); | |
| 352 message_loop_.RunAllPending(); | |
| 353 } | |
| 354 | |
| 355 TEST_F(SignedSettingsTest, RetrieveUnsignedPolicy) { | |
| 356 std::string serialized; | |
| 357 em::PolicyFetchResponse policy = BuildProto(fake_prop_, | |
| 358 std::string(), | |
| 359 &serialized); | |
| 360 ProtoDelegate d(policy); | |
| 361 d.expect_failure(SignedSettings::BAD_SIGNATURE); | |
| 362 scoped_refptr<SignedSettings> s(SignedSettings::CreateRetrievePolicyOp(&d)); | |
| 363 | |
| 364 MockSessionManagerClient* client = | |
| 365 mock_dbus_thread_manager_->mock_session_manager_client(); | |
| 366 EXPECT_CALL(*client, RetrieveDevicePolicy(_)) | |
| 367 .WillOnce(Retrieve(serialized)) | |
| 368 .RetiresOnSaturation(); | |
| 369 | |
| 370 s->Execute(); | |
| 371 message_loop_.RunAllPending(); | |
| 372 } | |
| 373 | |
| 374 TEST_F(SignedSettingsTest, RetrieveMalsignedPolicy) { | |
| 375 std::string signed_serialized; | |
| 376 em::PolicyFetchResponse signed_policy = BuildProto(fake_prop_, | |
| 377 fake_signature_, | |
| 378 &signed_serialized); | |
| 379 ProtoDelegate d(signed_policy); | |
| 380 d.expect_failure(SignedSettings::BAD_SIGNATURE); | |
| 381 scoped_refptr<SignedSettings> s(SignedSettings::CreateRetrievePolicyOp(&d)); | |
| 382 | |
| 383 MockSessionManagerClient* client = | |
| 384 mock_dbus_thread_manager_->mock_session_manager_client(); | |
| 385 EXPECT_CALL(*client, RetrieveDevicePolicy(_)) | |
| 386 .WillOnce(Retrieve(signed_serialized)) | |
| 387 .RetiresOnSaturation(); | |
| 388 | |
| 389 mock_service(s.get(), &m_); | |
| 390 EXPECT_CALL(m_, StartVerifyAttempt(fake_prop_, fake_value_signature_, _)) | |
| 391 .Times(1); | |
| 392 | |
| 393 s->Execute(); | |
| 394 message_loop_.RunAllPending(); | |
| 395 | |
| 396 s->OnKeyOpComplete(OwnerManager::OPERATION_FAILED, std::vector<uint8>()); | |
| 397 message_loop_.RunAllPending(); | |
| 398 } | |
| 399 | |
| 400 } // namespace chromeos | |
| OLD | NEW |