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 "base/macros.h" | 5 #include "base/macros.h" |
6 #include "base/strings/stringprintf.h" | 6 #include "base/strings/stringprintf.h" |
7 #include "base/threading/platform_thread.h" | 7 #include "base/threading/platform_thread.h" |
8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 9 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
10 #include "chrome/browser/sync/test/integration/bookmarks_helper.h" | 10 #include "chrome/browser/sync/test/integration/bookmarks_helper.h" |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 // Helper function that adds a bookmark and waits for either an auth error, or | 79 // Helper function that adds a bookmark and waits for either an auth error, or |
80 // for the bookmark to be committed. Returns true if it detects an auth | 80 // for the bookmark to be committed. Returns true if it detects an auth |
81 // error, false if the bookmark is committed successfully. | 81 // error, false if the bookmark is committed successfully. |
82 bool AttemptToTriggerAuthError() { | 82 bool AttemptToTriggerAuthError() { |
83 int bookmark_index = GetNextBookmarkIndex(); | 83 int bookmark_index = GetNextBookmarkIndex(); |
84 std::string title = base::StringPrintf("Bookmark %d", bookmark_index); | 84 std::string title = base::StringPrintf("Bookmark %d", bookmark_index); |
85 GURL url = GURL(base::StringPrintf("http://www.foo%d.com", bookmark_index)); | 85 GURL url = GURL(base::StringPrintf("http://www.foo%d.com", bookmark_index)); |
86 EXPECT_TRUE(AddURL(0, title, url) != NULL); | 86 EXPECT_TRUE(AddURL(0, title, url) != NULL); |
87 | 87 |
88 // Run until the bookmark is committed or an auth error is encountered. | 88 // Run until the bookmark is committed or an auth error is encountered. |
89 TestForAuthError checker_(GetSyncService((0))); | 89 TestForAuthError checker_(GetSyncService(0)); |
90 checker_.Wait(); | 90 checker_.Wait(); |
91 | 91 |
92 GoogleServiceAuthError oauth_error = | 92 GoogleServiceAuthError oauth_error = |
93 GetSyncService((0))->GetSyncTokenStatus().last_get_token_error; | 93 GetSyncService(0)->GetSyncTokenStatus().last_get_token_error; |
94 | 94 |
95 return oauth_error.state() != GoogleServiceAuthError::NONE; | 95 return oauth_error.state() != GoogleServiceAuthError::NONE; |
96 } | 96 } |
97 | 97 |
98 void DisableTokenFetchRetries() { | 98 void DisableTokenFetchRetries() { |
99 // If ProfileSyncService observes a transient error like SERVICE_UNAVAILABLE | 99 // If ProfileSyncService observes a transient error like SERVICE_UNAVAILABLE |
100 // or CONNECTION_FAILED, this means the OAuth2TokenService has given up | 100 // or CONNECTION_FAILED, this means the OAuth2TokenService has given up |
101 // trying to reach Gaia. In practice, OA2TS retries a fixed number of times, | 101 // trying to reach Gaia. In practice, OA2TS retries a fixed number of times, |
102 // but the count is transparent to PSS. | 102 // but the count is transparent to PSS. |
103 // Override the max retry count in TokenService so that we instantly trigger | 103 // Override the max retry count in TokenService so that we instantly trigger |
(...skipping 30 matching lines...) Expand all Loading... |
134 // HTTP_INTERNAL_SERVER_ERROR (500) errors. | 134 // HTTP_INTERNAL_SERVER_ERROR (500) errors. |
135 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnInternalServerError500) { | 135 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnInternalServerError500) { |
136 ASSERT_TRUE(SetupSync()); | 136 ASSERT_TRUE(SetupSync()); |
137 ASSERT_FALSE(AttemptToTriggerAuthError()); | 137 ASSERT_FALSE(AttemptToTriggerAuthError()); |
138 GetFakeServer()->SetUnauthenticated(); | 138 GetFakeServer()->SetUnauthenticated(); |
139 DisableTokenFetchRetries(); | 139 DisableTokenFetchRetries(); |
140 SetOAuth2TokenResponse(kValidOAuth2Token, | 140 SetOAuth2TokenResponse(kValidOAuth2Token, |
141 net::HTTP_INTERNAL_SERVER_ERROR, | 141 net::HTTP_INTERNAL_SERVER_ERROR, |
142 net::URLRequestStatus::SUCCESS); | 142 net::URLRequestStatus::SUCCESS); |
143 ASSERT_TRUE(AttemptToTriggerAuthError()); | 143 ASSERT_TRUE(AttemptToTriggerAuthError()); |
144 ASSERT_TRUE( | 144 ASSERT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest()); |
145 GetSyncService((0))->IsRetryingAccessTokenFetchForTest()); | |
146 } | 145 } |
147 | 146 |
148 // Verify that ProfileSyncService continues trying to fetch access tokens | 147 // Verify that ProfileSyncService continues trying to fetch access tokens |
149 // when OAuth2TokenService has encountered more than a fixed number of | 148 // when OAuth2TokenService has encountered more than a fixed number of |
150 // HTTP_FORBIDDEN (403) errors. | 149 // HTTP_FORBIDDEN (403) errors. |
151 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnHttpForbidden403) { | 150 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnHttpForbidden403) { |
152 ASSERT_TRUE(SetupSync()); | 151 ASSERT_TRUE(SetupSync()); |
153 ASSERT_FALSE(AttemptToTriggerAuthError()); | 152 ASSERT_FALSE(AttemptToTriggerAuthError()); |
154 GetFakeServer()->SetUnauthenticated(); | 153 GetFakeServer()->SetUnauthenticated(); |
155 DisableTokenFetchRetries(); | 154 DisableTokenFetchRetries(); |
156 SetOAuth2TokenResponse(kEmptyOAuth2Token, | 155 SetOAuth2TokenResponse(kEmptyOAuth2Token, |
157 net::HTTP_FORBIDDEN, | 156 net::HTTP_FORBIDDEN, |
158 net::URLRequestStatus::SUCCESS); | 157 net::URLRequestStatus::SUCCESS); |
159 ASSERT_TRUE(AttemptToTriggerAuthError()); | 158 ASSERT_TRUE(AttemptToTriggerAuthError()); |
160 ASSERT_TRUE( | 159 ASSERT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest()); |
161 GetSyncService((0))->IsRetryingAccessTokenFetchForTest()); | |
162 } | 160 } |
163 | 161 |
164 // Verify that ProfileSyncService continues trying to fetch access tokens | 162 // Verify that ProfileSyncService continues trying to fetch access tokens |
165 // when OAuth2TokenService has encountered a URLRequestStatus of FAILED. | 163 // when OAuth2TokenService has encountered a URLRequestStatus of FAILED. |
166 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnRequestFailed) { | 164 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnRequestFailed) { |
167 ASSERT_TRUE(SetupSync()); | 165 ASSERT_TRUE(SetupSync()); |
168 ASSERT_FALSE(AttemptToTriggerAuthError()); | 166 ASSERT_FALSE(AttemptToTriggerAuthError()); |
169 GetFakeServer()->SetUnauthenticated(); | 167 GetFakeServer()->SetUnauthenticated(); |
170 DisableTokenFetchRetries(); | 168 DisableTokenFetchRetries(); |
171 SetOAuth2TokenResponse(kEmptyOAuth2Token, | 169 SetOAuth2TokenResponse(kEmptyOAuth2Token, |
172 net::HTTP_INTERNAL_SERVER_ERROR, | 170 net::HTTP_INTERNAL_SERVER_ERROR, |
173 net::URLRequestStatus::FAILED); | 171 net::URLRequestStatus::FAILED); |
174 ASSERT_TRUE(AttemptToTriggerAuthError()); | 172 ASSERT_TRUE(AttemptToTriggerAuthError()); |
175 ASSERT_TRUE( | 173 ASSERT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest()); |
176 GetSyncService((0))->IsRetryingAccessTokenFetchForTest()); | |
177 } | 174 } |
178 | 175 |
179 // Verify that ProfileSyncService continues trying to fetch access tokens | 176 // Verify that ProfileSyncService continues trying to fetch access tokens |
180 // when OAuth2TokenService receives a malformed token. | 177 // when OAuth2TokenService receives a malformed token. |
181 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnMalformedToken) { | 178 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnMalformedToken) { |
182 ASSERT_TRUE(SetupSync()); | 179 ASSERT_TRUE(SetupSync()); |
183 ASSERT_FALSE(AttemptToTriggerAuthError()); | 180 ASSERT_FALSE(AttemptToTriggerAuthError()); |
184 GetFakeServer()->SetUnauthenticated(); | 181 GetFakeServer()->SetUnauthenticated(); |
185 DisableTokenFetchRetries(); | 182 DisableTokenFetchRetries(); |
186 SetOAuth2TokenResponse(kMalformedOAuth2Token, | 183 SetOAuth2TokenResponse(kMalformedOAuth2Token, |
187 net::HTTP_OK, | 184 net::HTTP_OK, |
188 net::URLRequestStatus::SUCCESS); | 185 net::URLRequestStatus::SUCCESS); |
189 ASSERT_TRUE(AttemptToTriggerAuthError()); | 186 ASSERT_TRUE(AttemptToTriggerAuthError()); |
190 ASSERT_TRUE( | 187 ASSERT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest()); |
191 GetSyncService((0))->IsRetryingAccessTokenFetchForTest()); | |
192 } | 188 } |
193 | 189 |
194 // Verify that ProfileSyncService ends up with an INVALID_GAIA_CREDENTIALS auth | 190 // Verify that ProfileSyncService ends up with an INVALID_GAIA_CREDENTIALS auth |
195 // error when an invalid_grant error is returned by OAuth2TokenService with an | 191 // error when an invalid_grant error is returned by OAuth2TokenService with an |
196 // HTTP_BAD_REQUEST (400) response code. | 192 // HTTP_BAD_REQUEST (400) response code. |
197 IN_PROC_BROWSER_TEST_F(SyncAuthTest, InvalidGrant) { | 193 IN_PROC_BROWSER_TEST_F(SyncAuthTest, InvalidGrant) { |
198 ASSERT_TRUE(SetupSync()); | 194 ASSERT_TRUE(SetupSync()); |
199 ASSERT_FALSE(AttemptToTriggerAuthError()); | 195 ASSERT_FALSE(AttemptToTriggerAuthError()); |
200 GetFakeServer()->SetUnauthenticated(); | 196 GetFakeServer()->SetUnauthenticated(); |
201 DisableTokenFetchRetries(); | 197 DisableTokenFetchRetries(); |
202 SetOAuth2TokenResponse(kInvalidGrantOAuth2Token, | 198 SetOAuth2TokenResponse(kInvalidGrantOAuth2Token, |
203 net::HTTP_BAD_REQUEST, | 199 net::HTTP_BAD_REQUEST, |
204 net::URLRequestStatus::SUCCESS); | 200 net::URLRequestStatus::SUCCESS); |
205 ASSERT_TRUE(AttemptToTriggerAuthError()); | 201 ASSERT_TRUE(AttemptToTriggerAuthError()); |
206 ASSERT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, | 202 ASSERT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, |
207 GetSyncService((0))->GetAuthError().state()); | 203 GetSyncService(0)->GetAuthError().state()); |
208 } | 204 } |
209 | 205 |
210 // Verify that ProfileSyncService retries after SERVICE_ERROR auth error when | 206 // Verify that ProfileSyncService retries after SERVICE_ERROR auth error when |
211 // an invalid_client error is returned by OAuth2TokenService with an | 207 // an invalid_client error is returned by OAuth2TokenService with an |
212 // HTTP_BAD_REQUEST (400) response code. | 208 // HTTP_BAD_REQUEST (400) response code. |
213 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryInvalidClient) { | 209 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryInvalidClient) { |
214 ASSERT_TRUE(SetupSync()); | 210 ASSERT_TRUE(SetupSync()); |
215 ASSERT_FALSE(AttemptToTriggerAuthError()); | 211 ASSERT_FALSE(AttemptToTriggerAuthError()); |
216 GetFakeServer()->SetUnauthenticated(); | 212 GetFakeServer()->SetUnauthenticated(); |
217 DisableTokenFetchRetries(); | 213 DisableTokenFetchRetries(); |
218 SetOAuth2TokenResponse(kInvalidClientOAuth2Token, | 214 SetOAuth2TokenResponse(kInvalidClientOAuth2Token, |
219 net::HTTP_BAD_REQUEST, | 215 net::HTTP_BAD_REQUEST, |
220 net::URLRequestStatus::SUCCESS); | 216 net::URLRequestStatus::SUCCESS); |
221 ASSERT_TRUE(AttemptToTriggerAuthError()); | 217 ASSERT_TRUE(AttemptToTriggerAuthError()); |
222 ASSERT_TRUE(GetSyncService((0))->IsRetryingAccessTokenFetchForTest()); | 218 ASSERT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest()); |
223 } | 219 } |
224 | 220 |
225 // Verify that ProfileSyncService retries after REQUEST_CANCELED auth error | 221 // Verify that ProfileSyncService retries after REQUEST_CANCELED auth error |
226 // when OAuth2TokenService has encountered a URLRequestStatus of CANCELED. | 222 // when OAuth2TokenService has encountered a URLRequestStatus of CANCELED. |
227 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryRequestCanceled) { | 223 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryRequestCanceled) { |
228 ASSERT_TRUE(SetupSync()); | 224 ASSERT_TRUE(SetupSync()); |
229 ASSERT_FALSE(AttemptToTriggerAuthError()); | 225 ASSERT_FALSE(AttemptToTriggerAuthError()); |
230 GetFakeServer()->SetUnauthenticated(); | 226 GetFakeServer()->SetUnauthenticated(); |
231 DisableTokenFetchRetries(); | 227 DisableTokenFetchRetries(); |
232 SetOAuth2TokenResponse(kEmptyOAuth2Token, | 228 SetOAuth2TokenResponse(kEmptyOAuth2Token, |
233 net::HTTP_INTERNAL_SERVER_ERROR, | 229 net::HTTP_INTERNAL_SERVER_ERROR, |
234 net::URLRequestStatus::CANCELED); | 230 net::URLRequestStatus::CANCELED); |
235 ASSERT_TRUE(AttemptToTriggerAuthError()); | 231 ASSERT_TRUE(AttemptToTriggerAuthError()); |
236 ASSERT_TRUE(GetSyncService((0))->IsRetryingAccessTokenFetchForTest()); | 232 ASSERT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest()); |
237 } | 233 } |
238 | 234 |
239 // Verify that ProfileSyncService fails initial sync setup during backend | 235 // Verify that ProfileSyncService fails initial sync setup during backend |
240 // initialization and ends up with an INVALID_GAIA_CREDENTIALS auth error when | 236 // initialization and ends up with an INVALID_GAIA_CREDENTIALS auth error when |
241 // an invalid_grant error is returned by OAuth2TokenService with an | 237 // an invalid_grant error is returned by OAuth2TokenService with an |
242 // HTTP_BAD_REQUEST (400) response code. | 238 // HTTP_BAD_REQUEST (400) response code. |
243 IN_PROC_BROWSER_TEST_F(SyncAuthTest, FailInitialSetupWithPersistentError) { | 239 IN_PROC_BROWSER_TEST_F(SyncAuthTest, FailInitialSetupWithPersistentError) { |
244 ASSERT_TRUE(SetupClients()); | 240 ASSERT_TRUE(SetupClients()); |
245 GetFakeServer()->SetUnauthenticated(); | 241 GetFakeServer()->SetUnauthenticated(); |
246 DisableTokenFetchRetries(); | 242 DisableTokenFetchRetries(); |
247 SetOAuth2TokenResponse(kInvalidGrantOAuth2Token, | 243 SetOAuth2TokenResponse(kInvalidGrantOAuth2Token, |
248 net::HTTP_BAD_REQUEST, | 244 net::HTTP_BAD_REQUEST, |
249 net::URLRequestStatus::SUCCESS); | 245 net::URLRequestStatus::SUCCESS); |
250 ASSERT_FALSE(GetClient(0)->SetupSync()); | 246 ASSERT_FALSE(GetClient(0)->SetupSync()); |
251 ASSERT_FALSE(GetSyncService((0))->IsSyncActive()); | 247 ASSERT_FALSE(GetSyncService(0)->IsSyncActive()); |
252 ASSERT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, | 248 ASSERT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS, |
253 GetSyncService((0))->GetAuthError().state()); | 249 GetSyncService(0)->GetAuthError().state()); |
254 } | 250 } |
255 | 251 |
256 // Verify that ProfileSyncService fails initial sync setup during backend | 252 // Verify that ProfileSyncService fails initial sync setup during backend |
257 // initialization, but continues trying to fetch access tokens when | 253 // initialization, but continues trying to fetch access tokens when |
258 // OAuth2TokenService receives an HTTP_INTERNAL_SERVER_ERROR (500) response | 254 // OAuth2TokenService receives an HTTP_INTERNAL_SERVER_ERROR (500) response |
259 // code. | 255 // code. |
260 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryInitialSetupWithTransientError) { | 256 IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryInitialSetupWithTransientError) { |
261 ASSERT_TRUE(SetupClients()); | 257 ASSERT_TRUE(SetupClients()); |
262 GetFakeServer()->SetUnauthenticated(); | 258 GetFakeServer()->SetUnauthenticated(); |
263 DisableTokenFetchRetries(); | 259 DisableTokenFetchRetries(); |
264 SetOAuth2TokenResponse(kEmptyOAuth2Token, | 260 SetOAuth2TokenResponse(kEmptyOAuth2Token, |
265 net::HTTP_INTERNAL_SERVER_ERROR, | 261 net::HTTP_INTERNAL_SERVER_ERROR, |
266 net::URLRequestStatus::SUCCESS); | 262 net::URLRequestStatus::SUCCESS); |
267 ASSERT_FALSE(GetClient(0)->SetupSync()); | 263 ASSERT_FALSE(GetClient(0)->SetupSync()); |
268 ASSERT_FALSE(GetSyncService((0))->IsSyncActive()); | 264 ASSERT_FALSE(GetSyncService(0)->IsSyncActive()); |
269 ASSERT_TRUE( | 265 ASSERT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest()); |
270 GetSyncService((0))->IsRetryingAccessTokenFetchForTest()); | |
271 } | 266 } |
272 | 267 |
273 // Verify that ProfileSyncService fetches a new token when an old token expires. | 268 // Verify that ProfileSyncService fetches a new token when an old token expires. |
274 IN_PROC_BROWSER_TEST_F(SyncAuthTest, TokenExpiry) { | 269 IN_PROC_BROWSER_TEST_F(SyncAuthTest, TokenExpiry) { |
275 // Initial sync succeeds with a short lived OAuth2 Token. | 270 // Initial sync succeeds with a short lived OAuth2 Token. |
276 ASSERT_TRUE(SetupClients()); | 271 ASSERT_TRUE(SetupClients()); |
277 GetFakeServer()->SetAuthenticated(); | 272 GetFakeServer()->SetAuthenticated(); |
278 DisableTokenFetchRetries(); | 273 DisableTokenFetchRetries(); |
279 SetOAuth2TokenResponse(kShortLivedOAuth2Token, | 274 SetOAuth2TokenResponse(kShortLivedOAuth2Token, |
280 net::HTTP_OK, | 275 net::HTTP_OK, |
281 net::URLRequestStatus::SUCCESS); | 276 net::URLRequestStatus::SUCCESS); |
282 ASSERT_TRUE(GetClient(0)->SetupSync()); | 277 ASSERT_TRUE(GetClient(0)->SetupSync()); |
283 std::string old_token = GetSyncService((0))->GetAccessTokenForTest(); | 278 std::string old_token = GetSyncService(0)->GetAccessTokenForTest(); |
284 | 279 |
285 // Wait until the token has expired. | 280 // Wait until the token has expired. |
286 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(5)); | 281 base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(5)); |
287 | 282 |
288 // Trigger an auth error on the server so PSS requests OA2TS for a new token | 283 // Trigger an auth error on the server so PSS requests OA2TS for a new token |
289 // during the next sync cycle. | 284 // during the next sync cycle. |
290 GetFakeServer()->SetUnauthenticated(); | 285 GetFakeServer()->SetUnauthenticated(); |
291 SetOAuth2TokenResponse(kEmptyOAuth2Token, | 286 SetOAuth2TokenResponse(kEmptyOAuth2Token, |
292 net::HTTP_INTERNAL_SERVER_ERROR, | 287 net::HTTP_INTERNAL_SERVER_ERROR, |
293 net::URLRequestStatus::SUCCESS); | 288 net::URLRequestStatus::SUCCESS); |
294 ASSERT_TRUE(AttemptToTriggerAuthError()); | 289 ASSERT_TRUE(AttemptToTriggerAuthError()); |
295 ASSERT_TRUE( | 290 ASSERT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest()); |
296 GetSyncService((0))->IsRetryingAccessTokenFetchForTest()); | |
297 | 291 |
298 // Trigger an auth success state and set up a new valid OAuth2 token. | 292 // Trigger an auth success state and set up a new valid OAuth2 token. |
299 GetFakeServer()->SetAuthenticated(); | 293 GetFakeServer()->SetAuthenticated(); |
300 SetOAuth2TokenResponse(kValidOAuth2Token, | 294 SetOAuth2TokenResponse(kValidOAuth2Token, |
301 net::HTTP_OK, | 295 net::HTTP_OK, |
302 net::URLRequestStatus::SUCCESS); | 296 net::URLRequestStatus::SUCCESS); |
303 | 297 |
304 // Verify that the next sync cycle is successful, and uses the new auth token. | 298 // Verify that the next sync cycle is successful, and uses the new auth token. |
305 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0)))); | 299 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService(0))); |
306 std::string new_token = GetSyncService((0))->GetAccessTokenForTest(); | 300 std::string new_token = GetSyncService(0)->GetAccessTokenForTest(); |
307 ASSERT_NE(old_token, new_token); | 301 ASSERT_NE(old_token, new_token); |
308 } | 302 } |
OLD | NEW |