OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/chromeos/settings/device_oauth2_token_service.h" | 5 #include "chrome/browser/chromeos/settings/device_oauth2_token_service.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/prefs/testing_pref_service.h" | 8 #include "base/prefs/testing_pref_service.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
11 #include "chrome/browser/chromeos/policy/device_policy_builder.h" | 11 #include "chrome/browser/chromeos/policy/device_policy_builder.h" |
12 #include "chrome/browser/chromeos/settings/cros_settings.h" | 12 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 13 #include "chrome/browser/chromeos/settings/device_oauth2_token_service_delegate.
h" |
13 #include "chrome/browser/chromeos/settings/device_settings_service.h" | 14 #include "chrome/browser/chromeos/settings/device_settings_service.h" |
14 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" | 15 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" |
15 #include "chrome/browser/chromeos/settings/token_encryptor.h" | 16 #include "chrome/browser/chromeos/settings/token_encryptor.h" |
16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
17 #include "chrome/test/base/scoped_testing_local_state.h" | 18 #include "chrome/test/base/scoped_testing_local_state.h" |
18 #include "chrome/test/base/testing_browser_process.h" | 19 #include "chrome/test/base/testing_browser_process.h" |
19 #include "chromeos/cryptohome/system_salt_getter.h" | 20 #include "chromeos/cryptohome/system_salt_getter.h" |
20 #include "chromeos/dbus/dbus_thread_manager.h" | 21 #include "chromeos/dbus/dbus_thread_manager.h" |
21 #include "chromeos/dbus/fake_cryptohome_client.h" | 22 #include "chromeos/dbus/fake_cryptohome_client.h" |
22 #include "components/ownership/mock_owner_key_util.h" | 23 #include "components/ownership/mock_owner_key_util.h" |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 TestingBrowserProcess::GetGlobal()->SetBrowserPolicyConnector(NULL); | 122 TestingBrowserProcess::GetGlobal()->SetBrowserPolicyConnector(NULL); |
122 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 123 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
123 DeviceSettingsService::Get()->UnsetSessionManager(); | 124 DeviceSettingsService::Get()->UnsetSessionManager(); |
124 DeviceSettingsService::Shutdown(); | 125 DeviceSettingsService::Shutdown(); |
125 SystemSaltGetter::Shutdown(); | 126 SystemSaltGetter::Shutdown(); |
126 DBusThreadManager::Shutdown(); | 127 DBusThreadManager::Shutdown(); |
127 base::RunLoop().RunUntilIdle(); | 128 base::RunLoop().RunUntilIdle(); |
128 } | 129 } |
129 | 130 |
130 void CreateService() { | 131 void CreateService() { |
131 oauth2_service_.reset(new DeviceOAuth2TokenService( | 132 DeviceOAuth2TokenServiceDelegate* delegate = |
132 request_context_getter_.get(), scoped_testing_local_state_.Get())); | 133 new DeviceOAuth2TokenServiceDelegate(request_context_getter_.get(), |
133 oauth2_service_->max_refresh_token_validation_retries_ = 0; | 134 scoped_testing_local_state_.Get()); |
| 135 delegate->max_refresh_token_validation_retries_ = 0; |
| 136 oauth2_service_.reset(new DeviceOAuth2TokenService(delegate)); |
134 oauth2_service_->set_max_authorization_token_fetch_retries_for_testing(0); | 137 oauth2_service_->set_max_authorization_token_fetch_retries_for_testing(0); |
135 } | 138 } |
136 | 139 |
137 // Utility method to set a value in Local State for the device refresh token | 140 // Utility method to set a value in Local State for the device refresh token |
138 // (it must have a non-empty value or it won't be used). | 141 // (it must have a non-empty value or it won't be used). |
139 void SetDeviceRefreshTokenInLocalState(const std::string& refresh_token) { | 142 void SetDeviceRefreshTokenInLocalState(const std::string& refresh_token) { |
140 scoped_testing_local_state_.Get()->SetUserPref( | 143 scoped_testing_local_state_.Get()->SetUserPref( |
141 prefs::kDeviceRobotAnyApiRefreshToken, | 144 prefs::kDeviceRobotAnyApiRefreshToken, |
142 new base::StringValue(refresh_token)); | 145 new base::StringValue(refresh_token)); |
143 } | 146 } |
144 | 147 |
145 std::string GetValidTokenInfoResponse(const std::string email) { | 148 std::string GetValidTokenInfoResponse(const std::string email) { |
146 return "{ \"email\": \"" + email + "\"," | 149 return "{ \"email\": \"" + email + "\"," |
147 " \"user_id\": \"1234567890\" }"; | 150 " \"user_id\": \"1234567890\" }"; |
148 } | 151 } |
149 | 152 |
150 bool RefreshTokenIsAvailable() { | 153 bool RefreshTokenIsAvailable() { |
151 return oauth2_service_->RefreshTokenIsAvailable( | 154 return oauth2_service_->RefreshTokenIsAvailable( |
152 oauth2_service_->GetRobotAccountId()); | 155 oauth2_service_->GetRobotAccountId()); |
153 } | 156 } |
154 | 157 |
155 std::string GetRefreshToken() { | 158 std::string GetRefreshToken() { |
156 if (!RefreshTokenIsAvailable()) | 159 if (!RefreshTokenIsAvailable()) |
157 return std::string(); | 160 return std::string(); |
158 | 161 |
159 return oauth2_service_->GetRefreshToken( | 162 return static_cast<DeviceOAuth2TokenServiceDelegate*>( |
160 oauth2_service_->GetRobotAccountId()); | 163 oauth2_service_->GetDelegate()) |
| 164 ->GetRefreshToken(oauth2_service_->GetRobotAccountId()); |
161 } | 165 } |
162 | 166 |
163 // A utility method to return fake URL results, for testing the refresh token | 167 // A utility method to return fake URL results, for testing the refresh token |
164 // validation logic. For a successful validation attempt, this method will be | 168 // validation logic. For a successful validation attempt, this method will be |
165 // called three times for the steps listed below (steps 1 and 2 happen in | 169 // called three times for the steps listed below (steps 1 and 2 happen in |
166 // parallel). | 170 // parallel). |
167 // | 171 // |
168 // Step 1a: fetch the access token for the tokeninfo API. | 172 // Step 1a: fetch the access token for the tokeninfo API. |
169 // Step 1b: call the tokeninfo API. | 173 // Step 1b: call the tokeninfo API. |
170 // Step 2: Fetch the access token for the requested scope | 174 // Step 2: Fetch the access token for the requested scope |
(...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 // Also make the robot account ID available. Verify that the token is | 470 // Also make the robot account ID available. Verify that the token is |
467 // announced now. | 471 // announced now. |
468 EXPECT_CALL(observer, OnRefreshTokenAvailable("robot@example.com")); | 472 EXPECT_CALL(observer, OnRefreshTokenAvailable("robot@example.com")); |
469 SetRobotAccountId("robot@example.com"); | 473 SetRobotAccountId("robot@example.com"); |
470 testing::Mock::VerifyAndClearExpectations(&observer); | 474 testing::Mock::VerifyAndClearExpectations(&observer); |
471 | 475 |
472 oauth2_service_->RemoveObserver(&observer); | 476 oauth2_service_->RemoveObserver(&observer); |
473 } | 477 } |
474 | 478 |
475 } // namespace chromeos | 479 } // namespace chromeos |
OLD | NEW |