| 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 "base/basictypes.h" |
| 6 #include "base/memory/scoped_ptr.h" |
| 7 #include "base/message_loop.h" |
| 8 #include "chrome/browser/policy/cloud_policy_service.h" |
| 9 #include "chrome/browser/policy/configuration_policy_provider_test.h" |
| 10 #include "chrome/browser/policy/mock_cloud_policy_store.h" |
| 11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 12 #include "chrome/browser/policy/mock_device_management_service.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" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace em = enterprise_management; |
| 22 |
| 23 using testing::AnyNumber; |
| 24 using testing::AtLeast; |
| 25 using testing::Mock; |
| 26 using testing::_; |
| 27 |
| 28 namespace policy { |
| 29 namespace { |
| 30 |
| 31 class TestHarness : public PolicyProviderTestHarness { |
| 32 public: |
| 33 explicit TestHarness(PolicyLevel level); |
| 34 virtual ~TestHarness(); |
| 35 |
| 36 virtual void SetUp() OVERRIDE; |
| 37 |
| 38 virtual ConfigurationPolicyProvider* CreateProvider( |
| 39 const PolicyDefinitionList* policy_definition_list) OVERRIDE; |
| 40 |
| 41 virtual void InstallEmptyPolicy() OVERRIDE; |
| 42 virtual void InstallStringPolicy(const std::string& policy_name, |
| 43 const std::string& policy_value) OVERRIDE; |
| 44 virtual void InstallIntegerPolicy(const std::string& policy_name, |
| 45 int policy_value) OVERRIDE; |
| 46 virtual void InstallBooleanPolicy(const std::string& policy_name, |
| 47 bool policy_value) OVERRIDE; |
| 48 virtual void InstallStringListPolicy( |
| 49 const std::string& policy_name, |
| 50 const base::ListValue* policy_value) OVERRIDE; |
| 51 virtual void InstallDictionaryPolicy( |
| 52 const std::string& policy_name, |
| 53 const base::DictionaryValue* policy_value) OVERRIDE; |
| 54 |
| 55 // Creates harnesses for mandatory and recommended levels, respectively. |
| 56 static PolicyProviderTestHarness* CreateMandatory(); |
| 57 static PolicyProviderTestHarness* CreateRecommended(); |
| 58 |
| 59 private: |
| 60 MockCloudPolicyStore* store_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(TestHarness); |
| 63 }; |
| 64 |
| 65 TestHarness::TestHarness(PolicyLevel level) |
| 66 : PolicyProviderTestHarness(level, POLICY_SCOPE_USER) {} |
| 67 |
| 68 TestHarness::~TestHarness() {} |
| 69 |
| 70 void TestHarness::SetUp() {} |
| 71 |
| 72 ConfigurationPolicyProvider* TestHarness::CreateProvider( |
| 73 const PolicyDefinitionList* policy_definition_list) { |
| 74 // Create and initialize the store. |
| 75 store_ = new MockCloudPolicyStore(); |
| 76 store_->NotifyStoreLoaded(); |
| 77 EXPECT_CALL(*store_, Load()); |
| 78 return new UserCloudPolicyManager(policy_definition_list, |
| 79 scoped_ptr<CloudPolicyStore>(store_).Pass(), |
| 80 false); |
| 81 } |
| 82 |
| 83 void TestHarness::InstallEmptyPolicy() {} |
| 84 |
| 85 void TestHarness::InstallStringPolicy(const std::string& policy_name, |
| 86 const std::string& policy_value) { |
| 87 store_->policy_map_.Set(policy_name, policy_level(), policy_scope(), |
| 88 base::Value::CreateStringValue(policy_value)); |
| 89 } |
| 90 |
| 91 void TestHarness::InstallIntegerPolicy(const std::string& policy_name, |
| 92 int policy_value) { |
| 93 store_->policy_map_.Set(policy_name, policy_level(), policy_scope(), |
| 94 base::Value::CreateIntegerValue(policy_value)); |
| 95 } |
| 96 |
| 97 void TestHarness::InstallBooleanPolicy(const std::string& policy_name, |
| 98 bool policy_value) { |
| 99 store_->policy_map_.Set(policy_name, policy_level(), policy_scope(), |
| 100 base::Value::CreateBooleanValue(policy_value)); |
| 101 } |
| 102 |
| 103 void TestHarness::InstallStringListPolicy(const std::string& policy_name, |
| 104 const base::ListValue* policy_value) { |
| 105 store_->policy_map_.Set(policy_name, policy_level(), policy_scope(), |
| 106 policy_value->DeepCopy()); |
| 107 } |
| 108 |
| 109 void TestHarness::InstallDictionaryPolicy( |
| 110 const std::string& policy_name, |
| 111 const base::DictionaryValue* policy_value) { |
| 112 store_->policy_map_.Set(policy_name, policy_level(), policy_scope(), |
| 113 policy_value->DeepCopy()); |
| 114 } |
| 115 |
| 116 // static |
| 117 PolicyProviderTestHarness* TestHarness::CreateMandatory() { |
| 118 return new TestHarness(POLICY_LEVEL_MANDATORY); |
| 119 } |
| 120 |
| 121 // static |
| 122 PolicyProviderTestHarness* TestHarness::CreateRecommended() { |
| 123 return new TestHarness(POLICY_LEVEL_RECOMMENDED); |
| 124 } |
| 125 |
| 126 // Instantiate abstract test case for basic policy reading tests. |
| 127 INSTANTIATE_TEST_CASE_P( |
| 128 UserCloudPolicyManagerProviderTest, |
| 129 ConfigurationPolicyProviderTest, |
| 130 testing::Values(TestHarness::CreateMandatory, |
| 131 TestHarness::CreateRecommended)); |
| 132 |
| 133 class UserCloudPolicyManagerTest : public testing::Test { |
| 134 protected: |
| 135 UserCloudPolicyManagerTest() |
| 136 : store_(NULL) {} |
| 137 |
| 138 virtual void SetUp() OVERRIDE { |
| 139 browser::RegisterLocalState(&prefs_); |
| 140 |
| 141 // Set up a policy map for testing. |
| 142 policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 143 base::Value::CreateStringValue("value")); |
| 144 expected_bundle_.Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( |
| 145 policy_map_); |
| 146 |
| 147 // Create a fake policy blob to deliver to the client. |
| 148 em::PolicyData policy_data; |
| 149 em::PolicyFetchResponse* policy_response = |
| 150 policy_blob_.mutable_policy_response()->add_response(); |
| 151 ASSERT_TRUE(policy_data.SerializeToString( |
| 152 policy_response->mutable_policy_data())); |
| 153 |
| 154 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)) |
| 155 .Times(AnyNumber()); |
| 156 } |
| 157 |
| 158 void CreateManager(bool wait_for_policy_fetch) { |
| 159 store_ = new MockCloudPolicyStore(); |
| 160 EXPECT_CALL(*store_, Load()); |
| 161 manager_.reset( |
| 162 new UserCloudPolicyManager(policy::GetChromePolicyDefinitionList(), |
| 163 scoped_ptr<CloudPolicyStore>(store_).Pass(), |
| 164 wait_for_policy_fetch)); |
| 165 registrar_.Init(manager_.get(), &observer_); |
| 166 } |
| 167 |
| 168 void CreateManagerWithPendingFetch() { |
| 169 CreateManager(true); |
| 170 manager_->Initialize(&prefs_, &device_management_service_, |
| 171 USER_AFFILIATION_NONE); |
| 172 EXPECT_FALSE(manager_->IsInitializationComplete()); |
| 173 |
| 174 // Finishing the Load() operation shouldn't set the initialized flag. |
| 175 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| 176 store_->NotifyStoreLoaded(); |
| 177 EXPECT_FALSE(manager_->IsInitializationComplete()); |
| 178 Mock::VerifyAndClearExpectations(&observer_); |
| 179 } |
| 180 |
| 181 // Required by the refresh scheduler that's created by the manager. |
| 182 MessageLoop loop_; |
| 183 |
| 184 // Convenience policy objects. |
| 185 em::DeviceManagementResponse policy_blob_; |
| 186 PolicyMap policy_map_; |
| 187 PolicyBundle expected_bundle_; |
| 188 |
| 189 // Policy infrastructure. |
| 190 TestingPrefService prefs_; |
| 191 MockConfigurationPolicyObserver observer_; |
| 192 MockDeviceManagementService device_management_service_; |
| 193 MockCloudPolicyStore* store_; |
| 194 scoped_ptr<UserCloudPolicyManager> manager_; |
| 195 ConfigurationPolicyObserverRegistrar registrar_; |
| 196 |
| 197 private: |
| 198 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); |
| 199 }; |
| 200 |
| 201 TEST_F(UserCloudPolicyManagerTest, Init) { |
| 202 CreateManager(false); |
| 203 PolicyBundle empty_bundle; |
| 204 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); |
| 205 EXPECT_FALSE(manager_->IsInitializationComplete()); |
| 206 |
| 207 store_->policy_map_.CopyFrom(policy_map_); |
| 208 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 209 store_->NotifyStoreLoaded(); |
| 210 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); |
| 211 EXPECT_TRUE(manager_->IsInitializationComplete()); |
| 212 } |
| 213 |
| 214 TEST_F(UserCloudPolicyManagerTest, Update) { |
| 215 CreateManager(false); |
| 216 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 217 store_->NotifyStoreLoaded(); |
| 218 EXPECT_TRUE(manager_->IsInitializationComplete()); |
| 219 PolicyBundle empty_bundle; |
| 220 EXPECT_TRUE(empty_bundle.Equals(manager_->policies())); |
| 221 |
| 222 store_->policy_map_.CopyFrom(policy_map_); |
| 223 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 224 store_->NotifyStoreLoaded(); |
| 225 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); |
| 226 EXPECT_TRUE(manager_->IsInitializationComplete()); |
| 227 } |
| 228 |
| 229 TEST_F(UserCloudPolicyManagerTest, RefreshNotRegistered) { |
| 230 CreateManager(false); |
| 231 manager_->Initialize(&prefs_, &device_management_service_, |
| 232 USER_AFFILIATION_NONE); |
| 233 |
| 234 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 235 store_->NotifyStoreLoaded(); |
| 236 Mock::VerifyAndClearExpectations(&observer_); |
| 237 |
| 238 // A refresh on a non-registered store should not block. |
| 239 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 240 manager_->RefreshPolicies(); |
| 241 Mock::VerifyAndClearExpectations(&observer_); |
| 242 } |
| 243 |
| 244 TEST_F(UserCloudPolicyManagerTest, RefreshSuccessful) { |
| 245 CreateManager(false); |
| 246 manager_->Initialize(&prefs_, &device_management_service_, |
| 247 USER_AFFILIATION_NONE); |
| 248 manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", |
| 249 "client_id"); |
| 250 |
| 251 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 252 store_->NotifyStoreLoaded(); |
| 253 Mock::VerifyAndClearExpectations(&observer_); |
| 254 |
| 255 // Start a refresh. |
| 256 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| 257 MockDeviceManagementJob* request_job; |
| 258 EXPECT_CALL(device_management_service_, |
| 259 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) |
| 260 .WillOnce(device_management_service_.CreateAsyncJob(&request_job)); |
| 261 manager_->RefreshPolicies(); |
| 262 ASSERT_TRUE(request_job); |
| 263 store_->policy_map_.CopyFrom(policy_map_); |
| 264 Mock::VerifyAndClearExpectations(&observer_); |
| 265 |
| 266 // A stray reload should be suppressed until the refresh completes. |
| 267 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| 268 store_->NotifyStoreLoaded(); |
| 269 Mock::VerifyAndClearExpectations(&observer_); |
| 270 |
| 271 // Respond to the policy fetch, which should trigger a write to |store_|. |
| 272 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| 273 EXPECT_CALL(*store_, Store(_)); |
| 274 request_job->SendResponse(DM_STATUS_SUCCESS, policy_blob_); |
| 275 Mock::VerifyAndClearExpectations(&observer_); |
| 276 |
| 277 // The load notification from |store_| should trigger the policy update. |
| 278 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 279 store_->NotifyStoreLoaded(); |
| 280 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); |
| 281 Mock::VerifyAndClearExpectations(&observer_); |
| 282 } |
| 283 |
| 284 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetch) { |
| 285 CreateManagerWithPendingFetch(); |
| 286 |
| 287 // Setting the token should trigger the policy fetch. |
| 288 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| 289 MockDeviceManagementJob* fetch_request = NULL; |
| 290 EXPECT_CALL(device_management_service_, |
| 291 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) |
| 292 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); |
| 293 manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", |
| 294 "client_id"); |
| 295 ASSERT_TRUE(fetch_request); |
| 296 EXPECT_FALSE(manager_->IsInitializationComplete()); |
| 297 Mock::VerifyAndClearExpectations(&observer_); |
| 298 |
| 299 // Respond to the policy fetch, which should trigger a write to |store_|. |
| 300 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| 301 EXPECT_CALL(*store_, Store(_)); |
| 302 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob_); |
| 303 EXPECT_FALSE(manager_->IsInitializationComplete()); |
| 304 Mock::VerifyAndClearExpectations(&observer_); |
| 305 |
| 306 // The load notification from |store_| should trigger the policy update and |
| 307 // flip the initialized bit. |
| 308 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 309 store_->NotifyStoreLoaded(); |
| 310 EXPECT_TRUE(manager_->IsInitializationComplete()); |
| 311 Mock::VerifyAndClearExpectations(&observer_); |
| 312 } |
| 313 |
| 314 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchError) { |
| 315 CreateManagerWithPendingFetch(); |
| 316 |
| 317 // Setting the token should trigger the policy fetch. |
| 318 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); |
| 319 MockDeviceManagementJob* fetch_request = NULL; |
| 320 EXPECT_CALL(device_management_service_, |
| 321 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) |
| 322 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); |
| 323 manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", |
| 324 "client_id"); |
| 325 ASSERT_TRUE(fetch_request); |
| 326 EXPECT_FALSE(manager_->IsInitializationComplete()); |
| 327 Mock::VerifyAndClearExpectations(&observer_); |
| 328 |
| 329 // Make the policy fetch fail, at which point the manager should bail out. |
| 330 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())).Times(AtLeast(1)); |
| 331 fetch_request->SendResponse(DM_STATUS_REQUEST_FAILED, policy_blob_); |
| 332 EXPECT_TRUE(manager_->IsInitializationComplete()); |
| 333 Mock::VerifyAndClearExpectations(&observer_); |
| 334 } |
| 335 |
| 336 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchCancel) { |
| 337 CreateManagerWithPendingFetch(); |
| 338 |
| 339 // Cancelling the initial fetch should flip the flag. |
| 340 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 341 manager_->CancelWaitForPolicyFetch(); |
| 342 EXPECT_TRUE(manager_->IsInitializationComplete()); |
| 343 Mock::VerifyAndClearExpectations(&observer_); |
| 344 } |
| 345 |
| 346 } // namespace |
| 347 } // namespace policy |
| OLD | NEW |