| 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/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "chrome/browser/policy/cloud_policy_data_store.h" | 10 #include "chrome/browser/policy/cloud_policy_data_store.h" |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 content::TestBrowserThread file_thread_; | 110 content::TestBrowserThread file_thread_; |
| 111 | 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(CloudPolicyControllerTest); | 112 DISALLOW_COPY_AND_ASSIGN(CloudPolicyControllerTest); |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 // If a device token is present when the controller starts up, it should | 115 // If a device token is present when the controller starts up, it should |
| 116 // fetch and apply policy. | 116 // fetch and apply policy. |
| 117 TEST_F(CloudPolicyControllerTest, StartupWithDeviceToken) { | 117 TEST_F(CloudPolicyControllerTest, StartupWithDeviceToken) { |
| 118 data_store_->SetupForTesting("fake_device_token", "device_id", "", "", | 118 data_store_->SetupForTesting("fake_device_token", "device_id", "", "", |
| 119 true); | 119 true); |
| 120 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce(DoAll( | 120 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _, _)).WillOnce(DoAll( |
| 121 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), | 121 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), |
| 122 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); | 122 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); |
| 123 CreateNewController(); | 123 CreateNewController(); |
| 124 loop_.RunAllPending(); | 124 loop_.RunAllPending(); |
| 125 ExpectHasSpdyPolicy(); | 125 ExpectHasSpdyPolicy(); |
| 126 } | 126 } |
| 127 | 127 |
| 128 // If no device token is present when the controller starts up, it should | 128 // If no device token is present when the controller starts up, it should |
| 129 // instruct the token_fetcher_ to fetch one. | 129 // instruct the token_fetcher_ to fetch one. |
| 130 TEST_F(CloudPolicyControllerTest, StartupWithoutDeviceToken) { | 130 TEST_F(CloudPolicyControllerTest, StartupWithoutDeviceToken) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 146 } | 146 } |
| 147 | 147 |
| 148 // After policy has been fetched successfully, a new fetch should be triggered | 148 // After policy has been fetched successfully, a new fetch should be triggered |
| 149 // after the refresh interval has timed out. | 149 // after the refresh interval has timed out. |
| 150 TEST_F(CloudPolicyControllerTest, RefreshAfterSuccessfulPolicy) { | 150 TEST_F(CloudPolicyControllerTest, RefreshAfterSuccessfulPolicy) { |
| 151 data_store_->SetupForTesting("device_token", "device_id", | 151 data_store_->SetupForTesting("device_token", "device_id", |
| 152 "DannoHelperDelegate@b.com", | 152 "DannoHelperDelegate@b.com", |
| 153 "auth_token", true); | 153 "auth_token", true); |
| 154 { | 154 { |
| 155 InSequence s; | 155 InSequence s; |
| 156 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 156 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _, _)).WillOnce( |
| 157 MockDeviceManagementBackendSucceedSpdyCloudPolicy()); | 157 MockDeviceManagementBackendSucceedSpdyCloudPolicy()); |
| 158 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce(DoAll( | 158 EXPECT_CALL(backend_, |
| 159 ProcessPolicyRequest(_, _, _, _, _, _)).WillOnce(DoAll( |
| 159 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), | 160 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), |
| 160 MockDeviceManagementBackendFailPolicy( | 161 MockDeviceManagementBackendFailPolicy( |
| 161 DeviceManagementBackend::kErrorRequestFailed))); | 162 DeviceManagementBackend::kErrorRequestFailed))); |
| 162 } | 163 } |
| 163 CreateNewController(); | 164 CreateNewController(); |
| 164 loop_.RunAllPending(); | 165 loop_.RunAllPending(); |
| 165 ExpectHasSpdyPolicy(); | 166 ExpectHasSpdyPolicy(); |
| 166 } | 167 } |
| 167 | 168 |
| 168 // If policy fetching failed, it should be retried. | 169 // If policy fetching failed, it should be retried. |
| 169 TEST_F(CloudPolicyControllerTest, RefreshAfterError) { | 170 TEST_F(CloudPolicyControllerTest, RefreshAfterError) { |
| 170 data_store_->SetupForTesting("device_token", "device_id", | 171 data_store_->SetupForTesting("device_token", "device_id", |
| 171 "DannoHelperDelegateImpl@b.com", | 172 "DannoHelperDelegateImpl@b.com", |
| 172 "auth_token", true); | 173 "auth_token", true); |
| 173 { | 174 { |
| 174 InSequence s; | 175 InSequence s; |
| 175 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 176 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _, _)).WillOnce( |
| 176 MockDeviceManagementBackendFailPolicy( | 177 MockDeviceManagementBackendFailPolicy( |
| 177 DeviceManagementBackend::kErrorRequestFailed)); | 178 DeviceManagementBackend::kErrorRequestFailed)); |
| 178 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce(DoAll( | 179 EXPECT_CALL(backend_, |
| 180 ProcessPolicyRequest(_, _, _, _, _, _)).WillOnce(DoAll( |
| 179 InvokeWithoutArgs(this, | 181 InvokeWithoutArgs(this, |
| 180 &CloudPolicyControllerTest::StopMessageLoop), | 182 &CloudPolicyControllerTest::StopMessageLoop), |
| 181 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); | 183 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); |
| 182 } | 184 } |
| 183 CreateNewController(); | 185 CreateNewController(); |
| 184 loop_.RunAllPending(); | 186 loop_.RunAllPending(); |
| 185 ExpectHasSpdyPolicy(); | 187 ExpectHasSpdyPolicy(); |
| 186 } | 188 } |
| 187 | 189 |
| 188 // If the backend reports that the device token was invalid, the controller | 190 // If the backend reports that the device token was invalid, the controller |
| 189 // should instruct the token fetcher to fetch a new token. | 191 // should instruct the token fetcher to fetch a new token. |
| 190 TEST_F(CloudPolicyControllerTest, InvalidToken) { | 192 TEST_F(CloudPolicyControllerTest, InvalidToken) { |
| 191 data_store_->SetupForTesting("device_token", "device_id", | 193 data_store_->SetupForTesting("device_token", "device_id", |
| 192 "standup@ten.am", "auth", true); | 194 "standup@ten.am", "auth", true); |
| 193 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 195 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _, _)).WillOnce( |
| 194 MockDeviceManagementBackendFailPolicy( | 196 MockDeviceManagementBackendFailPolicy( |
| 195 DeviceManagementBackend::kErrorServiceManagementTokenInvalid)); | 197 DeviceManagementBackend::kErrorServiceManagementTokenInvalid)); |
| 196 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1); | 198 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1); |
| 197 CreateNewController(); | 199 CreateNewController(); |
| 198 loop_.RunAllPending(); | 200 loop_.RunAllPending(); |
| 199 } | 201 } |
| 200 | 202 |
| 201 // If the backend reports that the device is unknown to the server, the | 203 // If the backend reports that the device is unknown to the server, the |
| 202 // controller should instruct the token fetcher to fetch a new token. | 204 // controller should instruct the token fetcher to fetch a new token. |
| 203 TEST_F(CloudPolicyControllerTest, DeviceNotFound) { | 205 TEST_F(CloudPolicyControllerTest, DeviceNotFound) { |
| 204 data_store_->SetupForTesting("device_token", "device_id", | 206 data_store_->SetupForTesting("device_token", "device_id", |
| 205 "me@you.com", "auth", true); | 207 "me@you.com", "auth", true); |
| 206 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 208 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _, _)).WillOnce( |
| 207 MockDeviceManagementBackendFailPolicy( | 209 MockDeviceManagementBackendFailPolicy( |
| 208 DeviceManagementBackend::kErrorServiceDeviceNotFound)); | 210 DeviceManagementBackend::kErrorServiceDeviceNotFound)); |
| 209 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1); | 211 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1); |
| 210 CreateNewController(); | 212 CreateNewController(); |
| 211 loop_.RunAllPending(); | 213 loop_.RunAllPending(); |
| 212 } | 214 } |
| 213 | 215 |
| 214 // If the backend reports that the device-id is already existing, the | 216 // If the backend reports that the device-id is already existing, the |
| 215 // controller should instruct the token fetcher to fetch a new token. | 217 // controller should instruct the token fetcher to fetch a new token. |
| 216 TEST_F(CloudPolicyControllerTest, DeviceIdConflict) { | 218 TEST_F(CloudPolicyControllerTest, DeviceIdConflict) { |
| 217 data_store_->SetupForTesting("device_token", "device_id", | 219 data_store_->SetupForTesting("device_token", "device_id", |
| 218 "me@you.com", "auth", true); | 220 "me@you.com", "auth", true); |
| 219 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 221 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _, _)).WillOnce( |
| 220 MockDeviceManagementBackendFailPolicy( | 222 MockDeviceManagementBackendFailPolicy( |
| 221 DeviceManagementBackend::kErrorServiceDeviceIdConflict)); | 223 DeviceManagementBackend::kErrorServiceDeviceIdConflict)); |
| 222 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1); | 224 EXPECT_CALL(*token_fetcher_.get(), FetchToken()).Times(1); |
| 223 CreateNewController(); | 225 CreateNewController(); |
| 224 loop_.RunAllPending(); | 226 loop_.RunAllPending(); |
| 225 } | 227 } |
| 226 | 228 |
| 227 // If the backend reports that the device is no longer managed, the controller | 229 // If the backend reports that the device is no longer managed, the controller |
| 228 // should instruct the token fetcher to fetch a new token (which will in turn | 230 // should instruct the token fetcher to fetch a new token (which will in turn |
| 229 // set and persist the correct 'unmanaged' state). | 231 // set and persist the correct 'unmanaged' state). |
| 230 TEST_F(CloudPolicyControllerTest, NoLongerManaged) { | 232 TEST_F(CloudPolicyControllerTest, NoLongerManaged) { |
| 231 data_store_->SetupForTesting("device_token", "device_id", | 233 data_store_->SetupForTesting("device_token", "device_id", |
| 232 "who@what.com", "auth", true); | 234 "who@what.com", "auth", true); |
| 233 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 235 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _, _)).WillOnce( |
| 234 MockDeviceManagementBackendFailPolicy( | 236 MockDeviceManagementBackendFailPolicy( |
| 235 DeviceManagementBackend::kErrorServiceManagementNotSupported)); | 237 DeviceManagementBackend::kErrorServiceManagementNotSupported)); |
| 236 EXPECT_CALL(*token_fetcher_.get(), SetUnmanagedState()).Times(1); | 238 EXPECT_CALL(*token_fetcher_.get(), SetUnmanagedState()).Times(1); |
| 237 CreateNewController(); | 239 CreateNewController(); |
| 238 loop_.RunAllPending(); | 240 loop_.RunAllPending(); |
| 239 } | 241 } |
| 240 | 242 |
| 241 // If the backend reports that the device has invalid serial number, the | 243 // If the backend reports that the device has invalid serial number, the |
| 242 // controller should instruct the token fetcher not to fetch a new token | 244 // controller should instruct the token fetcher not to fetch a new token |
| 243 // (which will in turn set and persist the correct 'sn invalid' state). | 245 // (which will in turn set and persist the correct 'sn invalid' state). |
| 244 TEST_F(CloudPolicyControllerTest, InvalidSerialNumber) { | 246 TEST_F(CloudPolicyControllerTest, InvalidSerialNumber) { |
| 245 data_store_->SetupForTesting("device_token", "device_id", | 247 data_store_->SetupForTesting("device_token", "device_id", |
| 246 "who@what.com", "auth", true); | 248 "who@what.com", "auth", true); |
| 247 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce( | 249 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _, _)).WillOnce( |
| 248 MockDeviceManagementBackendFailPolicy( | 250 MockDeviceManagementBackendFailPolicy( |
| 249 DeviceManagementBackend::kErrorServiceInvalidSerialNumber)); | 251 DeviceManagementBackend::kErrorServiceInvalidSerialNumber)); |
| 250 EXPECT_CALL(*token_fetcher_.get(), SetSerialNumberInvalidState()).Times(1); | 252 EXPECT_CALL(*token_fetcher_.get(), SetSerialNumberInvalidState()).Times(1); |
| 251 CreateNewController(); | 253 CreateNewController(); |
| 252 loop_.RunAllPending(); | 254 loop_.RunAllPending(); |
| 253 } | 255 } |
| 254 | 256 |
| 255 TEST_F(CloudPolicyControllerTest, DontSetFetchingDoneWithoutTokens) { | 257 TEST_F(CloudPolicyControllerTest, DontSetFetchingDoneWithoutTokens) { |
| 256 CreateNewWaitingCache(); | 258 CreateNewWaitingCache(); |
| 257 CreateNewController(); | 259 CreateNewController(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 279 loop_.RunAllPending(); | 281 loop_.RunAllPending(); |
| 280 // User is in an unmanaged domain. | 282 // User is in an unmanaged domain. |
| 281 EXPECT_TRUE(cache_->IsReady()); | 283 EXPECT_TRUE(cache_->IsReady()); |
| 282 EXPECT_TRUE(cache_->last_policy_refresh_time().is_null()); | 284 EXPECT_TRUE(cache_->last_policy_refresh_time().is_null()); |
| 283 } | 285 } |
| 284 | 286 |
| 285 TEST_F(CloudPolicyControllerTest, SetFetchingDoneAfterPolicyFetch) { | 287 TEST_F(CloudPolicyControllerTest, SetFetchingDoneAfterPolicyFetch) { |
| 286 CreateNewWaitingCache(); | 288 CreateNewWaitingCache(); |
| 287 data_store_->SetupForTesting("device_token", "device_id", | 289 data_store_->SetupForTesting("device_token", "device_id", |
| 288 "user@enterprise.com", "auth", true); | 290 "user@enterprise.com", "auth", true); |
| 289 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce(DoAll( | 291 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _, _)).WillOnce(DoAll( |
| 290 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), | 292 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), |
| 291 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); | 293 MockDeviceManagementBackendSucceedSpdyCloudPolicy())); |
| 292 CreateNewController(); | 294 CreateNewController(); |
| 293 loop_.RunAllPending(); | 295 loop_.RunAllPending(); |
| 294 EXPECT_TRUE(cache_->IsReady()); | 296 EXPECT_TRUE(cache_->IsReady()); |
| 295 EXPECT_FALSE(cache_->last_policy_refresh_time().is_null()); | 297 EXPECT_FALSE(cache_->last_policy_refresh_time().is_null()); |
| 296 } | 298 } |
| 297 | 299 |
| 298 TEST_F(CloudPolicyControllerTest, SetFetchingDoneAfterPolicyFetchFails) { | 300 TEST_F(CloudPolicyControllerTest, SetFetchingDoneAfterPolicyFetchFails) { |
| 299 CreateNewWaitingCache(); | 301 CreateNewWaitingCache(); |
| 300 data_store_->SetupForTesting("device_token", "device_id", | 302 data_store_->SetupForTesting("device_token", "device_id", |
| 301 "user@enterprise.com", "auth", true); | 303 "user@enterprise.com", "auth", true); |
| 302 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _)).WillOnce(DoAll( | 304 EXPECT_CALL(backend_, ProcessPolicyRequest(_, _, _, _, _, _)).WillOnce(DoAll( |
| 303 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), | 305 InvokeWithoutArgs(this, &CloudPolicyControllerTest::StopMessageLoop), |
| 304 MockDeviceManagementBackendFailPolicy( | 306 MockDeviceManagementBackendFailPolicy( |
| 305 DeviceManagementBackend::kErrorRequestFailed))); | 307 DeviceManagementBackend::kErrorRequestFailed))); |
| 306 CreateNewController(); | 308 CreateNewController(); |
| 307 loop_.RunAllPending(); | 309 loop_.RunAllPending(); |
| 308 EXPECT_TRUE(cache_->IsReady()); | 310 EXPECT_TRUE(cache_->IsReady()); |
| 309 EXPECT_TRUE(cache_->last_policy_refresh_time().is_null()); | 311 EXPECT_TRUE(cache_->last_policy_refresh_time().is_null()); |
| 310 } | 312 } |
| 311 | 313 |
| 312 } // namespace policy | 314 } // namespace policy |
| OLD | NEW |