1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/strings/stringprintf.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "build/buildflag.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "chrome/browser/sync/test/integration/bookmarks_helper.h"
#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
#include "chrome/browser/sync/test/integration/sync_service_impl_harness.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/browser/sync/test/integration/updated_progress_marker_checker.h"
#include "chrome/common/pref_names.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/prefs/pref_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/sync/service/sync_service_impl.h"
#include "components/sync/service/sync_token_status.h"
#include "content/public/test/browser_test.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "net/base/features.h"
#include "net/base/net_errors.h"
#include "net/http/http_status_code.h"
namespace {
constexpr char kShortLivedOAuth2Token[] = R"(
{
"refresh_token": "short_lived_refresh_token",
"access_token": "short_lived_access_token",
"expires_in": 5, // 5 seconds.
"token_type": "Bearer"
})";
constexpr char kValidOAuth2Token[] = R"({
"refresh_token": "new_refresh_token",
"access_token": "new_access_token",
"expires_in": 3600, // 1 hour.
"token_type": "Bearer"
})";
constexpr char kInvalidGrantOAuth2Token[] = R"({
"error": "invalid_grant"
})";
constexpr char kInvalidClientOAuth2Token[] = R"({
"error": "invalid_client"
})";
constexpr char kEmptyOAuth2Token[] = "";
constexpr char kMalformedOAuth2Token[] = R"({ "foo": )";
bool HasUserPrefValue(const PrefService* pref_service,
const std::string& pref) {
return pref_service->GetUserPrefValue(pref) != nullptr;
}
// Waits until local changes are committed or an auth error is encountered.
class TestForAuthError : public UpdatedProgressMarkerChecker {
public:
static bool HasAuthError(syncer::SyncService* service) {
// Note that depending on the nature of the auth error, sync may become
// paused (for persistent auth errors) or in some other cases transient
// errors may be surfaced via GetSyncTokenStatusForDebugging() (e.g. 401
// HTTP status codes returned by the Sync server when the access token has
// expired).
return service->GetTransportState() ==
syncer::SyncService::TransportState::PAUSED ||
service->GetSyncTokenStatusForDebugging()
.last_get_token_error.state() !=
GoogleServiceAuthError::NONE;
}
explicit TestForAuthError(syncer::SyncServiceImpl* service)
: UpdatedProgressMarkerChecker(service) {}
// StatusChangeChecker implementation.
bool IsExitConditionSatisfied(std::ostream* os) override {
*os << "Waiting for auth error";
return HasAuthError(service()) ||
UpdatedProgressMarkerChecker::IsExitConditionSatisfied(os);
}
};
class SyncTransportActiveChecker : public SingleClientStatusChangeChecker {
public:
explicit SyncTransportActiveChecker(syncer::SyncServiceImpl* service)
: SingleClientStatusChangeChecker(service) {}
// StatusChangeChecker implementation.
bool IsExitConditionSatisfied(std::ostream* os) override {
*os << "Waiting for sync transport to become active";
return service()->GetTransportState() ==
syncer::SyncService::TransportState::ACTIVE;
}
};
class SyncAuthTest : public SyncTest {
public:
SyncAuthTest() : SyncTest(SINGLE_CLIENT) {}
SyncAuthTest(const SyncAuthTest&) = delete;
SyncAuthTest& operator=(const SyncAuthTest&) = delete;
~SyncAuthTest() override = default;
// Helper function that adds a bookmark and waits for either an auth error, or
// for the bookmark to be committed. Returns true if it detects an auth
// error, false if the bookmark is committed successfully.
bool AttemptToTriggerAuthError() {
int bookmark_index = GetNextBookmarkIndex();
std::u16string title =
base::ASCIIToUTF16(base::StringPrintf("Bookmark %d", bookmark_index));
GURL url = GURL(base::StringPrintf("http://www.foo%d.com", bookmark_index));
EXPECT_NE(nullptr, bookmarks_helper::AddURL(0, title, url));
// Run until the bookmark is committed or an auth error is encountered.
TestForAuthError(GetSyncService(0)).Wait();
return TestForAuthError::HasAuthError(GetSyncService(0));
}
void DisableTokenFetchRetries() {
// If SyncServiceImpl observes a transient error like SERVICE_UNAVAILABLE
// or CONNECTION_FAILED, this means the access token fetcher has given
// up trying to reach Gaia. In practice, the access token fetching code
// retries a fixed number of times, but the count is transparent to PSS.
// Disable retries so that we instantly trigger the case where
// SyncServiceImpl must pick up where the access token fetcher left off
// (in terms of retries).
signin::DisableAccessTokenFetchRetries(
IdentityManagerFactory::GetForProfile(GetProfile(0)));
}
private:
int GetNextBookmarkIndex() { return bookmark_index_++; }
int bookmark_index_ = 0;
};
class SyncAuthTestOAuthTokens : public SyncAuthTest {
public:
SyncAuthTestOAuthTokens() {
// This test suite intercepts OAuth token requests, and IP Protection also
// requests OAuth tokens. Those requests race with the requests from the
// sync service, resulting in intermittent failures. Disabling IP Protection
// prevents these races.
feature_list_.InitAndDisableFeature(
net::features::kEnableIpProtectionProxy);
}
private:
base::test::ScopedFeatureList feature_list_;
};
// Verify that sync works with a valid OAuth2 token.
IN_PROC_BROWSER_TEST_F(SyncAuthTest, Sanity) {
ASSERT_TRUE(SetupSync());
GetFakeServer()->ClearHttpError();
DisableTokenFetchRetries();
SetOAuth2TokenResponse(kValidOAuth2Token, net::HTTP_OK, net::OK);
ASSERT_FALSE(AttemptToTriggerAuthError());
}
// Verify that SyncServiceImpl continues trying to fetch access tokens
// when the access token fetcher has encountered more than a fixed number of
// HTTP_INTERNAL_SERVER_ERROR (500) errors.
IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnInternalServerError500) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
GetFakeServer()->SetHttpError(net::HTTP_UNAUTHORIZED);
DisableTokenFetchRetries();
SetOAuth2TokenResponse(kValidOAuth2Token, net::HTTP_INTERNAL_SERVER_ERROR,
net::OK);
ASSERT_TRUE(AttemptToTriggerAuthError());
EXPECT_EQ(GetSyncService(0)->GetTransportState(),
syncer::SyncService::TransportState::ACTIVE);
EXPECT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest());
}
// Verify that SyncServiceImpl continues trying to fetch access tokens
// when the access token fetcher has encountered more than a fixed number of
// HTTP_FORBIDDEN (403) errors.
IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnHttpForbidden403) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
GetFakeServer()->SetHttpError(net::HTTP_UNAUTHORIZED);
DisableTokenFetchRetries();
SetOAuth2TokenResponse(kEmptyOAuth2Token, net::HTTP_FORBIDDEN, net::OK);
ASSERT_TRUE(AttemptToTriggerAuthError());
EXPECT_EQ(GetSyncService(0)->GetTransportState(),
syncer::SyncService::TransportState::ACTIVE);
EXPECT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest());
}
// Verify that SyncServiceImpl continues trying to fetch access tokens
// when the access token fetcher has encountered a URLRequestStatus of FAILED.
IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnRequestFailed) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
GetFakeServer()->SetHttpError(net::HTTP_UNAUTHORIZED);
DisableTokenFetchRetries();
SetOAuth2TokenResponse(kEmptyOAuth2Token, net::HTTP_INTERNAL_SERVER_ERROR,
net::ERR_FAILED);
ASSERT_TRUE(AttemptToTriggerAuthError());
EXPECT_EQ(GetSyncService(0)->GetTransportState(),
syncer::SyncService::TransportState::ACTIVE);
EXPECT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest());
}
// Verify that SyncServiceImpl continues trying to fetch access tokens
// when the access token fetcher receives a malformed token.
IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnMalformedToken) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
GetFakeServer()->SetHttpError(net::HTTP_UNAUTHORIZED);
DisableTokenFetchRetries();
SetOAuth2TokenResponse(kMalformedOAuth2Token, net::HTTP_OK, net::OK);
ASSERT_TRUE(AttemptToTriggerAuthError());
EXPECT_EQ(GetSyncService(0)->GetTransportState(),
syncer::SyncService::TransportState::ACTIVE);
EXPECT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest());
}
// Verify that SyncServiceImpl ends up with an INVALID_GAIA_CREDENTIALS auth
// error when an invalid_grant error is returned by the access token fetcher
// with an HTTP_BAD_REQUEST (400) response code.
IN_PROC_BROWSER_TEST_F(SyncAuthTest, InvalidGrant) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
GetFakeServer()->SetHttpError(net::HTTP_UNAUTHORIZED);
DisableTokenFetchRetries();
SetOAuth2TokenResponse(kInvalidGrantOAuth2Token, net::HTTP_BAD_REQUEST,
net::OK);
ASSERT_TRUE(AttemptToTriggerAuthError());
EXPECT_EQ(GetSyncService(0)->GetTransportState(),
syncer::SyncService::TransportState::PAUSED);
EXPECT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
GetSyncService(0)->GetAuthError().state());
EXPECT_FALSE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest());
}
// Verify that SyncServiceImpl does not retry after SERVICE_ERROR auth error
// when an invalid_client error is returned by the access token fetcher with an
// HTTP_BAD_REQUEST (400) response code.
IN_PROC_BROWSER_TEST_F(SyncAuthTest, InvalidClient) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
GetFakeServer()->SetHttpError(net::HTTP_UNAUTHORIZED);
DisableTokenFetchRetries();
SetOAuth2TokenResponse(kInvalidClientOAuth2Token, net::HTTP_BAD_REQUEST,
net::OK);
ASSERT_TRUE(AttemptToTriggerAuthError());
EXPECT_EQ(GetSyncService(0)->GetTransportState(),
syncer::SyncService::TransportState::PAUSED);
EXPECT_EQ(GoogleServiceAuthError::SERVICE_ERROR,
GetSyncService(0)->GetAuthError().state());
EXPECT_FALSE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest());
}
// Verify that SyncServiceImpl retries after REQUEST_CANCELED auth error
// when the access token fetcher has encountered a URLRequestStatus of
// CANCELED.
IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryRequestCanceled) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
GetFakeServer()->SetHttpError(net::HTTP_UNAUTHORIZED);
DisableTokenFetchRetries();
SetOAuth2TokenResponse(kEmptyOAuth2Token, net::HTTP_INTERNAL_SERVER_ERROR,
net::ERR_ABORTED);
ASSERT_TRUE(AttemptToTriggerAuthError());
EXPECT_EQ(GetSyncService(0)->GetTransportState(),
syncer::SyncService::TransportState::ACTIVE);
EXPECT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest());
}
// Verify that SyncServiceImpl fails initial sync setup during backend
// initialization and ends up with an INVALID_GAIA_CREDENTIALS auth error when
// an invalid_grant error is returned by the access token fetcher with an
// HTTP_BAD_REQUEST (400) response code.
IN_PROC_BROWSER_TEST_F(SyncAuthTest, FailInitialSetupWithPersistentError) {
ASSERT_TRUE(SetupClients());
GetFakeServer()->SetHttpError(net::HTTP_UNAUTHORIZED);
DisableTokenFetchRetries();
SetOAuth2TokenResponse(kInvalidGrantOAuth2Token, net::HTTP_BAD_REQUEST,
net::OK);
ASSERT_FALSE(GetClient(0)->SetupSync());
EXPECT_FALSE(GetSyncService(0)->IsSyncFeatureActive());
EXPECT_EQ(GetSyncService(0)->GetTransportState(),
syncer::SyncService::TransportState::PAUSED);
EXPECT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
GetSyncService(0)->GetAuthError().state());
}
// Verify that SyncServiceImpl fails initial sync setup during backend
// initialization, but continues trying to fetch access tokens when
// the access token fetcher receives an HTTP_INTERNAL_SERVER_ERROR (500)
// response code.
IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryInitialSetupWithTransientError) {
ASSERT_TRUE(SetupClients());
GetFakeServer()->SetHttpError(net::HTTP_UNAUTHORIZED);
DisableTokenFetchRetries();
SetOAuth2TokenResponse(kEmptyOAuth2Token, net::HTTP_INTERNAL_SERVER_ERROR,
net::OK);
ASSERT_FALSE(GetClient(0)->SetupSync());
ASSERT_FALSE(GetSyncService(0)->IsSyncFeatureActive());
EXPECT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest());
}
// Verify that SyncServiceImpl fetches a new token when an old token expires.
IN_PROC_BROWSER_TEST_F(SyncAuthTestOAuthTokens, TokenExpiry) {
// Initial sync succeeds with a short lived OAuth2 Token.
ASSERT_TRUE(SetupClients());
GetFakeServer()->ClearHttpError();
DisableTokenFetchRetries();
SetOAuth2TokenResponse(kShortLivedOAuth2Token, net::HTTP_OK, net::OK);
ASSERT_TRUE(GetClient(0)->SetupSync());
std::string old_token = GetSyncService(0)->GetAccessTokenForTest();
// Wait until the token has expired.
base::PlatformThread::Sleep(base::Seconds(5));
// Trigger an auth error on the server so PSS requests OA2TS for a new token
// during the next sync cycle.
GetFakeServer()->SetHttpError(net::HTTP_UNAUTHORIZED);
SetOAuth2TokenResponse(kEmptyOAuth2Token, net::HTTP_INTERNAL_SERVER_ERROR,
net::OK);
ASSERT_TRUE(AttemptToTriggerAuthError());
ASSERT_TRUE(GetSyncService(0)->IsRetryingAccessTokenFetchForTest());
// Trigger an auth success state and set up a new valid OAuth2 token.
GetFakeServer()->ClearHttpError();
SetOAuth2TokenResponse(kValidOAuth2Token, net::HTTP_OK, net::OK);
// Verify that the next sync cycle is successful, and uses the new auth token.
ASSERT_TRUE(UpdatedProgressMarkerChecker(GetSyncService(0)).Wait());
std::string new_token = GetSyncService(0)->GetAccessTokenForTest();
EXPECT_NE(old_token, new_token);
}
class NoAuthErrorChecker : public SingleClientStatusChangeChecker {
public:
explicit NoAuthErrorChecker(syncer::SyncServiceImpl* service)
: SingleClientStatusChangeChecker(service) {}
// StatusChangeChecker implementation.
bool IsExitConditionSatisfied(std::ostream* os) override {
*os << "Waiting for auth error to be cleared";
return service()->GetAuthError().state() == GoogleServiceAuthError::NONE;
}
};
IN_PROC_BROWSER_TEST_F(SyncAuthTest, SyncPausedState) {
ASSERT_TRUE(SetupSync());
ASSERT_TRUE(GetSyncService(0)->IsSyncFeatureActive());
ASSERT_EQ(GetSyncService(0)->GetTransportState(),
syncer::SyncService::TransportState::ACTIVE);
const syncer::DataTypeSet active_types =
GetSyncService(0)->GetActiveDataTypes();
ASSERT_FALSE(active_types.empty());
// Enter the "Sync paused" state.
GetClient(0)->EnterSyncPausedStateForPrimaryAccount();
ASSERT_TRUE(GetSyncService(0)->GetAuthError().IsPersistentError());
// Sync should have shut itself down.
EXPECT_EQ(GetSyncService(0)->GetTransportState(),
syncer::SyncService::TransportState::PAUSED);
EXPECT_FALSE(GetSyncService(0)->IsEngineInitialized());
// The active data types should now be empty.
EXPECT_TRUE(GetSyncService(0)->GetActiveDataTypes().empty());
// Clear the "Sync paused" state again.
GetClient(0)->ExitSyncPausedStateForPrimaryAccount();
// SyncService will clear its auth error state only once it gets a valid
// access token again, so wait for that to happen.
NoAuthErrorChecker(GetSyncService(0)).Wait();
ASSERT_FALSE(GetSyncService(0)->GetAuthError().IsPersistentError());
// Once the auth error is gone, wait for Sync to start up again.
ASSERT_TRUE(GetClient(0)->AwaitSyncSetupCompletion());
// Now the active data types should be back.
EXPECT_TRUE(GetSyncService(0)->IsSyncFeatureActive());
EXPECT_EQ(GetSyncService(0)->GetActiveDataTypes(), active_types);
}
IN_PROC_BROWSER_TEST_F(SyncAuthTest, ShouldTrackDeletionsInSyncPausedState) {
ASSERT_TRUE(SetupSync());
ASSERT_TRUE(GetSyncService(0)->IsSyncFeatureActive());
ASSERT_EQ(GetSyncService(0)->GetTransportState(),
syncer::SyncService::TransportState::ACTIVE);
// USS type.
ASSERT_TRUE(GetSyncService(0)->GetActiveDataTypes().Has(syncer::BOOKMARKS));
// Pseudo-USS type.
ASSERT_TRUE(GetSyncService(0)->GetActiveDataTypes().Has(syncer::PREFERENCES));
const GURL kTestURL("http://mail.google.com");
PrefService* pref_service = GetProfile(0)->GetPrefs();
// Create a bookmark...
const bookmarks::BookmarkNode* bar = bookmarks_helper::GetBookmarkBarNode(0);
ASSERT_FALSE(bookmarks_helper::HasNodeWithURL(0, kTestURL));
const bookmarks::BookmarkNode* bookmark = bookmarks_helper::AddURL(
0, bar, bar->children().size(), u"Title", kTestURL);
// ...set a pref...
ASSERT_FALSE(HasUserPrefValue(pref_service, prefs::kHomePageIsNewTabPage));
pref_service->SetBoolean(prefs::kHomePageIsNewTabPage, true);
// ...and wait for both to be synced up.
UpdatedProgressMarkerChecker(GetSyncService(0)).Wait();
// Enter the "Sync paused" state.
GetClient(0)->EnterSyncPausedStateForPrimaryAccount();
ASSERT_TRUE(GetSyncService(0)->GetAuthError().IsPersistentError());
// Sync should have shut itself down.
EXPECT_EQ(GetSyncService(0)->GetTransportState(),
syncer::SyncService::TransportState::PAUSED);
EXPECT_FALSE(GetSyncService(0)->IsEngineInitialized());
ASSERT_FALSE(GetSyncService(0)->GetActiveDataTypes().Has(syncer::BOOKMARKS));
ASSERT_FALSE(
GetSyncService(0)->GetActiveDataTypes().Has(syncer::PREFERENCES));
// Delete the bookmark and the pref.
// Note that AttemptToTriggerAuthError() also creates bookmarks, so the index
// of our test bookmark might have changed.
ASSERT_EQ(bar->children().back().get(), bookmark);
bookmarks_helper::Remove(0, bar, bar->children().size() - 1);
ASSERT_FALSE(bookmarks_helper::HasNodeWithURL(0, kTestURL));
pref_service->ClearPref(prefs::kHomePageIsNewTabPage);
// Clear the "Sync paused" state again.
GetClient(0)->ExitSyncPausedStateForPrimaryAccount();
// SyncService will clear its auth error state only once it gets a valid
// access token again, so wait for that to happen.
NoAuthErrorChecker(GetSyncService(0)).Wait();
ASSERT_FALSE(GetSyncService(0)->GetAuthError().IsPersistentError());
// Once the auth error is gone, wait for Sync to start up again.
ASSERT_TRUE(GetClient(0)->AwaitSyncSetupCompletion());
// Resuming sync could issue a reconfiguration, so wait until it finishes.
SyncTransportActiveChecker(GetSyncService(0)).Wait();
ASSERT_TRUE(GetSyncService(0)->IsSyncFeatureActive());
// Resuming sync should *not* have re-created the deleted items.
EXPECT_FALSE(bookmarks_helper::HasNodeWithURL(0, kTestURL));
EXPECT_FALSE(HasUserPrefValue(pref_service, prefs::kHomePageIsNewTabPage));
}
} // namespace
|