#include "components/autofill/core/browser/webdata/payments/autofill_wallet_metadata_sync_bridge.h"
#include <stddef.h>
#include <algorithm>
#include <memory>
#include <sstream>
#include <utility>
#include "base/base64.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/data_model/autofill_profile.h"
#include "components/autofill/core/browser/data_model/credit_card.h"
#include "components/autofill/core/browser/test_autofill_clock.h"
#include "components/autofill/core/browser/webdata/autofill_sync_metadata_table.h"
#include "components/autofill/core/browser/webdata/mock_autofill_webdata_backend.h"
#include "components/autofill/core/browser/webdata/payments/payments_autofill_table.h"
#include "components/autofill/core/browser/webdata/payments/payments_sync_bridge_test_util.h"
#include "components/autofill/core/browser/webdata/payments/payments_sync_bridge_util.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/os_crypt/sync/os_crypt_mocker.h"
#include "components/sync/base/client_tag_hash.h"
#include "components/sync/base/data_type.h"
#include "components/sync/engine/data_type_activation_response.h"
#include "components/sync/model/client_tag_based_data_type_processor.h"
#include "components/sync/model/data_batch.h"
#include "components/sync/protocol/autofill_specifics.pb.h"
#include "components/sync/protocol/data_type_state.pb.h"
#include "components/sync/protocol/entity_data.h"
#include "components/sync/protocol/entity_metadata.pb.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/test/mock_commit_queue.h"
#include "components/sync/test/mock_data_type_local_change_processor.h"
#include "components/webdata/common/web_database.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace autofill {
namespace {
ScopedTempDir;
IbanChangeKey;
WalletMetadataSpecifics;
DataBatch;
DataType;
EntityData;
KeyAndData;
MockDataTypeLocalChangeProcessor;
_;
ElementsAre;
ElementsAreArray;
IsEmpty;
Return;
UnorderedElementsAre;
constexpr char kAddr1ServerId[] = …;
constexpr char kAddr2ServerId[] = …;
constexpr char kCard1ServerId[] = …;
constexpr char kCard2ServerId[] = …;
constexpr Iban::InstrumentId kIban1InstrumentId = …;
constexpr Iban::InstrumentId kIban2InstrumentId = …;
constexpr char kAddr1SpecificsId[] = …;
constexpr char kCard1SpecificsId[] = …;
constexpr char kCard2SpecificsId[] = …;
constexpr char kIban1SpecificsId[] = …;
constexpr char kIban2SpecificsId[] = …;
const std::string kCard1StorageKey = …;
const std::string kIban1StorageKey = …;
const char kCard1SyncTag[] = …;
const char kIban1SyncTag[] = …;
const char kLocalAddr1ServerId[] = …;
const char kLocalAddr2ServerId[] = …;
const char kDefaultCacheGuid[] = …;
base::Time UseDateFromProtoValue(int64_t use_date_proto_value) { … }
const base::Time kDefaultTime = …;
int64_t UseDateToProtoValue(base::Time use_date) { … }
std::string GetCardStorageKey(const std::string& specifics_id) { … }
std::string GetIbanStorageKey(const std::string& specifics_id) { … }
WalletMetadataSpecifics CreateWalletMetadataSpecificsForCardWithDetails(
const std::string& specifics_id,
size_t use_count,
int64_t use_date,
const std::string& billing_address_id = "") { … }
WalletMetadataSpecifics CreateWalletMetadataSpecificsForCard(
const std::string& specifics_id) { … }
WalletMetadataSpecifics CreateWalletMetadataSpecificsForIbanWithDetails(
const std::string& specifics_id,
size_t use_count = 1,
int64_t use_date = UseDateToProtoValue(kDefaultTime)) { … }
CreditCard CreateServerCreditCardWithDetails(
const std::string& server_id,
size_t use_count,
int64_t use_date,
const std::string& billing_address_id = "") { … }
Iban CreateServerIbanWithDetails(
Iban::InstrumentId instrument_id,
size_t use_count = 1,
int64_t use_date = UseDateToProtoValue(kDefaultTime)) { … }
CreditCard CreateLocalCreditCardWithDetails(size_t use_count,
int64_t use_date) { … }
CreditCard CreateServerCreditCardFromSpecifics(
const WalletMetadataSpecifics& specifics) { … }
Iban CreateServerIbanFromSpecifics(const WalletMetadataSpecifics& specifics) { … }
void ExtractWalletMetadataSpecificsFromDataBatch(
std::unique_ptr<DataBatch> batch,
std::vector<WalletMetadataSpecifics>* output) { … }
std::string WalletMetadataSpecificsAsDebugString(
const WalletMetadataSpecifics& specifics) { … }
std::vector<std::string> GetSortedSerializedSpecifics(
const std::vector<WalletMetadataSpecifics>& specifics) { … }
MATCHER_P(EqualsSpecifics, expected, "") { … }
MATCHER_P(HasSpecifics, expected, "") { … }
}
class AutofillWalletMetadataSyncBridgeTest : public testing::Test { … };
TEST_F(AutofillWalletMetadataSyncBridgeTest, GetClientTagForCard) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest, GetClientTagForIban) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest, GetStorageKeyForCard) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest, GetStorageKeyForIban) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
GetAllDataForDebugging_ShouldReturnAllData_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
GetAllDataForDebugging_ShouldReturnAllData_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
GetData_ShouldNotReturnNonexistentData) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
GetData_ShouldReturnSelectedData_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
GetData_ShouldReturnSelectedData_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
GetData_ShouldReturnCompleteData_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
GetData_ShouldReturnCompleteData_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
ApplyDisableSyncChanges_ShouldWipeLocalDataWhenSyncStopped_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
ApplyDisableSyncChanges_ShouldWipeLocalDataWhenSyncStopped_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
DontSendLowerValueToServerOnUpdate_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
DontSendLowerValueToServerOnCreation_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
SendHigherValuesToServerOnLocalUpdate_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
SendHigherValuesToServerOnLocalUpdate_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
SendNewDataToServerOnLocalAddition_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
SendNewDataToServerOnLocalAddition_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
SendNewDataToServerOnLocalUpdate_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
SendNewDataToServerOnLocalUpdate_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
DeleteExistingDataFromServerOnLocalDeletion_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
DeleteExistingAllDataFromServerOnLocalDeletion_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
DoNotDeleteNonExistingDataFromServerOnLocalDeletion_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
DeleteExistingDataOnlyFromServerOnLocalDeletion_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest, DoNotPropagateNonSyncCards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
DeleteOldOrphanMetadataOnStartup_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
DeleteOldOrphanMetadataOnStartup_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
DoNotDeleteOldNonOrphanMetadataOnStartup_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
DoNotDeleteOldNonOrphanMetadataOnStartup_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
DoNotDeleteRecentOrphanMetadataOnStartup_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
DoNotDeleteRecentOrphanMetadataOnStartup_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
InitialSync_UploadUniqueLocalData_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
InitialSync_UploadUniqueLocalData_Ibans) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
InitialSync_UploadOnlyUniqueLocalData) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
RemoteDeletion_ShouldNotDeleteExistingLocalData_Cards) { … }
TEST_F(AutofillWalletMetadataSyncBridgeTest,
RemoteDeletion_ShouldNotDeleteExistingLocalData_Ibans) { … }
enum RemoteChangesMode { … };
class AutofillWalletMetadataSyncBridgeRemoteChangesTest
: public testing::WithParamInterface<RemoteChangesMode>,
public AutofillWalletMetadataSyncBridgeTest { … };
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest, EmptyUpdateIgnored) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
SameDataIgnored_Cards) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
SameDataIgnored_Ibans) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_PreferHigherValues_RemoteWins_Cards) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_PreferHigherValues_RemoteWins_Ibans) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_PreferHigherValues_LocalWins_Cards) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_PreferHigherValues_LocalWins_Ibans) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_PreferHigherValues_BothWin1_Cards) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_PreferHigherValues_BothWin1_Ibans) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_PreferHigherValues_BothWin2_Cards) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_PreferHigherValues_BothWin2_Ibans) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_PreferRemoteIfLocalHasInitialUseCount_Cards) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_PreferRemoteIfLocalHasInitialUseCount_Ibans) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_Card_PreferLocalBillingAddressId_RemoteWins) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_Card_PreferLocalBillingAddressId_LocalWins) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_Card_PreferNewerBillingAddressOutOfLocalIds_RemoteWins) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_Card_PreferNewerBillingAddressOutOfLocalIds_LocalWins) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_Card_PreferNewerBillingAddressOutOfServerIds_RemoteWins) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_Card_PreferNewerBillingAddressOutOfServerIds_LocalWins) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_Card_PreferNewerBillingAddressOutOfServerIds_BothWin1) { … }
TEST_P(AutofillWalletMetadataSyncBridgeRemoteChangesTest,
Conflict_Card_PreferNewerBillingAddressOutOfServerIds_BothWin2) { … }
INSTANTIATE_TEST_SUITE_P(…);
}
namespace sync_pb {
void PrintTo(const WalletMetadataSpecifics& specifics, std::ostream* os) { … }
}