This source file includes following definitions.
- IsExitConditionSatisfied
- GetDebugMessage
- bookmark_index_
- AttemptToTriggerAuthError
- SetAuthStateAndTokenResponse
- GetNextBookmarkIndex
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
- IN_PROC_BROWSER_TEST_F
#include "base/strings/stringprintf.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/test/integration/bookmarks_helper.h"
#include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
#include "chrome/browser/sync/test/integration/single_client_status_change_checker.h"
#include "chrome/browser/sync/test/integration/sync_integration_test_util.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "net/http/http_status_code.h"
#include "net/url_request/url_request_status.h"
using bookmarks_helper::AddURL;
using sync_integration_test_util::AwaitCommitActivityCompletion;
const char kShortLivedOAuth2Token[] =
"{"
" \"refresh_token\": \"short_lived_refresh_token\","
" \"access_token\": \"short_lived_access_token\","
" \"expires_in\": 5,"
" \"token_type\": \"Bearer\""
"}";
const char kValidOAuth2Token[] = "{"
" \"refresh_token\": \"new_refresh_token\","
" \"access_token\": \"new_access_token\","
" \"expires_in\": 3600,"
" \"token_type\": \"Bearer\""
"}";
const char kInvalidGrantOAuth2Token[] = "{"
" \"error\": \"invalid_grant\""
"}";
const char kInvalidClientOAuth2Token[] = "{"
" \"error\": \"invalid_client\""
"}";
const char kEmptyOAuth2Token[] = "";
const char kMalformedOAuth2Token[] = "{ \"foo\": ";
class TestForAuthError : public SingleClientStatusChangeChecker {
public:
explicit TestForAuthError(ProfileSyncService* service);
virtual ~TestForAuthError();
virtual bool IsExitConditionSatisfied() OVERRIDE;
virtual std::string GetDebugMessage() const OVERRIDE;
};
TestForAuthError::TestForAuthError(ProfileSyncService* service)
: SingleClientStatusChangeChecker(service) {}
TestForAuthError::~TestForAuthError() {}
bool TestForAuthError::IsExitConditionSatisfied() {
return !service()->HasUnsyncedItems() ||
(service()->GetSyncTokenStatus().last_get_token_error.state() !=
GoogleServiceAuthError::NONE);
}
std::string TestForAuthError::GetDebugMessage() const {
return "Waiting for auth error";
}
class SyncAuthTest : public SyncTest {
public:
SyncAuthTest() : SyncTest(SINGLE_CLIENT_LEGACY), bookmark_index_(0) {}
virtual ~SyncAuthTest() {}
bool AttemptToTriggerAuthError() {
int bookmark_index = GetNextBookmarkIndex();
std::wstring title = base::StringPrintf(L"Bookmark %d", bookmark_index);
GURL url = GURL(base::StringPrintf("http://www.foo%d.com", bookmark_index));
EXPECT_TRUE(AddURL(0, title, url) != NULL);
TestForAuthError checker_(GetSyncService((0)));
checker_.Wait();
GoogleServiceAuthError oauth_error =
GetSyncService((0))->GetSyncTokenStatus().last_get_token_error;
return oauth_error.state() != GoogleServiceAuthError::NONE;
}
void SetAuthStateAndTokenResponse(PythonServerAuthState auth_state,
const std::string& response_data,
net::HttpStatusCode response_code,
net::URLRequestStatus::Status status) {
TriggerAuthState(auth_state);
ProfileOAuth2TokenServiceFactory::GetForProfile(GetProfile(0))->
set_max_authorization_token_fetch_retries_for_testing(0);
SetOAuth2TokenResponse(response_data, response_code, status);
}
private:
int GetNextBookmarkIndex() {
return bookmark_index_++;
}
int bookmark_index_;
DISALLOW_COPY_AND_ASSIGN(SyncAuthTest);
};
IN_PROC_BROWSER_TEST_F(SyncAuthTest, Sanity) {
ASSERT_TRUE(SetupSync());
SetAuthStateAndTokenResponse(AUTHENTICATED_TRUE,
kValidOAuth2Token,
net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
ASSERT_FALSE(AttemptToTriggerAuthError());
}
IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnInternalServerError500) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
SetAuthStateAndTokenResponse(AUTHENTICATED_FALSE,
kValidOAuth2Token,
net::HTTP_INTERNAL_SERVER_ERROR,
net::URLRequestStatus::SUCCESS);
ASSERT_TRUE(AttemptToTriggerAuthError());
ASSERT_TRUE(
GetSyncService((0))->IsRetryingAccessTokenFetchForTest());
}
IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnHttpForbidden403) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
SetAuthStateAndTokenResponse(AUTHENTICATED_FALSE,
kEmptyOAuth2Token,
net::HTTP_FORBIDDEN,
net::URLRequestStatus::SUCCESS);
ASSERT_TRUE(AttemptToTriggerAuthError());
ASSERT_TRUE(
GetSyncService((0))->IsRetryingAccessTokenFetchForTest());
}
IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnRequestFailed) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
SetAuthStateAndTokenResponse(AUTHENTICATED_FALSE,
kEmptyOAuth2Token,
net::HTTP_INTERNAL_SERVER_ERROR,
net::URLRequestStatus::FAILED);
ASSERT_TRUE(AttemptToTriggerAuthError());
ASSERT_TRUE(
GetSyncService((0))->IsRetryingAccessTokenFetchForTest());
}
IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryOnMalformedToken) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
SetAuthStateAndTokenResponse(AUTHENTICATED_FALSE,
kMalformedOAuth2Token,
net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
ASSERT_TRUE(AttemptToTriggerAuthError());
ASSERT_TRUE(
GetSyncService((0))->IsRetryingAccessTokenFetchForTest());
}
IN_PROC_BROWSER_TEST_F(SyncAuthTest, InvalidGrant) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
SetAuthStateAndTokenResponse(AUTHENTICATED_FALSE,
kInvalidGrantOAuth2Token,
net::HTTP_BAD_REQUEST,
net::URLRequestStatus::SUCCESS);
ASSERT_TRUE(AttemptToTriggerAuthError());
ASSERT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
GetSyncService((0))->GetAuthError().state());
}
IN_PROC_BROWSER_TEST_F(SyncAuthTest, InvalidClient) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
SetAuthStateAndTokenResponse(AUTHENTICATED_FALSE,
kInvalidClientOAuth2Token,
net::HTTP_BAD_REQUEST,
net::URLRequestStatus::SUCCESS);
ASSERT_TRUE(AttemptToTriggerAuthError());
ASSERT_EQ(GoogleServiceAuthError::SERVICE_ERROR,
GetSyncService((0))->GetAuthError().state());
}
IN_PROC_BROWSER_TEST_F(SyncAuthTest, RequestCanceled) {
ASSERT_TRUE(SetupSync());
ASSERT_FALSE(AttemptToTriggerAuthError());
SetAuthStateAndTokenResponse(AUTHENTICATED_FALSE,
kEmptyOAuth2Token,
net::HTTP_INTERNAL_SERVER_ERROR,
net::URLRequestStatus::CANCELED);
ASSERT_TRUE(AttemptToTriggerAuthError());
ASSERT_EQ(GoogleServiceAuthError::REQUEST_CANCELED,
GetSyncService((0))->GetAuthError().state());
}
IN_PROC_BROWSER_TEST_F(SyncAuthTest, FailInitialSetupWithPersistentError) {
ASSERT_TRUE(SetupClients());
SetAuthStateAndTokenResponse(AUTHENTICATED_FALSE,
kInvalidGrantOAuth2Token,
net::HTTP_BAD_REQUEST,
net::URLRequestStatus::SUCCESS);
ASSERT_FALSE(GetClient(0)->SetupSync());
ASSERT_FALSE(GetSyncService((0))->sync_initialized());
ASSERT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
GetSyncService((0))->GetAuthError().state());
}
IN_PROC_BROWSER_TEST_F(SyncAuthTest, RetryInitialSetupWithTransientError) {
ASSERT_TRUE(SetupClients());
SetAuthStateAndTokenResponse(AUTHENTICATED_FALSE,
kEmptyOAuth2Token,
net::HTTP_INTERNAL_SERVER_ERROR,
net::URLRequestStatus::SUCCESS);
ASSERT_FALSE(GetClient(0)->SetupSync());
ASSERT_FALSE(GetSyncService((0))->sync_initialized());
ASSERT_TRUE(
GetSyncService((0))->IsRetryingAccessTokenFetchForTest());
}
IN_PROC_BROWSER_TEST_F(SyncAuthTest, TokenExpiry) {
ASSERT_TRUE(SetupClients());
SetAuthStateAndTokenResponse(AUTHENTICATED_TRUE,
kShortLivedOAuth2Token,
net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
ASSERT_TRUE(GetClient(0)->SetupSync());
std::string old_token = GetSyncService((0))->GetAccessTokenForTest();
base::PlatformThread::Sleep(base::TimeDelta::FromSeconds(5));
SetAuthStateAndTokenResponse(AUTHENTICATED_FALSE,
kEmptyOAuth2Token,
net::HTTP_INTERNAL_SERVER_ERROR,
net::URLRequestStatus::SUCCESS);
ASSERT_TRUE(AttemptToTriggerAuthError());
ASSERT_TRUE(
GetSyncService((0))->IsRetryingAccessTokenFetchForTest());
SetAuthStateAndTokenResponse(AUTHENTICATED_TRUE,
kValidOAuth2Token,
net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0))));
std::string new_token = GetSyncService((0))->GetAccessTokenForTest();
ASSERT_NE(old_token, new_token);
}