| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_controller.h" | 5 #include "chrome/browser/policy/cloud_policy_controller.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
| 9 #include "chrome/browser/policy/cloud_policy_data_store.h" | 9 #include "chrome/browser/policy/cloud_policy_data_store.h" |
| 10 #include "chrome/browser/policy/device_token_fetcher.h" | 10 #include "chrome/browser/policy/device_token_fetcher.h" |
| 11 #include "chrome/browser/policy/logging_work_scheduler.h" | 11 #include "chrome/browser/policy/logging_work_scheduler.h" |
| 12 #include "chrome/browser/policy/mock_device_management_backend.h" | 12 #include "chrome/browser/policy/mock_device_management_backend.h" |
| 13 #include "chrome/browser/policy/mock_device_management_service.h" | 13 #include "chrome/browser/policy/mock_device_management_service.h" |
| 14 #include "chrome/browser/policy/policy_notifier.h" | 14 #include "chrome/browser/policy/policy_notifier.h" |
| 15 #include "chrome/browser/policy/user_policy_cache.h" | 15 #include "chrome/browser/policy/user_policy_cache.h" |
| 16 #include "content/browser/browser_thread.h" | 16 #include "content/browser/browser_thread.h" |
| 17 #include "policy/policy_constants.h" | 17 #include "policy/policy_constants.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace policy { | 21 namespace policy { |
| 22 | 22 |
| 23 using ::testing::_; |
| 23 using ::testing::AnyNumber; | 24 using ::testing::AnyNumber; |
| 24 using ::testing::AtLeast; | 25 using ::testing::AtLeast; |
| 25 using ::testing::InSequence; | 26 using ::testing::InSequence; |
| 26 using ::testing::Mock; | 27 using ::testing::Mock; |
| 27 using ::testing::Return; | 28 using ::testing::Return; |
| 28 using ::testing::_; | |
| 29 | 29 |
| 30 class MockDeviceTokenFetcher : public DeviceTokenFetcher { | 30 class MockDeviceTokenFetcher : public DeviceTokenFetcher { |
| 31 public: | 31 public: |
| 32 explicit MockDeviceTokenFetcher(CloudPolicyCacheBase* cache) | 32 explicit MockDeviceTokenFetcher(CloudPolicyCacheBase* cache) |
| 33 : DeviceTokenFetcher(NULL, cache, NULL, NULL) {} | 33 : DeviceTokenFetcher(NULL, cache, NULL, NULL) {} |
| 34 virtual ~MockDeviceTokenFetcher() {} | 34 virtual ~MockDeviceTokenFetcher() {} |
| 35 | 35 |
| 36 MOCK_METHOD0(FetchToken, void()); | 36 MOCK_METHOD0(FetchToken, void()); |
| 37 MOCK_METHOD0(SetUnmanagedState, void()); | 37 MOCK_METHOD0(SetUnmanagedState, void()); |
| 38 | 38 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 BrowserThread file_thread_; | 97 BrowserThread file_thread_; |
| 98 | 98 |
| 99 DISALLOW_COPY_AND_ASSIGN(CloudPolicyControllerTest); | 99 DISALLOW_COPY_AND_ASSIGN(CloudPolicyControllerTest); |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 // If a device token is present when the controller starts up, it should | 102 // If a device token is present when the controller starts up, it should |
| 103 // fetch and apply policy. | 103 // fetch and apply policy. |
| 104 TEST_F(CloudPolicyControllerTest, StartupWithDeviceToken) { | 104 TEST_F(CloudPolicyControllerTest, StartupWithDeviceToken) { |
| 105 data_store_->SetupForTesting("fake_device_token", "device_id", "", "", | 105 data_store_->SetupForTesting("fake_device_token", "device_id", "", "", |
| 106 true); | 106 true); |
| 107 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce(DoAll( | 107 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(DoAll( |
| 108 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), | 108 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), |
| 109 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); | 109 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); |
| 110 CreateNewController(); | 110 CreateNewController(); |
| 111 loop_.RunAllPending(); | 111 loop_.RunAllPending(); |
| 112 ExpectHasSpdyPolicy(); | 112 ExpectHasSpdyPolicy(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 // If no device token is present when the controller starts up, it should | 115 // If no device token is present when the controller starts up, it should |
| 116 // instruct the token_fetcher_ to fetch one. | 116 // instruct the token_fetcher_ to fetch one. |
| 117 TEST_F(CloudPolicyControllerTest, StartupWithoutDeviceToken) { | 117 TEST_F(CloudPolicyControllerTest, StartupWithoutDeviceToken) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 133 } | 133 } |
| 134 | 134 |
| 135 // After policy has been fetched successfully, a new fetch should be triggered | 135 // After policy has been fetched successfully, a new fetch should be triggered |
| 136 // after the refresh interval has timed out. | 136 // after the refresh interval has timed out. |
| 137 TEST_F(CloudPolicyControllerTest, RefreshAfterSuccessfulPolicy) { | 137 TEST_F(CloudPolicyControllerTest, RefreshAfterSuccessfulPolicy) { |
| 138 data_store_->SetupForTesting("device_token", "device_id", | 138 data_store_->SetupForTesting("device_token", "device_id", |
| 139 "DannoHelperDelegate@b.com", | 139 "DannoHelperDelegate@b.com", |
| 140 "auth_token", true); | 140 "auth_token", true); |
| 141 { | 141 { |
| 142 InSequence s; | 142 InSequence s; |
| 143 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 143 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 144 MockDeviceManagementBackendSucceedSpdyCloudPolicy()); | 144 MockDeviceManagementBackendSucceedSpdyCloudPolicy()); |
| 145 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce(DoAll( | 145 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(DoAll( |
| 146 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), | 146 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), |
| 147 MockDeviceManagementBackendFailPolicy( | 147 MockDeviceManagementBackendFailPolicy( |
| 148 DeviceManagementBackend::kErrorRequestFailed))); | 148 DeviceManagementBackend::kErrorRequestFailed))); |
| 149 } | 149 } |
| 150 CreateNewController(); | 150 CreateNewController(); |
| 151 loop_.RunAllPending(); | 151 loop_.RunAllPending(); |
| 152 ExpectHasSpdyPolicy(); | 152 ExpectHasSpdyPolicy(); |
| 153 } | 153 } |
| 154 | 154 |
| 155 // If policy fetching failed, it should be retried. | 155 // If policy fetching failed, it should be retried. |
| 156 TEST_F(CloudPolicyControllerTest, RefreshAfterError) { | 156 TEST_F(CloudPolicyControllerTest, RefreshAfterError) { |
| 157 data_store_->SetupForTesting("device_token", "device_id", | 157 data_store_->SetupForTesting("device_token", "device_id", |
| 158 "DannoHelperDelegateImpl@b.com", | 158 "DannoHelperDelegateImpl@b.com", |
| 159 "auth_token", true); | 159 "auth_token", true); |
| 160 { | 160 { |
| 161 InSequence s; | 161 InSequence s; |
| 162 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 162 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 163 MockDeviceManagementBackendFailPolicy( | 163 MockDeviceManagementBackendFailPolicy( |
| 164 DeviceManagementBackend::kErrorRequestFailed)); | 164 DeviceManagementBackend::kErrorRequestFailed)); |
| 165 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce(DoAll( | 165 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce(DoAll( |
| 166 InvokeWithoutArgs(this, | 166 InvokeWithoutArgs(this, |
| 167 &CloudPolicyControllerTest::StopMessageLoop), | 167 &CloudPolicyControllerTest::StopMessageLoop), |
| 168 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); | 168 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); |
| 169 } | 169 } |
| 170 CreateNewController(); | 170 CreateNewController(); |
| 171 loop_.RunAllPending(); | 171 loop_.RunAllPending(); |
| 172 ExpectHasSpdyPolicy(); | 172 ExpectHasSpdyPolicy(); |
| 173 } | 173 } |
| 174 | 174 |
| 175 // If the backend reports that the device token was invalid, the controller | 175 // If the backend reports that the device token was invalid, the controller |
| 176 // should instruct the token fetcher to fetch a new token. | 176 // should instruct the token fetcher to fetch a new token. |
| 177 TEST_F(CloudPolicyControllerTest, InvalidToken) { | 177 TEST_F(CloudPolicyControllerTest, InvalidToken) { |
| 178 data_store_->SetupForTesting("device_token", "device_id", | 178 data_store_->SetupForTesting("device_token", "device_id", |
| 179 "standup@ten.am", "auth", true); | 179 "standup@ten.am", "auth", true); |
| 180 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 180 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 181 MockDeviceManagementBackendFailPolicy( | 181 MockDeviceManagementBackendFailPolicy( |
| 182 DeviceManagementBackend::kErrorServiceManagementTokenInvalid)); | 182 DeviceManagementBackend::kErrorServiceManagementTokenInvalid)); |
| 183 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1); | 183 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1); |
| 184 CreateNewController(); | 184 CreateNewController(); |
| 185 loop_.RunAllPending(); | 185 loop_.RunAllPending(); |
| 186 } | 186 } |
| 187 | 187 |
| 188 // If the backend reports that the device is unknown to the server, the | 188 // If the backend reports that the device is unknown to the server, the |
| 189 // controller should instruct the token fetcher to fetch a new token. | 189 // controller should instruct the token fetcher to fetch a new token. |
| 190 TEST_F(CloudPolicyControllerTest, DeviceNotFound) { | 190 TEST_F(CloudPolicyControllerTest, DeviceNotFound) { |
| 191 data_store_->SetupForTesting("device_token", "device_id", | 191 data_store_->SetupForTesting("device_token", "device_id", |
| 192 "me@you.com", "auth", true); | 192 "me@you.com", "auth", true); |
| 193 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 193 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 194 MockDeviceManagementBackendFailPolicy( | 194 MockDeviceManagementBackendFailPolicy( |
| 195 DeviceManagementBackend::kErrorServiceDeviceNotFound)); | 195 DeviceManagementBackend::kErrorServiceDeviceNotFound)); |
| 196 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1); | 196 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1); |
| 197 CreateNewController(); | 197 CreateNewController(); |
| 198 loop_.RunAllPending(); | 198 loop_.RunAllPending(); |
| 199 } | 199 } |
| 200 | 200 |
| 201 // If the backend reports that the device is no longer managed, the controller | 201 // If the backend reports that the device is no longer managed, the controller |
| 202 // should instruct the token fetcher to fetch a new token (which will in turn | 202 // should instruct the token fetcher to fetch a new token (which will in turn |
| 203 // set and persist the correct 'unmanaged' state). | 203 // set and persist the correct 'unmanaged' state). |
| 204 TEST_F(CloudPolicyControllerTest, NoLongerManaged) { | 204 TEST_F(CloudPolicyControllerTest, NoLongerManaged) { |
| 205 data_store_->SetupForTesting("device_token", "device_id", | 205 data_store_->SetupForTesting("device_token", "device_id", |
| 206 "who@what.com", "auth", true); | 206 "who@what.com", "auth", true); |
| 207 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 207 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _)).WillOnce( |
| 208 MockDeviceManagementBackendFailPolicy( | 208 MockDeviceManagementBackendFailPolicy( |
| 209 DeviceManagementBackend::kErrorServiceManagementNotSupported)); | 209 DeviceManagementBackend::kErrorServiceManagementNotSupported)); |
| 210 EXPECT_CALL(*token_fetcher_.get(), SetUnmanagedState()).Times(1); | 210 EXPECT_CALL(*token_fetcher_.get(), SetUnmanagedState()).Times(1); |
| 211 CreateNewController(); | 211 CreateNewController(); |
| 212 loop_.RunAllPending(); | 212 loop_.RunAllPending(); |
| 213 } | 213 } |
| 214 | 214 |
| 215 } // namespace policy | 215 } // namespace policy |
| OLD | NEW |