Chromium Code Reviews| 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_policy_manager.h" | |
| 6 | |
| 5 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" | |
| 6 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 8 #include "chrome/browser/policy/cloud_policy_service.h" | |
| 9 #include "chrome/browser/policy/configuration_policy_provider_test.h" | 11 #include "chrome/browser/policy/configuration_policy_provider_test.h" |
| 12 #include "chrome/browser/policy/mock_cloud_policy_client.h" | |
| 10 #include "chrome/browser/policy/mock_cloud_policy_store.h" | 13 #include "chrome/browser/policy/mock_cloud_policy_store.h" |
| 11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 14 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 12 #include "chrome/browser/policy/mock_device_management_service.h" | 15 #include "chrome/browser/policy/policy_builder.h" |
| 13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | |
| 14 #include "chrome/browser/policy/user_cloud_policy_manager.h" | |
| 15 #include "chrome/browser/prefs/browser_prefs.h" | |
| 16 #include "chrome/test/base/testing_pref_service.h" | |
| 17 #include "policy/policy_constants.h" | |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 18 |
| 21 namespace em = enterprise_management; | |
| 22 | |
| 23 using testing::AnyNumber; | |
| 24 using testing::AtLeast; | |
| 25 using testing::Mock; | 19 using testing::Mock; |
| 26 using testing::_; | 20 using testing::_; |
| 27 | 21 |
| 22 namespace em = enterprise_management; | |
| 23 | |
| 28 namespace policy { | 24 namespace policy { |
| 29 namespace { | 25 namespace { |
| 30 | 26 |
| 31 class TestHarness : public PolicyProviderTestHarness { | 27 class TestHarness : public PolicyProviderTestHarness { |
| 32 public: | 28 public: |
| 33 explicit TestHarness(PolicyLevel level); | 29 explicit TestHarness(PolicyLevel level); |
| 34 virtual ~TestHarness(); | 30 virtual ~TestHarness(); |
| 35 | 31 |
| 36 virtual void SetUp() OVERRIDE; | 32 virtual void SetUp() OVERRIDE; |
| 37 | 33 |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 68 TestHarness::~TestHarness() {} | 64 TestHarness::~TestHarness() {} |
| 69 | 65 |
| 70 void TestHarness::SetUp() {} | 66 void TestHarness::SetUp() {} |
| 71 | 67 |
| 72 ConfigurationPolicyProvider* TestHarness::CreateProvider( | 68 ConfigurationPolicyProvider* TestHarness::CreateProvider( |
| 73 const PolicyDefinitionList* policy_definition_list) { | 69 const PolicyDefinitionList* policy_definition_list) { |
| 74 // Create and initialize the store. | 70 // Create and initialize the store. |
| 75 store_ = new MockCloudPolicyStore(); | 71 store_ = new MockCloudPolicyStore(); |
| 76 store_->NotifyStoreLoaded(); | 72 store_->NotifyStoreLoaded(); |
| 77 EXPECT_CALL(*store_, Load()); | 73 EXPECT_CALL(*store_, Load()); |
| 78 return new UserCloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_).Pass(), | 74 return new CloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_)); |
| 79 false); | 75 Mock::VerifyAndClearExpectations(store_); |
|
Lei Zhang
2012/11/05 20:11:00
Umm, this does not look right...
Mattias Nissler (ping if slow)
2012/11/06 09:13:28
Indeed, thanks for pointing out.
CL with the fix
| |
| 80 } | 76 } |
| 81 | 77 |
| 82 void TestHarness::InstallEmptyPolicy() {} | 78 void TestHarness::InstallEmptyPolicy() {} |
| 83 | 79 |
| 84 void TestHarness::InstallStringPolicy(const std::string& policy_name, | 80 void TestHarness::InstallStringPolicy(const std::string& policy_name, |
| 85 const std::string& policy_value) { | 81 const std::string& policy_value) { |
| 86 store_->policy_map_.Set(policy_name, policy_level(), policy_scope(), | 82 store_->policy_map_.Set(policy_name, policy_level(), policy_scope(), |
| 87 base::Value::CreateStringValue(policy_value)); | 83 base::Value::CreateStringValue(policy_value)); |
| 88 } | 84 } |
| 89 | 85 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 return new TestHarness(POLICY_LEVEL_RECOMMENDED); | 118 return new TestHarness(POLICY_LEVEL_RECOMMENDED); |
| 123 } | 119 } |
| 124 | 120 |
| 125 // Instantiate abstract test case for basic policy reading tests. | 121 // Instantiate abstract test case for basic policy reading tests. |
| 126 INSTANTIATE_TEST_CASE_P( | 122 INSTANTIATE_TEST_CASE_P( |
| 127 UserCloudPolicyManagerProviderTest, | 123 UserCloudPolicyManagerProviderTest, |
| 128 ConfigurationPolicyProviderTest, | 124 ConfigurationPolicyProviderTest, |
| 129 testing::Values(TestHarness::CreateMandatory, | 125 testing::Values(TestHarness::CreateMandatory, |
| 130 TestHarness::CreateRecommended)); | 126 TestHarness::CreateRecommended)); |
| 131 | 127 |
| 132 class UserCloudPolicyManagerTest : public testing::Test { | 128 class TestCloudPolicyManager : public CloudPolicyManager { |
| 129 public: | |
| 130 explicit TestCloudPolicyManager(scoped_ptr<CloudPolicyStore> store) | |
| 131 : CloudPolicyManager(store.Pass()) {} | |
| 132 virtual ~TestCloudPolicyManager() {} | |
| 133 | |
| 134 // Publish the protected members for testing. | |
| 135 using CloudPolicyManager::InitializeService; | |
| 136 using CloudPolicyManager::ShutdownService; | |
| 137 using CloudPolicyManager::StartRefreshScheduler; | |
| 138 using CloudPolicyManager::CheckAndPublishPolicy; | |
| 139 | |
| 140 private: | |
| 141 DISALLOW_COPY_AND_ASSIGN(TestCloudPolicyManager); | |
| 142 }; | |
| 143 | |
| 144 MATCHER_P(ProtoMatches, proto, "") { | |
| 145 return arg.SerializePartialAsString() == proto.SerializePartialAsString(); | |
| 146 } | |
| 147 | |
| 148 class CloudPolicyManagerTest : public testing::Test { | |
| 133 protected: | 149 protected: |
| 134 UserCloudPolicyManagerTest() | 150 CloudPolicyManagerTest() {} |
| 135 : store_(NULL) {} | |
| 136 | 151 |
| 137 virtual void SetUp() OVERRIDE { | 152 virtual void SetUp() OVERRIDE { |
| 138 chrome::RegisterLocalState(&prefs_); | |
| 139 | |
| 140 // Set up a policy map for testing. | 153 // Set up a policy map for testing. |
| 141 policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 154 policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 142 base::Value::CreateStringValue("value")); | 155 base::Value::CreateStringValue("value")); |
| 143 expected_bundle_.Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( | 156 expected_bundle_.Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( |
| 144 policy_map_); | 157 policy_map_); |
| 145 | 158 |
| 146 // Create a fake policy blob to deliver to the client. | 159 policy_.payload().mutable_homepagelocation()->set_homepagelocation( |
| 147 em::PolicyData policy_data; | 160 "http://www.example.com"); |
| 148 em::PolicyFetchResponse* policy_response = | 161 policy_.Build(); |
| 149 policy_blob_.mutable_policy_response()->add_response(); | |
| 150 ASSERT_TRUE(policy_data.SerializeToString( | |
| 151 policy_response->mutable_policy_data())); | |
| 152 | 162 |
| 153 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)) | |
| 154 .Times(AnyNumber()); | |
| 155 } | |
| 156 | |
| 157 void CreateManager(bool wait_for_policy_fetch) { | |
| 158 store_ = new MockCloudPolicyStore(); | 163 store_ = new MockCloudPolicyStore(); |
| 159 EXPECT_CALL(*store_, Load()); | 164 EXPECT_CALL(*store_, Load()); |
| 160 manager_.reset( | 165 manager_.reset( |
| 161 new UserCloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_).Pass(), | 166 new TestCloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_))); |
| 162 wait_for_policy_fetch)); | 167 Mock::VerifyAndClearExpectations(store_); |
| 163 registrar_.Init(manager_.get(), &observer_); | 168 registrar_.Init(manager_.get(), &observer_); |
| 164 } | 169 } |
| 165 | 170 |
| 166 void CreateManagerWithPendingFetch() { | |
| 167 CreateManager(true); | |
| 168 manager_->Initialize(&prefs_, &device_management_service_, | |
| 169 USER_AFFILIATION_NONE); | |
| 170 EXPECT_FALSE(manager_->IsInitializationComplete()); | |
| 171 | |
| 172 // Finishing the Load() operation shouldn't set the initialized flag. | |
| 173 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 174 store_->NotifyStoreLoaded(); | |
| 175 EXPECT_FALSE(manager_->IsInitializationComplete()); | |
| 176 Mock::VerifyAndClearExpectations(&observer_); | |
| 177 } | |
| 178 | |
| 179 // Required by the refresh scheduler that's created by the manager. | 171 // Required by the refresh scheduler that's created by the manager. |
| 180 MessageLoop loop_; | 172 MessageLoop loop_; |
| 181 | 173 |
| 182 // Convenience policy objects. | 174 // Testing policy. |
| 183 em::DeviceManagementResponse policy_blob_; | 175 UserPolicyBuilder policy_; |
| 184 PolicyMap policy_map_; | 176 PolicyMap policy_map_; |
| 185 PolicyBundle expected_bundle_; | 177 PolicyBundle expected_bundle_; |
| 186 | 178 |
| 187 // Policy infrastructure. | 179 // Policy infrastructure. |
| 188 TestingPrefService prefs_; | |
| 189 MockConfigurationPolicyObserver observer_; | 180 MockConfigurationPolicyObserver observer_; |
| 190 MockDeviceManagementService device_management_service_; | |
| 191 MockCloudPolicyStore* store_; | 181 MockCloudPolicyStore* store_; |
| 192 scoped_ptr<UserCloudPolicyManager> manager_; | 182 scoped_ptr<TestCloudPolicyManager> manager_; |
| 193 ConfigurationPolicyObserverRegistrar registrar_; | 183 ConfigurationPolicyObserverRegistrar registrar_; |
| 194 | 184 |
| 195 private: | 185 private: |
| 196 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); | 186 DISALLOW_COPY_AND_ASSIGN(CloudPolicyManagerTest); |
| 197 }; | 187 }; |
| 198 | 188 |
| 199 TEST_F(UserCloudPolicyManagerTest, Init) { | 189 TEST_F(CloudPolicyManagerTest, InitAndShutdown) { |
| 200 CreateManager(false); | |
| 201 PolicyBundle empty_bundle; | 190 PolicyBundle empty_bundle; |
| 202 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); | 191 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); |
| 203 EXPECT_FALSE(manager_->IsInitializationComplete()); | 192 EXPECT_FALSE(manager_->IsInitializationComplete()); |
| 204 | 193 |
| 194 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 195 manager_->CheckAndPublishPolicy(); | |
| 196 Mock::VerifyAndClearExpectations(&observer_); | |
| 197 | |
| 198 store_->policy_map_.CopyFrom(policy_map_); | |
| 199 store_->policy_.reset(new em::PolicyData(policy_.policy_data())); | |
| 200 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
| 201 store_->NotifyStoreLoaded(); | |
| 202 Mock::VerifyAndClearExpectations(&observer_); | |
| 203 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); | |
| 204 EXPECT_TRUE(manager_->IsInitializationComplete()); | |
| 205 | |
| 206 MockCloudPolicyClient* client = new MockCloudPolicyClient(); | |
| 207 EXPECT_CALL(*client, SetupRegistration(_, _)); | |
| 208 manager_->InitializeService(scoped_ptr<CloudPolicyClient>(client)); | |
| 209 Mock::VerifyAndClearExpectations(client); | |
| 210 EXPECT_TRUE(manager_->cloud_policy_client()); | |
| 211 EXPECT_TRUE(manager_->cloud_policy_service()); | |
| 212 | |
| 213 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
| 214 manager_->CheckAndPublishPolicy(); | |
| 215 Mock::VerifyAndClearExpectations(&observer_); | |
| 216 | |
| 217 manager_->ShutdownService(); | |
| 218 EXPECT_FALSE(manager_->cloud_policy_client()); | |
| 219 EXPECT_FALSE(manager_->cloud_policy_service()); | |
| 220 } | |
| 221 | |
| 222 TEST_F(CloudPolicyManagerTest, RegistrationAndFetch) { | |
| 223 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
| 224 store_->NotifyStoreLoaded(); | |
| 225 Mock::VerifyAndClearExpectations(&observer_); | |
| 226 EXPECT_TRUE(manager_->IsInitializationComplete()); | |
| 227 | |
| 228 MockCloudPolicyClient* client = new MockCloudPolicyClient(); | |
| 229 manager_->InitializeService(scoped_ptr<CloudPolicyClient>(client)); | |
| 230 | |
| 231 client->SetDMToken(policy_.policy_data().request_token()); | |
| 232 client->NotifyRegistrationStateChanged(); | |
| 233 | |
| 234 client->SetPolicy(policy_.policy()); | |
| 235 EXPECT_CALL(*store_, Store(ProtoMatches(policy_.policy()))); | |
| 236 client->NotifyPolicyFetched(); | |
| 237 Mock::VerifyAndClearExpectations(store_); | |
| 238 | |
| 205 store_->policy_map_.CopyFrom(policy_map_); | 239 store_->policy_map_.CopyFrom(policy_map_); |
| 206 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | 240 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 207 store_->NotifyStoreLoaded(); | 241 store_->NotifyStoreLoaded(); |
| 242 Mock::VerifyAndClearExpectations(&observer_); | |
| 208 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); | 243 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); |
| 209 EXPECT_TRUE(manager_->IsInitializationComplete()); | |
| 210 } | 244 } |
| 211 | 245 |
| 212 TEST_F(UserCloudPolicyManagerTest, ShutdownAndRemovePolicy) { | 246 TEST_F(CloudPolicyManagerTest, Update) { |
| 213 // Load policy, make sure it goes away when ShutdownAndRemove() is called. | |
| 214 CreateManager(false); | |
| 215 store_->policy_map_.CopyFrom(policy_map_); | |
| 216 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | 247 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 217 store_->NotifyStoreLoaded(); | 248 store_->NotifyStoreLoaded(); |
| 218 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); | 249 Mock::VerifyAndClearExpectations(&observer_); |
| 219 EXPECT_TRUE(manager_->IsInitializationComplete()); | |
| 220 EXPECT_CALL(*store_, Clear()); | |
| 221 manager_->ShutdownAndRemovePolicy(); | |
| 222 EXPECT_FALSE(manager_->cloud_policy_service()); | |
| 223 } | |
| 224 | |
| 225 TEST_F(UserCloudPolicyManagerTest, Update) { | |
| 226 CreateManager(false); | |
| 227 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
| 228 store_->NotifyStoreLoaded(); | |
| 229 EXPECT_TRUE(manager_->IsInitializationComplete()); | 250 EXPECT_TRUE(manager_->IsInitializationComplete()); |
| 230 PolicyBundle empty_bundle; | 251 PolicyBundle empty_bundle; |
| 231 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); | 252 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); |
| 232 | 253 |
| 233 store_->policy_map_.CopyFrom(policy_map_); | 254 store_->policy_map_.CopyFrom(policy_map_); |
| 234 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | 255 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 235 store_->NotifyStoreLoaded(); | 256 store_->NotifyStoreLoaded(); |
| 257 Mock::VerifyAndClearExpectations(&observer_); | |
| 236 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); | 258 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); |
| 237 EXPECT_TRUE(manager_->IsInitializationComplete()); | 259 EXPECT_TRUE(manager_->IsInitializationComplete()); |
| 238 } | 260 } |
| 239 | 261 |
| 240 TEST_F(UserCloudPolicyManagerTest, RefreshNotRegistered) { | 262 TEST_F(CloudPolicyManagerTest, RefreshNotRegistered) { |
| 241 CreateManager(false); | 263 MockCloudPolicyClient* client = new MockCloudPolicyClient(); |
| 242 manager_->Initialize(&prefs_, &device_management_service_, | 264 manager_->InitializeService(scoped_ptr<CloudPolicyClient>(client)); |
| 243 USER_AFFILIATION_NONE); | |
| 244 | 265 |
| 245 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | 266 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 246 store_->NotifyStoreLoaded(); | 267 store_->NotifyStoreLoaded(); |
| 247 Mock::VerifyAndClearExpectations(&observer_); | 268 Mock::VerifyAndClearExpectations(&observer_); |
| 248 | 269 |
| 249 // A refresh on a non-registered store should not block. | 270 // A refresh on a non-registered store should not block. |
| 250 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | 271 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 251 manager_->RefreshPolicies(); | 272 manager_->RefreshPolicies(); |
| 252 Mock::VerifyAndClearExpectations(&observer_); | 273 Mock::VerifyAndClearExpectations(&observer_); |
| 253 } | 274 } |
| 254 | 275 |
| 255 TEST_F(UserCloudPolicyManagerTest, RefreshSuccessful) { | 276 TEST_F(CloudPolicyManagerTest, RefreshSuccessful) { |
| 256 CreateManager(false); | 277 MockCloudPolicyClient* client = new MockCloudPolicyClient(); |
| 257 manager_->Initialize(&prefs_, &device_management_service_, | 278 manager_->InitializeService(scoped_ptr<CloudPolicyClient>(client)); |
| 258 USER_AFFILIATION_NONE); | |
| 259 manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", | |
| 260 "client_id"); | |
| 261 | 279 |
| 280 // Simulate a store load. | |
| 281 store_->policy_.reset(new em::PolicyData(policy_.policy_data())); | |
| 262 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | 282 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 283 EXPECT_CALL(*client, SetupRegistration(_, _)); | |
| 263 store_->NotifyStoreLoaded(); | 284 store_->NotifyStoreLoaded(); |
| 285 Mock::VerifyAndClearExpectations(client); | |
| 264 Mock::VerifyAndClearExpectations(&observer_); | 286 Mock::VerifyAndClearExpectations(&observer_); |
| 265 | 287 |
| 288 // Acknowledge registration. | |
| 289 client->SetDMToken(policy_.policy_data().request_token()); | |
| 290 | |
| 266 // Start a refresh. | 291 // Start a refresh. |
| 267 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | 292 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| 268 MockDeviceManagementJob* request_job; | 293 EXPECT_CALL(*client, FetchPolicy()); |
| 269 EXPECT_CALL(device_management_service_, | |
| 270 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) | |
| 271 .WillOnce(device_management_service_.CreateAsyncJob(&request_job)); | |
| 272 manager_->RefreshPolicies(); | 294 manager_->RefreshPolicies(); |
| 273 ASSERT_TRUE(request_job); | 295 Mock::VerifyAndClearExpectations(client); |
| 296 Mock::VerifyAndClearExpectations(&observer_); | |
| 274 store_->policy_map_.CopyFrom(policy_map_); | 297 store_->policy_map_.CopyFrom(policy_map_); |
| 275 Mock::VerifyAndClearExpectations(&observer_); | |
| 276 | 298 |
| 277 // A stray reload should be suppressed until the refresh completes. | 299 // A stray reload should be suppressed until the refresh completes. |
| 278 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | 300 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| 279 store_->NotifyStoreLoaded(); | 301 store_->NotifyStoreLoaded(); |
| 280 Mock::VerifyAndClearExpectations(&observer_); | 302 Mock::VerifyAndClearExpectations(&observer_); |
| 281 | 303 |
| 282 // Respond to the policy fetch, which should trigger a write to |store_|. | 304 // Respond to the policy fetch, which should trigger a write to |store_|. |
| 283 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | 305 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| 284 EXPECT_CALL(*store_, Store(_)); | 306 EXPECT_CALL(*store_, Store(_)); |
| 285 request_job->SendResponse(DM_STATUS_SUCCESS, policy_blob_); | 307 client->SetPolicy(policy_.policy()); |
| 308 client->NotifyPolicyFetched(); | |
| 286 Mock::VerifyAndClearExpectations(&observer_); | 309 Mock::VerifyAndClearExpectations(&observer_); |
| 310 Mock::VerifyAndClearExpectations(store_); | |
| 287 | 311 |
| 288 // The load notification from |store_| should trigger the policy update. | 312 // The load notification from |store_| should trigger the policy update. |
| 289 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | 313 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 290 store_->NotifyStoreLoaded(); | 314 store_->NotifyStoreLoaded(); |
| 291 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); | 315 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); |
| 292 Mock::VerifyAndClearExpectations(&observer_); | 316 Mock::VerifyAndClearExpectations(&observer_); |
| 293 } | 317 } |
| 294 | 318 |
| 295 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetch) { | |
| 296 CreateManagerWithPendingFetch(); | |
| 297 | |
| 298 // Setting the token should trigger the policy fetch. | |
| 299 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 300 MockDeviceManagementJob* fetch_request = NULL; | |
| 301 EXPECT_CALL(device_management_service_, | |
| 302 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) | |
| 303 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); | |
| 304 manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", | |
| 305 "client_id"); | |
| 306 ASSERT_TRUE(fetch_request); | |
| 307 EXPECT_FALSE(manager_->IsInitializationComplete()); | |
| 308 Mock::VerifyAndClearExpectations(&observer_); | |
| 309 | |
| 310 // Respond to the policy fetch, which should trigger a write to |store_|. | |
| 311 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 312 EXPECT_CALL(*store_, Store(_)); | |
| 313 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob_); | |
| 314 EXPECT_FALSE(manager_->IsInitializationComplete()); | |
| 315 Mock::VerifyAndClearExpectations(&observer_); | |
| 316 | |
| 317 // The load notification from |store_| should trigger the policy update and | |
| 318 // flip the initialized bit. | |
| 319 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
| 320 store_->NotifyStoreLoaded(); | |
| 321 EXPECT_TRUE(manager_->IsInitializationComplete()); | |
| 322 Mock::VerifyAndClearExpectations(&observer_); | |
| 323 } | |
| 324 | |
| 325 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchError) { | |
| 326 CreateManagerWithPendingFetch(); | |
| 327 | |
| 328 // Setting the token should trigger the policy fetch. | |
| 329 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 330 MockDeviceManagementJob* fetch_request = NULL; | |
| 331 EXPECT_CALL(device_management_service_, | |
| 332 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) | |
| 333 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); | |
| 334 manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", | |
| 335 "client_id"); | |
| 336 ASSERT_TRUE(fetch_request); | |
| 337 EXPECT_FALSE(manager_->IsInitializationComplete()); | |
| 338 Mock::VerifyAndClearExpectations(&observer_); | |
| 339 | |
| 340 // Make the policy fetch fail, at which point the manager should bail out. | |
| 341 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())).Times(AtLeast(1)); | |
| 342 fetch_request->SendResponse(DM_STATUS_REQUEST_FAILED, policy_blob_); | |
| 343 EXPECT_TRUE(manager_->IsInitializationComplete()); | |
| 344 Mock::VerifyAndClearExpectations(&observer_); | |
| 345 } | |
| 346 | |
| 347 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchCancel) { | |
| 348 CreateManagerWithPendingFetch(); | |
| 349 | |
| 350 // Cancelling the initial fetch should flip the flag. | |
| 351 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
| 352 manager_->CancelWaitForPolicyFetch(); | |
| 353 EXPECT_TRUE(manager_->IsInitializationComplete()); | |
| 354 Mock::VerifyAndClearExpectations(&observer_); | |
| 355 } | |
| 356 | |
| 357 } // namespace | 319 } // namespace |
| 358 } // namespace policy | 320 } // namespace policy |
| OLD | NEW |