mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Remove CallNearbyIdentityApi flag.
PiperOrigin-RevId: 781101105
This commit is contained in:
committed by
Copybara-Service
parent
8f14bd10fc
commit
308c98044b
@@ -102,7 +102,6 @@ class FakeNearbyShareCertificateManager : public NearbyShareCertificateManager {
|
||||
void ClearPublicCertificates(std::function<void(bool)> callback) override;
|
||||
void SetVendorId(int32_t vendor_id) override {}
|
||||
std::string Dump() const override { return ""; }
|
||||
bool UsingIdentityRpc() override { return true; }
|
||||
|
||||
// Make protected methods from base class public in this fake class.
|
||||
using NearbyShareCertificateManager::NotifyPrivateCertificatesChanged;
|
||||
|
||||
@@ -132,9 +132,6 @@ class NearbyShareCertificateManager {
|
||||
// Dump certificates ID information for troubleshooting.
|
||||
virtual std::string Dump() const = 0;
|
||||
|
||||
// Returns true if configured to use Identity RPC.
|
||||
virtual bool UsingIdentityRpc() = 0;
|
||||
|
||||
protected:
|
||||
virtual void OnStart() = 0;
|
||||
virtual void OnStop() = 0;
|
||||
|
||||
@@ -293,9 +293,6 @@ NearbyShareCertificateManagerImpl::NearbyShareCertificateManagerImpl(
|
||||
})),
|
||||
executor_(context->CreateSequencedTaskRunner()) {
|
||||
local_device_data_manager_->AddObserver(this);
|
||||
if (!UsingIdentityRpc()) {
|
||||
contact_manager_->AddObserver(this);
|
||||
}
|
||||
}
|
||||
|
||||
NearbyShareCertificateManagerImpl::~NearbyShareCertificateManagerImpl() {
|
||||
@@ -336,10 +333,6 @@ void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
|
||||
});
|
||||
}
|
||||
|
||||
bool NearbyShareCertificateManagerImpl::UsingIdentityRpc() {
|
||||
return local_device_data_manager_->UsingIdentityRpc();
|
||||
}
|
||||
|
||||
void NearbyShareCertificateManagerImpl::CertificateDownloadContext::
|
||||
QuerySharedCredentialsFetchNextPage() {
|
||||
page_number_++;
|
||||
@@ -458,11 +451,7 @@ bool NearbyShareCertificateManagerImpl::DownloadPublicCertificatesInExecutor() {
|
||||
}
|
||||
download_succeeded = UpdatePublicCertificates(certificates);
|
||||
});
|
||||
if (UsingIdentityRpc()) {
|
||||
context->QuerySharedCredentialsFetchNextPage();
|
||||
} else {
|
||||
context->FetchNextPage();
|
||||
}
|
||||
context->QuerySharedCredentialsFetchNextPage();
|
||||
return download_succeeded;
|
||||
}
|
||||
|
||||
@@ -518,29 +507,18 @@ bool NearbyShareCertificateManagerImpl::UploadDeviceCertificatesInExecutor(
|
||||
bool upload_certificates_succeeded = false;
|
||||
bool regenerate_certificates = false;
|
||||
absl::Notification notification;
|
||||
if (local_device_data_manager_->UsingIdentityRpc()) {
|
||||
LOG(INFO) << __func__ << ": [Call Identity API] PublishDevice: upload "
|
||||
<< public_certs.size()
|
||||
<< " local device certificates, force_update_contacts = "
|
||||
<< force_update_contacts;
|
||||
local_device_data_manager_->PublishDevice(
|
||||
std::move(public_certs), force_update_contacts,
|
||||
[&upload_certificates_succeeded, ®enerate_certificates,
|
||||
¬ification](bool success, bool contact_removed) {
|
||||
upload_certificates_succeeded = success;
|
||||
regenerate_certificates = contact_removed;
|
||||
notification.Notify();
|
||||
});
|
||||
} else {
|
||||
LOG(INFO) << __func__
|
||||
<< ": [Call NearbyShare API] UploadCertificates: upload"
|
||||
<< public_certs.size() << " local device certificates.";
|
||||
local_device_data_manager_->UploadCertificates(
|
||||
std::move(public_certs), [&](bool success) {
|
||||
upload_certificates_succeeded = success;
|
||||
notification.Notify();
|
||||
});
|
||||
}
|
||||
LOG(INFO) << __func__ << ": [Call Identity API] PublishDevice: upload "
|
||||
<< public_certs.size()
|
||||
<< " local device certificates, force_update_contacts = "
|
||||
<< force_update_contacts;
|
||||
local_device_data_manager_->PublishDevice(
|
||||
std::move(public_certs), force_update_contacts,
|
||||
[&upload_certificates_succeeded, ®enerate_certificates,
|
||||
¬ification](bool success, bool contact_removed) {
|
||||
upload_certificates_succeeded = success;
|
||||
regenerate_certificates = contact_removed;
|
||||
notification.Notify();
|
||||
});
|
||||
notification.WaitForNotification();
|
||||
LOG(INFO) << "Upload local device certificates "
|
||||
<< (upload_certificates_succeeded ? "succeeded" : "failed")
|
||||
@@ -548,7 +526,7 @@ bool NearbyShareCertificateManagerImpl::UploadDeviceCertificatesInExecutor(
|
||||
if (!upload_certificates_succeeded) {
|
||||
return false;
|
||||
}
|
||||
if (regenerate_certificates && UsingIdentityRpc()) {
|
||||
if (regenerate_certificates) {
|
||||
LOG(INFO) << __func__
|
||||
<< ": [Call Identity API] Another call to PublishDevice after "
|
||||
"regenerating all Private certificates: ";
|
||||
|
||||
@@ -175,8 +175,6 @@ class NearbyShareCertificateManagerImpl
|
||||
// Dump certs information.
|
||||
std::string Dump() const override;
|
||||
|
||||
bool UsingIdentityRpc() override;
|
||||
|
||||
// Used by the private certificate expiration scheduler to determine the next
|
||||
// private certificate expiration time. Returns base::Time::Min() if
|
||||
// certificates are missing. This function never returns absl::nullopt.
|
||||
|
||||
@@ -142,9 +142,8 @@ class NearbyShareCertificateManagerImplTest
|
||||
NearbyShareCertificateStorageImpl::Factory::SetFactoryForTesting(nullptr);
|
||||
}
|
||||
|
||||
void Initialize(bool use_identity_rpc) {
|
||||
void Initialize() {
|
||||
// Setup Identity API.
|
||||
local_device_data_manager_->SetUsingIdentityRpc(use_identity_rpc);
|
||||
cert_manager_ = NearbyShareCertificateManagerImpl::Factory::Create(
|
||||
&fake_context_, mock_sharing_platform_,
|
||||
local_device_data_manager_.get(), contact_manager_.get(),
|
||||
@@ -235,24 +234,15 @@ class NearbyShareCertificateManagerImplTest
|
||||
}
|
||||
|
||||
void VerifyCertificatesUpload(bool expected_force_update_contacts) {
|
||||
if (local_device_data_manager_->UsingIdentityRpc()) {
|
||||
ASSERT_FALSE(local_device_data_manager_->publish_device_calls().empty());
|
||||
EXPECT_EQ(local_device_data_manager_->publish_device_calls()
|
||||
.back()
|
||||
.certificates.size(),
|
||||
2 * kNearbyShareNumPrivateCertificates);
|
||||
EXPECT_EQ(local_device_data_manager_->publish_device_calls()
|
||||
.back()
|
||||
.force_update_contacts,
|
||||
expected_force_update_contacts);
|
||||
} else {
|
||||
ASSERT_FALSE(
|
||||
local_device_data_manager_->upload_certificates_calls().empty());
|
||||
EXPECT_EQ(local_device_data_manager_->upload_certificates_calls()
|
||||
.back()
|
||||
.certificates.size(),
|
||||
2 * kNearbyShareNumPrivateCertificates);
|
||||
}
|
||||
ASSERT_FALSE(local_device_data_manager_->publish_device_calls().empty());
|
||||
EXPECT_EQ(local_device_data_manager_->publish_device_calls()
|
||||
.back()
|
||||
.certificates.size(),
|
||||
2 * kNearbyShareNumPrivateCertificates);
|
||||
EXPECT_EQ(local_device_data_manager_->publish_device_calls()
|
||||
.back()
|
||||
.force_update_contacts,
|
||||
expected_force_update_contacts);
|
||||
}
|
||||
|
||||
void InvokePrivateCertificateRefresh(bool expected_success) {
|
||||
@@ -317,23 +307,6 @@ class NearbyShareCertificateManagerImplTest
|
||||
kNearbyShareCertificateValidityPeriod);
|
||||
}
|
||||
|
||||
void InvokeCertificateUpload(bool expected_success) {
|
||||
size_t initial_num_upload_calls =
|
||||
local_device_data_manager_->upload_certificates_calls().size();
|
||||
local_device_data_manager_->SetUploadCertificatesResult(expected_success);
|
||||
size_t initial_num_handled_results =
|
||||
upload_scheduler_->handled_results().size();
|
||||
|
||||
upload_scheduler_->InvokeRequestCallback();
|
||||
Sync();
|
||||
EXPECT_EQ(local_device_data_manager_->upload_certificates_calls().size(),
|
||||
initial_num_upload_calls + 1);
|
||||
VerifyCertificatesUpload(/*expected_force_update_contacts=*/false);
|
||||
EXPECT_EQ(upload_scheduler_->handled_results().size(),
|
||||
initial_num_handled_results + 1);
|
||||
EXPECT_EQ(upload_scheduler_->handled_results().back(), expected_success);
|
||||
}
|
||||
|
||||
void InvokeCertUploadPublishDevice(bool contacts_removed,
|
||||
bool publish_device_success) {
|
||||
local_device_data_manager_->SetPublishDeviceResult(publish_device_success);
|
||||
@@ -568,7 +541,7 @@ class NearbyShareCertificateManagerImplTest
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
EncryptPrivateCertificateMetadataKey) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
// No certificates exist for hidden or unspecified visibility.
|
||||
EXPECT_FALSE(cert_manager_->EncryptPrivateCertificateMetadataKey(
|
||||
DeviceVisibility::DEVICE_VISIBILITY_HIDDEN));
|
||||
@@ -628,7 +601,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest, SignWithPrivateCertificate) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
NearbySharePrivateCertificate private_certificate =
|
||||
GetNearbyShareTestPrivateCertificate(
|
||||
DeviceVisibility::DEVICE_VISIBILITY_ALL_CONTACTS);
|
||||
@@ -668,7 +641,7 @@ TEST_F(NearbyShareCertificateManagerImplTest, SignWithPrivateCertificate) {
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
HashAuthenticationTokenWithPrivateCertificate) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
NearbySharePrivateCertificate private_certificate =
|
||||
GetNearbyShareTestPrivateCertificate(
|
||||
DeviceVisibility::DEVICE_VISIBILITY_ALL_CONTACTS);
|
||||
@@ -706,7 +679,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
GetDecryptedPublicCertificateSuccess) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
std::optional<NearbyShareDecryptedPublicCertificate> decrypted_pub_cert;
|
||||
cert_manager_->GetDecryptedPublicCertificate(
|
||||
metadata_encryption_keys_[0],
|
||||
@@ -725,7 +698,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
GetDecryptedPublicCertificateCertNotFound) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
auto private_cert = NearbySharePrivateCertificate(
|
||||
DeviceVisibility::DEVICE_VISIBILITY_ALL_CONTACTS, t0,
|
||||
GetNearbyShareTestMetadata());
|
||||
@@ -746,7 +719,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
GetDecryptedPublicCertificateGetPublicCertificatesFailure) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
std::optional<NearbyShareDecryptedPublicCertificate> decrypted_pub_cert;
|
||||
cert_manager_->GetDecryptedPublicCertificate(
|
||||
metadata_encryption_keys_[0],
|
||||
@@ -759,60 +732,29 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
EXPECT_FALSE(decrypted_pub_cert);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
DownloadPublicCertificatesSuccess) {
|
||||
Initialize(/*use_identity_rpc=*/false);
|
||||
ASSERT_NO_FATAL_FAILURE(DownloadPublicCertificatesFlow(
|
||||
/*num_pages=*/2, DownloadPublicCertificatesResult::kSuccess));
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
DownloadPublicCertificatesRPCFailure) {
|
||||
Initialize(/*use_identity_rpc=*/false);
|
||||
ASSERT_NO_FATAL_FAILURE(DownloadPublicCertificatesFlow(
|
||||
/*num_pages=*/2, DownloadPublicCertificatesResult::kHttpError));
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest, QuerySharedCredentialsSuccess) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
ASSERT_NO_FATAL_FAILURE(QuerySharedCredentialsFlow(
|
||||
/*num_pages=*/2, DownloadPublicCertificatesResult::kSuccess));
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
QuerySharedCredentialsRPCFailure) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
ASSERT_NO_FATAL_FAILURE(QuerySharedCredentialsFlow(
|
||||
/*num_pages=*/2, DownloadPublicCertificatesResult::kHttpError));
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest, ClearPublicCertificates) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
cert_manager_->ClearPublicCertificates([&](bool result) {});
|
||||
EXPECT_THAT(cert_store_->clear_public_certificates_callbacks(),
|
||||
::testing::SizeIs(1));
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
DownloadPublicCertificatesStoreFailure) {
|
||||
Initialize(/*use_identity_rpc=*/false);
|
||||
ASSERT_NO_FATAL_FAILURE(DownloadPublicCertificatesFlow(
|
||||
/*num_pages=*/2, DownloadPublicCertificatesResult::kStorageError));
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RefreshPrivateCertificates_NoCertificates_UploadSuccess) {
|
||||
Initialize(/*use_identity_rpc=*/false);
|
||||
cert_store_->ReplacePrivateCertificates({});
|
||||
|
||||
InvokePrivateCertificateRefresh(/*expected_success=*/true);
|
||||
|
||||
VerifyPrivateCertificates(/*expected_metadata=*/GetNearbyShareTestMetadata());
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RefreshPrivateCertificates_PublishDevice_NoCertificates_UploadSuccess) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
cert_store_->ReplacePrivateCertificates({});
|
||||
|
||||
InvokePrivateCertificateRefresh(/*expected_success=*/true);
|
||||
@@ -822,7 +764,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
ForceUploadPrivateCertificates_PublishDevice_Success) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
// All private certificates are valid.
|
||||
cert_store_->ReplacePrivateCertificates(private_certificates_);
|
||||
cert_manager_->ForceUploadPrivateCertificates();
|
||||
@@ -835,7 +777,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
ForceUploadPrivateCertificates_PublishDevice_ContactsRemoved) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
// All private certificates are valid.
|
||||
cert_store_->ReplacePrivateCertificates(private_certificates_);
|
||||
local_device_data_manager_->SetPublishDeviceContactsRemoved(
|
||||
@@ -850,7 +792,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
ForceUploadPrivateCertificates_NotLoggedIn_DoesNotCallPublishDevice) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
fake_account_manager_.SetAccount(std::nullopt);
|
||||
// All private certificates are valid.
|
||||
cert_store_->ReplacePrivateCertificates({});
|
||||
@@ -862,50 +804,9 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
EXPECT_EQ(local_device_data_manager_->publish_device_calls().size(), 0);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RefreshPrivateCertificates_NoCertificates_UploadFailure) {
|
||||
Initialize(/*use_identity_rpc=*/false);
|
||||
cert_store_->ReplacePrivateCertificates({});
|
||||
|
||||
InvokePrivateCertificateRefresh(/*expected_success=*/true);
|
||||
|
||||
VerifyPrivateCertificates(/*expected_metadata=*/GetNearbyShareTestMetadata());
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RevokePrivateCertificates_OnContactsUploaded) {
|
||||
Initialize(/*use_identity_rpc=*/false);
|
||||
// Destroy and recreate private certificates if contact data has changed since
|
||||
// the last successful upload.
|
||||
cert_manager_->Stop();
|
||||
for (bool did_contacts_change_since_last_upload : {true, false}) {
|
||||
cert_store_->ReplacePrivateCertificates(private_certificates_);
|
||||
contact_manager_->NotifyContactsUploaded(
|
||||
did_contacts_change_since_last_upload);
|
||||
|
||||
Sync();
|
||||
std::vector<NearbySharePrivateCertificate> certs =
|
||||
*cert_store_->GetPrivateCertificates();
|
||||
std::vector<std::string> cert_ids;
|
||||
for (const auto& cert : certs) {
|
||||
cert_ids.push_back(std::string(cert.id().begin(), cert.id().end()));
|
||||
}
|
||||
if (did_contacts_change_since_last_upload) {
|
||||
// New certificates should be generated.
|
||||
EXPECT_EQ(private_certificate_ids_.size(), cert_ids.size());
|
||||
EXPECT_THAT(cert_ids,
|
||||
Not(UnorderedElementsAreArray(private_certificate_ids_)));
|
||||
} else {
|
||||
EXPECT_THAT(private_certificate_ids_,
|
||||
UnorderedElementsAreArray(cert_ids.begin(), cert_ids.end()));
|
||||
}
|
||||
EXPECT_EQ(0, private_cert_exp_scheduler_->num_immediate_requests());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RefreshPrivateCertificates_OnLocalDeviceMetadataChanged) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
cert_manager_->Start();
|
||||
|
||||
// Destroy and recreate private certificates if any metadata fields change.
|
||||
@@ -940,34 +841,9 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RefreshPrivateCertificates_OnVendorIdChanged) {
|
||||
Initialize(/*use_identity_rpc=*/false);
|
||||
cert_store_->ReplacePrivateCertificates(private_certificates_);
|
||||
cert_manager_->Start();
|
||||
|
||||
cert_manager_->SetVendorId(12345);
|
||||
|
||||
Sync();
|
||||
std::vector<NearbySharePrivateCertificate> certs =
|
||||
*cert_store_->GetPrivateCertificates();
|
||||
std::vector<std::string> cert_ids;
|
||||
for (const auto& cert : certs) {
|
||||
cert_ids.push_back(std::string(cert.id().begin(), cert.id().end()));
|
||||
}
|
||||
// New certificates should be generated.
|
||||
EXPECT_EQ(private_certificate_ids_.size(), cert_ids.size());
|
||||
EXPECT_THAT(cert_ids,
|
||||
Not(UnorderedElementsAreArray(private_certificate_ids_)));
|
||||
|
||||
auto metadata = GetNearbyShareTestMetadata();
|
||||
metadata.set_vendor_id(12345);
|
||||
VerifyPrivateCertificates(/*expected_metadata=*/metadata);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RefreshPrivateCertificates_PublishDevice_OnVendorIdChanged) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
cert_store_->ReplacePrivateCertificates(private_certificates_);
|
||||
cert_manager_->Start();
|
||||
|
||||
@@ -990,22 +866,9 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
VerifyPrivateCertificates(/*expected_metadata=*/metadata);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RefreshPrivateCertificates_ExpiredCertificate) {
|
||||
Initialize(/*use_identity_rpc=*/false);
|
||||
// First certificates are expired;
|
||||
FastForward(kNearbyShareCertificateValidityPeriod * 1.5);
|
||||
cert_store_->ReplacePrivateCertificates(private_certificates_);
|
||||
|
||||
cert_manager_->Start();
|
||||
InvokePrivateCertificateRefresh(/*expected_success=*/true);
|
||||
|
||||
VerifyPrivateCertificates(/*expected_metadata=*/GetNearbyShareTestMetadata());
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RefreshPrivateCertificates_PublishDevice_ExpiredCertificate) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
// First certificates are expired;
|
||||
FastForward(kNearbyShareCertificateValidityPeriod * 1.5);
|
||||
cert_store_->ReplacePrivateCertificates(private_certificates_);
|
||||
@@ -1018,7 +881,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RefreshPrivateCertificates_BluetoothAdapterNotPresent) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
cert_store_->ReplacePrivateCertificates({});
|
||||
|
||||
SetBluetoothAdapterIsPresent(false);
|
||||
@@ -1029,33 +892,9 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
InvokePrivateCertificateRefresh(/*expected_success=*/false);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RefreshPrivateCertificates_MissingFullNameAndIconUrl) {
|
||||
Initialize(/*use_identity_rpc=*/false);
|
||||
cert_store_->ReplacePrivateCertificates({});
|
||||
|
||||
// Full name and icon URL are missing in the account.
|
||||
AccountManager::Account account{
|
||||
.email = kTestMetadataAccountName,
|
||||
};
|
||||
|
||||
fake_account_manager_.SetAccount(account);
|
||||
|
||||
cert_manager_->Start();
|
||||
InvokePrivateCertificateRefresh(/*expected_success=*/true);
|
||||
|
||||
// The full name and icon URL are not set.
|
||||
nearby::sharing::proto::EncryptedMetadata metadata =
|
||||
GetNearbyShareTestMetadata();
|
||||
metadata.clear_full_name();
|
||||
metadata.clear_icon_url();
|
||||
|
||||
VerifyPrivateCertificates(/*expected_metadata=*/metadata);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RemoveExpiredPublicCertificates_Success) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
// The public certificate expiration scheduler notifies the certificate
|
||||
// manager that a public certificate has expired.
|
||||
EXPECT_EQ(cert_store_->remove_expired_public_certificates_calls().size(), 0u);
|
||||
@@ -1072,7 +911,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
RemoveExpiredPublicCertificates_Failure) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
// The public certificate expiration scheduler notifies the certificate
|
||||
// manager that a public certificate has expired.
|
||||
EXPECT_EQ(cert_store_->remove_expired_public_certificates_calls().size(), 0u);
|
||||
@@ -1092,7 +931,7 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
TEST_F(
|
||||
NearbyShareCertificateManagerImplTest,
|
||||
ForceUploadPrivateCertificates_NoLogIn_DisablesPrivateCertExpirationTimer) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
fake_account_manager_.SetAccount(std::nullopt);
|
||||
// All private certificates are valid.
|
||||
cert_store_->ReplacePrivateCertificates({});
|
||||
@@ -1109,21 +948,9 @@ TEST_F(
|
||||
EXPECT_FALSE(next_schedule_time.has_value());
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
UploadCertificates_NoPrivateCertificates) {
|
||||
Initialize(/*use_identity_rpc=*/false);
|
||||
cert_store_->ReplacePrivateCertificates({});
|
||||
|
||||
upload_scheduler_->InvokeRequestCallback();
|
||||
Sync();
|
||||
EXPECT_EQ(local_device_data_manager_->upload_certificates_calls().size(), 0);
|
||||
EXPECT_EQ(upload_scheduler_->handled_results().size(), 1);
|
||||
EXPECT_EQ(upload_scheduler_->handled_results().back(), false);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
UploadCertificates_PublishDevice_NoPrivateCertificates) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
cert_store_->ReplacePrivateCertificates({});
|
||||
|
||||
upload_scheduler_->InvokeRequestCallback();
|
||||
@@ -1133,18 +960,9 @@ TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
EXPECT_EQ(upload_scheduler_->handled_results().back(), false);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest, UploadCertificates_Success) {
|
||||
Initialize(/*use_identity_rpc=*/false);
|
||||
cert_store_->ReplacePrivateCertificates(private_certificates_);
|
||||
|
||||
InvokeCertificateUpload(/*expected_success=*/true);
|
||||
|
||||
VerifyPrivateCertificates(/*expected_metadata=*/GetNearbyShareTestMetadata());
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareCertificateManagerImplTest,
|
||||
DownloadPublicCertificates_Success) {
|
||||
Initialize(/*use_identity_rpc=*/true);
|
||||
Initialize();
|
||||
|
||||
cert_manager_->DownloadPublicCertificates();
|
||||
|
||||
|
||||
@@ -174,8 +174,7 @@ NearbyShareContactManagerImpl::NearbyShareContactManagerImpl(
|
||||
/*require_connectivity=*/true,
|
||||
prefs::kNearbySharingSchedulerContactDownloadAndUploadName,
|
||||
[&] { DownloadContacts(); })),
|
||||
executor_(context->CreateSequencedTaskRunner()),
|
||||
use_identity_api_(local_device_data_manager_->UsingIdentityRpc()) {}
|
||||
executor_(context->CreateSequencedTaskRunner()) {}
|
||||
|
||||
NearbyShareContactManagerImpl::~NearbyShareContactManagerImpl() = default;
|
||||
|
||||
@@ -231,31 +230,7 @@ void NearbyShareContactManagerImpl::ContactDownloadContext::FetchNextPage() {
|
||||
}
|
||||
|
||||
void NearbyShareContactManagerImpl::DownloadContacts() {
|
||||
if (use_identity_api_) {
|
||||
LOG(INFO) << __func__ << ": [Call Identity API] Skipping DownloadContacts";
|
||||
return;
|
||||
}
|
||||
executor_->PostTask([this]() {
|
||||
LOG(INFO) << "Started to download contacts";
|
||||
if (!is_running()) {
|
||||
LOG(WARNING) << "Ignore contacts download, manager is not running.";
|
||||
return;
|
||||
}
|
||||
|
||||
if (!account_manager_.GetCurrentAccount().has_value()) {
|
||||
LOG(WARNING) << "Ignore contacts download, no logged in account.";
|
||||
contact_download_and_upload_scheduler_->HandleResult(/*success=*/true);
|
||||
return;
|
||||
}
|
||||
|
||||
// Currently Contacts download is synchronous. It completes after
|
||||
// FetchNextPage() returns.
|
||||
auto context = std::make_unique<ContactDownloadContext>(
|
||||
nearby_share_client_.get(),
|
||||
absl::bind_front(
|
||||
&NearbyShareContactManagerImpl::OnContactsDownloadCompleted, this));
|
||||
context->FetchNextPage();
|
||||
});
|
||||
LOG(INFO) << __func__ << ": [Call Identity API] Skipping DownloadContacts";
|
||||
}
|
||||
|
||||
void NearbyShareContactManagerImpl::GetContacts(ContactsCallback callback) {
|
||||
@@ -283,23 +258,15 @@ void NearbyShareContactManagerImpl::GetContacts(ContactsCallback callback) {
|
||||
}
|
||||
|
||||
void NearbyShareContactManagerImpl::OnStart() {
|
||||
if (use_identity_api_) {
|
||||
LOG(INFO) << __func__
|
||||
<< ": [Call Identity API] Skipping "
|
||||
"contact_download_and_upload_scheduler start.";
|
||||
return;
|
||||
}
|
||||
contact_download_and_upload_scheduler_->Start();
|
||||
LOG(INFO) << __func__
|
||||
<< ": [Call Identity API] Skipping "
|
||||
"contact_download_and_upload_scheduler start.";
|
||||
}
|
||||
|
||||
void NearbyShareContactManagerImpl::OnStop() {
|
||||
if (use_identity_api_) {
|
||||
LOG(INFO) << __func__
|
||||
<< ": [Call Identity API] Skipping "
|
||||
"contact_download_and_upload_scheduler stop.";
|
||||
return;
|
||||
}
|
||||
contact_download_and_upload_scheduler_->Stop();
|
||||
LOG(INFO) << __func__
|
||||
<< ": [Call Identity API] Skipping "
|
||||
"contact_download_and_upload_scheduler stop.";
|
||||
}
|
||||
|
||||
void NearbyShareContactManagerImpl::OnContactsDownloadSuccess(
|
||||
|
||||
@@ -149,9 +149,6 @@ class NearbyShareContactManagerImpl : public NearbyShareContactManager {
|
||||
std::unique_ptr<NearbyShareScheduler> contact_download_and_upload_scheduler_;
|
||||
|
||||
std::unique_ptr<TaskRunner> executor_ = nullptr;
|
||||
// Identity API does not support contacts upload/download. So essentially
|
||||
// contact manager is inactive.
|
||||
bool use_identity_api_ = false;
|
||||
};
|
||||
|
||||
} // namespace sharing
|
||||
|
||||
@@ -17,26 +17,18 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <random>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/container/flat_hash_set.h"
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/time/time.h"
|
||||
#include "internal/platform/implementation/account_manager.h"
|
||||
#include "internal/test/fake_account_manager.h"
|
||||
#include "sharing/common/nearby_share_prefs.h"
|
||||
#include "sharing/contacts/nearby_share_contact_manager.h"
|
||||
#include "sharing/contacts/nearby_share_contacts_sorter.h"
|
||||
#include "sharing/internal/api/fake_nearby_share_client.h"
|
||||
#include "sharing/internal/api/preference_manager.h"
|
||||
#include "sharing/internal/test/fake_context.h"
|
||||
@@ -53,104 +45,16 @@ namespace sharing {
|
||||
namespace {
|
||||
|
||||
using ::nearby::sharing::api::PreferenceManager;
|
||||
using ::nearby::sharing::proto::Contact;
|
||||
using ::nearby::sharing::proto::ContactRecord;
|
||||
|
||||
constexpr char kTestContactIdPrefix[] = "id_";
|
||||
constexpr char kTestContactEmailPrefix[] = "email_";
|
||||
constexpr char kTestContactPhonePrefix[] = "phone_";
|
||||
constexpr char kTestDefaultDeviceName[] = "Josh's Chromebook";
|
||||
constexpr char kTestProfileUserName[] = "test@google.com";
|
||||
constexpr char kTestAccountId[] = "test_account_id";
|
||||
constexpr char kTestDefaultContactsHash[] = "last_hash";
|
||||
const char* kTestPersonNames[] = {"BBB BBB", "CCC CCC", "AAA AAA"};
|
||||
|
||||
// From nearby_share_contact_manager_impl.cc.
|
||||
constexpr absl::Duration kContactDownloadPeriod = absl::Hours(12);
|
||||
|
||||
std::string GetTestContactId(size_t index) {
|
||||
return absl::StrCat(kTestContactIdPrefix, index);
|
||||
}
|
||||
std::string GetTestContactEmail(size_t index) {
|
||||
return absl::StrCat(kTestContactEmailPrefix, index);
|
||||
}
|
||||
std::string GetTestContactPhone(size_t index) {
|
||||
return absl::StrCat(kTestContactPhonePrefix, index);
|
||||
}
|
||||
|
||||
std::set<std::string> TestContactIds(size_t num_contacts) {
|
||||
std::set<std::string> ids;
|
||||
for (size_t i = 0; i < num_contacts; ++i) {
|
||||
ids.insert(GetTestContactId(i));
|
||||
}
|
||||
return ids;
|
||||
}
|
||||
|
||||
std::vector<ContactRecord> TestContactRecordList(size_t num_contacts) {
|
||||
std::vector<ContactRecord> contact_list;
|
||||
for (size_t i = 0; i < num_contacts; ++i) {
|
||||
ContactRecord contact;
|
||||
contact.set_id(GetTestContactId(i));
|
||||
contact.set_image_url("https://www.google.com/");
|
||||
contact.set_person_name(kTestPersonNames[i % 3]);
|
||||
contact.set_is_reachable(true);
|
||||
// only one of these fields should be set...
|
||||
switch ((i % 3)) {
|
||||
case 0:
|
||||
contact.add_identifiers()->set_account_name(GetTestContactEmail(i));
|
||||
break;
|
||||
case 1:
|
||||
contact.add_identifiers()->set_phone_number(GetTestContactPhone(i));
|
||||
break;
|
||||
case 2:
|
||||
contact.add_identifiers()->set_obfuscated_gaia("4938tyah");
|
||||
break;
|
||||
}
|
||||
contact_list.push_back(contact);
|
||||
}
|
||||
return contact_list;
|
||||
}
|
||||
|
||||
// Converts a list of ContactRecord protos, along with the allowlist, into a
|
||||
// list of Contact protos. To enable self-sharing across devices, we expect the
|
||||
// local device to include itself in the contact list as an allowed contact.
|
||||
// Partially from nearby_share_contact_manager_impl.cc.
|
||||
std::vector<Contact> BuildContactListToUpload(
|
||||
const std::vector<ContactRecord>& contact_records) {
|
||||
std::vector<Contact> contacts;
|
||||
for (const auto& contact_record : contact_records) {
|
||||
for (const auto& identifier : contact_record.identifiers()) {
|
||||
Contact contact;
|
||||
*contact.mutable_identifier() = identifier;
|
||||
contact.set_is_selected(/*is_selected=*/true);
|
||||
contacts.push_back(contact);
|
||||
}
|
||||
}
|
||||
|
||||
// Add self to list of contacts.
|
||||
Contact contact;
|
||||
contact.mutable_identifier()->set_account_name(kTestProfileUserName);
|
||||
contact.set_is_selected(true);
|
||||
contacts.push_back(contact);
|
||||
|
||||
return contacts;
|
||||
}
|
||||
|
||||
void VerifyDownloadNotificationContacts(
|
||||
const std::vector<ContactRecord>& expected_unordered_contacts,
|
||||
const std::vector<ContactRecord>& notification_contacts) {
|
||||
EXPECT_EQ(notification_contacts.size(), expected_unordered_contacts.size());
|
||||
|
||||
// Verify that observers receive contacts in sorted order.
|
||||
std::vector<ContactRecord> expected_ordered_contacts =
|
||||
expected_unordered_contacts;
|
||||
SortNearbyShareContactRecords(&expected_ordered_contacts);
|
||||
for (size_t i = 0; i < expected_ordered_contacts.size(); ++i) {
|
||||
EXPECT_EQ(notification_contacts[i].SerializeAsString(),
|
||||
expected_ordered_contacts[i].SerializeAsString());
|
||||
}
|
||||
}
|
||||
|
||||
class NearbyShareContactManagerImplTest
|
||||
: public ::testing::Test,
|
||||
public NearbyShareContactManager::Observer {
|
||||
@@ -166,7 +70,6 @@ class NearbyShareContactManagerImplTest
|
||||
NearbyShareContactManagerImplTest()
|
||||
: local_device_data_manager_(kTestDefaultDeviceName) {
|
||||
local_device_data_manager_.set_is_sync_mode(true);
|
||||
local_device_data_manager_.SetUsingIdentityRpc(false);
|
||||
}
|
||||
|
||||
~NearbyShareContactManagerImplTest() override = default;
|
||||
@@ -205,100 +108,6 @@ class NearbyShareContactManagerImplTest
|
||||
absl::Milliseconds(1000)));
|
||||
}
|
||||
|
||||
void SetUploadResult(bool success) {
|
||||
local_device_data_manager_.SetUploadContactsResult(success);
|
||||
}
|
||||
|
||||
void SetDownloadSuccessResult(const std::vector<ContactRecord>& contacts) {
|
||||
proto::ListContactPeopleResponse response;
|
||||
response.set_next_page_token(nullptr);
|
||||
response.mutable_contact_records()->Add(contacts.begin(), contacts.end());
|
||||
std::vector<absl::StatusOr<proto::ListContactPeopleResponse>> responses;
|
||||
responses.push_back(response);
|
||||
client()->SetListContactPeopleResponses(responses);
|
||||
}
|
||||
|
||||
void SetDownloadFailureResult() {
|
||||
std::vector<absl::StatusOr<proto::ListContactPeopleResponse>> responses;
|
||||
responses.push_back(absl::InternalError(""));
|
||||
client()->SetListContactPeopleResponses(responses);
|
||||
}
|
||||
|
||||
void DownloadContacts(bool download_success, bool expect_upload,
|
||||
bool upload_success,
|
||||
std::optional<std::vector<ContactRecord>> contacts,
|
||||
bool expect_contacts_changed,
|
||||
std::optional<std::vector<ContactRecord>>
|
||||
expected_contacts = std::nullopt) {
|
||||
// Track for download contacts.
|
||||
size_t num_handled_results =
|
||||
download_and_upload_scheduler()->handled_results().size();
|
||||
size_t num_download_notifications =
|
||||
contacts_downloaded_notifications_.size();
|
||||
size_t num_upload_contacts_calls =
|
||||
local_device_data_manager_.upload_contacts_calls().size();
|
||||
// Invoke upload callback from local device data manager.
|
||||
size_t num_upload_notifications = contacts_uploaded_notifications_.size();
|
||||
size_t num_download_and_upload_handled_results =
|
||||
download_and_upload_scheduler()->handled_results().size();
|
||||
|
||||
manager_->DownloadContacts();
|
||||
Sync();
|
||||
if (download_success) {
|
||||
VerifyDownloadNotificationSent(
|
||||
/*initial_num_notifications=*/num_download_notifications,
|
||||
expected_contacts.has_value() ? *expected_contacts : *contacts);
|
||||
|
||||
// Verify that contacts start uploading if needed.
|
||||
EXPECT_EQ(local_device_data_manager_.upload_contacts_calls().size(),
|
||||
num_upload_contacts_calls + (expect_upload ? 1 : 0));
|
||||
|
||||
// Verify that the success result is sent to the download/upload scheduler
|
||||
// if a subsequent upload isn't required.
|
||||
EXPECT_EQ(download_and_upload_scheduler()->handled_results().size(),
|
||||
num_handled_results + 1);
|
||||
if (!expect_upload) {
|
||||
EXPECT_TRUE(download_and_upload_scheduler()->handled_results().back());
|
||||
return;
|
||||
}
|
||||
|
||||
// Check on upload.
|
||||
FakeNearbyShareLocalDeviceDataManager::UploadContactsCall& call =
|
||||
local_device_data_manager_.upload_contacts_calls().back();
|
||||
|
||||
std::vector<Contact> expected_upload_contacts = BuildContactListToUpload(
|
||||
expected_contacts.has_value() ? *expected_contacts : *contacts);
|
||||
// Ordering doesn't matter. Otherwise, because of internal sorting,
|
||||
// comparison would be difficult.
|
||||
ASSERT_EQ(expected_upload_contacts.size(), call.contacts.size());
|
||||
absl::flat_hash_set<std::string> expected_contacts_set;
|
||||
absl::flat_hash_set<std::string> call_contacts_set;
|
||||
for (size_t i = 0; i < expected_upload_contacts.size(); ++i) {
|
||||
expected_contacts_set.insert(
|
||||
expected_upload_contacts[i].SerializeAsString());
|
||||
call_contacts_set.insert(call.contacts[i].SerializeAsString());
|
||||
}
|
||||
|
||||
// Verify upload notification was sent on success.
|
||||
EXPECT_EQ(contacts_uploaded_notifications_.size(),
|
||||
num_upload_notifications + (upload_success ? 1 : 0));
|
||||
if (upload_success) {
|
||||
EXPECT_EQ(contacts_uploaded_notifications_.back()
|
||||
.did_contacts_change_since_last_upload,
|
||||
expect_contacts_changed);
|
||||
}
|
||||
// Verify that the result is sent to download/upload scheduler.
|
||||
EXPECT_EQ(download_and_upload_scheduler()->handled_results().size(),
|
||||
num_download_and_upload_handled_results + 1);
|
||||
EXPECT_EQ(download_and_upload_scheduler()->handled_results().back(),
|
||||
upload_success);
|
||||
} else {
|
||||
EXPECT_EQ(download_and_upload_scheduler()->handled_results().size(),
|
||||
num_handled_results + 1);
|
||||
EXPECT_FALSE(download_and_upload_scheduler()->handled_results().back());
|
||||
}
|
||||
}
|
||||
|
||||
PreferenceManager& preference_manager() { return preference_manager_; }
|
||||
|
||||
std::vector<ContactsDownloadedNotification>&
|
||||
@@ -349,23 +158,6 @@ class NearbyShareContactManagerImplTest
|
||||
EXPECT_TRUE(download_and_upload_scheduler_instance.require_connectivity);
|
||||
}
|
||||
|
||||
void TriggerDownloadScheduler() {
|
||||
// Fire scheduler and verify downloader creation.
|
||||
download_and_upload_scheduler()->InvokeRequestCallback();
|
||||
}
|
||||
|
||||
void VerifyDownloadNotificationSent(
|
||||
size_t initial_num_notifications,
|
||||
const std::vector<ContactRecord>& expected_unordered_contacts) {
|
||||
EXPECT_EQ(contacts_downloaded_notifications_.size(),
|
||||
initial_num_notifications + 1);
|
||||
|
||||
// Verify notification sent to regular (not mojo) observers.
|
||||
VerifyDownloadNotificationContacts(
|
||||
expected_unordered_contacts,
|
||||
contacts_downloaded_notifications_.back().contacts);
|
||||
}
|
||||
|
||||
nearby::FakePreferenceManager preference_manager_;
|
||||
FakeAccountManager fake_account_manager_;
|
||||
FakeContext fake_context_;
|
||||
@@ -379,247 +171,6 @@ class NearbyShareContactManagerImplTest
|
||||
std::unique_ptr<NearbyShareContactManager> manager_;
|
||||
};
|
||||
|
||||
TEST_F(NearbyShareContactManagerImplTest, DownloadContacts_WithFirstUpload) {
|
||||
// Clear contact hash
|
||||
preference_manager().SetString(prefs::kNearbySharingContactUploadHashName,
|
||||
"");
|
||||
std::vector<ContactRecord> contact_records =
|
||||
TestContactRecordList(/*num_contacts=*/4u);
|
||||
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(true);
|
||||
// Because contacts have never been uploaded, a subsequent upload should be
|
||||
// requested, which succeeds.
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true);
|
||||
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(true);
|
||||
// When contacts are downloaded again, we detect that contacts have not
|
||||
// changed, so no upload should be made
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/false,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/false);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareContactManagerImplTest,
|
||||
DownloadContacts_DetectContactListChanged) {
|
||||
std::vector<ContactRecord> contact_records =
|
||||
TestContactRecordList(/*num_contacts=*/3u);
|
||||
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(true);
|
||||
// Because contacts have never been uploaded, a subsequent upload is
|
||||
// requested, which succeeds.
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true);
|
||||
|
||||
// When contacts are downloaded again, we detect that contacts have changed
|
||||
// since the last upload.
|
||||
contact_records = TestContactRecordList(/*num_contacts=*/4u);
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(true);
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareContactManagerImplTest,
|
||||
DownloadContacts_HashExpiration) {
|
||||
std::vector<ContactRecord> contact_records =
|
||||
TestContactRecordList(/*num_contacts=*/3u);
|
||||
std::set<std::string> allowlist = TestContactIds(/*num_contacts=*/2u);
|
||||
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(true);
|
||||
|
||||
// Because contacts have never been uploaded, a subsequent upload is
|
||||
// requested, which succeeds.
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true);
|
||||
|
||||
fake_context().fake_clock()->FastForward(absl::Hours(72));
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(true);
|
||||
// When contacts are downloaded again, we detect that contacts have not
|
||||
// changed, but since the hash expired, we upload again.
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareContactManagerImplTest,
|
||||
DownloadContacts_HashExpirationUploadFailed) {
|
||||
std::vector<ContactRecord> contact_records =
|
||||
TestContactRecordList(/*num_contacts=*/3u);
|
||||
std::set<std::string> allowlist = TestContactIds(/*num_contacts=*/2u);
|
||||
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(true);
|
||||
|
||||
// Because contacts have never been uploaded, a subsequent upload is
|
||||
// requested, which succeeds.
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true);
|
||||
|
||||
fake_context().fake_clock()->FastForward(absl::Hours(72));
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(false);
|
||||
// When contacts are downloaded again, we detect that contacts have not
|
||||
// changed, but since the hash expired, we upload again.
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/false,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true);
|
||||
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(true);
|
||||
// When contacts are downloaded again, we detect that contacts have not
|
||||
// changed, last upload failed, we upload again.
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareContactManagerImplTest, DownloadContacts_FailDownload) {
|
||||
SetDownloadFailureResult();
|
||||
DownloadContacts(/*download_success=*/false, /*expect_upload=*/false,
|
||||
/*upload_success=*/false,
|
||||
/*contacts=*/std::nullopt,
|
||||
/*expect_contacts_changed=*/false);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareContactManagerImplTest, DownloadContacts_RetryFailedUpload) {
|
||||
std::vector<ContactRecord> contact_records =
|
||||
TestContactRecordList(/*num_contacts=*/3u);
|
||||
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(true);
|
||||
|
||||
// Because contacts have never been uploaded, a subsequent upload is
|
||||
// requested, which succeeds.
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true);
|
||||
|
||||
// When contacts are downloaded again, we detect that contacts have changed
|
||||
// since the last upload. Fail this upload.
|
||||
contact_records = TestContactRecordList(/*num_contacts=*/4u);
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(false);
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/false,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true);
|
||||
|
||||
// When contacts are downloaded again, we should continue to indicate that
|
||||
// contacts have changed since the last upload, and attempt another upload.
|
||||
// (In other words, this tests that the contact-upload hash isn't updated
|
||||
// prematurely.)
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(true);
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareContactManagerImplTest,
|
||||
DownloadContacts_ListIsFilteredByReachable) {
|
||||
std::vector<ContactRecord> contact_records =
|
||||
TestContactRecordList(/*num_contacts=*/3u);
|
||||
|
||||
contact_records[0].set_is_reachable(false);
|
||||
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(true);
|
||||
|
||||
// Because contacts have never been uploaded, a subsequent upload is
|
||||
// requested, which succeeds.
|
||||
std::vector<ContactRecord> expected_contacts = contact_records;
|
||||
expected_contacts.erase(expected_contacts.begin());
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true,
|
||||
/*expected_contacts=*/expected_contacts);
|
||||
|
||||
ASSERT_EQ(contacts_downloaded_notifications().size(), 1);
|
||||
// We should have 1 filtered contact since we only marked one as unreachable.
|
||||
EXPECT_EQ(contacts_downloaded_notifications()
|
||||
.front()
|
||||
.num_unreachable_contacts_filtered_out,
|
||||
1);
|
||||
// We should have 2 contacts in the notification since we only marked one as
|
||||
// unreachable, out of 3.
|
||||
EXPECT_EQ(contacts_downloaded_notifications().front().contacts.size(), 2);
|
||||
}
|
||||
|
||||
TEST_F(NearbyShareContactManagerImplTest, ContactUploadHash) {
|
||||
EXPECT_EQ(preference_manager().GetString(
|
||||
prefs::kNearbySharingContactUploadHashName, std::string()),
|
||||
std::string(kTestDefaultContactsHash));
|
||||
|
||||
std::vector<ContactRecord> contact_records =
|
||||
TestContactRecordList(/*num_contacts=*/10u);
|
||||
|
||||
SetDownloadSuccessResult(contact_records);
|
||||
SetUploadResult(true);
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/true,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/contact_records,
|
||||
/*expect_contacts_changed=*/true);
|
||||
|
||||
// Hardcode expected contact upload hash to ensure that hashed value is
|
||||
// consistent across process starts. If this test starts to fail, check one
|
||||
// of the following:
|
||||
// 1. Did the test data change? No worries; just update this hash value.
|
||||
// 2. Did the hashing function change? As long as the function is stable
|
||||
// across (most) process starts, then everything is okay; just update
|
||||
// this hash value. A changed hash value will result in an extra
|
||||
// server call, so as long as the value is stable for the most part,
|
||||
// it's okay.
|
||||
const char kExpectedHash[] =
|
||||
"DEE0B27BDA7B56EEA8F7C9A46313D67170D0707A484A541DE907DD9CACE5935E";
|
||||
EXPECT_EQ(kExpectedHash,
|
||||
preference_manager().GetString(
|
||||
prefs::kNearbySharingContactUploadHashName, std::string()));
|
||||
|
||||
// Try a few different permutations of contacts to ensure that the hash is
|
||||
// invariant under ordering.
|
||||
std::default_random_engine rng;
|
||||
for (size_t i = 0; i < 10u; ++i) {
|
||||
// We do not expect an upload because the contacts did not change in any
|
||||
// way other than ordering.
|
||||
std::vector<ContactRecord> shuffled_contacts = contact_records;
|
||||
std::shuffle(shuffled_contacts.begin(), shuffled_contacts.end(), rng);
|
||||
|
||||
SetDownloadSuccessResult(shuffled_contacts);
|
||||
SetUploadResult(true);
|
||||
DownloadContacts(/*download_success=*/true, /*expect_upload=*/false,
|
||||
/*upload_success=*/true,
|
||||
/*contacts=*/shuffled_contacts,
|
||||
/*expect_contacts_changed=*/false);
|
||||
|
||||
EXPECT_EQ(preference_manager().GetString(
|
||||
prefs::kNearbySharingContactUploadHashName, std::string()),
|
||||
kExpectedHash);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace sharing
|
||||
} // namespace nearby
|
||||
|
||||
@@ -70,9 +70,6 @@ constexpr auto kShowAutoUpdateSetting =
|
||||
// When true, use gRpc client to access backend.
|
||||
constexpr auto kUseGrpcClient =
|
||||
flags::Flag<bool>(kConfigPackage, "45630055", false);
|
||||
// When true, call the 3P Nearby Identity API instead of the 1P private API
|
||||
constexpr auto kCallNearbyIdentityApi =
|
||||
flags::Flag<bool>(kConfigPackage, "45667328", false);
|
||||
// When true, delete the file payload which received unexpectedly.
|
||||
constexpr auto kDeleteUnexpectedReceivedFileFix =
|
||||
flags::Flag<bool>(kConfigPackage, "45657036", false);
|
||||
@@ -127,7 +124,6 @@ inline absl::btree_map<int, const flags::Flag<bool>&> GetBoolFlags() {
|
||||
{45411353, kSenderSkipsConfirmation},
|
||||
{45409033, kShowAutoUpdateSetting},
|
||||
{45630055, kUseGrpcClient},
|
||||
{45667328, kCallNearbyIdentityApi},
|
||||
{45657036, kDeleteUnexpectedReceivedFileFix},
|
||||
{45673628, kEnableWifiHotspotForHpRealtekDevices},
|
||||
{45665616, kHonor3PClientIdAndSecret},
|
||||
|
||||
@@ -125,11 +125,6 @@ class FakeNearbyShareLocalDeviceDataManager
|
||||
std::vector<nearby::sharing::proto::PublicCertificate> certificates,
|
||||
bool force_update_contacts, PublishDeviceCallback callback) override;
|
||||
|
||||
bool UsingIdentityRpc() override { return using_identity_rpc_; }
|
||||
void SetUsingIdentityRpc(bool using_identity_rpc) {
|
||||
using_identity_rpc_ = using_identity_rpc;
|
||||
}
|
||||
|
||||
// Make protected observer-notification methods from the base class public in
|
||||
// this fake class.
|
||||
using NearbyShareLocalDeviceDataManager::NotifyLocalDeviceDataChanged;
|
||||
@@ -188,7 +183,6 @@ class FakeNearbyShareLocalDeviceDataManager
|
||||
bool upload_certificate_result_ = false;
|
||||
bool publish_device_result_ = false;
|
||||
bool publish_device_contact_removed_ = false;
|
||||
bool using_identity_rpc_ = true;
|
||||
};
|
||||
|
||||
} // namespace sharing
|
||||
|
||||
@@ -104,9 +104,6 @@ class NearbyShareLocalDeviceDataManager {
|
||||
std::vector<nearby::sharing::proto::PublicCertificate> certificates,
|
||||
bool force_update_contacts, PublishDeviceCallback callback) = 0;
|
||||
|
||||
// Returns true if configured to use Identity RPC.
|
||||
virtual bool UsingIdentityRpc() = 0;
|
||||
|
||||
protected:
|
||||
void NotifyLocalDeviceDataChanged(bool did_device_name_change,
|
||||
bool did_full_name_change,
|
||||
|
||||
@@ -209,10 +209,6 @@ void NearbyShareLocalDeviceDataManagerImpl::UploadContacts(
|
||||
});
|
||||
}
|
||||
|
||||
bool NearbyShareLocalDeviceDataManagerImpl::UsingIdentityRpc() {
|
||||
return nearby_identity_client_ != nullptr;
|
||||
}
|
||||
|
||||
void NearbyShareLocalDeviceDataManagerImpl::PublishDevice(
|
||||
std::vector<nearby::sharing::proto::PublicCertificate> certificates,
|
||||
bool force_update_contacts, PublishDeviceCallback callback) {
|
||||
|
||||
@@ -88,8 +88,6 @@ class NearbyShareLocalDeviceDataManagerImpl
|
||||
std::vector<nearby::sharing::proto::PublicCertificate> certificates,
|
||||
bool force_update_contacts, PublishDeviceCallback callback) override;
|
||||
|
||||
bool UsingIdentityRpc() override;
|
||||
|
||||
// Creates a default device name of the form "<given name>'s <device type>."
|
||||
// For example, "Josh's Chromebook." If a given name cannot be found, returns
|
||||
// just the device type. If the resulting name is too long the user's name
|
||||
|
||||
@@ -670,13 +670,11 @@ void NearbySharingServiceImpl::RegisterReceiveSurface(
|
||||
<< background_receive_callbacks_map_.size();
|
||||
|
||||
if (IsVisibleInBackground(settings_->GetVisibility())) {
|
||||
if (certificate_manager_->UsingIdentityRpc()) {
|
||||
// The Identity API does not support contact manager which triggers
|
||||
// Certificate refresh in DownloadContacts. Force upload explicitly.
|
||||
VLOG(1) << __func__
|
||||
<< ": [Call Identity API] ForceUploadPrivateCertificates.";
|
||||
certificate_manager_->ForceUploadPrivateCertificates();
|
||||
}
|
||||
// The Identity API does not support contact manager which triggers
|
||||
// Certificate refresh in DownloadContacts. Force upload explicitly.
|
||||
VLOG(1) << __func__
|
||||
<< ": [Call Identity API] ForceUploadPrivateCertificates.";
|
||||
certificate_manager_->ForceUploadPrivateCertificates();
|
||||
}
|
||||
InvalidateReceiveSurfaceState();
|
||||
std::move(status_codes_callback)(StatusCodes::kOk);
|
||||
|
||||
Reference in New Issue
Block a user