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