| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/login/signed_settings.h" | 5 #include "chrome/browser/chromeos/login/signed_settings.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | |
| 8 #include "base/logging.h" | 7 #include "base/logging.h" |
| 9 #include "base/scoped_temp_dir.h" | |
| 10 #include "base/stringprintf.h" | |
| 11 #include "chrome/browser/chromeos/cros/cros_library.h" | 8 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 12 #include "chrome/browser/chromeos/cros/mock_library_loader.h" | 9 #include "chrome/browser/chromeos/cros/mock_library_loader.h" |
| 13 #include "chrome/browser/chromeos/cros_settings_names.h" | 10 #include "chrome/browser/chromeos/cros_settings_names.h" |
| 14 #include "chrome/browser/chromeos/dbus/mock_dbus_thread_manager.h" | 11 #include "chrome/browser/chromeos/dbus/mock_dbus_thread_manager.h" |
| 15 #include "chrome/browser/chromeos/dbus/mock_session_manager_client.h" | 12 #include "chrome/browser/chromeos/dbus/mock_session_manager_client.h" |
| 16 #include "chrome/browser/chromeos/login/mock_owner_key_utils.h" | 13 #include "chrome/browser/chromeos/login/mock_owner_key_utils.h" |
| 17 #include "chrome/browser/chromeos/login/mock_ownership_service.h" | 14 #include "chrome/browser/chromeos/login/mock_ownership_service.h" |
| 18 #include "chrome/browser/chromeos/login/owner_manager_unittest.h" | 15 #include "chrome/browser/chromeos/login/owner_manager_unittest.h" |
| 19 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" | 16 #include "chrome/browser/policy/proto/chrome_device_policy.pb.h" |
| 20 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 17 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
| 21 #include "content/test/test_browser_thread.h" | 18 #include "content/test/test_browser_thread.h" |
| 22 #include "crypto/rsa_private_key.h" | |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 21 |
| 26 using ::testing::A; | 22 using ::testing::A; |
| 27 using ::testing::AnyNumber; | 23 using ::testing::AnyNumber; |
| 28 using ::testing::Return; | 24 using ::testing::Return; |
| 29 using ::testing::ReturnRef; | 25 using ::testing::ReturnRef; |
| 30 using ::testing::SaveArg; | 26 using ::testing::SaveArg; |
| 31 using ::testing::StrEq; | 27 using ::testing::StrEq; |
| 32 using ::testing::WithArg; | 28 using ::testing::WithArg; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 public: | 72 public: |
| 77 explicit NormalDelegate(T to_expect) : DummyDelegate<T>(to_expect) {} | 73 explicit NormalDelegate(T to_expect) : DummyDelegate<T>(to_expect) {} |
| 78 virtual ~NormalDelegate() {} | 74 virtual ~NormalDelegate() {} |
| 79 protected: | 75 protected: |
| 80 virtual void compare_expected(T to_compare) { | 76 virtual void compare_expected(T to_compare) { |
| 81 // without this-> this won't build. | 77 // without this-> this won't build. |
| 82 EXPECT_EQ(this->expected_, to_compare); | 78 EXPECT_EQ(this->expected_, to_compare); |
| 83 } | 79 } |
| 84 }; | 80 }; |
| 85 | 81 |
| 86 // Specialized version for Value objects because these compare differently. | |
| 87 class PolicyDelegate : public DummyDelegate<const base::Value*> { | |
| 88 public: | |
| 89 explicit PolicyDelegate(const base::Value* to_expect) | |
| 90 : DummyDelegate<const base::Value*>(to_expect) {} | |
| 91 virtual ~PolicyDelegate() {} | |
| 92 protected: | |
| 93 virtual void compare_expected(const base::Value* to_compare) { | |
| 94 // without this-> this won't build. | |
| 95 EXPECT_TRUE(this->expected_->Equals(to_compare)); | |
| 96 } | |
| 97 }; | |
| 98 | |
| 99 class ProtoDelegate : public DummyDelegate<const em::PolicyFetchResponse&> { | 82 class ProtoDelegate : public DummyDelegate<const em::PolicyFetchResponse&> { |
| 100 public: | 83 public: |
| 101 explicit ProtoDelegate(const em::PolicyFetchResponse& e) | 84 explicit ProtoDelegate(const em::PolicyFetchResponse& e) |
| 102 : DummyDelegate<const em::PolicyFetchResponse&>(e) { | 85 : DummyDelegate<const em::PolicyFetchResponse&>(e) { |
| 103 } | 86 } |
| 104 virtual ~ProtoDelegate() {} | 87 virtual ~ProtoDelegate() {} |
| 105 protected: | 88 protected: |
| 106 virtual void compare_expected(const em::PolicyFetchResponse& to_compare) { | 89 virtual void compare_expected(const em::PolicyFetchResponse& to_compare) { |
| 107 std::string ex_string, comp_string; | 90 std::string ex_string, comp_string; |
| 108 EXPECT_TRUE(expected_.SerializeToString(&ex_string)); | 91 EXPECT_TRUE(expected_.SerializeToString(&ex_string)); |
| 109 EXPECT_TRUE(to_compare.SerializeToString(&comp_string)); | 92 EXPECT_TRUE(to_compare.SerializeToString(&comp_string)); |
| 110 EXPECT_EQ(ex_string, comp_string); | 93 EXPECT_EQ(ex_string, comp_string); |
| 111 } | 94 } |
| 112 }; | 95 }; |
| 113 | 96 |
| 114 } // anonymous namespace | 97 } // anonymous namespace |
| 115 | 98 |
| 116 class SignedSettingsTest : public testing::Test { | 99 class SignedSettingsTest : public testing::Test { |
| 117 public: | 100 public: |
| 118 SignedSettingsTest() | 101 SignedSettingsTest() |
| 119 : fake_email_("fakey@example.com"), | 102 : fake_prop_(kAccountsPrefAllowGuest), |
| 120 fake_domain_("*@example.com"), | |
| 121 fake_prop_(kAccountsPrefAllowGuest), | |
| 122 fake_signature_("false"), | 103 fake_signature_("false"), |
| 123 fake_value_(false), | 104 fake_value_(false), |
| 124 fake_value_signature_( | 105 fake_value_signature_( |
| 125 fake_signature_.c_str(), | 106 fake_signature_.c_str(), |
| 126 fake_signature_.c_str() + fake_signature_.length()), | 107 fake_signature_.c_str() + fake_signature_.length()), |
| 127 message_loop_(MessageLoop::TYPE_UI), | 108 message_loop_(MessageLoop::TYPE_UI), |
| 128 ui_thread_(BrowserThread::UI, &message_loop_), | 109 ui_thread_(BrowserThread::UI, &message_loop_), |
| 129 file_thread_(BrowserThread::FILE), | 110 file_thread_(BrowserThread::FILE), |
| 130 mock_(new MockKeyUtils), | 111 mock_(new MockKeyUtils), |
| 131 injector_(mock_) /* injector_ takes ownership of mock_ */, | 112 injector_(mock_) /* injector_ takes ownership of mock_ */, |
| 132 mock_dbus_thread_manager_(new MockDBusThreadManager) { | 113 mock_dbus_thread_manager_(new MockDBusThreadManager) { |
| 133 } | 114 } |
| 134 | 115 |
| 135 virtual ~SignedSettingsTest() {} | 116 virtual ~SignedSettingsTest() {} |
| 136 | 117 |
| 137 virtual void SetUp() { | 118 virtual void SetUp() { |
| 138 file_thread_.Start(); | 119 file_thread_.Start(); |
| 139 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_); | 120 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_); |
| 140 } | 121 } |
| 141 | 122 |
| 142 virtual void TearDown() { | 123 virtual void TearDown() { |
| 143 OwnerKeyUtils::set_factory(NULL); | 124 OwnerKeyUtils::set_factory(NULL); |
| 144 DBusThreadManager::Shutdown(); | 125 DBusThreadManager::Shutdown(); |
| 145 } | 126 } |
| 146 | 127 |
| 147 void mock_service(SignedSettings* s, MockOwnershipService* m) { | 128 void mock_service(SignedSettings* s, MockOwnershipService* m) { |
| 148 s->set_service(m); | 129 s->set_service(m); |
| 149 } | 130 } |
| 150 | 131 |
| 151 em::PolicyData BuildPolicyData(std::vector<std::string> whitelist) { | |
| 152 em::PolicyData to_return; | |
| 153 em::ChromeDeviceSettingsProto pol; | |
| 154 em::GuestModeEnabledProto* allow = pol.mutable_guest_mode_enabled(); | |
| 155 allow->set_guest_mode_enabled(false); | |
| 156 pol.mutable_device_proxy_settings()->set_proxy_mode("direct"); | |
| 157 | |
| 158 if (!whitelist.empty()) { | |
| 159 em::UserWhitelistProto* whitelist_proto = pol.mutable_user_whitelist(); | |
| 160 for (std::vector<std::string>::const_iterator it = whitelist.begin(); | |
| 161 it != whitelist.end(); | |
| 162 ++it) { | |
| 163 whitelist_proto->add_user_whitelist(*it); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 to_return.set_policy_type(SignedSettings::kDevicePolicyType); | |
| 168 to_return.set_policy_value(pol.SerializeAsString()); | |
| 169 return to_return; | |
| 170 } | |
| 171 | |
| 172 void SetAllowNewUsers(bool desired, em::PolicyData* poldata) { | |
| 173 em::ChromeDeviceSettingsProto pol; | |
| 174 pol.ParseFromString(poldata->policy_value()); | |
| 175 em::AllowNewUsersProto* allow = pol.mutable_allow_new_users(); | |
| 176 allow->set_allow_new_users(desired); | |
| 177 poldata->set_policy_value(pol.SerializeAsString()); | |
| 178 } | |
| 179 | |
| 180 void FailingStorePropertyOp(const OwnerManager::KeyOpCode return_code) { | |
| 181 NormalDelegate<bool> d(false); | |
| 182 scoped_refptr<SignedSettings> s( | |
| 183 SignedSettings::CreateStorePropertyOp(fake_prop_, fake_value_, &d)); | |
| 184 d.expect_failure(SignedSettings::MapKeyOpCode(return_code)); | |
| 185 | |
| 186 mock_service(s.get(), &m_); | |
| 187 EXPECT_CALL(m_, StartSigningAttempt(_, _)) | |
| 188 .Times(1); | |
| 189 EXPECT_CALL(m_, GetStatus(_)) | |
| 190 .WillOnce(Return(OwnershipService::OWNERSHIP_TAKEN)); | |
| 191 EXPECT_CALL(m_, has_cached_policy()) | |
| 192 .WillOnce(Return(true)); | |
| 193 em::PolicyData fake_pol; | |
| 194 EXPECT_CALL(m_, cached_policy()) | |
| 195 .WillOnce(ReturnRef(fake_pol)); | |
| 196 | |
| 197 s->Execute(); | |
| 198 s->OnKeyOpComplete(return_code, std::vector<uint8>()); | |
| 199 message_loop_.RunAllPending(); | |
| 200 } | |
| 201 | |
| 202 void FailingStorePolicyOp(const OwnerManager::KeyOpCode return_code) { | 132 void FailingStorePolicyOp(const OwnerManager::KeyOpCode return_code) { |
| 203 NormalDelegate<bool> d(false); | 133 NormalDelegate<bool> d(false); |
| 204 d.expect_failure(SignedSettings::MapKeyOpCode(return_code)); | 134 d.expect_failure(SignedSettings::MapKeyOpCode(return_code)); |
| 205 | 135 |
| 206 em::PolicyFetchResponse fake_policy; | 136 em::PolicyFetchResponse fake_policy; |
| 207 fake_policy.set_policy_data(fake_prop_); | 137 fake_policy.set_policy_data(fake_prop_); |
| 208 std::string serialized; | 138 std::string serialized; |
| 209 ASSERT_TRUE(fake_policy.SerializeToString(&serialized)); | 139 ASSERT_TRUE(fake_policy.SerializeToString(&serialized)); |
| 210 | 140 |
| 211 scoped_refptr<SignedSettings> s( | 141 scoped_refptr<SignedSettings> s( |
| 212 SignedSettings::CreateStorePolicyOp(&fake_policy, &d)); | 142 SignedSettings::CreateStorePolicyOp(&fake_policy, &d)); |
| 213 | 143 |
| 214 mock_service(s.get(), &m_); | 144 mock_service(s.get(), &m_); |
| 215 EXPECT_CALL(m_, StartSigningAttempt(StrEq(fake_prop_), _)) | 145 EXPECT_CALL(m_, StartSigningAttempt(StrEq(fake_prop_), _)) |
| 216 .Times(1); | 146 .Times(1); |
| 217 | 147 |
| 218 s->Execute(); | 148 s->Execute(); |
| 219 s->OnKeyOpComplete(return_code, std::vector<uint8>()); | 149 s->OnKeyOpComplete(return_code, std::vector<uint8>()); |
| 220 message_loop_.RunAllPending(); | 150 message_loop_.RunAllPending(); |
| 221 } | 151 } |
| 222 | 152 |
| 153 em::PolicyData BuildPolicyData(std::vector<std::string> whitelist) { |
| 154 em::PolicyData to_return; |
| 155 em::ChromeDeviceSettingsProto pol; |
| 156 em::GuestModeEnabledProto* allow = pol.mutable_guest_mode_enabled(); |
| 157 allow->set_guest_mode_enabled(false); |
| 158 pol.mutable_device_proxy_settings()->set_proxy_mode("direct"); |
| 159 |
| 160 if (!whitelist.empty()) { |
| 161 em::UserWhitelistProto* whitelist_proto = pol.mutable_user_whitelist(); |
| 162 for (std::vector<std::string>::const_iterator it = whitelist.begin(); |
| 163 it != whitelist.end(); |
| 164 ++it) { |
| 165 whitelist_proto->add_user_whitelist(*it); |
| 166 } |
| 167 } |
| 168 |
| 169 to_return.set_policy_type(chromeos::kDevicePolicyType); |
| 170 to_return.set_policy_value(pol.SerializeAsString()); |
| 171 return to_return; |
| 172 } |
| 173 |
| 223 em::PolicyFetchResponse BuildProto(const std::string& data, | 174 em::PolicyFetchResponse BuildProto(const std::string& data, |
| 224 const std::string& sig, | 175 const std::string& sig, |
| 225 std::string* out_serialized) { | 176 std::string* out_serialized) { |
| 226 em::PolicyFetchResponse fake_policy; | 177 em::PolicyFetchResponse fake_policy; |
| 227 if (!data.empty()) | 178 if (!data.empty()) |
| 228 fake_policy.set_policy_data(data); | 179 fake_policy.set_policy_data(data); |
| 229 if (!sig.empty()) | 180 if (!sig.empty()) |
| 230 fake_policy.set_policy_data_signature(sig); | 181 fake_policy.set_policy_data_signature(sig); |
| 231 EXPECT_TRUE(fake_policy.SerializeToString(out_serialized)); | 182 EXPECT_TRUE(fake_policy.SerializeToString(out_serialized)); |
| 232 return fake_policy; | 183 return fake_policy; |
| 233 } | 184 } |
| 234 | 185 |
| 235 void DoRetrieveProperty(const std::string& name, | |
| 236 const base::Value* value, | |
| 237 em::PolicyData* fake_pol) { | |
| 238 PolicyDelegate d(value); | |
| 239 d.expect_success(); | |
| 240 scoped_refptr<SignedSettings> s( | |
| 241 SignedSettings::CreateRetrievePropertyOp(name, &d)); | |
| 242 mock_service(s.get(), &m_); | |
| 243 EXPECT_CALL(m_, GetStatus(_)) | |
| 244 .WillOnce(Return(OwnershipService::OWNERSHIP_TAKEN)); | |
| 245 EXPECT_CALL(m_, has_cached_policy()) | |
| 246 .WillOnce(Return(true)); | |
| 247 | |
| 248 EXPECT_CALL(m_, cached_policy()) | |
| 249 .WillOnce(ReturnRef(*fake_pol)); | |
| 250 | |
| 251 s->Execute(); | |
| 252 message_loop_.RunAllPending(); | |
| 253 } | |
| 254 | |
| 255 const std::string fake_email_; | |
| 256 const std::string fake_domain_; | |
| 257 const std::string fake_prop_; | 186 const std::string fake_prop_; |
| 258 const std::string fake_signature_; | 187 const std::string fake_signature_; |
| 259 const base::FundamentalValue fake_value_; | 188 const base::FundamentalValue fake_value_; |
| 260 const std::vector<uint8> fake_value_signature_; | 189 const std::vector<uint8> fake_value_signature_; |
| 261 MockOwnershipService m_; | 190 MockOwnershipService m_; |
| 262 | 191 |
| 263 ScopedTempDir tmpdir_; | |
| 264 FilePath tmpfile_; | |
| 265 | |
| 266 MessageLoop message_loop_; | 192 MessageLoop message_loop_; |
| 267 content::TestBrowserThread ui_thread_; | 193 content::TestBrowserThread ui_thread_; |
| 268 content::TestBrowserThread file_thread_; | 194 content::TestBrowserThread file_thread_; |
| 269 | 195 |
| 270 std::vector<uint8> fake_public_key_; | |
| 271 scoped_ptr<crypto::RSAPrivateKey> fake_private_key_; | |
| 272 | |
| 273 MockKeyUtils* mock_; | 196 MockKeyUtils* mock_; |
| 274 MockInjector injector_; | 197 MockInjector injector_; |
| 275 MockDBusThreadManager* mock_dbus_thread_manager_; | 198 MockDBusThreadManager* mock_dbus_thread_manager_; |
| 276 | 199 |
| 277 ScopedStubCrosEnabler stub_cros_enabler_; | 200 ScopedStubCrosEnabler stub_cros_enabler_; |
| 278 }; | 201 }; |
| 279 | 202 |
| 280 ACTION_P(Retrieve, policy_blob) { arg0.Run(policy_blob); } | 203 ACTION_P(Retrieve, policy_blob) { arg0.Run(policy_blob); } |
| 281 ACTION_P(Store, success) { arg1.Run(success); } | 204 ACTION_P(Store, success) { arg1.Run(success); } |
| 282 ACTION_P(FinishKeyOp, s) { arg2->OnKeyOpComplete(OwnerManager::SUCCESS, s); } | |
| 283 | |
| 284 TEST_F(SignedSettingsTest, StoreProperty) { | |
| 285 NormalDelegate<bool> d(true); | |
| 286 d.expect_success(); | |
| 287 scoped_refptr<SignedSettings> s( | |
| 288 SignedSettings::CreateStorePropertyOp(fake_prop_, fake_value_, &d)); | |
| 289 | |
| 290 mock_service(s.get(), &m_); | |
| 291 EXPECT_CALL(m_, StartSigningAttempt(_, _)) | |
| 292 .Times(1); | |
| 293 EXPECT_CALL(m_, GetStatus(_)) | |
| 294 .WillOnce(Return(OwnershipService::OWNERSHIP_TAKEN)); | |
| 295 EXPECT_CALL(m_, has_cached_policy()) | |
| 296 .WillOnce(Return(true)); | |
| 297 em::PolicyData in_pol = | |
| 298 BuildPolicyData(std::vector<std::string>(1, fake_email_)); | |
| 299 EXPECT_CALL(m_, cached_policy()) | |
| 300 .WillOnce(ReturnRef(in_pol)); | |
| 301 em::PolicyData out_pol; | |
| 302 EXPECT_CALL(m_, set_cached_policy(A<const em::PolicyData&>())) | |
| 303 .WillOnce(SaveArg<0>(&out_pol)); | |
| 304 | |
| 305 MockSessionManagerClient* client = | |
| 306 mock_dbus_thread_manager_->mock_session_manager_client(); | |
| 307 EXPECT_CALL(*client, StorePolicy(_, _)) | |
| 308 .WillOnce(Store(true)) | |
| 309 .RetiresOnSaturation(); | |
| 310 | |
| 311 s->Execute(); | |
| 312 s->OnKeyOpComplete(OwnerManager::SUCCESS, std::vector<uint8>()); | |
| 313 message_loop_.RunAllPending(); | |
| 314 | |
| 315 ASSERT_TRUE(out_pol.has_policy_value()); | |
| 316 em::ChromeDeviceSettingsProto pol; | |
| 317 pol.ParseFromString(out_pol.policy_value()); | |
| 318 ASSERT_TRUE(pol.has_guest_mode_enabled()); | |
| 319 ASSERT_TRUE(pol.guest_mode_enabled().has_guest_mode_enabled()); | |
| 320 ASSERT_FALSE(pol.guest_mode_enabled().guest_mode_enabled()); | |
| 321 } | |
| 322 | |
| 323 TEST_F(SignedSettingsTest, StorePropertyNoKey) { | |
| 324 FailingStorePropertyOp(OwnerManager::KEY_UNAVAILABLE); | |
| 325 } | |
| 326 | |
| 327 TEST_F(SignedSettingsTest, StorePropertyFailed) { | |
| 328 FailingStorePropertyOp(OwnerManager::OPERATION_FAILED); | |
| 329 } | |
| 330 | |
| 331 TEST_F(SignedSettingsTest, RetrieveProperty) { | |
| 332 em::PolicyData fake_pol = BuildPolicyData(std::vector<std::string>()); | |
| 333 base::FundamentalValue fake_value(false); | |
| 334 DoRetrieveProperty(fake_prop_, &fake_value, &fake_pol); | |
| 335 } | |
| 336 | |
| 337 TEST_F(SignedSettingsTest, RetrieveOwnerProperty) { | |
| 338 em::PolicyData fake_pol = BuildPolicyData(std::vector<std::string>()); | |
| 339 fake_pol.set_username(fake_email_); | |
| 340 base::StringValue fake_value(fake_email_); | |
| 341 DoRetrieveProperty(kDeviceOwner, &fake_value, &fake_pol); | |
| 342 } | |
| 343 | |
| 344 TEST_F(SignedSettingsTest, ExplicitlyAllowNewUsers) { | |
| 345 em::PolicyData fake_pol = BuildPolicyData(std::vector<std::string>()); | |
| 346 SetAllowNewUsers(true, &fake_pol); | |
| 347 base::FundamentalValue fake_value(true); | |
| 348 DoRetrieveProperty(kAccountsPrefAllowNewUser, &fake_value, &fake_pol); | |
| 349 } | |
| 350 | |
| 351 TEST_F(SignedSettingsTest, ExplicitlyDisallowNewUsers) { | |
| 352 std::vector<std::string> whitelist(1, fake_email_ + "m"); | |
| 353 em::PolicyData fake_pol = BuildPolicyData(whitelist); | |
| 354 SetAllowNewUsers(false, &fake_pol); | |
| 355 base::FundamentalValue fake_value(false); | |
| 356 DoRetrieveProperty(kAccountsPrefAllowNewUser, &fake_value, &fake_pol); | |
| 357 } | |
| 358 | |
| 359 TEST_F(SignedSettingsTest, ImplicitlyDisallowNewUsers) { | |
| 360 std::vector<std::string> whitelist(1, fake_email_ + "m"); | |
| 361 em::PolicyData fake_pol = BuildPolicyData(whitelist); | |
| 362 base::FundamentalValue fake_value(false); | |
| 363 DoRetrieveProperty(kAccountsPrefAllowNewUser, &fake_value, &fake_pol); | |
| 364 } | |
| 365 | |
| 366 TEST_F(SignedSettingsTest, AccidentallyDisallowNewUsers) { | |
| 367 em::PolicyData fake_pol = BuildPolicyData(std::vector<std::string>()); | |
| 368 SetAllowNewUsers(false, &fake_pol); | |
| 369 base::FundamentalValue fake_value(true); | |
| 370 DoRetrieveProperty(kAccountsPrefAllowNewUser, &fake_value, &fake_pol); | |
| 371 } | |
| 372 | |
| 373 TEST_F(SignedSettingsTest, RetrievePropertyNotFound) { | |
| 374 PolicyDelegate d(&fake_value_); | |
| 375 d.expect_failure(SignedSettings::NOT_FOUND); | |
| 376 scoped_refptr<SignedSettings> s( | |
| 377 SignedSettings::CreateRetrievePropertyOp("unknown_prop", &d)); | |
| 378 mock_service(s.get(), &m_); | |
| 379 EXPECT_CALL(m_, GetStatus(_)) | |
| 380 .WillOnce(Return(OwnershipService::OWNERSHIP_TAKEN)); | |
| 381 EXPECT_CALL(m_, has_cached_policy()) | |
| 382 .WillOnce(Return(true)); | |
| 383 | |
| 384 em::PolicyData fake_pol = BuildPolicyData(std::vector<std::string>()); | |
| 385 EXPECT_CALL(m_, cached_policy()) | |
| 386 .WillOnce(ReturnRef(fake_pol)); | |
| 387 | |
| 388 s->Execute(); | |
| 389 message_loop_.RunAllPending(); | |
| 390 } | |
| 391 | |
| 392 TEST_F(SignedSettingsTest, RetrievePolicyToRetrieveProperty) { | |
| 393 base::FundamentalValue fake_value(false); | |
| 394 PolicyDelegate d(&fake_value); | |
| 395 d.expect_success(); | |
| 396 scoped_refptr<SignedSettings> s( | |
| 397 SignedSettings::CreateRetrievePropertyOp(fake_prop_, &d)); | |
| 398 | |
| 399 em::PolicyData fake_pol = BuildPolicyData(std::vector<std::string>()); | |
| 400 std::string data = fake_pol.SerializeAsString(); | |
| 401 std::string signed_serialized; | |
| 402 em::PolicyFetchResponse signed_policy = BuildProto(data, | |
| 403 fake_signature_, | |
| 404 &signed_serialized); | |
| 405 MockSessionManagerClient* client = | |
| 406 mock_dbus_thread_manager_->mock_session_manager_client(); | |
| 407 EXPECT_CALL(*client, RetrievePolicy(_)) | |
| 408 .WillOnce(Retrieve(signed_serialized)) | |
| 409 .RetiresOnSaturation(); | |
| 410 | |
| 411 mock_service(s.get(), &m_); | |
| 412 | |
| 413 EXPECT_CALL(m_, GetStatus(_)) | |
| 414 .WillOnce(Return(OwnershipService::OWNERSHIP_TAKEN)) | |
| 415 .WillOnce(Return(OwnershipService::OWNERSHIP_TAKEN)); | |
| 416 EXPECT_CALL(m_, has_cached_policy()) | |
| 417 .WillOnce(Return(false)) | |
| 418 .WillOnce(Return(true)); | |
| 419 em::PolicyData out_pol; | |
| 420 EXPECT_CALL(m_, set_cached_policy(A<const em::PolicyData&>())) | |
| 421 .WillOnce(SaveArg<0>(&out_pol)); | |
| 422 EXPECT_CALL(m_, cached_policy()) | |
| 423 .WillOnce(ReturnRef(out_pol)); | |
| 424 | |
| 425 EXPECT_CALL(m_, StartVerifyAttempt(data, fake_value_signature_, _)) | |
| 426 .WillOnce(FinishKeyOp(fake_value_signature_)) | |
| 427 .RetiresOnSaturation(); | |
| 428 | |
| 429 s->Execute(); | |
| 430 message_loop_.RunAllPending(); | |
| 431 } | |
| 432 | 205 |
| 433 TEST_F(SignedSettingsTest, SignAndStorePolicy) { | 206 TEST_F(SignedSettingsTest, SignAndStorePolicy) { |
| 434 NormalDelegate<bool> d(true); | 207 NormalDelegate<bool> d(true); |
| 435 d.expect_success(); | 208 d.expect_success(); |
| 436 | 209 |
| 437 em::PolicyData in_pol = BuildPolicyData(std::vector<std::string>()); | 210 em::PolicyData in_pol = BuildPolicyData(std::vector<std::string>()); |
| 438 std::string data_serialized = in_pol.SerializeAsString(); | 211 std::string data_serialized = in_pol.SerializeAsString(); |
| 439 std::string serialized; | 212 std::string serialized; |
| 440 em::PolicyFetchResponse fake_policy = BuildProto(data_serialized, | 213 em::PolicyFetchResponse fake_policy = BuildProto(data_serialized, |
| 441 std::string(), | 214 std::string(), |
| 442 &serialized); | 215 &serialized); |
| 443 scoped_refptr<SignedSettings> s( | 216 scoped_refptr<SignedSettings> s( |
| 444 SignedSettings::CreateStorePolicyOp(&fake_policy, &d)); | 217 SignedSettings::CreateStorePolicyOp(&fake_policy, &d)); |
| 445 | 218 |
| 446 mock_service(s.get(), &m_); | 219 mock_service(s.get(), &m_); |
| 447 EXPECT_CALL(m_, StartSigningAttempt(StrEq(data_serialized), _)) | 220 EXPECT_CALL(m_, StartSigningAttempt(StrEq(data_serialized), _)) |
| 448 .Times(1); | 221 .Times(1); |
| 449 em::PolicyData out_pol; | 222 em::PolicyData out_pol; |
| 450 EXPECT_CALL(m_, set_cached_policy(A<const em::PolicyData&>())) | |
| 451 .WillOnce(SaveArg<0>(&out_pol)); | |
| 452 | 223 |
| 453 // Ask for signature over unsigned policy. | 224 // Ask for signature over unsigned policy. |
| 454 s->Execute(); | 225 s->Execute(); |
| 455 message_loop_.RunAllPending(); | 226 message_loop_.RunAllPending(); |
| 456 | 227 |
| 457 // Fake out a successful signing. | 228 // Fake out a successful signing. |
| 458 std::string signed_serialized; | 229 std::string signed_serialized; |
| 459 em::PolicyFetchResponse signed_policy = BuildProto(data_serialized, | 230 em::PolicyFetchResponse signed_policy = BuildProto(data_serialized, |
| 460 fake_signature_, | 231 fake_signature_, |
| 461 &signed_serialized); | 232 &signed_serialized); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 481 scoped_refptr<SignedSettings> s( | 252 scoped_refptr<SignedSettings> s( |
| 482 SignedSettings::CreateStorePolicyOp(&signed_policy, &d)); | 253 SignedSettings::CreateStorePolicyOp(&signed_policy, &d)); |
| 483 MockSessionManagerClient* client = | 254 MockSessionManagerClient* client = |
| 484 mock_dbus_thread_manager_->mock_session_manager_client(); | 255 mock_dbus_thread_manager_->mock_session_manager_client(); |
| 485 EXPECT_CALL(*client, StorePolicy(signed_serialized, _)) | 256 EXPECT_CALL(*client, StorePolicy(signed_serialized, _)) |
| 486 .WillOnce(Store(true)) | 257 .WillOnce(Store(true)) |
| 487 .RetiresOnSaturation(); | 258 .RetiresOnSaturation(); |
| 488 | 259 |
| 489 mock_service(s.get(), &m_); | 260 mock_service(s.get(), &m_); |
| 490 em::PolicyData out_pol; | 261 em::PolicyData out_pol; |
| 491 EXPECT_CALL(m_, set_cached_policy(A<const em::PolicyData&>())) | |
| 492 .WillOnce(SaveArg<0>(&out_pol)); | |
| 493 | 262 |
| 494 s->Execute(); | 263 s->Execute(); |
| 495 message_loop_.RunAllPending(); | 264 message_loop_.RunAllPending(); |
| 496 } | 265 } |
| 497 | 266 |
| 498 TEST_F(SignedSettingsTest, StorePolicyNoKey) { | 267 TEST_F(SignedSettingsTest, StorePolicyNoKey) { |
| 499 FailingStorePolicyOp(OwnerManager::KEY_UNAVAILABLE); | 268 FailingStorePolicyOp(OwnerManager::KEY_UNAVAILABLE); |
| 500 } | 269 } |
| 501 | 270 |
| 502 TEST_F(SignedSettingsTest, StorePolicyFailed) { | 271 TEST_F(SignedSettingsTest, StorePolicyFailed) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 532 MockSessionManagerClient* client = | 301 MockSessionManagerClient* client = |
| 533 mock_dbus_thread_manager_->mock_session_manager_client(); | 302 mock_dbus_thread_manager_->mock_session_manager_client(); |
| 534 EXPECT_CALL(*client, RetrievePolicy(_)) | 303 EXPECT_CALL(*client, RetrievePolicy(_)) |
| 535 .WillOnce(Retrieve(signed_serialized)) | 304 .WillOnce(Retrieve(signed_serialized)) |
| 536 .RetiresOnSaturation(); | 305 .RetiresOnSaturation(); |
| 537 | 306 |
| 538 mock_service(s.get(), &m_); | 307 mock_service(s.get(), &m_); |
| 539 EXPECT_CALL(m_, StartVerifyAttempt(serialized, fake_value_signature_, _)) | 308 EXPECT_CALL(m_, StartVerifyAttempt(serialized, fake_value_signature_, _)) |
| 540 .Times(1); | 309 .Times(1); |
| 541 em::PolicyData out_pol; | 310 em::PolicyData out_pol; |
| 542 EXPECT_CALL(m_, set_cached_policy(A<const em::PolicyData&>())) | |
| 543 .WillOnce(SaveArg<0>(&out_pol)); | |
| 544 | 311 |
| 545 s->Execute(); | 312 s->Execute(); |
| 546 message_loop_.RunAllPending(); | 313 message_loop_.RunAllPending(); |
| 547 | 314 |
| 548 s->OnKeyOpComplete(OwnerManager::SUCCESS, std::vector<uint8>()); | 315 s->OnKeyOpComplete(OwnerManager::SUCCESS, std::vector<uint8>()); |
| 549 message_loop_.RunAllPending(); | 316 message_loop_.RunAllPending(); |
| 550 } | 317 } |
| 551 | 318 |
| 552 TEST_F(SignedSettingsTest, RetrieveNullPolicy) { | 319 TEST_F(SignedSettingsTest, RetrieveNullPolicy) { |
| 553 em::PolicyFetchResponse policy; | 320 em::PolicyFetchResponse policy; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 621 .Times(1); | 388 .Times(1); |
| 622 | 389 |
| 623 s->Execute(); | 390 s->Execute(); |
| 624 message_loop_.RunAllPending(); | 391 message_loop_.RunAllPending(); |
| 625 | 392 |
| 626 s->OnKeyOpComplete(OwnerManager::OPERATION_FAILED, std::vector<uint8>()); | 393 s->OnKeyOpComplete(OwnerManager::OPERATION_FAILED, std::vector<uint8>()); |
| 627 message_loop_.RunAllPending(); | 394 message_loop_.RunAllPending(); |
| 628 } | 395 } |
| 629 | 396 |
| 630 } // namespace chromeos | 397 } // namespace chromeos |
| OLD | NEW |