| 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/user_cloud_policy_manager.h" | 5 #include "chrome/browser/policy/user_cloud_policy_manager.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "chrome/browser/policy/mock_cloud_policy_store.h" | |
| 11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" | 10 #include "chrome/browser/policy/mock_configuration_policy_provider.h" |
| 12 #include "chrome/browser/policy/mock_device_management_service.h" | 11 #include "chrome/browser/policy/mock_user_cloud_policy_store.h" |
| 13 #include "chrome/browser/policy/proto/device_management_backend.pb.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" | 12 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 14 |
| 20 namespace em = enterprise_management; | 15 namespace em = enterprise_management; |
| 21 | 16 |
| 22 using testing::AnyNumber; | 17 using testing::AnyNumber; |
| 23 using testing::AtLeast; | 18 using testing::AtLeast; |
| 24 using testing::Mock; | 19 using testing::Mock; |
| 25 using testing::_; | 20 using testing::_; |
| 26 | 21 |
| 27 namespace policy { | 22 namespace policy { |
| 28 namespace { | 23 namespace { |
| 29 | 24 |
| 30 class UserCloudPolicyManagerTest : public testing::Test { | 25 class UserCloudPolicyManagerTest : public testing::Test { |
| 31 protected: | 26 protected: |
| 32 UserCloudPolicyManagerTest() | 27 UserCloudPolicyManagerTest() |
| 33 : store_(NULL) {} | 28 : store_(NULL) {} |
| 34 | 29 |
| 35 virtual void SetUp() OVERRIDE { | 30 virtual void SetUp() OVERRIDE { |
| 36 chrome::RegisterLocalState(&prefs_); | |
| 37 | |
| 38 // Set up a policy map for testing. | 31 // Set up a policy map for testing. |
| 39 policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 32 policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 40 base::Value::CreateStringValue("value")); | 33 base::Value::CreateStringValue("value")); |
| 41 expected_bundle_.Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( | 34 expected_bundle_.Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( |
| 42 policy_map_); | 35 policy_map_); |
| 43 | |
| 44 // Create a fake policy blob to deliver to the client. | |
| 45 em::PolicyData policy_data; | |
| 46 em::PolicyFetchResponse* policy_response = | |
| 47 policy_blob_.mutable_policy_response()->add_response(); | |
| 48 ASSERT_TRUE(policy_data.SerializeToString( | |
| 49 policy_response->mutable_policy_data())); | |
| 50 | |
| 51 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)) | |
| 52 .Times(AnyNumber()); | |
| 53 } | 36 } |
| 54 | 37 |
| 55 virtual void TearDown() OVERRIDE { | 38 virtual void TearDown() OVERRIDE { |
| 56 if (manager_) { | 39 if (manager_) { |
| 57 manager_->RemoveObserver(&observer_); | 40 manager_->RemoveObserver(&observer_); |
| 58 manager_->Shutdown(); | 41 manager_->Shutdown(); |
| 59 } | 42 } |
| 60 } | 43 } |
| 61 | 44 |
| 62 void CreateManager(bool wait_for_policy_fetch) { | 45 void CreateManager() { |
| 63 store_ = new MockCloudPolicyStore(); | 46 store_ = new MockUserCloudPolicyStore(); |
| 64 EXPECT_CALL(*store_, Load()); | 47 EXPECT_CALL(*store_, Load()); |
| 65 manager_.reset( | 48 manager_.reset( |
| 66 new UserCloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_), | 49 new UserCloudPolicyManager(NULL, |
| 67 wait_for_policy_fetch)); | 50 scoped_ptr<UserCloudPolicyStore>(store_))); |
| 68 manager_->Init(); | 51 manager_->Init(); |
| 69 manager_->AddObserver(&observer_); | 52 manager_->AddObserver(&observer_); |
| 70 Mock::VerifyAndClearExpectations(store_); | 53 Mock::VerifyAndClearExpectations(store_); |
| 71 } | 54 } |
| 72 | 55 |
| 73 void CreateManagerWithPendingFetch() { | |
| 74 CreateManager(true); | |
| 75 manager_->Initialize(&prefs_, &device_management_service_, | |
| 76 USER_AFFILIATION_NONE); | |
| 77 EXPECT_FALSE(manager_->IsInitializationComplete()); | |
| 78 | |
| 79 // Finishing the Load() operation shouldn't set the initialized flag. | |
| 80 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 81 store_->NotifyStoreLoaded(); | |
| 82 EXPECT_FALSE(manager_->IsInitializationComplete()); | |
| 83 Mock::VerifyAndClearExpectations(&observer_); | |
| 84 } | |
| 85 | |
| 86 // Required by the refresh scheduler that's created by the manager. | 56 // Required by the refresh scheduler that's created by the manager. |
| 87 MessageLoop loop_; | 57 MessageLoop loop_; |
| 88 | 58 |
| 89 // Convenience policy objects. | 59 // Convenience policy objects. |
| 90 em::DeviceManagementResponse policy_blob_; | |
| 91 PolicyMap policy_map_; | 60 PolicyMap policy_map_; |
| 92 PolicyBundle expected_bundle_; | 61 PolicyBundle expected_bundle_; |
| 93 | 62 |
| 94 // Policy infrastructure. | 63 // Policy infrastructure. |
| 95 TestingPrefService prefs_; | |
| 96 MockConfigurationPolicyObserver observer_; | 64 MockConfigurationPolicyObserver observer_; |
| 97 MockDeviceManagementService device_management_service_; | 65 MockUserCloudPolicyStore* store_; |
| 98 MockCloudPolicyStore* store_; | |
| 99 scoped_ptr<UserCloudPolicyManager> manager_; | 66 scoped_ptr<UserCloudPolicyManager> manager_; |
| 100 | 67 |
| 101 private: | 68 private: |
| 102 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); | 69 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); |
| 103 }; | 70 }; |
| 104 | 71 |
| 105 TEST_F(UserCloudPolicyManagerTest, ShutdownAndRemovePolicy) { | 72 TEST_F(UserCloudPolicyManagerTest, ShutdownAndRemovePolicy) { |
| 106 // Load policy, make sure it goes away when ShutdownAndRemove() is called. | 73 // Load policy, make sure it goes away when ShutdownAndRemove() is called. |
| 107 CreateManager(false); | 74 CreateManager(); |
| 108 store_->policy_map_.CopyFrom(policy_map_); | 75 store_->policy_map_.CopyFrom(policy_map_); |
| 109 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | 76 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); |
| 110 store_->NotifyStoreLoaded(); | 77 store_->NotifyStoreLoaded(); |
| 111 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); | 78 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); |
| 112 EXPECT_TRUE(manager_->IsInitializationComplete()); | 79 EXPECT_TRUE(manager_->IsInitializationComplete()); |
| 113 EXPECT_CALL(*store_, Clear()); | 80 EXPECT_CALL(*store_, Clear()); |
| 114 manager_->ShutdownAndRemovePolicy(); | 81 manager_->ShutdownAndRemovePolicy(); |
| 115 EXPECT_FALSE(manager_->cloud_policy_service()); | 82 EXPECT_FALSE(manager_->cloud_policy_service()); |
| 116 } | 83 } |
| 117 | 84 |
| 118 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetch) { | |
| 119 CreateManagerWithPendingFetch(); | |
| 120 | |
| 121 // Setting the token should trigger the policy fetch. | |
| 122 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 123 MockDeviceManagementJob* fetch_request = NULL; | |
| 124 EXPECT_CALL(device_management_service_, | |
| 125 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) | |
| 126 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); | |
| 127 manager_->cloud_policy_client()->SetupRegistration("dm_token", "client_id"); | |
| 128 ASSERT_TRUE(fetch_request); | |
| 129 EXPECT_FALSE(manager_->IsInitializationComplete()); | |
| 130 Mock::VerifyAndClearExpectations(&observer_); | |
| 131 | |
| 132 // Respond to the policy fetch, which should trigger a write to |store_|. | |
| 133 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 134 EXPECT_CALL(*store_, Store(_)); | |
| 135 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob_); | |
| 136 EXPECT_FALSE(manager_->IsInitializationComplete()); | |
| 137 Mock::VerifyAndClearExpectations(&observer_); | |
| 138 | |
| 139 // The load notification from |store_| should trigger the policy update and | |
| 140 // flip the initialized bit. | |
| 141 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
| 142 store_->NotifyStoreLoaded(); | |
| 143 EXPECT_TRUE(manager_->IsInitializationComplete()); | |
| 144 Mock::VerifyAndClearExpectations(&observer_); | |
| 145 } | |
| 146 | |
| 147 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchError) { | |
| 148 CreateManagerWithPendingFetch(); | |
| 149 | |
| 150 // Setting the token should trigger the policy fetch. | |
| 151 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); | |
| 152 MockDeviceManagementJob* fetch_request = NULL; | |
| 153 EXPECT_CALL(device_management_service_, | |
| 154 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) | |
| 155 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); | |
| 156 manager_->cloud_policy_client()->SetupRegistration("dm_token", "client_id"); | |
| 157 ASSERT_TRUE(fetch_request); | |
| 158 EXPECT_FALSE(manager_->IsInitializationComplete()); | |
| 159 Mock::VerifyAndClearExpectations(&observer_); | |
| 160 | |
| 161 // Make the policy fetch fail, at which point the manager should bail out. | |
| 162 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())).Times(AtLeast(1)); | |
| 163 fetch_request->SendResponse(DM_STATUS_REQUEST_FAILED, policy_blob_); | |
| 164 EXPECT_TRUE(manager_->IsInitializationComplete()); | |
| 165 Mock::VerifyAndClearExpectations(&observer_); | |
| 166 } | |
| 167 | |
| 168 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchCancel) { | |
| 169 CreateManagerWithPendingFetch(); | |
| 170 | |
| 171 // Cancelling the initial fetch should flip the flag. | |
| 172 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); | |
| 173 manager_->CancelWaitForPolicyFetch(); | |
| 174 EXPECT_TRUE(manager_->IsInitializationComplete()); | |
| 175 Mock::VerifyAndClearExpectations(&observer_); | |
| 176 } | |
| 177 | |
| 178 } // namespace | 85 } // namespace |
| 179 } // namespace policy | 86 } // namespace policy |
| OLD | NEW |