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