| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/policy/cloud/cloud_policy_manager.h" | 5 #include "chrome/browser/policy/cloud/cloud_policy_manager.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | |
| 9 #include "base/callback.h" | 8 #include "base/callback.h" |
| 10 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 11 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 13 #include "base/test/test_simple_task_runner.h" | |
| 14 #include "chrome/browser/invalidation/fake_invalidation_service.h" | |
| 15 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" | 12 #include "chrome/browser/policy/cloud/cloud_policy_constants.h" |
| 16 #include "chrome/browser/policy/cloud/cloud_policy_invalidator.h" | |
| 17 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" | 13 #include "chrome/browser/policy/cloud/mock_cloud_policy_client.h" |
| 18 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" | 14 #include "chrome/browser/policy/cloud/mock_cloud_policy_store.h" |
| 19 #include "chrome/browser/policy/cloud/policy_builder.h" | 15 #include "chrome/browser/policy/cloud/policy_builder.h" |
| 20 #include "chrome/browser/policy/configuration_policy_provider_test.h" | 16 #include "chrome/browser/policy/configuration_policy_provider_test.h" |
| 21 #include "chrome/browser/policy/external_data_fetcher.h" | 17 #include "chrome/browser/policy/external_data_fetcher.h" |
| 22 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 18 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 23 #include "sync/notifier/invalidation_util.h" | |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 26 | 21 |
| 27 using testing::Mock; | 22 using testing::Mock; |
| 28 using testing::_; | 23 using testing::_; |
| 29 | 24 |
| 30 namespace em = enterprise_management; | 25 namespace em = enterprise_management; |
| 31 | 26 |
| 32 namespace policy { | 27 namespace policy { |
| 33 namespace { | 28 namespace { |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 dm_protocol::kChromeUserPolicyType, | 138 dm_protocol::kChromeUserPolicyType, |
| 144 std::string()), | 139 std::string()), |
| 145 store) {} | 140 store) {} |
| 146 virtual ~TestCloudPolicyManager() {} | 141 virtual ~TestCloudPolicyManager() {} |
| 147 | 142 |
| 148 // Publish the protected members for testing. | 143 // Publish the protected members for testing. |
| 149 using CloudPolicyManager::client; | 144 using CloudPolicyManager::client; |
| 150 using CloudPolicyManager::store; | 145 using CloudPolicyManager::store; |
| 151 using CloudPolicyManager::service; | 146 using CloudPolicyManager::service; |
| 152 using CloudPolicyManager::CheckAndPublishPolicy; | 147 using CloudPolicyManager::CheckAndPublishPolicy; |
| 153 using CloudPolicyManager::StartRefreshScheduler; | |
| 154 | 148 |
| 155 private: | 149 private: |
| 156 DISALLOW_COPY_AND_ASSIGN(TestCloudPolicyManager); | 150 DISALLOW_COPY_AND_ASSIGN(TestCloudPolicyManager); |
| 157 }; | 151 }; |
| 158 | 152 |
| 159 MATCHER_P(ProtoMatches, proto, "") { | 153 MATCHER_P(ProtoMatches, proto, "") { |
| 160 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); | 154 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); |
| 161 } | 155 } |
| 162 | 156 |
| 163 class CloudPolicyManagerTest : public testing::Test { | 157 class CloudPolicyManagerTest : public testing::Test { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 180 manager_->Init(); | 174 manager_->Init(); |
| 181 Mock::VerifyAndClearExpectations(&store_); | 175 Mock::VerifyAndClearExpectations(&store_); |
| 182 manager_->AddObserver(&observer_); | 176 manager_->AddObserver(&observer_); |
| 183 } | 177 } |
| 184 | 178 |
| 185 virtual void TearDown() OVERRIDE { | 179 virtual void TearDown() OVERRIDE { |
| 186 manager_->RemoveObserver(&observer_); | 180 manager_->RemoveObserver(&observer_); |
| 187 manager_->Shutdown(); | 181 manager_->Shutdown(); |
| 188 } | 182 } |
| 189 | 183 |
| 190 // Sets up for an invalidations test. | |
| 191 void CreateInvalidator() { | |
| 192 // Add the invalidation registration info to the policy data. | |
| 193 em::PolicyData* policy_data = new em::PolicyData(policy_.policy_data()); | |
| 194 policy_data->set_invalidation_source(12345); | |
| 195 policy_data->set_invalidation_name("12345"); | |
| 196 store_.policy_.reset(policy_data); | |
| 197 | |
| 198 // Connect the core. | |
| 199 MockCloudPolicyClient* client = new MockCloudPolicyClient(); | |
| 200 EXPECT_CALL(*client, SetupRegistration(_, _)); | |
| 201 manager_->core()->Connect(scoped_ptr<CloudPolicyClient>(client)); | |
| 202 | |
| 203 // Create invalidation objects. | |
| 204 task_runner_ = new base::TestSimpleTaskRunner(); | |
| 205 invalidation_service_.reset(new invalidation::FakeInvalidationService()); | |
| 206 invalidator_.reset(new CloudPolicyInvalidator( | |
| 207 manager_.get(), | |
| 208 manager_->core()->store(), | |
| 209 task_runner_)); | |
| 210 } | |
| 211 | |
| 212 void ShutdownInvalidator() { | |
| 213 invalidator_->Shutdown(); | |
| 214 } | |
| 215 | |
| 216 // Call EnableInvalidations on the manager. | |
| 217 void EnableInvalidations() { | |
| 218 manager_->EnableInvalidations( | |
| 219 base::Bind( | |
| 220 &CloudPolicyInvalidator::InitializeWithService, | |
| 221 base::Unretained(invalidator_.get()), | |
| 222 base::Unretained(invalidation_service_.get()))); | |
| 223 } | |
| 224 | |
| 225 // Determine if the invalidator has registered with the invalidation service. | |
| 226 bool IsInvalidatorRegistered() { | |
| 227 syncer::ObjectIdSet object_ids = | |
| 228 invalidation_service_->invalidator_registrar().GetAllRegisteredIds(); | |
| 229 return object_ids.size() == 1 && | |
| 230 object_ids.begin()->source() == 12345 && | |
| 231 object_ids.begin()->name() == "12345"; | |
| 232 } | |
| 233 | |
| 234 // Determine if the invalidator is unregistered with the invalidation service. | |
| 235 bool IsInvalidatorUnregistered() { | |
| 236 syncer::ObjectIdSet object_ids = | |
| 237 invalidation_service_->invalidator_registrar().GetAllRegisteredIds(); | |
| 238 return object_ids.empty(); | |
| 239 } | |
| 240 | |
| 241 // Required by the refresh scheduler that's created by the manager. | 184 // Required by the refresh scheduler that's created by the manager. |
| 242 base::MessageLoop loop_; | 185 base::MessageLoop loop_; |
| 243 | 186 |
| 244 // Testing policy. | 187 // Testing policy. |
| 245 const PolicyNamespaceKey policy_ns_key_; | 188 const PolicyNamespaceKey policy_ns_key_; |
| 246 UserPolicyBuilder policy_; | 189 UserPolicyBuilder policy_; |
| 247 PolicyMap policy_map_; | 190 PolicyMap policy_map_; |
| 248 PolicyBundle expected_bundle_; | 191 PolicyBundle expected_bundle_; |
| 249 | 192 |
| 250 // Policy infrastructure. | 193 // Policy infrastructure. |
| 251 MockConfigurationPolicyObserver observer_; | 194 MockConfigurationPolicyObserver observer_; |
| 252 MockCloudPolicyStore store_; | 195 MockCloudPolicyStore store_; |
| 253 scoped_ptr<TestCloudPolicyManager> manager_; | 196 scoped_ptr<TestCloudPolicyManager> manager_; |
| 254 | 197 |
| 255 // Invalidation objects. | |
| 256 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | |
| 257 scoped_ptr<invalidation::FakeInvalidationService> invalidation_service_; | |
| 258 scoped_ptr<CloudPolicyInvalidator> invalidator_; | |
| 259 | |
| 260 private: | 198 private: |
| 261 DISALLOW_COPY_AND_ASSIGN(CloudPolicyManagerTest); | 199 DISALLOW_COPY_AND_ASSIGN(CloudPolicyManagerTest); |
| 262 }; | 200 }; |
| 263 | 201 |
| 264 TEST_F(CloudPolicyManagerTest, InitAndShutdown) { | 202 TEST_F(CloudPolicyManagerTest, InitAndShutdown) { |
| 265 PolicyBundle empty_bundle; | 203 PolicyBundle empty_bundle; |
| 266 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); | 204 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); |
| 267 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | 205 EXPECT_FALSE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); |
| 268 | 206 |
| 269 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | 207 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 TEST_F(CloudPolicyManagerTest, SignalOnError) { | 332 TEST_F(CloudPolicyManagerTest, SignalOnError) { |
| 395 // Simulate a failed load and verify that it triggers OnUpdatePolicy(). | 333 // Simulate a failed load and verify that it triggers OnUpdatePolicy(). |
| 396 store_.policy_.reset(new em::PolicyData(policy_.policy_data())); | 334 store_.policy_.reset(new em::PolicyData(policy_.policy_data())); |
| 397 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | 335 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 398 store_.NotifyStoreError(); | 336 store_.NotifyStoreError(); |
| 399 Mock::VerifyAndClearExpectations(&observer_); | 337 Mock::VerifyAndClearExpectations(&observer_); |
| 400 | 338 |
| 401 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); | 339 EXPECT_TRUE(manager_->IsInitializationComplete(POLICY_DOMAIN_CHROME)); |
| 402 } | 340 } |
| 403 | 341 |
| 404 TEST_F(CloudPolicyManagerTest, EnableInvalidationsBeforeRefreshScheduler) { | |
| 405 CreateInvalidator(); | |
| 406 EXPECT_TRUE(IsInvalidatorUnregistered()); | |
| 407 EnableInvalidations(); | |
| 408 EXPECT_TRUE(IsInvalidatorUnregistered()); | |
| 409 manager_->StartRefreshScheduler(); | |
| 410 EXPECT_TRUE(IsInvalidatorRegistered()); | |
| 411 ShutdownInvalidator(); | |
| 412 } | |
| 413 | |
| 414 TEST_F(CloudPolicyManagerTest, EnableInvalidationsAfterRefreshScheduler) { | |
| 415 CreateInvalidator(); | |
| 416 EXPECT_TRUE(IsInvalidatorUnregistered()); | |
| 417 manager_->StartRefreshScheduler(); | |
| 418 EXPECT_TRUE(IsInvalidatorUnregistered()); | |
| 419 EnableInvalidations(); | |
| 420 EXPECT_TRUE(IsInvalidatorRegistered()); | |
| 421 ShutdownInvalidator(); | |
| 422 } | |
| 423 | |
| 424 } // namespace | 342 } // namespace |
| 425 } // namespace policy | 343 } // namespace policy |
| OLD | NEW |