#include "components/sync/driver/sync_service_impl.h"
#include <map>
#include <memory>
#include <string>
#include <utility>
#include "base/command_line.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "components/prefs/testing_pref_service.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "components/signin/public/identity_manager/identity_test_utils.h"
#include "components/signin/public/identity_manager/primary_account_mutator.h"
#include "components/sync/base/command_line_switches.h"
#include "components/sync/base/features.h"
#include "components/sync/base/model_type.h"
#include "components/sync/base/pref_names.h"
#include "components/sync/base/stop_source.h"
#include "components/sync/base/sync_util.h"
#include "components/sync/base/user_selectable_type.h"
#include "components/sync/driver/configure_context.h"
#include "components/sync/driver/data_type_manager_impl.h"
#include "components/sync/driver/sync_service_observer.h"
#include "components/sync/driver/sync_token_status.h"
#include "components/sync/engine/nigori/key_derivation_params.h"
#include "components/sync/test/fake_data_type_controller.h"
#include "components/sync/test/fake_sync_api_component_factory.h"
#include "components/sync/test/fake_sync_engine.h"
#include "components/sync/test/mock_trusted_vault_client.h"
#include "components/sync/test/sync_client_mock.h"
#include "components/sync/test/sync_service_impl_bundle.h"
#include "components/version_info/version_info_values.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::_;
using testing::AllOf;
using testing::AnyNumber;
using testing::AtLeast;
using testing::ByMove;
using testing::Eq;
using testing::Not;
using testing::Return;
namespace syncer {
namespace {
MATCHER_P(ContainsDataType, type, "") {
return arg.Has(type);
}
constexpr char kTestUser[] = "test_user@gmail.com";
class TestSyncServiceObserver : public SyncServiceObserver {
public:
TestSyncServiceObserver() = default;
void OnStateChanged(SyncService* sync) override {
setup_in_progress_ = sync->IsSetupInProgress();
auth_error_ = sync->GetAuthError();
}
bool setup_in_progress() const { return setup_in_progress_; }
GoogleServiceAuthError auth_error() const { return auth_error_; }
private:
bool setup_in_progress_ = false;
GoogleServiceAuthError auth_error_;
};
class SyncServiceImplTest : public ::testing::Test {
protected:
SyncServiceImplTest() = default;
~SyncServiceImplTest() override = default;
void SetUp() override {
base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
kSyncDeferredStartupTimeoutSeconds, "0");
}
void TearDown() override {
ShutdownAndDeleteService();
}
void SignIn() {
identity_test_env()->MakePrimaryAccountAvailable(
kTestUser, signin::ConsentLevel::kSync);
}
void CreateService(SyncServiceImpl::StartBehavior behavior,
std::vector<std::pair<ModelType, bool>>
registered_types_and_transport_mode_support = {
{BOOKMARKS, false},
{DEVICE_INFO, true}}) {
DCHECK(!service_);
DataTypeController::TypeVector controllers;
for (const auto& [type, transport_mode_support] :
registered_types_and_transport_mode_support) {
auto controller = std::make_unique<FakeDataTypeController>(
type, transport_mode_support);
controller_map_[type] = controller.get();
controllers.push_back(std::move(controller));
}
std::unique_ptr<SyncClientMock> sync_client =
sync_service_impl_bundle_.CreateSyncClientMock();
sync_client_ = sync_client.get();
ON_CALL(*sync_client, CreateDataTypeControllers)
.WillByDefault(Return(ByMove(std::move(controllers))));
service_ = std::make_unique<SyncServiceImpl>(
sync_service_impl_bundle_.CreateBasicInitParams(
behavior, std::move(sync_client)));
}
void CreateServiceWithLocalSyncBackend() {
DCHECK(!service_);
DataTypeController::TypeVector controllers;
controllers.push_back(std::make_unique<FakeDataTypeController>(BOOKMARKS));
controllers.push_back(std::make_unique<FakeDataTypeController>(
DEVICE_INFO, true));
std::unique_ptr<SyncClientMock> sync_client =
sync_service_impl_bundle_.CreateSyncClientMock();
sync_client_ = sync_client.get();
ON_CALL(*sync_client, CreateDataTypeControllers)
.WillByDefault(Return(ByMove(std::move(controllers))));
SyncServiceImpl::InitParams init_params =
sync_service_impl_bundle_.CreateBasicInitParams(
SyncServiceImpl::AUTO_START, std::move(sync_client));
prefs()->SetBoolean(prefs::kEnableLocalSyncBackend, true);
init_params.identity_manager = nullptr;
service_ = std::make_unique<SyncServiceImpl>(std::move(init_params));
}
void ShutdownAndDeleteService() {
if (service_)
service_->Shutdown();
service_.reset();
}
void PopulatePrefsForNthSync() {
component_factory()->set_first_time_sync_configure_done(true);
SyncPrefs sync_prefs(prefs());
sync_prefs.SetSyncRequested(true);
sync_prefs.SetSelectedTypes(
true,
UserSelectableTypeSet::All(),
UserSelectableTypeSet::All());
sync_prefs.SetFirstSetupComplete();
}
void InitializeForNthSync(bool run_until_idle = true) {
PopulatePrefsForNthSync();
service_->Initialize();
if (run_until_idle) {
task_environment_.RunUntilIdle();
}
}
void InitializeForFirstSync(bool run_until_idle = true) {
service_->Initialize();
if (run_until_idle) {
task_environment_.RunUntilIdle();
}
}
void TriggerPassphraseRequired() {
service_->GetEncryptionObserverForTest()->OnPassphraseRequired(
KeyDerivationParams::CreateForPbkdf2(), sync_pb::EncryptedData());
}
void TriggerDataTypeStartRequest() {
service_->OnDataTypeRequestsSyncStartup(BOOKMARKS);
}
signin::IdentityManager* identity_manager() {
return sync_service_impl_bundle_.identity_manager();
}
signin::IdentityTestEnvironment* identity_test_env() {
return sync_service_impl_bundle_.identity_test_env();
}
SyncServiceImpl* service() { return service_.get(); }
SyncClientMock* sync_client() { return sync_client_; }
TestingPrefServiceSimple* prefs() {
return sync_service_impl_bundle_.pref_service();
}
FakeSyncApiComponentFactory* component_factory() {
return sync_service_impl_bundle_.component_factory();
}
DataTypeManagerImpl* data_type_manager() {
return component_factory()->last_created_data_type_manager();
}
FakeSyncEngine* engine() {
return component_factory()->last_created_engine();
}
MockSyncInvalidationsService* sync_invalidations_service() {
return sync_service_impl_bundle_.sync_invalidations_service();
}
MockTrustedVaultClient* trusted_vault_client() {
return sync_service_impl_bundle_.trusted_vault_client();
}
FakeDataTypeController* get_controller(ModelType type) {
return controller_map_[type];
}
private:
base::test::SingleThreadTaskEnvironment task_environment_;
SyncServiceImplBundle sync_service_impl_bundle_;
std::unique_ptr<SyncServiceImpl> service_;
raw_ptr<SyncClientMock> sync_client_;
std::map<ModelType, FakeDataTypeController*> controller_map_;
};
TEST_F(SyncServiceImplTest, InitialState) {
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
const std::string& url = service()->GetSyncServiceUrlForDebugging().spec();
EXPECT_TRUE(url == internal::kSyncServerUrl ||
url == internal::kSyncDevServerUrl);
}
TEST_F(SyncServiceImplTest, SuccessfulInitialization) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
EXPECT_EQ(SyncService::DisableReasonSet(), service()->GetDisableReasons());
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
}
TEST_F(SyncServiceImplTest, SuccessfulLocalBackendInitialization) {
CreateServiceWithLocalSyncBackend();
InitializeForNthSync();
EXPECT_EQ(SyncService::DisableReasonSet(), service()->GetDisableReasons());
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
}
TEST_F(SyncServiceImplTest, NeedsConfirmation) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
SyncPrefs sync_prefs(prefs());
sync_prefs.SetSyncRequested(true);
sync_prefs.SetSelectedTypes(
true,
UserSelectableTypeSet::All(),
UserSelectableTypeSet::All());
service()->Initialize();
EXPECT_EQ(SyncService::DisableReasonSet(), service()->GetDisableReasons());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
EXPECT_FALSE(service()->IsSyncFeatureActive());
EXPECT_FALSE(service()->IsSyncFeatureEnabled());
}
TEST_F(SyncServiceImplTest, ModelTypesForTransportMode) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForFirstSync();
ASSERT_FALSE(service()->IsSyncFeatureActive());
ASSERT_FALSE(service()->IsSyncFeatureEnabled());
base::RunLoop().RunUntilIdle();
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
EXPECT_FALSE(service()->GetActiveDataTypes().Has(BOOKMARKS));
EXPECT_TRUE(service()->GetActiveDataTypes().Has(DEVICE_INFO));
}
TEST_F(SyncServiceImplTest, SetupInProgress) {
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForFirstSync();
TestSyncServiceObserver observer;
service()->AddObserver(&observer);
std::unique_ptr<SyncSetupInProgressHandle> sync_blocker =
service()->GetSetupInProgressHandle();
EXPECT_TRUE(observer.setup_in_progress());
sync_blocker.reset();
EXPECT_FALSE(observer.setup_in_progress());
service()->RemoveObserver(&observer);
}
TEST_F(SyncServiceImplTest, DisabledByPolicyBeforeInit) {
prefs()->SetManagedPref(prefs::kSyncManaged, base::Value(true));
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
EXPECT_EQ(SyncService::DisableReasonSet(
SyncService::DISABLE_REASON_ENTERPRISE_POLICY,
SyncService::DISABLE_REASON_USER_CHOICE),
service()->GetDisableReasons());
EXPECT_EQ(SyncService::TransportState::DISABLED,
service()->GetTransportState());
}
TEST_F(SyncServiceImplTest, DisabledByPolicyBeforeInitThenPolicyRemoved) {
prefs()->SetManagedPref(prefs::kSyncManaged, base::Value(true));
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
EXPECT_EQ(SyncService::DisableReasonSet(
SyncService::DISABLE_REASON_ENTERPRISE_POLICY,
SyncService::DISABLE_REASON_USER_CHOICE),
service()->GetDisableReasons());
EXPECT_EQ(SyncService::TransportState::DISABLED,
service()->GetTransportState());
prefs()->SetManagedPref(prefs::kSyncManaged, base::Value(false));
EXPECT_EQ(
SyncService::DisableReasonSet(SyncService::DISABLE_REASON_USER_CHOICE),
service()->GetDisableReasons());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
service()->SetSyncFeatureRequested();
service()->GetUserSettings()->SetFirstSetupComplete(
syncer::SyncFirstSetupCompleteSource::BASIC_FLOW);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
EXPECT_TRUE(service()->GetDisableReasons().Empty());
}
TEST_F(SyncServiceImplTest, DisabledByPolicyAfterInit) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_EQ(SyncService::DisableReasonSet(), service()->GetDisableReasons());
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
prefs()->SetManagedPref(prefs::kSyncManaged, base::Value(true));
EXPECT_EQ(SyncService::DisableReasonSet(
SyncService::DISABLE_REASON_ENTERPRISE_POLICY,
SyncService::DISABLE_REASON_USER_CHOICE),
service()->GetDisableReasons());
EXPECT_EQ(SyncService::TransportState::DISABLED,
service()->GetTransportState());
}
TEST_F(SyncServiceImplTest, AbortedByShutdown) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
component_factory()->AllowFakeEngineInitCompletion(false);
InitializeForNthSync();
ASSERT_EQ(SyncService::TransportState::INITIALIZING,
service()->GetTransportState());
ShutdownAndDeleteService();
}
TEST_F(SyncServiceImplTest, EarlyRequestStop) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
component_factory()->AllowFakeEngineInitCompletion(false);
InitializeForNthSync();
ASSERT_EQ(SyncService::TransportState::INITIALIZING,
service()->GetTransportState());
component_factory()->AllowFakeEngineInitCompletion(true);
service()->StopAndClear();
EXPECT_EQ(
SyncService::DisableReasonSet(SyncService::DISABLE_REASON_USER_CHOICE),
service()->GetDisableReasons());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
EXPECT_FALSE(service()->IsSyncFeatureActive());
EXPECT_FALSE(service()->IsSyncFeatureEnabled());
service()->SetSyncFeatureRequested();
service()->GetUserSettings()->SetFirstSetupComplete(
syncer::SyncFirstSetupCompleteSource::BASIC_FLOW);
EXPECT_EQ(SyncService::DisableReasonSet(), service()->GetDisableReasons());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
EXPECT_TRUE(service()->IsSyncFeatureActive());
EXPECT_TRUE(service()->IsSyncFeatureEnabled());
}
TEST_F(SyncServiceImplTest, DisableAndEnableSyncTemporarily) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
SyncPrefs sync_prefs(prefs());
ASSERT_EQ(SyncService::DisableReasonSet(), service()->GetDisableReasons());
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
ASSERT_TRUE(service()->IsSyncFeatureActive());
ASSERT_TRUE(service()->IsSyncFeatureEnabled());
service()->StopAndClear();
EXPECT_FALSE(sync_prefs.IsSyncRequested());
EXPECT_EQ(
SyncService::DisableReasonSet(SyncService::DISABLE_REASON_USER_CHOICE),
service()->GetDisableReasons());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
EXPECT_FALSE(service()->IsSyncFeatureActive());
EXPECT_FALSE(service()->IsSyncFeatureEnabled());
service()->SetSyncFeatureRequested();
EXPECT_EQ(SyncService::DisableReasonSet(), service()->GetDisableReasons());
service()->GetUserSettings()->SetFirstSetupComplete(
syncer::SyncFirstSetupCompleteSource::BASIC_FLOW);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
EXPECT_TRUE(service()->IsSyncFeatureActive());
EXPECT_TRUE(service()->IsSyncFeatureEnabled());
}
#if !BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(SyncServiceImplTest, SignOutDisablesSyncTransportAndSyncFeature) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_EQ(SyncService::DisableReasonSet(), service()->GetDisableReasons());
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
signin::PrimaryAccountMutator* account_mutator =
identity_manager()->GetPrimaryAccountMutator();
DCHECK(account_mutator) << "Account mutator should only be null on ChromeOS.";
account_mutator->ClearPrimaryAccount(
signin_metrics::ProfileSignout::kTest,
signin_metrics::SignoutDelete::kIgnoreMetric);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(
SyncService::DisableReasonSet(SyncService::DISABLE_REASON_NOT_SIGNED_IN,
SyncService::DISABLE_REASON_USER_CHOICE),
service()->GetDisableReasons());
EXPECT_EQ(SyncService::TransportState::DISABLED,
service()->GetTransportState());
}
TEST_F(SyncServiceImplTest,
SignOutClearsSyncTransportDataAndSyncTheFeaturePrefs) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_TRUE(service()->GetUserSettings()->IsFirstSetupComplete());
ASSERT_EQ(SyncService::DisableReasonSet(), service()->GetDisableReasons());
ASSERT_EQ(0, component_factory()->clear_transport_data_call_count());
signin::PrimaryAccountMutator* account_mutator =
identity_manager()->GetPrimaryAccountMutator();
DCHECK(account_mutator) << "Account mutator should only be null on ChromeOS.";
account_mutator->ClearPrimaryAccount(
signin_metrics::ProfileSignout::kTest,
signin_metrics::SignoutDelete::kIgnoreMetric);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(service()->GetUserSettings()->IsFirstSetupComplete());
EXPECT_EQ(
SyncService::DisableReasonSet(SyncService::DISABLE_REASON_NOT_SIGNED_IN,
SyncService::DISABLE_REASON_USER_CHOICE),
service()->GetDisableReasons());
EXPECT_EQ(1, component_factory()->clear_transport_data_call_count());
}
TEST_F(SyncServiceImplTest, DisableReasonUserChoiceIfStartsSignedOut) {
SyncPrefs sync_prefs(prefs());
sync_prefs.SetSyncRequested(true);
CreateService(SyncServiceImpl::MANUAL_START);
service()->Initialize();
EXPECT_TRUE(service()->GetDisableReasons().Has(
SyncService::DISABLE_REASON_USER_CHOICE));
}
#endif
TEST_F(SyncServiceImplTest, GetSyncTokenStatus) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync(false);
SyncTokenStatus token_status = service()->GetSyncTokenStatusForDebugging();
ASSERT_EQ(CONNECTION_NOT_ATTEMPTED, token_status.connection_status);
ASSERT_TRUE(token_status.connection_status_update_time.is_null());
ASSERT_TRUE(token_status.token_request_time.is_null());
ASSERT_TRUE(token_status.token_response_time.is_null());
ASSERT_FALSE(token_status.has_token);
base::RunLoop().RunUntilIdle();
token_status = service()->GetSyncTokenStatusForDebugging();
EXPECT_TRUE(token_status.connection_status_update_time.is_null());
EXPECT_FALSE(token_status.token_request_time.is_null());
EXPECT_FALSE(token_status.token_response_time.is_null());
EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(),
token_status.last_get_token_error);
EXPECT_TRUE(token_status.next_token_request_time.is_null());
EXPECT_TRUE(token_status.has_token);
service()->OnConnectionStatusChange(CONNECTION_AUTH_ERROR);
token_status = service()->GetSyncTokenStatusForDebugging();
EXPECT_EQ(CONNECTION_AUTH_ERROR, token_status.connection_status);
EXPECT_FALSE(token_status.connection_status_update_time.is_null());
EXPECT_FALSE(token_status.token_request_time.is_null());
EXPECT_FALSE(token_status.token_response_time.is_null());
EXPECT_EQ(GoogleServiceAuthError::AuthErrorNone(),
token_status.last_get_token_error);
EXPECT_FALSE(token_status.next_token_request_time.is_null());
EXPECT_FALSE(token_status.has_token);
service()->OnConnectionStatusChange(CONNECTION_OK);
token_status = service()->GetSyncTokenStatusForDebugging();
EXPECT_EQ(CONNECTION_OK, token_status.connection_status);
}
TEST_F(SyncServiceImplTest, RevokeAccessTokenFromTokenService) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
const CoreAccountId primary_account_id =
identity_manager()->GetPrimaryAccountId(signin::ConsentLevel::kSync);
ASSERT_EQ(primary_account_id, engine()->authenticated_account_id());
service()->OnConnectionStatusChange(CONNECTION_AUTH_ERROR);
base::RunLoop().RunUntilIdle();
ASSERT_FALSE(service()->GetAccessTokenForTest().empty());
AccountInfo secondary_account_info =
identity_test_env()->MakeAccountAvailable("test_user2@gmail.com");
identity_test_env()->RemoveRefreshTokenForAccount(
secondary_account_info.account_id);
EXPECT_FALSE(service()->GetAccessTokenForTest().empty());
identity_test_env()->RemoveRefreshTokenForPrimaryAccount();
EXPECT_TRUE(service()->GetAccessTokenForTest().empty());
}
TEST_F(SyncServiceImplTest, CredentialsRejectedByClient_StopSync) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
TestSyncServiceObserver observer;
service()->AddObserver(&observer);
const CoreAccountId primary_account_id =
identity_manager()->GetPrimaryAccountId(signin::ConsentLevel::kSync);
ASSERT_EQ(primary_account_id, engine()->authenticated_account_id());
service()->OnConnectionStatusChange(CONNECTION_AUTH_ERROR);
base::RunLoop().RunUntilIdle();
ASSERT_FALSE(service()->GetAccessTokenForTest().empty());
ASSERT_EQ(GoogleServiceAuthError::AuthErrorNone(), service()->GetAuthError());
ASSERT_EQ(GoogleServiceAuthError::AuthErrorNone(), observer.auth_error());
identity_test_env()->SetInvalidRefreshTokenForPrimaryAccount();
const GoogleServiceAuthError rejected_by_client =
GoogleServiceAuthError::FromInvalidGaiaCredentialsReason(
GoogleServiceAuthError::InvalidGaiaCredentialsReason::
CREDENTIALS_REJECTED_BY_CLIENT);
ASSERT_EQ(rejected_by_client,
identity_test_env()
->identity_manager()
->GetErrorStateOfRefreshTokenForAccount(primary_account_id));
EXPECT_TRUE(service()->GetAccessTokenForTest().empty());
EXPECT_EQ(rejected_by_client, observer.auth_error());
EXPECT_FALSE(service()->IsEngineInitialized());
EXPECT_EQ(SyncService::TransportState::PAUSED,
service()->GetTransportState());
service()->RemoveObserver(&observer);
}
#if !BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(SyncServiceImplTest, SignOutRevokeAccessToken) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
const CoreAccountId primary_account_id =
identity_manager()->GetPrimaryAccountId(signin::ConsentLevel::kSync);
ASSERT_EQ(primary_account_id, engine()->authenticated_account_id());
service()->OnConnectionStatusChange(CONNECTION_AUTH_ERROR);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(service()->GetAccessTokenForTest().empty());
signin::PrimaryAccountMutator* account_mutator =
identity_manager()->GetPrimaryAccountMutator();
DCHECK(account_mutator);
account_mutator->ClearPrimaryAccount(
signin_metrics::ProfileSignout::kTest,
signin_metrics::SignoutDelete::kIgnoreMetric);
EXPECT_TRUE(service()->GetAccessTokenForTest().empty());
}
#endif
TEST_F(SyncServiceImplTest, StopAndClearWillClearDataAndSwitchToTransportMode) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
ASSERT_EQ(0, component_factory()->clear_transport_data_call_count());
service()->StopAndClear();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
EXPECT_FALSE(service()->IsSyncFeatureEnabled());
EXPECT_EQ(1, component_factory()->clear_transport_data_call_count());
}
TEST_F(SyncServiceImplTest, ClearTransportDataOnInitializeWhenSignedOut) {
identity_test_env()->WaitForRefreshTokensLoaded();
CreateService(SyncServiceImpl::MANUAL_START);
ASSERT_EQ(0, component_factory()->clear_transport_data_call_count());
InitializeForNthSync();
EXPECT_EQ(1, component_factory()->clear_transport_data_call_count());
}
TEST_F(SyncServiceImplTest, StopSyncAndClearTwiceDoesNotCrash) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
service()->StopAndClear();
EXPECT_FALSE(service()->IsSyncFeatureEnabled());
service()->StopAndClear();
EXPECT_FALSE(service()->IsSyncFeatureEnabled());
}
TEST_F(SyncServiceImplTest, CredentialErrorReturned) {
identity_test_env()->SetAutomaticIssueOfAccessTokens(false);
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
const CoreAccountId primary_account_id =
identity_manager()->GetPrimaryAccountId(signin::ConsentLevel::kSync);
ASSERT_EQ(primary_account_id, engine()->authenticated_account_id());
TestSyncServiceObserver observer;
service()->AddObserver(&observer);
service()->OnConnectionStatusChange(CONNECTION_AUTH_ERROR);
base::RunLoop().RunUntilIdle();
identity_test_env()->WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
primary_account_id, "access token", base::Time::Max());
ASSERT_FALSE(service()->GetAccessTokenForTest().empty());
ASSERT_EQ(GoogleServiceAuthError::NONE, service()->GetAuthError().state());
identity_test_env()->SetRefreshTokenForPrimaryAccount();
base::RunLoop().RunUntilIdle();
identity_test_env()->WaitForAccessTokenRequestIfNecessaryAndRespondWithError(
GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
EXPECT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
service()->GetAuthError().state());
EXPECT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
observer.auth_error().state());
EXPECT_EQ(SyncService::TransportState::PAUSED,
service()->GetTransportState());
service()->RemoveObserver(&observer);
}
TEST_F(SyncServiceImplTest, CredentialErrorClearsOnNewToken) {
identity_test_env()->SetAutomaticIssueOfAccessTokens(false);
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
const CoreAccountId primary_account_id =
identity_manager()->GetPrimaryAccountId(signin::ConsentLevel::kSync);
ASSERT_EQ(primary_account_id, engine()->authenticated_account_id());
TestSyncServiceObserver observer;
service()->AddObserver(&observer);
service()->OnConnectionStatusChange(CONNECTION_AUTH_ERROR);
base::RunLoop().RunUntilIdle();
identity_test_env()->WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
primary_account_id, "access token", base::Time::Max());
ASSERT_FALSE(service()->GetAccessTokenForTest().empty());
ASSERT_EQ(GoogleServiceAuthError::NONE, service()->GetAuthError().state());
identity_test_env()->SetRefreshTokenForPrimaryAccount();
base::RunLoop().RunUntilIdle();
identity_test_env()->WaitForAccessTokenRequestIfNecessaryAndRespondWithError(
GoogleServiceAuthError(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS));
ASSERT_EQ(GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS,
service()->GetAuthError().state());
ASSERT_EQ(SyncService::TransportState::PAUSED,
service()->GetTransportState());
identity_test_env()->SetRefreshTokenForPrimaryAccount();
base::RunLoop().RunUntilIdle();
identity_test_env()->WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
"this one works", base::Time::Now() + base::Days(10));
EXPECT_EQ(GoogleServiceAuthError::NONE, service()->GetAuthError().state());
EXPECT_EQ(GoogleServiceAuthError::NONE, observer.auth_error().state());
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
service()->RemoveObserver(&observer);
}
TEST_F(SyncServiceImplTest, DisableSyncFlag) {
base::CommandLine::ForCurrentProcess()->AppendSwitch(kDisableSync);
EXPECT_FALSE(IsSyncAllowedByFlag());
}
TEST_F(SyncServiceImplTest, NoDisableSyncFlag) {
EXPECT_TRUE(IsSyncAllowedByFlag());
}
TEST_F(SyncServiceImplTest, ResetSyncData) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
SyncProtocolError client_cmd;
client_cmd.action = RESET_LOCAL_SYNC_DATA;
service()->OnActionableProtocolError(client_cmd);
}
TEST_F(SyncServiceImplTest, DisableSyncOnClient) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
ASSERT_EQ(0, get_controller(BOOKMARKS)->model()->clear_metadata_call_count());
EXPECT_CALL(
*trusted_vault_client(),
ClearLocalDataForAccount(Eq(identity_manager()->GetPrimaryAccountInfo(
signin::ConsentLevel::kSync))));
SyncProtocolError client_cmd;
client_cmd.action = DISABLE_SYNC_ON_CLIENT;
service()->OnActionableProtocolError(client_cmd);
#if BUILDFLAG(IS_CHROMEOS_ASH)
EXPECT_TRUE(
identity_manager()->HasPrimaryAccount(signin::ConsentLevel::kSync));
EXPECT_EQ(
SyncService::DisableReasonSet(SyncService::DISABLE_REASON_USER_CHOICE),
service()->GetDisableReasons());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
#else
EXPECT_FALSE(
identity_manager()->HasPrimaryAccount(signin::ConsentLevel::kSync));
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
EXPECT_FALSE(
identity_manager()->HasPrimaryAccount(signin::ConsentLevel::kSignin));
#endif
EXPECT_EQ(
SyncService::DisableReasonSet(SyncService::DISABLE_REASON_NOT_SIGNED_IN,
SyncService::DISABLE_REASON_USER_CHOICE),
service()->GetDisableReasons());
EXPECT_EQ(SyncService::TransportState::DISABLED,
service()->GetTransportState());
EXPECT_TRUE(service()->GetLastSyncedTimeForDebugging().is_null());
#endif
EXPECT_GT(get_controller(BOOKMARKS)->model()->clear_metadata_call_count(), 0);
EXPECT_FALSE(service()->IsSyncFeatureEnabled());
EXPECT_FALSE(service()->IsSyncFeatureActive());
}
TEST_F(SyncServiceImplTest,
DisableSyncOnClientLogsPassphraseTypeForNotMyBirthday) {
const PassphraseType kPassphraseType = PassphraseType::kKeystorePassphrase;
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
service()->GetEncryptionObserverForTest()->OnPassphraseTypeChanged(
kPassphraseType, base::Time());
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
ASSERT_TRUE(service()->IsSyncFeatureEnabled());
ASSERT_EQ(kPassphraseType, service()->GetUserSettings()->GetPassphraseType());
SyncProtocolError client_cmd;
client_cmd.action = DISABLE_SYNC_ON_CLIENT;
client_cmd.error_type = NOT_MY_BIRTHDAY;
base::HistogramTester histogram_tester;
service()->OnActionableProtocolError(client_cmd);
ASSERT_FALSE(service()->IsSyncFeatureEnabled());
histogram_tester.ExpectUniqueSample(
"Sync.PassphraseTypeUponNotMyBirthdayOrEncryptionObsolete",
kPassphraseType,
1);
}
TEST_F(SyncServiceImplTest,
DisableSyncOnClientLogsPassphraseTypeForEncryptionObsolete) {
const PassphraseType kPassphraseType = PassphraseType::kKeystorePassphrase;
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
service()->GetEncryptionObserverForTest()->OnPassphraseTypeChanged(
kPassphraseType, base::Time());
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
ASSERT_TRUE(service()->IsSyncFeatureEnabled());
ASSERT_EQ(kPassphraseType, service()->GetUserSettings()->GetPassphraseType());
SyncProtocolError client_cmd;
client_cmd.action = DISABLE_SYNC_ON_CLIENT;
client_cmd.error_type = ENCRYPTION_OBSOLETE;
base::HistogramTester histogram_tester;
service()->OnActionableProtocolError(client_cmd);
ASSERT_FALSE(service()->IsSyncFeatureEnabled());
histogram_tester.ExpectUniqueSample(
"Sync.PassphraseTypeUponNotMyBirthdayOrEncryptionObsolete",
kPassphraseType,
1);
}
TEST_F(SyncServiceImplTest, LocalBackendUnimpactedByPolicy) {
prefs()->SetManagedPref(prefs::kSyncManaged, base::Value(false));
CreateServiceWithLocalSyncBackend();
InitializeForNthSync();
EXPECT_EQ(SyncService::DisableReasonSet(), service()->GetDisableReasons());
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
prefs()->SetManagedPref(prefs::kSyncManaged, base::Value(true));
EXPECT_EQ(SyncService::DisableReasonSet(), service()->GetDisableReasons());
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
prefs()->SetManagedPref(prefs::kSyncManaged, base::Value(false));
EXPECT_EQ(SyncService::DisableReasonSet(), service()->GetDisableReasons());
EXPECT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
}
TEST_F(SyncServiceImplTest, ConfigureDataTypeManagerReason) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForFirstSync();
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
EXPECT_EQ(CONFIGURE_REASON_NEW_CLIENT,
data_type_manager()->last_configure_reason_for_test());
service()->GetSetupInProgressHandle();
EXPECT_EQ(CONFIGURE_REASON_RECONFIGURATION,
data_type_manager()->last_configure_reason_for_test());
ShutdownAndDeleteService();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
EXPECT_EQ(CONFIGURE_REASON_NEWLY_ENABLED_DATA_TYPE,
data_type_manager()->last_configure_reason_for_test());
service()->GetSetupInProgressHandle();
EXPECT_EQ(CONFIGURE_REASON_RECONFIGURATION,
data_type_manager()->last_configure_reason_for_test());
ShutdownAndDeleteService();
}
TEST_F(SyncServiceImplTest, ShouldProvideDisableReasonsAfterShutdown) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForFirstSync();
service()->Shutdown();
EXPECT_FALSE(service()->GetDisableReasons().Empty());
}
TEST_F(SyncServiceImplTest, ShouldSendDataTypesToSyncInvalidationsService) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START,
{
{BOOKMARKS, false},
{DEVICE_INFO, true},
});
EXPECT_CALL(*sync_invalidations_service(),
SetInterestedDataTypes(AllOf(ContainsDataType(BOOKMARKS),
ContainsDataType(DEVICE_INFO))));
InitializeForNthSync();
EXPECT_TRUE(engine()->started_handling_invalidations());
}
TEST_F(SyncServiceImplTest, ShouldEnableAndDisableInvalidationsForSessions) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START,
{{SESSIONS, false}, {TYPED_URLS, false}});
InitializeForNthSync();
EXPECT_CALL(*sync_invalidations_service(),
SetInterestedDataTypes(ContainsDataType(SESSIONS)));
service()->SetInvalidationsForSessionsEnabled(true);
EXPECT_CALL(*sync_invalidations_service(),
SetInterestedDataTypes(Not(ContainsDataType(SESSIONS))));
service()->SetInvalidationsForSessionsEnabled(false);
}
TEST_F(SyncServiceImplTest, ShouldNotSubscribeToProxyTypes) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START,
{
{BOOKMARKS, false},
{DEVICE_INFO, true},
});
get_controller(BOOKMARKS)
->model()
->EnableSkipEngineConnectionForActivationResponse();
EXPECT_CALL(*sync_invalidations_service(),
SetInterestedDataTypes(AllOf(ContainsDataType(DEVICE_INFO),
Not(ContainsDataType(BOOKMARKS)))));
InitializeForNthSync();
}
TEST_F(SyncServiceImplTest,
ShouldActivateSyncInvalidationsServiceWhenSyncIsInitialized) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
EXPECT_CALL(*sync_invalidations_service(), StartListening())
.Times(AtLeast(1));
InitializeForFirstSync();
}
TEST_F(SyncServiceImplTest,
ShouldNotStartListeningInvalidationsWhenLocalSyncEnabled) {
CreateServiceWithLocalSyncBackend();
EXPECT_CALL(*sync_invalidations_service(), StartListening()).Times(0);
InitializeForFirstSync();
}
TEST_F(SyncServiceImplTest,
ShouldNotStopListeningPermanentlyOnShutdownBrowserAndKeepData) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForFirstSync();
EXPECT_CALL(*sync_invalidations_service(), StopListeningPermanently())
.Times(0);
ShutdownAndDeleteService();
}
TEST_F(SyncServiceImplTest,
ShouldStopListeningPermanentlyOnDisableSyncAndClearData) {
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForFirstSync();
EXPECT_CALL(*sync_invalidations_service(), StopListeningPermanently());
service()->StopAndClear();
}
TEST_F(SyncServiceImplTest, ShouldCallStopUponResetEngineIfAlreadyShutDown) {
base::test::ScopedFeatureList feature_list(
syncer::kSyncAllowClearingMetadataWhenDataTypeIsStopped);
SignIn();
CreateService(SyncServiceImpl::MANUAL_START);
InitializeForNthSync();
ASSERT_EQ(SyncService::TransportState::ACTIVE,
service()->GetTransportState());
service()->OnConnectionStatusChange(CONNECTION_AUTH_ERROR);
base::RunLoop().RunUntilIdle();
identity_test_env()->SetInvalidRefreshTokenForPrimaryAccount();
ASSERT_FALSE(service()->IsEngineInitialized());
ASSERT_EQ(SyncService::TransportState::PAUSED,
service()->GetTransportState());
EXPECT_EQ(0, get_controller(BOOKMARKS)->model()->clear_metadata_call_count());
service()->StopAndClear();
EXPECT_EQ(1, get_controller(BOOKMARKS)->model()->clear_metadata_call_count());
}
}
}