From 1b7142da77f7d9f892dbd465d5e476d0d7138cfe Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Mon, 22 May 2023 11:50:05 -0700 Subject: [PATCH] Plumb CredentialManager::UpdateRemotePublicCredentials and CredentialManager::GetPublicCredentials through to PresenceService PiperOrigin-RevId: 534136547 --- presence/implementation/credential_manager.h | 3 +- .../implementation/credential_manager_impl.h | 3 +- presence/implementation/service_controller.h | 8 +++ .../implementation/service_controller_impl.cc | 17 ++++++ .../implementation/service_controller_impl.h | 8 +++ presence/presence_service.cc | 17 ++++++ presence/presence_service.h | 9 +++ presence/presence_service_test.cc | 59 +++++++++++++++++++ 8 files changed, 122 insertions(+), 2 deletions(-) diff --git a/presence/implementation/credential_manager.h b/presence/implementation/credential_manager.h index 01cadc6c..a8f206f3 100644 --- a/presence/implementation/credential_manager.h +++ b/presence/implementation/credential_manager.h @@ -70,7 +70,8 @@ class CredentialManager { const CredentialSelector& credential_selector, GetLocalCredentialsResultCallback callback) = 0; - // Used to fetch remote public creds when scanning. + // Used to fetch local/remote public creds based on the value + // of public_credential_type. virtual void GetPublicCredentials( const CredentialSelector& credential_selector, PublicCredentialType public_credential_type, diff --git a/presence/implementation/credential_manager_impl.h b/presence/implementation/credential_manager_impl.h index 00779132..fe644294 100644 --- a/presence/implementation/credential_manager_impl.h +++ b/presence/implementation/credential_manager_impl.h @@ -88,7 +88,8 @@ class CredentialManagerImpl : public CredentialManager { GetLocalCredentialsSync(const CredentialSelector& credential_selector, absl::Duration timeout); - // Used to fetch remote public creds when scanning. + // Used to fetch local/remote public creds based on the value of + // public_credential_type. void GetPublicCredentials( const CredentialSelector& credential_selector, PublicCredentialType public_credential_type, diff --git a/presence/implementation/service_controller.h b/presence/implementation/service_controller.h index ab041728..bd27c003 100644 --- a/presence/implementation/service_controller.h +++ b/presence/implementation/service_controller.h @@ -49,6 +49,14 @@ class ServiceController { int credential_life_cycle_days, int contiguous_copy_of_credentials, GenerateCredentialsResultCallback credentials_generated_cb) = 0; virtual ::nearby::internal::Metadata GetLocalDeviceMetadata() = 0; + virtual void GetLocalPublicCredentials( + const CredentialSelector& credential_selector, + GetPublicCredentialsResultCallback callback) = 0; + virtual void UpdateRemotePublicCredentials( + absl::string_view manager_app_id, absl::string_view account_name, + const std::vector& + remote_public_creds, + UpdateRemotePublicCredentialsCallback credentials_updated_cb) = 0; }; } // namespace presence diff --git a/presence/implementation/service_controller_impl.cc b/presence/implementation/service_controller_impl.cc index efeed31d..60efdc31 100644 --- a/presence/implementation/service_controller_impl.cc +++ b/presence/implementation/service_controller_impl.cc @@ -53,5 +53,22 @@ void ServiceControllerImpl::UpdateLocalDeviceMetadata( std::move(credentials_generated_cb)); } +void ServiceControllerImpl::GetLocalPublicCredentials( + const CredentialSelector& credential_selector, + GetPublicCredentialsResultCallback callback) { + credential_manager_.GetPublicCredentials( + credential_selector, PublicCredentialType::kLocalPublicCredential, + std::move(callback)); +} + +void ServiceControllerImpl::UpdateRemotePublicCredentials( + absl::string_view manager_app_id, absl::string_view account_name, + const std::vector& remote_public_creds, + UpdateRemotePublicCredentialsCallback credentials_updated_cb) { + credential_manager_.UpdateRemotePublicCredentials( + manager_app_id, account_name, remote_public_creds, + std::move(credentials_updated_cb)); +} + } // namespace presence } // namespace nearby diff --git a/presence/implementation/service_controller_impl.h b/presence/implementation/service_controller_impl.h index 6233b772..3590d34d 100644 --- a/presence/implementation/service_controller_impl.h +++ b/presence/implementation/service_controller_impl.h @@ -57,6 +57,14 @@ class ServiceControllerImpl : public ServiceController { ::nearby::internal::Metadata GetLocalDeviceMetadata() override { return credential_manager_.GetLocalDeviceMetadata(); } + void GetLocalPublicCredentials( + const CredentialSelector& credential_selector, + GetPublicCredentialsResultCallback callback) override; + void UpdateRemotePublicCredentials( + absl::string_view manager_app_id, absl::string_view account_name, + const std::vector& + remote_public_creds, + UpdateRemotePublicCredentialsCallback credentials_updated_cb) override; SingleThreadExecutor& GetBackgroundExecutor() { return executor_; } diff --git a/presence/presence_service.cc b/presence/presence_service.cc index 5fee1268..f4a1868c 100644 --- a/presence/presence_service.cc +++ b/presence/presence_service.cc @@ -16,6 +16,7 @@ #include #include +#include #include "internal/platform/borrowable.h" #include "presence/data_types.h" @@ -52,5 +53,21 @@ void PresenceService::StopBroadcast(BroadcastSessionId session) { service_controller_->StopBroadcast(session); } +void PresenceService::GetLocalPublicCredentials( + const CredentialSelector& credential_selector, + GetPublicCredentialsResultCallback callback) { + service_controller_->GetLocalPublicCredentials(credential_selector, + std::move(callback)); +} + +void PresenceService::UpdateRemotePublicCredentials( + absl::string_view manager_app_id, absl::string_view account_name, + const std::vector& remote_public_creds, + UpdateRemotePublicCredentialsCallback credentials_updated_cb) { + service_controller_->UpdateRemotePublicCredentials( + manager_app_id, account_name, remote_public_creds, + std::move(credentials_updated_cb)); +} + } // namespace presence } // namespace nearby diff --git a/presence/presence_service.h b/presence/presence_service.h index 623e6169..be221ab5 100644 --- a/presence/presence_service.h +++ b/presence/presence_service.h @@ -65,6 +65,15 @@ class PresenceService { PresenceDeviceProvider* GetLocalDeviceProvider() { return provider_.get(); } + void GetLocalPublicCredentials(const CredentialSelector& credential_selector, + GetPublicCredentialsResultCallback callback); + + void UpdateRemotePublicCredentials( + absl::string_view manager_app_id, absl::string_view account_name, + const std::vector& + remote_public_creds, + UpdateRemotePublicCredentialsCallback credentials_updated_cb); + // Testing only. ::nearby::internal::Metadata GetLocalDeviceMetadata() { return service_controller_->GetLocalDeviceMetadata(); diff --git a/presence/presence_service_test.cc b/presence/presence_service_test.cc index 564754d1..2c33aac3 100644 --- a/presence/presence_service_test.cc +++ b/presence/presence_service_test.cc @@ -14,9 +14,14 @@ #include "presence/presence_service.h" +#include +#include +#include + #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" +#include "internal/platform/count_down_latch.h" #include "internal/platform/medium_environment.h" #include "presence/presence_client.h" @@ -26,6 +31,9 @@ namespace { using Metadata = ::nearby::internal::Metadata; +constexpr absl::string_view kManagerAppId = "TEST_MANAGER_APP"; +constexpr absl::string_view kAccountName = "test account"; + class PresenceServiceTest : public testing::Test { protected: nearby::MediumEnvironment& env_{nearby::MediumEnvironment::Instance()}; @@ -40,6 +48,14 @@ Metadata CreateTestMetadata(absl::string_view account_name) { return metadata; } +CredentialSelector BuildDefaultCredentialSelector() { + CredentialSelector credential_selector; + credential_selector.manager_app_id = std::string(kManagerAppId); + credential_selector.account_name = std::string(kAccountName); + credential_selector.identity_type = internal::IDENTITY_TYPE_PRIVATE; + return credential_selector; +} + TEST_F(PresenceServiceTest, DefaultConstructorWorks) { PresenceService presence_service; } @@ -83,6 +99,49 @@ TEST_F(PresenceServiceTest, TestGetDeviceProvider) { EXPECT_NE(presence_service.GetLocalDeviceProvider(), nullptr); } +TEST_F(PresenceServiceTest, TestGetPublicCredentials) { + PresenceService presence_service; + CredentialSelector selector = BuildDefaultCredentialSelector(); + absl::Status status; + nearby::CountDownLatch fetched_latch(1); + presence_service.GetLocalPublicCredentials( + selector, + {.credentials_fetched_cb = + [&status, &fetched_latch]( + absl::StatusOr> + result) { + status = result.status(); + fetched_latch.CountDown(); + }}); + EXPECT_TRUE(fetched_latch.Await().Ok()); + EXPECT_THAT(status, testing::status::StatusIs(absl::StatusCode::kNotFound)); +} + +TEST_F(PresenceServiceTest, TestUpdateRemotePublicCredentials) { + PresenceService presence_service; + internal::SharedCredential public_credential_for_test; + public_credential_for_test.set_identity_type( + internal::IdentityType::IDENTITY_TYPE_TRUSTED); + std::vector public_credentials{ + {public_credential_for_test}}; + + nearby::CountDownLatch updated_latch(1); + UpdateRemotePublicCredentialsCallback update_credentials_cb{ + .credentials_updated_cb = + [&updated_latch](absl::Status status) { + if (status.ok()) { + updated_latch.CountDown(); + } + }, + }; + + presence_service.UpdateRemotePublicCredentials( + kManagerAppId, kAccountName, public_credentials, + std::move(update_credentials_cb)); + + EXPECT_TRUE(updated_latch.Await().Ok()); +} + } // namespace } // namespace presence } // namespace nearby