Plumb CredentialManager::UpdateRemotePublicCredentials and CredentialManager::GetPublicCredentials through to PresenceService

PiperOrigin-RevId: 534136547
This commit is contained in:
Anay Wadhera
2023-05-22 11:52:06 -07:00
committed by Copybara-Service
parent 9ee02c43ad
commit 1b7142da77
8 changed files with 122 additions and 2 deletions
+2 -1
View File
@@ -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,
@@ -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,
@@ -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<nearby::internal::SharedCredential>&
remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb) = 0;
};
} // namespace presence
@@ -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<nearby::internal::SharedCredential>& 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
@@ -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<nearby::internal::SharedCredential>&
remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb) override;
SingleThreadExecutor& GetBackgroundExecutor() { return executor_; }
+17
View File
@@ -16,6 +16,7 @@
#include <memory>
#include <utility>
#include <vector>
#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<nearby::internal::SharedCredential>& 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
+9
View File
@@ -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<nearby::internal::SharedCredential>&
remote_public_creds,
UpdateRemotePublicCredentialsCallback credentials_updated_cb);
// Testing only.
::nearby::internal::Metadata GetLocalDeviceMetadata() {
return service_controller_->GetLocalDeviceMetadata();
+59
View File
@@ -14,9 +14,14 @@
#include "presence/presence_service.h"
#include <string>
#include <utility>
#include <vector>
#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<std::vector<nearby::internal::SharedCredential>>
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<internal::SharedCredential> 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