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 oauth2_service_.reset( |
132 request_context_getter_.get(), scoped_testing_local_state_.Get())); | 133 new DeviceOAuth2TokenService(new DeviceOAuth2TokenServiceDelegate( |
133 oauth2_service_->max_refresh_token_validation_retries_ = 0; | 134 request_context_getter_.get(), scoped_testing_local_state_.Get()))); |
135 static_cast<DeviceOAuth2TokenServiceDelegate*>( | |
Mattias Nissler (ping if slow)
2015/07/01 12:34:14
This is much cleaner if you just create the Device
gogerald1
2015/07/01 17:58:42
Done.
| |
136 oauth2_service_->GetDelegate())->max_refresh_token_validation_retries_ = | |
137 0; | |
134 oauth2_service_->set_max_authorization_token_fetch_retries_for_testing(0); | 138 oauth2_service_->set_max_authorization_token_fetch_retries_for_testing(0); |
135 } | 139 } |
136 | 140 |
137 // Utility method to set a value in Local State for the device refresh token | 141 // 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). | 142 // (it must have a non-empty value or it won't be used). |
139 void SetDeviceRefreshTokenInLocalState(const std::string& refresh_token) { | 143 void SetDeviceRefreshTokenInLocalState(const std::string& refresh_token) { |
140 scoped_testing_local_state_.Get()->SetUserPref( | 144 scoped_testing_local_state_.Get()->SetUserPref( |
141 prefs::kDeviceRobotAnyApiRefreshToken, | 145 prefs::kDeviceRobotAnyApiRefreshToken, |
142 new base::StringValue(refresh_token)); | 146 new base::StringValue(refresh_token)); |
143 } | 147 } |
144 | 148 |
145 std::string GetValidTokenInfoResponse(const std::string email) { | 149 std::string GetValidTokenInfoResponse(const std::string email) { |
146 return "{ \"email\": \"" + email + "\"," | 150 return "{ \"email\": \"" + email + "\"," |
147 " \"user_id\": \"1234567890\" }"; | 151 " \"user_id\": \"1234567890\" }"; |
148 } | 152 } |
149 | 153 |
150 bool RefreshTokenIsAvailable() { | 154 bool RefreshTokenIsAvailable() { |
151 return oauth2_service_->RefreshTokenIsAvailable( | 155 return oauth2_service_->RefreshTokenIsAvailable( |
152 oauth2_service_->GetRobotAccountId()); | 156 oauth2_service_->GetRobotAccountId()); |
153 } | 157 } |
154 | 158 |
155 std::string GetRefreshToken() { | 159 std::string GetRefreshToken() { |
156 if (!RefreshTokenIsAvailable()) | 160 if (!RefreshTokenIsAvailable()) |
157 return std::string(); | 161 return std::string(); |
158 | 162 |
159 return oauth2_service_->GetRefreshToken( | 163 return static_cast<DeviceOAuth2TokenServiceDelegate*>( |
160 oauth2_service_->GetRobotAccountId()); | 164 oauth2_service_->GetDelegate()) |
165 ->GetRefreshToken(oauth2_service_->GetRobotAccountId()); | |
161 } | 166 } |
162 | 167 |
163 // A utility method to return fake URL results, for testing the refresh token | 168 // 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 | 169 // 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 | 170 // called three times for the steps listed below (steps 1 and 2 happen in |
166 // parallel). | 171 // parallel). |
167 // | 172 // |
168 // Step 1a: fetch the access token for the tokeninfo API. | 173 // Step 1a: fetch the access token for the tokeninfo API. |
169 // Step 1b: call the tokeninfo API. | 174 // Step 1b: call the tokeninfo API. |
170 // Step 2: Fetch the access token for the requested scope | 175 // 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 | 471 // Also make the robot account ID available. Verify that the token is |
467 // announced now. | 472 // announced now. |
468 EXPECT_CALL(observer, OnRefreshTokenAvailable("robot@example.com")); | 473 EXPECT_CALL(observer, OnRefreshTokenAvailable("robot@example.com")); |
469 SetRobotAccountId("robot@example.com"); | 474 SetRobotAccountId("robot@example.com"); |
470 testing::Mock::VerifyAndClearExpectations(&observer); | 475 testing::Mock::VerifyAndClearExpectations(&observer); |
471 | 476 |
472 oauth2_service_->RemoveObserver(&observer); | 477 oauth2_service_->RemoveObserver(&observer); |
473 } | 478 } |
474 | 479 |
475 } // namespace chromeos | 480 } // namespace chromeos |
OLD | NEW |